Commit | Line | Data |
---|---|---|
b996ad0e RG |
1 | /* |
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> | |
b996ad0e | 26 | #include <linux/i2c.h> |
8aef7e8f | 27 | #include <asm/unaligned.h> |
b996ad0e | 28 | |
e20908d9 | 29 | #define DRIVER_VERSION "1.1.0" |
b996ad0e RG |
30 | |
31 | #define BQ27x00_REG_TEMP 0x06 | |
32 | #define BQ27x00_REG_VOLT 0x08 | |
b996ad0e RG |
33 | #define BQ27x00_REG_AI 0x14 |
34 | #define BQ27x00_REG_FLAGS 0x0A | |
b996ad0e | 35 | |
e20908d9 GI |
36 | #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ |
37 | ||
38 | #define BQ27500_REG_SOC 0x2c | |
39 | ||
b996ad0e RG |
40 | /* If the system has several batteries we need a different name for each |
41 | * of them... | |
42 | */ | |
43 | static DEFINE_IDR(battery_id); | |
44 | static DEFINE_MUTEX(battery_mutex); | |
45 | ||
46 | struct bq27x00_device_info; | |
47 | struct bq27x00_access_methods { | |
48 | int (*read)(u8 reg, int *rt_value, int b_single, | |
49 | struct bq27x00_device_info *di); | |
50 | }; | |
51 | ||
e20908d9 GI |
52 | enum bq27x00_chip { BQ27000, BQ27500 }; |
53 | ||
b996ad0e RG |
54 | struct bq27x00_device_info { |
55 | struct device *dev; | |
56 | int id; | |
b996ad0e RG |
57 | struct bq27x00_access_methods *bus; |
58 | struct power_supply bat; | |
e20908d9 | 59 | enum bq27x00_chip chip; |
b996ad0e RG |
60 | |
61 | struct i2c_client *client; | |
62 | }; | |
63 | ||
64 | static enum power_supply_property bq27x00_battery_props[] = { | |
65 | POWER_SUPPLY_PROP_PRESENT, | |
66 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
67 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
68 | POWER_SUPPLY_PROP_CAPACITY, | |
69 | POWER_SUPPLY_PROP_TEMP, | |
70 | }; | |
71 | ||
72 | /* | |
73 | * Common code for BQ27x00 devices | |
74 | */ | |
75 | ||
76 | static int bq27x00_read(u8 reg, int *rt_value, int b_single, | |
77 | struct bq27x00_device_info *di) | |
78 | { | |
97f70c23 | 79 | return di->bus->read(reg, rt_value, b_single, di); |
b996ad0e RG |
80 | } |
81 | ||
82 | /* | |
b4de3608 | 83 | * Return the battery temperature in tenths of degree Celsius |
b996ad0e RG |
84 | * Or < 0 if something fails. |
85 | */ | |
86 | static int bq27x00_battery_temperature(struct bq27x00_device_info *di) | |
87 | { | |
88 | int ret; | |
89 | int temp = 0; | |
90 | ||
91 | ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di); | |
92 | if (ret) { | |
93 | dev_err(di->dev, "error reading temperature\n"); | |
94 | return ret; | |
95 | } | |
96 | ||
e20908d9 GI |
97 | if (di->chip == BQ27500) |
98 | return temp - 2731; | |
99 | else | |
100 | return ((temp >> 2) - 273) * 10; | |
b996ad0e RG |
101 | } |
102 | ||
103 | /* | |
104 | * Return the battery Voltage in milivolts | |
105 | * Or < 0 if something fails. | |
106 | */ | |
107 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di) | |
108 | { | |
109 | int ret; | |
110 | int volt = 0; | |
111 | ||
112 | ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di); | |
113 | if (ret) { | |
114 | dev_err(di->dev, "error reading voltage\n"); | |
115 | return ret; | |
116 | } | |
117 | ||
118 | return volt; | |
119 | } | |
120 | ||
121 | /* | |
122 | * Return the battery average current | |
123 | * Note that current can be negative signed as well | |
124 | * Or 0 if something fails. | |
125 | */ | |
126 | static int bq27x00_battery_current(struct bq27x00_device_info *di) | |
127 | { | |
128 | int ret; | |
129 | int curr = 0; | |
130 | int flags = 0; | |
131 | ||
132 | ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di); | |
133 | if (ret) { | |
134 | dev_err(di->dev, "error reading current\n"); | |
135 | return 0; | |
136 | } | |
e20908d9 GI |
137 | |
138 | if (di->chip == BQ27500) { | |
139 | /* bq27500 returns signed value */ | |
140 | curr = (int)(s16)curr; | |
141 | } else { | |
142 | ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di); | |
143 | if (ret < 0) { | |
144 | dev_err(di->dev, "error reading flags\n"); | |
145 | return 0; | |
146 | } | |
147 | if ((flags & (1 << 7)) != 0) { | |
148 | dev_dbg(di->dev, "negative current!\n"); | |
149 | return -curr; | |
150 | } | |
b996ad0e | 151 | } |
e20908d9 | 152 | |
b996ad0e RG |
153 | return curr; |
154 | } | |
155 | ||
156 | /* | |
157 | * Return the battery Relative State-of-Charge | |
158 | * Or < 0 if something fails. | |
159 | */ | |
160 | static int bq27x00_battery_rsoc(struct bq27x00_device_info *di) | |
161 | { | |
162 | int ret; | |
163 | int rsoc = 0; | |
164 | ||
e20908d9 GI |
165 | if (di->chip == BQ27500) |
166 | ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di); | |
167 | else | |
168 | ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di); | |
b996ad0e RG |
169 | if (ret) { |
170 | dev_err(di->dev, "error reading relative State-of-Charge\n"); | |
171 | return ret; | |
172 | } | |
173 | ||
97f70c23 | 174 | return rsoc; |
b996ad0e RG |
175 | } |
176 | ||
177 | #define to_bq27x00_device_info(x) container_of((x), \ | |
178 | struct bq27x00_device_info, bat); | |
179 | ||
180 | static int bq27x00_battery_get_property(struct power_supply *psy, | |
181 | enum power_supply_property psp, | |
182 | union power_supply_propval *val) | |
183 | { | |
184 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); | |
185 | ||
186 | switch (psp) { | |
187 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
188 | case POWER_SUPPLY_PROP_PRESENT: | |
189 | val->intval = bq27x00_battery_voltage(di); | |
190 | if (psp == POWER_SUPPLY_PROP_PRESENT) | |
191 | val->intval = val->intval <= 0 ? 0 : 1; | |
192 | break; | |
193 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
194 | val->intval = bq27x00_battery_current(di); | |
195 | break; | |
196 | case POWER_SUPPLY_PROP_CAPACITY: | |
197 | val->intval = bq27x00_battery_rsoc(di); | |
198 | break; | |
199 | case POWER_SUPPLY_PROP_TEMP: | |
200 | val->intval = bq27x00_battery_temperature(di); | |
201 | break; | |
202 | default: | |
203 | return -EINVAL; | |
204 | } | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | static void bq27x00_powersupply_init(struct bq27x00_device_info *di) | |
210 | { | |
211 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; | |
212 | di->bat.properties = bq27x00_battery_props; | |
213 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); | |
214 | di->bat.get_property = bq27x00_battery_get_property; | |
215 | di->bat.external_power_changed = NULL; | |
216 | } | |
217 | ||
218 | /* | |
e20908d9 | 219 | * i2c specific code |
b996ad0e RG |
220 | */ |
221 | ||
e20908d9 | 222 | static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single, |
b996ad0e RG |
223 | struct bq27x00_device_info *di) |
224 | { | |
225 | struct i2c_client *client = di->client; | |
226 | struct i2c_msg msg[1]; | |
227 | unsigned char data[2]; | |
228 | int err; | |
229 | ||
230 | if (!client->adapter) | |
231 | return -ENODEV; | |
232 | ||
233 | msg->addr = client->addr; | |
234 | msg->flags = 0; | |
235 | msg->len = 1; | |
236 | msg->buf = data; | |
237 | ||
238 | data[0] = reg; | |
239 | err = i2c_transfer(client->adapter, msg, 1); | |
240 | ||
241 | if (err >= 0) { | |
242 | if (!b_single) | |
243 | msg->len = 2; | |
244 | else | |
245 | msg->len = 1; | |
246 | ||
247 | msg->flags = I2C_M_RD; | |
248 | err = i2c_transfer(client->adapter, msg, 1); | |
249 | if (err >= 0) { | |
250 | if (!b_single) | |
97f70c23 | 251 | *rt_value = get_unaligned_le16(data); |
b996ad0e RG |
252 | else |
253 | *rt_value = data[0]; | |
254 | ||
255 | return 0; | |
256 | } | |
257 | } | |
258 | return err; | |
259 | } | |
260 | ||
e20908d9 | 261 | static int bq27x00_battery_probe(struct i2c_client *client, |
b996ad0e RG |
262 | const struct i2c_device_id *id) |
263 | { | |
264 | char *name; | |
265 | struct bq27x00_device_info *di; | |
266 | struct bq27x00_access_methods *bus; | |
267 | int num; | |
268 | int retval = 0; | |
269 | ||
270 | /* Get new ID for the new battery device */ | |
271 | retval = idr_pre_get(&battery_id, GFP_KERNEL); | |
272 | if (retval == 0) | |
273 | return -ENOMEM; | |
274 | mutex_lock(&battery_mutex); | |
275 | retval = idr_get_new(&battery_id, client, &num); | |
276 | mutex_unlock(&battery_mutex); | |
277 | if (retval < 0) | |
278 | return retval; | |
279 | ||
e20908d9 | 280 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
b996ad0e RG |
281 | if (!name) { |
282 | dev_err(&client->dev, "failed to allocate device name\n"); | |
283 | retval = -ENOMEM; | |
284 | goto batt_failed_1; | |
285 | } | |
286 | ||
287 | di = kzalloc(sizeof(*di), GFP_KERNEL); | |
288 | if (!di) { | |
289 | dev_err(&client->dev, "failed to allocate device info data\n"); | |
290 | retval = -ENOMEM; | |
291 | goto batt_failed_2; | |
292 | } | |
293 | di->id = num; | |
e20908d9 | 294 | di->chip = id->driver_data; |
b996ad0e RG |
295 | |
296 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); | |
297 | if (!bus) { | |
298 | dev_err(&client->dev, "failed to allocate access method " | |
299 | "data\n"); | |
300 | retval = -ENOMEM; | |
301 | goto batt_failed_3; | |
302 | } | |
303 | ||
304 | i2c_set_clientdata(client, di); | |
305 | di->dev = &client->dev; | |
306 | di->bat.name = name; | |
e20908d9 | 307 | bus->read = &bq27x00_read_i2c; |
b996ad0e RG |
308 | di->bus = bus; |
309 | di->client = client; | |
310 | ||
311 | bq27x00_powersupply_init(di); | |
312 | ||
313 | retval = power_supply_register(&client->dev, &di->bat); | |
314 | if (retval) { | |
315 | dev_err(&client->dev, "failed to register battery\n"); | |
316 | goto batt_failed_4; | |
317 | } | |
318 | ||
319 | dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION); | |
320 | ||
321 | return 0; | |
322 | ||
323 | batt_failed_4: | |
324 | kfree(bus); | |
325 | batt_failed_3: | |
326 | kfree(di); | |
327 | batt_failed_2: | |
328 | kfree(name); | |
329 | batt_failed_1: | |
330 | mutex_lock(&battery_mutex); | |
331 | idr_remove(&battery_id, num); | |
332 | mutex_unlock(&battery_mutex); | |
333 | ||
334 | return retval; | |
335 | } | |
336 | ||
e20908d9 | 337 | static int bq27x00_battery_remove(struct i2c_client *client) |
b996ad0e RG |
338 | { |
339 | struct bq27x00_device_info *di = i2c_get_clientdata(client); | |
340 | ||
341 | power_supply_unregister(&di->bat); | |
342 | ||
343 | kfree(di->bat.name); | |
344 | ||
345 | mutex_lock(&battery_mutex); | |
346 | idr_remove(&battery_id, di->id); | |
347 | mutex_unlock(&battery_mutex); | |
348 | ||
349 | kfree(di); | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
354 | /* | |
355 | * Module stuff | |
356 | */ | |
357 | ||
e20908d9 GI |
358 | static const struct i2c_device_id bq27x00_id[] = { |
359 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ | |
360 | { "bq27500", BQ27500 }, | |
b996ad0e RG |
361 | {}, |
362 | }; | |
363 | ||
e20908d9 | 364 | static struct i2c_driver bq27x00_battery_driver = { |
b996ad0e | 365 | .driver = { |
e20908d9 | 366 | .name = "bq27x00-battery", |
b996ad0e | 367 | }, |
e20908d9 GI |
368 | .probe = bq27x00_battery_probe, |
369 | .remove = bq27x00_battery_remove, | |
370 | .id_table = bq27x00_id, | |
b996ad0e RG |
371 | }; |
372 | ||
373 | static int __init bq27x00_battery_init(void) | |
374 | { | |
375 | int ret; | |
376 | ||
e20908d9 | 377 | ret = i2c_add_driver(&bq27x00_battery_driver); |
b996ad0e | 378 | if (ret) |
e20908d9 | 379 | printk(KERN_ERR "Unable to register BQ27x00 driver\n"); |
b996ad0e RG |
380 | |
381 | return ret; | |
382 | } | |
383 | module_init(bq27x00_battery_init); | |
384 | ||
385 | static void __exit bq27x00_battery_exit(void) | |
386 | { | |
e20908d9 | 387 | i2c_del_driver(&bq27x00_battery_driver); |
b996ad0e RG |
388 | } |
389 | module_exit(bq27x00_battery_exit); | |
390 | ||
391 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | |
392 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); | |
393 | MODULE_LICENSE("GPL"); |