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