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