8a18604b84ca3b2365c6ed9b80b19bd95617d612
[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 Celsius
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 -= 2731;
271         else
272                 temp = ((temp * 5) - 5463) / 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 void bq27x00_update(struct bq27x00_device_info *di)
314 {
315         struct bq27x00_reg_cache cache = {0, };
316         bool is_bq27500 = di->chip == BQ27500;
317         int flags_changed;
318
319         cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
320         if (cache.flags >= 0) {
321                 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
322                         dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
323                         cache.capacity = -ENODATA;
324                         cache.energy = -ENODATA;
325                         cache.time_to_empty = -ENODATA;
326                         cache.time_to_empty_avg = -ENODATA;
327                         cache.time_to_full = -ENODATA;
328                         cache.charge_full = -ENODATA;
329                 } else {
330                         cache.capacity = bq27x00_battery_read_rsoc(di);
331                         cache.energy = bq27x00_battery_read_energy(di);
332                         cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
333                         cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
334                         cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
335                         cache.charge_full = bq27x00_battery_read_lmd(di);
336                 }
337                 cache.temperature = bq27x00_battery_read_temperature(di);
338                 cache.cycle_count = bq27x00_battery_read_cyct(di);
339
340                 /* We only have to read charge design full once */
341                 if (di->charge_design_full <= 0)
342                         di->charge_design_full = bq27x00_battery_read_ilmd(di);
343         }
344
345         flags_changed = di->cache.flags ^ cache.flags;
346         di->cache = cache;
347         if (is_bq27500)
348                 flags_changed &= BQ27500_FLAGS_IMPORTANT;
349         else
350                 flags_changed &= BQ27000_FLAGS_IMPORTANT;
351         if (flags_changed)
352                 power_supply_changed(&di->bat);
353
354         di->last_update = jiffies;
355 }
356
357 static void bq27x00_battery_poll(struct work_struct *work)
358 {
359         struct bq27x00_device_info *di =
360                 container_of(work, struct bq27x00_device_info, work.work);
361
362         bq27x00_update(di);
363
364         if (poll_interval > 0) {
365                 /* The timer does not have to be accurate. */
366                 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
367                 schedule_delayed_work(&di->work, poll_interval * HZ);
368         }
369 }
370
371 /*
372  * Return the battery average current in µA
373  * Note that current can be negative signed as well
374  * Or 0 if something fails.
375  */
376 static int bq27x00_battery_current(struct bq27x00_device_info *di,
377         union power_supply_propval *val)
378 {
379         int curr;
380         int flags;
381
382         curr = bq27x00_read(di, BQ27x00_REG_AI, false);
383         if (curr < 0) {
384                 dev_err(di->dev, "error reading current\n");
385                 return curr;
386         }
387
388         if (di->chip == BQ27500) {
389                 /* bq27500 returns signed value */
390                 val->intval = (int)((s16)curr) * 1000;
391         } else {
392                 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
393                 if (flags & BQ27000_FLAG_CHGS) {
394                         dev_dbg(di->dev, "negative current!\n");
395                         curr = -curr;
396                 }
397
398                 val->intval = curr * 3570 / BQ27000_RS;
399         }
400
401         return 0;
402 }
403
404 static int bq27x00_battery_status(struct bq27x00_device_info *di,
405         union power_supply_propval *val)
406 {
407         int status;
408
409         if (di->chip == BQ27500) {
410                 if (di->cache.flags & BQ27500_FLAG_FC)
411                         status = POWER_SUPPLY_STATUS_FULL;
412                 else if (di->cache.flags & BQ27500_FLAG_DSC)
413                         status = POWER_SUPPLY_STATUS_DISCHARGING;
414                 else
415                         status = POWER_SUPPLY_STATUS_CHARGING;
416         } else {
417                 if (di->cache.flags & BQ27000_FLAG_FC)
418                         status = POWER_SUPPLY_STATUS_FULL;
419                 else if (di->cache.flags & BQ27000_FLAG_CHGS)
420                         status = POWER_SUPPLY_STATUS_CHARGING;
421                 else if (power_supply_am_i_supplied(&di->bat))
422                         status = POWER_SUPPLY_STATUS_NOT_CHARGING;
423                 else
424                         status = POWER_SUPPLY_STATUS_DISCHARGING;
425         }
426
427         val->intval = status;
428
429         return 0;
430 }
431
432 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
433         union power_supply_propval *val)
434 {
435         int level;
436
437         if (di->chip == BQ27500) {
438                 if (di->cache.flags & BQ27500_FLAG_FC)
439                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
440                 else if (di->cache.flags & BQ27500_FLAG_SOC1)
441                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
442                 else if (di->cache.flags & BQ27500_FLAG_SOCF)
443                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
444                 else
445                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
446         } else {
447                 if (di->cache.flags & BQ27000_FLAG_FC)
448                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
449                 else if (di->cache.flags & BQ27000_FLAG_EDV1)
450                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
451                 else if (di->cache.flags & BQ27000_FLAG_EDVF)
452                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
453                 else
454                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
455         }
456
457         val->intval = level;
458
459         return 0;
460 }
461
462 /*
463  * Return the battery Voltage in milivolts
464  * Or < 0 if something fails.
465  */
466 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
467         union power_supply_propval *val)
468 {
469         int volt;
470
471         volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
472         if (volt < 0) {
473                 dev_err(di->dev, "error reading voltage\n");
474                 return volt;
475         }
476
477         val->intval = volt * 1000;
478
479         return 0;
480 }
481
482 static int bq27x00_simple_value(int value,
483         union power_supply_propval *val)
484 {
485         if (value < 0)
486                 return value;
487
488         val->intval = value;
489
490         return 0;
491 }
492
493 #define to_bq27x00_device_info(x) container_of((x), \
494                                 struct bq27x00_device_info, bat);
495
496 static int bq27x00_battery_get_property(struct power_supply *psy,
497                                         enum power_supply_property psp,
498                                         union power_supply_propval *val)
499 {
500         int ret = 0;
501         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
502
503         mutex_lock(&di->lock);
504         if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
505                 cancel_delayed_work_sync(&di->work);
506                 bq27x00_battery_poll(&di->work.work);
507         }
508         mutex_unlock(&di->lock);
509
510         if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
511                 return -ENODEV;
512
513         switch (psp) {
514         case POWER_SUPPLY_PROP_STATUS:
515                 ret = bq27x00_battery_status(di, val);
516                 break;
517         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
518                 ret = bq27x00_battery_voltage(di, val);
519                 break;
520         case POWER_SUPPLY_PROP_PRESENT:
521                 val->intval = di->cache.flags < 0 ? 0 : 1;
522                 break;
523         case POWER_SUPPLY_PROP_CURRENT_NOW:
524                 ret = bq27x00_battery_current(di, val);
525                 break;
526         case POWER_SUPPLY_PROP_CAPACITY:
527                 ret = bq27x00_simple_value(di->cache.capacity, val);
528                 break;
529         case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
530                 ret = bq27x00_battery_capacity_level(di, val);
531                 break;
532         case POWER_SUPPLY_PROP_TEMP:
533                 ret = bq27x00_simple_value(di->cache.temperature, val);
534                 break;
535         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
536                 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
537                 break;
538         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
539                 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
540                 break;
541         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
542                 ret = bq27x00_simple_value(di->cache.time_to_full, val);
543                 break;
544         case POWER_SUPPLY_PROP_TECHNOLOGY:
545                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
546                 break;
547         case POWER_SUPPLY_PROP_CHARGE_NOW:
548                 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
549                 break;
550         case POWER_SUPPLY_PROP_CHARGE_FULL:
551                 ret = bq27x00_simple_value(di->cache.charge_full, val);
552                 break;
553         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
554                 ret = bq27x00_simple_value(di->charge_design_full, val);
555                 break;
556         case POWER_SUPPLY_PROP_CYCLE_COUNT:
557                 ret = bq27x00_simple_value(di->cache.cycle_count, val);
558                 break;
559         case POWER_SUPPLY_PROP_ENERGY_NOW:
560                 ret = bq27x00_simple_value(di->cache.energy, val);
561                 break;
562         default:
563                 return -EINVAL;
564         }
565
566         return ret;
567 }
568
569 static void bq27x00_external_power_changed(struct power_supply *psy)
570 {
571         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
572
573         cancel_delayed_work_sync(&di->work);
574         set_timer_slack(&di->work.timer, 2 * HZ);
575         schedule_delayed_work(&di->work, 2 * HZ);
576 }
577
578 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
579 {
580         int ret;
581
582         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
583         di->bat.properties = bq27x00_battery_props;
584         di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
585         di->bat.get_property = bq27x00_battery_get_property;
586         di->bat.external_power_changed = bq27x00_external_power_changed;
587
588         INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
589         mutex_init(&di->lock);
590
591         ret = power_supply_register(di->dev, &di->bat);
592         if (ret) {
593                 dev_err(di->dev, "failed to register battery: %d\n", ret);
594                 return ret;
595         }
596
597         dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
598
599         bq27x00_update(di);
600
601         return 0;
602 }
603
604 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
605 {
606         /*
607          * power_supply_unregister call bq27x00_battery_get_property which
608          * call bq27x00_battery_poll.
609          * Make sure that bq27x00_battery_poll will not call
610          * schedule_delayed_work again after unregister (which cause OOPS).
611          */
612         poll_interval = 0;
613
614         cancel_delayed_work_sync(&di->work);
615
616         power_supply_unregister(&di->bat);
617
618         mutex_destroy(&di->lock);
619 }
620
621
622 /* i2c specific code */
623 #ifdef CONFIG_BATTERY_BQ27X00_I2C
624
625 /* If the system has several batteries we need a different name for each
626  * of them...
627  */
628 static DEFINE_IDR(battery_id);
629 static DEFINE_MUTEX(battery_mutex);
630
631 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
632 {
633         struct i2c_client *client = to_i2c_client(di->dev);
634         struct i2c_msg msg[2];
635         unsigned char data[2];
636         int ret;
637
638         if (!client->adapter)
639                 return -ENODEV;
640
641         msg[0].addr = client->addr;
642         msg[0].flags = 0;
643         msg[0].buf = &reg;
644         msg[0].len = sizeof(reg);
645         msg[1].addr = client->addr;
646         msg[1].flags = I2C_M_RD;
647         msg[1].buf = data;
648         if (single)
649                 msg[1].len = 1;
650         else
651                 msg[1].len = 2;
652
653         ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
654         if (ret < 0)
655                 return ret;
656
657         if (!single)
658                 ret = get_unaligned_le16(data);
659         else
660                 ret = data[0];
661
662         return ret;
663 }
664
665 static int bq27x00_battery_probe(struct i2c_client *client,
666                                  const struct i2c_device_id *id)
667 {
668         char *name;
669         struct bq27x00_device_info *di;
670         int num;
671         int retval = 0;
672
673         /* Get new ID for the new battery device */
674         retval = idr_pre_get(&battery_id, GFP_KERNEL);
675         if (retval == 0)
676                 return -ENOMEM;
677         mutex_lock(&battery_mutex);
678         retval = idr_get_new(&battery_id, client, &num);
679         mutex_unlock(&battery_mutex);
680         if (retval < 0)
681                 return retval;
682
683         name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
684         if (!name) {
685                 dev_err(&client->dev, "failed to allocate device name\n");
686                 retval = -ENOMEM;
687                 goto batt_failed_1;
688         }
689
690         di = kzalloc(sizeof(*di), GFP_KERNEL);
691         if (!di) {
692                 dev_err(&client->dev, "failed to allocate device info data\n");
693                 retval = -ENOMEM;
694                 goto batt_failed_2;
695         }
696
697         di->id = num;
698         di->dev = &client->dev;
699         di->chip = id->driver_data;
700         di->bat.name = name;
701         di->bus.read = &bq27x00_read_i2c;
702
703         if (bq27x00_powersupply_init(di))
704                 goto batt_failed_3;
705
706         i2c_set_clientdata(client, di);
707
708         return 0;
709
710 batt_failed_3:
711         kfree(di);
712 batt_failed_2:
713         kfree(name);
714 batt_failed_1:
715         mutex_lock(&battery_mutex);
716         idr_remove(&battery_id, num);
717         mutex_unlock(&battery_mutex);
718
719         return retval;
720 }
721
722 static int bq27x00_battery_remove(struct i2c_client *client)
723 {
724         struct bq27x00_device_info *di = i2c_get_clientdata(client);
725
726         bq27x00_powersupply_unregister(di);
727
728         kfree(di->bat.name);
729
730         mutex_lock(&battery_mutex);
731         idr_remove(&battery_id, di->id);
732         mutex_unlock(&battery_mutex);
733
734         kfree(di);
735
736         return 0;
737 }
738
739 static const struct i2c_device_id bq27x00_id[] = {
740         { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
741         { "bq27500", BQ27500 },
742         {},
743 };
744 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
745
746 static struct i2c_driver bq27x00_battery_driver = {
747         .driver = {
748                 .name = "bq27x00-battery",
749         },
750         .probe = bq27x00_battery_probe,
751         .remove = bq27x00_battery_remove,
752         .id_table = bq27x00_id,
753 };
754
755 static inline int bq27x00_battery_i2c_init(void)
756 {
757         int ret = i2c_add_driver(&bq27x00_battery_driver);
758         if (ret)
759                 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
760
761         return ret;
762 }
763
764 static inline void bq27x00_battery_i2c_exit(void)
765 {
766         i2c_del_driver(&bq27x00_battery_driver);
767 }
768
769 #else
770
771 static inline int bq27x00_battery_i2c_init(void) { return 0; }
772 static inline void bq27x00_battery_i2c_exit(void) {};
773
774 #endif
775
776 /* platform specific code */
777 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
778
779 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
780                         bool single)
781 {
782         struct device *dev = di->dev;
783         struct bq27000_platform_data *pdata = dev->platform_data;
784         unsigned int timeout = 3;
785         int upper, lower;
786         int temp;
787
788         if (!single) {
789                 /* Make sure the value has not changed in between reading the
790                  * lower and the upper part */
791                 upper = pdata->read(dev, reg + 1);
792                 do {
793                         temp = upper;
794                         if (upper < 0)
795                                 return upper;
796
797                         lower = pdata->read(dev, reg);
798                         if (lower < 0)
799                                 return lower;
800
801                         upper = pdata->read(dev, reg + 1);
802                 } while (temp != upper && --timeout);
803
804                 if (timeout == 0)
805                         return -EIO;
806
807                 return (upper << 8) | lower;
808         }
809
810         return pdata->read(dev, reg);
811 }
812
813 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
814 {
815         struct bq27x00_device_info *di;
816         struct bq27000_platform_data *pdata = pdev->dev.platform_data;
817         int ret;
818
819         if (!pdata) {
820                 dev_err(&pdev->dev, "no platform_data supplied\n");
821                 return -EINVAL;
822         }
823
824         if (!pdata->read) {
825                 dev_err(&pdev->dev, "no hdq read callback supplied\n");
826                 return -EINVAL;
827         }
828
829         di = kzalloc(sizeof(*di), GFP_KERNEL);
830         if (!di) {
831                 dev_err(&pdev->dev, "failed to allocate device info data\n");
832                 return -ENOMEM;
833         }
834
835         platform_set_drvdata(pdev, di);
836
837         di->dev = &pdev->dev;
838         di->chip = BQ27000;
839
840         di->bat.name = pdata->name ?: dev_name(&pdev->dev);
841         di->bus.read = &bq27000_read_platform;
842
843         ret = bq27x00_powersupply_init(di);
844         if (ret)
845                 goto err_free;
846
847         return 0;
848
849 err_free:
850         platform_set_drvdata(pdev, NULL);
851         kfree(di);
852
853         return ret;
854 }
855
856 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
857 {
858         struct bq27x00_device_info *di = platform_get_drvdata(pdev);
859
860         bq27x00_powersupply_unregister(di);
861
862         platform_set_drvdata(pdev, NULL);
863         kfree(di);
864
865         return 0;
866 }
867
868 static struct platform_driver bq27000_battery_driver = {
869         .probe  = bq27000_battery_probe,
870         .remove = __devexit_p(bq27000_battery_remove),
871         .driver = {
872                 .name = "bq27000-battery",
873                 .owner = THIS_MODULE,
874         },
875 };
876
877 static inline int bq27x00_battery_platform_init(void)
878 {
879         int ret = platform_driver_register(&bq27000_battery_driver);
880         if (ret)
881                 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
882
883         return ret;
884 }
885
886 static inline void bq27x00_battery_platform_exit(void)
887 {
888         platform_driver_unregister(&bq27000_battery_driver);
889 }
890
891 #else
892
893 static inline int bq27x00_battery_platform_init(void) { return 0; }
894 static inline void bq27x00_battery_platform_exit(void) {};
895
896 #endif
897
898 /*
899  * Module stuff
900  */
901
902 static int __init bq27x00_battery_init(void)
903 {
904         int ret;
905
906         ret = bq27x00_battery_i2c_init();
907         if (ret)
908                 return ret;
909
910         ret = bq27x00_battery_platform_init();
911         if (ret)
912                 bq27x00_battery_i2c_exit();
913
914         return ret;
915 }
916 module_init(bq27x00_battery_init);
917
918 static void __exit bq27x00_battery_exit(void)
919 {
920         bq27x00_battery_platform_exit();
921         bq27x00_battery_i2c_exit();
922 }
923 module_exit(bq27x00_battery_exit);
924
925 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
926 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
927 MODULE_LICENSE("GPL");