xref: /linux/drivers/power/supply/bq27xxx_battery.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * BQ27xxx battery driver
3  *
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>
8  *
9  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
10  *
11  * This package is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  *
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-g1
26  * http://www.ti.com/product/bq27510-g2
27  * http://www.ti.com/product/bq27510-g3
28  * http://www.ti.com/product/bq27520-g4
29  * http://www.ti.com/product/bq27520-g1
30  * http://www.ti.com/product/bq27520-g2
31  * http://www.ti.com/product/bq27520-g3
32  * http://www.ti.com/product/bq27520-g4
33  * 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
41  * http://www.ti.com/product/bq27425-g1
42  * http://www.ti.com/product/bq27411-g1
43  * http://www.ti.com/product/bq27621-g1
44  */
45 
46 #include <linux/device.h>
47 #include <linux/module.h>
48 #include <linux/mutex.h>
49 #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>
55 #include <linux/slab.h>
56 #include <linux/of.h>
57 
58 #include <linux/power/bq27xxx_battery.h>
59 
60 #define DRIVER_VERSION		"1.2.0"
61 
62 #define BQ27XXX_MANUFACTURER	"Texas Instruments"
63 
64 /* 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)
71 #define BQ27XXX_FLAG_UT		BIT(14)
72 #define BQ27XXX_FLAG_OT		BIT(15)
73 
74 /* BQ27000 has different layout for Flags register */
75 #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 */
80 
81 #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 */
84 
85 #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  */
92 
93 enum 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 */
111 	BQ27XXX_REG_MAX,	/* sentinel */
112 };
113 
114 /* Register mappings */
115 static 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 	},
154 	[BQ2750X] = {
155 		[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 	},
173 	[BQ2751X] = {
174 		[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 	},
192 	[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 	},
211 	[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 	},
230 	[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 	},
249 	[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 	},
268 	[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 	},
287 	[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 	},
306 	[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 	},
325 	[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 	},
344 	[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 	},
420 };
421 
422 static enum power_supply_property bq27000_battery_props[] = {
423 	POWER_SUPPLY_PROP_STATUS,
424 	POWER_SUPPLY_PROP_PRESENT,
425 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
426 	POWER_SUPPLY_PROP_CURRENT_NOW,
427 	POWER_SUPPLY_PROP_CAPACITY,
428 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
429 	POWER_SUPPLY_PROP_TEMP,
430 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
431 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
432 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
433 	POWER_SUPPLY_PROP_TECHNOLOGY,
434 	POWER_SUPPLY_PROP_CHARGE_FULL,
435 	POWER_SUPPLY_PROP_CHARGE_NOW,
436 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
437 	POWER_SUPPLY_PROP_CYCLE_COUNT,
438 	POWER_SUPPLY_PROP_ENERGY_NOW,
439 	POWER_SUPPLY_PROP_POWER_AVG,
440 	POWER_SUPPLY_PROP_HEALTH,
441 	POWER_SUPPLY_PROP_MANUFACTURER,
442 };
443 
444 static enum power_supply_property bq27010_battery_props[] = {
445 	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,
452 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
453 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
454 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
455 	POWER_SUPPLY_PROP_TECHNOLOGY,
456 	POWER_SUPPLY_PROP_CHARGE_FULL,
457 	POWER_SUPPLY_PROP_CHARGE_NOW,
458 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
459 	POWER_SUPPLY_PROP_CYCLE_COUNT,
460 	POWER_SUPPLY_PROP_HEALTH,
461 	POWER_SUPPLY_PROP_MANUFACTURER,
462 };
463 
464 static enum power_supply_property bq2750x_battery_props[] = {
465 	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 
482 static enum power_supply_property bq2751x_battery_props[] = {
483 	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 
500 static 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 
521 static 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 
542 static 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 
563 static 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 
581 static 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 
601 static 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 
622 static 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 
642 static 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 
659 static 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 
677 static enum power_supply_property bq27541_battery_props[] = {
678 	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,
693 	POWER_SUPPLY_PROP_MANUFACTURER,
694 };
695 
696 static enum power_supply_property bq27545_battery_props[] = {
697 	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,
708 	POWER_SUPPLY_PROP_HEALTH,
709 	POWER_SUPPLY_PROP_CYCLE_COUNT,
710 	POWER_SUPPLY_PROP_POWER_AVG,
711 	POWER_SUPPLY_PROP_MANUFACTURER,
712 };
713 
714 static 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 
735 static 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),
741 	BQ27XXX_PROP(BQ2750X, bq2750x_battery_props),
742 	BQ27XXX_PROP(BQ2751X, bq2751x_battery_props),
743 	BQ27XXX_PROP(BQ27500, bq27500_battery_props),
744 	BQ27XXX_PROP(BQ27510G1, bq27510g1_battery_props),
745 	BQ27XXX_PROP(BQ27510G2, bq27510g2_battery_props),
746 	BQ27XXX_PROP(BQ27510G3, bq27510g3_battery_props),
747 	BQ27XXX_PROP(BQ27520G1, bq27520g1_battery_props),
748 	BQ27XXX_PROP(BQ27520G2, bq27520g2_battery_props),
749 	BQ27XXX_PROP(BQ27520G3, bq27520g3_battery_props),
750 	BQ27XXX_PROP(BQ27520G4, bq27520g4_battery_props),
751 	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 
757 static DEFINE_MUTEX(bq27xxx_list_lock);
758 static LIST_HEAD(bq27xxx_battery_devices);
759 
760 static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
761 {
762 	struct bq27xxx_device_info *di;
763 	unsigned int prev_val = *(unsigned int *) kp->arg;
764 	int ret;
765 
766 	ret = param_set_uint(val, kp);
767 	if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
768 		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 
780 static const struct kernel_param_ops param_ops_poll_interval = {
781 	.get = param_get_uint,
782 	.set = poll_interval_param_set,
783 };
784 
785 static unsigned int poll_interval = 360;
786 module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
787 MODULE_PARM_DESC(poll_interval,
788 		 "battery poll interval in seconds - 0 disables polling");
789 
790 /*
791  * Common code for BQ27xxx devices
792  */
793 
794 static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
795 			       bool single)
796 {
797 	/* Reports EINVAL for invalid/missing registers */
798 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
799 		return -EINVAL;
800 
801 	return di->bus.read(di, di->regs[reg_index], single);
802 }
803 
804 /*
805  * Return the battery State-of-Charge
806  * Or < 0 if something fails.
807  */
808 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
809 {
810 	int soc;
811 
812 	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);
816 
817 	if (soc < 0)
818 		dev_dbg(di->dev, "error reading State-of-Charge\n");
819 
820 	return soc;
821 }
822 
823 /*
824  * Return a battery charge value in µAh
825  * Or < 0 if something fails.
826  */
827 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
828 {
829 	int charge;
830 
831 	charge = bq27xxx_read(di, reg, false);
832 	if (charge < 0) {
833 		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
834 			reg, charge);
835 		return charge;
836 	}
837 
838 	if (di->chip == BQ27000 || di->chip == BQ27010)
839 		charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
840 	else
841 		charge *= 1000;
842 
843 	return charge;
844 }
845 
846 /*
847  * Return the battery Nominal available capacity in µAh
848  * Or < 0 if something fails.
849  */
850 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
851 {
852 	int flags;
853 
854 	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;
858 	}
859 
860 	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  */
867 static 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  */
876 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
877 {
878 	int dcap;
879 
880 	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);
884 
885 	if (dcap < 0) {
886 		dev_dbg(di->dev, "error reading initial last measured discharge\n");
887 		return dcap;
888 	}
889 
890 	if (di->chip == BQ27000 || di->chip == BQ27010)
891 		dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
892 	else
893 		dcap *= 1000;
894 
895 	return dcap;
896 }
897 
898 /*
899  * Return the battery Available energy in µWh
900  * Or < 0 if something fails.
901  */
902 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
903 {
904 	int ae;
905 
906 	ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
907 	if (ae < 0) {
908 		dev_dbg(di->dev, "error reading available energy\n");
909 		return ae;
910 	}
911 
912 	if (di->chip == BQ27000 || di->chip == BQ27010)
913 		ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
914 	else
915 		ae *= 1000;
916 
917 	return ae;
918 }
919 
920 /*
921  * Return the battery temperature in tenths of degree Kelvin
922  * Or < 0 if something fails.
923  */
924 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
925 {
926 	int temp;
927 
928 	temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
929 	if (temp < 0) {
930 		dev_err(di->dev, "error reading temperature\n");
931 		return temp;
932 	}
933 
934 	if (di->chip == BQ27000 || di->chip == BQ27010)
935 		temp = 5 * temp / 2;
936 
937 	return temp;
938 }
939 
940 /*
941  * Return the battery Cycle count total
942  * Or < 0 if something fails.
943  */
944 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
945 {
946 	int cyct;
947 
948 	cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
949 	if (cyct < 0)
950 		dev_err(di->dev, "error reading cycle count total\n");
951 
952 	return cyct;
953 }
954 
955 /*
956  * Read a time register.
957  * Return < 0 if something fails.
958  */
959 static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
960 {
961 	int tval;
962 
963 	tval = bq27xxx_read(di, reg, false);
964 	if (tval < 0) {
965 		dev_dbg(di->dev, "error reading time register %02x: %d\n",
966 			reg, tval);
967 		return tval;
968 	}
969 
970 	if (tval == 65535)
971 		return -ENODATA;
972 
973 	return tval * 60;
974 }
975 
976 /*
977  * Read an average power register.
978  * Return < 0 if something fails.
979  */
980 static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
981 {
982 	int tval;
983 
984 	tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
985 	if (tval < 0) {
986 		dev_err(di->dev, "error reading average power register  %02x: %d\n",
987 			BQ27XXX_REG_AP, tval);
988 		return tval;
989 	}
990 
991 	if (di->chip == BQ27000 || di->chip == BQ27010)
992 		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
993 	else
994 		return tval;
995 }
996 
997 /*
998  * Returns true if a battery over temperature condition is detected
999  */
1000 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
1001 {
1002 	switch (di->chip) {
1003 	case BQ2750X:
1004 	case BQ2751X:
1005 	case BQ27500:
1006 	case BQ27510G1:
1007 	case BQ27510G2:
1008 	case BQ27510G3:
1009 	case BQ27520G1:
1010 	case BQ27520G2:
1011 	case BQ27520G3:
1012 	case BQ27520G4:
1013 	case BQ27541:
1014 	case BQ27545:
1015 		return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
1016 	case BQ27530:
1017 	case BQ27421:
1018 		return flags & BQ27XXX_FLAG_OT;
1019 	default:
1020 		return false;
1021 	}
1022 }
1023 
1024 /*
1025  * Returns true if a battery under temperature condition is detected
1026  */
1027 static 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  */
1038 static 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);
1042 	else
1043 		return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
1044 }
1045 
1046 /*
1047  * Read flag register.
1048  * Return < 0 if something fails.
1049  */
1050 static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
1051 {
1052 	int flags;
1053 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
1054 
1055 	flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
1056 	if (flags < 0) {
1057 		dev_err(di->dev, "error reading flag register:%d\n", flags);
1058 		return flags;
1059 	}
1060 
1061 	/* 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;
1068 
1069 	return POWER_SUPPLY_HEALTH_GOOD;
1070 }
1071 
1072 void bq27xxx_battery_update(struct bq27xxx_device_info *di)
1073 {
1074 	struct bq27xxx_reg_cache cache = {0, };
1075 	bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
1076 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
1077 
1078 	cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
1079 	if ((cache.flags & 0xff) == 0xff)
1080 		cache.flags = -1; /* read error */
1081 	if (cache.flags >= 0) {
1082 		cache.temperature = bq27xxx_battery_read_temperature(di);
1083 		if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
1084 			dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
1085 			cache.capacity = -ENODATA;
1086 			cache.energy = -ENODATA;
1087 			cache.time_to_empty = -ENODATA;
1088 			cache.time_to_empty_avg = -ENODATA;
1089 			cache.time_to_full = -ENODATA;
1090 			cache.charge_full = -ENODATA;
1091 			cache.health = -ENODATA;
1092 		} else {
1093 			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);
1100 			cache.capacity = bq27xxx_battery_read_soc(di);
1101 			if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
1102 				cache.energy = bq27xxx_battery_read_energy(di);
1103 			cache.health = bq27xxx_battery_read_health(di);
1104 		}
1105 		if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
1106 			cache.cycle_count = bq27xxx_battery_read_cyct(di);
1107 		if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
1108 			cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
1109 
1110 		/* We only have to read charge design full once */
1111 		if (di->charge_design_full <= 0)
1112 			di->charge_design_full = bq27xxx_battery_read_dcap(di);
1113 	}
1114 
1115 	if (di->cache.capacity != cache.capacity)
1116 		power_supply_changed(di->bat);
1117 
1118 	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
1119 		di->cache = cache;
1120 
1121 	di->last_update = jiffies;
1122 }
1123 EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
1124 
1125 static void bq27xxx_battery_poll(struct work_struct *work)
1126 {
1127 	struct bq27xxx_device_info *di =
1128 			container_of(work, struct bq27xxx_device_info,
1129 				     work.work);
1130 
1131 	bq27xxx_battery_update(di);
1132 
1133 	if (poll_interval > 0)
1134 		schedule_delayed_work(&di->work, poll_interval * HZ);
1135 }
1136 
1137 /*
1138  * Return the battery average current in µA
1139  * Note that current can be negative signed as well
1140  * Or 0 if something fails.
1141  */
1142 static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
1143 				   union power_supply_propval *val)
1144 {
1145 	int curr;
1146 	int flags;
1147 
1148 	curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
1149 	if (curr < 0) {
1150 		dev_err(di->dev, "error reading current\n");
1151 		return curr;
1152 	}
1153 
1154 	if (di->chip == BQ27000 || di->chip == BQ27010) {
1155 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
1156 		if (flags & BQ27000_FLAG_CHGS) {
1157 			dev_dbg(di->dev, "negative current!\n");
1158 			curr = -curr;
1159 		}
1160 
1161 		val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1162 	} else {
1163 		/* Other gauges return signed value */
1164 		val->intval = (int)((s16)curr) * 1000;
1165 	}
1166 
1167 	return 0;
1168 }
1169 
1170 static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
1171 				  union power_supply_propval *val)
1172 {
1173 	int status;
1174 
1175 	if (di->chip == BQ27000 || di->chip == BQ27010) {
1176 		if (di->cache.flags & BQ27000_FLAG_FC)
1177 			status = POWER_SUPPLY_STATUS_FULL;
1178 		else if (di->cache.flags & BQ27000_FLAG_CHGS)
1179 			status = POWER_SUPPLY_STATUS_CHARGING;
1180 		else if (power_supply_am_i_supplied(di->bat))
1181 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1182 		else
1183 			status = POWER_SUPPLY_STATUS_DISCHARGING;
1184 	} 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;
1191 	}
1192 
1193 	val->intval = status;
1194 
1195 	return 0;
1196 }
1197 
1198 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
1199 					  union power_supply_propval *val)
1200 {
1201 	int level;
1202 
1203 	if (di->chip == BQ27000 || di->chip == BQ27010) {
1204 		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;
1212 	} 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;
1221 	}
1222 
1223 	val->intval = level;
1224 
1225 	return 0;
1226 }
1227 
1228 /*
1229  * Return the battery Voltage in millivolts
1230  * Or < 0 if something fails.
1231  */
1232 static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
1233 				   union power_supply_propval *val)
1234 {
1235 	int volt;
1236 
1237 	volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
1238 	if (volt < 0) {
1239 		dev_err(di->dev, "error reading voltage\n");
1240 		return volt;
1241 	}
1242 
1243 	val->intval = volt * 1000;
1244 
1245 	return 0;
1246 }
1247 
1248 static int bq27xxx_simple_value(int value,
1249 				union power_supply_propval *val)
1250 {
1251 	if (value < 0)
1252 		return value;
1253 
1254 	val->intval = value;
1255 
1256 	return 0;
1257 }
1258 
1259 static int bq27xxx_battery_get_property(struct power_supply *psy,
1260 					enum power_supply_property psp,
1261 					union power_supply_propval *val)
1262 {
1263 	int ret = 0;
1264 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
1265 
1266 	mutex_lock(&di->lock);
1267 	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
1268 		cancel_delayed_work_sync(&di->work);
1269 		bq27xxx_battery_poll(&di->work.work);
1270 	}
1271 	mutex_unlock(&di->lock);
1272 
1273 	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
1274 		return -ENODEV;
1275 
1276 	switch (psp) {
1277 	case POWER_SUPPLY_PROP_STATUS:
1278 		ret = bq27xxx_battery_status(di, val);
1279 		break;
1280 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1281 		ret = bq27xxx_battery_voltage(di, val);
1282 		break;
1283 	case POWER_SUPPLY_PROP_PRESENT:
1284 		val->intval = di->cache.flags < 0 ? 0 : 1;
1285 		break;
1286 	case POWER_SUPPLY_PROP_CURRENT_NOW:
1287 		ret = bq27xxx_battery_current(di, val);
1288 		break;
1289 	case POWER_SUPPLY_PROP_CAPACITY:
1290 		ret = bq27xxx_simple_value(di->cache.capacity, val);
1291 		break;
1292 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1293 		ret = bq27xxx_battery_capacity_level(di, val);
1294 		break;
1295 	case POWER_SUPPLY_PROP_TEMP:
1296 		ret = bq27xxx_simple_value(di->cache.temperature, val);
1297 		if (ret == 0)
1298 			val->intval -= 2731; /* convert decidegree k to c */
1299 		break;
1300 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
1301 		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
1302 		break;
1303 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
1304 		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
1305 		break;
1306 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
1307 		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
1308 		break;
1309 	case POWER_SUPPLY_PROP_TECHNOLOGY:
1310 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1311 		break;
1312 	case POWER_SUPPLY_PROP_CHARGE_NOW:
1313 		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
1314 		break;
1315 	case POWER_SUPPLY_PROP_CHARGE_FULL:
1316 		ret = bq27xxx_simple_value(di->cache.charge_full, val);
1317 		break;
1318 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1319 		ret = bq27xxx_simple_value(di->charge_design_full, val);
1320 		break;
1321 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
1322 		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
1323 		break;
1324 	case POWER_SUPPLY_PROP_ENERGY_NOW:
1325 		ret = bq27xxx_simple_value(di->cache.energy, val);
1326 		break;
1327 	case POWER_SUPPLY_PROP_POWER_AVG:
1328 		ret = bq27xxx_simple_value(di->cache.power_avg, val);
1329 		break;
1330 	case POWER_SUPPLY_PROP_HEALTH:
1331 		ret = bq27xxx_simple_value(di->cache.health, val);
1332 		break;
1333 	case POWER_SUPPLY_PROP_MANUFACTURER:
1334 		val->strval = BQ27XXX_MANUFACTURER;
1335 		break;
1336 	default:
1337 		return -EINVAL;
1338 	}
1339 
1340 	return ret;
1341 }
1342 
1343 static void bq27xxx_external_power_changed(struct power_supply *psy)
1344 {
1345 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
1346 
1347 	cancel_delayed_work_sync(&di->work);
1348 	schedule_delayed_work(&di->work, 0);
1349 }
1350 
1351 int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
1352 {
1353 	struct power_supply_desc *psy_desc;
1354 	struct power_supply_config psy_cfg = { .drv_data = di, };
1355 
1356 	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
1357 	mutex_init(&di->lock);
1358 	di->regs = bq27xxx_regs[di->chip];
1359 
1360 	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
1361 	if (!psy_desc)
1362 		return -ENOMEM;
1363 
1364 	psy_desc->name = di->name;
1365 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1366 	psy_desc->properties = bq27xxx_battery_props[di->chip].props;
1367 	psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
1368 	psy_desc->get_property = bq27xxx_battery_get_property;
1369 	psy_desc->external_power_changed = bq27xxx_external_power_changed;
1370 
1371 	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
1372 	if (IS_ERR(di->bat)) {
1373 		dev_err(di->dev, "failed to register battery\n");
1374 		return PTR_ERR(di->bat);
1375 	}
1376 
1377 	dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1378 
1379 	bq27xxx_battery_update(di);
1380 
1381 	mutex_lock(&bq27xxx_list_lock);
1382 	list_add(&di->list, &bq27xxx_battery_devices);
1383 	mutex_unlock(&bq27xxx_list_lock);
1384 
1385 	return 0;
1386 }
1387 EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
1388 
1389 void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
1390 {
1391 	/*
1392 	 * power_supply_unregister call bq27xxx_battery_get_property which
1393 	 * call bq27xxx_battery_poll.
1394 	 * Make sure that bq27xxx_battery_poll will not call
1395 	 * schedule_delayed_work again after unregister (which cause OOPS).
1396 	 */
1397 	poll_interval = 0;
1398 
1399 	cancel_delayed_work_sync(&di->work);
1400 
1401 	power_supply_unregister(di->bat);
1402 
1403 	mutex_lock(&bq27xxx_list_lock);
1404 	list_del(&di->list);
1405 	mutex_unlock(&bq27xxx_list_lock);
1406 
1407 	mutex_destroy(&di->lock);
1408 }
1409 EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
1410 
1411 static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1412 					 bool single)
1413 {
1414 	struct device *dev = di->dev;
1415 	struct bq27xxx_platform_data *pdata = dev->platform_data;
1416 	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 
1439 		return (upper << 8) | lower;
1440 	}
1441 
1442 	return pdata->read(dev, reg);
1443 }
1444 
1445 static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
1446 {
1447 	struct bq27xxx_device_info *di;
1448 	struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
1449 
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 
1460 	if (!pdata->chip) {
1461 		dev_err(&pdev->dev, "no device supplied\n");
1462 		return -EINVAL;
1463 	}
1464 
1465 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1466 	if (!di)
1467 		return -ENOMEM;
1468 
1469 	platform_set_drvdata(pdev, di);
1470 
1471 	di->dev = &pdev->dev;
1472 	di->chip = pdata->chip;
1473 	di->name = pdata->name ?: dev_name(&pdev->dev);
1474 	di->bus.read = bq27xxx_battery_platform_read;
1475 
1476 	return bq27xxx_battery_setup(di);
1477 }
1478 
1479 static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
1480 {
1481 	struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
1482 
1483 	bq27xxx_battery_teardown(di);
1484 
1485 	return 0;
1486 }
1487 
1488 static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
1489 	{ "bq27000-battery", },
1490 	{ /* sentinel */ }
1491 };
1492 MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
1493 
1494 #ifdef CONFIG_OF
1495 static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
1496 	{ .compatible = "ti,bq27000" },
1497 	{},
1498 };
1499 MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
1500 #endif
1501 
1502 static struct platform_driver bq27xxx_battery_platform_driver = {
1503 	.probe	= bq27xxx_battery_platform_probe,
1504 	.remove = bq27xxx_battery_platform_remove,
1505 	.driver = {
1506 		.name = "bq27000-battery",
1507 		.of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
1508 	},
1509 	.id_table = bq27xxx_battery_platform_id_table,
1510 };
1511 module_platform_driver(bq27xxx_battery_platform_driver);
1512 
1513 MODULE_ALIAS("platform:bq27000-battery");
1514 
1515 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1516 MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
1517 MODULE_LICENSE("GPL");
1518