Merge branch 'om-gta02-2.6.37' into om-2.6.37
[kernel.git] / drivers / power / bq27x00_battery.c
1 /*
2  * BQ27x00 battery driver
3  *
4  * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5  * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6  * Copyright (C) 2010 Lars-Peter Clausen <lars@metafoo.de>
7  *
8  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
9  *
10  * This package is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  *
18  */
19 #include <linux/module.h>
20 #include <linux/param.h>
21 #include <linux/jiffies.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/power_supply.h>
26 #include <linux/idr.h>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
29 #include <asm/unaligned.h>
30
31 #include <linux/power/bq27x00_battery.h>
32
33 #define DRIVER_VERSION                  "1.1.0"
34
35 #define BQ27x00_REG_TEMP                0x06
36 #define BQ27x00_REG_VOLT                0x08
37 #define BQ27x00_REG_AI                  0x14
38 #define BQ27x00_REG_FLAGS               0x0A
39 #define BQ27x00_REG_LMD                 0x12
40 #define BQ27x00_REG_TTE                 0x16
41 #define BQ27x00_REG_TTF                 0x18
42 #define BQ27x00_REG_TTECP               0x26
43
44 #define BQ27000_REG_RSOC                0x0B /* Relative State-of-Charge */
45 #define BQ27000_FLAG_VDQ                BIT(3)
46 #define BQ27000_FLAG_CHGS               BIT(7)
47
48 #define BQ27500_REG_SOC                 0x2c
49 #define BQ27500_FLAG_DSC                BIT(0)
50 #define BQ27500_FLAG_FC                 BIT(9)
51
52 struct bq27x00_device_info;
53 struct bq27x00_access_methods {
54         int (*read)(u8 reg, bool single, struct bq27x00_device_info *di);
55 };
56
57 enum bq27x00_chip { BQ27000, BQ27500 };
58
59 struct bq27x00_values {
60         int temperature;
61         int voltage;
62         int current_now;
63         int time_to_empty;
64         int time_to_empty_avg;
65         int time_to_full;
66         int charge_full;
67         int capacity;
68         int flags;
69 };
70
71 struct bq27x00_device_info {
72         struct device           *dev;
73         int                     id;
74         enum bq27x00_chip       chip;
75
76         struct bq27x00_access_methods   bus;
77
78         struct power_supply     bat;
79
80         struct bq27x00_values values;
81
82         unsigned long last_update;
83         struct delayed_work work;
84 };
85
86 static enum power_supply_property bq27x00_battery_props[] = {
87         POWER_SUPPLY_PROP_STATUS,
88         POWER_SUPPLY_PROP_PRESENT,
89         POWER_SUPPLY_PROP_VOLTAGE_NOW,
90         POWER_SUPPLY_PROP_CURRENT_NOW,
91         POWER_SUPPLY_PROP_CAPACITY,
92         POWER_SUPPLY_PROP_TEMP,
93         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
94         POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
95         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
96         POWER_SUPPLY_PROP_TECHNOLOGY,
97         POWER_SUPPLY_PROP_CHARGE_FULL,
98         POWER_SUPPLY_PROP_HEALTH,
99 };
100
101 /*
102  * Common code for BQ27x00 devices
103  */
104
105 static int bq27x00_read(u8 reg, bool single, struct bq27x00_device_info *di)
106 {
107         return di->bus.read(reg, single, di);
108 }
109
110 /*
111  * Return the battery Voltage in milivolts
112  * Or < 0 if something fails.
113  */
114 static int bq27x00_battery_read_voltage(struct bq27x00_device_info *di)
115 {
116         int volt;
117
118         volt = bq27x00_read(BQ27x00_REG_VOLT, 0, di);
119         if (volt < 0)
120                 return volt;
121
122         return volt * 1000;
123 }
124
125 /*
126  * Return the battery Relative State-of-Charge
127  * Or < 0 if something fails.
128  */
129 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
130 {
131         int rsoc;
132
133         if (di->chip == BQ27500)
134                 rsoc = bq27x00_read(BQ27500_REG_SOC, 0, di);
135         else
136                 rsoc = bq27x00_read(BQ27000_REG_RSOC, 1, di);
137         if (rsoc < 0) {
138                 dev_err(di->dev, "error reading relative State-of-Charge\n");
139         }
140
141         return rsoc;
142 }
143
144 /*
145  * Read a time register.
146  * Return < 0 if something fails.
147  */
148 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, int reg)
149 {
150         int tval;
151
152         tval = bq27x00_read(reg, 0, di);
153         if (tval < 0) {
154                 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
155                 return tval;
156         }
157
158         if (tval == 65535)
159                 return -ENODATA;
160
161         return tval * 60;
162 }
163
164 static int bq27x00_battery_read_charge_full(struct bq27x00_device_info *di)
165 {
166         int charge;
167
168         charge = bq27x00_read(BQ27x00_REG_LMD, 0, di);
169         if (charge < 0) {
170                 dev_err(di->dev, "error reading register charge full: %d\n", charge);
171                 return charge;
172         }
173
174         if (di->chip == BQ27500)
175                 charge *= 1000;
176         else
177                 charge = charge * 357 / 2;
178
179         return charge;
180 }
181
182 static void bq27x00_update(struct bq27x00_device_info *di)
183 {
184         struct bq27x00_values values = {0, };
185
186         values.voltage = bq27x00_battery_read_voltage(di);
187         if (values.voltage >= 0) {
188                 values.flags = bq27x00_read(BQ27x00_REG_FLAGS, di->chip == BQ27500, di);
189                 values.current_now = bq27x00_read(BQ27x00_REG_AI, 0, di);
190                 values.capacity = bq27x00_battery_read_rsoc(di);
191                 values.temperature = bq27x00_read(BQ27x00_REG_TEMP, 0, di);
192                 values.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
193                 values.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
194                 values.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
195                 values.charge_full = bq27x00_battery_read_charge_full(di);
196         }
197
198         if (memcmp(&di->values, &values, sizeof(values)) != 0) {
199                 di->values = values;
200                 power_supply_changed(&di->bat);
201         }
202
203         di->last_update = jiffies;
204 }
205
206 /*
207  * Return the battery temperature in tenths of degree Celsius
208  * Or < 0 if something fails.
209  */
210 static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
211         union power_supply_propval *val)
212 {
213         if (di->values.temperature < 0)
214                 return di->values.temperature;
215
216         if (di->chip == BQ27500)
217                 val->intval = di->values.temperature - 2731;
218         else
219                 val->intval = ((di->values.temperature * 5) - 5463) >> 1;
220
221         return 0;
222 }
223
224 /*
225  * Return the battery average current
226  * Note that current can be negative signed as well
227  * Or 0 if something fails.
228  */
229 static int bq27x00_battery_current(struct bq27x00_device_info *di,
230         union power_supply_propval *val)
231 {
232         int curr = 0;
233
234         if (di->values.current_now < 0)
235                 return di->values.current_now;
236
237         if (di->chip == BQ27500) {
238                 /* bq27500 returns signed value */
239                 val->intval = (int)((s16)di->values.current_now) * 1000;
240         } else {
241                 if (di->values.flags < 0)
242                         return di->values.flags;
243
244                 curr = di->values.current_now;
245                 if (di->values.flags & BQ27000_FLAG_CHGS) {
246                         dev_dbg(di->dev, "negative current!\n");
247                         curr = -curr;
248                 }
249
250                 val->intval = curr * 3570 / 20;
251         }
252
253         return 0;
254 }
255
256 static int bq27x00_battery_status(struct bq27x00_device_info *di,
257         union power_supply_propval *val)
258 {
259         int status;
260
261         if (di->values.flags < 0)
262                 return di->values.flags;
263
264         if (di->chip == BQ27500) {
265                 if (di->values.flags & BQ27500_FLAG_FC)
266                         status = POWER_SUPPLY_STATUS_FULL;
267                 else if (di->values.flags & BQ27500_FLAG_DSC)
268                         status = POWER_SUPPLY_STATUS_DISCHARGING;
269                 else
270                         status = POWER_SUPPLY_STATUS_CHARGING;
271         } else {
272                 if (di->values.flags & BQ27000_FLAG_CHGS)
273                         status = POWER_SUPPLY_STATUS_CHARGING;
274                 else if (di->values.time_to_full == 0)
275                         status = POWER_SUPPLY_STATUS_FULL;
276                 else if (power_supply_am_i_supplied(&di->bat))
277                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
278                 else
279                         status = POWER_SUPPLY_STATUS_DISCHARGING;
280         }
281
282         val->intval = status;
283
284         return 0;
285 }
286
287 static int bq27x00_simple_value(int value,
288         union power_supply_propval *val)
289 {
290         if (value < 0)
291                 return value;
292
293         val->intval = value;
294
295         return 0;
296 }
297
298 static int bq27x00_battery_health(struct bq27x00_device_info *di,
299         union power_supply_propval *val)
300 {
301         if (di->values.flags < 0)
302                 return di->values.flags;
303
304         if (di->values.flags & BQ27000_FLAG_VDQ)
305                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
306         else
307                 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
308
309         return 0;
310 }
311
312 #define to_bq27x00_device_info(x) container_of((x), \
313                                 struct bq27x00_device_info, bat);
314
315 static int bq27x00_battery_get_property(struct power_supply *psy,
316                                         enum power_supply_property psp,
317                                         union power_supply_propval *val)
318 {
319         int ret = 0;
320         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
321
322         if (time_is_before_jiffies(di->last_update + 6 * HZ)) {
323                 cancel_delayed_work(&di->work);
324                 bq27x00_update(di);
325                 schedule_delayed_work(&di->work, 60 * HZ);
326         }
327
328         if (psp != POWER_SUPPLY_PROP_PRESENT && di->values.voltage <= 0)
329                 return -ENODEV;
330
331         switch (psp) {
332         case POWER_SUPPLY_PROP_STATUS:
333                 ret = bq27x00_battery_status(di, val);
334                 break;
335         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
336                 ret = bq27x00_simple_value(di->values.voltage, val);
337                 break;
338         case POWER_SUPPLY_PROP_PRESENT:
339                 val->intval = di->values.voltage <= 0 ? 0 : 1;
340                 break;
341         case POWER_SUPPLY_PROP_CURRENT_NOW:
342                 ret = bq27x00_battery_current(di, val);
343                 break;
344         case POWER_SUPPLY_PROP_CAPACITY:
345                 ret = bq27x00_simple_value(di->values.capacity, val);
346                 break;
347         case POWER_SUPPLY_PROP_TEMP:
348                 ret = bq27x00_battery_temperature(di, val);
349                 break;
350         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
351                 ret = bq27x00_simple_value(di->values.time_to_empty, val);
352                 break;
353         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
354                 ret = bq27x00_simple_value(di->values.time_to_empty_avg, val);
355                 break;
356         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
357                 ret = bq27x00_simple_value(di->values.time_to_full, val);
358                 break;
359         case POWER_SUPPLY_PROP_TECHNOLOGY:
360                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
361                 break;
362         case POWER_SUPPLY_PROP_CHARGE_FULL:
363                 ret = bq27x00_simple_value(di->values.charge_full, val);
364                 break;
365         case POWER_SUPPLY_PROP_HEALTH:
366                 ret = bq27x00_battery_health(di, val);
367                 break;
368         default:
369                 return -EINVAL;
370         }
371
372         return ret;
373 }
374
375 static void bq27x00_battery_work(struct work_struct *work)
376 {
377         struct bq27x00_device_info *di =
378                 container_of(work, struct bq27x00_device_info, work.work);
379
380         bq27x00_update(di);
381         schedule_delayed_work(&di->work, 60 * HZ);
382 }
383
384 static void bq27x00_external_power_changed(struct power_supply *psy)
385 {
386         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
387
388         schedule_delayed_work(&di->work, 0);
389 }
390
391 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
392 {
393         int ret;
394
395         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
396         di->bat.properties = bq27x00_battery_props;
397         di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
398         di->bat.get_property = bq27x00_battery_get_property;
399         di->bat.external_power_changed = bq27x00_external_power_changed;
400
401         INIT_DELAYED_WORK(&di->work, bq27x00_battery_work);
402
403         ret = power_supply_register(di->dev, &di->bat);
404         if (ret) {
405                 dev_err(di->dev, "failed to register battery: %d\n", ret);
406                 return ret;
407         }
408
409         dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
410
411         bq27x00_update(di);
412
413         return 0;
414 }
415
416
417 /* i2c specific code */
418 #ifdef CONFIG_BATTERY_BQ27X00_I2C
419
420 /* If the system has several batteries we need a different name for each
421  * of them...
422  */
423 static DEFINE_IDR(battery_id);
424 static DEFINE_MUTEX(battery_mutex);
425
426 static int bq27x00_read_i2c(u8 reg, bool single, struct bq27x00_device_info *di)
427 {
428         struct i2c_client *client = to_i2c_client(di->dev);
429         struct i2c_msg msg[1];
430         unsigned char data[2];
431         int ret;
432
433         if (!client->adapter)
434                 return -ENODEV;
435
436         msg->addr = client->addr;
437         msg->flags = 0;
438         msg->len = 1;
439         msg->buf = data;
440
441         data[0] = reg;
442         ret = i2c_transfer(client->adapter, msg, 1);
443
444         if (ret >= 0) {
445                 if (!single)
446                         msg->len = 2;
447                 else
448                         msg->len = 1;
449
450                 msg->flags = I2C_M_RD;
451                 if (ret >= 0) {
452                         if (!single)
453                                 ret = get_unaligned_le16(data);
454                         else
455                                 ret = data[0];
456                 }
457         }
458         return ret;
459 }
460
461 static int bq27x00_battery_probe(struct i2c_client *client,
462                                  const struct i2c_device_id *id)
463 {
464         char *name;
465         struct bq27x00_device_info *di;
466         int num;
467         int retval = 0;
468
469         /* Get new ID for the new battery device */
470         retval = idr_pre_get(&battery_id, GFP_KERNEL);
471         if (retval == 0)
472                 return -ENOMEM;
473         mutex_lock(&battery_mutex);
474         retval = idr_get_new(&battery_id, client, &num);
475         mutex_unlock(&battery_mutex);
476         if (retval < 0)
477                 return retval;
478
479         name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
480         if (!name) {
481                 dev_err(&client->dev, "failed to allocate device name\n");
482                 retval = -ENOMEM;
483                 goto batt_failed_1;
484         }
485
486         di = kzalloc(sizeof(*di), GFP_KERNEL);
487         if (!di) {
488                 dev_err(&client->dev, "failed to allocate device info data\n");
489                 retval = -ENOMEM;
490                 goto batt_failed_2;
491         }
492
493         di->id = num;
494         di->chip = id->driver_data;
495
496         i2c_set_clientdata(client, di);
497
498         di->dev = &client->dev;
499         di->bat.name = name;
500
501         di->bus.read = &bq27x00_read_i2c;
502
503         if (bq27x00_powersupply_init(di))
504                 goto batt_failed_3;
505
506         return 0;
507
508 batt_failed_3:
509         kfree(di);
510 batt_failed_2:
511         kfree(name);
512 batt_failed_1:
513         mutex_lock(&battery_mutex);
514         idr_remove(&battery_id, num);
515         mutex_unlock(&battery_mutex);
516
517         return retval;
518 }
519
520 static int bq27x00_battery_remove(struct i2c_client *client)
521 {
522         struct bq27x00_device_info *di = i2c_get_clientdata(client);
523
524         power_supply_unregister(&di->bat);
525
526         kfree(di->bat.name);
527
528         mutex_lock(&battery_mutex);
529         idr_remove(&battery_id, di->id);
530         mutex_unlock(&battery_mutex);
531
532         kfree(di);
533
534         return 0;
535 }
536
537 static const struct i2c_device_id bq27x00_id[] = {
538         { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
539         { "bq27500", BQ27500 },
540         {},
541 };
542
543 static struct i2c_driver bq27x00_battery_driver = {
544         .driver = {
545                 .name = "bq27x00-battery",
546         },
547         .probe = bq27x00_battery_probe,
548         .remove = bq27x00_battery_remove,
549         .id_table = bq27x00_id,
550 };
551
552 static inline int bq27x00_battery_i2c_init(void)
553 {
554         int ret = i2c_add_driver(&bq27x00_battery_driver);
555         if (ret)
556                 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
557
558         return ret;
559 }
560
561 static inline void bq27x00_battery_i2c_exit(void)
562 {
563         i2c_del_driver(&bq27x00_battery_driver);
564 }
565
566 #else
567
568 static inline int bq27x00_battery_i2c_init(void) { return 0; }
569 static inline void bq27x00_battery_i2c_exit(void) {};
570
571 #endif
572
573 /* platform specific code */
574 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
575
576 static int bq27000_read_platform(u8 reg, bool single,
577                         struct bq27x00_device_info *di)
578 {
579         struct device *dev = di->dev;
580         struct bq27000_platform_data *pdata = dev->platform_data;
581         unsigned int timeout = 3;
582         int upper, lower;
583         int temp;
584
585         if (!single) {
586                 /* Make sure the value has not changed in between reading the lower and
587                  * the upper part */
588                 upper = pdata->read(dev, reg + 1);
589                 do {
590                         temp = upper;
591                         if (upper < 0)
592                                 return upper;
593
594                         lower = pdata->read(dev, reg);
595                         if (lower < 0)
596                                 return lower;
597
598                         upper = pdata->read(dev, reg + 1);
599                 } while(temp != upper && --timeout);
600
601                 if (timeout == 0)
602                         return -ETIMEDOUT;
603
604                 return (upper << 8) | lower;
605         }
606
607         return pdata->read(dev, reg);
608 }
609
610 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
611 {
612         struct bq27x00_device_info *di;
613         struct bq27000_platform_data *pdata = pdev->dev.platform_data;
614         int ret;
615
616         if (!pdata) {
617                 dev_err(&pdev->dev, "no platform_data supplied\n");
618                 return -EINVAL;
619         }
620
621         if (!pdata->read) {
622                 dev_err(&pdev->dev, "no hdq read callback supplied\n");
623                 return -EINVAL;
624         }
625
626         di = kzalloc(sizeof(*di), GFP_KERNEL);
627         if (!di) {
628                 dev_err(&pdev->dev, "failed to allocate device info data\n");
629                 return -ENOMEM;
630         }
631
632         platform_set_drvdata(pdev, di);
633
634         di->dev = &pdev->dev;
635         di->chip = BQ27000;
636
637         di->bat.name = pdata->name ?: dev_name(&pdev->dev);
638         di->bus.read = &bq27000_read_platform;
639
640         ret = bq27x00_powersupply_init(di);
641         if (ret)
642                 goto err_free;
643
644         return 0;
645
646 err_free:
647         platform_set_drvdata(pdev, NULL);
648         kfree(di);
649
650         return ret;
651 }
652
653 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
654 {
655         struct bq27x00_device_info *di = platform_get_drvdata(pdev);
656
657         power_supply_unregister(&di->bat);
658         platform_set_drvdata(pdev, NULL);
659         kfree(di);
660
661         return 0;
662 }
663
664 static struct platform_driver bq27000_battery_driver = {
665         .probe  = bq27000_battery_probe,
666         .remove = __devexit_p(bq27000_battery_remove),
667         .driver = {
668                 .name = "bq27000-battery",
669                 .owner = THIS_MODULE,
670         },
671 };
672
673 static inline int bq27x00_battery_platform_init(void)
674 {
675         int ret = platform_driver_register(&bq27000_battery_driver);
676         if (ret)
677                 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
678
679         return ret;
680 }
681
682 static inline void bq27x00_battery_platform_exit(void)
683 {
684         platform_driver_unregister(&bq27000_battery_driver);
685 }
686
687 #else
688
689 static inline int bq27x00_battery_platform_init(void) { return 0; }
690 static inline void bq27x00_battery_platform_exit(void) {};
691
692 #endif
693
694 /*
695  * Module stuff
696  */
697
698 static int __init bq27x00_battery_init(void)
699 {
700         int ret;
701
702         ret = bq27x00_battery_i2c_init();
703         if (ret)
704             return ret;
705
706         ret = bq27x00_battery_platform_init();
707         if (ret)
708             bq27x00_battery_i2c_exit();
709
710         return ret;
711 }
712 module_init(bq27x00_battery_init);
713
714 static void __exit bq27x00_battery_exit(void)
715 {
716         bq27x00_battery_platform_exit();
717         bq27x00_battery_i2c_exit();
718 }
719 module_exit(bq27x00_battery_exit);
720
721 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
722 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
723 MODULE_LICENSE("GPL");