ata_piix: Add new short cable ID
[linux-2.6] / drivers / power / ds2760_battery.c
1 /*
2  * Driver for batteries with DS2760 chips inside.
3  *
4  * Copyright © 2007 Anton Vorontsov
5  *             2004-2007 Matt Reimer
6  *             2004 Szabolcs Gyurko
7  *
8  * Use consistent with the GNU GPL is permitted,
9  * provided that this copyright notice is
10  * preserved in its entirety in all copies and derived works.
11  *
12  * Author:  Anton Vorontsov <cbou@mail.ru>
13  *          February 2007
14  *
15  *          Matt Reimer <mreimer@vpop.net>
16  *          April 2004, 2005, 2007
17  *
18  *          Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
19  *          September 2004
20  */
21
22 #include <linux/module.h>
23 #include <linux/param.h>
24 #include <linux/jiffies.h>
25 #include <linux/workqueue.h>
26 #include <linux/pm.h>
27 #include <linux/platform_device.h>
28 #include <linux/power_supply.h>
29
30 #include "../w1/w1.h"
31 #include "../w1/slaves/w1_ds2760.h"
32
33 struct ds2760_device_info {
34         struct device *dev;
35
36         /* DS2760 data, valid after calling ds2760_battery_read_status() */
37         unsigned long update_time;      /* jiffies when data read */
38         char raw[DS2760_DATA_SIZE];     /* raw DS2760 data */
39         int voltage_raw;                /* units of 4.88 mV */
40         int voltage_uV;                 /* units of µV */
41         int current_raw;                /* units of 0.625 mA */
42         int current_uA;                 /* units of µA */
43         int accum_current_raw;          /* units of 0.25 mAh */
44         int accum_current_uAh;          /* units of µAh */
45         int temp_raw;                   /* units of 0.125 °C */
46         int temp_C;                     /* units of 0.1 °C */
47         int rated_capacity;             /* units of µAh */
48         int rem_capacity;               /* percentage */
49         int full_active_uAh;            /* units of µAh */
50         int empty_uAh;                  /* units of µAh */
51         int life_sec;                   /* units of seconds */
52         int charge_status;              /* POWER_SUPPLY_STATUS_* */
53
54         int full_counter;
55         struct power_supply bat;
56         struct device *w1_dev;
57         struct workqueue_struct *monitor_wqueue;
58         struct delayed_work monitor_work;
59 };
60
61 static unsigned int cache_time = 1000;
62 module_param(cache_time, uint, 0644);
63 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
64
65 static unsigned int pmod_enabled;
66 module_param(pmod_enabled, bool, 0644);
67 MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
68
69 /* Some batteries have their rated capacity stored a N * 10 mAh, while
70  * others use an index into this table. */
71 static int rated_capacities[] = {
72         0,
73         920,    /* Samsung */
74         920,    /* BYD */
75         920,    /* Lishen */
76         920,    /* NEC */
77         1440,   /* Samsung */
78         1440,   /* BYD */
79         1440,   /* Lishen */
80         1440,   /* NEC */
81         2880,   /* Samsung */
82         2880,   /* BYD */
83         2880,   /* Lishen */
84         2880    /* NEC */
85 };
86
87 /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
88  * temp is in Celsius */
89 static int battery_interpolate(int array[], int temp)
90 {
91         int index, dt;
92
93         if (temp <= 0)
94                 return array[0];
95         if (temp >= 40)
96                 return array[4];
97
98         index = temp / 10;
99         dt    = temp % 10;
100
101         return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
102 }
103
104 static int ds2760_battery_read_status(struct ds2760_device_info *di)
105 {
106         int ret, i, start, count, scale[5];
107
108         if (di->update_time && time_before(jiffies, di->update_time +
109                                            msecs_to_jiffies(cache_time)))
110                 return 0;
111
112         /* The first time we read the entire contents of SRAM/EEPROM,
113          * but after that we just read the interesting bits that change. */
114         if (di->update_time == 0) {
115                 start = 0;
116                 count = DS2760_DATA_SIZE;
117         } else {
118                 start = DS2760_VOLTAGE_MSB;
119                 count = DS2760_TEMP_LSB - start + 1;
120         }
121
122         ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
123         if (ret != count) {
124                 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
125                          di->w1_dev);
126                 return 1;
127         }
128
129         di->update_time = jiffies;
130
131         /* DS2760 reports voltage in units of 4.88mV, but the battery class
132          * reports in units of uV, so convert by multiplying by 4880. */
133         di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
134                           (di->raw[DS2760_VOLTAGE_LSB] >> 5);
135         di->voltage_uV = di->voltage_raw * 4880;
136
137         /* DS2760 reports current in signed units of 0.625mA, but the battery
138          * class reports in units of µA, so convert by multiplying by 625. */
139         di->current_raw =
140             (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
141                           (di->raw[DS2760_CURRENT_LSB] >> 3);
142         di->current_uA = di->current_raw * 625;
143
144         /* DS2760 reports accumulated current in signed units of 0.25mAh. */
145         di->accum_current_raw =
146             (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
147                            di->raw[DS2760_CURRENT_ACCUM_LSB];
148         di->accum_current_uAh = di->accum_current_raw * 250;
149
150         /* DS2760 reports temperature in signed units of 0.125°C, but the
151          * battery class reports in units of 1/10 °C, so we convert by
152          * multiplying by .125 * 10 = 1.25. */
153         di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
154                                      (di->raw[DS2760_TEMP_LSB] >> 5);
155         di->temp_C = di->temp_raw + (di->temp_raw / 4);
156
157         /* At least some battery monitors (e.g. HP iPAQ) store the battery's
158          * maximum rated capacity. */
159         if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
160                 di->rated_capacity = rated_capacities[
161                         (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
162         else
163                 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
164
165         di->rated_capacity *= 1000; /* convert to µAh */
166
167         /* Calculate the full level at the present temperature. */
168         di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
169                               di->raw[DS2760_ACTIVE_FULL + 1];
170
171         scale[0] = di->raw[DS2760_ACTIVE_FULL] << 8 |
172                    di->raw[DS2760_ACTIVE_FULL + 1];
173         for (i = 1; i < 5; i++)
174                 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
175
176         di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
177         di->full_active_uAh *= 1000; /* convert to µAh */
178
179         /* Calculate the empty level at the present temperature. */
180         scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
181         for (i = 3; i >= 0; i--)
182                 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
183
184         di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
185         di->empty_uAh *= 1000; /* convert to µAh */
186
187         if (di->full_active_uAh == di->empty_uAh)
188                 di->rem_capacity = 0;
189         else
190                 /* From Maxim Application Note 131: remaining capacity =
191                  * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
192                 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
193                                     (di->full_active_uAh - di->empty_uAh);
194
195         if (di->rem_capacity < 0)
196                 di->rem_capacity = 0;
197         if (di->rem_capacity > 100)
198                 di->rem_capacity = 100;
199
200         if (di->current_uA)
201                 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
202                                  3600L) / di->current_uA;
203         else
204                 di->life_sec = 0;
205
206         return 0;
207 }
208
209 static void ds2760_battery_update_status(struct ds2760_device_info *di)
210 {
211         int old_charge_status = di->charge_status;
212
213         ds2760_battery_read_status(di);
214
215         if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
216                 di->full_counter = 0;
217
218         if (power_supply_am_i_supplied(&di->bat)) {
219                 if (di->current_uA > 10000) {
220                         di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
221                         di->full_counter = 0;
222                 } else if (di->current_uA < -5000) {
223                         if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
224                                 dev_notice(di->dev, "not enough power to "
225                                            "charge\n");
226                         di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
227                         di->full_counter = 0;
228                 } else if (di->current_uA < 10000 &&
229                             di->charge_status != POWER_SUPPLY_STATUS_FULL) {
230
231                         /* Don't consider the battery to be full unless
232                          * we've seen the current < 10 mA at least two
233                          * consecutive times. */
234
235                         di->full_counter++;
236
237                         if (di->full_counter < 2) {
238                                 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
239                         } else {
240                                 unsigned char acr[2];
241                                 int acr_val;
242
243                                 /* acr is in units of 0.25 mAh */
244                                 acr_val = di->full_active_uAh * 4L / 1000;
245
246                                 acr[0] = acr_val >> 8;
247                                 acr[1] = acr_val & 0xff;
248
249                                 if (w1_ds2760_write(di->w1_dev, acr,
250                                     DS2760_CURRENT_ACCUM_MSB, 2) < 2)
251                                         dev_warn(di->dev,
252                                                  "ACR reset failed\n");
253
254                                 di->charge_status = POWER_SUPPLY_STATUS_FULL;
255                         }
256                 }
257         } else {
258                 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
259                 di->full_counter = 0;
260         }
261
262         if (di->charge_status != old_charge_status)
263                 power_supply_changed(&di->bat);
264 }
265
266 static void ds2760_battery_write_status(struct ds2760_device_info *di,
267                                         char status)
268 {
269         if (status == di->raw[DS2760_STATUS_REG])
270                 return;
271
272         w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1);
273         w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
274         w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
275 }
276
277 static void ds2760_battery_work(struct work_struct *work)
278 {
279         struct ds2760_device_info *di = container_of(work,
280                 struct ds2760_device_info, monitor_work.work);
281         const int interval = HZ * 60;
282
283         dev_dbg(di->dev, "%s\n", __func__);
284
285         ds2760_battery_update_status(di);
286         queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
287 }
288
289 #define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
290                                               bat);
291
292 static void ds2760_battery_external_power_changed(struct power_supply *psy)
293 {
294         struct ds2760_device_info *di = to_ds2760_device_info(psy);
295
296         dev_dbg(di->dev, "%s\n", __func__);
297
298         cancel_delayed_work(&di->monitor_work);
299         queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
300 }
301
302 static int ds2760_battery_get_property(struct power_supply *psy,
303                                        enum power_supply_property psp,
304                                        union power_supply_propval *val)
305 {
306         struct ds2760_device_info *di = to_ds2760_device_info(psy);
307
308         switch (psp) {
309         case POWER_SUPPLY_PROP_STATUS:
310                 val->intval = di->charge_status;
311                 return 0;
312         default:
313                 break;
314         }
315
316         ds2760_battery_read_status(di);
317
318         switch (psp) {
319         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
320                 val->intval = di->voltage_uV;
321                 break;
322         case POWER_SUPPLY_PROP_CURRENT_NOW:
323                 val->intval = di->current_uA;
324                 break;
325         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
326                 val->intval = di->rated_capacity;
327                 break;
328         case POWER_SUPPLY_PROP_CHARGE_FULL:
329                 val->intval = di->full_active_uAh;
330                 break;
331         case POWER_SUPPLY_PROP_CHARGE_EMPTY:
332                 val->intval = di->empty_uAh;
333                 break;
334         case POWER_SUPPLY_PROP_CHARGE_NOW:
335                 val->intval = di->accum_current_uAh;
336                 break;
337         case POWER_SUPPLY_PROP_TEMP:
338                 val->intval = di->temp_C;
339                 break;
340         default:
341                 return -EINVAL;
342         }
343
344         return 0;
345 }
346
347 static enum power_supply_property ds2760_battery_props[] = {
348         POWER_SUPPLY_PROP_STATUS,
349         POWER_SUPPLY_PROP_VOLTAGE_NOW,
350         POWER_SUPPLY_PROP_CURRENT_NOW,
351         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
352         POWER_SUPPLY_PROP_CHARGE_FULL,
353         POWER_SUPPLY_PROP_CHARGE_EMPTY,
354         POWER_SUPPLY_PROP_CHARGE_NOW,
355         POWER_SUPPLY_PROP_TEMP,
356 };
357
358 static int ds2760_battery_probe(struct platform_device *pdev)
359 {
360         char status;
361         int retval = 0;
362         struct ds2760_device_info *di;
363
364         di = kzalloc(sizeof(*di), GFP_KERNEL);
365         if (!di) {
366                 retval = -ENOMEM;
367                 goto di_alloc_failed;
368         }
369
370         platform_set_drvdata(pdev, di);
371
372         di->dev                 = &pdev->dev;
373         di->w1_dev              = pdev->dev.parent;
374         di->bat.name            = dev_name(&pdev->dev);
375         di->bat.type            = POWER_SUPPLY_TYPE_BATTERY;
376         di->bat.properties      = ds2760_battery_props;
377         di->bat.num_properties  = ARRAY_SIZE(ds2760_battery_props);
378         di->bat.get_property    = ds2760_battery_get_property;
379         di->bat.external_power_changed =
380                                   ds2760_battery_external_power_changed;
381
382         di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
383
384         retval = power_supply_register(&pdev->dev, &di->bat);
385         if (retval) {
386                 dev_err(di->dev, "failed to register battery\n");
387                 goto batt_failed;
388         }
389
390         /* enable sleep mode feature */
391         ds2760_battery_read_status(di);
392         status = di->raw[DS2760_STATUS_REG];
393         if (pmod_enabled)
394                 status |= DS2760_STATUS_PMOD;
395         else
396                 status &= ~DS2760_STATUS_PMOD;
397
398         ds2760_battery_write_status(di, status);
399
400         INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
401         di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
402         if (!di->monitor_wqueue) {
403                 retval = -ESRCH;
404                 goto workqueue_failed;
405         }
406         queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
407
408         goto success;
409
410 workqueue_failed:
411         power_supply_unregister(&di->bat);
412 batt_failed:
413         kfree(di);
414 di_alloc_failed:
415 success:
416         return retval;
417 }
418
419 static int ds2760_battery_remove(struct platform_device *pdev)
420 {
421         struct ds2760_device_info *di = platform_get_drvdata(pdev);
422
423         cancel_rearming_delayed_workqueue(di->monitor_wqueue,
424                                           &di->monitor_work);
425         destroy_workqueue(di->monitor_wqueue);
426         power_supply_unregister(&di->bat);
427
428         return 0;
429 }
430
431 #ifdef CONFIG_PM
432
433 static int ds2760_battery_suspend(struct platform_device *pdev,
434                                   pm_message_t state)
435 {
436         struct ds2760_device_info *di = platform_get_drvdata(pdev);
437
438         di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
439
440         return 0;
441 }
442
443 static int ds2760_battery_resume(struct platform_device *pdev)
444 {
445         struct ds2760_device_info *di = platform_get_drvdata(pdev);
446
447         di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
448         power_supply_changed(&di->bat);
449
450         cancel_delayed_work(&di->monitor_work);
451         queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
452
453         return 0;
454 }
455
456 #else
457
458 #define ds2760_battery_suspend NULL
459 #define ds2760_battery_resume NULL
460
461 #endif /* CONFIG_PM */
462
463 MODULE_ALIAS("platform:ds2760-battery");
464
465 static struct platform_driver ds2760_battery_driver = {
466         .driver = {
467                 .name = "ds2760-battery",
468         },
469         .probe    = ds2760_battery_probe,
470         .remove   = ds2760_battery_remove,
471         .suspend  = ds2760_battery_suspend,
472         .resume   = ds2760_battery_resume,
473 };
474
475 static int __init ds2760_battery_init(void)
476 {
477         return platform_driver_register(&ds2760_battery_driver);
478 }
479
480 static void __exit ds2760_battery_exit(void)
481 {
482         platform_driver_unregister(&ds2760_battery_driver);
483 }
484
485 module_init(ds2760_battery_init);
486 module_exit(ds2760_battery_exit);
487
488 MODULE_LICENSE("GPL");
489 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
490               "Matt Reimer <mreimer@vpop.net>, "
491               "Anton Vorontsov <cbou@mail.ru>");
492 MODULE_DESCRIPTION("ds2760 battery driver");