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