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>
8 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 #include <linux/module.h>
20 #include <linux/param.h>
21 #include <linux/jiffies.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/power_supply.h>
26 #include <linux/idr.h>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
29 #include <asm/unaligned.h>
31 #include <linux/power/bq27x00_battery.h>
33 #define DRIVER_VERSION "1.1.0"
35 #define BQ27x00_REG_TEMP 0x06
36 #define BQ27x00_REG_VOLT 0x08
37 #define BQ27x00_REG_AI 0x14
38 #define BQ27x00_REG_FLAGS 0x0A
39 #define BQ27x00_REG_TTE 0x16
40 #define BQ27x00_REG_TTF 0x18
41 #define BQ27x00_REG_TTECP 0x26
43 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
44 #define BQ27000_FLAG_CHGS BIT(7)
46 #define BQ27500_REG_SOC 0x2c
47 #define BQ27500_FLAG_DSC BIT(0)
48 #define BQ27500_FLAG_FC BIT(9)
50 #define BQ27000_RS 20 /* Resistor sense */
52 struct bq27x00_device_info
;
53 struct bq27x00_access_methods
{
54 int (*read
)(struct bq27x00_device_info
*di
, u8 reg
, bool single
);
57 enum bq27x00_chip
{ BQ27000
, BQ27500
};
59 struct bq27x00_reg_cache
{
62 int time_to_empty_avg
;
70 struct bq27x00_device_info
{
73 enum bq27x00_chip chip
;
75 struct bq27x00_reg_cache cache
;
76 unsigned long last_update
;
78 struct power_supply bat
;
80 struct bq27x00_access_methods bus
;
83 static enum power_supply_property bq27x00_battery_props
[] = {
84 POWER_SUPPLY_PROP_STATUS
,
85 POWER_SUPPLY_PROP_PRESENT
,
86 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
87 POWER_SUPPLY_PROP_CURRENT_NOW
,
88 POWER_SUPPLY_PROP_CAPACITY
,
89 POWER_SUPPLY_PROP_TEMP
,
90 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
91 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
92 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
93 POWER_SUPPLY_PROP_TECHNOLOGY
,
97 * Common code for BQ27x00 devices
100 static inline int bq27x00_read(struct bq27x00_device_info
*di
, u8 reg
,
103 return di
->bus
.read(di
, reg
, single
);
107 * Return the battery Relative State-of-Charge
108 * Or < 0 if something fails.
110 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info
*di
)
114 if (di
->chip
== BQ27500
)
115 rsoc
= bq27x00_read(di
, BQ27500_REG_SOC
, false);
117 rsoc
= bq27x00_read(di
, BQ27000_REG_RSOC
, true);
120 dev_err(di
->dev
, "error reading relative State-of-Charge\n");
126 * Read a time register.
127 * Return < 0 if something fails.
129 static int bq27x00_battery_read_time(struct bq27x00_device_info
*di
, u8 reg
)
133 tval
= bq27x00_read(di
, reg
, false);
135 dev_err(di
->dev
, "error reading register %02x: %d\n", reg
, tval
);
145 static void bq27x00_update(struct bq27x00_device_info
*di
)
147 struct bq27x00_reg_cache cache
= {0, };
148 bool is_bq27500
= di
->chip
== BQ27500
;
150 cache
.flags
= bq27x00_read(di
, BQ27x00_REG_FLAGS
, is_bq27500
);
151 if (cache
.flags
>= 0) {
152 cache
.capacity
= bq27x00_battery_read_rsoc(di
);
153 cache
.temperature
= bq27x00_read(di
, BQ27x00_REG_TEMP
, false);
154 cache
.time_to_empty
= bq27x00_battery_read_time(di
, BQ27x00_REG_TTE
);
155 cache
.time_to_empty_avg
= bq27x00_battery_read_time(di
, BQ27x00_REG_TTECP
);
156 cache
.time_to_full
= bq27x00_battery_read_time(di
, BQ27x00_REG_TTF
);
159 cache
.current_now
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
162 /* Ignore current_now which is a snapshot of the current battery state
163 * and is likely to be different even between two consecutive reads */
164 if (memcmp(&di
->cache
, &cache
, sizeof(cache
) - sizeof(int)) != 0) {
166 power_supply_changed(&di
->bat
);
169 di
->last_update
= jiffies
;
173 * Return the battery temperature in tenths of degree Celsius
174 * Or < 0 if something fails.
176 static int bq27x00_battery_temperature(struct bq27x00_device_info
*di
,
177 union power_supply_propval
*val
)
179 if (di
->cache
.temperature
< 0)
180 return di
->cache
.temperature
;
182 if (di
->chip
== BQ27500
)
183 val
->intval
= di
->cache
.temperature
- 2731;
185 val
->intval
= ((di
->cache
.temperature
* 5) - 5463) / 2;
191 * Return the battery average current
192 * Note that current can be negative signed as well
193 * Or 0 if something fails.
195 static int bq27x00_battery_current(struct bq27x00_device_info
*di
,
196 union power_supply_propval
*val
)
200 if (di
->chip
== BQ27500
)
201 curr
= bq27x00_read(di
, BQ27x00_REG_AI
, false);
203 curr
= di
->cache
.current_now
;
208 if (di
->chip
== BQ27500
) {
209 /* bq27500 returns signed value */
210 val
->intval
= (int)((s16
)curr
) * 1000;
212 if (di
->cache
.flags
& BQ27000_FLAG_CHGS
) {
213 dev_dbg(di
->dev
, "negative current!\n");
217 val
->intval
= curr
* 3570 / BQ27000_RS
;
223 static int bq27x00_battery_status(struct bq27x00_device_info
*di
,
224 union power_supply_propval
*val
)
228 if (di
->chip
== BQ27500
) {
229 if (di
->cache
.flags
& BQ27500_FLAG_FC
)
230 status
= POWER_SUPPLY_STATUS_FULL
;
231 else if (di
->cache
.flags
& BQ27500_FLAG_DSC
)
232 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
234 status
= POWER_SUPPLY_STATUS_CHARGING
;
236 if (di
->cache
.flags
& BQ27000_FLAG_CHGS
)
237 status
= POWER_SUPPLY_STATUS_CHARGING
;
239 status
= POWER_SUPPLY_STATUS_DISCHARGING
;
242 val
->intval
= status
;
248 * Return the battery Voltage in milivolts
249 * Or < 0 if something fails.
251 static int bq27x00_battery_voltage(struct bq27x00_device_info
*di
,
252 union power_supply_propval
*val
)
256 volt
= bq27x00_read(di
, BQ27x00_REG_VOLT
, false);
260 val
->intval
= volt
* 1000;
265 static int bq27x00_simple_value(int value
,
266 union power_supply_propval
*val
)
276 #define to_bq27x00_device_info(x) container_of((x), \
277 struct bq27x00_device_info, bat);
279 static int bq27x00_battery_get_property(struct power_supply
*psy
,
280 enum power_supply_property psp
,
281 union power_supply_propval
*val
)
284 struct bq27x00_device_info
*di
= to_bq27x00_device_info(psy
);
286 if (time_is_before_jiffies(di
->last_update
+ 5 * HZ
))
289 if (psp
!= POWER_SUPPLY_PROP_PRESENT
&& di
->cache
.flags
< 0)
293 case POWER_SUPPLY_PROP_STATUS
:
294 ret
= bq27x00_battery_status(di
, val
);
296 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
297 ret
= bq27x00_battery_voltage(di
, val
);
299 case POWER_SUPPLY_PROP_PRESENT
:
300 val
->intval
= di
->cache
.flags
< 0 ? 0 : 1;
302 case POWER_SUPPLY_PROP_CURRENT_NOW
:
303 ret
= bq27x00_battery_current(di
, val
);
305 case POWER_SUPPLY_PROP_CAPACITY
:
306 ret
= bq27x00_simple_value(di
->cache
.capacity
, val
);
308 case POWER_SUPPLY_PROP_TEMP
:
309 ret
= bq27x00_battery_temperature(di
, val
);
311 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
312 ret
= bq27x00_simple_value(di
->cache
.time_to_empty
, val
);
314 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
315 ret
= bq27x00_simple_value(di
->cache
.time_to_empty_avg
, val
);
317 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
318 ret
= bq27x00_simple_value(di
->cache
.time_to_full
, val
);
320 case POWER_SUPPLY_PROP_TECHNOLOGY
:
321 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
330 static int bq27x00_powersupply_init(struct bq27x00_device_info
*di
)
334 di
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
335 di
->bat
.properties
= bq27x00_battery_props
;
336 di
->bat
.num_properties
= ARRAY_SIZE(bq27x00_battery_props
);
337 di
->bat
.get_property
= bq27x00_battery_get_property
;
338 di
->bat
.external_power_changed
= NULL
;
340 ret
= power_supply_register(di
->dev
, &di
->bat
);
342 dev_err(di
->dev
, "failed to register battery: %d\n", ret
);
346 dev_info(di
->dev
, "support ver. %s enabled\n", DRIVER_VERSION
);
354 /* i2c specific code */
355 #ifdef CONFIG_BATTERY_BQ27X00_I2C
357 /* If the system has several batteries we need a different name for each
360 static DEFINE_IDR(battery_id
);
361 static DEFINE_MUTEX(battery_mutex
);
363 static int bq27x00_read_i2c(struct bq27x00_device_info
*di
, u8 reg
, bool single
)
365 struct i2c_client
*client
= to_i2c_client(di
->dev
);
366 struct i2c_msg msg
[1];
367 unsigned char data
[2];
370 if (!client
->adapter
)
373 msg
->addr
= client
->addr
;
379 ret
= i2c_transfer(client
->adapter
, msg
, 1);
387 msg
->flags
= I2C_M_RD
;
388 ret
= i2c_transfer(client
->adapter
, msg
, 1);
391 ret
= get_unaligned_le16(data
);
399 static int bq27x00_battery_probe(struct i2c_client
*client
,
400 const struct i2c_device_id
*id
)
403 struct bq27x00_device_info
*di
;
407 /* Get new ID for the new battery device */
408 retval
= idr_pre_get(&battery_id
, GFP_KERNEL
);
411 mutex_lock(&battery_mutex
);
412 retval
= idr_get_new(&battery_id
, client
, &num
);
413 mutex_unlock(&battery_mutex
);
417 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
419 dev_err(&client
->dev
, "failed to allocate device name\n");
424 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
426 dev_err(&client
->dev
, "failed to allocate device info data\n");
432 di
->dev
= &client
->dev
;
433 di
->chip
= id
->driver_data
;
435 di
->bus
.read
= &bq27x00_read_i2c
;
437 if (bq27x00_powersupply_init(di
))
440 i2c_set_clientdata(client
, di
);
449 mutex_lock(&battery_mutex
);
450 idr_remove(&battery_id
, num
);
451 mutex_unlock(&battery_mutex
);
456 static int bq27x00_battery_remove(struct i2c_client
*client
)
458 struct bq27x00_device_info
*di
= i2c_get_clientdata(client
);
460 power_supply_unregister(&di
->bat
);
464 mutex_lock(&battery_mutex
);
465 idr_remove(&battery_id
, di
->id
);
466 mutex_unlock(&battery_mutex
);
473 static const struct i2c_device_id bq27x00_id
[] = {
474 { "bq27200", BQ27000
}, /* bq27200 is same as bq27000, but with i2c */
475 { "bq27500", BQ27500
},
479 static struct i2c_driver bq27x00_battery_driver
= {
481 .name
= "bq27x00-battery",
483 .probe
= bq27x00_battery_probe
,
484 .remove
= bq27x00_battery_remove
,
485 .id_table
= bq27x00_id
,
488 static inline int bq27x00_battery_i2c_init(void)
490 int ret
= i2c_add_driver(&bq27x00_battery_driver
);
492 printk(KERN_ERR
"Unable to register BQ27x00 i2c driver\n");
497 static inline void bq27x00_battery_i2c_exit(void)
499 i2c_del_driver(&bq27x00_battery_driver
);
504 static inline int bq27x00_battery_i2c_init(void) { return 0; }
505 static inline void bq27x00_battery_i2c_exit(void) {};
509 /* platform specific code */
510 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
512 static int bq27000_read_platform(struct bq27x00_device_info
*di
, u8 reg
,
515 struct device
*dev
= di
->dev
;
516 struct bq27000_platform_data
*pdata
= dev
->platform_data
;
517 unsigned int timeout
= 3;
522 /* Make sure the value has not changed in between reading the
523 * lower and the upper part */
524 upper
= pdata
->read(dev
, reg
+ 1);
530 lower
= pdata
->read(dev
, reg
);
534 upper
= pdata
->read(dev
, reg
+ 1);
535 } while (temp
!= upper
&& --timeout
);
540 return (upper
<< 8) | lower
;
543 return pdata
->read(dev
, reg
);
546 static int __devinit
bq27000_battery_probe(struct platform_device
*pdev
)
548 struct bq27x00_device_info
*di
;
549 struct bq27000_platform_data
*pdata
= pdev
->dev
.platform_data
;
553 dev_err(&pdev
->dev
, "no platform_data supplied\n");
558 dev_err(&pdev
->dev
, "no hdq read callback supplied\n");
562 di
= kzalloc(sizeof(*di
), GFP_KERNEL
);
564 dev_err(&pdev
->dev
, "failed to allocate device info data\n");
568 platform_set_drvdata(pdev
, di
);
570 di
->dev
= &pdev
->dev
;
573 di
->bat
.name
= pdata
->name
?: dev_name(&pdev
->dev
);
574 di
->bus
.read
= &bq27000_read_platform
;
576 ret
= bq27x00_powersupply_init(di
);
583 platform_set_drvdata(pdev
, NULL
);
589 static int __devexit
bq27000_battery_remove(struct platform_device
*pdev
)
591 struct bq27x00_device_info
*di
= platform_get_drvdata(pdev
);
593 power_supply_unregister(&di
->bat
);
594 platform_set_drvdata(pdev
, NULL
);
600 static struct platform_driver bq27000_battery_driver
= {
601 .probe
= bq27000_battery_probe
,
602 .remove
= __devexit_p(bq27000_battery_remove
),
604 .name
= "bq27000-battery",
605 .owner
= THIS_MODULE
,
609 static inline int bq27x00_battery_platform_init(void)
611 int ret
= platform_driver_register(&bq27000_battery_driver
);
613 printk(KERN_ERR
"Unable to register BQ27000 platform driver\n");
618 static inline void bq27x00_battery_platform_exit(void)
620 platform_driver_unregister(&bq27000_battery_driver
);
625 static inline int bq27x00_battery_platform_init(void) { return 0; }
626 static inline void bq27x00_battery_platform_exit(void) {};
634 static int __init
bq27x00_battery_init(void)
638 ret
= bq27x00_battery_i2c_init();
642 ret
= bq27x00_battery_platform_init();
644 bq27x00_battery_i2c_exit();
648 module_init(bq27x00_battery_init
);
650 static void __exit
bq27x00_battery_exit(void)
652 bq27x00_battery_platform_exit();
653 bq27x00_battery_i2c_exit();
655 module_exit(bq27x00_battery_exit
);
657 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
658 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
659 MODULE_LICENSE("GPL");