]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/power/bq27x00_battery.c
bq27x00_battery: Add support for power average and health properties
[karo-tx-linux.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-2011 Lars-Peter Clausen <lars@metafoo.de>
7  * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
8  *
9  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
10  *
11  * This package is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  */
20
21 /*
22  * Datasheets:
23  * http://focus.ti.com/docs/prod/folders/print/bq27000.html
24  * http://focus.ti.com/docs/prod/folders/print/bq27500.html
25  */
26
27 #include <linux/module.h>
28 #include <linux/param.h>
29 #include <linux/jiffies.h>
30 #include <linux/workqueue.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/power_supply.h>
34 #include <linux/idr.h>
35 #include <linux/i2c.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
38
39 #include <linux/power/bq27x00_battery.h>
40
41 #define DRIVER_VERSION                  "1.2.0"
42
43 #define BQ27x00_REG_TEMP                0x06
44 #define BQ27x00_REG_VOLT                0x08
45 #define BQ27x00_REG_AI                  0x14
46 #define BQ27x00_REG_FLAGS               0x0A
47 #define BQ27x00_REG_TTE                 0x16
48 #define BQ27x00_REG_TTF                 0x18
49 #define BQ27x00_REG_TTECP               0x26
50 #define BQ27x00_REG_NAC                 0x0C /* Nominal available capacity */
51 #define BQ27x00_REG_LMD                 0x12 /* Last measured discharge */
52 #define BQ27x00_REG_CYCT                0x2A /* Cycle count total */
53 #define BQ27x00_REG_AE                  0x22 /* Available energy */
54 #define BQ27x00_POWER_AVG               0x24
55
56 #define BQ27000_REG_RSOC                0x0B /* Relative State-of-Charge */
57 #define BQ27000_REG_ILMD                0x76 /* Initial last measured discharge */
58 #define BQ27000_FLAG_EDVF               BIT(0) /* Final End-of-Discharge-Voltage flag */
59 #define BQ27000_FLAG_EDV1               BIT(1) /* First End-of-Discharge-Voltage flag */
60 #define BQ27000_FLAG_CI                 BIT(4) /* Capacity Inaccurate flag */
61 #define BQ27000_FLAG_FC                 BIT(5)
62 #define BQ27000_FLAG_CHGS               BIT(7) /* Charge state flag */
63
64 #define BQ27500_REG_SOC                 0x2C
65 #define BQ27500_REG_DCAP                0x3C /* Design capacity */
66 #define BQ27500_FLAG_DSC                BIT(0)
67 #define BQ27500_FLAG_SOCF               BIT(1) /* State-of-Charge threshold final */
68 #define BQ27500_FLAG_SOC1               BIT(2) /* State-of-Charge threshold 1 */
69 #define BQ27500_FLAG_FC                 BIT(9)
70 #define BQ27500_FLAG_OTC                BIT(15)
71
72 #define BQ27000_RS                      20 /* Resistor sense */
73 #define BQ27x00_POWER_CONSTANT          (256 * 29200 / 1000)
74
75 struct bq27x00_device_info;
76 struct bq27x00_access_methods {
77         int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
78 };
79
80 enum bq27x00_chip { BQ27000, BQ27500 };
81
82 struct bq27x00_reg_cache {
83         int temperature;
84         int time_to_empty;
85         int time_to_empty_avg;
86         int time_to_full;
87         int charge_full;
88         int cycle_count;
89         int capacity;
90         int energy;
91         int flags;
92         int power_avg;
93         int health;
94 };
95
96 struct bq27x00_device_info {
97         struct device           *dev;
98         int                     id;
99         enum bq27x00_chip       chip;
100
101         struct bq27x00_reg_cache cache;
102         int charge_design_full;
103
104         unsigned long last_update;
105         struct delayed_work work;
106
107         struct power_supply     bat;
108
109         struct bq27x00_access_methods bus;
110
111         struct mutex lock;
112 };
113
114 static enum power_supply_property bq27x00_battery_props[] = {
115         POWER_SUPPLY_PROP_STATUS,
116         POWER_SUPPLY_PROP_PRESENT,
117         POWER_SUPPLY_PROP_VOLTAGE_NOW,
118         POWER_SUPPLY_PROP_CURRENT_NOW,
119         POWER_SUPPLY_PROP_CAPACITY,
120         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
121         POWER_SUPPLY_PROP_TEMP,
122         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
123         POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
124         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
125         POWER_SUPPLY_PROP_TECHNOLOGY,
126         POWER_SUPPLY_PROP_CHARGE_FULL,
127         POWER_SUPPLY_PROP_CHARGE_NOW,
128         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
129         POWER_SUPPLY_PROP_CYCLE_COUNT,
130         POWER_SUPPLY_PROP_ENERGY_NOW,
131         POWER_SUPPLY_PROP_POWER_AVG,
132         POWER_SUPPLY_PROP_HEALTH,
133 };
134
135 static unsigned int poll_interval = 360;
136 module_param(poll_interval, uint, 0644);
137 MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
138                                 "0 disables polling");
139
140 /*
141  * Common code for BQ27x00 devices
142  */
143
144 static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
145                 bool single)
146 {
147         return di->bus.read(di, reg, single);
148 }
149
150 /*
151  * Return the battery Relative State-of-Charge
152  * Or < 0 if something fails.
153  */
154 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
155 {
156         int rsoc;
157
158         if (di->chip == BQ27500)
159                 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
160         else
161                 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
162
163         if (rsoc < 0)
164                 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
165
166         return rsoc;
167 }
168
169 /*
170  * Return a battery charge value in µAh
171  * Or < 0 if something fails.
172  */
173 static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
174 {
175         int charge;
176
177         charge = bq27x00_read(di, reg, false);
178         if (charge < 0) {
179                 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
180                         reg, charge);
181                 return charge;
182         }
183
184         if (di->chip == BQ27500)
185                 charge *= 1000;
186         else
187                 charge = charge * 3570 / BQ27000_RS;
188
189         return charge;
190 }
191
192 /*
193  * Return the battery Nominal available capaciy in µAh
194  * Or < 0 if something fails.
195  */
196 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
197 {
198         return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
199 }
200
201 /*
202  * Return the battery Last measured discharge in µAh
203  * Or < 0 if something fails.
204  */
205 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
206 {
207         return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
208 }
209
210 /*
211  * Return the battery Initial last measured discharge in µAh
212  * Or < 0 if something fails.
213  */
214 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
215 {
216         int ilmd;
217
218         if (di->chip == BQ27500)
219                 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
220         else
221                 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
222
223         if (ilmd < 0) {
224                 dev_dbg(di->dev, "error reading initial last measured discharge\n");
225                 return ilmd;
226         }
227
228         if (di->chip == BQ27500)
229                 ilmd *= 1000;
230         else
231                 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
232
233         return ilmd;
234 }
235
236 /*
237  * Return the battery Available energy in µWh
238  * Or < 0 if something fails.
239  */
240 static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
241 {
242         int ae;
243
244         ae = bq27x00_read(di, BQ27x00_REG_AE, false);
245         if (ae < 0) {
246                 dev_dbg(di->dev, "error reading available energy\n");
247                 return ae;
248         }
249
250         if (di->chip == BQ27500)
251                 ae *= 1000;
252         else
253                 ae = ae * 29200 / BQ27000_RS;
254
255         return ae;
256 }
257
258 /*
259  * Return the battery temperature in tenths of degree Celsius
260  * Or < 0 if something fails.
261  */
262 static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
263 {
264         int temp;
265
266         temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
267         if (temp < 0) {
268                 dev_err(di->dev, "error reading temperature\n");
269                 return temp;
270         }
271
272         if (di->chip == BQ27500)
273                 temp -= 2731;
274         else
275                 temp = ((temp * 5) - 5463) / 2;
276
277         return temp;
278 }
279
280 /*
281  * Return the battery Cycle count total
282  * Or < 0 if something fails.
283  */
284 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
285 {
286         int cyct;
287
288         cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
289         if (cyct < 0)
290                 dev_err(di->dev, "error reading cycle count total\n");
291
292         return cyct;
293 }
294
295 /*
296  * Read a time register.
297  * Return < 0 if something fails.
298  */
299 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
300 {
301         int tval;
302
303         tval = bq27x00_read(di, reg, false);
304         if (tval < 0) {
305                 dev_dbg(di->dev, "error reading time register %02x: %d\n",
306                         reg, tval);
307                 return tval;
308         }
309
310         if (tval == 65535)
311                 return -ENODATA;
312
313         return tval * 60;
314 }
315
316 /*
317  * Read a power avg register.
318  * Return < 0 if something fails.
319  */
320 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
321 {
322         int tval;
323
324         tval = bq27x00_read(di, reg, false);
325         if (tval < 0) {
326                 dev_err(di->dev, "error reading power avg rgister  %02x: %d\n",
327                         reg, tval);
328                 return tval;
329         }
330
331         if (di->chip == BQ27500)
332                 return tval;
333         else
334                 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
335 }
336
337 /*
338  * Read flag register.
339  * Return < 0 if something fails.
340  */
341 static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
342 {
343         int tval;
344
345         tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
346         if (tval < 0) {
347                 dev_err(di->dev, "error reading flag register:%d\n", tval);
348                 return tval;
349         }
350
351         if ((di->chip == BQ27500)) {
352                 if (tval & BQ27500_FLAG_SOCF)
353                         tval = POWER_SUPPLY_HEALTH_DEAD;
354                 else if (tval & BQ27500_FLAG_OTC)
355                         tval = POWER_SUPPLY_HEALTH_OVERHEAT;
356                 else
357                         tval = POWER_SUPPLY_HEALTH_GOOD;
358                 return tval;
359         } else {
360                 if (tval & BQ27000_FLAG_EDV1)
361                         tval = POWER_SUPPLY_HEALTH_DEAD;
362                 else
363                         tval = POWER_SUPPLY_HEALTH_GOOD;
364                 return tval;
365         }
366
367         return -1;
368 }
369
370 static void bq27x00_update(struct bq27x00_device_info *di)
371 {
372         struct bq27x00_reg_cache cache = {0, };
373         bool is_bq27500 = di->chip == BQ27500;
374
375         cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, !is_bq27500);
376         if (cache.flags >= 0) {
377                 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
378                         dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
379                         cache.capacity = -ENODATA;
380                         cache.energy = -ENODATA;
381                         cache.time_to_empty = -ENODATA;
382                         cache.time_to_empty_avg = -ENODATA;
383                         cache.time_to_full = -ENODATA;
384                         cache.charge_full = -ENODATA;
385                         cache.health = -ENODATA;
386                 } else {
387                         cache.capacity = bq27x00_battery_read_rsoc(di);
388                         cache.energy = bq27x00_battery_read_energy(di);
389                         cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
390                         cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
391                         cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
392                         cache.charge_full = bq27x00_battery_read_lmd(di);
393                         cache.health = bq27x00_battery_read_health(di);
394                 }
395                 cache.temperature = bq27x00_battery_read_temperature(di);
396                 cache.cycle_count = bq27x00_battery_read_cyct(di);
397                 cache.power_avg =
398                         bq27x00_battery_read_pwr_avg(di, BQ27x00_POWER_AVG);
399
400                 /* We only have to read charge design full once */
401                 if (di->charge_design_full <= 0)
402                         di->charge_design_full = bq27x00_battery_read_ilmd(di);
403         }
404
405         if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
406                 di->cache = cache;
407                 power_supply_changed(&di->bat);
408         }
409
410         di->last_update = jiffies;
411 }
412
413 static void bq27x00_battery_poll(struct work_struct *work)
414 {
415         struct bq27x00_device_info *di =
416                 container_of(work, struct bq27x00_device_info, work.work);
417
418         bq27x00_update(di);
419
420         if (poll_interval > 0) {
421                 /* The timer does not have to be accurate. */
422                 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
423                 schedule_delayed_work(&di->work, poll_interval * HZ);
424         }
425 }
426
427 /*
428  * Return the battery average current in µA
429  * Note that current can be negative signed as well
430  * Or 0 if something fails.
431  */
432 static int bq27x00_battery_current(struct bq27x00_device_info *di,
433         union power_supply_propval *val)
434 {
435         int curr;
436         int flags;
437
438         curr = bq27x00_read(di, BQ27x00_REG_AI, false);
439         if (curr < 0) {
440                 dev_err(di->dev, "error reading current\n");
441                 return curr;
442         }
443
444         if (di->chip == BQ27500) {
445                 /* bq27500 returns signed value */
446                 val->intval = (int)((s16)curr) * 1000;
447         } else {
448                 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
449                 if (flags & BQ27000_FLAG_CHGS) {
450                         dev_dbg(di->dev, "negative current!\n");
451                         curr = -curr;
452                 }
453
454                 val->intval = curr * 3570 / BQ27000_RS;
455         }
456
457         return 0;
458 }
459
460 static int bq27x00_battery_status(struct bq27x00_device_info *di,
461         union power_supply_propval *val)
462 {
463         int status;
464
465         if (di->chip == BQ27500) {
466                 if (di->cache.flags & BQ27500_FLAG_FC)
467                         status = POWER_SUPPLY_STATUS_FULL;
468                 else if (di->cache.flags & BQ27500_FLAG_DSC)
469                         status = POWER_SUPPLY_STATUS_DISCHARGING;
470                 else
471                         status = POWER_SUPPLY_STATUS_CHARGING;
472         } else {
473                 if (di->cache.flags & BQ27000_FLAG_FC)
474                         status = POWER_SUPPLY_STATUS_FULL;
475                 else if (di->cache.flags & BQ27000_FLAG_CHGS)
476                         status = POWER_SUPPLY_STATUS_CHARGING;
477                 else if (power_supply_am_i_supplied(&di->bat))
478                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
479                 else
480                         status = POWER_SUPPLY_STATUS_DISCHARGING;
481         }
482
483         val->intval = status;
484
485         return 0;
486 }
487
488 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
489         union power_supply_propval *val)
490 {
491         int level;
492
493         if (di->chip == BQ27500) {
494                 if (di->cache.flags & BQ27500_FLAG_FC)
495                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
496                 else if (di->cache.flags & BQ27500_FLAG_SOC1)
497                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
498                 else if (di->cache.flags & BQ27500_FLAG_SOCF)
499                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
500                 else
501                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
502         } else {
503                 if (di->cache.flags & BQ27000_FLAG_FC)
504                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
505                 else if (di->cache.flags & BQ27000_FLAG_EDV1)
506                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
507                 else if (di->cache.flags & BQ27000_FLAG_EDVF)
508                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
509                 else
510                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
511         }
512
513         val->intval = level;
514
515         return 0;
516 }
517
518 /*
519  * Return the battery Voltage in millivolts
520  * Or < 0 if something fails.
521  */
522 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
523         union power_supply_propval *val)
524 {
525         int volt;
526
527         volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
528         if (volt < 0) {
529                 dev_err(di->dev, "error reading voltage\n");
530                 return volt;
531         }
532
533         val->intval = volt * 1000;
534
535         return 0;
536 }
537
538 static int bq27x00_simple_value(int value,
539         union power_supply_propval *val)
540 {
541         if (value < 0)
542                 return value;
543
544         val->intval = value;
545
546         return 0;
547 }
548
549 #define to_bq27x00_device_info(x) container_of((x), \
550                                 struct bq27x00_device_info, bat);
551
552 static int bq27x00_battery_get_property(struct power_supply *psy,
553                                         enum power_supply_property psp,
554                                         union power_supply_propval *val)
555 {
556         int ret = 0;
557         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
558
559         mutex_lock(&di->lock);
560         if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
561                 cancel_delayed_work_sync(&di->work);
562                 bq27x00_battery_poll(&di->work.work);
563         }
564         mutex_unlock(&di->lock);
565
566         if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
567                 return -ENODEV;
568
569         switch (psp) {
570         case POWER_SUPPLY_PROP_STATUS:
571                 ret = bq27x00_battery_status(di, val);
572                 break;
573         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
574                 ret = bq27x00_battery_voltage(di, val);
575                 break;
576         case POWER_SUPPLY_PROP_PRESENT:
577                 val->intval = di->cache.flags < 0 ? 0 : 1;
578                 break;
579         case POWER_SUPPLY_PROP_CURRENT_NOW:
580                 ret = bq27x00_battery_current(di, val);
581                 break;
582         case POWER_SUPPLY_PROP_CAPACITY:
583                 ret = bq27x00_simple_value(di->cache.capacity, val);
584                 break;
585         case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
586                 ret = bq27x00_battery_capacity_level(di, val);
587                 break;
588         case POWER_SUPPLY_PROP_TEMP:
589                 ret = bq27x00_simple_value(di->cache.temperature, val);
590                 break;
591         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
592                 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
593                 break;
594         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
595                 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
596                 break;
597         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
598                 ret = bq27x00_simple_value(di->cache.time_to_full, val);
599                 break;
600         case POWER_SUPPLY_PROP_TECHNOLOGY:
601                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
602                 break;
603         case POWER_SUPPLY_PROP_CHARGE_NOW:
604                 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
605                 break;
606         case POWER_SUPPLY_PROP_CHARGE_FULL:
607                 ret = bq27x00_simple_value(di->cache.charge_full, val);
608                 break;
609         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
610                 ret = bq27x00_simple_value(di->charge_design_full, val);
611                 break;
612         case POWER_SUPPLY_PROP_CYCLE_COUNT:
613                 ret = bq27x00_simple_value(di->cache.cycle_count, val);
614                 break;
615         case POWER_SUPPLY_PROP_ENERGY_NOW:
616                 ret = bq27x00_simple_value(di->cache.energy, val);
617                 break;
618         case POWER_SUPPLY_PROP_POWER_AVG:
619                 ret = bq27x00_simple_value(di->cache.power_avg, val);
620                 break;
621         case POWER_SUPPLY_PROP_HEALTH:
622                 ret = bq27x00_simple_value(di->cache.health, val);
623                 break;
624         default:
625                 return -EINVAL;
626         }
627
628         return ret;
629 }
630
631 static void bq27x00_external_power_changed(struct power_supply *psy)
632 {
633         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
634
635         cancel_delayed_work_sync(&di->work);
636         schedule_delayed_work(&di->work, 0);
637 }
638
639 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
640 {
641         int ret;
642
643         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
644         di->bat.properties = bq27x00_battery_props;
645         di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
646         di->bat.get_property = bq27x00_battery_get_property;
647         di->bat.external_power_changed = bq27x00_external_power_changed;
648
649         INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
650         mutex_init(&di->lock);
651
652         ret = power_supply_register(di->dev, &di->bat);
653         if (ret) {
654                 dev_err(di->dev, "failed to register battery: %d\n", ret);
655                 return ret;
656         }
657
658         dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
659
660         bq27x00_update(di);
661
662         return 0;
663 }
664
665 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
666 {
667         /*
668          * power_supply_unregister call bq27x00_battery_get_property which
669          * call bq27x00_battery_poll.
670          * Make sure that bq27x00_battery_poll will not call
671          * schedule_delayed_work again after unregister (which cause OOPS).
672          */
673         poll_interval = 0;
674
675         cancel_delayed_work_sync(&di->work);
676
677         power_supply_unregister(&di->bat);
678
679         mutex_destroy(&di->lock);
680 }
681
682
683 /* i2c specific code */
684 #ifdef CONFIG_BATTERY_BQ27X00_I2C
685
686 /* If the system has several batteries we need a different name for each
687  * of them...
688  */
689 static DEFINE_IDR(battery_id);
690 static DEFINE_MUTEX(battery_mutex);
691
692 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
693 {
694         struct i2c_client *client = to_i2c_client(di->dev);
695         struct i2c_msg msg[2];
696         unsigned char data[2];
697         int ret;
698
699         if (!client->adapter)
700                 return -ENODEV;
701
702         msg[0].addr = client->addr;
703         msg[0].flags = 0;
704         msg[0].buf = &reg;
705         msg[0].len = sizeof(reg);
706         msg[1].addr = client->addr;
707         msg[1].flags = I2C_M_RD;
708         msg[1].buf = data;
709         if (single)
710                 msg[1].len = 1;
711         else
712                 msg[1].len = 2;
713
714         ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
715         if (ret < 0)
716                 return ret;
717
718         if (!single)
719                 ret = get_unaligned_le16(data);
720         else
721                 ret = data[0];
722
723         return ret;
724 }
725
726 static int bq27x00_battery_probe(struct i2c_client *client,
727                                  const struct i2c_device_id *id)
728 {
729         char *name;
730         struct bq27x00_device_info *di;
731         int num;
732         int retval = 0;
733
734         /* Get new ID for the new battery device */
735         retval = idr_pre_get(&battery_id, GFP_KERNEL);
736         if (retval == 0)
737                 return -ENOMEM;
738         mutex_lock(&battery_mutex);
739         retval = idr_get_new(&battery_id, client, &num);
740         mutex_unlock(&battery_mutex);
741         if (retval < 0)
742                 return retval;
743
744         name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
745         if (!name) {
746                 dev_err(&client->dev, "failed to allocate device name\n");
747                 retval = -ENOMEM;
748                 goto batt_failed_1;
749         }
750
751         di = kzalloc(sizeof(*di), GFP_KERNEL);
752         if (!di) {
753                 dev_err(&client->dev, "failed to allocate device info data\n");
754                 retval = -ENOMEM;
755                 goto batt_failed_2;
756         }
757
758         di->id = num;
759         di->dev = &client->dev;
760         di->chip = id->driver_data;
761         di->bat.name = name;
762         di->bus.read = &bq27x00_read_i2c;
763
764         if (bq27x00_powersupply_init(di))
765                 goto batt_failed_3;
766
767         i2c_set_clientdata(client, di);
768
769         return 0;
770
771 batt_failed_3:
772         kfree(di);
773 batt_failed_2:
774         kfree(name);
775 batt_failed_1:
776         mutex_lock(&battery_mutex);
777         idr_remove(&battery_id, num);
778         mutex_unlock(&battery_mutex);
779
780         return retval;
781 }
782
783 static int bq27x00_battery_remove(struct i2c_client *client)
784 {
785         struct bq27x00_device_info *di = i2c_get_clientdata(client);
786
787         bq27x00_powersupply_unregister(di);
788
789         kfree(di->bat.name);
790
791         mutex_lock(&battery_mutex);
792         idr_remove(&battery_id, di->id);
793         mutex_unlock(&battery_mutex);
794
795         kfree(di);
796
797         return 0;
798 }
799
800 static const struct i2c_device_id bq27x00_id[] = {
801         { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
802         { "bq27500", BQ27500 },
803         {},
804 };
805 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
806
807 static struct i2c_driver bq27x00_battery_driver = {
808         .driver = {
809                 .name = "bq27x00-battery",
810         },
811         .probe = bq27x00_battery_probe,
812         .remove = bq27x00_battery_remove,
813         .id_table = bq27x00_id,
814 };
815
816 static inline int bq27x00_battery_i2c_init(void)
817 {
818         int ret = i2c_add_driver(&bq27x00_battery_driver);
819         if (ret)
820                 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
821
822         return ret;
823 }
824
825 static inline void bq27x00_battery_i2c_exit(void)
826 {
827         i2c_del_driver(&bq27x00_battery_driver);
828 }
829
830 #else
831
832 static inline int bq27x00_battery_i2c_init(void) { return 0; }
833 static inline void bq27x00_battery_i2c_exit(void) {};
834
835 #endif
836
837 /* platform specific code */
838 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
839
840 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
841                         bool single)
842 {
843         struct device *dev = di->dev;
844         struct bq27000_platform_data *pdata = dev->platform_data;
845         unsigned int timeout = 3;
846         int upper, lower;
847         int temp;
848
849         if (!single) {
850                 /* Make sure the value has not changed in between reading the
851                  * lower and the upper part */
852                 upper = pdata->read(dev, reg + 1);
853                 do {
854                         temp = upper;
855                         if (upper < 0)
856                                 return upper;
857
858                         lower = pdata->read(dev, reg);
859                         if (lower < 0)
860                                 return lower;
861
862                         upper = pdata->read(dev, reg + 1);
863                 } while (temp != upper && --timeout);
864
865                 if (timeout == 0)
866                         return -EIO;
867
868                 return (upper << 8) | lower;
869         }
870
871         return pdata->read(dev, reg);
872 }
873
874 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
875 {
876         struct bq27x00_device_info *di;
877         struct bq27000_platform_data *pdata = pdev->dev.platform_data;
878         int ret;
879
880         if (!pdata) {
881                 dev_err(&pdev->dev, "no platform_data supplied\n");
882                 return -EINVAL;
883         }
884
885         if (!pdata->read) {
886                 dev_err(&pdev->dev, "no hdq read callback supplied\n");
887                 return -EINVAL;
888         }
889
890         di = kzalloc(sizeof(*di), GFP_KERNEL);
891         if (!di) {
892                 dev_err(&pdev->dev, "failed to allocate device info data\n");
893                 return -ENOMEM;
894         }
895
896         platform_set_drvdata(pdev, di);
897
898         di->dev = &pdev->dev;
899         di->chip = BQ27000;
900
901         di->bat.name = pdata->name ?: dev_name(&pdev->dev);
902         di->bus.read = &bq27000_read_platform;
903
904         ret = bq27x00_powersupply_init(di);
905         if (ret)
906                 goto err_free;
907
908         return 0;
909
910 err_free:
911         platform_set_drvdata(pdev, NULL);
912         kfree(di);
913
914         return ret;
915 }
916
917 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
918 {
919         struct bq27x00_device_info *di = platform_get_drvdata(pdev);
920
921         bq27x00_powersupply_unregister(di);
922
923         platform_set_drvdata(pdev, NULL);
924         kfree(di);
925
926         return 0;
927 }
928
929 static struct platform_driver bq27000_battery_driver = {
930         .probe  = bq27000_battery_probe,
931         .remove = __devexit_p(bq27000_battery_remove),
932         .driver = {
933                 .name = "bq27000-battery",
934                 .owner = THIS_MODULE,
935         },
936 };
937
938 static inline int bq27x00_battery_platform_init(void)
939 {
940         int ret = platform_driver_register(&bq27000_battery_driver);
941         if (ret)
942                 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
943
944         return ret;
945 }
946
947 static inline void bq27x00_battery_platform_exit(void)
948 {
949         platform_driver_unregister(&bq27000_battery_driver);
950 }
951
952 #else
953
954 static inline int bq27x00_battery_platform_init(void) { return 0; }
955 static inline void bq27x00_battery_platform_exit(void) {};
956
957 #endif
958
959 /*
960  * Module stuff
961  */
962
963 static int __init bq27x00_battery_init(void)
964 {
965         int ret;
966
967         ret = bq27x00_battery_i2c_init();
968         if (ret)
969                 return ret;
970
971         ret = bq27x00_battery_platform_init();
972         if (ret)
973                 bq27x00_battery_i2c_exit();
974
975         return ret;
976 }
977 module_init(bq27x00_battery_init);
978
979 static void __exit bq27x00_battery_exit(void)
980 {
981         bq27x00_battery_platform_exit();
982         bq27x00_battery_i2c_exit();
983 }
984 module_exit(bq27x00_battery_exit);
985
986 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
987 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
988 MODULE_LICENSE("GPL");