blob: 537915cc491e9e0100ccbf7f3b9cd95f1aed08c7 [file] [log] [blame]
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001/*
2 * BQ27x00 battery driver
3 *
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6 *
7 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
8 *
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.
12 *
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.
16 *
17 */
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>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070026#include <linux/i2c.h>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070027#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070028
29#define DRIVER_VERSION "1.0.0"
30
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
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070036
37/* If the system has several batteries we need a different name for each
38 * of them...
39 */
40static DEFINE_IDR(battery_id);
41static DEFINE_MUTEX(battery_mutex);
42
43struct bq27x00_device_info;
44struct bq27x00_access_methods {
45 int (*read)(u8 reg, int *rt_value, int b_single,
46 struct bq27x00_device_info *di);
47};
48
49struct bq27x00_device_info {
50 struct device *dev;
51 int id;
52 int voltage_uV;
53 int current_uA;
54 int temp_C;
55 int charge_rsoc;
56 struct bq27x00_access_methods *bus;
57 struct power_supply bat;
58
59 struct i2c_client *client;
60};
61
62static enum power_supply_property bq27x00_battery_props[] = {
63 POWER_SUPPLY_PROP_PRESENT,
64 POWER_SUPPLY_PROP_VOLTAGE_NOW,
65 POWER_SUPPLY_PROP_CURRENT_NOW,
66 POWER_SUPPLY_PROP_CAPACITY,
67 POWER_SUPPLY_PROP_TEMP,
68};
69
70/*
71 * Common code for BQ27x00 devices
72 */
73
74static int bq27x00_read(u8 reg, int *rt_value, int b_single,
75 struct bq27x00_device_info *di)
76{
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +020077 return di->bus->read(reg, rt_value, b_single, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070078}
79
80/*
Adam Buchbinderb731d7b2009-03-13 12:15:26 -040081 * Return the battery temperature in Celsius degrees
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070082 * Or < 0 if something fails.
83 */
84static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
85{
86 int ret;
87 int temp = 0;
88
89 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
90 if (ret) {
91 dev_err(di->dev, "error reading temperature\n");
92 return ret;
93 }
94
95 return (temp >> 2) - 273;
96}
97
98/*
99 * Return the battery Voltage in milivolts
100 * Or < 0 if something fails.
101 */
102static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
103{
104 int ret;
105 int volt = 0;
106
107 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
108 if (ret) {
109 dev_err(di->dev, "error reading voltage\n");
110 return ret;
111 }
112
113 return volt;
114}
115
116/*
117 * Return the battery average current
118 * Note that current can be negative signed as well
119 * Or 0 if something fails.
120 */
121static int bq27x00_battery_current(struct bq27x00_device_info *di)
122{
123 int ret;
124 int curr = 0;
125 int flags = 0;
126
127 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
128 if (ret) {
129 dev_err(di->dev, "error reading current\n");
130 return 0;
131 }
132 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
133 if (ret < 0) {
134 dev_err(di->dev, "error reading flags\n");
135 return 0;
136 }
137 if ((flags & (1 << 7)) != 0) {
138 dev_dbg(di->dev, "negative current!\n");
139 return -curr;
140 }
141 return curr;
142}
143
144/*
145 * Return the battery Relative State-of-Charge
146 * Or < 0 if something fails.
147 */
148static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
149{
150 int ret;
151 int rsoc = 0;
152
153 ret = bq27x00_read(BQ27x00_REG_RSOC, &rsoc, 1, di);
154 if (ret) {
155 dev_err(di->dev, "error reading relative State-of-Charge\n");
156 return ret;
157 }
158
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +0200159 return rsoc;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700160}
161
162#define to_bq27x00_device_info(x) container_of((x), \
163 struct bq27x00_device_info, bat);
164
165static int bq27x00_battery_get_property(struct power_supply *psy,
166 enum power_supply_property psp,
167 union power_supply_propval *val)
168{
169 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
170
171 switch (psp) {
172 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
173 case POWER_SUPPLY_PROP_PRESENT:
174 val->intval = bq27x00_battery_voltage(di);
175 if (psp == POWER_SUPPLY_PROP_PRESENT)
176 val->intval = val->intval <= 0 ? 0 : 1;
177 break;
178 case POWER_SUPPLY_PROP_CURRENT_NOW:
179 val->intval = bq27x00_battery_current(di);
180 break;
181 case POWER_SUPPLY_PROP_CAPACITY:
182 val->intval = bq27x00_battery_rsoc(di);
183 break;
184 case POWER_SUPPLY_PROP_TEMP:
185 val->intval = bq27x00_battery_temperature(di);
186 break;
187 default:
188 return -EINVAL;
189 }
190
191 return 0;
192}
193
194static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
195{
196 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
197 di->bat.properties = bq27x00_battery_props;
198 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
199 di->bat.get_property = bq27x00_battery_get_property;
200 di->bat.external_power_changed = NULL;
201}
202
203/*
204 * BQ27200 specific code
205 */
206
207static int bq27200_read(u8 reg, int *rt_value, int b_single,
208 struct bq27x00_device_info *di)
209{
210 struct i2c_client *client = di->client;
211 struct i2c_msg msg[1];
212 unsigned char data[2];
213 int err;
214
215 if (!client->adapter)
216 return -ENODEV;
217
218 msg->addr = client->addr;
219 msg->flags = 0;
220 msg->len = 1;
221 msg->buf = data;
222
223 data[0] = reg;
224 err = i2c_transfer(client->adapter, msg, 1);
225
226 if (err >= 0) {
227 if (!b_single)
228 msg->len = 2;
229 else
230 msg->len = 1;
231
232 msg->flags = I2C_M_RD;
233 err = i2c_transfer(client->adapter, msg, 1);
234 if (err >= 0) {
235 if (!b_single)
Grazvydas Ignotas97f70c22010-02-12 23:56:46 +0200236 *rt_value = get_unaligned_le16(data);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700237 else
238 *rt_value = data[0];
239
240 return 0;
241 }
242 }
243 return err;
244}
245
246static int bq27200_battery_probe(struct i2c_client *client,
247 const struct i2c_device_id *id)
248{
249 char *name;
250 struct bq27x00_device_info *di;
251 struct bq27x00_access_methods *bus;
252 int num;
253 int retval = 0;
254
255 /* Get new ID for the new battery device */
256 retval = idr_pre_get(&battery_id, GFP_KERNEL);
257 if (retval == 0)
258 return -ENOMEM;
259 mutex_lock(&battery_mutex);
260 retval = idr_get_new(&battery_id, client, &num);
261 mutex_unlock(&battery_mutex);
262 if (retval < 0)
263 return retval;
264
265 name = kasprintf(GFP_KERNEL, "bq27200-%d", num);
266 if (!name) {
267 dev_err(&client->dev, "failed to allocate device name\n");
268 retval = -ENOMEM;
269 goto batt_failed_1;
270 }
271
272 di = kzalloc(sizeof(*di), GFP_KERNEL);
273 if (!di) {
274 dev_err(&client->dev, "failed to allocate device info data\n");
275 retval = -ENOMEM;
276 goto batt_failed_2;
277 }
278 di->id = num;
279
280 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
281 if (!bus) {
282 dev_err(&client->dev, "failed to allocate access method "
283 "data\n");
284 retval = -ENOMEM;
285 goto batt_failed_3;
286 }
287
288 i2c_set_clientdata(client, di);
289 di->dev = &client->dev;
290 di->bat.name = name;
291 bus->read = &bq27200_read;
292 di->bus = bus;
293 di->client = client;
294
295 bq27x00_powersupply_init(di);
296
297 retval = power_supply_register(&client->dev, &di->bat);
298 if (retval) {
299 dev_err(&client->dev, "failed to register battery\n");
300 goto batt_failed_4;
301 }
302
303 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
304
305 return 0;
306
307batt_failed_4:
308 kfree(bus);
309batt_failed_3:
310 kfree(di);
311batt_failed_2:
312 kfree(name);
313batt_failed_1:
314 mutex_lock(&battery_mutex);
315 idr_remove(&battery_id, num);
316 mutex_unlock(&battery_mutex);
317
318 return retval;
319}
320
321static int bq27200_battery_remove(struct i2c_client *client)
322{
323 struct bq27x00_device_info *di = i2c_get_clientdata(client);
324
325 power_supply_unregister(&di->bat);
326
327 kfree(di->bat.name);
328
329 mutex_lock(&battery_mutex);
330 idr_remove(&battery_id, di->id);
331 mutex_unlock(&battery_mutex);
332
333 kfree(di);
334
335 return 0;
336}
337
338/*
339 * Module stuff
340 */
341
342static const struct i2c_device_id bq27200_id[] = {
343 { "bq27200", 0 },
344 {},
345};
346
347static struct i2c_driver bq27200_battery_driver = {
348 .driver = {
349 .name = "bq27200-battery",
350 },
351 .probe = bq27200_battery_probe,
352 .remove = bq27200_battery_remove,
353 .id_table = bq27200_id,
354};
355
356static int __init bq27x00_battery_init(void)
357{
358 int ret;
359
360 ret = i2c_add_driver(&bq27200_battery_driver);
361 if (ret)
362 printk(KERN_ERR "Unable to register BQ27200 driver\n");
363
364 return ret;
365}
366module_init(bq27x00_battery_init);
367
368static void __exit bq27x00_battery_exit(void)
369{
370 i2c_del_driver(&bq27200_battery_driver);
371}
372module_exit(bq27x00_battery_exit);
373
374MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
375MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
376MODULE_LICENSE("GPL");