blob: 6b027a418943a50b56354eb50846700525733ecd [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>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070049
Andrew F. Davis081bab22015-09-22 14:35:06 -050050#include <linux/power/bq27xxx_battery.h>
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020051
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050052#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070053
Andrew F. Davisdb04fc52015-07-22 16:51:53 -050054#define BQ27XXX_MANUFACTURER "Texas Instruments"
55
Andrew F. Davisd74534c2015-09-22 14:35:09 -050056/* BQ27XXX Flags */
57#define BQ27XXX_FLAG_DSC BIT(0)
58#define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
59#define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
60#define BQ27XXX_FLAG_FC BIT(9)
61#define BQ27XXX_FLAG_OTD BIT(14)
62#define BQ27XXX_FLAG_OTC BIT(15)
Andrew F. Davis74aab842015-09-22 14:35:10 -050063#define BQ27XXX_FLAG_UT BIT(14)
64#define BQ27XXX_FLAG_OT BIT(15)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070065
Andrew F. Davisd74534c2015-09-22 14:35:09 -050066/* BQ27000 has different layout for Flags register */
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050067#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
68#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
69#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
70#define BQ27000_FLAG_FC BIT(5)
71#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020072
Andrew F. Davisd74534c2015-09-22 14:35:09 -050073#define BQ27XXX_RS (20) /* Resistor sense mOhm */
74#define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
75#define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
Pali Rohára2e51182010-05-24 20:52:13 +020076
Andrew F. Davisd74534c2015-09-22 14:35:09 -050077#define INVALID_REG_ADDR 0xff
78
79/*
80 * bq27xxx_reg_index - Register names
81 *
82 * These are indexes into a device's register mapping array.
83 */
84enum bq27xxx_reg_index {
85 BQ27XXX_REG_CTRL = 0, /* Control */
86 BQ27XXX_REG_TEMP, /* Temperature */
87 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
88 BQ27XXX_REG_VOLT, /* Voltage */
89 BQ27XXX_REG_AI, /* Average Current */
90 BQ27XXX_REG_FLAGS, /* Flags */
91 BQ27XXX_REG_TTE, /* Time-to-Empty */
92 BQ27XXX_REG_TTF, /* Time-to-Full */
93 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
94 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
95 BQ27XXX_REG_NAC, /* Nominal Available Capacity */
96 BQ27XXX_REG_FCC, /* Full Charge Capacity */
97 BQ27XXX_REG_CYCT, /* Cycle Count */
98 BQ27XXX_REG_AE, /* Available Energy */
99 BQ27XXX_REG_SOC, /* State-of-Charge */
100 BQ27XXX_REG_DCAP, /* Design Capacity */
101 BQ27XXX_REG_AP, /* Average Power */
102};
103
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500104/* Register mappings */
105static u8 bq27000_regs[] = {
106 0x00, /* CONTROL */
107 0x06, /* TEMP */
108 INVALID_REG_ADDR, /* INT TEMP - NA*/
109 0x08, /* VOLT */
110 0x14, /* AVG CURR */
111 0x0a, /* FLAGS */
112 0x16, /* TTE */
113 0x18, /* TTF */
114 0x1c, /* TTES */
115 0x26, /* TTECP */
116 0x0c, /* NAC */
117 0x12, /* LMD(FCC) */
118 0x2a, /* CYCT */
119 0x22, /* AE */
120 0x0b, /* SOC(RSOC) */
121 0x76, /* DCAP(ILMD) */
122 0x24, /* AP */
123};
124
125static u8 bq27010_regs[] = {
126 0x00, /* CONTROL */
127 0x06, /* TEMP */
128 INVALID_REG_ADDR, /* INT TEMP - NA*/
129 0x08, /* VOLT */
130 0x14, /* AVG CURR */
131 0x0a, /* FLAGS */
132 0x16, /* TTE */
133 0x18, /* TTF */
134 0x1c, /* TTES */
135 0x26, /* TTECP */
136 0x0c, /* NAC */
137 0x12, /* LMD(FCC) */
138 0x2a, /* CYCT */
139 INVALID_REG_ADDR, /* AE - NA */
140 0x0b, /* SOC(RSOC) */
141 0x76, /* DCAP(ILMD) */
142 INVALID_REG_ADDR, /* AP - NA */
143};
144
145static u8 bq27500_regs[] = {
146 0x00, /* CONTROL */
147 0x06, /* TEMP */
148 0x28, /* INT TEMP */
149 0x08, /* VOLT */
150 0x14, /* AVG CURR */
151 0x0a, /* FLAGS */
152 0x16, /* TTE */
153 INVALID_REG_ADDR, /* TTF - NA */
154 0x1a, /* TTES */
155 INVALID_REG_ADDR, /* TTECP - NA */
156 0x0c, /* NAC */
157 0x12, /* LMD(FCC) */
H. Nikolaus Schaller099867a2015-12-17 11:12:54 +0100158 0x2a, /* CYCT */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500159 INVALID_REG_ADDR, /* AE - NA */
H. Nikolaus Schaller099867a2015-12-17 11:12:54 +0100160 0x2c, /* SOC(RSOC) */
161 0x3c, /* DCAP(ILMD) */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500162 INVALID_REG_ADDR, /* AP - NA */
163};
164
165static u8 bq27530_regs[] = {
166 0x00, /* CONTROL */
167 0x06, /* TEMP */
168 0x32, /* INT TEMP */
169 0x08, /* VOLT */
170 0x14, /* AVG CURR */
171 0x0a, /* FLAGS */
172 0x16, /* TTE */
173 INVALID_REG_ADDR, /* TTF - NA */
174 INVALID_REG_ADDR, /* TTES - NA */
175 INVALID_REG_ADDR, /* TTECP - NA */
176 0x0c, /* NAC */
177 0x12, /* LMD(FCC) */
178 0x2a, /* CYCT */
179 INVALID_REG_ADDR, /* AE - NA */
180 0x2c, /* SOC(RSOC) */
181 INVALID_REG_ADDR, /* DCAP - NA */
182 0x24, /* AP */
183};
184
185static u8 bq27541_regs[] = {
186 0x00, /* CONTROL */
187 0x06, /* TEMP */
188 0x28, /* INT TEMP */
189 0x08, /* VOLT */
190 0x14, /* AVG CURR */
191 0x0a, /* FLAGS */
192 0x16, /* TTE */
193 INVALID_REG_ADDR, /* TTF - NA */
194 INVALID_REG_ADDR, /* TTES - NA */
195 INVALID_REG_ADDR, /* TTECP - NA */
196 0x0c, /* NAC */
197 0x12, /* LMD(FCC) */
198 0x2a, /* CYCT */
199 INVALID_REG_ADDR, /* AE - NA */
200 0x2c, /* SOC(RSOC) */
201 0x3c, /* DCAP */
Liu Xiang265b6042016-01-09 22:10:39 +0800202 0x24, /* AP */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500203};
204
205static u8 bq27545_regs[] = {
206 0x00, /* CONTROL */
207 0x06, /* TEMP */
208 0x28, /* INT TEMP */
209 0x08, /* VOLT */
210 0x14, /* AVG CURR */
211 0x0a, /* FLAGS */
212 0x16, /* TTE */
213 INVALID_REG_ADDR, /* TTF - NA */
214 INVALID_REG_ADDR, /* TTES - NA */
215 INVALID_REG_ADDR, /* TTECP - NA */
216 0x0c, /* NAC */
217 0x12, /* LMD(FCC) */
218 0x2a, /* CYCT */
219 INVALID_REG_ADDR, /* AE - NA */
220 0x2c, /* SOC(RSOC) */
221 INVALID_REG_ADDR, /* DCAP - NA */
222 0x24, /* AP */
223};
224
225static u8 bq27421_regs[] = {
226 0x00, /* CONTROL */
227 0x02, /* TEMP */
228 0x1e, /* INT TEMP */
229 0x04, /* VOLT */
230 0x10, /* AVG CURR */
231 0x06, /* FLAGS */
232 INVALID_REG_ADDR, /* TTE - NA */
233 INVALID_REG_ADDR, /* TTF - NA */
234 INVALID_REG_ADDR, /* TTES - NA */
235 INVALID_REG_ADDR, /* TTECP - NA */
236 0x08, /* NAC */
237 0x0e, /* FCC */
238 INVALID_REG_ADDR, /* CYCT - NA */
239 INVALID_REG_ADDR, /* AE - NA */
240 0x1c, /* SOC */
241 0x3c, /* DCAP */
242 0x18, /* AP */
243};
244
245static u8 *bq27xxx_regs[] = {
246 [BQ27000] = bq27000_regs,
247 [BQ27010] = bq27010_regs,
248 [BQ27500] = bq27500_regs,
249 [BQ27530] = bq27530_regs,
250 [BQ27541] = bq27541_regs,
251 [BQ27545] = bq27545_regs,
252 [BQ27421] = bq27421_regs,
253};
254
255static enum power_supply_property bq27000_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200256 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700257 POWER_SUPPLY_PROP_PRESENT,
258 POWER_SUPPLY_PROP_VOLTAGE_NOW,
259 POWER_SUPPLY_PROP_CURRENT_NOW,
260 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100261 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700262 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200263 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
264 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
265 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200266 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100267 POWER_SUPPLY_PROP_CHARGE_FULL,
268 POWER_SUPPLY_PROP_CHARGE_NOW,
269 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200270 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100271 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530272 POWER_SUPPLY_PROP_POWER_AVG,
273 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500274 POWER_SUPPLY_PROP_MANUFACTURER,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700275};
276
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500277static enum power_supply_property bq27010_battery_props[] = {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700278 POWER_SUPPLY_PROP_STATUS,
279 POWER_SUPPLY_PROP_PRESENT,
280 POWER_SUPPLY_PROP_VOLTAGE_NOW,
281 POWER_SUPPLY_PROP_CURRENT_NOW,
282 POWER_SUPPLY_PROP_CAPACITY,
283 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
284 POWER_SUPPLY_PROP_TEMP,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500285 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
286 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
287 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700288 POWER_SUPPLY_PROP_TECHNOLOGY,
289 POWER_SUPPLY_PROP_CHARGE_FULL,
290 POWER_SUPPLY_PROP_CHARGE_NOW,
291 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500292 POWER_SUPPLY_PROP_CYCLE_COUNT,
293 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500294 POWER_SUPPLY_PROP_MANUFACTURER,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700295};
296
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500297static enum power_supply_property bq27500_battery_props[] = {
298 POWER_SUPPLY_PROP_STATUS,
299 POWER_SUPPLY_PROP_PRESENT,
300 POWER_SUPPLY_PROP_VOLTAGE_NOW,
301 POWER_SUPPLY_PROP_CURRENT_NOW,
302 POWER_SUPPLY_PROP_CAPACITY,
303 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
304 POWER_SUPPLY_PROP_TEMP,
305 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
306 POWER_SUPPLY_PROP_TECHNOLOGY,
307 POWER_SUPPLY_PROP_CHARGE_FULL,
308 POWER_SUPPLY_PROP_CHARGE_NOW,
309 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
310 POWER_SUPPLY_PROP_CYCLE_COUNT,
311 POWER_SUPPLY_PROP_HEALTH,
312 POWER_SUPPLY_PROP_MANUFACTURER,
313};
314
315static enum power_supply_property bq27530_battery_props[] = {
316 POWER_SUPPLY_PROP_STATUS,
317 POWER_SUPPLY_PROP_PRESENT,
318 POWER_SUPPLY_PROP_VOLTAGE_NOW,
319 POWER_SUPPLY_PROP_CURRENT_NOW,
320 POWER_SUPPLY_PROP_CAPACITY,
321 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
322 POWER_SUPPLY_PROP_TEMP,
323 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
324 POWER_SUPPLY_PROP_TECHNOLOGY,
325 POWER_SUPPLY_PROP_CHARGE_FULL,
326 POWER_SUPPLY_PROP_CHARGE_NOW,
327 POWER_SUPPLY_PROP_POWER_AVG,
328 POWER_SUPPLY_PROP_HEALTH,
329 POWER_SUPPLY_PROP_CYCLE_COUNT,
330 POWER_SUPPLY_PROP_MANUFACTURER,
331};
332
333static enum power_supply_property bq27541_battery_props[] = {
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700334 POWER_SUPPLY_PROP_STATUS,
335 POWER_SUPPLY_PROP_PRESENT,
336 POWER_SUPPLY_PROP_VOLTAGE_NOW,
337 POWER_SUPPLY_PROP_CURRENT_NOW,
338 POWER_SUPPLY_PROP_CAPACITY,
339 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
340 POWER_SUPPLY_PROP_TEMP,
341 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
342 POWER_SUPPLY_PROP_TECHNOLOGY,
343 POWER_SUPPLY_PROP_CHARGE_FULL,
344 POWER_SUPPLY_PROP_CHARGE_NOW,
345 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
346 POWER_SUPPLY_PROP_CYCLE_COUNT,
347 POWER_SUPPLY_PROP_POWER_AVG,
348 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500349 POWER_SUPPLY_PROP_MANUFACTURER,
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700350};
351
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500352static enum power_supply_property bq27545_battery_props[] = {
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100353 POWER_SUPPLY_PROP_STATUS,
354 POWER_SUPPLY_PROP_PRESENT,
355 POWER_SUPPLY_PROP_VOLTAGE_NOW,
356 POWER_SUPPLY_PROP_CURRENT_NOW,
357 POWER_SUPPLY_PROP_CAPACITY,
358 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
359 POWER_SUPPLY_PROP_TEMP,
360 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
361 POWER_SUPPLY_PROP_TECHNOLOGY,
362 POWER_SUPPLY_PROP_CHARGE_FULL,
363 POWER_SUPPLY_PROP_CHARGE_NOW,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500364 POWER_SUPPLY_PROP_HEALTH,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100365 POWER_SUPPLY_PROP_CYCLE_COUNT,
366 POWER_SUPPLY_PROP_POWER_AVG,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500367 POWER_SUPPLY_PROP_MANUFACTURER,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100368};
369
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500370static enum power_supply_property bq27421_battery_props[] = {
371 POWER_SUPPLY_PROP_STATUS,
372 POWER_SUPPLY_PROP_PRESENT,
373 POWER_SUPPLY_PROP_VOLTAGE_NOW,
374 POWER_SUPPLY_PROP_CURRENT_NOW,
375 POWER_SUPPLY_PROP_CAPACITY,
376 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
377 POWER_SUPPLY_PROP_TEMP,
378 POWER_SUPPLY_PROP_TECHNOLOGY,
379 POWER_SUPPLY_PROP_CHARGE_FULL,
380 POWER_SUPPLY_PROP_CHARGE_NOW,
381 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
382 POWER_SUPPLY_PROP_MANUFACTURER,
383};
384
385#define BQ27XXX_PROP(_id, _prop) \
386 [_id] = { \
387 .props = _prop, \
388 .size = ARRAY_SIZE(_prop), \
389 }
390
391static struct {
392 enum power_supply_property *props;
393 size_t size;
394} bq27xxx_battery_props[] = {
395 BQ27XXX_PROP(BQ27000, bq27000_battery_props),
396 BQ27XXX_PROP(BQ27010, bq27010_battery_props),
397 BQ27XXX_PROP(BQ27500, bq27500_battery_props),
398 BQ27XXX_PROP(BQ27530, bq27530_battery_props),
399 BQ27XXX_PROP(BQ27541, bq27541_battery_props),
400 BQ27XXX_PROP(BQ27545, bq27545_battery_props),
401 BQ27XXX_PROP(BQ27421, bq27421_battery_props),
402};
403
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100404static unsigned int poll_interval = 360;
405module_param(poll_interval, uint, 0644);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500406MODULE_PARM_DESC(poll_interval,
407 "battery poll interval in seconds - 0 disables polling");
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100408
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700409/*
Andrew F. Davis081bab22015-09-22 14:35:06 -0500410 * Common code for BQ27xxx devices
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700411 */
412
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500413static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500414 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700415{
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500416 /* Reports EINVAL for invalid/missing registers */
417 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
418 return -EINVAL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100419
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500420 return di->bus.read(di, di->regs[reg_index], single);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700421}
422
423/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500424 * Return the battery State-of-Charge
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100425 * Or < 0 if something fails.
426 */
Andrew F. Davisc5709032015-09-22 14:35:08 -0500427static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100428{
Andrew F. Davisc5709032015-09-22 14:35:08 -0500429 int soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100430
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100431 if (di->chip == BQ27000 || di->chip == BQ27010)
432 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
433 else
434 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100435
Andrew F. Davisc5709032015-09-22 14:35:08 -0500436 if (soc < 0)
437 dev_dbg(di->dev, "error reading State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100438
Andrew F. Davisc5709032015-09-22 14:35:08 -0500439 return soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100440}
441
442/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100443 * Return a battery charge value in µAh
444 * Or < 0 if something fails.
445 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500446static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
Pali Rohár631c17e2011-02-01 00:08:02 +0100447{
448 int charge;
449
Andrew F. Davis081bab22015-09-22 14:35:06 -0500450 charge = bq27xxx_read(di, reg, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100451 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100452 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
453 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100454 return charge;
455 }
456
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500457 if (di->chip == BQ27000 || di->chip == BQ27010)
458 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100459 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500460 charge *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100461
462 return charge;
463}
464
465/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500466 * Return the battery Nominal available capacity in µAh
Pali Rohár631c17e2011-02-01 00:08:02 +0100467 * Or < 0 if something fails.
468 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500469static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100470{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100471 int flags;
Pali Roháre59ec4a2012-11-10 15:42:48 +0100472
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500473 if (di->chip == BQ27000 || di->chip == BQ27010) {
474 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
475 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
476 return -ENODATA;
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500477 }
Pali Rohár631c17e2011-02-01 00:08:02 +0100478
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500479 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
480}
481
482/*
483 * Return the battery Full Charge Capacity in µAh
484 * Or < 0 if something fails.
485 */
486static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
487{
488 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
489}
490
491/*
492 * Return the Design Capacity in µAh
493 * Or < 0 if something fails.
494 */
495static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
496{
497 int dcap;
498
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100499 if (di->chip == BQ27000 || di->chip == BQ27010)
500 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
501 else
502 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500503
504 if (dcap < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100505 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500506 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100507 }
508
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500509 if (di->chip == BQ27000 || di->chip == BQ27010)
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +0100510 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100511 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500512 dcap *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100513
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500514 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100515}
516
517/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100518 * Return the battery Available energy in µWh
519 * Or < 0 if something fails.
520 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500521static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
Pali Rohára8f6bd22011-11-01 01:43:06 +0100522{
523 int ae;
524
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500525 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
Pali Rohára8f6bd22011-11-01 01:43:06 +0100526 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100527 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100528 return ae;
529 }
530
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500531 if (di->chip == BQ27000 || di->chip == BQ27010)
532 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100533 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500534 ae *= 1000;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100535
536 return ae;
537}
538
539/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100540 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100541 * Or < 0 if something fails.
542 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500543static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
Pali Rohárd149e982011-11-01 01:43:07 +0100544{
545 int temp;
546
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500547 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
Pali Rohárd149e982011-11-01 01:43:07 +0100548 if (temp < 0) {
549 dev_err(di->dev, "error reading temperature\n");
550 return temp;
551 }
552
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500553 if (di->chip == BQ27000 || di->chip == BQ27010)
Pali Rohár5dc34432013-02-02 11:06:09 +0100554 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100555
556 return temp;
557}
558
559/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100560 * Return the battery Cycle count total
561 * Or < 0 if something fails.
562 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500563static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100564{
565 int cyct;
566
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500567 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100568 if (cyct < 0)
569 dev_err(di->dev, "error reading cycle count total\n");
570
571 return cyct;
572}
573
574/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100575 * Read a time register.
576 * Return < 0 if something fails.
577 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500578static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100579{
580 int tval;
581
Andrew F. Davis081bab22015-09-22 14:35:06 -0500582 tval = bq27xxx_read(di, reg, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100583 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100584 dev_dbg(di->dev, "error reading time register %02x: %d\n",
585 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100586 return tval;
587 }
588
589 if (tval == 65535)
590 return -ENODATA;
591
592 return tval * 60;
593}
594
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530595/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500596 * Read an average power register.
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530597 * Return < 0 if something fails.
598 */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500599static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530600{
601 int tval;
602
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500603 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530604 if (tval < 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500605 dev_err(di->dev, "error reading average power register %02x: %d\n",
606 BQ27XXX_REG_AP, tval);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530607 return tval;
608 }
609
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500610 if (di->chip == BQ27000 || di->chip == BQ27010)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500611 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500612 else
613 return tval;
614}
615
616/*
617 * Returns true if a battery over temperature condition is detected
618 */
Andrew F. Davis74aab842015-09-22 14:35:10 -0500619static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500620{
Andrew F. Davis74aab842015-09-22 14:35:10 -0500621 if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500622 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
Andrew F. Davis74aab842015-09-22 14:35:10 -0500623 if (di->chip == BQ27530 || di->chip == BQ27421)
624 return flags & BQ27XXX_FLAG_OT;
625
626 return false;
627}
628
629/*
630 * Returns true if a battery under temperature condition is detected
631 */
632static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
633{
634 if (di->chip == BQ27530 || di->chip == BQ27421)
635 return flags & BQ27XXX_FLAG_UT;
636
637 return false;
638}
639
640/*
641 * Returns true if a low state of charge condition is detected
642 */
643static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
644{
645 if (di->chip == BQ27000 || di->chip == BQ27010)
646 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500647 else
Andrew F. Davis74aab842015-09-22 14:35:10 -0500648 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530649}
650
651/*
652 * Read flag register.
653 * Return < 0 if something fails.
654 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500655static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530656{
Andrzej Hajda2edd69a2015-09-28 10:51:27 +0200657 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530658
Andrew F. Davis74aab842015-09-22 14:35:10 -0500659 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
660 if (flags < 0) {
661 dev_err(di->dev, "error reading flag register:%d\n", flags);
662 return flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530663 }
664
Andrew F. Davis74aab842015-09-22 14:35:10 -0500665 /* Unlikely but important to return first */
666 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
667 return POWER_SUPPLY_HEALTH_OVERHEAT;
668 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
669 return POWER_SUPPLY_HEALTH_COLD;
670 if (unlikely(bq27xxx_battery_dead(di, flags)))
671 return POWER_SUPPLY_HEALTH_DEAD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530672
Andrew F. Davis74aab842015-09-22 14:35:10 -0500673 return POWER_SUPPLY_HEALTH_GOOD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530674}
675
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600676void bq27xxx_battery_update(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100677{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500678 struct bq27xxx_reg_cache cache = {0, };
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500679 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
680 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100681
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500682 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
Marek Belisko3dd843e2014-06-30 21:49:56 +0200683 if ((cache.flags & 0xff) == 0xff)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500684 cache.flags = -1; /* read error */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100685 if (cache.flags >= 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500686 cache.temperature = bq27xxx_battery_read_temperature(di);
687 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
NeilBrown1f94b252015-10-07 20:42:38 +0200688 dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100689 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100690 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100691 cache.time_to_empty = -ENODATA;
692 cache.time_to_empty_avg = -ENODATA;
693 cache.time_to_full = -ENODATA;
694 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530695 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100696 } else {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500697 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
698 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
699 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
700 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
701 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
702 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
703 cache.charge_full = bq27xxx_battery_read_fcc(di);
Andrew F. Davisc5709032015-09-22 14:35:08 -0500704 cache.capacity = bq27xxx_battery_read_soc(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500705 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500706 cache.energy = bq27xxx_battery_read_energy(di);
Andrew F. Davis081bab22015-09-22 14:35:06 -0500707 cache.health = bq27xxx_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100708 }
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500709 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500710 cache.cycle_count = bq27xxx_battery_read_cyct(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500711 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
712 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100713
Pali Rohár631c17e2011-02-01 00:08:02 +0100714 /* We only have to read charge design full once */
715 if (di->charge_design_full <= 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500716 di->charge_design_full = bq27xxx_battery_read_dcap(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100717 }
718
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700719 if (di->cache.capacity != cache.capacity)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100720 power_supply_changed(di->bat);
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700721
722 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
723 di->cache = cache;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100724
725 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700726}
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600727EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700728
Andrew F. Davis081bab22015-09-22 14:35:06 -0500729static void bq27xxx_battery_poll(struct work_struct *work)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100730{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500731 struct bq27xxx_device_info *di =
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500732 container_of(work, struct bq27xxx_device_info,
733 work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100734
Andrew F. Davis081bab22015-09-22 14:35:06 -0500735 bq27xxx_battery_update(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100736
737 if (poll_interval > 0) {
738 /* The timer does not have to be accurate. */
739 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
740 schedule_delayed_work(&di->work, poll_interval * HZ);
741 }
742}
743
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700744/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100745 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700746 * Note that current can be negative signed as well
747 * Or 0 if something fails.
748 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500749static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500750 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700751{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100752 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100753 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700754
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500755 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100756 if (curr < 0) {
757 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100758 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100759 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200760
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500761 if (di->chip == BQ27000 || di->chip == BQ27010) {
762 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
Pali Rohárb68f6212011-11-01 01:43:03 +0100763 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200764 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74fd2010-02-27 17:06:44 +0200765 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200766 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100767
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500768 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
769 } else {
770 /* Other gauges return signed value */
771 val->intval = (int)((s16)curr) * 1000;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700772 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200773
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100774 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700775}
776
Andrew F. Davis081bab22015-09-22 14:35:06 -0500777static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500778 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200779{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200780 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200781
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500782 if (di->chip == BQ27000 || di->chip == BQ27010) {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200783 if (di->cache.flags & BQ27000_FLAG_FC)
784 status = POWER_SUPPLY_STATUS_FULL;
785 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200786 status = POWER_SUPPLY_STATUS_CHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100787 else if (power_supply_am_i_supplied(di->bat))
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200788 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200789 else
790 status = POWER_SUPPLY_STATUS_DISCHARGING;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500791 } else {
792 if (di->cache.flags & BQ27XXX_FLAG_FC)
793 status = POWER_SUPPLY_STATUS_FULL;
794 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
795 status = POWER_SUPPLY_STATUS_DISCHARGING;
796 else
797 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200798 }
799
800 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100801
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200802 return 0;
803}
804
Andrew F. Davis081bab22015-09-22 14:35:06 -0500805static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500806 union power_supply_propval *val)
Pali Rohárd66bab32011-11-01 01:43:04 +0100807{
808 int level;
809
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500810 if (di->chip == BQ27000 || di->chip == BQ27010) {
Pali Rohárd66bab32011-11-01 01:43:04 +0100811 if (di->cache.flags & BQ27000_FLAG_FC)
812 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
813 else if (di->cache.flags & BQ27000_FLAG_EDV1)
814 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
815 else if (di->cache.flags & BQ27000_FLAG_EDVF)
816 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
817 else
818 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500819 } else {
820 if (di->cache.flags & BQ27XXX_FLAG_FC)
821 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
822 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
823 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
824 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
825 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
826 else
827 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Pali Rohárd66bab32011-11-01 01:43:04 +0100828 }
829
830 val->intval = level;
831
832 return 0;
833}
834
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200835/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100836 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100837 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200838 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500839static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500840 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200841{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100842 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200843
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500844 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100845 if (volt < 0) {
846 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100847 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100848 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200849
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100850 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200851
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100852 return 0;
853}
854
Andrew F. Davis081bab22015-09-22 14:35:06 -0500855static int bq27xxx_simple_value(int value,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500856 union power_supply_propval *val)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100857{
858 if (value < 0)
859 return value;
860
861 val->intval = value;
862
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200863 return 0;
864}
865
Andrew F. Davis081bab22015-09-22 14:35:06 -0500866static int bq27xxx_battery_get_property(struct power_supply *psy,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700867 enum power_supply_property psp,
868 union power_supply_propval *val)
869{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200870 int ret = 0;
Andrew F. Davis081bab22015-09-22 14:35:06 -0500871 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200872
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100873 mutex_lock(&di->lock);
874 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
875 cancel_delayed_work_sync(&di->work);
Andrew F. Davis081bab22015-09-22 14:35:06 -0500876 bq27xxx_battery_poll(&di->work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100877 }
878 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100879
880 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200881 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700882
883 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200884 case POWER_SUPPLY_PROP_STATUS:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500885 ret = bq27xxx_battery_status(di, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200886 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700887 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500888 ret = bq27xxx_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200889 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700890 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100891 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700892 break;
893 case POWER_SUPPLY_PROP_CURRENT_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500894 ret = bq27xxx_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700895 break;
896 case POWER_SUPPLY_PROP_CAPACITY:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500897 ret = bq27xxx_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700898 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100899 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500900 ret = bq27xxx_battery_capacity_level(di, val);
Pali Rohárd66bab32011-11-01 01:43:04 +0100901 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700902 case POWER_SUPPLY_PROP_TEMP:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500903 ret = bq27xxx_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +0100904 if (ret == 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500905 val->intval -= 2731; /* convert decidegree k to c */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700906 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200907 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500908 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200909 break;
910 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500911 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200912 break;
913 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500914 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200915 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200916 case POWER_SUPPLY_PROP_TECHNOLOGY:
917 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
918 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100919 case POWER_SUPPLY_PROP_CHARGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500920 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100921 break;
922 case POWER_SUPPLY_PROP_CHARGE_FULL:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500923 ret = bq27xxx_simple_value(di->cache.charge_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100924 break;
925 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500926 ret = bq27xxx_simple_value(di->charge_design_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100927 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200928 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500929 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100930 break;
931 case POWER_SUPPLY_PROP_ENERGY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500932 ret = bq27xxx_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100933 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530934 case POWER_SUPPLY_PROP_POWER_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500935 ret = bq27xxx_simple_value(di->cache.power_avg, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530936 break;
937 case POWER_SUPPLY_PROP_HEALTH:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500938 ret = bq27xxx_simple_value(di->cache.health, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530939 break;
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500940 case POWER_SUPPLY_PROP_MANUFACTURER:
941 val->strval = BQ27XXX_MANUFACTURER;
942 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700943 default:
944 return -EINVAL;
945 }
946
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200947 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700948}
949
Andrew F. Davis081bab22015-09-22 14:35:06 -0500950static void bq27xxx_external_power_changed(struct power_supply *psy)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100951{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500952 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100953
954 cancel_delayed_work_sync(&di->work);
955 schedule_delayed_work(&di->work, 0);
956}
957
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600958int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700959{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100960 struct power_supply_desc *psy_desc;
961 struct power_supply_config psy_cfg = { .drv_data = di, };
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200962
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600963 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
964 mutex_init(&di->lock);
965 di->regs = bq27xxx_regs[di->chip];
966
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100967 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
968 if (!psy_desc)
969 return -ENOMEM;
970
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600971 psy_desc->name = di->name;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100972 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500973 psy_desc->properties = bq27xxx_battery_props[di->chip].props;
974 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
Andrew F. Davis081bab22015-09-22 14:35:06 -0500975 psy_desc->get_property = bq27xxx_battery_get_property;
976 psy_desc->external_power_changed = bq27xxx_external_power_changed;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100977
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100978 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
979 if (IS_ERR(di->bat)) {
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600980 dev_err(di->dev, "failed to register battery\n");
981 return PTR_ERR(di->bat);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200982 }
983
984 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
985
Andrew F. Davis081bab22015-09-22 14:35:06 -0500986 bq27xxx_battery_update(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100987
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200988 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700989}
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600990EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700991
Andrew F. Davis703df6c2015-11-23 10:53:51 -0600992void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100993{
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100994 /*
Andrew F. Davis081bab22015-09-22 14:35:06 -0500995 * power_supply_unregister call bq27xxx_battery_get_property which
996 * call bq27xxx_battery_poll.
997 * Make sure that bq27xxx_battery_poll will not call
Pali Rohár8cfaaa82011-11-01 01:43:11 +0100998 * schedule_delayed_work again after unregister (which cause OOPS).
999 */
1000 poll_interval = 0;
1001
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001002 cancel_delayed_work_sync(&di->work);
1003
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001004 power_supply_unregister(di->bat);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001005
1006 mutex_destroy(&di->lock);
1007}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001008EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001009
Andrew F. Davis081bab22015-09-22 14:35:06 -05001010static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1011 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001012{
1013 struct device *dev = di->dev;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001014 struct bq27xxx_platform_data *pdata = dev->platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001015 unsigned int timeout = 3;
1016 int upper, lower;
1017 int temp;
1018
1019 if (!single) {
1020 /* Make sure the value has not changed in between reading the
1021 * lower and the upper part */
1022 upper = pdata->read(dev, reg + 1);
1023 do {
1024 temp = upper;
1025 if (upper < 0)
1026 return upper;
1027
1028 lower = pdata->read(dev, reg);
1029 if (lower < 0)
1030 return lower;
1031
1032 upper = pdata->read(dev, reg + 1);
1033 } while (temp != upper && --timeout);
1034
1035 if (timeout == 0)
1036 return -EIO;
1037
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001038 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001039 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001040
1041 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001042}
1043
Andrew F. Davis081bab22015-09-22 14:35:06 -05001044static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001045{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001046 struct bq27xxx_device_info *di;
1047 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001048
1049 if (!pdata) {
1050 dev_err(&pdev->dev, "no platform_data supplied\n");
1051 return -EINVAL;
1052 }
1053
1054 if (!pdata->read) {
1055 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1056 return -EINVAL;
1057 }
1058
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001059 if (!pdata->chip) {
1060 dev_err(&pdev->dev, "no device supplied\n");
1061 return -EINVAL;
1062 }
1063
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301064 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001065 if (!di)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001066 return -ENOMEM;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001067
1068 platform_set_drvdata(pdev, di);
1069
1070 di->dev = &pdev->dev;
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001071 di->chip = pdata->chip;
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001072 di->name = pdata->name ?: dev_name(&pdev->dev);
1073 di->bus.read = bq27xxx_battery_platform_read;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001074
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001075 return bq27xxx_battery_setup(di);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001076}
1077
Andrew F. Davis081bab22015-09-22 14:35:06 -05001078static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001079{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001080 struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001081
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001082 bq27xxx_battery_teardown(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001083
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001084 return 0;
1085}
1086
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001087static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
1088 { "bq27000-battery", },
1089 { /* sentinel */ }
1090};
1091MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);
1092
Andrew F. Davis081bab22015-09-22 14:35:06 -05001093static struct platform_driver bq27xxx_battery_platform_driver = {
1094 .probe = bq27xxx_battery_platform_probe,
1095 .remove = bq27xxx_battery_platform_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001096 .driver = {
1097 .name = "bq27000-battery",
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001098 },
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001099 .id_table = bq27xxx_battery_platform_id_table,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001100};
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001101module_platform_driver(bq27xxx_battery_platform_driver);
Marek Belisko8ebb7e92015-04-30 22:05:58 +02001102
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001103MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
Andrew F. Davis081bab22015-09-22 14:35:06 -05001104MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001105MODULE_LICENSE("GPL");