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> | |
7fb7ba58 | 6 | * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de> |
b996ad0e RG |
7 | * |
8 | * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. | |
9 | * | |
10 | * This package is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
15 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
16 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
17 | * | |
18 | */ | |
19 | #include <linux/module.h> | |
20 | #include <linux/param.h> | |
21 | #include <linux/jiffies.h> | |
22 | #include <linux/workqueue.h> | |
23 | #include <linux/delay.h> | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/power_supply.h> | |
26 | #include <linux/idr.h> | |
b996ad0e | 27 | #include <linux/i2c.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
8aef7e8f | 29 | #include <asm/unaligned.h> |
b996ad0e | 30 | |
7fb7ba58 LPC |
31 | #include <linux/power/bq27x00_battery.h> |
32 | ||
e20908d9 | 33 | #define DRIVER_VERSION "1.1.0" |
b996ad0e RG |
34 | |
35 | #define BQ27x00_REG_TEMP 0x06 | |
36 | #define BQ27x00_REG_VOLT 0x08 | |
b996ad0e RG |
37 | #define BQ27x00_REG_AI 0x14 |
38 | #define BQ27x00_REG_FLAGS 0x0A | |
4e924a81 GI |
39 | #define BQ27x00_REG_TTE 0x16 |
40 | #define BQ27x00_REG_TTF 0x18 | |
41 | #define BQ27x00_REG_TTECP 0x26 | |
b996ad0e | 42 | |
e20908d9 | 43 | #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ |
4e924a81 | 44 | #define BQ27000_FLAG_CHGS BIT(7) |
e20908d9 GI |
45 | |
46 | #define BQ27500_REG_SOC 0x2c | |
4e924a81 GI |
47 | #define BQ27500_FLAG_DSC BIT(0) |
48 | #define BQ27500_FLAG_FC BIT(9) | |
e20908d9 | 49 | |
a2e5118c PR |
50 | #define BQ27000_RS 20 /* Resistor sense */ |
51 | ||
b996ad0e RG |
52 | struct bq27x00_device_info; |
53 | struct bq27x00_access_methods { | |
297a533b | 54 | int (*read)(struct bq27x00_device_info *di, u8 reg, bool single); |
b996ad0e RG |
55 | }; |
56 | ||
e20908d9 GI |
57 | enum bq27x00_chip { BQ27000, BQ27500 }; |
58 | ||
297a533b LPC |
59 | struct bq27x00_reg_cache { |
60 | int temperature; | |
61 | int time_to_empty; | |
62 | int time_to_empty_avg; | |
63 | int time_to_full; | |
64 | int capacity; | |
65 | int flags; | |
66 | ||
67 | int current_now; | |
68 | }; | |
69 | ||
b996ad0e RG |
70 | struct bq27x00_device_info { |
71 | struct device *dev; | |
72 | int id; | |
e20908d9 | 73 | enum bq27x00_chip chip; |
b996ad0e | 74 | |
297a533b LPC |
75 | struct bq27x00_reg_cache cache; |
76 | unsigned long last_update; | |
77 | ||
a40402ef LPC |
78 | struct power_supply bat; |
79 | ||
80 | struct bq27x00_access_methods bus; | |
b996ad0e RG |
81 | }; |
82 | ||
83 | static enum power_supply_property bq27x00_battery_props[] = { | |
4e924a81 | 84 | POWER_SUPPLY_PROP_STATUS, |
b996ad0e RG |
85 | POWER_SUPPLY_PROP_PRESENT, |
86 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
87 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
88 | POWER_SUPPLY_PROP_CAPACITY, | |
89 | POWER_SUPPLY_PROP_TEMP, | |
4e924a81 GI |
90 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
91 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, | |
92 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, | |
5661f334 | 93 | POWER_SUPPLY_PROP_TECHNOLOGY, |
b996ad0e RG |
94 | }; |
95 | ||
96 | /* | |
97 | * Common code for BQ27x00 devices | |
98 | */ | |
99 | ||
a40402ef | 100 | static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, |
297a533b | 101 | bool single) |
b996ad0e | 102 | { |
297a533b | 103 | return di->bus.read(di, reg, single); |
b996ad0e RG |
104 | } |
105 | ||
106 | /* | |
297a533b | 107 | * Return the battery Relative State-of-Charge |
b996ad0e RG |
108 | * Or < 0 if something fails. |
109 | */ | |
297a533b | 110 | static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di) |
b996ad0e | 111 | { |
297a533b | 112 | int rsoc; |
b996ad0e | 113 | |
e20908d9 | 114 | if (di->chip == BQ27500) |
297a533b | 115 | rsoc = bq27x00_read(di, BQ27500_REG_SOC, false); |
e20908d9 | 116 | else |
297a533b LPC |
117 | rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true); |
118 | ||
119 | if (rsoc < 0) | |
120 | dev_err(di->dev, "error reading relative State-of-Charge\n"); | |
121 | ||
122 | return rsoc; | |
b996ad0e RG |
123 | } |
124 | ||
125 | /* | |
297a533b LPC |
126 | * Read a time register. |
127 | * Return < 0 if something fails. | |
b996ad0e | 128 | */ |
297a533b | 129 | static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg) |
b996ad0e | 130 | { |
297a533b | 131 | int tval; |
b996ad0e | 132 | |
297a533b LPC |
133 | tval = bq27x00_read(di, reg, false); |
134 | if (tval < 0) { | |
135 | dev_err(di->dev, "error reading register %02x: %d\n", reg, tval); | |
136 | return tval; | |
b996ad0e RG |
137 | } |
138 | ||
297a533b LPC |
139 | if (tval == 65535) |
140 | return -ENODATA; | |
141 | ||
142 | return tval * 60; | |
143 | } | |
144 | ||
145 | static void bq27x00_update(struct bq27x00_device_info *di) | |
146 | { | |
147 | struct bq27x00_reg_cache cache = {0, }; | |
148 | bool is_bq27500 = di->chip == BQ27500; | |
149 | ||
150 | cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500); | |
151 | if (cache.flags >= 0) { | |
152 | cache.capacity = bq27x00_battery_read_rsoc(di); | |
153 | cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false); | |
154 | cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE); | |
155 | cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP); | |
156 | cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF); | |
157 | ||
158 | if (!is_bq27500) | |
159 | cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false); | |
160 | } | |
161 | ||
162 | /* Ignore current_now which is a snapshot of the current battery state | |
163 | * and is likely to be different even between two consecutive reads */ | |
164 | if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) { | |
165 | di->cache = cache; | |
166 | power_supply_changed(&di->bat); | |
167 | } | |
168 | ||
169 | di->last_update = jiffies; | |
170 | } | |
171 | ||
172 | /* | |
173 | * Return the battery temperature in tenths of degree Celsius | |
174 | * Or < 0 if something fails. | |
175 | */ | |
176 | static int bq27x00_battery_temperature(struct bq27x00_device_info *di, | |
177 | union power_supply_propval *val) | |
178 | { | |
179 | if (di->cache.temperature < 0) | |
180 | return di->cache.temperature; | |
181 | ||
182 | if (di->chip == BQ27500) | |
183 | val->intval = di->cache.temperature - 2731; | |
184 | else | |
185 | val->intval = ((di->cache.temperature * 5) - 5463) / 2; | |
186 | ||
187 | return 0; | |
b996ad0e RG |
188 | } |
189 | ||
190 | /* | |
191 | * Return the battery average current | |
192 | * Note that current can be negative signed as well | |
193 | * Or 0 if something fails. | |
194 | */ | |
297a533b LPC |
195 | static int bq27x00_battery_current(struct bq27x00_device_info *di, |
196 | union power_supply_propval *val) | |
b996ad0e | 197 | { |
297a533b | 198 | int curr; |
b996ad0e | 199 | |
297a533b LPC |
200 | if (di->chip == BQ27500) |
201 | curr = bq27x00_read(di, BQ27x00_REG_AI, false); | |
202 | else | |
203 | curr = di->cache.current_now; | |
204 | ||
205 | if (curr < 0) | |
206 | return curr; | |
e20908d9 GI |
207 | |
208 | if (di->chip == BQ27500) { | |
209 | /* bq27500 returns signed value */ | |
297a533b | 210 | val->intval = (int)((s16)curr) * 1000; |
e20908d9 | 211 | } else { |
297a533b | 212 | if (di->cache.flags & BQ27000_FLAG_CHGS) { |
e20908d9 | 213 | dev_dbg(di->dev, "negative current!\n"); |
afbc74fd | 214 | curr = -curr; |
e20908d9 | 215 | } |
b996ad0e | 216 | |
297a533b | 217 | val->intval = curr * 3570 / BQ27000_RS; |
b996ad0e RG |
218 | } |
219 | ||
297a533b | 220 | return 0; |
b996ad0e RG |
221 | } |
222 | ||
4e924a81 | 223 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
297a533b | 224 | union power_supply_propval *val) |
4e924a81 | 225 | { |
4e924a81 | 226 | int status; |
4e924a81 GI |
227 | |
228 | if (di->chip == BQ27500) { | |
297a533b | 229 | if (di->cache.flags & BQ27500_FLAG_FC) |
4e924a81 | 230 | status = POWER_SUPPLY_STATUS_FULL; |
297a533b | 231 | else if (di->cache.flags & BQ27500_FLAG_DSC) |
4e924a81 GI |
232 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
233 | else | |
234 | status = POWER_SUPPLY_STATUS_CHARGING; | |
235 | } else { | |
297a533b | 236 | if (di->cache.flags & BQ27000_FLAG_CHGS) |
4e924a81 GI |
237 | status = POWER_SUPPLY_STATUS_CHARGING; |
238 | else | |
239 | status = POWER_SUPPLY_STATUS_DISCHARGING; | |
240 | } | |
241 | ||
242 | val->intval = status; | |
297a533b | 243 | |
4e924a81 GI |
244 | return 0; |
245 | } | |
246 | ||
247 | /* | |
297a533b LPC |
248 | * Return the battery Voltage in milivolts |
249 | * Or < 0 if something fails. | |
4e924a81 | 250 | */ |
297a533b LPC |
251 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di, |
252 | union power_supply_propval *val) | |
4e924a81 | 253 | { |
297a533b | 254 | int volt; |
4e924a81 | 255 | |
297a533b LPC |
256 | volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); |
257 | if (volt < 0) | |
258 | return volt; | |
4e924a81 | 259 | |
297a533b LPC |
260 | val->intval = volt * 1000; |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static int bq27x00_simple_value(int value, | |
266 | union power_supply_propval *val) | |
267 | { | |
268 | if (value < 0) | |
269 | return value; | |
270 | ||
271 | val->intval = value; | |
4e924a81 | 272 | |
4e924a81 GI |
273 | return 0; |
274 | } | |
275 | ||
b996ad0e RG |
276 | #define to_bq27x00_device_info(x) container_of((x), \ |
277 | struct bq27x00_device_info, bat); | |
278 | ||
279 | static int bq27x00_battery_get_property(struct power_supply *psy, | |
280 | enum power_supply_property psp, | |
281 | union power_supply_propval *val) | |
282 | { | |
4e924a81 | 283 | int ret = 0; |
b996ad0e | 284 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
3413b4ea | 285 | |
297a533b LPC |
286 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) |
287 | bq27x00_update(di); | |
288 | ||
289 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) | |
3413b4ea | 290 | return -ENODEV; |
b996ad0e RG |
291 | |
292 | switch (psp) { | |
4e924a81 GI |
293 | case POWER_SUPPLY_PROP_STATUS: |
294 | ret = bq27x00_battery_status(di, val); | |
295 | break; | |
b996ad0e | 296 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
297a533b | 297 | ret = bq27x00_battery_voltage(di, val); |
3413b4ea | 298 | break; |
b996ad0e | 299 | case POWER_SUPPLY_PROP_PRESENT: |
297a533b | 300 | val->intval = di->cache.flags < 0 ? 0 : 1; |
b996ad0e RG |
301 | break; |
302 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
297a533b | 303 | ret = bq27x00_battery_current(di, val); |
b996ad0e RG |
304 | break; |
305 | case POWER_SUPPLY_PROP_CAPACITY: | |
297a533b | 306 | ret = bq27x00_simple_value(di->cache.capacity, val); |
b996ad0e RG |
307 | break; |
308 | case POWER_SUPPLY_PROP_TEMP: | |
297a533b | 309 | ret = bq27x00_battery_temperature(di, val); |
b996ad0e | 310 | break; |
4e924a81 | 311 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
297a533b | 312 | ret = bq27x00_simple_value(di->cache.time_to_empty, val); |
4e924a81 GI |
313 | break; |
314 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: | |
297a533b | 315 | ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); |
4e924a81 GI |
316 | break; |
317 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: | |
297a533b | 318 | ret = bq27x00_simple_value(di->cache.time_to_full, val); |
4e924a81 | 319 | break; |
5661f334 LPC |
320 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
321 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; | |
322 | break; | |
b996ad0e RG |
323 | default: |
324 | return -EINVAL; | |
325 | } | |
326 | ||
4e924a81 | 327 | return ret; |
b996ad0e RG |
328 | } |
329 | ||
a40402ef | 330 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di) |
b996ad0e | 331 | { |
a40402ef LPC |
332 | int ret; |
333 | ||
b996ad0e RG |
334 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
335 | di->bat.properties = bq27x00_battery_props; | |
336 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); | |
337 | di->bat.get_property = bq27x00_battery_get_property; | |
338 | di->bat.external_power_changed = NULL; | |
a40402ef LPC |
339 | |
340 | ret = power_supply_register(di->dev, &di->bat); | |
341 | if (ret) { | |
342 | dev_err(di->dev, "failed to register battery: %d\n", ret); | |
343 | return ret; | |
344 | } | |
345 | ||
346 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); | |
347 | ||
297a533b LPC |
348 | bq27x00_update(di); |
349 | ||
a40402ef | 350 | return 0; |
b996ad0e RG |
351 | } |
352 | ||
7fb7ba58 LPC |
353 | |
354 | /* i2c specific code */ | |
355 | #ifdef CONFIG_BATTERY_BQ27X00_I2C | |
356 | ||
357 | /* If the system has several batteries we need a different name for each | |
358 | * of them... | |
b996ad0e | 359 | */ |
7fb7ba58 LPC |
360 | static DEFINE_IDR(battery_id); |
361 | static DEFINE_MUTEX(battery_mutex); | |
b996ad0e | 362 | |
297a533b | 363 | static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single) |
b996ad0e | 364 | { |
a40402ef | 365 | struct i2c_client *client = to_i2c_client(di->dev); |
b996ad0e RG |
366 | struct i2c_msg msg[1]; |
367 | unsigned char data[2]; | |
297a533b | 368 | int ret; |
b996ad0e RG |
369 | |
370 | if (!client->adapter) | |
371 | return -ENODEV; | |
372 | ||
373 | msg->addr = client->addr; | |
374 | msg->flags = 0; | |
375 | msg->len = 1; | |
376 | msg->buf = data; | |
377 | ||
378 | data[0] = reg; | |
297a533b | 379 | ret = i2c_transfer(client->adapter, msg, 1); |
b996ad0e | 380 | |
297a533b | 381 | if (ret >= 0) { |
a40402ef | 382 | if (!single) |
b996ad0e RG |
383 | msg->len = 2; |
384 | else | |
385 | msg->len = 1; | |
386 | ||
387 | msg->flags = I2C_M_RD; | |
297a533b LPC |
388 | ret = i2c_transfer(client->adapter, msg, 1); |
389 | if (ret >= 0) { | |
a40402ef | 390 | if (!single) |
297a533b | 391 | ret = get_unaligned_le16(data); |
b996ad0e | 392 | else |
297a533b | 393 | ret = data[0]; |
b996ad0e RG |
394 | } |
395 | } | |
297a533b | 396 | return ret; |
b996ad0e RG |
397 | } |
398 | ||
e20908d9 | 399 | static int bq27x00_battery_probe(struct i2c_client *client, |
b996ad0e RG |
400 | const struct i2c_device_id *id) |
401 | { | |
402 | char *name; | |
403 | struct bq27x00_device_info *di; | |
b996ad0e RG |
404 | int num; |
405 | int retval = 0; | |
406 | ||
407 | /* Get new ID for the new battery device */ | |
408 | retval = idr_pre_get(&battery_id, GFP_KERNEL); | |
409 | if (retval == 0) | |
410 | return -ENOMEM; | |
411 | mutex_lock(&battery_mutex); | |
412 | retval = idr_get_new(&battery_id, client, &num); | |
413 | mutex_unlock(&battery_mutex); | |
414 | if (retval < 0) | |
415 | return retval; | |
416 | ||
e20908d9 | 417 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
b996ad0e RG |
418 | if (!name) { |
419 | dev_err(&client->dev, "failed to allocate device name\n"); | |
420 | retval = -ENOMEM; | |
421 | goto batt_failed_1; | |
422 | } | |
423 | ||
424 | di = kzalloc(sizeof(*di), GFP_KERNEL); | |
425 | if (!di) { | |
426 | dev_err(&client->dev, "failed to allocate device info data\n"); | |
427 | retval = -ENOMEM; | |
428 | goto batt_failed_2; | |
429 | } | |
a40402ef | 430 | |
b996ad0e | 431 | di->id = num; |
a40402ef | 432 | di->dev = &client->dev; |
e20908d9 | 433 | di->chip = id->driver_data; |
a40402ef LPC |
434 | di->bat.name = name; |
435 | di->bus.read = &bq27x00_read_i2c; | |
b996ad0e | 436 | |
a40402ef | 437 | if (bq27x00_powersupply_init(di)) |
b996ad0e | 438 | goto batt_failed_3; |
b996ad0e RG |
439 | |
440 | i2c_set_clientdata(client, di); | |
b996ad0e RG |
441 | |
442 | return 0; | |
443 | ||
b996ad0e RG |
444 | batt_failed_3: |
445 | kfree(di); | |
446 | batt_failed_2: | |
447 | kfree(name); | |
448 | batt_failed_1: | |
449 | mutex_lock(&battery_mutex); | |
450 | idr_remove(&battery_id, num); | |
451 | mutex_unlock(&battery_mutex); | |
452 | ||
453 | return retval; | |
454 | } | |
455 | ||
e20908d9 | 456 | static int bq27x00_battery_remove(struct i2c_client *client) |
b996ad0e RG |
457 | { |
458 | struct bq27x00_device_info *di = i2c_get_clientdata(client); | |
459 | ||
460 | power_supply_unregister(&di->bat); | |
461 | ||
462 | kfree(di->bat.name); | |
463 | ||
464 | mutex_lock(&battery_mutex); | |
465 | idr_remove(&battery_id, di->id); | |
466 | mutex_unlock(&battery_mutex); | |
467 | ||
468 | kfree(di); | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
e20908d9 GI |
473 | static const struct i2c_device_id bq27x00_id[] = { |
474 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ | |
475 | { "bq27500", BQ27500 }, | |
b996ad0e RG |
476 | {}, |
477 | }; | |
478 | ||
e20908d9 | 479 | static struct i2c_driver bq27x00_battery_driver = { |
b996ad0e | 480 | .driver = { |
e20908d9 | 481 | .name = "bq27x00-battery", |
b996ad0e | 482 | }, |
e20908d9 GI |
483 | .probe = bq27x00_battery_probe, |
484 | .remove = bq27x00_battery_remove, | |
485 | .id_table = bq27x00_id, | |
b996ad0e RG |
486 | }; |
487 | ||
7fb7ba58 LPC |
488 | static inline int bq27x00_battery_i2c_init(void) |
489 | { | |
490 | int ret = i2c_add_driver(&bq27x00_battery_driver); | |
491 | if (ret) | |
492 | printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); | |
493 | ||
494 | return ret; | |
495 | } | |
496 | ||
497 | static inline void bq27x00_battery_i2c_exit(void) | |
498 | { | |
499 | i2c_del_driver(&bq27x00_battery_driver); | |
500 | } | |
501 | ||
502 | #else | |
503 | ||
504 | static inline int bq27x00_battery_i2c_init(void) { return 0; } | |
505 | static inline void bq27x00_battery_i2c_exit(void) {}; | |
506 | ||
507 | #endif | |
508 | ||
509 | /* platform specific code */ | |
510 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM | |
511 | ||
512 | static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg, | |
297a533b | 513 | bool single) |
7fb7ba58 LPC |
514 | { |
515 | struct device *dev = di->dev; | |
516 | struct bq27000_platform_data *pdata = dev->platform_data; | |
517 | unsigned int timeout = 3; | |
518 | int upper, lower; | |
519 | int temp; | |
520 | ||
521 | if (!single) { | |
522 | /* Make sure the value has not changed in between reading the | |
523 | * lower and the upper part */ | |
524 | upper = pdata->read(dev, reg + 1); | |
525 | do { | |
526 | temp = upper; | |
527 | if (upper < 0) | |
528 | return upper; | |
529 | ||
530 | lower = pdata->read(dev, reg); | |
531 | if (lower < 0) | |
532 | return lower; | |
533 | ||
534 | upper = pdata->read(dev, reg + 1); | |
535 | } while (temp != upper && --timeout); | |
536 | ||
537 | if (timeout == 0) | |
538 | return -EIO; | |
539 | ||
297a533b | 540 | return (upper << 8) | lower; |
7fb7ba58 | 541 | } |
297a533b LPC |
542 | |
543 | return pdata->read(dev, reg); | |
7fb7ba58 LPC |
544 | } |
545 | ||
546 | static int __devinit bq27000_battery_probe(struct platform_device *pdev) | |
547 | { | |
548 | struct bq27x00_device_info *di; | |
549 | struct bq27000_platform_data *pdata = pdev->dev.platform_data; | |
550 | int ret; | |
551 | ||
552 | if (!pdata) { | |
553 | dev_err(&pdev->dev, "no platform_data supplied\n"); | |
554 | return -EINVAL; | |
555 | } | |
556 | ||
557 | if (!pdata->read) { | |
558 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); | |
559 | return -EINVAL; | |
560 | } | |
561 | ||
562 | di = kzalloc(sizeof(*di), GFP_KERNEL); | |
563 | if (!di) { | |
564 | dev_err(&pdev->dev, "failed to allocate device info data\n"); | |
565 | return -ENOMEM; | |
566 | } | |
567 | ||
568 | platform_set_drvdata(pdev, di); | |
569 | ||
570 | di->dev = &pdev->dev; | |
571 | di->chip = BQ27000; | |
572 | ||
573 | di->bat.name = pdata->name ?: dev_name(&pdev->dev); | |
574 | di->bus.read = &bq27000_read_platform; | |
575 | ||
576 | ret = bq27x00_powersupply_init(di); | |
577 | if (ret) | |
578 | goto err_free; | |
579 | ||
580 | return 0; | |
581 | ||
582 | err_free: | |
583 | platform_set_drvdata(pdev, NULL); | |
584 | kfree(di); | |
585 | ||
586 | return ret; | |
587 | } | |
588 | ||
589 | static int __devexit bq27000_battery_remove(struct platform_device *pdev) | |
590 | { | |
591 | struct bq27x00_device_info *di = platform_get_drvdata(pdev); | |
592 | ||
593 | power_supply_unregister(&di->bat); | |
594 | platform_set_drvdata(pdev, NULL); | |
595 | kfree(di); | |
596 | ||
597 | return 0; | |
598 | } | |
599 | ||
600 | static struct platform_driver bq27000_battery_driver = { | |
601 | .probe = bq27000_battery_probe, | |
602 | .remove = __devexit_p(bq27000_battery_remove), | |
603 | .driver = { | |
604 | .name = "bq27000-battery", | |
605 | .owner = THIS_MODULE, | |
606 | }, | |
607 | }; | |
608 | ||
609 | static inline int bq27x00_battery_platform_init(void) | |
610 | { | |
611 | int ret = platform_driver_register(&bq27000_battery_driver); | |
612 | if (ret) | |
613 | printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); | |
614 | ||
615 | return ret; | |
616 | } | |
617 | ||
618 | static inline void bq27x00_battery_platform_exit(void) | |
619 | { | |
620 | platform_driver_unregister(&bq27000_battery_driver); | |
621 | } | |
622 | ||
623 | #else | |
624 | ||
625 | static inline int bq27x00_battery_platform_init(void) { return 0; } | |
626 | static inline void bq27x00_battery_platform_exit(void) {}; | |
627 | ||
628 | #endif | |
629 | ||
630 | /* | |
631 | * Module stuff | |
632 | */ | |
633 | ||
b996ad0e RG |
634 | static int __init bq27x00_battery_init(void) |
635 | { | |
636 | int ret; | |
637 | ||
7fb7ba58 LPC |
638 | ret = bq27x00_battery_i2c_init(); |
639 | if (ret) | |
640 | return ret; | |
641 | ||
642 | ret = bq27x00_battery_platform_init(); | |
b996ad0e | 643 | if (ret) |
7fb7ba58 | 644 | bq27x00_battery_i2c_exit(); |
b996ad0e RG |
645 | |
646 | return ret; | |
647 | } | |
648 | module_init(bq27x00_battery_init); | |
649 | ||
650 | static void __exit bq27x00_battery_exit(void) | |
651 | { | |
7fb7ba58 LPC |
652 | bq27x00_battery_platform_exit(); |
653 | bq27x00_battery_i2c_exit(); | |
b996ad0e RG |
654 | } |
655 | module_exit(bq27x00_battery_exit); | |
656 | ||
657 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | |
658 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); | |
659 | MODULE_LICENSE("GPL"); |