28 #include <linux/module.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
38 #include <asm/unaligned.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
52 #define BQ27x00_REG_LMD 0x12
53 #define BQ27x00_REG_CYCT 0x2A
54 #define BQ27x00_REG_AE 0x22
55 #define BQ27x00_POWER_AVG 0x24
57 #define BQ27000_REG_RSOC 0x0B
58 #define BQ27000_REG_ILMD 0x76
59 #define BQ27000_FLAG_EDVF BIT(0)
60 #define BQ27000_FLAG_EDV1 BIT(1)
61 #define BQ27000_FLAG_CI BIT(4)
62 #define BQ27000_FLAG_FC BIT(5)
63 #define BQ27000_FLAG_CHGS BIT(7)
65 #define BQ27500_REG_SOC 0x2C
66 #define BQ27500_REG_DCAP 0x3C
67 #define BQ27500_FLAG_DSC BIT(0)
68 #define BQ27500_FLAG_SOCF BIT(1)
69 #define BQ27500_FLAG_SOC1 BIT(2)
70 #define BQ27500_FLAG_FC BIT(9)
71 #define BQ27500_FLAG_OTC BIT(15)
74 #define BQ27425_REG_OFFSET 0x04
75 #define BQ27425_REG_SOC 0x18
78 #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000)
154 static unsigned int poll_interval = 360;
157 "0 disables polling");
168 return di->
bus.read(di, reg, single);
199 dev_dbg(di->
dev,
"error reading relative State-of-Charge\n");
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))
253 if (bq27xxx_is_chip_version_higher(di))
259 dev_dbg(di->
dev,
"error reading initial last measured discharge\n");
263 if (bq27xxx_is_chip_version_higher(di))
281 dev_dbg(di->
dev,
"error reading available energy\n");
303 dev_err(di->
dev,
"error reading temperature\n");
307 if (bq27xxx_is_chip_version_higher(di))
310 temp = ((temp * 5) - 5463) / 2;
325 dev_err(di->
dev,
"error reading cycle count total\n");
338 tval = bq27x00_read(di, reg,
false);
340 dev_dbg(di->
dev,
"error reading time register %02x: %d\n",
359 tval = bq27x00_read(di, reg,
false);
361 dev_err(di->
dev,
"error reading power avg rgister %02x: %d\n",
382 dev_err(di->
dev,
"error reading flag register:%d\n", tval);
412 if (cache.
flags >= 0) {
413 if (!is_bq27500 && !is_bq27425
415 dev_info(di->
dev,
"battery is not calibrated! ignoring capacity values\n");
424 cache.
capacity = bq27x00_battery_read_rsoc(di);
426 cache.
energy = bq27x00_battery_read_energy(di);
428 bq27x00_battery_read_time(di,
431 bq27x00_battery_read_time(di,
434 bq27x00_battery_read_time(di,
438 cache.
health = bq27x00_battery_read_health(di);
440 cache.
temperature = bq27x00_battery_read_temperature(di);
452 if (
memcmp(&di->
cache, &cache,
sizeof(cache)) != 0) {
467 if (poll_interval > 0) {
491 if (bq27xxx_is_chip_version_higher(di)) {
512 if (bq27xxx_is_chip_version_higher(di)) {
540 if (bq27xxx_is_chip_version_higher(di)) {
545 else if (di->
cache.flags & BQ27500_FLAG_SOCF)
580 val->
intval = volt * 1000;
585 static int bq27x00_simple_value(
int value,
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,
609 bq27x00_battery_poll(&di->
work.work);
618 ret = bq27x00_battery_status(di, val);
621 ret = bq27x00_battery_voltage(di, val);
627 ret = bq27x00_battery_current(di, val);
630 ret = bq27x00_simple_value(di->
cache.capacity, val);
633 ret = bq27x00_battery_capacity_level(di, val);
636 ret = bq27x00_simple_value(di->
cache.temperature, val);
639 ret = bq27x00_simple_value(di->
cache.time_to_empty, val);
642 ret = bq27x00_simple_value(di->
cache.time_to_empty_avg, val);
645 ret = bq27x00_simple_value(di->
cache.time_to_full, val);
651 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
654 ret = bq27x00_simple_value(di->
cache.charge_full, val);
660 ret = bq27x00_simple_value(di->
cache.cycle_count, val);
663 ret = bq27x00_simple_value(di->
cache.energy, val);
666 ret = bq27x00_simple_value(di->
cache.power_avg, val);
669 ret = bq27x00_simple_value(di->
cache.health, val);
678 static void bq27x00_external_power_changed(
struct power_supply *psy)
693 di->
bat.properties = bq27425_battery_props;
696 di->
bat.properties = bq27x00_battery_props;
699 di->
bat.get_property = bq27x00_battery_get_property;
700 di->
bat.external_power_changed = bq27x00_external_power_changed;
707 dev_err(di->
dev,
"failed to register battery: %d\n", ret);
737 #ifdef CONFIG_BATTERY_BQ27X00_I2C
749 unsigned char data[2];
772 ret = get_unaligned_le16(data);
779 static int bq27x00_battery_probe(
struct i2c_client *client,
799 dev_err(&client->
dev,
"failed to allocate device name\n");
806 dev_err(&client->
dev,
"failed to allocate device info data\n");
813 di->
chip =
id->driver_data;
815 di->
bus.read = &bq27x00_read_i2c;
817 retval = bq27x00_powersupply_init(di);
821 i2c_set_clientdata(client, di);
837 static int bq27x00_battery_remove(
struct i2c_client *client)
841 bq27x00_powersupply_unregister(di);
862 static struct i2c_driver bq27x00_battery_driver = {
864 .name =
"bq27x00-battery",
866 .probe = bq27x00_battery_probe,
867 .remove = bq27x00_battery_remove,
868 .id_table = bq27x00_id,
871 static inline int bq27x00_battery_i2c_init(
void)
873 int ret = i2c_add_driver(&bq27x00_battery_driver);
880 static inline void bq27x00_battery_i2c_exit(
void)
887 static inline int bq27x00_battery_i2c_init(
void) {
return 0; }
888 static inline void bq27x00_battery_i2c_exit(
void) {};
893 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
907 upper = pdata->
read(dev, reg + 1);
913 lower = pdata->
read(dev, reg);
917 upper = pdata->
read(dev, reg + 1);
918 }
while (temp != upper && --timeout);
923 return (upper << 8) | lower;
926 return pdata->
read(dev, reg);
936 dev_err(&pdev->
dev,
"no platform_data supplied\n");
941 dev_err(&pdev->
dev,
"no hdq read callback supplied\n");
947 dev_err(&pdev->
dev,
"failed to allocate device info data\n");
951 platform_set_drvdata(pdev, di);
956 di->
bat.name = pdata->
name ?: dev_name(&pdev->
dev);
957 di->
bus.read = &bq27000_read_platform;
959 ret = bq27x00_powersupply_init(di);
966 platform_set_drvdata(pdev,
NULL);
976 bq27x00_powersupply_unregister(di);
978 platform_set_drvdata(pdev,
NULL);
985 .
probe = bq27000_battery_probe,
988 .name =
"bq27000-battery",
993 static inline int bq27x00_battery_platform_init(
void)
1002 static inline void bq27x00_battery_platform_exit(
void)
1009 static inline int bq27x00_battery_platform_init(
void) {
return 0; }
1010 static inline void bq27x00_battery_platform_exit(
void) {};
1018 static int __init bq27x00_battery_init(
void)
1022 ret = bq27x00_battery_i2c_init();
1026 ret = bq27x00_battery_platform_init();
1028 bq27x00_battery_i2c_exit();
1034 static void __exit bq27x00_battery_exit(
void)
1036 bq27x00_battery_platform_exit();
1037 bq27x00_battery_i2c_exit();