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