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
28 #include <linux/module.h>
29 #include <linux/param.h>
30 #include <linux/jiffies.h>
31 #include <linux/workqueue.h>
32 #include <linux/delay.h>
33 #include <linux/platform_device.h>
34 #include <linux/power_supply.h>
35 #include <linux/idr.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
38 #include <asm/unaligned.h>
40 #include <linux/power/bq27x00_battery.h>
42 #define DRIVER_VERSION "1.2.0"
44 #define BQ27x00_REG_TEMP 0x06
45 #define BQ27x00_REG_VOLT 0x08
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 capacity */
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 energy */
55 #define BQ27x00_POWER_AVG 0x24
57 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
58 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
59 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
60 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
61 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
62 #define BQ27000_FLAG_FC BIT(5)
63 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
65 #define BQ27500_REG_SOC 0x2C
66 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
67 #define BQ27500_FLAG_DSC BIT(0)
68 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
69 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
70 #define BQ27500_FLAG_FC BIT(9)
71 #define BQ27500_FLAG_OTC BIT(15)
73 /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
74 #define BQ27425_REG_OFFSET 0x04
75 #define BQ27425_REG_SOC 0x18 /* Register address plus offset */
77 #define BQ27000_RS 20 /* Resistor sense */
78 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
80 struct bq27x00_device_info
;
81 struct bq27x00_access_methods
{
82 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
85 enum bq27x00_chip
{ BQ27000
, BQ27500
, BQ27425
};
87 struct bq27x00_reg_cache
{
90 int time_to_empty_avg
;
101 struct bq27x00_device_info
{
104 enum bq27x00_chip chip
;
106 struct bq27x00_reg_cache cache
;
107 int charge_design_full
;
109 unsigned long last_update
;
110 struct delayed_work work
;
112 struct power_supply bat
;
114 struct bq27x00_access_methods bus
;
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 POWER_SUPPLY_PROP_POWER_AVG
,
137 POWER_SUPPLY_PROP_HEALTH
,
140 static enum power_supply_property bq27425_battery_props
[] = {
141 POWER_SUPPLY_PROP_STATUS
,
142 POWER_SUPPLY_PROP_PRESENT
,
143 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
144 POWER_SUPPLY_PROP_CURRENT_NOW
,
145 POWER_SUPPLY_PROP_CAPACITY
,
146 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
147 POWER_SUPPLY_PROP_TEMP
,
148 POWER_SUPPLY_PROP_TECHNOLOGY
,
149 POWER_SUPPLY_PROP_CHARGE_FULL
,
150 POWER_SUPPLY_PROP_CHARGE_NOW
,
151 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
154 static unsigned int poll_interval
= 360;
155 module_param(poll_interval
, uint
, 0644);
156 MODULE_PARM_DESC(poll_interval
, "battery poll interval in seconds - " \
157 "0 disables polling");
160 * Common code for BQ27x00 devices
163 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
166 if (di
->chip
== BQ27425
)
167 return di
->bus
.read(di
, reg
- BQ27425_REG_OFFSET
, single
);
168 return di
->bus
.read(di
, reg
, single
);
172 * Higher versions of the chip like BQ27425 and BQ27500
173 * differ from BQ27000 and BQ27200 in calculation of certain
174 * parameters. Hence we need to check for the chip type.
176 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info
*di
)
178 if (di
->chip
== BQ27425
|| di
->chip
== BQ27500
)
184 * Return the battery Relative State-of-Charge
185 * Or < 0 if something fails.
187 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
191 if (di
->chip
== BQ27500
)
192 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
193 else if (di
->chip
== BQ27425
)
194 rsoc
= bq27x00_read(di
, BQ27425_REG_SOC
, false);
196 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
199 dev_dbg(di
->dev
, "error reading relative State-of-Charge\n");
205 * Return a battery charge value in µAh
206 * Or < 0 if something fails.
208 static int bq27x00_battery_read_charge(struct bq27x00_device_info
*di
, u8 reg
)
212 charge
= bq27x00_read(di
, reg
, false);
214 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
219 if (bq27xxx_is_chip_version_higher(di
))
222 charge
= charge
* 3570 / BQ27000_RS
;
228 * Return the battery Nominal available capaciy in µAh
229 * Or < 0 if something fails.
231 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info
*di
)
234 bool is_bq27500
= di
->chip
== BQ27500
;
235 bool is_higher
= bq27xxx_is_chip_version_higher(di
);
237 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, !is_bq27500
);
238 if (flags
>= 0 && !is_higher
&& (flags
& BQ27000_FLAG_CI
))
241 return bq27x00_battery_read_charge(di
, BQ27x00_REG_NAC
);
245 * Return the battery Last measured discharge in µAh
246 * Or < 0 if something fails.
248 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info
*di
)
250 return bq27x00_battery_read_charge(di
, BQ27x00_REG_LMD
);
254 * Return the battery Initial last measured discharge in µAh
255 * Or < 0 if something fails.
257 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info
*di
)
261 if (bq27xxx_is_chip_version_higher(di
))
262 ilmd
= bq27x00_read(di
, BQ27500_REG_DCAP
, false);
264 ilmd
= bq27x00_read(di
, BQ27000_REG_ILMD
, true);
267 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
271 if (bq27xxx_is_chip_version_higher(di
))
274 ilmd
= ilmd
* 256 * 3570 / BQ27000_RS
;
280 * Return the battery Available energy in µWh
281 * Or < 0 if something fails.
283 static int bq27x00_battery_read_energy(struct bq27x00_device_info
*di
)
287 ae
= bq27x00_read(di
, BQ27x00_REG_AE
, false);
289 dev_dbg(di
->dev
, "error reading available energy\n");
293 if (di
->chip
== BQ27500
)
296 ae
= ae
* 29200 / BQ27000_RS
;
302 * Return the battery temperature in tenths of degree Celsius
303 * Or < 0 if something fails.
305 static int bq27x00_battery_read_temperature(struct bq27x00_device_info
*di
)
309 temp
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
311 dev_err(di
->dev
, "error reading temperature\n");
315 if (bq27xxx_is_chip_version_higher(di
))
318 temp
= ((temp
* 5) - 5463) / 2;
324 * Return the battery Cycle count total
325 * Or < 0 if something fails.
327 static int bq27x00_battery_read_cyct(struct bq27x00_device_info
*di
)
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",
360 * Read a power avg register.
361 * Return < 0 if something fails.
363 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info
*di
, u8 reg
)
367 tval
= bq27x00_read(di
, reg
, false);
369 dev_err(di
->dev
, "error reading power avg rgister %02x: %d\n",
374 if (di
->chip
== BQ27500
)
377 return (tval
* BQ27x00_POWER_CONSTANT
) / BQ27000_RS
;
381 * Read flag register.
382 * Return < 0 if something fails.
384 static int bq27x00_battery_read_health(struct bq27x00_device_info
*di
)
388 tval
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
390 dev_err(di
->dev
, "error reading flag register:%d\n", tval
);
394 if ((di
->chip
== BQ27500
)) {
395 if (tval
& BQ27500_FLAG_SOCF
)
396 tval
= POWER_SUPPLY_HEALTH_DEAD
;
397 else if (tval
& BQ27500_FLAG_OTC
)
398 tval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
400 tval
= POWER_SUPPLY_HEALTH_GOOD
;
403 if (tval
& BQ27000_FLAG_EDV1
)
404 tval
= POWER_SUPPLY_HEALTH_DEAD
;
406 tval
= POWER_SUPPLY_HEALTH_GOOD
;
413 static void bq27x00_update(struct bq27x00_device_info
*di
)
415 struct bq27x00_reg_cache cache
= {0, };
416 bool is_bq27500
= di
->chip
== BQ27500
;
417 bool is_bq27425
= di
->chip
== BQ27425
;
419 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, !is_bq27500
);
420 if (cache
.flags
>= 0) {
421 if (!is_bq27500
&& !is_bq27425
422 && (cache
.flags
& BQ27000_FLAG_CI
)) {
423 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
424 cache
.capacity
= -ENODATA
;
425 cache
.energy
= -ENODATA
;
426 cache
.time_to_empty
= -ENODATA
;
427 cache
.time_to_empty_avg
= -ENODATA
;
428 cache
.time_to_full
= -ENODATA
;
429 cache
.charge_full
= -ENODATA
;
430 cache
.health
= -ENODATA
;
432 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
434 cache
.energy
= bq27x00_battery_read_energy(di
);
435 cache
.time_to_empty
=
436 bq27x00_battery_read_time(di
,
438 cache
.time_to_empty_avg
=
439 bq27x00_battery_read_time(di
,
442 bq27x00_battery_read_time(di
,
445 cache
.charge_full
= bq27x00_battery_read_lmd(di
);
446 cache
.health
= bq27x00_battery_read_health(di
);
448 cache
.temperature
= bq27x00_battery_read_temperature(di
);
450 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
451 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
453 bq27x00_battery_read_pwr_avg(di
, BQ27x00_POWER_AVG
);
455 /* We only have to read charge design full once */
456 if (di
->charge_design_full
<= 0)
457 di
->charge_design_full
= bq27x00_battery_read_ilmd(di
);
460 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0) {
462 power_supply_changed(&di
->bat
);
465 di
->last_update
= jiffies
;
468 static void bq27x00_battery_poll(struct work_struct
*work
)
470 struct bq27x00_device_info
*di
=
471 container_of(work
, struct bq27x00_device_info
, work
.work
);
475 if (poll_interval
> 0) {
476 /* The timer does not have to be accurate. */
477 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
478 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
483 * Return the battery average current in µA
484 * Note that current can be negative signed as well
485 * Or 0 if something fails.
487 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
488 union power_supply_propval
*val
)
493 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
495 dev_err(di
->dev
, "error reading current\n");
499 if (bq27xxx_is_chip_version_higher(di
)) {
500 /* bq27500 returns signed value */
501 val
->intval
= (int)((s16
)curr
) * 1000;
503 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
504 if (flags
& BQ27000_FLAG_CHGS
) {
505 dev_dbg(di
->dev
, "negative current!\n");
509 val
->intval
= curr
* 3570 / BQ27000_RS
;
515 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
516 union power_supply_propval
*val
)
520 if (bq27xxx_is_chip_version_higher(di
)) {
521 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
522 status
= POWER_SUPPLY_STATUS_FULL
;
523 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
524 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
526 status
= POWER_SUPPLY_STATUS_CHARGING
;
528 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
529 status
= POWER_SUPPLY_STATUS_FULL
;
530 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
531 status
= POWER_SUPPLY_STATUS_CHARGING
;
532 else if (power_supply_am_i_supplied(&di
->bat
))
533 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
535 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
538 val
->intval
= status
;
543 static int bq27x00_battery_capacity_level(struct bq27x00_device_info
*di
,
544 union power_supply_propval
*val
)
548 if (bq27xxx_is_chip_version_higher(di
)) {
549 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
550 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
551 else if (di
->cache
.flags
& BQ27500_FLAG_SOC1
)
552 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
553 else if (di
->cache
.flags
& BQ27500_FLAG_SOCF
)
554 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
556 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
558 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
559 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
560 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
561 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
562 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
563 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
565 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
574 * Return the battery Voltage in millivolts
575 * Or < 0 if something fails.
577 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
578 union power_supply_propval
*val
)
582 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
584 dev_err(di
->dev
, "error reading voltage\n");
588 val
->intval
= volt
* 1000;
593 static int bq27x00_simple_value(int value
,
594 union power_supply_propval
*val
)
604 #define to_bq27x00_device_info(x) container_of((x), \
605 struct bq27x00_device_info, bat);
607 static int bq27x00_battery_get_property(struct power_supply
*psy
,
608 enum power_supply_property psp
,
609 union power_supply_propval
*val
)
612 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
614 mutex_lock(&di
->lock
);
615 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
616 cancel_delayed_work_sync(&di
->work
);
617 bq27x00_battery_poll(&di
->work
.work
);
619 mutex_unlock(&di
->lock
);
621 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
625 case POWER_SUPPLY_PROP_STATUS
:
626 ret
= bq27x00_battery_status(di
, val
);
628 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
629 ret
= bq27x00_battery_voltage(di
, val
);
631 case POWER_SUPPLY_PROP_PRESENT
:
632 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
634 case POWER_SUPPLY_PROP_CURRENT_NOW
:
635 ret
= bq27x00_battery_current(di
, val
);
637 case POWER_SUPPLY_PROP_CAPACITY
:
638 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
640 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
641 ret
= bq27x00_battery_capacity_level(di
, val
);
643 case POWER_SUPPLY_PROP_TEMP
:
644 ret
= bq27x00_simple_value(di
->cache
.temperature
, val
);
646 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
647 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
649 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
650 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
652 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
653 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
655 case POWER_SUPPLY_PROP_TECHNOLOGY
:
656 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
658 case POWER_SUPPLY_PROP_CHARGE_NOW
:
659 ret
= bq27x00_simple_value(bq27x00_battery_read_nac(di
), val
);
661 case POWER_SUPPLY_PROP_CHARGE_FULL
:
662 ret
= bq27x00_simple_value(di
->cache
.charge_full
, val
);
664 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
665 ret
= bq27x00_simple_value(di
->charge_design_full
, val
);
667 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
668 ret
= bq27x00_simple_value(di
->cache
.cycle_count
, val
);
670 case POWER_SUPPLY_PROP_ENERGY_NOW
:
671 ret
= bq27x00_simple_value(di
->cache
.energy
, val
);
673 case POWER_SUPPLY_PROP_POWER_AVG
:
674 ret
= bq27x00_simple_value(di
->cache
.power_avg
, val
);
676 case POWER_SUPPLY_PROP_HEALTH
:
677 ret
= bq27x00_simple_value(di
->cache
.health
, val
);
686 static void bq27x00_external_power_changed(struct power_supply
*psy
)
688 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
690 cancel_delayed_work_sync(&di
->work
);
691 schedule_delayed_work(&di
->work
, 0);
694 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
)
698 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
700 if (di
->chip
== BQ27425
) {
701 di
->bat
.properties
= bq27425_battery_props
;
702 di
->bat
.num_properties
= ARRAY_SIZE(bq27425_battery_props
);
704 di
->bat
.properties
= bq27x00_battery_props
;
705 di
->bat
.num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
707 di
->bat
.get_property
= bq27x00_battery_get_property
;
708 di
->bat
.external_power_changed
= bq27x00_external_power_changed
;
710 INIT_DELAYED_WORK(&di
->work
, bq27x00_battery_poll
);
711 mutex_init(&di
->lock
);
713 ret
= power_supply_register(di
->dev
, &di
->bat
);
715 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
719 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
726 static void bq27x00_powersupply_unregister(struct bq27x00_device_info
*di
)
729 * power_supply_unregister call bq27x00_battery_get_property which
730 * call bq27x00_battery_poll.
731 * Make sure that bq27x00_battery_poll will not call
732 * schedule_delayed_work again after unregister (which cause OOPS).
736 cancel_delayed_work_sync(&di
->work
);
738 power_supply_unregister(&di
->bat
);
740 mutex_destroy(&di
->lock
);
744 /* i2c specific code */
745 #ifdef CONFIG_BATTERY_BQ27X00_I2C
747 /* If the system has several batteries we need a different name for each
750 static DEFINE_IDR(battery_id
);
751 static DEFINE_MUTEX(battery_mutex
);
753 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
755 struct i2c_client
*client
= to_i2c_client(di
->dev
);
756 struct i2c_msg msg
[2];
757 unsigned char data
[2];
760 if (!client
->adapter
)
763 msg
[0].addr
= client
->addr
;
766 msg
[0].len
= sizeof(reg
);
767 msg
[1].addr
= client
->addr
;
768 msg
[1].flags
= I2C_M_RD
;
775 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
780 ret
= get_unaligned_le16(data
);
787 static int bq27x00_battery_probe(struct i2c_client
*client
,
788 const struct i2c_device_id
*id
)
791 struct bq27x00_device_info
*di
;
795 /* Get new ID for the new battery device */
796 retval
= idr_pre_get(&battery_id
, GFP_KERNEL
);
799 mutex_lock(&battery_mutex
);
800 retval
= idr_get_new(&battery_id
, client
, &num
);
801 mutex_unlock(&battery_mutex
);
805 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
807 dev_err(&client
->dev
, "failed to allocate device name\n");
812 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
814 dev_err(&client
->dev
, "failed to allocate device info data\n");
820 di
->dev
= &client
->dev
;
821 di
->chip
= id
->driver_data
;
823 di
->bus
.read
= &bq27x00_read_i2c
;
825 retval
= bq27x00_powersupply_init(di
);
829 i2c_set_clientdata(client
, di
);
838 mutex_lock(&battery_mutex
);
839 idr_remove(&battery_id
, num
);
840 mutex_unlock(&battery_mutex
);
845 static int bq27x00_battery_remove(struct i2c_client
*client
)
847 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
849 bq27x00_powersupply_unregister(di
);
853 mutex_lock(&battery_mutex
);
854 idr_remove(&battery_id
, di
->id
);
855 mutex_unlock(&battery_mutex
);
862 static const struct i2c_device_id bq27x00_id
[] = {
863 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
864 { "bq27500", BQ27500
},
865 { "bq27425", BQ27425
},
868 MODULE_DEVICE_TABLE(i2c
, bq27x00_id
);
870 static struct i2c_driver bq27x00_battery_driver
= {
872 .name
= "bq27x00-battery",
874 .probe
= bq27x00_battery_probe
,
875 .remove
= bq27x00_battery_remove
,
876 .id_table
= bq27x00_id
,
879 static inline int bq27x00_battery_i2c_init(void)
881 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
883 printk(KERN_ERR
"Unable to register BQ27x00 i2c driver\n");
888 static inline void bq27x00_battery_i2c_exit(void)
890 i2c_del_driver(&bq27x00_battery_driver
);
895 static inline int bq27x00_battery_i2c_init(void) { return 0; }
896 static inline void bq27x00_battery_i2c_exit(void) {};
900 /* platform specific code */
901 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
903 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
906 struct device
*dev
= di
->dev
;
907 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
908 unsigned int timeout
= 3;
913 /* Make sure the value has not changed in between reading the
914 * lower and the upper part */
915 upper
= pdata
->read(dev
, reg
+ 1);
921 lower
= pdata
->read(dev
, reg
);
925 upper
= pdata
->read(dev
, reg
+ 1);
926 } while (temp
!= upper
&& --timeout
);
931 return (upper
<< 8) | lower
;
934 return pdata
->read(dev
, reg
);
937 static int bq27000_battery_probe(struct platform_device
*pdev
)
939 struct bq27x00_device_info
*di
;
940 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
944 dev_err(&pdev
->dev
, "no platform_data supplied\n");
949 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
953 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
955 dev_err(&pdev
->dev
, "failed to allocate device info data\n");
959 platform_set_drvdata(pdev
, di
);
961 di
->dev
= &pdev
->dev
;
964 di
->bat
.name
= pdata
->name
?: dev_name(&pdev
->dev
);
965 di
->bus
.read
= &bq27000_read_platform
;
967 ret
= bq27x00_powersupply_init(di
);
974 platform_set_drvdata(pdev
, NULL
);
980 static int bq27000_battery_remove(struct platform_device
*pdev
)
982 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
984 bq27x00_powersupply_unregister(di
);
986 platform_set_drvdata(pdev
, NULL
);
992 static struct platform_driver bq27000_battery_driver
= {
993 .probe
= bq27000_battery_probe
,
994 .remove
= bq27000_battery_remove
,
996 .name
= "bq27000-battery",
997 .owner
= THIS_MODULE
,
1001 static inline int bq27x00_battery_platform_init(void)
1003 int ret
= platform_driver_register(&bq27000_battery_driver
);
1005 printk(KERN_ERR
"Unable to register BQ27000 platform driver\n");
1010 static inline void bq27x00_battery_platform_exit(void)
1012 platform_driver_unregister(&bq27000_battery_driver
);
1017 static inline int bq27x00_battery_platform_init(void) { return 0; }
1018 static inline void bq27x00_battery_platform_exit(void) {};
1026 static int __init
bq27x00_battery_init(void)
1030 ret
= bq27x00_battery_i2c_init();
1034 ret
= bq27x00_battery_platform_init();
1036 bq27x00_battery_i2c_exit();
1040 module_init(bq27x00_battery_init
);
1042 static void __exit
bq27x00_battery_exit(void)
1044 bq27x00_battery_platform_exit();
1045 bq27x00_battery_i2c_exit();
1047 module_exit(bq27x00_battery_exit
);
1049 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1050 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1051 MODULE_LICENSE("GPL");