1 /*
2  * Driver for the TI bq24190 battery charger.
3  *
4  * Author: Mark A. Greer <mgreer@animalcreek.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/power_supply.h>
18 #include <linux/power/bq24190_charger.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/workqueue.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
24 #include <linux/extcon-provider.h>
25 
26 #define	BQ24190_MANUFACTURER	"Texas Instruments"
27 
28 #define BQ24190_REG_ISC		0x00 /* Input Source Control */
29 #define BQ24190_REG_ISC_EN_HIZ_MASK		BIT(7)
30 #define BQ24190_REG_ISC_EN_HIZ_SHIFT		7
31 #define BQ24190_REG_ISC_VINDPM_MASK		(BIT(6) | BIT(5) | BIT(4) | \
32 						 BIT(3))
33 #define BQ24190_REG_ISC_VINDPM_SHIFT		3
34 #define BQ24190_REG_ISC_IINLIM_MASK		(BIT(2) | BIT(1) | BIT(0))
35 #define BQ24190_REG_ISC_IINLIM_SHIFT		0
36 
37 #define BQ24190_REG_POC		0x01 /* Power-On Configuration */
38 #define BQ24190_REG_POC_RESET_MASK		BIT(7)
39 #define BQ24190_REG_POC_RESET_SHIFT		7
40 #define BQ24190_REG_POC_WDT_RESET_MASK		BIT(6)
41 #define BQ24190_REG_POC_WDT_RESET_SHIFT		6
42 #define BQ24190_REG_POC_CHG_CONFIG_MASK		(BIT(5) | BIT(4))
43 #define BQ24190_REG_POC_CHG_CONFIG_SHIFT	4
44 #define BQ24190_REG_POC_CHG_CONFIG_DISABLE		0x0
45 #define BQ24190_REG_POC_CHG_CONFIG_CHARGE		0x1
46 #define BQ24190_REG_POC_CHG_CONFIG_OTG			0x2
47 #define BQ24190_REG_POC_SYS_MIN_MASK		(BIT(3) | BIT(2) | BIT(1))
48 #define BQ24190_REG_POC_SYS_MIN_SHIFT		1
49 #define BQ24190_REG_POC_SYS_MIN_MIN			3000
50 #define BQ24190_REG_POC_SYS_MIN_MAX			3700
51 #define BQ24190_REG_POC_BOOST_LIM_MASK		BIT(0)
52 #define BQ24190_REG_POC_BOOST_LIM_SHIFT		0
53 
54 #define BQ24190_REG_CCC		0x02 /* Charge Current Control */
55 #define BQ24190_REG_CCC_ICHG_MASK		(BIT(7) | BIT(6) | BIT(5) | \
56 						 BIT(4) | BIT(3) | BIT(2))
57 #define BQ24190_REG_CCC_ICHG_SHIFT		2
58 #define BQ24190_REG_CCC_FORCE_20PCT_MASK	BIT(0)
59 #define BQ24190_REG_CCC_FORCE_20PCT_SHIFT	0
60 
61 #define BQ24190_REG_PCTCC	0x03 /* Pre-charge/Termination Current Cntl */
62 #define BQ24190_REG_PCTCC_IPRECHG_MASK		(BIT(7) | BIT(6) | BIT(5) | \
63 						 BIT(4))
64 #define BQ24190_REG_PCTCC_IPRECHG_SHIFT		4
65 #define BQ24190_REG_PCTCC_IPRECHG_MIN			128
66 #define BQ24190_REG_PCTCC_IPRECHG_MAX			2048
67 #define BQ24190_REG_PCTCC_ITERM_MASK		(BIT(3) | BIT(2) | BIT(1) | \
68 						 BIT(0))
69 #define BQ24190_REG_PCTCC_ITERM_SHIFT		0
70 #define BQ24190_REG_PCTCC_ITERM_MIN			128
71 #define BQ24190_REG_PCTCC_ITERM_MAX			2048
72 
73 #define BQ24190_REG_CVC		0x04 /* Charge Voltage Control */
74 #define BQ24190_REG_CVC_VREG_MASK		(BIT(7) | BIT(6) | BIT(5) | \
75 						 BIT(4) | BIT(3) | BIT(2))
76 #define BQ24190_REG_CVC_VREG_SHIFT		2
77 #define BQ24190_REG_CVC_BATLOWV_MASK		BIT(1)
78 #define BQ24190_REG_CVC_BATLOWV_SHIFT		1
79 #define BQ24190_REG_CVC_VRECHG_MASK		BIT(0)
80 #define BQ24190_REG_CVC_VRECHG_SHIFT		0
81 
82 #define BQ24190_REG_CTTC	0x05 /* Charge Term/Timer Control */
83 #define BQ24190_REG_CTTC_EN_TERM_MASK		BIT(7)
84 #define BQ24190_REG_CTTC_EN_TERM_SHIFT		7
85 #define BQ24190_REG_CTTC_TERM_STAT_MASK		BIT(6)
86 #define BQ24190_REG_CTTC_TERM_STAT_SHIFT	6
87 #define BQ24190_REG_CTTC_WATCHDOG_MASK		(BIT(5) | BIT(4))
88 #define BQ24190_REG_CTTC_WATCHDOG_SHIFT		4
89 #define BQ24190_REG_CTTC_EN_TIMER_MASK		BIT(3)
90 #define BQ24190_REG_CTTC_EN_TIMER_SHIFT		3
91 #define BQ24190_REG_CTTC_CHG_TIMER_MASK		(BIT(2) | BIT(1))
92 #define BQ24190_REG_CTTC_CHG_TIMER_SHIFT	1
93 #define BQ24190_REG_CTTC_JEITA_ISET_MASK	BIT(0)
94 #define BQ24190_REG_CTTC_JEITA_ISET_SHIFT	0
95 
96 #define BQ24190_REG_ICTRC	0x06 /* IR Comp/Thermal Regulation Control */
97 #define BQ24190_REG_ICTRC_BAT_COMP_MASK		(BIT(7) | BIT(6) | BIT(5))
98 #define BQ24190_REG_ICTRC_BAT_COMP_SHIFT	5
99 #define BQ24190_REG_ICTRC_VCLAMP_MASK		(BIT(4) | BIT(3) | BIT(2))
100 #define BQ24190_REG_ICTRC_VCLAMP_SHIFT		2
101 #define BQ24190_REG_ICTRC_TREG_MASK		(BIT(1) | BIT(0))
102 #define BQ24190_REG_ICTRC_TREG_SHIFT		0
103 
104 #define BQ24190_REG_MOC		0x07 /* Misc. Operation Control */
105 #define BQ24190_REG_MOC_DPDM_EN_MASK		BIT(7)
106 #define BQ24190_REG_MOC_DPDM_EN_SHIFT		7
107 #define BQ24190_REG_MOC_TMR2X_EN_MASK		BIT(6)
108 #define BQ24190_REG_MOC_TMR2X_EN_SHIFT		6
109 #define BQ24190_REG_MOC_BATFET_DISABLE_MASK	BIT(5)
110 #define BQ24190_REG_MOC_BATFET_DISABLE_SHIFT	5
111 #define BQ24190_REG_MOC_JEITA_VSET_MASK		BIT(4)
112 #define BQ24190_REG_MOC_JEITA_VSET_SHIFT	4
113 #define BQ24190_REG_MOC_INT_MASK_MASK		(BIT(1) | BIT(0))
114 #define BQ24190_REG_MOC_INT_MASK_SHIFT		0
115 
116 #define BQ24190_REG_SS		0x08 /* System Status */
117 #define BQ24190_REG_SS_VBUS_STAT_MASK		(BIT(7) | BIT(6))
118 #define BQ24190_REG_SS_VBUS_STAT_SHIFT		6
119 #define BQ24190_REG_SS_CHRG_STAT_MASK		(BIT(5) | BIT(4))
120 #define BQ24190_REG_SS_CHRG_STAT_SHIFT		4
121 #define BQ24190_REG_SS_DPM_STAT_MASK		BIT(3)
122 #define BQ24190_REG_SS_DPM_STAT_SHIFT		3
123 #define BQ24190_REG_SS_PG_STAT_MASK		BIT(2)
124 #define BQ24190_REG_SS_PG_STAT_SHIFT		2
125 #define BQ24190_REG_SS_THERM_STAT_MASK		BIT(1)
126 #define BQ24190_REG_SS_THERM_STAT_SHIFT		1
127 #define BQ24190_REG_SS_VSYS_STAT_MASK		BIT(0)
128 #define BQ24190_REG_SS_VSYS_STAT_SHIFT		0
129 
130 #define BQ24190_REG_F		0x09 /* Fault */
131 #define BQ24190_REG_F_WATCHDOG_FAULT_MASK	BIT(7)
132 #define BQ24190_REG_F_WATCHDOG_FAULT_SHIFT	7
133 #define BQ24190_REG_F_BOOST_FAULT_MASK		BIT(6)
134 #define BQ24190_REG_F_BOOST_FAULT_SHIFT		6
135 #define BQ24190_REG_F_CHRG_FAULT_MASK		(BIT(5) | BIT(4))
136 #define BQ24190_REG_F_CHRG_FAULT_SHIFT		4
137 #define BQ24190_REG_F_BAT_FAULT_MASK		BIT(3)
138 #define BQ24190_REG_F_BAT_FAULT_SHIFT		3
139 #define BQ24190_REG_F_NTC_FAULT_MASK		(BIT(2) | BIT(1) | BIT(0))
140 #define BQ24190_REG_F_NTC_FAULT_SHIFT		0
141 
142 #define BQ24190_REG_VPRS	0x0A /* Vendor/Part/Revision Status */
143 #define BQ24190_REG_VPRS_PN_MASK		(BIT(5) | BIT(4) | BIT(3))
144 #define BQ24190_REG_VPRS_PN_SHIFT		3
145 #define BQ24190_REG_VPRS_PN_24190			0x4
146 #define BQ24190_REG_VPRS_PN_24192			0x5 /* Also 24193, 24196 */
147 #define BQ24190_REG_VPRS_PN_24192I			0x3
148 #define BQ24190_REG_VPRS_TS_PROFILE_MASK	BIT(2)
149 #define BQ24190_REG_VPRS_TS_PROFILE_SHIFT	2
150 #define BQ24190_REG_VPRS_DEV_REG_MASK		(BIT(1) | BIT(0))
151 #define BQ24190_REG_VPRS_DEV_REG_SHIFT		0
152 
153 /*
154  * The FAULT register is latched by the bq24190 (except for NTC_FAULT)
155  * so the first read after a fault returns the latched value and subsequent
156  * reads return the current value.  In order to return the fault status
157  * to the user, have the interrupt handler save the reg's value and retrieve
158  * it in the appropriate health/status routine.
159  */
160 struct bq24190_dev_info {
161 	struct i2c_client		*client;
162 	struct device			*dev;
163 	struct extcon_dev		*edev;
164 	struct power_supply		*charger;
165 	struct power_supply		*battery;
166 	struct delayed_work		input_current_limit_work;
167 	char				model_name[I2C_NAME_SIZE];
168 	bool				initialized;
169 	bool				irq_event;
170 	u16				sys_min;
171 	u16				iprechg;
172 	u16				iterm;
173 	struct mutex			f_reg_lock;
174 	u8				f_reg;
175 	u8				ss_reg;
176 	u8				watchdog;
177 };
178 
179 static const unsigned int bq24190_usb_extcon_cable[] = {
180 	EXTCON_USB,
181 	EXTCON_NONE,
182 };
183 
184 /*
185  * The tables below provide a 2-way mapping for the value that goes in
186  * the register field and the real-world value that it represents.
187  * The index of the array is the value that goes in the register; the
188  * number at that index in the array is the real-world value that it
189  * represents.
190  */
191 
192 /* REG00[2:0] (IINLIM) in uAh */
193 static const int bq24190_isc_iinlim_values[] = {
194 	 100000,  150000,  500000,  900000, 1200000, 1500000, 2000000, 3000000
195 };
196 
197 /* REG02[7:2] (ICHG) in uAh */
198 static const int bq24190_ccc_ichg_values[] = {
199 	 512000,  576000,  640000,  704000,  768000,  832000,  896000,  960000,
200 	1024000, 1088000, 1152000, 1216000, 1280000, 1344000, 1408000, 1472000,
201 	1536000, 1600000, 1664000, 1728000, 1792000, 1856000, 1920000, 1984000,
202 	2048000, 2112000, 2176000, 2240000, 2304000, 2368000, 2432000, 2496000,
203 	2560000, 2624000, 2688000, 2752000, 2816000, 2880000, 2944000, 3008000,
204 	3072000, 3136000, 3200000, 3264000, 3328000, 3392000, 3456000, 3520000,
205 	3584000, 3648000, 3712000, 3776000, 3840000, 3904000, 3968000, 4032000,
206 	4096000, 4160000, 4224000, 4288000, 4352000, 4416000, 4480000, 4544000
207 };
208 
209 /* REG04[7:2] (VREG) in uV */
210 static const int bq24190_cvc_vreg_values[] = {
211 	3504000, 3520000, 3536000, 3552000, 3568000, 3584000, 3600000, 3616000,
212 	3632000, 3648000, 3664000, 3680000, 3696000, 3712000, 3728000, 3744000,
213 	3760000, 3776000, 3792000, 3808000, 3824000, 3840000, 3856000, 3872000,
214 	3888000, 3904000, 3920000, 3936000, 3952000, 3968000, 3984000, 4000000,
215 	4016000, 4032000, 4048000, 4064000, 4080000, 4096000, 4112000, 4128000,
216 	4144000, 4160000, 4176000, 4192000, 4208000, 4224000, 4240000, 4256000,
217 	4272000, 4288000, 4304000, 4320000, 4336000, 4352000, 4368000, 4384000,
218 	4400000
219 };
220 
221 /* REG06[1:0] (TREG) in tenths of degrees Celsius */
222 static const int bq24190_ictrc_treg_values[] = {
223 	600, 800, 1000, 1200
224 };
225 
226 /*
227  * Return the index in 'tbl' of greatest value that is less than or equal to
228  * 'val'.  The index range returned is 0 to 'tbl_size' - 1.  Assumes that
229  * the values in 'tbl' are sorted from smallest to largest and 'tbl_size'
230  * is less than 2^8.
231  */
232 static u8 bq24190_find_idx(const int tbl[], int tbl_size, int v)
233 {
234 	int i;
235 
236 	for (i = 1; i < tbl_size; i++)
237 		if (v < tbl[i])
238 			break;
239 
240 	return i - 1;
241 }
242 
243 /* Basic driver I/O routines */
244 
245 static int bq24190_read(struct bq24190_dev_info *bdi, u8 reg, u8 *data)
246 {
247 	int ret;
248 
249 	ret = i2c_smbus_read_byte_data(bdi->client, reg);
250 	if (ret < 0)
251 		return ret;
252 
253 	*data = ret;
254 	return 0;
255 }
256 
257 static int bq24190_write(struct bq24190_dev_info *bdi, u8 reg, u8 data)
258 {
259 	return i2c_smbus_write_byte_data(bdi->client, reg, data);
260 }
261 
262 static int bq24190_read_mask(struct bq24190_dev_info *bdi, u8 reg,
263 		u8 mask, u8 shift, u8 *data)
264 {
265 	u8 v;
266 	int ret;
267 
268 	ret = bq24190_read(bdi, reg, &v);
269 	if (ret < 0)
270 		return ret;
271 
272 	v &= mask;
273 	v >>= shift;
274 	*data = v;
275 
276 	return 0;
277 }
278 
279 static int bq24190_write_mask(struct bq24190_dev_info *bdi, u8 reg,
280 		u8 mask, u8 shift, u8 data)
281 {
282 	u8 v;
283 	int ret;
284 
285 	ret = bq24190_read(bdi, reg, &v);
286 	if (ret < 0)
287 		return ret;
288 
289 	v &= ~mask;
290 	v |= ((data << shift) & mask);
291 
292 	return bq24190_write(bdi, reg, v);
293 }
294 
295 static int bq24190_get_field_val(struct bq24190_dev_info *bdi,
296 		u8 reg, u8 mask, u8 shift,
297 		const int tbl[], int tbl_size,
298 		int *val)
299 {
300 	u8 v;
301 	int ret;
302 
303 	ret = bq24190_read_mask(bdi, reg, mask, shift, &v);
304 	if (ret < 0)
305 		return ret;
306 
307 	v = (v >= tbl_size) ? (tbl_size - 1) : v;
308 	*val = tbl[v];
309 
310 	return 0;
311 }
312 
313 static int bq24190_set_field_val(struct bq24190_dev_info *bdi,
314 		u8 reg, u8 mask, u8 shift,
315 		const int tbl[], int tbl_size,
316 		int val)
317 {
318 	u8 idx;
319 
320 	idx = bq24190_find_idx(tbl, tbl_size, val);
321 
322 	return bq24190_write_mask(bdi, reg, mask, shift, idx);
323 }
324 
325 #ifdef CONFIG_SYSFS
326 /*
327  * There are a numerous options that are configurable on the bq24190
328  * that go well beyond what the power_supply properties provide access to.
329  * Provide sysfs access to them so they can be examined and possibly modified
330  * on the fly.  They will be provided for the charger power_supply object only
331  * and will be prefixed by 'f_' to make them easier to recognize.
332  */
333 
334 #define BQ24190_SYSFS_FIELD(_name, r, f, m, store)			\
335 {									\
336 	.attr	= __ATTR(f_##_name, m, bq24190_sysfs_show, store),	\
337 	.reg	= BQ24190_REG_##r,					\
338 	.mask	= BQ24190_REG_##r##_##f##_MASK,				\
339 	.shift	= BQ24190_REG_##r##_##f##_SHIFT,			\
340 }
341 
342 #define BQ24190_SYSFS_FIELD_RW(_name, r, f)				\
343 		BQ24190_SYSFS_FIELD(_name, r, f, S_IWUSR | S_IRUGO,	\
344 				bq24190_sysfs_store)
345 
346 #define BQ24190_SYSFS_FIELD_RO(_name, r, f)				\
347 		BQ24190_SYSFS_FIELD(_name, r, f, S_IRUGO, NULL)
348 
349 static ssize_t bq24190_sysfs_show(struct device *dev,
350 		struct device_attribute *attr, char *buf);
351 static ssize_t bq24190_sysfs_store(struct device *dev,
352 		struct device_attribute *attr, const char *buf, size_t count);
353 
354 struct bq24190_sysfs_field_info {
355 	struct device_attribute	attr;
356 	u8	reg;
357 	u8	mask;
358 	u8	shift;
359 };
360 
361 /* On i386 ptrace-abi.h defines SS that breaks the macro calls below. */
362 #undef SS
363 
364 static struct bq24190_sysfs_field_info bq24190_sysfs_field_tbl[] = {
365 			/*	sysfs name	reg	field in reg */
366 	BQ24190_SYSFS_FIELD_RW(en_hiz,		ISC,	EN_HIZ),
367 	BQ24190_SYSFS_FIELD_RW(vindpm,		ISC,	VINDPM),
368 	BQ24190_SYSFS_FIELD_RW(iinlim,		ISC,	IINLIM),
369 	BQ24190_SYSFS_FIELD_RW(chg_config,	POC,	CHG_CONFIG),
370 	BQ24190_SYSFS_FIELD_RW(sys_min,		POC,	SYS_MIN),
371 	BQ24190_SYSFS_FIELD_RW(boost_lim,	POC,	BOOST_LIM),
372 	BQ24190_SYSFS_FIELD_RW(ichg,		CCC,	ICHG),
373 	BQ24190_SYSFS_FIELD_RW(force_20_pct,	CCC,	FORCE_20PCT),
374 	BQ24190_SYSFS_FIELD_RW(iprechg,		PCTCC,	IPRECHG),
375 	BQ24190_SYSFS_FIELD_RW(iterm,		PCTCC,	ITERM),
376 	BQ24190_SYSFS_FIELD_RW(vreg,		CVC,	VREG),
377 	BQ24190_SYSFS_FIELD_RW(batlowv,		CVC,	BATLOWV),
378 	BQ24190_SYSFS_FIELD_RW(vrechg,		CVC,	VRECHG),
379 	BQ24190_SYSFS_FIELD_RW(en_term,		CTTC,	EN_TERM),
380 	BQ24190_SYSFS_FIELD_RW(term_stat,	CTTC,	TERM_STAT),
381 	BQ24190_SYSFS_FIELD_RO(watchdog,	CTTC,	WATCHDOG),
382 	BQ24190_SYSFS_FIELD_RW(en_timer,	CTTC,	EN_TIMER),
383 	BQ24190_SYSFS_FIELD_RW(chg_timer,	CTTC,	CHG_TIMER),
384 	BQ24190_SYSFS_FIELD_RW(jeta_iset,	CTTC,	JEITA_ISET),
385 	BQ24190_SYSFS_FIELD_RW(bat_comp,	ICTRC,	BAT_COMP),
386 	BQ24190_SYSFS_FIELD_RW(vclamp,		ICTRC,	VCLAMP),
387 	BQ24190_SYSFS_FIELD_RW(treg,		ICTRC,	TREG),
388 	BQ24190_SYSFS_FIELD_RW(dpdm_en,		MOC,	DPDM_EN),
389 	BQ24190_SYSFS_FIELD_RW(tmr2x_en,	MOC,	TMR2X_EN),
390 	BQ24190_SYSFS_FIELD_RW(batfet_disable,	MOC,	BATFET_DISABLE),
391 	BQ24190_SYSFS_FIELD_RW(jeita_vset,	MOC,	JEITA_VSET),
392 	BQ24190_SYSFS_FIELD_RO(int_mask,	MOC,	INT_MASK),
393 	BQ24190_SYSFS_FIELD_RO(vbus_stat,	SS,	VBUS_STAT),
394 	BQ24190_SYSFS_FIELD_RO(chrg_stat,	SS,	CHRG_STAT),
395 	BQ24190_SYSFS_FIELD_RO(dpm_stat,	SS,	DPM_STAT),
396 	BQ24190_SYSFS_FIELD_RO(pg_stat,		SS,	PG_STAT),
397 	BQ24190_SYSFS_FIELD_RO(therm_stat,	SS,	THERM_STAT),
398 	BQ24190_SYSFS_FIELD_RO(vsys_stat,	SS,	VSYS_STAT),
399 	BQ24190_SYSFS_FIELD_RO(watchdog_fault,	F,	WATCHDOG_FAULT),
400 	BQ24190_SYSFS_FIELD_RO(boost_fault,	F,	BOOST_FAULT),
401 	BQ24190_SYSFS_FIELD_RO(chrg_fault,	F,	CHRG_FAULT),
402 	BQ24190_SYSFS_FIELD_RO(bat_fault,	F,	BAT_FAULT),
403 	BQ24190_SYSFS_FIELD_RO(ntc_fault,	F,	NTC_FAULT),
404 	BQ24190_SYSFS_FIELD_RO(pn,		VPRS,	PN),
405 	BQ24190_SYSFS_FIELD_RO(ts_profile,	VPRS,	TS_PROFILE),
406 	BQ24190_SYSFS_FIELD_RO(dev_reg,		VPRS,	DEV_REG),
407 };
408 
409 static struct attribute *
410 	bq24190_sysfs_attrs[ARRAY_SIZE(bq24190_sysfs_field_tbl) + 1];
411 
412 ATTRIBUTE_GROUPS(bq24190_sysfs);
413 
414 static void bq24190_sysfs_init_attrs(void)
415 {
416 	int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl);
417 
418 	for (i = 0; i < limit; i++)
419 		bq24190_sysfs_attrs[i] = &bq24190_sysfs_field_tbl[i].attr.attr;
420 
421 	bq24190_sysfs_attrs[limit] = NULL; /* Has additional entry for this */
422 }
423 
424 static struct bq24190_sysfs_field_info *bq24190_sysfs_field_lookup(
425 		const char *name)
426 {
427 	int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl);
428 
429 	for (i = 0; i < limit; i++)
430 		if (!strcmp(name, bq24190_sysfs_field_tbl[i].attr.attr.name))
431 			break;
432 
433 	if (i >= limit)
434 		return NULL;
435 
436 	return &bq24190_sysfs_field_tbl[i];
437 }
438 
439 static ssize_t bq24190_sysfs_show(struct device *dev,
440 		struct device_attribute *attr, char *buf)
441 {
442 	struct power_supply *psy = dev_get_drvdata(dev);
443 	struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
444 	struct bq24190_sysfs_field_info *info;
445 	ssize_t count;
446 	int ret;
447 	u8 v;
448 
449 	info = bq24190_sysfs_field_lookup(attr->attr.name);
450 	if (!info)
451 		return -EINVAL;
452 
453 	ret = pm_runtime_get_sync(bdi->dev);
454 	if (ret < 0)
455 		return ret;
456 
457 	ret = bq24190_read_mask(bdi, info->reg, info->mask, info->shift, &v);
458 	if (ret)
459 		count = ret;
460 	else
461 		count = scnprintf(buf, PAGE_SIZE, "%hhx\n", v);
462 
463 	pm_runtime_mark_last_busy(bdi->dev);
464 	pm_runtime_put_autosuspend(bdi->dev);
465 
466 	return count;
467 }
468 
469 static ssize_t bq24190_sysfs_store(struct device *dev,
470 		struct device_attribute *attr, const char *buf, size_t count)
471 {
472 	struct power_supply *psy = dev_get_drvdata(dev);
473 	struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
474 	struct bq24190_sysfs_field_info *info;
475 	int ret;
476 	u8 v;
477 
478 	info = bq24190_sysfs_field_lookup(attr->attr.name);
479 	if (!info)
480 		return -EINVAL;
481 
482 	ret = kstrtou8(buf, 0, &v);
483 	if (ret < 0)
484 		return ret;
485 
486 	ret = pm_runtime_get_sync(bdi->dev);
487 	if (ret < 0)
488 		return ret;
489 
490 	ret = bq24190_write_mask(bdi, info->reg, info->mask, info->shift, v);
491 	if (ret)
492 		count = ret;
493 
494 	pm_runtime_mark_last_busy(bdi->dev);
495 	pm_runtime_put_autosuspend(bdi->dev);
496 
497 	return count;
498 }
499 #endif
500 
501 #ifdef CONFIG_REGULATOR
502 static int bq24190_set_charge_mode(struct regulator_dev *dev, u8 val)
503 {
504 	struct bq24190_dev_info *bdi = rdev_get_drvdata(dev);
505 	int ret;
506 
507 	ret = pm_runtime_get_sync(bdi->dev);
508 	if (ret < 0) {
509 		dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret);
510 		pm_runtime_put_noidle(bdi->dev);
511 		return ret;
512 	}
513 
514 	ret = bq24190_write_mask(bdi, BQ24190_REG_POC,
515 				 BQ24190_REG_POC_CHG_CONFIG_MASK,
516 				 BQ24190_REG_POC_CHG_CONFIG_SHIFT, val);
517 
518 	pm_runtime_mark_last_busy(bdi->dev);
519 	pm_runtime_put_autosuspend(bdi->dev);
520 
521 	return ret;
522 }
523 
524 static int bq24190_vbus_enable(struct regulator_dev *dev)
525 {
526 	return bq24190_set_charge_mode(dev, BQ24190_REG_POC_CHG_CONFIG_OTG);
527 }
528 
529 static int bq24190_vbus_disable(struct regulator_dev *dev)
530 {
531 	return bq24190_set_charge_mode(dev, BQ24190_REG_POC_CHG_CONFIG_CHARGE);
532 }
533 
534 static int bq24190_vbus_is_enabled(struct regulator_dev *dev)
535 {
536 	struct bq24190_dev_info *bdi = rdev_get_drvdata(dev);
537 	int ret;
538 	u8 val;
539 
540 	ret = pm_runtime_get_sync(bdi->dev);
541 	if (ret < 0) {
542 		dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret);
543 		pm_runtime_put_noidle(bdi->dev);
544 		return ret;
545 	}
546 
547 	ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
548 				BQ24190_REG_POC_CHG_CONFIG_MASK,
549 				BQ24190_REG_POC_CHG_CONFIG_SHIFT, &val);
550 
551 	pm_runtime_mark_last_busy(bdi->dev);
552 	pm_runtime_put_autosuspend(bdi->dev);
553 
554 	return ret ? ret : val == BQ24190_REG_POC_CHG_CONFIG_OTG;
555 }
556 
557 static const struct regulator_ops bq24190_vbus_ops = {
558 	.enable = bq24190_vbus_enable,
559 	.disable = bq24190_vbus_disable,
560 	.is_enabled = bq24190_vbus_is_enabled,
561 };
562 
563 static const struct regulator_desc bq24190_vbus_desc = {
564 	.name = "usb_otg_vbus",
565 	.of_match = "usb-otg-vbus",
566 	.type = REGULATOR_VOLTAGE,
567 	.owner = THIS_MODULE,
568 	.ops = &bq24190_vbus_ops,
569 	.fixed_uV = 5000000,
570 	.n_voltages = 1,
571 };
572 
573 static const struct regulator_init_data bq24190_vbus_init_data = {
574 	.constraints = {
575 		.valid_ops_mask = REGULATOR_CHANGE_STATUS,
576 	},
577 };
578 
579 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi)
580 {
581 	struct bq24190_platform_data *pdata = bdi->dev->platform_data;
582 	struct regulator_config cfg = { };
583 	struct regulator_dev *reg;
584 	int ret = 0;
585 
586 	cfg.dev = bdi->dev;
587 	if (pdata && pdata->regulator_init_data)
588 		cfg.init_data = pdata->regulator_init_data;
589 	else
590 		cfg.init_data = &bq24190_vbus_init_data;
591 	cfg.driver_data = bdi;
592 	reg = devm_regulator_register(bdi->dev, &bq24190_vbus_desc, &cfg);
593 	if (IS_ERR(reg)) {
594 		ret = PTR_ERR(reg);
595 		dev_err(bdi->dev, "Can't register regulator: %d\n", ret);
596 	}
597 
598 	return ret;
599 }
600 #else
601 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi)
602 {
603 	return 0;
604 }
605 #endif
606 
607 static int bq24190_set_config(struct bq24190_dev_info *bdi)
608 {
609 	int ret;
610 	u8 v;
611 
612 	ret = bq24190_read(bdi, BQ24190_REG_CTTC, &v);
613 	if (ret < 0)
614 		return ret;
615 
616 	bdi->watchdog = ((v & BQ24190_REG_CTTC_WATCHDOG_MASK) >>
617 					BQ24190_REG_CTTC_WATCHDOG_SHIFT);
618 
619 	/*
620 	 * According to the "Host Mode and default Mode" section of the
621 	 * manual, a write to any register causes the bq24190 to switch
622 	 * from default mode to host mode.  It will switch back to default
623 	 * mode after a WDT timeout unless the WDT is turned off as well.
624 	 * So, by simply turning off the WDT, we accomplish both with the
625 	 * same write.
626 	 */
627 	v &= ~BQ24190_REG_CTTC_WATCHDOG_MASK;
628 
629 	ret = bq24190_write(bdi, BQ24190_REG_CTTC, v);
630 	if (ret < 0)
631 		return ret;
632 
633 	if (bdi->sys_min) {
634 		v = bdi->sys_min / 100 - 30; // manual section 9.5.1.2, table 9
635 		ret = bq24190_write_mask(bdi, BQ24190_REG_POC,
636 					 BQ24190_REG_POC_SYS_MIN_MASK,
637 					 BQ24190_REG_POC_SYS_MIN_SHIFT,
638 					 v);
639 		if (ret < 0)
640 			return ret;
641 	}
642 
643 	if (bdi->iprechg) {
644 		v = bdi->iprechg / 128 - 1; // manual section 9.5.1.4, table 11
645 		ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC,
646 					 BQ24190_REG_PCTCC_IPRECHG_MASK,
647 					 BQ24190_REG_PCTCC_IPRECHG_SHIFT,
648 					 v);
649 		if (ret < 0)
650 			return ret;
651 	}
652 
653 	if (bdi->iterm) {
654 		v = bdi->iterm / 128 - 1; // manual section 9.5.1.4, table 11
655 		ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC,
656 					 BQ24190_REG_PCTCC_ITERM_MASK,
657 					 BQ24190_REG_PCTCC_ITERM_SHIFT,
658 					 v);
659 		if (ret < 0)
660 			return ret;
661 	}
662 
663 	return 0;
664 }
665 
666 static int bq24190_register_reset(struct bq24190_dev_info *bdi)
667 {
668 	int ret, limit = 100;
669 	u8 v;
670 
671 	/*
672 	 * This prop. can be passed on device instantiation from platform code:
673 	 * struct property_entry pe[] =
674 	 *   { PROPERTY_ENTRY_BOOL("disable-reset"), ... };
675 	 * struct i2c_board_info bi =
676 	 *   { .type = "bq24190", .addr = 0x6b, .properties = pe, .irq = irq };
677 	 * struct i2c_adapter ad = { ... };
678 	 * i2c_add_adapter(&ad);
679 	 * i2c_new_device(&ad, &bi);
680 	 */
681 	if (device_property_read_bool(bdi->dev, "disable-reset"))
682 		return 0;
683 
684 	/* Reset the registers */
685 	ret = bq24190_write_mask(bdi, BQ24190_REG_POC,
686 			BQ24190_REG_POC_RESET_MASK,
687 			BQ24190_REG_POC_RESET_SHIFT,
688 			0x1);
689 	if (ret < 0)
690 		return ret;
691 
692 	/* Reset bit will be cleared by hardware so poll until it is */
693 	do {
694 		ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
695 				BQ24190_REG_POC_RESET_MASK,
696 				BQ24190_REG_POC_RESET_SHIFT,
697 				&v);
698 		if (ret < 0)
699 			return ret;
700 
701 		if (v == 0)
702 			return 0;
703 
704 		usleep_range(100, 200);
705 	} while (--limit);
706 
707 	return -EIO;
708 }
709 
710 /* Charger power supply property routines */
711 
712 static int bq24190_charger_get_charge_type(struct bq24190_dev_info *bdi,
713 		union power_supply_propval *val)
714 {
715 	u8 v;
716 	int type, ret;
717 
718 	ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
719 			BQ24190_REG_POC_CHG_CONFIG_MASK,
720 			BQ24190_REG_POC_CHG_CONFIG_SHIFT,
721 			&v);
722 	if (ret < 0)
723 		return ret;
724 
725 	/* If POC[CHG_CONFIG] (REG01[5:4]) == 0, charge is disabled */
726 	if (!v) {
727 		type = POWER_SUPPLY_CHARGE_TYPE_NONE;
728 	} else {
729 		ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
730 				BQ24190_REG_CCC_FORCE_20PCT_MASK,
731 				BQ24190_REG_CCC_FORCE_20PCT_SHIFT,
732 				&v);
733 		if (ret < 0)
734 			return ret;
735 
736 		type = (v) ? POWER_SUPPLY_CHARGE_TYPE_TRICKLE :
737 			     POWER_SUPPLY_CHARGE_TYPE_FAST;
738 	}
739 
740 	val->intval = type;
741 
742 	return 0;
743 }
744 
745 static int bq24190_charger_set_charge_type(struct bq24190_dev_info *bdi,
746 		const union power_supply_propval *val)
747 {
748 	u8 chg_config, force_20pct, en_term;
749 	int ret;
750 
751 	/*
752 	 * According to the "Termination when REG02[0] = 1" section of
753 	 * the bq24190 manual, the trickle charge could be less than the
754 	 * termination current so it recommends turning off the termination
755 	 * function.
756 	 *
757 	 * Note: AFAICT from the datasheet, the user will have to manually
758 	 * turn off the charging when in 20% mode.  If its not turned off,
759 	 * there could be battery damage.  So, use this mode at your own risk.
760 	 */
761 	switch (val->intval) {
762 	case POWER_SUPPLY_CHARGE_TYPE_NONE:
763 		chg_config = 0x0;
764 		break;
765 	case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
766 		chg_config = 0x1;
767 		force_20pct = 0x1;
768 		en_term = 0x0;
769 		break;
770 	case POWER_SUPPLY_CHARGE_TYPE_FAST:
771 		chg_config = 0x1;
772 		force_20pct = 0x0;
773 		en_term = 0x1;
774 		break;
775 	default:
776 		return -EINVAL;
777 	}
778 
779 	if (chg_config) { /* Enabling the charger */
780 		ret = bq24190_write_mask(bdi, BQ24190_REG_CCC,
781 				BQ24190_REG_CCC_FORCE_20PCT_MASK,
782 				BQ24190_REG_CCC_FORCE_20PCT_SHIFT,
783 				force_20pct);
784 		if (ret < 0)
785 			return ret;
786 
787 		ret = bq24190_write_mask(bdi, BQ24190_REG_CTTC,
788 				BQ24190_REG_CTTC_EN_TERM_MASK,
789 				BQ24190_REG_CTTC_EN_TERM_SHIFT,
790 				en_term);
791 		if (ret < 0)
792 			return ret;
793 	}
794 
795 	return bq24190_write_mask(bdi, BQ24190_REG_POC,
796 			BQ24190_REG_POC_CHG_CONFIG_MASK,
797 			BQ24190_REG_POC_CHG_CONFIG_SHIFT, chg_config);
798 }
799 
800 static int bq24190_charger_get_health(struct bq24190_dev_info *bdi,
801 		union power_supply_propval *val)
802 {
803 	u8 v;
804 	int health;
805 
806 	mutex_lock(&bdi->f_reg_lock);
807 	v = bdi->f_reg;
808 	mutex_unlock(&bdi->f_reg_lock);
809 
810 	if (v & BQ24190_REG_F_NTC_FAULT_MASK) {
811 		switch (v >> BQ24190_REG_F_NTC_FAULT_SHIFT & 0x7) {
812 		case 0x1: /* TS1  Cold */
813 		case 0x3: /* TS2  Cold */
814 		case 0x5: /* Both Cold */
815 			health = POWER_SUPPLY_HEALTH_COLD;
816 			break;
817 		case 0x2: /* TS1  Hot */
818 		case 0x4: /* TS2  Hot */
819 		case 0x6: /* Both Hot */
820 			health = POWER_SUPPLY_HEALTH_OVERHEAT;
821 			break;
822 		default:
823 			health = POWER_SUPPLY_HEALTH_UNKNOWN;
824 		}
825 	} else if (v & BQ24190_REG_F_BAT_FAULT_MASK) {
826 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
827 	} else if (v & BQ24190_REG_F_CHRG_FAULT_MASK) {
828 		switch (v >> BQ24190_REG_F_CHRG_FAULT_SHIFT & 0x3) {
829 		case 0x1: /* Input Fault (VBUS OVP or VBAT<VBUS<3.8V) */
830 			/*
831 			 * This could be over-voltage or under-voltage
832 			 * and there's no way to tell which.  Instead
833 			 * of looking foolish and returning 'OVERVOLTAGE'
834 			 * when its really under-voltage, just return
835 			 * 'UNSPEC_FAILURE'.
836 			 */
837 			health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
838 			break;
839 		case 0x2: /* Thermal Shutdown */
840 			health = POWER_SUPPLY_HEALTH_OVERHEAT;
841 			break;
842 		case 0x3: /* Charge Safety Timer Expiration */
843 			health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
844 			break;
845 		default:  /* prevent compiler warning */
846 			health = -1;
847 		}
848 	} else if (v & BQ24190_REG_F_BOOST_FAULT_MASK) {
849 		/*
850 		 * This could be over-current or over-voltage but there's
851 		 * no way to tell which.  Return 'OVERVOLTAGE' since there
852 		 * isn't an 'OVERCURRENT' value defined that we can return
853 		 * even if it was over-current.
854 		 */
855 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
856 	} else {
857 		health = POWER_SUPPLY_HEALTH_GOOD;
858 	}
859 
860 	val->intval = health;
861 
862 	return 0;
863 }
864 
865 static int bq24190_charger_get_online(struct bq24190_dev_info *bdi,
866 		union power_supply_propval *val)
867 {
868 	u8 pg_stat, batfet_disable;
869 	int ret;
870 
871 	ret = bq24190_read_mask(bdi, BQ24190_REG_SS,
872 			BQ24190_REG_SS_PG_STAT_MASK,
873 			BQ24190_REG_SS_PG_STAT_SHIFT, &pg_stat);
874 	if (ret < 0)
875 		return ret;
876 
877 	ret = bq24190_read_mask(bdi, BQ24190_REG_MOC,
878 			BQ24190_REG_MOC_BATFET_DISABLE_MASK,
879 			BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable);
880 	if (ret < 0)
881 		return ret;
882 
883 	val->intval = pg_stat && !batfet_disable;
884 
885 	return 0;
886 }
887 
888 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi,
889 				      const union power_supply_propval *val);
890 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi,
891 				      union power_supply_propval *val);
892 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi,
893 					      union power_supply_propval *val);
894 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi,
895 					      const union power_supply_propval *val);
896 
897 static int bq24190_charger_set_online(struct bq24190_dev_info *bdi,
898 				      const union power_supply_propval *val)
899 {
900 	return bq24190_battery_set_online(bdi, val);
901 }
902 
903 static int bq24190_charger_get_status(struct bq24190_dev_info *bdi,
904 				      union power_supply_propval *val)
905 {
906 	return bq24190_battery_get_status(bdi, val);
907 }
908 
909 static int bq24190_charger_get_temp_alert_max(struct bq24190_dev_info *bdi,
910 					      union power_supply_propval *val)
911 {
912 	return bq24190_battery_get_temp_alert_max(bdi, val);
913 }
914 
915 static int bq24190_charger_set_temp_alert_max(struct bq24190_dev_info *bdi,
916 					      const union power_supply_propval *val)
917 {
918 	return bq24190_battery_set_temp_alert_max(bdi, val);
919 }
920 
921 static int bq24190_charger_get_precharge(struct bq24190_dev_info *bdi,
922 		union power_supply_propval *val)
923 {
924 	u8 v;
925 	int ret;
926 
927 	ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC,
928 			BQ24190_REG_PCTCC_IPRECHG_MASK,
929 			BQ24190_REG_PCTCC_IPRECHG_SHIFT, &v);
930 	if (ret < 0)
931 		return ret;
932 
933 	val->intval = ++v * 128 * 1000;
934 	return 0;
935 }
936 
937 static int bq24190_charger_get_charge_term(struct bq24190_dev_info *bdi,
938 		union power_supply_propval *val)
939 {
940 	u8 v;
941 	int ret;
942 
943 	ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC,
944 			BQ24190_REG_PCTCC_ITERM_MASK,
945 			BQ24190_REG_PCTCC_ITERM_SHIFT, &v);
946 	if (ret < 0)
947 		return ret;
948 
949 	val->intval = ++v * 128 * 1000;
950 	return 0;
951 }
952 
953 static int bq24190_charger_get_current(struct bq24190_dev_info *bdi,
954 		union power_supply_propval *val)
955 {
956 	u8 v;
957 	int curr, ret;
958 
959 	ret = bq24190_get_field_val(bdi, BQ24190_REG_CCC,
960 			BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
961 			bq24190_ccc_ichg_values,
962 			ARRAY_SIZE(bq24190_ccc_ichg_values), &curr);
963 	if (ret < 0)
964 		return ret;
965 
966 	ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
967 			BQ24190_REG_CCC_FORCE_20PCT_MASK,
968 			BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
969 	if (ret < 0)
970 		return ret;
971 
972 	/* If FORCE_20PCT is enabled, then current is 20% of ICHG value */
973 	if (v)
974 		curr /= 5;
975 
976 	val->intval = curr;
977 	return 0;
978 }
979 
980 static int bq24190_charger_get_current_max(struct bq24190_dev_info *bdi,
981 		union power_supply_propval *val)
982 {
983 	int idx = ARRAY_SIZE(bq24190_ccc_ichg_values) - 1;
984 
985 	val->intval = bq24190_ccc_ichg_values[idx];
986 	return 0;
987 }
988 
989 static int bq24190_charger_set_current(struct bq24190_dev_info *bdi,
990 		const union power_supply_propval *val)
991 {
992 	u8 v;
993 	int ret, curr = val->intval;
994 
995 	ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
996 			BQ24190_REG_CCC_FORCE_20PCT_MASK,
997 			BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
998 	if (ret < 0)
999 		return ret;
1000 
1001 	/* If FORCE_20PCT is enabled, have to multiply value passed in by 5 */
1002 	if (v)
1003 		curr *= 5;
1004 
1005 	return bq24190_set_field_val(bdi, BQ24190_REG_CCC,
1006 			BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
1007 			bq24190_ccc_ichg_values,
1008 			ARRAY_SIZE(bq24190_ccc_ichg_values), curr);
1009 }
1010 
1011 static int bq24190_charger_get_voltage(struct bq24190_dev_info *bdi,
1012 		union power_supply_propval *val)
1013 {
1014 	int voltage, ret;
1015 
1016 	ret = bq24190_get_field_val(bdi, BQ24190_REG_CVC,
1017 			BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
1018 			bq24190_cvc_vreg_values,
1019 			ARRAY_SIZE(bq24190_cvc_vreg_values), &voltage);
1020 	if (ret < 0)
1021 		return ret;
1022 
1023 	val->intval = voltage;
1024 	return 0;
1025 }
1026 
1027 static int bq24190_charger_get_voltage_max(struct bq24190_dev_info *bdi,
1028 		union power_supply_propval *val)
1029 {
1030 	int idx = ARRAY_SIZE(bq24190_cvc_vreg_values) - 1;
1031 
1032 	val->intval = bq24190_cvc_vreg_values[idx];
1033 	return 0;
1034 }
1035 
1036 static int bq24190_charger_set_voltage(struct bq24190_dev_info *bdi,
1037 		const union power_supply_propval *val)
1038 {
1039 	return bq24190_set_field_val(bdi, BQ24190_REG_CVC,
1040 			BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
1041 			bq24190_cvc_vreg_values,
1042 			ARRAY_SIZE(bq24190_cvc_vreg_values), val->intval);
1043 }
1044 
1045 static int bq24190_charger_get_iinlimit(struct bq24190_dev_info *bdi,
1046 		union power_supply_propval *val)
1047 {
1048 	int iinlimit, ret;
1049 
1050 	ret = bq24190_get_field_val(bdi, BQ24190_REG_ISC,
1051 			BQ24190_REG_ISC_IINLIM_MASK,
1052 			BQ24190_REG_ISC_IINLIM_SHIFT,
1053 			bq24190_isc_iinlim_values,
1054 			ARRAY_SIZE(bq24190_isc_iinlim_values), &iinlimit);
1055 	if (ret < 0)
1056 		return ret;
1057 
1058 	val->intval = iinlimit;
1059 	return 0;
1060 }
1061 
1062 static int bq24190_charger_set_iinlimit(struct bq24190_dev_info *bdi,
1063 		const union power_supply_propval *val)
1064 {
1065 	return bq24190_set_field_val(bdi, BQ24190_REG_ISC,
1066 			BQ24190_REG_ISC_IINLIM_MASK,
1067 			BQ24190_REG_ISC_IINLIM_SHIFT,
1068 			bq24190_isc_iinlim_values,
1069 			ARRAY_SIZE(bq24190_isc_iinlim_values), val->intval);
1070 }
1071 
1072 static int bq24190_charger_get_property(struct power_supply *psy,
1073 		enum power_supply_property psp, union power_supply_propval *val)
1074 {
1075 	struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1076 	int ret;
1077 
1078 	dev_dbg(bdi->dev, "prop: %d\n", psp);
1079 
1080 	ret = pm_runtime_get_sync(bdi->dev);
1081 	if (ret < 0)
1082 		return ret;
1083 
1084 	switch (psp) {
1085 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
1086 		ret = bq24190_charger_get_charge_type(bdi, val);
1087 		break;
1088 	case POWER_SUPPLY_PROP_HEALTH:
1089 		ret = bq24190_charger_get_health(bdi, val);
1090 		break;
1091 	case POWER_SUPPLY_PROP_ONLINE:
1092 		ret = bq24190_charger_get_online(bdi, val);
1093 		break;
1094 	case POWER_SUPPLY_PROP_STATUS:
1095 		ret = bq24190_charger_get_status(bdi, val);
1096 		break;
1097 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1098 		ret =  bq24190_charger_get_temp_alert_max(bdi, val);
1099 		break;
1100 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
1101 		ret = bq24190_charger_get_precharge(bdi, val);
1102 		break;
1103 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
1104 		ret = bq24190_charger_get_charge_term(bdi, val);
1105 		break;
1106 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1107 		ret = bq24190_charger_get_current(bdi, val);
1108 		break;
1109 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
1110 		ret = bq24190_charger_get_current_max(bdi, val);
1111 		break;
1112 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1113 		ret = bq24190_charger_get_voltage(bdi, val);
1114 		break;
1115 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
1116 		ret = bq24190_charger_get_voltage_max(bdi, val);
1117 		break;
1118 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1119 		ret = bq24190_charger_get_iinlimit(bdi, val);
1120 		break;
1121 	case POWER_SUPPLY_PROP_SCOPE:
1122 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
1123 		ret = 0;
1124 		break;
1125 	case POWER_SUPPLY_PROP_MODEL_NAME:
1126 		val->strval = bdi->model_name;
1127 		ret = 0;
1128 		break;
1129 	case POWER_SUPPLY_PROP_MANUFACTURER:
1130 		val->strval = BQ24190_MANUFACTURER;
1131 		ret = 0;
1132 		break;
1133 	default:
1134 		ret = -ENODATA;
1135 	}
1136 
1137 	pm_runtime_mark_last_busy(bdi->dev);
1138 	pm_runtime_put_autosuspend(bdi->dev);
1139 
1140 	return ret;
1141 }
1142 
1143 static int bq24190_charger_set_property(struct power_supply *psy,
1144 		enum power_supply_property psp,
1145 		const union power_supply_propval *val)
1146 {
1147 	struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1148 	int ret;
1149 
1150 	dev_dbg(bdi->dev, "prop: %d\n", psp);
1151 
1152 	ret = pm_runtime_get_sync(bdi->dev);
1153 	if (ret < 0)
1154 		return ret;
1155 
1156 	switch (psp) {
1157 	case POWER_SUPPLY_PROP_ONLINE:
1158 		ret = bq24190_charger_set_online(bdi, val);
1159 		break;
1160 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1161 		ret = bq24190_charger_set_temp_alert_max(bdi, val);
1162 		break;
1163 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
1164 		ret = bq24190_charger_set_charge_type(bdi, val);
1165 		break;
1166 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1167 		ret = bq24190_charger_set_current(bdi, val);
1168 		break;
1169 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1170 		ret = bq24190_charger_set_voltage(bdi, val);
1171 		break;
1172 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1173 		ret = bq24190_charger_set_iinlimit(bdi, val);
1174 		break;
1175 	default:
1176 		ret = -EINVAL;
1177 	}
1178 
1179 	pm_runtime_mark_last_busy(bdi->dev);
1180 	pm_runtime_put_autosuspend(bdi->dev);
1181 
1182 	return ret;
1183 }
1184 
1185 static int bq24190_charger_property_is_writeable(struct power_supply *psy,
1186 		enum power_supply_property psp)
1187 {
1188 	switch (psp) {
1189 	case POWER_SUPPLY_PROP_ONLINE:
1190 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1191 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
1192 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1193 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1194 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1195 		return 1;
1196 	default:
1197 		return 0;
1198 	}
1199 }
1200 
1201 static void bq24190_input_current_limit_work(struct work_struct *work)
1202 {
1203 	struct bq24190_dev_info *bdi =
1204 		container_of(work, struct bq24190_dev_info,
1205 			     input_current_limit_work.work);
1206 
1207 	power_supply_set_input_current_limit_from_supplier(bdi->charger);
1208 }
1209 
1210 /* Sync the input-current-limit with our parent supply (if we have one) */
1211 static void bq24190_charger_external_power_changed(struct power_supply *psy)
1212 {
1213 	struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1214 
1215 	/*
1216 	 * The Power-Good detection may take up to 220ms, sometimes
1217 	 * the external charger detection is quicker, and the bq24190 will
1218 	 * reset to iinlim based on its own charger detection (which is not
1219 	 * hooked up when using external charger detection) resulting in a
1220 	 * too low default 500mA iinlim. Delay setting the input-current-limit
1221 	 * for 300ms to avoid this.
1222 	 */
1223 	queue_delayed_work(system_wq, &bdi->input_current_limit_work,
1224 			   msecs_to_jiffies(300));
1225 }
1226 
1227 static enum power_supply_property bq24190_charger_properties[] = {
1228 	POWER_SUPPLY_PROP_CHARGE_TYPE,
1229 	POWER_SUPPLY_PROP_HEALTH,
1230 	POWER_SUPPLY_PROP_ONLINE,
1231 	POWER_SUPPLY_PROP_STATUS,
1232 	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
1233 	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
1234 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
1235 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1236 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
1237 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1238 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
1239 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1240 	POWER_SUPPLY_PROP_SCOPE,
1241 	POWER_SUPPLY_PROP_MODEL_NAME,
1242 	POWER_SUPPLY_PROP_MANUFACTURER,
1243 };
1244 
1245 static char *bq24190_charger_supplied_to[] = {
1246 	"main-battery",
1247 };
1248 
1249 static const struct power_supply_desc bq24190_charger_desc = {
1250 	.name			= "bq24190-charger",
1251 	.type			= POWER_SUPPLY_TYPE_USB,
1252 	.properties		= bq24190_charger_properties,
1253 	.num_properties		= ARRAY_SIZE(bq24190_charger_properties),
1254 	.get_property		= bq24190_charger_get_property,
1255 	.set_property		= bq24190_charger_set_property,
1256 	.property_is_writeable	= bq24190_charger_property_is_writeable,
1257 	.external_power_changed	= bq24190_charger_external_power_changed,
1258 };
1259 
1260 /* Battery power supply property routines */
1261 
1262 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi,
1263 		union power_supply_propval *val)
1264 {
1265 	u8 ss_reg, chrg_fault;
1266 	int status, ret;
1267 
1268 	mutex_lock(&bdi->f_reg_lock);
1269 	chrg_fault = bdi->f_reg;
1270 	mutex_unlock(&bdi->f_reg_lock);
1271 
1272 	chrg_fault &= BQ24190_REG_F_CHRG_FAULT_MASK;
1273 	chrg_fault >>= BQ24190_REG_F_CHRG_FAULT_SHIFT;
1274 
1275 	ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
1276 	if (ret < 0)
1277 		return ret;
1278 
1279 	/*
1280 	 * The battery must be discharging when any of these are true:
1281 	 * - there is no good power source;
1282 	 * - there is a charge fault.
1283 	 * Could also be discharging when in "supplement mode" but
1284 	 * there is no way to tell when its in that mode.
1285 	 */
1286 	if (!(ss_reg & BQ24190_REG_SS_PG_STAT_MASK) || chrg_fault) {
1287 		status = POWER_SUPPLY_STATUS_DISCHARGING;
1288 	} else {
1289 		ss_reg &= BQ24190_REG_SS_CHRG_STAT_MASK;
1290 		ss_reg >>= BQ24190_REG_SS_CHRG_STAT_SHIFT;
1291 
1292 		switch (ss_reg) {
1293 		case 0x0: /* Not Charging */
1294 			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1295 			break;
1296 		case 0x1: /* Pre-charge */
1297 		case 0x2: /* Fast Charging */
1298 			status = POWER_SUPPLY_STATUS_CHARGING;
1299 			break;
1300 		case 0x3: /* Charge Termination Done */
1301 			status = POWER_SUPPLY_STATUS_FULL;
1302 			break;
1303 		default:
1304 			ret = -EIO;
1305 		}
1306 	}
1307 
1308 	if (!ret)
1309 		val->intval = status;
1310 
1311 	return ret;
1312 }
1313 
1314 static int bq24190_battery_get_health(struct bq24190_dev_info *bdi,
1315 		union power_supply_propval *val)
1316 {
1317 	u8 v;
1318 	int health;
1319 
1320 	mutex_lock(&bdi->f_reg_lock);
1321 	v = bdi->f_reg;
1322 	mutex_unlock(&bdi->f_reg_lock);
1323 
1324 	if (v & BQ24190_REG_F_BAT_FAULT_MASK) {
1325 		health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1326 	} else {
1327 		v &= BQ24190_REG_F_NTC_FAULT_MASK;
1328 		v >>= BQ24190_REG_F_NTC_FAULT_SHIFT;
1329 
1330 		switch (v) {
1331 		case 0x0: /* Normal */
1332 			health = POWER_SUPPLY_HEALTH_GOOD;
1333 			break;
1334 		case 0x1: /* TS1 Cold */
1335 		case 0x3: /* TS2 Cold */
1336 		case 0x5: /* Both Cold */
1337 			health = POWER_SUPPLY_HEALTH_COLD;
1338 			break;
1339 		case 0x2: /* TS1 Hot */
1340 		case 0x4: /* TS2 Hot */
1341 		case 0x6: /* Both Hot */
1342 			health = POWER_SUPPLY_HEALTH_OVERHEAT;
1343 			break;
1344 		default:
1345 			health = POWER_SUPPLY_HEALTH_UNKNOWN;
1346 		}
1347 	}
1348 
1349 	val->intval = health;
1350 	return 0;
1351 }
1352 
1353 static int bq24190_battery_get_online(struct bq24190_dev_info *bdi,
1354 		union power_supply_propval *val)
1355 {
1356 	u8 batfet_disable;
1357 	int ret;
1358 
1359 	ret = bq24190_read_mask(bdi, BQ24190_REG_MOC,
1360 			BQ24190_REG_MOC_BATFET_DISABLE_MASK,
1361 			BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable);
1362 	if (ret < 0)
1363 		return ret;
1364 
1365 	val->intval = !batfet_disable;
1366 	return 0;
1367 }
1368 
1369 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi,
1370 		const union power_supply_propval *val)
1371 {
1372 	return bq24190_write_mask(bdi, BQ24190_REG_MOC,
1373 			BQ24190_REG_MOC_BATFET_DISABLE_MASK,
1374 			BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, !val->intval);
1375 }
1376 
1377 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi,
1378 		union power_supply_propval *val)
1379 {
1380 	int temp, ret;
1381 
1382 	ret = bq24190_get_field_val(bdi, BQ24190_REG_ICTRC,
1383 			BQ24190_REG_ICTRC_TREG_MASK,
1384 			BQ24190_REG_ICTRC_TREG_SHIFT,
1385 			bq24190_ictrc_treg_values,
1386 			ARRAY_SIZE(bq24190_ictrc_treg_values), &temp);
1387 	if (ret < 0)
1388 		return ret;
1389 
1390 	val->intval = temp;
1391 	return 0;
1392 }
1393 
1394 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi,
1395 		const union power_supply_propval *val)
1396 {
1397 	return bq24190_set_field_val(bdi, BQ24190_REG_ICTRC,
1398 			BQ24190_REG_ICTRC_TREG_MASK,
1399 			BQ24190_REG_ICTRC_TREG_SHIFT,
1400 			bq24190_ictrc_treg_values,
1401 			ARRAY_SIZE(bq24190_ictrc_treg_values), val->intval);
1402 }
1403 
1404 static int bq24190_battery_get_property(struct power_supply *psy,
1405 		enum power_supply_property psp, union power_supply_propval *val)
1406 {
1407 	struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1408 	int ret;
1409 
1410 	dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n");
1411 	dev_dbg(bdi->dev, "prop: %d\n", psp);
1412 
1413 	ret = pm_runtime_get_sync(bdi->dev);
1414 	if (ret < 0)
1415 		return ret;
1416 
1417 	switch (psp) {
1418 	case POWER_SUPPLY_PROP_STATUS:
1419 		ret = bq24190_battery_get_status(bdi, val);
1420 		break;
1421 	case POWER_SUPPLY_PROP_HEALTH:
1422 		ret = bq24190_battery_get_health(bdi, val);
1423 		break;
1424 	case POWER_SUPPLY_PROP_ONLINE:
1425 		ret = bq24190_battery_get_online(bdi, val);
1426 		break;
1427 	case POWER_SUPPLY_PROP_TECHNOLOGY:
1428 		/* Could be Li-on or Li-polymer but no way to tell which */
1429 		val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
1430 		ret = 0;
1431 		break;
1432 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1433 		ret = bq24190_battery_get_temp_alert_max(bdi, val);
1434 		break;
1435 	case POWER_SUPPLY_PROP_SCOPE:
1436 		val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
1437 		ret = 0;
1438 		break;
1439 	default:
1440 		ret = -ENODATA;
1441 	}
1442 
1443 	pm_runtime_mark_last_busy(bdi->dev);
1444 	pm_runtime_put_autosuspend(bdi->dev);
1445 
1446 	return ret;
1447 }
1448 
1449 static int bq24190_battery_set_property(struct power_supply *psy,
1450 		enum power_supply_property psp,
1451 		const union power_supply_propval *val)
1452 {
1453 	struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1454 	int ret;
1455 
1456 	dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n");
1457 	dev_dbg(bdi->dev, "prop: %d\n", psp);
1458 
1459 	ret = pm_runtime_get_sync(bdi->dev);
1460 	if (ret < 0)
1461 		return ret;
1462 
1463 	switch (psp) {
1464 	case POWER_SUPPLY_PROP_ONLINE:
1465 		ret = bq24190_battery_set_online(bdi, val);
1466 		break;
1467 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1468 		ret = bq24190_battery_set_temp_alert_max(bdi, val);
1469 		break;
1470 	default:
1471 		ret = -EINVAL;
1472 	}
1473 
1474 	pm_runtime_mark_last_busy(bdi->dev);
1475 	pm_runtime_put_autosuspend(bdi->dev);
1476 
1477 	return ret;
1478 }
1479 
1480 static int bq24190_battery_property_is_writeable(struct power_supply *psy,
1481 		enum power_supply_property psp)
1482 {
1483 	int ret;
1484 
1485 	switch (psp) {
1486 	case POWER_SUPPLY_PROP_ONLINE:
1487 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1488 		ret = 1;
1489 		break;
1490 	default:
1491 		ret = 0;
1492 	}
1493 
1494 	return ret;
1495 }
1496 
1497 static enum power_supply_property bq24190_battery_properties[] = {
1498 	POWER_SUPPLY_PROP_STATUS,
1499 	POWER_SUPPLY_PROP_HEALTH,
1500 	POWER_SUPPLY_PROP_ONLINE,
1501 	POWER_SUPPLY_PROP_TECHNOLOGY,
1502 	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
1503 	POWER_SUPPLY_PROP_SCOPE,
1504 };
1505 
1506 static const struct power_supply_desc bq24190_battery_desc = {
1507 	.name			= "bq24190-battery",
1508 	.type			= POWER_SUPPLY_TYPE_BATTERY,
1509 	.properties		= bq24190_battery_properties,
1510 	.num_properties		= ARRAY_SIZE(bq24190_battery_properties),
1511 	.get_property		= bq24190_battery_get_property,
1512 	.set_property		= bq24190_battery_set_property,
1513 	.property_is_writeable	= bq24190_battery_property_is_writeable,
1514 };
1515 
1516 static int bq24190_configure_usb_otg(struct bq24190_dev_info *bdi, u8 ss_reg)
1517 {
1518 	bool otg_enabled;
1519 	int ret;
1520 
1521 	otg_enabled = !!(ss_reg & BQ24190_REG_SS_VBUS_STAT_MASK);
1522 	ret = extcon_set_state_sync(bdi->edev, EXTCON_USB, otg_enabled);
1523 	if (ret < 0)
1524 		dev_err(bdi->dev, "Can't set extcon state to %d: %d\n",
1525 			otg_enabled, ret);
1526 
1527 	return ret;
1528 }
1529 
1530 static void bq24190_check_status(struct bq24190_dev_info *bdi)
1531 {
1532 	const u8 battery_mask_ss = BQ24190_REG_SS_CHRG_STAT_MASK;
1533 	const u8 battery_mask_f = BQ24190_REG_F_BAT_FAULT_MASK
1534 				| BQ24190_REG_F_NTC_FAULT_MASK;
1535 	bool alert_charger = false, alert_battery = false;
1536 	u8 ss_reg = 0, f_reg = 0;
1537 	int i, ret;
1538 
1539 	ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
1540 	if (ret < 0) {
1541 		dev_err(bdi->dev, "Can't read SS reg: %d\n", ret);
1542 		return;
1543 	}
1544 
1545 	i = 0;
1546 	do {
1547 		ret = bq24190_read(bdi, BQ24190_REG_F, &f_reg);
1548 		if (ret < 0) {
1549 			dev_err(bdi->dev, "Can't read F reg: %d\n", ret);
1550 			return;
1551 		}
1552 	} while (f_reg && ++i < 2);
1553 
1554 	/* ignore over/under voltage fault after disconnect */
1555 	if (f_reg == (1 << BQ24190_REG_F_CHRG_FAULT_SHIFT) &&
1556 	    !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK))
1557 		f_reg = 0;
1558 
1559 	if (f_reg != bdi->f_reg) {
1560 		dev_warn(bdi->dev,
1561 			"Fault: boost %d, charge %d, battery %d, ntc %d\n",
1562 			!!(f_reg & BQ24190_REG_F_BOOST_FAULT_MASK),
1563 			!!(f_reg & BQ24190_REG_F_CHRG_FAULT_MASK),
1564 			!!(f_reg & BQ24190_REG_F_BAT_FAULT_MASK),
1565 			!!(f_reg & BQ24190_REG_F_NTC_FAULT_MASK));
1566 
1567 		mutex_lock(&bdi->f_reg_lock);
1568 		if ((bdi->f_reg & battery_mask_f) != (f_reg & battery_mask_f))
1569 			alert_battery = true;
1570 		if ((bdi->f_reg & ~battery_mask_f) != (f_reg & ~battery_mask_f))
1571 			alert_charger = true;
1572 		bdi->f_reg = f_reg;
1573 		mutex_unlock(&bdi->f_reg_lock);
1574 	}
1575 
1576 	if (ss_reg != bdi->ss_reg) {
1577 		/*
1578 		 * The device is in host mode so when PG_STAT goes from 1->0
1579 		 * (i.e., power removed) HIZ needs to be disabled.
1580 		 */
1581 		if ((bdi->ss_reg & BQ24190_REG_SS_PG_STAT_MASK) &&
1582 				!(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) {
1583 			ret = bq24190_write_mask(bdi, BQ24190_REG_ISC,
1584 					BQ24190_REG_ISC_EN_HIZ_MASK,
1585 					BQ24190_REG_ISC_EN_HIZ_SHIFT,
1586 					0);
1587 			if (ret < 0)
1588 				dev_err(bdi->dev, "Can't access ISC reg: %d\n",
1589 					ret);
1590 		}
1591 
1592 		if ((bdi->ss_reg & battery_mask_ss) != (ss_reg & battery_mask_ss))
1593 			alert_battery = true;
1594 		if ((bdi->ss_reg & ~battery_mask_ss) != (ss_reg & ~battery_mask_ss))
1595 			alert_charger = true;
1596 		bdi->ss_reg = ss_reg;
1597 	}
1598 
1599 	if (alert_charger || alert_battery) {
1600 		power_supply_changed(bdi->charger);
1601 		bq24190_configure_usb_otg(bdi, ss_reg);
1602 	}
1603 	if (alert_battery && bdi->battery)
1604 		power_supply_changed(bdi->battery);
1605 
1606 	dev_dbg(bdi->dev, "ss_reg: 0x%02x, f_reg: 0x%02x\n", ss_reg, f_reg);
1607 }
1608 
1609 static irqreturn_t bq24190_irq_handler_thread(int irq, void *data)
1610 {
1611 	struct bq24190_dev_info *bdi = data;
1612 	int error;
1613 
1614 	bdi->irq_event = true;
1615 	error = pm_runtime_get_sync(bdi->dev);
1616 	if (error < 0) {
1617 		dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1618 		pm_runtime_put_noidle(bdi->dev);
1619 		return IRQ_NONE;
1620 	}
1621 	bq24190_check_status(bdi);
1622 	pm_runtime_mark_last_busy(bdi->dev);
1623 	pm_runtime_put_autosuspend(bdi->dev);
1624 	bdi->irq_event = false;
1625 
1626 	return IRQ_HANDLED;
1627 }
1628 
1629 static int bq24190_hw_init(struct bq24190_dev_info *bdi)
1630 {
1631 	u8 v;
1632 	int ret;
1633 
1634 	/* First check that the device really is what its supposed to be */
1635 	ret = bq24190_read_mask(bdi, BQ24190_REG_VPRS,
1636 			BQ24190_REG_VPRS_PN_MASK,
1637 			BQ24190_REG_VPRS_PN_SHIFT,
1638 			&v);
1639 	if (ret < 0)
1640 		return ret;
1641 
1642 	switch (v) {
1643 	case BQ24190_REG_VPRS_PN_24190:
1644 	case BQ24190_REG_VPRS_PN_24192:
1645 	case BQ24190_REG_VPRS_PN_24192I:
1646 		break;
1647 	default:
1648 		dev_err(bdi->dev, "Error unknown model: 0x%02x\n", v);
1649 		return -ENODEV;
1650 	}
1651 
1652 	ret = bq24190_register_reset(bdi);
1653 	if (ret < 0)
1654 		return ret;
1655 
1656 	ret = bq24190_set_config(bdi);
1657 	if (ret < 0)
1658 		return ret;
1659 
1660 	return bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
1661 }
1662 
1663 static int bq24190_get_config(struct bq24190_dev_info *bdi)
1664 {
1665 	const char * const s = "ti,system-minimum-microvolt";
1666 	struct power_supply_battery_info info = {};
1667 	int v;
1668 
1669 	if (device_property_read_u32(bdi->dev, s, &v) == 0) {
1670 		v /= 1000;
1671 		if (v >= BQ24190_REG_POC_SYS_MIN_MIN
1672 		 && v <= BQ24190_REG_POC_SYS_MIN_MAX)
1673 			bdi->sys_min = v;
1674 		else
1675 			dev_warn(bdi->dev, "invalid value for %s: %u\n", s, v);
1676 	}
1677 
1678 	if (bdi->dev->of_node &&
1679 	    !power_supply_get_battery_info(bdi->charger, &info)) {
1680 		v = info.precharge_current_ua / 1000;
1681 		if (v >= BQ24190_REG_PCTCC_IPRECHG_MIN
1682 		 && v <= BQ24190_REG_PCTCC_IPRECHG_MAX)
1683 			bdi->iprechg = v;
1684 		else
1685 			dev_warn(bdi->dev, "invalid value for battery:precharge-current-microamp: %d\n",
1686 				 v);
1687 
1688 		v = info.charge_term_current_ua / 1000;
1689 		if (v >= BQ24190_REG_PCTCC_ITERM_MIN
1690 		 && v <= BQ24190_REG_PCTCC_ITERM_MAX)
1691 			bdi->iterm = v;
1692 		else
1693 			dev_warn(bdi->dev, "invalid value for battery:charge-term-current-microamp: %d\n",
1694 				 v);
1695 	}
1696 
1697 	return 0;
1698 }
1699 
1700 static int bq24190_probe(struct i2c_client *client,
1701 		const struct i2c_device_id *id)
1702 {
1703 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1704 	struct device *dev = &client->dev;
1705 	struct power_supply_config charger_cfg = {}, battery_cfg = {};
1706 	struct bq24190_dev_info *bdi;
1707 	int ret;
1708 
1709 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1710 		dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1711 		return -ENODEV;
1712 	}
1713 
1714 	bdi = devm_kzalloc(dev, sizeof(*bdi), GFP_KERNEL);
1715 	if (!bdi) {
1716 		dev_err(dev, "Can't alloc bdi struct\n");
1717 		return -ENOMEM;
1718 	}
1719 
1720 	bdi->client = client;
1721 	bdi->dev = dev;
1722 	strncpy(bdi->model_name, id->name, I2C_NAME_SIZE);
1723 	mutex_init(&bdi->f_reg_lock);
1724 	bdi->f_reg = 0;
1725 	bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
1726 	INIT_DELAYED_WORK(&bdi->input_current_limit_work,
1727 			  bq24190_input_current_limit_work);
1728 
1729 	i2c_set_clientdata(client, bdi);
1730 
1731 	if (client->irq <= 0) {
1732 		dev_err(dev, "Can't get irq info\n");
1733 		return -EINVAL;
1734 	}
1735 
1736 	bdi->edev = devm_extcon_dev_allocate(dev, bq24190_usb_extcon_cable);
1737 	if (IS_ERR(bdi->edev))
1738 		return PTR_ERR(bdi->edev);
1739 
1740 	ret = devm_extcon_dev_register(dev, bdi->edev);
1741 	if (ret < 0)
1742 		return ret;
1743 
1744 	pm_runtime_enable(dev);
1745 	pm_runtime_use_autosuspend(dev);
1746 	pm_runtime_set_autosuspend_delay(dev, 600);
1747 	ret = pm_runtime_get_sync(dev);
1748 	if (ret < 0) {
1749 		dev_err(dev, "pm_runtime_get failed: %i\n", ret);
1750 		goto out_pmrt;
1751 	}
1752 
1753 #ifdef CONFIG_SYSFS
1754 	bq24190_sysfs_init_attrs();
1755 	charger_cfg.attr_grp = bq24190_sysfs_groups;
1756 #endif
1757 
1758 	charger_cfg.drv_data = bdi;
1759 	charger_cfg.of_node = dev->of_node;
1760 	charger_cfg.supplied_to = bq24190_charger_supplied_to;
1761 	charger_cfg.num_supplicants = ARRAY_SIZE(bq24190_charger_supplied_to),
1762 	bdi->charger = power_supply_register(dev, &bq24190_charger_desc,
1763 						&charger_cfg);
1764 	if (IS_ERR(bdi->charger)) {
1765 		dev_err(dev, "Can't register charger\n");
1766 		ret = PTR_ERR(bdi->charger);
1767 		goto out_pmrt;
1768 	}
1769 
1770 	/* the battery class is deprecated and will be removed. */
1771 	/* in the interim, this property hides it.              */
1772 	if (!device_property_read_bool(dev, "omit-battery-class")) {
1773 		battery_cfg.drv_data = bdi;
1774 		bdi->battery = power_supply_register(dev, &bq24190_battery_desc,
1775 						     &battery_cfg);
1776 		if (IS_ERR(bdi->battery)) {
1777 			dev_err(dev, "Can't register battery\n");
1778 			ret = PTR_ERR(bdi->battery);
1779 			goto out_charger;
1780 		}
1781 	}
1782 
1783 	ret = bq24190_get_config(bdi);
1784 	if (ret < 0) {
1785 		dev_err(dev, "Can't get devicetree config\n");
1786 		goto out_charger;
1787 	}
1788 
1789 	ret = bq24190_hw_init(bdi);
1790 	if (ret < 0) {
1791 		dev_err(dev, "Hardware init failed\n");
1792 		goto out_charger;
1793 	}
1794 
1795 	ret = bq24190_configure_usb_otg(bdi, bdi->ss_reg);
1796 	if (ret < 0)
1797 		goto out_charger;
1798 
1799 	bdi->initialized = true;
1800 
1801 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
1802 			bq24190_irq_handler_thread,
1803 			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1804 			"bq24190-charger", bdi);
1805 	if (ret < 0) {
1806 		dev_err(dev, "Can't set up irq handler\n");
1807 		goto out_charger;
1808 	}
1809 
1810 	ret = bq24190_register_vbus_regulator(bdi);
1811 	if (ret < 0)
1812 		goto out_charger;
1813 
1814 	enable_irq_wake(client->irq);
1815 
1816 	pm_runtime_mark_last_busy(dev);
1817 	pm_runtime_put_autosuspend(dev);
1818 
1819 	return 0;
1820 
1821 out_charger:
1822 	if (!IS_ERR_OR_NULL(bdi->battery))
1823 		power_supply_unregister(bdi->battery);
1824 	power_supply_unregister(bdi->charger);
1825 
1826 out_pmrt:
1827 	pm_runtime_put_sync(dev);
1828 	pm_runtime_dont_use_autosuspend(dev);
1829 	pm_runtime_disable(dev);
1830 	return ret;
1831 }
1832 
1833 static int bq24190_remove(struct i2c_client *client)
1834 {
1835 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1836 	int error;
1837 
1838 	error = pm_runtime_get_sync(bdi->dev);
1839 	if (error < 0) {
1840 		dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1841 		pm_runtime_put_noidle(bdi->dev);
1842 	}
1843 
1844 	bq24190_register_reset(bdi);
1845 	if (bdi->battery)
1846 		power_supply_unregister(bdi->battery);
1847 	power_supply_unregister(bdi->charger);
1848 	if (error >= 0)
1849 		pm_runtime_put_sync(bdi->dev);
1850 	pm_runtime_dont_use_autosuspend(bdi->dev);
1851 	pm_runtime_disable(bdi->dev);
1852 
1853 	return 0;
1854 }
1855 
1856 static __maybe_unused int bq24190_runtime_suspend(struct device *dev)
1857 {
1858 	struct i2c_client *client = to_i2c_client(dev);
1859 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1860 
1861 	if (!bdi->initialized)
1862 		return 0;
1863 
1864 	dev_dbg(bdi->dev, "%s\n", __func__);
1865 
1866 	return 0;
1867 }
1868 
1869 static __maybe_unused int bq24190_runtime_resume(struct device *dev)
1870 {
1871 	struct i2c_client *client = to_i2c_client(dev);
1872 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1873 
1874 	if (!bdi->initialized)
1875 		return 0;
1876 
1877 	if (!bdi->irq_event) {
1878 		dev_dbg(bdi->dev, "checking events on possible wakeirq\n");
1879 		bq24190_check_status(bdi);
1880 	}
1881 
1882 	return 0;
1883 }
1884 
1885 static __maybe_unused int bq24190_pm_suspend(struct device *dev)
1886 {
1887 	struct i2c_client *client = to_i2c_client(dev);
1888 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1889 	int error;
1890 
1891 	error = pm_runtime_get_sync(bdi->dev);
1892 	if (error < 0) {
1893 		dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1894 		pm_runtime_put_noidle(bdi->dev);
1895 	}
1896 
1897 	bq24190_register_reset(bdi);
1898 
1899 	if (error >= 0) {
1900 		pm_runtime_mark_last_busy(bdi->dev);
1901 		pm_runtime_put_autosuspend(bdi->dev);
1902 	}
1903 
1904 	return 0;
1905 }
1906 
1907 static __maybe_unused int bq24190_pm_resume(struct device *dev)
1908 {
1909 	struct i2c_client *client = to_i2c_client(dev);
1910 	struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1911 	int error;
1912 
1913 	bdi->f_reg = 0;
1914 	bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
1915 
1916 	error = pm_runtime_get_sync(bdi->dev);
1917 	if (error < 0) {
1918 		dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1919 		pm_runtime_put_noidle(bdi->dev);
1920 	}
1921 
1922 	bq24190_register_reset(bdi);
1923 	bq24190_set_config(bdi);
1924 	bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
1925 
1926 	if (error >= 0) {
1927 		pm_runtime_mark_last_busy(bdi->dev);
1928 		pm_runtime_put_autosuspend(bdi->dev);
1929 	}
1930 
1931 	/* Things may have changed while suspended so alert upper layer */
1932 	power_supply_changed(bdi->charger);
1933 	if (bdi->battery)
1934 		power_supply_changed(bdi->battery);
1935 
1936 	return 0;
1937 }
1938 
1939 static const struct dev_pm_ops bq24190_pm_ops = {
1940 	SET_RUNTIME_PM_OPS(bq24190_runtime_suspend, bq24190_runtime_resume,
1941 			   NULL)
1942 	SET_SYSTEM_SLEEP_PM_OPS(bq24190_pm_suspend, bq24190_pm_resume)
1943 };
1944 
1945 static const struct i2c_device_id bq24190_i2c_ids[] = {
1946 	{ "bq24190" },
1947 	{ "bq24192" },
1948 	{ "bq24192i" },
1949 	{ "bq24196" },
1950 	{ },
1951 };
1952 MODULE_DEVICE_TABLE(i2c, bq24190_i2c_ids);
1953 
1954 #ifdef CONFIG_OF
1955 static const struct of_device_id bq24190_of_match[] = {
1956 	{ .compatible = "ti,bq24190", },
1957 	{ .compatible = "ti,bq24192", },
1958 	{ .compatible = "ti,bq24192i", },
1959 	{ .compatible = "ti,bq24196", },
1960 	{ },
1961 };
1962 MODULE_DEVICE_TABLE(of, bq24190_of_match);
1963 #else
1964 static const struct of_device_id bq24190_of_match[] = {
1965 	{ },
1966 };
1967 #endif
1968 
1969 static struct i2c_driver bq24190_driver = {
1970 	.probe		= bq24190_probe,
1971 	.remove		= bq24190_remove,
1972 	.id_table	= bq24190_i2c_ids,
1973 	.driver = {
1974 		.name		= "bq24190-charger",
1975 		.pm		= &bq24190_pm_ops,
1976 		.of_match_table	= of_match_ptr(bq24190_of_match),
1977 	},
1978 };
1979 module_i2c_driver(bq24190_driver);
1980 
1981 MODULE_LICENSE("GPL");
1982 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
1983 MODULE_DESCRIPTION("TI BQ24190 Charger Driver");
1984