blob: 51f0961ecf3e015c5ff9752d8999ac1281c48e34 [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>
Liam Breck0670c9b2017-06-07 11:37:55 -07008 * Copyright (C) 2017 Liam Breck <kernel@networkimprov.net>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07009 *
10 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
11 *
12 * This package is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
Pali Rohár631c17e2011-02-01 00:08:02 +010020 * Datasheets:
Andrew F. Davisd74534c2015-09-22 14:35:09 -050021 * http://www.ti.com/product/bq27000
22 * http://www.ti.com/product/bq27200
23 * http://www.ti.com/product/bq27010
24 * http://www.ti.com/product/bq27210
25 * http://www.ti.com/product/bq27500
Chris Lapabd281772017-01-11 12:44:41 +110026 * http://www.ti.com/product/bq27510-g1
Chris Lapa698a2bf2017-01-11 12:44:42 +110027 * http://www.ti.com/product/bq27510-g2
Andrew F. Davisd74534c2015-09-22 14:35:09 -050028 * http://www.ti.com/product/bq27510-g3
29 * http://www.ti.com/product/bq27520-g4
Chris Lapa68f2a812017-01-11 12:44:44 +110030 * http://www.ti.com/product/bq27520-g1
Chris Lapaa5deb9a2017-01-11 12:44:45 +110031 * http://www.ti.com/product/bq27520-g2
Chris Lapa825e9152017-01-11 12:44:46 +110032 * http://www.ti.com/product/bq27520-g3
Chris Lapa8835cae2017-01-11 12:44:47 +110033 * http://www.ti.com/product/bq27520-g4
Andrew F. Davisd74534c2015-09-22 14:35:09 -050034 * http://www.ti.com/product/bq27530-g1
35 * http://www.ti.com/product/bq27531-g1
36 * http://www.ti.com/product/bq27541-g1
37 * http://www.ti.com/product/bq27542-g1
38 * http://www.ti.com/product/bq27546-g1
39 * http://www.ti.com/product/bq27742-g1
40 * http://www.ti.com/product/bq27545-g1
41 * http://www.ti.com/product/bq27421-g1
Saranya Gopala66f59b2012-07-13 20:20:07 -070042 * http://www.ti.com/product/bq27425-g1
Andrew F. Davisd74534c2015-09-22 14:35:09 -050043 * http://www.ti.com/product/bq27411-g1
44 * http://www.ti.com/product/bq27621-g1
Pali Rohár631c17e2011-02-01 00:08:02 +010045 */
46
Himangi Saraogi1cb82fd2014-05-28 22:58:49 +053047#include <linux/device.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070048#include <linux/module.h>
Matt Ranostay1d727062016-09-19 20:43:02 -070049#include <linux/mutex.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070050#include <linux/param.h>
51#include <linux/jiffies.h>
52#include <linux/workqueue.h>
53#include <linux/delay.h>
54#include <linux/platform_device.h>
55#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Pali Rohár7df3a742016-02-21 12:28:22 +010057#include <linux/of.h>
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070058
Andrew F. Davis081bab22015-09-22 14:35:06 -050059#include <linux/power/bq27xxx_battery.h>
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +020060
Andrew F. Davisdb04fc52015-07-22 16:51:53 -050061#define BQ27XXX_MANUFACTURER "Texas Instruments"
62
Andrew F. Davisd74534c2015-09-22 14:35:09 -050063/* BQ27XXX Flags */
64#define BQ27XXX_FLAG_DSC BIT(0)
65#define BQ27XXX_FLAG_SOCF BIT(1) /* State-of-Charge threshold final */
66#define BQ27XXX_FLAG_SOC1 BIT(2) /* State-of-Charge threshold 1 */
Liam Breck0670c9b2017-06-07 11:37:55 -070067#define BQ27XXX_FLAG_CFGUP BIT(4)
Andrew F. Davisd74534c2015-09-22 14:35:09 -050068#define BQ27XXX_FLAG_FC BIT(9)
69#define BQ27XXX_FLAG_OTD BIT(14)
70#define BQ27XXX_FLAG_OTC BIT(15)
Andrew F. Davis74aab842015-09-22 14:35:10 -050071#define BQ27XXX_FLAG_UT BIT(14)
72#define BQ27XXX_FLAG_OT BIT(15)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -070073
Andrew F. Davisd74534c2015-09-22 14:35:09 -050074/* BQ27000 has different layout for Flags register */
Andrew F. Davis6eb207f2015-07-22 16:51:54 -050075#define BQ27000_FLAG_EDVF BIT(0) /* Final End-of-Discharge-Voltage flag */
76#define BQ27000_FLAG_EDV1 BIT(1) /* First End-of-Discharge-Voltage flag */
77#define BQ27000_FLAG_CI BIT(4) /* Capacity Inaccurate flag */
78#define BQ27000_FLAG_FC BIT(5)
79#define BQ27000_FLAG_CHGS BIT(7) /* Charge state flag */
Grazvydas Ignotase20908d2010-02-12 23:57:23 +020080
Liam Breck0670c9b2017-06-07 11:37:55 -070081/* control register params */
82#define BQ27XXX_SEALED 0x20
83#define BQ27XXX_SET_CFGUPDATE 0x13
84#define BQ27XXX_SOFT_RESET 0x42
85#define BQ27XXX_RESET 0x41
86
Andrew F. Davisd74534c2015-09-22 14:35:09 -050087#define BQ27XXX_RS (20) /* Resistor sense mOhm */
88#define BQ27XXX_POWER_CONSTANT (29200) /* 29.2 µV^2 * 1000 */
89#define BQ27XXX_CURRENT_CONSTANT (3570) /* 3.57 µV * 1000 */
Pali Rohára2e51182010-05-24 20:52:13 +020090
Andrew F. Davisd74534c2015-09-22 14:35:09 -050091#define INVALID_REG_ADDR 0xff
92
93/*
94 * bq27xxx_reg_index - Register names
95 *
96 * These are indexes into a device's register mapping array.
97 */
Andrew F. Davis2e05b512016-06-17 17:02:35 -050098
Andrew F. Davisd74534c2015-09-22 14:35:09 -050099enum bq27xxx_reg_index {
100 BQ27XXX_REG_CTRL = 0, /* Control */
101 BQ27XXX_REG_TEMP, /* Temperature */
102 BQ27XXX_REG_INT_TEMP, /* Internal Temperature */
103 BQ27XXX_REG_VOLT, /* Voltage */
104 BQ27XXX_REG_AI, /* Average Current */
105 BQ27XXX_REG_FLAGS, /* Flags */
106 BQ27XXX_REG_TTE, /* Time-to-Empty */
107 BQ27XXX_REG_TTF, /* Time-to-Full */
108 BQ27XXX_REG_TTES, /* Time-to-Empty Standby */
109 BQ27XXX_REG_TTECP, /* Time-to-Empty at Constant Power */
110 BQ27XXX_REG_NAC, /* Nominal Available Capacity */
111 BQ27XXX_REG_FCC, /* Full Charge Capacity */
112 BQ27XXX_REG_CYCT, /* Cycle Count */
113 BQ27XXX_REG_AE, /* Available Energy */
114 BQ27XXX_REG_SOC, /* State-of-Charge */
115 BQ27XXX_REG_DCAP, /* Design Capacity */
116 BQ27XXX_REG_AP, /* Average Power */
Liam Breck0670c9b2017-06-07 11:37:55 -0700117 BQ27XXX_DM_CTRL, /* Block Data Control */
118 BQ27XXX_DM_CLASS, /* Data Class */
119 BQ27XXX_DM_BLOCK, /* Data Block */
120 BQ27XXX_DM_DATA, /* Block Data */
121 BQ27XXX_DM_CKSUM, /* Block Data Checksum */
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500122 BQ27XXX_REG_MAX, /* sentinel */
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500123};
124
Liam Breck0670c9b2017-06-07 11:37:55 -0700125#define BQ27XXX_DM_REG_ROWS \
126 [BQ27XXX_DM_CTRL] = 0x61, \
127 [BQ27XXX_DM_CLASS] = 0x3e, \
128 [BQ27XXX_DM_BLOCK] = 0x3f, \
129 [BQ27XXX_DM_DATA] = 0x40, \
130 [BQ27XXX_DM_CKSUM] = 0x60
131
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500132/* Register mappings */
Liam Breck9aade6d2017-08-23 20:36:13 -0700133static u8
134 bq27000_regs[BQ27XXX_REG_MAX] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500135 [BQ27XXX_REG_CTRL] = 0x00,
136 [BQ27XXX_REG_TEMP] = 0x06,
137 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
138 [BQ27XXX_REG_VOLT] = 0x08,
139 [BQ27XXX_REG_AI] = 0x14,
140 [BQ27XXX_REG_FLAGS] = 0x0a,
141 [BQ27XXX_REG_TTE] = 0x16,
142 [BQ27XXX_REG_TTF] = 0x18,
143 [BQ27XXX_REG_TTES] = 0x1c,
144 [BQ27XXX_REG_TTECP] = 0x26,
145 [BQ27XXX_REG_NAC] = 0x0c,
146 [BQ27XXX_REG_FCC] = 0x12,
147 [BQ27XXX_REG_CYCT] = 0x2a,
148 [BQ27XXX_REG_AE] = 0x22,
149 [BQ27XXX_REG_SOC] = 0x0b,
150 [BQ27XXX_REG_DCAP] = 0x76,
151 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700152 [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
153 [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
154 [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
155 [BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
156 [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500157 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700158 bq27010_regs[BQ27XXX_REG_MAX] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500159 [BQ27XXX_REG_CTRL] = 0x00,
160 [BQ27XXX_REG_TEMP] = 0x06,
161 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
162 [BQ27XXX_REG_VOLT] = 0x08,
163 [BQ27XXX_REG_AI] = 0x14,
164 [BQ27XXX_REG_FLAGS] = 0x0a,
165 [BQ27XXX_REG_TTE] = 0x16,
166 [BQ27XXX_REG_TTF] = 0x18,
167 [BQ27XXX_REG_TTES] = 0x1c,
168 [BQ27XXX_REG_TTECP] = 0x26,
169 [BQ27XXX_REG_NAC] = 0x0c,
170 [BQ27XXX_REG_FCC] = 0x12,
171 [BQ27XXX_REG_CYCT] = 0x2a,
172 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
173 [BQ27XXX_REG_SOC] = 0x0b,
174 [BQ27XXX_REG_DCAP] = 0x76,
175 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
Liam Breck0670c9b2017-06-07 11:37:55 -0700176 [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
177 [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
178 [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
179 [BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
180 [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500181 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700182 bq2750x_regs[BQ27XXX_REG_MAX] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500183 [BQ27XXX_REG_CTRL] = 0x00,
184 [BQ27XXX_REG_TEMP] = 0x06,
185 [BQ27XXX_REG_INT_TEMP] = 0x28,
186 [BQ27XXX_REG_VOLT] = 0x08,
187 [BQ27XXX_REG_AI] = 0x14,
188 [BQ27XXX_REG_FLAGS] = 0x0a,
189 [BQ27XXX_REG_TTE] = 0x16,
190 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
191 [BQ27XXX_REG_TTES] = 0x1a,
192 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
193 [BQ27XXX_REG_NAC] = 0x0c,
194 [BQ27XXX_REG_FCC] = 0x12,
195 [BQ27XXX_REG_CYCT] = 0x2a,
196 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
197 [BQ27XXX_REG_SOC] = 0x2c,
198 [BQ27XXX_REG_DCAP] = 0x3c,
199 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
Liam Breck0670c9b2017-06-07 11:37:55 -0700200 BQ27XXX_DM_REG_ROWS,
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500201 },
Liam Breck10593612017-08-23 20:36:17 -0700202#define bq2751x_regs bq27510g3_regs
203#define bq2752x_regs bq27510g3_regs
Liam Breck9aade6d2017-08-23 20:36:13 -0700204 bq27500_regs[BQ27XXX_REG_MAX] = {
Chris Lapa32833632017-01-11 12:44:40 +1100205 [BQ27XXX_REG_CTRL] = 0x00,
206 [BQ27XXX_REG_TEMP] = 0x06,
207 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
208 [BQ27XXX_REG_VOLT] = 0x08,
209 [BQ27XXX_REG_AI] = 0x14,
210 [BQ27XXX_REG_FLAGS] = 0x0a,
211 [BQ27XXX_REG_TTE] = 0x16,
212 [BQ27XXX_REG_TTF] = 0x18,
213 [BQ27XXX_REG_TTES] = 0x1c,
214 [BQ27XXX_REG_TTECP] = 0x26,
215 [BQ27XXX_REG_NAC] = 0x0c,
216 [BQ27XXX_REG_FCC] = 0x12,
217 [BQ27XXX_REG_CYCT] = 0x2a,
218 [BQ27XXX_REG_AE] = 0x22,
219 [BQ27XXX_REG_SOC] = 0x2c,
220 [BQ27XXX_REG_DCAP] = 0x3c,
221 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700222 BQ27XXX_DM_REG_ROWS,
Chris Lapa32833632017-01-11 12:44:40 +1100223 },
Liam Breck10593612017-08-23 20:36:17 -0700224#define bq27510g1_regs bq27500_regs
225#define bq27510g2_regs bq27500_regs
Liam Breck9aade6d2017-08-23 20:36:13 -0700226 bq27510g3_regs[BQ27XXX_REG_MAX] = {
Chris Lapa71375aa2017-01-11 12:44:43 +1100227 [BQ27XXX_REG_CTRL] = 0x00,
228 [BQ27XXX_REG_TEMP] = 0x06,
229 [BQ27XXX_REG_INT_TEMP] = 0x28,
230 [BQ27XXX_REG_VOLT] = 0x08,
231 [BQ27XXX_REG_AI] = 0x14,
232 [BQ27XXX_REG_FLAGS] = 0x0a,
233 [BQ27XXX_REG_TTE] = 0x16,
234 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
235 [BQ27XXX_REG_TTES] = 0x1a,
236 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
237 [BQ27XXX_REG_NAC] = 0x0c,
238 [BQ27XXX_REG_FCC] = 0x12,
239 [BQ27XXX_REG_CYCT] = 0x1e,
240 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
241 [BQ27XXX_REG_SOC] = 0x20,
242 [BQ27XXX_REG_DCAP] = 0x2e,
243 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
Liam Breck0670c9b2017-06-07 11:37:55 -0700244 BQ27XXX_DM_REG_ROWS,
Chris Lapa71375aa2017-01-11 12:44:43 +1100245 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700246 bq27520g1_regs[BQ27XXX_REG_MAX] = {
Chris Lapa68f2a812017-01-11 12:44:44 +1100247 [BQ27XXX_REG_CTRL] = 0x00,
248 [BQ27XXX_REG_TEMP] = 0x06,
249 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
250 [BQ27XXX_REG_VOLT] = 0x08,
251 [BQ27XXX_REG_AI] = 0x14,
252 [BQ27XXX_REG_FLAGS] = 0x0a,
253 [BQ27XXX_REG_TTE] = 0x16,
254 [BQ27XXX_REG_TTF] = 0x18,
255 [BQ27XXX_REG_TTES] = 0x1c,
256 [BQ27XXX_REG_TTECP] = 0x26,
257 [BQ27XXX_REG_NAC] = 0x0c,
258 [BQ27XXX_REG_FCC] = 0x12,
259 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
260 [BQ27XXX_REG_AE] = 0x22,
261 [BQ27XXX_REG_SOC] = 0x2c,
262 [BQ27XXX_REG_DCAP] = 0x3c,
263 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700264 BQ27XXX_DM_REG_ROWS,
Chris Lapa68f2a812017-01-11 12:44:44 +1100265 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700266 bq27520g2_regs[BQ27XXX_REG_MAX] = {
Chris Lapaa5deb9a2017-01-11 12:44:45 +1100267 [BQ27XXX_REG_CTRL] = 0x00,
268 [BQ27XXX_REG_TEMP] = 0x06,
269 [BQ27XXX_REG_INT_TEMP] = 0x36,
270 [BQ27XXX_REG_VOLT] = 0x08,
271 [BQ27XXX_REG_AI] = 0x14,
272 [BQ27XXX_REG_FLAGS] = 0x0a,
273 [BQ27XXX_REG_TTE] = 0x16,
274 [BQ27XXX_REG_TTF] = 0x18,
275 [BQ27XXX_REG_TTES] = 0x1c,
276 [BQ27XXX_REG_TTECP] = 0x26,
277 [BQ27XXX_REG_NAC] = 0x0c,
278 [BQ27XXX_REG_FCC] = 0x12,
279 [BQ27XXX_REG_CYCT] = 0x2a,
280 [BQ27XXX_REG_AE] = 0x22,
281 [BQ27XXX_REG_SOC] = 0x2c,
282 [BQ27XXX_REG_DCAP] = 0x3c,
283 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700284 BQ27XXX_DM_REG_ROWS,
Chris Lapaa5deb9a2017-01-11 12:44:45 +1100285 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700286 bq27520g3_regs[BQ27XXX_REG_MAX] = {
Chris Lapa825e9152017-01-11 12:44:46 +1100287 [BQ27XXX_REG_CTRL] = 0x00,
288 [BQ27XXX_REG_TEMP] = 0x06,
289 [BQ27XXX_REG_INT_TEMP] = 0x36,
290 [BQ27XXX_REG_VOLT] = 0x08,
291 [BQ27XXX_REG_AI] = 0x14,
292 [BQ27XXX_REG_FLAGS] = 0x0a,
293 [BQ27XXX_REG_TTE] = 0x16,
294 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
295 [BQ27XXX_REG_TTES] = 0x1c,
296 [BQ27XXX_REG_TTECP] = 0x26,
297 [BQ27XXX_REG_NAC] = 0x0c,
298 [BQ27XXX_REG_FCC] = 0x12,
299 [BQ27XXX_REG_CYCT] = 0x2a,
300 [BQ27XXX_REG_AE] = 0x22,
301 [BQ27XXX_REG_SOC] = 0x2c,
302 [BQ27XXX_REG_DCAP] = 0x3c,
303 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700304 BQ27XXX_DM_REG_ROWS,
Chris Lapa825e9152017-01-11 12:44:46 +1100305 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700306 bq27520g4_regs[BQ27XXX_REG_MAX] = {
Chris Lapa8835cae2017-01-11 12:44:47 +1100307 [BQ27XXX_REG_CTRL] = 0x00,
308 [BQ27XXX_REG_TEMP] = 0x06,
309 [BQ27XXX_REG_INT_TEMP] = 0x28,
310 [BQ27XXX_REG_VOLT] = 0x08,
311 [BQ27XXX_REG_AI] = 0x14,
312 [BQ27XXX_REG_FLAGS] = 0x0a,
313 [BQ27XXX_REG_TTE] = 0x16,
314 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
315 [BQ27XXX_REG_TTES] = 0x1c,
316 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
317 [BQ27XXX_REG_NAC] = 0x0c,
318 [BQ27XXX_REG_FCC] = 0x12,
319 [BQ27XXX_REG_CYCT] = 0x1e,
320 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
321 [BQ27XXX_REG_SOC] = 0x20,
322 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
323 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
Liam Breck0670c9b2017-06-07 11:37:55 -0700324 BQ27XXX_DM_REG_ROWS,
Chris Lapa8835cae2017-01-11 12:44:47 +1100325 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700326 bq27530_regs[BQ27XXX_REG_MAX] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500327 [BQ27XXX_REG_CTRL] = 0x00,
328 [BQ27XXX_REG_TEMP] = 0x06,
329 [BQ27XXX_REG_INT_TEMP] = 0x32,
330 [BQ27XXX_REG_VOLT] = 0x08,
331 [BQ27XXX_REG_AI] = 0x14,
332 [BQ27XXX_REG_FLAGS] = 0x0a,
333 [BQ27XXX_REG_TTE] = 0x16,
334 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
335 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
336 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
337 [BQ27XXX_REG_NAC] = 0x0c,
338 [BQ27XXX_REG_FCC] = 0x12,
339 [BQ27XXX_REG_CYCT] = 0x2a,
340 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
341 [BQ27XXX_REG_SOC] = 0x2c,
342 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
343 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700344 BQ27XXX_DM_REG_ROWS,
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500345 },
Liam Breck3a731c62017-08-23 20:36:14 -0700346#define bq27531_regs bq27530_regs
Liam Breck9aade6d2017-08-23 20:36:13 -0700347 bq27541_regs[BQ27XXX_REG_MAX] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500348 [BQ27XXX_REG_CTRL] = 0x00,
349 [BQ27XXX_REG_TEMP] = 0x06,
350 [BQ27XXX_REG_INT_TEMP] = 0x28,
351 [BQ27XXX_REG_VOLT] = 0x08,
352 [BQ27XXX_REG_AI] = 0x14,
353 [BQ27XXX_REG_FLAGS] = 0x0a,
354 [BQ27XXX_REG_TTE] = 0x16,
355 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
356 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
357 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
358 [BQ27XXX_REG_NAC] = 0x0c,
359 [BQ27XXX_REG_FCC] = 0x12,
360 [BQ27XXX_REG_CYCT] = 0x2a,
361 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
362 [BQ27XXX_REG_SOC] = 0x2c,
363 [BQ27XXX_REG_DCAP] = 0x3c,
364 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700365 BQ27XXX_DM_REG_ROWS,
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500366 },
Liam Breck3a731c62017-08-23 20:36:14 -0700367#define bq27542_regs bq27541_regs
368#define bq27546_regs bq27541_regs
369#define bq27742_regs bq27541_regs
Liam Breck9aade6d2017-08-23 20:36:13 -0700370 bq27545_regs[BQ27XXX_REG_MAX] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500371 [BQ27XXX_REG_CTRL] = 0x00,
372 [BQ27XXX_REG_TEMP] = 0x06,
373 [BQ27XXX_REG_INT_TEMP] = 0x28,
374 [BQ27XXX_REG_VOLT] = 0x08,
375 [BQ27XXX_REG_AI] = 0x14,
376 [BQ27XXX_REG_FLAGS] = 0x0a,
377 [BQ27XXX_REG_TTE] = 0x16,
378 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
379 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
380 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
381 [BQ27XXX_REG_NAC] = 0x0c,
382 [BQ27XXX_REG_FCC] = 0x12,
383 [BQ27XXX_REG_CYCT] = 0x2a,
384 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
385 [BQ27XXX_REG_SOC] = 0x2c,
386 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
387 [BQ27XXX_REG_AP] = 0x24,
Liam Breck0670c9b2017-06-07 11:37:55 -0700388 BQ27XXX_DM_REG_ROWS,
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500389 },
Liam Breck9aade6d2017-08-23 20:36:13 -0700390 bq27421_regs[BQ27XXX_REG_MAX] = {
Andrew F. Davis2e05b512016-06-17 17:02:35 -0500391 [BQ27XXX_REG_CTRL] = 0x00,
392 [BQ27XXX_REG_TEMP] = 0x02,
393 [BQ27XXX_REG_INT_TEMP] = 0x1e,
394 [BQ27XXX_REG_VOLT] = 0x04,
395 [BQ27XXX_REG_AI] = 0x10,
396 [BQ27XXX_REG_FLAGS] = 0x06,
397 [BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
398 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
399 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
400 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
401 [BQ27XXX_REG_NAC] = 0x08,
402 [BQ27XXX_REG_FCC] = 0x0e,
403 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
404 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
405 [BQ27XXX_REG_SOC] = 0x1c,
406 [BQ27XXX_REG_DCAP] = 0x3c,
407 [BQ27XXX_REG_AP] = 0x18,
Liam Breck0670c9b2017-06-07 11:37:55 -0700408 BQ27XXX_DM_REG_ROWS,
Liam Breck9aade6d2017-08-23 20:36:13 -0700409 };
Liam Breck3a731c62017-08-23 20:36:14 -0700410#define bq27425_regs bq27421_regs
411#define bq27441_regs bq27421_regs
412#define bq27621_regs bq27421_regs
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500413
Liam Breck9aade6d2017-08-23 20:36:13 -0700414static enum power_supply_property bq27000_props[] = {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200415 POWER_SUPPLY_PROP_STATUS,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700416 POWER_SUPPLY_PROP_PRESENT,
417 POWER_SUPPLY_PROP_VOLTAGE_NOW,
418 POWER_SUPPLY_PROP_CURRENT_NOW,
419 POWER_SUPPLY_PROP_CAPACITY,
Pali Rohárd66bab32011-11-01 01:43:04 +0100420 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700421 POWER_SUPPLY_PROP_TEMP,
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +0200422 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
423 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
424 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Lars-Peter Clausen5661f332010-05-24 20:36:52 +0200425 POWER_SUPPLY_PROP_TECHNOLOGY,
Pali Rohár631c17e2011-02-01 00:08:02 +0100426 POWER_SUPPLY_PROP_CHARGE_FULL,
427 POWER_SUPPLY_PROP_CHARGE_NOW,
428 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Pali Rohár73c244a2011-04-30 23:59:35 +0200429 POWER_SUPPLY_PROP_CYCLE_COUNT,
Pali Rohár631c17e2011-02-01 00:08:02 +0100430 POWER_SUPPLY_PROP_ENERGY_NOW,
Syed Rafiuddin9903e622012-05-17 12:51:15 +0530431 POWER_SUPPLY_PROP_POWER_AVG,
432 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500433 POWER_SUPPLY_PROP_MANUFACTURER,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700434};
435
Liam Breck9aade6d2017-08-23 20:36:13 -0700436static enum power_supply_property bq27010_props[] = {
Saranya Gopala66f59b2012-07-13 20:20:07 -0700437 POWER_SUPPLY_PROP_STATUS,
438 POWER_SUPPLY_PROP_PRESENT,
439 POWER_SUPPLY_PROP_VOLTAGE_NOW,
440 POWER_SUPPLY_PROP_CURRENT_NOW,
441 POWER_SUPPLY_PROP_CAPACITY,
442 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
443 POWER_SUPPLY_PROP_TEMP,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500444 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
445 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
446 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700447 POWER_SUPPLY_PROP_TECHNOLOGY,
448 POWER_SUPPLY_PROP_CHARGE_FULL,
449 POWER_SUPPLY_PROP_CHARGE_NOW,
450 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500451 POWER_SUPPLY_PROP_CYCLE_COUNT,
452 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500453 POWER_SUPPLY_PROP_MANUFACTURER,
Saranya Gopala66f59b2012-07-13 20:20:07 -0700454};
455
Liam Breck10593612017-08-23 20:36:17 -0700456#define bq2750x_props bq27510g3_props
457#define bq2751x_props bq27510g3_props
458#define bq2752x_props bq27510g3_props
Andrew F. Davis3bee9ea2016-11-04 13:33:13 -0500459
Liam Breck9aade6d2017-08-23 20:36:13 -0700460static enum power_supply_property bq27500_props[] = {
Chris Lapa32833632017-01-11 12:44:40 +1100461 POWER_SUPPLY_PROP_STATUS,
462 POWER_SUPPLY_PROP_PRESENT,
463 POWER_SUPPLY_PROP_VOLTAGE_NOW,
464 POWER_SUPPLY_PROP_CURRENT_NOW,
465 POWER_SUPPLY_PROP_CAPACITY,
466 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
467 POWER_SUPPLY_PROP_TEMP,
468 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
469 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
470 POWER_SUPPLY_PROP_TECHNOLOGY,
471 POWER_SUPPLY_PROP_CHARGE_FULL,
472 POWER_SUPPLY_PROP_CHARGE_NOW,
473 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
474 POWER_SUPPLY_PROP_CYCLE_COUNT,
475 POWER_SUPPLY_PROP_ENERGY_NOW,
476 POWER_SUPPLY_PROP_POWER_AVG,
477 POWER_SUPPLY_PROP_HEALTH,
478 POWER_SUPPLY_PROP_MANUFACTURER,
479};
Liam Breck10593612017-08-23 20:36:17 -0700480#define bq27510g1_props bq27500_props
481#define bq27510g2_props bq27500_props
Chris Lapa698a2bf2017-01-11 12:44:42 +1100482
Liam Breck9aade6d2017-08-23 20:36:13 -0700483static enum power_supply_property bq27510g3_props[] = {
Chris Lapa71375aa2017-01-11 12:44:43 +1100484 POWER_SUPPLY_PROP_STATUS,
485 POWER_SUPPLY_PROP_PRESENT,
486 POWER_SUPPLY_PROP_VOLTAGE_NOW,
487 POWER_SUPPLY_PROP_CURRENT_NOW,
488 POWER_SUPPLY_PROP_CAPACITY,
489 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
490 POWER_SUPPLY_PROP_TEMP,
491 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
492 POWER_SUPPLY_PROP_TECHNOLOGY,
493 POWER_SUPPLY_PROP_CHARGE_FULL,
494 POWER_SUPPLY_PROP_CHARGE_NOW,
495 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
496 POWER_SUPPLY_PROP_CYCLE_COUNT,
497 POWER_SUPPLY_PROP_HEALTH,
498 POWER_SUPPLY_PROP_MANUFACTURER,
499};
500
Liam Breck9aade6d2017-08-23 20:36:13 -0700501static enum power_supply_property bq27520g1_props[] = {
Chris Lapa68f2a812017-01-11 12:44:44 +1100502 POWER_SUPPLY_PROP_STATUS,
503 POWER_SUPPLY_PROP_PRESENT,
504 POWER_SUPPLY_PROP_VOLTAGE_NOW,
505 POWER_SUPPLY_PROP_CURRENT_NOW,
506 POWER_SUPPLY_PROP_CAPACITY,
507 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
508 POWER_SUPPLY_PROP_TEMP,
509 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
510 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
511 POWER_SUPPLY_PROP_TECHNOLOGY,
512 POWER_SUPPLY_PROP_CHARGE_FULL,
513 POWER_SUPPLY_PROP_CHARGE_NOW,
514 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
515 POWER_SUPPLY_PROP_ENERGY_NOW,
516 POWER_SUPPLY_PROP_POWER_AVG,
517 POWER_SUPPLY_PROP_HEALTH,
518 POWER_SUPPLY_PROP_MANUFACTURER,
519};
520
Liam Breck10593612017-08-23 20:36:17 -0700521#define bq27520g2_props bq27500_props
Chris Lapaa5deb9a2017-01-11 12:44:45 +1100522
Liam Breck9aade6d2017-08-23 20:36:13 -0700523static enum power_supply_property bq27520g3_props[] = {
Chris Lapa825e9152017-01-11 12:44:46 +1100524 POWER_SUPPLY_PROP_STATUS,
525 POWER_SUPPLY_PROP_PRESENT,
526 POWER_SUPPLY_PROP_VOLTAGE_NOW,
527 POWER_SUPPLY_PROP_CURRENT_NOW,
528 POWER_SUPPLY_PROP_CAPACITY,
529 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
530 POWER_SUPPLY_PROP_TEMP,
531 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
532 POWER_SUPPLY_PROP_TECHNOLOGY,
533 POWER_SUPPLY_PROP_CHARGE_FULL,
534 POWER_SUPPLY_PROP_CHARGE_NOW,
535 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
536 POWER_SUPPLY_PROP_CYCLE_COUNT,
537 POWER_SUPPLY_PROP_ENERGY_NOW,
538 POWER_SUPPLY_PROP_POWER_AVG,
539 POWER_SUPPLY_PROP_HEALTH,
540 POWER_SUPPLY_PROP_MANUFACTURER,
541};
542
Liam Breck9aade6d2017-08-23 20:36:13 -0700543static enum power_supply_property bq27520g4_props[] = {
Chris Lapa8835cae2017-01-11 12:44:47 +1100544 POWER_SUPPLY_PROP_STATUS,
545 POWER_SUPPLY_PROP_PRESENT,
546 POWER_SUPPLY_PROP_VOLTAGE_NOW,
547 POWER_SUPPLY_PROP_CURRENT_NOW,
548 POWER_SUPPLY_PROP_CAPACITY,
549 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
550 POWER_SUPPLY_PROP_TEMP,
551 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
552 POWER_SUPPLY_PROP_TECHNOLOGY,
553 POWER_SUPPLY_PROP_CHARGE_FULL,
554 POWER_SUPPLY_PROP_CHARGE_NOW,
555 POWER_SUPPLY_PROP_CYCLE_COUNT,
556 POWER_SUPPLY_PROP_HEALTH,
557 POWER_SUPPLY_PROP_MANUFACTURER,
558};
559
Liam Breck9aade6d2017-08-23 20:36:13 -0700560static enum power_supply_property bq27530_props[] = {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500561 POWER_SUPPLY_PROP_STATUS,
562 POWER_SUPPLY_PROP_PRESENT,
563 POWER_SUPPLY_PROP_VOLTAGE_NOW,
564 POWER_SUPPLY_PROP_CURRENT_NOW,
565 POWER_SUPPLY_PROP_CAPACITY,
566 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
567 POWER_SUPPLY_PROP_TEMP,
568 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
569 POWER_SUPPLY_PROP_TECHNOLOGY,
570 POWER_SUPPLY_PROP_CHARGE_FULL,
571 POWER_SUPPLY_PROP_CHARGE_NOW,
572 POWER_SUPPLY_PROP_POWER_AVG,
573 POWER_SUPPLY_PROP_HEALTH,
574 POWER_SUPPLY_PROP_CYCLE_COUNT,
575 POWER_SUPPLY_PROP_MANUFACTURER,
576};
Liam Breck3a731c62017-08-23 20:36:14 -0700577#define bq27531_props bq27530_props
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500578
Liam Breck9aade6d2017-08-23 20:36:13 -0700579static enum power_supply_property bq27541_props[] = {
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700580 POWER_SUPPLY_PROP_STATUS,
581 POWER_SUPPLY_PROP_PRESENT,
582 POWER_SUPPLY_PROP_VOLTAGE_NOW,
583 POWER_SUPPLY_PROP_CURRENT_NOW,
584 POWER_SUPPLY_PROP_CAPACITY,
585 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
586 POWER_SUPPLY_PROP_TEMP,
587 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
588 POWER_SUPPLY_PROP_TECHNOLOGY,
589 POWER_SUPPLY_PROP_CHARGE_FULL,
590 POWER_SUPPLY_PROP_CHARGE_NOW,
591 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
592 POWER_SUPPLY_PROP_CYCLE_COUNT,
593 POWER_SUPPLY_PROP_POWER_AVG,
594 POWER_SUPPLY_PROP_HEALTH,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500595 POWER_SUPPLY_PROP_MANUFACTURER,
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700596};
Liam Breck3a731c62017-08-23 20:36:14 -0700597#define bq27542_props bq27541_props
598#define bq27546_props bq27541_props
599#define bq27742_props bq27541_props
Puthikorn Voravootivat628ef022014-09-09 12:20:35 -0700600
Liam Breck9aade6d2017-08-23 20:36:13 -0700601static enum power_supply_property bq27545_props[] = {
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100602 POWER_SUPPLY_PROP_STATUS,
603 POWER_SUPPLY_PROP_PRESENT,
604 POWER_SUPPLY_PROP_VOLTAGE_NOW,
605 POWER_SUPPLY_PROP_CURRENT_NOW,
606 POWER_SUPPLY_PROP_CAPACITY,
607 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
608 POWER_SUPPLY_PROP_TEMP,
609 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
610 POWER_SUPPLY_PROP_TECHNOLOGY,
611 POWER_SUPPLY_PROP_CHARGE_FULL,
612 POWER_SUPPLY_PROP_CHARGE_NOW,
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500613 POWER_SUPPLY_PROP_HEALTH,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100614 POWER_SUPPLY_PROP_CYCLE_COUNT,
615 POWER_SUPPLY_PROP_POWER_AVG,
Andrew F. Davisdb04fc52015-07-22 16:51:53 -0500616 POWER_SUPPLY_PROP_MANUFACTURER,
Alexandre Bellonif46bf822015-02-28 18:59:28 +0100617};
618
Liam Breck9aade6d2017-08-23 20:36:13 -0700619static enum power_supply_property bq27421_props[] = {
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500620 POWER_SUPPLY_PROP_STATUS,
621 POWER_SUPPLY_PROP_PRESENT,
622 POWER_SUPPLY_PROP_VOLTAGE_NOW,
623 POWER_SUPPLY_PROP_CURRENT_NOW,
624 POWER_SUPPLY_PROP_CAPACITY,
625 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
626 POWER_SUPPLY_PROP_TEMP,
627 POWER_SUPPLY_PROP_TECHNOLOGY,
628 POWER_SUPPLY_PROP_CHARGE_FULL,
629 POWER_SUPPLY_PROP_CHARGE_NOW,
630 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
631 POWER_SUPPLY_PROP_MANUFACTURER,
632};
Liam Breck3a731c62017-08-23 20:36:14 -0700633#define bq27425_props bq27421_props
634#define bq27441_props bq27421_props
635#define bq27621_props bq27421_props
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500636
Liam Breck05045372017-08-23 20:36:15 -0700637struct bq27xxx_dm_reg {
638 u8 subclass_id;
639 u8 offset;
640 u8 bytes;
641 u16 min, max;
642};
643
644enum bq27xxx_dm_reg_id {
645 BQ27XXX_DM_DESIGN_CAPACITY = 0,
646 BQ27XXX_DM_DESIGN_ENERGY,
647 BQ27XXX_DM_TERMINATE_VOLTAGE,
648};
649
650#define bq27000_dm_regs 0
651#define bq27010_dm_regs 0
652#define bq2750x_dm_regs 0
653#define bq2751x_dm_regs 0
654#define bq2752x_dm_regs 0
655
656#if 0 /* not yet tested */
657static struct bq27xxx_dm_reg bq27500_dm_regs[] = {
658 [BQ27XXX_DM_DESIGN_CAPACITY] = { 48, 10, 2, 0, 65535 },
659 [BQ27XXX_DM_DESIGN_ENERGY] = { }, /* missing on chip */
660 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 48, 2, 1000, 32767 },
661};
662#else
663#define bq27500_dm_regs 0
664#endif
665
666/* todo create data memory definitions from datasheets and test on chips */
667#define bq27510g1_dm_regs 0
668#define bq27510g2_dm_regs 0
669#define bq27510g3_dm_regs 0
670#define bq27520g1_dm_regs 0
671#define bq27520g2_dm_regs 0
672#define bq27520g3_dm_regs 0
673#define bq27520g4_dm_regs 0
674#define bq27530_dm_regs 0
675#define bq27531_dm_regs 0
676#define bq27541_dm_regs 0
677#define bq27542_dm_regs 0
678#define bq27546_dm_regs 0
679#define bq27742_dm_regs 0
680
681#if 0 /* not yet tested */
682static struct bq27xxx_dm_reg bq27545_dm_regs[] = {
683 [BQ27XXX_DM_DESIGN_CAPACITY] = { 48, 23, 2, 0, 32767 },
684 [BQ27XXX_DM_DESIGN_ENERGY] = { 48, 25, 2, 0, 32767 },
685 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 67, 2, 2800, 3700 },
686};
687#else
688#define bq27545_dm_regs 0
689#endif
690
Liam Breck05045372017-08-23 20:36:15 -0700691static struct bq27xxx_dm_reg bq27421_dm_regs[] = {
692 [BQ27XXX_DM_DESIGN_CAPACITY] = { 82, 10, 2, 0, 8000 },
693 [BQ27XXX_DM_DESIGN_ENERGY] = { 82, 12, 2, 0, 32767 },
694 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 16, 2, 2500, 3700 },
695};
Liam Breck05045372017-08-23 20:36:15 -0700696
697static struct bq27xxx_dm_reg bq27425_dm_regs[] = {
698 [BQ27XXX_DM_DESIGN_CAPACITY] = { 82, 12, 2, 0, 32767 },
699 [BQ27XXX_DM_DESIGN_ENERGY] = { 82, 14, 2, 0, 32767 },
700 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 18, 2, 2800, 3700 },
701};
702
703#if 0 /* not yet tested */
704#define bq27441_dm_regs bq27421_dm_regs
705#else
706#define bq27441_dm_regs 0
707#endif
708
709#if 0 /* not yet tested */
710static struct bq27xxx_dm_reg bq27621_dm_regs[] = {
711 [BQ27XXX_DM_DESIGN_CAPACITY] = { 82, 3, 2, 0, 8000 },
712 [BQ27XXX_DM_DESIGN_ENERGY] = { 82, 5, 2, 0, 32767 },
713 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 9, 2, 2500, 3700 },
714};
715#else
716#define bq27621_dm_regs 0
717#endif
718
Liam Breck3a731c62017-08-23 20:36:14 -0700719#define BQ27XXX_O_ZERO 0x00000001
720#define BQ27XXX_O_OTDC 0x00000002
721#define BQ27XXX_O_UTOT 0x00000004
Liam Breck05045372017-08-23 20:36:15 -0700722#define BQ27XXX_O_CFGUP 0x00000008
723#define BQ27XXX_O_RAM 0x00000010
Liam Breck3a731c62017-08-23 20:36:14 -0700724
Liam Breck05045372017-08-23 20:36:15 -0700725#define BQ27XXX_DATA(ref, key, opt) { \
Liam Breck3a731c62017-08-23 20:36:14 -0700726 .opts = (opt), \
Liam Breck05045372017-08-23 20:36:15 -0700727 .unseal_key = key, \
Liam Breck9aade6d2017-08-23 20:36:13 -0700728 .regs = ref##_regs, \
Liam Breck05045372017-08-23 20:36:15 -0700729 .dm_regs = ref##_dm_regs, \
Liam Breck9aade6d2017-08-23 20:36:13 -0700730 .props = ref##_props, \
731 .props_size = ARRAY_SIZE(ref##_props) }
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500732
733static struct {
Liam Breck3a731c62017-08-23 20:36:14 -0700734 u32 opts;
Liam Breck05045372017-08-23 20:36:15 -0700735 u32 unseal_key;
Liam Breck9aade6d2017-08-23 20:36:13 -0700736 u8 *regs;
Liam Breck05045372017-08-23 20:36:15 -0700737 struct bq27xxx_dm_reg *dm_regs;
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500738 enum power_supply_property *props;
Liam Breck9aade6d2017-08-23 20:36:13 -0700739 size_t props_size;
740} bq27xxx_chip_data[] = {
Liam Breck05045372017-08-23 20:36:15 -0700741 [BQ27000] = BQ27XXX_DATA(bq27000, 0 , BQ27XXX_O_ZERO),
742 [BQ27010] = BQ27XXX_DATA(bq27010, 0 , BQ27XXX_O_ZERO),
743 [BQ2750X] = BQ27XXX_DATA(bq2750x, 0 , BQ27XXX_O_OTDC),
744 [BQ2751X] = BQ27XXX_DATA(bq2751x, 0 , BQ27XXX_O_OTDC),
745 [BQ2752X] = BQ27XXX_DATA(bq2752x, 0 , BQ27XXX_O_OTDC),
746 [BQ27500] = BQ27XXX_DATA(bq27500, 0x04143672, BQ27XXX_O_OTDC),
747 [BQ27510G1] = BQ27XXX_DATA(bq27510g1, 0 , BQ27XXX_O_OTDC),
748 [BQ27510G2] = BQ27XXX_DATA(bq27510g2, 0 , BQ27XXX_O_OTDC),
749 [BQ27510G3] = BQ27XXX_DATA(bq27510g3, 0 , BQ27XXX_O_OTDC),
750 [BQ27520G1] = BQ27XXX_DATA(bq27520g1, 0 , BQ27XXX_O_OTDC),
751 [BQ27520G2] = BQ27XXX_DATA(bq27520g2, 0 , BQ27XXX_O_OTDC),
752 [BQ27520G3] = BQ27XXX_DATA(bq27520g3, 0 , BQ27XXX_O_OTDC),
753 [BQ27520G4] = BQ27XXX_DATA(bq27520g4, 0 , BQ27XXX_O_OTDC),
754 [BQ27530] = BQ27XXX_DATA(bq27530, 0 , BQ27XXX_O_UTOT),
755 [BQ27531] = BQ27XXX_DATA(bq27531, 0 , BQ27XXX_O_UTOT),
756 [BQ27541] = BQ27XXX_DATA(bq27541, 0 , BQ27XXX_O_OTDC),
757 [BQ27542] = BQ27XXX_DATA(bq27542, 0 , BQ27XXX_O_OTDC),
758 [BQ27546] = BQ27XXX_DATA(bq27546, 0 , BQ27XXX_O_OTDC),
759 [BQ27742] = BQ27XXX_DATA(bq27742, 0 , BQ27XXX_O_OTDC),
760 [BQ27545] = BQ27XXX_DATA(bq27545, 0x04143672, BQ27XXX_O_OTDC),
761 [BQ27421] = BQ27XXX_DATA(bq27421, 0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
762 [BQ27425] = BQ27XXX_DATA(bq27425, 0x04143672, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP),
763 [BQ27441] = BQ27XXX_DATA(bq27441, 0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
764 [BQ27621] = BQ27XXX_DATA(bq27621, 0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500765};
766
Matt Ranostay1d727062016-09-19 20:43:02 -0700767static DEFINE_MUTEX(bq27xxx_list_lock);
768static LIST_HEAD(bq27xxx_battery_devices);
769
Liam Breck0670c9b2017-06-07 11:37:55 -0700770#define BQ27XXX_MSLEEP(i) usleep_range((i)*1000, (i)*1000+500)
771
772#define BQ27XXX_DM_SZ 32
773
774/**
775 * struct bq27xxx_dm_buf - chip data memory buffer
776 * @class: data memory subclass_id
777 * @block: data memory block number
778 * @data: data from/for the block
779 * @has_data: true if data has been filled by read
780 * @dirty: true if data has changed since last read/write
781 *
782 * Encapsulates info required to manage chip data memory blocks.
783 */
784struct bq27xxx_dm_buf {
785 u8 class;
786 u8 block;
787 u8 data[BQ27XXX_DM_SZ];
788 bool has_data, dirty;
789};
790
Liam Breckccce4402017-06-07 11:37:56 -0700791#define BQ27XXX_DM_BUF(di, i) { \
792 .class = (di)->dm_regs[i].subclass_id, \
793 .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
794}
795
796static inline u16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf,
797 struct bq27xxx_dm_reg *reg)
798{
799 if (buf->class == reg->subclass_id &&
800 buf->block == reg->offset / BQ27XXX_DM_SZ)
801 return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ);
802
803 return NULL;
804}
805
Liam Breckccce4402017-06-07 11:37:56 -0700806static const char * const bq27xxx_dm_reg_name[] = {
807 [BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
808 [BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
809 [BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
810};
811
812
813static bool bq27xxx_dt_to_nvm = true;
814module_param_named(dt_monitored_battery_updates_nvm, bq27xxx_dt_to_nvm, bool, 0444);
815MODULE_PARM_DESC(dt_monitored_battery_updates_nvm,
816 "Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
817 "Users must set this =0 when installing a different type of battery!\n"
818 "Default is =1."
819#ifndef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
820 "\nSetting this affects future kernel updates, not the current configuration."
821#endif
822);
Liam Breck0670c9b2017-06-07 11:37:55 -0700823
Matt Ranostay1d727062016-09-19 20:43:02 -0700824static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
825{
826 struct bq27xxx_device_info *di;
Matt Ranostay950b6c22016-09-30 21:45:30 -0700827 unsigned int prev_val = *(unsigned int *) kp->arg;
Matt Ranostay1d727062016-09-19 20:43:02 -0700828 int ret;
829
830 ret = param_set_uint(val, kp);
Matt Ranostay950b6c22016-09-30 21:45:30 -0700831 if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
Matt Ranostay1d727062016-09-19 20:43:02 -0700832 return ret;
833
834 mutex_lock(&bq27xxx_list_lock);
835 list_for_each_entry(di, &bq27xxx_battery_devices, list) {
836 cancel_delayed_work_sync(&di->work);
837 schedule_delayed_work(&di->work, 0);
838 }
839 mutex_unlock(&bq27xxx_list_lock);
840
841 return ret;
842}
843
844static const struct kernel_param_ops param_ops_poll_interval = {
845 .get = param_get_uint,
846 .set = poll_interval_param_set,
847};
848
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100849static unsigned int poll_interval = 360;
Matt Ranostay1d727062016-09-19 20:43:02 -0700850module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
Andrew F. Davis6d0a1812015-07-22 16:51:56 -0500851MODULE_PARM_DESC(poll_interval,
852 "battery poll interval in seconds - 0 disables polling");
Lars-Peter Clausen740b7552011-01-07 20:14:53 +0100853
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700854/*
Andrew F. Davis081bab22015-09-22 14:35:06 -0500855 * Common code for BQ27xxx devices
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700856 */
857
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500858static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
Andrew F. Davisce33edf2015-07-22 16:51:55 -0500859 bool single)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -0700860{
Matt Ranostay14073f62017-06-07 11:37:54 -0700861 int ret;
862
Andrew F. Davisd74534c2015-09-22 14:35:09 -0500863 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
864 return -EINVAL;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +0100865
Matt Ranostay14073f62017-06-07 11:37:54 -0700866 ret = di->bus.read(di, di->regs[reg_index], single);
867 if (ret < 0)
868 dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n",
869 di->regs[reg_index], reg_index);
870
871 return ret;
872}
873
874static inline int bq27xxx_write(struct bq27xxx_device_info *di, int reg_index,
875 u16 value, bool single)
876{
877 int ret;
878
879 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
880 return -EINVAL;
881
882 if (!di->bus.write)
883 return -EPERM;
884
885 ret = di->bus.write(di, di->regs[reg_index], value, single);
886 if (ret < 0)
887 dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n",
888 di->regs[reg_index], reg_index);
889
890 return ret;
891}
892
893static inline int bq27xxx_read_block(struct bq27xxx_device_info *di, int reg_index,
894 u8 *data, int len)
895{
896 int ret;
897
898 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
899 return -EINVAL;
900
901 if (!di->bus.read_bulk)
902 return -EPERM;
903
904 ret = di->bus.read_bulk(di, di->regs[reg_index], data, len);
905 if (ret < 0)
906 dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n",
907 di->regs[reg_index], reg_index);
908
909 return ret;
910}
911
912static inline int bq27xxx_write_block(struct bq27xxx_device_info *di, int reg_index,
913 u8 *data, int len)
914{
915 int ret;
916
917 if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
918 return -EINVAL;
919
920 if (!di->bus.write_bulk)
921 return -EPERM;
922
923 ret = di->bus.write_bulk(di, di->regs[reg_index], data, len);
924 if (ret < 0)
925 dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n",
926 di->regs[reg_index], reg_index);
927
928 return ret;
Saranya Gopala66f59b2012-07-13 20:20:07 -0700929}
930
Liam Breck0670c9b2017-06-07 11:37:55 -0700931static int bq27xxx_battery_seal(struct bq27xxx_device_info *di)
932{
933 int ret;
934
935 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_SEALED, false);
936 if (ret < 0) {
937 dev_err(di->dev, "bus error on seal: %d\n", ret);
938 return ret;
939 }
940
941 return 0;
942}
943
944static int bq27xxx_battery_unseal(struct bq27xxx_device_info *di)
945{
946 int ret;
947
948 if (di->unseal_key == 0) {
949 dev_err(di->dev, "unseal failed due to missing key\n");
950 return -EINVAL;
951 }
952
953 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false);
954 if (ret < 0)
955 goto out;
956
957 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false);
958 if (ret < 0)
959 goto out;
960
961 return 0;
962
963out:
964 dev_err(di->dev, "bus error on unseal: %d\n", ret);
965 return ret;
966}
967
968static u8 bq27xxx_battery_checksum_dm_block(struct bq27xxx_dm_buf *buf)
969{
970 u16 sum = 0;
971 int i;
972
973 for (i = 0; i < BQ27XXX_DM_SZ; i++)
974 sum += buf->data[i];
975 sum &= 0xff;
976
977 return 0xff - sum;
978}
979
980static int bq27xxx_battery_read_dm_block(struct bq27xxx_device_info *di,
981 struct bq27xxx_dm_buf *buf)
982{
983 int ret;
984
985 buf->has_data = false;
986
987 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
988 if (ret < 0)
989 goto out;
990
991 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
992 if (ret < 0)
993 goto out;
994
995 BQ27XXX_MSLEEP(1);
996
997 ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
998 if (ret < 0)
999 goto out;
1000
1001 ret = bq27xxx_read(di, BQ27XXX_DM_CKSUM, true);
1002 if (ret < 0)
1003 goto out;
1004
1005 if ((u8)ret != bq27xxx_battery_checksum_dm_block(buf)) {
1006 ret = -EINVAL;
1007 goto out;
1008 }
1009
1010 buf->has_data = true;
1011 buf->dirty = false;
1012
1013 return 0;
1014
1015out:
1016 dev_err(di->dev, "bus error reading chip memory: %d\n", ret);
1017 return ret;
1018}
1019
Liam Breckccce4402017-06-07 11:37:56 -07001020static void bq27xxx_battery_update_dm_block(struct bq27xxx_device_info *di,
1021 struct bq27xxx_dm_buf *buf,
1022 enum bq27xxx_dm_reg_id reg_id,
1023 unsigned int val)
1024{
1025 struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
1026 const char *str = bq27xxx_dm_reg_name[reg_id];
1027 u16 *prev = bq27xxx_dm_reg_ptr(buf, reg);
1028
1029 if (prev == NULL) {
1030 dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
1031 return;
1032 }
1033
1034 if (reg->bytes != 2) {
1035 dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
1036 return;
1037 }
1038
1039 if (!buf->has_data)
1040 return;
1041
1042 if (be16_to_cpup(prev) == val) {
1043 dev_info(di->dev, "%s has %u\n", str, val);
1044 return;
1045 }
1046
1047#ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
Liam Breck05045372017-08-23 20:36:15 -07001048 if (!(di->opts & BQ27XXX_O_RAM) && !bq27xxx_dt_to_nvm) {
Liam Breckccce4402017-06-07 11:37:56 -07001049#else
Liam Breck05045372017-08-23 20:36:15 -07001050 if (!(di->opts & BQ27XXX_O_RAM)) {
Liam Breckccce4402017-06-07 11:37:56 -07001051#endif
1052 /* devicetree and NVM differ; defer to NVM */
1053 dev_warn(di->dev, "%s has %u; update to %u disallowed "
1054#ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
1055 "by dt_monitored_battery_updates_nvm=0"
1056#else
1057 "for flash/NVM data memory"
1058#endif
1059 "\n", str, be16_to_cpup(prev), val);
1060 return;
1061 }
1062
1063 dev_info(di->dev, "update %s to %u\n", str, val);
1064
1065 *prev = cpu_to_be16(val);
1066 buf->dirty = true;
1067}
1068
Liam Breck0670c9b2017-06-07 11:37:55 -07001069static int bq27xxx_battery_cfgupdate_priv(struct bq27xxx_device_info *di, bool active)
1070{
1071 const int limit = 100;
1072 u16 cmd = active ? BQ27XXX_SET_CFGUPDATE : BQ27XXX_SOFT_RESET;
1073 int ret, try = limit;
1074
1075 ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, cmd, false);
1076 if (ret < 0)
1077 return ret;
1078
1079 do {
1080 BQ27XXX_MSLEEP(25);
1081 ret = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
1082 if (ret < 0)
1083 return ret;
1084 } while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try);
1085
Liam Breck05045372017-08-23 20:36:15 -07001086 if (!try && di->chip != BQ27425) { // 425 has a bug
Liam Breck0670c9b2017-06-07 11:37:55 -07001087 dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active);
1088 return -EINVAL;
1089 }
1090
1091 if (limit - try > 3)
1092 dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try);
1093
1094 return 0;
1095}
1096
1097static inline int bq27xxx_battery_set_cfgupdate(struct bq27xxx_device_info *di)
1098{
1099 int ret = bq27xxx_battery_cfgupdate_priv(di, true);
1100 if (ret < 0 && ret != -EINVAL)
1101 dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret);
1102
1103 return ret;
1104}
1105
1106static inline int bq27xxx_battery_soft_reset(struct bq27xxx_device_info *di)
1107{
1108 int ret = bq27xxx_battery_cfgupdate_priv(di, false);
1109 if (ret < 0 && ret != -EINVAL)
1110 dev_err(di->dev, "bus error on soft_reset: %d\n", ret);
1111
1112 return ret;
1113}
1114
1115static int bq27xxx_battery_write_dm_block(struct bq27xxx_device_info *di,
1116 struct bq27xxx_dm_buf *buf)
1117{
Liam Breck05045372017-08-23 20:36:15 -07001118 bool cfgup = di->opts & BQ27XXX_O_CFGUP;
Liam Breck0670c9b2017-06-07 11:37:55 -07001119 int ret;
1120
1121 if (!buf->dirty)
1122 return 0;
1123
1124 if (cfgup) {
1125 ret = bq27xxx_battery_set_cfgupdate(di);
1126 if (ret < 0)
1127 return ret;
1128 }
1129
1130 ret = bq27xxx_write(di, BQ27XXX_DM_CTRL, 0, true);
1131 if (ret < 0)
1132 goto out;
1133
1134 ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
1135 if (ret < 0)
1136 goto out;
1137
1138 ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
1139 if (ret < 0)
1140 goto out;
1141
1142 BQ27XXX_MSLEEP(1);
1143
1144 ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
1145 if (ret < 0)
1146 goto out;
1147
1148 ret = bq27xxx_write(di, BQ27XXX_DM_CKSUM,
1149 bq27xxx_battery_checksum_dm_block(buf), true);
1150 if (ret < 0)
1151 goto out;
1152
1153 /* DO NOT read BQ27XXX_DM_CKSUM here to verify it! That may cause NVM
1154 * corruption on the '425 chip (and perhaps others), which can damage
1155 * the chip.
1156 */
1157
1158 if (cfgup) {
1159 BQ27XXX_MSLEEP(1);
1160 ret = bq27xxx_battery_soft_reset(di);
1161 if (ret < 0)
1162 return ret;
1163 } else {
1164 BQ27XXX_MSLEEP(100); /* flash DM updates in <100ms */
1165 }
1166
1167 buf->dirty = false;
1168
1169 return 0;
1170
1171out:
1172 if (cfgup)
1173 bq27xxx_battery_soft_reset(di);
1174
1175 dev_err(di->dev, "bus error writing chip memory: %d\n", ret);
1176 return ret;
1177}
1178
Liam Breckccce4402017-06-07 11:37:56 -07001179static void bq27xxx_battery_set_config(struct bq27xxx_device_info *di,
1180 struct power_supply_battery_info *info)
1181{
1182 struct bq27xxx_dm_buf bd = BQ27XXX_DM_BUF(di, BQ27XXX_DM_DESIGN_CAPACITY);
1183 struct bq27xxx_dm_buf bt = BQ27XXX_DM_BUF(di, BQ27XXX_DM_TERMINATE_VOLTAGE);
1184 bool updated;
1185
1186 if (bq27xxx_battery_unseal(di) < 0)
1187 return;
1188
1189 if (info->charge_full_design_uah != -EINVAL &&
1190 info->energy_full_design_uwh != -EINVAL) {
1191 bq27xxx_battery_read_dm_block(di, &bd);
1192 /* assume design energy & capacity are in same block */
1193 bq27xxx_battery_update_dm_block(di, &bd,
1194 BQ27XXX_DM_DESIGN_CAPACITY,
1195 info->charge_full_design_uah / 1000);
1196 bq27xxx_battery_update_dm_block(di, &bd,
1197 BQ27XXX_DM_DESIGN_ENERGY,
1198 info->energy_full_design_uwh / 1000);
1199 }
1200
1201 if (info->voltage_min_design_uv != -EINVAL) {
1202 bool same = bd.class == bt.class && bd.block == bt.block;
1203 if (!same)
1204 bq27xxx_battery_read_dm_block(di, &bt);
1205 bq27xxx_battery_update_dm_block(di, same ? &bd : &bt,
1206 BQ27XXX_DM_TERMINATE_VOLTAGE,
1207 info->voltage_min_design_uv / 1000);
1208 }
1209
1210 updated = bd.dirty || bt.dirty;
1211
1212 bq27xxx_battery_write_dm_block(di, &bd);
1213 bq27xxx_battery_write_dm_block(di, &bt);
1214
1215 bq27xxx_battery_seal(di);
1216
Liam Breck05045372017-08-23 20:36:15 -07001217 if (updated && !(di->opts & BQ27XXX_O_CFGUP)) {
Liam Breckccce4402017-06-07 11:37:56 -07001218 bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_RESET, false);
1219 BQ27XXX_MSLEEP(300); /* reset time is not documented */
1220 }
1221 /* assume bq27xxx_battery_update() is called hereafter */
1222}
1223
1224static void bq27xxx_battery_settings(struct bq27xxx_device_info *di)
1225{
1226 struct power_supply_battery_info info = {};
1227 unsigned int min, max;
1228
1229 if (power_supply_get_battery_info(di->bat, &info) < 0)
1230 return;
1231
1232 if (!di->dm_regs) {
1233 dev_warn(di->dev, "data memory update not supported for chip\n");
1234 return;
1235 }
1236
1237 if (info.energy_full_design_uwh != info.charge_full_design_uah) {
1238 if (info.energy_full_design_uwh == -EINVAL)
1239 dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
1240 else if (info.charge_full_design_uah == -EINVAL)
1241 dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
1242 }
1243
1244 /* assume min == 0 */
1245 max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
1246 if (info.energy_full_design_uwh > max * 1000) {
1247 dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
1248 info.energy_full_design_uwh);
1249 info.energy_full_design_uwh = -EINVAL;
1250 }
1251
1252 /* assume min == 0 */
1253 max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
1254 if (info.charge_full_design_uah > max * 1000) {
1255 dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
1256 info.charge_full_design_uah);
1257 info.charge_full_design_uah = -EINVAL;
1258 }
1259
1260 min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
1261 max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
1262 if ((info.voltage_min_design_uv < min * 1000 ||
1263 info.voltage_min_design_uv > max * 1000) &&
1264 info.voltage_min_design_uv != -EINVAL) {
1265 dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
1266 info.voltage_min_design_uv);
1267 info.voltage_min_design_uv = -EINVAL;
1268 }
1269
1270 if ((info.energy_full_design_uwh != -EINVAL &&
1271 info.charge_full_design_uah != -EINVAL) ||
1272 info.voltage_min_design_uv != -EINVAL)
1273 bq27xxx_battery_set_config(di, &info);
1274}
1275
Saranya Gopala66f59b2012-07-13 20:20:07 -07001276/*
Andrew F. Davisc5709032015-09-22 14:35:08 -05001277 * Return the battery State-of-Charge
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001278 * Or < 0 if something fails.
1279 */
Andrew F. Davisc5709032015-09-22 14:35:08 -05001280static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001281{
Andrew F. Davisc5709032015-09-22 14:35:08 -05001282 int soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001283
Liam Breck3a731c62017-08-23 20:36:14 -07001284 if (di->opts & BQ27XXX_O_ZERO)
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +01001285 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
1286 else
1287 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001288
Andrew F. Davisc5709032015-09-22 14:35:08 -05001289 if (soc < 0)
1290 dev_dbg(di->dev, "error reading State-of-Charge\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001291
Andrew F. Davisc5709032015-09-22 14:35:08 -05001292 return soc;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001293}
1294
1295/*
Pali Rohár631c17e2011-02-01 00:08:02 +01001296 * Return a battery charge value in µAh
1297 * Or < 0 if something fails.
1298 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001299static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
Pali Rohár631c17e2011-02-01 00:08:02 +01001300{
1301 int charge;
1302
Andrew F. Davis081bab22015-09-22 14:35:06 -05001303 charge = bq27xxx_read(di, reg, false);
Pali Rohár631c17e2011-02-01 00:08:02 +01001304 if (charge < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001305 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
1306 reg, charge);
Pali Rohár631c17e2011-02-01 00:08:02 +01001307 return charge;
1308 }
1309
Liam Breck3a731c62017-08-23 20:36:14 -07001310 if (di->opts & BQ27XXX_O_ZERO)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001311 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +01001312 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001313 charge *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +01001314
1315 return charge;
1316}
1317
1318/*
Andrew F. Davisc5709032015-09-22 14:35:08 -05001319 * Return the battery Nominal available capacity in µAh
Pali Rohár631c17e2011-02-01 00:08:02 +01001320 * Or < 0 if something fails.
1321 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001322static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +01001323{
Pali Roháre59ec4a2012-11-10 15:42:48 +01001324 int flags;
Pali Roháre59ec4a2012-11-10 15:42:48 +01001325
Liam Breck3a731c62017-08-23 20:36:14 -07001326 if (di->opts & BQ27XXX_O_ZERO) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001327 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
1328 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
1329 return -ENODATA;
Andrew F. Davis6d0a1812015-07-22 16:51:56 -05001330 }
Pali Rohár631c17e2011-02-01 00:08:02 +01001331
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001332 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
1333}
1334
1335/*
1336 * Return the battery Full Charge Capacity in µAh
1337 * Or < 0 if something fails.
1338 */
1339static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
1340{
1341 return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
1342}
1343
1344/*
1345 * Return the Design Capacity in µAh
1346 * Or < 0 if something fails.
1347 */
1348static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
1349{
1350 int dcap;
1351
Liam Breck3a731c62017-08-23 20:36:14 -07001352 if (di->opts & BQ27XXX_O_ZERO)
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +01001353 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
1354 else
1355 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001356
1357 if (dcap < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001358 dev_dbg(di->dev, "error reading initial last measured discharge\n");
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001359 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +01001360 }
1361
Liam Breck3a731c62017-08-23 20:36:14 -07001362 if (di->opts & BQ27XXX_O_ZERO)
H. Nikolaus Schaller549d7b32015-12-17 11:12:53 +01001363 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
Pali Rohár631c17e2011-02-01 00:08:02 +01001364 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001365 dcap *= 1000;
Pali Rohár631c17e2011-02-01 00:08:02 +01001366
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001367 return dcap;
Pali Rohár631c17e2011-02-01 00:08:02 +01001368}
1369
1370/*
Pali Rohára8f6bd22011-11-01 01:43:06 +01001371 * Return the battery Available energy in µWh
1372 * Or < 0 if something fails.
1373 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001374static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
Pali Rohára8f6bd22011-11-01 01:43:06 +01001375{
1376 int ae;
1377
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001378 ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
Pali Rohára8f6bd22011-11-01 01:43:06 +01001379 if (ae < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001380 dev_dbg(di->dev, "error reading available energy\n");
Pali Rohára8f6bd22011-11-01 01:43:06 +01001381 return ae;
1382 }
1383
Liam Breck3a731c62017-08-23 20:36:14 -07001384 if (di->opts & BQ27XXX_O_ZERO)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001385 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
Pali Rohára8f6bd22011-11-01 01:43:06 +01001386 else
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001387 ae *= 1000;
Pali Rohára8f6bd22011-11-01 01:43:06 +01001388
1389 return ae;
1390}
1391
1392/*
Pali Rohár5dc34432013-02-02 11:06:09 +01001393 * Return the battery temperature in tenths of degree Kelvin
Pali Rohárd149e982011-11-01 01:43:07 +01001394 * Or < 0 if something fails.
1395 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001396static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
Pali Rohárd149e982011-11-01 01:43:07 +01001397{
1398 int temp;
1399
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001400 temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
Pali Rohárd149e982011-11-01 01:43:07 +01001401 if (temp < 0) {
1402 dev_err(di->dev, "error reading temperature\n");
1403 return temp;
1404 }
1405
Liam Breck3a731c62017-08-23 20:36:14 -07001406 if (di->opts & BQ27XXX_O_ZERO)
Pali Rohár5dc34432013-02-02 11:06:09 +01001407 temp = 5 * temp / 2;
Pali Rohárd149e982011-11-01 01:43:07 +01001408
1409 return temp;
1410}
1411
1412/*
Pali Rohár631c17e2011-02-01 00:08:02 +01001413 * Return the battery Cycle count total
1414 * Or < 0 if something fails.
1415 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001416static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
Pali Rohár631c17e2011-02-01 00:08:02 +01001417{
1418 int cyct;
1419
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001420 cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
Pali Rohár631c17e2011-02-01 00:08:02 +01001421 if (cyct < 0)
1422 dev_err(di->dev, "error reading cycle count total\n");
1423
1424 return cyct;
1425}
1426
1427/*
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001428 * Read a time register.
1429 * Return < 0 if something fails.
1430 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001431static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001432{
1433 int tval;
1434
Andrew F. Davis081bab22015-09-22 14:35:06 -05001435 tval = bq27xxx_read(di, reg, false);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001436 if (tval < 0) {
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001437 dev_dbg(di->dev, "error reading time register %02x: %d\n",
1438 reg, tval);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001439 return tval;
1440 }
1441
1442 if (tval == 65535)
1443 return -ENODATA;
1444
1445 return tval * 60;
1446}
1447
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301448/*
Andrew F. Davisc5709032015-09-22 14:35:08 -05001449 * Read an average power register.
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301450 * Return < 0 if something fails.
1451 */
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001452static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301453{
1454 int tval;
1455
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001456 tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301457 if (tval < 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001458 dev_err(di->dev, "error reading average power register %02x: %d\n",
1459 BQ27XXX_REG_AP, tval);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301460 return tval;
1461 }
1462
Liam Breck3a731c62017-08-23 20:36:14 -07001463 if (di->opts & BQ27XXX_O_ZERO)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001464 return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001465 else
1466 return tval;
1467}
1468
1469/*
1470 * Returns true if a battery over temperature condition is detected
1471 */
Andrew F. Davis74aab842015-09-22 14:35:10 -05001472static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001473{
Liam Breck3a731c62017-08-23 20:36:14 -07001474 if (di->opts & BQ27XXX_O_OTDC)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001475 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
Liam Breck3a731c62017-08-23 20:36:14 -07001476 if (di->opts & BQ27XXX_O_UTOT)
Andrew F. Davis74aab842015-09-22 14:35:10 -05001477 return flags & BQ27XXX_FLAG_OT;
Liam Breck3a731c62017-08-23 20:36:14 -07001478
1479 return false;
Andrew F. Davis74aab842015-09-22 14:35:10 -05001480}
1481
1482/*
1483 * Returns true if a battery under temperature condition is detected
1484 */
1485static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
1486{
Liam Breck3a731c62017-08-23 20:36:14 -07001487 if (di->opts & BQ27XXX_O_UTOT)
Andrew F. Davis74aab842015-09-22 14:35:10 -05001488 return flags & BQ27XXX_FLAG_UT;
1489
1490 return false;
1491}
1492
1493/*
1494 * Returns true if a low state of charge condition is detected
1495 */
1496static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
1497{
Liam Breck3a731c62017-08-23 20:36:14 -07001498 if (di->opts & BQ27XXX_O_ZERO)
Andrew F. Davis74aab842015-09-22 14:35:10 -05001499 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001500 else
Andrew F. Davis74aab842015-09-22 14:35:10 -05001501 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301502}
1503
1504/*
1505 * Read flag register.
1506 * Return < 0 if something fails.
1507 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001508static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301509{
Andrzej Hajda2edd69a2015-09-28 10:51:27 +02001510 int flags;
Liam Breck3a731c62017-08-23 20:36:14 -07001511 bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301512
H. Nikolaus Schallere4a404a2016-07-18 18:12:09 +02001513 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
Andrew F. Davis74aab842015-09-22 14:35:10 -05001514 if (flags < 0) {
1515 dev_err(di->dev, "error reading flag register:%d\n", flags);
1516 return flags;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301517 }
1518
Andrew F. Davis74aab842015-09-22 14:35:10 -05001519 /* Unlikely but important to return first */
1520 if (unlikely(bq27xxx_battery_overtemp(di, flags)))
1521 return POWER_SUPPLY_HEALTH_OVERHEAT;
1522 if (unlikely(bq27xxx_battery_undertemp(di, flags)))
1523 return POWER_SUPPLY_HEALTH_COLD;
1524 if (unlikely(bq27xxx_battery_dead(di, flags)))
1525 return POWER_SUPPLY_HEALTH_DEAD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301526
Andrew F. Davis74aab842015-09-22 14:35:10 -05001527 return POWER_SUPPLY_HEALTH_GOOD;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301528}
1529
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001530void bq27xxx_battery_update(struct bq27xxx_device_info *di)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001531{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001532 struct bq27xxx_reg_cache cache = {0, };
Liam Breck3a731c62017-08-23 20:36:14 -07001533 bool has_ci_flag = di->opts & BQ27XXX_O_ZERO;
1534 bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001535
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001536 cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
Marek Belisko3dd843e2014-06-30 21:49:56 +02001537 if ((cache.flags & 0xff) == 0xff)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001538 cache.flags = -1; /* read error */
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001539 if (cache.flags >= 0) {
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001540 cache.temperature = bq27xxx_battery_read_temperature(di);
1541 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
NeilBrown1f94b252015-10-07 20:42:38 +02001542 dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
Pali Rohár4b226c22011-11-01 01:43:05 +01001543 cache.capacity = -ENODATA;
Pali Rohára8f6bd22011-11-01 01:43:06 +01001544 cache.energy = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +01001545 cache.time_to_empty = -ENODATA;
1546 cache.time_to_empty_avg = -ENODATA;
1547 cache.time_to_full = -ENODATA;
1548 cache.charge_full = -ENODATA;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301549 cache.health = -ENODATA;
Pali Rohár4b226c22011-11-01 01:43:05 +01001550 } else {
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001551 if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
1552 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
1553 if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
1554 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
1555 if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
1556 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
1557 cache.charge_full = bq27xxx_battery_read_fcc(di);
Andrew F. Davisc5709032015-09-22 14:35:08 -05001558 cache.capacity = bq27xxx_battery_read_soc(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001559 if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001560 cache.energy = bq27xxx_battery_read_energy(di);
Andrew F. Davis081bab22015-09-22 14:35:06 -05001561 cache.health = bq27xxx_battery_read_health(di);
Pali Rohár4b226c22011-11-01 01:43:05 +01001562 }
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001563 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
Andrew F. Davis081bab22015-09-22 14:35:06 -05001564 cache.cycle_count = bq27xxx_battery_read_cyct(di);
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001565 if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
1566 cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001567
Pali Rohár631c17e2011-02-01 00:08:02 +01001568 /* We only have to read charge design full once */
1569 if (di->charge_design_full <= 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001570 di->charge_design_full = bq27xxx_battery_read_dcap(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001571 }
1572
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -07001573 if (di->cache.capacity != cache.capacity)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001574 power_supply_changed(di->bat);
Puthikorn Voravootivat90f04a22014-10-21 18:15:37 -07001575
1576 if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
1577 di->cache = cache;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001578
1579 di->last_update = jiffies;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001580}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001581EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001582
Andrew F. Davis081bab22015-09-22 14:35:06 -05001583static void bq27xxx_battery_poll(struct work_struct *work)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001584{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001585 struct bq27xxx_device_info *di =
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001586 container_of(work, struct bq27xxx_device_info,
1587 work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001588
Andrew F. Davis081bab22015-09-22 14:35:06 -05001589 bq27xxx_battery_update(di);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001590
Thomas Gleixner53bf8372016-07-04 09:50:31 +00001591 if (poll_interval > 0)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001592 schedule_delayed_work(&di->work, poll_interval * HZ);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001593}
1594
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001595/*
Pali Rohárbf7d4142011-02-01 00:23:13 +01001596 * Return the battery average current in µA
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001597 * Note that current can be negative signed as well
1598 * Or 0 if something fails.
1599 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001600static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001601 union power_supply_propval *val)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001602{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001603 int curr;
Pali Rohárb68f6212011-11-01 01:43:03 +01001604 int flags;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001605
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001606 curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001607 if (curr < 0) {
1608 dev_err(di->dev, "error reading current\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001609 return curr;
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001610 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001611
Liam Breck3a731c62017-08-23 20:36:14 -07001612 if (di->opts & BQ27XXX_O_ZERO) {
H. Nikolaus Schallere4a404a2016-07-18 18:12:09 +02001613 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
Pali Rohárb68f6212011-11-01 01:43:03 +01001614 if (flags & BQ27000_FLAG_CHGS) {
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001615 dev_dbg(di->dev, "negative current!\n");
Grazvydas Ignotasafbc74f2010-02-27 17:06:44 +02001616 curr = -curr;
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001617 }
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001618
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001619 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1620 } else {
1621 /* Other gauges return signed value */
1622 val->intval = (int)((s16)curr) * 1000;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001623 }
Grazvydas Ignotase20908d2010-02-12 23:57:23 +02001624
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001625 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001626}
1627
Andrew F. Davis081bab22015-09-22 14:35:06 -05001628static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001629 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001630{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001631 int status;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001632
Liam Breck3a731c62017-08-23 20:36:14 -07001633 if (di->opts & BQ27XXX_O_ZERO) {
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +02001634 if (di->cache.flags & BQ27000_FLAG_FC)
1635 status = POWER_SUPPLY_STATUS_FULL;
1636 else if (di->cache.flags & BQ27000_FLAG_CHGS)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001637 status = POWER_SUPPLY_STATUS_CHARGING;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001638 else if (power_supply_am_i_supplied(di->bat))
Lars-Peter Clausenc1b9ab62010-09-18 17:31:16 +02001639 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001640 else
1641 status = POWER_SUPPLY_STATUS_DISCHARGING;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001642 } else {
1643 if (di->cache.flags & BQ27XXX_FLAG_FC)
1644 status = POWER_SUPPLY_STATUS_FULL;
1645 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
1646 status = POWER_SUPPLY_STATUS_DISCHARGING;
1647 else
1648 status = POWER_SUPPLY_STATUS_CHARGING;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001649 }
1650
1651 val->intval = status;
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001652
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001653 return 0;
1654}
1655
Andrew F. Davis081bab22015-09-22 14:35:06 -05001656static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001657 union power_supply_propval *val)
Pali Rohárd66bab32011-11-01 01:43:04 +01001658{
1659 int level;
1660
Liam Breck3a731c62017-08-23 20:36:14 -07001661 if (di->opts & BQ27XXX_O_ZERO) {
Pali Rohárd66bab32011-11-01 01:43:04 +01001662 if (di->cache.flags & BQ27000_FLAG_FC)
1663 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1664 else if (di->cache.flags & BQ27000_FLAG_EDV1)
1665 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1666 else if (di->cache.flags & BQ27000_FLAG_EDVF)
1667 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1668 else
1669 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001670 } else {
1671 if (di->cache.flags & BQ27XXX_FLAG_FC)
1672 level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1673 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
1674 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1675 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
1676 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1677 else
1678 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
Pali Rohárd66bab32011-11-01 01:43:04 +01001679 }
1680
1681 val->intval = level;
1682
1683 return 0;
1684}
1685
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001686/*
Thomas Weber91fe4d52012-02-17 17:46:21 +01001687 * Return the battery Voltage in millivolts
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001688 * Or < 0 if something fails.
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001689 */
Andrew F. Davis081bab22015-09-22 14:35:06 -05001690static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001691 union power_supply_propval *val)
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001692{
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001693 int volt;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001694
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001695 volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001696 if (volt < 0) {
1697 dev_err(di->dev, "error reading voltage\n");
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001698 return volt;
Pali Rohárc6cd4f22011-11-01 01:43:09 +01001699 }
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001700
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001701 val->intval = volt * 1000;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001702
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001703 return 0;
1704}
1705
Andrew F. Davis081bab22015-09-22 14:35:06 -05001706static int bq27xxx_simple_value(int value,
Andrew F. Davisce33edf2015-07-22 16:51:55 -05001707 union power_supply_propval *val)
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001708{
1709 if (value < 0)
1710 return value;
1711
1712 val->intval = value;
1713
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001714 return 0;
1715}
1716
Andrew F. Davis081bab22015-09-22 14:35:06 -05001717static int bq27xxx_battery_get_property(struct power_supply *psy,
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001718 enum power_supply_property psp,
1719 union power_supply_propval *val)
1720{
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001721 int ret = 0;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001722 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +02001723
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001724 mutex_lock(&di->lock);
1725 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
1726 cancel_delayed_work_sync(&di->work);
Andrew F. Davis081bab22015-09-22 14:35:06 -05001727 bq27xxx_battery_poll(&di->work.work);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001728 }
1729 mutex_unlock(&di->lock);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001730
1731 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +02001732 return -ENODEV;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001733
1734 switch (psp) {
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001735 case POWER_SUPPLY_PROP_STATUS:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001736 ret = bq27xxx_battery_status(di, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001737 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001738 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001739 ret = bq27xxx_battery_voltage(di, val);
Lars-Peter Clausen3413b4e2010-05-24 21:57:33 +02001740 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001741 case POWER_SUPPLY_PROP_PRESENT:
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001742 val->intval = di->cache.flags < 0 ? 0 : 1;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001743 break;
1744 case POWER_SUPPLY_PROP_CURRENT_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001745 ret = bq27xxx_battery_current(di, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001746 break;
1747 case POWER_SUPPLY_PROP_CAPACITY:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001748 ret = bq27xxx_simple_value(di->cache.capacity, val);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001749 break;
Pali Rohárd66bab32011-11-01 01:43:04 +01001750 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001751 ret = bq27xxx_battery_capacity_level(di, val);
Pali Rohárd66bab32011-11-01 01:43:04 +01001752 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001753 case POWER_SUPPLY_PROP_TEMP:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001754 ret = bq27xxx_simple_value(di->cache.temperature, val);
Pali Rohár5dc34432013-02-02 11:06:09 +01001755 if (ret == 0)
Andrew F. Davisd74534c2015-09-22 14:35:09 -05001756 val->intval -= 2731; /* convert decidegree k to c */
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001757 break;
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001758 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001759 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001760 break;
1761 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001762 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001763 break;
1764 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001765 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001766 break;
Lars-Peter Clausen5661f332010-05-24 20:36:52 +02001767 case POWER_SUPPLY_PROP_TECHNOLOGY:
1768 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1769 break;
Pali Rohár631c17e2011-02-01 00:08:02 +01001770 case POWER_SUPPLY_PROP_CHARGE_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001771 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001772 break;
1773 case POWER_SUPPLY_PROP_CHARGE_FULL:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001774 ret = bq27xxx_simple_value(di->cache.charge_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001775 break;
1776 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001777 ret = bq27xxx_simple_value(di->charge_design_full, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001778 break;
Liam Breckccce4402017-06-07 11:37:56 -07001779 /*
1780 * TODO: Implement these to make registers set from
1781 * power_supply_battery_info visible in sysfs.
1782 */
1783 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
1784 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1785 return -EINVAL;
Pali Rohár73c244a2011-04-30 23:59:35 +02001786 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001787 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001788 break;
1789 case POWER_SUPPLY_PROP_ENERGY_NOW:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001790 ret = bq27xxx_simple_value(di->cache.energy, val);
Pali Rohár631c17e2011-02-01 00:08:02 +01001791 break;
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301792 case POWER_SUPPLY_PROP_POWER_AVG:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001793 ret = bq27xxx_simple_value(di->cache.power_avg, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301794 break;
1795 case POWER_SUPPLY_PROP_HEALTH:
Andrew F. Davis081bab22015-09-22 14:35:06 -05001796 ret = bq27xxx_simple_value(di->cache.health, val);
Syed Rafiuddin9903e622012-05-17 12:51:15 +05301797 break;
Andrew F. Davisdb04fc52015-07-22 16:51:53 -05001798 case POWER_SUPPLY_PROP_MANUFACTURER:
1799 val->strval = BQ27XXX_MANUFACTURER;
1800 break;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001801 default:
1802 return -EINVAL;
1803 }
1804
Grazvydas Ignotas4e924a82010-02-27 17:06:09 +02001805 return ret;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001806}
1807
Andrew F. Davis081bab22015-09-22 14:35:06 -05001808static void bq27xxx_external_power_changed(struct power_supply *psy)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001809{
Andrew F. Davis081bab22015-09-22 14:35:06 -05001810 struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001811
1812 cancel_delayed_work_sync(&di->work);
1813 schedule_delayed_work(&di->work, 0);
1814}
1815
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001816int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001817{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001818 struct power_supply_desc *psy_desc;
Liam Breckccce4402017-06-07 11:37:56 -07001819 struct power_supply_config psy_cfg = {
1820 .of_node = di->dev->of_node,
1821 .drv_data = di,
1822 };
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001823
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001824 INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
1825 mutex_init(&di->lock);
Liam Breck3a731c62017-08-23 20:36:14 -07001826
Liam Breck05045372017-08-23 20:36:15 -07001827 di->regs = bq27xxx_chip_data[di->chip].regs;
1828 di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
1829 di->dm_regs = bq27xxx_chip_data[di->chip].dm_regs;
1830 di->opts = bq27xxx_chip_data[di->chip].opts;
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001831
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001832 psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
1833 if (!psy_desc)
1834 return -ENOMEM;
1835
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001836 psy_desc->name = di->name;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001837 psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
Liam Breck9aade6d2017-08-23 20:36:13 -07001838 psy_desc->properties = bq27xxx_chip_data[di->chip].props;
1839 psy_desc->num_properties = bq27xxx_chip_data[di->chip].props_size;
Andrew F. Davis081bab22015-09-22 14:35:06 -05001840 psy_desc->get_property = bq27xxx_battery_get_property;
1841 psy_desc->external_power_changed = bq27xxx_external_power_changed;
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001842
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001843 di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
1844 if (IS_ERR(di->bat)) {
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001845 dev_err(di->dev, "failed to register battery\n");
1846 return PTR_ERR(di->bat);
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001847 }
1848
Liam Breckccce4402017-06-07 11:37:56 -07001849 bq27xxx_battery_settings(di);
Andrew F. Davis081bab22015-09-22 14:35:06 -05001850 bq27xxx_battery_update(di);
Lars-Peter Clausen297a5332011-01-07 20:12:47 +01001851
Matt Ranostay1d727062016-09-19 20:43:02 -07001852 mutex_lock(&bq27xxx_list_lock);
1853 list_add(&di->list, &bq27xxx_battery_devices);
1854 mutex_unlock(&bq27xxx_list_lock);
1855
Lars-Peter Clausena40402e2010-05-24 19:37:58 +02001856 return 0;
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001857}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001858EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001859
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001860void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001861{
Pali Rohár8cfaaa82011-11-01 01:43:11 +01001862 /*
Andrew F. Davis081bab22015-09-22 14:35:06 -05001863 * power_supply_unregister call bq27xxx_battery_get_property which
1864 * call bq27xxx_battery_poll.
1865 * Make sure that bq27xxx_battery_poll will not call
Pali Rohár8cfaaa82011-11-01 01:43:11 +01001866 * schedule_delayed_work again after unregister (which cause OOPS).
1867 */
1868 poll_interval = 0;
1869
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001870 cancel_delayed_work_sync(&di->work);
1871
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001872 power_supply_unregister(di->bat);
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001873
Matt Ranostay1d727062016-09-19 20:43:02 -07001874 mutex_lock(&bq27xxx_list_lock);
1875 list_del(&di->list);
1876 mutex_unlock(&bq27xxx_list_lock);
1877
Lars-Peter Clausen740b7552011-01-07 20:14:53 +01001878 mutex_destroy(&di->lock);
1879}
Andrew F. Davis703df6c2015-11-23 10:53:51 -06001880EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
Lars-Peter Clausen7fb7ba52010-05-24 19:55:27 +02001881
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001882MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
Andrew F. Davis081bab22015-09-22 14:35:06 -05001883MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
Rodolfo Giomettib996ad02008-08-20 16:52:58 -07001884MODULE_LICENSE("GPL");