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>
27 #include <linux/i2c.h>
29 #define DRIVER_VERSION "1.0.0"
31 #define BQ27x00_REG_TEMP 0x06
32 #define BQ27x00_REG_VOLT 0x08
33 #define BQ27x00_REG_RSOC 0x0B /* Relative State-of-Charge */
34 #define BQ27x00_REG_AI 0x14
35 #define BQ27x00_REG_FLAGS 0x0A
36 #define HIGH_BYTE(A) ((A) << 8)
38 /* If the system has several batteries we need a different name for each
41 static DEFINE_IDR(battery_id);
42 static DEFINE_MUTEX(battery_mutex);
44 struct bq27x00_device_info;
45 struct bq27x00_access_methods {
46 int (*read)(u8 reg, int *rt_value, int b_single,
47 struct bq27x00_device_info *di);
50 struct bq27x00_device_info {
57 struct bq27x00_access_methods *bus;
58 struct power_supply bat;
60 struct i2c_client *client;
63 static enum power_supply_property bq27x00_battery_props[] = {
64 POWER_SUPPLY_PROP_PRESENT,
65 POWER_SUPPLY_PROP_VOLTAGE_NOW,
66 POWER_SUPPLY_PROP_CURRENT_NOW,
67 POWER_SUPPLY_PROP_CAPACITY,
68 POWER_SUPPLY_PROP_TEMP,
72 * Common code for BQ27x00 devices
75 static int bq27x00_read(u8 reg, int *rt_value, int b_single,
76 struct bq27x00_device_info *di)
80 ret = di->bus->read(reg, rt_value, b_single, di);
81 *rt_value = be16_to_cpu(*rt_value);
87 * Return the battery temperature in Celcius degrees
88 * Or < 0 if something fails.
90 static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
95 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
97 dev_err(di->dev, "error reading temperature\n");
101 return (temp >> 2) - 273;
105 * Return the battery Voltage in milivolts
106 * Or < 0 if something fails.
108 static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
113 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
115 dev_err(di->dev, "error reading voltage\n");
123 * Return the battery average current
124 * Note that current can be negative signed as well
125 * Or 0 if something fails.
127 static int bq27x00_battery_current(struct bq27x00_device_info *di)
133 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
135 dev_err(di->dev, "error reading current\n");
138 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
140 dev_err(di->dev, "error reading flags\n");
143 if ((flags & (1 << 7)) != 0) {
144 dev_dbg(di->dev, "negative current!\n");
151 * Return the battery Relative State-of-Charge
152 * Or < 0 if something fails.
154 static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
159 ret = bq27x00_read(BQ27x00_REG_RSOC, &rsoc, 1, di);
161 dev_err(di->dev, "error reading relative State-of-Charge\n");
168 #define to_bq27x00_device_info(x) container_of((x), \
169 struct bq27x00_device_info, bat);
171 static int bq27x00_battery_get_property(struct power_supply *psy,
172 enum power_supply_property psp,
173 union power_supply_propval *val)
175 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
178 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
179 case POWER_SUPPLY_PROP_PRESENT:
180 val->intval = bq27x00_battery_voltage(di);
181 if (psp == POWER_SUPPLY_PROP_PRESENT)
182 val->intval = val->intval <= 0 ? 0 : 1;
184 case POWER_SUPPLY_PROP_CURRENT_NOW:
185 val->intval = bq27x00_battery_current(di);
187 case POWER_SUPPLY_PROP_CAPACITY:
188 val->intval = bq27x00_battery_rsoc(di);
190 case POWER_SUPPLY_PROP_TEMP:
191 val->intval = bq27x00_battery_temperature(di);
200 static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
202 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
203 di->bat.properties = bq27x00_battery_props;
204 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
205 di->bat.get_property = bq27x00_battery_get_property;
206 di->bat.external_power_changed = NULL;
210 * BQ27200 specific code
213 static int bq27200_read(u8 reg, int *rt_value, int b_single,
214 struct bq27x00_device_info *di)
216 struct i2c_client *client = di->client;
217 struct i2c_msg msg[1];
218 unsigned char data[2];
221 if (!client->adapter)
224 msg->addr = client->addr;
230 err = i2c_transfer(client->adapter, msg, 1);
238 msg->flags = I2C_M_RD;
239 err = i2c_transfer(client->adapter, msg, 1);
242 *rt_value = data[1] | HIGH_BYTE(data[0]);
252 static int bq27200_battery_probe(struct i2c_client *client,
253 const struct i2c_device_id *id)
256 struct bq27x00_device_info *di;
257 struct bq27x00_access_methods *bus;
261 /* Get new ID for the new battery device */
262 retval = idr_pre_get(&battery_id, GFP_KERNEL);
265 mutex_lock(&battery_mutex);
266 retval = idr_get_new(&battery_id, client, &num);
267 mutex_unlock(&battery_mutex);
271 name = kasprintf(GFP_KERNEL, "bq27200-%d", num);
273 dev_err(&client->dev, "failed to allocate device name\n");
278 di = kzalloc(sizeof(*di), GFP_KERNEL);
280 dev_err(&client->dev, "failed to allocate device info data\n");
286 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
288 dev_err(&client->dev, "failed to allocate access method "
294 i2c_set_clientdata(client, di);
295 di->dev = &client->dev;
297 bus->read = &bq27200_read;
301 bq27x00_powersupply_init(di);
303 retval = power_supply_register(&client->dev, &di->bat);
305 dev_err(&client->dev, "failed to register battery\n");
309 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
320 mutex_lock(&battery_mutex);
321 idr_remove(&battery_id, num);
322 mutex_unlock(&battery_mutex);
327 static int bq27200_battery_remove(struct i2c_client *client)
329 struct bq27x00_device_info *di = i2c_get_clientdata(client);
331 power_supply_unregister(&di->bat);
335 mutex_lock(&battery_mutex);
336 idr_remove(&battery_id, di->id);
337 mutex_unlock(&battery_mutex);
348 static const struct i2c_device_id bq27200_id[] = {
353 static struct i2c_driver bq27200_battery_driver = {
355 .name = "bq27200-battery",
357 .probe = bq27200_battery_probe,
358 .remove = bq27200_battery_remove,
359 .id_table = bq27200_id,
362 static int __init bq27x00_battery_init(void)
366 ret = i2c_add_driver(&bq27200_battery_driver);
368 printk(KERN_ERR "Unable to register BQ27200 driver\n");
372 module_init(bq27x00_battery_init);
374 static void __exit bq27x00_battery_exit(void)
376 i2c_del_driver(&bq27200_battery_driver);
378 module_exit(bq27x00_battery_exit);
380 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
381 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
382 MODULE_LICENSE("GPL");