a09f23bc605dada443d2e71bed819cd0f6d93cc1
[kernel-power] / kernel-power-2.6.28 / debian / patches / bq27x00_battery.patch
1 --- kernel-power-2.6.28/drivers/power/bq27x00_battery.c 2011-02-18 19:36:55.000000000 +0100
2 +++ kernel-power-2.6.28/drivers/power/bq27x00_battery.c 2011-02-18 19:39:01.000000000 +0100
3 @@ -3,6 +3,8 @@
4   *
5   * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
6   * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
7 + * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
8 + * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
9   *
10   * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
11   *
12 @@ -15,6 +17,13 @@
13   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14   *
15   */
16 +
17 +/*
18 + * Datasheets:
19 + * http://focus.ti.com/docs/prod/folders/print/bq27000.html
20 + * http://focus.ti.com/docs/prod/folders/print/bq27500.html
21 + */
22 +
23  #include <linux/module.h>
24  #include <linux/param.h>
25  #include <linux/jiffies.h>
26 @@ -24,144 +33,416 @@
27  #include <linux/power_supply.h>
28  #include <linux/idr.h>
29  #include <linux/i2c.h>
30 +#include <linux/slab.h>
31  #include <asm/unaligned.h>
32  
33 -#define DRIVER_VERSION                 "1.0.0"
34 +//#include <linux/power/bq27x00_battery.h>
35 +
36 +#define CONFIG_BATTERY_BQ27X00_I2C
37 +
38 +struct bq27000_platform_data {
39 +       const char *name;
40 +       int (*read)(struct device *dev, unsigned int);
41 +};
42 +
43 +#define DRIVER_VERSION                 "1.2.0"
44  
45  #define BQ27x00_REG_TEMP               0x06
46  #define BQ27x00_REG_VOLT               0x08
47 -#define BQ27x00_REG_RSOC               0x0B /* Relative State-of-Charge */
48  #define BQ27x00_REG_AI                 0x14
49  #define BQ27x00_REG_FLAGS              0x0A
50 +#define BQ27x00_REG_TTE                        0x16
51 +#define BQ27x00_REG_TTF                        0x18
52 +#define BQ27x00_REG_TTECP              0x26
53 +#define BQ27x00_REG_NAC                        0x0C /* Nominal available capaciy */
54 +#define BQ27x00_REG_LMD                        0x12 /* Last measured discharge */
55 +#define BQ27x00_REG_CYCT               0x2A /* Cycle count total */
56 +#define BQ27x00_REG_AE                 0x22 /* Available enery */
57 +
58 +#define BQ27000_REG_RSOC               0x0B /* Relative State-of-Charge */
59 +#define BQ27000_REG_ILMD               0x76 /* Initial last measured discharge */
60 +#define BQ27000_FLAG_CHGS              BIT(7)
61 +#define BQ27000_FLAG_FC                        BIT(5)
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_FC                        BIT(9)
67  
68 -/* If the system has several batteries we need a different name for each
69 - * of them...
70 - */
71 -static DEFINE_IDR(battery_id);
72 -static DEFINE_MUTEX(battery_mutex);
73 +#define BQ27000_RS                     20 /* Resistor sense */
74  
75  struct bq27x00_device_info;
76  struct bq27x00_access_methods {
77 -       int (*read)(u8 reg, int *rt_value, int b_single,
78 -               struct bq27x00_device_info *di);
79 +       int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
80 +};
81 +
82 +enum bq27x00_chip { BQ27000, BQ27500 };
83 +
84 +struct bq27x00_reg_cache {
85 +       int temperature;
86 +       int time_to_empty;
87 +       int time_to_empty_avg;
88 +       int time_to_full;
89 +       int charge_full;
90 +       int charge_counter;
91 +       int capacity;
92 +       int flags;
93 +
94 +       int current_now;
95  };
96  
97  struct bq27x00_device_info {
98         struct device           *dev;
99         int                     id;
100 -       int                     voltage_uV;
101 -       int                     current_uA;
102 -       int                     temp_C;
103 -       int                     charge_rsoc;
104 -       struct bq27x00_access_methods   *bus;
105 +       enum bq27x00_chip       chip;
106 +
107 +       struct bq27x00_reg_cache cache;
108 +       int charge_design_full;
109 +
110 +       unsigned long last_update;
111 +       struct delayed_work work;
112 +
113         struct power_supply     bat;
114  
115 -       struct i2c_client       *client;
116 +       struct bq27x00_access_methods bus;
117 +
118 +       struct mutex lock;
119  };
120  
121  static enum power_supply_property bq27x00_battery_props[] = {
122 +       POWER_SUPPLY_PROP_STATUS,
123         POWER_SUPPLY_PROP_PRESENT,
124         POWER_SUPPLY_PROP_VOLTAGE_NOW,
125         POWER_SUPPLY_PROP_CURRENT_NOW,
126         POWER_SUPPLY_PROP_CAPACITY,
127         POWER_SUPPLY_PROP_TEMP,
128 +       POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
129 +       POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
130 +       POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
131 +       POWER_SUPPLY_PROP_TECHNOLOGY,
132 +       POWER_SUPPLY_PROP_CHARGE_FULL,
133 +       POWER_SUPPLY_PROP_CHARGE_NOW,
134 +       POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
135 +       POWER_SUPPLY_PROP_CHARGE_COUNTER,
136 +//     POWER_SUPPLY_PROP_ENERGY_NOW,
137  };
138  
139 +static unsigned int poll_interval = 360;
140 +module_param(poll_interval, uint, 0644);
141 +MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
142 +                               "0 disables polling");
143 +
144  /*
145   * Common code for BQ27x00 devices
146   */
147  
148 -static int bq27x00_read(u8 reg, int *rt_value, int b_single,
149 -                       struct bq27x00_device_info *di)
150 +static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
151 +               bool single)
152  {
153 -       int ret;
154 +       return di->bus.read(di, reg, single);
155 +}
156 +
157 +/*
158 + * Return the battery Relative State-of-Charge
159 + * Or < 0 if something fails.
160 + */
161 +static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
162 +{
163 +       int rsoc;
164  
165 -       ret = di->bus->read(reg, rt_value, b_single, di);
166 -       *rt_value = be16_to_cpu(*rt_value);
167 +       if (di->chip == BQ27500)
168 +               rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
169 +       else
170 +               rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
171  
172 -       return ret;
173 +       if (rsoc < 0)
174 +               dev_err(di->dev, "error reading relative State-of-Charge\n");
175 +
176 +       return rsoc;
177  }
178  
179  /*
180 - * Return the battery temperature in Celcius degrees
181 + * Return a battery charge value in µAh
182   * Or < 0 if something fails.
183   */
184 -static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
185 +static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
186  {
187 -       int ret;
188 -       int temp = 0;
189 +       int charge;
190  
191 -       ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
192 -       if (ret) {
193 -               dev_err(di->dev, "error reading temperature\n");
194 -               return ret;
195 +       charge = bq27x00_read(di, reg, false);
196 +       if (charge < 0) {
197 +               dev_err(di->dev, "error reading nominal available capacity\n");
198 +               return charge;
199         }
200  
201 -       return (temp >> 2) - 273;
202 +       if (di->chip == BQ27500)
203 +               charge *= 1000;
204 +       else
205 +               charge = charge * 3570 / BQ27000_RS;
206 +
207 +       return charge;
208  }
209  
210  /*
211 - * Return the battery Voltage in milivolts
212 + * Return the battery Nominal available capaciy in µAh
213   * Or < 0 if something fails.
214   */
215 -static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
216 +static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
217  {
218 -       int ret;
219 -       int volt = 0;
220 +       return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
221 +}
222  
223 -       ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
224 -       if (ret) {
225 -               dev_err(di->dev, "error reading voltage\n");
226 -               return ret;
227 +/*
228 + * Return the battery Last measured discharge in µAh
229 + * Or < 0 if something fails.
230 + */
231 +static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
232 +{
233 +       return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
234 +}
235 +
236 +/*
237 + * Return the battery Initial last measured discharge in µAh
238 + * Or < 0 if something fails.
239 + */
240 +static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
241 +{
242 +       int ilmd;
243 +
244 +       if (di->chip == BQ27500)
245 +               ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
246 +       else
247 +               ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
248 +
249 +       if (ilmd < 0) {
250 +               dev_err(di->dev, "error reading initial last measured discharge\n");
251 +               return ilmd;
252         }
253  
254 -       return volt;
255 +       if (di->chip == BQ27500)
256 +               ilmd *= 1000;
257 +       else
258 +               ilmd = ilmd * 256 * 3570 / BQ27000_RS;
259 +
260 +       return ilmd;
261  }
262  
263  /*
264 - * Return the battery average current
265 - * Note that current can be negative signed as well
266 - * Or 0 if something fails.
267 + * Return the battery Cycle count total
268 + * Or < 0 if something fails.
269   */
270 -static int bq27x00_battery_current(struct bq27x00_device_info *di)
271 +static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
272  {
273 -       int ret;
274 -       int curr = 0;
275 -       int flags = 0;
276 +       int cyct;
277  
278 -       ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
279 -       if (ret) {
280 -               dev_err(di->dev, "error reading current\n");
281 -               return 0;
282 +       cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
283 +       if (cyct < 0)
284 +               dev_err(di->dev, "error reading cycle count total\n");
285 +
286 +       return cyct;
287 +}
288 +
289 +/*
290 + * Read a time register.
291 + * Return < 0 if something fails.
292 + */
293 +static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
294 +{
295 +       int tval;
296 +
297 +       tval = bq27x00_read(di, reg, false);
298 +       if (tval < 0) {
299 +               dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
300 +               return tval;
301         }
302 -       ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
303 -       if (ret < 0) {
304 -               dev_err(di->dev, "error reading flags\n");
305 +
306 +       if (tval == 65535)
307 +//             return -ENODATA;
308                 return 0;
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 +
318 +       cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
319 +       if (cache.flags >= 0) {
320 +               cache.capacity = bq27x00_battery_read_rsoc(di);
321 +               cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
322 +               cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
323 +               cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
324 +               cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
325 +               cache.charge_full = bq27x00_battery_read_lmd(di);
326 +               cache.charge_counter = bq27x00_battery_read_cyct(di);
327 +
328 +               if (!is_bq27500)
329 +                       cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false);
330 +
331 +               /* We only have to read charge design full once */
332 +               if (di->charge_design_full <= 0)
333 +                       di->charge_design_full = bq27x00_battery_read_ilmd(di);
334         }
335 -       if ((flags & (1 << 7)) != 0) {
336 -               dev_dbg(di->dev, "negative current!\n");
337 -               return -curr;
338 +
339 +       /* Ignore current_now which is a snapshot of the current battery state
340 +        * and is likely to be different even between two consecutive reads */
341 +       if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) {
342 +               di->cache = cache;
343 +               power_supply_changed(&di->bat);
344 +       }
345 +
346 +       di->last_update = jiffies;
347 +}
348 +
349 +static void bq27x00_battery_poll(struct work_struct *work)
350 +{
351 +       struct bq27x00_device_info *di =
352 +               container_of(work, struct bq27x00_device_info, work.work);
353 +
354 +       bq27x00_update(di);
355 +
356 +       if (poll_interval > 0) {
357 +               /* The timer does not have to be accurate. */
358 +//             set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
359 +               schedule_delayed_work(&di->work, poll_interval * HZ);
360         }
361 -       return curr;
362  }
363  
364 +
365  /*
366 - * Return the battery Relative State-of-Charge
367 + * Return the battery temperature in tenths of degree Celsius
368   * Or < 0 if something fails.
369   */
370 -static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
371 +static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
372 +       union power_supply_propval *val)
373  {
374 -       int ret;
375 -       int rsoc = 0;
376 +       if (di->cache.temperature < 0)
377 +               return di->cache.temperature;
378  
379 -       ret = bq27x00_read(BQ27x00_REG_RSOC, &rsoc, 1, di);
380 -       if (ret) {
381 -               dev_err(di->dev, "error reading relative State-of-Charge\n");
382 -               return ret;
383 +       if (di->chip == BQ27500)
384 +               val->intval = di->cache.temperature - 2731;
385 +       else
386 +               val->intval = ((di->cache.temperature * 5) - 5463) / 2;
387 +
388 +       return 0;
389 +}
390 +
391 +/*
392 + * Return the battery average current in µA
393 + * Note that current can be negative signed as well
394 + * Or 0 if something fails.
395 + */
396 +static int bq27x00_battery_current(struct bq27x00_device_info *di,
397 +       union power_supply_propval *val)
398 +{
399 +       int curr;
400 +
401 +       if (di->chip == BQ27500)
402 +           curr = bq27x00_read(di, BQ27x00_REG_AI, false);
403 +       else
404 +           curr = di->cache.current_now;
405 +
406 +       if (curr < 0)
407 +               return curr;
408 +
409 +       if (di->chip == BQ27500) {
410 +               /* bq27500 returns signed value */
411 +               val->intval = (int)((s16)curr) * 1000;
412 +       } else {
413 +               if (di->cache.flags & BQ27000_FLAG_CHGS) {
414 +                       dev_dbg(di->dev, "negative current!\n");
415 +                       curr = -curr;
416 +               }
417 +
418 +               val->intval = curr * 3570 / BQ27000_RS;
419         }
420  
421 -       return rsoc >> 8;
422 +       return 0;
423 +}
424 +
425 +static int bq27x00_battery_status(struct bq27x00_device_info *di,
426 +       union power_supply_propval *val)
427 +{
428 +       int status;
429 +
430 +       if (di->chip == BQ27500) {
431 +               if (di->cache.flags & BQ27500_FLAG_FC)
432 +                       status = POWER_SUPPLY_STATUS_FULL;
433 +               else if (di->cache.flags & BQ27500_FLAG_DSC)
434 +                       status = POWER_SUPPLY_STATUS_DISCHARGING;
435 +               else
436 +                       status = POWER_SUPPLY_STATUS_CHARGING;
437 +       } else {
438 +               if (di->cache.flags & BQ27000_FLAG_FC)
439 +                       status = POWER_SUPPLY_STATUS_FULL;
440 +               else if (di->cache.flags & BQ27000_FLAG_CHGS)
441 +                       status = POWER_SUPPLY_STATUS_CHARGING;
442 +               else if (power_supply_am_i_supplied(&di->bat))
443 +                       status = POWER_SUPPLY_STATUS_NOT_CHARGING;
444 +               else
445 +                       status = POWER_SUPPLY_STATUS_DISCHARGING;
446 +       }
447 +
448 +       val->intval = status;
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 +/*
472 + * Return the battery Available energy in µWh
473 + * Or < 0 if something fails.
474 + */
475 +static int bq27x00_battery_energy(struct bq27x00_device_info *di,
476 +       union power_supply_propval *val)
477 +{
478 +       int ae;
479 +
480 +       ae = bq27x00_read(di, BQ27x00_REG_AE, false);
481 +       if (ae < 0) {
482 +               dev_err(di->dev, "error reading available energy\n");
483 +               return ae;
484 +       }
485 +
486 +       if (di->chip == BQ27500)
487 +               ae *= 1000;
488 +       else
489 +               ae = ae * 29200 / BQ27000_RS;
490 +
491 +       val->intval = ae;
492 +
493 +       return 0;
494 +}
495 +
496 +
497 +static int bq27x00_simple_value(int value,
498 +       union power_supply_propval *val)
499 +{
500 +       if (value < 0)
501 +               return value;
502 +
503 +       val->intval = value;
504 +
505 +       return 0;
506  }
507  
508  #define to_bq27x00_device_info(x) container_of((x), \
509 @@ -171,89 +452,164 @@ static int bq27x00_battery_get_property(
510                                         enum power_supply_property psp,
511                                         union power_supply_propval *val)
512  {
513 +       int ret = 0;
514         struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
515  
516 +       mutex_lock(&di->lock);
517 +       if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
518 +               cancel_delayed_work_sync(&di->work);
519 +               bq27x00_battery_poll(&di->work.work);
520 +       }
521 +       mutex_unlock(&di->lock);
522 +
523 +       if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
524 +               return -ENODEV;
525 +
526         switch (psp) {
527 +       case POWER_SUPPLY_PROP_STATUS:
528 +               ret = bq27x00_battery_status(di, val);
529 +               break;
530         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
531 +               ret = bq27x00_battery_voltage(di, val);
532 +               break;
533         case POWER_SUPPLY_PROP_PRESENT:
534 -               val->intval = bq27x00_battery_voltage(di);
535 -               if (psp == POWER_SUPPLY_PROP_PRESENT)
536 -                       val->intval = val->intval <= 0 ? 0 : 1;
537 +               val->intval = di->cache.flags < 0 ? 0 : 1;
538                 break;
539         case POWER_SUPPLY_PROP_CURRENT_NOW:
540 -               val->intval = bq27x00_battery_current(di);
541 +               ret = bq27x00_battery_current(di, val);
542                 break;
543         case POWER_SUPPLY_PROP_CAPACITY:
544 -               val->intval = bq27x00_battery_rsoc(di);
545 +               ret = bq27x00_simple_value(di->cache.capacity, val);
546                 break;
547         case POWER_SUPPLY_PROP_TEMP:
548 -               val->intval = bq27x00_battery_temperature(di);
549 +               ret = bq27x00_battery_temperature(di, val);
550 +               break;
551 +       case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
552 +               ret = bq27x00_simple_value(di->cache.time_to_empty, val);
553 +               break;
554 +       case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
555 +               ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
556 +               break;
557 +       case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
558 +               ret = bq27x00_simple_value(di->cache.time_to_full, val);
559 +               break;
560 +       case POWER_SUPPLY_PROP_TECHNOLOGY:
561 +               val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
562 +               break;
563 +       case POWER_SUPPLY_PROP_CHARGE_NOW:
564 +               ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
565 +               break;
566 +       case POWER_SUPPLY_PROP_CHARGE_FULL:
567 +               ret = bq27x00_simple_value(di->cache.charge_full, val);
568 +               break;
569 +       case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
570 +               ret = bq27x00_simple_value(di->charge_design_full, val);
571 +               break;
572 +       case POWER_SUPPLY_PROP_CHARGE_COUNTER:
573 +               ret = bq27x00_simple_value(di->cache.charge_counter, val);
574 +               break;
575 +       case POWER_SUPPLY_PROP_ENERGY_NOW:
576 +               ret = bq27x00_battery_energy(di, val);
577                 break;
578         default:
579                 return -EINVAL;
580         }
581  
582 -       return 0;
583 +       return ret;
584  }
585  
586 -static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
587 +static void bq27x00_external_power_changed(struct power_supply *psy)
588  {
589 +       struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
590 +
591 +       cancel_delayed_work_sync(&di->work);
592 +       schedule_delayed_work(&di->work, 0);
593 +}
594 +
595 +static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
596 +{
597 +       int ret;
598 +
599         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
600         di->bat.properties = bq27x00_battery_props;
601         di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
602         di->bat.get_property = bq27x00_battery_get_property;
603 -       di->bat.external_power_changed = NULL;
604 +       di->bat.external_power_changed = bq27x00_external_power_changed;
605 +
606 +       INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
607 +       mutex_init(&di->lock);
608 +
609 +       ret = power_supply_register(di->dev, &di->bat);
610 +       if (ret) {
611 +               dev_err(di->dev, "failed to register battery: %d\n", ret);
612 +               return ret;
613 +       }
614 +
615 +       dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
616 +
617 +       bq27x00_update(di);
618 +
619 +       return 0;
620  }
621  
622 -/*
623 - * BQ27200 specific code
624 +static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
625 +{
626 +       cancel_delayed_work_sync(&di->work);
627 +
628 +       power_supply_unregister(&di->bat);
629 +
630 +       mutex_destroy(&di->lock);
631 +}
632 +
633 +
634 +/* i2c specific code */
635 +#ifdef CONFIG_BATTERY_BQ27X00_I2C
636 +
637 +/* If the system has several batteries we need a different name for each
638 + * of them...
639   */
640 +static DEFINE_IDR(battery_id);
641 +static DEFINE_MUTEX(battery_mutex);
642  
643 -static int bq27200_read(u8 reg, int *rt_value, int b_single,
644 -                       struct bq27x00_device_info *di)
645 +static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
646  {
647 -       struct i2c_client *client = di->client;
648 -       struct i2c_msg msg[1];
649 +       struct i2c_client *client = to_i2c_client(di->dev);
650 +       struct i2c_msg msg[2];
651         unsigned char data[2];
652 -       int err;
653 +       int ret;
654  
655         if (!client->adapter)
656                 return -ENODEV;
657  
658 -       msg->addr = client->addr;
659 -       msg->flags = 0;
660 -       msg->len = 1;
661 -       msg->buf = data;
662 -
663 -       data[0] = reg;
664 -       err = i2c_transfer(client->adapter, msg, 1);
665 -
666 -       if (err >= 0) {
667 -               if (!b_single)
668 -                       msg->len = 2;
669 -               else
670 -                       msg->len = 1;
671 +       msg[0].addr = client->addr;
672 +       msg[0].flags = 0;
673 +       msg[0].buf = &reg;
674 +       msg[0].len = sizeof(reg);
675 +       msg[1].addr = client->addr;
676 +       msg[1].flags = I2C_M_RD;
677 +       msg[1].buf = data;
678 +       if (single)
679 +               msg[1].len = 1;
680 +       else
681 +               msg[1].len = 2;
682  
683 -               msg->flags = I2C_M_RD;
684 -               err = i2c_transfer(client->adapter, msg, 1);
685 -               if (err >= 0) {
686 -                       if (!b_single)
687 -                               *rt_value = get_unaligned_be16(data);
688 -                       else
689 -                               *rt_value = data[0];
690 +       ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
691 +       if (ret < 0)
692 +               return ret;
693  
694 -                       return 0;
695 -               }
696 -       }
697 -       return err;
698 +       if (!single)
699 +               ret = get_unaligned_le16(data);
700 +       else
701 +               ret = data[0];
702 +
703 +       return ret;
704  }
705  
706 -static int bq27200_battery_probe(struct i2c_client *client,
707 +static int bq27x00_battery_probe(struct i2c_client *client,
708                                  const struct i2c_device_id *id)
709  {
710         char *name;
711         struct bq27x00_device_info *di;
712 -       struct bq27x00_access_methods *bus;
713         int num;
714         int retval = 0;
715  
716 @@ -267,7 +623,7 @@ static int bq27200_battery_probe(struct 
717         if (retval < 0)
718                 return retval;
719  
720 -       name = kasprintf(GFP_KERNEL, "bq27200-%d", num);
721 +       name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
722         if (!name) {
723                 dev_err(&client->dev, "failed to allocate device name\n");
724                 retval = -ENOMEM;
725 @@ -280,37 +636,20 @@ static int bq27200_battery_probe(struct 
726                 retval = -ENOMEM;
727                 goto batt_failed_2;
728         }
729 -       di->id = num;
730  
731 -       bus = kzalloc(sizeof(*bus), GFP_KERNEL);
732 -       if (!bus) {
733 -               dev_err(&client->dev, "failed to allocate access method "
734 -                                       "data\n");
735 -               retval = -ENOMEM;
736 -               goto batt_failed_3;
737 -       }
738 -
739 -       i2c_set_clientdata(client, di);
740 +       di->id = num;
741         di->dev = &client->dev;
742 +       di->chip = id->driver_data;
743         di->bat.name = name;
744 -       bus->read = &bq27200_read;
745 -       di->bus = bus;
746 -       di->client = client;
747 -
748 -       bq27x00_powersupply_init(di);
749 +       di->bus.read = &bq27x00_read_i2c;
750  
751 -       retval = power_supply_register(&client->dev, &di->bat);
752 -       if (retval) {
753 -               dev_err(&client->dev, "failed to register battery\n");
754 -               goto batt_failed_4;
755 -       }
756 +       if (bq27x00_powersupply_init(di))
757 +               goto batt_failed_3;
758  
759 -       dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
760 +       i2c_set_clientdata(client, di);
761  
762         return 0;
763  
764 -batt_failed_4:
765 -       kfree(bus);
766  batt_failed_3:
767         kfree(di);
768  batt_failed_2:
769 @@ -323,11 +662,11 @@ batt_failed_1:
770         return retval;
771  }
772  
773 -static int bq27200_battery_remove(struct i2c_client *client)
774 +static int bq27x00_battery_remove(struct i2c_client *client)
775  {
776         struct bq27x00_device_info *di = i2c_get_clientdata(client);
777  
778 -       power_supply_unregister(&di->bat);
779 +       bq27x00_powersupply_unregister(di);
780  
781         kfree(di->bat.name);
782  
783 @@ -340,31 +679,180 @@ static int bq27200_battery_remove(struct
784         return 0;
785  }
786  
787 -/*
788 - * Module stuff
789 - */
790 -
791 -static const struct i2c_device_id bq27200_id[] = {
792 -       { "bq27200", 0 },
793 +static const struct i2c_device_id bq27x00_id[] = {
794 +       { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
795 +       { "bq27500", BQ27500 },
796         {},
797  };
798 +MODULE_DEVICE_TABLE(i2c, bq27x00_id);
799 +
800 +static struct i2c_driver bq27x00_battery_driver = {
801 +       .driver = {
802 +               .name = "bq27x00-battery",
803 +       },
804 +       .probe = bq27x00_battery_probe,
805 +       .remove = bq27x00_battery_remove,
806 +       .id_table = bq27x00_id,
807 +};
808 +
809 +static inline int bq27x00_battery_i2c_init(void)
810 +{
811 +       int ret = i2c_add_driver(&bq27x00_battery_driver);
812 +       if (ret)
813 +               printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
814 +
815 +       return ret;
816 +}
817 +
818 +static inline void bq27x00_battery_i2c_exit(void)
819 +{
820 +       i2c_del_driver(&bq27x00_battery_driver);
821 +}
822 +
823 +#else
824 +
825 +static inline int bq27x00_battery_i2c_init(void) { return 0; }
826 +static inline void bq27x00_battery_i2c_exit(void) {};
827 +
828 +#endif
829 +
830 +/* platform specific code */
831 +#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
832 +
833 +static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
834 +                       bool single)
835 +{
836 +       struct device *dev = di->dev;
837 +       struct bq27000_platform_data *pdata = dev->platform_data;
838 +       unsigned int timeout = 3;
839 +       int upper, lower;
840 +       int temp;
841 +
842 +       if (!single) {
843 +               /* Make sure the value has not changed in between reading the
844 +                * lower and the upper part */
845 +               upper = pdata->read(dev, reg + 1);
846 +               do {
847 +                       temp = upper;
848 +                       if (upper < 0)
849 +                               return upper;
850 +
851 +                       lower = pdata->read(dev, reg);
852 +                       if (lower < 0)
853 +                               return lower;
854 +
855 +                       upper = pdata->read(dev, reg + 1);
856 +               } while (temp != upper && --timeout);
857 +
858 +               if (timeout == 0)
859 +                       return -EIO;
860 +
861 +               return (upper << 8) | lower;
862 +       }
863 +
864 +       return pdata->read(dev, reg);
865 +}
866 +
867 +static int __devinit bq27000_battery_probe(struct platform_device *pdev)
868 +{
869 +       struct bq27x00_device_info *di;
870 +       struct bq27000_platform_data *pdata = pdev->dev.platform_data;
871 +       int ret;
872 +
873 +       if (!pdata) {
874 +               dev_err(&pdev->dev, "no platform_data supplied\n");
875 +               return -EINVAL;
876 +       }
877 +
878 +       if (!pdata->read) {
879 +               dev_err(&pdev->dev, "no hdq read callback supplied\n");
880 +               return -EINVAL;
881 +       }
882 +
883 +       di = kzalloc(sizeof(*di), GFP_KERNEL);
884 +       if (!di) {
885 +               dev_err(&pdev->dev, "failed to allocate device info data\n");
886 +               return -ENOMEM;
887 +       }
888 +
889 +       platform_set_drvdata(pdev, di);
890 +
891 +       di->dev = &pdev->dev;
892 +       di->chip = BQ27000;
893 +
894 +       di->bat.name = pdata->name ?: dev_name(&pdev->dev);
895 +       di->bus.read = &bq27000_read_platform;
896  
897 -static struct i2c_driver bq27200_battery_driver = {
898 +       ret = bq27x00_powersupply_init(di);
899 +       if (ret)
900 +               goto err_free;
901 +
902 +       return 0;
903 +
904 +err_free:
905 +       platform_set_drvdata(pdev, NULL);
906 +       kfree(di);
907 +
908 +       return ret;
909 +}
910 +
911 +static int __devexit bq27000_battery_remove(struct platform_device *pdev)
912 +{
913 +       struct bq27x00_device_info *di = platform_get_drvdata(pdev);
914 +
915 +       bq27x00_powersupply_unregister(di);
916 +
917 +       platform_set_drvdata(pdev, NULL);
918 +       kfree(di);
919 +
920 +       return 0;
921 +}
922 +
923 +static struct platform_driver bq27000_battery_driver = {
924 +       .probe  = bq27000_battery_probe,
925 +       .remove = __devexit_p(bq27000_battery_remove),
926         .driver = {
927 -               .name = "bq27200-battery",
928 +               .name = "bq27000-battery",
929 +               .owner = THIS_MODULE,
930         },
931 -       .probe = bq27200_battery_probe,
932 -       .remove = bq27200_battery_remove,
933 -       .id_table = bq27200_id,
934  };
935  
936 +static inline int bq27x00_battery_platform_init(void)
937 +{
938 +       int ret = platform_driver_register(&bq27000_battery_driver);
939 +       if (ret)
940 +               printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
941 +
942 +       return ret;
943 +}
944 +
945 +static inline void bq27x00_battery_platform_exit(void)
946 +{
947 +       platform_driver_unregister(&bq27000_battery_driver);
948 +}
949 +
950 +#else
951 +
952 +static inline int bq27x00_battery_platform_init(void) { return 0; }
953 +static inline void bq27x00_battery_platform_exit(void) {};
954 +
955 +#endif
956 +
957 +/*
958 + * Module stuff
959 + */
960 +
961  static int __init bq27x00_battery_init(void)
962  {
963         int ret;
964  
965 -       ret = i2c_add_driver(&bq27200_battery_driver);
966 +       ret = bq27x00_battery_i2c_init();
967 +       if (ret)
968 +               return ret;
969 +
970 +       ret = bq27x00_battery_platform_init();
971         if (ret)
972 -               printk(KERN_ERR "Unable to register BQ27200 driver\n");
973 +               bq27x00_battery_i2c_exit();
974  
975         return ret;
976  }
977 @@ -372,7 +860,8 @@ module_init(bq27x00_battery_init);
978  
979  static void __exit bq27x00_battery_exit(void)
980  {
981 -       i2c_del_driver(&bq27200_battery_driver);
982 +       bq27x00_battery_platform_exit();
983 +       bq27x00_battery_i2c_exit();
984  }
985  module_exit(bq27x00_battery_exit);
986