1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * TI BQ25890 charger driver
4  *
5  * Copyright (C) 2015 Intel Corporation
6  */
7 
8 #include <linux/module.h>
9 #include <linux/i2c.h>
10 #include <linux/power_supply.h>
11 #include <linux/regmap.h>
12 #include <linux/types.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/usb/phy.h>
17 
18 #include <linux/acpi.h>
19 #include <linux/of.h>
20 
21 #define BQ25890_MANUFACTURER		"Texas Instruments"
22 #define BQ25890_IRQ_PIN			"bq25890_irq"
23 
24 #define BQ25890_ID			3
25 #define BQ25896_ID			0
26 
27 enum bq25890_fields {
28 	F_EN_HIZ, F_EN_ILIM, F_IILIM,				     /* Reg00 */
29 	F_BHOT, F_BCOLD, F_VINDPM_OFS,				     /* Reg01 */
30 	F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
31 	F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN,	     /* Reg02 */
32 	F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,    /* Reg03 */
33 	F_PUMPX_EN, F_ICHG,					     /* Reg04 */
34 	F_IPRECHG, F_ITERM,					     /* Reg05 */
35 	F_VREG, F_BATLOWV, F_VRECHG,				     /* Reg06 */
36 	F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
37 	F_JEITA_ISET,						     /* Reg07 */
38 	F_BATCMP, F_VCLAMP, F_TREG,				     /* Reg08 */
39 	F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
40 	F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN,	     /* Reg09 */
41 	F_BOOSTV, F_BOOSTI,					     /* Reg0A */
42 	F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_VSYS_STAT, /* Reg0B */
43 	F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
44 	F_NTC_FAULT,						     /* Reg0C */
45 	F_FORCE_VINDPM, F_VINDPM,				     /* Reg0D */
46 	F_THERM_STAT, F_BATV,					     /* Reg0E */
47 	F_SYSV,							     /* Reg0F */
48 	F_TSPCT,						     /* Reg10 */
49 	F_VBUS_GD, F_VBUSV,					     /* Reg11 */
50 	F_ICHGR,						     /* Reg12 */
51 	F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM,			     /* Reg13 */
52 	F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV,   /* Reg14 */
53 
54 	F_MAX_FIELDS
55 };
56 
57 /* initial field values, converted to register values */
58 struct bq25890_init_data {
59 	u8 ichg;	/* charge current		*/
60 	u8 vreg;	/* regulation voltage		*/
61 	u8 iterm;	/* termination current		*/
62 	u8 iprechg;	/* precharge current		*/
63 	u8 sysvmin;	/* minimum system voltage limit */
64 	u8 boostv;	/* boost regulation voltage	*/
65 	u8 boosti;	/* boost current limit		*/
66 	u8 boostf;	/* boost frequency		*/
67 	u8 ilim_en;	/* enable ILIM pin		*/
68 	u8 treg;	/* thermal regulation threshold */
69 };
70 
71 struct bq25890_state {
72 	u8 online;
73 	u8 chrg_status;
74 	u8 chrg_fault;
75 	u8 vsys_status;
76 	u8 boost_fault;
77 	u8 bat_fault;
78 };
79 
80 struct bq25890_device {
81 	struct i2c_client *client;
82 	struct device *dev;
83 	struct power_supply *charger;
84 
85 	struct usb_phy *usb_phy;
86 	struct notifier_block usb_nb;
87 	struct work_struct usb_work;
88 	unsigned long usb_event;
89 
90 	struct regmap *rmap;
91 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
92 
93 	int chip_id;
94 	struct bq25890_init_data init_data;
95 	struct bq25890_state state;
96 
97 	struct mutex lock; /* protect state data */
98 };
99 
100 static const struct regmap_range bq25890_readonly_reg_ranges[] = {
101 	regmap_reg_range(0x0b, 0x0c),
102 	regmap_reg_range(0x0e, 0x13),
103 };
104 
105 static const struct regmap_access_table bq25890_writeable_regs = {
106 	.no_ranges = bq25890_readonly_reg_ranges,
107 	.n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
108 };
109 
110 static const struct regmap_range bq25890_volatile_reg_ranges[] = {
111 	regmap_reg_range(0x00, 0x00),
112 	regmap_reg_range(0x09, 0x09),
113 	regmap_reg_range(0x0b, 0x0c),
114 	regmap_reg_range(0x0e, 0x14),
115 };
116 
117 static const struct regmap_access_table bq25890_volatile_regs = {
118 	.yes_ranges = bq25890_volatile_reg_ranges,
119 	.n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
120 };
121 
122 static const struct regmap_config bq25890_regmap_config = {
123 	.reg_bits = 8,
124 	.val_bits = 8,
125 
126 	.max_register = 0x14,
127 	.cache_type = REGCACHE_RBTREE,
128 
129 	.wr_table = &bq25890_writeable_regs,
130 	.volatile_table = &bq25890_volatile_regs,
131 };
132 
133 static const struct reg_field bq25890_reg_fields[] = {
134 	/* REG00 */
135 	[F_EN_HIZ]		= REG_FIELD(0x00, 7, 7),
136 	[F_EN_ILIM]		= REG_FIELD(0x00, 6, 6),
137 	[F_IILIM]		= REG_FIELD(0x00, 0, 5),
138 	/* REG01 */
139 	[F_BHOT]		= REG_FIELD(0x01, 6, 7),
140 	[F_BCOLD]		= REG_FIELD(0x01, 5, 5),
141 	[F_VINDPM_OFS]		= REG_FIELD(0x01, 0, 4),
142 	/* REG02 */
143 	[F_CONV_START]		= REG_FIELD(0x02, 7, 7),
144 	[F_CONV_RATE]		= REG_FIELD(0x02, 6, 6),
145 	[F_BOOSTF]		= REG_FIELD(0x02, 5, 5),
146 	[F_ICO_EN]		= REG_FIELD(0x02, 4, 4),
147 	[F_HVDCP_EN]		= REG_FIELD(0x02, 3, 3),  // reserved on BQ25896
148 	[F_MAXC_EN]		= REG_FIELD(0x02, 2, 2),  // reserved on BQ25896
149 	[F_FORCE_DPM]		= REG_FIELD(0x02, 1, 1),
150 	[F_AUTO_DPDM_EN]	= REG_FIELD(0x02, 0, 0),
151 	/* REG03 */
152 	[F_BAT_LOAD_EN]		= REG_FIELD(0x03, 7, 7),
153 	[F_WD_RST]		= REG_FIELD(0x03, 6, 6),
154 	[F_OTG_CFG]		= REG_FIELD(0x03, 5, 5),
155 	[F_CHG_CFG]		= REG_FIELD(0x03, 4, 4),
156 	[F_SYSVMIN]		= REG_FIELD(0x03, 1, 3),
157 	/* MIN_VBAT_SEL on BQ25896 */
158 	/* REG04 */
159 	[F_PUMPX_EN]		= REG_FIELD(0x04, 7, 7),
160 	[F_ICHG]		= REG_FIELD(0x04, 0, 6),
161 	/* REG05 */
162 	[F_IPRECHG]		= REG_FIELD(0x05, 4, 7),
163 	[F_ITERM]		= REG_FIELD(0x05, 0, 3),
164 	/* REG06 */
165 	[F_VREG]		= REG_FIELD(0x06, 2, 7),
166 	[F_BATLOWV]		= REG_FIELD(0x06, 1, 1),
167 	[F_VRECHG]		= REG_FIELD(0x06, 0, 0),
168 	/* REG07 */
169 	[F_TERM_EN]		= REG_FIELD(0x07, 7, 7),
170 	[F_STAT_DIS]		= REG_FIELD(0x07, 6, 6),
171 	[F_WD]			= REG_FIELD(0x07, 4, 5),
172 	[F_TMR_EN]		= REG_FIELD(0x07, 3, 3),
173 	[F_CHG_TMR]		= REG_FIELD(0x07, 1, 2),
174 	[F_JEITA_ISET]		= REG_FIELD(0x07, 0, 0),
175 	/* REG08 */
176 	[F_BATCMP]		= REG_FIELD(0x08, 5, 7),
177 	[F_VCLAMP]		= REG_FIELD(0x08, 2, 4),
178 	[F_TREG]		= REG_FIELD(0x08, 0, 1),
179 	/* REG09 */
180 	[F_FORCE_ICO]		= REG_FIELD(0x09, 7, 7),
181 	[F_TMR2X_EN]		= REG_FIELD(0x09, 6, 6),
182 	[F_BATFET_DIS]		= REG_FIELD(0x09, 5, 5),
183 	[F_JEITA_VSET]		= REG_FIELD(0x09, 4, 4),
184 	[F_BATFET_DLY]		= REG_FIELD(0x09, 3, 3),
185 	[F_BATFET_RST_EN]	= REG_FIELD(0x09, 2, 2),
186 	[F_PUMPX_UP]		= REG_FIELD(0x09, 1, 1),
187 	[F_PUMPX_DN]		= REG_FIELD(0x09, 0, 0),
188 	/* REG0A */
189 	[F_BOOSTV]		= REG_FIELD(0x0A, 4, 7),
190 	/* PFM_OTG_DIS 3 on BQ25896 */
191 	[F_BOOSTI]		= REG_FIELD(0x0A, 0, 2),
192 	/* REG0B */
193 	[F_VBUS_STAT]		= REG_FIELD(0x0B, 5, 7),
194 	[F_CHG_STAT]		= REG_FIELD(0x0B, 3, 4),
195 	[F_PG_STAT]		= REG_FIELD(0x0B, 2, 2),
196 	[F_SDP_STAT]		= REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
197 	[F_VSYS_STAT]		= REG_FIELD(0x0B, 0, 0),
198 	/* REG0C */
199 	[F_WD_FAULT]		= REG_FIELD(0x0C, 7, 7),
200 	[F_BOOST_FAULT]		= REG_FIELD(0x0C, 6, 6),
201 	[F_CHG_FAULT]		= REG_FIELD(0x0C, 4, 5),
202 	[F_BAT_FAULT]		= REG_FIELD(0x0C, 3, 3),
203 	[F_NTC_FAULT]		= REG_FIELD(0x0C, 0, 2),
204 	/* REG0D */
205 	[F_FORCE_VINDPM]	= REG_FIELD(0x0D, 7, 7),
206 	[F_VINDPM]		= REG_FIELD(0x0D, 0, 6),
207 	/* REG0E */
208 	[F_THERM_STAT]		= REG_FIELD(0x0E, 7, 7),
209 	[F_BATV]		= REG_FIELD(0x0E, 0, 6),
210 	/* REG0F */
211 	[F_SYSV]		= REG_FIELD(0x0F, 0, 6),
212 	/* REG10 */
213 	[F_TSPCT]		= REG_FIELD(0x10, 0, 6),
214 	/* REG11 */
215 	[F_VBUS_GD]		= REG_FIELD(0x11, 7, 7),
216 	[F_VBUSV]		= REG_FIELD(0x11, 0, 6),
217 	/* REG12 */
218 	[F_ICHGR]		= REG_FIELD(0x12, 0, 6),
219 	/* REG13 */
220 	[F_VDPM_STAT]		= REG_FIELD(0x13, 7, 7),
221 	[F_IDPM_STAT]		= REG_FIELD(0x13, 6, 6),
222 	[F_IDPM_LIM]		= REG_FIELD(0x13, 0, 5),
223 	/* REG14 */
224 	[F_REG_RST]		= REG_FIELD(0x14, 7, 7),
225 	[F_ICO_OPTIMIZED]	= REG_FIELD(0x14, 6, 6),
226 	[F_PN]			= REG_FIELD(0x14, 3, 5),
227 	[F_TS_PROFILE]		= REG_FIELD(0x14, 2, 2),
228 	[F_DEV_REV]		= REG_FIELD(0x14, 0, 1)
229 };
230 
231 /*
232  * Most of the val -> idx conversions can be computed, given the minimum,
233  * maximum and the step between values. For the rest of conversions, we use
234  * lookup tables.
235  */
236 enum bq25890_table_ids {
237 	/* range tables */
238 	TBL_ICHG,
239 	TBL_ITERM,
240 	TBL_VREG,
241 	TBL_BOOSTV,
242 	TBL_SYSVMIN,
243 
244 	/* lookup tables */
245 	TBL_TREG,
246 	TBL_BOOSTI,
247 };
248 
249 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
250 static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
251 
252 #define BQ25890_TREG_TBL_SIZE		ARRAY_SIZE(bq25890_treg_tbl)
253 
254 /* Boost mode current limit lookup table, in uA */
255 static const u32 bq25890_boosti_tbl[] = {
256 	500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
257 };
258 
259 #define BQ25890_BOOSTI_TBL_SIZE		ARRAY_SIZE(bq25890_boosti_tbl)
260 
261 struct bq25890_range {
262 	u32 min;
263 	u32 max;
264 	u32 step;
265 };
266 
267 struct bq25890_lookup {
268 	const u32 *tbl;
269 	u32 size;
270 };
271 
272 static const union {
273 	struct bq25890_range  rt;
274 	struct bq25890_lookup lt;
275 } bq25890_tables[] = {
276 	/* range tables */
277 	[TBL_ICHG] =	{ .rt = {0,	  5056000, 64000} },	 /* uA */
278 	[TBL_ITERM] =	{ .rt = {64000,   1024000, 64000} },	 /* uA */
279 	[TBL_VREG] =	{ .rt = {3840000, 4608000, 16000} },	 /* uV */
280 	[TBL_BOOSTV] =	{ .rt = {4550000, 5510000, 64000} },	 /* uV */
281 	[TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} },	 /* uV */
282 
283 	/* lookup tables */
284 	[TBL_TREG] =	{ .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
285 	[TBL_BOOSTI] =	{ .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
286 };
287 
288 static int bq25890_field_read(struct bq25890_device *bq,
289 			      enum bq25890_fields field_id)
290 {
291 	int ret;
292 	int val;
293 
294 	ret = regmap_field_read(bq->rmap_fields[field_id], &val);
295 	if (ret < 0)
296 		return ret;
297 
298 	return val;
299 }
300 
301 static int bq25890_field_write(struct bq25890_device *bq,
302 			       enum bq25890_fields field_id, u8 val)
303 {
304 	return regmap_field_write(bq->rmap_fields[field_id], val);
305 }
306 
307 static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
308 {
309 	u8 idx;
310 
311 	if (id >= TBL_TREG) {
312 		const u32 *tbl = bq25890_tables[id].lt.tbl;
313 		u32 tbl_size = bq25890_tables[id].lt.size;
314 
315 		for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
316 			;
317 	} else {
318 		const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
319 		u8 rtbl_size;
320 
321 		rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
322 
323 		for (idx = 1;
324 		     idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
325 		     idx++)
326 			;
327 	}
328 
329 	return idx - 1;
330 }
331 
332 static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
333 {
334 	const struct bq25890_range *rtbl;
335 
336 	/* lookup table? */
337 	if (id >= TBL_TREG)
338 		return bq25890_tables[id].lt.tbl[idx];
339 
340 	/* range table */
341 	rtbl = &bq25890_tables[id].rt;
342 
343 	return (rtbl->min + idx * rtbl->step);
344 }
345 
346 enum bq25890_status {
347 	STATUS_NOT_CHARGING,
348 	STATUS_PRE_CHARGING,
349 	STATUS_FAST_CHARGING,
350 	STATUS_TERMINATION_DONE,
351 };
352 
353 enum bq25890_chrg_fault {
354 	CHRG_FAULT_NORMAL,
355 	CHRG_FAULT_INPUT,
356 	CHRG_FAULT_THERMAL_SHUTDOWN,
357 	CHRG_FAULT_TIMER_EXPIRED,
358 };
359 
360 static int bq25890_power_supply_get_property(struct power_supply *psy,
361 					     enum power_supply_property psp,
362 					     union power_supply_propval *val)
363 {
364 	int ret;
365 	struct bq25890_device *bq = power_supply_get_drvdata(psy);
366 	struct bq25890_state state;
367 
368 	mutex_lock(&bq->lock);
369 	state = bq->state;
370 	mutex_unlock(&bq->lock);
371 
372 	switch (psp) {
373 	case POWER_SUPPLY_PROP_STATUS:
374 		if (!state.online)
375 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
376 		else if (state.chrg_status == STATUS_NOT_CHARGING)
377 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
378 		else if (state.chrg_status == STATUS_PRE_CHARGING ||
379 			 state.chrg_status == STATUS_FAST_CHARGING)
380 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
381 		else if (state.chrg_status == STATUS_TERMINATION_DONE)
382 			val->intval = POWER_SUPPLY_STATUS_FULL;
383 		else
384 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
385 
386 		break;
387 
388 	case POWER_SUPPLY_PROP_MANUFACTURER:
389 		val->strval = BQ25890_MANUFACTURER;
390 		break;
391 
392 	case POWER_SUPPLY_PROP_MODEL_NAME:
393 		if (bq->chip_id == BQ25890_ID)
394 			val->strval = "BQ25890";
395 		else if (bq->chip_id == BQ25896_ID)
396 			val->strval = "BQ25896";
397 		else
398 			val->strval = "UNKNOWN";
399 
400 		break;
401 
402 	case POWER_SUPPLY_PROP_ONLINE:
403 		val->intval = state.online;
404 		break;
405 
406 	case POWER_SUPPLY_PROP_HEALTH:
407 		if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
408 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
409 		else if (state.bat_fault)
410 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
411 		else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
412 			val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
413 		else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
414 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
415 		else
416 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
417 		break;
418 
419 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
420 		ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
421 		if (ret < 0)
422 			return ret;
423 
424 		/* converted_val = ADC_val * 50mA (table 10.3.19) */
425 		val->intval = ret * 50000;
426 		break;
427 
428 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
429 		val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
430 		break;
431 
432 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
433 		if (!state.online) {
434 			val->intval = 0;
435 			break;
436 		}
437 
438 		ret = bq25890_field_read(bq, F_BATV); /* read measured value */
439 		if (ret < 0)
440 			return ret;
441 
442 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
443 		val->intval = 2304000 + ret * 20000;
444 		break;
445 
446 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
447 		val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
448 		break;
449 
450 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
451 		val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
452 		break;
453 
454 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
455 		ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
456 		if (ret < 0)
457 			return ret;
458 
459 		/* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
460 		val->intval = 2304000 + ret * 20000;
461 		break;
462 
463 	default:
464 		return -EINVAL;
465 	}
466 
467 	return 0;
468 }
469 
470 static int bq25890_get_chip_state(struct bq25890_device *bq,
471 				  struct bq25890_state *state)
472 {
473 	int i, ret;
474 
475 	struct {
476 		enum bq25890_fields id;
477 		u8 *data;
478 	} state_fields[] = {
479 		{F_CHG_STAT,	&state->chrg_status},
480 		{F_PG_STAT,	&state->online},
481 		{F_VSYS_STAT,	&state->vsys_status},
482 		{F_BOOST_FAULT, &state->boost_fault},
483 		{F_BAT_FAULT,	&state->bat_fault},
484 		{F_CHG_FAULT,	&state->chrg_fault}
485 	};
486 
487 	for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
488 		ret = bq25890_field_read(bq, state_fields[i].id);
489 		if (ret < 0)
490 			return ret;
491 
492 		*state_fields[i].data = ret;
493 	}
494 
495 	dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
496 		state->chrg_status, state->online, state->vsys_status,
497 		state->chrg_fault, state->boost_fault, state->bat_fault);
498 
499 	return 0;
500 }
501 
502 static bool bq25890_state_changed(struct bq25890_device *bq,
503 				  struct bq25890_state *new_state)
504 {
505 	struct bq25890_state old_state;
506 
507 	mutex_lock(&bq->lock);
508 	old_state = bq->state;
509 	mutex_unlock(&bq->lock);
510 
511 	return (old_state.chrg_status != new_state->chrg_status ||
512 		old_state.chrg_fault != new_state->chrg_fault	||
513 		old_state.online != new_state->online		||
514 		old_state.bat_fault != new_state->bat_fault	||
515 		old_state.boost_fault != new_state->boost_fault ||
516 		old_state.vsys_status != new_state->vsys_status);
517 }
518 
519 static void bq25890_handle_state_change(struct bq25890_device *bq,
520 					struct bq25890_state *new_state)
521 {
522 	int ret;
523 	struct bq25890_state old_state;
524 
525 	mutex_lock(&bq->lock);
526 	old_state = bq->state;
527 	mutex_unlock(&bq->lock);
528 
529 	if (!new_state->online) {			     /* power removed */
530 		/* disable ADC */
531 		ret = bq25890_field_write(bq, F_CONV_START, 0);
532 		if (ret < 0)
533 			goto error;
534 	} else if (!old_state.online) {			    /* power inserted */
535 		/* enable ADC, to have control of charge current/voltage */
536 		ret = bq25890_field_write(bq, F_CONV_START, 1);
537 		if (ret < 0)
538 			goto error;
539 	}
540 
541 	return;
542 
543 error:
544 	dev_err(bq->dev, "Error communicating with the chip.\n");
545 }
546 
547 static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
548 {
549 	struct bq25890_device *bq = private;
550 	int ret;
551 	struct bq25890_state state;
552 
553 	ret = bq25890_get_chip_state(bq, &state);
554 	if (ret < 0)
555 		goto handled;
556 
557 	if (!bq25890_state_changed(bq, &state))
558 		goto handled;
559 
560 	bq25890_handle_state_change(bq, &state);
561 
562 	mutex_lock(&bq->lock);
563 	bq->state = state;
564 	mutex_unlock(&bq->lock);
565 
566 	power_supply_changed(bq->charger);
567 
568 handled:
569 	return IRQ_HANDLED;
570 }
571 
572 static int bq25890_chip_reset(struct bq25890_device *bq)
573 {
574 	int ret;
575 	int rst_check_counter = 10;
576 
577 	ret = bq25890_field_write(bq, F_REG_RST, 1);
578 	if (ret < 0)
579 		return ret;
580 
581 	do {
582 		ret = bq25890_field_read(bq, F_REG_RST);
583 		if (ret < 0)
584 			return ret;
585 
586 		usleep_range(5, 10);
587 	} while (ret == 1 && --rst_check_counter);
588 
589 	if (!rst_check_counter)
590 		return -ETIMEDOUT;
591 
592 	return 0;
593 }
594 
595 static int bq25890_hw_init(struct bq25890_device *bq)
596 {
597 	int ret;
598 	int i;
599 	struct bq25890_state state;
600 
601 	const struct {
602 		enum bq25890_fields id;
603 		u32 value;
604 	} init_data[] = {
605 		{F_ICHG,	 bq->init_data.ichg},
606 		{F_VREG,	 bq->init_data.vreg},
607 		{F_ITERM,	 bq->init_data.iterm},
608 		{F_IPRECHG,	 bq->init_data.iprechg},
609 		{F_SYSVMIN,	 bq->init_data.sysvmin},
610 		{F_BOOSTV,	 bq->init_data.boostv},
611 		{F_BOOSTI,	 bq->init_data.boosti},
612 		{F_BOOSTF,	 bq->init_data.boostf},
613 		{F_EN_ILIM,	 bq->init_data.ilim_en},
614 		{F_TREG,	 bq->init_data.treg}
615 	};
616 
617 	ret = bq25890_chip_reset(bq);
618 	if (ret < 0) {
619 		dev_dbg(bq->dev, "Reset failed %d\n", ret);
620 		return ret;
621 	}
622 
623 	/* disable watchdog */
624 	ret = bq25890_field_write(bq, F_WD, 0);
625 	if (ret < 0) {
626 		dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
627 		return ret;
628 	}
629 
630 	/* initialize currents/voltages and other parameters */
631 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
632 		ret = bq25890_field_write(bq, init_data[i].id,
633 					  init_data[i].value);
634 		if (ret < 0) {
635 			dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
636 			return ret;
637 		}
638 	}
639 
640 	/* Configure ADC for continuous conversions. This does not enable it. */
641 	ret = bq25890_field_write(bq, F_CONV_RATE, 1);
642 	if (ret < 0) {
643 		dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
644 		return ret;
645 	}
646 
647 	ret = bq25890_get_chip_state(bq, &state);
648 	if (ret < 0) {
649 		dev_dbg(bq->dev, "Get state failed %d\n", ret);
650 		return ret;
651 	}
652 
653 	mutex_lock(&bq->lock);
654 	bq->state = state;
655 	mutex_unlock(&bq->lock);
656 
657 	return 0;
658 }
659 
660 static enum power_supply_property bq25890_power_supply_props[] = {
661 	POWER_SUPPLY_PROP_MANUFACTURER,
662 	POWER_SUPPLY_PROP_MODEL_NAME,
663 	POWER_SUPPLY_PROP_STATUS,
664 	POWER_SUPPLY_PROP_ONLINE,
665 	POWER_SUPPLY_PROP_HEALTH,
666 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
667 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
668 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
669 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
670 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
671 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
672 };
673 
674 static char *bq25890_charger_supplied_to[] = {
675 	"main-battery",
676 };
677 
678 static const struct power_supply_desc bq25890_power_supply_desc = {
679 	.name = "bq25890-charger",
680 	.type = POWER_SUPPLY_TYPE_USB,
681 	.properties = bq25890_power_supply_props,
682 	.num_properties = ARRAY_SIZE(bq25890_power_supply_props),
683 	.get_property = bq25890_power_supply_get_property,
684 };
685 
686 static int bq25890_power_supply_init(struct bq25890_device *bq)
687 {
688 	struct power_supply_config psy_cfg = { .drv_data = bq, };
689 
690 	psy_cfg.supplied_to = bq25890_charger_supplied_to;
691 	psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
692 
693 	bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc,
694 					    &psy_cfg);
695 
696 	return PTR_ERR_OR_ZERO(bq->charger);
697 }
698 
699 static void bq25890_usb_work(struct work_struct *data)
700 {
701 	int ret;
702 	struct bq25890_device *bq =
703 			container_of(data, struct bq25890_device, usb_work);
704 
705 	switch (bq->usb_event) {
706 	case USB_EVENT_ID:
707 		/* Enable boost mode */
708 		ret = bq25890_field_write(bq, F_OTG_CFG, 1);
709 		if (ret < 0)
710 			goto error;
711 		break;
712 
713 	case USB_EVENT_NONE:
714 		/* Disable boost mode */
715 		ret = bq25890_field_write(bq, F_OTG_CFG, 0);
716 		if (ret < 0)
717 			goto error;
718 
719 		power_supply_changed(bq->charger);
720 		break;
721 	}
722 
723 	return;
724 
725 error:
726 	dev_err(bq->dev, "Error switching to boost/charger mode.\n");
727 }
728 
729 static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
730 				void *priv)
731 {
732 	struct bq25890_device *bq =
733 			container_of(nb, struct bq25890_device, usb_nb);
734 
735 	bq->usb_event = val;
736 	queue_work(system_power_efficient_wq, &bq->usb_work);
737 
738 	return NOTIFY_OK;
739 }
740 
741 static int bq25890_irq_probe(struct bq25890_device *bq)
742 {
743 	struct gpio_desc *irq;
744 
745 	irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
746 	if (IS_ERR(irq)) {
747 		dev_err(bq->dev, "Could not probe irq pin.\n");
748 		return PTR_ERR(irq);
749 	}
750 
751 	return gpiod_to_irq(irq);
752 }
753 
754 static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
755 {
756 	int ret;
757 	u32 property;
758 	int i;
759 	struct bq25890_init_data *init = &bq->init_data;
760 	struct {
761 		char *name;
762 		bool optional;
763 		enum bq25890_table_ids tbl_id;
764 		u8 *conv_data; /* holds converted value from given property */
765 	} props[] = {
766 		/* required properties */
767 		{"ti,charge-current", false, TBL_ICHG, &init->ichg},
768 		{"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
769 		{"ti,termination-current", false, TBL_ITERM, &init->iterm},
770 		{"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
771 		{"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
772 		{"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
773 		{"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
774 
775 		/* optional properties */
776 		{"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg}
777 	};
778 
779 	/* initialize data for optional properties */
780 	init->treg = 3; /* 120 degrees Celsius */
781 
782 	for (i = 0; i < ARRAY_SIZE(props); i++) {
783 		ret = device_property_read_u32(bq->dev, props[i].name,
784 					       &property);
785 		if (ret < 0) {
786 			if (props[i].optional)
787 				continue;
788 
789 			dev_err(bq->dev, "Unable to read property %d %s\n", ret,
790 				props[i].name);
791 
792 			return ret;
793 		}
794 
795 		*props[i].conv_data = bq25890_find_idx(property,
796 						       props[i].tbl_id);
797 	}
798 
799 	return 0;
800 }
801 
802 static int bq25890_fw_probe(struct bq25890_device *bq)
803 {
804 	int ret;
805 	struct bq25890_init_data *init = &bq->init_data;
806 
807 	ret = bq25890_fw_read_u32_props(bq);
808 	if (ret < 0)
809 		return ret;
810 
811 	init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
812 	init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
813 
814 	return 0;
815 }
816 
817 static int bq25890_probe(struct i2c_client *client,
818 			 const struct i2c_device_id *id)
819 {
820 	struct i2c_adapter *adapter = client->adapter;
821 	struct device *dev = &client->dev;
822 	struct bq25890_device *bq;
823 	int ret;
824 	int i;
825 
826 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
827 		dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
828 		return -ENODEV;
829 	}
830 
831 	bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
832 	if (!bq)
833 		return -ENOMEM;
834 
835 	bq->client = client;
836 	bq->dev = dev;
837 
838 	mutex_init(&bq->lock);
839 
840 	bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
841 	if (IS_ERR(bq->rmap)) {
842 		dev_err(dev, "failed to allocate register map\n");
843 		return PTR_ERR(bq->rmap);
844 	}
845 
846 	for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
847 		const struct reg_field *reg_fields = bq25890_reg_fields;
848 
849 		bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
850 							     reg_fields[i]);
851 		if (IS_ERR(bq->rmap_fields[i])) {
852 			dev_err(dev, "cannot allocate regmap field\n");
853 			return PTR_ERR(bq->rmap_fields[i]);
854 		}
855 	}
856 
857 	i2c_set_clientdata(client, bq);
858 
859 	bq->chip_id = bq25890_field_read(bq, F_PN);
860 	if (bq->chip_id < 0) {
861 		dev_err(dev, "Cannot read chip ID.\n");
862 		return bq->chip_id;
863 	}
864 
865 	if ((bq->chip_id != BQ25890_ID) && (bq->chip_id != BQ25896_ID)) {
866 		dev_err(dev, "Chip with ID=%d, not supported!\n", bq->chip_id);
867 		return -ENODEV;
868 	}
869 
870 	if (!dev->platform_data) {
871 		ret = bq25890_fw_probe(bq);
872 		if (ret < 0) {
873 			dev_err(dev, "Cannot read device properties.\n");
874 			return ret;
875 		}
876 	} else {
877 		return -ENODEV;
878 	}
879 
880 	ret = bq25890_hw_init(bq);
881 	if (ret < 0) {
882 		dev_err(dev, "Cannot initialize the chip.\n");
883 		return ret;
884 	}
885 
886 	if (client->irq <= 0)
887 		client->irq = bq25890_irq_probe(bq);
888 
889 	if (client->irq < 0) {
890 		dev_err(dev, "No irq resource found.\n");
891 		return client->irq;
892 	}
893 
894 	/* OTG reporting */
895 	bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
896 	if (!IS_ERR_OR_NULL(bq->usb_phy)) {
897 		INIT_WORK(&bq->usb_work, bq25890_usb_work);
898 		bq->usb_nb.notifier_call = bq25890_usb_notifier;
899 		usb_register_notifier(bq->usb_phy, &bq->usb_nb);
900 	}
901 
902 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
903 					bq25890_irq_handler_thread,
904 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
905 					BQ25890_IRQ_PIN, bq);
906 	if (ret)
907 		goto irq_fail;
908 
909 	ret = bq25890_power_supply_init(bq);
910 	if (ret < 0) {
911 		dev_err(dev, "Failed to register power supply\n");
912 		goto irq_fail;
913 	}
914 
915 	return 0;
916 
917 irq_fail:
918 	if (!IS_ERR_OR_NULL(bq->usb_phy))
919 		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
920 
921 	return ret;
922 }
923 
924 static int bq25890_remove(struct i2c_client *client)
925 {
926 	struct bq25890_device *bq = i2c_get_clientdata(client);
927 
928 	power_supply_unregister(bq->charger);
929 
930 	if (!IS_ERR_OR_NULL(bq->usb_phy))
931 		usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
932 
933 	/* reset all registers to default values */
934 	bq25890_chip_reset(bq);
935 
936 	return 0;
937 }
938 
939 #ifdef CONFIG_PM_SLEEP
940 static int bq25890_suspend(struct device *dev)
941 {
942 	struct bq25890_device *bq = dev_get_drvdata(dev);
943 
944 	/*
945 	 * If charger is removed, while in suspend, make sure ADC is diabled
946 	 * since it consumes slightly more power.
947 	 */
948 	return bq25890_field_write(bq, F_CONV_START, 0);
949 }
950 
951 static int bq25890_resume(struct device *dev)
952 {
953 	int ret;
954 	struct bq25890_state state;
955 	struct bq25890_device *bq = dev_get_drvdata(dev);
956 
957 	ret = bq25890_get_chip_state(bq, &state);
958 	if (ret < 0)
959 		return ret;
960 
961 	mutex_lock(&bq->lock);
962 	bq->state = state;
963 	mutex_unlock(&bq->lock);
964 
965 	/* Re-enable ADC only if charger is plugged in. */
966 	if (state.online) {
967 		ret = bq25890_field_write(bq, F_CONV_START, 1);
968 		if (ret < 0)
969 			return ret;
970 	}
971 
972 	/* signal userspace, maybe state changed while suspended */
973 	power_supply_changed(bq->charger);
974 
975 	return 0;
976 }
977 #endif
978 
979 static const struct dev_pm_ops bq25890_pm = {
980 	SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
981 };
982 
983 static const struct i2c_device_id bq25890_i2c_ids[] = {
984 	{ "bq25890", 0 },
985 	{},
986 };
987 MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
988 
989 static const struct of_device_id bq25890_of_match[] = {
990 	{ .compatible = "ti,bq25890", },
991 	{ },
992 };
993 MODULE_DEVICE_TABLE(of, bq25890_of_match);
994 
995 static const struct acpi_device_id bq25890_acpi_match[] = {
996 	{"BQ258900", 0},
997 	{},
998 };
999 MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1000 
1001 static struct i2c_driver bq25890_driver = {
1002 	.driver = {
1003 		.name = "bq25890-charger",
1004 		.of_match_table = of_match_ptr(bq25890_of_match),
1005 		.acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1006 		.pm = &bq25890_pm,
1007 	},
1008 	.probe = bq25890_probe,
1009 	.remove = bq25890_remove,
1010 	.id_table = bq25890_i2c_ids,
1011 };
1012 module_i2c_driver(bq25890_driver);
1013 
1014 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1015 MODULE_DESCRIPTION("bq25890 charger driver");
1016 MODULE_LICENSE("GPL");
1017