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