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