2 * BQ27xxx 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://www.ti.com/product/bq27000
21 * http://www.ti.com/product/bq27200
22 * http://www.ti.com/product/bq27010
23 * http://www.ti.com/product/bq27210
24 * http://www.ti.com/product/bq27500
25 * http://www.ti.com/product/bq27510-g3
26 * http://www.ti.com/product/bq27520-g4
27 * http://www.ti.com/product/bq27530-g1
28 * http://www.ti.com/product/bq27531-g1
29 * http://www.ti.com/product/bq27541-g1
30 * http://www.ti.com/product/bq27542-g1
31 * http://www.ti.com/product/bq27546-g1
32 * http://www.ti.com/product/bq27742-g1
33 * http://www.ti.com/product/bq27545-g1
34 * http://www.ti.com/product/bq27421-g1
35 * http://www.ti.com/product/bq27425-g1
36 * http://www.ti.com/product/bq27411-g1
37 * http://www.ti.com/product/bq27621-g1
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/param.h>
43 #include <linux/jiffies.h>
44 #include <linux/workqueue.h>
45 #include <linux/delay.h>
46 #include <linux/platform_device.h>
47 #include <linux/power_supply.h>
48 #include <linux/idr.h>
49 #include <linux/i2c.h>
50 #include <linux/slab.h>
51 #include <linux/interrupt.h>
52 #include <asm/unaligned.h>
54 #include <linux/power/bq27xxx_battery.h>
56 #define DRIVER_VERSION "1.2.0"
58 #define BQ27XXX_MANUFACTURER "Texas Instruments"
61 #define BQ27XXX_FLAG_DSC BIT(0)
62 #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
63 #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
64 #define BQ27XXX_FLAG_FC BIT(9)
65 #define BQ27XXX_FLAG_OTD BIT(14)
66 #define BQ27XXX_FLAG_OTC BIT(15)
67 #define BQ27XXX_FLAG_UT BIT(14)
68 #define BQ27XXX_FLAG_OT BIT(15)
70 /* BQ27000 has different layout for Flags register */
71 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
72 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
73 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
74 #define BQ27000_FLAG_FC BIT(5)
75 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
77 #define BQ27XXX_RS (20) /* Resistor sense mOhm */
78 #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
79 #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
81 struct bq27xxx_device_info
;
82 struct bq27xxx_access_methods
{
83 int (*read
)(struct bq27xxx_device_info
*di
, u8 reg
, bool single
);
86 #define INVALID_REG_ADDR 0xff
89 * bq27xxx_reg_index - Register names
91 * These are indexes into a device's register mapping array.
93 enum bq27xxx_reg_index
{
94 BQ27XXX_REG_CTRL
= 0, /* Control */
95 BQ27XXX_REG_TEMP
, /* Temperature */
96 BQ27XXX_REG_INT_TEMP
, /* Internal Temperature */
97 BQ27XXX_REG_VOLT
, /* Voltage */
98 BQ27XXX_REG_AI
, /* Average Current */
99 BQ27XXX_REG_FLAGS
, /* Flags */
100 BQ27XXX_REG_TTE
, /* Time-to-Empty */
101 BQ27XXX_REG_TTF
, /* Time-to-Full */
102 BQ27XXX_REG_TTES
, /* Time-to-Empty Standby */
103 BQ27XXX_REG_TTECP
, /* Time-to-Empty at Constant Power */
104 BQ27XXX_REG_NAC
, /* Nominal Available Capacity */
105 BQ27XXX_REG_FCC
, /* Full Charge Capacity */
106 BQ27XXX_REG_CYCT
, /* Cycle Count */
107 BQ27XXX_REG_AE
, /* Available Energy */
108 BQ27XXX_REG_SOC
, /* State-of-Charge */
109 BQ27XXX_REG_DCAP
, /* Design Capacity */
110 BQ27XXX_REG_AP
, /* Average Power */
113 struct bq27xxx_reg_cache
{
116 int time_to_empty_avg
;
127 struct bq27xxx_device_info
{
130 enum bq27xxx_chip chip
;
132 struct bq27xxx_reg_cache cache
;
133 int charge_design_full
;
135 unsigned long last_update
;
136 struct delayed_work work
;
138 struct power_supply
*bat
;
140 struct bq27xxx_access_methods bus
;
147 /* Register mappings */
148 static u8 bq27000_regs
[] = {
151 INVALID_REG_ADDR
, /* INT TEMP - NA*/
163 0x0b, /* SOC(RSOC) */
164 0x76, /* DCAP(ILMD) */
168 static u8 bq27010_regs
[] = {
171 INVALID_REG_ADDR
, /* INT TEMP - NA*/
182 INVALID_REG_ADDR
, /* AE - NA */
183 0x0b, /* SOC(RSOC) */
184 0x76, /* DCAP(ILMD) */
185 INVALID_REG_ADDR
, /* AP - NA */
188 static u8 bq27500_regs
[] = {
196 INVALID_REG_ADDR
, /* TTF - NA */
198 INVALID_REG_ADDR
, /* TTECP - NA */
202 INVALID_REG_ADDR
, /* AE - NA */
203 0x20, /* SOC(RSOC) */
204 0x2e, /* DCAP(ILMD) */
205 INVALID_REG_ADDR
, /* AP - NA */
208 static u8 bq27530_regs
[] = {
216 INVALID_REG_ADDR
, /* TTF - NA */
217 INVALID_REG_ADDR
, /* TTES - NA */
218 INVALID_REG_ADDR
, /* TTECP - NA */
222 INVALID_REG_ADDR
, /* AE - NA */
223 0x2c, /* SOC(RSOC) */
224 INVALID_REG_ADDR
, /* DCAP - NA */
228 static u8 bq27541_regs
[] = {
236 INVALID_REG_ADDR
, /* TTF - NA */
237 INVALID_REG_ADDR
, /* TTES - NA */
238 INVALID_REG_ADDR
, /* TTECP - NA */
242 INVALID_REG_ADDR
, /* AE - NA */
243 0x2c, /* SOC(RSOC) */
248 static u8 bq27545_regs
[] = {
256 INVALID_REG_ADDR
, /* TTF - NA */
257 INVALID_REG_ADDR
, /* TTES - NA */
258 INVALID_REG_ADDR
, /* TTECP - NA */
262 INVALID_REG_ADDR
, /* AE - NA */
263 0x2c, /* SOC(RSOC) */
264 INVALID_REG_ADDR
, /* DCAP - NA */
268 static u8 bq27421_regs
[] = {
275 INVALID_REG_ADDR
, /* TTE - NA */
276 INVALID_REG_ADDR
, /* TTF - NA */
277 INVALID_REG_ADDR
, /* TTES - NA */
278 INVALID_REG_ADDR
, /* TTECP - NA */
281 INVALID_REG_ADDR
, /* CYCT - NA */
282 INVALID_REG_ADDR
, /* AE - NA */
288 static u8
*bq27xxx_regs
[] __maybe_unused
= {
289 [BQ27000
] = bq27000_regs
,
290 [BQ27010
] = bq27010_regs
,
291 [BQ27500
] = bq27500_regs
,
292 [BQ27530
] = bq27530_regs
,
293 [BQ27541
] = bq27541_regs
,
294 [BQ27545
] = bq27545_regs
,
295 [BQ27421
] = bq27421_regs
,
298 static enum power_supply_property bq27000_battery_props
[] = {
299 POWER_SUPPLY_PROP_STATUS
,
300 POWER_SUPPLY_PROP_PRESENT
,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
302 POWER_SUPPLY_PROP_CURRENT_NOW
,
303 POWER_SUPPLY_PROP_CAPACITY
,
304 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
305 POWER_SUPPLY_PROP_TEMP
,
306 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
307 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
308 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
309 POWER_SUPPLY_PROP_TECHNOLOGY
,
310 POWER_SUPPLY_PROP_CHARGE_FULL
,
311 POWER_SUPPLY_PROP_CHARGE_NOW
,
312 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
313 POWER_SUPPLY_PROP_CYCLE_COUNT
,
314 POWER_SUPPLY_PROP_ENERGY_NOW
,
315 POWER_SUPPLY_PROP_POWER_AVG
,
316 POWER_SUPPLY_PROP_HEALTH
,
317 POWER_SUPPLY_PROP_MANUFACTURER
,
320 static enum power_supply_property bq27010_battery_props
[] = {
321 POWER_SUPPLY_PROP_STATUS
,
322 POWER_SUPPLY_PROP_PRESENT
,
323 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
324 POWER_SUPPLY_PROP_CURRENT_NOW
,
325 POWER_SUPPLY_PROP_CAPACITY
,
326 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
327 POWER_SUPPLY_PROP_TEMP
,
328 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
329 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
330 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
331 POWER_SUPPLY_PROP_TECHNOLOGY
,
332 POWER_SUPPLY_PROP_CHARGE_FULL
,
333 POWER_SUPPLY_PROP_CHARGE_NOW
,
334 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
335 POWER_SUPPLY_PROP_CYCLE_COUNT
,
336 POWER_SUPPLY_PROP_HEALTH
,
337 POWER_SUPPLY_PROP_MANUFACTURER
,
340 static enum power_supply_property bq27500_battery_props
[] = {
341 POWER_SUPPLY_PROP_STATUS
,
342 POWER_SUPPLY_PROP_PRESENT
,
343 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
344 POWER_SUPPLY_PROP_CURRENT_NOW
,
345 POWER_SUPPLY_PROP_CAPACITY
,
346 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
347 POWER_SUPPLY_PROP_TEMP
,
348 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
349 POWER_SUPPLY_PROP_TECHNOLOGY
,
350 POWER_SUPPLY_PROP_CHARGE_FULL
,
351 POWER_SUPPLY_PROP_CHARGE_NOW
,
352 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
353 POWER_SUPPLY_PROP_CYCLE_COUNT
,
354 POWER_SUPPLY_PROP_HEALTH
,
355 POWER_SUPPLY_PROP_MANUFACTURER
,
358 static enum power_supply_property bq27530_battery_props
[] = {
359 POWER_SUPPLY_PROP_STATUS
,
360 POWER_SUPPLY_PROP_PRESENT
,
361 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
362 POWER_SUPPLY_PROP_CURRENT_NOW
,
363 POWER_SUPPLY_PROP_CAPACITY
,
364 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
365 POWER_SUPPLY_PROP_TEMP
,
366 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
367 POWER_SUPPLY_PROP_TECHNOLOGY
,
368 POWER_SUPPLY_PROP_CHARGE_FULL
,
369 POWER_SUPPLY_PROP_CHARGE_NOW
,
370 POWER_SUPPLY_PROP_POWER_AVG
,
371 POWER_SUPPLY_PROP_HEALTH
,
372 POWER_SUPPLY_PROP_CYCLE_COUNT
,
373 POWER_SUPPLY_PROP_MANUFACTURER
,
376 static enum power_supply_property bq27541_battery_props
[] = {
377 POWER_SUPPLY_PROP_STATUS
,
378 POWER_SUPPLY_PROP_PRESENT
,
379 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
380 POWER_SUPPLY_PROP_CURRENT_NOW
,
381 POWER_SUPPLY_PROP_CAPACITY
,
382 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
383 POWER_SUPPLY_PROP_TEMP
,
384 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
385 POWER_SUPPLY_PROP_TECHNOLOGY
,
386 POWER_SUPPLY_PROP_CHARGE_FULL
,
387 POWER_SUPPLY_PROP_CHARGE_NOW
,
388 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
389 POWER_SUPPLY_PROP_CYCLE_COUNT
,
390 POWER_SUPPLY_PROP_POWER_AVG
,
391 POWER_SUPPLY_PROP_HEALTH
,
392 POWER_SUPPLY_PROP_MANUFACTURER
,
395 static enum power_supply_property bq27545_battery_props
[] = {
396 POWER_SUPPLY_PROP_STATUS
,
397 POWER_SUPPLY_PROP_PRESENT
,
398 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
399 POWER_SUPPLY_PROP_CURRENT_NOW
,
400 POWER_SUPPLY_PROP_CAPACITY
,
401 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
402 POWER_SUPPLY_PROP_TEMP
,
403 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
404 POWER_SUPPLY_PROP_TECHNOLOGY
,
405 POWER_SUPPLY_PROP_CHARGE_FULL
,
406 POWER_SUPPLY_PROP_CHARGE_NOW
,
407 POWER_SUPPLY_PROP_HEALTH
,
408 POWER_SUPPLY_PROP_CYCLE_COUNT
,
409 POWER_SUPPLY_PROP_POWER_AVG
,
410 POWER_SUPPLY_PROP_MANUFACTURER
,
413 static enum power_supply_property bq27421_battery_props
[] = {
414 POWER_SUPPLY_PROP_STATUS
,
415 POWER_SUPPLY_PROP_PRESENT
,
416 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
417 POWER_SUPPLY_PROP_CURRENT_NOW
,
418 POWER_SUPPLY_PROP_CAPACITY
,
419 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
420 POWER_SUPPLY_PROP_TEMP
,
421 POWER_SUPPLY_PROP_TECHNOLOGY
,
422 POWER_SUPPLY_PROP_CHARGE_FULL
,
423 POWER_SUPPLY_PROP_CHARGE_NOW
,
424 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
425 POWER_SUPPLY_PROP_MANUFACTURER
,
428 #define BQ27XXX_PROP(_id, _prop) \
431 .size = ARRAY_SIZE(_prop), \
435 enum power_supply_property
*props
;
437 } bq27xxx_battery_props
[] = {
438 BQ27XXX_PROP(BQ27000
, bq27000_battery_props
),
439 BQ27XXX_PROP(BQ27010
, bq27010_battery_props
),
440 BQ27XXX_PROP(BQ27500
, bq27500_battery_props
),
441 BQ27XXX_PROP(BQ27530
, bq27530_battery_props
),
442 BQ27XXX_PROP(BQ27541
, bq27541_battery_props
),
443 BQ27XXX_PROP(BQ27545
, bq27545_battery_props
),
444 BQ27XXX_PROP(BQ27421
, bq27421_battery_props
),
447 static unsigned int poll_interval
= 360;
448 module_param(poll_interval
, uint
, 0644);
449 MODULE_PARM_DESC(poll_interval
,
450 "battery poll interval in seconds - 0 disables polling");
453 * Common code for BQ27xxx devices
456 static inline int bq27xxx_read(struct bq27xxx_device_info
*di
, int reg_index
,
459 /* Reports EINVAL for invalid/missing registers */
460 if (!di
|| di
->regs
[reg_index
] == INVALID_REG_ADDR
)
463 return di
->bus
.read(di
, di
->regs
[reg_index
], single
);
467 * Return the battery State-of-Charge
468 * Or < 0 if something fails.
470 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info
*di
)
474 soc
= bq27xxx_read(di
, BQ27XXX_REG_SOC
, false);
477 dev_dbg(di
->dev
, "error reading State-of-Charge\n");
483 * Return a battery charge value in µAh
484 * Or < 0 if something fails.
486 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info
*di
, u8 reg
)
490 charge
= bq27xxx_read(di
, reg
, false);
492 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
497 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
)
498 charge
*= BQ27XXX_CURRENT_CONSTANT
/ BQ27XXX_RS
;
506 * Return the battery Nominal available capacity in µAh
507 * Or < 0 if something fails.
509 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info
*di
)
513 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
) {
514 flags
= bq27xxx_read(di
, BQ27XXX_REG_FLAGS
, true);
515 if (flags
>= 0 && (flags
& BQ27000_FLAG_CI
))
519 return bq27xxx_battery_read_charge(di
, BQ27XXX_REG_NAC
);
523 * Return the battery Full Charge Capacity in µAh
524 * Or < 0 if something fails.
526 static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info
*di
)
528 return bq27xxx_battery_read_charge(di
, BQ27XXX_REG_FCC
);
532 * Return the Design Capacity in µAh
533 * Or < 0 if something fails.
535 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info
*di
)
539 dcap
= bq27xxx_read(di
, BQ27XXX_REG_DCAP
, false);
542 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
546 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
)
547 dcap
*= BQ27XXX_CURRENT_CONSTANT
/ BQ27XXX_RS
;
555 * Return the battery Available energy in µWh
556 * Or < 0 if something fails.
558 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info
*di
)
562 ae
= bq27xxx_read(di
, BQ27XXX_REG_AE
, false);
564 dev_dbg(di
->dev
, "error reading available energy\n");
568 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
)
569 ae
*= BQ27XXX_POWER_CONSTANT
/ BQ27XXX_RS
;
577 * Return the battery temperature in tenths of degree Kelvin
578 * Or < 0 if something fails.
580 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info
*di
)
584 temp
= bq27xxx_read(di
, BQ27XXX_REG_TEMP
, false);
586 dev_err(di
->dev
, "error reading temperature\n");
590 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
)
597 * Return the battery Cycle count total
598 * Or < 0 if something fails.
600 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info
*di
)
604 cyct
= bq27xxx_read(di
, BQ27XXX_REG_CYCT
, false);
606 dev_err(di
->dev
, "error reading cycle count total\n");
612 * Read a time register.
613 * Return < 0 if something fails.
615 static int bq27xxx_battery_read_time(struct bq27xxx_device_info
*di
, u8 reg
)
619 tval
= bq27xxx_read(di
, reg
, false);
621 dev_dbg(di
->dev
, "error reading time register %02x: %d\n",
633 * Read an average power register.
634 * Return < 0 if something fails.
636 static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info
*di
)
640 tval
= bq27xxx_read(di
, BQ27XXX_REG_AP
, false);
642 dev_err(di
->dev
, "error reading average power register %02x: %d\n",
643 BQ27XXX_REG_AP
, tval
);
647 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
)
648 return (tval
* BQ27XXX_POWER_CONSTANT
) / BQ27XXX_RS
;
654 * Returns true if a battery over temperature condition is detected
656 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info
*di
, u16 flags
)
658 if (di
->chip
== BQ27500
|| di
->chip
== BQ27541
|| di
->chip
== BQ27545
)
659 return flags
& (BQ27XXX_FLAG_OTC
| BQ27XXX_FLAG_OTD
);
660 if (di
->chip
== BQ27530
|| di
->chip
== BQ27421
)
661 return flags
& BQ27XXX_FLAG_OT
;
667 * Returns true if a battery under temperature condition is detected
669 static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info
*di
, u16 flags
)
671 if (di
->chip
== BQ27530
|| di
->chip
== BQ27421
)
672 return flags
& BQ27XXX_FLAG_UT
;
678 * Returns true if a low state of charge condition is detected
680 static bool bq27xxx_battery_dead(struct bq27xxx_device_info
*di
, u16 flags
)
682 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
)
683 return flags
& (BQ27000_FLAG_EDV1
| BQ27000_FLAG_EDVF
);
685 return flags
& (BQ27XXX_FLAG_SOC1
| BQ27XXX_FLAG_SOCF
);
689 * Read flag register.
690 * Return < 0 if something fails.
692 static int bq27xxx_battery_read_health(struct bq27xxx_device_info
*di
)
696 flags
= bq27xxx_read(di
, BQ27XXX_REG_FLAGS
, false);
698 dev_err(di
->dev
, "error reading flag register:%d\n", flags
);
702 /* Unlikely but important to return first */
703 if (unlikely(bq27xxx_battery_overtemp(di
, flags
)))
704 return POWER_SUPPLY_HEALTH_OVERHEAT
;
705 if (unlikely(bq27xxx_battery_undertemp(di
, flags
)))
706 return POWER_SUPPLY_HEALTH_COLD
;
707 if (unlikely(bq27xxx_battery_dead(di
, flags
)))
708 return POWER_SUPPLY_HEALTH_DEAD
;
710 return POWER_SUPPLY_HEALTH_GOOD
;
713 static void bq27xxx_battery_update(struct bq27xxx_device_info
*di
)
715 struct bq27xxx_reg_cache cache
= {0, };
716 bool has_ci_flag
= di
->chip
== BQ27000
|| di
->chip
== BQ27010
;
717 bool has_singe_flag
= di
->chip
== BQ27000
|| di
->chip
== BQ27010
;
719 cache
.flags
= bq27xxx_read(di
, BQ27XXX_REG_FLAGS
, has_singe_flag
);
720 if ((cache
.flags
& 0xff) == 0xff)
721 cache
.flags
= -1; /* read error */
722 if (cache
.flags
>= 0) {
723 cache
.temperature
= bq27xxx_battery_read_temperature(di
);
724 if (has_ci_flag
&& (cache
.flags
& BQ27000_FLAG_CI
)) {
725 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
726 cache
.capacity
= -ENODATA
;
727 cache
.energy
= -ENODATA
;
728 cache
.time_to_empty
= -ENODATA
;
729 cache
.time_to_empty_avg
= -ENODATA
;
730 cache
.time_to_full
= -ENODATA
;
731 cache
.charge_full
= -ENODATA
;
732 cache
.health
= -ENODATA
;
734 if (di
->regs
[BQ27XXX_REG_TTE
] != INVALID_REG_ADDR
)
735 cache
.time_to_empty
= bq27xxx_battery_read_time(di
, BQ27XXX_REG_TTE
);
736 if (di
->regs
[BQ27XXX_REG_TTECP
] != INVALID_REG_ADDR
)
737 cache
.time_to_empty_avg
= bq27xxx_battery_read_time(di
, BQ27XXX_REG_TTECP
);
738 if (di
->regs
[BQ27XXX_REG_TTF
] != INVALID_REG_ADDR
)
739 cache
.time_to_full
= bq27xxx_battery_read_time(di
, BQ27XXX_REG_TTF
);
740 cache
.charge_full
= bq27xxx_battery_read_fcc(di
);
741 cache
.capacity
= bq27xxx_battery_read_soc(di
);
742 if (di
->regs
[BQ27XXX_REG_AE
] != INVALID_REG_ADDR
)
743 cache
.energy
= bq27xxx_battery_read_energy(di
);
744 cache
.health
= bq27xxx_battery_read_health(di
);
746 if (di
->regs
[BQ27XXX_REG_CYCT
] != INVALID_REG_ADDR
)
747 cache
.cycle_count
= bq27xxx_battery_read_cyct(di
);
748 if (di
->regs
[BQ27XXX_REG_AP
] != INVALID_REG_ADDR
)
749 cache
.power_avg
= bq27xxx_battery_read_pwr_avg(di
);
751 /* We only have to read charge design full once */
752 if (di
->charge_design_full
<= 0)
753 di
->charge_design_full
= bq27xxx_battery_read_dcap(di
);
756 if (di
->cache
.capacity
!= cache
.capacity
)
757 power_supply_changed(di
->bat
);
759 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0)
762 di
->last_update
= jiffies
;
765 static void bq27xxx_battery_poll(struct work_struct
*work
)
767 struct bq27xxx_device_info
*di
=
768 container_of(work
, struct bq27xxx_device_info
,
771 bq27xxx_battery_update(di
);
773 if (poll_interval
> 0) {
774 /* The timer does not have to be accurate. */
775 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
776 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
781 * Return the battery average current in µA
782 * Note that current can be negative signed as well
783 * Or 0 if something fails.
785 static int bq27xxx_battery_current(struct bq27xxx_device_info
*di
,
786 union power_supply_propval
*val
)
791 curr
= bq27xxx_read(di
, BQ27XXX_REG_AI
, false);
793 dev_err(di
->dev
, "error reading current\n");
797 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
) {
798 flags
= bq27xxx_read(di
, BQ27XXX_REG_FLAGS
, false);
799 if (flags
& BQ27000_FLAG_CHGS
) {
800 dev_dbg(di
->dev
, "negative current!\n");
804 val
->intval
= curr
* BQ27XXX_CURRENT_CONSTANT
/ BQ27XXX_RS
;
806 /* Other gauges return signed value */
807 val
->intval
= (int)((s16
)curr
) * 1000;
813 static int bq27xxx_battery_status(struct bq27xxx_device_info
*di
,
814 union power_supply_propval
*val
)
818 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
) {
819 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
820 status
= POWER_SUPPLY_STATUS_FULL
;
821 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
822 status
= POWER_SUPPLY_STATUS_CHARGING
;
823 else if (power_supply_am_i_supplied(di
->bat
))
824 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
826 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
828 if (di
->cache
.flags
& BQ27XXX_FLAG_FC
)
829 status
= POWER_SUPPLY_STATUS_FULL
;
830 else if (di
->cache
.flags
& BQ27XXX_FLAG_DSC
)
831 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
833 status
= POWER_SUPPLY_STATUS_CHARGING
;
836 val
->intval
= status
;
841 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info
*di
,
842 union power_supply_propval
*val
)
846 if (di
->chip
== BQ27000
|| di
->chip
== BQ27010
) {
847 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
848 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
849 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
850 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
851 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
852 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
854 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
856 if (di
->cache
.flags
& BQ27XXX_FLAG_FC
)
857 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
858 else if (di
->cache
.flags
& BQ27XXX_FLAG_SOC1
)
859 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
860 else if (di
->cache
.flags
& BQ27XXX_FLAG_SOCF
)
861 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
863 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
872 * Return the battery Voltage in millivolts
873 * Or < 0 if something fails.
875 static int bq27xxx_battery_voltage(struct bq27xxx_device_info
*di
,
876 union power_supply_propval
*val
)
880 volt
= bq27xxx_read(di
, BQ27XXX_REG_VOLT
, false);
882 dev_err(di
->dev
, "error reading voltage\n");
886 val
->intval
= volt
* 1000;
891 static int bq27xxx_simple_value(int value
,
892 union power_supply_propval
*val
)
902 static int bq27xxx_battery_get_property(struct power_supply
*psy
,
903 enum power_supply_property psp
,
904 union power_supply_propval
*val
)
907 struct bq27xxx_device_info
*di
= power_supply_get_drvdata(psy
);
909 mutex_lock(&di
->lock
);
910 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
911 cancel_delayed_work_sync(&di
->work
);
912 bq27xxx_battery_poll(&di
->work
.work
);
914 mutex_unlock(&di
->lock
);
916 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
920 case POWER_SUPPLY_PROP_STATUS
:
921 ret
= bq27xxx_battery_status(di
, val
);
923 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
924 ret
= bq27xxx_battery_voltage(di
, val
);
926 case POWER_SUPPLY_PROP_PRESENT
:
927 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
929 case POWER_SUPPLY_PROP_CURRENT_NOW
:
930 ret
= bq27xxx_battery_current(di
, val
);
932 case POWER_SUPPLY_PROP_CAPACITY
:
933 ret
= bq27xxx_simple_value(di
->cache
.capacity
, val
);
935 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
936 ret
= bq27xxx_battery_capacity_level(di
, val
);
938 case POWER_SUPPLY_PROP_TEMP
:
939 ret
= bq27xxx_simple_value(di
->cache
.temperature
, val
);
941 val
->intval
-= 2731; /* convert decidegree k to c */
943 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
944 ret
= bq27xxx_simple_value(di
->cache
.time_to_empty
, val
);
946 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
947 ret
= bq27xxx_simple_value(di
->cache
.time_to_empty_avg
, val
);
949 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
950 ret
= bq27xxx_simple_value(di
->cache
.time_to_full
, val
);
952 case POWER_SUPPLY_PROP_TECHNOLOGY
:
953 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
955 case POWER_SUPPLY_PROP_CHARGE_NOW
:
956 ret
= bq27xxx_simple_value(bq27xxx_battery_read_nac(di
), val
);
958 case POWER_SUPPLY_PROP_CHARGE_FULL
:
959 ret
= bq27xxx_simple_value(di
->cache
.charge_full
, val
);
961 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
962 ret
= bq27xxx_simple_value(di
->charge_design_full
, val
);
964 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
965 ret
= bq27xxx_simple_value(di
->cache
.cycle_count
, val
);
967 case POWER_SUPPLY_PROP_ENERGY_NOW
:
968 ret
= bq27xxx_simple_value(di
->cache
.energy
, val
);
970 case POWER_SUPPLY_PROP_POWER_AVG
:
971 ret
= bq27xxx_simple_value(di
->cache
.power_avg
, val
);
973 case POWER_SUPPLY_PROP_HEALTH
:
974 ret
= bq27xxx_simple_value(di
->cache
.health
, val
);
976 case POWER_SUPPLY_PROP_MANUFACTURER
:
977 val
->strval
= BQ27XXX_MANUFACTURER
;
986 static void bq27xxx_external_power_changed(struct power_supply
*psy
)
988 struct bq27xxx_device_info
*di
= power_supply_get_drvdata(psy
);
990 cancel_delayed_work_sync(&di
->work
);
991 schedule_delayed_work(&di
->work
, 0);
994 static int __maybe_unused
bq27xxx_powersupply_init(struct bq27xxx_device_info
*di
,
998 struct power_supply_desc
*psy_desc
;
999 struct power_supply_config psy_cfg
= { .drv_data
= di
, };
1001 psy_desc
= devm_kzalloc(di
->dev
, sizeof(*psy_desc
), GFP_KERNEL
);
1005 psy_desc
->name
= name
;
1006 psy_desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
1007 psy_desc
->properties
= bq27xxx_battery_props
[di
->chip
].props
;
1008 psy_desc
->num_properties
= bq27xxx_battery_props
[di
->chip
].size
;
1009 psy_desc
->get_property
= bq27xxx_battery_get_property
;
1010 psy_desc
->external_power_changed
= bq27xxx_external_power_changed
;
1012 INIT_DELAYED_WORK(&di
->work
, bq27xxx_battery_poll
);
1013 mutex_init(&di
->lock
);
1015 di
->bat
= power_supply_register_no_ws(di
->dev
, psy_desc
, &psy_cfg
);
1016 if (IS_ERR(di
->bat
)) {
1017 ret
= PTR_ERR(di
->bat
);
1018 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
1022 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
1024 bq27xxx_battery_update(di
);
1029 static void __maybe_unused
bq27xxx_powersupply_unregister(struct bq27xxx_device_info
*di
)
1032 * power_supply_unregister call bq27xxx_battery_get_property which
1033 * call bq27xxx_battery_poll.
1034 * Make sure that bq27xxx_battery_poll will not call
1035 * schedule_delayed_work again after unregister (which cause OOPS).
1039 cancel_delayed_work_sync(&di
->work
);
1041 power_supply_unregister(di
->bat
);
1043 mutex_destroy(&di
->lock
);
1046 /* i2c specific code */
1047 #ifdef CONFIG_BATTERY_BQ27XXX_I2C
1049 /* If the system has several batteries we need a different name for each
1052 static DEFINE_IDR(battery_id
);
1053 static DEFINE_MUTEX(battery_mutex
);
1055 static irqreturn_t
bq27xxx_battery_irq_handler_thread(int irq
, void *data
)
1057 struct bq27xxx_device_info
*di
= data
;
1059 bq27xxx_battery_update(di
);
1064 static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info
*di
, u8 reg
,
1067 struct i2c_client
*client
= to_i2c_client(di
->dev
);
1068 struct i2c_msg msg
[2];
1069 unsigned char data
[2];
1072 if (!client
->adapter
)
1075 msg
[0].addr
= client
->addr
;
1078 msg
[0].len
= sizeof(reg
);
1079 msg
[1].addr
= client
->addr
;
1080 msg
[1].flags
= I2C_M_RD
;
1087 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
1092 ret
= get_unaligned_le16(data
);
1099 static int bq27xxx_battery_i2c_probe(struct i2c_client
*client
,
1100 const struct i2c_device_id
*id
)
1103 struct bq27xxx_device_info
*di
;
1107 /* Get new ID for the new battery device */
1108 mutex_lock(&battery_mutex
);
1109 num
= idr_alloc(&battery_id
, client
, 0, 0, GFP_KERNEL
);
1110 mutex_unlock(&battery_mutex
);
1114 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "%s-%d", id
->name
, num
);
1120 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
1127 di
->dev
= &client
->dev
;
1128 di
->chip
= id
->driver_data
;
1129 di
->bus
.read
= &bq27xxx_battery_i2c_read
;
1130 di
->regs
= bq27xxx_regs
[di
->chip
];
1132 retval
= bq27xxx_powersupply_init(di
, name
);
1136 /* Schedule a polling after about 1 min */
1137 schedule_delayed_work(&di
->work
, 60 * HZ
);
1139 i2c_set_clientdata(client
, di
);
1142 retval
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1143 NULL
, bq27xxx_battery_irq_handler_thread
,
1147 dev_err(&client
->dev
,
1148 "Unable to register IRQ %d error %d\n",
1149 client
->irq
, retval
);
1157 mutex_lock(&battery_mutex
);
1158 idr_remove(&battery_id
, num
);
1159 mutex_unlock(&battery_mutex
);
1164 static int bq27xxx_battery_i2c_remove(struct i2c_client
*client
)
1166 struct bq27xxx_device_info
*di
= i2c_get_clientdata(client
);
1168 bq27xxx_powersupply_unregister(di
);
1170 mutex_lock(&battery_mutex
);
1171 idr_remove(&battery_id
, di
->id
);
1172 mutex_unlock(&battery_mutex
);
1177 static const struct i2c_device_id bq27xxx_id
[] = {
1178 { "bq27200", BQ27000
},
1179 { "bq27210", BQ27010
},
1180 { "bq27500", BQ27500
},
1181 { "bq27510", BQ27500
},
1182 { "bq27520", BQ27500
},
1183 { "bq27530", BQ27530
},
1184 { "bq27531", BQ27530
},
1185 { "bq27541", BQ27541
},
1186 { "bq27542", BQ27541
},
1187 { "bq27546", BQ27541
},
1188 { "bq27742", BQ27541
},
1189 { "bq27545", BQ27545
},
1190 { "bq27421", BQ27421
},
1191 { "bq27425", BQ27421
},
1192 { "bq27441", BQ27421
},
1193 { "bq27621", BQ27421
},
1196 MODULE_DEVICE_TABLE(i2c
, bq27xxx_id
);
1198 static struct i2c_driver bq27xxx_battery_i2c_driver
= {
1200 .name
= "bq27xxx-battery",
1202 .probe
= bq27xxx_battery_i2c_probe
,
1203 .remove
= bq27xxx_battery_i2c_remove
,
1204 .id_table
= bq27xxx_id
,
1207 static inline int bq27xxx_battery_i2c_init(void)
1209 int ret
= i2c_add_driver(&bq27xxx_battery_i2c_driver
);
1212 pr_err("Unable to register BQ27xxx i2c driver\n");
1217 static inline void bq27xxx_battery_i2c_exit(void)
1219 i2c_del_driver(&bq27xxx_battery_i2c_driver
);
1224 static inline int bq27xxx_battery_i2c_init(void) { return 0; }
1225 static inline void bq27xxx_battery_i2c_exit(void) {};
1229 /* platform specific code */
1230 #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
1232 static int bq27xxx_battery_platform_read(struct bq27xxx_device_info
*di
, u8 reg
,
1235 struct device
*dev
= di
->dev
;
1236 struct bq27xxx_platform_data
*pdata
= dev
->platform_data
;
1237 unsigned int timeout
= 3;
1242 /* Make sure the value has not changed in between reading the
1243 * lower and the upper part */
1244 upper
= pdata
->read(dev
, reg
+ 1);
1250 lower
= pdata
->read(dev
, reg
);
1254 upper
= pdata
->read(dev
, reg
+ 1);
1255 } while (temp
!= upper
&& --timeout
);
1260 return (upper
<< 8) | lower
;
1263 return pdata
->read(dev
, reg
);
1266 static int bq27xxx_battery_platform_probe(struct platform_device
*pdev
)
1268 struct bq27xxx_device_info
*di
;
1269 struct bq27xxx_platform_data
*pdata
= pdev
->dev
.platform_data
;
1273 dev_err(&pdev
->dev
, "no platform_data supplied\n");
1278 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
1283 dev_err(&pdev
->dev
, "no device supplied\n");
1287 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
1291 platform_set_drvdata(pdev
, di
);
1293 di
->dev
= &pdev
->dev
;
1294 di
->chip
= pdata
->chip
;
1295 di
->regs
= bq27xxx_regs
[di
->chip
];
1297 name
= pdata
->name
?: dev_name(&pdev
->dev
);
1298 di
->bus
.read
= &bq27xxx_battery_platform_read
;
1300 return bq27xxx_powersupply_init(di
, name
);
1303 static int bq27xxx_battery_platform_remove(struct platform_device
*pdev
)
1305 struct bq27xxx_device_info
*di
= platform_get_drvdata(pdev
);
1307 bq27xxx_powersupply_unregister(di
);
1312 static struct platform_driver bq27xxx_battery_platform_driver
= {
1313 .probe
= bq27xxx_battery_platform_probe
,
1314 .remove
= bq27xxx_battery_platform_remove
,
1316 .name
= "bq27000-battery",
1320 static inline int bq27xxx_battery_platform_init(void)
1322 int ret
= platform_driver_register(&bq27xxx_battery_platform_driver
);
1325 pr_err("Unable to register BQ27xxx platform driver\n");
1330 static inline void bq27xxx_battery_platform_exit(void)
1332 platform_driver_unregister(&bq27xxx_battery_platform_driver
);
1337 static inline int bq27xxx_battery_platform_init(void) { return 0; }
1338 static inline void bq27xxx_battery_platform_exit(void) {};
1346 static int __init
bq27xxx_battery_init(void)
1350 ret
= bq27xxx_battery_i2c_init();
1354 ret
= bq27xxx_battery_platform_init();
1356 bq27xxx_battery_i2c_exit();
1360 module_init(bq27xxx_battery_init
);
1362 static void __exit
bq27xxx_battery_exit(void)
1364 bq27xxx_battery_platform_exit();
1365 bq27xxx_battery_i2c_exit();
1367 module_exit(bq27xxx_battery_exit
);
1369 #ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
1370 MODULE_ALIAS("platform:bq27000-battery");
1373 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1374 MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
1375 MODULE_LICENSE("GPL");