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 (0x1C + BQ27425_REG_OFFSET)
80 #define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
82 #define BQ27000_RS 20 /* Resistor sense */
83 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
85 struct bq27x00_device_info
;
86 struct bq27x00_access_methods
{
87 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
90 enum bq27x00_chip
{ BQ27000
, BQ27500
, BQ27425
, BQ27742
};
92 struct bq27x00_reg_cache
{
95 int time_to_empty_avg
;
106 struct bq27x00_device_info
{
109 enum bq27x00_chip chip
;
111 struct bq27x00_reg_cache cache
;
112 int charge_design_full
;
114 unsigned long last_update
;
115 struct delayed_work work
;
117 struct power_supply bat
;
119 struct bq27x00_access_methods bus
;
124 static enum power_supply_property bq27x00_battery_props
[] = {
125 POWER_SUPPLY_PROP_STATUS
,
126 POWER_SUPPLY_PROP_PRESENT
,
127 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
128 POWER_SUPPLY_PROP_CURRENT_NOW
,
129 POWER_SUPPLY_PROP_CAPACITY
,
130 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
131 POWER_SUPPLY_PROP_TEMP
,
132 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
133 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
134 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
135 POWER_SUPPLY_PROP_TECHNOLOGY
,
136 POWER_SUPPLY_PROP_CHARGE_FULL
,
137 POWER_SUPPLY_PROP_CHARGE_NOW
,
138 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
139 POWER_SUPPLY_PROP_CYCLE_COUNT
,
140 POWER_SUPPLY_PROP_ENERGY_NOW
,
141 POWER_SUPPLY_PROP_POWER_AVG
,
142 POWER_SUPPLY_PROP_HEALTH
,
145 static enum power_supply_property bq27425_battery_props
[] = {
146 POWER_SUPPLY_PROP_STATUS
,
147 POWER_SUPPLY_PROP_PRESENT
,
148 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
149 POWER_SUPPLY_PROP_CURRENT_NOW
,
150 POWER_SUPPLY_PROP_CAPACITY
,
151 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
152 POWER_SUPPLY_PROP_TEMP
,
153 POWER_SUPPLY_PROP_TECHNOLOGY
,
154 POWER_SUPPLY_PROP_CHARGE_FULL
,
155 POWER_SUPPLY_PROP_CHARGE_NOW
,
156 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
159 static enum power_supply_property bq27742_battery_props
[] = {
160 POWER_SUPPLY_PROP_STATUS
,
161 POWER_SUPPLY_PROP_PRESENT
,
162 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
163 POWER_SUPPLY_PROP_CURRENT_NOW
,
164 POWER_SUPPLY_PROP_CAPACITY
,
165 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
166 POWER_SUPPLY_PROP_TEMP
,
167 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
168 POWER_SUPPLY_PROP_TECHNOLOGY
,
169 POWER_SUPPLY_PROP_CHARGE_FULL
,
170 POWER_SUPPLY_PROP_CHARGE_NOW
,
171 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
172 POWER_SUPPLY_PROP_CYCLE_COUNT
,
173 POWER_SUPPLY_PROP_POWER_AVG
,
174 POWER_SUPPLY_PROP_HEALTH
,
177 static unsigned int poll_interval
= 360;
178 module_param(poll_interval
, uint
, 0644);
179 MODULE_PARM_DESC(poll_interval
, "battery poll interval in seconds - " \
180 "0 disables polling");
183 * Common code for BQ27x00 devices
186 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
189 if (di
->chip
== BQ27425
)
190 return di
->bus
.read(di
, reg
- BQ27425_REG_OFFSET
, single
);
191 return di
->bus
.read(di
, reg
, single
);
195 * Higher versions of the chip like BQ27425 and BQ27500
196 * differ from BQ27000 and BQ27200 in calculation of certain
197 * parameters. Hence we need to check for the chip type.
199 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info
*di
)
201 if (di
->chip
== BQ27425
|| di
->chip
== BQ27500
|| di
->chip
== BQ27742
)
207 * Return the battery Relative State-of-Charge
208 * Or < 0 if something fails.
210 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
214 if (di
->chip
== BQ27500
|| di
->chip
== BQ27742
)
215 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
216 else if (di
->chip
== BQ27425
)
217 rsoc
= bq27x00_read(di
, BQ27425_REG_SOC
, false);
219 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
222 dev_dbg(di
->dev
, "error reading relative State-of-Charge\n");
228 * Return a battery charge value in µAh
229 * Or < 0 if something fails.
231 static int bq27x00_battery_read_charge(struct bq27x00_device_info
*di
, u8 reg
)
235 charge
= bq27x00_read(di
, reg
, false);
237 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
242 if (bq27xxx_is_chip_version_higher(di
))
245 charge
= charge
* 3570 / BQ27000_RS
;
251 * Return the battery Nominal available capaciy in µAh
252 * Or < 0 if something fails.
254 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info
*di
)
257 bool is_bq27500
= di
->chip
== BQ27500
;
258 bool is_bq27742
= di
->chip
== BQ27742
;
259 bool is_higher
= bq27xxx_is_chip_version_higher(di
);
260 bool flags_1b
= !(is_bq27500
|| is_bq27742
);
262 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, flags_1b
);
263 if (flags
>= 0 && !is_higher
&& (flags
& BQ27000_FLAG_CI
))
266 return bq27x00_battery_read_charge(di
, BQ27x00_REG_NAC
);
270 * Return the battery Last measured discharge in µAh
271 * Or < 0 if something fails.
273 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info
*di
)
275 return bq27x00_battery_read_charge(di
, BQ27x00_REG_LMD
);
279 * Return the battery Initial last measured discharge in µAh
280 * Or < 0 if something fails.
282 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info
*di
)
286 if (bq27xxx_is_chip_version_higher(di
)) {
287 if (di
->chip
== BQ27425
)
288 ilmd
= bq27x00_read(di
, BQ27425_REG_DCAP
, false);
290 ilmd
= bq27x00_read(di
, BQ27500_REG_DCAP
, false);
292 ilmd
= bq27x00_read(di
, BQ27000_REG_ILMD
, true);
295 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
299 if (bq27xxx_is_chip_version_higher(di
))
302 ilmd
= ilmd
* 256 * 3570 / BQ27000_RS
;
308 * Return the battery Available energy in µWh
309 * Or < 0 if something fails.
311 static int bq27x00_battery_read_energy(struct bq27x00_device_info
*di
)
315 ae
= bq27x00_read(di
, BQ27x00_REG_AE
, false);
317 dev_dbg(di
->dev
, "error reading available energy\n");
321 if (di
->chip
== BQ27500
)
324 ae
= ae
* 29200 / BQ27000_RS
;
330 * Return the battery temperature in tenths of degree Kelvin
331 * Or < 0 if something fails.
333 static int bq27x00_battery_read_temperature(struct bq27x00_device_info
*di
)
337 temp
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
339 dev_err(di
->dev
, "error reading temperature\n");
343 if (!bq27xxx_is_chip_version_higher(di
))
350 * Return the battery Cycle count total
351 * Or < 0 if something fails.
353 static int bq27x00_battery_read_cyct(struct bq27x00_device_info
*di
)
357 cyct
= bq27x00_read(di
, BQ27x00_REG_CYCT
, false);
359 dev_err(di
->dev
, "error reading cycle count total\n");
365 * Read a time register.
366 * Return < 0 if something fails.
368 static int bq27x00_battery_read_time(struct bq27x00_device_info
*di
, u8 reg
)
372 tval
= bq27x00_read(di
, reg
, false);
374 dev_dbg(di
->dev
, "error reading time register %02x: %d\n",
386 * Read a power avg register.
387 * Return < 0 if something fails.
389 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info
*di
, u8 reg
)
393 tval
= bq27x00_read(di
, reg
, false);
395 dev_err(di
->dev
, "error reading power avg rgister %02x: %d\n",
400 if (di
->chip
== BQ27500
)
403 return (tval
* BQ27x00_POWER_CONSTANT
) / BQ27000_RS
;
407 * Read flag register.
408 * Return < 0 if something fails.
410 static int bq27x00_battery_read_health(struct bq27x00_device_info
*di
)
414 tval
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
416 dev_err(di
->dev
, "error reading flag register:%d\n", tval
);
420 if ((di
->chip
== BQ27500
)) {
421 if (tval
& BQ27500_FLAG_SOCF
)
422 tval
= POWER_SUPPLY_HEALTH_DEAD
;
423 else if (tval
& BQ27500_FLAG_OTC
)
424 tval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
426 tval
= POWER_SUPPLY_HEALTH_GOOD
;
429 if (tval
& BQ27000_FLAG_EDV1
)
430 tval
= POWER_SUPPLY_HEALTH_DEAD
;
432 tval
= POWER_SUPPLY_HEALTH_GOOD
;
439 static void bq27x00_update(struct bq27x00_device_info
*di
)
441 struct bq27x00_reg_cache cache
= {0, };
442 bool is_bq27500
= di
->chip
== BQ27500
;
443 bool is_bq27425
= di
->chip
== BQ27425
;
444 bool is_bq27742
= di
->chip
== BQ27742
;
445 bool flags_1b
= !(is_bq27500
|| is_bq27742
);
447 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, flags_1b
);
448 if ((cache
.flags
& 0xff) == 0xff)
451 if (cache
.flags
>= 0) {
452 if (!is_bq27500
&& !is_bq27425
&& !is_bq27742
453 && (cache
.flags
& BQ27000_FLAG_CI
)) {
454 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
455 cache
.capacity
= -ENODATA
;
456 cache
.energy
= -ENODATA
;
457 cache
.time_to_empty
= -ENODATA
;
458 cache
.time_to_empty_avg
= -ENODATA
;
459 cache
.time_to_full
= -ENODATA
;
460 cache
.charge_full
= -ENODATA
;
461 cache
.health
= -ENODATA
;
463 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
465 cache
.time_to_empty
=
466 bq27x00_battery_read_time(di
,
468 else if (!is_bq27425
) {
469 cache
.energy
= bq27x00_battery_read_energy(di
);
470 cache
.time_to_empty
=
471 bq27x00_battery_read_time(di
,
473 cache
.time_to_empty_avg
=
474 bq27x00_battery_read_time(di
,
477 bq27x00_battery_read_time(di
,
480 cache
.charge_full
= bq27x00_battery_read_lmd(di
);
481 cache
.health
= bq27x00_battery_read_health(di
);
483 cache
.temperature
= bq27x00_battery_read_temperature(di
);
485 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
488 bq27x00_battery_read_pwr_avg(di
,
492 bq27x00_battery_read_pwr_avg(di
,
495 /* We only have to read charge design full once */
496 if (di
->charge_design_full
<= 0)
497 di
->charge_design_full
= bq27x00_battery_read_ilmd(di
);
500 if (di
->cache
.capacity
!= cache
.capacity
)
501 power_supply_changed(&di
->bat
);
503 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0)
506 di
->last_update
= jiffies
;
509 static void bq27x00_battery_poll(struct work_struct
*work
)
511 struct bq27x00_device_info
*di
=
512 container_of(work
, struct bq27x00_device_info
, work
.work
);
516 if (poll_interval
> 0) {
517 /* The timer does not have to be accurate. */
518 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
519 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
524 * Return the battery average current in µA
525 * Note that current can be negative signed as well
526 * Or 0 if something fails.
528 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
529 union power_supply_propval
*val
)
534 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
536 dev_err(di
->dev
, "error reading current\n");
540 if (bq27xxx_is_chip_version_higher(di
)) {
541 /* bq27500 returns signed value */
542 val
->intval
= (int)((s16
)curr
) * 1000;
544 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
545 if (flags
& BQ27000_FLAG_CHGS
) {
546 dev_dbg(di
->dev
, "negative current!\n");
550 val
->intval
= curr
* 3570 / BQ27000_RS
;
556 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
557 union power_supply_propval
*val
)
561 if (bq27xxx_is_chip_version_higher(di
)) {
562 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
563 status
= POWER_SUPPLY_STATUS_FULL
;
564 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
565 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
567 status
= POWER_SUPPLY_STATUS_CHARGING
;
569 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
570 status
= POWER_SUPPLY_STATUS_FULL
;
571 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
572 status
= POWER_SUPPLY_STATUS_CHARGING
;
573 else if (power_supply_am_i_supplied(&di
->bat
))
574 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
576 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
579 val
->intval
= status
;
584 static int bq27x00_battery_capacity_level(struct bq27x00_device_info
*di
,
585 union power_supply_propval
*val
)
589 if (bq27xxx_is_chip_version_higher(di
)) {
590 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
591 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
592 else if (di
->cache
.flags
& BQ27500_FLAG_SOC1
)
593 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
594 else if (di
->cache
.flags
& BQ27500_FLAG_SOCF
)
595 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
597 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
599 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
600 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
601 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
602 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
603 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
604 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
606 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
615 * Return the battery Voltage in millivolts
616 * Or < 0 if something fails.
618 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
619 union power_supply_propval
*val
)
623 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
625 dev_err(di
->dev
, "error reading voltage\n");
629 val
->intval
= volt
* 1000;
634 static int bq27x00_simple_value(int value
,
635 union power_supply_propval
*val
)
645 #define to_bq27x00_device_info(x) container_of((x), \
646 struct bq27x00_device_info, bat);
648 static int bq27x00_battery_get_property(struct power_supply
*psy
,
649 enum power_supply_property psp
,
650 union power_supply_propval
*val
)
653 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
655 mutex_lock(&di
->lock
);
656 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
657 cancel_delayed_work_sync(&di
->work
);
658 bq27x00_battery_poll(&di
->work
.work
);
660 mutex_unlock(&di
->lock
);
662 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
666 case POWER_SUPPLY_PROP_STATUS
:
667 ret
= bq27x00_battery_status(di
, val
);
669 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
670 ret
= bq27x00_battery_voltage(di
, val
);
672 case POWER_SUPPLY_PROP_PRESENT
:
673 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
675 case POWER_SUPPLY_PROP_CURRENT_NOW
:
676 ret
= bq27x00_battery_current(di
, val
);
678 case POWER_SUPPLY_PROP_CAPACITY
:
679 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
681 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
682 ret
= bq27x00_battery_capacity_level(di
, val
);
684 case POWER_SUPPLY_PROP_TEMP
:
685 ret
= bq27x00_simple_value(di
->cache
.temperature
, val
);
689 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
690 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
692 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
693 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
695 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
696 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
698 case POWER_SUPPLY_PROP_TECHNOLOGY
:
699 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
701 case POWER_SUPPLY_PROP_CHARGE_NOW
:
702 ret
= bq27x00_simple_value(bq27x00_battery_read_nac(di
), val
);
704 case POWER_SUPPLY_PROP_CHARGE_FULL
:
705 ret
= bq27x00_simple_value(di
->cache
.charge_full
, val
);
707 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
708 ret
= bq27x00_simple_value(di
->charge_design_full
, val
);
710 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
711 ret
= bq27x00_simple_value(di
->cache
.cycle_count
, val
);
713 case POWER_SUPPLY_PROP_ENERGY_NOW
:
714 ret
= bq27x00_simple_value(di
->cache
.energy
, val
);
716 case POWER_SUPPLY_PROP_POWER_AVG
:
717 ret
= bq27x00_simple_value(di
->cache
.power_avg
, val
);
719 case POWER_SUPPLY_PROP_HEALTH
:
720 ret
= bq27x00_simple_value(di
->cache
.health
, val
);
729 static void bq27x00_external_power_changed(struct power_supply
*psy
)
731 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
733 cancel_delayed_work_sync(&di
->work
);
734 schedule_delayed_work(&di
->work
, 0);
737 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
)
741 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
742 if (di
->chip
== BQ27425
) {
743 di
->bat
.properties
= bq27425_battery_props
;
744 di
->bat
.num_properties
= ARRAY_SIZE(bq27425_battery_props
);
745 } else if (di
->chip
== BQ27742
) {
746 di
->bat
.properties
= bq27742_battery_props
;
747 di
->bat
.num_properties
= ARRAY_SIZE(bq27742_battery_props
);
749 di
->bat
.properties
= bq27x00_battery_props
;
750 di
->bat
.num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
752 di
->bat
.get_property
= bq27x00_battery_get_property
;
753 di
->bat
.external_power_changed
= bq27x00_external_power_changed
;
755 INIT_DELAYED_WORK(&di
->work
, bq27x00_battery_poll
);
756 mutex_init(&di
->lock
);
758 ret
= power_supply_register(di
->dev
, &di
->bat
);
760 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
764 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
771 static void bq27x00_powersupply_unregister(struct bq27x00_device_info
*di
)
774 * power_supply_unregister call bq27x00_battery_get_property which
775 * call bq27x00_battery_poll.
776 * Make sure that bq27x00_battery_poll will not call
777 * schedule_delayed_work again after unregister (which cause OOPS).
781 cancel_delayed_work_sync(&di
->work
);
783 power_supply_unregister(&di
->bat
);
785 mutex_destroy(&di
->lock
);
789 /* i2c specific code */
790 #ifdef CONFIG_BATTERY_BQ27X00_I2C
792 /* If the system has several batteries we need a different name for each
795 static DEFINE_IDR(battery_id
);
796 static DEFINE_MUTEX(battery_mutex
);
798 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
800 struct i2c_client
*client
= to_i2c_client(di
->dev
);
801 struct i2c_msg msg
[2];
802 unsigned char data
[2];
805 if (!client
->adapter
)
808 msg
[0].addr
= client
->addr
;
811 msg
[0].len
= sizeof(reg
);
812 msg
[1].addr
= client
->addr
;
813 msg
[1].flags
= I2C_M_RD
;
820 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
825 ret
= get_unaligned_le16(data
);
832 static int bq27x00_battery_probe(struct i2c_client
*client
,
833 const struct i2c_device_id
*id
)
836 struct bq27x00_device_info
*di
;
840 /* Get new ID for the new battery device */
841 mutex_lock(&battery_mutex
);
842 num
= idr_alloc(&battery_id
, client
, 0, 0, GFP_KERNEL
);
843 mutex_unlock(&battery_mutex
);
847 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
849 dev_err(&client
->dev
, "failed to allocate device name\n");
854 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
856 dev_err(&client
->dev
, "failed to allocate device info data\n");
862 di
->dev
= &client
->dev
;
863 di
->chip
= id
->driver_data
;
865 di
->bus
.read
= &bq27x00_read_i2c
;
867 retval
= bq27x00_powersupply_init(di
);
871 i2c_set_clientdata(client
, di
);
878 mutex_lock(&battery_mutex
);
879 idr_remove(&battery_id
, num
);
880 mutex_unlock(&battery_mutex
);
885 static int bq27x00_battery_remove(struct i2c_client
*client
)
887 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
889 bq27x00_powersupply_unregister(di
);
893 mutex_lock(&battery_mutex
);
894 idr_remove(&battery_id
, di
->id
);
895 mutex_unlock(&battery_mutex
);
900 static const struct i2c_device_id bq27x00_id
[] = {
901 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
902 { "bq27500", BQ27500
},
903 { "bq27425", BQ27425
},
904 { "bq27742", BQ27742
},
907 MODULE_DEVICE_TABLE(i2c
, bq27x00_id
);
909 static struct i2c_driver bq27x00_battery_driver
= {
911 .name
= "bq27x00-battery",
913 .probe
= bq27x00_battery_probe
,
914 .remove
= bq27x00_battery_remove
,
915 .id_table
= bq27x00_id
,
918 static inline int bq27x00_battery_i2c_init(void)
920 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
922 printk(KERN_ERR
"Unable to register BQ27x00 i2c driver\n");
927 static inline void bq27x00_battery_i2c_exit(void)
929 i2c_del_driver(&bq27x00_battery_driver
);
934 static inline int bq27x00_battery_i2c_init(void) { return 0; }
935 static inline void bq27x00_battery_i2c_exit(void) {};
939 /* platform specific code */
940 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
942 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
945 struct device
*dev
= di
->dev
;
946 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
947 unsigned int timeout
= 3;
952 /* Make sure the value has not changed in between reading the
953 * lower and the upper part */
954 upper
= pdata
->read(dev
, reg
+ 1);
960 lower
= pdata
->read(dev
, reg
);
964 upper
= pdata
->read(dev
, reg
+ 1);
965 } while (temp
!= upper
&& --timeout
);
970 return (upper
<< 8) | lower
;
973 return pdata
->read(dev
, reg
);
976 static int bq27000_battery_probe(struct platform_device
*pdev
)
978 struct bq27x00_device_info
*di
;
979 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
982 dev_err(&pdev
->dev
, "no platform_data supplied\n");
987 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
991 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
993 dev_err(&pdev
->dev
, "failed to allocate device info data\n");
997 platform_set_drvdata(pdev
, di
);
999 di
->dev
= &pdev
->dev
;
1002 di
->bat
.name
= pdata
->name
?: dev_name(&pdev
->dev
);
1003 di
->bus
.read
= &bq27000_read_platform
;
1005 return bq27x00_powersupply_init(di
);
1008 static int bq27000_battery_remove(struct platform_device
*pdev
)
1010 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
1012 bq27x00_powersupply_unregister(di
);
1017 static struct platform_driver bq27000_battery_driver
= {
1018 .probe
= bq27000_battery_probe
,
1019 .remove
= bq27000_battery_remove
,
1021 .name
= "bq27000-battery",
1025 static inline int bq27x00_battery_platform_init(void)
1027 int ret
= platform_driver_register(&bq27000_battery_driver
);
1029 printk(KERN_ERR
"Unable to register BQ27000 platform driver\n");
1034 static inline void bq27x00_battery_platform_exit(void)
1036 platform_driver_unregister(&bq27000_battery_driver
);
1041 static inline int bq27x00_battery_platform_init(void) { return 0; }
1042 static inline void bq27x00_battery_platform_exit(void) {};
1050 static int __init
bq27x00_battery_init(void)
1054 ret
= bq27x00_battery_i2c_init();
1058 ret
= bq27x00_battery_platform_init();
1060 bq27x00_battery_i2c_exit();
1064 module_init(bq27x00_battery_init
);
1066 static void __exit
bq27x00_battery_exit(void)
1068 bq27x00_battery_platform_exit();
1069 bq27x00_battery_i2c_exit();
1071 module_exit(bq27x00_battery_exit
);
1073 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1074 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1075 MODULE_LICENSE("GPL");