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