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