Merge tag 'fixes-for-v4.8-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi...
[linux/fpc-iii.git] / drivers / power / bq27xxx_battery.c
blob323d05a12f9b80fc855286d74a9b0e72021d1a03
1 /*
2 * BQ27xxx battery driver
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
7 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
9 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
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.
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.
19 * Datasheets:
20 * http://www.ti.com/product/bq27000
21 * http://www.ti.com/product/bq27200
22 * http://www.ti.com/product/bq27010
23 * http://www.ti.com/product/bq27210
24 * http://www.ti.com/product/bq27500
25 * http://www.ti.com/product/bq27510-g3
26 * http://www.ti.com/product/bq27520-g4
27 * http://www.ti.com/product/bq27530-g1
28 * http://www.ti.com/product/bq27531-g1
29 * http://www.ti.com/product/bq27541-g1
30 * http://www.ti.com/product/bq27542-g1
31 * http://www.ti.com/product/bq27546-g1
32 * http://www.ti.com/product/bq27742-g1
33 * http://www.ti.com/product/bq27545-g1
34 * http://www.ti.com/product/bq27421-g1
35 * http://www.ti.com/product/bq27425-g1
36 * http://www.ti.com/product/bq27411-g1
37 * http://www.ti.com/product/bq27621-g1
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/param.h>
43 #include <linux/jiffies.h>
44 #include <linux/workqueue.h>
45 #include <linux/delay.h>
46 #include <linux/platform_device.h>
47 #include <linux/power_supply.h>
48 #include <linux/slab.h>
49 #include <linux/of.h>
51 #include <linux/power/bq27xxx_battery.h>
53 #define DRIVER_VERSION "1.2.0"
55 #define BQ27XXX_MANUFACTURER "Texas Instruments"
57 /* BQ27XXX Flags */
58 #define BQ27XXX_FLAG_DSC BIT(0)
59 #define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
60 #define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
61 #define BQ27XXX_FLAG_FC BIT(9)
62 #define BQ27XXX_FLAG_OTD BIT(14)
63 #define BQ27XXX_FLAG_OTC BIT(15)
64 #define BQ27XXX_FLAG_UT BIT(14)
65 #define BQ27XXX_FLAG_OT BIT(15)
67 /* BQ27000 has different layout for Flags register */
68 #define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
69 #define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
70 #define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
71 #define BQ27000_FLAG_FC BIT(5)
72 #define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
74 #define BQ27XXX_RS (20) /* Resistor sense mOhm */
75 #define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
76 #define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
78 #define INVALID_REG_ADDR 0xff
81 * bq27xxx_reg_index - Register names
83 * These are indexes into a device's register mapping array.
86 enum bq27xxx_reg_index {
87 BQ27XXX_REG_CTRL = 0, /* Control */
88 BQ27XXX_REG_TEMP, /* Temperature */
89 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
90 BQ27XXX_REG_VOLT, /* Voltage */
91 BQ27XXX_REG_AI, /* Average Current */
92 BQ27XXX_REG_FLAGS, /* Flags */
93 BQ27XXX_REG_TTE, /* Time-to-Empty */
94 BQ27XXX_REG_TTF, /* Time-to-Full */
95 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
96 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
97 BQ27XXX_REG_NAC, /* Nominal Available Capacity */
98 BQ27XXX_REG_FCC, /* Full Charge Capacity */
99 BQ27XXX_REG_CYCT, /* Cycle Count */
100 BQ27XXX_REG_AE, /* Available Energy */
101 BQ27XXX_REG_SOC, /* State-of-Charge */
102 BQ27XXX_REG_DCAP, /* Design Capacity */
103 BQ27XXX_REG_AP, /* Average Power */
104 BQ27XXX_REG_MAX, /* sentinel */
107 /* Register mappings */
108 static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = {
109 [BQ27000] = {
110 [BQ27XXX_REG_CTRL] = 0x00,
111 [BQ27XXX_REG_TEMP] = 0x06,
112 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
113 [BQ27XXX_REG_VOLT] = 0x08,
114 [BQ27XXX_REG_AI] = 0x14,
115 [BQ27XXX_REG_FLAGS] = 0x0a,
116 [BQ27XXX_REG_TTE] = 0x16,
117 [BQ27XXX_REG_TTF] = 0x18,
118 [BQ27XXX_REG_TTES] = 0x1c,
119 [BQ27XXX_REG_TTECP] = 0x26,
120 [BQ27XXX_REG_NAC] = 0x0c,
121 [BQ27XXX_REG_FCC] = 0x12,
122 [BQ27XXX_REG_CYCT] = 0x2a,
123 [BQ27XXX_REG_AE] = 0x22,
124 [BQ27XXX_REG_SOC] = 0x0b,
125 [BQ27XXX_REG_DCAP] = 0x76,
126 [BQ27XXX_REG_AP] = 0x24,
128 [BQ27010] = {
129 [BQ27XXX_REG_CTRL] = 0x00,
130 [BQ27XXX_REG_TEMP] = 0x06,
131 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
132 [BQ27XXX_REG_VOLT] = 0x08,
133 [BQ27XXX_REG_AI] = 0x14,
134 [BQ27XXX_REG_FLAGS] = 0x0a,
135 [BQ27XXX_REG_TTE] = 0x16,
136 [BQ27XXX_REG_TTF] = 0x18,
137 [BQ27XXX_REG_TTES] = 0x1c,
138 [BQ27XXX_REG_TTECP] = 0x26,
139 [BQ27XXX_REG_NAC] = 0x0c,
140 [BQ27XXX_REG_FCC] = 0x12,
141 [BQ27XXX_REG_CYCT] = 0x2a,
142 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
143 [BQ27XXX_REG_SOC] = 0x0b,
144 [BQ27XXX_REG_DCAP] = 0x76,
145 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
147 [BQ27500] = {
148 [BQ27XXX_REG_CTRL] = 0x00,
149 [BQ27XXX_REG_TEMP] = 0x06,
150 [BQ27XXX_REG_INT_TEMP] = 0x28,
151 [BQ27XXX_REG_VOLT] = 0x08,
152 [BQ27XXX_REG_AI] = 0x14,
153 [BQ27XXX_REG_FLAGS] = 0x0a,
154 [BQ27XXX_REG_TTE] = 0x16,
155 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
156 [BQ27XXX_REG_TTES] = 0x1a,
157 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
158 [BQ27XXX_REG_NAC] = 0x0c,
159 [BQ27XXX_REG_FCC] = 0x12,
160 [BQ27XXX_REG_CYCT] = 0x2a,
161 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
162 [BQ27XXX_REG_SOC] = 0x2c,
163 [BQ27XXX_REG_DCAP] = 0x3c,
164 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
166 [BQ27530] = {
167 [BQ27XXX_REG_CTRL] = 0x00,
168 [BQ27XXX_REG_TEMP] = 0x06,
169 [BQ27XXX_REG_INT_TEMP] = 0x32,
170 [BQ27XXX_REG_VOLT] = 0x08,
171 [BQ27XXX_REG_AI] = 0x14,
172 [BQ27XXX_REG_FLAGS] = 0x0a,
173 [BQ27XXX_REG_TTE] = 0x16,
174 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
175 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
176 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
177 [BQ27XXX_REG_NAC] = 0x0c,
178 [BQ27XXX_REG_FCC] = 0x12,
179 [BQ27XXX_REG_CYCT] = 0x2a,
180 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
181 [BQ27XXX_REG_SOC] = 0x2c,
182 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
183 [BQ27XXX_REG_AP] = 0x24,
185 [BQ27541] = {
186 [BQ27XXX_REG_CTRL] = 0x00,
187 [BQ27XXX_REG_TEMP] = 0x06,
188 [BQ27XXX_REG_INT_TEMP] = 0x28,
189 [BQ27XXX_REG_VOLT] = 0x08,
190 [BQ27XXX_REG_AI] = 0x14,
191 [BQ27XXX_REG_FLAGS] = 0x0a,
192 [BQ27XXX_REG_TTE] = 0x16,
193 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
194 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
195 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
196 [BQ27XXX_REG_NAC] = 0x0c,
197 [BQ27XXX_REG_FCC] = 0x12,
198 [BQ27XXX_REG_CYCT] = 0x2a,
199 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
200 [BQ27XXX_REG_SOC] = 0x2c,
201 [BQ27XXX_REG_DCAP] = 0x3c,
202 [BQ27XXX_REG_AP] = 0x24,
204 [BQ27545] = {
205 [BQ27XXX_REG_CTRL] = 0x00,
206 [BQ27XXX_REG_TEMP] = 0x06,
207 [BQ27XXX_REG_INT_TEMP] = 0x28,
208 [BQ27XXX_REG_VOLT] = 0x08,
209 [BQ27XXX_REG_AI] = 0x14,
210 [BQ27XXX_REG_FLAGS] = 0x0a,
211 [BQ27XXX_REG_TTE] = 0x16,
212 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
213 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
214 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
215 [BQ27XXX_REG_NAC] = 0x0c,
216 [BQ27XXX_REG_FCC] = 0x12,
217 [BQ27XXX_REG_CYCT] = 0x2a,
218 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
219 [BQ27XXX_REG_SOC] = 0x2c,
220 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
221 [BQ27XXX_REG_AP] = 0x24,
223 [BQ27421] = {
224 [BQ27XXX_REG_CTRL] = 0x00,
225 [BQ27XXX_REG_TEMP] = 0x02,
226 [BQ27XXX_REG_INT_TEMP] = 0x1e,
227 [BQ27XXX_REG_VOLT] = 0x04,
228 [BQ27XXX_REG_AI] = 0x10,
229 [BQ27XXX_REG_FLAGS] = 0x06,
230 [BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
231 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
232 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
233 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
234 [BQ27XXX_REG_NAC] = 0x08,
235 [BQ27XXX_REG_FCC] = 0x0e,
236 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
237 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
238 [BQ27XXX_REG_SOC] = 0x1c,
239 [BQ27XXX_REG_DCAP] = 0x3c,
240 [BQ27XXX_REG_AP] = 0x18,
244 static enum power_supply_property bq27000_battery_props[] = {
245 POWER_SUPPLY_PROP_STATUS,
246 POWER_SUPPLY_PROP_PRESENT,
247 POWER_SUPPLY_PROP_VOLTAGE_NOW,
248 POWER_SUPPLY_PROP_CURRENT_NOW,
249 POWER_SUPPLY_PROP_CAPACITY,
250 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
251 POWER_SUPPLY_PROP_TEMP,
252 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
253 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
254 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
255 POWER_SUPPLY_PROP_TECHNOLOGY,
256 POWER_SUPPLY_PROP_CHARGE_FULL,
257 POWER_SUPPLY_PROP_CHARGE_NOW,
258 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
259 POWER_SUPPLY_PROP_CYCLE_COUNT,
260 POWER_SUPPLY_PROP_ENERGY_NOW,
261 POWER_SUPPLY_PROP_POWER_AVG,
262 POWER_SUPPLY_PROP_HEALTH,
263 POWER_SUPPLY_PROP_MANUFACTURER,
266 static enum power_supply_property bq27010_battery_props[] = {
267 POWER_SUPPLY_PROP_STATUS,
268 POWER_SUPPLY_PROP_PRESENT,
269 POWER_SUPPLY_PROP_VOLTAGE_NOW,
270 POWER_SUPPLY_PROP_CURRENT_NOW,
271 POWER_SUPPLY_PROP_CAPACITY,
272 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
273 POWER_SUPPLY_PROP_TEMP,
274 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
275 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
276 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
277 POWER_SUPPLY_PROP_TECHNOLOGY,
278 POWER_SUPPLY_PROP_CHARGE_FULL,
279 POWER_SUPPLY_PROP_CHARGE_NOW,
280 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
281 POWER_SUPPLY_PROP_CYCLE_COUNT,
282 POWER_SUPPLY_PROP_HEALTH,
283 POWER_SUPPLY_PROP_MANUFACTURER,
286 static enum power_supply_property bq27500_battery_props[] = {
287 POWER_SUPPLY_PROP_STATUS,
288 POWER_SUPPLY_PROP_PRESENT,
289 POWER_SUPPLY_PROP_VOLTAGE_NOW,
290 POWER_SUPPLY_PROP_CURRENT_NOW,
291 POWER_SUPPLY_PROP_CAPACITY,
292 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
293 POWER_SUPPLY_PROP_TEMP,
294 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
295 POWER_SUPPLY_PROP_TECHNOLOGY,
296 POWER_SUPPLY_PROP_CHARGE_FULL,
297 POWER_SUPPLY_PROP_CHARGE_NOW,
298 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
299 POWER_SUPPLY_PROP_CYCLE_COUNT,
300 POWER_SUPPLY_PROP_HEALTH,
301 POWER_SUPPLY_PROP_MANUFACTURER,
304 static enum power_supply_property bq27530_battery_props[] = {
305 POWER_SUPPLY_PROP_STATUS,
306 POWER_SUPPLY_PROP_PRESENT,
307 POWER_SUPPLY_PROP_VOLTAGE_NOW,
308 POWER_SUPPLY_PROP_CURRENT_NOW,
309 POWER_SUPPLY_PROP_CAPACITY,
310 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
311 POWER_SUPPLY_PROP_TEMP,
312 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
313 POWER_SUPPLY_PROP_TECHNOLOGY,
314 POWER_SUPPLY_PROP_CHARGE_FULL,
315 POWER_SUPPLY_PROP_CHARGE_NOW,
316 POWER_SUPPLY_PROP_POWER_AVG,
317 POWER_SUPPLY_PROP_HEALTH,
318 POWER_SUPPLY_PROP_CYCLE_COUNT,
319 POWER_SUPPLY_PROP_MANUFACTURER,
322 static enum power_supply_property bq27541_battery_props[] = {
323 POWER_SUPPLY_PROP_STATUS,
324 POWER_SUPPLY_PROP_PRESENT,
325 POWER_SUPPLY_PROP_VOLTAGE_NOW,
326 POWER_SUPPLY_PROP_CURRENT_NOW,
327 POWER_SUPPLY_PROP_CAPACITY,
328 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
329 POWER_SUPPLY_PROP_TEMP,
330 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
331 POWER_SUPPLY_PROP_TECHNOLOGY,
332 POWER_SUPPLY_PROP_CHARGE_FULL,
333 POWER_SUPPLY_PROP_CHARGE_NOW,
334 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
335 POWER_SUPPLY_PROP_CYCLE_COUNT,
336 POWER_SUPPLY_PROP_POWER_AVG,
337 POWER_SUPPLY_PROP_HEALTH,
338 POWER_SUPPLY_PROP_MANUFACTURER,
341 static enum power_supply_property bq27545_battery_props[] = {
342 POWER_SUPPLY_PROP_STATUS,
343 POWER_SUPPLY_PROP_PRESENT,
344 POWER_SUPPLY_PROP_VOLTAGE_NOW,
345 POWER_SUPPLY_PROP_CURRENT_NOW,
346 POWER_SUPPLY_PROP_CAPACITY,
347 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
348 POWER_SUPPLY_PROP_TEMP,
349 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
350 POWER_SUPPLY_PROP_TECHNOLOGY,
351 POWER_SUPPLY_PROP_CHARGE_FULL,
352 POWER_SUPPLY_PROP_CHARGE_NOW,
353 POWER_SUPPLY_PROP_HEALTH,
354 POWER_SUPPLY_PROP_CYCLE_COUNT,
355 POWER_SUPPLY_PROP_POWER_AVG,
356 POWER_SUPPLY_PROP_MANUFACTURER,
359 static enum power_supply_property bq27421_battery_props[] = {
360 POWER_SUPPLY_PROP_STATUS,
361 POWER_SUPPLY_PROP_PRESENT,
362 POWER_SUPPLY_PROP_VOLTAGE_NOW,
363 POWER_SUPPLY_PROP_CURRENT_NOW,
364 POWER_SUPPLY_PROP_CAPACITY,
365 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
366 POWER_SUPPLY_PROP_TEMP,
367 POWER_SUPPLY_PROP_TECHNOLOGY,
368 POWER_SUPPLY_PROP_CHARGE_FULL,
369 POWER_SUPPLY_PROP_CHARGE_NOW,
370 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
371 POWER_SUPPLY_PROP_MANUFACTURER,
374 #define BQ27XXX_PROP(_id, _prop) \
375 [_id] = { \
376 .props = _prop, \
377 .size = ARRAY_SIZE(_prop), \
380 static struct {
381 enum power_supply_property *props;
382 size_t size;
383 } bq27xxx_battery_props[] = {
384 BQ27XXX_PROP(BQ27000, bq27000_battery_props),
385 BQ27XXX_PROP(BQ27010, bq27010_battery_props),
386 BQ27XXX_PROP(BQ27500, bq27500_battery_props),
387 BQ27XXX_PROP(BQ27530, bq27530_battery_props),
388 BQ27XXX_PROP(BQ27541, bq27541_battery_props),
389 BQ27XXX_PROP(BQ27545, bq27545_battery_props),
390 BQ27XXX_PROP(BQ27421, bq27421_battery_props),
393 static unsigned int poll_interval = 360;
394 module_param(poll_interval, uint, 0644);
395 MODULE_PARM_DESC(poll_interval,
396 "battery poll interval in seconds - 0 disables polling");
399 * Common code for BQ27xxx devices
402 static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
403 bool single)
405 /* Reports EINVAL for invalid/missing registers */
406 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
407 return -EINVAL;
409 return di->bus.read(di, di->regs[reg_index], single);
413 * Return the battery State-of-Charge
414 * Or < 0 if something fails.
416 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
418 int soc;
420 if (di->chip == BQ27000 || di->chip == BQ27010)
421 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
422 else
423 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
425 if (soc < 0)
426 dev_dbg(di->dev, "error reading State-of-Charge\n");
428 return soc;
432 * Return a battery charge value in µAh
433 * Or < 0 if something fails.
435 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
437 int charge;
439 charge = bq27xxx_read(di, reg, false);
440 if (charge < 0) {
441 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
442 reg, charge);
443 return charge;
446 if (di->chip == BQ27000 || di->chip == BQ27010)
447 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
448 else
449 charge *= 1000;
451 return charge;
455 * Return the battery Nominal available capacity in µAh
456 * Or < 0 if something fails.
458 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
460 int flags;
462 if (di->chip == BQ27000 || di->chip == BQ27010) {
463 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
464 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
465 return -ENODATA;
468 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
472 * Return the battery Full Charge Capacity in µAh
473 * Or < 0 if something fails.
475 static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
477 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
481 * Return the Design Capacity in µAh
482 * Or < 0 if something fails.
484 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
486 int dcap;
488 if (di->chip == BQ27000 || di->chip == BQ27010)
489 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
490 else
491 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
493 if (dcap < 0) {
494 dev_dbg(di->dev, "error reading initial last measured discharge\n");
495 return dcap;
498 if (di->chip == BQ27000 || di->chip == BQ27010)
499 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
500 else
501 dcap *= 1000;
503 return dcap;
507 * Return the battery Available energy in µWh
508 * Or < 0 if something fails.
510 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
512 int ae;
514 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
515 if (ae < 0) {
516 dev_dbg(di->dev, "error reading available energy\n");
517 return ae;
520 if (di->chip == BQ27000 || di->chip == BQ27010)
521 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
522 else
523 ae *= 1000;
525 return ae;
529 * Return the battery temperature in tenths of degree Kelvin
530 * Or < 0 if something fails.
532 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
534 int temp;
536 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
537 if (temp < 0) {
538 dev_err(di->dev, "error reading temperature\n");
539 return temp;
542 if (di->chip == BQ27000 || di->chip == BQ27010)
543 temp = 5 * temp / 2;
545 return temp;
549 * Return the battery Cycle count total
550 * Or < 0 if something fails.
552 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
554 int cyct;
556 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
557 if (cyct < 0)
558 dev_err(di->dev, "error reading cycle count total\n");
560 return cyct;
564 * Read a time register.
565 * Return < 0 if something fails.
567 static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
569 int tval;
571 tval = bq27xxx_read(di, reg, false);
572 if (tval < 0) {
573 dev_dbg(di->dev, "error reading time register %02x: %d\n",
574 reg, tval);
575 return tval;
578 if (tval == 65535)
579 return -ENODATA;
581 return tval * 60;
585 * Read an average power register.
586 * Return < 0 if something fails.
588 static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
590 int tval;
592 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
593 if (tval < 0) {
594 dev_err(di->dev, "error reading average power register %02x: %d\n",
595 BQ27XXX_REG_AP, tval);
596 return tval;
599 if (di->chip == BQ27000 || di->chip == BQ27010)
600 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
601 else
602 return tval;
606 * Returns true if a battery over temperature condition is detected
608 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
610 if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
611 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
612 if (di->chip == BQ27530 || di->chip == BQ27421)
613 return flags & BQ27XXX_FLAG_OT;
615 return false;
619 * Returns true if a battery under temperature condition is detected
621 static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
623 if (di->chip == BQ27530 || di->chip == BQ27421)
624 return flags & BQ27XXX_FLAG_UT;
626 return false;
630 * Returns true if a low state of charge condition is detected
632 static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
634 if (di->chip == BQ27000 || di->chip == BQ27010)
635 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
636 else
637 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
641 * Read flag register.
642 * Return < 0 if something fails.
644 static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
646 int flags;
648 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
649 if (flags < 0) {
650 dev_err(di->dev, "error reading flag register:%d\n", flags);
651 return flags;
654 /* Unlikely but important to return first */
655 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
656 return POWER_SUPPLY_HEALTH_OVERHEAT;
657 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
658 return POWER_SUPPLY_HEALTH_COLD;
659 if (unlikely(bq27xxx_battery_dead(di, flags)))
660 return POWER_SUPPLY_HEALTH_DEAD;
662 return POWER_SUPPLY_HEALTH_GOOD;
665 void bq27xxx_battery_update(struct bq27xxx_device_info *di)
667 struct bq27xxx_reg_cache cache = {0, };
668 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
669 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
671 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
672 if ((cache.flags & 0xff) == 0xff)
673 cache.flags = -1; /* read error */
674 if (cache.flags >= 0) {
675 cache.temperature = bq27xxx_battery_read_temperature(di);
676 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
677 dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
678 cache.capacity = -ENODATA;
679 cache.energy = -ENODATA;
680 cache.time_to_empty = -ENODATA;
681 cache.time_to_empty_avg = -ENODATA;
682 cache.time_to_full = -ENODATA;
683 cache.charge_full = -ENODATA;
684 cache.health = -ENODATA;
685 } else {
686 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
687 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
688 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
689 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
690 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
691 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
692 cache.charge_full = bq27xxx_battery_read_fcc(di);
693 cache.capacity = bq27xxx_battery_read_soc(di);
694 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
695 cache.energy = bq27xxx_battery_read_energy(di);
696 cache.health = bq27xxx_battery_read_health(di);
698 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
699 cache.cycle_count = bq27xxx_battery_read_cyct(di);
700 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
701 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
703 /* We only have to read charge design full once */
704 if (di->charge_design_full <= 0)
705 di->charge_design_full = bq27xxx_battery_read_dcap(di);
708 if (di->cache.capacity != cache.capacity)
709 power_supply_changed(di->bat);
711 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
712 di->cache = cache;
714 di->last_update = jiffies;
716 EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
718 static void bq27xxx_battery_poll(struct work_struct *work)
720 struct bq27xxx_device_info *di =
721 container_of(work, struct bq27xxx_device_info,
722 work.work);
724 bq27xxx_battery_update(di);
726 if (poll_interval > 0)
727 schedule_delayed_work(&di->work, poll_interval * HZ);
731 * Return the battery average current in µA
732 * Note that current can be negative signed as well
733 * Or 0 if something fails.
735 static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
736 union power_supply_propval *val)
738 int curr;
739 int flags;
741 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
742 if (curr < 0) {
743 dev_err(di->dev, "error reading current\n");
744 return curr;
747 if (di->chip == BQ27000 || di->chip == BQ27010) {
748 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
749 if (flags & BQ27000_FLAG_CHGS) {
750 dev_dbg(di->dev, "negative current!\n");
751 curr = -curr;
754 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
755 } else {
756 /* Other gauges return signed value */
757 val->intval = (int)((s16)curr) * 1000;
760 return 0;
763 static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
764 union power_supply_propval *val)
766 int status;
768 if (di->chip == BQ27000 || di->chip == BQ27010) {
769 if (di->cache.flags & BQ27000_FLAG_FC)
770 status = POWER_SUPPLY_STATUS_FULL;
771 else if (di->cache.flags & BQ27000_FLAG_CHGS)
772 status = POWER_SUPPLY_STATUS_CHARGING;
773 else if (power_supply_am_i_supplied(di->bat))
774 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
775 else
776 status = POWER_SUPPLY_STATUS_DISCHARGING;
777 } else {
778 if (di->cache.flags & BQ27XXX_FLAG_FC)
779 status = POWER_SUPPLY_STATUS_FULL;
780 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
781 status = POWER_SUPPLY_STATUS_DISCHARGING;
782 else
783 status = POWER_SUPPLY_STATUS_CHARGING;
786 val->intval = status;
788 return 0;
791 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
792 union power_supply_propval *val)
794 int level;
796 if (di->chip == BQ27000 || di->chip == BQ27010) {
797 if (di->cache.flags & BQ27000_FLAG_FC)
798 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
799 else if (di->cache.flags & BQ27000_FLAG_EDV1)
800 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
801 else if (di->cache.flags & BQ27000_FLAG_EDVF)
802 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
803 else
804 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
805 } else {
806 if (di->cache.flags & BQ27XXX_FLAG_FC)
807 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
808 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
809 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
810 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
811 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
812 else
813 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
816 val->intval = level;
818 return 0;
822 * Return the battery Voltage in millivolts
823 * Or < 0 if something fails.
825 static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
826 union power_supply_propval *val)
828 int volt;
830 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
831 if (volt < 0) {
832 dev_err(di->dev, "error reading voltage\n");
833 return volt;
836 val->intval = volt * 1000;
838 return 0;
841 static int bq27xxx_simple_value(int value,
842 union power_supply_propval *val)
844 if (value < 0)
845 return value;
847 val->intval = value;
849 return 0;
852 static int bq27xxx_battery_get_property(struct power_supply *psy,
853 enum power_supply_property psp,
854 union power_supply_propval *val)
856 int ret = 0;
857 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
859 mutex_lock(&di->lock);
860 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
861 cancel_delayed_work_sync(&di->work);
862 bq27xxx_battery_poll(&di->work.work);
864 mutex_unlock(&di->lock);
866 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
867 return -ENODEV;
869 switch (psp) {
870 case POWER_SUPPLY_PROP_STATUS:
871 ret = bq27xxx_battery_status(di, val);
872 break;
873 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
874 ret = bq27xxx_battery_voltage(di, val);
875 break;
876 case POWER_SUPPLY_PROP_PRESENT:
877 val->intval = di->cache.flags < 0 ? 0 : 1;
878 break;
879 case POWER_SUPPLY_PROP_CURRENT_NOW:
880 ret = bq27xxx_battery_current(di, val);
881 break;
882 case POWER_SUPPLY_PROP_CAPACITY:
883 ret = bq27xxx_simple_value(di->cache.capacity, val);
884 break;
885 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
886 ret = bq27xxx_battery_capacity_level(di, val);
887 break;
888 case POWER_SUPPLY_PROP_TEMP:
889 ret = bq27xxx_simple_value(di->cache.temperature, val);
890 if (ret == 0)
891 val->intval -= 2731; /* convert decidegree k to c */
892 break;
893 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
894 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
895 break;
896 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
897 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
898 break;
899 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
900 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
901 break;
902 case POWER_SUPPLY_PROP_TECHNOLOGY:
903 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
904 break;
905 case POWER_SUPPLY_PROP_CHARGE_NOW:
906 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
907 break;
908 case POWER_SUPPLY_PROP_CHARGE_FULL:
909 ret = bq27xxx_simple_value(di->cache.charge_full, val);
910 break;
911 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
912 ret = bq27xxx_simple_value(di->charge_design_full, val);
913 break;
914 case POWER_SUPPLY_PROP_CYCLE_COUNT:
915 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
916 break;
917 case POWER_SUPPLY_PROP_ENERGY_NOW:
918 ret = bq27xxx_simple_value(di->cache.energy, val);
919 break;
920 case POWER_SUPPLY_PROP_POWER_AVG:
921 ret = bq27xxx_simple_value(di->cache.power_avg, val);
922 break;
923 case POWER_SUPPLY_PROP_HEALTH:
924 ret = bq27xxx_simple_value(di->cache.health, val);
925 break;
926 case POWER_SUPPLY_PROP_MANUFACTURER:
927 val->strval = BQ27XXX_MANUFACTURER;
928 break;
929 default:
930 return -EINVAL;
933 return ret;
936 static void bq27xxx_external_power_changed(struct power_supply *psy)
938 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
940 cancel_delayed_work_sync(&di->work);
941 schedule_delayed_work(&di->work, 0);
944 int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
946 struct power_supply_desc *psy_desc;
947 struct power_supply_config psy_cfg = { .drv_data = di, };
949 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
950 mutex_init(&di->lock);
951 di->regs = bq27xxx_regs[di->chip];
953 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
954 if (!psy_desc)
955 return -ENOMEM;
957 psy_desc->name = di->name;
958 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
959 psy_desc->properties = bq27xxx_battery_props[di->chip].props;
960 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
961 psy_desc->get_property = bq27xxx_battery_get_property;
962 psy_desc->external_power_changed = bq27xxx_external_power_changed;
964 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
965 if (IS_ERR(di->bat)) {
966 dev_err(di->dev, "failed to register battery\n");
967 return PTR_ERR(di->bat);
970 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
972 bq27xxx_battery_update(di);
974 return 0;
976 EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
978 void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
981 * power_supply_unregister call bq27xxx_battery_get_property which
982 * call bq27xxx_battery_poll.
983 * Make sure that bq27xxx_battery_poll will not call
984 * schedule_delayed_work again after unregister (which cause OOPS).
986 poll_interval = 0;
988 cancel_delayed_work_sync(&di->work);
990 power_supply_unregister(di->bat);
992 mutex_destroy(&di->lock);
994 EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
996 static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
997 bool single)
999 struct device *dev = di->dev;
1000 struct bq27xxx_platform_data *pdata = dev->platform_data;
1001 unsigned int timeout = 3;
1002 int upper, lower;
1003 int temp;
1005 if (!single) {
1006 /* Make sure the value has not changed in between reading the
1007 * lower and the upper part */
1008 upper = pdata->read(dev, reg + 1);
1009 do {
1010 temp = upper;
1011 if (upper < 0)
1012 return upper;
1014 lower = pdata->read(dev, reg);
1015 if (lower < 0)
1016 return lower;
1018 upper = pdata->read(dev, reg + 1);
1019 } while (temp != upper && --timeout);
1021 if (timeout == 0)
1022 return -EIO;
1024 return (upper << 8) | lower;
1027 return pdata->read(dev, reg);
1030 static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
1032 struct bq27xxx_device_info *di;
1033 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
1035 if (!pdata) {
1036 dev_err(&pdev->dev, "no platform_data supplied\n");
1037 return -EINVAL;
1040 if (!pdata->read) {
1041 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1042 return -EINVAL;
1045 if (!pdata->chip) {
1046 dev_err(&pdev->dev, "no device supplied\n");
1047 return -EINVAL;
1050 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1051 if (!di)
1052 return -ENOMEM;
1054 platform_set_drvdata(pdev, di);
1056 di->dev = &pdev->dev;
1057 di->chip = pdata->chip;
1058 di->name = pdata->name ?: dev_name(&pdev->dev);
1059 di->bus.read = bq27xxx_battery_platform_read;
1061 return bq27xxx_battery_setup(di);
1064 static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
1066 struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
1068 bq27xxx_battery_teardown(di);
1070 return 0;
1073 static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
1074 { "bq27000-battery", },
1075 { /* sentinel */ }
1077 MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
1079 #ifdef CONFIG_OF
1080 static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
1081 { .compatible = "ti,bq27000" },
1084 MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
1085 #endif
1087 static struct platform_driver bq27xxx_battery_platform_driver = {
1088 .probe = bq27xxx_battery_platform_probe,
1089 .remove = bq27xxx_battery_platform_remove,
1090 .driver = {
1091 .name = "bq27000-battery",
1092 .of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
1094 .id_table = bq27xxx_battery_platform_id_table,
1096 module_platform_driver(bq27xxx_battery_platform_driver);
1098 MODULE_ALIAS("platform:bq27000-battery");
1100 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1101 MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
1102 MODULE_LICENSE("GPL");