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
27 #include <linux/module.h>
28 #include <linux/param.h>
29 #include <linux/jiffies.h>
30 #include <linux/workqueue.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/power_supply.h>
34 #include <linux/idr.h>
35 #include <linux/i2c.h>
36 #include <linux/slab.h>
37 #include <asm/unaligned.h>
39 #include <linux/power/bq27x00_battery.h>
41 #define DRIVER_VERSION "1.2.0"
43 #define BQ27x00_REG_TEMP 0x06
44 #define BQ27x00_REG_VOLT 0x08
45 #define BQ27x00_REG_AI 0x14
46 #define BQ27x00_REG_FLAGS 0x0A
47 #define BQ27x00_REG_TTE 0x16
48 #define BQ27x00_REG_TTF 0x18
49 #define BQ27x00_REG_TTECP 0x26
50 #define BQ27x00_REG_NAC 0x0C /* Nominal available capaciy */
51 #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
52 #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
53 #define BQ27x00_REG_AE 0x22 /* Available enery */
55 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
56 #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
57 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
58 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
59 #define BQ27000_FLAG_FC BIT(5)
60 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
62 #define BQ27500_REG_SOC 0x2C
63 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
64 #define BQ27500_FLAG_DSC BIT(0)
65 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
66 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
67 #define BQ27500_FLAG_FC BIT(9)
69 #define BQ27000_RS 20 /* Resistor sense */
71 struct bq27x00_device_info;
72 struct bq27x00_access_methods {
73 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
76 enum bq27x00_chip { BQ27000, BQ27500 };
78 struct bq27x00_reg_cache {
81 int time_to_empty_avg;
89 struct bq27x00_device_info {
92 enum bq27x00_chip chip;
94 struct bq27x00_reg_cache cache;
95 int charge_design_full;
97 unsigned long last_update;
98 struct delayed_work work;
100 struct power_supply bat;
102 struct bq27x00_access_methods bus;
107 static enum power_supply_property bq27x00_battery_props[] = {
108 POWER_SUPPLY_PROP_STATUS,
109 POWER_SUPPLY_PROP_PRESENT,
110 POWER_SUPPLY_PROP_VOLTAGE_NOW,
111 POWER_SUPPLY_PROP_CURRENT_NOW,
112 POWER_SUPPLY_PROP_CAPACITY,
113 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
114 POWER_SUPPLY_PROP_TEMP,
115 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
116 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
117 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
118 POWER_SUPPLY_PROP_TECHNOLOGY,
119 POWER_SUPPLY_PROP_CHARGE_FULL,
120 POWER_SUPPLY_PROP_CHARGE_NOW,
121 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
122 POWER_SUPPLY_PROP_CYCLE_COUNT,
123 POWER_SUPPLY_PROP_ENERGY_NOW,
126 static unsigned int poll_interval = 360;
127 module_param(poll_interval, uint, 0644);
128 MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
129 "0 disables polling");
132 * Common code for BQ27x00 devices
135 static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
138 return di->bus.read(di, reg, single);
142 * Return the battery Relative State-of-Charge
143 * Or < 0 if something fails.
145 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
149 if (di->chip == BQ27500)
150 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
152 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
155 dev_err(di->dev, "error reading relative State-of-Charge\n");
161 * Return a battery charge value in µAh
162 * Or < 0 if something fails.
164 static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
168 charge = bq27x00_read(di, reg, false);
170 dev_err(di->dev, "error reading nominal available capacity\n");
174 if (di->chip == BQ27500)
177 charge = charge * 3570 / BQ27000_RS;
183 * Return the battery Nominal available capaciy in µAh
184 * Or < 0 if something fails.
186 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
188 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
192 * Return the battery Last measured discharge in µAh
193 * Or < 0 if something fails.
195 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
197 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
201 * Return the battery Initial last measured discharge in µAh
202 * Or < 0 if something fails.
204 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
208 if (di->chip == BQ27500)
209 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
211 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
214 dev_err(di->dev, "error reading initial last measured discharge\n");
218 if (di->chip == BQ27500)
221 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
227 * Return the battery Cycle count total
228 * Or < 0 if something fails.
230 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
234 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
236 dev_err(di->dev, "error reading cycle count total\n");
242 * Read a time register.
243 * Return < 0 if something fails.
245 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
249 tval = bq27x00_read(di, reg, false);
251 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
261 static void bq27x00_update(struct bq27x00_device_info *di)
263 struct bq27x00_reg_cache cache = {0, };
264 bool is_bq27500 = di->chip == BQ27500;
266 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
267 if (cache.flags >= 0) {
268 cache.capacity = bq27x00_battery_read_rsoc(di);
269 cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
270 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
271 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
272 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
273 cache.charge_full = bq27x00_battery_read_lmd(di);
274 cache.cycle_count = bq27x00_battery_read_cyct(di);
276 /* We only have to read charge design full once */
277 if (di->charge_design_full <= 0)
278 di->charge_design_full = bq27x00_battery_read_ilmd(di);
281 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
283 power_supply_changed(&di->bat);
286 di->last_update = jiffies;
289 static void bq27x00_battery_poll(struct work_struct *work)
291 struct bq27x00_device_info *di =
292 container_of(work, struct bq27x00_device_info, work.work);
296 if (poll_interval > 0) {
297 /* The timer does not have to be accurate. */
298 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
299 schedule_delayed_work(&di->work, poll_interval * HZ);
305 * Return the battery temperature in tenths of degree Celsius
306 * Or < 0 if something fails.
308 static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
309 union power_supply_propval *val)
311 if (di->cache.temperature < 0)
312 return di->cache.temperature;
314 if (di->chip == BQ27500)
315 val->intval = di->cache.temperature - 2731;
317 val->intval = ((di->cache.temperature * 5) - 5463) / 2;
323 * Return the battery average current in µA
324 * Note that current can be negative signed as well
325 * Or 0 if something fails.
327 static int bq27x00_battery_current(struct bq27x00_device_info *di,
328 union power_supply_propval *val)
333 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
337 if (di->chip == BQ27500) {
338 /* bq27500 returns signed value */
339 val->intval = (int)((s16)curr) * 1000;
341 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
342 if (flags & BQ27000_FLAG_CHGS) {
343 dev_dbg(di->dev, "negative current!\n");
347 val->intval = curr * 3570 / BQ27000_RS;
353 static int bq27x00_battery_status(struct bq27x00_device_info *di,
354 union power_supply_propval *val)
358 if (di->chip == BQ27500) {
359 if (di->cache.flags & BQ27500_FLAG_FC)
360 status = POWER_SUPPLY_STATUS_FULL;
361 else if (di->cache.flags & BQ27500_FLAG_DSC)
362 status = POWER_SUPPLY_STATUS_DISCHARGING;
364 status = POWER_SUPPLY_STATUS_CHARGING;
366 if (di->cache.flags & BQ27000_FLAG_FC)
367 status = POWER_SUPPLY_STATUS_FULL;
368 else if (di->cache.flags & BQ27000_FLAG_CHGS)
369 status = POWER_SUPPLY_STATUS_CHARGING;
370 else if (power_supply_am_i_supplied(&di->bat))
371 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
373 status = POWER_SUPPLY_STATUS_DISCHARGING;
376 val->intval = status;
381 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
382 union power_supply_propval *val)
386 if (di->chip == BQ27500) {
387 if (di->cache.flags & BQ27500_FLAG_FC)
388 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
389 else if (di->cache.flags & BQ27500_FLAG_SOC1)
390 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
391 else if (di->cache.flags & BQ27500_FLAG_SOCF)
392 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
394 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
396 if (di->cache.flags & BQ27000_FLAG_FC)
397 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
398 else if (di->cache.flags & BQ27000_FLAG_EDV1)
399 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
400 else if (di->cache.flags & BQ27000_FLAG_EDVF)
401 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
403 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
412 * Return the battery Voltage in milivolts
413 * Or < 0 if something fails.
415 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
416 union power_supply_propval *val)
420 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
424 val->intval = volt * 1000;
430 * Return the battery Available energy in µWh
431 * Or < 0 if something fails.
433 static int bq27x00_battery_energy(struct bq27x00_device_info *di,
434 union power_supply_propval *val)
438 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
440 dev_err(di->dev, "error reading available energy\n");
444 if (di->chip == BQ27500)
447 ae = ae * 29200 / BQ27000_RS;
455 static int bq27x00_simple_value(int value,
456 union power_supply_propval *val)
466 #define to_bq27x00_device_info(x) container_of((x), \
467 struct bq27x00_device_info, bat);
469 static int bq27x00_battery_get_property(struct power_supply *psy,
470 enum power_supply_property psp,
471 union power_supply_propval *val)
474 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
476 mutex_lock(&di->lock);
477 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
478 cancel_delayed_work_sync(&di->work);
479 bq27x00_battery_poll(&di->work.work);
481 mutex_unlock(&di->lock);
483 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
487 case POWER_SUPPLY_PROP_STATUS:
488 ret = bq27x00_battery_status(di, val);
490 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
491 ret = bq27x00_battery_voltage(di, val);
493 case POWER_SUPPLY_PROP_PRESENT:
494 val->intval = di->cache.flags < 0 ? 0 : 1;
496 case POWER_SUPPLY_PROP_CURRENT_NOW:
497 ret = bq27x00_battery_current(di, val);
499 case POWER_SUPPLY_PROP_CAPACITY:
500 ret = bq27x00_simple_value(di->cache.capacity, val);
502 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
503 ret = bq27x00_battery_capacity_level(di, val);
505 case POWER_SUPPLY_PROP_TEMP:
506 ret = bq27x00_battery_temperature(di, val);
508 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
509 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
511 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
512 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
514 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
515 ret = bq27x00_simple_value(di->cache.time_to_full, val);
517 case POWER_SUPPLY_PROP_TECHNOLOGY:
518 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
520 case POWER_SUPPLY_PROP_CHARGE_NOW:
521 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
523 case POWER_SUPPLY_PROP_CHARGE_FULL:
524 ret = bq27x00_simple_value(di->cache.charge_full, val);
526 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
527 ret = bq27x00_simple_value(di->charge_design_full, val);
529 case POWER_SUPPLY_PROP_CYCLE_COUNT:
530 ret = bq27x00_simple_value(di->cache.cycle_count, val);
532 case POWER_SUPPLY_PROP_ENERGY_NOW:
533 ret = bq27x00_battery_energy(di, val);
542 static void bq27x00_external_power_changed(struct power_supply *psy)
544 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
546 cancel_delayed_work_sync(&di->work);
547 schedule_delayed_work(&di->work, 0);
550 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
554 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
555 di->bat.properties = bq27x00_battery_props;
556 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
557 di->bat.get_property = bq27x00_battery_get_property;
558 di->bat.external_power_changed = bq27x00_external_power_changed;
560 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
561 mutex_init(&di->lock);
563 ret = power_supply_register(di->dev, &di->bat);
565 dev_err(di->dev, "failed to register battery: %d\n", ret);
569 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
576 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
578 cancel_delayed_work_sync(&di->work);
580 power_supply_unregister(&di->bat);
582 mutex_destroy(&di->lock);
586 /* i2c specific code */
587 #ifdef CONFIG_BATTERY_BQ27X00_I2C
589 /* If the system has several batteries we need a different name for each
592 static DEFINE_IDR(battery_id);
593 static DEFINE_MUTEX(battery_mutex);
595 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
597 struct i2c_client *client = to_i2c_client(di->dev);
598 struct i2c_msg msg[2];
599 unsigned char data[2];
602 if (!client->adapter)
605 msg[0].addr = client->addr;
608 msg[0].len = sizeof(reg);
609 msg[1].addr = client->addr;
610 msg[1].flags = I2C_M_RD;
617 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
622 ret = get_unaligned_le16(data);
629 static int bq27x00_battery_probe(struct i2c_client *client,
630 const struct i2c_device_id *id)
633 struct bq27x00_device_info *di;
637 /* Get new ID for the new battery device */
638 retval = idr_pre_get(&battery_id, GFP_KERNEL);
641 mutex_lock(&battery_mutex);
642 retval = idr_get_new(&battery_id, client, &num);
643 mutex_unlock(&battery_mutex);
647 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
649 dev_err(&client->dev, "failed to allocate device name\n");
654 di = kzalloc(sizeof(*di), GFP_KERNEL);
656 dev_err(&client->dev, "failed to allocate device info data\n");
662 di->dev = &client->dev;
663 di->chip = id->driver_data;
665 di->bus.read = &bq27x00_read_i2c;
667 if (bq27x00_powersupply_init(di))
670 i2c_set_clientdata(client, di);
679 mutex_lock(&battery_mutex);
680 idr_remove(&battery_id, num);
681 mutex_unlock(&battery_mutex);
686 static int bq27x00_battery_remove(struct i2c_client *client)
688 struct bq27x00_device_info *di = i2c_get_clientdata(client);
690 bq27x00_powersupply_unregister(di);
694 mutex_lock(&battery_mutex);
695 idr_remove(&battery_id, di->id);
696 mutex_unlock(&battery_mutex);
703 static const struct i2c_device_id bq27x00_id[] = {
704 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
705 { "bq27500", BQ27500 },
708 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
710 static struct i2c_driver bq27x00_battery_driver = {
712 .name = "bq27x00-battery",
714 .probe = bq27x00_battery_probe,
715 .remove = bq27x00_battery_remove,
716 .id_table = bq27x00_id,
719 static inline int bq27x00_battery_i2c_init(void)
721 int ret = i2c_add_driver(&bq27x00_battery_driver);
723 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
728 static inline void bq27x00_battery_i2c_exit(void)
730 i2c_del_driver(&bq27x00_battery_driver);
735 static inline int bq27x00_battery_i2c_init(void) { return 0; }
736 static inline void bq27x00_battery_i2c_exit(void) {};
740 /* platform specific code */
741 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
743 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
746 struct device *dev = di->dev;
747 struct bq27000_platform_data *pdata = dev->platform_data;
748 unsigned int timeout = 3;
753 /* Make sure the value has not changed in between reading the
754 * lower and the upper part */
755 upper = pdata->read(dev, reg + 1);
761 lower = pdata->read(dev, reg);
765 upper = pdata->read(dev, reg + 1);
766 } while (temp != upper && --timeout);
771 return (upper << 8) | lower;
774 return pdata->read(dev, reg);
777 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
779 struct bq27x00_device_info *di;
780 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
784 dev_err(&pdev->dev, "no platform_data supplied\n");
789 dev_err(&pdev->dev, "no hdq read callback supplied\n");
793 di = kzalloc(sizeof(*di), GFP_KERNEL);
795 dev_err(&pdev->dev, "failed to allocate device info data\n");
799 platform_set_drvdata(pdev, di);
801 di->dev = &pdev->dev;
804 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
805 di->bus.read = &bq27000_read_platform;
807 ret = bq27x00_powersupply_init(di);
814 platform_set_drvdata(pdev, NULL);
820 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
822 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
824 bq27x00_powersupply_unregister(di);
826 platform_set_drvdata(pdev, NULL);
832 static struct platform_driver bq27000_battery_driver = {
833 .probe = bq27000_battery_probe,
834 .remove = __devexit_p(bq27000_battery_remove),
836 .name = "bq27000-battery",
837 .owner = THIS_MODULE,
841 static inline int bq27x00_battery_platform_init(void)
843 int ret = platform_driver_register(&bq27000_battery_driver);
845 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
850 static inline void bq27x00_battery_platform_exit(void)
852 platform_driver_unregister(&bq27000_battery_driver);
857 static inline int bq27x00_battery_platform_init(void) { return 0; }
858 static inline void bq27x00_battery_platform_exit(void) {};
866 static int __init bq27x00_battery_init(void)
870 ret = bq27x00_battery_i2c_init();
874 ret = bq27x00_battery_platform_init();
876 bq27x00_battery_i2c_exit();
880 module_init(bq27x00_battery_init);
882 static void __exit bq27x00_battery_exit(void)
884 bq27x00_battery_platform_exit();
885 bq27x00_battery_i2c_exit();
887 module_exit(bq27x00_battery_exit);
889 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
890 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
891 MODULE_LICENSE("GPL");