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