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