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