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