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
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/param.h>
31 #include <linux/jiffies.h>
32 #include <linux/workqueue.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/power_supply.h>
36 #include <linux/idr.h>
37 #include <linux/i2c.h>
38 #include <linux/slab.h>
39 #include <asm/unaligned.h>
41 #include <linux/power/bq27x00_battery.h>
43 #define DRIVER_VERSION "1.2.0"
45 #define BQ27x00_REG_TEMP 0x06
46 #define BQ27x00_REG_VOLT 0x08
47 #define BQ27x00_REG_AI 0x14
48 #define BQ27x00_REG_FLAGS 0x0A
49 #define BQ27x00_REG_TTE 0x16
50 #define BQ27x00_REG_TTF 0x18
51 #define BQ27x00_REG_TTECP 0x26
52 #define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
53 #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
54 #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
55 #define BQ27x00_REG_AE 0x22 /* Available energy */
56 #define BQ27x00_POWER_AVG 0x24
58 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
59 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
60 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
61 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
62 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
63 #define BQ27000_FLAG_FC BIT(5)
64 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
66 #define BQ27500_REG_SOC 0x2C
67 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
68 #define BQ27500_FLAG_DSC BIT(0)
69 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
70 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
71 #define BQ27500_FLAG_FC BIT(9)
72 #define BQ27500_FLAG_OTC BIT(15)
74 /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
75 #define BQ27425_REG_OFFSET 0x04
76 #define BQ27425_REG_SOC 0x18 /* Register address plus offset */
78 #define BQ27000_RS 20 /* Resistor sense */
79 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
81 struct bq27x00_device_info
;
82 struct bq27x00_access_methods
{
83 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
86 enum bq27x00_chip
{ BQ27000
, BQ27500
, BQ27425
};
88 struct bq27x00_reg_cache
{
91 int time_to_empty_avg
;
102 struct bq27x00_device_info
{
105 enum bq27x00_chip chip
;
107 struct bq27x00_reg_cache cache
;
108 int charge_design_full
;
110 unsigned long last_update
;
111 struct delayed_work work
;
113 struct power_supply bat
;
115 struct bq27x00_access_methods bus
;
120 static enum power_supply_property bq27x00_battery_props
[] = {
121 POWER_SUPPLY_PROP_STATUS
,
122 POWER_SUPPLY_PROP_PRESENT
,
123 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
124 POWER_SUPPLY_PROP_CURRENT_NOW
,
125 POWER_SUPPLY_PROP_CAPACITY
,
126 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
127 POWER_SUPPLY_PROP_TEMP
,
128 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
129 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
130 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
131 POWER_SUPPLY_PROP_TECHNOLOGY
,
132 POWER_SUPPLY_PROP_CHARGE_FULL
,
133 POWER_SUPPLY_PROP_CHARGE_NOW
,
134 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
135 POWER_SUPPLY_PROP_CYCLE_COUNT
,
136 POWER_SUPPLY_PROP_ENERGY_NOW
,
137 POWER_SUPPLY_PROP_POWER_AVG
,
138 POWER_SUPPLY_PROP_HEALTH
,
141 static enum power_supply_property bq27425_battery_props
[] = {
142 POWER_SUPPLY_PROP_STATUS
,
143 POWER_SUPPLY_PROP_PRESENT
,
144 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
145 POWER_SUPPLY_PROP_CURRENT_NOW
,
146 POWER_SUPPLY_PROP_CAPACITY
,
147 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
148 POWER_SUPPLY_PROP_TEMP
,
149 POWER_SUPPLY_PROP_TECHNOLOGY
,
150 POWER_SUPPLY_PROP_CHARGE_FULL
,
151 POWER_SUPPLY_PROP_CHARGE_NOW
,
152 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
155 static unsigned int poll_interval
= 360;
156 module_param(poll_interval
, uint
, 0644);
157 MODULE_PARM_DESC(poll_interval
, "battery poll interval in seconds - " \
158 "0 disables polling");
161 * Common code for BQ27x00 devices
164 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
167 if (di
->chip
== BQ27425
)
168 return di
->bus
.read(di
, reg
- BQ27425_REG_OFFSET
, single
);
169 return di
->bus
.read(di
, reg
, single
);
173 * Higher versions of the chip like BQ27425 and BQ27500
174 * differ from BQ27000 and BQ27200 in calculation of certain
175 * parameters. Hence we need to check for the chip type.
177 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info
*di
)
179 if (di
->chip
== BQ27425
|| di
->chip
== BQ27500
)
185 * Return the battery Relative State-of-Charge
186 * Or < 0 if something fails.
188 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
192 if (di
->chip
== BQ27500
)
193 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
194 else if (di
->chip
== BQ27425
)
195 rsoc
= bq27x00_read(di
, BQ27425_REG_SOC
, false);
197 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
200 dev_dbg(di
->dev
, "error reading relative State-of-Charge\n");
206 * Return a battery charge value in µAh
207 * Or < 0 if something fails.
209 static int bq27x00_battery_read_charge(struct bq27x00_device_info
*di
, u8 reg
)
213 charge
= bq27x00_read(di
, reg
, false);
215 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
220 if (bq27xxx_is_chip_version_higher(di
))
223 charge
= charge
* 3570 / BQ27000_RS
;
229 * Return the battery Nominal available capaciy in µAh
230 * Or < 0 if something fails.
232 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info
*di
)
235 bool is_bq27500
= di
->chip
== BQ27500
;
236 bool is_higher
= bq27xxx_is_chip_version_higher(di
);
238 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, !is_bq27500
);
239 if (flags
>= 0 && !is_higher
&& (flags
& BQ27000_FLAG_CI
))
242 return bq27x00_battery_read_charge(di
, BQ27x00_REG_NAC
);
246 * Return the battery Last measured discharge in µAh
247 * Or < 0 if something fails.
249 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info
*di
)
251 return bq27x00_battery_read_charge(di
, BQ27x00_REG_LMD
);
255 * Return the battery Initial last measured discharge in µAh
256 * Or < 0 if something fails.
258 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info
*di
)
262 if (bq27xxx_is_chip_version_higher(di
))
263 ilmd
= bq27x00_read(di
, BQ27500_REG_DCAP
, false);
265 ilmd
= bq27x00_read(di
, BQ27000_REG_ILMD
, true);
268 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
272 if (bq27xxx_is_chip_version_higher(di
))
275 ilmd
= ilmd
* 256 * 3570 / BQ27000_RS
;
281 * Return the battery Available energy in µWh
282 * Or < 0 if something fails.
284 static int bq27x00_battery_read_energy(struct bq27x00_device_info
*di
)
288 ae
= bq27x00_read(di
, BQ27x00_REG_AE
, false);
290 dev_dbg(di
->dev
, "error reading available energy\n");
294 if (di
->chip
== BQ27500
)
297 ae
= ae
* 29200 / BQ27000_RS
;
303 * Return the battery temperature in tenths of degree Kelvin
304 * Or < 0 if something fails.
306 static int bq27x00_battery_read_temperature(struct bq27x00_device_info
*di
)
310 temp
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
312 dev_err(di
->dev
, "error reading temperature\n");
316 if (!bq27xxx_is_chip_version_higher(di
))
323 * Return the battery Cycle count total
324 * Or < 0 if something fails.
326 static int bq27x00_battery_read_cyct(struct bq27x00_device_info
*di
)
330 cyct
= bq27x00_read(di
, BQ27x00_REG_CYCT
, false);
332 dev_err(di
->dev
, "error reading cycle count total\n");
338 * Read a time register.
339 * Return < 0 if something fails.
341 static int bq27x00_battery_read_time(struct bq27x00_device_info
*di
, u8 reg
)
345 tval
= bq27x00_read(di
, reg
, false);
347 dev_dbg(di
->dev
, "error reading time register %02x: %d\n",
359 * Read a power avg register.
360 * Return < 0 if something fails.
362 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info
*di
, u8 reg
)
366 tval
= bq27x00_read(di
, reg
, false);
368 dev_err(di
->dev
, "error reading power avg rgister %02x: %d\n",
373 if (di
->chip
== BQ27500
)
376 return (tval
* BQ27x00_POWER_CONSTANT
) / BQ27000_RS
;
380 * Read flag register.
381 * Return < 0 if something fails.
383 static int bq27x00_battery_read_health(struct bq27x00_device_info
*di
)
387 tval
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
389 dev_err(di
->dev
, "error reading flag register:%d\n", tval
);
393 if ((di
->chip
== BQ27500
)) {
394 if (tval
& BQ27500_FLAG_SOCF
)
395 tval
= POWER_SUPPLY_HEALTH_DEAD
;
396 else if (tval
& BQ27500_FLAG_OTC
)
397 tval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
399 tval
= POWER_SUPPLY_HEALTH_GOOD
;
402 if (tval
& BQ27000_FLAG_EDV1
)
403 tval
= POWER_SUPPLY_HEALTH_DEAD
;
405 tval
= POWER_SUPPLY_HEALTH_GOOD
;
412 static void bq27x00_update(struct bq27x00_device_info
*di
)
414 struct bq27x00_reg_cache cache
= {0, };
415 bool is_bq27500
= di
->chip
== BQ27500
;
416 bool is_bq27425
= di
->chip
== BQ27425
;
418 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, !is_bq27500
);
419 if ((cache
.flags
& 0xff) == 0xff)
422 if (cache
.flags
>= 0) {
423 if (!is_bq27500
&& !is_bq27425
424 && (cache
.flags
& BQ27000_FLAG_CI
)) {
425 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
426 cache
.capacity
= -ENODATA
;
427 cache
.energy
= -ENODATA
;
428 cache
.time_to_empty
= -ENODATA
;
429 cache
.time_to_empty_avg
= -ENODATA
;
430 cache
.time_to_full
= -ENODATA
;
431 cache
.charge_full
= -ENODATA
;
432 cache
.health
= -ENODATA
;
434 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
436 cache
.energy
= bq27x00_battery_read_energy(di
);
437 cache
.time_to_empty
=
438 bq27x00_battery_read_time(di
,
440 cache
.time_to_empty_avg
=
441 bq27x00_battery_read_time(di
,
444 bq27x00_battery_read_time(di
,
447 cache
.charge_full
= bq27x00_battery_read_lmd(di
);
448 cache
.health
= bq27x00_battery_read_health(di
);
450 cache
.temperature
= bq27x00_battery_read_temperature(di
);
452 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
454 bq27x00_battery_read_pwr_avg(di
, BQ27x00_POWER_AVG
);
456 /* We only have to read charge design full once */
457 if (di
->charge_design_full
<= 0)
458 di
->charge_design_full
= bq27x00_battery_read_ilmd(di
);
461 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0) {
463 power_supply_changed(&di
->bat
);
466 di
->last_update
= jiffies
;
469 static void bq27x00_battery_poll(struct work_struct
*work
)
471 struct bq27x00_device_info
*di
=
472 container_of(work
, struct bq27x00_device_info
, work
.work
);
476 if (poll_interval
> 0) {
477 /* The timer does not have to be accurate. */
478 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
479 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
484 * Return the battery average current in µA
485 * Note that current can be negative signed as well
486 * Or 0 if something fails.
488 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
489 union power_supply_propval
*val
)
494 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
496 dev_err(di
->dev
, "error reading current\n");
500 if (bq27xxx_is_chip_version_higher(di
)) {
501 /* bq27500 returns signed value */
502 val
->intval
= (int)((s16
)curr
) * 1000;
504 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
505 if (flags
& BQ27000_FLAG_CHGS
) {
506 dev_dbg(di
->dev
, "negative current!\n");
510 val
->intval
= curr
* 3570 / BQ27000_RS
;
516 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
517 union power_supply_propval
*val
)
521 if (bq27xxx_is_chip_version_higher(di
)) {
522 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
523 status
= POWER_SUPPLY_STATUS_FULL
;
524 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
525 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
527 status
= POWER_SUPPLY_STATUS_CHARGING
;
529 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
530 status
= POWER_SUPPLY_STATUS_FULL
;
531 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
532 status
= POWER_SUPPLY_STATUS_CHARGING
;
533 else if (power_supply_am_i_supplied(&di
->bat
))
534 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
536 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
539 val
->intval
= status
;
544 static int bq27x00_battery_capacity_level(struct bq27x00_device_info
*di
,
545 union power_supply_propval
*val
)
549 if (bq27xxx_is_chip_version_higher(di
)) {
550 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
551 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
552 else if (di
->cache
.flags
& BQ27500_FLAG_SOC1
)
553 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
554 else if (di
->cache
.flags
& BQ27500_FLAG_SOCF
)
555 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
557 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
559 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
560 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
561 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
562 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
563 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
564 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
566 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
575 * Return the battery Voltage in millivolts
576 * Or < 0 if something fails.
578 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
579 union power_supply_propval
*val
)
583 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
585 dev_err(di
->dev
, "error reading voltage\n");
589 val
->intval
= volt
* 1000;
594 static int bq27x00_simple_value(int value
,
595 union power_supply_propval
*val
)
605 #define to_bq27x00_device_info(x) container_of((x), \
606 struct bq27x00_device_info, bat);
608 static int bq27x00_battery_get_property(struct power_supply
*psy
,
609 enum power_supply_property psp
,
610 union power_supply_propval
*val
)
613 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
615 mutex_lock(&di
->lock
);
616 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
617 cancel_delayed_work_sync(&di
->work
);
618 bq27x00_battery_poll(&di
->work
.work
);
620 mutex_unlock(&di
->lock
);
622 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
626 case POWER_SUPPLY_PROP_STATUS
:
627 ret
= bq27x00_battery_status(di
, val
);
629 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
630 ret
= bq27x00_battery_voltage(di
, val
);
632 case POWER_SUPPLY_PROP_PRESENT
:
633 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
635 case POWER_SUPPLY_PROP_CURRENT_NOW
:
636 ret
= bq27x00_battery_current(di
, val
);
638 case POWER_SUPPLY_PROP_CAPACITY
:
639 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
641 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
642 ret
= bq27x00_battery_capacity_level(di
, val
);
644 case POWER_SUPPLY_PROP_TEMP
:
645 ret
= bq27x00_simple_value(di
->cache
.temperature
, val
);
649 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
650 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
652 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
653 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
655 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
656 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
658 case POWER_SUPPLY_PROP_TECHNOLOGY
:
659 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
661 case POWER_SUPPLY_PROP_CHARGE_NOW
:
662 ret
= bq27x00_simple_value(bq27x00_battery_read_nac(di
), val
);
664 case POWER_SUPPLY_PROP_CHARGE_FULL
:
665 ret
= bq27x00_simple_value(di
->cache
.charge_full
, val
);
667 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
668 ret
= bq27x00_simple_value(di
->charge_design_full
, val
);
670 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
671 ret
= bq27x00_simple_value(di
->cache
.cycle_count
, val
);
673 case POWER_SUPPLY_PROP_ENERGY_NOW
:
674 ret
= bq27x00_simple_value(di
->cache
.energy
, val
);
676 case POWER_SUPPLY_PROP_POWER_AVG
:
677 ret
= bq27x00_simple_value(di
->cache
.power_avg
, val
);
679 case POWER_SUPPLY_PROP_HEALTH
:
680 ret
= bq27x00_simple_value(di
->cache
.health
, val
);
689 static void bq27x00_external_power_changed(struct power_supply
*psy
)
691 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
693 cancel_delayed_work_sync(&di
->work
);
694 schedule_delayed_work(&di
->work
, 0);
697 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
)
701 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
702 if (di
->chip
== BQ27425
) {
703 di
->bat
.properties
= bq27425_battery_props
;
704 di
->bat
.num_properties
= ARRAY_SIZE(bq27425_battery_props
);
706 di
->bat
.properties
= bq27x00_battery_props
;
707 di
->bat
.num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
709 di
->bat
.get_property
= bq27x00_battery_get_property
;
710 di
->bat
.external_power_changed
= bq27x00_external_power_changed
;
712 INIT_DELAYED_WORK(&di
->work
, bq27x00_battery_poll
);
713 mutex_init(&di
->lock
);
715 ret
= power_supply_register(di
->dev
, &di
->bat
);
717 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
721 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
728 static void bq27x00_powersupply_unregister(struct bq27x00_device_info
*di
)
731 * power_supply_unregister call bq27x00_battery_get_property which
732 * call bq27x00_battery_poll.
733 * Make sure that bq27x00_battery_poll will not call
734 * schedule_delayed_work again after unregister (which cause OOPS).
738 cancel_delayed_work_sync(&di
->work
);
740 power_supply_unregister(&di
->bat
);
742 mutex_destroy(&di
->lock
);
746 /* i2c specific code */
747 #ifdef CONFIG_BATTERY_BQ27X00_I2C
749 /* If the system has several batteries we need a different name for each
752 static DEFINE_IDR(battery_id
);
753 static DEFINE_MUTEX(battery_mutex
);
755 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
757 struct i2c_client
*client
= to_i2c_client(di
->dev
);
758 struct i2c_msg msg
[2];
759 unsigned char data
[2];
762 if (!client
->adapter
)
765 msg
[0].addr
= client
->addr
;
768 msg
[0].len
= sizeof(reg
);
769 msg
[1].addr
= client
->addr
;
770 msg
[1].flags
= I2C_M_RD
;
777 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
782 ret
= get_unaligned_le16(data
);
789 static int bq27x00_battery_probe(struct i2c_client
*client
,
790 const struct i2c_device_id
*id
)
793 struct bq27x00_device_info
*di
;
797 /* Get new ID for the new battery device */
798 mutex_lock(&battery_mutex
);
799 num
= idr_alloc(&battery_id
, client
, 0, 0, GFP_KERNEL
);
800 mutex_unlock(&battery_mutex
);
804 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
806 dev_err(&client
->dev
, "failed to allocate device name\n");
811 di
= devm_kzalloc(&client
->dev
, sizeof(*di
), GFP_KERNEL
);
813 dev_err(&client
->dev
, "failed to allocate device info data\n");
819 di
->dev
= &client
->dev
;
820 di
->chip
= id
->driver_data
;
822 di
->bus
.read
= &bq27x00_read_i2c
;
824 retval
= bq27x00_powersupply_init(di
);
828 i2c_set_clientdata(client
, di
);
835 mutex_lock(&battery_mutex
);
836 idr_remove(&battery_id
, num
);
837 mutex_unlock(&battery_mutex
);
842 static int bq27x00_battery_remove(struct i2c_client
*client
)
844 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
846 bq27x00_powersupply_unregister(di
);
850 mutex_lock(&battery_mutex
);
851 idr_remove(&battery_id
, di
->id
);
852 mutex_unlock(&battery_mutex
);
857 static const struct i2c_device_id bq27x00_id
[] = {
858 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
859 { "bq27500", BQ27500
},
860 { "bq27425", BQ27425
},
863 MODULE_DEVICE_TABLE(i2c
, bq27x00_id
);
865 static struct i2c_driver bq27x00_battery_driver
= {
867 .name
= "bq27x00-battery",
869 .probe
= bq27x00_battery_probe
,
870 .remove
= bq27x00_battery_remove
,
871 .id_table
= bq27x00_id
,
874 static inline int bq27x00_battery_i2c_init(void)
876 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
878 printk(KERN_ERR
"Unable to register BQ27x00 i2c driver\n");
883 static inline void bq27x00_battery_i2c_exit(void)
885 i2c_del_driver(&bq27x00_battery_driver
);
890 static inline int bq27x00_battery_i2c_init(void) { return 0; }
891 static inline void bq27x00_battery_i2c_exit(void) {};
895 /* platform specific code */
896 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
898 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
901 struct device
*dev
= di
->dev
;
902 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
903 unsigned int timeout
= 3;
908 /* Make sure the value has not changed in between reading the
909 * lower and the upper part */
910 upper
= pdata
->read(dev
, reg
+ 1);
916 lower
= pdata
->read(dev
, reg
);
920 upper
= pdata
->read(dev
, reg
+ 1);
921 } while (temp
!= upper
&& --timeout
);
926 return (upper
<< 8) | lower
;
929 return pdata
->read(dev
, reg
);
932 static int bq27000_battery_probe(struct platform_device
*pdev
)
934 struct bq27x00_device_info
*di
;
935 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
938 dev_err(&pdev
->dev
, "no platform_data supplied\n");
943 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
947 di
= devm_kzalloc(&pdev
->dev
, sizeof(*di
), GFP_KERNEL
);
949 dev_err(&pdev
->dev
, "failed to allocate device info data\n");
953 platform_set_drvdata(pdev
, di
);
955 di
->dev
= &pdev
->dev
;
958 di
->bat
.name
= pdata
->name
?: dev_name(&pdev
->dev
);
959 di
->bus
.read
= &bq27000_read_platform
;
961 return bq27x00_powersupply_init(di
);
964 static int bq27000_battery_remove(struct platform_device
*pdev
)
966 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
968 bq27x00_powersupply_unregister(di
);
973 static struct platform_driver bq27000_battery_driver
= {
974 .probe
= bq27000_battery_probe
,
975 .remove
= bq27000_battery_remove
,
977 .name
= "bq27000-battery",
978 .owner
= THIS_MODULE
,
982 static inline int bq27x00_battery_platform_init(void)
984 int ret
= platform_driver_register(&bq27000_battery_driver
);
986 printk(KERN_ERR
"Unable to register BQ27000 platform driver\n");
991 static inline void bq27x00_battery_platform_exit(void)
993 platform_driver_unregister(&bq27000_battery_driver
);
998 static inline int bq27x00_battery_platform_init(void) { return 0; }
999 static inline void bq27x00_battery_platform_exit(void) {};
1007 static int __init
bq27x00_battery_init(void)
1011 ret
= bq27x00_battery_i2c_init();
1015 ret
= bq27x00_battery_platform_init();
1017 bq27x00_battery_i2c_exit();
1021 module_init(bq27x00_battery_init
);
1023 static void __exit
bq27x00_battery_exit(void)
1025 bq27x00_battery_platform_exit();
1026 bq27x00_battery_i2c_exit();
1028 module_exit(bq27x00_battery_exit
);
1030 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1031 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1032 MODULE_LICENSE("GPL");