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