GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / power / supply / bq27xxx_battery.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * BQ27xxx battery driver
4  *
5  * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
6  * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
7  * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
8  * Copyright (C) 2011 Pali Rohár <pali@kernel.org>
9  * Copyright (C) 2017 Liam Breck <kernel@networkimprov.net>
10  *
11  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
12  *
13  * Datasheets:
14  * https://www.ti.com/product/bq27000
15  * https://www.ti.com/product/bq27200
16  * https://www.ti.com/product/bq27010
17  * https://www.ti.com/product/bq27210
18  * https://www.ti.com/product/bq27500
19  * https://www.ti.com/product/bq27510-g1
20  * https://www.ti.com/product/bq27510-g2
21  * https://www.ti.com/product/bq27510-g3
22  * https://www.ti.com/product/bq27520-g1
23  * https://www.ti.com/product/bq27520-g2
24  * https://www.ti.com/product/bq27520-g3
25  * https://www.ti.com/product/bq27520-g4
26  * https://www.ti.com/product/bq27530-g1
27  * https://www.ti.com/product/bq27531-g1
28  * https://www.ti.com/product/bq27541-g1
29  * https://www.ti.com/product/bq27542-g1
30  * https://www.ti.com/product/bq27546-g1
31  * https://www.ti.com/product/bq27742-g1
32  * https://www.ti.com/product/bq27545-g1
33  * https://www.ti.com/product/bq27421-g1
34  * https://www.ti.com/product/bq27425-g1
35  * https://www.ti.com/product/bq27426
36  * https://www.ti.com/product/bq27411-g1
37  * https://www.ti.com/product/bq27441-g1
38  * https://www.ti.com/product/bq27621-g1
39  * https://www.ti.com/product/bq27z561
40  * https://www.ti.com/product/bq28z610
41  * https://www.ti.com/product/bq34z100-g1
42  */
43
44 #include <linux/device.h>
45 #include <linux/module.h>
46 #include <linux/mutex.h>
47 #include <linux/param.h>
48 #include <linux/jiffies.h>
49 #include <linux/workqueue.h>
50 #include <linux/delay.h>
51 #include <linux/platform_device.h>
52 #include <linux/power_supply.h>
53 #include <linux/slab.h>
54 #include <linux/of.h>
55
56 #include <linux/power/bq27xxx_battery.h>
57
58 #define BQ27XXX_MANUFACTURER    "Texas Instruments"
59
60 /* 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_CFGUP      BIT(4)
65 #define BQ27XXX_FLAG_FC         BIT(9)
66 #define BQ27XXX_FLAG_OTD        BIT(14)
67 #define BQ27XXX_FLAG_OTC        BIT(15)
68 #define BQ27XXX_FLAG_UT         BIT(14)
69 #define BQ27XXX_FLAG_OT         BIT(15)
70
71 /* BQ27000 has different layout for Flags register */
72 #define BQ27000_FLAG_EDVF       BIT(0) /* Final End-of-Discharge-Voltage flag */
73 #define BQ27000_FLAG_EDV1       BIT(1) /* First End-of-Discharge-Voltage flag */
74 #define BQ27000_FLAG_CI         BIT(4) /* Capacity Inaccurate flag */
75 #define BQ27000_FLAG_FC         BIT(5)
76 #define BQ27000_FLAG_CHGS       BIT(7) /* Charge state flag */
77
78 /* BQ27Z561 has different layout for Flags register */
79 #define BQ27Z561_FLAG_FDC       BIT(4) /* Battery fully discharged */
80 #define BQ27Z561_FLAG_FC        BIT(5) /* Battery fully charged */
81 #define BQ27Z561_FLAG_DIS_CH    BIT(6) /* Battery is discharging */
82
83 /* control register params */
84 #define BQ27XXX_SEALED                  0x20
85 #define BQ27XXX_SET_CFGUPDATE           0x13
86 #define BQ27XXX_SOFT_RESET              0x42
87 #define BQ27XXX_RESET                   0x41
88
89 #define BQ27XXX_RS                      (20) /* Resistor sense mOhm */
90 #define BQ27XXX_POWER_CONSTANT          (29200) /* 29.2 µV^2 * 1000 */
91 #define BQ27XXX_CURRENT_CONSTANT        (3570) /* 3.57 µV * 1000 */
92
93 #define INVALID_REG_ADDR        0xff
94
95 /*
96  * bq27xxx_reg_index - Register names
97  *
98  * These are indexes into a device's register mapping array.
99  */
100
101 enum bq27xxx_reg_index {
102         BQ27XXX_REG_CTRL = 0,   /* Control */
103         BQ27XXX_REG_TEMP,       /* Temperature */
104         BQ27XXX_REG_INT_TEMP,   /* Internal Temperature */
105         BQ27XXX_REG_VOLT,       /* Voltage */
106         BQ27XXX_REG_AI,         /* Average Current */
107         BQ27XXX_REG_FLAGS,      /* Flags */
108         BQ27XXX_REG_TTE,        /* Time-to-Empty */
109         BQ27XXX_REG_TTF,        /* Time-to-Full */
110         BQ27XXX_REG_TTES,       /* Time-to-Empty Standby */
111         BQ27XXX_REG_TTECP,      /* Time-to-Empty at Constant Power */
112         BQ27XXX_REG_NAC,        /* Nominal Available Capacity */
113         BQ27XXX_REG_FCC,        /* Full Charge Capacity */
114         BQ27XXX_REG_CYCT,       /* Cycle Count */
115         BQ27XXX_REG_AE,         /* Available Energy */
116         BQ27XXX_REG_SOC,        /* State-of-Charge */
117         BQ27XXX_REG_DCAP,       /* Design Capacity */
118         BQ27XXX_REG_AP,         /* Average Power */
119         BQ27XXX_DM_CTRL,        /* Block Data Control */
120         BQ27XXX_DM_CLASS,       /* Data Class */
121         BQ27XXX_DM_BLOCK,       /* Data Block */
122         BQ27XXX_DM_DATA,        /* Block Data */
123         BQ27XXX_DM_CKSUM,       /* Block Data Checksum */
124         BQ27XXX_REG_MAX,        /* sentinel */
125 };
126
127 #define BQ27XXX_DM_REG_ROWS \
128         [BQ27XXX_DM_CTRL] = 0x61,  \
129         [BQ27XXX_DM_CLASS] = 0x3e, \
130         [BQ27XXX_DM_BLOCK] = 0x3f, \
131         [BQ27XXX_DM_DATA] = 0x40,  \
132         [BQ27XXX_DM_CKSUM] = 0x60
133
134 /* Register mappings */
135 static u8
136         bq27000_regs[BQ27XXX_REG_MAX] = {
137                 [BQ27XXX_REG_CTRL] = 0x00,
138                 [BQ27XXX_REG_TEMP] = 0x06,
139                 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
140                 [BQ27XXX_REG_VOLT] = 0x08,
141                 [BQ27XXX_REG_AI] = 0x14,
142                 [BQ27XXX_REG_FLAGS] = 0x0a,
143                 [BQ27XXX_REG_TTE] = 0x16,
144                 [BQ27XXX_REG_TTF] = 0x18,
145                 [BQ27XXX_REG_TTES] = 0x1c,
146                 [BQ27XXX_REG_TTECP] = 0x26,
147                 [BQ27XXX_REG_NAC] = 0x0c,
148                 [BQ27XXX_REG_FCC] = 0x12,
149                 [BQ27XXX_REG_CYCT] = 0x2a,
150                 [BQ27XXX_REG_AE] = 0x22,
151                 [BQ27XXX_REG_SOC] = 0x0b,
152                 [BQ27XXX_REG_DCAP] = 0x76,
153                 [BQ27XXX_REG_AP] = 0x24,
154                 [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
155                 [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
156                 [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
157                 [BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
158                 [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
159         },
160         bq27010_regs[BQ27XXX_REG_MAX] = {
161                 [BQ27XXX_REG_CTRL] = 0x00,
162                 [BQ27XXX_REG_TEMP] = 0x06,
163                 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
164                 [BQ27XXX_REG_VOLT] = 0x08,
165                 [BQ27XXX_REG_AI] = 0x14,
166                 [BQ27XXX_REG_FLAGS] = 0x0a,
167                 [BQ27XXX_REG_TTE] = 0x16,
168                 [BQ27XXX_REG_TTF] = 0x18,
169                 [BQ27XXX_REG_TTES] = 0x1c,
170                 [BQ27XXX_REG_TTECP] = 0x26,
171                 [BQ27XXX_REG_NAC] = 0x0c,
172                 [BQ27XXX_REG_FCC] = 0x12,
173                 [BQ27XXX_REG_CYCT] = 0x2a,
174                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
175                 [BQ27XXX_REG_SOC] = 0x0b,
176                 [BQ27XXX_REG_DCAP] = 0x76,
177                 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
178                 [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
179                 [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
180                 [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
181                 [BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
182                 [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
183         },
184         bq2750x_regs[BQ27XXX_REG_MAX] = {
185                 [BQ27XXX_REG_CTRL] = 0x00,
186                 [BQ27XXX_REG_TEMP] = 0x06,
187                 [BQ27XXX_REG_INT_TEMP] = 0x28,
188                 [BQ27XXX_REG_VOLT] = 0x08,
189                 [BQ27XXX_REG_AI] = 0x14,
190                 [BQ27XXX_REG_FLAGS] = 0x0a,
191                 [BQ27XXX_REG_TTE] = 0x16,
192                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
193                 [BQ27XXX_REG_TTES] = 0x1a,
194                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
195                 [BQ27XXX_REG_NAC] = 0x0c,
196                 [BQ27XXX_REG_FCC] = 0x12,
197                 [BQ27XXX_REG_CYCT] = 0x2a,
198                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
199                 [BQ27XXX_REG_SOC] = 0x2c,
200                 [BQ27XXX_REG_DCAP] = 0x3c,
201                 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
202                 BQ27XXX_DM_REG_ROWS,
203         },
204 #define bq2751x_regs bq27510g3_regs
205 #define bq2752x_regs bq27510g3_regs
206         bq27500_regs[BQ27XXX_REG_MAX] = {
207                 [BQ27XXX_REG_CTRL] = 0x00,
208                 [BQ27XXX_REG_TEMP] = 0x06,
209                 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
210                 [BQ27XXX_REG_VOLT] = 0x08,
211                 [BQ27XXX_REG_AI] = 0x14,
212                 [BQ27XXX_REG_FLAGS] = 0x0a,
213                 [BQ27XXX_REG_TTE] = 0x16,
214                 [BQ27XXX_REG_TTF] = 0x18,
215                 [BQ27XXX_REG_TTES] = 0x1c,
216                 [BQ27XXX_REG_TTECP] = 0x26,
217                 [BQ27XXX_REG_NAC] = 0x0c,
218                 [BQ27XXX_REG_FCC] = 0x12,
219                 [BQ27XXX_REG_CYCT] = 0x2a,
220                 [BQ27XXX_REG_AE] = 0x22,
221                 [BQ27XXX_REG_SOC] = 0x2c,
222                 [BQ27XXX_REG_DCAP] = 0x3c,
223                 [BQ27XXX_REG_AP] = 0x24,
224                 BQ27XXX_DM_REG_ROWS,
225         },
226 #define bq27510g1_regs bq27500_regs
227 #define bq27510g2_regs bq27500_regs
228         bq27510g3_regs[BQ27XXX_REG_MAX] = {
229                 [BQ27XXX_REG_CTRL] = 0x00,
230                 [BQ27XXX_REG_TEMP] = 0x06,
231                 [BQ27XXX_REG_INT_TEMP] = 0x28,
232                 [BQ27XXX_REG_VOLT] = 0x08,
233                 [BQ27XXX_REG_AI] = 0x14,
234                 [BQ27XXX_REG_FLAGS] = 0x0a,
235                 [BQ27XXX_REG_TTE] = 0x16,
236                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
237                 [BQ27XXX_REG_TTES] = 0x1a,
238                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
239                 [BQ27XXX_REG_NAC] = 0x0c,
240                 [BQ27XXX_REG_FCC] = 0x12,
241                 [BQ27XXX_REG_CYCT] = 0x1e,
242                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
243                 [BQ27XXX_REG_SOC] = 0x20,
244                 [BQ27XXX_REG_DCAP] = 0x2e,
245                 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
246                 BQ27XXX_DM_REG_ROWS,
247         },
248         bq27520g1_regs[BQ27XXX_REG_MAX] = {
249                 [BQ27XXX_REG_CTRL] = 0x00,
250                 [BQ27XXX_REG_TEMP] = 0x06,
251                 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
252                 [BQ27XXX_REG_VOLT] = 0x08,
253                 [BQ27XXX_REG_AI] = 0x14,
254                 [BQ27XXX_REG_FLAGS] = 0x0a,
255                 [BQ27XXX_REG_TTE] = 0x16,
256                 [BQ27XXX_REG_TTF] = 0x18,
257                 [BQ27XXX_REG_TTES] = 0x1c,
258                 [BQ27XXX_REG_TTECP] = 0x26,
259                 [BQ27XXX_REG_NAC] = 0x0c,
260                 [BQ27XXX_REG_FCC] = 0x12,
261                 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
262                 [BQ27XXX_REG_AE] = 0x22,
263                 [BQ27XXX_REG_SOC] = 0x2c,
264                 [BQ27XXX_REG_DCAP] = 0x3c,
265                 [BQ27XXX_REG_AP] = 0x24,
266                 BQ27XXX_DM_REG_ROWS,
267         },
268         bq27520g2_regs[BQ27XXX_REG_MAX] = {
269                 [BQ27XXX_REG_CTRL] = 0x00,
270                 [BQ27XXX_REG_TEMP] = 0x06,
271                 [BQ27XXX_REG_INT_TEMP] = 0x36,
272                 [BQ27XXX_REG_VOLT] = 0x08,
273                 [BQ27XXX_REG_AI] = 0x14,
274                 [BQ27XXX_REG_FLAGS] = 0x0a,
275                 [BQ27XXX_REG_TTE] = 0x16,
276                 [BQ27XXX_REG_TTF] = 0x18,
277                 [BQ27XXX_REG_TTES] = 0x1c,
278                 [BQ27XXX_REG_TTECP] = 0x26,
279                 [BQ27XXX_REG_NAC] = 0x0c,
280                 [BQ27XXX_REG_FCC] = 0x12,
281                 [BQ27XXX_REG_CYCT] = 0x2a,
282                 [BQ27XXX_REG_AE] = 0x22,
283                 [BQ27XXX_REG_SOC] = 0x2c,
284                 [BQ27XXX_REG_DCAP] = 0x3c,
285                 [BQ27XXX_REG_AP] = 0x24,
286                 BQ27XXX_DM_REG_ROWS,
287         },
288         bq27520g3_regs[BQ27XXX_REG_MAX] = {
289                 [BQ27XXX_REG_CTRL] = 0x00,
290                 [BQ27XXX_REG_TEMP] = 0x06,
291                 [BQ27XXX_REG_INT_TEMP] = 0x36,
292                 [BQ27XXX_REG_VOLT] = 0x08,
293                 [BQ27XXX_REG_AI] = 0x14,
294                 [BQ27XXX_REG_FLAGS] = 0x0a,
295                 [BQ27XXX_REG_TTE] = 0x16,
296                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
297                 [BQ27XXX_REG_TTES] = 0x1c,
298                 [BQ27XXX_REG_TTECP] = 0x26,
299                 [BQ27XXX_REG_NAC] = 0x0c,
300                 [BQ27XXX_REG_FCC] = 0x12,
301                 [BQ27XXX_REG_CYCT] = 0x2a,
302                 [BQ27XXX_REG_AE] = 0x22,
303                 [BQ27XXX_REG_SOC] = 0x2c,
304                 [BQ27XXX_REG_DCAP] = 0x3c,
305                 [BQ27XXX_REG_AP] = 0x24,
306                 BQ27XXX_DM_REG_ROWS,
307         },
308         bq27520g4_regs[BQ27XXX_REG_MAX] = {
309                 [BQ27XXX_REG_CTRL] = 0x00,
310                 [BQ27XXX_REG_TEMP] = 0x06,
311                 [BQ27XXX_REG_INT_TEMP] = 0x28,
312                 [BQ27XXX_REG_VOLT] = 0x08,
313                 [BQ27XXX_REG_AI] = 0x14,
314                 [BQ27XXX_REG_FLAGS] = 0x0a,
315                 [BQ27XXX_REG_TTE] = 0x16,
316                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
317                 [BQ27XXX_REG_TTES] = 0x1c,
318                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
319                 [BQ27XXX_REG_NAC] = 0x0c,
320                 [BQ27XXX_REG_FCC] = 0x12,
321                 [BQ27XXX_REG_CYCT] = 0x1e,
322                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
323                 [BQ27XXX_REG_SOC] = 0x20,
324                 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
325                 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
326                 BQ27XXX_DM_REG_ROWS,
327         },
328         bq27521_regs[BQ27XXX_REG_MAX] = {
329                 [BQ27XXX_REG_CTRL] = 0x02,
330                 [BQ27XXX_REG_TEMP] = 0x0a,
331                 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
332                 [BQ27XXX_REG_VOLT] = 0x0c,
333                 [BQ27XXX_REG_AI] = 0x0e,
334                 [BQ27XXX_REG_FLAGS] = 0x08,
335                 [BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
336                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
337                 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
338                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
339                 [BQ27XXX_REG_NAC] = INVALID_REG_ADDR,
340                 [BQ27XXX_REG_FCC] = INVALID_REG_ADDR,
341                 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
342                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
343                 [BQ27XXX_REG_SOC] = INVALID_REG_ADDR,
344                 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
345                 [BQ27XXX_REG_AP] = INVALID_REG_ADDR,
346                 [BQ27XXX_DM_CTRL] = INVALID_REG_ADDR,
347                 [BQ27XXX_DM_CLASS] = INVALID_REG_ADDR,
348                 [BQ27XXX_DM_BLOCK] = INVALID_REG_ADDR,
349                 [BQ27XXX_DM_DATA] = INVALID_REG_ADDR,
350                 [BQ27XXX_DM_CKSUM] = INVALID_REG_ADDR,
351         },
352         bq27530_regs[BQ27XXX_REG_MAX] = {
353                 [BQ27XXX_REG_CTRL] = 0x00,
354                 [BQ27XXX_REG_TEMP] = 0x06,
355                 [BQ27XXX_REG_INT_TEMP] = 0x32,
356                 [BQ27XXX_REG_VOLT] = 0x08,
357                 [BQ27XXX_REG_AI] = 0x14,
358                 [BQ27XXX_REG_FLAGS] = 0x0a,
359                 [BQ27XXX_REG_TTE] = 0x16,
360                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
361                 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
362                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
363                 [BQ27XXX_REG_NAC] = 0x0c,
364                 [BQ27XXX_REG_FCC] = 0x12,
365                 [BQ27XXX_REG_CYCT] = 0x2a,
366                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
367                 [BQ27XXX_REG_SOC] = 0x2c,
368                 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
369                 [BQ27XXX_REG_AP] = 0x24,
370                 BQ27XXX_DM_REG_ROWS,
371         },
372 #define bq27531_regs bq27530_regs
373         bq27541_regs[BQ27XXX_REG_MAX] = {
374                 [BQ27XXX_REG_CTRL] = 0x00,
375                 [BQ27XXX_REG_TEMP] = 0x06,
376                 [BQ27XXX_REG_INT_TEMP] = 0x28,
377                 [BQ27XXX_REG_VOLT] = 0x08,
378                 [BQ27XXX_REG_AI] = 0x14,
379                 [BQ27XXX_REG_FLAGS] = 0x0a,
380                 [BQ27XXX_REG_TTE] = 0x16,
381                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
382                 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
383                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
384                 [BQ27XXX_REG_NAC] = 0x0c,
385                 [BQ27XXX_REG_FCC] = 0x12,
386                 [BQ27XXX_REG_CYCT] = 0x2a,
387                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
388                 [BQ27XXX_REG_SOC] = 0x2c,
389                 [BQ27XXX_REG_DCAP] = 0x3c,
390                 [BQ27XXX_REG_AP] = 0x24,
391                 BQ27XXX_DM_REG_ROWS,
392         },
393 #define bq27542_regs bq27541_regs
394 #define bq27546_regs bq27541_regs
395 #define bq27742_regs bq27541_regs
396         bq27545_regs[BQ27XXX_REG_MAX] = {
397                 [BQ27XXX_REG_CTRL] = 0x00,
398                 [BQ27XXX_REG_TEMP] = 0x06,
399                 [BQ27XXX_REG_INT_TEMP] = 0x28,
400                 [BQ27XXX_REG_VOLT] = 0x08,
401                 [BQ27XXX_REG_AI] = 0x14,
402                 [BQ27XXX_REG_FLAGS] = 0x0a,
403                 [BQ27XXX_REG_TTE] = 0x16,
404                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
405                 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
406                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
407                 [BQ27XXX_REG_NAC] = 0x0c,
408                 [BQ27XXX_REG_FCC] = 0x12,
409                 [BQ27XXX_REG_CYCT] = 0x2a,
410                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
411                 [BQ27XXX_REG_SOC] = 0x2c,
412                 [BQ27XXX_REG_DCAP] = INVALID_REG_ADDR,
413                 [BQ27XXX_REG_AP] = 0x24,
414                 BQ27XXX_DM_REG_ROWS,
415         },
416         bq27421_regs[BQ27XXX_REG_MAX] = {
417                 [BQ27XXX_REG_CTRL] = 0x00,
418                 [BQ27XXX_REG_TEMP] = 0x02,
419                 [BQ27XXX_REG_INT_TEMP] = 0x1e,
420                 [BQ27XXX_REG_VOLT] = 0x04,
421                 [BQ27XXX_REG_AI] = 0x10,
422                 [BQ27XXX_REG_FLAGS] = 0x06,
423                 [BQ27XXX_REG_TTE] = INVALID_REG_ADDR,
424                 [BQ27XXX_REG_TTF] = INVALID_REG_ADDR,
425                 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
426                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
427                 [BQ27XXX_REG_NAC] = 0x08,
428                 [BQ27XXX_REG_FCC] = 0x0e,
429                 [BQ27XXX_REG_CYCT] = INVALID_REG_ADDR,
430                 [BQ27XXX_REG_AE] = INVALID_REG_ADDR,
431                 [BQ27XXX_REG_SOC] = 0x1c,
432                 [BQ27XXX_REG_DCAP] = 0x3c,
433                 [BQ27XXX_REG_AP] = 0x18,
434                 BQ27XXX_DM_REG_ROWS,
435         },
436 #define bq27411_regs bq27421_regs
437 #define bq27425_regs bq27421_regs
438 #define bq27426_regs bq27421_regs
439 #define bq27441_regs bq27421_regs
440 #define bq27621_regs bq27421_regs
441         bq27z561_regs[BQ27XXX_REG_MAX] = {
442                 [BQ27XXX_REG_CTRL] = 0x00,
443                 [BQ27XXX_REG_TEMP] = 0x06,
444                 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
445                 [BQ27XXX_REG_VOLT] = 0x08,
446                 [BQ27XXX_REG_AI] = 0x14,
447                 [BQ27XXX_REG_FLAGS] = 0x0a,
448                 [BQ27XXX_REG_TTE] = 0x16,
449                 [BQ27XXX_REG_TTF] = 0x18,
450                 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
451                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
452                 [BQ27XXX_REG_NAC] = INVALID_REG_ADDR,
453                 [BQ27XXX_REG_FCC] = 0x12,
454                 [BQ27XXX_REG_CYCT] = 0x2a,
455                 [BQ27XXX_REG_AE] = 0x22,
456                 [BQ27XXX_REG_SOC] = 0x2c,
457                 [BQ27XXX_REG_DCAP] = 0x3c,
458                 [BQ27XXX_REG_AP] = 0x22,
459                 BQ27XXX_DM_REG_ROWS,
460         },
461         bq28z610_regs[BQ27XXX_REG_MAX] = {
462                 [BQ27XXX_REG_CTRL] = 0x00,
463                 [BQ27XXX_REG_TEMP] = 0x06,
464                 [BQ27XXX_REG_INT_TEMP] = INVALID_REG_ADDR,
465                 [BQ27XXX_REG_VOLT] = 0x08,
466                 [BQ27XXX_REG_AI] = 0x14,
467                 [BQ27XXX_REG_FLAGS] = 0x0a,
468                 [BQ27XXX_REG_TTE] = 0x16,
469                 [BQ27XXX_REG_TTF] = 0x18,
470                 [BQ27XXX_REG_TTES] = INVALID_REG_ADDR,
471                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
472                 [BQ27XXX_REG_NAC] = INVALID_REG_ADDR,
473                 [BQ27XXX_REG_FCC] = 0x12,
474                 [BQ27XXX_REG_CYCT] = 0x2a,
475                 [BQ27XXX_REG_AE] = 0x22,
476                 [BQ27XXX_REG_SOC] = 0x2c,
477                 [BQ27XXX_REG_DCAP] = 0x3c,
478                 [BQ27XXX_REG_AP] = 0x22,
479                 BQ27XXX_DM_REG_ROWS,
480         },
481         bq34z100_regs[BQ27XXX_REG_MAX] = {
482                 [BQ27XXX_REG_CTRL] = 0x00,
483                 [BQ27XXX_REG_TEMP] = 0x0c,
484                 [BQ27XXX_REG_INT_TEMP] = 0x2a,
485                 [BQ27XXX_REG_VOLT] = 0x08,
486                 [BQ27XXX_REG_AI] = 0x0a,
487                 [BQ27XXX_REG_FLAGS] = 0x0e,
488                 [BQ27XXX_REG_TTE] = 0x18,
489                 [BQ27XXX_REG_TTF] = 0x1a,
490                 [BQ27XXX_REG_TTES] = 0x1e,
491                 [BQ27XXX_REG_TTECP] = INVALID_REG_ADDR,
492                 [BQ27XXX_REG_NAC] = INVALID_REG_ADDR,
493                 [BQ27XXX_REG_FCC] = 0x06,
494                 [BQ27XXX_REG_CYCT] = 0x2c,
495                 [BQ27XXX_REG_AE] = 0x24,
496                 [BQ27XXX_REG_SOC] = 0x02,
497                 [BQ27XXX_REG_DCAP] = 0x3c,
498                 [BQ27XXX_REG_AP] = 0x22,
499                 BQ27XXX_DM_REG_ROWS,
500         };
501
502 static enum power_supply_property bq27000_props[] = {
503         POWER_SUPPLY_PROP_STATUS,
504         POWER_SUPPLY_PROP_PRESENT,
505         POWER_SUPPLY_PROP_VOLTAGE_NOW,
506         POWER_SUPPLY_PROP_CURRENT_NOW,
507         POWER_SUPPLY_PROP_CAPACITY,
508         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
509         POWER_SUPPLY_PROP_TEMP,
510         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
511         POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
512         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
513         POWER_SUPPLY_PROP_TECHNOLOGY,
514         POWER_SUPPLY_PROP_CHARGE_FULL,
515         POWER_SUPPLY_PROP_CHARGE_NOW,
516         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
517         POWER_SUPPLY_PROP_CYCLE_COUNT,
518         POWER_SUPPLY_PROP_ENERGY_NOW,
519         POWER_SUPPLY_PROP_POWER_AVG,
520         POWER_SUPPLY_PROP_HEALTH,
521         POWER_SUPPLY_PROP_MANUFACTURER,
522 };
523
524 static enum power_supply_property bq27010_props[] = {
525         POWER_SUPPLY_PROP_STATUS,
526         POWER_SUPPLY_PROP_PRESENT,
527         POWER_SUPPLY_PROP_VOLTAGE_NOW,
528         POWER_SUPPLY_PROP_CURRENT_NOW,
529         POWER_SUPPLY_PROP_CAPACITY,
530         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
531         POWER_SUPPLY_PROP_TEMP,
532         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
533         POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
534         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
535         POWER_SUPPLY_PROP_TECHNOLOGY,
536         POWER_SUPPLY_PROP_CHARGE_FULL,
537         POWER_SUPPLY_PROP_CHARGE_NOW,
538         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
539         POWER_SUPPLY_PROP_CYCLE_COUNT,
540         POWER_SUPPLY_PROP_HEALTH,
541         POWER_SUPPLY_PROP_MANUFACTURER,
542 };
543
544 #define bq2750x_props bq27510g3_props
545 #define bq2751x_props bq27510g3_props
546 #define bq2752x_props bq27510g3_props
547
548 static enum power_supply_property bq27500_props[] = {
549         POWER_SUPPLY_PROP_STATUS,
550         POWER_SUPPLY_PROP_PRESENT,
551         POWER_SUPPLY_PROP_VOLTAGE_NOW,
552         POWER_SUPPLY_PROP_CURRENT_NOW,
553         POWER_SUPPLY_PROP_CAPACITY,
554         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
555         POWER_SUPPLY_PROP_TEMP,
556         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
557         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
558         POWER_SUPPLY_PROP_TECHNOLOGY,
559         POWER_SUPPLY_PROP_CHARGE_FULL,
560         POWER_SUPPLY_PROP_CHARGE_NOW,
561         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
562         POWER_SUPPLY_PROP_CYCLE_COUNT,
563         POWER_SUPPLY_PROP_ENERGY_NOW,
564         POWER_SUPPLY_PROP_POWER_AVG,
565         POWER_SUPPLY_PROP_HEALTH,
566         POWER_SUPPLY_PROP_MANUFACTURER,
567 };
568 #define bq27510g1_props bq27500_props
569 #define bq27510g2_props bq27500_props
570
571 static enum power_supply_property bq27510g3_props[] = {
572         POWER_SUPPLY_PROP_STATUS,
573         POWER_SUPPLY_PROP_PRESENT,
574         POWER_SUPPLY_PROP_VOLTAGE_NOW,
575         POWER_SUPPLY_PROP_CURRENT_NOW,
576         POWER_SUPPLY_PROP_CAPACITY,
577         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
578         POWER_SUPPLY_PROP_TEMP,
579         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
580         POWER_SUPPLY_PROP_TECHNOLOGY,
581         POWER_SUPPLY_PROP_CHARGE_FULL,
582         POWER_SUPPLY_PROP_CHARGE_NOW,
583         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
584         POWER_SUPPLY_PROP_CYCLE_COUNT,
585         POWER_SUPPLY_PROP_HEALTH,
586         POWER_SUPPLY_PROP_MANUFACTURER,
587 };
588
589 static enum power_supply_property bq27520g1_props[] = {
590         POWER_SUPPLY_PROP_STATUS,
591         POWER_SUPPLY_PROP_PRESENT,
592         POWER_SUPPLY_PROP_VOLTAGE_NOW,
593         POWER_SUPPLY_PROP_CURRENT_NOW,
594         POWER_SUPPLY_PROP_CAPACITY,
595         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
596         POWER_SUPPLY_PROP_TEMP,
597         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
598         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
599         POWER_SUPPLY_PROP_TECHNOLOGY,
600         POWER_SUPPLY_PROP_CHARGE_FULL,
601         POWER_SUPPLY_PROP_CHARGE_NOW,
602         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
603         POWER_SUPPLY_PROP_ENERGY_NOW,
604         POWER_SUPPLY_PROP_POWER_AVG,
605         POWER_SUPPLY_PROP_HEALTH,
606         POWER_SUPPLY_PROP_MANUFACTURER,
607 };
608
609 #define bq27520g2_props bq27500_props
610
611 static enum power_supply_property bq27520g3_props[] = {
612         POWER_SUPPLY_PROP_STATUS,
613         POWER_SUPPLY_PROP_PRESENT,
614         POWER_SUPPLY_PROP_VOLTAGE_NOW,
615         POWER_SUPPLY_PROP_CURRENT_NOW,
616         POWER_SUPPLY_PROP_CAPACITY,
617         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
618         POWER_SUPPLY_PROP_TEMP,
619         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
620         POWER_SUPPLY_PROP_TECHNOLOGY,
621         POWER_SUPPLY_PROP_CHARGE_FULL,
622         POWER_SUPPLY_PROP_CHARGE_NOW,
623         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
624         POWER_SUPPLY_PROP_CYCLE_COUNT,
625         POWER_SUPPLY_PROP_ENERGY_NOW,
626         POWER_SUPPLY_PROP_POWER_AVG,
627         POWER_SUPPLY_PROP_HEALTH,
628         POWER_SUPPLY_PROP_MANUFACTURER,
629 };
630
631 static enum power_supply_property bq27520g4_props[] = {
632         POWER_SUPPLY_PROP_STATUS,
633         POWER_SUPPLY_PROP_PRESENT,
634         POWER_SUPPLY_PROP_VOLTAGE_NOW,
635         POWER_SUPPLY_PROP_CURRENT_NOW,
636         POWER_SUPPLY_PROP_CAPACITY,
637         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
638         POWER_SUPPLY_PROP_TEMP,
639         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
640         POWER_SUPPLY_PROP_TECHNOLOGY,
641         POWER_SUPPLY_PROP_CHARGE_FULL,
642         POWER_SUPPLY_PROP_CHARGE_NOW,
643         POWER_SUPPLY_PROP_CYCLE_COUNT,
644         POWER_SUPPLY_PROP_HEALTH,
645         POWER_SUPPLY_PROP_MANUFACTURER,
646 };
647
648 static enum power_supply_property bq27521_props[] = {
649         POWER_SUPPLY_PROP_STATUS,
650         POWER_SUPPLY_PROP_PRESENT,
651         POWER_SUPPLY_PROP_VOLTAGE_NOW,
652         POWER_SUPPLY_PROP_CURRENT_NOW,
653         POWER_SUPPLY_PROP_TEMP,
654         POWER_SUPPLY_PROP_TECHNOLOGY,
655 };
656
657 static enum power_supply_property bq27530_props[] = {
658         POWER_SUPPLY_PROP_STATUS,
659         POWER_SUPPLY_PROP_PRESENT,
660         POWER_SUPPLY_PROP_VOLTAGE_NOW,
661         POWER_SUPPLY_PROP_CURRENT_NOW,
662         POWER_SUPPLY_PROP_CAPACITY,
663         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
664         POWER_SUPPLY_PROP_TEMP,
665         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
666         POWER_SUPPLY_PROP_TECHNOLOGY,
667         POWER_SUPPLY_PROP_CHARGE_FULL,
668         POWER_SUPPLY_PROP_CHARGE_NOW,
669         POWER_SUPPLY_PROP_POWER_AVG,
670         POWER_SUPPLY_PROP_HEALTH,
671         POWER_SUPPLY_PROP_CYCLE_COUNT,
672         POWER_SUPPLY_PROP_MANUFACTURER,
673 };
674 #define bq27531_props bq27530_props
675
676 static enum power_supply_property bq27541_props[] = {
677         POWER_SUPPLY_PROP_STATUS,
678         POWER_SUPPLY_PROP_PRESENT,
679         POWER_SUPPLY_PROP_VOLTAGE_NOW,
680         POWER_SUPPLY_PROP_CURRENT_NOW,
681         POWER_SUPPLY_PROP_CAPACITY,
682         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
683         POWER_SUPPLY_PROP_TEMP,
684         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
685         POWER_SUPPLY_PROP_TECHNOLOGY,
686         POWER_SUPPLY_PROP_CHARGE_FULL,
687         POWER_SUPPLY_PROP_CHARGE_NOW,
688         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
689         POWER_SUPPLY_PROP_CYCLE_COUNT,
690         POWER_SUPPLY_PROP_POWER_AVG,
691         POWER_SUPPLY_PROP_HEALTH,
692         POWER_SUPPLY_PROP_MANUFACTURER,
693 };
694 #define bq27542_props bq27541_props
695 #define bq27546_props bq27541_props
696 #define bq27742_props bq27541_props
697
698 static enum power_supply_property bq27545_props[] = {
699         POWER_SUPPLY_PROP_STATUS,
700         POWER_SUPPLY_PROP_PRESENT,
701         POWER_SUPPLY_PROP_VOLTAGE_NOW,
702         POWER_SUPPLY_PROP_CURRENT_NOW,
703         POWER_SUPPLY_PROP_CAPACITY,
704         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
705         POWER_SUPPLY_PROP_TEMP,
706         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
707         POWER_SUPPLY_PROP_TECHNOLOGY,
708         POWER_SUPPLY_PROP_CHARGE_FULL,
709         POWER_SUPPLY_PROP_CHARGE_NOW,
710         POWER_SUPPLY_PROP_HEALTH,
711         POWER_SUPPLY_PROP_CYCLE_COUNT,
712         POWER_SUPPLY_PROP_POWER_AVG,
713         POWER_SUPPLY_PROP_MANUFACTURER,
714 };
715
716 static enum power_supply_property bq27421_props[] = {
717         POWER_SUPPLY_PROP_STATUS,
718         POWER_SUPPLY_PROP_PRESENT,
719         POWER_SUPPLY_PROP_VOLTAGE_NOW,
720         POWER_SUPPLY_PROP_CURRENT_NOW,
721         POWER_SUPPLY_PROP_CAPACITY,
722         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
723         POWER_SUPPLY_PROP_TEMP,
724         POWER_SUPPLY_PROP_TECHNOLOGY,
725         POWER_SUPPLY_PROP_CHARGE_FULL,
726         POWER_SUPPLY_PROP_CHARGE_NOW,
727         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
728         POWER_SUPPLY_PROP_MANUFACTURER,
729 };
730 #define bq27411_props bq27421_props
731 #define bq27425_props bq27421_props
732 #define bq27426_props bq27421_props
733 #define bq27441_props bq27421_props
734 #define bq27621_props bq27421_props
735
736 static enum power_supply_property bq27z561_props[] = {
737         POWER_SUPPLY_PROP_STATUS,
738         POWER_SUPPLY_PROP_PRESENT,
739         POWER_SUPPLY_PROP_VOLTAGE_NOW,
740         POWER_SUPPLY_PROP_CURRENT_NOW,
741         POWER_SUPPLY_PROP_CAPACITY,
742         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
743         POWER_SUPPLY_PROP_TEMP,
744         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
745         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
746         POWER_SUPPLY_PROP_TECHNOLOGY,
747         POWER_SUPPLY_PROP_CHARGE_FULL,
748         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
749         POWER_SUPPLY_PROP_CYCLE_COUNT,
750         POWER_SUPPLY_PROP_POWER_AVG,
751         POWER_SUPPLY_PROP_HEALTH,
752         POWER_SUPPLY_PROP_MANUFACTURER,
753 };
754
755 static enum power_supply_property bq28z610_props[] = {
756         POWER_SUPPLY_PROP_STATUS,
757         POWER_SUPPLY_PROP_PRESENT,
758         POWER_SUPPLY_PROP_VOLTAGE_NOW,
759         POWER_SUPPLY_PROP_CURRENT_NOW,
760         POWER_SUPPLY_PROP_CAPACITY,
761         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
762         POWER_SUPPLY_PROP_TEMP,
763         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
764         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
765         POWER_SUPPLY_PROP_TECHNOLOGY,
766         POWER_SUPPLY_PROP_CHARGE_FULL,
767         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
768         POWER_SUPPLY_PROP_CYCLE_COUNT,
769         POWER_SUPPLY_PROP_POWER_AVG,
770         POWER_SUPPLY_PROP_HEALTH,
771         POWER_SUPPLY_PROP_MANUFACTURER,
772 };
773
774 static enum power_supply_property bq34z100_props[] = {
775         POWER_SUPPLY_PROP_STATUS,
776         POWER_SUPPLY_PROP_PRESENT,
777         POWER_SUPPLY_PROP_VOLTAGE_NOW,
778         POWER_SUPPLY_PROP_CURRENT_NOW,
779         POWER_SUPPLY_PROP_CAPACITY,
780         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
781         POWER_SUPPLY_PROP_TEMP,
782         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
783         POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
784         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
785         POWER_SUPPLY_PROP_TECHNOLOGY,
786         POWER_SUPPLY_PROP_CHARGE_FULL,
787         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
788         POWER_SUPPLY_PROP_CYCLE_COUNT,
789         POWER_SUPPLY_PROP_ENERGY_NOW,
790         POWER_SUPPLY_PROP_POWER_AVG,
791         POWER_SUPPLY_PROP_HEALTH,
792         POWER_SUPPLY_PROP_MANUFACTURER,
793 };
794
795 struct bq27xxx_dm_reg {
796         u8 subclass_id;
797         u8 offset;
798         u8 bytes;
799         u16 min, max;
800 };
801
802 enum bq27xxx_dm_reg_id {
803         BQ27XXX_DM_DESIGN_CAPACITY = 0,
804         BQ27XXX_DM_DESIGN_ENERGY,
805         BQ27XXX_DM_TERMINATE_VOLTAGE,
806 };
807
808 #define bq27000_dm_regs 0
809 #define bq27010_dm_regs 0
810 #define bq2750x_dm_regs 0
811 #define bq2751x_dm_regs 0
812 #define bq2752x_dm_regs 0
813
814 #if 0 /* not yet tested */
815 static struct bq27xxx_dm_reg bq27500_dm_regs[] = {
816         [BQ27XXX_DM_DESIGN_CAPACITY]   = { 48, 10, 2,    0, 65535 },
817         [BQ27XXX_DM_DESIGN_ENERGY]     = { }, /* missing on chip */
818         [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 48, 2, 1000, 32767 },
819 };
820 #else
821 #define bq27500_dm_regs 0
822 #endif
823
824 /* todo create data memory definitions from datasheets and test on chips */
825 #define bq27510g1_dm_regs 0
826 #define bq27510g2_dm_regs 0
827 #define bq27510g3_dm_regs 0
828 #define bq27520g1_dm_regs 0
829 #define bq27520g2_dm_regs 0
830 #define bq27520g3_dm_regs 0
831 #define bq27520g4_dm_regs 0
832 #define bq27521_dm_regs 0
833 #define bq27530_dm_regs 0
834 #define bq27531_dm_regs 0
835 #define bq27541_dm_regs 0
836 #define bq27542_dm_regs 0
837 #define bq27546_dm_regs 0
838 #define bq27742_dm_regs 0
839
840 #if 0 /* not yet tested */
841 static struct bq27xxx_dm_reg bq27545_dm_regs[] = {
842         [BQ27XXX_DM_DESIGN_CAPACITY]   = { 48, 23, 2,    0, 32767 },
843         [BQ27XXX_DM_DESIGN_ENERGY]     = { 48, 25, 2,    0, 32767 },
844         [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 67, 2, 2800,  3700 },
845 };
846 #else
847 #define bq27545_dm_regs 0
848 #endif
849
850 static struct bq27xxx_dm_reg bq27411_dm_regs[] = {
851         [BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 10, 2,    0, 32767 },
852         [BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 12, 2,    0, 32767 },
853         [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 16, 2, 2800,  3700 },
854 };
855
856 static struct bq27xxx_dm_reg bq27421_dm_regs[] = {
857         [BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 10, 2,    0,  8000 },
858         [BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 12, 2,    0, 32767 },
859         [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 16, 2, 2500,  3700 },
860 };
861
862 static struct bq27xxx_dm_reg bq27425_dm_regs[] = {
863         [BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 12, 2,    0, 32767 },
864         [BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 14, 2,    0, 32767 },
865         [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 18, 2, 2800,  3700 },
866 };
867
868 static struct bq27xxx_dm_reg bq27426_dm_regs[] = {
869         [BQ27XXX_DM_DESIGN_CAPACITY]   = { 82,  6, 2,    0,  8000 },
870         [BQ27XXX_DM_DESIGN_ENERGY]     = { 82,  8, 2,    0, 32767 },
871         [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 10, 2, 2500,  3700 },
872 };
873
874 #if 0 /* not yet tested */
875 #define bq27441_dm_regs bq27421_dm_regs
876 #else
877 #define bq27441_dm_regs 0
878 #endif
879
880 #if 0 /* not yet tested */
881 static struct bq27xxx_dm_reg bq27621_dm_regs[] = {
882         [BQ27XXX_DM_DESIGN_CAPACITY]   = { 82, 3, 2,    0,  8000 },
883         [BQ27XXX_DM_DESIGN_ENERGY]     = { 82, 5, 2,    0, 32767 },
884         [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 9, 2, 2500,  3700 },
885 };
886 #else
887 #define bq27621_dm_regs 0
888 #endif
889
890 #define bq27z561_dm_regs 0
891 #define bq28z610_dm_regs 0
892 #define bq34z100_dm_regs 0
893
894 #define BQ27XXX_O_ZERO          BIT(0)
895 #define BQ27XXX_O_OTDC          BIT(1) /* has OTC/OTD overtemperature flags */
896 #define BQ27XXX_O_UTOT          BIT(2) /* has OT overtemperature flag */
897 #define BQ27XXX_O_CFGUP         BIT(3)
898 #define BQ27XXX_O_RAM           BIT(4)
899 #define BQ27Z561_O_BITS         BIT(5)
900 #define BQ27XXX_O_SOC_SI        BIT(6) /* SoC is single register */
901 #define BQ27XXX_O_HAS_CI        BIT(7) /* has Capacity Inaccurate flag */
902 #define BQ27XXX_O_MUL_CHEM      BIT(8) /* multiple chemistries supported */
903
904 #define BQ27XXX_DATA(ref, key, opt) {           \
905         .opts = (opt),                          \
906         .unseal_key = key,                      \
907         .regs  = ref##_regs,                    \
908         .dm_regs = ref##_dm_regs,               \
909         .props = ref##_props,                   \
910         .props_size = ARRAY_SIZE(ref##_props) }
911
912 static struct {
913         u32 opts;
914         u32 unseal_key;
915         u8 *regs;
916         struct bq27xxx_dm_reg *dm_regs;
917         enum power_supply_property *props;
918         size_t props_size;
919 } bq27xxx_chip_data[] = {
920         [BQ27000]   = BQ27XXX_DATA(bq27000,   0         , BQ27XXX_O_ZERO | BQ27XXX_O_SOC_SI | BQ27XXX_O_HAS_CI),
921         [BQ27010]   = BQ27XXX_DATA(bq27010,   0         , BQ27XXX_O_ZERO | BQ27XXX_O_SOC_SI | BQ27XXX_O_HAS_CI),
922         [BQ2750X]   = BQ27XXX_DATA(bq2750x,   0         , BQ27XXX_O_OTDC),
923         [BQ2751X]   = BQ27XXX_DATA(bq2751x,   0         , BQ27XXX_O_OTDC),
924         [BQ2752X]   = BQ27XXX_DATA(bq2752x,   0         , BQ27XXX_O_OTDC),
925         [BQ27500]   = BQ27XXX_DATA(bq27500,   0x04143672, BQ27XXX_O_OTDC),
926         [BQ27510G1] = BQ27XXX_DATA(bq27510g1, 0         , BQ27XXX_O_OTDC),
927         [BQ27510G2] = BQ27XXX_DATA(bq27510g2, 0         , BQ27XXX_O_OTDC),
928         [BQ27510G3] = BQ27XXX_DATA(bq27510g3, 0         , BQ27XXX_O_OTDC),
929         [BQ27520G1] = BQ27XXX_DATA(bq27520g1, 0         , BQ27XXX_O_OTDC),
930         [BQ27520G2] = BQ27XXX_DATA(bq27520g2, 0         , BQ27XXX_O_OTDC),
931         [BQ27520G3] = BQ27XXX_DATA(bq27520g3, 0         , BQ27XXX_O_OTDC),
932         [BQ27520G4] = BQ27XXX_DATA(bq27520g4, 0         , BQ27XXX_O_OTDC),
933         [BQ27521]   = BQ27XXX_DATA(bq27521,   0         , 0),
934         [BQ27530]   = BQ27XXX_DATA(bq27530,   0         , BQ27XXX_O_UTOT),
935         [BQ27531]   = BQ27XXX_DATA(bq27531,   0         , BQ27XXX_O_UTOT),
936         [BQ27541]   = BQ27XXX_DATA(bq27541,   0         , BQ27XXX_O_OTDC),
937         [BQ27542]   = BQ27XXX_DATA(bq27542,   0         , BQ27XXX_O_OTDC),
938         [BQ27546]   = BQ27XXX_DATA(bq27546,   0         , BQ27XXX_O_OTDC),
939         [BQ27742]   = BQ27XXX_DATA(bq27742,   0         , BQ27XXX_O_OTDC),
940         [BQ27545]   = BQ27XXX_DATA(bq27545,   0x04143672, BQ27XXX_O_OTDC),
941         [BQ27411]   = BQ27XXX_DATA(bq27411,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
942         [BQ27421]   = BQ27XXX_DATA(bq27421,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
943         [BQ27425]   = BQ27XXX_DATA(bq27425,   0x04143672, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP),
944         [BQ27426]   = BQ27XXX_DATA(bq27426,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
945         [BQ27441]   = BQ27XXX_DATA(bq27441,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
946         [BQ27621]   = BQ27XXX_DATA(bq27621,   0x80008000, BQ27XXX_O_UTOT | BQ27XXX_O_CFGUP | BQ27XXX_O_RAM),
947         [BQ27Z561]  = BQ27XXX_DATA(bq27z561,  0         , BQ27Z561_O_BITS),
948         [BQ28Z610]  = BQ27XXX_DATA(bq28z610,  0         , BQ27Z561_O_BITS),
949         [BQ34Z100]  = BQ27XXX_DATA(bq34z100,  0         , BQ27XXX_O_OTDC | BQ27XXX_O_SOC_SI | \
950                                                           BQ27XXX_O_HAS_CI | BQ27XXX_O_MUL_CHEM),
951 };
952
953 static DEFINE_MUTEX(bq27xxx_list_lock);
954 static LIST_HEAD(bq27xxx_battery_devices);
955
956 #define BQ27XXX_MSLEEP(i) usleep_range((i)*1000, (i)*1000+500)
957
958 #define BQ27XXX_DM_SZ   32
959
960 /**
961  * struct bq27xxx_dm_buf - chip data memory buffer
962  * @class: data memory subclass_id
963  * @block: data memory block number
964  * @data: data from/for the block
965  * @has_data: true if data has been filled by read
966  * @dirty: true if data has changed since last read/write
967  *
968  * Encapsulates info required to manage chip data memory blocks.
969  */
970 struct bq27xxx_dm_buf {
971         u8 class;
972         u8 block;
973         u8 data[BQ27XXX_DM_SZ];
974         bool has_data, dirty;
975 };
976
977 #define BQ27XXX_DM_BUF(di, i) { \
978         .class = (di)->dm_regs[i].subclass_id, \
979         .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \
980 }
981
982 static inline u16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf,
983                                       struct bq27xxx_dm_reg *reg)
984 {
985         if (buf->class == reg->subclass_id &&
986             buf->block == reg->offset / BQ27XXX_DM_SZ)
987                 return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ);
988
989         return NULL;
990 }
991
992 static const char * const bq27xxx_dm_reg_name[] = {
993         [BQ27XXX_DM_DESIGN_CAPACITY] = "design-capacity",
994         [BQ27XXX_DM_DESIGN_ENERGY] = "design-energy",
995         [BQ27XXX_DM_TERMINATE_VOLTAGE] = "terminate-voltage",
996 };
997
998
999 static bool bq27xxx_dt_to_nvm = true;
1000 module_param_named(dt_monitored_battery_updates_nvm, bq27xxx_dt_to_nvm, bool, 0444);
1001 MODULE_PARM_DESC(dt_monitored_battery_updates_nvm,
1002         "Devicetree monitored-battery config updates data memory on NVM/flash chips.\n"
1003         "Users must set this =0 when installing a different type of battery!\n"
1004         "Default is =1."
1005 #ifndef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
1006         "\nSetting this affects future kernel updates, not the current configuration."
1007 #endif
1008 );
1009
1010 static int poll_interval_param_set(const char *val, const struct kernel_param *kp)
1011 {
1012         struct bq27xxx_device_info *di;
1013         unsigned int prev_val = *(unsigned int *) kp->arg;
1014         int ret;
1015
1016         ret = param_set_uint(val, kp);
1017         if (ret < 0 || prev_val == *(unsigned int *) kp->arg)
1018                 return ret;
1019
1020         mutex_lock(&bq27xxx_list_lock);
1021         list_for_each_entry(di, &bq27xxx_battery_devices, list) {
1022                 cancel_delayed_work_sync(&di->work);
1023                 schedule_delayed_work(&di->work, 0);
1024         }
1025         mutex_unlock(&bq27xxx_list_lock);
1026
1027         return ret;
1028 }
1029
1030 static const struct kernel_param_ops param_ops_poll_interval = {
1031         .get = param_get_uint,
1032         .set = poll_interval_param_set,
1033 };
1034
1035 static unsigned int poll_interval = 360;
1036 module_param_cb(poll_interval, &param_ops_poll_interval, &poll_interval, 0644);
1037 MODULE_PARM_DESC(poll_interval,
1038                  "battery poll interval in seconds - 0 disables polling");
1039
1040 /*
1041  * Common code for BQ27xxx devices
1042  */
1043
1044 static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
1045                                bool single)
1046 {
1047         int ret;
1048
1049         if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
1050                 return -EINVAL;
1051
1052         ret = di->bus.read(di, di->regs[reg_index], single);
1053         if (ret < 0)
1054                 dev_dbg(di->dev, "failed to read register 0x%02x (index %d)\n",
1055                         di->regs[reg_index], reg_index);
1056
1057         return ret;
1058 }
1059
1060 static inline int bq27xxx_write(struct bq27xxx_device_info *di, int reg_index,
1061                                 u16 value, bool single)
1062 {
1063         int ret;
1064
1065         if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
1066                 return -EINVAL;
1067
1068         if (!di->bus.write)
1069                 return -EPERM;
1070
1071         ret = di->bus.write(di, di->regs[reg_index], value, single);
1072         if (ret < 0)
1073                 dev_dbg(di->dev, "failed to write register 0x%02x (index %d)\n",
1074                         di->regs[reg_index], reg_index);
1075
1076         return ret;
1077 }
1078
1079 static inline int bq27xxx_read_block(struct bq27xxx_device_info *di, int reg_index,
1080                                      u8 *data, int len)
1081 {
1082         int ret;
1083
1084         if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
1085                 return -EINVAL;
1086
1087         if (!di->bus.read_bulk)
1088                 return -EPERM;
1089
1090         ret = di->bus.read_bulk(di, di->regs[reg_index], data, len);
1091         if (ret < 0)
1092                 dev_dbg(di->dev, "failed to read_bulk register 0x%02x (index %d)\n",
1093                         di->regs[reg_index], reg_index);
1094
1095         return ret;
1096 }
1097
1098 static inline int bq27xxx_write_block(struct bq27xxx_device_info *di, int reg_index,
1099                                       u8 *data, int len)
1100 {
1101         int ret;
1102
1103         if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
1104                 return -EINVAL;
1105
1106         if (!di->bus.write_bulk)
1107                 return -EPERM;
1108
1109         ret = di->bus.write_bulk(di, di->regs[reg_index], data, len);
1110         if (ret < 0)
1111                 dev_dbg(di->dev, "failed to write_bulk register 0x%02x (index %d)\n",
1112                         di->regs[reg_index], reg_index);
1113
1114         return ret;
1115 }
1116
1117 static int bq27xxx_battery_seal(struct bq27xxx_device_info *di)
1118 {
1119         int ret;
1120
1121         ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_SEALED, false);
1122         if (ret < 0) {
1123                 dev_err(di->dev, "bus error on seal: %d\n", ret);
1124                 return ret;
1125         }
1126
1127         return 0;
1128 }
1129
1130 static int bq27xxx_battery_unseal(struct bq27xxx_device_info *di)
1131 {
1132         int ret;
1133
1134         if (di->unseal_key == 0) {
1135                 dev_err(di->dev, "unseal failed due to missing key\n");
1136                 return -EINVAL;
1137         }
1138
1139         ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)(di->unseal_key >> 16), false);
1140         if (ret < 0)
1141                 goto out;
1142
1143         ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, (u16)di->unseal_key, false);
1144         if (ret < 0)
1145                 goto out;
1146
1147         return 0;
1148
1149 out:
1150         dev_err(di->dev, "bus error on unseal: %d\n", ret);
1151         return ret;
1152 }
1153
1154 static u8 bq27xxx_battery_checksum_dm_block(struct bq27xxx_dm_buf *buf)
1155 {
1156         u16 sum = 0;
1157         int i;
1158
1159         for (i = 0; i < BQ27XXX_DM_SZ; i++)
1160                 sum += buf->data[i];
1161         sum &= 0xff;
1162
1163         return 0xff - sum;
1164 }
1165
1166 static int bq27xxx_battery_read_dm_block(struct bq27xxx_device_info *di,
1167                                          struct bq27xxx_dm_buf *buf)
1168 {
1169         int ret;
1170
1171         buf->has_data = false;
1172
1173         ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
1174         if (ret < 0)
1175                 goto out;
1176
1177         ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
1178         if (ret < 0)
1179                 goto out;
1180
1181         BQ27XXX_MSLEEP(1);
1182
1183         ret = bq27xxx_read_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
1184         if (ret < 0)
1185                 goto out;
1186
1187         ret = bq27xxx_read(di, BQ27XXX_DM_CKSUM, true);
1188         if (ret < 0)
1189                 goto out;
1190
1191         if ((u8)ret != bq27xxx_battery_checksum_dm_block(buf)) {
1192                 ret = -EINVAL;
1193                 goto out;
1194         }
1195
1196         buf->has_data = true;
1197         buf->dirty = false;
1198
1199         return 0;
1200
1201 out:
1202         dev_err(di->dev, "bus error reading chip memory: %d\n", ret);
1203         return ret;
1204 }
1205
1206 static void bq27xxx_battery_update_dm_block(struct bq27xxx_device_info *di,
1207                                             struct bq27xxx_dm_buf *buf,
1208                                             enum bq27xxx_dm_reg_id reg_id,
1209                                             unsigned int val)
1210 {
1211         struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id];
1212         const char *str = bq27xxx_dm_reg_name[reg_id];
1213         u16 *prev = bq27xxx_dm_reg_ptr(buf, reg);
1214
1215         if (prev == NULL) {
1216                 dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
1217                 return;
1218         }
1219
1220         if (reg->bytes != 2) {
1221                 dev_warn(di->dev, "%s dm spec has unsupported byte size\n", str);
1222                 return;
1223         }
1224
1225         if (!buf->has_data)
1226                 return;
1227
1228         if (be16_to_cpup(prev) == val) {
1229                 dev_info(di->dev, "%s has %u\n", str, val);
1230                 return;
1231         }
1232
1233 #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
1234         if (!(di->opts & BQ27XXX_O_RAM) && !bq27xxx_dt_to_nvm) {
1235 #else
1236         if (!(di->opts & BQ27XXX_O_RAM)) {
1237 #endif
1238                 /* devicetree and NVM differ; defer to NVM */
1239                 dev_warn(di->dev, "%s has %u; update to %u disallowed "
1240 #ifdef CONFIG_BATTERY_BQ27XXX_DT_UPDATES_NVM
1241                          "by dt_monitored_battery_updates_nvm=0"
1242 #else
1243                          "for flash/NVM data memory"
1244 #endif
1245                          "\n", str, be16_to_cpup(prev), val);
1246                 return;
1247         }
1248
1249         dev_info(di->dev, "update %s to %u\n", str, val);
1250
1251         *prev = cpu_to_be16(val);
1252         buf->dirty = true;
1253 }
1254
1255 static int bq27xxx_battery_cfgupdate_priv(struct bq27xxx_device_info *di, bool active)
1256 {
1257         const int limit = 100;
1258         u16 cmd = active ? BQ27XXX_SET_CFGUPDATE : BQ27XXX_SOFT_RESET;
1259         int ret, try = limit;
1260
1261         ret = bq27xxx_write(di, BQ27XXX_REG_CTRL, cmd, false);
1262         if (ret < 0)
1263                 return ret;
1264
1265         do {
1266                 BQ27XXX_MSLEEP(25);
1267                 ret = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
1268                 if (ret < 0)
1269                         return ret;
1270         } while (!!(ret & BQ27XXX_FLAG_CFGUP) != active && --try);
1271
1272         if (!try && di->chip != BQ27425) { // 425 has a bug
1273                 dev_err(di->dev, "timed out waiting for cfgupdate flag %d\n", active);
1274                 return -EINVAL;
1275         }
1276
1277         if (limit - try > 3)
1278                 dev_warn(di->dev, "cfgupdate %d, retries %d\n", active, limit - try);
1279
1280         return 0;
1281 }
1282
1283 static inline int bq27xxx_battery_set_cfgupdate(struct bq27xxx_device_info *di)
1284 {
1285         int ret = bq27xxx_battery_cfgupdate_priv(di, true);
1286         if (ret < 0 && ret != -EINVAL)
1287                 dev_err(di->dev, "bus error on set_cfgupdate: %d\n", ret);
1288
1289         return ret;
1290 }
1291
1292 static inline int bq27xxx_battery_soft_reset(struct bq27xxx_device_info *di)
1293 {
1294         int ret = bq27xxx_battery_cfgupdate_priv(di, false);
1295         if (ret < 0 && ret != -EINVAL)
1296                 dev_err(di->dev, "bus error on soft_reset: %d\n", ret);
1297
1298         return ret;
1299 }
1300
1301 static int bq27xxx_battery_write_dm_block(struct bq27xxx_device_info *di,
1302                                           struct bq27xxx_dm_buf *buf)
1303 {
1304         bool cfgup = di->opts & BQ27XXX_O_CFGUP;
1305         int ret;
1306
1307         if (!buf->dirty)
1308                 return 0;
1309
1310         if (cfgup) {
1311                 ret = bq27xxx_battery_set_cfgupdate(di);
1312                 if (ret < 0)
1313                         return ret;
1314         }
1315
1316         ret = bq27xxx_write(di, BQ27XXX_DM_CTRL, 0, true);
1317         if (ret < 0)
1318                 goto out;
1319
1320         ret = bq27xxx_write(di, BQ27XXX_DM_CLASS, buf->class, true);
1321         if (ret < 0)
1322                 goto out;
1323
1324         ret = bq27xxx_write(di, BQ27XXX_DM_BLOCK, buf->block, true);
1325         if (ret < 0)
1326                 goto out;
1327
1328         BQ27XXX_MSLEEP(1);
1329
1330         ret = bq27xxx_write_block(di, BQ27XXX_DM_DATA, buf->data, BQ27XXX_DM_SZ);
1331         if (ret < 0)
1332                 goto out;
1333
1334         ret = bq27xxx_write(di, BQ27XXX_DM_CKSUM,
1335                             bq27xxx_battery_checksum_dm_block(buf), true);
1336         if (ret < 0)
1337                 goto out;
1338
1339         /* DO NOT read BQ27XXX_DM_CKSUM here to verify it! That may cause NVM
1340          * corruption on the '425 chip (and perhaps others), which can damage
1341          * the chip.
1342          */
1343
1344         if (cfgup) {
1345                 BQ27XXX_MSLEEP(1);
1346                 ret = bq27xxx_battery_soft_reset(di);
1347                 if (ret < 0)
1348                         return ret;
1349         } else {
1350                 BQ27XXX_MSLEEP(100); /* flash DM updates in <100ms */
1351         }
1352
1353         buf->dirty = false;
1354
1355         return 0;
1356
1357 out:
1358         if (cfgup)
1359                 bq27xxx_battery_soft_reset(di);
1360
1361         dev_err(di->dev, "bus error writing chip memory: %d\n", ret);
1362         return ret;
1363 }
1364
1365 static void bq27xxx_battery_set_config(struct bq27xxx_device_info *di,
1366                                        struct power_supply_battery_info *info)
1367 {
1368         struct bq27xxx_dm_buf bd = BQ27XXX_DM_BUF(di, BQ27XXX_DM_DESIGN_CAPACITY);
1369         struct bq27xxx_dm_buf bt = BQ27XXX_DM_BUF(di, BQ27XXX_DM_TERMINATE_VOLTAGE);
1370         bool updated;
1371
1372         if (bq27xxx_battery_unseal(di) < 0)
1373                 return;
1374
1375         if (info->charge_full_design_uah != -EINVAL &&
1376             info->energy_full_design_uwh != -EINVAL) {
1377                 bq27xxx_battery_read_dm_block(di, &bd);
1378                 /* assume design energy & capacity are in same block */
1379                 bq27xxx_battery_update_dm_block(di, &bd,
1380                                         BQ27XXX_DM_DESIGN_CAPACITY,
1381                                         info->charge_full_design_uah / 1000);
1382                 bq27xxx_battery_update_dm_block(di, &bd,
1383                                         BQ27XXX_DM_DESIGN_ENERGY,
1384                                         info->energy_full_design_uwh / 1000);
1385         }
1386
1387         if (info->voltage_min_design_uv != -EINVAL) {
1388                 bool same = bd.class == bt.class && bd.block == bt.block;
1389                 if (!same)
1390                         bq27xxx_battery_read_dm_block(di, &bt);
1391                 bq27xxx_battery_update_dm_block(di, same ? &bd : &bt,
1392                                         BQ27XXX_DM_TERMINATE_VOLTAGE,
1393                                         info->voltage_min_design_uv / 1000);
1394         }
1395
1396         updated = bd.dirty || bt.dirty;
1397
1398         bq27xxx_battery_write_dm_block(di, &bd);
1399         bq27xxx_battery_write_dm_block(di, &bt);
1400
1401         bq27xxx_battery_seal(di);
1402
1403         if (updated && !(di->opts & BQ27XXX_O_CFGUP)) {
1404                 bq27xxx_write(di, BQ27XXX_REG_CTRL, BQ27XXX_RESET, false);
1405                 BQ27XXX_MSLEEP(300); /* reset time is not documented */
1406         }
1407         /* assume bq27xxx_battery_update() is called hereafter */
1408 }
1409
1410 static void bq27xxx_battery_settings(struct bq27xxx_device_info *di)
1411 {
1412         struct power_supply_battery_info info = {};
1413         unsigned int min, max;
1414
1415         if (power_supply_get_battery_info(di->bat, &info) < 0)
1416                 return;
1417
1418         if (!di->dm_regs) {
1419                 dev_warn(di->dev, "data memory update not supported for chip\n");
1420                 return;
1421         }
1422
1423         if (info.energy_full_design_uwh != info.charge_full_design_uah) {
1424                 if (info.energy_full_design_uwh == -EINVAL)
1425                         dev_warn(di->dev, "missing battery:energy-full-design-microwatt-hours\n");
1426                 else if (info.charge_full_design_uah == -EINVAL)
1427                         dev_warn(di->dev, "missing battery:charge-full-design-microamp-hours\n");
1428         }
1429
1430         /* assume min == 0 */
1431         max = di->dm_regs[BQ27XXX_DM_DESIGN_ENERGY].max;
1432         if (info.energy_full_design_uwh > max * 1000) {
1433                 dev_err(di->dev, "invalid battery:energy-full-design-microwatt-hours %d\n",
1434                         info.energy_full_design_uwh);
1435                 info.energy_full_design_uwh = -EINVAL;
1436         }
1437
1438         /* assume min == 0 */
1439         max = di->dm_regs[BQ27XXX_DM_DESIGN_CAPACITY].max;
1440         if (info.charge_full_design_uah > max * 1000) {
1441                 dev_err(di->dev, "invalid battery:charge-full-design-microamp-hours %d\n",
1442                         info.charge_full_design_uah);
1443                 info.charge_full_design_uah = -EINVAL;
1444         }
1445
1446         min = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].min;
1447         max = di->dm_regs[BQ27XXX_DM_TERMINATE_VOLTAGE].max;
1448         if ((info.voltage_min_design_uv < min * 1000 ||
1449              info.voltage_min_design_uv > max * 1000) &&
1450              info.voltage_min_design_uv != -EINVAL) {
1451                 dev_err(di->dev, "invalid battery:voltage-min-design-microvolt %d\n",
1452                         info.voltage_min_design_uv);
1453                 info.voltage_min_design_uv = -EINVAL;
1454         }
1455
1456         if ((info.energy_full_design_uwh != -EINVAL &&
1457              info.charge_full_design_uah != -EINVAL) ||
1458              info.voltage_min_design_uv  != -EINVAL)
1459                 bq27xxx_battery_set_config(di, &info);
1460 }
1461
1462 /*
1463  * Return the battery State-of-Charge
1464  * Or < 0 if something fails.
1465  */
1466 static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
1467 {
1468         int soc;
1469
1470         if (di->opts & BQ27XXX_O_SOC_SI)
1471                 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
1472         else
1473                 soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
1474
1475         if (soc < 0)
1476                 dev_dbg(di->dev, "error reading State-of-Charge\n");
1477
1478         return soc;
1479 }
1480
1481 /*
1482  * Return a battery charge value in µAh
1483  * Or < 0 if something fails.
1484  */
1485 static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
1486 {
1487         int charge;
1488
1489         charge = bq27xxx_read(di, reg, false);
1490         if (charge < 0) {
1491                 dev_dbg(di->dev, "error reading charge register %02x: %d\n",
1492                         reg, charge);
1493                 return charge;
1494         }
1495
1496         if (di->opts & BQ27XXX_O_ZERO)
1497                 charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1498         else
1499                 charge *= 1000;
1500
1501         return charge;
1502 }
1503
1504 /*
1505  * Return the battery Nominal available capacity in µAh
1506  * Or < 0 if something fails.
1507  */
1508 static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
1509 {
1510         int flags;
1511
1512         if (di->opts & BQ27XXX_O_ZERO) {
1513                 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
1514                 if (flags >= 0 && (flags & BQ27000_FLAG_CI))
1515                         return -ENODATA;
1516         }
1517
1518         return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
1519 }
1520
1521 /*
1522  * Return the battery Full Charge Capacity in µAh
1523  * Or < 0 if something fails.
1524  */
1525 static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
1526 {
1527         return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
1528 }
1529
1530 /*
1531  * Return the Design Capacity in µAh
1532  * Or < 0 if something fails.
1533  */
1534 static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
1535 {
1536         int dcap;
1537
1538         if (di->opts & BQ27XXX_O_ZERO)
1539                 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
1540         else
1541                 dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
1542
1543         if (dcap < 0) {
1544                 dev_dbg(di->dev, "error reading initial last measured discharge\n");
1545                 return dcap;
1546         }
1547
1548         if (di->opts & BQ27XXX_O_ZERO)
1549                 dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1550         else
1551                 dcap *= 1000;
1552
1553         return dcap;
1554 }
1555
1556 /*
1557  * Return the battery Available energy in µWh
1558  * Or < 0 if something fails.
1559  */
1560 static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
1561 {
1562         int ae;
1563
1564         ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
1565         if (ae < 0) {
1566                 dev_dbg(di->dev, "error reading available energy\n");
1567                 return ae;
1568         }
1569
1570         if (di->opts & BQ27XXX_O_ZERO)
1571                 ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
1572         else
1573                 ae *= 1000;
1574
1575         return ae;
1576 }
1577
1578 /*
1579  * Return the battery temperature in tenths of degree Kelvin
1580  * Or < 0 if something fails.
1581  */
1582 static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
1583 {
1584         int temp;
1585
1586         temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
1587         if (temp < 0) {
1588                 dev_err(di->dev, "error reading temperature\n");
1589                 return temp;
1590         }
1591
1592         if (di->opts & BQ27XXX_O_ZERO)
1593                 temp = 5 * temp / 2;
1594
1595         return temp;
1596 }
1597
1598 /*
1599  * Return the battery Cycle count total
1600  * Or < 0 if something fails.
1601  */
1602 static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
1603 {
1604         int cyct;
1605
1606         cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
1607         if (cyct < 0)
1608                 dev_err(di->dev, "error reading cycle count total\n");
1609
1610         return cyct;
1611 }
1612
1613 /*
1614  * Read a time register.
1615  * Return < 0 if something fails.
1616  */
1617 static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
1618 {
1619         int tval;
1620
1621         tval = bq27xxx_read(di, reg, false);
1622         if (tval < 0) {
1623                 dev_dbg(di->dev, "error reading time register %02x: %d\n",
1624                         reg, tval);
1625                 return tval;
1626         }
1627
1628         if (tval == 65535)
1629                 return -ENODATA;
1630
1631         return tval * 60;
1632 }
1633
1634 /*
1635  * Returns true if a battery over temperature condition is detected
1636  */
1637 static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
1638 {
1639         if (di->opts & BQ27XXX_O_OTDC)
1640                 return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
1641         if (di->opts & BQ27XXX_O_UTOT)
1642                 return flags & BQ27XXX_FLAG_OT;
1643
1644         return false;
1645 }
1646
1647 /*
1648  * Returns true if a battery under temperature condition is detected
1649  */
1650 static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
1651 {
1652         if (di->opts & BQ27XXX_O_UTOT)
1653                 return flags & BQ27XXX_FLAG_UT;
1654
1655         return false;
1656 }
1657
1658 /*
1659  * Returns true if a low state of charge condition is detected
1660  */
1661 static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
1662 {
1663         if (di->opts & BQ27XXX_O_ZERO)
1664                 return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
1665         else if (di->opts & BQ27Z561_O_BITS)
1666                 return flags & BQ27Z561_FLAG_FDC;
1667         else
1668                 return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
1669 }
1670
1671 static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
1672 {
1673         /* Unlikely but important to return first */
1674         if (unlikely(bq27xxx_battery_overtemp(di, di->cache.flags)))
1675                 return POWER_SUPPLY_HEALTH_OVERHEAT;
1676         if (unlikely(bq27xxx_battery_undertemp(di, di->cache.flags)))
1677                 return POWER_SUPPLY_HEALTH_COLD;
1678         if (unlikely(bq27xxx_battery_dead(di, di->cache.flags)))
1679                 return POWER_SUPPLY_HEALTH_DEAD;
1680
1681         return POWER_SUPPLY_HEALTH_GOOD;
1682 }
1683
1684 void bq27xxx_battery_update(struct bq27xxx_device_info *di)
1685 {
1686         struct bq27xxx_reg_cache cache = {0, };
1687         bool has_ci_flag = di->opts & BQ27XXX_O_HAS_CI;
1688         bool has_singe_flag = di->opts & BQ27XXX_O_ZERO;
1689
1690         cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
1691         if ((cache.flags & 0xff) == 0xff)
1692                 cache.flags = -1; /* read error */
1693         if (cache.flags >= 0) {
1694                 cache.temperature = bq27xxx_battery_read_temperature(di);
1695                 if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
1696                         dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
1697                         cache.capacity = -ENODATA;
1698                         cache.energy = -ENODATA;
1699                         cache.time_to_empty = -ENODATA;
1700                         cache.time_to_empty_avg = -ENODATA;
1701                         cache.time_to_full = -ENODATA;
1702                         cache.charge_full = -ENODATA;
1703                         cache.health = -ENODATA;
1704                 } else {
1705                         if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
1706                                 cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
1707                         if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
1708                                 cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
1709                         if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
1710                                 cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
1711
1712                         cache.charge_full = bq27xxx_battery_read_fcc(di);
1713                         cache.capacity = bq27xxx_battery_read_soc(di);
1714                         if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
1715                                 cache.energy = bq27xxx_battery_read_energy(di);
1716                         di->cache.flags = cache.flags;
1717                         cache.health = bq27xxx_battery_read_health(di);
1718                 }
1719                 if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
1720                         cache.cycle_count = bq27xxx_battery_read_cyct(di);
1721
1722                 /* We only have to read charge design full once */
1723                 if (di->charge_design_full <= 0)
1724                         di->charge_design_full = bq27xxx_battery_read_dcap(di);
1725         }
1726
1727         if ((di->cache.capacity != cache.capacity) ||
1728             (di->cache.flags != cache.flags))
1729                 power_supply_changed(di->bat);
1730
1731         if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
1732                 di->cache = cache;
1733
1734         di->last_update = jiffies;
1735 }
1736 EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
1737
1738 static void bq27xxx_battery_poll(struct work_struct *work)
1739 {
1740         struct bq27xxx_device_info *di =
1741                         container_of(work, struct bq27xxx_device_info,
1742                                      work.work);
1743
1744         bq27xxx_battery_update(di);
1745
1746         if (poll_interval > 0)
1747                 schedule_delayed_work(&di->work, poll_interval * HZ);
1748 }
1749
1750 /*
1751  * Return the battery average current in µA
1752  * Note that current can be negative signed as well
1753  * Or 0 if something fails.
1754  */
1755 static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
1756                                    union power_supply_propval *val)
1757 {
1758         int curr;
1759         int flags;
1760
1761         curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
1762         if (curr < 0) {
1763                 dev_err(di->dev, "error reading current\n");
1764                 return curr;
1765         }
1766
1767         if (di->opts & BQ27XXX_O_ZERO) {
1768                 flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
1769                 if (flags & BQ27000_FLAG_CHGS) {
1770                         dev_dbg(di->dev, "negative current!\n");
1771                         curr = -curr;
1772                 }
1773
1774                 val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
1775         } else {
1776                 /* Other gauges return signed value */
1777                 val->intval = (int)((s16)curr) * 1000;
1778         }
1779
1780         return 0;
1781 }
1782
1783 /*
1784  * Get the average power in µW
1785  * Return < 0 if something fails.
1786  */
1787 static int bq27xxx_battery_pwr_avg(struct bq27xxx_device_info *di,
1788                                    union power_supply_propval *val)
1789 {
1790         int power;
1791
1792         power = bq27xxx_read(di, BQ27XXX_REG_AP, false);
1793         if (power < 0) {
1794                 dev_err(di->dev,
1795                         "error reading average power register %02x: %d\n",
1796                         BQ27XXX_REG_AP, power);
1797                 return power;
1798         }
1799
1800         if (di->opts & BQ27XXX_O_ZERO)
1801                 val->intval = (power * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
1802         else
1803                 /* Other gauges return a signed value in units of 10mW */
1804                 val->intval = (int)((s16)power) * 10000;
1805
1806         return 0;
1807 }
1808
1809 static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
1810                                   union power_supply_propval *val)
1811 {
1812         int status;
1813
1814         if (di->opts & BQ27XXX_O_ZERO) {
1815                 if (di->cache.flags & BQ27000_FLAG_FC)
1816                         status = POWER_SUPPLY_STATUS_FULL;
1817                 else if (di->cache.flags & BQ27000_FLAG_CHGS)
1818                         status = POWER_SUPPLY_STATUS_CHARGING;
1819                 else
1820                         status = POWER_SUPPLY_STATUS_DISCHARGING;
1821         } else if (di->opts & BQ27Z561_O_BITS) {
1822                 if (di->cache.flags & BQ27Z561_FLAG_FC)
1823                         status = POWER_SUPPLY_STATUS_FULL;
1824                 else if (di->cache.flags & BQ27Z561_FLAG_DIS_CH)
1825                         status = POWER_SUPPLY_STATUS_DISCHARGING;
1826                 else
1827                         status = POWER_SUPPLY_STATUS_CHARGING;
1828         } else {
1829                 if (di->cache.flags & BQ27XXX_FLAG_FC)
1830                         status = POWER_SUPPLY_STATUS_FULL;
1831                 else if (di->cache.flags & BQ27XXX_FLAG_DSC)
1832                         status = POWER_SUPPLY_STATUS_DISCHARGING;
1833                 else
1834                         status = POWER_SUPPLY_STATUS_CHARGING;
1835         }
1836
1837         if ((status == POWER_SUPPLY_STATUS_DISCHARGING) &&
1838             (power_supply_am_i_supplied(di->bat) > 0))
1839                 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1840
1841         val->intval = status;
1842
1843         return 0;
1844 }
1845
1846 static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
1847                                           union power_supply_propval *val)
1848 {
1849         int level;
1850
1851         if (di->opts & BQ27XXX_O_ZERO) {
1852                 if (di->cache.flags & BQ27000_FLAG_FC)
1853                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1854                 else if (di->cache.flags & BQ27000_FLAG_EDV1)
1855                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1856                 else if (di->cache.flags & BQ27000_FLAG_EDVF)
1857                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1858                 else
1859                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1860         } else if (di->opts & BQ27Z561_O_BITS) {
1861                 if (di->cache.flags & BQ27Z561_FLAG_FC)
1862                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1863                 else if (di->cache.flags & BQ27Z561_FLAG_FDC)
1864                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1865                 else
1866                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1867         } else {
1868                 if (di->cache.flags & BQ27XXX_FLAG_FC)
1869                         level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1870                 else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
1871                         level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1872                 else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
1873                         level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1874                 else
1875                         level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1876         }
1877
1878         val->intval = level;
1879
1880         return 0;
1881 }
1882
1883 /*
1884  * Return the battery Voltage in millivolts
1885  * Or < 0 if something fails.
1886  */
1887 static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
1888                                    union power_supply_propval *val)
1889 {
1890         int volt;
1891
1892         volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
1893         if (volt < 0) {
1894                 dev_err(di->dev, "error reading voltage\n");
1895                 return volt;
1896         }
1897
1898         val->intval = volt * 1000;
1899
1900         return 0;
1901 }
1902
1903 static int bq27xxx_simple_value(int value,
1904                                 union power_supply_propval *val)
1905 {
1906         if (value < 0)
1907                 return value;
1908
1909         val->intval = value;
1910
1911         return 0;
1912 }
1913
1914 static int bq27xxx_battery_get_property(struct power_supply *psy,
1915                                         enum power_supply_property psp,
1916                                         union power_supply_propval *val)
1917 {
1918         int ret = 0;
1919         struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
1920
1921         mutex_lock(&di->lock);
1922         if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
1923                 cancel_delayed_work_sync(&di->work);
1924                 bq27xxx_battery_poll(&di->work.work);
1925         }
1926         mutex_unlock(&di->lock);
1927
1928         if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
1929                 return -ENODEV;
1930
1931         switch (psp) {
1932         case POWER_SUPPLY_PROP_STATUS:
1933                 ret = bq27xxx_battery_status(di, val);
1934                 break;
1935         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1936                 ret = bq27xxx_battery_voltage(di, val);
1937                 break;
1938         case POWER_SUPPLY_PROP_PRESENT:
1939                 val->intval = di->cache.flags < 0 ? 0 : 1;
1940                 break;
1941         case POWER_SUPPLY_PROP_CURRENT_NOW:
1942                 ret = bq27xxx_battery_current(di, val);
1943                 break;
1944         case POWER_SUPPLY_PROP_CAPACITY:
1945                 ret = bq27xxx_simple_value(di->cache.capacity, val);
1946                 break;
1947         case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1948                 ret = bq27xxx_battery_capacity_level(di, val);
1949                 break;
1950         case POWER_SUPPLY_PROP_TEMP:
1951                 ret = bq27xxx_simple_value(di->cache.temperature, val);
1952                 if (ret == 0)
1953                         val->intval -= 2731; /* convert decidegree k to c */
1954                 break;
1955         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
1956                 ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
1957                 break;
1958         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
1959                 ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
1960                 break;
1961         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
1962                 ret = bq27xxx_simple_value(di->cache.time_to_full, val);
1963                 break;
1964         case POWER_SUPPLY_PROP_TECHNOLOGY:
1965                 if (di->opts & BQ27XXX_O_MUL_CHEM)
1966                         val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
1967                 else
1968                         val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1969                 break;
1970         case POWER_SUPPLY_PROP_CHARGE_NOW:
1971                 ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
1972                 break;
1973         case POWER_SUPPLY_PROP_CHARGE_FULL:
1974                 ret = bq27xxx_simple_value(di->cache.charge_full, val);
1975                 break;
1976         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1977                 ret = bq27xxx_simple_value(di->charge_design_full, val);
1978                 break;
1979         /*
1980          * TODO: Implement these to make registers set from
1981          * power_supply_battery_info visible in sysfs.
1982          */
1983         case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
1984         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1985                 return -EINVAL;
1986         case POWER_SUPPLY_PROP_CYCLE_COUNT:
1987                 ret = bq27xxx_simple_value(di->cache.cycle_count, val);
1988                 break;
1989         case POWER_SUPPLY_PROP_ENERGY_NOW:
1990                 ret = bq27xxx_simple_value(di->cache.energy, val);
1991                 break;
1992         case POWER_SUPPLY_PROP_POWER_AVG:
1993                 ret = bq27xxx_battery_pwr_avg(di, val);
1994                 break;
1995         case POWER_SUPPLY_PROP_HEALTH:
1996                 ret = bq27xxx_simple_value(di->cache.health, val);
1997                 break;
1998         case POWER_SUPPLY_PROP_MANUFACTURER:
1999                 val->strval = BQ27XXX_MANUFACTURER;
2000                 break;
2001         default:
2002                 return -EINVAL;
2003         }
2004
2005         return ret;
2006 }
2007
2008 static void bq27xxx_external_power_changed(struct power_supply *psy)
2009 {
2010         struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
2011
2012         cancel_delayed_work_sync(&di->work);
2013         schedule_delayed_work(&di->work, 0);
2014 }
2015
2016 int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
2017 {
2018         struct power_supply_desc *psy_desc;
2019         struct power_supply_config psy_cfg = {
2020                 .of_node = di->dev->of_node,
2021                 .drv_data = di,
2022         };
2023
2024         INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
2025         mutex_init(&di->lock);
2026
2027         di->regs       = bq27xxx_chip_data[di->chip].regs;
2028         di->unseal_key = bq27xxx_chip_data[di->chip].unseal_key;
2029         di->dm_regs    = bq27xxx_chip_data[di->chip].dm_regs;
2030         di->opts       = bq27xxx_chip_data[di->chip].opts;
2031
2032         psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
2033         if (!psy_desc)
2034                 return -ENOMEM;
2035
2036         psy_desc->name = di->name;
2037         psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
2038         psy_desc->properties = bq27xxx_chip_data[di->chip].props;
2039         psy_desc->num_properties = bq27xxx_chip_data[di->chip].props_size;
2040         psy_desc->get_property = bq27xxx_battery_get_property;
2041         psy_desc->external_power_changed = bq27xxx_external_power_changed;
2042
2043         di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
2044         if (IS_ERR(di->bat))
2045                 return dev_err_probe(di->dev, PTR_ERR(di->bat),
2046                                      "failed to register battery\n");
2047
2048         bq27xxx_battery_settings(di);
2049         bq27xxx_battery_update(di);
2050
2051         mutex_lock(&bq27xxx_list_lock);
2052         list_add(&di->list, &bq27xxx_battery_devices);
2053         mutex_unlock(&bq27xxx_list_lock);
2054
2055         return 0;
2056 }
2057 EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
2058
2059 void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
2060 {
2061         /*
2062          * power_supply_unregister call bq27xxx_battery_get_property which
2063          * call bq27xxx_battery_poll.
2064          * Make sure that bq27xxx_battery_poll will not call
2065          * schedule_delayed_work again after unregister (which cause OOPS).
2066          */
2067         poll_interval = 0;
2068
2069         cancel_delayed_work_sync(&di->work);
2070
2071         power_supply_unregister(di->bat);
2072
2073         mutex_lock(&bq27xxx_list_lock);
2074         list_del(&di->list);
2075         mutex_unlock(&bq27xxx_list_lock);
2076
2077         mutex_destroy(&di->lock);
2078 }
2079 EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
2080
2081 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
2082 MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
2083 MODULE_LICENSE("GPL");