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/module.h>
29 #include <linux/param.h>
30 #include <linux/jiffies.h>
31 #include <linux/workqueue.h>
32 #include <linux/delay.h>
33 #include <linux/platform_device.h>
34 #include <linux/power_supply.h>
35 #include <linux/idr.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
38 #include <asm/unaligned.h>
40 #include <linux/power/bq27x00_battery.h>
42 #define DRIVER_VERSION "1.2.0"
44 #define BQ27x00_REG_TEMP 0x06
45 #define BQ27x00_REG_VOLT 0x08
46 #define BQ27x00_REG_AI 0x14
47 #define BQ27x00_REG_FLAGS 0x0A
48 #define BQ27x00_REG_TTE 0x16
49 #define BQ27x00_REG_TTF 0x18
50 #define BQ27x00_REG_TTECP 0x26
51 #define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */
52 #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
53 #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
54 #define BQ27x00_REG_AE 0x22 /* Available energy */
55 #define BQ27x00_POWER_AVG 0x24
57 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
58 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
59 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
60 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
61 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
62 #define BQ27000_FLAG_FC BIT(5)
63 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
65 #define BQ27500_REG_SOC 0x2C
66 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
67 #define BQ27500_FLAG_DSC BIT(0)
68 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
69 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
70 #define BQ27500_FLAG_FC BIT(9)
71 #define BQ27500_FLAG_OTC BIT(15)
73 /* bq27425 register addresses are same as bq27x00 addresses minus 4 */
74 #define BQ27425_REG_OFFSET 0x04
75 #define BQ27425_REG_SOC 0x18 /* Register address plus offset */
77 #define BQ27000_RS 20 /* Resistor sense */
78 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
80 struct bq27x00_device_info
;
81 struct bq27x00_access_methods
{
82 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
85 enum bq27x00_chip
{ BQ27000
, BQ27500
, BQ27425
};
87 struct bq27x00_reg_cache
{
90 int time_to_empty_avg
;
101 struct bq27x00_device_info
{
104 enum bq27x00_chip chip
;
106 struct bq27x00_reg_cache cache
;
107 int charge_design_full
;
109 unsigned long last_update
;
110 struct delayed_work work
;
112 struct power_supply bat
;
114 struct bq27x00_access_methods bus
;
119 static enum power_supply_property bq27x00_battery_props
[] = {
120 POWER_SUPPLY_PROP_STATUS
,
121 POWER_SUPPLY_PROP_PRESENT
,
122 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
123 POWER_SUPPLY_PROP_CURRENT_NOW
,
124 POWER_SUPPLY_PROP_CAPACITY
,
125 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
126 POWER_SUPPLY_PROP_TEMP
,
127 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
128 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
129 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
130 POWER_SUPPLY_PROP_TECHNOLOGY
,
131 POWER_SUPPLY_PROP_CHARGE_FULL
,
132 POWER_SUPPLY_PROP_CHARGE_NOW
,
133 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
134 POWER_SUPPLY_PROP_CYCLE_COUNT
,
135 POWER_SUPPLY_PROP_ENERGY_NOW
,
136 POWER_SUPPLY_PROP_POWER_AVG
,
137 POWER_SUPPLY_PROP_HEALTH
,
140 static enum power_supply_property bq27425_battery_props
[] = {
141 POWER_SUPPLY_PROP_STATUS
,
142 POWER_SUPPLY_PROP_PRESENT
,
143 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
144 POWER_SUPPLY_PROP_CURRENT_NOW
,
145 POWER_SUPPLY_PROP_CAPACITY
,
146 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
147 POWER_SUPPLY_PROP_TEMP
,
148 POWER_SUPPLY_PROP_TECHNOLOGY
,
149 POWER_SUPPLY_PROP_CHARGE_FULL
,
150 POWER_SUPPLY_PROP_CHARGE_NOW
,
151 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
154 static unsigned int poll_interval
= 360;
155 module_param(poll_interval
, uint
, 0644);
156 MODULE_PARM_DESC(poll_interval
, "battery poll interval in seconds - " \
157 "0 disables polling");
160 * Common code for BQ27x00 devices
163 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
166 if (di
->chip
== BQ27425
)
167 return di
->bus
.read(di
, reg
- BQ27425_REG_OFFSET
, single
);
168 return di
->bus
.read(di
, reg
, single
);
172 * Higher versions of the chip like BQ27425 and BQ27500
173 * differ from BQ27000 and BQ27200 in calculation of certain
174 * parameters. Hence we need to check for the chip type.
176 static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info
*di
)
178 if (di
->chip
== BQ27425
|| di
->chip
== BQ27500
)
184 * Return the battery Relative State-of-Charge
185 * Or < 0 if something fails.
187 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
191 if (di
->chip
== BQ27500
)
192 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
193 else if (di
->chip
== BQ27425
)
194 rsoc
= bq27x00_read(di
, BQ27425_REG_SOC
, false);
196 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
199 dev_dbg(di
->dev
, "error reading relative State-of-Charge\n");
205 * Return a battery charge value in µAh
206 * Or < 0 if something fails.
208 static int bq27x00_battery_read_charge(struct bq27x00_device_info
*di
, u8 reg
)
212 charge
= bq27x00_read(di
, reg
, false);
214 dev_dbg(di
->dev
, "error reading charge register %02x: %d\n",
219 if (bq27xxx_is_chip_version_higher(di
))
222 charge
= charge
* 3570 / BQ27000_RS
;
228 * Return the battery Nominal available capaciy in µAh
229 * Or < 0 if something fails.
231 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info
*di
)
233 return bq27x00_battery_read_charge(di
, BQ27x00_REG_NAC
);
237 * Return the battery Last measured discharge in µAh
238 * Or < 0 if something fails.
240 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info
*di
)
242 return bq27x00_battery_read_charge(di
, BQ27x00_REG_LMD
);
246 * Return the battery Initial last measured discharge in µAh
247 * Or < 0 if something fails.
249 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info
*di
)
253 if (bq27xxx_is_chip_version_higher(di
))
254 ilmd
= bq27x00_read(di
, BQ27500_REG_DCAP
, false);
256 ilmd
= bq27x00_read(di
, BQ27000_REG_ILMD
, true);
259 dev_dbg(di
->dev
, "error reading initial last measured discharge\n");
263 if (bq27xxx_is_chip_version_higher(di
))
266 ilmd
= ilmd
* 256 * 3570 / BQ27000_RS
;
272 * Return the battery Available energy in µWh
273 * Or < 0 if something fails.
275 static int bq27x00_battery_read_energy(struct bq27x00_device_info
*di
)
279 ae
= bq27x00_read(di
, BQ27x00_REG_AE
, false);
281 dev_dbg(di
->dev
, "error reading available energy\n");
285 if (di
->chip
== BQ27500
)
288 ae
= ae
* 29200 / BQ27000_RS
;
294 * Return the battery temperature in tenths of degree Celsius
295 * Or < 0 if something fails.
297 static int bq27x00_battery_read_temperature(struct bq27x00_device_info
*di
)
301 temp
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
303 dev_err(di
->dev
, "error reading temperature\n");
307 if (bq27xxx_is_chip_version_higher(di
))
310 temp
= ((temp
* 5) - 5463) / 2;
316 * Return the battery Cycle count total
317 * Or < 0 if something fails.
319 static int bq27x00_battery_read_cyct(struct bq27x00_device_info
*di
)
323 cyct
= bq27x00_read(di
, BQ27x00_REG_CYCT
, false);
325 dev_err(di
->dev
, "error reading cycle count total\n");
331 * Read a time register.
332 * Return < 0 if something fails.
334 static int bq27x00_battery_read_time(struct bq27x00_device_info
*di
, u8 reg
)
338 tval
= bq27x00_read(di
, reg
, false);
340 dev_dbg(di
->dev
, "error reading time register %02x: %d\n",
352 * Read a power avg register.
353 * Return < 0 if something fails.
355 static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info
*di
, u8 reg
)
359 tval
= bq27x00_read(di
, reg
, false);
361 dev_err(di
->dev
, "error reading power avg rgister %02x: %d\n",
366 if (di
->chip
== BQ27500
)
369 return (tval
* BQ27x00_POWER_CONSTANT
) / BQ27000_RS
;
373 * Read flag register.
374 * Return < 0 if something fails.
376 static int bq27x00_battery_read_health(struct bq27x00_device_info
*di
)
380 tval
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
382 dev_err(di
->dev
, "error reading flag register:%d\n", tval
);
386 if ((di
->chip
== BQ27500
)) {
387 if (tval
& BQ27500_FLAG_SOCF
)
388 tval
= POWER_SUPPLY_HEALTH_DEAD
;
389 else if (tval
& BQ27500_FLAG_OTC
)
390 tval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
392 tval
= POWER_SUPPLY_HEALTH_GOOD
;
395 if (tval
& BQ27000_FLAG_EDV1
)
396 tval
= POWER_SUPPLY_HEALTH_DEAD
;
398 tval
= POWER_SUPPLY_HEALTH_GOOD
;
405 static void bq27x00_update(struct bq27x00_device_info
*di
)
407 struct bq27x00_reg_cache cache
= {0, };
408 bool is_bq27500
= di
->chip
== BQ27500
;
409 bool is_bq27425
= di
->chip
== BQ27425
;
411 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, !is_bq27500
);
412 if (cache
.flags
>= 0) {
413 if (!is_bq27500
&& !is_bq27425
414 && (cache
.flags
& BQ27000_FLAG_CI
)) {
415 dev_info(di
->dev
, "battery is not calibrated! ignoring capacity values\n");
416 cache
.capacity
= -ENODATA
;
417 cache
.energy
= -ENODATA
;
418 cache
.time_to_empty
= -ENODATA
;
419 cache
.time_to_empty_avg
= -ENODATA
;
420 cache
.time_to_full
= -ENODATA
;
421 cache
.charge_full
= -ENODATA
;
422 cache
.health
= -ENODATA
;
424 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
426 cache
.energy
= bq27x00_battery_read_energy(di
);
427 cache
.time_to_empty
=
428 bq27x00_battery_read_time(di
,
430 cache
.time_to_empty_avg
=
431 bq27x00_battery_read_time(di
,
434 bq27x00_battery_read_time(di
,
437 cache
.charge_full
= bq27x00_battery_read_lmd(di
);
438 cache
.health
= bq27x00_battery_read_health(di
);
440 cache
.temperature
= bq27x00_battery_read_temperature(di
);
442 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
443 cache
.cycle_count
= bq27x00_battery_read_cyct(di
);
445 bq27x00_battery_read_pwr_avg(di
, BQ27x00_POWER_AVG
);
447 /* We only have to read charge design full once */
448 if (di
->charge_design_full
<= 0)
449 di
->charge_design_full
= bq27x00_battery_read_ilmd(di
);
452 if (memcmp(&di
->cache
, &cache
, sizeof(cache
)) != 0) {
454 power_supply_changed(&di
->bat
);
457 di
->last_update
= jiffies
;
460 static void bq27x00_battery_poll(struct work_struct
*work
)
462 struct bq27x00_device_info
*di
=
463 container_of(work
, struct bq27x00_device_info
, work
.work
);
467 if (poll_interval
> 0) {
468 /* The timer does not have to be accurate. */
469 set_timer_slack(&di
->work
.timer
, poll_interval
* HZ
/ 4);
470 schedule_delayed_work(&di
->work
, poll_interval
* HZ
);
475 * Return the battery average current in µA
476 * Note that current can be negative signed as well
477 * Or 0 if something fails.
479 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
480 union power_supply_propval
*val
)
485 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
487 dev_err(di
->dev
, "error reading current\n");
491 if (bq27xxx_is_chip_version_higher(di
)) {
492 /* bq27500 returns signed value */
493 val
->intval
= (int)((s16
)curr
) * 1000;
495 flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, false);
496 if (flags
& BQ27000_FLAG_CHGS
) {
497 dev_dbg(di
->dev
, "negative current!\n");
501 val
->intval
= curr
* 3570 / BQ27000_RS
;
507 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
508 union power_supply_propval
*val
)
512 if (bq27xxx_is_chip_version_higher(di
)) {
513 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
514 status
= POWER_SUPPLY_STATUS_FULL
;
515 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
516 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
518 status
= POWER_SUPPLY_STATUS_CHARGING
;
520 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
521 status
= POWER_SUPPLY_STATUS_FULL
;
522 else if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
523 status
= POWER_SUPPLY_STATUS_CHARGING
;
524 else if (power_supply_am_i_supplied(&di
->bat
))
525 status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
527 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
530 val
->intval
= status
;
535 static int bq27x00_battery_capacity_level(struct bq27x00_device_info
*di
,
536 union power_supply_propval
*val
)
540 if (bq27xxx_is_chip_version_higher(di
)) {
541 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
542 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
543 else if (di
->cache
.flags
& BQ27500_FLAG_SOC1
)
544 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
545 else if (di
->cache
.flags
& BQ27500_FLAG_SOCF
)
546 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
548 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
550 if (di
->cache
.flags
& BQ27000_FLAG_FC
)
551 level
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
552 else if (di
->cache
.flags
& BQ27000_FLAG_EDV1
)
553 level
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
554 else if (di
->cache
.flags
& BQ27000_FLAG_EDVF
)
555 level
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
557 level
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
566 * Return the battery Voltage in millivolts
567 * Or < 0 if something fails.
569 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
570 union power_supply_propval
*val
)
574 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
576 dev_err(di
->dev
, "error reading voltage\n");
580 val
->intval
= volt
* 1000;
585 static int bq27x00_simple_value(int value
,
586 union power_supply_propval
*val
)
596 #define to_bq27x00_device_info(x) container_of((x), \
597 struct bq27x00_device_info, bat);
599 static int bq27x00_battery_get_property(struct power_supply
*psy
,
600 enum power_supply_property psp
,
601 union power_supply_propval
*val
)
604 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
606 mutex_lock(&di
->lock
);
607 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
)) {
608 cancel_delayed_work_sync(&di
->work
);
609 bq27x00_battery_poll(&di
->work
.work
);
611 mutex_unlock(&di
->lock
);
613 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
617 case POWER_SUPPLY_PROP_STATUS
:
618 ret
= bq27x00_battery_status(di
, val
);
620 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
621 ret
= bq27x00_battery_voltage(di
, val
);
623 case POWER_SUPPLY_PROP_PRESENT
:
624 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
626 case POWER_SUPPLY_PROP_CURRENT_NOW
:
627 ret
= bq27x00_battery_current(di
, val
);
629 case POWER_SUPPLY_PROP_CAPACITY
:
630 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
632 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
633 ret
= bq27x00_battery_capacity_level(di
, val
);
635 case POWER_SUPPLY_PROP_TEMP
:
636 ret
= bq27x00_simple_value(di
->cache
.temperature
, val
);
638 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
639 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
641 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
642 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
644 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
645 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
647 case POWER_SUPPLY_PROP_TECHNOLOGY
:
648 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
650 case POWER_SUPPLY_PROP_CHARGE_NOW
:
651 ret
= bq27x00_simple_value(bq27x00_battery_read_nac(di
), val
);
653 case POWER_SUPPLY_PROP_CHARGE_FULL
:
654 ret
= bq27x00_simple_value(di
->cache
.charge_full
, val
);
656 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
657 ret
= bq27x00_simple_value(di
->charge_design_full
, val
);
659 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
660 ret
= bq27x00_simple_value(di
->cache
.cycle_count
, val
);
662 case POWER_SUPPLY_PROP_ENERGY_NOW
:
663 ret
= bq27x00_simple_value(di
->cache
.energy
, val
);
665 case POWER_SUPPLY_PROP_POWER_AVG
:
666 ret
= bq27x00_simple_value(di
->cache
.power_avg
, val
);
668 case POWER_SUPPLY_PROP_HEALTH
:
669 ret
= bq27x00_simple_value(di
->cache
.health
, val
);
678 static void bq27x00_external_power_changed(struct power_supply
*psy
)
680 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
682 cancel_delayed_work_sync(&di
->work
);
683 schedule_delayed_work(&di
->work
, 0);
686 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
)
690 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
692 if (di
->chip
== BQ27425
) {
693 di
->bat
.properties
= bq27425_battery_props
;
694 di
->bat
.num_properties
= ARRAY_SIZE(bq27425_battery_props
);
696 di
->bat
.properties
= bq27x00_battery_props
;
697 di
->bat
.num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
699 di
->bat
.get_property
= bq27x00_battery_get_property
;
700 di
->bat
.external_power_changed
= bq27x00_external_power_changed
;
702 INIT_DELAYED_WORK(&di
->work
, bq27x00_battery_poll
);
703 mutex_init(&di
->lock
);
705 ret
= power_supply_register(di
->dev
, &di
->bat
);
707 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
711 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
718 static void bq27x00_powersupply_unregister(struct bq27x00_device_info
*di
)
721 * power_supply_unregister call bq27x00_battery_get_property which
722 * call bq27x00_battery_poll.
723 * Make sure that bq27x00_battery_poll will not call
724 * schedule_delayed_work again after unregister (which cause OOPS).
728 cancel_delayed_work_sync(&di
->work
);
730 power_supply_unregister(&di
->bat
);
732 mutex_destroy(&di
->lock
);
736 /* i2c specific code */
737 #ifdef CONFIG_BATTERY_BQ27X00_I2C
739 /* If the system has several batteries we need a different name for each
742 static DEFINE_IDR(battery_id
);
743 static DEFINE_MUTEX(battery_mutex
);
745 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
747 struct i2c_client
*client
= to_i2c_client(di
->dev
);
748 struct i2c_msg msg
[2];
749 unsigned char data
[2];
752 if (!client
->adapter
)
755 msg
[0].addr
= client
->addr
;
758 msg
[0].len
= sizeof(reg
);
759 msg
[1].addr
= client
->addr
;
760 msg
[1].flags
= I2C_M_RD
;
767 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
772 ret
= get_unaligned_le16(data
);
779 static int bq27x00_battery_probe(struct i2c_client
*client
,
780 const struct i2c_device_id
*id
)
783 struct bq27x00_device_info
*di
;
787 /* Get new ID for the new battery device */
788 retval
= idr_pre_get(&battery_id
, GFP_KERNEL
);
791 mutex_lock(&battery_mutex
);
792 retval
= idr_get_new(&battery_id
, client
, &num
);
793 mutex_unlock(&battery_mutex
);
797 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
799 dev_err(&client
->dev
, "failed to allocate device name\n");
804 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
806 dev_err(&client
->dev
, "failed to allocate device info data\n");
812 di
->dev
= &client
->dev
;
813 di
->chip
= id
->driver_data
;
815 di
->bus
.read
= &bq27x00_read_i2c
;
817 if (bq27x00_powersupply_init(di
))
820 i2c_set_clientdata(client
, di
);
829 mutex_lock(&battery_mutex
);
830 idr_remove(&battery_id
, num
);
831 mutex_unlock(&battery_mutex
);
836 static int bq27x00_battery_remove(struct i2c_client
*client
)
838 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
840 bq27x00_powersupply_unregister(di
);
844 mutex_lock(&battery_mutex
);
845 idr_remove(&battery_id
, di
->id
);
846 mutex_unlock(&battery_mutex
);
853 static const struct i2c_device_id bq27x00_id
[] = {
854 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
855 { "bq27500", BQ27500
},
856 { "bq27425", BQ27425
},
859 MODULE_DEVICE_TABLE(i2c
, bq27x00_id
);
861 static struct i2c_driver bq27x00_battery_driver
= {
863 .name
= "bq27x00-battery",
865 .probe
= bq27x00_battery_probe
,
866 .remove
= bq27x00_battery_remove
,
867 .id_table
= bq27x00_id
,
870 static inline int bq27x00_battery_i2c_init(void)
872 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
874 printk(KERN_ERR
"Unable to register BQ27x00 i2c driver\n");
879 static inline void bq27x00_battery_i2c_exit(void)
881 i2c_del_driver(&bq27x00_battery_driver
);
886 static inline int bq27x00_battery_i2c_init(void) { return 0; }
887 static inline void bq27x00_battery_i2c_exit(void) {};
891 /* platform specific code */
892 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
894 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
897 struct device
*dev
= di
->dev
;
898 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
899 unsigned int timeout
= 3;
904 /* Make sure the value has not changed in between reading the
905 * lower and the upper part */
906 upper
= pdata
->read(dev
, reg
+ 1);
912 lower
= pdata
->read(dev
, reg
);
916 upper
= pdata
->read(dev
, reg
+ 1);
917 } while (temp
!= upper
&& --timeout
);
922 return (upper
<< 8) | lower
;
925 return pdata
->read(dev
, reg
);
928 static int __devinit
bq27000_battery_probe(struct platform_device
*pdev
)
930 struct bq27x00_device_info
*di
;
931 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
935 dev_err(&pdev
->dev
, "no platform_data supplied\n");
940 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
944 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
946 dev_err(&pdev
->dev
, "failed to allocate device info data\n");
950 platform_set_drvdata(pdev
, di
);
952 di
->dev
= &pdev
->dev
;
955 di
->bat
.name
= pdata
->name
?: dev_name(&pdev
->dev
);
956 di
->bus
.read
= &bq27000_read_platform
;
958 ret
= bq27x00_powersupply_init(di
);
965 platform_set_drvdata(pdev
, NULL
);
971 static int __devexit
bq27000_battery_remove(struct platform_device
*pdev
)
973 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
975 bq27x00_powersupply_unregister(di
);
977 platform_set_drvdata(pdev
, NULL
);
983 static struct platform_driver bq27000_battery_driver
= {
984 .probe
= bq27000_battery_probe
,
985 .remove
= __devexit_p(bq27000_battery_remove
),
987 .name
= "bq27000-battery",
988 .owner
= THIS_MODULE
,
992 static inline int bq27x00_battery_platform_init(void)
994 int ret
= platform_driver_register(&bq27000_battery_driver
);
996 printk(KERN_ERR
"Unable to register BQ27000 platform driver\n");
1001 static inline void bq27x00_battery_platform_exit(void)
1003 platform_driver_unregister(&bq27000_battery_driver
);
1008 static inline int bq27x00_battery_platform_init(void) { return 0; }
1009 static inline void bq27x00_battery_platform_exit(void) {};
1017 static int __init
bq27x00_battery_init(void)
1021 ret
= bq27x00_battery_i2c_init();
1025 ret
= bq27x00_battery_platform_init();
1027 bq27x00_battery_i2c_exit();
1031 module_init(bq27x00_battery_init
);
1033 static void __exit
bq27x00_battery_exit(void)
1035 bq27x00_battery_platform_exit();
1036 bq27x00_battery_i2c_exit();
1038 module_exit(bq27x00_battery_exit
);
1040 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1041 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
1042 MODULE_LICENSE("GPL");