2 * BQ27x00 battery driver
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
7 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
9 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
11 * This package is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
24 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
25 * http://www.ti.com/product/bq27425-g1
26 * http://www.ti.com/product/BQ27742-G1
29 #include <linux/device.h>
30 #include <linux/module.h>
31 #include <linux/param.h>
32 #include <linux/jiffies.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <linux/platform_device.h>
36 #include <linux/power_supply.h>
37 #include <linux/idr.h>
38 #include <linux/i2c.h>
39 #include <linux/slab.h>
40 #include <asm/unaligned.h>
42 #include <linux/power/bq27x00_battery.h>
44 #define DRIVER_VERSION "1.2.0"
46 #define BQ27x00_REG_TEMP 0x06
47 #define BQ27x00_REG_VOLT 0x08
48 #define BQ27x00_REG_AI 0x14
49 #define BQ27x00_REG_FLAGS 0x0A
50 #define BQ27x00_REG_TTE 0x16
51 #define BQ27x00_REG_TTF 0x18
52 #define BQ27x00_REG_TTECP 0x26
53 #define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
54 #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
55 #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
56 #define BQ27x00_REG_AE 0x22 /* Available energy */
57 #define BQ27x00_POWER_AVG 0x24
59 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
60 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
61 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
62 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
63 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
64 #define BQ27000_FLAG_FC BIT(5)
65 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
67 #define BQ27500_REG_SOC 0x2C
68 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
69 #define BQ27500_FLAG_DSC BIT(0)
70 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
71 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
72 #define BQ27500_FLAG_FC BIT(9)
73 #define BQ27500_FLAG_OTC BIT(15)
75 #define BQ27742_POWER_AVG 0x76
77 /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
78 #define BQ27425_REG_OFFSET 0x04
79 #define BQ27425_REG_SOC 0x18 /* Register address plus offset */
81 #define BQ27000_RS 20 /* Resistor sense */
82 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
84 struct bq27x00_device_info;
85 struct bq27x00_access_methods {
86 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
89 enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742};
91 struct bq27x00_reg_cache {
94 int time_to_empty_avg;
105 struct bq27x00_device_info {
108 enum bq27x00_chip chip;
110 struct bq27x00_reg_cache cache;
111 int charge_design_full;
113 unsigned long last_update;
114 struct delayed_work work;
116 struct power_supply bat;
118 struct bq27x00_access_methods bus;
123 static enum power_supply_property bq27x00_battery_props[] = {
124 POWER_SUPPLY_PROP_STATUS,
125 POWER_SUPPLY_PROP_PRESENT,
126 POWER_SUPPLY_PROP_VOLTAGE_NOW,
127 POWER_SUPPLY_PROP_CURRENT_NOW,
128 POWER_SUPPLY_PROP_CAPACITY,
129 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
130 POWER_SUPPLY_PROP_TEMP,
131 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
132 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
133 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
134 POWER_SUPPLY_PROP_TECHNOLOGY,
135 POWER_SUPPLY_PROP_CHARGE_FULL,
136 POWER_SUPPLY_PROP_CHARGE_NOW,
137 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
138 POWER_SUPPLY_PROP_CYCLE_COUNT,
139 POWER_SUPPLY_PROP_ENERGY_NOW,
140 POWER_SUPPLY_PROP_POWER_AVG,
141 POWER_SUPPLY_PROP_HEALTH,
144 static enum power_supply_property bq27425_battery_props[] = {
145 POWER_SUPPLY_PROP_STATUS,
146 POWER_SUPPLY_PROP_PRESENT,
147 POWER_SUPPLY_PROP_VOLTAGE_NOW,
148 POWER_SUPPLY_PROP_CURRENT_NOW,
149 POWER_SUPPLY_PROP_CAPACITY,
150 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
151 POWER_SUPPLY_PROP_TEMP,
152 POWER_SUPPLY_PROP_TECHNOLOGY,
153 POWER_SUPPLY_PROP_CHARGE_FULL,
154 POWER_SUPPLY_PROP_CHARGE_NOW,
155 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
158 static enum power_supply_property bq27742_battery_props[] = {
159 POWER_SUPPLY_PROP_STATUS,
160 POWER_SUPPLY_PROP_PRESENT,
161 POWER_SUPPLY_PROP_VOLTAGE_NOW,
162 POWER_SUPPLY_PROP_CURRENT_NOW,
163 POWER_SUPPLY_PROP_CAPACITY,
164 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
165 POWER_SUPPLY_PROP_TEMP,
166 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
167 POWER_SUPPLY_PROP_TECHNOLOGY,
168 POWER_SUPPLY_PROP_CHARGE_FULL,
169 POWER_SUPPLY_PROP_CHARGE_NOW,
170 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
171 POWER_SUPPLY_PROP_CYCLE_COUNT,
172 POWER_SUPPLY_PROP_POWER_AVG,
173 POWER_SUPPLY_PROP_HEALTH,
176 static unsigned int poll_interval = 360;
177 module_param(poll_interval, uint, 0644);
178 MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
179 "0 disables polling");
182 * Common code for BQ27x00 devices
185 static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
188 if (di->chip == BQ27425)
189 return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
190 return di->bus.read(di, reg, single);
194 * Higher versions of the chip like BQ27425 and BQ27500
195 * differ from BQ27000 and BQ27200 in calculation of certain
196 * parameters. Hence we need to check for the chip type.
198 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di)
200 if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742)
206 * Return the battery Relative State-of-Charge
207 * Or < 0 if something fails.
209 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
213 if (di->chip == BQ27500 || di->chip == BQ27742)
214 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
215 else if (di->chip == BQ27425)
216 rsoc = bq27x00_read(di, BQ27425_REG_SOC, false);
218 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
221 dev_dbg(di->dev, "error reading relative State-of-Charge\n");
227 * Return a battery charge value in µAh
228 * Or < 0 if something fails.
230 static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
234 charge = bq27x00_read(di, reg, false);
236 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
241 if (bq27xxx_is_chip_version_higher(di))
244 charge = charge * 3570 / BQ27000_RS;
250 * Return the battery Nominal available capaciy in µAh
251 * Or < 0 if something fails.
253 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
256 bool is_bq27500 = di->chip == BQ27500;
257 bool is_bq27742 = di->chip == BQ27742;
258 bool is_higher = bq27xxx_is_chip_version_higher(di);
259 bool flags_1b = !(is_bq27500 || is_bq27742);
261 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
262 if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
265 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
269 * Return the battery Last measured discharge in µAh
270 * Or < 0 if something fails.
272 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
274 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
278 * Return the battery Initial last measured discharge in µAh
279 * Or < 0 if something fails.
281 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
285 if (bq27xxx_is_chip_version_higher(di))
286 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
288 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
291 dev_dbg(di->dev, "error reading initial last measured discharge\n");
295 if (bq27xxx_is_chip_version_higher(di))
298 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
304 * Return the battery Available energy in µWh
305 * Or < 0 if something fails.
307 static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
311 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
313 dev_dbg(di->dev, "error reading available energy\n");
317 if (di->chip == BQ27500)
320 ae = ae * 29200 / BQ27000_RS;
326 * Return the battery temperature in tenths of degree Kelvin
327 * Or < 0 if something fails.
329 static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di)
333 temp = bq27x00_read(di, BQ27x00_REG_TEMP, false);
335 dev_err(di->dev, "error reading temperature\n");
339 if (!bq27xxx_is_chip_version_higher(di))
346 * Return the battery Cycle count total
347 * Or < 0 if something fails.
349 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
353 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
355 dev_err(di->dev, "error reading cycle count total\n");
361 * Read a time register.
362 * Return < 0 if something fails.
364 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
368 tval = bq27x00_read(di, reg, false);
370 dev_dbg(di->dev, "error reading time register %02x: %d\n",
382 * Read a power avg register.
383 * Return < 0 if something fails.
385 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg)
389 tval = bq27x00_read(di, reg, false);
391 dev_err(di->dev, "error reading power avg rgister %02x: %d\n",
396 if (di->chip == BQ27500)
399 return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS;
403 * Read flag register.
404 * Return < 0 if something fails.
406 static int bq27x00_battery_read_health(struct bq27x00_device_info *di)
410 tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
412 dev_err(di->dev, "error reading flag register:%d\n", tval);
416 if ((di->chip == BQ27500)) {
417 if (tval & BQ27500_FLAG_SOCF)
418 tval = POWER_SUPPLY_HEALTH_DEAD;
419 else if (tval & BQ27500_FLAG_OTC)
420 tval = POWER_SUPPLY_HEALTH_OVERHEAT;
422 tval = POWER_SUPPLY_HEALTH_GOOD;
425 if (tval & BQ27000_FLAG_EDV1)
426 tval = POWER_SUPPLY_HEALTH_DEAD;
428 tval = POWER_SUPPLY_HEALTH_GOOD;
435 static void bq27x00_update(struct bq27x00_device_info *di)
437 struct bq27x00_reg_cache cache = {0, };
438 bool is_bq27500 = di->chip == BQ27500;
439 bool is_bq27425 = di->chip == BQ27425;
440 bool is_bq27742 = di->chip == BQ27742;
441 bool flags_1b = !(is_bq27500 || is_bq27742);
443 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b);
444 if ((cache.flags & 0xff) == 0xff)
447 if (cache.flags >= 0) {
448 if (!is_bq27500 && !is_bq27425 && !is_bq27742
449 && (cache.flags & BQ27000_FLAG_CI)) {
450 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
451 cache.capacity = -ENODATA;
452 cache.energy = -ENODATA;
453 cache.time_to_empty = -ENODATA;
454 cache.time_to_empty_avg = -ENODATA;
455 cache.time_to_full = -ENODATA;
456 cache.charge_full = -ENODATA;
457 cache.health = -ENODATA;
459 cache.capacity = bq27x00_battery_read_rsoc(di);
461 cache.time_to_empty =
462 bq27x00_battery_read_time(di,
464 else if (!is_bq27425) {
465 cache.energy = bq27x00_battery_read_energy(di);
466 cache.time_to_empty =
467 bq27x00_battery_read_time(di,
469 cache.time_to_empty_avg =
470 bq27x00_battery_read_time(di,
473 bq27x00_battery_read_time(di,
476 cache.charge_full = bq27x00_battery_read_lmd(di);
477 cache.health = bq27x00_battery_read_health(di);
479 cache.temperature = bq27x00_battery_read_temperature(di);
481 cache.cycle_count = bq27x00_battery_read_cyct(di);
484 bq27x00_battery_read_pwr_avg(di,
488 bq27x00_battery_read_pwr_avg(di,
491 /* We only have to read charge design full once */
492 if (di->charge_design_full <= 0)
493 di->charge_design_full = bq27x00_battery_read_ilmd(di);
496 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
498 power_supply_changed(&di->bat);
501 di->last_update = jiffies;
504 static void bq27x00_battery_poll(struct work_struct *work)
506 struct bq27x00_device_info *di =
507 container_of(work, struct bq27x00_device_info, work.work);
511 if (poll_interval > 0) {
512 /* The timer does not have to be accurate. */
513 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
514 schedule_delayed_work(&di->work, poll_interval * HZ);
519 * Return the battery average current in µA
520 * Note that current can be negative signed as well
521 * Or 0 if something fails.
523 static int bq27x00_battery_current(struct bq27x00_device_info *di,
524 union power_supply_propval *val)
529 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
531 dev_err(di->dev, "error reading current\n");
535 if (bq27xxx_is_chip_version_higher(di)) {
536 /* bq27500 returns signed value */
537 val->intval = (int)((s16)curr) * 1000;
539 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
540 if (flags & BQ27000_FLAG_CHGS) {
541 dev_dbg(di->dev, "negative current!\n");
545 val->intval = curr * 3570 / BQ27000_RS;
551 static int bq27x00_battery_status(struct bq27x00_device_info *di,
552 union power_supply_propval *val)
556 if (bq27xxx_is_chip_version_higher(di)) {
557 if (di->cache.flags & BQ27500_FLAG_FC)
558 status = POWER_SUPPLY_STATUS_FULL;
559 else if (di->cache.flags & BQ27500_FLAG_DSC)
560 status = POWER_SUPPLY_STATUS_DISCHARGING;
562 status = POWER_SUPPLY_STATUS_CHARGING;
564 if (di->cache.flags & BQ27000_FLAG_FC)
565 status = POWER_SUPPLY_STATUS_FULL;
566 else if (di->cache.flags & BQ27000_FLAG_CHGS)
567 status = POWER_SUPPLY_STATUS_CHARGING;
568 else if (power_supply_am_i_supplied(&di->bat))
569 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
571 status = POWER_SUPPLY_STATUS_DISCHARGING;
574 val->intval = status;
579 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
580 union power_supply_propval *val)
584 if (bq27xxx_is_chip_version_higher(di)) {
585 if (di->cache.flags & BQ27500_FLAG_FC)
586 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
587 else if (di->cache.flags & BQ27500_FLAG_SOC1)
588 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
589 else if (di->cache.flags & BQ27500_FLAG_SOCF)
590 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
592 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
594 if (di->cache.flags & BQ27000_FLAG_FC)
595 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
596 else if (di->cache.flags & BQ27000_FLAG_EDV1)
597 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
598 else if (di->cache.flags & BQ27000_FLAG_EDVF)
599 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
601 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
610 * Return the battery Voltage in millivolts
611 * Or < 0 if something fails.
613 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
614 union power_supply_propval *val)
618 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
620 dev_err(di->dev, "error reading voltage\n");
624 val->intval = volt * 1000;
629 static int bq27x00_simple_value(int value,
630 union power_supply_propval *val)
640 #define to_bq27x00_device_info(x) container_of((x), \
641 struct bq27x00_device_info, bat);
643 static int bq27x00_battery_get_property(struct power_supply *psy,
644 enum power_supply_property psp,
645 union power_supply_propval *val)
648 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
650 mutex_lock(&di->lock);
651 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
652 cancel_delayed_work_sync(&di->work);
653 bq27x00_battery_poll(&di->work.work);
655 mutex_unlock(&di->lock);
657 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
661 case POWER_SUPPLY_PROP_STATUS:
662 ret = bq27x00_battery_status(di, val);
664 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
665 ret = bq27x00_battery_voltage(di, val);
667 case POWER_SUPPLY_PROP_PRESENT:
668 val->intval = di->cache.flags < 0 ? 0 : 1;
670 case POWER_SUPPLY_PROP_CURRENT_NOW:
671 ret = bq27x00_battery_current(di, val);
673 case POWER_SUPPLY_PROP_CAPACITY:
674 ret = bq27x00_simple_value(di->cache.capacity, val);
676 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
677 ret = bq27x00_battery_capacity_level(di, val);
679 case POWER_SUPPLY_PROP_TEMP:
680 ret = bq27x00_simple_value(di->cache.temperature, val);
684 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
685 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
687 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
688 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
690 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
691 ret = bq27x00_simple_value(di->cache.time_to_full, val);
693 case POWER_SUPPLY_PROP_TECHNOLOGY:
694 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
696 case POWER_SUPPLY_PROP_CHARGE_NOW:
697 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
699 case POWER_SUPPLY_PROP_CHARGE_FULL:
700 ret = bq27x00_simple_value(di->cache.charge_full, val);
702 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
703 ret = bq27x00_simple_value(di->charge_design_full, val);
705 case POWER_SUPPLY_PROP_CYCLE_COUNT:
706 ret = bq27x00_simple_value(di->cache.cycle_count, val);
708 case POWER_SUPPLY_PROP_ENERGY_NOW:
709 ret = bq27x00_simple_value(di->cache.energy, val);
711 case POWER_SUPPLY_PROP_POWER_AVG:
712 ret = bq27x00_simple_value(di->cache.power_avg, val);
714 case POWER_SUPPLY_PROP_HEALTH:
715 ret = bq27x00_simple_value(di->cache.health, val);
724 static void bq27x00_external_power_changed(struct power_supply *psy)
726 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
728 cancel_delayed_work_sync(&di->work);
729 schedule_delayed_work(&di->work, 0);
732 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
736 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
737 if (di->chip == BQ27425) {
738 di->bat.properties = bq27425_battery_props;
739 di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props);
740 } else if (di->chip == BQ27742) {
741 di->bat.properties = bq27742_battery_props;
742 di->bat.num_properties = ARRAY_SIZE(bq27742_battery_props);
744 di->bat.properties = bq27x00_battery_props;
745 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
747 di->bat.get_property = bq27x00_battery_get_property;
748 di->bat.external_power_changed = bq27x00_external_power_changed;
750 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
751 mutex_init(&di->lock);
753 ret = power_supply_register(di->dev, &di->bat);
755 dev_err(di->dev, "failed to register battery: %d\n", ret);
759 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
766 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
769 * power_supply_unregister call bq27x00_battery_get_property which
770 * call bq27x00_battery_poll.
771 * Make sure that bq27x00_battery_poll will not call
772 * schedule_delayed_work again after unregister (which cause OOPS).
776 cancel_delayed_work_sync(&di->work);
778 power_supply_unregister(&di->bat);
780 mutex_destroy(&di->lock);
784 /* i2c specific code */
785 #ifdef CONFIG_BATTERY_BQ27X00_I2C
787 /* If the system has several batteries we need a different name for each
790 static DEFINE_IDR(battery_id);
791 static DEFINE_MUTEX(battery_mutex);
793 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
795 struct i2c_client *client = to_i2c_client(di->dev);
796 struct i2c_msg msg[2];
797 unsigned char data[2];
800 if (!client->adapter)
803 msg[0].addr = client->addr;
806 msg[0].len = sizeof(reg);
807 msg[1].addr = client->addr;
808 msg[1].flags = I2C_M_RD;
815 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
820 ret = get_unaligned_le16(data);
827 static int bq27x00_battery_probe(struct i2c_client *client,
828 const struct i2c_device_id *id)
831 struct bq27x00_device_info *di;
835 /* Get new ID for the new battery device */
836 mutex_lock(&battery_mutex);
837 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
838 mutex_unlock(&battery_mutex);
842 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
844 dev_err(&client->dev, "failed to allocate device name\n");
849 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
851 dev_err(&client->dev, "failed to allocate device info data\n");
857 di->dev = &client->dev;
858 di->chip = id->driver_data;
860 di->bus.read = &bq27x00_read_i2c;
862 retval = bq27x00_powersupply_init(di);
866 i2c_set_clientdata(client, di);
873 mutex_lock(&battery_mutex);
874 idr_remove(&battery_id, num);
875 mutex_unlock(&battery_mutex);
880 static int bq27x00_battery_remove(struct i2c_client *client)
882 struct bq27x00_device_info *di = i2c_get_clientdata(client);
884 bq27x00_powersupply_unregister(di);
888 mutex_lock(&battery_mutex);
889 idr_remove(&battery_id, di->id);
890 mutex_unlock(&battery_mutex);
895 static const struct i2c_device_id bq27x00_id[] = {
896 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
897 { "bq27500", BQ27500 },
898 { "bq27425", BQ27425 },
899 { "bq27742", BQ27742 },
902 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
904 static struct i2c_driver bq27x00_battery_driver = {
906 .name = "bq27x00-battery",
908 .probe = bq27x00_battery_probe,
909 .remove = bq27x00_battery_remove,
910 .id_table = bq27x00_id,
913 static inline int bq27x00_battery_i2c_init(void)
915 int ret = i2c_add_driver(&bq27x00_battery_driver);
917 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
922 static inline void bq27x00_battery_i2c_exit(void)
924 i2c_del_driver(&bq27x00_battery_driver);
929 static inline int bq27x00_battery_i2c_init(void) { return 0; }
930 static inline void bq27x00_battery_i2c_exit(void) {};
934 /* platform specific code */
935 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
937 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
940 struct device *dev = di->dev;
941 struct bq27000_platform_data *pdata = dev->platform_data;
942 unsigned int timeout = 3;
947 /* Make sure the value has not changed in between reading the
948 * lower and the upper part */
949 upper = pdata->read(dev, reg + 1);
955 lower = pdata->read(dev, reg);
959 upper = pdata->read(dev, reg + 1);
960 } while (temp != upper && --timeout);
965 return (upper << 8) | lower;
968 return pdata->read(dev, reg);
971 static int bq27000_battery_probe(struct platform_device *pdev)
973 struct bq27x00_device_info *di;
974 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
977 dev_err(&pdev->dev, "no platform_data supplied\n");
982 dev_err(&pdev->dev, "no hdq read callback supplied\n");
986 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
988 dev_err(&pdev->dev, "failed to allocate device info data\n");
992 platform_set_drvdata(pdev, di);
994 di->dev = &pdev->dev;
997 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
998 di->bus.read = &bq27000_read_platform;
1000 return bq27x00_powersupply_init(di);
1003 static int bq27000_battery_remove(struct platform_device *pdev)
1005 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
1007 bq27x00_powersupply_unregister(di);
1012 static struct platform_driver bq27000_battery_driver = {
1013 .probe = bq27000_battery_probe,
1014 .remove = bq27000_battery_remove,
1016 .name = "bq27000-battery",
1017 .owner = THIS_MODULE,
1021 static inline int bq27x00_battery_platform_init(void)
1023 int ret = platform_driver_register(&bq27000_battery_driver);
1025 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
1030 static inline void bq27x00_battery_platform_exit(void)
1032 platform_driver_unregister(&bq27000_battery_driver);
1037 static inline int bq27x00_battery_platform_init(void) { return 0; }
1038 static inline void bq27x00_battery_platform_exit(void) {};
1046 static int __init bq27x00_battery_init(void)
1050 ret = bq27x00_battery_i2c_init();
1054 ret = bq27x00_battery_platform_init();
1056 bq27x00_battery_i2c_exit();
1060 module_init(bq27x00_battery_init);
1062 static void __exit bq27x00_battery_exit(void)
1064 bq27x00_battery_platform_exit();
1065 bq27x00_battery_i2c_exit();
1067 module_exit(bq27x00_battery_exit);
1069 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1070 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1071 MODULE_LICENSE("GPL");