blob: 880233ce9343809999ead8d957d65054c076b592 [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>
48#include <linux/idr.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070049#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Andrew F. Davis8807feb2015-09-22 14:35:11 -050051#include <linux/interrupt.h>
Harvey Harrison8aef7e82008-09-22 14:53:50 -070052#include <asm/unaligned.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070053
Andrew F. Davis081bab22015-09-22 14:35:06 -050054#include <linux/power/bq27xxx_battery.h>
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020055
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050056#define DRIVER_VERSION "1.2.0"
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070057
Andrew F. Davisdb04fc52015-07-22 16:51:53 -050058#define BQ27XXX_MANUFACTURER "Texas Instruments"
59
Andrew F. Davisd74534c2015-09-22 14:35:09 -050060/* BQ27XXX Flags */
61#define BQ27XXX_FLAG_DSC BIT(0)
62#define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
63#define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
64#define BQ27XXX_FLAG_FC BIT(9)
65#define BQ27XXX_FLAG_OTD BIT(14)
66#define BQ27XXX_FLAG_OTC BIT(15)
Andrew F. Davis74aab842015-09-22 14:35:10 -050067#define BQ27XXX_FLAG_UT BIT(14)
68#define BQ27XXX_FLAG_OT BIT(15)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070069
Andrew F. Davisd74534c2015-09-22 14:35:09 -050070/* BQ27000 has different layout for Flags register */
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050071#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
72#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
73#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
74#define BQ27000_FLAG_FC BIT(5)
75#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020076
Andrew F. Davisd74534c2015-09-22 14:35:09 -050077#define BQ27XXX_RS (20) /* Resistor sense mOhm */
78#define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
79#define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
Pali Rohára2e51182010-05-24 20:52:13 +020080
Andrew F. Davis081bab22015-09-22 14:35:06 -050081struct bq27xxx_device_info;
82struct bq27xxx_access_methods {
83 int (*read)(struct bq27xxx_device_info *di, u8 reg, bool single);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070084};
85
Andrew F. Davisd74534c2015-09-22 14:35:09 -050086#define INVALID_REG_ADDR 0xff
87
88/*
89 * bq27xxx_reg_index - Register names
90 *
91 * These are indexes into a device's register mapping array.
92 */
93enum bq27xxx_reg_index {
94 BQ27XXX_REG_CTRL = 0, /* Control */
95 BQ27XXX_REG_TEMP, /* Temperature */
96 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
97 BQ27XXX_REG_VOLT, /* Voltage */
98 BQ27XXX_REG_AI, /* Average Current */
99 BQ27XXX_REG_FLAGS, /* Flags */
100 BQ27XXX_REG_TTE, /* Time-to-Empty */
101 BQ27XXX_REG_TTF, /* Time-to-Full */
102 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
103 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
104 BQ27XXX_REG_NAC, /* Nominal Available Capacity */
105 BQ27XXX_REG_FCC, /* Full Charge Capacity */
106 BQ27XXX_REG_CYCT, /* Cycle Count */
107 BQ27XXX_REG_AE, /* Available Energy */
108 BQ27XXX_REG_SOC, /* State-of-Charge */
109 BQ27XXX_REG_DCAP, /* Design Capacity */
110 BQ27XXX_REG_AP, /* Average Power */
111};
112
Andrew F. Davis081bab22015-09-22 14:35:06 -0500113struct bq27xxx_reg_cache {
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100114 int temperature;
115 int time_to_empty;
116 int time_to_empty_avg;
117 int time_to_full;
Pali Rohár631c17e2011-02-01 00:08:02 +0100118 int charge_full;
Pali Rohár73c244a2011-04-30 23:59:35 +0200119 int cycle_count;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100120 int capacity;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100121 int energy;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100122 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530123 int power_avg;
124 int health;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100125};
126
Andrew F. Davis081bab22015-09-22 14:35:06 -0500127struct bq27xxx_device_info {
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500128 struct device *dev;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700129 int id;
Andrew F. Davis081bab22015-09-22 14:35:06 -0500130 enum bq27xxx_chip chip;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700131
Andrew F. Davis081bab22015-09-22 14:35:06 -0500132 struct bq27xxx_reg_cache cache;
Pali Rohár631c17e2011-02-01 00:08:02 +0100133 int charge_design_full;
134
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100135 unsigned long last_update;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100136 struct delayed_work work;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100137
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100138 struct power_supply *bat;
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200139
Andrew F. Davis081bab22015-09-22 14:35:06 -0500140 struct bq27xxx_access_methods bus;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100141
142 struct mutex lock;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500143
144 u8 *regs;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700145};
146
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500147/* Register mappings */
148static u8 bq27000_regs[] = {
149 0x00, /* CONTROL */
150 0x06, /* TEMP */
151 INVALID_REG_ADDR, /* INT TEMP - NA*/
152 0x08, /* VOLT */
153 0x14, /* AVG CURR */
154 0x0a, /* FLAGS */
155 0x16, /* TTE */
156 0x18, /* TTF */
157 0x1c, /* TTES */
158 0x26, /* TTECP */
159 0x0c, /* NAC */
160 0x12, /* LMD(FCC) */
161 0x2a, /* CYCT */
162 0x22, /* AE */
163 0x0b, /* SOC(RSOC) */
164 0x76, /* DCAP(ILMD) */
165 0x24, /* AP */
166};
167
168static u8 bq27010_regs[] = {
169 0x00, /* CONTROL */
170 0x06, /* TEMP */
171 INVALID_REG_ADDR, /* INT TEMP - NA*/
172 0x08, /* VOLT */
173 0x14, /* AVG CURR */
174 0x0a, /* FLAGS */
175 0x16, /* TTE */
176 0x18, /* TTF */
177 0x1c, /* TTES */
178 0x26, /* TTECP */
179 0x0c, /* NAC */
180 0x12, /* LMD(FCC) */
181 0x2a, /* CYCT */
182 INVALID_REG_ADDR, /* AE - NA */
183 0x0b, /* SOC(RSOC) */
184 0x76, /* DCAP(ILMD) */
185 INVALID_REG_ADDR, /* AP - NA */
186};
187
188static u8 bq27500_regs[] = {
189 0x00, /* CONTROL */
190 0x06, /* TEMP */
191 0x28, /* INT TEMP */
192 0x08, /* VOLT */
193 0x14, /* AVG CURR */
194 0x0a, /* FLAGS */
195 0x16, /* TTE */
196 INVALID_REG_ADDR, /* TTF - NA */
197 0x1a, /* TTES */
198 INVALID_REG_ADDR, /* TTECP - NA */
199 0x0c, /* NAC */
200 0x12, /* LMD(FCC) */
201 0x1e, /* CYCT */
202 INVALID_REG_ADDR, /* AE - NA */
203 0x20, /* SOC(RSOC) */
204 0x2e, /* DCAP(ILMD) */
205 INVALID_REG_ADDR, /* AP - NA */
206};
207
208static u8 bq27530_regs[] = {
209 0x00, /* CONTROL */
210 0x06, /* TEMP */
211 0x32, /* INT TEMP */
212 0x08, /* VOLT */
213 0x14, /* AVG CURR */
214 0x0a, /* FLAGS */
215 0x16, /* TTE */
216 INVALID_REG_ADDR, /* TTF - NA */
217 INVALID_REG_ADDR, /* TTES - NA */
218 INVALID_REG_ADDR, /* TTECP - NA */
219 0x0c, /* NAC */
220 0x12, /* LMD(FCC) */
221 0x2a, /* CYCT */
222 INVALID_REG_ADDR, /* AE - NA */
223 0x2c, /* SOC(RSOC) */
224 INVALID_REG_ADDR, /* DCAP - NA */
225 0x24, /* AP */
226};
227
228static u8 bq27541_regs[] = {
229 0x00, /* CONTROL */
230 0x06, /* TEMP */
231 0x28, /* INT TEMP */
232 0x08, /* VOLT */
233 0x14, /* AVG CURR */
234 0x0a, /* FLAGS */
235 0x16, /* TTE */
236 INVALID_REG_ADDR, /* TTF - NA */
237 INVALID_REG_ADDR, /* TTES - NA */
238 INVALID_REG_ADDR, /* TTECP - NA */
239 0x0c, /* NAC */
240 0x12, /* LMD(FCC) */
241 0x2a, /* CYCT */
242 INVALID_REG_ADDR, /* AE - NA */
243 0x2c, /* SOC(RSOC) */
244 0x3c, /* DCAP */
245 0x76, /* AP */
246};
247
248static u8 bq27545_regs[] = {
249 0x00, /* CONTROL */
250 0x06, /* TEMP */
251 0x28, /* INT TEMP */
252 0x08, /* VOLT */
253 0x14, /* AVG CURR */
254 0x0a, /* FLAGS */
255 0x16, /* TTE */
256 INVALID_REG_ADDR, /* TTF - NA */
257 INVALID_REG_ADDR, /* TTES - NA */
258 INVALID_REG_ADDR, /* TTECP - NA */
259 0x0c, /* NAC */
260 0x12, /* LMD(FCC) */
261 0x2a, /* CYCT */
262 INVALID_REG_ADDR, /* AE - NA */
263 0x2c, /* SOC(RSOC) */
264 INVALID_REG_ADDR, /* DCAP - NA */
265 0x24, /* AP */
266};
267
268static u8 bq27421_regs[] = {
269 0x00, /* CONTROL */
270 0x02, /* TEMP */
271 0x1e, /* INT TEMP */
272 0x04, /* VOLT */
273 0x10, /* AVG CURR */
274 0x06, /* FLAGS */
275 INVALID_REG_ADDR, /* TTE - NA */
276 INVALID_REG_ADDR, /* TTF - NA */
277 INVALID_REG_ADDR, /* TTES - NA */
278 INVALID_REG_ADDR, /* TTECP - NA */
279 0x08, /* NAC */
280 0x0e, /* FCC */
281 INVALID_REG_ADDR, /* CYCT - NA */
282 INVALID_REG_ADDR, /* AE - NA */
283 0x1c, /* SOC */
284 0x3c, /* DCAP */
285 0x18, /* AP */
286};
287
288static u8 *bq27xxx_regs[] = {
289 [BQ27000] = bq27000_regs,
290 [BQ27010] = bq27010_regs,
291 [BQ27500] = bq27500_regs,
292 [BQ27530] = bq27530_regs,
293 [BQ27541] = bq27541_regs,
294 [BQ27545] = bq27545_regs,
295 [BQ27421] = bq27421_regs,
296};
297
298static enum power_supply_property bq27000_battery_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200299 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700300 POWER_SUPPLY_PROP_PRESENT,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 POWER_SUPPLY_PROP_CURRENT_NOW,
303 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100304 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700305 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200306 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
307 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
308 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200309 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100310 POWER_SUPPLY_PROP_CHARGE_FULL,
311 POWER_SUPPLY_PROP_CHARGE_NOW,
312 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200313 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100314 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530315 POWER_SUPPLY_PROP_POWER_AVG,
316 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500317 POWER_SUPPLY_PROP_MANUFACTURER,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700318};
319
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500320static enum power_supply_property bq27010_battery_props[] = {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700321 POWER_SUPPLY_PROP_STATUS,
322 POWER_SUPPLY_PROP_PRESENT,
323 POWER_SUPPLY_PROP_VOLTAGE_NOW,
324 POWER_SUPPLY_PROP_CURRENT_NOW,
325 POWER_SUPPLY_PROP_CAPACITY,
326 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
327 POWER_SUPPLY_PROP_TEMP,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500328 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
329 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
330 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700331 POWER_SUPPLY_PROP_TECHNOLOGY,
332 POWER_SUPPLY_PROP_CHARGE_FULL,
333 POWER_SUPPLY_PROP_CHARGE_NOW,
334 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500335 POWER_SUPPLY_PROP_CYCLE_COUNT,
336 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500337 POWER_SUPPLY_PROP_MANUFACTURER,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700338};
339
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500340static enum power_supply_property bq27500_battery_props[] = {
341 POWER_SUPPLY_PROP_STATUS,
342 POWER_SUPPLY_PROP_PRESENT,
343 POWER_SUPPLY_PROP_VOLTAGE_NOW,
344 POWER_SUPPLY_PROP_CURRENT_NOW,
345 POWER_SUPPLY_PROP_CAPACITY,
346 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
347 POWER_SUPPLY_PROP_TEMP,
348 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
349 POWER_SUPPLY_PROP_TECHNOLOGY,
350 POWER_SUPPLY_PROP_CHARGE_FULL,
351 POWER_SUPPLY_PROP_CHARGE_NOW,
352 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
353 POWER_SUPPLY_PROP_CYCLE_COUNT,
354 POWER_SUPPLY_PROP_HEALTH,
355 POWER_SUPPLY_PROP_MANUFACTURER,
356};
357
358static enum power_supply_property bq27530_battery_props[] = {
359 POWER_SUPPLY_PROP_STATUS,
360 POWER_SUPPLY_PROP_PRESENT,
361 POWER_SUPPLY_PROP_VOLTAGE_NOW,
362 POWER_SUPPLY_PROP_CURRENT_NOW,
363 POWER_SUPPLY_PROP_CAPACITY,
364 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
365 POWER_SUPPLY_PROP_TEMP,
366 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
367 POWER_SUPPLY_PROP_TECHNOLOGY,
368 POWER_SUPPLY_PROP_CHARGE_FULL,
369 POWER_SUPPLY_PROP_CHARGE_NOW,
370 POWER_SUPPLY_PROP_POWER_AVG,
371 POWER_SUPPLY_PROP_HEALTH,
372 POWER_SUPPLY_PROP_CYCLE_COUNT,
373 POWER_SUPPLY_PROP_MANUFACTURER,
374};
375
376static enum power_supply_property bq27541_battery_props[] = {
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700377 POWER_SUPPLY_PROP_STATUS,
378 POWER_SUPPLY_PROP_PRESENT,
379 POWER_SUPPLY_PROP_VOLTAGE_NOW,
380 POWER_SUPPLY_PROP_CURRENT_NOW,
381 POWER_SUPPLY_PROP_CAPACITY,
382 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
383 POWER_SUPPLY_PROP_TEMP,
384 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
385 POWER_SUPPLY_PROP_TECHNOLOGY,
386 POWER_SUPPLY_PROP_CHARGE_FULL,
387 POWER_SUPPLY_PROP_CHARGE_NOW,
388 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
389 POWER_SUPPLY_PROP_CYCLE_COUNT,
390 POWER_SUPPLY_PROP_POWER_AVG,
391 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500392 POWER_SUPPLY_PROP_MANUFACTURER,
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700393};
394
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500395static enum power_supply_property bq27545_battery_props[] = {
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100396 POWER_SUPPLY_PROP_STATUS,
397 POWER_SUPPLY_PROP_PRESENT,
398 POWER_SUPPLY_PROP_VOLTAGE_NOW,
399 POWER_SUPPLY_PROP_CURRENT_NOW,
400 POWER_SUPPLY_PROP_CAPACITY,
401 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
402 POWER_SUPPLY_PROP_TEMP,
403 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
404 POWER_SUPPLY_PROP_TECHNOLOGY,
405 POWER_SUPPLY_PROP_CHARGE_FULL,
406 POWER_SUPPLY_PROP_CHARGE_NOW,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500407 POWER_SUPPLY_PROP_HEALTH,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100408 POWER_SUPPLY_PROP_CYCLE_COUNT,
409 POWER_SUPPLY_PROP_POWER_AVG,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500410 POWER_SUPPLY_PROP_MANUFACTURER,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100411};
412
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500413static enum power_supply_property bq27421_battery_props[] = {
414 POWER_SUPPLY_PROP_STATUS,
415 POWER_SUPPLY_PROP_PRESENT,
416 POWER_SUPPLY_PROP_VOLTAGE_NOW,
417 POWER_SUPPLY_PROP_CURRENT_NOW,
418 POWER_SUPPLY_PROP_CAPACITY,
419 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
420 POWER_SUPPLY_PROP_TEMP,
421 POWER_SUPPLY_PROP_TECHNOLOGY,
422 POWER_SUPPLY_PROP_CHARGE_FULL,
423 POWER_SUPPLY_PROP_CHARGE_NOW,
424 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
425 POWER_SUPPLY_PROP_MANUFACTURER,
426};
427
428#define BQ27XXX_PROP(_id, _prop) \
429 [_id] = { \
430 .props = _prop, \
431 .size = ARRAY_SIZE(_prop), \
432 }
433
434static struct {
435 enum power_supply_property *props;
436 size_t size;
437} bq27xxx_battery_props[] = {
438 BQ27XXX_PROP(BQ27000, bq27000_battery_props),
439 BQ27XXX_PROP(BQ27010, bq27010_battery_props),
440 BQ27XXX_PROP(BQ27500, bq27500_battery_props),
441 BQ27XXX_PROP(BQ27530, bq27530_battery_props),
442 BQ27XXX_PROP(BQ27541, bq27541_battery_props),
443 BQ27XXX_PROP(BQ27545, bq27545_battery_props),
444 BQ27XXX_PROP(BQ27421, bq27421_battery_props),
445};
446
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100447static unsigned int poll_interval = 360;
448module_param(poll_interval, uint, 0644);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500449MODULE_PARM_DESC(poll_interval,
450 "battery poll interval in seconds - 0 disables polling");
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100451
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700452/*
Andrew F. Davis081bab22015-09-22 14:35:06 -0500453 * Common code for BQ27xxx devices
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700454 */
455
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500456static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500457 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700458{
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500459 /* Reports EINVAL for invalid/missing registers */
460 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
461 return -EINVAL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100462
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500463 return di->bus.read(di, di->regs[reg_index], single);
Saranya Gopala66f59b2012-07-13 20:20:07 -0700464}
465
466/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500467 * Return the battery State-of-Charge
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100468 * Or < 0 if something fails.
469 */
Andrew F. Davisc5709032015-09-22 14:35:08 -0500470static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100471{
Andrew F. Davisc5709032015-09-22 14:35:08 -0500472 int soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100473
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500474 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100475
Andrew F. Davisc5709032015-09-22 14:35:08 -0500476 if (soc < 0)
477 dev_dbg(di->dev, "error reading State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100478
Andrew F. Davisc5709032015-09-22 14:35:08 -0500479 return soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100480}
481
482/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100483 * Return a battery charge value in µAh
484 * Or < 0 if something fails.
485 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500486static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
Pali Rohár631c17e2011-02-01 00:08:02 +0100487{
488 int charge;
489
Andrew F. Davis081bab22015-09-22 14:35:06 -0500490 charge = bq27xxx_read(di, reg, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100491 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100492 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
493 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +0100494 return charge;
495 }
496
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500497 if (di->chip == BQ27000 || di->chip == BQ27010)
498 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100499 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500500 charge *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100501
502 return charge;
503}
504
505/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500506 * Return the battery Nominal available capacity in µAh
Pali Rohár631c17e2011-02-01 00:08:02 +0100507 * Or < 0 if something fails.
508 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500509static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100510{
Pali Roháre59ec4a2012-11-10 15:42:48 +0100511 int flags;
Pali Roháre59ec4a2012-11-10 15:42:48 +0100512
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500513 if (di->chip == BQ27000 || di->chip == BQ27010) {
514 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
515 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
516 return -ENODATA;
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500517 }
Pali Rohár631c17e2011-02-01 00:08:02 +0100518
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500519 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
520}
521
522/*
523 * Return the battery Full Charge Capacity in µAh
524 * Or < 0 if something fails.
525 */
526static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
527{
528 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
529}
530
531/*
532 * Return the Design Capacity in µAh
533 * Or < 0 if something fails.
534 */
535static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
536{
537 int dcap;
538
539 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
540
541 if (dcap < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100542 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500543 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100544 }
545
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500546 if (di->chip == BQ27000 || di->chip == BQ27010)
547 dcap *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +0100548 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500549 dcap *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +0100550
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500551 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +0100552}
553
554/*
Pali Rohára8f6bd22011-11-01 01:43:06 +0100555 * Return the battery Available energy in µWh
556 * Or < 0 if something fails.
557 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500558static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
Pali Rohára8f6bd22011-11-01 01:43:06 +0100559{
560 int ae;
561
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500562 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
Pali Rohára8f6bd22011-11-01 01:43:06 +0100563 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100564 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +0100565 return ae;
566 }
567
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500568 if (di->chip == BQ27000 || di->chip == BQ27010)
569 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100570 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500571 ae *= 1000;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100572
573 return ae;
574}
575
576/*
Pali Rohár5dc34432013-02-02 11:06:09 +0100577 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +0100578 * Or < 0 if something fails.
579 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500580static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
Pali Rohárd149e982011-11-01 01:43:07 +0100581{
582 int temp;
583
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500584 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
Pali Rohárd149e982011-11-01 01:43:07 +0100585 if (temp < 0) {
586 dev_err(di->dev, "error reading temperature\n");
587 return temp;
588 }
589
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500590 if (di->chip == BQ27000 || di->chip == BQ27010)
Pali Rohár5dc34432013-02-02 11:06:09 +0100591 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +0100592
593 return temp;
594}
595
596/*
Pali Rohár631c17e2011-02-01 00:08:02 +0100597 * Return the battery Cycle count total
598 * Or < 0 if something fails.
599 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500600static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +0100601{
602 int cyct;
603
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500604 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
Pali Rohár631c17e2011-02-01 00:08:02 +0100605 if (cyct < 0)
606 dev_err(di->dev, "error reading cycle count total\n");
607
608 return cyct;
609}
610
611/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100612 * Read a time register.
613 * Return < 0 if something fails.
614 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500615static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100616{
617 int tval;
618
Andrew F. Davis081bab22015-09-22 14:35:06 -0500619 tval = bq27xxx_read(di, reg, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100620 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100621 dev_dbg(di->dev, "error reading time register %02x: %d\n",
622 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100623 return tval;
624 }
625
626 if (tval == 65535)
627 return -ENODATA;
628
629 return tval * 60;
630}
631
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530632/*
Andrew F. Davisc5709032015-09-22 14:35:08 -0500633 * Read an average power register.
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530634 * Return < 0 if something fails.
635 */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500636static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530637{
638 int tval;
639
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500640 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530641 if (tval < 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500642 dev_err(di->dev, "error reading average power register %02x: %d\n",
643 BQ27XXX_REG_AP, tval);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530644 return tval;
645 }
646
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500647 if (di->chip == BQ27000 || di->chip == BQ27010)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500648 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500649 else
650 return tval;
651}
652
653/*
654 * Returns true if a battery over temperature condition is detected
655 */
Andrew F. Davis74aab842015-09-22 14:35:10 -0500656static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500657{
Andrew F. Davis74aab842015-09-22 14:35:10 -0500658 if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500659 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
Andrew F. Davis74aab842015-09-22 14:35:10 -0500660 if (di->chip == BQ27530 || di->chip == BQ27421)
661 return flags & BQ27XXX_FLAG_OT;
662
663 return false;
664}
665
666/*
667 * Returns true if a battery under temperature condition is detected
668 */
669static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
670{
671 if (di->chip == BQ27530 || di->chip == BQ27421)
672 return flags & BQ27XXX_FLAG_UT;
673
674 return false;
675}
676
677/*
678 * Returns true if a low state of charge condition is detected
679 */
680static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
681{
682 if (di->chip == BQ27000 || di->chip == BQ27010)
683 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500684 else
Andrew F. Davis74aab842015-09-22 14:35:10 -0500685 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530686}
687
688/*
689 * Read flag register.
690 * Return < 0 if something fails.
691 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500692static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530693{
Andrzej Hajda2edd69a2015-09-28 10:51:27 +0200694 int flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530695
Andrew F. Davis74aab842015-09-22 14:35:10 -0500696 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
697 if (flags < 0) {
698 dev_err(di->dev, "error reading flag register:%d\n", flags);
699 return flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530700 }
701
Andrew F. Davis74aab842015-09-22 14:35:10 -0500702 /* Unlikely but important to return first */
703 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
704 return POWER_SUPPLY_HEALTH_OVERHEAT;
705 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
706 return POWER_SUPPLY_HEALTH_COLD;
707 if (unlikely(bq27xxx_battery_dead(di, flags)))
708 return POWER_SUPPLY_HEALTH_DEAD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530709
Andrew F. Davis74aab842015-09-22 14:35:10 -0500710 return POWER_SUPPLY_HEALTH_GOOD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530711}
712
Andrew F. Davis081bab22015-09-22 14:35:06 -0500713static void bq27xxx_battery_update(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100714{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500715 struct bq27xxx_reg_cache cache = {0, };
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500716 bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
717 bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100718
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500719 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
Marek Belisko3dd843e2014-06-30 21:49:56 +0200720 if ((cache.flags & 0xff) == 0xff)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500721 cache.flags = -1; /* read error */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100722 if (cache.flags >= 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500723 cache.temperature = bq27xxx_battery_read_temperature(di);
724 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100725 dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +0100726 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +0100727 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100728 cache.time_to_empty = -ENODATA;
729 cache.time_to_empty_avg = -ENODATA;
730 cache.time_to_full = -ENODATA;
731 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530732 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +0100733 } else {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500734 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
735 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
736 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
737 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
738 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
739 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
740 cache.charge_full = bq27xxx_battery_read_fcc(di);
Andrew F. Davisc5709032015-09-22 14:35:08 -0500741 cache.capacity = bq27xxx_battery_read_soc(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500742 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500743 cache.energy = bq27xxx_battery_read_energy(di);
Andrew F. Davis081bab22015-09-22 14:35:06 -0500744 cache.health = bq27xxx_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +0100745 }
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500746 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -0500747 cache.cycle_count = bq27xxx_battery_read_cyct(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500748 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
749 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100750
Pali Rohár631c17e2011-02-01 00:08:02 +0100751 /* We only have to read charge design full once */
752 if (di->charge_design_full <= 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500753 di->charge_design_full = bq27xxx_battery_read_dcap(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100754 }
755
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700756 if (di->cache.capacity != cache.capacity)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100757 power_supply_changed(di->bat);
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -0700758
759 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
760 di->cache = cache;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100761
762 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700763}
764
Andrew F. Davis081bab22015-09-22 14:35:06 -0500765static void bq27xxx_battery_poll(struct work_struct *work)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100766{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500767 struct bq27xxx_device_info *di =
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500768 container_of(work, struct bq27xxx_device_info,
769 work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100770
Andrew F. Davis081bab22015-09-22 14:35:06 -0500771 bq27xxx_battery_update(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100772
773 if (poll_interval > 0) {
774 /* The timer does not have to be accurate. */
775 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
776 schedule_delayed_work(&di->work, poll_interval * HZ);
777 }
778}
779
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700780/*
Pali Rohárbf7d4142011-02-01 00:23:13 +0100781 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700782 * Note that current can be negative signed as well
783 * Or 0 if something fails.
784 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500785static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500786 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700787{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100788 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +0100789 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700790
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500791 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100792 if (curr < 0) {
793 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100794 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100795 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200796
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500797 if (di->chip == BQ27000 || di->chip == BQ27010) {
798 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
Pali Rohárb68f6212011-11-01 01:43:03 +0100799 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200800 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +0200801 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200802 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100803
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500804 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
805 } else {
806 /* Other gauges return signed value */
807 val->intval = (int)((s16)curr) * 1000;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700808 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +0200809
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100810 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700811}
812
Andrew F. Davis081bab22015-09-22 14:35:06 -0500813static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500814 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200815{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200816 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200817
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500818 if (di->chip == BQ27000 || di->chip == BQ27010) {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200819 if (di->cache.flags & BQ27000_FLAG_FC)
820 status = POWER_SUPPLY_STATUS_FULL;
821 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200822 status = POWER_SUPPLY_STATUS_CHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100823 else if (power_supply_am_i_supplied(di->bat))
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +0200824 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200825 else
826 status = POWER_SUPPLY_STATUS_DISCHARGING;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500827 } else {
828 if (di->cache.flags & BQ27XXX_FLAG_FC)
829 status = POWER_SUPPLY_STATUS_FULL;
830 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
831 status = POWER_SUPPLY_STATUS_DISCHARGING;
832 else
833 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200834 }
835
836 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100837
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200838 return 0;
839}
840
Andrew F. Davis081bab22015-09-22 14:35:06 -0500841static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500842 union power_supply_propval *val)
Pali Rohárd66bab32011-11-01 01:43:04 +0100843{
844 int level;
845
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500846 if (di->chip == BQ27000 || di->chip == BQ27010) {
Pali Rohárd66bab32011-11-01 01:43:04 +0100847 if (di->cache.flags & BQ27000_FLAG_FC)
848 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
849 else if (di->cache.flags & BQ27000_FLAG_EDV1)
850 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
851 else if (di->cache.flags & BQ27000_FLAG_EDVF)
852 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
853 else
854 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500855 } else {
856 if (di->cache.flags & BQ27XXX_FLAG_FC)
857 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
858 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
859 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
860 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
861 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
862 else
863 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Pali Rohárd66bab32011-11-01 01:43:04 +0100864 }
865
866 val->intval = level;
867
868 return 0;
869}
870
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200871/*
Thomas Weber91fe4d52012-02-17 17:46:21 +0100872 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100873 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200874 */
Andrew F. Davis081bab22015-09-22 14:35:06 -0500875static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500876 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200877{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100878 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200879
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500880 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100881 if (volt < 0) {
882 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100883 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +0100884 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200885
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100886 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200887
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100888 return 0;
889}
890
Andrew F. Davis081bab22015-09-22 14:35:06 -0500891static int bq27xxx_simple_value(int value,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500892 union power_supply_propval *val)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100893{
894 if (value < 0)
895 return value;
896
897 val->intval = value;
898
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200899 return 0;
900}
901
Andrew F. Davis081bab22015-09-22 14:35:06 -0500902static int bq27xxx_battery_get_property(struct power_supply *psy,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700903 enum power_supply_property psp,
904 union power_supply_propval *val)
905{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200906 int ret = 0;
Andrew F. Davis081bab22015-09-22 14:35:06 -0500907 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200908
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100909 mutex_lock(&di->lock);
910 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
911 cancel_delayed_work_sync(&di->work);
Andrew F. Davis081bab22015-09-22 14:35:06 -0500912 bq27xxx_battery_poll(&di->work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100913 }
914 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100915
916 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200917 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700918
919 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200920 case POWER_SUPPLY_PROP_STATUS:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500921 ret = bq27xxx_battery_status(di, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200922 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700923 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500924 ret = bq27xxx_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +0200925 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700926 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100927 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700928 break;
929 case POWER_SUPPLY_PROP_CURRENT_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500930 ret = bq27xxx_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700931 break;
932 case POWER_SUPPLY_PROP_CAPACITY:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500933 ret = bq27xxx_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700934 break;
Pali Rohárd66bab32011-11-01 01:43:04 +0100935 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500936 ret = bq27xxx_battery_capacity_level(di, val);
Pali Rohárd66bab32011-11-01 01:43:04 +0100937 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700938 case POWER_SUPPLY_PROP_TEMP:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500939 ret = bq27xxx_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +0100940 if (ret == 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500941 val->intval -= 2731; /* convert decidegree k to c */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700942 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200943 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500944 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200945 break;
946 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500947 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200948 break;
949 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500950 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200951 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200952 case POWER_SUPPLY_PROP_TECHNOLOGY:
953 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
954 break;
Pali Rohár631c17e2011-02-01 00:08:02 +0100955 case POWER_SUPPLY_PROP_CHARGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500956 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100957 break;
958 case POWER_SUPPLY_PROP_CHARGE_FULL:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500959 ret = bq27xxx_simple_value(di->cache.charge_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100960 break;
961 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500962 ret = bq27xxx_simple_value(di->charge_design_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100963 break;
Pali Rohár73c244a2011-04-30 23:59:35 +0200964 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500965 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100966 break;
967 case POWER_SUPPLY_PROP_ENERGY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500968 ret = bq27xxx_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +0100969 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530970 case POWER_SUPPLY_PROP_POWER_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500971 ret = bq27xxx_simple_value(di->cache.power_avg, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530972 break;
973 case POWER_SUPPLY_PROP_HEALTH:
Andrew F. Davis081bab22015-09-22 14:35:06 -0500974 ret = bq27xxx_simple_value(di->cache.health, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530975 break;
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500976 case POWER_SUPPLY_PROP_MANUFACTURER:
977 val->strval = BQ27XXX_MANUFACTURER;
978 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700979 default:
980 return -EINVAL;
981 }
982
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200983 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700984}
985
Andrew F. Davis081bab22015-09-22 14:35:06 -0500986static void bq27xxx_external_power_changed(struct power_supply *psy)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100987{
Andrew F. Davis081bab22015-09-22 14:35:06 -0500988 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100989
990 cancel_delayed_work_sync(&di->work);
991 schedule_delayed_work(&di->work, 0);
992}
993
Andrew F. Davis081bab22015-09-22 14:35:06 -0500994static int bq27xxx_powersupply_init(struct bq27xxx_device_info *di,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100995 const char *name)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700996{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +0200997 int ret;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100998 struct power_supply_desc *psy_desc;
999 struct power_supply_config psy_cfg = { .drv_data = di, };
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001000
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001001 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
1002 if (!psy_desc)
1003 return -ENOMEM;
1004
1005 psy_desc->name = name;
1006 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001007 psy_desc->properties = bq27xxx_battery_props[di->chip].props;
1008 psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001009 psy_desc->get_property = bq27xxx_battery_get_property;
1010 psy_desc->external_power_changed = bq27xxx_external_power_changed;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001011
Andrew F. Davis081bab22015-09-22 14:35:06 -05001012 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001013 mutex_init(&di->lock);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001014
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001015 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
1016 if (IS_ERR(di->bat)) {
1017 ret = PTR_ERR(di->bat);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001018 dev_err(di->dev, "failed to register battery: %d\n", ret);
1019 return ret;
1020 }
1021
1022 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
1023
Andrew F. Davis081bab22015-09-22 14:35:06 -05001024 bq27xxx_battery_update(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001025
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001026 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001027}
1028
Andrew F. Davis081bab22015-09-22 14:35:06 -05001029static void bq27xxx_powersupply_unregister(struct bq27xxx_device_info *di)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001030{
Pali Rohár8cfaaa82011-11-01 01:43:11 +01001031 /*
Andrew F. Davis081bab22015-09-22 14:35:06 -05001032 * power_supply_unregister call bq27xxx_battery_get_property which
1033 * call bq27xxx_battery_poll.
1034 * Make sure that bq27xxx_battery_poll will not call
Pali Rohár8cfaaa82011-11-01 01:43:11 +01001035 * schedule_delayed_work again after unregister (which cause OOPS).
1036 */
1037 poll_interval = 0;
1038
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001039 cancel_delayed_work_sync(&di->work);
1040
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001041 power_supply_unregister(di->bat);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001042
1043 mutex_destroy(&di->lock);
1044}
1045
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001046/* i2c specific code */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001047#ifdef CONFIG_BATTERY_BQ27XXX_I2C
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001048
1049/* If the system has several batteries we need a different name for each
1050 * of them...
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001051 */
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001052static DEFINE_IDR(battery_id);
1053static DEFINE_MUTEX(battery_mutex);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001054
Sebastian Reichel41a90db2015-10-16 15:44:11 +02001055static irqreturn_t bq27xxx_battery_irq_handler_thread(int irq, void *data)
1056{
1057 struct bq27xxx_device_info *di = data;
1058
1059 bq27xxx_battery_update(di);
1060
1061 return IRQ_HANDLED;
1062}
1063
Andrew F. Davis081bab22015-09-22 14:35:06 -05001064static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg,
1065 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001066{
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001067 struct i2c_client *client = to_i2c_client(di->dev);
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +02001068 struct i2c_msg msg[2];
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001069 unsigned char data[2];
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001070 int ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001071
1072 if (!client->adapter)
1073 return -ENODEV;
1074
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +02001075 msg[0].addr = client->addr;
1076 msg[0].flags = 0;
1077 msg[0].buf = &reg;
1078 msg[0].len = sizeof(reg);
1079 msg[1].addr = client->addr;
1080 msg[1].flags = I2C_M_RD;
1081 msg[1].buf = data;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +01001082 if (single)
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +02001083 msg[1].len = 1;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +01001084 else
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +02001085 msg[1].len = 2;
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +01001086
Grazvydas Ignotas9e912f42011-02-15 23:27:35 +02001087 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
Lars-Peter Clausen2ec523a2011-02-02 19:35:07 +01001088 if (ret < 0)
1089 return ret;
1090
1091 if (!single)
1092 ret = get_unaligned_le16(data);
1093 else
1094 ret = data[0];
1095
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001096 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001097}
1098
Andrew F. Davis081bab22015-09-22 14:35:06 -05001099static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
1100 const struct i2c_device_id *id)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001101{
1102 char *name;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001103 struct bq27xxx_device_info *di;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001104 int num;
1105 int retval = 0;
1106
1107 /* Get new ID for the new battery device */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001108 mutex_lock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -08001109 num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001110 mutex_unlock(&battery_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -08001111 if (num < 0)
1112 return num;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001113
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001114 name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001115 if (!name) {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001116 retval = -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001117 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001118 }
1119
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301120 di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001121 if (!di) {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001122 retval = -ENOMEM;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001123 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001124 }
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001125
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001126 di->id = num;
1127 di->dev = &client->dev;
1128 di->chip = id->driver_data;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001129 di->bus.read = &bq27xxx_battery_i2c_read;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001130 di->regs = bq27xxx_regs[di->chip];
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001131
Andrew F. Davis081bab22015-09-22 14:35:06 -05001132 retval = bq27xxx_powersupply_init(di, name);
Julia Lawallf7760992012-08-19 10:44:20 +02001133 if (retval)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001134 goto batt_failed;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001135
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001136 /* Schedule a polling after about 1 min */
1137 schedule_delayed_work(&di->work, 60 * HZ);
1138
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001139 i2c_set_clientdata(client, di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001140
Andrew F. Davis8807feb2015-09-22 14:35:11 -05001141 if (client->irq) {
1142 retval = devm_request_threaded_irq(&client->dev, client->irq,
1143 NULL, bq27xxx_battery_irq_handler_thread,
1144 IRQF_ONESHOT,
1145 name, di);
1146 if (retval) {
1147 dev_err(&client->dev,
1148 "Unable to register IRQ %d error %d\n",
1149 client->irq, retval);
1150 return retval;
1151 }
1152 }
1153
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001154 return 0;
1155
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001156batt_failed:
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001157 mutex_lock(&battery_mutex);
1158 idr_remove(&battery_id, num);
1159 mutex_unlock(&battery_mutex);
1160
1161 return retval;
1162}
1163
Andrew F. Davis081bab22015-09-22 14:35:06 -05001164static int bq27xxx_battery_i2c_remove(struct i2c_client *client)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001165{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001166 struct bq27xxx_device_info *di = i2c_get_clientdata(client);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001167
Andrew F. Davis081bab22015-09-22 14:35:06 -05001168 bq27xxx_powersupply_unregister(di);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001169
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001170 mutex_lock(&battery_mutex);
1171 idr_remove(&battery_id, di->id);
1172 mutex_unlock(&battery_mutex);
1173
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001174 return 0;
1175}
1176
Andrew F. Davis081bab22015-09-22 14:35:06 -05001177static const struct i2c_device_id bq27xxx_id[] = {
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001178 { "bq27200", BQ27000 },
1179 { "bq27210", BQ27010 },
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001180 { "bq27500", BQ27500 },
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001181 { "bq27510", BQ27500 },
1182 { "bq27520", BQ27500 },
1183 { "bq27530", BQ27530 },
1184 { "bq27531", BQ27530 },
1185 { "bq27541", BQ27541 },
1186 { "bq27542", BQ27541 },
1187 { "bq27546", BQ27541 },
1188 { "bq27742", BQ27541 },
1189 { "bq27545", BQ27545 },
1190 { "bq27421", BQ27421 },
1191 { "bq27425", BQ27421 },
1192 { "bq27441", BQ27421 },
1193 { "bq27621", BQ27421 },
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001194 {},
1195};
Andrew F. Davis081bab22015-09-22 14:35:06 -05001196MODULE_DEVICE_TABLE(i2c, bq27xxx_id);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001197
Andrew F. Davis081bab22015-09-22 14:35:06 -05001198static struct i2c_driver bq27xxx_battery_i2c_driver = {
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001199 .driver = {
Andrew F. Davis081bab22015-09-22 14:35:06 -05001200 .name = "bq27xxx-battery",
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001201 },
Andrew F. Davis081bab22015-09-22 14:35:06 -05001202 .probe = bq27xxx_battery_i2c_probe,
1203 .remove = bq27xxx_battery_i2c_remove,
1204 .id_table = bq27xxx_id,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001205};
1206
Andrew F. Davis081bab22015-09-22 14:35:06 -05001207static inline int bq27xxx_battery_i2c_init(void)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001208{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001209 int ret = i2c_add_driver(&bq27xxx_battery_i2c_driver);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001210
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001211 if (ret)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001212 pr_err("Unable to register BQ27xxx i2c driver\n");
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001213
1214 return ret;
1215}
1216
Andrew F. Davis081bab22015-09-22 14:35:06 -05001217static inline void bq27xxx_battery_i2c_exit(void)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001218{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001219 i2c_del_driver(&bq27xxx_battery_i2c_driver);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001220}
1221
1222#else
1223
Andrew F. Davis081bab22015-09-22 14:35:06 -05001224static inline int bq27xxx_battery_i2c_init(void) { return 0; }
1225static inline void bq27xxx_battery_i2c_exit(void) {};
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001226
1227#endif
1228
1229/* platform specific code */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001230#ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001231
Andrew F. Davis081bab22015-09-22 14:35:06 -05001232static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
1233 bool single)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001234{
1235 struct device *dev = di->dev;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001236 struct bq27xxx_platform_data *pdata = dev->platform_data;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001237 unsigned int timeout = 3;
1238 int upper, lower;
1239 int temp;
1240
1241 if (!single) {
1242 /* Make sure the value has not changed in between reading the
1243 * lower and the upper part */
1244 upper = pdata->read(dev, reg + 1);
1245 do {
1246 temp = upper;
1247 if (upper < 0)
1248 return upper;
1249
1250 lower = pdata->read(dev, reg);
1251 if (lower < 0)
1252 return lower;
1253
1254 upper = pdata->read(dev, reg + 1);
1255 } while (temp != upper && --timeout);
1256
1257 if (timeout == 0)
1258 return -EIO;
1259
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001260 return (upper << 8) | lower;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001261 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001262
1263 return pdata->read(dev, reg);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001264}
1265
Andrew F. Davis081bab22015-09-22 14:35:06 -05001266static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001267{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001268 struct bq27xxx_device_info *di;
1269 struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001270 const char *name;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001271
1272 if (!pdata) {
1273 dev_err(&pdev->dev, "no platform_data supplied\n");
1274 return -EINVAL;
1275 }
1276
1277 if (!pdata->read) {
1278 dev_err(&pdev->dev, "no hdq read callback supplied\n");
1279 return -EINVAL;
1280 }
1281
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001282 if (!pdata->chip) {
1283 dev_err(&pdev->dev, "no device supplied\n");
1284 return -EINVAL;
1285 }
1286
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +05301287 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001288 if (!di)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001289 return -ENOMEM;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001290
1291 platform_set_drvdata(pdev, di);
1292
1293 di->dev = &pdev->dev;
Andrew F. Davis424cfde2015-09-22 14:35:07 -05001294 di->chip = pdata->chip;
Sebastian Reichel0077ae72015-10-16 15:44:10 +02001295 di->regs = bq27xxx_regs[di->chip];
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001296
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001297 name = pdata->name ?: dev_name(&pdev->dev);
Andrew F. Davis081bab22015-09-22 14:35:06 -05001298 di->bus.read = &bq27xxx_battery_platform_read;
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001299
Andrew F. Davis081bab22015-09-22 14:35:06 -05001300 return bq27xxx_powersupply_init(di, name);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001301}
1302
Andrew F. Davis081bab22015-09-22 14:35:06 -05001303static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001304{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001305 struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001306
Andrew F. Davis081bab22015-09-22 14:35:06 -05001307 bq27xxx_powersupply_unregister(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001308
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001309 return 0;
1310}
1311
Andrew F. Davis081bab22015-09-22 14:35:06 -05001312static struct platform_driver bq27xxx_battery_platform_driver = {
1313 .probe = bq27xxx_battery_platform_probe,
1314 .remove = bq27xxx_battery_platform_remove,
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001315 .driver = {
1316 .name = "bq27000-battery",
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001317 },
1318};
1319
Andrew F. Davis081bab22015-09-22 14:35:06 -05001320static inline int bq27xxx_battery_platform_init(void)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001321{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001322 int ret = platform_driver_register(&bq27xxx_battery_platform_driver);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001323
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001324 if (ret)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001325 pr_err("Unable to register BQ27xxx platform driver\n");
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001326
1327 return ret;
1328}
1329
Andrew F. Davis081bab22015-09-22 14:35:06 -05001330static inline void bq27xxx_battery_platform_exit(void)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001331{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001332 platform_driver_unregister(&bq27xxx_battery_platform_driver);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001333}
1334
1335#else
1336
Andrew F. Davis081bab22015-09-22 14:35:06 -05001337static inline int bq27xxx_battery_platform_init(void) { return 0; }
1338static inline void bq27xxx_battery_platform_exit(void) {};
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001339
1340#endif
1341
1342/*
1343 * Module stuff
1344 */
1345
Andrew F. Davis081bab22015-09-22 14:35:06 -05001346static int __init bq27xxx_battery_init(void)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001347{
1348 int ret;
1349
Andrew F. Davis081bab22015-09-22 14:35:06 -05001350 ret = bq27xxx_battery_i2c_init();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001351 if (ret)
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001352 return ret;
1353
Andrew F. Davis081bab22015-09-22 14:35:06 -05001354 ret = bq27xxx_battery_platform_init();
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001355 if (ret)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001356 bq27xxx_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001357
1358 return ret;
1359}
Andrew F. Davis081bab22015-09-22 14:35:06 -05001360module_init(bq27xxx_battery_init);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001361
Andrew F. Davis081bab22015-09-22 14:35:06 -05001362static void __exit bq27xxx_battery_exit(void)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001363{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001364 bq27xxx_battery_platform_exit();
1365 bq27xxx_battery_i2c_exit();
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001366}
Andrew F. Davis081bab22015-09-22 14:35:06 -05001367module_exit(bq27xxx_battery_exit);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001368
Andrew F. Davis081bab22015-09-22 14:35:06 -05001369#ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
Marek Belisko8ebb7e92015-04-30 22:05:58 +02001370MODULE_ALIAS("platform:bq27000-battery");
1371#endif
1372
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001373MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
Andrew F. Davis081bab22015-09-22 14:35:06 -05001374MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001375MODULE_LICENSE("GPL");