• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-g3
26  * http://www.ti.com/product/bq27520-g4
27  * http://www.ti.com/product/bq27530-g1
28  * http://www.ti.com/product/bq27531-g1
29  * http://www.ti.com/product/bq27541-g1
30  * http://www.ti.com/product/bq27542-g1
31  * http://www.ti.com/product/bq27546-g1
32  * http://www.ti.com/product/bq27742-g1
33  * http://www.ti.com/product/bq27545-g1
34  * http://www.ti.com/product/bq27421-g1
35  * http://www.ti.com/product/bq27425-g1
36  * http://www.ti.com/product/bq27411-g1
37  * http://www.ti.com/product/bq27621-g1
38  */
39 
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/mutex.h>
43 #include <linux/param.h>
44 #include <linux/jiffies.h>
45 #include <linux/workqueue.h>
46 #include <linux/delay.h>
47 #include <linux/platform_device.h>
48 #include <linux/power_supply.h>
49 #include <linux/slab.h>
50 #include <linux/of.h>
51 
52 #include <linux/power/bq27xxx_battery.h>
53 
54 #define DRIVER_VERSION		"1.2.0"
55 
56 #define BQ27XXX_MANUFACTURER	"Texas Instruments"
57 
58 /* BQ27XXX Flags */
59 #define BQ27XXX_FLAG_DSC	BIT(0)
60 #define BQ27XXX_FLAG_SOCF	BIT(1) /* State-of-Charge threshold final */
61 #define BQ27XXX_FLAG_SOC1	BIT(2) /* State-of-Charge threshold 1 */
62 #define BQ27XXX_FLAG_FC		BIT(9)
63 #define BQ27XXX_FLAG_OTD	BIT(14)
64 #define BQ27XXX_FLAG_OTC	BIT(15)
65 #define BQ27XXX_FLAG_UT		BIT(14)
66 #define BQ27XXX_FLAG_OT		BIT(15)
67 
68 /* BQ27000 has different layout for Flags register */
69 #define BQ27000_FLAG_EDVF	BIT(0) /* Final End-of-Discharge-Voltage flag */
70 #define BQ27000_FLAG_EDV1	BIT(1) /* First End-of-Discharge-Voltage flag */
71 #define BQ27000_FLAG_CI		BIT(4) /* Capacity Inaccurate flag */
72 #define BQ27000_FLAG_FC		BIT(5)
73 #define BQ27000_FLAG_CHGS	BIT(7) /* Charge state flag */
74 
75 #define BQ27XXX_RS			(20) /* Resistor sense mOhm */
76 #define BQ27XXX_POWER_CONSTANT		(29200) /* 29.2 µV^2 * 1000 */
77 #define BQ27XXX_CURRENT_CONSTANT	(3570) /* 3.57 µV * 1000 */
78 
79 #define INVALID_REG_ADDR	0xff
80 
81 /*
82  * bq27xxx_reg_index - Register names
83  *
84  * These are indexes into a device's register mapping array.
85  */
86 
87 enum bq27xxx_reg_index {
88 	BQ27XXX_REG_CTRL = 0,	/* Control */
89 	BQ27XXX_REG_TEMP,	/* Temperature */
90 	BQ27XXX_REG_INT_TEMP,	/* Internal Temperature */
91 	BQ27XXX_REG_VOLT,	/* Voltage */
92 	BQ27XXX_REG_AI,		/* Average Current */
93 	BQ27XXX_REG_FLAGS,	/* Flags */
94 	BQ27XXX_REG_TTE,	/* Time-to-Empty */
95 	BQ27XXX_REG_TTF,	/* Time-to-Full */
96 	BQ27XXX_REG_TTES,	/* Time-to-Empty Standby */
97 	BQ27XXX_REG_TTECP,	/* Time-to-Empty at Constant Power */
98 	BQ27XXX_REG_NAC,	/* Nominal Available Capacity */
99 	BQ27XXX_REG_FCC,	/* Full Charge Capacity */
100 	BQ27XXX_REG_CYCT,	/* Cycle Count */
101 	BQ27XXX_REG_AE,		/* Available Energy */
102 	BQ27XXX_REG_SOC,	/* State-of-Charge */
103 	BQ27XXX_REG_DCAP,	/* Design Capacity */
104 	BQ27XXX_REG_AP,		/* Average Power */
105 	BQ27XXX_REG_MAX,	/* sentinel */
106 };
107 
108 /* Register mappings */
109 static u8 bq27xxx_regs[][BQ27XXX_REG_MAX] = {
110 	[BQ27000] = {
111 		[BQ27XXX_REG_CTRL] = 0x00,
112 		[BQ27XXX_REG_TEMP] = 0x06,
113 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
114 		[BQ27XXX_REG_VOLT] = 0x08,
115 		[BQ27XXX_REG_AI] = 0x14,
116 		[BQ27XXX_REG_FLAGS] = 0x0a,
117 		[BQ27XXX_REG_TTE] = 0x16,
118 		[BQ27XXX_REG_TTF] = 0x18,
119 		[BQ27XXX_REG_TTES] = 0x1c,
120 		[BQ27XXX_REG_TTECP] = 0x26,
121 		[BQ27XXX_REG_NAC] = 0x0c,
122 		[BQ27XXX_REG_FCC] = 0x12,
123 		[BQ27XXX_REG_CYCT] = 0x2a,
124 		[BQ27XXX_REG_AE] = 0x22,
125 		[BQ27XXX_REG_SOC] = 0x0b,
126 		[BQ27XXX_REG_DCAP] = 0x76,
127 		[BQ27XXX_REG_AP] = 0x24,
128 	},
129 	[BQ27010] = {
130 		[BQ27XXX_REG_CTRL] = 0x00,
131 		[BQ27XXX_REG_TEMP] = 0x06,
132 		[BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
133 		[BQ27XXX_REG_VOLT] = 0x08,
134 		[BQ27XXX_REG_AI] = 0x14,
135 		[BQ27XXX_REG_FLAGS] = 0x0a,
136 		[BQ27XXX_REG_TTE] = 0x16,
137 		[BQ27XXX_REG_TTF] = 0x18,
138 		[BQ27XXX_REG_TTES] = 0x1c,
139 		[BQ27XXX_REG_TTECP] = 0x26,
140 		[BQ27XXX_REG_NAC] = 0x0c,
141 		[BQ27XXX_REG_FCC] = 0x12,
142 		[BQ27XXX_REG_CYCT] = 0x2a,
143 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
144 		[BQ27XXX_REG_SOC] = 0x0b,
145 		[BQ27XXX_REG_DCAP] = 0x76,
146 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
147 	},
148 	[BQ27500] = {
149 		[BQ27XXX_REG_CTRL] = 0x00,
150 		[BQ27XXX_REG_TEMP] = 0x06,
151 		[BQ27XXX_REG_INT_TEMP] = 0x28,
152 		[BQ27XXX_REG_VOLT] = 0x08,
153 		[BQ27XXX_REG_AI] = 0x14,
154 		[BQ27XXX_REG_FLAGS] = 0x0a,
155 		[BQ27XXX_REG_TTE] = 0x16,
156 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
157 		[BQ27XXX_REG_TTES] = 0x1a,
158 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
159 		[BQ27XXX_REG_NAC] = 0x0c,
160 		[BQ27XXX_REG_FCC] = 0x12,
161 		[BQ27XXX_REG_CYCT] = 0x2a,
162 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
163 		[BQ27XXX_REG_SOC] = 0x2c,
164 		[BQ27XXX_REG_DCAP] = 0x3c,
165 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
166 	},
167 	[BQ27510] = {
168 		[BQ27XXX_REG_CTRL] = 0x00,
169 		[BQ27XXX_REG_TEMP] = 0x06,
170 		[BQ27XXX_REG_INT_TEMP] = 0x28,
171 		[BQ27XXX_REG_VOLT] = 0x08,
172 		[BQ27XXX_REG_AI] = 0x14,
173 		[BQ27XXX_REG_FLAGS] = 0x0a,
174 		[BQ27XXX_REG_TTE] = 0x16,
175 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
176 		[BQ27XXX_REG_TTES] = 0x1a,
177 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
178 		[BQ27XXX_REG_NAC] = 0x0c,
179 		[BQ27XXX_REG_FCC] = 0x12,
180 		[BQ27XXX_REG_CYCT] = 0x1e,
181 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
182 		[BQ27XXX_REG_SOC] = 0x20,
183 		[BQ27XXX_REG_DCAP] = 0x2e,
184 		[BQ27XXX_REG_AP] = INVALID_REG_ADDR,
185 	},
186 	[BQ27530] = {
187 		[BQ27XXX_REG_CTRL] = 0x00,
188 		[BQ27XXX_REG_TEMP] = 0x06,
189 		[BQ27XXX_REG_INT_TEMP] = 0x32,
190 		[BQ27XXX_REG_VOLT] = 0x08,
191 		[BQ27XXX_REG_AI] = 0x14,
192 		[BQ27XXX_REG_FLAGS] = 0x0a,
193 		[BQ27XXX_REG_TTE] = 0x16,
194 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
195 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
196 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
197 		[BQ27XXX_REG_NAC] = 0x0c,
198 		[BQ27XXX_REG_FCC] = 0x12,
199 		[BQ27XXX_REG_CYCT] = 0x2a,
200 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
201 		[BQ27XXX_REG_SOC] = 0x2c,
202 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
203 		[BQ27XXX_REG_AP] = 0x24,
204 	},
205 	[BQ27541] = {
206 		[BQ27XXX_REG_CTRL] = 0x00,
207 		[BQ27XXX_REG_TEMP] = 0x06,
208 		[BQ27XXX_REG_INT_TEMP] = 0x28,
209 		[BQ27XXX_REG_VOLT] = 0x08,
210 		[BQ27XXX_REG_AI] = 0x14,
211 		[BQ27XXX_REG_FLAGS] = 0x0a,
212 		[BQ27XXX_REG_TTE] = 0x16,
213 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
214 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
215 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
216 		[BQ27XXX_REG_NAC] = 0x0c,
217 		[BQ27XXX_REG_FCC] = 0x12,
218 		[BQ27XXX_REG_CYCT] = 0x2a,
219 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
220 		[BQ27XXX_REG_SOC] = 0x2c,
221 		[BQ27XXX_REG_DCAP] = 0x3c,
222 		[BQ27XXX_REG_AP] = 0x24,
223 	},
224 	[BQ27545] = {
225 		[BQ27XXX_REG_CTRL] = 0x00,
226 		[BQ27XXX_REG_TEMP] = 0x06,
227 		[BQ27XXX_REG_INT_TEMP] = 0x28,
228 		[BQ27XXX_REG_VOLT] = 0x08,
229 		[BQ27XXX_REG_AI] = 0x14,
230 		[BQ27XXX_REG_FLAGS] = 0x0a,
231 		[BQ27XXX_REG_TTE] = 0x16,
232 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
233 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
234 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
235 		[BQ27XXX_REG_NAC] = 0x0c,
236 		[BQ27XXX_REG_FCC] = 0x12,
237 		[BQ27XXX_REG_CYCT] = 0x2a,
238 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
239 		[BQ27XXX_REG_SOC] = 0x2c,
240 		[BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
241 		[BQ27XXX_REG_AP] = 0x24,
242 	},
243 	[BQ27421] = {
244 		[BQ27XXX_REG_CTRL] = 0x00,
245 		[BQ27XXX_REG_TEMP] = 0x02,
246 		[BQ27XXX_REG_INT_TEMP] = 0x1e,
247 		[BQ27XXX_REG_VOLT] = 0x04,
248 		[BQ27XXX_REG_AI] = 0x10,
249 		[BQ27XXX_REG_FLAGS] = 0x06,
250 		[BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
251 		[BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
252 		[BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
253 		[BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
254 		[BQ27XXX_REG_NAC] = 0x08,
255 		[BQ27XXX_REG_FCC] = 0x0e,
256 		[BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
257 		[BQ27XXX_REG_AE] = INVALID_REG_ADDR,
258 		[BQ27XXX_REG_SOC] = 0x1c,
259 		[BQ27XXX_REG_DCAP] = 0x3c,
260 		[BQ27XXX_REG_AP] = 0x18,
261 	},
262 };
263 
264 static enum power_supply_property bq27000_battery_props[] = {
265 	POWER_SUPPLY_PROP_STATUS,
266 	POWER_SUPPLY_PROP_PRESENT,
267 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
268 	POWER_SUPPLY_PROP_CURRENT_NOW,
269 	POWER_SUPPLY_PROP_CAPACITY,
270 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
271 	POWER_SUPPLY_PROP_TEMP,
272 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
273 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
274 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
275 	POWER_SUPPLY_PROP_TECHNOLOGY,
276 	POWER_SUPPLY_PROP_CHARGE_FULL,
277 	POWER_SUPPLY_PROP_CHARGE_NOW,
278 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
279 	POWER_SUPPLY_PROP_CYCLE_COUNT,
280 	POWER_SUPPLY_PROP_ENERGY_NOW,
281 	POWER_SUPPLY_PROP_POWER_AVG,
282 	POWER_SUPPLY_PROP_HEALTH,
283 	POWER_SUPPLY_PROP_MANUFACTURER,
284 };
285 
286 static enum power_supply_property bq27010_battery_props[] = {
287 	POWER_SUPPLY_PROP_STATUS,
288 	POWER_SUPPLY_PROP_PRESENT,
289 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
290 	POWER_SUPPLY_PROP_CURRENT_NOW,
291 	POWER_SUPPLY_PROP_CAPACITY,
292 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
293 	POWER_SUPPLY_PROP_TEMP,
294 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
295 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
296 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
297 	POWER_SUPPLY_PROP_TECHNOLOGY,
298 	POWER_SUPPLY_PROP_CHARGE_FULL,
299 	POWER_SUPPLY_PROP_CHARGE_NOW,
300 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
301 	POWER_SUPPLY_PROP_CYCLE_COUNT,
302 	POWER_SUPPLY_PROP_HEALTH,
303 	POWER_SUPPLY_PROP_MANUFACTURER,
304 };
305 
306 static enum power_supply_property bq27500_battery_props[] = {
307 	POWER_SUPPLY_PROP_STATUS,
308 	POWER_SUPPLY_PROP_PRESENT,
309 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
310 	POWER_SUPPLY_PROP_CURRENT_NOW,
311 	POWER_SUPPLY_PROP_CAPACITY,
312 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
313 	POWER_SUPPLY_PROP_TEMP,
314 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
315 	POWER_SUPPLY_PROP_TECHNOLOGY,
316 	POWER_SUPPLY_PROP_CHARGE_FULL,
317 	POWER_SUPPLY_PROP_CHARGE_NOW,
318 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
319 	POWER_SUPPLY_PROP_CYCLE_COUNT,
320 	POWER_SUPPLY_PROP_HEALTH,
321 	POWER_SUPPLY_PROP_MANUFACTURER,
322 };
323 
324 static enum power_supply_property bq27510_battery_props[] = {
325 	POWER_SUPPLY_PROP_STATUS,
326 	POWER_SUPPLY_PROP_PRESENT,
327 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
328 	POWER_SUPPLY_PROP_CURRENT_NOW,
329 	POWER_SUPPLY_PROP_CAPACITY,
330 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
331 	POWER_SUPPLY_PROP_TEMP,
332 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
333 	POWER_SUPPLY_PROP_TECHNOLOGY,
334 	POWER_SUPPLY_PROP_CHARGE_FULL,
335 	POWER_SUPPLY_PROP_CHARGE_NOW,
336 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
337 	POWER_SUPPLY_PROP_CYCLE_COUNT,
338 	POWER_SUPPLY_PROP_HEALTH,
339 	POWER_SUPPLY_PROP_MANUFACTURER,
340 };
341 
342 static enum power_supply_property bq27530_battery_props[] = {
343 	POWER_SUPPLY_PROP_STATUS,
344 	POWER_SUPPLY_PROP_PRESENT,
345 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
346 	POWER_SUPPLY_PROP_CURRENT_NOW,
347 	POWER_SUPPLY_PROP_CAPACITY,
348 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
349 	POWER_SUPPLY_PROP_TEMP,
350 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
351 	POWER_SUPPLY_PROP_TECHNOLOGY,
352 	POWER_SUPPLY_PROP_CHARGE_FULL,
353 	POWER_SUPPLY_PROP_CHARGE_NOW,
354 	POWER_SUPPLY_PROP_POWER_AVG,
355 	POWER_SUPPLY_PROP_HEALTH,
356 	POWER_SUPPLY_PROP_CYCLE_COUNT,
357 	POWER_SUPPLY_PROP_MANUFACTURER,
358 };
359 
360 static enum power_supply_property bq27541_battery_props[] = {
361 	POWER_SUPPLY_PROP_STATUS,
362 	POWER_SUPPLY_PROP_PRESENT,
363 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
364 	POWER_SUPPLY_PROP_CURRENT_NOW,
365 	POWER_SUPPLY_PROP_CAPACITY,
366 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
367 	POWER_SUPPLY_PROP_TEMP,
368 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
369 	POWER_SUPPLY_PROP_TECHNOLOGY,
370 	POWER_SUPPLY_PROP_CHARGE_FULL,
371 	POWER_SUPPLY_PROP_CHARGE_NOW,
372 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
373 	POWER_SUPPLY_PROP_CYCLE_COUNT,
374 	POWER_SUPPLY_PROP_POWER_AVG,
375 	POWER_SUPPLY_PROP_HEALTH,
376 	POWER_SUPPLY_PROP_MANUFACTURER,
377 };
378 
379 static enum power_supply_property bq27545_battery_props[] = {
380 	POWER_SUPPLY_PROP_STATUS,
381 	POWER_SUPPLY_PROP_PRESENT,
382 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
383 	POWER_SUPPLY_PROP_CURRENT_NOW,
384 	POWER_SUPPLY_PROP_CAPACITY,
385 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
386 	POWER_SUPPLY_PROP_TEMP,
387 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
388 	POWER_SUPPLY_PROP_TECHNOLOGY,
389 	POWER_SUPPLY_PROP_CHARGE_FULL,
390 	POWER_SUPPLY_PROP_CHARGE_NOW,
391 	POWER_SUPPLY_PROP_HEALTH,
392 	POWER_SUPPLY_PROP_CYCLE_COUNT,
393 	POWER_SUPPLY_PROP_POWER_AVG,
394 	POWER_SUPPLY_PROP_MANUFACTURER,
395 };
396 
397 static enum power_supply_property bq27421_battery_props[] = {
398 	POWER_SUPPLY_PROP_STATUS,
399 	POWER_SUPPLY_PROP_PRESENT,
400 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
401 	POWER_SUPPLY_PROP_CURRENT_NOW,
402 	POWER_SUPPLY_PROP_CAPACITY,
403 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
404 	POWER_SUPPLY_PROP_TEMP,
405 	POWER_SUPPLY_PROP_TECHNOLOGY,
406 	POWER_SUPPLY_PROP_CHARGE_FULL,
407 	POWER_SUPPLY_PROP_CHARGE_NOW,
408 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
409 	POWER_SUPPLY_PROP_MANUFACTURER,
410 };
411 
412 #define BQ27XXX_PROP(_id, _prop)		\
413 	[_id] = {				\
414 		.props = _prop,			\
415 		.size = ARRAY_SIZE(_prop),	\
416 	}
417 
418 static struct {
419 	enum power_supply_property *props;
420 	size_t size;
421 } bq27xxx_battery_props[] = {
422 	BQ27XXX_PROP(BQ27000, bq27000_battery_props),
423 	BQ27XXX_PROP(BQ27010, bq27010_battery_props),
424 	BQ27XXX_PROP(BQ27500, bq27500_battery_props),
425 	BQ27XXX_PROP(BQ27510, bq27510_battery_props),
426 	BQ27XXX_PROP(BQ27530, bq27530_battery_props),
427 	BQ27XXX_PROP(BQ27541, bq27541_battery_props),
428 	BQ27XXX_PROP(BQ27545, bq27545_battery_props),
429 	BQ27XXX_PROP(BQ27421, bq27421_battery_props),
430 };
431 
432 static DEFINE_MUTEX(bq27xxx_list_lock);
433 static LIST_HEAD(bq27xxx_battery_devices);
434 
poll_interval_param_set(const char * val,const struct kernel_param * kp)435 static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
436 {
437 	struct bq27xxx_device_info *di;
438 	int ret;
439 
440 	ret = param_set_uint(val, kp);
441 	if (ret < 0)
442 		return ret;
443 
444 	mutex_lock(&bq27xxx_list_lock);
445 	list_for_each_entry(di, &bq27xxx_battery_devices, list) {
446 		cancel_delayed_work_sync(&di->work);
447 		schedule_delayed_work(&di->work, 0);
448 	}
449 	mutex_unlock(&bq27xxx_list_lock);
450 
451 	return ret;
452 }
453 
454 static const struct kernel_param_ops param_ops_poll_interval = {
455 	.get = param_get_uint,
456 	.set = poll_interval_param_set,
457 };
458 
459 static unsigned int poll_interval = 360;
460 module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
461 MODULE_PARM_DESC(poll_interval,
462 		 "battery poll interval in seconds - 0 disables polling");
463 
464 /*
465  * Common code for BQ27xxx devices
466  */
467 
bq27xxx_read(struct bq27xxx_device_info * di,int reg_index,bool single)468 static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
469 			       bool single)
470 {
471 	/* Reports EINVAL for invalid/missing registers */
472 	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
473 		return -EINVAL;
474 
475 	return di->bus.read(di, di->regs[reg_index], single);
476 }
477 
478 /*
479  * Return the battery State-of-Charge
480  * Or < 0 if something fails.
481  */
bq27xxx_battery_read_soc(struct bq27xxx_device_info * di)482 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
483 {
484 	int soc;
485 
486 	if (di->chip == BQ27000 || di->chip == BQ27010)
487 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
488 	else
489 		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
490 
491 	if (soc < 0)
492 		dev_dbg(di->dev, "error reading State-of-Charge\n");
493 
494 	return soc;
495 }
496 
497 /*
498  * Return a battery charge value in µAh
499  * Or < 0 if something fails.
500  */
bq27xxx_battery_read_charge(struct bq27xxx_device_info * di,u8 reg)501 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
502 {
503 	int charge;
504 
505 	charge = bq27xxx_read(di, reg, false);
506 	if (charge < 0) {
507 		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
508 			reg, charge);
509 		return charge;
510 	}
511 
512 	if (di->chip == BQ27000 || di->chip == BQ27010)
513 		charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
514 	else
515 		charge *= 1000;
516 
517 	return charge;
518 }
519 
520 /*
521  * Return the battery Nominal available capacity in µAh
522  * Or < 0 if something fails.
523  */
bq27xxx_battery_read_nac(struct bq27xxx_device_info * di)524 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
525 {
526 	int flags;
527 
528 	if (di->chip == BQ27000 || di->chip == BQ27010) {
529 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
530 		if (flags >= 0 && (flags & BQ27000_FLAG_CI))
531 			return -ENODATA;
532 	}
533 
534 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
535 }
536 
537 /*
538  * Return the battery Full Charge Capacity in µAh
539  * Or < 0 if something fails.
540  */
bq27xxx_battery_read_fcc(struct bq27xxx_device_info * di)541 static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
542 {
543 	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
544 }
545 
546 /*
547  * Return the Design Capacity in µAh
548  * Or < 0 if something fails.
549  */
bq27xxx_battery_read_dcap(struct bq27xxx_device_info * di)550 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
551 {
552 	int dcap;
553 
554 	if (di->chip == BQ27000 || di->chip == BQ27010)
555 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
556 	else
557 		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
558 
559 	if (dcap < 0) {
560 		dev_dbg(di->dev, "error reading initial last measured discharge\n");
561 		return dcap;
562 	}
563 
564 	if (di->chip == BQ27000 || di->chip == BQ27010)
565 		dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
566 	else
567 		dcap *= 1000;
568 
569 	return dcap;
570 }
571 
572 /*
573  * Return the battery Available energy in µWh
574  * Or < 0 if something fails.
575  */
bq27xxx_battery_read_energy(struct bq27xxx_device_info * di)576 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
577 {
578 	int ae;
579 
580 	ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
581 	if (ae < 0) {
582 		dev_dbg(di->dev, "error reading available energy\n");
583 		return ae;
584 	}
585 
586 	if (di->chip == BQ27000 || di->chip == BQ27010)
587 		ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
588 	else
589 		ae *= 1000;
590 
591 	return ae;
592 }
593 
594 /*
595  * Return the battery temperature in tenths of degree Kelvin
596  * Or < 0 if something fails.
597  */
bq27xxx_battery_read_temperature(struct bq27xxx_device_info * di)598 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
599 {
600 	int temp;
601 
602 	temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
603 	if (temp < 0) {
604 		dev_err(di->dev, "error reading temperature\n");
605 		return temp;
606 	}
607 
608 	if (di->chip == BQ27000 || di->chip == BQ27010)
609 		temp = 5 * temp / 2;
610 
611 	return temp;
612 }
613 
614 /*
615  * Return the battery Cycle count total
616  * Or < 0 if something fails.
617  */
bq27xxx_battery_read_cyct(struct bq27xxx_device_info * di)618 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
619 {
620 	int cyct;
621 
622 	cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
623 	if (cyct < 0)
624 		dev_err(di->dev, "error reading cycle count total\n");
625 
626 	return cyct;
627 }
628 
629 /*
630  * Read a time register.
631  * Return < 0 if something fails.
632  */
bq27xxx_battery_read_time(struct bq27xxx_device_info * di,u8 reg)633 static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
634 {
635 	int tval;
636 
637 	tval = bq27xxx_read(di, reg, false);
638 	if (tval < 0) {
639 		dev_dbg(di->dev, "error reading time register %02x: %d\n",
640 			reg, tval);
641 		return tval;
642 	}
643 
644 	if (tval == 65535)
645 		return -ENODATA;
646 
647 	return tval * 60;
648 }
649 
650 /*
651  * Read an average power register.
652  * Return < 0 if something fails.
653  */
bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info * di)654 static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
655 {
656 	int tval;
657 
658 	tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
659 	if (tval < 0) {
660 		dev_err(di->dev, "error reading average power register  %02x: %d\n",
661 			BQ27XXX_REG_AP, tval);
662 		return tval;
663 	}
664 
665 	if (di->chip == BQ27000 || di->chip == BQ27010)
666 		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
667 	else
668 		return tval;
669 }
670 
671 /*
672  * Returns true if a battery over temperature condition is detected
673  */
bq27xxx_battery_overtemp(struct bq27xxx_device_info * di,u16 flags)674 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
675 {
676 	if (di->chip == BQ27500 || di->chip == BQ27510 ||
677 	    di->chip == BQ27541 || di->chip == BQ27545)
678 		return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
679 	if (di->chip == BQ27530 || di->chip == BQ27421)
680 		return flags & BQ27XXX_FLAG_OT;
681 
682 	return false;
683 }
684 
685 /*
686  * Returns true if a battery under temperature condition is detected
687  */
bq27xxx_battery_undertemp(struct bq27xxx_device_info * di,u16 flags)688 static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
689 {
690 	if (di->chip == BQ27530 || di->chip == BQ27421)
691 		return flags & BQ27XXX_FLAG_UT;
692 
693 	return false;
694 }
695 
696 /*
697  * Returns true if a low state of charge condition is detected
698  */
bq27xxx_battery_dead(struct bq27xxx_device_info * di,u16 flags)699 static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
700 {
701 	if (di->chip == BQ27000 || di->chip == BQ27010)
702 		return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
703 	else
704 		return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
705 }
706 
707 /*
708  * Read flag register.
709  * Return < 0 if something fails.
710  */
bq27xxx_battery_read_health(struct bq27xxx_device_info * di)711 static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
712 {
713 	int flags;
714 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
715 
716 	flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
717 	if (flags < 0) {
718 		dev_err(di->dev, "error reading flag register:%d\n", flags);
719 		return flags;
720 	}
721 
722 	/* Unlikely but important to return first */
723 	if (unlikely(bq27xxx_battery_overtemp(di, flags)))
724 		return POWER_SUPPLY_HEALTH_OVERHEAT;
725 	if (unlikely(bq27xxx_battery_undertemp(di, flags)))
726 		return POWER_SUPPLY_HEALTH_COLD;
727 	if (unlikely(bq27xxx_battery_dead(di, flags)))
728 		return POWER_SUPPLY_HEALTH_DEAD;
729 
730 	return POWER_SUPPLY_HEALTH_GOOD;
731 }
732 
bq27xxx_battery_update(struct bq27xxx_device_info * di)733 void bq27xxx_battery_update(struct bq27xxx_device_info *di)
734 {
735 	struct bq27xxx_reg_cache cache = {0, };
736 	bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
737 	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
738 
739 	cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
740 	if ((cache.flags & 0xff) == 0xff)
741 		cache.flags = -1; /* read error */
742 	if (cache.flags >= 0) {
743 		cache.temperature = bq27xxx_battery_read_temperature(di);
744 		if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
745 			dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
746 			cache.capacity = -ENODATA;
747 			cache.energy = -ENODATA;
748 			cache.time_to_empty = -ENODATA;
749 			cache.time_to_empty_avg = -ENODATA;
750 			cache.time_to_full = -ENODATA;
751 			cache.charge_full = -ENODATA;
752 			cache.health = -ENODATA;
753 		} else {
754 			if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
755 				cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
756 			if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
757 				cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
758 			if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
759 				cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
760 			cache.charge_full = bq27xxx_battery_read_fcc(di);
761 			cache.capacity = bq27xxx_battery_read_soc(di);
762 			if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
763 				cache.energy = bq27xxx_battery_read_energy(di);
764 			cache.health = bq27xxx_battery_read_health(di);
765 		}
766 		if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
767 			cache.cycle_count = bq27xxx_battery_read_cyct(di);
768 		if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
769 			cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
770 
771 		/* We only have to read charge design full once */
772 		if (di->charge_design_full <= 0)
773 			di->charge_design_full = bq27xxx_battery_read_dcap(di);
774 	}
775 
776 	if (di->cache.capacity != cache.capacity)
777 		power_supply_changed(di->bat);
778 
779 	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
780 		di->cache = cache;
781 
782 	di->last_update = jiffies;
783 }
784 EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
785 
bq27xxx_battery_poll(struct work_struct * work)786 static void bq27xxx_battery_poll(struct work_struct *work)
787 {
788 	struct bq27xxx_device_info *di =
789 			container_of(work, struct bq27xxx_device_info,
790 				     work.work);
791 
792 	bq27xxx_battery_update(di);
793 
794 	if (poll_interval > 0)
795 		schedule_delayed_work(&di->work, poll_interval * HZ);
796 }
797 
798 /*
799  * Return the battery average current in µA
800  * Note that current can be negative signed as well
801  * Or 0 if something fails.
802  */
bq27xxx_battery_current(struct bq27xxx_device_info * di,union power_supply_propval * val)803 static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
804 				   union power_supply_propval *val)
805 {
806 	int curr;
807 	int flags;
808 
809 	curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
810 	if (curr < 0) {
811 		dev_err(di->dev, "error reading current\n");
812 		return curr;
813 	}
814 
815 	if (di->chip == BQ27000 || di->chip == BQ27010) {
816 		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
817 		if (flags & BQ27000_FLAG_CHGS) {
818 			dev_dbg(di->dev, "negative current!\n");
819 			curr = -curr;
820 		}
821 
822 		val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
823 	} else {
824 		/* Other gauges return signed value */
825 		val->intval = (int)((s16)curr) * 1000;
826 	}
827 
828 	return 0;
829 }
830 
bq27xxx_battery_status(struct bq27xxx_device_info * di,union power_supply_propval * val)831 static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
832 				  union power_supply_propval *val)
833 {
834 	int status;
835 
836 	if (di->chip == BQ27000 || di->chip == BQ27010) {
837 		if (di->cache.flags & BQ27000_FLAG_FC)
838 			status = POWER_SUPPLY_STATUS_FULL;
839 		else if (di->cache.flags & BQ27000_FLAG_CHGS)
840 			status = POWER_SUPPLY_STATUS_CHARGING;
841 		else if (power_supply_am_i_supplied(di->bat))
842 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
843 		else
844 			status = POWER_SUPPLY_STATUS_DISCHARGING;
845 	} else {
846 		if (di->cache.flags & BQ27XXX_FLAG_FC)
847 			status = POWER_SUPPLY_STATUS_FULL;
848 		else if (di->cache.flags & BQ27XXX_FLAG_DSC)
849 			status = POWER_SUPPLY_STATUS_DISCHARGING;
850 		else
851 			status = POWER_SUPPLY_STATUS_CHARGING;
852 	}
853 
854 	val->intval = status;
855 
856 	return 0;
857 }
858 
bq27xxx_battery_capacity_level(struct bq27xxx_device_info * di,union power_supply_propval * val)859 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
860 					  union power_supply_propval *val)
861 {
862 	int level;
863 
864 	if (di->chip == BQ27000 || di->chip == BQ27010) {
865 		if (di->cache.flags & BQ27000_FLAG_FC)
866 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
867 		else if (di->cache.flags & BQ27000_FLAG_EDV1)
868 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
869 		else if (di->cache.flags & BQ27000_FLAG_EDVF)
870 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
871 		else
872 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
873 	} else {
874 		if (di->cache.flags & BQ27XXX_FLAG_FC)
875 			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
876 		else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
877 			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
878 		else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
879 			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
880 		else
881 			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
882 	}
883 
884 	val->intval = level;
885 
886 	return 0;
887 }
888 
889 /*
890  * Return the battery Voltage in millivolts
891  * Or < 0 if something fails.
892  */
bq27xxx_battery_voltage(struct bq27xxx_device_info * di,union power_supply_propval * val)893 static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
894 				   union power_supply_propval *val)
895 {
896 	int volt;
897 
898 	volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
899 	if (volt < 0) {
900 		dev_err(di->dev, "error reading voltage\n");
901 		return volt;
902 	}
903 
904 	val->intval = volt * 1000;
905 
906 	return 0;
907 }
908 
bq27xxx_simple_value(int value,union power_supply_propval * val)909 static int bq27xxx_simple_value(int value,
910 				union power_supply_propval *val)
911 {
912 	if (value < 0)
913 		return value;
914 
915 	val->intval = value;
916 
917 	return 0;
918 }
919 
bq27xxx_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)920 static int bq27xxx_battery_get_property(struct power_supply *psy,
921 					enum power_supply_property psp,
922 					union power_supply_propval *val)
923 {
924 	int ret = 0;
925 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
926 
927 	mutex_lock(&di->lock);
928 	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
929 		cancel_delayed_work_sync(&di->work);
930 		bq27xxx_battery_poll(&di->work.work);
931 	}
932 	mutex_unlock(&di->lock);
933 
934 	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
935 		return -ENODEV;
936 
937 	switch (psp) {
938 	case POWER_SUPPLY_PROP_STATUS:
939 		ret = bq27xxx_battery_status(di, val);
940 		break;
941 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
942 		ret = bq27xxx_battery_voltage(di, val);
943 		break;
944 	case POWER_SUPPLY_PROP_PRESENT:
945 		val->intval = di->cache.flags < 0 ? 0 : 1;
946 		break;
947 	case POWER_SUPPLY_PROP_CURRENT_NOW:
948 		ret = bq27xxx_battery_current(di, val);
949 		break;
950 	case POWER_SUPPLY_PROP_CAPACITY:
951 		ret = bq27xxx_simple_value(di->cache.capacity, val);
952 		break;
953 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
954 		ret = bq27xxx_battery_capacity_level(di, val);
955 		break;
956 	case POWER_SUPPLY_PROP_TEMP:
957 		ret = bq27xxx_simple_value(di->cache.temperature, val);
958 		if (ret == 0)
959 			val->intval -= 2731; /* convert decidegree k to c */
960 		break;
961 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
962 		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
963 		break;
964 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
965 		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
966 		break;
967 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
968 		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
969 		break;
970 	case POWER_SUPPLY_PROP_TECHNOLOGY:
971 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
972 		break;
973 	case POWER_SUPPLY_PROP_CHARGE_NOW:
974 		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
975 		break;
976 	case POWER_SUPPLY_PROP_CHARGE_FULL:
977 		ret = bq27xxx_simple_value(di->cache.charge_full, val);
978 		break;
979 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
980 		ret = bq27xxx_simple_value(di->charge_design_full, val);
981 		break;
982 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
983 		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
984 		break;
985 	case POWER_SUPPLY_PROP_ENERGY_NOW:
986 		ret = bq27xxx_simple_value(di->cache.energy, val);
987 		break;
988 	case POWER_SUPPLY_PROP_POWER_AVG:
989 		ret = bq27xxx_simple_value(di->cache.power_avg, val);
990 		break;
991 	case POWER_SUPPLY_PROP_HEALTH:
992 		ret = bq27xxx_simple_value(di->cache.health, val);
993 		break;
994 	case POWER_SUPPLY_PROP_MANUFACTURER:
995 		val->strval = BQ27XXX_MANUFACTURER;
996 		break;
997 	default:
998 		return -EINVAL;
999 	}
1000 
1001 	return ret;
1002 }
1003 
bq27xxx_external_power_changed(struct power_supply * psy)1004 static void bq27xxx_external_power_changed(struct power_supply *psy)
1005 {
1006 	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
1007 
1008 	cancel_delayed_work_sync(&di->work);
1009 	schedule_delayed_work(&di->work, 0);
1010 }
1011 
bq27xxx_battery_setup(struct bq27xxx_device_info * di)1012 int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
1013 {
1014 	struct power_supply_desc *psy_desc;
1015 	struct power_supply_config psy_cfg = { .drv_data = di, };
1016 
1017 	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
1018 	mutex_init(&di->lock);
1019 	di->regs = bq27xxx_regs[di->chip];
1020 
1021 	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
1022 	if (!psy_desc)
1023 		return -ENOMEM;
1024 
1025 	psy_desc->name = di->name;
1026 	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1027 	psy_desc->properties = bq27xxx_battery_props[di->chip].props;
1028 	psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
1029 	psy_desc->get_property = bq27xxx_battery_get_property;
1030 	psy_desc->external_power_changed = bq27xxx_external_power_changed;
1031 
1032 	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
1033 	if (IS_ERR(di->bat)) {
1034 		dev_err(di->dev, "failed to register battery\n");
1035 		return PTR_ERR(di->bat);
1036 	}
1037 
1038 	dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1039 
1040 	bq27xxx_battery_update(di);
1041 
1042 	mutex_lock(&bq27xxx_list_lock);
1043 	list_add(&di->list, &bq27xxx_battery_devices);
1044 	mutex_unlock(&bq27xxx_list_lock);
1045 
1046 	return 0;
1047 }
1048 EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
1049 
bq27xxx_battery_teardown(struct bq27xxx_device_info * di)1050 void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
1051 {
1052 	/*
1053 	 * power_supply_unregister call bq27xxx_battery_get_property which
1054 	 * call bq27xxx_battery_poll.
1055 	 * Make sure that bq27xxx_battery_poll will not call
1056 	 * schedule_delayed_work again after unregister (which cause OOPS).
1057 	 */
1058 	poll_interval = 0;
1059 
1060 	cancel_delayed_work_sync(&di->work);
1061 
1062 	power_supply_unregister(di->bat);
1063 
1064 	mutex_lock(&bq27xxx_list_lock);
1065 	list_del(&di->list);
1066 	mutex_unlock(&bq27xxx_list_lock);
1067 
1068 	mutex_destroy(&di->lock);
1069 }
1070 EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
1071 
bq27xxx_battery_platform_read(struct bq27xxx_device_info * di,u8 reg,bool single)1072 static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1073 					 bool single)
1074 {
1075 	struct device *dev = di->dev;
1076 	struct bq27xxx_platform_data *pdata = dev->platform_data;
1077 	unsigned int timeout = 3;
1078 	int upper, lower;
1079 	int temp;
1080 
1081 	if (!single) {
1082 		/* Make sure the value has not changed in between reading the
1083 		 * lower and the upper part */
1084 		upper = pdata->read(dev, reg + 1);
1085 		do {
1086 			temp = upper;
1087 			if (upper < 0)
1088 				return upper;
1089 
1090 			lower = pdata->read(dev, reg);
1091 			if (lower < 0)
1092 				return lower;
1093 
1094 			upper = pdata->read(dev, reg + 1);
1095 		} while (temp != upper && --timeout);
1096 
1097 		if (timeout == 0)
1098 			return -EIO;
1099 
1100 		return (upper << 8) | lower;
1101 	}
1102 
1103 	return pdata->read(dev, reg);
1104 }
1105 
bq27xxx_battery_platform_probe(struct platform_device * pdev)1106 static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
1107 {
1108 	struct bq27xxx_device_info *di;
1109 	struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
1110 
1111 	if (!pdata) {
1112 		dev_err(&pdev->dev, "no platform_data supplied\n");
1113 		return -EINVAL;
1114 	}
1115 
1116 	if (!pdata->read) {
1117 		dev_err(&pdev->dev, "no hdq read callback supplied\n");
1118 		return -EINVAL;
1119 	}
1120 
1121 	if (!pdata->chip) {
1122 		dev_err(&pdev->dev, "no device supplied\n");
1123 		return -EINVAL;
1124 	}
1125 
1126 	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1127 	if (!di)
1128 		return -ENOMEM;
1129 
1130 	platform_set_drvdata(pdev, di);
1131 
1132 	di->dev = &pdev->dev;
1133 	di->chip = pdata->chip;
1134 	di->name = pdata->name ?: dev_name(&pdev->dev);
1135 	di->bus.read = bq27xxx_battery_platform_read;
1136 
1137 	return bq27xxx_battery_setup(di);
1138 }
1139 
bq27xxx_battery_platform_remove(struct platform_device * pdev)1140 static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
1141 {
1142 	struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
1143 
1144 	bq27xxx_battery_teardown(di);
1145 
1146 	return 0;
1147 }
1148 
1149 static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
1150 	{ "bq27000-battery", },
1151 	{ /* sentinel */ }
1152 };
1153 MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
1154 
1155 #ifdef CONFIG_OF
1156 static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
1157 	{ .compatible = "ti,bq27000" },
1158 	{},
1159 };
1160 MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
1161 #endif
1162 
1163 static struct platform_driver bq27xxx_battery_platform_driver = {
1164 	.probe	= bq27xxx_battery_platform_probe,
1165 	.remove = bq27xxx_battery_platform_remove,
1166 	.driver = {
1167 		.name = "bq27000-battery",
1168 		.of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
1169 	},
1170 	.id_table = bq27xxx_battery_platform_id_table,
1171 };
1172 module_platform_driver(bq27xxx_battery_platform_driver);
1173 
1174 MODULE_ALIAS("platform:bq27000-battery");
1175 
1176 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1177 MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
1178 MODULE_LICENSE("GPL");
1179