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