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_CI BIT(4) /* Capacity Inaccurate flag */
60 #define BQ27000_FLAG_FC BIT(5)
61 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
63 #define BQ27500_REG_SOC 0x2C
64 #define BQ27500_REG_DCAP 0x3C /* Design capacity */
65 #define BQ27500_FLAG_DSC BIT(0)
66 #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
67 #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
68 #define BQ27500_FLAG_FC BIT(9)
70 #define BQ27000_RS 20 /* Resistor sense */
72 struct bq27x00_device_info;
73 struct bq27x00_access_methods {
74 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
77 enum bq27x00_chip { BQ27000, BQ27500 };
79 struct bq27x00_reg_cache {
82 int time_to_empty_avg;
91 struct bq27x00_device_info {
94 enum bq27x00_chip chip;
96 struct bq27x00_reg_cache cache;
97 int charge_design_full;
99 unsigned long last_update;
100 struct delayed_work work;
102 struct power_supply bat;
104 struct bq27x00_access_methods bus;
109 static enum power_supply_property bq27x00_battery_props[] = {
110 POWER_SUPPLY_PROP_STATUS,
111 POWER_SUPPLY_PROP_PRESENT,
112 POWER_SUPPLY_PROP_VOLTAGE_NOW,
113 POWER_SUPPLY_PROP_CURRENT_NOW,
114 POWER_SUPPLY_PROP_CAPACITY,
115 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
116 POWER_SUPPLY_PROP_TEMP,
117 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
118 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
119 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
120 POWER_SUPPLY_PROP_TECHNOLOGY,
121 POWER_SUPPLY_PROP_CHARGE_FULL,
122 POWER_SUPPLY_PROP_CHARGE_NOW,
123 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
124 POWER_SUPPLY_PROP_CYCLE_COUNT,
125 POWER_SUPPLY_PROP_ENERGY_NOW,
128 static unsigned int poll_interval = 360;
129 module_param(poll_interval, uint, 0644);
130 MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
131 "0 disables polling");
134 * Common code for BQ27x00 devices
137 static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
140 return di->bus.read(di, reg, single);
144 * Return the battery Relative State-of-Charge
145 * Or < 0 if something fails.
147 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
151 if (di->chip == BQ27500)
152 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
154 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
157 dev_err(di->dev, "error reading relative State-of-Charge\n");
163 * Return a battery charge value in µAh
164 * Or < 0 if something fails.
166 static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
170 charge = bq27x00_read(di, reg, false);
172 dev_err(di->dev, "error reading nominal available capacity\n");
176 if (di->chip == BQ27500)
179 charge = charge * 3570 / BQ27000_RS;
185 * Return the battery Nominal available capaciy in µAh
186 * Or < 0 if something fails.
188 static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
190 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
194 * Return the battery Last measured discharge in µAh
195 * Or < 0 if something fails.
197 static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
199 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
203 * Return the battery Initial last measured discharge in µAh
204 * Or < 0 if something fails.
206 static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
210 if (di->chip == BQ27500)
211 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
213 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
216 dev_err(di->dev, "error reading initial last measured discharge\n");
220 if (di->chip == BQ27500)
223 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
229 * Return the battery Available energy in µWh
230 * Or < 0 if something fails.
232 static int bq27x00_battery_read_energy(struct bq27x00_device_info *di)
236 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
238 dev_err(di->dev, "error reading available energy\n");
242 if (di->chip == BQ27500)
245 ae = ae * 29200 / BQ27000_RS;
251 * Return the battery Cycle count total
252 * Or < 0 if something fails.
254 static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
258 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
260 dev_err(di->dev, "error reading cycle count total\n");
266 * Read a time register.
267 * Return < 0 if something fails.
269 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
273 tval = bq27x00_read(di, reg, false);
275 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
285 static void bq27x00_update(struct bq27x00_device_info *di)
287 struct bq27x00_reg_cache cache = {0, };
288 bool is_bq27500 = di->chip == BQ27500;
290 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
291 if (cache.flags >= 0) {
292 if (!is_bq27500 && (cache.flags & BQ27000_FLAG_CI)) {
293 cache.capacity = -ENODATA;
294 cache.energy = -ENODATA;
295 cache.time_to_empty = -ENODATA;
296 cache.time_to_empty_avg = -ENODATA;
297 cache.time_to_full = -ENODATA;
298 cache.charge_full = -ENODATA;
300 cache.capacity = bq27x00_battery_read_rsoc(di);
301 cache.energy = bq27x00_battery_read_energy(di);
302 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
303 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
304 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
305 cache.charge_full = bq27x00_battery_read_lmd(di);
307 cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
308 cache.cycle_count = bq27x00_battery_read_cyct(di);
310 /* We only have to read charge design full once */
311 if (di->charge_design_full <= 0)
312 di->charge_design_full = bq27x00_battery_read_ilmd(di);
315 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) {
317 power_supply_changed(&di->bat);
320 di->last_update = jiffies;
323 static void bq27x00_battery_poll(struct work_struct *work)
325 struct bq27x00_device_info *di =
326 container_of(work, struct bq27x00_device_info, work.work);
330 if (poll_interval > 0) {
331 /* The timer does not have to be accurate. */
332 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
333 schedule_delayed_work(&di->work, poll_interval * HZ);
339 * Return the battery temperature in tenths of degree Celsius
340 * Or < 0 if something fails.
342 static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
343 union power_supply_propval *val)
345 if (di->cache.temperature < 0)
346 return di->cache.temperature;
348 if (di->chip == BQ27500)
349 val->intval = di->cache.temperature - 2731;
351 val->intval = ((di->cache.temperature * 5) - 5463) / 2;
357 * Return the battery average current in µA
358 * Note that current can be negative signed as well
359 * Or 0 if something fails.
361 static int bq27x00_battery_current(struct bq27x00_device_info *di,
362 union power_supply_propval *val)
367 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
371 if (di->chip == BQ27500) {
372 /* bq27500 returns signed value */
373 val->intval = (int)((s16)curr) * 1000;
375 flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false);
376 if (flags & BQ27000_FLAG_CHGS) {
377 dev_dbg(di->dev, "negative current!\n");
381 val->intval = curr * 3570 / BQ27000_RS;
387 static int bq27x00_battery_status(struct bq27x00_device_info *di,
388 union power_supply_propval *val)
392 if (di->chip == BQ27500) {
393 if (di->cache.flags & BQ27500_FLAG_FC)
394 status = POWER_SUPPLY_STATUS_FULL;
395 else if (di->cache.flags & BQ27500_FLAG_DSC)
396 status = POWER_SUPPLY_STATUS_DISCHARGING;
398 status = POWER_SUPPLY_STATUS_CHARGING;
400 if (di->cache.flags & BQ27000_FLAG_FC)
401 status = POWER_SUPPLY_STATUS_FULL;
402 else if (di->cache.flags & BQ27000_FLAG_CHGS)
403 status = POWER_SUPPLY_STATUS_CHARGING;
404 else if (power_supply_am_i_supplied(&di->bat))
405 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
407 status = POWER_SUPPLY_STATUS_DISCHARGING;
410 val->intval = status;
415 static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di,
416 union power_supply_propval *val)
420 if (di->chip == BQ27500) {
421 if (di->cache.flags & BQ27500_FLAG_FC)
422 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
423 else if (di->cache.flags & BQ27500_FLAG_SOC1)
424 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
425 else if (di->cache.flags & BQ27500_FLAG_SOCF)
426 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
428 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
430 if (di->cache.flags & BQ27000_FLAG_FC)
431 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
432 else if (di->cache.flags & BQ27000_FLAG_EDV1)
433 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
434 else if (di->cache.flags & BQ27000_FLAG_EDVF)
435 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
437 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
446 * Return the battery Voltage in milivolts
447 * Or < 0 if something fails.
449 static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
450 union power_supply_propval *val)
454 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
458 val->intval = volt * 1000;
463 static int bq27x00_simple_value(int value,
464 union power_supply_propval *val)
474 #define to_bq27x00_device_info(x) container_of((x), \
475 struct bq27x00_device_info, bat);
477 static int bq27x00_battery_get_property(struct power_supply *psy,
478 enum power_supply_property psp,
479 union power_supply_propval *val)
482 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
484 mutex_lock(&di->lock);
485 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
486 cancel_delayed_work_sync(&di->work);
487 bq27x00_battery_poll(&di->work.work);
489 mutex_unlock(&di->lock);
491 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
495 case POWER_SUPPLY_PROP_STATUS:
496 ret = bq27x00_battery_status(di, val);
498 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
499 ret = bq27x00_battery_voltage(di, val);
501 case POWER_SUPPLY_PROP_PRESENT:
502 val->intval = di->cache.flags < 0 ? 0 : 1;
504 case POWER_SUPPLY_PROP_CURRENT_NOW:
505 ret = bq27x00_battery_current(di, val);
507 case POWER_SUPPLY_PROP_CAPACITY:
508 ret = bq27x00_simple_value(di->cache.capacity, val);
510 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
511 ret = bq27x00_battery_capacity_level(di, val);
513 case POWER_SUPPLY_PROP_TEMP:
514 ret = bq27x00_battery_temperature(di, val);
516 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
517 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
519 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
520 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
522 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
523 ret = bq27x00_simple_value(di->cache.time_to_full, val);
525 case POWER_SUPPLY_PROP_TECHNOLOGY:
526 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
528 case POWER_SUPPLY_PROP_CHARGE_NOW:
529 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
531 case POWER_SUPPLY_PROP_CHARGE_FULL:
532 ret = bq27x00_simple_value(di->cache.charge_full, val);
534 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
535 ret = bq27x00_simple_value(di->charge_design_full, val);
537 case POWER_SUPPLY_PROP_CYCLE_COUNT:
538 ret = bq27x00_simple_value(di->cache.cycle_count, val);
540 case POWER_SUPPLY_PROP_ENERGY_NOW:
541 ret = bq27x00_simple_value(di->cache.energy, val);
550 static void bq27x00_external_power_changed(struct power_supply *psy)
552 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
554 cancel_delayed_work_sync(&di->work);
555 schedule_delayed_work(&di->work, 0);
558 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
562 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
563 di->bat.properties = bq27x00_battery_props;
564 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
565 di->bat.get_property = bq27x00_battery_get_property;
566 di->bat.external_power_changed = bq27x00_external_power_changed;
568 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
569 mutex_init(&di->lock);
571 ret = power_supply_register(di->dev, &di->bat);
573 dev_err(di->dev, "failed to register battery: %d\n", ret);
577 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
584 static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
586 cancel_delayed_work_sync(&di->work);
588 power_supply_unregister(&di->bat);
590 mutex_destroy(&di->lock);
594 /* i2c specific code */
595 #ifdef CONFIG_BATTERY_BQ27X00_I2C
597 /* If the system has several batteries we need a different name for each
600 static DEFINE_IDR(battery_id);
601 static DEFINE_MUTEX(battery_mutex);
603 static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
605 struct i2c_client *client = to_i2c_client(di->dev);
606 struct i2c_msg msg[2];
607 unsigned char data[2];
610 if (!client->adapter)
613 msg[0].addr = client->addr;
616 msg[0].len = sizeof(reg);
617 msg[1].addr = client->addr;
618 msg[1].flags = I2C_M_RD;
625 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
630 ret = get_unaligned_le16(data);
637 static int bq27x00_battery_probe(struct i2c_client *client,
638 const struct i2c_device_id *id)
641 struct bq27x00_device_info *di;
645 /* Get new ID for the new battery device */
646 retval = idr_pre_get(&battery_id, GFP_KERNEL);
649 mutex_lock(&battery_mutex);
650 retval = idr_get_new(&battery_id, client, &num);
651 mutex_unlock(&battery_mutex);
655 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
657 dev_err(&client->dev, "failed to allocate device name\n");
662 di = kzalloc(sizeof(*di), GFP_KERNEL);
664 dev_err(&client->dev, "failed to allocate device info data\n");
670 di->dev = &client->dev;
671 di->chip = id->driver_data;
673 di->bus.read = &bq27x00_read_i2c;
675 if (bq27x00_powersupply_init(di))
678 i2c_set_clientdata(client, di);
687 mutex_lock(&battery_mutex);
688 idr_remove(&battery_id, num);
689 mutex_unlock(&battery_mutex);
694 static int bq27x00_battery_remove(struct i2c_client *client)
696 struct bq27x00_device_info *di = i2c_get_clientdata(client);
698 bq27x00_powersupply_unregister(di);
702 mutex_lock(&battery_mutex);
703 idr_remove(&battery_id, di->id);
704 mutex_unlock(&battery_mutex);
711 static const struct i2c_device_id bq27x00_id[] = {
712 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
713 { "bq27500", BQ27500 },
716 MODULE_DEVICE_TABLE(i2c, bq27x00_id);
718 static struct i2c_driver bq27x00_battery_driver = {
720 .name = "bq27x00-battery",
722 .probe = bq27x00_battery_probe,
723 .remove = bq27x00_battery_remove,
724 .id_table = bq27x00_id,
727 static inline int bq27x00_battery_i2c_init(void)
729 int ret = i2c_add_driver(&bq27x00_battery_driver);
731 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
736 static inline void bq27x00_battery_i2c_exit(void)
738 i2c_del_driver(&bq27x00_battery_driver);
743 static inline int bq27x00_battery_i2c_init(void) { return 0; }
744 static inline void bq27x00_battery_i2c_exit(void) {};
748 /* platform specific code */
749 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
751 static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
754 struct device *dev = di->dev;
755 struct bq27000_platform_data *pdata = dev->platform_data;
756 unsigned int timeout = 3;
761 /* Make sure the value has not changed in between reading the
762 * lower and the upper part */
763 upper = pdata->read(dev, reg + 1);
769 lower = pdata->read(dev, reg);
773 upper = pdata->read(dev, reg + 1);
774 } while (temp != upper && --timeout);
779 return (upper << 8) | lower;
782 return pdata->read(dev, reg);
785 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
787 struct bq27x00_device_info *di;
788 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
792 dev_err(&pdev->dev, "no platform_data supplied\n");
797 dev_err(&pdev->dev, "no hdq read callback supplied\n");
801 di = kzalloc(sizeof(*di), GFP_KERNEL);
803 dev_err(&pdev->dev, "failed to allocate device info data\n");
807 platform_set_drvdata(pdev, di);
809 di->dev = &pdev->dev;
812 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
813 di->bus.read = &bq27000_read_platform;
815 ret = bq27x00_powersupply_init(di);
822 platform_set_drvdata(pdev, NULL);
828 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
830 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
832 bq27x00_powersupply_unregister(di);
834 platform_set_drvdata(pdev, NULL);
840 static struct platform_driver bq27000_battery_driver = {
841 .probe = bq27000_battery_probe,
842 .remove = __devexit_p(bq27000_battery_remove),
844 .name = "bq27000-battery",
845 .owner = THIS_MODULE,
849 static inline int bq27x00_battery_platform_init(void)
851 int ret = platform_driver_register(&bq27000_battery_driver);
853 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
858 static inline void bq27x00_battery_platform_exit(void)
860 platform_driver_unregister(&bq27000_battery_driver);
865 static inline int bq27x00_battery_platform_init(void) { return 0; }
866 static inline void bq27x00_battery_platform_exit(void) {};
874 static int __init bq27x00_battery_init(void)
878 ret = bq27x00_battery_i2c_init();
882 ret = bq27x00_battery_platform_init();
884 bq27x00_battery_i2c_exit();
888 module_init(bq27x00_battery_init);
890 static void __exit bq27x00_battery_exit(void)
892 bq27x00_battery_platform_exit();
893 bq27x00_battery_i2c_exit();
895 module_exit(bq27x00_battery_exit);
897 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
898 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
899 MODULE_LICENSE("GPL");