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 BQ27XXX_MANUFACTURER "Texas Instruments"
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 #define BQ27510_REG_SOC 0x20
78 #define BQ27510_REG_DCAP 0x2E /* Design capacity */
79 #define BQ27510_REG_CYCT 0x1E /* Cycle count total */
81 /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
82 #define BQ27425_REG_OFFSET 0x04
83 #define BQ27425_REG_SOC (0x1C + BQ27425_REG_OFFSET)
84 #define BQ27425_REG_DCAP (0x3C + BQ27425_REG_OFFSET)
86 #define BQ27000_RS 20 /* Resistor sense */
87 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
89 struct bq27x00_device_info
;
90 struct bq27x00_access_methods
{
91 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
94 enum bq27x00_chip
{ BQ27000
, BQ27500
, BQ27425
, BQ27742
, BQ27510
};
96 struct bq27x00_reg_cache
{
99 int time_to_empty_avg
;
110 struct bq27x00_device_info
{
113 enum bq27x00_chip chip
;
115 struct bq27x00_reg_cache cache
;
116 int charge_design_full
;
118 unsigned long last_update
;
119 struct delayed_work work
;
121 struct power_supply
*bat
;
123 struct bq27x00_access_methods bus
;
128 static enum power_supply_property bq27x00_battery_props
[] = {
129 POWER_SUPPLY_PROP_STATUS
,
130 POWER_SUPPLY_PROP_PRESENT
,
131 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
132 POWER_SUPPLY_PROP_CURRENT_NOW
,
133 POWER_SUPPLY_PROP_CAPACITY
,
134 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
135 POWER_SUPPLY_PROP_TEMP
,
136 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
137 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
138 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
139 POWER_SUPPLY_PROP_TECHNOLOGY
,
140 POWER_SUPPLY_PROP_CHARGE_FULL
,
141 POWER_SUPPLY_PROP_CHARGE_NOW
,
142 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
143 POWER_SUPPLY_PROP_CYCLE_COUNT
,
144 POWER_SUPPLY_PROP_ENERGY_NOW
,
145 POWER_SUPPLY_PROP_POWER_AVG
,
146 POWER_SUPPLY_PROP_HEALTH
,
147 POWER_SUPPLY_PROP_MANUFACTURER
,
150 static enum power_supply_property bq27425_battery_props
[] = {
151 POWER_SUPPLY_PROP_STATUS
,
152 POWER_SUPPLY_PROP_PRESENT
,
153 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
154 POWER_SUPPLY_PROP_CURRENT_NOW
,
155 POWER_SUPPLY_PROP_CAPACITY
,
156 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
157 POWER_SUPPLY_PROP_TEMP
,
158 POWER_SUPPLY_PROP_TECHNOLOGY
,
159 POWER_SUPPLY_PROP_CHARGE_FULL
,
160 POWER_SUPPLY_PROP_CHARGE_NOW
,
161 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
162 POWER_SUPPLY_PROP_MANUFACTURER
,
165 static enum power_supply_property bq27742_battery_props
[] = {
166 POWER_SUPPLY_PROP_STATUS
,
167 POWER_SUPPLY_PROP_PRESENT
,
168 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
169 POWER_SUPPLY_PROP_CURRENT_NOW
,
170 POWER_SUPPLY_PROP_CAPACITY
,
171 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
172 POWER_SUPPLY_PROP_TEMP
,
173 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
174 POWER_SUPPLY_PROP_TECHNOLOGY
,
175 POWER_SUPPLY_PROP_CHARGE_FULL
,
176 POWER_SUPPLY_PROP_CHARGE_NOW
,
177 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
178 POWER_SUPPLY_PROP_CYCLE_COUNT
,
179 POWER_SUPPLY_PROP_POWER_AVG
,
180 POWER_SUPPLY_PROP_HEALTH
,
181 POWER_SUPPLY_PROP_MANUFACTURER
,
184 static enum power_supply_property bq27510_battery_props
[] = {
185 POWER_SUPPLY_PROP_STATUS
,
186 POWER_SUPPLY_PROP_PRESENT
,
187 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
188 POWER_SUPPLY_PROP_CURRENT_NOW
,
189 POWER_SUPPLY_PROP_CAPACITY
,
190 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
191 POWER_SUPPLY_PROP_TEMP
,
192 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
193 POWER_SUPPLY_PROP_TECHNOLOGY
,
194 POWER_SUPPLY_PROP_CHARGE_FULL
,
195 POWER_SUPPLY_PROP_CHARGE_NOW
,
196 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
197 POWER_SUPPLY_PROP_CYCLE_COUNT
,
198 POWER_SUPPLY_PROP_POWER_AVG
,
199 POWER_SUPPLY_PROP_HEALTH
,
200 POWER_SUPPLY_PROP_MANUFACTURER
,
203 static unsigned int poll_interval
= 360;
204 module_param(poll_interval
, uint
, 0644);
205 MODULE_PARM_DESC(poll_interval
,
206 "battery poll interval in seconds - 0 disables polling");
209 * Common code for BQ27x00 devices
212 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
215 if (di
->chip
== BQ27425
)
216 return di
->bus
.read(di
, reg
- BQ27425_REG_OFFSET
, single
);
217 return di
->bus
.read(di
, reg
, single
);
221 * Higher versions of the chip like BQ27425 and BQ27500
222 * differ from BQ27000 and BQ27200 in calculation of certain
223 * parameters. Hence we need to check for the chip type.
225 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info
*di
)
227 if (di
->chip
== BQ27425
|| di
->chip
== BQ27500
|| di
->chip
== BQ27742
228 || di
->chip
== BQ27510
)
234 * Return the battery Relative State-of-Charge
235 * Or < 0 if something fails.
237 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
241 if (di
->chip
== BQ27500
|| di
->chip
== BQ27742
)
242 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
243 else if (di
->chip
== BQ27510
)
244 rsoc
= bq27x00_read(di
, BQ27510_REG_SOC
, false);
245 else if (di
->chip
== BQ27425
)
246 rsoc
= bq27x00_read(di
, BQ27425_REG_SOC
, false);
248 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
251 dev_dbg(di
->dev
, "error reading relative State-of-Charge\n");
257 * Return a battery charge value in µAh
258 * Or < 0 if something fails.
260 static int bq27x00_battery_read_charge(struct bq27x00_device_info
*di
, u8 reg
)
264 charge
= bq27x00_read(di
, reg
, false);
266 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
271 if (bq27xxx_is_chip_version_higher(di
))
274 charge
= charge
* 3570 / BQ27000_RS
;
280 * Return the battery Nominal available capaciy in µAh
281 * Or < 0 if something fails.
283 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info
*di
)
286 bool is_bq27500
= di
->chip
== BQ27500
;
287 bool is_bq27742
= di
->chip
== BQ27742
;
288 bool is_higher
= bq27xxx_is_chip_version_higher(di
);
289 bool flags_1b
= !(is_bq27500
|| is_bq27742
);
291 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, flags_1b
);
292 if (flags
>= 0 && !is_higher
&& (flags
& BQ27000_FLAG_CI
))
295 return bq27x00_battery_read_charge(di
, BQ27x00_REG_NAC
);
299 * Return the battery Last measured discharge in µAh
300 * Or < 0 if something fails.
302 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info
*di
)
304 return bq27x00_battery_read_charge(di
, BQ27x00_REG_LMD
);
308 * Return the battery Initial last measured discharge in µAh
309 * Or < 0 if something fails.
311 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info
*di
)
315 if (bq27xxx_is_chip_version_higher(di
)) {
316 if (di
->chip
== BQ27425
)
317 ilmd
= bq27x00_read(di
, BQ27425_REG_DCAP
, false);
318 else if (di
->chip
== BQ27510
)
319 ilmd
= bq27x00_read(di
, BQ27510_REG_DCAP
, false);
321 ilmd
= bq27x00_read(di
, BQ27500_REG_DCAP
, false);
323 ilmd
= bq27x00_read(di
, BQ27000_REG_ILMD
, true);
327 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
331 if (bq27xxx_is_chip_version_higher(di
))
334 ilmd
= ilmd
* 256 * 3570 / BQ27000_RS
;
340 * Return the battery Available energy in µWh
341 * Or < 0 if something fails.
343 static int bq27x00_battery_read_energy(struct bq27x00_device_info
*di
)
347 ae
= bq27x00_read(di
, BQ27x00_REG_AE
, false);
349 dev_dbg(di
->dev
, "error reading available energy\n");
353 if (di
->chip
== BQ27500
)
356 ae
= ae
* 29200 / BQ27000_RS
;
362 * Return the battery temperature in tenths of degree Kelvin
363 * Or < 0 if something fails.
365 static int bq27x00_battery_read_temperature(struct bq27x00_device_info
*di
)
369 temp
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
371 dev_err(di
->dev
, "error reading temperature\n");
375 if (!bq27xxx_is_chip_version_higher(di
))
382 * Return the battery Cycle count total
383 * Or < 0 if something fails.
385 static int bq27x00_battery_read_cyct(struct bq27x00_device_info
*di
)
389 if (di
->chip
== BQ27510
)
390 cyct
= bq27x00_read(di
, BQ27510_REG_CYCT
, false);
392 cyct
= bq27x00_read(di
, BQ27x00_REG_CYCT
, false);
394 dev_err(di
->dev
, "error reading cycle count total\n");
400 * Read a time register.
401 * Return < 0 if something fails.
403 static int bq27x00_battery_read_time(struct bq27x00_device_info
*di
, u8 reg
)
407 tval
= bq27x00_read(di
, reg
, false);
409 dev_dbg(di
->dev
, "error reading time register %02x: %d\n",
421 * Read a power avg register.
422 * Return < 0 if something fails.
424 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info
*di
, u8 reg
)
428 tval
= bq27x00_read(di
, reg
, false);
430 dev_err(di
->dev
, "error reading power avg rgister %02x: %d\n",
435 if (di
->chip
== BQ27500
)
438 return (tval
* BQ27x00_POWER_CONSTANT
) / BQ27000_RS
;
442 * Read flag register.
443 * Return < 0 if something fails.
445 static int bq27x00_battery_read_health(struct bq27x00_device_info
*di
)
449 tval
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
451 dev_err(di
->dev
, "error reading flag register:%d\n", tval
);
455 if (di
->chip
== BQ27500
) {
456 if (tval
& BQ27500_FLAG_SOCF
)
457 tval
= POWER_SUPPLY_HEALTH_DEAD
;
458 else if (tval
& BQ27500_FLAG_OTC
)
459 tval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
461 tval
= POWER_SUPPLY_HEALTH_GOOD
;
463 } else if (di
->chip
== BQ27510
) {
464 if (tval
& BQ27500_FLAG_OTC
)
465 return POWER_SUPPLY_HEALTH_OVERHEAT
;
466 return POWER_SUPPLY_HEALTH_GOOD
;
468 if (tval
& BQ27000_FLAG_EDV1
)
469 tval
= POWER_SUPPLY_HEALTH_DEAD
;
471 tval
= POWER_SUPPLY_HEALTH_GOOD
;
478 static void bq27x00_update(struct bq27x00_device_info
*di
)
480 struct bq27x00_reg_cache cache
= {0, };
481 bool is_bq27500
= di
->chip
== BQ27500
;
482 bool is_bq27510
= di
->chip
== BQ27510
;
483 bool is_bq27425
= di
->chip
== BQ27425
;
484 bool is_bq27742
= di
->chip
== BQ27742
;
485 bool flags_1b
= !(is_bq27500
|| is_bq27742
);
487 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, flags_1b
);
488 if ((cache
.flags
& 0xff) == 0xff)
491 if (cache
.flags
>= 0) {
492 if (!is_bq27500
&& !is_bq27425
&& !is_bq27742
&& !is_bq27510
493 && (cache
.flags
& BQ27000_FLAG_CI
)) {
494 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
495 cache
.capacity
= -ENODATA
;
496 cache
.energy
= -ENODATA
;
497 cache
.time_to_empty
= -ENODATA
;
498 cache
.time_to_empty_avg
= -ENODATA
;
499 cache
.time_to_full
= -ENODATA
;
500 cache
.charge_full
= -ENODATA
;
501 cache
.health
= -ENODATA
;
503 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
504 if (is_bq27742
|| is_bq27510
)
505 cache
.time_to_empty
=
506 bq27x00_battery_read_time(di
,
508 else if (!is_bq27425
) {
509 cache
.energy
= bq27x00_battery_read_energy(di
);
510 cache
.time_to_empty
=
511 bq27x00_battery_read_time(di
,
513 cache
.time_to_empty_avg
=
514 bq27x00_battery_read_time(di
,
517 bq27x00_battery_read_time(di
,
520 cache
.charge_full
= bq27x00_battery_read_lmd(di
);
521 cache
.health
= bq27x00_battery_read_health(di
);
523 cache
.temperature
= bq27x00_battery_read_temperature(di
);
525 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
528 bq27x00_battery_read_pwr_avg(di
,
532 bq27x00_battery_read_pwr_avg(di
,
535 /* We only have to read charge design full once */
536 if (di
->charge_design_full
<= 0)
537 di
->charge_design_full
= bq27x00_battery_read_ilmd(di
);
540 if (di
->cache
.capacity
!= cache
.capacity
)
541 power_supply_changed(di
->bat
);
543 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0)
546 di
->last_update
= jiffies
;
549 static void bq27x00_battery_poll(struct work_struct
*work
)
551 struct bq27x00_device_info
*di
=
552 container_of(work
, struct bq27x00_device_info
, work
.work
);
556 if (poll_interval
> 0) {
557 /* The timer does not have to be accurate. */
558 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
559 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
564 * Return the battery average current in µA
565 * Note that current can be negative signed as well
566 * Or 0 if something fails.
568 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
569 union power_supply_propval
*val
)
574 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
576 dev_err(di
->dev
, "error reading current\n");
580 if (bq27xxx_is_chip_version_higher(di
)) {
581 /* bq27500 returns signed value */
582 val
->intval
= (int)((s16
)curr
) * 1000;
584 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
585 if (flags
& BQ27000_FLAG_CHGS
) {
586 dev_dbg(di
->dev
, "negative current!\n");
590 val
->intval
= curr
* 3570 / BQ27000_RS
;
596 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
597 union power_supply_propval
*val
)
601 if (bq27xxx_is_chip_version_higher(di
)) {
602 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
603 status
= POWER_SUPPLY_STATUS_FULL
;
604 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
605 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
607 status
= POWER_SUPPLY_STATUS_CHARGING
;
609 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
610 status
= POWER_SUPPLY_STATUS_FULL
;
611 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
612 status
= POWER_SUPPLY_STATUS_CHARGING
;
613 else if (power_supply_am_i_supplied(di
->bat
))
614 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
616 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
619 val
->intval
= status
;
624 static int bq27x00_battery_capacity_level(struct bq27x00_device_info
*di
,
625 union power_supply_propval
*val
)
629 if (bq27xxx_is_chip_version_higher(di
)) {
630 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
631 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
632 else if (di
->cache
.flags
& BQ27500_FLAG_SOC1
)
633 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
634 else if (di
->cache
.flags
& BQ27500_FLAG_SOCF
)
635 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
637 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
639 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
640 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
641 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
642 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
643 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
644 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
646 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
655 * Return the battery Voltage in millivolts
656 * Or < 0 if something fails.
658 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
659 union power_supply_propval
*val
)
663 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
665 dev_err(di
->dev
, "error reading voltage\n");
669 val
->intval
= volt
* 1000;
674 static int bq27x00_simple_value(int value
,
675 union power_supply_propval
*val
)
685 static int bq27x00_battery_get_property(struct power_supply
*psy
,
686 enum power_supply_property psp
,
687 union power_supply_propval
*val
)
690 struct bq27x00_device_info
*di
= power_supply_get_drvdata(psy
);
692 mutex_lock(&di
->lock
);
693 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
694 cancel_delayed_work_sync(&di
->work
);
695 bq27x00_battery_poll(&di
->work
.work
);
697 mutex_unlock(&di
->lock
);
699 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
703 case POWER_SUPPLY_PROP_STATUS
:
704 ret
= bq27x00_battery_status(di
, val
);
706 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
707 ret
= bq27x00_battery_voltage(di
, val
);
709 case POWER_SUPPLY_PROP_PRESENT
:
710 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
712 case POWER_SUPPLY_PROP_CURRENT_NOW
:
713 ret
= bq27x00_battery_current(di
, val
);
715 case POWER_SUPPLY_PROP_CAPACITY
:
716 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
718 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
719 ret
= bq27x00_battery_capacity_level(di
, val
);
721 case POWER_SUPPLY_PROP_TEMP
:
722 ret
= bq27x00_simple_value(di
->cache
.temperature
, val
);
726 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
727 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
729 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
730 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
732 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
733 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
735 case POWER_SUPPLY_PROP_TECHNOLOGY
:
736 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
738 case POWER_SUPPLY_PROP_CHARGE_NOW
:
739 ret
= bq27x00_simple_value(bq27x00_battery_read_nac(di
), val
);
741 case POWER_SUPPLY_PROP_CHARGE_FULL
:
742 ret
= bq27x00_simple_value(di
->cache
.charge_full
, val
);
744 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
745 ret
= bq27x00_simple_value(di
->charge_design_full
, val
);
747 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
748 ret
= bq27x00_simple_value(di
->cache
.cycle_count
, val
);
750 case POWER_SUPPLY_PROP_ENERGY_NOW
:
751 ret
= bq27x00_simple_value(di
->cache
.energy
, val
);
753 case POWER_SUPPLY_PROP_POWER_AVG
:
754 ret
= bq27x00_simple_value(di
->cache
.power_avg
, val
);
756 case POWER_SUPPLY_PROP_HEALTH
:
757 ret
= bq27x00_simple_value(di
->cache
.health
, val
);
759 case POWER_SUPPLY_PROP_MANUFACTURER
:
760 val
->strval
= BQ27XXX_MANUFACTURER
;
769 static void bq27x00_external_power_changed(struct power_supply
*psy
)
771 struct bq27x00_device_info
*di
= power_supply_get_drvdata(psy
);
773 cancel_delayed_work_sync(&di
->work
);
774 schedule_delayed_work(&di
->work
, 0);
777 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
,
781 struct power_supply_desc
*psy_desc
;
782 struct power_supply_config psy_cfg
= { .drv_data
= di
, };
784 psy_desc
= devm_kzalloc(di
->dev
, sizeof(*psy_desc
), GFP_KERNEL
);
788 psy_desc
->name
= name
;
789 psy_desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
790 if (di
->chip
== BQ27425
) {
791 psy_desc
->properties
= bq27425_battery_props
;
792 psy_desc
->num_properties
= ARRAY_SIZE(bq27425_battery_props
);
793 } else if (di
->chip
== BQ27742
) {
794 psy_desc
->properties
= bq27742_battery_props
;
795 psy_desc
->num_properties
= ARRAY_SIZE(bq27742_battery_props
);
796 } else if (di
->chip
== BQ27510
) {
797 psy_desc
->properties
= bq27510_battery_props
;
798 psy_desc
->num_properties
= ARRAY_SIZE(bq27510_battery_props
);
800 psy_desc
->properties
= bq27x00_battery_props
;
801 psy_desc
->num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
803 psy_desc
->get_property
= bq27x00_battery_get_property
;
804 psy_desc
->external_power_changed
= bq27x00_external_power_changed
;
806 INIT_DELAYED_WORK(&di
->work
, bq27x00_battery_poll
);
807 mutex_init(&di
->lock
);
809 di
->bat
= power_supply_register_no_ws(di
->dev
, psy_desc
, &psy_cfg
);
810 if (IS_ERR(di
->bat
)) {
811 ret
= PTR_ERR(di
->bat
);
812 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
816 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
823 static void bq27x00_powersupply_unregister(struct bq27x00_device_info
*di
)
826 * power_supply_unregister call bq27x00_battery_get_property which
827 * call bq27x00_battery_poll.
828 * Make sure that bq27x00_battery_poll will not call
829 * schedule_delayed_work again after unregister (which cause OOPS).
833 cancel_delayed_work_sync(&di
->work
);
835 power_supply_unregister(di
->bat
);
837 mutex_destroy(&di
->lock
);
840 /* i2c specific code */
841 #ifdef CONFIG_BATTERY_BQ27X00_I2C
843 /* If the system has several batteries we need a different name for each
846 static DEFINE_IDR(battery_id
);
847 static DEFINE_MUTEX(battery_mutex
);
849 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
851 struct i2c_client
*client
= to_i2c_client(di
->dev
);
852 struct i2c_msg msg
[2];
853 unsigned char data
[2];
856 if (!client
->adapter
)
859 msg
[0].addr
= client
->addr
;
862 msg
[0].len
= sizeof(reg
);
863 msg
[1].addr
= client
->addr
;
864 msg
[1].flags
= I2C_M_RD
;
871 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
876 ret
= get_unaligned_le16(data
);
883 static int bq27x00_battery_probe(struct i2c_client
*client
,
884 const struct i2c_device_id
*id
)
887 struct bq27x00_device_info
*di
;
891 /* Get new ID for the new battery device */
892 mutex_lock(&battery_mutex
);
893 num
= idr_alloc(&battery_id
, client
, 0, 0, GFP_KERNEL
);
894 mutex_unlock(&battery_mutex
);
898 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "%s-%d", id
->name
, num
);
904 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
911 di
->dev
= &client
->dev
;
912 di
->chip
= id
->driver_data
;
913 di
->bus
.read
= &bq27x00_read_i2c
;
915 retval
= bq27x00_powersupply_init(di
, name
);
919 i2c_set_clientdata(client
, di
);
924 mutex_lock(&battery_mutex
);
925 idr_remove(&battery_id
, num
);
926 mutex_unlock(&battery_mutex
);
931 static int bq27x00_battery_remove(struct i2c_client
*client
)
933 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
935 bq27x00_powersupply_unregister(di
);
937 mutex_lock(&battery_mutex
);
938 idr_remove(&battery_id
, di
->id
);
939 mutex_unlock(&battery_mutex
);
944 static const struct i2c_device_id bq27x00_id
[] = {
945 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
946 { "bq27500", BQ27500
},
947 { "bq27425", BQ27425
},
948 { "bq27742", BQ27742
},
949 { "bq27510", BQ27510
},
952 MODULE_DEVICE_TABLE(i2c
, bq27x00_id
);
954 static struct i2c_driver bq27x00_battery_driver
= {
956 .name
= "bq27x00-battery",
958 .probe
= bq27x00_battery_probe
,
959 .remove
= bq27x00_battery_remove
,
960 .id_table
= bq27x00_id
,
963 static inline int bq27x00_battery_i2c_init(void)
965 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
968 pr_err("Unable to register BQ27x00 i2c driver\n");
973 static inline void bq27x00_battery_i2c_exit(void)
975 i2c_del_driver(&bq27x00_battery_driver
);
980 static inline int bq27x00_battery_i2c_init(void) { return 0; }
981 static inline void bq27x00_battery_i2c_exit(void) {};
985 /* platform specific code */
986 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
988 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
991 struct device
*dev
= di
->dev
;
992 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
993 unsigned int timeout
= 3;
998 /* Make sure the value has not changed in between reading the
999 * lower and the upper part */
1000 upper
= pdata
->read(dev
, reg
+ 1);
1006 lower
= pdata
->read(dev
, reg
);
1010 upper
= pdata
->read(dev
, reg
+ 1);
1011 } while (temp
!= upper
&& --timeout
);
1016 return (upper
<< 8) | lower
;
1019 return pdata
->read(dev
, reg
);
1022 static int bq27000_battery_probe(struct platform_device
*pdev
)
1024 struct bq27x00_device_info
*di
;
1025 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
1029 dev_err(&pdev
->dev
, "no platform_data supplied\n");
1034 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
1038 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
1042 platform_set_drvdata(pdev
, di
);
1044 di
->dev
= &pdev
->dev
;
1047 name
= pdata
->name
?: dev_name(&pdev
->dev
);
1048 di
->bus
.read
= &bq27000_read_platform
;
1050 return bq27x00_powersupply_init(di
, name
);
1053 static int bq27000_battery_remove(struct platform_device
*pdev
)
1055 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
1057 bq27x00_powersupply_unregister(di
);
1062 static struct platform_driver bq27000_battery_driver
= {
1063 .probe
= bq27000_battery_probe
,
1064 .remove
= bq27000_battery_remove
,
1066 .name
= "bq27000-battery",
1070 static inline int bq27x00_battery_platform_init(void)
1072 int ret
= platform_driver_register(&bq27000_battery_driver
);
1075 pr_err("Unable to register BQ27000 platform driver\n");
1080 static inline void bq27x00_battery_platform_exit(void)
1082 platform_driver_unregister(&bq27000_battery_driver
);
1087 static inline int bq27x00_battery_platform_init(void) { return 0; }
1088 static inline void bq27x00_battery_platform_exit(void) {};
1096 static int __init
bq27x00_battery_init(void)
1100 ret
= bq27x00_battery_i2c_init();
1104 ret
= bq27x00_battery_platform_init();
1106 bq27x00_battery_i2c_exit();
1110 module_init(bq27x00_battery_init
);
1112 static void __exit
bq27x00_battery_exit(void)
1114 bq27x00_battery_platform_exit();
1115 bq27x00_battery_i2c_exit();
1117 module_exit(bq27x00_battery_exit
);
1119 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
1120 MODULE_ALIAS("platform:bq27000-battery");
1123 #ifdef CONFIG_BATTERY_BQ27X00_I2C
1124 MODULE_ALIAS("i2c:bq27000-battery");
1127 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1128 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1129 MODULE_LICENSE("GPL");