blob: 45f6ebf88df6b7962f288a599ca8363d2131923d [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
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
Saranya Gopala66f59b2012-07-13 20:20:07 -070035 * http://www.ti.com/product/bq27425-g1
Andrew F. Davisd74534c2015-09-22 14:35:09 -050036 * http://www.ti.com/product/bq27411-g1
37 * http://www.ti.com/product/bq27621-g1
Pali Rohár631c17e2011-02-01 00:08:02 +010038 */
39
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +053040#include <linux/device.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070041#include <linux/module.h>
42#include <linux/param.h>
43#include <linux/jiffies.h>
44#include <linux/workqueue.h>
45#include <linux/delay.h>
46#include <linux/platform_device.h>
47#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Pali Rohár7df3a742016-02-21 12:28:22 +010049#include <linux/of.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070050
Andrew F. Davis081bab22015-09-22 14:35:06 -050051#include <linux/power/bq27xxx_battery.h>
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020052
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050053#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070054
Andrew F. Davisdb04fc52015-07-22 16:51:53 -050055#define BQ27XXX_MANUFACTURER "Texas Instruments"
56
Andrew F. Davisd74534c2015-09-22 14:35:09 -050057/* BQ27XXX Flags */
58#define BQ27XXX_FLAG_DSC BIT(0)
59#define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
60#define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
61#define BQ27XXX_FLAG_FC BIT(9)
62#define BQ27XXX_FLAG_OTD BIT(14)
63#define BQ27XXX_FLAG_OTC BIT(15)
Andrew F. Davis74aab842015-09-22 14:35:10 -050064#define BQ27XXX_FLAG_UT BIT(14)
65#define BQ27XXX_FLAG_OT BIT(15)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070066
Andrew F. Davisd74534c2015-09-22 14:35:09 -050067/* BQ27000 has different layout for Flags register */
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050068#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
69#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
70#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
71#define BQ27000_FLAG_FC BIT(5)
72#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020073
Andrew F. Davisd74534c2015-09-22 14:35:09 -050074#define BQ27XXX_RS (20) /* Resistor sense mOhm */
75#define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
76#define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
Pali Rohára2e51182010-05-24 20:52:13 +020077
Andrew F. Davisd74534c2015-09-22 14:35:09 -050078#define INVALID_REG_ADDR 0xff
79
80/*
81 * bq27xxx_reg_index - Register names
82 *
83 * These are indexes into a device's register mapping array.
84 */
85enum bq27xxx_reg_index {
86 BQ27XXX_REG_CTRL = 0, /* Control */
87 BQ27XXX_REG_TEMP, /* Temperature */
88 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
89 BQ27XXX_REG_VOLT, /* Voltage */
90 BQ27XXX_REG_AI, /* Average Current */
91 BQ27XXX_REG_FLAGS, /* Flags */
92 BQ27XXX_REG_TTE, /* Time-to-Empty */
93 BQ27XXX_REG_TTF, /* Time-to-Full */
94 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
95 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
96 BQ27XXX_REG_NAC, /* Nominal Available Capacity */
97 BQ27XXX_REG_FCC, /* Full Charge Capacity */
98 BQ27XXX_REG_CYCT, /* Cycle Count */
99 BQ27XXX_REG_AE, /* Available Energy */
100 BQ27XXX_REG_SOC, /* State-of-Charge */
101 BQ27XXX_REG_DCAP, /* Design Capacity */
102 BQ27XXX_REG_AP, /* Average Power */
103};
104
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500105/* Register mappings */
106static u8 bq27000_regs[] = {
107 0x00, /* CONTROL */
108 0x06, /* TEMP */
109 INVALID_REG_ADDR, /* INT TEMP - NA*/
110 0x08, /* VOLT */
111 0x14, /* AVG CURR */
112 0x0a, /* FLAGS */
113 0x16, /* TTE */
114 0x18, /* TTF */
115 0x1c, /* TTES */
116 0x26, /* TTECP */
117 0x0c, /* NAC */
118 0x12, /* LMD(FCC) */
119 0x2a, /* CYCT */
120 0x22, /* AE */
121 0x0b, /* SOC(RSOC) */
122 0x76, /* DCAP(ILMD) */
123 0x24, /* AP */
124};
125
126static u8 bq27010_regs[] = {
127 0x00, /* CONTROL */
128 0x06, /* TEMP */
129 INVALID_REG_ADDR, /* INT TEMP - NA*/
130 0x08, /* VOLT */
131 0x14, /* AVG CURR */
132 0x0a, /* FLAGS */
133 0x16, /* TTE */
134 0x18, /* TTF */
135 0x1c, /* TTES */
136 0x26, /* TTECP */
137 0x0c, /* NAC */
138 0x12, /* LMD(FCC) */
139 0x2a, /* CYCT */
140 INVALID_REG_ADDR, /* AE - NA */
141 0x0b, /* SOC(RSOC) */
142 0x76, /* DCAP(ILMD) */
143 INVALID_REG_ADDR, /* AP - NA */
144};
145
146static u8 bq27500_regs[] = {
147 0x00, /* CONTROL */
148 0x06, /* TEMP */
149 0x28, /* INT TEMP */
150 0x08, /* VOLT */
151 0x14, /* AVG CURR */
152 0x0a, /* FLAGS */
153 0x16, /* TTE */
154 INVALID_REG_ADDR, /* TTF - NA */
155 0x1a, /* TTES */
156 INVALID_REG_ADDR, /* TTECP - NA */
157 0x0c, /* NAC */
158 0x12, /* LMD(FCC) */
H. Nikolaus Schaller099867a2015-12-17 11:12:54 +0100159 0x2a, /* CYCT */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500160 INVALID_REG_ADDR, /* AE - NA */
H. Nikolaus Schaller099867a2015-12-17 11:12:54 +0100161 0x2c, /* SOC(RSOC) */
162 0x3c, /* DCAP(ILMD) */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500163 INVALID_REG_ADDR, /* AP - NA */
164};
165
166static u8 bq27530_regs[] = {
167 0x00, /* CONTROL */
168 0x06, /* TEMP */
169 0x32, /* INT TEMP */
170 0x08, /* VOLT */
171 0x14, /* AVG CURR */
172 0x0a, /* FLAGS */
173 0x16, /* TTE */
174 INVALID_REG_ADDR, /* TTF - NA */
175 INVALID_REG_ADDR, /* TTES - NA */
176 INVALID_REG_ADDR, /* TTECP - NA */
177 0x0c, /* NAC */
178 0x12, /* LMD(FCC) */
179 0x2a, /* CYCT */
180 INVALID_REG_ADDR, /* AE - NA */
181 0x2c, /* SOC(RSOC) */
182 INVALID_REG_ADDR, /* DCAP - NA */
183 0x24, /* AP */
184};
185
186static u8 bq27541_regs[] = {
187 0x00, /* CONTROL */
188 0x06, /* TEMP */
189 0x28, /* INT TEMP */
190 0x08, /* VOLT */
191 0x14, /* AVG CURR */
192 0x0a, /* FLAGS */
193 0x16, /* TTE */
194 INVALID_REG_ADDR, /* TTF - NA */
195 INVALID_REG_ADDR, /* TTES - NA */
196 INVALID_REG_ADDR, /* TTECP - NA */
197 0x0c, /* NAC */
198 0x12, /* LMD(FCC) */
199 0x2a, /* CYCT */
200 INVALID_REG_ADDR, /* AE - NA */
201 0x2c, /* SOC(RSOC) */
202 0x3c, /* DCAP */
Liu Xiang265b6042016-01-09 22:10:39 +0800203 0x24, /* AP */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500204};
205
206static u8 bq27545_regs[] = {
207 0x00, /* CONTROL */
208 0x06, /* TEMP */
209 0x28, /* INT TEMP */
210 0x08, /* VOLT */
211 0x14, /* AVG CURR */
212 0x0a, /* FLAGS */
213 0x16, /* TTE */
214 INVALID_REG_ADDR, /* TTF - NA */
215 INVALID_REG_ADDR, /* TTES - NA */
216 INVALID_REG_ADDR, /* TTECP - NA */
217 0x0c, /* NAC */
218 0x12, /* LMD(FCC) */
219 0x2a, /* CYCT */
220 INVALID_REG_ADDR, /* AE - NA */
221 0x2c, /* SOC(RSOC) */
222 INVALID_REG_ADDR, /* DCAP - NA */
223 0x24, /* AP */
224};
225
226static u8 bq27421_regs[] = {
227 0x00, /* CONTROL */
228 0x02, /* TEMP */
229 0x1e, /* INT TEMP */
230 0x04, /* VOLT */
231 0x10, /* AVG CURR */
232 0x06, /* FLAGS */
233 INVALID_REG_ADDR, /* TTE - NA */
234 INVALID_REG_ADDR, /* TTF - NA */
235 INVALID_REG_ADDR, /* TTES - NA */
236 INVALID_REG_ADDR, /* TTECP - NA */
237 0x08, /* NAC */
238 0x0e, /* FCC */
239 INVALID_REG_ADDR, /* CYCT - NA */
240 INVALID_REG_ADDR, /* AE - NA */
241 0x1c, /* SOC */
242 0x3c, /* DCAP */
243 0x18, /* AP */
244};
245
246static u8 *bq27xxx_regs[] = {
247 [BQ27000] = bq27000_regs,
248 [BQ27010] = bq27010_regs,
249 [BQ27500] = bq27500_regs,
250 [BQ27530] = bq27530_regs,
251 [BQ27541] = bq27541_regs,
252 [BQ27545] = bq27545_regs,
253 [BQ27421] = bq27421_regs,
254};
255
256static enum power_supply_property bq27000_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200257 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700258 POWER_SUPPLY_PROP_PRESENT,
259 POWER_SUPPLY_PROP_VOLTAGE_NOW,
260 POWER_SUPPLY_PROP_CURRENT_NOW,
261 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100262 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700263 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200264 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
265 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
266 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200267 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100268 POWER_SUPPLY_PROP_CHARGE_FULL,
269 POWER_SUPPLY_PROP_CHARGE_NOW,
270 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200271 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100272 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530273 POWER_SUPPLY_PROP_POWER_AVG,
274 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500275 POWER_SUPPLY_PROP_MANUFACTURER,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700276};
277
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500278static enum power_supply_property bq27010_battery_props[] = {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700279 POWER_SUPPLY_PROP_STATUS,
280 POWER_SUPPLY_PROP_PRESENT,
281 POWER_SUPPLY_PROP_VOLTAGE_NOW,
282 POWER_SUPPLY_PROP_CURRENT_NOW,
283 POWER_SUPPLY_PROP_CAPACITY,
284 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
285 POWER_SUPPLY_PROP_TEMP,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500286 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
287 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
288 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700289 POWER_SUPPLY_PROP_TECHNOLOGY,
290 POWER_SUPPLY_PROP_CHARGE_FULL,
291 POWER_SUPPLY_PROP_CHARGE_NOW,
292 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500293 POWER_SUPPLY_PROP_CYCLE_COUNT,
294 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500295 POWER_SUPPLY_PROP_MANUFACTURER,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700296};
297
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500298static enum power_supply_property bq27500_battery_props[] = {
299 POWER_SUPPLY_PROP_STATUS,
300 POWER_SUPPLY_PROP_PRESENT,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 POWER_SUPPLY_PROP_CURRENT_NOW,
303 POWER_SUPPLY_PROP_CAPACITY,
304 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
305 POWER_SUPPLY_PROP_TEMP,
306 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
307 POWER_SUPPLY_PROP_TECHNOLOGY,
308 POWER_SUPPLY_PROP_CHARGE_FULL,
309 POWER_SUPPLY_PROP_CHARGE_NOW,
310 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
311 POWER_SUPPLY_PROP_CYCLE_COUNT,
312 POWER_SUPPLY_PROP_HEALTH,
313 POWER_SUPPLY_PROP_MANUFACTURER,
314};
315
316static enum power_supply_property bq27530_battery_props[] = {
317 POWER_SUPPLY_PROP_STATUS,
318 POWER_SUPPLY_PROP_PRESENT,
319 POWER_SUPPLY_PROP_VOLTAGE_NOW,
320 POWER_SUPPLY_PROP_CURRENT_NOW,
321 POWER_SUPPLY_PROP_CAPACITY,
322 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
323 POWER_SUPPLY_PROP_TEMP,
324 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
325 POWER_SUPPLY_PROP_TECHNOLOGY,
326 POWER_SUPPLY_PROP_CHARGE_FULL,
327 POWER_SUPPLY_PROP_CHARGE_NOW,
328 POWER_SUPPLY_PROP_POWER_AVG,
329 POWER_SUPPLY_PROP_HEALTH,
330 POWER_SUPPLY_PROP_CYCLE_COUNT,
331 POWER_SUPPLY_PROP_MANUFACTURER,
332};
333
334static enum power_supply_property bq27541_battery_props[] = {
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700335 POWER_SUPPLY_PROP_STATUS,
336 POWER_SUPPLY_PROP_PRESENT,
337 POWER_SUPPLY_PROP_VOLTAGE_NOW,
338 POWER_SUPPLY_PROP_CURRENT_NOW,
339 POWER_SUPPLY_PROP_CAPACITY,
340 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
341 POWER_SUPPLY_PROP_TEMP,
342 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
343 POWER_SUPPLY_PROP_TECHNOLOGY,
344 POWER_SUPPLY_PROP_CHARGE_FULL,
345 POWER_SUPPLY_PROP_CHARGE_NOW,
346 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
347 POWER_SUPPLY_PROP_CYCLE_COUNT,
348 POWER_SUPPLY_PROP_POWER_AVG,
349 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500350 POWER_SUPPLY_PROP_MANUFACTURER,
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700351};
352
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500353static enum power_supply_property bq27545_battery_props[] = {
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100354 POWER_SUPPLY_PROP_STATUS,
355 POWER_SUPPLY_PROP_PRESENT,
356 POWER_SUPPLY_PROP_VOLTAGE_NOW,
357 POWER_SUPPLY_PROP_CURRENT_NOW,
358 POWER_SUPPLY_PROP_CAPACITY,
359 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
360 POWER_SUPPLY_PROP_TEMP,
361 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
362 POWER_SUPPLY_PROP_TECHNOLOGY,
363 POWER_SUPPLY_PROP_CHARGE_FULL,
364 POWER_SUPPLY_PROP_CHARGE_NOW,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500365 POWER_SUPPLY_PROP_HEALTH,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100366 POWER_SUPPLY_PROP_CYCLE_COUNT,
367 POWER_SUPPLY_PROP_POWER_AVG,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500368 POWER_SUPPLY_PROP_MANUFACTURER,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100369};
370
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500371static enum power_supply_property bq27421_battery_props[] = {
372 POWER_SUPPLY_PROP_STATUS,
373 POWER_SUPPLY_PROP_PRESENT,
374 POWER_SUPPLY_PROP_VOLTAGE_NOW,
375 POWER_SUPPLY_PROP_CURRENT_NOW,
376 POWER_SUPPLY_PROP_CAPACITY,
377 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
378 POWER_SUPPLY_PROP_TEMP,
379 POWER_SUPPLY_PROP_TECHNOLOGY,
380 POWER_SUPPLY_PROP_CHARGE_FULL,
381 POWER_SUPPLY_PROP_CHARGE_NOW,
382 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
383 POWER_SUPPLY_PROP_MANUFACTURER,
384};
385
386#define BQ27XXX_PROP(_id, _prop) \
387 [_id] = { \
388 .props = _prop, \
389 .size = ARRAY_SIZE(_prop), \
390 }
391
392static struct {
393 enum power_supply_property *props;
394 size_t size;
395} bq27xxx_battery_props[] = {
396 BQ27XXX_PROP(BQ27000, bq27000_battery_props),
397 BQ27XXX_PROP(BQ27010, bq27010_battery_props),
398 BQ27XXX_PROP(BQ27500, bq27500_battery_props),
399 BQ27XXX_PROP(BQ27530, bq27530_battery_props),
400 BQ27XXX_PROP(BQ27541, bq27541_battery_props),
401 BQ27XXX_PROP(BQ27545, bq27545_battery_props),
402 BQ27XXX_PROP(BQ27421, bq27421_battery_props),
403};
404
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100405static unsigned int poll_interval = 360;
406module_param(poll_interval, uint, 0644);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500407MODULE_PARM_DESC(poll_interval,
408 "battery poll interval in seconds - 0 disables polling");
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100409
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700410/*
Andrew F. Davis081bab22015-09-22 14:35:06 -0500411 * Common code for BQ27xxx devices
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700412 */
413
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500414static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500415 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700416{
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500417 /* Reports EINVAL for invalid/missing registers */
418 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
419 return -EINVAL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100420
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500421 return di->bus.read(di, di->regs[reg_index], single);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700422}
423
424/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500425 * Return the battery State-of-Charge
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100426 * Or < 0 if something fails.
427 */
Andrew F. Davisc5709032015-09-22 14:35:08 -0500428static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100429{
Andrew F. Davisc5709032015-09-22 14:35:08 -0500430 int soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100431
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100432 if (di->chip == BQ27000 || di->chip == BQ27010)
433 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
434 else
435 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100436
Andrew F. Davisc5709032015-09-22 14:35:08 -0500437 if (soc < 0)
438 dev_dbg(di->dev, "error reading State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100439
Andrew F. Davisc5709032015-09-22 14:35:08 -0500440 return soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100441}
442
443/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100444 * Return a battery charge value in µAh
445 * Or < 0 if something fails.
446 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500447static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
Pali Rohár631c17e2011-02-01 00:08:02 +0100448{
449 int charge;
450
Andrew F. Davis081bab22015-09-22 14:35:06 -0500451 charge = bq27xxx_read(di, reg, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100452 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100453 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
454 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100455 return charge;
456 }
457
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500458 if (di->chip == BQ27000 || di->chip == BQ27010)
459 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100460 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500461 charge *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100462
463 return charge;
464}
465
466/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500467 * Return the battery Nominal available capacity in µAh
Pali Rohár631c17e2011-02-01 00:08:02 +0100468 * Or < 0 if something fails.
469 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500470static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100471{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100472 int flags;
Pali Roháre59ec4a2012-11-10 15:42:48 +0100473
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500474 if (di->chip == BQ27000 || di->chip == BQ27010) {
475 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
476 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
477 return -ENODATA;
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500478 }
Pali Rohár631c17e2011-02-01 00:08:02 +0100479
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500480 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
481}
482
483/*
484 * Return the battery Full Charge Capacity in µAh
485 * Or < 0 if something fails.
486 */
487static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
488{
489 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
490}
491
492/*
493 * Return the Design Capacity in µAh
494 * Or < 0 if something fails.
495 */
496static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
497{
498 int dcap;
499
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100500 if (di->chip == BQ27000 || di->chip == BQ27010)
501 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
502 else
503 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500504
505 if (dcap < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100506 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500507 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100508 }
509
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500510 if (di->chip == BQ27000 || di->chip == BQ27010)
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100511 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100512 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500513 dcap *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100514
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500515 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100516}
517
518/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100519 * Return the battery Available energy in µWh
520 * Or < 0 if something fails.
521 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500522static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
Pali Rohára8f6bd22011-11-01 01:43:06 +0100523{
524 int ae;
525
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500526 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
Pali Rohára8f6bd22011-11-01 01:43:06 +0100527 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100528 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100529 return ae;
530 }
531
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500532 if (di->chip == BQ27000 || di->chip == BQ27010)
533 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100534 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500535 ae *= 1000;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100536
537 return ae;
538}
539
540/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100541 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100542 * Or < 0 if something fails.
543 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500544static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
Pali Rohárd149e982011-11-01 01:43:07 +0100545{
546 int temp;
547
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500548 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
Pali Rohárd149e982011-11-01 01:43:07 +0100549 if (temp < 0) {
550 dev_err(di->dev, "error reading temperature\n");
551 return temp;
552 }
553
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500554 if (di->chip == BQ27000 || di->chip == BQ27010)
Pali Rohár5dc34432013-02-02 11:06:09 +0100555 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100556
557 return temp;
558}
559
560/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100561 * Return the battery Cycle count total
562 * Or < 0 if something fails.
563 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500564static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100565{
566 int cyct;
567
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500568 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100569 if (cyct < 0)
570 dev_err(di->dev, "error reading cycle count total\n");
571
572 return cyct;
573}
574
575/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100576 * Read a time register.
577 * Return < 0 if something fails.
578 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500579static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100580{
581 int tval;
582
Andrew F. Davis081bab22015-09-22 14:35:06 -0500583 tval = bq27xxx_read(di, reg, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100584 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100585 dev_dbg(di->dev, "error reading time register %02x: %d\n",
586 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100587 return tval;
588 }
589
590 if (tval == 65535)
591 return -ENODATA;
592
593 return tval * 60;
594}
595
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530596/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500597 * Read an average power register.
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530598 * Return < 0 if something fails.
599 */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500600static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530601{
602 int tval;
603
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500604 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530605 if (tval < 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500606 dev_err(di->dev, "error reading average power register %02x: %d\n",
607 BQ27XXX_REG_AP, tval);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530608 return tval;
609 }
610
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500611 if (di->chip == BQ27000 || di->chip == BQ27010)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500612 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500613 else
614 return tval;
615}
616
617/*
618 * Returns true if a battery over temperature condition is detected
619 */
Andrew F. Davis74aab842015-09-22 14:35:10 -0500620static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500621{
Andrew F. Davis74aab842015-09-22 14:35:10 -0500622 if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500623 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
Andrew F. Davis74aab842015-09-22 14:35:10 -0500624 if (di->chip == BQ27530 || di->chip == BQ27421)
625 return flags & BQ27XXX_FLAG_OT;
626
627 return false;
628}
629
630/*
631 * Returns true if a battery under temperature condition is detected
632 */
633static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
634{
635 if (di->chip == BQ27530 || di->chip == BQ27421)
636 return flags & BQ27XXX_FLAG_UT;
637
638 return false;
639}
640
641/*
642 * Returns true if a low state of charge condition is detected
643 */
644static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
645{
646 if (di->chip == BQ27000 || di->chip == BQ27010)
647 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500648 else
Andrew F. Davis74aab842015-09-22 14:35:10 -0500649 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530650}
651
652/*
653 * Read flag register.
654 * Return < 0 if something fails.
655 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500656static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530657{
Andrzej Hajda2edd69a2015-09-28 10:51:27 +0200658 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530659
Andrew F. Davis74aab842015-09-22 14:35:10 -0500660 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
661 if (flags < 0) {
662 dev_err(di->dev, "error reading flag register:%d\n", flags);
663 return flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530664 }
665
Andrew F. Davis74aab842015-09-22 14:35:10 -0500666 /* Unlikely but important to return first */
667 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
668 return POWER_SUPPLY_HEALTH_OVERHEAT;
669 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
670 return POWER_SUPPLY_HEALTH_COLD;
671 if (unlikely(bq27xxx_battery_dead(di, flags)))
672 return POWER_SUPPLY_HEALTH_DEAD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530673
Andrew F. Davis74aab842015-09-22 14:35:10 -0500674 return POWER_SUPPLY_HEALTH_GOOD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530675}
676
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600677void bq27xxx_battery_update(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100678{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500679 struct bq27xxx_reg_cache cache = {0, };
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500680 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
681 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100682
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500683 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
Marek Belisko3dd843e2014-06-30 21:49:56 +0200684 if ((cache.flags & 0xff) == 0xff)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500685 cache.flags = -1; /* read error */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100686 if (cache.flags >= 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500687 cache.temperature = bq27xxx_battery_read_temperature(di);
688 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
NeilBrown1f94b252015-10-07 20:42:38 +0200689 dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100690 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100691 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100692 cache.time_to_empty = -ENODATA;
693 cache.time_to_empty_avg = -ENODATA;
694 cache.time_to_full = -ENODATA;
695 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530696 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100697 } else {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500698 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
699 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
700 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
701 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
702 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
703 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
704 cache.charge_full = bq27xxx_battery_read_fcc(di);
Andrew F. Davisc5709032015-09-22 14:35:08 -0500705 cache.capacity = bq27xxx_battery_read_soc(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500706 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500707 cache.energy = bq27xxx_battery_read_energy(di);
Andrew F. Davis081bab22015-09-22 14:35:06 -0500708 cache.health = bq27xxx_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100709 }
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500710 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500711 cache.cycle_count = bq27xxx_battery_read_cyct(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500712 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
713 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100714
Pali Rohár631c17e2011-02-01 00:08:02 +0100715 /* We only have to read charge design full once */
716 if (di->charge_design_full <= 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500717 di->charge_design_full = bq27xxx_battery_read_dcap(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100718 }
719
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700720 if (di->cache.capacity != cache.capacity)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100721 power_supply_changed(di->bat);
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700722
723 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
724 di->cache = cache;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100725
726 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700727}
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600728EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700729
Andrew F. Davis081bab22015-09-22 14:35:06 -0500730static void bq27xxx_battery_poll(struct work_struct *work)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100731{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500732 struct bq27xxx_device_info *di =
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500733 container_of(work, struct bq27xxx_device_info,
734 work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100735
Andrew F. Davis081bab22015-09-22 14:35:06 -0500736 bq27xxx_battery_update(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100737
738 if (poll_interval > 0) {
739 /* The timer does not have to be accurate. */
740 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
741 schedule_delayed_work(&di->work, poll_interval * HZ);
742 }
743}
744
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700745/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100746 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700747 * Note that current can be negative signed as well
748 * Or 0 if something fails.
749 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500750static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500751 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700752{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100753 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100754 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700755
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500756 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100757 if (curr < 0) {
758 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100759 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100760 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200761
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500762 if (di->chip == BQ27000 || di->chip == BQ27010) {
763 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
Pali Rohárb68f6212011-11-01 01:43:03 +0100764 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200765 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74fd2010-02-27 17:06:44 +0200766 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200767 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100768
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500769 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
770 } else {
771 /* Other gauges return signed value */
772 val->intval = (int)((s16)curr) * 1000;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700773 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200774
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100775 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700776}
777
Andrew F. Davis081bab22015-09-22 14:35:06 -0500778static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500779 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200780{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200781 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200782
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500783 if (di->chip == BQ27000 || di->chip == BQ27010) {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200784 if (di->cache.flags & BQ27000_FLAG_FC)
785 status = POWER_SUPPLY_STATUS_FULL;
786 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200787 status = POWER_SUPPLY_STATUS_CHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100788 else if (power_supply_am_i_supplied(di->bat))
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200789 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200790 else
791 status = POWER_SUPPLY_STATUS_DISCHARGING;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500792 } else {
793 if (di->cache.flags & BQ27XXX_FLAG_FC)
794 status = POWER_SUPPLY_STATUS_FULL;
795 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
796 status = POWER_SUPPLY_STATUS_DISCHARGING;
797 else
798 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200799 }
800
801 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100802
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200803 return 0;
804}
805
Andrew F. Davis081bab22015-09-22 14:35:06 -0500806static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500807 union power_supply_propval *val)
Pali Rohárd66bab32011-11-01 01:43:04 +0100808{
809 int level;
810
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500811 if (di->chip == BQ27000 || di->chip == BQ27010) {
Pali Rohárd66bab32011-11-01 01:43:04 +0100812 if (di->cache.flags & BQ27000_FLAG_FC)
813 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
814 else if (di->cache.flags & BQ27000_FLAG_EDV1)
815 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
816 else if (di->cache.flags & BQ27000_FLAG_EDVF)
817 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
818 else
819 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500820 } else {
821 if (di->cache.flags & BQ27XXX_FLAG_FC)
822 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
823 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
824 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
825 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
826 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
827 else
828 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Pali Rohárd66bab32011-11-01 01:43:04 +0100829 }
830
831 val->intval = level;
832
833 return 0;
834}
835
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200836/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100837 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100838 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200839 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500840static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500841 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200842{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100843 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200844
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500845 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100846 if (volt < 0) {
847 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100848 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100849 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200850
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100851 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200852
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100853 return 0;
854}
855
Andrew F. Davis081bab22015-09-22 14:35:06 -0500856static int bq27xxx_simple_value(int value,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500857 union power_supply_propval *val)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100858{
859 if (value < 0)
860 return value;
861
862 val->intval = value;
863
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200864 return 0;
865}
866
Andrew F. Davis081bab22015-09-22 14:35:06 -0500867static int bq27xxx_battery_get_property(struct power_supply *psy,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700868 enum power_supply_property psp,
869 union power_supply_propval *val)
870{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200871 int ret = 0;
Andrew F. Davis081bab22015-09-22 14:35:06 -0500872 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200873
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100874 mutex_lock(&di->lock);
875 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
876 cancel_delayed_work_sync(&di->work);
Andrew F. Davis081bab22015-09-22 14:35:06 -0500877 bq27xxx_battery_poll(&di->work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100878 }
879 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100880
881 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200882 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700883
884 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200885 case POWER_SUPPLY_PROP_STATUS:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500886 ret = bq27xxx_battery_status(di, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200887 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700888 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500889 ret = bq27xxx_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200890 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700891 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100892 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700893 break;
894 case POWER_SUPPLY_PROP_CURRENT_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500895 ret = bq27xxx_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700896 break;
897 case POWER_SUPPLY_PROP_CAPACITY:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500898 ret = bq27xxx_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700899 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100900 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500901 ret = bq27xxx_battery_capacity_level(di, val);
Pali Rohárd66bab32011-11-01 01:43:04 +0100902 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700903 case POWER_SUPPLY_PROP_TEMP:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500904 ret = bq27xxx_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +0100905 if (ret == 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500906 val->intval -= 2731; /* convert decidegree k to c */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700907 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200908 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500909 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200910 break;
911 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500912 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200913 break;
914 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500915 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200916 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200917 case POWER_SUPPLY_PROP_TECHNOLOGY:
918 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
919 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100920 case POWER_SUPPLY_PROP_CHARGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500921 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100922 break;
923 case POWER_SUPPLY_PROP_CHARGE_FULL:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500924 ret = bq27xxx_simple_value(di->cache.charge_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100925 break;
926 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500927 ret = bq27xxx_simple_value(di->charge_design_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100928 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200929 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500930 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100931 break;
932 case POWER_SUPPLY_PROP_ENERGY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500933 ret = bq27xxx_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100934 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530935 case POWER_SUPPLY_PROP_POWER_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500936 ret = bq27xxx_simple_value(di->cache.power_avg, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530937 break;
938 case POWER_SUPPLY_PROP_HEALTH:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500939 ret = bq27xxx_simple_value(di->cache.health, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530940 break;
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500941 case POWER_SUPPLY_PROP_MANUFACTURER:
942 val->strval = BQ27XXX_MANUFACTURER;
943 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700944 default:
945 return -EINVAL;
946 }
947
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200948 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700949}
950
Andrew F. Davis081bab22015-09-22 14:35:06 -0500951static void bq27xxx_external_power_changed(struct power_supply *psy)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100952{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500953 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100954
955 cancel_delayed_work_sync(&di->work);
956 schedule_delayed_work(&di->work, 0);
957}
958
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600959int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700960{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100961 struct power_supply_desc *psy_desc;
962 struct power_supply_config psy_cfg = { .drv_data = di, };
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200963
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600964 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
965 mutex_init(&di->lock);
966 di->regs = bq27xxx_regs[di->chip];
967
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100968 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
969 if (!psy_desc)
970 return -ENOMEM;
971
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600972 psy_desc->name = di->name;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100973 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500974 psy_desc->properties = bq27xxx_battery_props[di->chip].props;
975 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
Andrew F. Davis081bab22015-09-22 14:35:06 -0500976 psy_desc->get_property = bq27xxx_battery_get_property;
977 psy_desc->external_power_changed = bq27xxx_external_power_changed;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100978
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100979 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
980 if (IS_ERR(di->bat)) {
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600981 dev_err(di->dev, "failed to register battery\n");
982 return PTR_ERR(di->bat);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200983 }
984
985 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
986
Andrew F. Davis081bab22015-09-22 14:35:06 -0500987 bq27xxx_battery_update(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100988
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200989 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700990}
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600991EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700992
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600993void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100994{
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100995 /*
Andrew F. Davis081bab22015-09-22 14:35:06 -0500996 * power_supply_unregister call bq27xxx_battery_get_property which
997 * call bq27xxx_battery_poll.
998 * Make sure that bq27xxx_battery_poll will not call
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100999 * schedule_delayed_work again after unregister (which cause OOPS).
1000 */
1001 poll_interval = 0;
1002
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001003 cancel_delayed_work_sync(&di->work);
1004
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001005 power_supply_unregister(di->bat);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001006
1007 mutex_destroy(&di->lock);
1008}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001009EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001010
Andrew F. Davis081bab22015-09-22 14:35:06 -05001011static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1012 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001013{
1014 struct device *dev = di->dev;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001015 struct bq27xxx_platform_data *pdata = dev->platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001016 unsigned int timeout = 3;
1017 int upper, lower;
1018 int temp;
1019
1020 if (!single) {
1021 /* Make sure the value has not changed in between reading the
1022 * lower and the upper part */
1023 upper = pdata->read(dev, reg + 1);
1024 do {
1025 temp = upper;
1026 if (upper < 0)
1027 return upper;
1028
1029 lower = pdata->read(dev, reg);
1030 if (lower < 0)
1031 return lower;
1032
1033 upper = pdata->read(dev, reg + 1);
1034 } while (temp != upper && --timeout);
1035
1036 if (timeout == 0)
1037 return -EIO;
1038
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001039 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001040 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001041
1042 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001043}
1044
Andrew F. Davis081bab22015-09-22 14:35:06 -05001045static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001046{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001047 struct bq27xxx_device_info *di;
1048 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001049
1050 if (!pdata) {
1051 dev_err(&pdev->dev, "no platform_data supplied\n");
1052 return -EINVAL;
1053 }
1054
1055 if (!pdata->read) {
1056 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1057 return -EINVAL;
1058 }
1059
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001060 if (!pdata->chip) {
1061 dev_err(&pdev->dev, "no device supplied\n");
1062 return -EINVAL;
1063 }
1064
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301065 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001066 if (!di)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001067 return -ENOMEM;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001068
1069 platform_set_drvdata(pdev, di);
1070
1071 di->dev = &pdev->dev;
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001072 di->chip = pdata->chip;
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001073 di->name = pdata->name ?: dev_name(&pdev->dev);
1074 di->bus.read = bq27xxx_battery_platform_read;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001075
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001076 return bq27xxx_battery_setup(di);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001077}
1078
Andrew F. Davis081bab22015-09-22 14:35:06 -05001079static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001080{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001081 struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001082
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001083 bq27xxx_battery_teardown(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001084
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001085 return 0;
1086}
1087
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001088static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
1089 { "bq27000-battery", },
1090 { /* sentinel */ }
1091};
1092MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
1093
Pali Rohár7df3a742016-02-21 12:28:22 +01001094#ifdef CONFIG_OF
1095static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
1096 { .compatible = "ti,bq27000" },
1097 {},
1098};
1099MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
1100#endif
1101
Andrew F. Davis081bab22015-09-22 14:35:06 -05001102static struct platform_driver bq27xxx_battery_platform_driver = {
1103 .probe = bq27xxx_battery_platform_probe,
1104 .remove = bq27xxx_battery_platform_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001105 .driver = {
1106 .name = "bq27000-battery",
Pali Rohár7df3a742016-02-21 12:28:22 +01001107 .of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001108 },
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001109 .id_table = bq27xxx_battery_platform_id_table,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001110};
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001111module_platform_driver(bq27xxx_battery_platform_driver);
Marek Belisko8ebb7e92015-04-30 22:05:58 +02001112
Pali Rohár7df3a742016-02-21 12:28:22 +01001113MODULE_ALIAS("platform:bq27000-battery");
1114
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001115MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
Andrew F. Davis081bab22015-09-22 14:35:06 -05001116MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001117MODULE_LICENSE("GPL");