blob: 398801a21b865eef7c0fb4e1e7bce8449e239f6a [file] [log] [blame]
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001/*
Andrew F. Davis081bab22015-09-22 14:35:06 -05002 * BQ27xxx battery driver
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07003 *
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02006 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
Pali Rohár73c244a2011-04-30 23:59:35 +02007 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07008 *
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 *
Pali Rohár631c17e2011-02-01 00:08:02 +010019 * Datasheets:
Andrew F. Davisd74534c2015-09-22 14:35:09 -050020 * 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
Chris Lapabd281772017-01-11 12:44:41 +110025 * http://www.ti.com/product/bq27510-g1
Chris Lapa698a2bf2017-01-11 12:44:42 +110026 * http://www.ti.com/product/bq27510-g2
Andrew F. Davisd74534c2015-09-22 14:35:09 -050027 * http://www.ti.com/product/bq27510-g3
28 * http://www.ti.com/product/bq27520-g4
Chris Lapa68f2a812017-01-11 12:44:44 +110029 * http://www.ti.com/product/bq27520-g1
Chris Lapaa5deb9a2017-01-11 12:44:45 +110030 * http://www.ti.com/product/bq27520-g2
Chris Lapa825e9152017-01-11 12:44:46 +110031 * http://www.ti.com/product/bq27520-g3
Chris Lapa8835cae2017-01-11 12:44:47 +110032 * http://www.ti.com/product/bq27520-g4
Andrew F. Davisd74534c2015-09-22 14:35:09 -050033 * http://www.ti.com/product/bq27530-g1
34 * http://www.ti.com/product/bq27531-g1
35 * http://www.ti.com/product/bq27541-g1
36 * http://www.ti.com/product/bq27542-g1
37 * http://www.ti.com/product/bq27546-g1
38 * http://www.ti.com/product/bq27742-g1
39 * http://www.ti.com/product/bq27545-g1
40 * http://www.ti.com/product/bq27421-g1
Saranya Gopala66f59b2012-07-13 20:20:07 -070041 * http://www.ti.com/product/bq27425-g1
Andrew F. Davisd74534c2015-09-22 14:35:09 -050042 * http://www.ti.com/product/bq27411-g1
43 * http://www.ti.com/product/bq27621-g1
Pali Rohár631c17e2011-02-01 00:08:02 +010044 */
45
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +053046#include <linux/device.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070047#include <linux/module.h>
Matt Ranostay1d727062016-09-19 20:43:02 -070048#include <linux/mutex.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070049#include <linux/param.h>
50#include <linux/jiffies.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/platform_device.h>
54#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Pali Rohár7df3a742016-02-21 12:28:22 +010056#include <linux/of.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070057
Andrew F. Davis081bab22015-09-22 14:35:06 -050058#include <linux/power/bq27xxx_battery.h>
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020059
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050060#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070061
Andrew F. Davisdb04fc52015-07-22 16:51:53 -050062#define BQ27XXX_MANUFACTURER "Texas Instruments"
63
Andrew F. Davisd74534c2015-09-22 14:35:09 -050064/* BQ27XXX Flags */
65#define BQ27XXX_FLAG_DSC BIT(0)
66#define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
67#define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
68#define BQ27XXX_FLAG_FC BIT(9)
69#define BQ27XXX_FLAG_OTD BIT(14)
70#define BQ27XXX_FLAG_OTC BIT(15)
Andrew F. Davis74aab842015-09-22 14:35:10 -050071#define BQ27XXX_FLAG_UT BIT(14)
72#define BQ27XXX_FLAG_OT BIT(15)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070073
Andrew F. Davisd74534c2015-09-22 14:35:09 -050074/* BQ27000 has different layout for Flags register */
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050075#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
76#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
77#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
78#define BQ27000_FLAG_FC BIT(5)
79#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020080
Andrew F. Davisd74534c2015-09-22 14:35:09 -050081#define BQ27XXX_RS (20) /* Resistor sense mOhm */
82#define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
83#define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
Pali Rohára2e51182010-05-24 20:52:13 +020084
Andrew F. Davisd74534c2015-09-22 14:35:09 -050085#define INVALID_REG_ADDR 0xff
86
87/*
88 * bq27xxx_reg_index - Register names
89 *
90 * These are indexes into a device's register mapping array.
91 */
Andrew F. Davis2e05b512016-06-17 17:02:35 -050092
Andrew F. Davisd74534c2015-09-22 14:35:09 -050093enum bq27xxx_reg_index {
94 BQ27XXX_REG_CTRL = 0, /* Control */
95 BQ27XXX_REG_TEMP, /* Temperature */
96 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
97 BQ27XXX_REG_VOLT, /* Voltage */
98 BQ27XXX_REG_AI, /* Average Current */
99 BQ27XXX_REG_FLAGS, /* Flags */
100 BQ27XXX_REG_TTE, /* Time-to-Empty */
101 BQ27XXX_REG_TTF, /* Time-to-Full */
102 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
103 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
104 BQ27XXX_REG_NAC, /* Nominal Available Capacity */
105 BQ27XXX_REG_FCC, /* Full Charge Capacity */
106 BQ27XXX_REG_CYCT, /* Cycle Count */
107 BQ27XXX_REG_AE, /* Available Energy */
108 BQ27XXX_REG_SOC, /* State-of-Charge */
109 BQ27XXX_REG_DCAP, /* Design Capacity */
110 BQ27XXX_REG_AP, /* Average Power */
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500111 BQ27XXX_REG_MAX, /* sentinel */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500112};
113
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500114/* Register mappings */
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500115static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = {
116 [BQ27000] = {
117 [BQ27XXX_REG_CTRL] = 0x00,
118 [BQ27XXX_REG_TEMP] = 0x06,
119 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
120 [BQ27XXX_REG_VOLT] = 0x08,
121 [BQ27XXX_REG_AI] = 0x14,
122 [BQ27XXX_REG_FLAGS] = 0x0a,
123 [BQ27XXX_REG_TTE] = 0x16,
124 [BQ27XXX_REG_TTF] = 0x18,
125 [BQ27XXX_REG_TTES] = 0x1c,
126 [BQ27XXX_REG_TTECP] = 0x26,
127 [BQ27XXX_REG_NAC] = 0x0c,
128 [BQ27XXX_REG_FCC] = 0x12,
129 [BQ27XXX_REG_CYCT] = 0x2a,
130 [BQ27XXX_REG_AE] = 0x22,
131 [BQ27XXX_REG_SOC] = 0x0b,
132 [BQ27XXX_REG_DCAP] = 0x76,
133 [BQ27XXX_REG_AP] = 0x24,
134 },
135 [BQ27010] = {
136 [BQ27XXX_REG_CTRL] = 0x00,
137 [BQ27XXX_REG_TEMP] = 0x06,
138 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
139 [BQ27XXX_REG_VOLT] = 0x08,
140 [BQ27XXX_REG_AI] = 0x14,
141 [BQ27XXX_REG_FLAGS] = 0x0a,
142 [BQ27XXX_REG_TTE] = 0x16,
143 [BQ27XXX_REG_TTF] = 0x18,
144 [BQ27XXX_REG_TTES] = 0x1c,
145 [BQ27XXX_REG_TTECP] = 0x26,
146 [BQ27XXX_REG_NAC] = 0x0c,
147 [BQ27XXX_REG_FCC] = 0x12,
148 [BQ27XXX_REG_CYCT] = 0x2a,
149 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
150 [BQ27XXX_REG_SOC] = 0x0b,
151 [BQ27XXX_REG_DCAP] = 0x76,
152 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
153 },
Chris Lapa818e3012017-01-11 12:44:38 +1100154 [BQ2750X] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500155 [BQ27XXX_REG_CTRL] = 0x00,
156 [BQ27XXX_REG_TEMP] = 0x06,
157 [BQ27XXX_REG_INT_TEMP] = 0x28,
158 [BQ27XXX_REG_VOLT] = 0x08,
159 [BQ27XXX_REG_AI] = 0x14,
160 [BQ27XXX_REG_FLAGS] = 0x0a,
161 [BQ27XXX_REG_TTE] = 0x16,
162 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
163 [BQ27XXX_REG_TTES] = 0x1a,
164 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
165 [BQ27XXX_REG_NAC] = 0x0c,
166 [BQ27XXX_REG_FCC] = 0x12,
167 [BQ27XXX_REG_CYCT] = 0x2a,
168 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
169 [BQ27XXX_REG_SOC] = 0x2c,
170 [BQ27XXX_REG_DCAP] = 0x3c,
171 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
172 },
Chris Lapa6da6e4b2017-01-11 12:44:39 +1100173 [BQ2751X] = {
Andrew F. Davis3bee9ea2016-11-04 13:33:13 -0500174 [BQ27XXX_REG_CTRL] = 0x00,
175 [BQ27XXX_REG_TEMP] = 0x06,
176 [BQ27XXX_REG_INT_TEMP] = 0x28,
177 [BQ27XXX_REG_VOLT] = 0x08,
178 [BQ27XXX_REG_AI] = 0x14,
179 [BQ27XXX_REG_FLAGS] = 0x0a,
180 [BQ27XXX_REG_TTE] = 0x16,
181 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
182 [BQ27XXX_REG_TTES] = 0x1a,
183 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
184 [BQ27XXX_REG_NAC] = 0x0c,
185 [BQ27XXX_REG_FCC] = 0x12,
186 [BQ27XXX_REG_CYCT] = 0x1e,
187 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
188 [BQ27XXX_REG_SOC] = 0x20,
189 [BQ27XXX_REG_DCAP] = 0x2e,
190 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
191 },
Chris Lapa32833632017-01-11 12:44:40 +1100192 [BQ27500] = {
193 [BQ27XXX_REG_CTRL] = 0x00,
194 [BQ27XXX_REG_TEMP] = 0x06,
195 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
196 [BQ27XXX_REG_VOLT] = 0x08,
197 [BQ27XXX_REG_AI] = 0x14,
198 [BQ27XXX_REG_FLAGS] = 0x0a,
199 [BQ27XXX_REG_TTE] = 0x16,
200 [BQ27XXX_REG_TTF] = 0x18,
201 [BQ27XXX_REG_TTES] = 0x1c,
202 [BQ27XXX_REG_TTECP] = 0x26,
203 [BQ27XXX_REG_NAC] = 0x0c,
204 [BQ27XXX_REG_FCC] = 0x12,
205 [BQ27XXX_REG_CYCT] = 0x2a,
206 [BQ27XXX_REG_AE] = 0x22,
207 [BQ27XXX_REG_SOC] = 0x2c,
208 [BQ27XXX_REG_DCAP] = 0x3c,
209 [BQ27XXX_REG_AP] = 0x24,
210 },
Chris Lapabd281772017-01-11 12:44:41 +1100211 [BQ27510G1] = {
212 [BQ27XXX_REG_CTRL] = 0x00,
213 [BQ27XXX_REG_TEMP] = 0x06,
214 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
215 [BQ27XXX_REG_VOLT] = 0x08,
216 [BQ27XXX_REG_AI] = 0x14,
217 [BQ27XXX_REG_FLAGS] = 0x0a,
218 [BQ27XXX_REG_TTE] = 0x16,
219 [BQ27XXX_REG_TTF] = 0x18,
220 [BQ27XXX_REG_TTES] = 0x1c,
221 [BQ27XXX_REG_TTECP] = 0x26,
222 [BQ27XXX_REG_NAC] = 0x0c,
223 [BQ27XXX_REG_FCC] = 0x12,
224 [BQ27XXX_REG_CYCT] = 0x2a,
225 [BQ27XXX_REG_AE] = 0x22,
226 [BQ27XXX_REG_SOC] = 0x2c,
227 [BQ27XXX_REG_DCAP] = 0x3c,
228 [BQ27XXX_REG_AP] = 0x24,
229 },
Chris Lapa698a2bf2017-01-11 12:44:42 +1100230 [BQ27510G2] = {
231 [BQ27XXX_REG_CTRL] = 0x00,
232 [BQ27XXX_REG_TEMP] = 0x06,
233 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
234 [BQ27XXX_REG_VOLT] = 0x08,
235 [BQ27XXX_REG_AI] = 0x14,
236 [BQ27XXX_REG_FLAGS] = 0x0a,
237 [BQ27XXX_REG_TTE] = 0x16,
238 [BQ27XXX_REG_TTF] = 0x18,
239 [BQ27XXX_REG_TTES] = 0x1c,
240 [BQ27XXX_REG_TTECP] = 0x26,
241 [BQ27XXX_REG_NAC] = 0x0c,
242 [BQ27XXX_REG_FCC] = 0x12,
243 [BQ27XXX_REG_CYCT] = 0x2a,
244 [BQ27XXX_REG_AE] = 0x22,
245 [BQ27XXX_REG_SOC] = 0x2c,
246 [BQ27XXX_REG_DCAP] = 0x3c,
247 [BQ27XXX_REG_AP] = 0x24,
248 },
Chris Lapa71375aa2017-01-11 12:44:43 +1100249 [BQ27510G3] = {
250 [BQ27XXX_REG_CTRL] = 0x00,
251 [BQ27XXX_REG_TEMP] = 0x06,
252 [BQ27XXX_REG_INT_TEMP] = 0x28,
253 [BQ27XXX_REG_VOLT] = 0x08,
254 [BQ27XXX_REG_AI] = 0x14,
255 [BQ27XXX_REG_FLAGS] = 0x0a,
256 [BQ27XXX_REG_TTE] = 0x16,
257 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
258 [BQ27XXX_REG_TTES] = 0x1a,
259 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
260 [BQ27XXX_REG_NAC] = 0x0c,
261 [BQ27XXX_REG_FCC] = 0x12,
262 [BQ27XXX_REG_CYCT] = 0x1e,
263 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
264 [BQ27XXX_REG_SOC] = 0x20,
265 [BQ27XXX_REG_DCAP] = 0x2e,
266 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
267 },
Chris Lapa68f2a812017-01-11 12:44:44 +1100268 [BQ27520G1] = {
269 [BQ27XXX_REG_CTRL] = 0x00,
270 [BQ27XXX_REG_TEMP] = 0x06,
271 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
272 [BQ27XXX_REG_VOLT] = 0x08,
273 [BQ27XXX_REG_AI] = 0x14,
274 [BQ27XXX_REG_FLAGS] = 0x0a,
275 [BQ27XXX_REG_TTE] = 0x16,
276 [BQ27XXX_REG_TTF] = 0x18,
277 [BQ27XXX_REG_TTES] = 0x1c,
278 [BQ27XXX_REG_TTECP] = 0x26,
279 [BQ27XXX_REG_NAC] = 0x0c,
280 [BQ27XXX_REG_FCC] = 0x12,
281 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
282 [BQ27XXX_REG_AE] = 0x22,
283 [BQ27XXX_REG_SOC] = 0x2c,
284 [BQ27XXX_REG_DCAP] = 0x3c,
285 [BQ27XXX_REG_AP] = 0x24,
286 },
Chris Lapaa5deb9a2017-01-11 12:44:45 +1100287 [BQ27520G2] = {
288 [BQ27XXX_REG_CTRL] = 0x00,
289 [BQ27XXX_REG_TEMP] = 0x06,
290 [BQ27XXX_REG_INT_TEMP] = 0x36,
291 [BQ27XXX_REG_VOLT] = 0x08,
292 [BQ27XXX_REG_AI] = 0x14,
293 [BQ27XXX_REG_FLAGS] = 0x0a,
294 [BQ27XXX_REG_TTE] = 0x16,
295 [BQ27XXX_REG_TTF] = 0x18,
296 [BQ27XXX_REG_TTES] = 0x1c,
297 [BQ27XXX_REG_TTECP] = 0x26,
298 [BQ27XXX_REG_NAC] = 0x0c,
299 [BQ27XXX_REG_FCC] = 0x12,
300 [BQ27XXX_REG_CYCT] = 0x2a,
301 [BQ27XXX_REG_AE] = 0x22,
302 [BQ27XXX_REG_SOC] = 0x2c,
303 [BQ27XXX_REG_DCAP] = 0x3c,
304 [BQ27XXX_REG_AP] = 0x24,
305 },
Chris Lapa825e9152017-01-11 12:44:46 +1100306 [BQ27520G3] = {
307 [BQ27XXX_REG_CTRL] = 0x00,
308 [BQ27XXX_REG_TEMP] = 0x06,
309 [BQ27XXX_REG_INT_TEMP] = 0x36,
310 [BQ27XXX_REG_VOLT] = 0x08,
311 [BQ27XXX_REG_AI] = 0x14,
312 [BQ27XXX_REG_FLAGS] = 0x0a,
313 [BQ27XXX_REG_TTE] = 0x16,
314 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
315 [BQ27XXX_REG_TTES] = 0x1c,
316 [BQ27XXX_REG_TTECP] = 0x26,
317 [BQ27XXX_REG_NAC] = 0x0c,
318 [BQ27XXX_REG_FCC] = 0x12,
319 [BQ27XXX_REG_CYCT] = 0x2a,
320 [BQ27XXX_REG_AE] = 0x22,
321 [BQ27XXX_REG_SOC] = 0x2c,
322 [BQ27XXX_REG_DCAP] = 0x3c,
323 [BQ27XXX_REG_AP] = 0x24,
324 },
Chris Lapa8835cae2017-01-11 12:44:47 +1100325 [BQ27520G4] = {
326 [BQ27XXX_REG_CTRL] = 0x00,
327 [BQ27XXX_REG_TEMP] = 0x06,
328 [BQ27XXX_REG_INT_TEMP] = 0x28,
329 [BQ27XXX_REG_VOLT] = 0x08,
330 [BQ27XXX_REG_AI] = 0x14,
331 [BQ27XXX_REG_FLAGS] = 0x0a,
332 [BQ27XXX_REG_TTE] = 0x16,
333 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
334 [BQ27XXX_REG_TTES] = 0x1c,
335 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
336 [BQ27XXX_REG_NAC] = 0x0c,
337 [BQ27XXX_REG_FCC] = 0x12,
338 [BQ27XXX_REG_CYCT] = 0x1e,
339 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
340 [BQ27XXX_REG_SOC] = 0x20,
341 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
342 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
343 },
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500344 [BQ27530] = {
345 [BQ27XXX_REG_CTRL] = 0x00,
346 [BQ27XXX_REG_TEMP] = 0x06,
347 [BQ27XXX_REG_INT_TEMP] = 0x32,
348 [BQ27XXX_REG_VOLT] = 0x08,
349 [BQ27XXX_REG_AI] = 0x14,
350 [BQ27XXX_REG_FLAGS] = 0x0a,
351 [BQ27XXX_REG_TTE] = 0x16,
352 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
353 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
354 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
355 [BQ27XXX_REG_NAC] = 0x0c,
356 [BQ27XXX_REG_FCC] = 0x12,
357 [BQ27XXX_REG_CYCT] = 0x2a,
358 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
359 [BQ27XXX_REG_SOC] = 0x2c,
360 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
361 [BQ27XXX_REG_AP] = 0x24,
362 },
363 [BQ27541] = {
364 [BQ27XXX_REG_CTRL] = 0x00,
365 [BQ27XXX_REG_TEMP] = 0x06,
366 [BQ27XXX_REG_INT_TEMP] = 0x28,
367 [BQ27XXX_REG_VOLT] = 0x08,
368 [BQ27XXX_REG_AI] = 0x14,
369 [BQ27XXX_REG_FLAGS] = 0x0a,
370 [BQ27XXX_REG_TTE] = 0x16,
371 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
372 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
373 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
374 [BQ27XXX_REG_NAC] = 0x0c,
375 [BQ27XXX_REG_FCC] = 0x12,
376 [BQ27XXX_REG_CYCT] = 0x2a,
377 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
378 [BQ27XXX_REG_SOC] = 0x2c,
379 [BQ27XXX_REG_DCAP] = 0x3c,
380 [BQ27XXX_REG_AP] = 0x24,
381 },
382 [BQ27545] = {
383 [BQ27XXX_REG_CTRL] = 0x00,
384 [BQ27XXX_REG_TEMP] = 0x06,
385 [BQ27XXX_REG_INT_TEMP] = 0x28,
386 [BQ27XXX_REG_VOLT] = 0x08,
387 [BQ27XXX_REG_AI] = 0x14,
388 [BQ27XXX_REG_FLAGS] = 0x0a,
389 [BQ27XXX_REG_TTE] = 0x16,
390 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
391 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
392 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
393 [BQ27XXX_REG_NAC] = 0x0c,
394 [BQ27XXX_REG_FCC] = 0x12,
395 [BQ27XXX_REG_CYCT] = 0x2a,
396 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
397 [BQ27XXX_REG_SOC] = 0x2c,
398 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
399 [BQ27XXX_REG_AP] = 0x24,
400 },
401 [BQ27421] = {
402 [BQ27XXX_REG_CTRL] = 0x00,
403 [BQ27XXX_REG_TEMP] = 0x02,
404 [BQ27XXX_REG_INT_TEMP] = 0x1e,
405 [BQ27XXX_REG_VOLT] = 0x04,
406 [BQ27XXX_REG_AI] = 0x10,
407 [BQ27XXX_REG_FLAGS] = 0x06,
408 [BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
409 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
410 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
411 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
412 [BQ27XXX_REG_NAC] = 0x08,
413 [BQ27XXX_REG_FCC] = 0x0e,
414 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
415 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
416 [BQ27XXX_REG_SOC] = 0x1c,
417 [BQ27XXX_REG_DCAP] = 0x3c,
418 [BQ27XXX_REG_AP] = 0x18,
419 },
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500420};
421
422static enum power_supply_property bq27000_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200423 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700424 POWER_SUPPLY_PROP_PRESENT,
425 POWER_SUPPLY_PROP_VOLTAGE_NOW,
426 POWER_SUPPLY_PROP_CURRENT_NOW,
427 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100428 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700429 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200430 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
431 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
432 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200433 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100434 POWER_SUPPLY_PROP_CHARGE_FULL,
435 POWER_SUPPLY_PROP_CHARGE_NOW,
436 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200437 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100438 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530439 POWER_SUPPLY_PROP_POWER_AVG,
440 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500441 POWER_SUPPLY_PROP_MANUFACTURER,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700442};
443
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500444static enum power_supply_property bq27010_battery_props[] = {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700445 POWER_SUPPLY_PROP_STATUS,
446 POWER_SUPPLY_PROP_PRESENT,
447 POWER_SUPPLY_PROP_VOLTAGE_NOW,
448 POWER_SUPPLY_PROP_CURRENT_NOW,
449 POWER_SUPPLY_PROP_CAPACITY,
450 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
451 POWER_SUPPLY_PROP_TEMP,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500452 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
453 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
454 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700455 POWER_SUPPLY_PROP_TECHNOLOGY,
456 POWER_SUPPLY_PROP_CHARGE_FULL,
457 POWER_SUPPLY_PROP_CHARGE_NOW,
458 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500459 POWER_SUPPLY_PROP_CYCLE_COUNT,
460 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500461 POWER_SUPPLY_PROP_MANUFACTURER,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700462};
463
Chris Lapa818e3012017-01-11 12:44:38 +1100464static enum power_supply_property bq2750x_battery_props[] = {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500465 POWER_SUPPLY_PROP_STATUS,
466 POWER_SUPPLY_PROP_PRESENT,
467 POWER_SUPPLY_PROP_VOLTAGE_NOW,
468 POWER_SUPPLY_PROP_CURRENT_NOW,
469 POWER_SUPPLY_PROP_CAPACITY,
470 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
471 POWER_SUPPLY_PROP_TEMP,
472 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
473 POWER_SUPPLY_PROP_TECHNOLOGY,
474 POWER_SUPPLY_PROP_CHARGE_FULL,
475 POWER_SUPPLY_PROP_CHARGE_NOW,
476 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
477 POWER_SUPPLY_PROP_CYCLE_COUNT,
478 POWER_SUPPLY_PROP_HEALTH,
479 POWER_SUPPLY_PROP_MANUFACTURER,
480};
481
Chris Lapa6da6e4b2017-01-11 12:44:39 +1100482static enum power_supply_property bq2751x_battery_props[] = {
Andrew F. Davis3bee9ea2016-11-04 13:33:13 -0500483 POWER_SUPPLY_PROP_STATUS,
484 POWER_SUPPLY_PROP_PRESENT,
485 POWER_SUPPLY_PROP_VOLTAGE_NOW,
486 POWER_SUPPLY_PROP_CURRENT_NOW,
487 POWER_SUPPLY_PROP_CAPACITY,
488 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
489 POWER_SUPPLY_PROP_TEMP,
490 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
491 POWER_SUPPLY_PROP_TECHNOLOGY,
492 POWER_SUPPLY_PROP_CHARGE_FULL,
493 POWER_SUPPLY_PROP_CHARGE_NOW,
494 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
495 POWER_SUPPLY_PROP_CYCLE_COUNT,
496 POWER_SUPPLY_PROP_HEALTH,
497 POWER_SUPPLY_PROP_MANUFACTURER,
498};
499
Chris Lapa32833632017-01-11 12:44:40 +1100500static enum power_supply_property bq27500_battery_props[] = {
501 POWER_SUPPLY_PROP_STATUS,
502 POWER_SUPPLY_PROP_PRESENT,
503 POWER_SUPPLY_PROP_VOLTAGE_NOW,
504 POWER_SUPPLY_PROP_CURRENT_NOW,
505 POWER_SUPPLY_PROP_CAPACITY,
506 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
507 POWER_SUPPLY_PROP_TEMP,
508 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
509 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
510 POWER_SUPPLY_PROP_TECHNOLOGY,
511 POWER_SUPPLY_PROP_CHARGE_FULL,
512 POWER_SUPPLY_PROP_CHARGE_NOW,
513 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
514 POWER_SUPPLY_PROP_CYCLE_COUNT,
515 POWER_SUPPLY_PROP_ENERGY_NOW,
516 POWER_SUPPLY_PROP_POWER_AVG,
517 POWER_SUPPLY_PROP_HEALTH,
518 POWER_SUPPLY_PROP_MANUFACTURER,
519};
520
Chris Lapabd281772017-01-11 12:44:41 +1100521static enum power_supply_property bq27510g1_battery_props[] = {
522 POWER_SUPPLY_PROP_STATUS,
523 POWER_SUPPLY_PROP_PRESENT,
524 POWER_SUPPLY_PROP_VOLTAGE_NOW,
525 POWER_SUPPLY_PROP_CURRENT_NOW,
526 POWER_SUPPLY_PROP_CAPACITY,
527 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
528 POWER_SUPPLY_PROP_TEMP,
529 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
530 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
531 POWER_SUPPLY_PROP_TECHNOLOGY,
532 POWER_SUPPLY_PROP_CHARGE_FULL,
533 POWER_SUPPLY_PROP_CHARGE_NOW,
534 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
535 POWER_SUPPLY_PROP_CYCLE_COUNT,
536 POWER_SUPPLY_PROP_ENERGY_NOW,
537 POWER_SUPPLY_PROP_POWER_AVG,
538 POWER_SUPPLY_PROP_HEALTH,
539 POWER_SUPPLY_PROP_MANUFACTURER,
540};
541
Chris Lapa698a2bf2017-01-11 12:44:42 +1100542static enum power_supply_property bq27510g2_battery_props[] = {
543 POWER_SUPPLY_PROP_STATUS,
544 POWER_SUPPLY_PROP_PRESENT,
545 POWER_SUPPLY_PROP_VOLTAGE_NOW,
546 POWER_SUPPLY_PROP_CURRENT_NOW,
547 POWER_SUPPLY_PROP_CAPACITY,
548 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
549 POWER_SUPPLY_PROP_TEMP,
550 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
551 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
552 POWER_SUPPLY_PROP_TECHNOLOGY,
553 POWER_SUPPLY_PROP_CHARGE_FULL,
554 POWER_SUPPLY_PROP_CHARGE_NOW,
555 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
556 POWER_SUPPLY_PROP_CYCLE_COUNT,
557 POWER_SUPPLY_PROP_ENERGY_NOW,
558 POWER_SUPPLY_PROP_POWER_AVG,
559 POWER_SUPPLY_PROP_HEALTH,
560 POWER_SUPPLY_PROP_MANUFACTURER,
561};
562
Chris Lapa71375aa2017-01-11 12:44:43 +1100563static enum power_supply_property bq27510g3_battery_props[] = {
564 POWER_SUPPLY_PROP_STATUS,
565 POWER_SUPPLY_PROP_PRESENT,
566 POWER_SUPPLY_PROP_VOLTAGE_NOW,
567 POWER_SUPPLY_PROP_CURRENT_NOW,
568 POWER_SUPPLY_PROP_CAPACITY,
569 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
570 POWER_SUPPLY_PROP_TEMP,
571 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
572 POWER_SUPPLY_PROP_TECHNOLOGY,
573 POWER_SUPPLY_PROP_CHARGE_FULL,
574 POWER_SUPPLY_PROP_CHARGE_NOW,
575 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
576 POWER_SUPPLY_PROP_CYCLE_COUNT,
577 POWER_SUPPLY_PROP_HEALTH,
578 POWER_SUPPLY_PROP_MANUFACTURER,
579};
580
Chris Lapa68f2a812017-01-11 12:44:44 +1100581static enum power_supply_property bq27520g1_battery_props[] = {
582 POWER_SUPPLY_PROP_STATUS,
583 POWER_SUPPLY_PROP_PRESENT,
584 POWER_SUPPLY_PROP_VOLTAGE_NOW,
585 POWER_SUPPLY_PROP_CURRENT_NOW,
586 POWER_SUPPLY_PROP_CAPACITY,
587 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
588 POWER_SUPPLY_PROP_TEMP,
589 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
590 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
591 POWER_SUPPLY_PROP_TECHNOLOGY,
592 POWER_SUPPLY_PROP_CHARGE_FULL,
593 POWER_SUPPLY_PROP_CHARGE_NOW,
594 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
595 POWER_SUPPLY_PROP_ENERGY_NOW,
596 POWER_SUPPLY_PROP_POWER_AVG,
597 POWER_SUPPLY_PROP_HEALTH,
598 POWER_SUPPLY_PROP_MANUFACTURER,
599};
600
Chris Lapaa5deb9a2017-01-11 12:44:45 +1100601static enum power_supply_property bq27520g2_battery_props[] = {
602 POWER_SUPPLY_PROP_STATUS,
603 POWER_SUPPLY_PROP_PRESENT,
604 POWER_SUPPLY_PROP_VOLTAGE_NOW,
605 POWER_SUPPLY_PROP_CURRENT_NOW,
606 POWER_SUPPLY_PROP_CAPACITY,
607 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
608 POWER_SUPPLY_PROP_TEMP,
609 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
610 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
611 POWER_SUPPLY_PROP_TECHNOLOGY,
612 POWER_SUPPLY_PROP_CHARGE_FULL,
613 POWER_SUPPLY_PROP_CHARGE_NOW,
614 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
615 POWER_SUPPLY_PROP_CYCLE_COUNT,
616 POWER_SUPPLY_PROP_ENERGY_NOW,
617 POWER_SUPPLY_PROP_POWER_AVG,
618 POWER_SUPPLY_PROP_HEALTH,
619 POWER_SUPPLY_PROP_MANUFACTURER,
620};
621
Chris Lapa825e9152017-01-11 12:44:46 +1100622static enum power_supply_property bq27520g3_battery_props[] = {
623 POWER_SUPPLY_PROP_STATUS,
624 POWER_SUPPLY_PROP_PRESENT,
625 POWER_SUPPLY_PROP_VOLTAGE_NOW,
626 POWER_SUPPLY_PROP_CURRENT_NOW,
627 POWER_SUPPLY_PROP_CAPACITY,
628 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
629 POWER_SUPPLY_PROP_TEMP,
630 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
631 POWER_SUPPLY_PROP_TECHNOLOGY,
632 POWER_SUPPLY_PROP_CHARGE_FULL,
633 POWER_SUPPLY_PROP_CHARGE_NOW,
634 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
635 POWER_SUPPLY_PROP_CYCLE_COUNT,
636 POWER_SUPPLY_PROP_ENERGY_NOW,
637 POWER_SUPPLY_PROP_POWER_AVG,
638 POWER_SUPPLY_PROP_HEALTH,
639 POWER_SUPPLY_PROP_MANUFACTURER,
640};
641
Chris Lapa8835cae2017-01-11 12:44:47 +1100642static enum power_supply_property bq27520g4_battery_props[] = {
643 POWER_SUPPLY_PROP_STATUS,
644 POWER_SUPPLY_PROP_PRESENT,
645 POWER_SUPPLY_PROP_VOLTAGE_NOW,
646 POWER_SUPPLY_PROP_CURRENT_NOW,
647 POWER_SUPPLY_PROP_CAPACITY,
648 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
649 POWER_SUPPLY_PROP_TEMP,
650 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
651 POWER_SUPPLY_PROP_TECHNOLOGY,
652 POWER_SUPPLY_PROP_CHARGE_FULL,
653 POWER_SUPPLY_PROP_CHARGE_NOW,
654 POWER_SUPPLY_PROP_CYCLE_COUNT,
655 POWER_SUPPLY_PROP_HEALTH,
656 POWER_SUPPLY_PROP_MANUFACTURER,
657};
658
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500659static enum power_supply_property bq27530_battery_props[] = {
660 POWER_SUPPLY_PROP_STATUS,
661 POWER_SUPPLY_PROP_PRESENT,
662 POWER_SUPPLY_PROP_VOLTAGE_NOW,
663 POWER_SUPPLY_PROP_CURRENT_NOW,
664 POWER_SUPPLY_PROP_CAPACITY,
665 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
666 POWER_SUPPLY_PROP_TEMP,
667 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
668 POWER_SUPPLY_PROP_TECHNOLOGY,
669 POWER_SUPPLY_PROP_CHARGE_FULL,
670 POWER_SUPPLY_PROP_CHARGE_NOW,
671 POWER_SUPPLY_PROP_POWER_AVG,
672 POWER_SUPPLY_PROP_HEALTH,
673 POWER_SUPPLY_PROP_CYCLE_COUNT,
674 POWER_SUPPLY_PROP_MANUFACTURER,
675};
676
677static enum power_supply_property bq27541_battery_props[] = {
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700678 POWER_SUPPLY_PROP_STATUS,
679 POWER_SUPPLY_PROP_PRESENT,
680 POWER_SUPPLY_PROP_VOLTAGE_NOW,
681 POWER_SUPPLY_PROP_CURRENT_NOW,
682 POWER_SUPPLY_PROP_CAPACITY,
683 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
684 POWER_SUPPLY_PROP_TEMP,
685 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
686 POWER_SUPPLY_PROP_TECHNOLOGY,
687 POWER_SUPPLY_PROP_CHARGE_FULL,
688 POWER_SUPPLY_PROP_CHARGE_NOW,
689 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
690 POWER_SUPPLY_PROP_CYCLE_COUNT,
691 POWER_SUPPLY_PROP_POWER_AVG,
692 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500693 POWER_SUPPLY_PROP_MANUFACTURER,
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700694};
695
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500696static enum power_supply_property bq27545_battery_props[] = {
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100697 POWER_SUPPLY_PROP_STATUS,
698 POWER_SUPPLY_PROP_PRESENT,
699 POWER_SUPPLY_PROP_VOLTAGE_NOW,
700 POWER_SUPPLY_PROP_CURRENT_NOW,
701 POWER_SUPPLY_PROP_CAPACITY,
702 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
703 POWER_SUPPLY_PROP_TEMP,
704 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
705 POWER_SUPPLY_PROP_TECHNOLOGY,
706 POWER_SUPPLY_PROP_CHARGE_FULL,
707 POWER_SUPPLY_PROP_CHARGE_NOW,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500708 POWER_SUPPLY_PROP_HEALTH,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100709 POWER_SUPPLY_PROP_CYCLE_COUNT,
710 POWER_SUPPLY_PROP_POWER_AVG,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500711 POWER_SUPPLY_PROP_MANUFACTURER,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100712};
713
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500714static enum power_supply_property bq27421_battery_props[] = {
715 POWER_SUPPLY_PROP_STATUS,
716 POWER_SUPPLY_PROP_PRESENT,
717 POWER_SUPPLY_PROP_VOLTAGE_NOW,
718 POWER_SUPPLY_PROP_CURRENT_NOW,
719 POWER_SUPPLY_PROP_CAPACITY,
720 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
721 POWER_SUPPLY_PROP_TEMP,
722 POWER_SUPPLY_PROP_TECHNOLOGY,
723 POWER_SUPPLY_PROP_CHARGE_FULL,
724 POWER_SUPPLY_PROP_CHARGE_NOW,
725 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
726 POWER_SUPPLY_PROP_MANUFACTURER,
727};
728
729#define BQ27XXX_PROP(_id, _prop) \
730 [_id] = { \
731 .props = _prop, \
732 .size = ARRAY_SIZE(_prop), \
733 }
734
735static struct {
736 enum power_supply_property *props;
737 size_t size;
738} bq27xxx_battery_props[] = {
739 BQ27XXX_PROP(BQ27000, bq27000_battery_props),
740 BQ27XXX_PROP(BQ27010, bq27010_battery_props),
Chris Lapa818e3012017-01-11 12:44:38 +1100741 BQ27XXX_PROP(BQ2750X, bq2750x_battery_props),
Chris Lapa6da6e4b2017-01-11 12:44:39 +1100742 BQ27XXX_PROP(BQ2751X, bq2751x_battery_props),
Chris Lapa32833632017-01-11 12:44:40 +1100743 BQ27XXX_PROP(BQ27500, bq27500_battery_props),
Chris Lapabd281772017-01-11 12:44:41 +1100744 BQ27XXX_PROP(BQ27510G1, bq27510g1_battery_props),
Chris Lapa698a2bf2017-01-11 12:44:42 +1100745 BQ27XXX_PROP(BQ27510G2, bq27510g2_battery_props),
Chris Lapa71375aa2017-01-11 12:44:43 +1100746 BQ27XXX_PROP(BQ27510G3, bq27510g3_battery_props),
Chris Lapa68f2a812017-01-11 12:44:44 +1100747 BQ27XXX_PROP(BQ27520G1, bq27520g1_battery_props),
Chris Lapaa5deb9a2017-01-11 12:44:45 +1100748 BQ27XXX_PROP(BQ27520G2, bq27520g2_battery_props),
Chris Lapa825e9152017-01-11 12:44:46 +1100749 BQ27XXX_PROP(BQ27520G3, bq27520g3_battery_props),
Chris Lapa8835cae2017-01-11 12:44:47 +1100750 BQ27XXX_PROP(BQ27520G4, bq27520g4_battery_props),
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500751 BQ27XXX_PROP(BQ27530, bq27530_battery_props),
752 BQ27XXX_PROP(BQ27541, bq27541_battery_props),
753 BQ27XXX_PROP(BQ27545, bq27545_battery_props),
754 BQ27XXX_PROP(BQ27421, bq27421_battery_props),
755};
756
Matt Ranostay1d727062016-09-19 20:43:02 -0700757static DEFINE_MUTEX(bq27xxx_list_lock);
758static LIST_HEAD(bq27xxx_battery_devices);
759
760static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
761{
762 struct bq27xxx_device_info *di;
Matt Ranostay950b6c22016-09-30 21:45:30 -0700763 unsigned int prev_val = *(unsigned int *) kp->arg;
Matt Ranostay1d727062016-09-19 20:43:02 -0700764 int ret;
765
766 ret = param_set_uint(val, kp);
Matt Ranostay950b6c22016-09-30 21:45:30 -0700767 if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
Matt Ranostay1d727062016-09-19 20:43:02 -0700768 return ret;
769
770 mutex_lock(&bq27xxx_list_lock);
771 list_for_each_entry(di, &bq27xxx_battery_devices, list) {
772 cancel_delayed_work_sync(&di->work);
773 schedule_delayed_work(&di->work, 0);
774 }
775 mutex_unlock(&bq27xxx_list_lock);
776
777 return ret;
778}
779
780static const struct kernel_param_ops param_ops_poll_interval = {
781 .get = param_get_uint,
782 .set = poll_interval_param_set,
783};
784
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100785static unsigned int poll_interval = 360;
Matt Ranostay1d727062016-09-19 20:43:02 -0700786module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500787MODULE_PARM_DESC(poll_interval,
788 "battery poll interval in seconds - 0 disables polling");
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100789
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700790/*
Andrew F. Davis081bab22015-09-22 14:35:06 -0500791 * Common code for BQ27xxx devices
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700792 */
793
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500794static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500795 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700796{
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500797 /* Reports EINVAL for invalid/missing registers */
798 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
799 return -EINVAL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100800
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500801 return di->bus.read(di, di->regs[reg_index], single);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700802}
803
804/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500805 * Return the battery State-of-Charge
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100806 * Or < 0 if something fails.
807 */
Andrew F. Davisc5709032015-09-22 14:35:08 -0500808static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100809{
Andrew F. Davisc5709032015-09-22 14:35:08 -0500810 int soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100811
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100812 if (di->chip == BQ27000 || di->chip == BQ27010)
813 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
814 else
815 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100816
Andrew F. Davisc5709032015-09-22 14:35:08 -0500817 if (soc < 0)
818 dev_dbg(di->dev, "error reading State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100819
Andrew F. Davisc5709032015-09-22 14:35:08 -0500820 return soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100821}
822
823/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100824 * Return a battery charge value in µAh
825 * Or < 0 if something fails.
826 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500827static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
Pali Rohár631c17e2011-02-01 00:08:02 +0100828{
829 int charge;
830
Andrew F. Davis081bab22015-09-22 14:35:06 -0500831 charge = bq27xxx_read(di, reg, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100832 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100833 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
834 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100835 return charge;
836 }
837
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500838 if (di->chip == BQ27000 || di->chip == BQ27010)
839 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100840 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500841 charge *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100842
843 return charge;
844}
845
846/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500847 * Return the battery Nominal available capacity in µAh
Pali Rohár631c17e2011-02-01 00:08:02 +0100848 * Or < 0 if something fails.
849 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500850static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100851{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100852 int flags;
Pali Roháre59ec4a2012-11-10 15:42:48 +0100853
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500854 if (di->chip == BQ27000 || di->chip == BQ27010) {
855 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
856 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
857 return -ENODATA;
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500858 }
Pali Rohár631c17e2011-02-01 00:08:02 +0100859
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500860 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
861}
862
863/*
864 * Return the battery Full Charge Capacity in µAh
865 * Or < 0 if something fails.
866 */
867static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
868{
869 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
870}
871
872/*
873 * Return the Design Capacity in µAh
874 * Or < 0 if something fails.
875 */
876static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
877{
878 int dcap;
879
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100880 if (di->chip == BQ27000 || di->chip == BQ27010)
881 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
882 else
883 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500884
885 if (dcap < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100886 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500887 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100888 }
889
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500890 if (di->chip == BQ27000 || di->chip == BQ27010)
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100891 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100892 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500893 dcap *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100894
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500895 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100896}
897
898/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100899 * Return the battery Available energy in µWh
900 * Or < 0 if something fails.
901 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500902static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
Pali Rohára8f6bd22011-11-01 01:43:06 +0100903{
904 int ae;
905
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500906 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
Pali Rohára8f6bd22011-11-01 01:43:06 +0100907 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100908 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100909 return ae;
910 }
911
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500912 if (di->chip == BQ27000 || di->chip == BQ27010)
913 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100914 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500915 ae *= 1000;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100916
917 return ae;
918}
919
920/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100921 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100922 * Or < 0 if something fails.
923 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500924static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
Pali Rohárd149e982011-11-01 01:43:07 +0100925{
926 int temp;
927
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500928 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
Pali Rohárd149e982011-11-01 01:43:07 +0100929 if (temp < 0) {
930 dev_err(di->dev, "error reading temperature\n");
931 return temp;
932 }
933
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500934 if (di->chip == BQ27000 || di->chip == BQ27010)
Pali Rohár5dc34432013-02-02 11:06:09 +0100935 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100936
937 return temp;
938}
939
940/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100941 * Return the battery Cycle count total
942 * Or < 0 if something fails.
943 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500944static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100945{
946 int cyct;
947
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500948 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100949 if (cyct < 0)
950 dev_err(di->dev, "error reading cycle count total\n");
951
952 return cyct;
953}
954
955/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100956 * Read a time register.
957 * Return < 0 if something fails.
958 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500959static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100960{
961 int tval;
962
Andrew F. Davis081bab22015-09-22 14:35:06 -0500963 tval = bq27xxx_read(di, reg, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100964 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100965 dev_dbg(di->dev, "error reading time register %02x: %d\n",
966 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100967 return tval;
968 }
969
970 if (tval == 65535)
971 return -ENODATA;
972
973 return tval * 60;
974}
975
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530976/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500977 * Read an average power register.
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530978 * Return < 0 if something fails.
979 */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500980static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530981{
982 int tval;
983
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500984 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530985 if (tval < 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500986 dev_err(di->dev, "error reading average power register %02x: %d\n",
987 BQ27XXX_REG_AP, tval);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530988 return tval;
989 }
990
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500991 if (di->chip == BQ27000 || di->chip == BQ27010)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500992 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500993 else
994 return tval;
995}
996
997/*
998 * Returns true if a battery over temperature condition is detected
999 */
Andrew F. Davis74aab842015-09-22 14:35:10 -05001000static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001001{
Chris Lapae839a442017-01-09 11:47:35 +11001002 switch (di->chip) {
Chris Lapa818e3012017-01-11 12:44:38 +11001003 case BQ2750X:
Chris Lapa6da6e4b2017-01-11 12:44:39 +11001004 case BQ2751X:
Chris Lapa32833632017-01-11 12:44:40 +11001005 case BQ27500:
Chris Lapabd281772017-01-11 12:44:41 +11001006 case BQ27510G1:
Chris Lapa698a2bf2017-01-11 12:44:42 +11001007 case BQ27510G2:
Chris Lapa71375aa2017-01-11 12:44:43 +11001008 case BQ27510G3:
Chris Lapa68f2a812017-01-11 12:44:44 +11001009 case BQ27520G1:
Chris Lapaa5deb9a2017-01-11 12:44:45 +11001010 case BQ27520G2:
Chris Lapa825e9152017-01-11 12:44:46 +11001011 case BQ27520G3:
Chris Lapa8835cae2017-01-11 12:44:47 +11001012 case BQ27520G4:
Chris Lapae839a442017-01-09 11:47:35 +11001013 case BQ27541:
1014 case BQ27545:
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001015 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
Chris Lapae839a442017-01-09 11:47:35 +11001016 case BQ27530:
1017 case BQ27421:
Andrew F. Davis74aab842015-09-22 14:35:10 -05001018 return flags & BQ27XXX_FLAG_OT;
Chris Lapae839a442017-01-09 11:47:35 +11001019 default:
1020 return false;
1021 }
Andrew F. Davis74aab842015-09-22 14:35:10 -05001022}
1023
1024/*
1025 * Returns true if a battery under temperature condition is detected
1026 */
1027static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
1028{
1029 if (di->chip == BQ27530 || di->chip == BQ27421)
1030 return flags & BQ27XXX_FLAG_UT;
1031
1032 return false;
1033}
1034
1035/*
1036 * Returns true if a low state of charge condition is detected
1037 */
1038static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
1039{
1040 if (di->chip == BQ27000 || di->chip == BQ27010)
1041 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001042 else
Andrew F. Davis74aab842015-09-22 14:35:10 -05001043 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301044}
1045
1046/*
1047 * Read flag register.
1048 * Return < 0 if something fails.
1049 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001050static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301051{
Andrzej Hajda2edd69a2015-09-28 10:51:27 +02001052 int flags;
H. Nikolaus Schallere4a404a2016-07-18 18:12:09 +02001053 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301054
H. Nikolaus Schallere4a404a2016-07-18 18:12:09 +02001055 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
Andrew F. Davis74aab842015-09-22 14:35:10 -05001056 if (flags < 0) {
1057 dev_err(di->dev, "error reading flag register:%d\n", flags);
1058 return flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301059 }
1060
Andrew F. Davis74aab842015-09-22 14:35:10 -05001061 /* Unlikely but important to return first */
1062 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
1063 return POWER_SUPPLY_HEALTH_OVERHEAT;
1064 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
1065 return POWER_SUPPLY_HEALTH_COLD;
1066 if (unlikely(bq27xxx_battery_dead(di, flags)))
1067 return POWER_SUPPLY_HEALTH_DEAD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301068
Andrew F. Davis74aab842015-09-22 14:35:10 -05001069 return POWER_SUPPLY_HEALTH_GOOD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301070}
1071
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001072void bq27xxx_battery_update(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001073{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001074 struct bq27xxx_reg_cache cache = {0, };
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001075 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
1076 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001077
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001078 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
Marek Belisko3dd843e2014-06-30 21:49:56 +02001079 if ((cache.flags & 0xff) == 0xff)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001080 cache.flags = -1; /* read error */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001081 if (cache.flags >= 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001082 cache.temperature = bq27xxx_battery_read_temperature(di);
1083 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
NeilBrown1f94b252015-10-07 20:42:38 +02001084 dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +01001085 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +01001086 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +01001087 cache.time_to_empty = -ENODATA;
1088 cache.time_to_empty_avg = -ENODATA;
1089 cache.time_to_full = -ENODATA;
1090 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301091 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +01001092 } else {
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001093 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
1094 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
1095 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
1096 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
1097 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
1098 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
1099 cache.charge_full = bq27xxx_battery_read_fcc(di);
Andrew F. Davisc5709032015-09-22 14:35:08 -05001100 cache.capacity = bq27xxx_battery_read_soc(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001101 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001102 cache.energy = bq27xxx_battery_read_energy(di);
Andrew F. Davis081bab22015-09-22 14:35:06 -05001103 cache.health = bq27xxx_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +01001104 }
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001105 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001106 cache.cycle_count = bq27xxx_battery_read_cyct(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001107 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
1108 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001109
Pali Rohár631c17e2011-02-01 00:08:02 +01001110 /* We only have to read charge design full once */
1111 if (di->charge_design_full <= 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001112 di->charge_design_full = bq27xxx_battery_read_dcap(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001113 }
1114
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -07001115 if (di->cache.capacity != cache.capacity)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001116 power_supply_changed(di->bat);
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -07001117
1118 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
1119 di->cache = cache;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001120
1121 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001122}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001123EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001124
Andrew F. Davis081bab22015-09-22 14:35:06 -05001125static void bq27xxx_battery_poll(struct work_struct *work)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001126{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001127 struct bq27xxx_device_info *di =
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001128 container_of(work, struct bq27xxx_device_info,
1129 work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001130
Andrew F. Davis081bab22015-09-22 14:35:06 -05001131 bq27xxx_battery_update(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001132
Thomas Gleixner53bf8372016-07-04 09:50:31 +00001133 if (poll_interval > 0)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001134 schedule_delayed_work(&di->work, poll_interval * HZ);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001135}
1136
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001137/*
Pali Rohárbf7d4142011-02-01 00:23:13 +01001138 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001139 * Note that current can be negative signed as well
1140 * Or 0 if something fails.
1141 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001142static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001143 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001144{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001145 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +01001146 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001147
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001148 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001149 if (curr < 0) {
1150 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001151 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001152 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001153
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001154 if (di->chip == BQ27000 || di->chip == BQ27010) {
H. Nikolaus Schallere4a404a2016-07-18 18:12:09 +02001155 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
Pali Rohárb68f6212011-11-01 01:43:03 +01001156 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001157 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +02001158 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001159 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001160
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001161 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1162 } else {
1163 /* Other gauges return signed value */
1164 val->intval = (int)((s16)curr) * 1000;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001165 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001166
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001167 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001168}
1169
Andrew F. Davis081bab22015-09-22 14:35:06 -05001170static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001171 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001172{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001173 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001174
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001175 if (di->chip == BQ27000 || di->chip == BQ27010) {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +02001176 if (di->cache.flags & BQ27000_FLAG_FC)
1177 status = POWER_SUPPLY_STATUS_FULL;
1178 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001179 status = POWER_SUPPLY_STATUS_CHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001180 else if (power_supply_am_i_supplied(di->bat))
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +02001181 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001182 else
1183 status = POWER_SUPPLY_STATUS_DISCHARGING;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001184 } else {
1185 if (di->cache.flags & BQ27XXX_FLAG_FC)
1186 status = POWER_SUPPLY_STATUS_FULL;
1187 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
1188 status = POWER_SUPPLY_STATUS_DISCHARGING;
1189 else
1190 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001191 }
1192
1193 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001194
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001195 return 0;
1196}
1197
Andrew F. Davis081bab22015-09-22 14:35:06 -05001198static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001199 union power_supply_propval *val)
Pali Rohárd66bab32011-11-01 01:43:04 +01001200{
1201 int level;
1202
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001203 if (di->chip == BQ27000 || di->chip == BQ27010) {
Pali Rohárd66bab32011-11-01 01:43:04 +01001204 if (di->cache.flags & BQ27000_FLAG_FC)
1205 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1206 else if (di->cache.flags & BQ27000_FLAG_EDV1)
1207 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1208 else if (di->cache.flags & BQ27000_FLAG_EDVF)
1209 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1210 else
1211 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001212 } else {
1213 if (di->cache.flags & BQ27XXX_FLAG_FC)
1214 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1215 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
1216 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1217 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
1218 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1219 else
1220 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Pali Rohárd66bab32011-11-01 01:43:04 +01001221 }
1222
1223 val->intval = level;
1224
1225 return 0;
1226}
1227
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001228/*
Thomas Weber91fe4d52012-02-17 17:46:21 +01001229 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001230 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001231 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001232static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001233 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001234{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001235 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001236
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001237 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001238 if (volt < 0) {
1239 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001240 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001241 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001242
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001243 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001244
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001245 return 0;
1246}
1247
Andrew F. Davis081bab22015-09-22 14:35:06 -05001248static int bq27xxx_simple_value(int value,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001249 union power_supply_propval *val)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001250{
1251 if (value < 0)
1252 return value;
1253
1254 val->intval = value;
1255
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001256 return 0;
1257}
1258
Andrew F. Davis081bab22015-09-22 14:35:06 -05001259static int bq27xxx_battery_get_property(struct power_supply *psy,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001260 enum power_supply_property psp,
1261 union power_supply_propval *val)
1262{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001263 int ret = 0;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001264 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +02001265
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001266 mutex_lock(&di->lock);
1267 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
1268 cancel_delayed_work_sync(&di->work);
Andrew F. Davis081bab22015-09-22 14:35:06 -05001269 bq27xxx_battery_poll(&di->work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001270 }
1271 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001272
1273 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +02001274 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001275
1276 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001277 case POWER_SUPPLY_PROP_STATUS:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001278 ret = bq27xxx_battery_status(di, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001279 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001280 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001281 ret = bq27xxx_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +02001282 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001283 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001284 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001285 break;
1286 case POWER_SUPPLY_PROP_CURRENT_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001287 ret = bq27xxx_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001288 break;
1289 case POWER_SUPPLY_PROP_CAPACITY:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001290 ret = bq27xxx_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001291 break;
Pali Rohárd66bab32011-11-01 01:43:04 +01001292 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001293 ret = bq27xxx_battery_capacity_level(di, val);
Pali Rohárd66bab32011-11-01 01:43:04 +01001294 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001295 case POWER_SUPPLY_PROP_TEMP:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001296 ret = bq27xxx_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +01001297 if (ret == 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001298 val->intval -= 2731; /* convert decidegree k to c */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001299 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001300 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001301 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001302 break;
1303 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001304 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001305 break;
1306 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001307 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001308 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +02001309 case POWER_SUPPLY_PROP_TECHNOLOGY:
1310 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1311 break;
Pali Rohár631c17e2011-02-01 00:08:02 +01001312 case POWER_SUPPLY_PROP_CHARGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001313 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001314 break;
1315 case POWER_SUPPLY_PROP_CHARGE_FULL:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001316 ret = bq27xxx_simple_value(di->cache.charge_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001317 break;
1318 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001319 ret = bq27xxx_simple_value(di->charge_design_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001320 break;
Pali Rohár73c244a2011-04-30 23:59:35 +02001321 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001322 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001323 break;
1324 case POWER_SUPPLY_PROP_ENERGY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001325 ret = bq27xxx_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001326 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301327 case POWER_SUPPLY_PROP_POWER_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001328 ret = bq27xxx_simple_value(di->cache.power_avg, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301329 break;
1330 case POWER_SUPPLY_PROP_HEALTH:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001331 ret = bq27xxx_simple_value(di->cache.health, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301332 break;
Andrew F. Davisdb04fc52015-07-22 16:51:53 -05001333 case POWER_SUPPLY_PROP_MANUFACTURER:
1334 val->strval = BQ27XXX_MANUFACTURER;
1335 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001336 default:
1337 return -EINVAL;
1338 }
1339
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001340 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001341}
1342
Andrew F. Davis081bab22015-09-22 14:35:06 -05001343static void bq27xxx_external_power_changed(struct power_supply *psy)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001344{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001345 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001346
1347 cancel_delayed_work_sync(&di->work);
1348 schedule_delayed_work(&di->work, 0);
1349}
1350
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001351int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001352{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001353 struct power_supply_desc *psy_desc;
1354 struct power_supply_config psy_cfg = { .drv_data = di, };
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001355
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001356 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
1357 mutex_init(&di->lock);
1358 di->regs = bq27xxx_regs[di->chip];
1359
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001360 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
1361 if (!psy_desc)
1362 return -ENOMEM;
1363
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001364 psy_desc->name = di->name;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001365 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001366 psy_desc->properties = bq27xxx_battery_props[di->chip].props;
1367 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001368 psy_desc->get_property = bq27xxx_battery_get_property;
1369 psy_desc->external_power_changed = bq27xxx_external_power_changed;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001370
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001371 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
1372 if (IS_ERR(di->bat)) {
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001373 dev_err(di->dev, "failed to register battery\n");
1374 return PTR_ERR(di->bat);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001375 }
1376
1377 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1378
Andrew F. Davis081bab22015-09-22 14:35:06 -05001379 bq27xxx_battery_update(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001380
Matt Ranostay1d727062016-09-19 20:43:02 -07001381 mutex_lock(&bq27xxx_list_lock);
1382 list_add(&di->list, &bq27xxx_battery_devices);
1383 mutex_unlock(&bq27xxx_list_lock);
1384
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001385 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001386}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001387EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001388
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001389void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001390{
Pali Rohár8cfaaa82011-11-01 01:43:11 +01001391 /*
Andrew F. Davis081bab22015-09-22 14:35:06 -05001392 * power_supply_unregister call bq27xxx_battery_get_property which
1393 * call bq27xxx_battery_poll.
1394 * Make sure that bq27xxx_battery_poll will not call
Pali Rohár8cfaaa82011-11-01 01:43:11 +01001395 * schedule_delayed_work again after unregister (which cause OOPS).
1396 */
1397 poll_interval = 0;
1398
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001399 cancel_delayed_work_sync(&di->work);
1400
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001401 power_supply_unregister(di->bat);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001402
Matt Ranostay1d727062016-09-19 20:43:02 -07001403 mutex_lock(&bq27xxx_list_lock);
1404 list_del(&di->list);
1405 mutex_unlock(&bq27xxx_list_lock);
1406
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001407 mutex_destroy(&di->lock);
1408}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001409EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001410
Andrew F. Davis081bab22015-09-22 14:35:06 -05001411static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1412 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001413{
1414 struct device *dev = di->dev;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001415 struct bq27xxx_platform_data *pdata = dev->platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001416 unsigned int timeout = 3;
1417 int upper, lower;
1418 int temp;
1419
1420 if (!single) {
1421 /* Make sure the value has not changed in between reading the
1422 * lower and the upper part */
1423 upper = pdata->read(dev, reg + 1);
1424 do {
1425 temp = upper;
1426 if (upper < 0)
1427 return upper;
1428
1429 lower = pdata->read(dev, reg);
1430 if (lower < 0)
1431 return lower;
1432
1433 upper = pdata->read(dev, reg + 1);
1434 } while (temp != upper && --timeout);
1435
1436 if (timeout == 0)
1437 return -EIO;
1438
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001439 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001440 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001441
1442 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001443}
1444
Andrew F. Davis081bab22015-09-22 14:35:06 -05001445static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001446{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001447 struct bq27xxx_device_info *di;
1448 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001449
1450 if (!pdata) {
1451 dev_err(&pdev->dev, "no platform_data supplied\n");
1452 return -EINVAL;
1453 }
1454
1455 if (!pdata->read) {
1456 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1457 return -EINVAL;
1458 }
1459
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001460 if (!pdata->chip) {
1461 dev_err(&pdev->dev, "no device supplied\n");
1462 return -EINVAL;
1463 }
1464
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301465 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001466 if (!di)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001467 return -ENOMEM;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001468
1469 platform_set_drvdata(pdev, di);
1470
1471 di->dev = &pdev->dev;
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001472 di->chip = pdata->chip;
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001473 di->name = pdata->name ?: dev_name(&pdev->dev);
1474 di->bus.read = bq27xxx_battery_platform_read;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001475
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001476 return bq27xxx_battery_setup(di);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001477}
1478
Andrew F. Davis081bab22015-09-22 14:35:06 -05001479static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001480{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001481 struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001482
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001483 bq27xxx_battery_teardown(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001484
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001485 return 0;
1486}
1487
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001488static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
1489 { "bq27000-battery", },
1490 { /* sentinel */ }
1491};
1492MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
1493
Pali Rohár7df3a742016-02-21 12:28:22 +01001494#ifdef CONFIG_OF
1495static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
1496 { .compatible = "ti,bq27000" },
1497 {},
1498};
1499MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
1500#endif
1501
Andrew F. Davis081bab22015-09-22 14:35:06 -05001502static struct platform_driver bq27xxx_battery_platform_driver = {
1503 .probe = bq27xxx_battery_platform_probe,
1504 .remove = bq27xxx_battery_platform_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001505 .driver = {
1506 .name = "bq27000-battery",
Pali Rohár7df3a742016-02-21 12:28:22 +01001507 .of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001508 },
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001509 .id_table = bq27xxx_battery_platform_id_table,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001510};
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001511module_platform_driver(bq27xxx_battery_platform_driver);
Marek Belisko8ebb7e92015-04-30 22:05:58 +02001512
Pali Rohár7df3a742016-02-21 12:28:22 +01001513MODULE_ALIAS("platform:bq27000-battery");
1514
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001515MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
Andrew F. Davis081bab22015-09-22 14:35:06 -05001516MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001517MODULE_LICENSE("GPL");