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 /* If the system has several batteries we need a different name for each
50 static DEFINE_IDR(battery_id);
51 static DEFINE_MUTEX(battery_mutex);
53 struct bq27x00_device_info;
54 struct bq27x00_access_methods {
55 int (*read)(u8 reg, int *rt_value, int b_single,
56 struct bq27x00_device_info *di);
59 enum bq27x00_chip { BQ27000, BQ27500 };
61 struct bq27x00_device_info {
64 struct bq27x00_access_methods *bus;
65 struct power_supply bat;
66 enum bq27x00_chip chip;
68 struct i2c_client *client;
71 static enum power_supply_property bq27x00_battery_props[] = {
72 POWER_SUPPLY_PROP_STATUS,
73 POWER_SUPPLY_PROP_PRESENT,
74 POWER_SUPPLY_PROP_VOLTAGE_NOW,
75 POWER_SUPPLY_PROP_CURRENT_NOW,
76 POWER_SUPPLY_PROP_CAPACITY,
77 POWER_SUPPLY_PROP_TEMP,
78 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
79 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
80 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
81 POWER_SUPPLY_PROP_TECHNOLOGY,
85 * Common code for BQ27x00 devices
88 static int bq27x00_read(u8 reg, int *rt_value, int b_single,
89 struct bq27x00_device_info *di)
91 return di->bus->read(reg, rt_value, b_single, di);
95 * Return the battery temperature in tenths of degree Celsius
96 * Or < 0 if something fails.
98 static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
103 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
105 dev_err(di->dev, "error reading temperature\n");
109 if (di->chip == BQ27500)
112 return ((temp * 5) - 5463) / 2;
116 * Return the battery Voltage in milivolts
117 * Or < 0 if something fails.
119 static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
124 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
126 dev_err(di->dev, "error reading voltage\n");
134 * Return the battery average current
135 * Note that current can be negative signed as well
136 * Or 0 if something fails.
138 static int bq27x00_battery_current(struct bq27x00_device_info *di)
144 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
146 dev_err(di->dev, "error reading current\n");
150 if (di->chip == BQ27500) {
151 /* bq27500 returns signed value */
152 curr = (int)(s16)curr;
154 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
156 dev_err(di->dev, "error reading flags\n");
159 if (flags & BQ27000_FLAG_CHGS) {
160 dev_dbg(di->dev, "negative current!\n");
169 * Return the battery Relative State-of-Charge
170 * Or < 0 if something fails.
172 static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
177 if (di->chip == BQ27500)
178 ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
180 ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
182 dev_err(di->dev, "error reading relative State-of-Charge\n");
189 static int bq27x00_battery_status(struct bq27x00_device_info *di,
190 union power_supply_propval *val)
196 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
198 dev_err(di->dev, "error reading flags\n");
202 if (di->chip == BQ27500) {
203 if (flags & BQ27500_FLAG_FC)
204 status = POWER_SUPPLY_STATUS_FULL;
205 else if (flags & BQ27500_FLAG_DSC)
206 status = POWER_SUPPLY_STATUS_DISCHARGING;
208 status = POWER_SUPPLY_STATUS_CHARGING;
210 if (flags & BQ27000_FLAG_CHGS)
211 status = POWER_SUPPLY_STATUS_CHARGING;
213 status = POWER_SUPPLY_STATUS_DISCHARGING;
216 val->intval = status;
221 * Read a time register.
222 * Return < 0 if something fails.
224 static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
225 union power_supply_propval *val)
230 ret = bq27x00_read(reg, &tval, 0, di);
232 dev_err(di->dev, "error reading register %02x\n", reg);
239 val->intval = tval * 60;
243 #define to_bq27x00_device_info(x) container_of((x), \
244 struct bq27x00_device_info, bat);
246 static int bq27x00_battery_get_property(struct power_supply *psy,
247 enum power_supply_property psp,
248 union power_supply_propval *val)
251 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
254 case POWER_SUPPLY_PROP_STATUS:
255 ret = bq27x00_battery_status(di, val);
257 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
258 case POWER_SUPPLY_PROP_PRESENT:
259 val->intval = bq27x00_battery_voltage(di);
260 if (psp == POWER_SUPPLY_PROP_PRESENT)
261 val->intval = val->intval <= 0 ? 0 : 1;
263 case POWER_SUPPLY_PROP_CURRENT_NOW:
264 val->intval = bq27x00_battery_current(di);
266 case POWER_SUPPLY_PROP_CAPACITY:
267 val->intval = bq27x00_battery_rsoc(di);
269 case POWER_SUPPLY_PROP_TEMP:
270 val->intval = bq27x00_battery_temperature(di);
272 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
273 ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
275 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
276 ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
278 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
279 ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
281 case POWER_SUPPLY_PROP_TECHNOLOGY:
282 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
291 static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
293 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
294 di->bat.properties = bq27x00_battery_props;
295 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
296 di->bat.get_property = bq27x00_battery_get_property;
297 di->bat.external_power_changed = NULL;
304 static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
305 struct bq27x00_device_info *di)
307 struct i2c_client *client = di->client;
308 struct i2c_msg msg[1];
309 unsigned char data[2];
312 if (!client->adapter)
315 msg->addr = client->addr;
321 err = i2c_transfer(client->adapter, msg, 1);
329 msg->flags = I2C_M_RD;
330 err = i2c_transfer(client->adapter, msg, 1);
333 *rt_value = get_unaligned_le16(data);
343 static int bq27x00_battery_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
347 struct bq27x00_device_info *di;
348 struct bq27x00_access_methods *bus;
352 /* Get new ID for the new battery device */
353 retval = idr_pre_get(&battery_id, GFP_KERNEL);
356 mutex_lock(&battery_mutex);
357 retval = idr_get_new(&battery_id, client, &num);
358 mutex_unlock(&battery_mutex);
362 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
364 dev_err(&client->dev, "failed to allocate device name\n");
369 di = kzalloc(sizeof(*di), GFP_KERNEL);
371 dev_err(&client->dev, "failed to allocate device info data\n");
376 di->chip = id->driver_data;
378 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
380 dev_err(&client->dev, "failed to allocate access method "
386 i2c_set_clientdata(client, di);
387 di->dev = &client->dev;
389 bus->read = &bq27x00_read_i2c;
393 bq27x00_powersupply_init(di);
395 retval = power_supply_register(&client->dev, &di->bat);
397 dev_err(&client->dev, "failed to register battery\n");
401 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
412 mutex_lock(&battery_mutex);
413 idr_remove(&battery_id, num);
414 mutex_unlock(&battery_mutex);
419 static int bq27x00_battery_remove(struct i2c_client *client)
421 struct bq27x00_device_info *di = i2c_get_clientdata(client);
423 power_supply_unregister(&di->bat);
428 mutex_lock(&battery_mutex);
429 idr_remove(&battery_id, di->id);
430 mutex_unlock(&battery_mutex);
441 static const struct i2c_device_id bq27x00_id[] = {
442 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
443 { "bq27500", BQ27500 },
447 static struct i2c_driver bq27x00_battery_driver = {
449 .name = "bq27x00-battery",
451 .probe = bq27x00_battery_probe,
452 .remove = bq27x00_battery_remove,
453 .id_table = bq27x00_id,
456 static int __init bq27x00_battery_init(void)
460 ret = i2c_add_driver(&bq27x00_battery_driver);
462 printk(KERN_ERR "Unable to register BQ27x00 driver\n");
466 module_init(bq27x00_battery_init);
468 static void __exit bq27x00_battery_exit(void)
470 i2c_del_driver(&bq27x00_battery_driver);
472 module_exit(bq27x00_battery_exit);
474 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
475 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
476 MODULE_LICENSE("GPL");