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> |
73c244a8 | 7 | * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com> |
b996ad0e RG |
8 | * |
9 | * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. | |
10 | * | |
11 | * This package is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
14 | * | |
15 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
17 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
18 | * | |
19 | */ | |
631c17ee PR |
20 | |
21 | /* | |
22 | * Datasheets: | |
23 | * http://focus.ti.com/docs/prod/folders/print/bq27000.html | |
24 | * http://focus.ti.com/docs/prod/folders/print/bq27500.html | |
a66f59ba | 25 | * http://www.ti.com/product/bq27425-g1 |
628ef02c | 26 | * http://www.ti.com/product/BQ27742-G1 |
631c17ee PR |
27 | */ |
28 | ||
1cb82fdb | 29 | #include <linux/device.h> |
b996ad0e RG |
30 | #include <linux/module.h> |
31 | #include <linux/param.h> | |
32 | #include <linux/jiffies.h> | |
33 | #include <linux/workqueue.h> | |
34 | #include <linux/delay.h> | |
35 | #include <linux/platform_device.h> | |
36 | #include <linux/power_supply.h> | |
37 | #include <linux/idr.h> | |
b996ad0e | 38 | #include <linux/i2c.h> |
5a0e3ad6 | 39 | #include <linux/slab.h> |
8aef7e8f | 40 | #include <asm/unaligned.h> |
b996ad0e | 41 | |
7fb7ba58 LPC |
42 | #include <linux/power/bq27x00_battery.h> |
43 | ||
631c17ee | 44 | #define DRIVER_VERSION "1.2.0" |
b996ad0e RG |
45 | |
46 | #define BQ27x00_REG_TEMP 0x06 | |
47 | #define BQ27x00_REG_VOLT 0x08 | |
b996ad0e RG |
48 | #define BQ27x00_REG_AI 0x14 |
49 | #define BQ27x00_REG_FLAGS 0x0A | |
4e924a81 GI |
50 | #define BQ27x00_REG_TTE 0x16 |
51 | #define BQ27x00_REG_TTF 0x18 | |
52 | #define BQ27x00_REG_TTECP 0x26 | |
202e0116 | 53 | #define BQ27x00_REG_NAC 0x0C /* Nominal available capacity */ |
631c17ee PR |
54 | #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */ |
55 | #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */ | |
202e0116 | 56 | #define BQ27x00_REG_AE 0x22 /* Available energy */ |
9903e627 | 57 | #define BQ27x00_POWER_AVG 0x24 |
b996ad0e | 58 | |
e20908d9 | 59 | #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ |
631c17ee | 60 | #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */ |
d66bab3f PR |
61 | #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */ |
62 | #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */ | |
4b226c2c | 63 | #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */ |
c1b9ab67 | 64 | #define BQ27000_FLAG_FC BIT(5) |
d66bab3f | 65 | #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */ |
e20908d9 | 66 | |
bf7d4140 | 67 | #define BQ27500_REG_SOC 0x2C |
631c17ee | 68 | #define BQ27500_REG_DCAP 0x3C /* Design capacity */ |
b7aaacf5 | 69 | #define BQ27500_FLAG_DSC BIT(0) |
d66bab3f PR |
70 | #define BQ27500_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */ |
71 | #define BQ27500_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */ | |
b7aaacf5 | 72 | #define BQ27500_FLAG_FC BIT(9) |
9903e627 | 73 | #define BQ27500_FLAG_OTC BIT(15) |
e20908d9 | 74 | |
628ef02c PV |
75 | #define BQ27742_POWER_AVG 0x76 |
76 | ||
a66f59ba SG |
77 | /* bq27425 register addresses are same as bq27x00 addresses minus 4 */ |
78 | #define BQ27425_REG_OFFSET 0x04 | |
79 | #define BQ27425_REG_SOC 0x18 /* Register address plus offset */ | |
80 | ||
a2e5118c | 81 | #define BQ27000_RS 20 /* Resistor sense */ |
9903e627 | 82 | #define BQ27x00_POWER_CONSTANT (256 * 29200 / 1000) |
a2e5118c | 83 | |
b996ad0e RG |
84 | struct bq27x00_device_info; |
85 | struct bq27x00_access_methods { | |
297a533b | 86 | int (*read)(struct bq27x00_device_info *di, u8 reg, bool single); |
b996ad0e RG |
87 | }; |
88 | ||
628ef02c | 89 | enum bq27x00_chip { BQ27000, BQ27500, BQ27425, BQ27742}; |
e20908d9 | 90 | |
297a533b LPC |
91 | struct bq27x00_reg_cache { |
92 | int temperature; | |
93 | int time_to_empty; | |
94 | int time_to_empty_avg; | |
95 | int time_to_full; | |
631c17ee | 96 | int charge_full; |
73c244a8 | 97 | int cycle_count; |
297a533b | 98 | int capacity; |
a8f6bd23 | 99 | int energy; |
297a533b | 100 | int flags; |
9903e627 SR |
101 | int power_avg; |
102 | int health; | |
297a533b LPC |
103 | }; |
104 | ||
b996ad0e RG |
105 | struct bq27x00_device_info { |
106 | struct device *dev; | |
107 | int id; | |
e20908d9 | 108 | enum bq27x00_chip chip; |
b996ad0e | 109 | |
297a533b | 110 | struct bq27x00_reg_cache cache; |
631c17ee PR |
111 | int charge_design_full; |
112 | ||
297a533b | 113 | unsigned long last_update; |
740b755a | 114 | struct delayed_work work; |
297a533b | 115 | |
a40402ef LPC |
116 | struct power_supply bat; |
117 | ||
118 | struct bq27x00_access_methods bus; | |
740b755a LPC |
119 | |
120 | struct mutex lock; | |
b996ad0e RG |
121 | }; |
122 | ||
123 | static enum power_supply_property bq27x00_battery_props[] = { | |
4e924a81 | 124 | POWER_SUPPLY_PROP_STATUS, |
b996ad0e RG |
125 | POWER_SUPPLY_PROP_PRESENT, |
126 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
127 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
128 | POWER_SUPPLY_PROP_CAPACITY, | |
d66bab3f | 129 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
b996ad0e | 130 | POWER_SUPPLY_PROP_TEMP, |
4e924a81 GI |
131 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
132 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, | |
133 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, | |
5661f334 | 134 | POWER_SUPPLY_PROP_TECHNOLOGY, |
631c17ee PR |
135 | POWER_SUPPLY_PROP_CHARGE_FULL, |
136 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
137 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
73c244a8 | 138 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
631c17ee | 139 | POWER_SUPPLY_PROP_ENERGY_NOW, |
9903e627 SR |
140 | POWER_SUPPLY_PROP_POWER_AVG, |
141 | POWER_SUPPLY_PROP_HEALTH, | |
b996ad0e RG |
142 | }; |
143 | ||
a66f59ba SG |
144 | static enum power_supply_property bq27425_battery_props[] = { |
145 | POWER_SUPPLY_PROP_STATUS, | |
146 | POWER_SUPPLY_PROP_PRESENT, | |
147 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
148 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
149 | POWER_SUPPLY_PROP_CAPACITY, | |
150 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, | |
151 | POWER_SUPPLY_PROP_TEMP, | |
152 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
153 | POWER_SUPPLY_PROP_CHARGE_FULL, | |
154 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
155 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
156 | }; | |
157 | ||
628ef02c PV |
158 | static enum power_supply_property bq27742_battery_props[] = { |
159 | POWER_SUPPLY_PROP_STATUS, | |
160 | POWER_SUPPLY_PROP_PRESENT, | |
161 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
162 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
163 | POWER_SUPPLY_PROP_CAPACITY, | |
164 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, | |
165 | POWER_SUPPLY_PROP_TEMP, | |
166 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, | |
167 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
168 | POWER_SUPPLY_PROP_CHARGE_FULL, | |
169 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
170 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
171 | POWER_SUPPLY_PROP_CYCLE_COUNT, | |
172 | POWER_SUPPLY_PROP_POWER_AVG, | |
173 | POWER_SUPPLY_PROP_HEALTH, | |
174 | }; | |
175 | ||
740b755a LPC |
176 | static unsigned int poll_interval = 360; |
177 | module_param(poll_interval, uint, 0644); | |
178 | MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \ | |
179 | "0 disables polling"); | |
180 | ||
b996ad0e RG |
181 | /* |
182 | * Common code for BQ27x00 devices | |
183 | */ | |
184 | ||
a40402ef | 185 | static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, |
297a533b | 186 | bool single) |
b996ad0e | 187 | { |
a66f59ba SG |
188 | if (di->chip == BQ27425) |
189 | return di->bus.read(di, reg - BQ27425_REG_OFFSET, single); | |
297a533b | 190 | return di->bus.read(di, reg, single); |
b996ad0e RG |
191 | } |
192 | ||
a66f59ba SG |
193 | /* |
194 | * Higher versions of the chip like BQ27425 and BQ27500 | |
195 | * differ from BQ27000 and BQ27200 in calculation of certain | |
196 | * parameters. Hence we need to check for the chip type. | |
197 | */ | |
198 | static bool bq27xxx_is_chip_version_higher(struct bq27x00_device_info *di) | |
199 | { | |
628ef02c | 200 | if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742) |
a66f59ba SG |
201 | return true; |
202 | return false; | |
203 | } | |
204 | ||
b996ad0e | 205 | /* |
297a533b | 206 | * Return the battery Relative State-of-Charge |
b996ad0e RG |
207 | * Or < 0 if something fails. |
208 | */ | |
297a533b | 209 | static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di) |
b996ad0e | 210 | { |
297a533b | 211 | int rsoc; |
b996ad0e | 212 | |
628ef02c | 213 | if (di->chip == BQ27500 || di->chip == BQ27742) |
297a533b | 214 | rsoc = bq27x00_read(di, BQ27500_REG_SOC, false); |
a66f59ba SG |
215 | else if (di->chip == BQ27425) |
216 | rsoc = bq27x00_read(di, BQ27425_REG_SOC, false); | |
e20908d9 | 217 | else |
297a533b LPC |
218 | rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true); |
219 | ||
220 | if (rsoc < 0) | |
c6cd4f26 | 221 | dev_dbg(di->dev, "error reading relative State-of-Charge\n"); |
297a533b LPC |
222 | |
223 | return rsoc; | |
b996ad0e RG |
224 | } |
225 | ||
631c17ee PR |
226 | /* |
227 | * Return a battery charge value in µAh | |
228 | * Or < 0 if something fails. | |
229 | */ | |
230 | static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg) | |
231 | { | |
232 | int charge; | |
233 | ||
234 | charge = bq27x00_read(di, reg, false); | |
235 | if (charge < 0) { | |
c6cd4f26 PR |
236 | dev_dbg(di->dev, "error reading charge register %02x: %d\n", |
237 | reg, charge); | |
631c17ee PR |
238 | return charge; |
239 | } | |
240 | ||
a66f59ba | 241 | if (bq27xxx_is_chip_version_higher(di)) |
631c17ee PR |
242 | charge *= 1000; |
243 | else | |
244 | charge = charge * 3570 / BQ27000_RS; | |
245 | ||
246 | return charge; | |
247 | } | |
248 | ||
249 | /* | |
250 | * Return the battery Nominal available capaciy in µAh | |
251 | * Or < 0 if something fails. | |
252 | */ | |
253 | static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di) | |
254 | { | |
e59ec4a1 PR |
255 | int flags; |
256 | bool is_bq27500 = di->chip == BQ27500; | |
a3c0c3e7 | 257 | bool is_bq27742 = di->chip == BQ27742; |
e59ec4a1 | 258 | bool is_higher = bq27xxx_is_chip_version_higher(di); |
a3c0c3e7 | 259 | bool flags_1b = !(is_bq27500 || is_bq27742); |
e59ec4a1 | 260 | |
a3c0c3e7 | 261 | flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b); |
e59ec4a1 PR |
262 | if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI)) |
263 | return -ENODATA; | |
264 | ||
631c17ee PR |
265 | return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC); |
266 | } | |
267 | ||
268 | /* | |
269 | * Return the battery Last measured discharge in µAh | |
270 | * Or < 0 if something fails. | |
271 | */ | |
272 | static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di) | |
273 | { | |
274 | return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD); | |
275 | } | |
276 | ||
277 | /* | |
278 | * Return the battery Initial last measured discharge in µAh | |
279 | * Or < 0 if something fails. | |
280 | */ | |
281 | static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di) | |
282 | { | |
283 | int ilmd; | |
284 | ||
a66f59ba | 285 | if (bq27xxx_is_chip_version_higher(di)) |
631c17ee PR |
286 | ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false); |
287 | else | |
288 | ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true); | |
289 | ||
290 | if (ilmd < 0) { | |
c6cd4f26 | 291 | dev_dbg(di->dev, "error reading initial last measured discharge\n"); |
631c17ee PR |
292 | return ilmd; |
293 | } | |
294 | ||
a66f59ba | 295 | if (bq27xxx_is_chip_version_higher(di)) |
631c17ee PR |
296 | ilmd *= 1000; |
297 | else | |
298 | ilmd = ilmd * 256 * 3570 / BQ27000_RS; | |
299 | ||
300 | return ilmd; | |
301 | } | |
302 | ||
a8f6bd23 PR |
303 | /* |
304 | * Return the battery Available energy in µWh | |
305 | * Or < 0 if something fails. | |
306 | */ | |
307 | static int bq27x00_battery_read_energy(struct bq27x00_device_info *di) | |
308 | { | |
309 | int ae; | |
310 | ||
311 | ae = bq27x00_read(di, BQ27x00_REG_AE, false); | |
312 | if (ae < 0) { | |
c6cd4f26 | 313 | dev_dbg(di->dev, "error reading available energy\n"); |
a8f6bd23 PR |
314 | return ae; |
315 | } | |
316 | ||
317 | if (di->chip == BQ27500) | |
318 | ae *= 1000; | |
319 | else | |
320 | ae = ae * 29200 / BQ27000_RS; | |
321 | ||
322 | return ae; | |
323 | } | |
324 | ||
d149e98e | 325 | /* |
5dc3443e | 326 | * Return the battery temperature in tenths of degree Kelvin |
d149e98e PR |
327 | * Or < 0 if something fails. |
328 | */ | |
329 | static int bq27x00_battery_read_temperature(struct bq27x00_device_info *di) | |
330 | { | |
331 | int temp; | |
332 | ||
333 | temp = bq27x00_read(di, BQ27x00_REG_TEMP, false); | |
334 | if (temp < 0) { | |
335 | dev_err(di->dev, "error reading temperature\n"); | |
336 | return temp; | |
337 | } | |
338 | ||
5dc3443e PR |
339 | if (!bq27xxx_is_chip_version_higher(di)) |
340 | temp = 5 * temp / 2; | |
d149e98e PR |
341 | |
342 | return temp; | |
343 | } | |
344 | ||
631c17ee PR |
345 | /* |
346 | * Return the battery Cycle count total | |
347 | * Or < 0 if something fails. | |
348 | */ | |
349 | static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di) | |
350 | { | |
351 | int cyct; | |
352 | ||
353 | cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false); | |
354 | if (cyct < 0) | |
355 | dev_err(di->dev, "error reading cycle count total\n"); | |
356 | ||
357 | return cyct; | |
358 | } | |
359 | ||
b996ad0e | 360 | /* |
297a533b LPC |
361 | * Read a time register. |
362 | * Return < 0 if something fails. | |
b996ad0e | 363 | */ |
297a533b | 364 | static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg) |
b996ad0e | 365 | { |
297a533b | 366 | int tval; |
b996ad0e | 367 | |
297a533b LPC |
368 | tval = bq27x00_read(di, reg, false); |
369 | if (tval < 0) { | |
c6cd4f26 PR |
370 | dev_dbg(di->dev, "error reading time register %02x: %d\n", |
371 | reg, tval); | |
297a533b | 372 | return tval; |
b996ad0e RG |
373 | } |
374 | ||
297a533b LPC |
375 | if (tval == 65535) |
376 | return -ENODATA; | |
377 | ||
378 | return tval * 60; | |
379 | } | |
380 | ||
9903e627 SR |
381 | /* |
382 | * Read a power avg register. | |
383 | * Return < 0 if something fails. | |
384 | */ | |
385 | static int bq27x00_battery_read_pwr_avg(struct bq27x00_device_info *di, u8 reg) | |
386 | { | |
387 | int tval; | |
388 | ||
389 | tval = bq27x00_read(di, reg, false); | |
390 | if (tval < 0) { | |
391 | dev_err(di->dev, "error reading power avg rgister %02x: %d\n", | |
392 | reg, tval); | |
393 | return tval; | |
394 | } | |
395 | ||
396 | if (di->chip == BQ27500) | |
397 | return tval; | |
398 | else | |
399 | return (tval * BQ27x00_POWER_CONSTANT) / BQ27000_RS; | |
400 | } | |
401 | ||
402 | /* | |
403 | * Read flag register. | |
404 | * Return < 0 if something fails. | |
405 | */ | |
406 | static int bq27x00_battery_read_health(struct bq27x00_device_info *di) | |
407 | { | |
408 | int tval; | |
409 | ||
410 | tval = bq27x00_read(di, BQ27x00_REG_FLAGS, false); | |
411 | if (tval < 0) { | |
412 | dev_err(di->dev, "error reading flag register:%d\n", tval); | |
413 | return tval; | |
414 | } | |
415 | ||
416 | if ((di->chip == BQ27500)) { | |
417 | if (tval & BQ27500_FLAG_SOCF) | |
418 | tval = POWER_SUPPLY_HEALTH_DEAD; | |
419 | else if (tval & BQ27500_FLAG_OTC) | |
420 | tval = POWER_SUPPLY_HEALTH_OVERHEAT; | |
421 | else | |
422 | tval = POWER_SUPPLY_HEALTH_GOOD; | |
423 | return tval; | |
424 | } else { | |
425 | if (tval & BQ27000_FLAG_EDV1) | |
426 | tval = POWER_SUPPLY_HEALTH_DEAD; | |
427 | else | |
428 | tval = POWER_SUPPLY_HEALTH_GOOD; | |
429 | return tval; | |
430 | } | |
431 | ||
432 | return -1; | |
433 | } | |
434 | ||
297a533b LPC |
435 | static void bq27x00_update(struct bq27x00_device_info *di) |
436 | { | |
437 | struct bq27x00_reg_cache cache = {0, }; | |
438 | bool is_bq27500 = di->chip == BQ27500; | |
a66f59ba | 439 | bool is_bq27425 = di->chip == BQ27425; |
628ef02c | 440 | bool is_bq27742 = di->chip == BQ27742; |
a3c0c3e7 | 441 | bool flags_1b = !(is_bq27500 || is_bq27742); |
297a533b | 442 | |
a3c0c3e7 | 443 | cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, flags_1b); |
3dd843e1 MB |
444 | if ((cache.flags & 0xff) == 0xff) |
445 | /* read error */ | |
446 | cache.flags = -1; | |
297a533b | 447 | if (cache.flags >= 0) { |
a3c0c3e7 | 448 | if (!is_bq27500 && !is_bq27425 && !is_bq27742 |
a66f59ba | 449 | && (cache.flags & BQ27000_FLAG_CI)) { |
c6cd4f26 | 450 | dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n"); |
4b226c2c | 451 | cache.capacity = -ENODATA; |
a8f6bd23 | 452 | cache.energy = -ENODATA; |
4b226c2c PR |
453 | cache.time_to_empty = -ENODATA; |
454 | cache.time_to_empty_avg = -ENODATA; | |
455 | cache.time_to_full = -ENODATA; | |
456 | cache.charge_full = -ENODATA; | |
9903e627 | 457 | cache.health = -ENODATA; |
4b226c2c PR |
458 | } else { |
459 | cache.capacity = bq27x00_battery_read_rsoc(di); | |
628ef02c PV |
460 | if (is_bq27742) |
461 | cache.time_to_empty = | |
462 | bq27x00_battery_read_time(di, | |
463 | BQ27x00_REG_TTE); | |
464 | else if (!is_bq27425) { | |
a66f59ba SG |
465 | cache.energy = bq27x00_battery_read_energy(di); |
466 | cache.time_to_empty = | |
467 | bq27x00_battery_read_time(di, | |
468 | BQ27x00_REG_TTE); | |
469 | cache.time_to_empty_avg = | |
470 | bq27x00_battery_read_time(di, | |
471 | BQ27x00_REG_TTECP); | |
472 | cache.time_to_full = | |
473 | bq27x00_battery_read_time(di, | |
474 | BQ27x00_REG_TTF); | |
475 | } | |
a3c0c3e7 | 476 | cache.charge_full = bq27x00_battery_read_lmd(di); |
9903e627 | 477 | cache.health = bq27x00_battery_read_health(di); |
4b226c2c | 478 | } |
d149e98e | 479 | cache.temperature = bq27x00_battery_read_temperature(di); |
a66f59ba SG |
480 | if (!is_bq27425) |
481 | cache.cycle_count = bq27x00_battery_read_cyct(di); | |
628ef02c PV |
482 | if (is_bq27742) |
483 | cache.power_avg = | |
484 | bq27x00_battery_read_pwr_avg(di, | |
485 | BQ27742_POWER_AVG); | |
486 | else | |
487 | cache.power_avg = | |
488 | bq27x00_battery_read_pwr_avg(di, | |
489 | BQ27x00_POWER_AVG); | |
297a533b | 490 | |
631c17ee PR |
491 | /* We only have to read charge design full once */ |
492 | if (di->charge_design_full <= 0) | |
493 | di->charge_design_full = bq27x00_battery_read_ilmd(di); | |
297a533b LPC |
494 | } |
495 | ||
b68f6216 | 496 | if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) { |
297a533b LPC |
497 | di->cache = cache; |
498 | power_supply_changed(&di->bat); | |
499 | } | |
500 | ||
501 | di->last_update = jiffies; | |
502 | } | |
503 | ||
740b755a LPC |
504 | static void bq27x00_battery_poll(struct work_struct *work) |
505 | { | |
506 | struct bq27x00_device_info *di = | |
507 | container_of(work, struct bq27x00_device_info, work.work); | |
508 | ||
509 | bq27x00_update(di); | |
510 | ||
511 | if (poll_interval > 0) { | |
512 | /* The timer does not have to be accurate. */ | |
513 | set_timer_slack(&di->work.timer, poll_interval * HZ / 4); | |
514 | schedule_delayed_work(&di->work, poll_interval * HZ); | |
515 | } | |
516 | } | |
517 | ||
b996ad0e | 518 | /* |
bf7d4140 | 519 | * Return the battery average current in µA |
b996ad0e RG |
520 | * Note that current can be negative signed as well |
521 | * Or 0 if something fails. | |
522 | */ | |
297a533b LPC |
523 | static int bq27x00_battery_current(struct bq27x00_device_info *di, |
524 | union power_supply_propval *val) | |
b996ad0e | 525 | { |
297a533b | 526 | int curr; |
b68f6216 | 527 | int flags; |
b996ad0e | 528 | |
b68f6216 | 529 | curr = bq27x00_read(di, BQ27x00_REG_AI, false); |
c6cd4f26 PR |
530 | if (curr < 0) { |
531 | dev_err(di->dev, "error reading current\n"); | |
297a533b | 532 | return curr; |
c6cd4f26 | 533 | } |
e20908d9 | 534 | |
a66f59ba | 535 | if (bq27xxx_is_chip_version_higher(di)) { |
e20908d9 | 536 | /* bq27500 returns signed value */ |
297a533b | 537 | val->intval = (int)((s16)curr) * 1000; |
e20908d9 | 538 | } else { |
b68f6216 PR |
539 | flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false); |
540 | if (flags & BQ27000_FLAG_CHGS) { | |
e20908d9 | 541 | dev_dbg(di->dev, "negative current!\n"); |
afbc74fd | 542 | curr = -curr; |
e20908d9 | 543 | } |
b996ad0e | 544 | |
297a533b | 545 | val->intval = curr * 3570 / BQ27000_RS; |
b996ad0e RG |
546 | } |
547 | ||
297a533b | 548 | return 0; |
b996ad0e RG |
549 | } |
550 | ||
4e924a81 | 551 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
297a533b | 552 | union power_supply_propval *val) |
4e924a81 | 553 | { |
4e924a81 | 554 | int status; |
4e924a81 | 555 | |
a66f59ba | 556 | if (bq27xxx_is_chip_version_higher(di)) { |
297a533b | 557 | if (di->cache.flags & BQ27500_FLAG_FC) |
4e924a81 | 558 | status = POWER_SUPPLY_STATUS_FULL; |
b7aaacf5 | 559 | else if (di->cache.flags & BQ27500_FLAG_DSC) |
4e924a81 | 560 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
270968c0 | 561 | else |
b7aaacf5 | 562 | status = POWER_SUPPLY_STATUS_CHARGING; |
4e924a81 | 563 | } else { |
c1b9ab67 LPC |
564 | if (di->cache.flags & BQ27000_FLAG_FC) |
565 | status = POWER_SUPPLY_STATUS_FULL; | |
566 | else if (di->cache.flags & BQ27000_FLAG_CHGS) | |
4e924a81 | 567 | status = POWER_SUPPLY_STATUS_CHARGING; |
c1b9ab67 LPC |
568 | else if (power_supply_am_i_supplied(&di->bat)) |
569 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
4e924a81 GI |
570 | else |
571 | status = POWER_SUPPLY_STATUS_DISCHARGING; | |
572 | } | |
573 | ||
574 | val->intval = status; | |
297a533b | 575 | |
4e924a81 GI |
576 | return 0; |
577 | } | |
578 | ||
d66bab3f PR |
579 | static int bq27x00_battery_capacity_level(struct bq27x00_device_info *di, |
580 | union power_supply_propval *val) | |
581 | { | |
582 | int level; | |
583 | ||
a66f59ba | 584 | if (bq27xxx_is_chip_version_higher(di)) { |
d66bab3f PR |
585 | if (di->cache.flags & BQ27500_FLAG_FC) |
586 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; | |
587 | else if (di->cache.flags & BQ27500_FLAG_SOC1) | |
588 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; | |
589 | else if (di->cache.flags & BQ27500_FLAG_SOCF) | |
590 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; | |
591 | else | |
592 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
593 | } else { | |
594 | if (di->cache.flags & BQ27000_FLAG_FC) | |
595 | level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; | |
596 | else if (di->cache.flags & BQ27000_FLAG_EDV1) | |
597 | level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; | |
598 | else if (di->cache.flags & BQ27000_FLAG_EDVF) | |
599 | level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; | |
600 | else | |
601 | level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
602 | } | |
603 | ||
604 | val->intval = level; | |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
4e924a81 | 609 | /* |
91fe4d50 | 610 | * Return the battery Voltage in millivolts |
297a533b | 611 | * Or < 0 if something fails. |
4e924a81 | 612 | */ |
297a533b LPC |
613 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di, |
614 | union power_supply_propval *val) | |
4e924a81 | 615 | { |
297a533b | 616 | int volt; |
4e924a81 | 617 | |
297a533b | 618 | volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); |
c6cd4f26 PR |
619 | if (volt < 0) { |
620 | dev_err(di->dev, "error reading voltage\n"); | |
297a533b | 621 | return volt; |
c6cd4f26 | 622 | } |
4e924a81 | 623 | |
297a533b LPC |
624 | val->intval = volt * 1000; |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
629 | static int bq27x00_simple_value(int value, | |
630 | union power_supply_propval *val) | |
631 | { | |
632 | if (value < 0) | |
633 | return value; | |
634 | ||
635 | val->intval = value; | |
4e924a81 | 636 | |
4e924a81 GI |
637 | return 0; |
638 | } | |
639 | ||
b996ad0e RG |
640 | #define to_bq27x00_device_info(x) container_of((x), \ |
641 | struct bq27x00_device_info, bat); | |
642 | ||
643 | static int bq27x00_battery_get_property(struct power_supply *psy, | |
644 | enum power_supply_property psp, | |
645 | union power_supply_propval *val) | |
646 | { | |
4e924a81 | 647 | int ret = 0; |
b996ad0e | 648 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
3413b4ea | 649 | |
740b755a LPC |
650 | mutex_lock(&di->lock); |
651 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) { | |
652 | cancel_delayed_work_sync(&di->work); | |
653 | bq27x00_battery_poll(&di->work.work); | |
654 | } | |
655 | mutex_unlock(&di->lock); | |
297a533b LPC |
656 | |
657 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) | |
3413b4ea | 658 | return -ENODEV; |
b996ad0e RG |
659 | |
660 | switch (psp) { | |
4e924a81 GI |
661 | case POWER_SUPPLY_PROP_STATUS: |
662 | ret = bq27x00_battery_status(di, val); | |
663 | break; | |
b996ad0e | 664 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
297a533b | 665 | ret = bq27x00_battery_voltage(di, val); |
3413b4ea | 666 | break; |
b996ad0e | 667 | case POWER_SUPPLY_PROP_PRESENT: |
297a533b | 668 | val->intval = di->cache.flags < 0 ? 0 : 1; |
b996ad0e RG |
669 | break; |
670 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
297a533b | 671 | ret = bq27x00_battery_current(di, val); |
b996ad0e RG |
672 | break; |
673 | case POWER_SUPPLY_PROP_CAPACITY: | |
297a533b | 674 | ret = bq27x00_simple_value(di->cache.capacity, val); |
b996ad0e | 675 | break; |
d66bab3f PR |
676 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
677 | ret = bq27x00_battery_capacity_level(di, val); | |
678 | break; | |
b996ad0e | 679 | case POWER_SUPPLY_PROP_TEMP: |
d149e98e | 680 | ret = bq27x00_simple_value(di->cache.temperature, val); |
5dc3443e PR |
681 | if (ret == 0) |
682 | val->intval -= 2731; | |
b996ad0e | 683 | break; |
4e924a81 | 684 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
297a533b | 685 | ret = bq27x00_simple_value(di->cache.time_to_empty, val); |
4e924a81 GI |
686 | break; |
687 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: | |
297a533b | 688 | ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); |
4e924a81 GI |
689 | break; |
690 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: | |
297a533b | 691 | ret = bq27x00_simple_value(di->cache.time_to_full, val); |
4e924a81 | 692 | break; |
5661f334 LPC |
693 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
694 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; | |
695 | break; | |
631c17ee PR |
696 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
697 | ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val); | |
698 | break; | |
699 | case POWER_SUPPLY_PROP_CHARGE_FULL: | |
700 | ret = bq27x00_simple_value(di->cache.charge_full, val); | |
701 | break; | |
702 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
703 | ret = bq27x00_simple_value(di->charge_design_full, val); | |
704 | break; | |
73c244a8 PR |
705 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
706 | ret = bq27x00_simple_value(di->cache.cycle_count, val); | |
631c17ee PR |
707 | break; |
708 | case POWER_SUPPLY_PROP_ENERGY_NOW: | |
a8f6bd23 | 709 | ret = bq27x00_simple_value(di->cache.energy, val); |
631c17ee | 710 | break; |
9903e627 SR |
711 | case POWER_SUPPLY_PROP_POWER_AVG: |
712 | ret = bq27x00_simple_value(di->cache.power_avg, val); | |
713 | break; | |
714 | case POWER_SUPPLY_PROP_HEALTH: | |
715 | ret = bq27x00_simple_value(di->cache.health, val); | |
716 | break; | |
b996ad0e RG |
717 | default: |
718 | return -EINVAL; | |
719 | } | |
720 | ||
4e924a81 | 721 | return ret; |
b996ad0e RG |
722 | } |
723 | ||
740b755a LPC |
724 | static void bq27x00_external_power_changed(struct power_supply *psy) |
725 | { | |
726 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); | |
727 | ||
728 | cancel_delayed_work_sync(&di->work); | |
729 | schedule_delayed_work(&di->work, 0); | |
730 | } | |
731 | ||
a40402ef | 732 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di) |
b996ad0e | 733 | { |
a40402ef LPC |
734 | int ret; |
735 | ||
b996ad0e | 736 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
a66f59ba SG |
737 | if (di->chip == BQ27425) { |
738 | di->bat.properties = bq27425_battery_props; | |
739 | di->bat.num_properties = ARRAY_SIZE(bq27425_battery_props); | |
628ef02c PV |
740 | } else if (di->chip == BQ27742) { |
741 | di->bat.properties = bq27742_battery_props; | |
742 | di->bat.num_properties = ARRAY_SIZE(bq27742_battery_props); | |
a66f59ba SG |
743 | } else { |
744 | di->bat.properties = bq27x00_battery_props; | |
745 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); | |
746 | } | |
b996ad0e | 747 | di->bat.get_property = bq27x00_battery_get_property; |
740b755a LPC |
748 | di->bat.external_power_changed = bq27x00_external_power_changed; |
749 | ||
750 | INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll); | |
751 | mutex_init(&di->lock); | |
a40402ef LPC |
752 | |
753 | ret = power_supply_register(di->dev, &di->bat); | |
754 | if (ret) { | |
755 | dev_err(di->dev, "failed to register battery: %d\n", ret); | |
756 | return ret; | |
757 | } | |
758 | ||
759 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); | |
760 | ||
297a533b LPC |
761 | bq27x00_update(di); |
762 | ||
a40402ef | 763 | return 0; |
b996ad0e RG |
764 | } |
765 | ||
740b755a LPC |
766 | static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di) |
767 | { | |
8cfaaa81 PR |
768 | /* |
769 | * power_supply_unregister call bq27x00_battery_get_property which | |
770 | * call bq27x00_battery_poll. | |
771 | * Make sure that bq27x00_battery_poll will not call | |
772 | * schedule_delayed_work again after unregister (which cause OOPS). | |
773 | */ | |
774 | poll_interval = 0; | |
775 | ||
740b755a LPC |
776 | cancel_delayed_work_sync(&di->work); |
777 | ||
778 | power_supply_unregister(&di->bat); | |
779 | ||
780 | mutex_destroy(&di->lock); | |
781 | } | |
782 | ||
7fb7ba58 LPC |
783 | |
784 | /* i2c specific code */ | |
785 | #ifdef CONFIG_BATTERY_BQ27X00_I2C | |
786 | ||
787 | /* If the system has several batteries we need a different name for each | |
788 | * of them... | |
b996ad0e | 789 | */ |
7fb7ba58 LPC |
790 | static DEFINE_IDR(battery_id); |
791 | static DEFINE_MUTEX(battery_mutex); | |
b996ad0e | 792 | |
297a533b | 793 | static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single) |
b996ad0e | 794 | { |
a40402ef | 795 | struct i2c_client *client = to_i2c_client(di->dev); |
9e912f45 | 796 | struct i2c_msg msg[2]; |
b996ad0e | 797 | unsigned char data[2]; |
297a533b | 798 | int ret; |
b996ad0e RG |
799 | |
800 | if (!client->adapter) | |
801 | return -ENODEV; | |
802 | ||
9e912f45 GI |
803 | msg[0].addr = client->addr; |
804 | msg[0].flags = 0; | |
805 | msg[0].buf = ® | |
806 | msg[0].len = sizeof(reg); | |
807 | msg[1].addr = client->addr; | |
808 | msg[1].flags = I2C_M_RD; | |
809 | msg[1].buf = data; | |
2ec523a8 | 810 | if (single) |
9e912f45 | 811 | msg[1].len = 1; |
2ec523a8 | 812 | else |
9e912f45 | 813 | msg[1].len = 2; |
2ec523a8 | 814 | |
9e912f45 | 815 | ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
2ec523a8 LPC |
816 | if (ret < 0) |
817 | return ret; | |
818 | ||
819 | if (!single) | |
820 | ret = get_unaligned_le16(data); | |
821 | else | |
822 | ret = data[0]; | |
b996ad0e | 823 | |
297a533b | 824 | return ret; |
b996ad0e RG |
825 | } |
826 | ||
e20908d9 | 827 | static int bq27x00_battery_probe(struct i2c_client *client, |
b996ad0e RG |
828 | const struct i2c_device_id *id) |
829 | { | |
830 | char *name; | |
831 | struct bq27x00_device_info *di; | |
b996ad0e RG |
832 | int num; |
833 | int retval = 0; | |
834 | ||
835 | /* Get new ID for the new battery device */ | |
b996ad0e | 836 | mutex_lock(&battery_mutex); |
05e2cefa | 837 | num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL); |
b996ad0e | 838 | mutex_unlock(&battery_mutex); |
05e2cefa TH |
839 | if (num < 0) |
840 | return num; | |
b996ad0e | 841 | |
e20908d9 | 842 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
b996ad0e RG |
843 | if (!name) { |
844 | dev_err(&client->dev, "failed to allocate device name\n"); | |
845 | retval = -ENOMEM; | |
846 | goto batt_failed_1; | |
847 | } | |
848 | ||
1cb82fdb | 849 | di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL); |
b996ad0e RG |
850 | if (!di) { |
851 | dev_err(&client->dev, "failed to allocate device info data\n"); | |
852 | retval = -ENOMEM; | |
853 | goto batt_failed_2; | |
854 | } | |
a40402ef | 855 | |
b996ad0e | 856 | di->id = num; |
a40402ef | 857 | di->dev = &client->dev; |
e20908d9 | 858 | di->chip = id->driver_data; |
a40402ef LPC |
859 | di->bat.name = name; |
860 | di->bus.read = &bq27x00_read_i2c; | |
b996ad0e | 861 | |
f7760995 JL |
862 | retval = bq27x00_powersupply_init(di); |
863 | if (retval) | |
1cb82fdb | 864 | goto batt_failed_2; |
b996ad0e RG |
865 | |
866 | i2c_set_clientdata(client, di); | |
b996ad0e RG |
867 | |
868 | return 0; | |
869 | ||
b996ad0e RG |
870 | batt_failed_2: |
871 | kfree(name); | |
872 | batt_failed_1: | |
873 | mutex_lock(&battery_mutex); | |
874 | idr_remove(&battery_id, num); | |
875 | mutex_unlock(&battery_mutex); | |
876 | ||
877 | return retval; | |
878 | } | |
879 | ||
e20908d9 | 880 | static int bq27x00_battery_remove(struct i2c_client *client) |
b996ad0e RG |
881 | { |
882 | struct bq27x00_device_info *di = i2c_get_clientdata(client); | |
883 | ||
740b755a | 884 | bq27x00_powersupply_unregister(di); |
b996ad0e RG |
885 | |
886 | kfree(di->bat.name); | |
887 | ||
888 | mutex_lock(&battery_mutex); | |
889 | idr_remove(&battery_id, di->id); | |
890 | mutex_unlock(&battery_mutex); | |
891 | ||
b996ad0e RG |
892 | return 0; |
893 | } | |
894 | ||
e20908d9 GI |
895 | static const struct i2c_device_id bq27x00_id[] = { |
896 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ | |
897 | { "bq27500", BQ27500 }, | |
a66f59ba | 898 | { "bq27425", BQ27425 }, |
628ef02c | 899 | { "bq27742", BQ27742 }, |
b996ad0e RG |
900 | {}, |
901 | }; | |
fd9b958c | 902 | MODULE_DEVICE_TABLE(i2c, bq27x00_id); |
b996ad0e | 903 | |
e20908d9 | 904 | static struct i2c_driver bq27x00_battery_driver = { |
b996ad0e | 905 | .driver = { |
e20908d9 | 906 | .name = "bq27x00-battery", |
b996ad0e | 907 | }, |
e20908d9 GI |
908 | .probe = bq27x00_battery_probe, |
909 | .remove = bq27x00_battery_remove, | |
910 | .id_table = bq27x00_id, | |
b996ad0e RG |
911 | }; |
912 | ||
7fb7ba58 LPC |
913 | static inline int bq27x00_battery_i2c_init(void) |
914 | { | |
915 | int ret = i2c_add_driver(&bq27x00_battery_driver); | |
916 | if (ret) | |
917 | printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); | |
918 | ||
919 | return ret; | |
920 | } | |
921 | ||
922 | static inline void bq27x00_battery_i2c_exit(void) | |
923 | { | |
924 | i2c_del_driver(&bq27x00_battery_driver); | |
925 | } | |
926 | ||
927 | #else | |
928 | ||
929 | static inline int bq27x00_battery_i2c_init(void) { return 0; } | |
930 | static inline void bq27x00_battery_i2c_exit(void) {}; | |
931 | ||
932 | #endif | |
933 | ||
934 | /* platform specific code */ | |
935 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM | |
936 | ||
937 | static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg, | |
297a533b | 938 | bool single) |
7fb7ba58 LPC |
939 | { |
940 | struct device *dev = di->dev; | |
941 | struct bq27000_platform_data *pdata = dev->platform_data; | |
942 | unsigned int timeout = 3; | |
943 | int upper, lower; | |
944 | int temp; | |
945 | ||
946 | if (!single) { | |
947 | /* Make sure the value has not changed in between reading the | |
948 | * lower and the upper part */ | |
949 | upper = pdata->read(dev, reg + 1); | |
950 | do { | |
951 | temp = upper; | |
952 | if (upper < 0) | |
953 | return upper; | |
954 | ||
955 | lower = pdata->read(dev, reg); | |
956 | if (lower < 0) | |
957 | return lower; | |
958 | ||
959 | upper = pdata->read(dev, reg + 1); | |
960 | } while (temp != upper && --timeout); | |
961 | ||
962 | if (timeout == 0) | |
963 | return -EIO; | |
964 | ||
297a533b | 965 | return (upper << 8) | lower; |
7fb7ba58 | 966 | } |
297a533b LPC |
967 | |
968 | return pdata->read(dev, reg); | |
7fb7ba58 LPC |
969 | } |
970 | ||
c8afa640 | 971 | static int bq27000_battery_probe(struct platform_device *pdev) |
7fb7ba58 LPC |
972 | { |
973 | struct bq27x00_device_info *di; | |
974 | struct bq27000_platform_data *pdata = pdev->dev.platform_data; | |
7fb7ba58 LPC |
975 | |
976 | if (!pdata) { | |
977 | dev_err(&pdev->dev, "no platform_data supplied\n"); | |
978 | return -EINVAL; | |
979 | } | |
980 | ||
981 | if (!pdata->read) { | |
982 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); | |
983 | return -EINVAL; | |
984 | } | |
985 | ||
1cb82fdb | 986 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
7fb7ba58 LPC |
987 | if (!di) { |
988 | dev_err(&pdev->dev, "failed to allocate device info data\n"); | |
989 | return -ENOMEM; | |
990 | } | |
991 | ||
992 | platform_set_drvdata(pdev, di); | |
993 | ||
994 | di->dev = &pdev->dev; | |
995 | di->chip = BQ27000; | |
996 | ||
997 | di->bat.name = pdata->name ?: dev_name(&pdev->dev); | |
998 | di->bus.read = &bq27000_read_platform; | |
999 | ||
1cb82fdb | 1000 | return bq27x00_powersupply_init(di); |
7fb7ba58 LPC |
1001 | } |
1002 | ||
415ec69f | 1003 | static int bq27000_battery_remove(struct platform_device *pdev) |
7fb7ba58 LPC |
1004 | { |
1005 | struct bq27x00_device_info *di = platform_get_drvdata(pdev); | |
1006 | ||
740b755a LPC |
1007 | bq27x00_powersupply_unregister(di); |
1008 | ||
7fb7ba58 LPC |
1009 | return 0; |
1010 | } | |
1011 | ||
1012 | static struct platform_driver bq27000_battery_driver = { | |
1013 | .probe = bq27000_battery_probe, | |
28ea73f4 | 1014 | .remove = bq27000_battery_remove, |
7fb7ba58 LPC |
1015 | .driver = { |
1016 | .name = "bq27000-battery", | |
1017 | .owner = THIS_MODULE, | |
1018 | }, | |
1019 | }; | |
1020 | ||
1021 | static inline int bq27x00_battery_platform_init(void) | |
1022 | { | |
1023 | int ret = platform_driver_register(&bq27000_battery_driver); | |
1024 | if (ret) | |
1025 | printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); | |
1026 | ||
1027 | return ret; | |
1028 | } | |
1029 | ||
1030 | static inline void bq27x00_battery_platform_exit(void) | |
1031 | { | |
1032 | platform_driver_unregister(&bq27000_battery_driver); | |
1033 | } | |
1034 | ||
1035 | #else | |
1036 | ||
1037 | static inline int bq27x00_battery_platform_init(void) { return 0; } | |
1038 | static inline void bq27x00_battery_platform_exit(void) {}; | |
1039 | ||
1040 | #endif | |
1041 | ||
1042 | /* | |
1043 | * Module stuff | |
1044 | */ | |
1045 | ||
b996ad0e RG |
1046 | static int __init bq27x00_battery_init(void) |
1047 | { | |
1048 | int ret; | |
1049 | ||
7fb7ba58 LPC |
1050 | ret = bq27x00_battery_i2c_init(); |
1051 | if (ret) | |
1052 | return ret; | |
1053 | ||
1054 | ret = bq27x00_battery_platform_init(); | |
b996ad0e | 1055 | if (ret) |
7fb7ba58 | 1056 | bq27x00_battery_i2c_exit(); |
b996ad0e RG |
1057 | |
1058 | return ret; | |
1059 | } | |
1060 | module_init(bq27x00_battery_init); | |
1061 | ||
1062 | static void __exit bq27x00_battery_exit(void) | |
1063 | { | |
7fb7ba58 LPC |
1064 | bq27x00_battery_platform_exit(); |
1065 | bq27x00_battery_i2c_exit(); | |
b996ad0e RG |
1066 | } |
1067 | module_exit(bq27x00_battery_exit); | |
1068 | ||
1069 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | |
1070 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); | |
1071 | MODULE_LICENSE("GPL"); |