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