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-31 22:08:03.185443335 +0100
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>
10 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
13 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 + * http://focus.ti.com/docs/prod/folders/print/bq27000.html
20 + * http://focus.ti.com/docs/prod/folders/print/bq27500.html
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>
38 +#include <linux/power/bq27x00_battery.h>
40 -#define DRIVER_VERSION "1.0.0"
41 +#define DRIVER_VERSION "1.2.0"
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 */
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 */
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 */
72 -/* If the system has several batteries we need a different name for each
75 -static DEFINE_IDR(battery_id);
76 -static DEFINE_MUTEX(battery_mutex);
77 +#define BQ27000_RS 20 /* Resistor sense */
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);
86 +enum bq27x00_chip { BQ27000, BQ27500 };
88 +struct bq27x00_reg_cache {
91 + int time_to_empty_avg;
100 +struct bq27x00_reg_device {
101 + struct miscdevice miscdev;
102 + struct bq27x00_device_info *di;
103 + struct list_head list;
106 struct bq27x00_device_info {
108 + struct bq27x00_reg_device *regdev;
114 - struct bq27x00_access_methods *bus;
115 + enum bq27x00_chip chip;
117 + struct bq27x00_reg_cache cache;
118 + int charge_design_full;
120 + unsigned long last_update;
121 + struct delayed_work work;
123 struct power_supply bat;
125 - struct i2c_client *client;
126 + struct bq27x00_access_methods bus;
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,
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");
155 +/* If the system has several batteries we need a different name for each
158 +static DEFINE_IDR(battery_id);
159 +static DEFINE_MUTEX(battery_mutex);
160 +static LIST_HEAD(battery_list);
163 * Common code for BQ27x00 devices
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,
172 + return di->bus.read(di, reg, single);
176 + * Return the battery Relative State-of-Charge
177 + * Or < 0 if something fails.
179 +static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
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);
188 + rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
192 + dev_dbg(di->dev, "error reading relative State-of-Charge\n");
198 - * Return the battery temperature in Celcius degrees
199 + * Return a battery charge value in µAh
200 * Or < 0 if something fails.
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)
209 - ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
211 + charge = bq27x00_read(di, reg, false);
213 + dev_dbg(di->dev, "error reading charge register %02x: %d\n", reg, charge);
217 + if (di->chip == BQ27500)
220 + charge = charge * 3570 / BQ27000_RS;
226 + * Return the battery Nominal available capaciy in µAh
227 + * Or < 0 if something fails.
229 +static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
231 + return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
235 + * Return the battery Last measured discharge in µAh
236 + * Or < 0 if something fails.
238 +static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
240 + return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
244 + * Return the battery Initial last measured discharge in µAh
245 + * Or < 0 if something fails.
247 +static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
251 + if (di->chip == BQ27500)
252 + ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
254 + ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
257 + dev_dbg(di->dev, "error reading initial last measured discharge\n");
261 + if (di->chip == BQ27500)
264 + ilmd = ilmd * 256 * 3570 / BQ27000_RS;
270 + * Return the battery Available energy in µWh
271 + * Or < 0 if something fails.
273 +static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
277 + ae = bq27x00_read(di, BQ27x00_REG_AE, false);
279 + dev_dbg(di->dev, "error reading available energy\n");
283 + if (di->chip == BQ27500)
286 + ae = ae * 29200 / BQ27000_RS;
292 + * Return the battery temperature in tenths of degree Celsius
293 + * Or < 0 if something fails.
295 +static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
299 + temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
301 dev_err(di->dev, "error reading temperature\n");
306 - return (temp >> 2) - 273;
307 + if (di->chip == BQ27500)
310 + temp = ((temp * 5) - 5463) / 2;
316 - * Return the battery Voltage in milivolts
317 + * Return the battery Cycle count total
318 * Or < 0 if something fails.
320 -static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
321 +static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
327 - ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
329 - dev_err(di->dev, "error reading voltage\n");
331 + cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
333 + dev_err(di->dev, "error reading cycle count total\n");
339 + * Read a time register.
340 + * Return < 0 if something fails.
342 +static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
346 + tval = bq27x00_read(di, reg, false);
348 + dev_dbg(di->dev, "error reading time register %02x: %d\n", reg, tval);
358 +static void bq27x00_update(struct bq27x00_device_info *di)
360 + struct bq27x00_reg_cache cache = {0, };
361 + bool is_bq27500 = di->chip == BQ27500;
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;
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);
381 + cache.temperature = bq27x00_battery_read_temperature(di);
382 + cache.cycle_count = bq27x00_battery_read_cyct(di);
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);
390 + if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
392 + power_supply_changed(&di->bat);
395 + di->last_update = jiffies;
398 +static void bq27x00_battery_poll(struct work_struct *work)
400 + struct bq27x00_device_info *di =
401 + container_of(work, struct bq27x00_device_info, work.work);
403 + bq27x00_update(di);
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);
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.
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)
428 - ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
430 + curr = bq27x00_read(di, BQ27x00_REG_AI, false);
432 dev_err(di->dev, "error reading current\n");
437 + if (di->chip == BQ27500) {
438 + /* bq27500 returns signed value */
439 + val->intval = (int)((s16)curr) * 1000;
441 + flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
442 + if (flags & BQ27000_FLAG_CHGS) {
443 + dev_dbg(di->dev, "negative current!\n");
447 + val->intval = curr * 3570 / BQ27000_RS;
453 +static int bq27x00_battery_status(struct bq27x00_device_info *di,
454 + union power_supply_propval *val)
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;
468 + status = POWER_SUPPLY_STATUS_UNKNOWN;
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;
477 + status = POWER_SUPPLY_STATUS_DISCHARGING;
479 - ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
481 - dev_err(di->dev, "error reading flags\n");
484 - if ((flags & (1 << 7)) != 0) {
485 - dev_dbg(di->dev, "negative current!\n");
488 + val->intval = status;
493 +static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
494 + union power_supply_propval *val)
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;
506 + level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
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;
515 + level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
519 + val->intval = level;
525 - * Return the battery Relative State-of-Charge
526 + * Return the battery Voltage in milivolts
527 * Or < 0 if something fails.
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)
537 - ret = bq27x00_read(BQ27x00_REG_RSOC, &rsoc, 1, di);
539 - dev_err(di->dev, "error reading relative State-of-Charge\n");
541 + volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
543 + dev_err(di->dev, "error reading voltage\n");
548 + val->intval = volt * 1000;
553 +static int bq27x00_simple_value(int value,
554 + union power_supply_propval *val)
559 + val->intval = value;
564 #define to_bq27x00_device_info(x) container_of((x), \
565 @@ -171,89 +508,278 @@ static int bq27x00_battery_get_property(
566 enum power_supply_property psp,
567 union power_supply_propval *val)
570 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
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);
577 + mutex_unlock(&di->lock);
579 + if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
583 + case POWER_SUPPLY_PROP_STATUS:
584 + ret = bq27x00_battery_status(di, val);
586 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
587 + ret = bq27x00_battery_voltage(di, val);
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;
595 case POWER_SUPPLY_PROP_CURRENT_NOW:
596 - val->intval = bq27x00_battery_current(di);
597 + ret = bq27x00_battery_current(di, val);
599 case POWER_SUPPLY_PROP_CAPACITY:
600 - val->intval = bq27x00_battery_rsoc(di);
601 + ret = bq27x00_simple_value(di->cache.capacity, val);
603 + case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
604 + ret = bq27x00_battery_capacity_level(di, val);
606 case POWER_SUPPLY_PROP_TEMP:
607 - val->intval = bq27x00_battery_temperature(di);
608 + ret = bq27x00_simple_value(di->cache.temperature, val);
610 + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
611 + ret = bq27x00_simple_value(di->cache.time_to_empty, val);
613 + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
614 + ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
616 + case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
617 + ret = bq27x00_simple_value(di->cache.time_to_full, val);
619 + case POWER_SUPPLY_PROP_TECHNOLOGY:
620 + val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
622 + case POWER_SUPPLY_PROP_CHARGE_NOW:
623 + ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
625 + case POWER_SUPPLY_PROP_CHARGE_FULL:
626 + ret = bq27x00_simple_value(di->cache.charge_full, val);
628 + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
629 + ret = bq27x00_simple_value(di->charge_design_full, val);
631 + case POWER_SUPPLY_PROP_CYCLE_COUNT:
632 + ret = bq27x00_simple_value(di->cache.cycle_count, val);
634 + case POWER_SUPPLY_PROP_ENERGY_NOW:
635 + ret = bq27x00_simple_value(di->cache.energy, val);
644 +static void bq27x00_external_power_changed(struct power_supply *psy)
646 + struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
648 + cancel_delayed_work_sync(&di->work);
649 + schedule_delayed_work(&di->work, 0);
652 +/* Code for register device access */
654 +static struct bq27x00_reg_device * bq27x00_battery_reg_find_device(int minor)
656 + struct bq27x00_reg_device *regdev;
658 + list_for_each_entry(regdev, &battery_list, list)
659 + if (regdev->miscdev.minor == minor)
665 +static long bq27x00_battery_reg_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
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);
675 + if (cmd != BQ27X00_READ_REG)
678 + ret = copy_from_user(¶m, (void __user *)arg, sizeof(param));
682 + param.ret = bq27x00_read(regdev->di, param.reg, param.single);
684 + ret = copy_to_user((void __user *)arg, ¶m, sizeof(param));
691 +static int bq27x00_battery_reg_open(struct inode *inode, struct file *file)
693 + if (!try_module_get(THIS_MODULE))
699 +static int bq27x00_battery_reg_release(struct inode *inode, struct file *file)
701 + module_put(THIS_MODULE);
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,
712 +static int bq27x00_battery_reg_init(struct bq27x00_device_info *di)
714 + struct bq27x00_reg_device *regdev;
719 + regdev = kzalloc(sizeof *regdev, GFP_KERNEL);
723 + regdev->miscdev.minor = MISC_DYNAMIC_MINOR;
724 + regdev->miscdev.name = di->bat.name;
725 + regdev->miscdev.fops = &bq27x00_reg_fileops;
727 + ret = misc_register(®dev->miscdev);
734 + di->regdev = regdev;
736 + INIT_LIST_HEAD(®dev->list);
738 + mutex_lock(&battery_mutex);
739 + list_add(®dev->list, &battery_list);
740 + mutex_unlock(&battery_mutex);
745 -static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
746 +static void bq27x00_battery_reg_exit(struct bq27x00_device_info *di)
751 + misc_deregister(&di->regdev->miscdev);
753 + mutex_lock(&battery_mutex);
754 + list_del(&di->regdev->list);
755 + mutex_unlock(&battery_mutex);
761 +static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
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;
772 + INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
773 + mutex_init(&di->lock);
775 + ret = power_supply_register(di->dev, &di->bat);
777 + dev_err(di->dev, "failed to register battery: %d\n", ret);
781 + dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
783 + bq27x00_update(di);
784 + bq27x00_battery_reg_init(di);
790 - * BQ27200 specific code
792 +static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
794 + /* power_supply_unregister call bq27x00_battery_get_property which call bq27x00_battery_poll */
795 + /* make sure that bq27x00_battery_poll will not call schedule_delayed_work again after unregister (which cause OOPS) */
798 + cancel_delayed_work_sync(&di->work);
800 + bq27x00_battery_reg_exit(di);
802 -static int bq27200_read(u8 reg, int *rt_value, int b_single,
803 - struct bq27x00_device_info *di)
804 + power_supply_unregister(&di->bat);
806 + mutex_destroy(&di->lock);
810 +/* i2c specific code */
811 +#ifdef CONFIG_BATTERY_BQ27X00_I2C
813 +static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
815 - struct i2c_client *client = di->client;
816 - struct i2c_msg msg[1];
817 + struct i2c_client *client = to_i2c_client(di->dev);
818 + struct i2c_msg msg[2];
819 unsigned char data[2];
823 if (!client->adapter)
826 - msg->addr = client->addr;
832 - err = i2c_transfer(client->adapter, msg, 1);
839 + msg[0].addr = client->addr;
842 + msg[0].len = sizeof(reg);
843 + msg[1].addr = client->addr;
844 + msg[1].flags = I2C_M_RD;
851 + mutex_lock(&battery_mutex);
852 + ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
853 + mutex_unlock(&battery_mutex);
857 - msg->flags = I2C_M_RD;
858 - err = i2c_transfer(client->adapter, msg, 1);
861 - *rt_value = get_unaligned_be16(data);
863 - *rt_value = data[0];
865 + ret = get_unaligned_le16(data);
876 -static int bq27200_battery_probe(struct i2c_client *client,
877 +static int bq27x00_battery_probe(struct i2c_client *client,
878 const struct i2c_device_id *id)
881 struct bq27x00_device_info *di;
882 - struct bq27x00_access_methods *bus;
886 @@ -267,7 +793,7 @@ static int bq27200_battery_probe(struct
890 - name = kasprintf(GFP_KERNEL, "bq27200-%d", num);
891 + name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
893 dev_err(&client->dev, "failed to allocate device name\n");
895 @@ -280,37 +806,20 @@ static int bq27200_battery_probe(struct
901 - bus = kzalloc(sizeof(*bus), GFP_KERNEL);
903 - dev_err(&client->dev, "failed to allocate access method "
906 - goto batt_failed_3;
909 - i2c_set_clientdata(client, di);
911 di->dev = &client->dev;
912 + di->chip = id->driver_data;
914 - bus->read = &bq27200_read;
916 - di->client = client;
917 + di->bus.read = &bq27x00_read_i2c;
919 - bq27x00_powersupply_init(di);
921 - retval = power_supply_register(&client->dev, &di->bat);
923 - dev_err(&client->dev, "failed to register battery\n");
924 - goto batt_failed_4;
926 + if (bq27x00_powersupply_init(di))
927 + goto batt_failed_3;
929 - dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
930 + i2c_set_clientdata(client, di);
939 @@ -323,11 +832,11 @@ batt_failed_1:
943 -static int bq27200_battery_remove(struct i2c_client *client)
944 +static int bq27x00_battery_remove(struct i2c_client *client)
946 struct bq27x00_device_info *di = i2c_get_clientdata(client);
948 - power_supply_unregister(&di->bat);
949 + bq27x00_powersupply_unregister(di);
953 @@ -340,31 +849,180 @@ static int bq27200_battery_remove(struct
961 -static const struct i2c_device_id bq27200_id[] = {
963 +static const struct i2c_device_id bq27x00_id[] = {
964 + { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
965 + { "bq27500", BQ27500 },
968 +MODULE_DEVICE_TABLE(i2c, bq27x00_id);
970 +static struct i2c_driver bq27x00_battery_driver = {
972 + .name = "bq27x00-battery",
974 + .probe = bq27x00_battery_probe,
975 + .remove = bq27x00_battery_remove,
976 + .id_table = bq27x00_id,
979 +static inline int bq27x00_battery_i2c_init(void)
981 + int ret = i2c_add_driver(&bq27x00_battery_driver);
983 + printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
988 +static inline void bq27x00_battery_i2c_exit(void)
990 + i2c_del_driver(&bq27x00_battery_driver);
995 +static inline int bq27x00_battery_i2c_init(void) { return 0; }
996 +static inline void bq27x00_battery_i2c_exit(void) {};
1000 +/* platform specific code */
1001 +#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
1003 +static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
1006 + struct device *dev = di->dev;
1007 + struct bq27000_platform_data *pdata = dev->platform_data;
1008 + unsigned int timeout = 3;
1013 + /* Make sure the value has not changed in between reading the
1014 + * lower and the upper part */
1015 + upper = pdata->read(dev, reg + 1);
1021 + lower = pdata->read(dev, reg);
1025 + upper = pdata->read(dev, reg + 1);
1026 + } while (temp != upper && --timeout);
1031 + return (upper << 8) | lower;
1034 + return pdata->read(dev, reg);
1037 +static int __devinit bq27000_battery_probe(struct platform_device *pdev)
1039 + struct bq27x00_device_info *di;
1040 + struct bq27000_platform_data *pdata = pdev->dev.platform_data;
1044 + dev_err(&pdev->dev, "no platform_data supplied\n");
1048 + if (!pdata->read) {
1049 + dev_err(&pdev->dev, "no hdq read callback supplied\n");
1053 -static struct i2c_driver bq27200_battery_driver = {
1054 + di = kzalloc(sizeof(*di), GFP_KERNEL);
1056 + dev_err(&pdev->dev, "failed to allocate device info data\n");
1060 + platform_set_drvdata(pdev, di);
1062 + di->dev = &pdev->dev;
1063 + di->chip = BQ27000;
1065 + di->bat.name = pdata->name ?: dev_name(&pdev->dev);
1066 + di->bus.read = &bq27000_read_platform;
1068 + ret = bq27x00_powersupply_init(di);
1075 + platform_set_drvdata(pdev, NULL);
1081 +static int __devexit bq27000_battery_remove(struct platform_device *pdev)
1083 + struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1085 + bq27x00_powersupply_unregister(di);
1087 + platform_set_drvdata(pdev, NULL);
1093 +static struct platform_driver bq27000_battery_driver = {
1094 + .probe = bq27000_battery_probe,
1095 + .remove = __devexit_p(bq27000_battery_remove),
1097 - .name = "bq27200-battery",
1098 + .name = "bq27000-battery",
1099 + .owner = THIS_MODULE,
1101 - .probe = bq27200_battery_probe,
1102 - .remove = bq27200_battery_remove,
1103 - .id_table = bq27200_id,
1106 +static inline int bq27x00_battery_platform_init(void)
1108 + int ret = platform_driver_register(&bq27000_battery_driver);
1110 + printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
1115 +static inline void bq27x00_battery_platform_exit(void)
1117 + platform_driver_unregister(&bq27000_battery_driver);
1122 +static inline int bq27x00_battery_platform_init(void) { return 0; }
1123 +static inline void bq27x00_battery_platform_exit(void) {};
1131 static int __init bq27x00_battery_init(void)
1135 - ret = i2c_add_driver(&bq27200_battery_driver);
1136 + ret = bq27x00_battery_i2c_init();
1140 + ret = bq27x00_battery_platform_init();
1142 - printk(KERN_ERR "Unable to register BQ27200 driver\n");
1143 + bq27x00_battery_i2c_exit();
1147 @@ -372,7 +1030,8 @@ module_init(bq27x00_battery_init);
1149 static void __exit bq27x00_battery_exit(void)
1151 - i2c_del_driver(&bq27200_battery_driver);
1152 + bq27x00_battery_platform_exit();
1153 + bq27x00_battery_i2c_exit();
1155 module_exit(bq27x00_battery_exit);