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 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_LMD 0x12
40 #define BQ27x00_REG_TTE 0x16
41 #define BQ27x00_REG_TTF 0x18
42 #define BQ27x00_REG_TTECP 0x26
44 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
45 #define BQ27000_FLAG_VDQ BIT(3)
46 #define BQ27000_FLAG_CHGS BIT(7)
48 #define BQ27500_REG_SOC 0x2c
49 #define BQ27500_FLAG_DSC BIT(0)
50 #define BQ27500_FLAG_FC BIT(9)
52 struct bq27x00_device_info;
53 struct bq27x00_access_methods {
54 int (*read)(u8 reg, bool single, struct bq27x00_device_info *di);
57 enum bq27x00_chip { BQ27000, BQ27500 };
59 struct bq27x00_values {
64 int time_to_empty_avg;
71 struct bq27x00_device_info {
74 enum bq27x00_chip chip;
76 struct bq27x00_access_methods bus;
78 struct power_supply bat;
80 struct bq27x00_values values;
82 unsigned long last_update;
83 struct delayed_work work;
86 static enum power_supply_property bq27x00_battery_props[] = {
87 POWER_SUPPLY_PROP_STATUS,
88 POWER_SUPPLY_PROP_PRESENT,
89 POWER_SUPPLY_PROP_VOLTAGE_NOW,
90 POWER_SUPPLY_PROP_CURRENT_NOW,
91 POWER_SUPPLY_PROP_CAPACITY,
92 POWER_SUPPLY_PROP_TEMP,
93 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
94 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
95 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
96 POWER_SUPPLY_PROP_TECHNOLOGY,
97 POWER_SUPPLY_PROP_CHARGE_FULL,
98 POWER_SUPPLY_PROP_HEALTH,
102 * Common code for BQ27x00 devices
105 static int bq27x00_read(u8 reg, bool single, struct bq27x00_device_info *di)
107 return di->bus.read(reg, single, di);
111 * Return the battery Voltage in milivolts
112 * Or < 0 if something fails.
114 static int bq27x00_battery_read_voltage(struct bq27x00_device_info *di)
118 volt = bq27x00_read(BQ27x00_REG_VOLT, 0, di);
126 * Return the battery Relative State-of-Charge
127 * Or < 0 if something fails.
129 static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
133 if (di->chip == BQ27500)
134 rsoc = bq27x00_read(BQ27500_REG_SOC, 0, di);
136 rsoc = bq27x00_read(BQ27000_REG_RSOC, 1, di);
138 dev_err(di->dev, "error reading relative State-of-Charge\n");
145 * Read a time register.
146 * Return < 0 if something fails.
148 static int bq27x00_battery_read_time(struct bq27x00_device_info *di, int reg)
152 tval = bq27x00_read(reg, 0, di);
154 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
164 static int bq27x00_battery_read_charge_full(struct bq27x00_device_info *di)
168 charge = bq27x00_read(BQ27x00_REG_LMD, 0, di);
170 dev_err(di->dev, "error reading register charge full: %d\n", charge);
174 if (di->chip == BQ27500)
177 charge = charge * 357 / 2;
182 static void bq27x00_update(struct bq27x00_device_info *di)
184 struct bq27x00_values values = {0, };
186 values.voltage = bq27x00_battery_read_voltage(di);
187 if (values.voltage >= 0) {
188 values.flags = bq27x00_read(BQ27x00_REG_FLAGS, di->chip == BQ27500, di);
189 values.current_now = bq27x00_read(BQ27x00_REG_AI, 0, di);
190 values.capacity = bq27x00_battery_read_rsoc(di);
191 values.temperature = bq27x00_read(BQ27x00_REG_TEMP, 0, di);
192 values.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
193 values.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
194 values.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
195 values.charge_full = bq27x00_battery_read_charge_full(di);
198 if (memcmp(&di->values, &values, sizeof(values)) != 0) {
200 power_supply_changed(&di->bat);
203 di->last_update = jiffies;
207 * Return the battery temperature in tenths of degree Celsius
208 * Or < 0 if something fails.
210 static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
211 union power_supply_propval *val)
213 if (di->values.temperature < 0)
214 return di->values.temperature;
216 if (di->chip == BQ27500)
217 val->intval = di->values.temperature - 2731;
219 val->intval = ((di->values.temperature * 5) - 5463) >> 1;
225 * Return the battery average current
226 * Note that current can be negative signed as well
227 * Or 0 if something fails.
229 static int bq27x00_battery_current(struct bq27x00_device_info *di,
230 union power_supply_propval *val)
234 if (di->values.current_now < 0)
235 return di->values.current_now;
237 if (di->chip == BQ27500) {
238 /* bq27500 returns signed value */
239 val->intval = (int)((s16)di->values.current_now) * 1000;
241 if (di->values.flags < 0)
242 return di->values.flags;
244 curr = di->values.current_now;
245 if (di->values.flags & BQ27000_FLAG_CHGS) {
246 dev_dbg(di->dev, "negative current!\n");
250 val->intval = curr * 3570 / 20;
256 static int bq27x00_battery_status(struct bq27x00_device_info *di,
257 union power_supply_propval *val)
261 if (di->values.flags < 0)
262 return di->values.flags;
264 if (di->chip == BQ27500) {
265 if (di->values.flags & BQ27500_FLAG_FC)
266 status = POWER_SUPPLY_STATUS_FULL;
267 else if (di->values.flags & BQ27500_FLAG_DSC)
268 status = POWER_SUPPLY_STATUS_DISCHARGING;
270 status = POWER_SUPPLY_STATUS_CHARGING;
272 if (di->values.flags & BQ27000_FLAG_CHGS)
273 status = POWER_SUPPLY_STATUS_CHARGING;
274 else if (di->values.time_to_full == 0)
275 status = POWER_SUPPLY_STATUS_FULL;
276 else if (power_supply_am_i_supplied(&di->bat))
277 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
279 status = POWER_SUPPLY_STATUS_DISCHARGING;
282 val->intval = status;
287 static int bq27x00_simple_value(int value,
288 union power_supply_propval *val)
298 static int bq27x00_battery_health(struct bq27x00_device_info *di,
299 union power_supply_propval *val)
301 if (di->values.flags < 0)
302 return di->values.flags;
304 if (di->values.flags & BQ27000_FLAG_VDQ)
305 val->intval = POWER_SUPPLY_HEALTH_GOOD;
307 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
312 #define to_bq27x00_device_info(x) container_of((x), \
313 struct bq27x00_device_info, bat);
315 static int bq27x00_battery_get_property(struct power_supply *psy,
316 enum power_supply_property psp,
317 union power_supply_propval *val)
320 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
322 if (time_is_before_jiffies(di->last_update + 6 * HZ)) {
323 cancel_delayed_work(&di->work);
325 schedule_delayed_work(&di->work, 60 * HZ);
328 if (psp != POWER_SUPPLY_PROP_PRESENT && di->values.voltage <= 0)
332 case POWER_SUPPLY_PROP_STATUS:
333 ret = bq27x00_battery_status(di, val);
335 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
336 ret = bq27x00_simple_value(di->values.voltage, val);
338 case POWER_SUPPLY_PROP_PRESENT:
339 val->intval = di->values.voltage <= 0 ? 0 : 1;
341 case POWER_SUPPLY_PROP_CURRENT_NOW:
342 ret = bq27x00_battery_current(di, val);
344 case POWER_SUPPLY_PROP_CAPACITY:
345 ret = bq27x00_simple_value(di->values.capacity, val);
347 case POWER_SUPPLY_PROP_TEMP:
348 ret = bq27x00_battery_temperature(di, val);
350 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
351 ret = bq27x00_simple_value(di->values.time_to_empty, val);
353 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
354 ret = bq27x00_simple_value(di->values.time_to_empty_avg, val);
356 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
357 ret = bq27x00_simple_value(di->values.time_to_full, val);
359 case POWER_SUPPLY_PROP_TECHNOLOGY:
360 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
362 case POWER_SUPPLY_PROP_CHARGE_FULL:
363 ret = bq27x00_simple_value(di->values.charge_full, val);
365 case POWER_SUPPLY_PROP_HEALTH:
366 ret = bq27x00_battery_health(di, val);
375 static void bq27x00_battery_work(struct work_struct *work)
377 struct bq27x00_device_info *di =
378 container_of(work, struct bq27x00_device_info, work.work);
381 schedule_delayed_work(&di->work, 60 * HZ);
384 static void bq27x00_external_power_changed(struct power_supply *psy)
386 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
388 schedule_delayed_work(&di->work, 0);
391 static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
395 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
396 di->bat.properties = bq27x00_battery_props;
397 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
398 di->bat.get_property = bq27x00_battery_get_property;
399 di->bat.external_power_changed = bq27x00_external_power_changed;
401 INIT_DELAYED_WORK(&di->work, bq27x00_battery_work);
403 ret = power_supply_register(di->dev, &di->bat);
405 dev_err(di->dev, "failed to register battery: %d\n", ret);
409 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
417 /* i2c specific code */
418 #ifdef CONFIG_BATTERY_BQ27X00_I2C
420 /* If the system has several batteries we need a different name for each
423 static DEFINE_IDR(battery_id);
424 static DEFINE_MUTEX(battery_mutex);
426 static int bq27x00_read_i2c(u8 reg, bool single, struct bq27x00_device_info *di)
428 struct i2c_client *client = to_i2c_client(di->dev);
429 struct i2c_msg msg[1];
430 unsigned char data[2];
433 if (!client->adapter)
436 msg->addr = client->addr;
442 ret = i2c_transfer(client->adapter, msg, 1);
450 msg->flags = I2C_M_RD;
453 ret = get_unaligned_le16(data);
461 static int bq27x00_battery_probe(struct i2c_client *client,
462 const struct i2c_device_id *id)
465 struct bq27x00_device_info *di;
469 /* Get new ID for the new battery device */
470 retval = idr_pre_get(&battery_id, GFP_KERNEL);
473 mutex_lock(&battery_mutex);
474 retval = idr_get_new(&battery_id, client, &num);
475 mutex_unlock(&battery_mutex);
479 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
481 dev_err(&client->dev, "failed to allocate device name\n");
486 di = kzalloc(sizeof(*di), GFP_KERNEL);
488 dev_err(&client->dev, "failed to allocate device info data\n");
494 di->chip = id->driver_data;
496 i2c_set_clientdata(client, di);
498 di->dev = &client->dev;
501 di->bus.read = &bq27x00_read_i2c;
503 if (bq27x00_powersupply_init(di))
513 mutex_lock(&battery_mutex);
514 idr_remove(&battery_id, num);
515 mutex_unlock(&battery_mutex);
520 static int bq27x00_battery_remove(struct i2c_client *client)
522 struct bq27x00_device_info *di = i2c_get_clientdata(client);
524 power_supply_unregister(&di->bat);
528 mutex_lock(&battery_mutex);
529 idr_remove(&battery_id, di->id);
530 mutex_unlock(&battery_mutex);
537 static const struct i2c_device_id bq27x00_id[] = {
538 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
539 { "bq27500", BQ27500 },
543 static struct i2c_driver bq27x00_battery_driver = {
545 .name = "bq27x00-battery",
547 .probe = bq27x00_battery_probe,
548 .remove = bq27x00_battery_remove,
549 .id_table = bq27x00_id,
552 static inline int bq27x00_battery_i2c_init(void)
554 int ret = i2c_add_driver(&bq27x00_battery_driver);
556 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
561 static inline void bq27x00_battery_i2c_exit(void)
563 i2c_del_driver(&bq27x00_battery_driver);
568 static inline int bq27x00_battery_i2c_init(void) { return 0; }
569 static inline void bq27x00_battery_i2c_exit(void) {};
573 /* platform specific code */
574 #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
576 static int bq27000_read_platform(u8 reg, bool single,
577 struct bq27x00_device_info *di)
579 struct device *dev = di->dev;
580 struct bq27000_platform_data *pdata = dev->platform_data;
581 unsigned int timeout = 3;
586 /* Make sure the value has not changed in between reading the lower and
588 upper = pdata->read(dev, reg + 1);
594 lower = pdata->read(dev, reg);
598 upper = pdata->read(dev, reg + 1);
599 } while(temp != upper && --timeout);
604 return (upper << 8) | lower;
607 return pdata->read(dev, reg);
610 static int __devinit bq27000_battery_probe(struct platform_device *pdev)
612 struct bq27x00_device_info *di;
613 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
617 dev_err(&pdev->dev, "no platform_data supplied\n");
622 dev_err(&pdev->dev, "no hdq read callback supplied\n");
626 di = kzalloc(sizeof(*di), GFP_KERNEL);
628 dev_err(&pdev->dev, "failed to allocate device info data\n");
632 platform_set_drvdata(pdev, di);
634 di->dev = &pdev->dev;
637 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
638 di->bus.read = &bq27000_read_platform;
640 ret = bq27x00_powersupply_init(di);
647 platform_set_drvdata(pdev, NULL);
653 static int __devexit bq27000_battery_remove(struct platform_device *pdev)
655 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
657 power_supply_unregister(&di->bat);
658 platform_set_drvdata(pdev, NULL);
664 static struct platform_driver bq27000_battery_driver = {
665 .probe = bq27000_battery_probe,
666 .remove = __devexit_p(bq27000_battery_remove),
668 .name = "bq27000-battery",
669 .owner = THIS_MODULE,
673 static inline int bq27x00_battery_platform_init(void)
675 int ret = platform_driver_register(&bq27000_battery_driver);
677 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
682 static inline void bq27x00_battery_platform_exit(void)
684 platform_driver_unregister(&bq27000_battery_driver);
689 static inline int bq27x00_battery_platform_init(void) { return 0; }
690 static inline void bq27x00_battery_platform_exit(void) {};
698 static int __init bq27x00_battery_init(void)
702 ret = bq27x00_battery_i2c_init();
706 ret = bq27x00_battery_platform_init();
708 bq27x00_battery_i2c_exit();
712 module_init(bq27x00_battery_init);
714 static void __exit bq27x00_battery_exit(void)
716 bq27x00_battery_platform_exit();
717 bq27x00_battery_i2c_exit();
719 module_exit(bq27x00_battery_exit);
721 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
722 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
723 MODULE_LICENSE("GPL");