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 0x18 /* Register address plus offset */
81 #define BQ27000_RS 20 /* Resistor sense */
82 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
84 struct bq27x00_device_info
;
85 struct bq27x00_access_methods
{
86 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
89 enum bq27x00_chip
{ BQ27000
, BQ27500
, BQ27425
, BQ27742
};
91 struct bq27x00_reg_cache
{
94 int time_to_empty_avg
;
105 struct bq27x00_device_info
{
108 enum bq27x00_chip chip
;
110 struct bq27x00_reg_cache cache
;
111 int charge_design_full
;
113 unsigned long last_update
;
114 struct delayed_work work
;
116 struct power_supply bat
;
118 struct bq27x00_access_methods bus
;
123 static enum power_supply_property bq27x00_battery_props
[] = {
124 POWER_SUPPLY_PROP_STATUS
,
125 POWER_SUPPLY_PROP_PRESENT
,
126 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
127 POWER_SUPPLY_PROP_CURRENT_NOW
,
128 POWER_SUPPLY_PROP_CAPACITY
,
129 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
130 POWER_SUPPLY_PROP_TEMP
,
131 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
132 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
133 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
134 POWER_SUPPLY_PROP_TECHNOLOGY
,
135 POWER_SUPPLY_PROP_CHARGE_FULL
,
136 POWER_SUPPLY_PROP_CHARGE_NOW
,
137 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
138 POWER_SUPPLY_PROP_CYCLE_COUNT
,
139 POWER_SUPPLY_PROP_ENERGY_NOW
,
140 POWER_SUPPLY_PROP_POWER_AVG
,
141 POWER_SUPPLY_PROP_HEALTH
,
144 static enum power_supply_property bq27425_battery_props
[] = {
145 POWER_SUPPLY_PROP_STATUS
,
146 POWER_SUPPLY_PROP_PRESENT
,
147 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
148 POWER_SUPPLY_PROP_CURRENT_NOW
,
149 POWER_SUPPLY_PROP_CAPACITY
,
150 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
151 POWER_SUPPLY_PROP_TEMP
,
152 POWER_SUPPLY_PROP_TECHNOLOGY
,
153 POWER_SUPPLY_PROP_CHARGE_FULL
,
154 POWER_SUPPLY_PROP_CHARGE_NOW
,
155 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
158 static enum power_supply_property bq27742_battery_props
[] = {
159 POWER_SUPPLY_PROP_STATUS
,
160 POWER_SUPPLY_PROP_PRESENT
,
161 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
162 POWER_SUPPLY_PROP_CURRENT_NOW
,
163 POWER_SUPPLY_PROP_CAPACITY
,
164 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
165 POWER_SUPPLY_PROP_TEMP
,
166 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
167 POWER_SUPPLY_PROP_TECHNOLOGY
,
168 POWER_SUPPLY_PROP_CHARGE_FULL
,
169 POWER_SUPPLY_PROP_CHARGE_NOW
,
170 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
171 POWER_SUPPLY_PROP_CYCLE_COUNT
,
172 POWER_SUPPLY_PROP_POWER_AVG
,
173 POWER_SUPPLY_PROP_HEALTH
,
176 static unsigned int poll_interval
= 360;
177 module_param(poll_interval
, uint
, 0644);
178 MODULE_PARM_DESC(poll_interval
, "battery poll interval in seconds - " \
179 "0 disables polling");
182 * Common code for BQ27x00 devices
185 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
188 if (di
->chip
== BQ27425
)
189 return di
->bus
.read(di
, reg
- BQ27425_REG_OFFSET
, single
);
190 return di
->bus
.read(di
, reg
, single
);
194 * Higher versions of the chip like BQ27425 and BQ27500
195 * differ from BQ27000 and BQ27200 in calculation of certain
196 * parameters. Hence we need to check for the chip type.
198 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info
*di
)
200 if (di
->chip
== BQ27425
|| di
->chip
== BQ27500
|| di
->chip
== BQ27742
)
206 * Return the battery Relative State-of-Charge
207 * Or < 0 if something fails.
209 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
213 if (di
->chip
== BQ27500
|| di
->chip
== BQ27742
)
214 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
215 else if (di
->chip
== BQ27425
)
216 rsoc
= bq27x00_read(di
, BQ27425_REG_SOC
, false);
218 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
221 dev_dbg(di
->dev
, "error reading relative State-of-Charge\n");
227 * Return a battery charge value in µAh
228 * Or < 0 if something fails.
230 static int bq27x00_battery_read_charge(struct bq27x00_device_info
*di
, u8 reg
)
234 charge
= bq27x00_read(di
, reg
, false);
236 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
241 if (bq27xxx_is_chip_version_higher(di
))
244 charge
= charge
* 3570 / BQ27000_RS
;
250 * Return the battery Nominal available capaciy in µAh
251 * Or < 0 if something fails.
253 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info
*di
)
256 bool is_bq27500
= di
->chip
== BQ27500
;
257 bool is_higher
= bq27xxx_is_chip_version_higher(di
);
259 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, !is_bq27500
);
260 if (flags
>= 0 && !is_higher
&& (flags
& BQ27000_FLAG_CI
))
263 return bq27x00_battery_read_charge(di
, BQ27x00_REG_NAC
);
267 * Return the battery Last measured discharge in µAh
268 * Or < 0 if something fails.
270 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info
*di
)
272 return bq27x00_battery_read_charge(di
, BQ27x00_REG_LMD
);
276 * Return the battery Initial last measured discharge in µAh
277 * Or < 0 if something fails.
279 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info
*di
)
283 if (bq27xxx_is_chip_version_higher(di
))
284 ilmd
= bq27x00_read(di
, BQ27500_REG_DCAP
, false);
286 ilmd
= bq27x00_read(di
, BQ27000_REG_ILMD
, true);
289 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
293 if (bq27xxx_is_chip_version_higher(di
))
296 ilmd
= ilmd
* 256 * 3570 / BQ27000_RS
;
302 * Return the battery Available energy in µWh
303 * Or < 0 if something fails.
305 static int bq27x00_battery_read_energy(struct bq27x00_device_info
*di
)
309 ae
= bq27x00_read(di
, BQ27x00_REG_AE
, false);
311 dev_dbg(di
->dev
, "error reading available energy\n");
315 if (di
->chip
== BQ27500
)
318 ae
= ae
* 29200 / BQ27000_RS
;
324 * Return the battery temperature in tenths of degree Kelvin
325 * Or < 0 if something fails.
327 static int bq27x00_battery_read_temperature(struct bq27x00_device_info
*di
)
331 temp
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
333 dev_err(di
->dev
, "error reading temperature\n");
337 if (!bq27xxx_is_chip_version_higher(di
))
344 * Return the battery Cycle count total
345 * Or < 0 if something fails.
347 static int bq27x00_battery_read_cyct(struct bq27x00_device_info
*di
)
351 cyct
= bq27x00_read(di
, BQ27x00_REG_CYCT
, false);
353 dev_err(di
->dev
, "error reading cycle count total\n");
359 * Read a time register.
360 * Return < 0 if something fails.
362 static int bq27x00_battery_read_time(struct bq27x00_device_info
*di
, u8 reg
)
366 tval
= bq27x00_read(di
, reg
, false);
368 dev_dbg(di
->dev
, "error reading time register %02x: %d\n",
380 * Read a power avg register.
381 * Return < 0 if something fails.
383 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info
*di
, u8 reg
)
387 tval
= bq27x00_read(di
, reg
, false);
389 dev_err(di
->dev
, "error reading power avg rgister %02x: %d\n",
394 if (di
->chip
== BQ27500
)
397 return (tval
* BQ27x00_POWER_CONSTANT
) / BQ27000_RS
;
401 * Read flag register.
402 * Return < 0 if something fails.
404 static int bq27x00_battery_read_health(struct bq27x00_device_info
*di
)
408 tval
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
410 dev_err(di
->dev
, "error reading flag register:%d\n", tval
);
414 if ((di
->chip
== BQ27500
)) {
415 if (tval
& BQ27500_FLAG_SOCF
)
416 tval
= POWER_SUPPLY_HEALTH_DEAD
;
417 else if (tval
& BQ27500_FLAG_OTC
)
418 tval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
420 tval
= POWER_SUPPLY_HEALTH_GOOD
;
423 if (tval
& BQ27000_FLAG_EDV1
)
424 tval
= POWER_SUPPLY_HEALTH_DEAD
;
426 tval
= POWER_SUPPLY_HEALTH_GOOD
;
433 static void bq27x00_update(struct bq27x00_device_info
*di
)
435 struct bq27x00_reg_cache cache
= {0, };
436 bool is_bq27500
= di
->chip
== BQ27500
;
437 bool is_bq27425
= di
->chip
== BQ27425
;
438 bool is_bq27742
= di
->chip
== BQ27742
;
440 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, !is_bq27500
);
441 if ((cache
.flags
& 0xff) == 0xff)
444 if (cache
.flags
>= 0) {
445 if (!is_bq27500
&& !is_bq27425
446 && (cache
.flags
& BQ27000_FLAG_CI
)) {
447 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
448 cache
.capacity
= -ENODATA
;
449 cache
.energy
= -ENODATA
;
450 cache
.time_to_empty
= -ENODATA
;
451 cache
.time_to_empty_avg
= -ENODATA
;
452 cache
.time_to_full
= -ENODATA
;
453 cache
.charge_full
= -ENODATA
;
454 cache
.health
= -ENODATA
;
456 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
458 cache
.time_to_empty
=
459 bq27x00_battery_read_time(di
,
461 else if (!is_bq27425
) {
462 cache
.energy
= bq27x00_battery_read_energy(di
);
463 cache
.time_to_empty
=
464 bq27x00_battery_read_time(di
,
466 cache
.time_to_empty_avg
=
467 bq27x00_battery_read_time(di
,
470 bq27x00_battery_read_time(di
,
475 bq27x00_battery_read_lmd(di
);
476 cache
.health
= bq27x00_battery_read_health(di
);
478 cache
.temperature
= bq27x00_battery_read_temperature(di
);
480 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
483 bq27x00_battery_read_pwr_avg(di
,
487 bq27x00_battery_read_pwr_avg(di
,
490 /* We only have to read charge design full once */
491 if (di
->charge_design_full
<= 0)
492 di
->charge_design_full
= bq27x00_battery_read_ilmd(di
);
495 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0) {
497 power_supply_changed(&di
->bat
);
500 di
->last_update
= jiffies
;
503 static void bq27x00_battery_poll(struct work_struct
*work
)
505 struct bq27x00_device_info
*di
=
506 container_of(work
, struct bq27x00_device_info
, work
.work
);
510 if (poll_interval
> 0) {
511 /* The timer does not have to be accurate. */
512 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
513 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
518 * Return the battery average current in µA
519 * Note that current can be negative signed as well
520 * Or 0 if something fails.
522 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
523 union power_supply_propval
*val
)
528 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
530 dev_err(di
->dev
, "error reading current\n");
534 if (bq27xxx_is_chip_version_higher(di
)) {
535 /* bq27500 returns signed value */
536 val
->intval
= (int)((s16
)curr
) * 1000;
538 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
539 if (flags
& BQ27000_FLAG_CHGS
) {
540 dev_dbg(di
->dev
, "negative current!\n");
544 val
->intval
= curr
* 3570 / BQ27000_RS
;
550 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
551 union power_supply_propval
*val
)
555 if (bq27xxx_is_chip_version_higher(di
)) {
556 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
557 status
= POWER_SUPPLY_STATUS_FULL
;
558 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
559 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
561 status
= POWER_SUPPLY_STATUS_CHARGING
;
563 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
564 status
= POWER_SUPPLY_STATUS_FULL
;
565 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
566 status
= POWER_SUPPLY_STATUS_CHARGING
;
567 else if (power_supply_am_i_supplied(&di
->bat
))
568 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
570 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
573 val
->intval
= status
;
578 static int bq27x00_battery_capacity_level(struct bq27x00_device_info
*di
,
579 union power_supply_propval
*val
)
583 if (bq27xxx_is_chip_version_higher(di
)) {
584 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
585 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
586 else if (di
->cache
.flags
& BQ27500_FLAG_SOC1
)
587 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
588 else if (di
->cache
.flags
& BQ27500_FLAG_SOCF
)
589 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
591 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
593 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
594 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
595 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
596 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
597 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
598 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
600 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
609 * Return the battery Voltage in millivolts
610 * Or < 0 if something fails.
612 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
613 union power_supply_propval
*val
)
617 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
619 dev_err(di
->dev
, "error reading voltage\n");
623 val
->intval
= volt
* 1000;
628 static int bq27x00_simple_value(int value
,
629 union power_supply_propval
*val
)
639 #define to_bq27x00_device_info(x) container_of((x), \
640 struct bq27x00_device_info, bat);
642 static int bq27x00_battery_get_property(struct power_supply
*psy
,
643 enum power_supply_property psp
,
644 union power_supply_propval
*val
)
647 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
649 mutex_lock(&di
->lock
);
650 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
651 cancel_delayed_work_sync(&di
->work
);
652 bq27x00_battery_poll(&di
->work
.work
);
654 mutex_unlock(&di
->lock
);
656 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
660 case POWER_SUPPLY_PROP_STATUS
:
661 ret
= bq27x00_battery_status(di
, val
);
663 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
664 ret
= bq27x00_battery_voltage(di
, val
);
666 case POWER_SUPPLY_PROP_PRESENT
:
667 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
669 case POWER_SUPPLY_PROP_CURRENT_NOW
:
670 ret
= bq27x00_battery_current(di
, val
);
672 case POWER_SUPPLY_PROP_CAPACITY
:
673 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
675 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
676 ret
= bq27x00_battery_capacity_level(di
, val
);
678 case POWER_SUPPLY_PROP_TEMP
:
679 ret
= bq27x00_simple_value(di
->cache
.temperature
, val
);
683 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
684 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
686 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
687 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
689 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
690 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
692 case POWER_SUPPLY_PROP_TECHNOLOGY
:
693 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
695 case POWER_SUPPLY_PROP_CHARGE_NOW
:
696 ret
= bq27x00_simple_value(bq27x00_battery_read_nac(di
), val
);
698 case POWER_SUPPLY_PROP_CHARGE_FULL
:
699 ret
= bq27x00_simple_value(di
->cache
.charge_full
, val
);
701 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
702 ret
= bq27x00_simple_value(di
->charge_design_full
, val
);
704 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
705 ret
= bq27x00_simple_value(di
->cache
.cycle_count
, val
);
707 case POWER_SUPPLY_PROP_ENERGY_NOW
:
708 ret
= bq27x00_simple_value(di
->cache
.energy
, val
);
710 case POWER_SUPPLY_PROP_POWER_AVG
:
711 ret
= bq27x00_simple_value(di
->cache
.power_avg
, val
);
713 case POWER_SUPPLY_PROP_HEALTH
:
714 ret
= bq27x00_simple_value(di
->cache
.health
, val
);
723 static void bq27x00_external_power_changed(struct power_supply
*psy
)
725 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
727 cancel_delayed_work_sync(&di
->work
);
728 schedule_delayed_work(&di
->work
, 0);
731 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
)
735 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
736 if (di
->chip
== BQ27425
) {
737 di
->bat
.properties
= bq27425_battery_props
;
738 di
->bat
.num_properties
= ARRAY_SIZE(bq27425_battery_props
);
739 } else if (di
->chip
== BQ27742
) {
740 di
->bat
.properties
= bq27742_battery_props
;
741 di
->bat
.num_properties
= ARRAY_SIZE(bq27742_battery_props
);
743 di
->bat
.properties
= bq27x00_battery_props
;
744 di
->bat
.num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
746 di
->bat
.get_property
= bq27x00_battery_get_property
;
747 di
->bat
.external_power_changed
= bq27x00_external_power_changed
;
749 INIT_DELAYED_WORK(&di
->work
, bq27x00_battery_poll
);
750 mutex_init(&di
->lock
);
752 ret
= power_supply_register(di
->dev
, &di
->bat
);
754 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
758 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
765 static void bq27x00_powersupply_unregister(struct bq27x00_device_info
*di
)
768 * power_supply_unregister call bq27x00_battery_get_property which
769 * call bq27x00_battery_poll.
770 * Make sure that bq27x00_battery_poll will not call
771 * schedule_delayed_work again after unregister (which cause OOPS).
775 cancel_delayed_work_sync(&di
->work
);
777 power_supply_unregister(&di
->bat
);
779 mutex_destroy(&di
->lock
);
783 /* i2c specific code */
784 #ifdef CONFIG_BATTERY_BQ27X00_I2C
786 /* If the system has several batteries we need a different name for each
789 static DEFINE_IDR(battery_id
);
790 static DEFINE_MUTEX(battery_mutex
);
792 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
794 struct i2c_client
*client
= to_i2c_client(di
->dev
);
795 struct i2c_msg msg
[2];
796 unsigned char data
[2];
799 if (!client
->adapter
)
802 msg
[0].addr
= client
->addr
;
805 msg
[0].len
= sizeof(reg
);
806 msg
[1].addr
= client
->addr
;
807 msg
[1].flags
= I2C_M_RD
;
814 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
819 ret
= get_unaligned_le16(data
);
826 static int bq27x00_battery_probe(struct i2c_client
*client
,
827 const struct i2c_device_id
*id
)
830 struct bq27x00_device_info
*di
;
834 /* Get new ID for the new battery device */
835 mutex_lock(&battery_mutex
);
836 num
= idr_alloc(&battery_id
, client
, 0, 0, GFP_KERNEL
);
837 mutex_unlock(&battery_mutex
);
841 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
843 dev_err(&client
->dev
, "failed to allocate device name\n");
848 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
850 dev_err(&client
->dev
, "failed to allocate device info data\n");
856 di
->dev
= &client
->dev
;
857 di
->chip
= id
->driver_data
;
859 di
->bus
.read
= &bq27x00_read_i2c
;
861 retval
= bq27x00_powersupply_init(di
);
865 i2c_set_clientdata(client
, di
);
872 mutex_lock(&battery_mutex
);
873 idr_remove(&battery_id
, num
);
874 mutex_unlock(&battery_mutex
);
879 static int bq27x00_battery_remove(struct i2c_client
*client
)
881 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
883 bq27x00_powersupply_unregister(di
);
887 mutex_lock(&battery_mutex
);
888 idr_remove(&battery_id
, di
->id
);
889 mutex_unlock(&battery_mutex
);
894 static const struct i2c_device_id bq27x00_id
[] = {
895 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
896 { "bq27500", BQ27500
},
897 { "bq27425", BQ27425
},
898 { "bq27742", BQ27742
},
901 MODULE_DEVICE_TABLE(i2c
, bq27x00_id
);
903 static struct i2c_driver bq27x00_battery_driver
= {
905 .name
= "bq27x00-battery",
907 .probe
= bq27x00_battery_probe
,
908 .remove
= bq27x00_battery_remove
,
909 .id_table
= bq27x00_id
,
912 static inline int bq27x00_battery_i2c_init(void)
914 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
916 printk(KERN_ERR
"Unable to register BQ27x00 i2c driver\n");
921 static inline void bq27x00_battery_i2c_exit(void)
923 i2c_del_driver(&bq27x00_battery_driver
);
928 static inline int bq27x00_battery_i2c_init(void) { return 0; }
929 static inline void bq27x00_battery_i2c_exit(void) {};
933 /* platform specific code */
934 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
936 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
939 struct device
*dev
= di
->dev
;
940 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
941 unsigned int timeout
= 3;
946 /* Make sure the value has not changed in between reading the
947 * lower and the upper part */
948 upper
= pdata
->read(dev
, reg
+ 1);
954 lower
= pdata
->read(dev
, reg
);
958 upper
= pdata
->read(dev
, reg
+ 1);
959 } while (temp
!= upper
&& --timeout
);
964 return (upper
<< 8) | lower
;
967 return pdata
->read(dev
, reg
);
970 static int bq27000_battery_probe(struct platform_device
*pdev
)
972 struct bq27x00_device_info
*di
;
973 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
976 dev_err(&pdev
->dev
, "no platform_data supplied\n");
981 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
985 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
987 dev_err(&pdev
->dev
, "failed to allocate device info data\n");
991 platform_set_drvdata(pdev
, di
);
993 di
->dev
= &pdev
->dev
;
996 di
->bat
.name
= pdata
->name
?: dev_name(&pdev
->dev
);
997 di
->bus
.read
= &bq27000_read_platform
;
999 return bq27x00_powersupply_init(di
);
1002 static int bq27000_battery_remove(struct platform_device
*pdev
)
1004 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
1006 bq27x00_powersupply_unregister(di
);
1011 static struct platform_driver bq27000_battery_driver
= {
1012 .probe
= bq27000_battery_probe
,
1013 .remove
= bq27000_battery_remove
,
1015 .name
= "bq27000-battery",
1016 .owner
= THIS_MODULE
,
1020 static inline int bq27x00_battery_platform_init(void)
1022 int ret
= platform_driver_register(&bq27000_battery_driver
);
1024 printk(KERN_ERR
"Unable to register BQ27000 platform driver\n");
1029 static inline void bq27x00_battery_platform_exit(void)
1031 platform_driver_unregister(&bq27000_battery_driver
);
1036 static inline int bq27x00_battery_platform_init(void) { return 0; }
1037 static inline void bq27x00_battery_platform_exit(void) {};
1045 static int __init
bq27x00_battery_init(void)
1049 ret
= bq27x00_battery_i2c_init();
1053 ret
= bq27x00_battery_platform_init();
1055 bq27x00_battery_i2c_exit();
1059 module_init(bq27x00_battery_init
);
1061 static void __exit
bq27x00_battery_exit(void)
1063 bq27x00_battery_platform_exit();
1064 bq27x00_battery_i2c_exit();
1066 module_exit(bq27x00_battery_exit
);
1068 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1069 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1070 MODULE_LICENSE("GPL");