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.
20 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
21 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
22 * http://www.ti.com/product/bq27425-g1
23 * http://www.ti.com/product/BQ27742-G1
24 * http://www.ti.com/product/BQ27510-G3
27 #include <linux/device.h>
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 #define BQ27742_POWER_AVG 0x76
75 #define BQ27510_REG_SOC 0x20
76 #define BQ27510_REG_DCAP 0x2E /* Design capacity */
77 #define BQ27510_REG_CYCT 0x1E /* Cycle count total */
79 /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
80 #define BQ27425_REG_OFFSET 0x04
81 #define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET)
82 #define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
84 #define BQ27000_RS 20 /* Resistor sense */
85 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
87 struct bq27x00_device_info
;
88 struct bq27x00_access_methods
{
89 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
92 enum bq27x00_chip
{ BQ27000
, BQ27500
, BQ27425
, BQ27742
, BQ27510
};
94 struct bq27x00_reg_cache
{
97 int time_to_empty_avg
;
108 struct bq27x00_device_info
{
111 enum bq27x00_chip chip
;
113 struct bq27x00_reg_cache cache
;
114 int charge_design_full
;
116 unsigned long last_update
;
117 struct delayed_work work
;
119 struct power_supply
*bat
;
121 struct bq27x00_access_methods bus
;
126 static enum power_supply_property bq27x00_battery_props
[] = {
127 POWER_SUPPLY_PROP_STATUS
,
128 POWER_SUPPLY_PROP_PRESENT
,
129 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
130 POWER_SUPPLY_PROP_CURRENT_NOW
,
131 POWER_SUPPLY_PROP_CAPACITY
,
132 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
133 POWER_SUPPLY_PROP_TEMP
,
134 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
135 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
136 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
137 POWER_SUPPLY_PROP_TECHNOLOGY
,
138 POWER_SUPPLY_PROP_CHARGE_FULL
,
139 POWER_SUPPLY_PROP_CHARGE_NOW
,
140 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
141 POWER_SUPPLY_PROP_CYCLE_COUNT
,
142 POWER_SUPPLY_PROP_ENERGY_NOW
,
143 POWER_SUPPLY_PROP_POWER_AVG
,
144 POWER_SUPPLY_PROP_HEALTH
,
147 static enum power_supply_property bq27425_battery_props
[] = {
148 POWER_SUPPLY_PROP_STATUS
,
149 POWER_SUPPLY_PROP_PRESENT
,
150 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
151 POWER_SUPPLY_PROP_CURRENT_NOW
,
152 POWER_SUPPLY_PROP_CAPACITY
,
153 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
154 POWER_SUPPLY_PROP_TEMP
,
155 POWER_SUPPLY_PROP_TECHNOLOGY
,
156 POWER_SUPPLY_PROP_CHARGE_FULL
,
157 POWER_SUPPLY_PROP_CHARGE_NOW
,
158 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
161 static enum power_supply_property bq27742_battery_props
[] = {
162 POWER_SUPPLY_PROP_STATUS
,
163 POWER_SUPPLY_PROP_PRESENT
,
164 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
165 POWER_SUPPLY_PROP_CURRENT_NOW
,
166 POWER_SUPPLY_PROP_CAPACITY
,
167 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
168 POWER_SUPPLY_PROP_TEMP
,
169 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
170 POWER_SUPPLY_PROP_TECHNOLOGY
,
171 POWER_SUPPLY_PROP_CHARGE_FULL
,
172 POWER_SUPPLY_PROP_CHARGE_NOW
,
173 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
174 POWER_SUPPLY_PROP_CYCLE_COUNT
,
175 POWER_SUPPLY_PROP_POWER_AVG
,
176 POWER_SUPPLY_PROP_HEALTH
,
179 static enum power_supply_property bq27510_battery_props
[] = {
180 POWER_SUPPLY_PROP_STATUS
,
181 POWER_SUPPLY_PROP_PRESENT
,
182 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
183 POWER_SUPPLY_PROP_CURRENT_NOW
,
184 POWER_SUPPLY_PROP_CAPACITY
,
185 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
186 POWER_SUPPLY_PROP_TEMP
,
187 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
188 POWER_SUPPLY_PROP_TECHNOLOGY
,
189 POWER_SUPPLY_PROP_CHARGE_FULL
,
190 POWER_SUPPLY_PROP_CHARGE_NOW
,
191 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
192 POWER_SUPPLY_PROP_CYCLE_COUNT
,
193 POWER_SUPPLY_PROP_POWER_AVG
,
194 POWER_SUPPLY_PROP_HEALTH
,
197 static unsigned int poll_interval
= 360;
198 module_param(poll_interval
, uint
, 0644);
199 MODULE_PARM_DESC(poll_interval
, "battery poll interval in seconds - " \
200 "0 disables polling");
203 * Common code for BQ27x00 devices
206 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
209 if (di
->chip
== BQ27425
)
210 return di
->bus
.read(di
, reg
- BQ27425_REG_OFFSET
, single
);
211 return di
->bus
.read(di
, reg
, single
);
215 * Higher versions of the chip like BQ27425 and BQ27500
216 * differ from BQ27000 and BQ27200 in calculation of certain
217 * parameters. Hence we need to check for the chip type.
219 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info
*di
)
221 if (di
->chip
== BQ27425
|| di
->chip
== BQ27500
|| di
->chip
== BQ27742
222 || di
->chip
== BQ27510
)
228 * Return the battery Relative State-of-Charge
229 * Or < 0 if something fails.
231 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
235 if (di
->chip
== BQ27500
|| di
->chip
== BQ27742
)
236 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
237 else if (di
->chip
== BQ27510
)
238 rsoc
= bq27x00_read(di
, BQ27510_REG_SOC
, false);
239 else if (di
->chip
== BQ27425
)
240 rsoc
= bq27x00_read(di
, BQ27425_REG_SOC
, false);
242 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
245 dev_dbg(di
->dev
, "error reading relative State-of-Charge\n");
251 * Return a battery charge value in µAh
252 * Or < 0 if something fails.
254 static int bq27x00_battery_read_charge(struct bq27x00_device_info
*di
, u8 reg
)
258 charge
= bq27x00_read(di
, reg
, false);
260 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
265 if (bq27xxx_is_chip_version_higher(di
))
268 charge
= charge
* 3570 / BQ27000_RS
;
274 * Return the battery Nominal available capaciy in µAh
275 * Or < 0 if something fails.
277 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info
*di
)
280 bool is_bq27500
= di
->chip
== BQ27500
;
281 bool is_bq27742
= di
->chip
== BQ27742
;
282 bool is_higher
= bq27xxx_is_chip_version_higher(di
);
283 bool flags_1b
= !(is_bq27500
|| is_bq27742
);
285 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, flags_1b
);
286 if (flags
>= 0 && !is_higher
&& (flags
& BQ27000_FLAG_CI
))
289 return bq27x00_battery_read_charge(di
, BQ27x00_REG_NAC
);
293 * Return the battery Last measured discharge in µAh
294 * Or < 0 if something fails.
296 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info
*di
)
298 return bq27x00_battery_read_charge(di
, BQ27x00_REG_LMD
);
302 * Return the battery Initial last measured discharge in µAh
303 * Or < 0 if something fails.
305 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info
*di
)
309 if (bq27xxx_is_chip_version_higher(di
)) {
310 if (di
->chip
== BQ27425
)
311 ilmd
= bq27x00_read(di
, BQ27425_REG_DCAP
, false);
312 else if (di
->chip
== BQ27510
)
313 ilmd
= bq27x00_read(di
, BQ27510_REG_DCAP
, false);
315 ilmd
= bq27x00_read(di
, BQ27500_REG_DCAP
, false);
317 ilmd
= bq27x00_read(di
, BQ27000_REG_ILMD
, true);
320 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
324 if (bq27xxx_is_chip_version_higher(di
))
327 ilmd
= ilmd
* 256 * 3570 / BQ27000_RS
;
333 * Return the battery Available energy in µWh
334 * Or < 0 if something fails.
336 static int bq27x00_battery_read_energy(struct bq27x00_device_info
*di
)
340 ae
= bq27x00_read(di
, BQ27x00_REG_AE
, false);
342 dev_dbg(di
->dev
, "error reading available energy\n");
346 if (di
->chip
== BQ27500
)
349 ae
= ae
* 29200 / BQ27000_RS
;
355 * Return the battery temperature in tenths of degree Kelvin
356 * Or < 0 if something fails.
358 static int bq27x00_battery_read_temperature(struct bq27x00_device_info
*di
)
362 temp
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
364 dev_err(di
->dev
, "error reading temperature\n");
368 if (!bq27xxx_is_chip_version_higher(di
))
375 * Return the battery Cycle count total
376 * Or < 0 if something fails.
378 static int bq27x00_battery_read_cyct(struct bq27x00_device_info
*di
)
382 if (di
->chip
== BQ27510
)
383 cyct
= bq27x00_read(di
, BQ27510_REG_CYCT
, false);
385 cyct
= bq27x00_read(di
, BQ27x00_REG_CYCT
, false);
387 dev_err(di
->dev
, "error reading cycle count total\n");
393 * Read a time register.
394 * Return < 0 if something fails.
396 static int bq27x00_battery_read_time(struct bq27x00_device_info
*di
, u8 reg
)
400 tval
= bq27x00_read(di
, reg
, false);
402 dev_dbg(di
->dev
, "error reading time register %02x: %d\n",
414 * Read a power avg register.
415 * Return < 0 if something fails.
417 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info
*di
, u8 reg
)
421 tval
= bq27x00_read(di
, reg
, false);
423 dev_err(di
->dev
, "error reading power avg rgister %02x: %d\n",
428 if (di
->chip
== BQ27500
)
431 return (tval
* BQ27x00_POWER_CONSTANT
) / BQ27000_RS
;
435 * Read flag register.
436 * Return < 0 if something fails.
438 static int bq27x00_battery_read_health(struct bq27x00_device_info
*di
)
442 tval
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
444 dev_err(di
->dev
, "error reading flag register:%d\n", tval
);
448 if ((di
->chip
== BQ27500
)) {
449 if (tval
& BQ27500_FLAG_SOCF
)
450 tval
= POWER_SUPPLY_HEALTH_DEAD
;
451 else if (tval
& BQ27500_FLAG_OTC
)
452 tval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
454 tval
= POWER_SUPPLY_HEALTH_GOOD
;
456 } else if (di
->chip
== BQ27510
) {
457 if (tval
& BQ27500_FLAG_OTC
)
458 return POWER_SUPPLY_HEALTH_OVERHEAT
;
459 return POWER_SUPPLY_HEALTH_GOOD
;
461 if (tval
& BQ27000_FLAG_EDV1
)
462 tval
= POWER_SUPPLY_HEALTH_DEAD
;
464 tval
= POWER_SUPPLY_HEALTH_GOOD
;
471 static void bq27x00_update(struct bq27x00_device_info
*di
)
473 struct bq27x00_reg_cache cache
= {0, };
474 bool is_bq27500
= di
->chip
== BQ27500
;
475 bool is_bq27510
= di
->chip
== BQ27510
;
476 bool is_bq27425
= di
->chip
== BQ27425
;
477 bool is_bq27742
= di
->chip
== BQ27742
;
478 bool flags_1b
= !(is_bq27500
|| is_bq27742
);
480 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, flags_1b
);
481 if ((cache
.flags
& 0xff) == 0xff)
484 if (cache
.flags
>= 0) {
485 if (!is_bq27500
&& !is_bq27425
&& !is_bq27742
&& !is_bq27510
486 && (cache
.flags
& BQ27000_FLAG_CI
)) {
487 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
488 cache
.capacity
= -ENODATA
;
489 cache
.energy
= -ENODATA
;
490 cache
.time_to_empty
= -ENODATA
;
491 cache
.time_to_empty_avg
= -ENODATA
;
492 cache
.time_to_full
= -ENODATA
;
493 cache
.charge_full
= -ENODATA
;
494 cache
.health
= -ENODATA
;
496 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
497 if (is_bq27742
|| is_bq27510
)
498 cache
.time_to_empty
=
499 bq27x00_battery_read_time(di
,
501 else if (!is_bq27425
) {
502 cache
.energy
= bq27x00_battery_read_energy(di
);
503 cache
.time_to_empty
=
504 bq27x00_battery_read_time(di
,
506 cache
.time_to_empty_avg
=
507 bq27x00_battery_read_time(di
,
510 bq27x00_battery_read_time(di
,
513 cache
.charge_full
= bq27x00_battery_read_lmd(di
);
514 cache
.health
= bq27x00_battery_read_health(di
);
516 cache
.temperature
= bq27x00_battery_read_temperature(di
);
518 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
521 bq27x00_battery_read_pwr_avg(di
,
525 bq27x00_battery_read_pwr_avg(di
,
528 /* We only have to read charge design full once */
529 if (di
->charge_design_full
<= 0)
530 di
->charge_design_full
= bq27x00_battery_read_ilmd(di
);
533 if (di
->cache
.capacity
!= cache
.capacity
)
534 power_supply_changed(di
->bat
);
536 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0)
539 di
->last_update
= jiffies
;
542 static void bq27x00_battery_poll(struct work_struct
*work
)
544 struct bq27x00_device_info
*di
=
545 container_of(work
, struct bq27x00_device_info
, work
.work
);
549 if (poll_interval
> 0) {
550 /* The timer does not have to be accurate. */
551 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
552 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
557 * Return the battery average current in µA
558 * Note that current can be negative signed as well
559 * Or 0 if something fails.
561 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
562 union power_supply_propval
*val
)
567 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
569 dev_err(di
->dev
, "error reading current\n");
573 if (bq27xxx_is_chip_version_higher(di
)) {
574 /* bq27500 returns signed value */
575 val
->intval
= (int)((s16
)curr
) * 1000;
577 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
578 if (flags
& BQ27000_FLAG_CHGS
) {
579 dev_dbg(di
->dev
, "negative current!\n");
583 val
->intval
= curr
* 3570 / BQ27000_RS
;
589 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
590 union power_supply_propval
*val
)
594 if (bq27xxx_is_chip_version_higher(di
)) {
595 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
596 status
= POWER_SUPPLY_STATUS_FULL
;
597 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
598 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
600 status
= POWER_SUPPLY_STATUS_CHARGING
;
602 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
603 status
= POWER_SUPPLY_STATUS_FULL
;
604 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
605 status
= POWER_SUPPLY_STATUS_CHARGING
;
606 else if (power_supply_am_i_supplied(di
->bat
))
607 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
609 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
612 val
->intval
= status
;
617 static int bq27x00_battery_capacity_level(struct bq27x00_device_info
*di
,
618 union power_supply_propval
*val
)
622 if (bq27xxx_is_chip_version_higher(di
)) {
623 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
624 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
625 else if (di
->cache
.flags
& BQ27500_FLAG_SOC1
)
626 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
627 else if (di
->cache
.flags
& BQ27500_FLAG_SOCF
)
628 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
630 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
632 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
633 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
634 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
635 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
636 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
637 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
639 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
648 * Return the battery Voltage in millivolts
649 * Or < 0 if something fails.
651 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
652 union power_supply_propval
*val
)
656 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
658 dev_err(di
->dev
, "error reading voltage\n");
662 val
->intval
= volt
* 1000;
667 static int bq27x00_simple_value(int value
,
668 union power_supply_propval
*val
)
678 static int bq27x00_battery_get_property(struct power_supply
*psy
,
679 enum power_supply_property psp
,
680 union power_supply_propval
*val
)
683 struct bq27x00_device_info
*di
= power_supply_get_drvdata(psy
);
685 mutex_lock(&di
->lock
);
686 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
687 cancel_delayed_work_sync(&di
->work
);
688 bq27x00_battery_poll(&di
->work
.work
);
690 mutex_unlock(&di
->lock
);
692 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
696 case POWER_SUPPLY_PROP_STATUS
:
697 ret
= bq27x00_battery_status(di
, val
);
699 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
700 ret
= bq27x00_battery_voltage(di
, val
);
702 case POWER_SUPPLY_PROP_PRESENT
:
703 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
705 case POWER_SUPPLY_PROP_CURRENT_NOW
:
706 ret
= bq27x00_battery_current(di
, val
);
708 case POWER_SUPPLY_PROP_CAPACITY
:
709 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
711 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
712 ret
= bq27x00_battery_capacity_level(di
, val
);
714 case POWER_SUPPLY_PROP_TEMP
:
715 ret
= bq27x00_simple_value(di
->cache
.temperature
, val
);
719 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
720 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
722 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
723 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
725 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
726 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
728 case POWER_SUPPLY_PROP_TECHNOLOGY
:
729 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
731 case POWER_SUPPLY_PROP_CHARGE_NOW
:
732 ret
= bq27x00_simple_value(bq27x00_battery_read_nac(di
), val
);
734 case POWER_SUPPLY_PROP_CHARGE_FULL
:
735 ret
= bq27x00_simple_value(di
->cache
.charge_full
, val
);
737 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
738 ret
= bq27x00_simple_value(di
->charge_design_full
, val
);
740 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
741 ret
= bq27x00_simple_value(di
->cache
.cycle_count
, val
);
743 case POWER_SUPPLY_PROP_ENERGY_NOW
:
744 ret
= bq27x00_simple_value(di
->cache
.energy
, val
);
746 case POWER_SUPPLY_PROP_POWER_AVG
:
747 ret
= bq27x00_simple_value(di
->cache
.power_avg
, val
);
749 case POWER_SUPPLY_PROP_HEALTH
:
750 ret
= bq27x00_simple_value(di
->cache
.health
, val
);
759 static void bq27x00_external_power_changed(struct power_supply
*psy
)
761 struct bq27x00_device_info
*di
= power_supply_get_drvdata(psy
);
763 cancel_delayed_work_sync(&di
->work
);
764 schedule_delayed_work(&di
->work
, 0);
767 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
,
771 struct power_supply_desc
*psy_desc
;
772 struct power_supply_config psy_cfg
= { .drv_data
= di
, };
774 psy_desc
= devm_kzalloc(di
->dev
, sizeof(*psy_desc
), GFP_KERNEL
);
778 psy_desc
->name
= name
;
779 psy_desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
780 if (di
->chip
== BQ27425
) {
781 psy_desc
->properties
= bq27425_battery_props
;
782 psy_desc
->num_properties
= ARRAY_SIZE(bq27425_battery_props
);
783 } else if (di
->chip
== BQ27742
) {
784 psy_desc
->properties
= bq27742_battery_props
;
785 psy_desc
->num_properties
= ARRAY_SIZE(bq27742_battery_props
);
786 } else if (di
->chip
== BQ27510
) {
787 psy_desc
->properties
= bq27510_battery_props
;
788 psy_desc
->num_properties
= ARRAY_SIZE(bq27510_battery_props
);
790 psy_desc
->properties
= bq27x00_battery_props
;
791 psy_desc
->num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
793 psy_desc
->get_property
= bq27x00_battery_get_property
;
794 psy_desc
->external_power_changed
= bq27x00_external_power_changed
;
796 INIT_DELAYED_WORK(&di
->work
, bq27x00_battery_poll
);
797 mutex_init(&di
->lock
);
799 di
->bat
= power_supply_register_no_ws(di
->dev
, psy_desc
, &psy_cfg
);
800 if (IS_ERR(di
->bat
)) {
801 ret
= PTR_ERR(di
->bat
);
802 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
806 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
813 static void bq27x00_powersupply_unregister(struct bq27x00_device_info
*di
)
816 * power_supply_unregister call bq27x00_battery_get_property which
817 * call bq27x00_battery_poll.
818 * Make sure that bq27x00_battery_poll will not call
819 * schedule_delayed_work again after unregister (which cause OOPS).
823 cancel_delayed_work_sync(&di
->work
);
825 power_supply_unregister(di
->bat
);
827 mutex_destroy(&di
->lock
);
831 /* i2c specific code */
832 #ifdef CONFIG_BATTERY_BQ27X00_I2C
834 /* If the system has several batteries we need a different name for each
837 static DEFINE_IDR(battery_id
);
838 static DEFINE_MUTEX(battery_mutex
);
840 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
842 struct i2c_client
*client
= to_i2c_client(di
->dev
);
843 struct i2c_msg msg
[2];
844 unsigned char data
[2];
847 if (!client
->adapter
)
850 msg
[0].addr
= client
->addr
;
853 msg
[0].len
= sizeof(reg
);
854 msg
[1].addr
= client
->addr
;
855 msg
[1].flags
= I2C_M_RD
;
862 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
867 ret
= get_unaligned_le16(data
);
874 static int bq27x00_battery_probe(struct i2c_client
*client
,
875 const struct i2c_device_id
*id
)
878 struct bq27x00_device_info
*di
;
882 /* Get new ID for the new battery device */
883 mutex_lock(&battery_mutex
);
884 num
= idr_alloc(&battery_id
, client
, 0, 0, GFP_KERNEL
);
885 mutex_unlock(&battery_mutex
);
889 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "%s-%d", id
->name
, num
);
891 dev_err(&client
->dev
, "failed to allocate device name\n");
896 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
898 dev_err(&client
->dev
, "failed to allocate device info data\n");
904 di
->dev
= &client
->dev
;
905 di
->chip
= id
->driver_data
;
906 di
->bus
.read
= &bq27x00_read_i2c
;
908 retval
= bq27x00_powersupply_init(di
, name
);
912 i2c_set_clientdata(client
, di
);
917 mutex_lock(&battery_mutex
);
918 idr_remove(&battery_id
, num
);
919 mutex_unlock(&battery_mutex
);
924 static int bq27x00_battery_remove(struct i2c_client
*client
)
926 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
928 bq27x00_powersupply_unregister(di
);
930 mutex_lock(&battery_mutex
);
931 idr_remove(&battery_id
, di
->id
);
932 mutex_unlock(&battery_mutex
);
937 static const struct i2c_device_id bq27x00_id
[] = {
938 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
939 { "bq27500", BQ27500
},
940 { "bq27425", BQ27425
},
941 { "bq27742", BQ27742
},
942 { "bq27510", BQ27510
},
945 MODULE_DEVICE_TABLE(i2c
, bq27x00_id
);
947 static struct i2c_driver bq27x00_battery_driver
= {
949 .name
= "bq27x00-battery",
951 .probe
= bq27x00_battery_probe
,
952 .remove
= bq27x00_battery_remove
,
953 .id_table
= bq27x00_id
,
956 static inline int bq27x00_battery_i2c_init(void)
958 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
960 printk(KERN_ERR
"Unable to register BQ27x00 i2c driver\n");
965 static inline void bq27x00_battery_i2c_exit(void)
967 i2c_del_driver(&bq27x00_battery_driver
);
972 static inline int bq27x00_battery_i2c_init(void) { return 0; }
973 static inline void bq27x00_battery_i2c_exit(void) {};
977 /* platform specific code */
978 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
980 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
983 struct device
*dev
= di
->dev
;
984 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
985 unsigned int timeout
= 3;
990 /* Make sure the value has not changed in between reading the
991 * lower and the upper part */
992 upper
= pdata
->read(dev
, reg
+ 1);
998 lower
= pdata
->read(dev
, reg
);
1002 upper
= pdata
->read(dev
, reg
+ 1);
1003 } while (temp
!= upper
&& --timeout
);
1008 return (upper
<< 8) | lower
;
1011 return pdata
->read(dev
, reg
);
1014 static int bq27000_battery_probe(struct platform_device
*pdev
)
1016 struct bq27x00_device_info
*di
;
1017 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
1021 dev_err(&pdev
->dev
, "no platform_data supplied\n");
1026 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
1030 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
1032 dev_err(&pdev
->dev
, "failed to allocate device info data\n");
1036 platform_set_drvdata(pdev
, di
);
1038 di
->dev
= &pdev
->dev
;
1041 name
= pdata
->name
?: dev_name(&pdev
->dev
);
1042 di
->bus
.read
= &bq27000_read_platform
;
1044 return bq27x00_powersupply_init(di
, name
);
1047 static int bq27000_battery_remove(struct platform_device
*pdev
)
1049 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
1051 bq27x00_powersupply_unregister(di
);
1056 static struct platform_driver bq27000_battery_driver
= {
1057 .probe
= bq27000_battery_probe
,
1058 .remove
= bq27000_battery_remove
,
1060 .name
= "bq27000-battery",
1064 static inline int bq27x00_battery_platform_init(void)
1066 int ret
= platform_driver_register(&bq27000_battery_driver
);
1068 printk(KERN_ERR
"Unable to register BQ27000 platform driver\n");
1073 static inline void bq27x00_battery_platform_exit(void)
1075 platform_driver_unregister(&bq27000_battery_driver
);
1080 static inline int bq27x00_battery_platform_init(void) { return 0; }
1081 static inline void bq27x00_battery_platform_exit(void) {};
1089 static int __init
bq27x00_battery_init(void)
1093 ret
= bq27x00_battery_i2c_init();
1097 ret
= bq27x00_battery_platform_init();
1099 bq27x00_battery_i2c_exit();
1103 module_init(bq27x00_battery_init
);
1105 static void __exit
bq27x00_battery_exit(void)
1107 bq27x00_battery_platform_exit();
1108 bq27x00_battery_i2c_exit();
1110 module_exit(bq27x00_battery_exit
);
1112 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
1113 MODULE_ALIAS("platform:bq27000-battery");
1116 #ifdef CONFIG_BATTERY_BQ27X00_I2C
1117 MODULE_ALIAS("i2c:bq27000-battery");
1120 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1121 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1122 MODULE_LICENSE("GPL");