2 * BQ27x00 battery driver
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
7 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
9 * This package is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 #include <linux/module.h>
19 #include <linux/param.h>
20 #include <linux/jiffies.h>
21 #include <linux/workqueue.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/power_supply.h>
25 #include <linux/idr.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <asm/unaligned.h>
30 #define DRIVER_VERSION "1.1.0"
32 #define BQ27x00_REG_TEMP 0x06
33 #define BQ27x00_REG_VOLT 0x08
34 #define BQ27x00_REG_AI 0x14
35 #define BQ27x00_REG_FLAGS 0x0A
36 #define BQ27x00_REG_TTE 0x16
37 #define BQ27x00_REG_TTF 0x18
38 #define BQ27x00_REG_TTECP 0x26
40 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
41 #define BQ27000_FLAG_CHGS BIT(7)
43 #define BQ27500_REG_SOC 0x2c
44 #define BQ27500_FLAG_DSC BIT(0)
45 #define BQ27500_FLAG_FC BIT(9)
47 #define BQ27000_RS 20 /* Resistor sense */
49 /* If the system has several batteries we need a different name for each
52 static DEFINE_IDR(battery_id);
53 static DEFINE_MUTEX(battery_mutex);
55 struct bq27x00_device_info;
56 struct bq27x00_access_methods {
57 int (*read)(u8 reg, int *rt_value, int b_single,
58 struct bq27x00_device_info *di);
61 enum bq27x00_chip { BQ27000, BQ27500 };
63 struct bq27x00_device_info {
66 struct bq27x00_access_methods *bus;
67 struct power_supply bat;
68 enum bq27x00_chip chip;
70 struct i2c_client *client;
73 static enum power_supply_property bq27x00_battery_props[] = {
74 POWER_SUPPLY_PROP_STATUS,
75 POWER_SUPPLY_PROP_PRESENT,
76 POWER_SUPPLY_PROP_VOLTAGE_NOW,
77 POWER_SUPPLY_PROP_CURRENT_NOW,
78 POWER_SUPPLY_PROP_CAPACITY,
79 POWER_SUPPLY_PROP_TEMP,
80 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
81 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
82 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
83 POWER_SUPPLY_PROP_TECHNOLOGY,
87 * Common code for BQ27x00 devices
90 static int bq27x00_read(u8 reg, int *rt_value, int b_single,
91 struct bq27x00_device_info *di)
93 return di->bus->read(reg, rt_value, b_single, di);
97 * Return the battery temperature in tenths of degree Celsius
98 * Or < 0 if something fails.
100 static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
105 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
107 dev_err(di->dev, "error reading temperature\n");
111 if (di->chip == BQ27500)
114 return ((temp * 5) - 5463) / 2;
118 * Return the battery Voltage in milivolts
119 * Or < 0 if something fails.
121 static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
126 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
128 dev_err(di->dev, "error reading voltage\n");
136 * Return the battery average current
137 * Note that current can be negative signed as well
138 * Or 0 if something fails.
140 static int bq27x00_battery_current(struct bq27x00_device_info *di)
146 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
148 dev_err(di->dev, "error reading current\n");
152 if (di->chip == BQ27500) {
153 /* bq27500 returns signed value */
154 curr = (int)((s16)curr) * 1000;
156 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
158 dev_err(di->dev, "error reading flags\n");
161 if (flags & BQ27000_FLAG_CHGS) {
162 dev_dbg(di->dev, "negative current!\n");
165 curr = curr * 3570 / BQ27000_RS;
172 * Return the battery Relative State-of-Charge
173 * Or < 0 if something fails.
175 static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
180 if (di->chip == BQ27500)
181 ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
183 ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
185 dev_err(di->dev, "error reading relative State-of-Charge\n");
192 static int bq27x00_battery_status(struct bq27x00_device_info *di,
193 union power_supply_propval *val)
199 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
201 dev_err(di->dev, "error reading flags\n");
205 if (di->chip == BQ27500) {
206 if (flags & BQ27500_FLAG_FC)
207 status = POWER_SUPPLY_STATUS_FULL;
208 else if (flags & BQ27500_FLAG_DSC)
209 status = POWER_SUPPLY_STATUS_DISCHARGING;
211 status = POWER_SUPPLY_STATUS_CHARGING;
213 if (flags & BQ27000_FLAG_CHGS)
214 status = POWER_SUPPLY_STATUS_CHARGING;
216 status = POWER_SUPPLY_STATUS_DISCHARGING;
219 val->intval = status;
224 * Read a time register.
225 * Return < 0 if something fails.
227 static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
228 union power_supply_propval *val)
233 ret = bq27x00_read(reg, &tval, 0, di);
235 dev_err(di->dev, "error reading register %02x\n", reg);
242 val->intval = tval * 60;
246 #define to_bq27x00_device_info(x) container_of((x), \
247 struct bq27x00_device_info, bat);
249 static int bq27x00_battery_get_property(struct power_supply *psy,
250 enum power_supply_property psp,
251 union power_supply_propval *val)
254 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
257 case POWER_SUPPLY_PROP_STATUS:
258 ret = bq27x00_battery_status(di, val);
260 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
261 case POWER_SUPPLY_PROP_PRESENT:
262 val->intval = bq27x00_battery_voltage(di);
263 if (psp == POWER_SUPPLY_PROP_PRESENT)
264 val->intval = val->intval <= 0 ? 0 : 1;
266 case POWER_SUPPLY_PROP_CURRENT_NOW:
267 val->intval = bq27x00_battery_current(di);
269 case POWER_SUPPLY_PROP_CAPACITY:
270 val->intval = bq27x00_battery_rsoc(di);
272 case POWER_SUPPLY_PROP_TEMP:
273 val->intval = bq27x00_battery_temperature(di);
275 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
276 ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
278 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
279 ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
281 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
282 ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
284 case POWER_SUPPLY_PROP_TECHNOLOGY:
285 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
294 static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
296 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
297 di->bat.properties = bq27x00_battery_props;
298 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
299 di->bat.get_property = bq27x00_battery_get_property;
300 di->bat.external_power_changed = NULL;
307 static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
308 struct bq27x00_device_info *di)
310 struct i2c_client *client = di->client;
311 struct i2c_msg msg[1];
312 unsigned char data[2];
315 if (!client->adapter)
318 msg->addr = client->addr;
324 err = i2c_transfer(client->adapter, msg, 1);
332 msg->flags = I2C_M_RD;
333 err = i2c_transfer(client->adapter, msg, 1);
336 *rt_value = get_unaligned_le16(data);
346 static int bq27x00_battery_probe(struct i2c_client *client,
347 const struct i2c_device_id *id)
350 struct bq27x00_device_info *di;
351 struct bq27x00_access_methods *bus;
355 /* Get new ID for the new battery device */
356 retval = idr_pre_get(&battery_id, GFP_KERNEL);
359 mutex_lock(&battery_mutex);
360 retval = idr_get_new(&battery_id, client, &num);
361 mutex_unlock(&battery_mutex);
365 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
367 dev_err(&client->dev, "failed to allocate device name\n");
372 di = kzalloc(sizeof(*di), GFP_KERNEL);
374 dev_err(&client->dev, "failed to allocate device info data\n");
379 di->chip = id->driver_data;
381 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
383 dev_err(&client->dev, "failed to allocate access method "
389 i2c_set_clientdata(client, di);
390 di->dev = &client->dev;
392 bus->read = &bq27x00_read_i2c;
396 bq27x00_powersupply_init(di);
398 retval = power_supply_register(&client->dev, &di->bat);
400 dev_err(&client->dev, "failed to register battery\n");
404 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
415 mutex_lock(&battery_mutex);
416 idr_remove(&battery_id, num);
417 mutex_unlock(&battery_mutex);
422 static int bq27x00_battery_remove(struct i2c_client *client)
424 struct bq27x00_device_info *di = i2c_get_clientdata(client);
426 power_supply_unregister(&di->bat);
431 mutex_lock(&battery_mutex);
432 idr_remove(&battery_id, di->id);
433 mutex_unlock(&battery_mutex);
444 static const struct i2c_device_id bq27x00_id[] = {
445 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
446 { "bq27500", BQ27500 },
450 static struct i2c_driver bq27x00_battery_driver = {
452 .name = "bq27x00-battery",
454 .probe = bq27x00_battery_probe,
455 .remove = bq27x00_battery_remove,
456 .id_table = bq27x00_id,
459 static int __init bq27x00_battery_init(void)
463 ret = i2c_add_driver(&bq27x00_battery_driver);
465 printk(KERN_ERR "Unable to register BQ27x00 driver\n");
469 module_init(bq27x00_battery_init);
471 static void __exit bq27x00_battery_exit(void)
473 i2c_del_driver(&bq27x00_battery_driver);
475 module_exit(bq27x00_battery_exit);
477 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
478 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
479 MODULE_LICENSE("GPL");