xref: /linux/drivers/power/supply/bq24257_charger.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * TI BQ24257 charger driver
4  *
5  * Copyright (C) 2015 Intel Corporation
6  *
7  * Datasheets:
8  * https://www.ti.com/product/bq24250
9  * https://www.ti.com/product/bq24251
10  * https://www.ti.com/product/bq24257
11  */
12 
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/power_supply.h>
16 #include <linux/regmap.h>
17 #include <linux/types.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 
22 #include <linux/acpi.h>
23 #include <linux/of.h>
24 
25 #define BQ24257_REG_1			0x00
26 #define BQ24257_REG_2			0x01
27 #define BQ24257_REG_3			0x02
28 #define BQ24257_REG_4			0x03
29 #define BQ24257_REG_5			0x04
30 #define BQ24257_REG_6			0x05
31 #define BQ24257_REG_7			0x06
32 
33 #define BQ24257_MANUFACTURER		"Texas Instruments"
34 #define BQ24257_PG_GPIO			"pg"
35 
36 #define BQ24257_ILIM_SET_DELAY		1000	/* msec */
37 
38 enum bq2425x_chip {
39 	BQ24250,
40 	BQ24251,
41 	BQ24257,
42 };
43 
44 struct bq2425x_chip_info {
45 	const char *const name;
46 	enum bq2425x_chip chip;
47 };
48 
49 enum bq24257_fields {
50 	F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT,			    /* REG 1 */
51 	F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE,  /* REG 2 */
52 	F_VBAT, F_USB_DET,					    /* REG 3 */
53 	F_ICHG, F_ITERM,					    /* REG 4 */
54 	F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
55 	F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_EN, F_TS_STAT,	    /* REG 6 */
56 	F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM,		    /* REG 7 */
57 
58 	F_MAX_FIELDS
59 };
60 
61 /* initial field values, converted from uV/uA */
62 struct bq24257_init_data {
63 	u8 ichg;	/* charge current      */
64 	u8 vbat;	/* regulation voltage  */
65 	u8 iterm;	/* termination current */
66 	u8 iilimit;	/* input current limit */
67 	u8 vovp;	/* over voltage protection voltage */
68 	u8 vindpm;	/* VDMP input threshold voltage */
69 };
70 
71 struct bq24257_state {
72 	u8 status;
73 	u8 fault;
74 	bool power_good;
75 };
76 
77 struct bq24257_device {
78 	struct i2c_client *client;
79 	struct device *dev;
80 	struct power_supply *charger;
81 
82 	const struct bq2425x_chip_info *info;
83 
84 	struct regmap *rmap;
85 	struct regmap_field *rmap_fields[F_MAX_FIELDS];
86 
87 	struct gpio_desc *pg;
88 
89 	struct delayed_work iilimit_setup_work;
90 
91 	struct bq24257_init_data init_data;
92 	struct bq24257_state state;
93 
94 	struct mutex lock; /* protect state data */
95 
96 	bool iilimit_autoset_enable;
97 };
98 
99 static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
100 {
101 	switch (reg) {
102 	case BQ24257_REG_2:
103 	case BQ24257_REG_4:
104 		return false;
105 
106 	default:
107 		return true;
108 	}
109 }
110 
111 static const struct regmap_config bq24257_regmap_config = {
112 	.reg_bits = 8,
113 	.val_bits = 8,
114 
115 	.max_register = BQ24257_REG_7,
116 	.cache_type = REGCACHE_RBTREE,
117 
118 	.volatile_reg = bq24257_is_volatile_reg,
119 };
120 
121 static const struct reg_field bq24257_reg_fields[] = {
122 	/* REG 1 */
123 	[F_WD_FAULT]		= REG_FIELD(BQ24257_REG_1, 7, 7),
124 	[F_WD_EN]		= REG_FIELD(BQ24257_REG_1, 6, 6),
125 	[F_STAT]		= REG_FIELD(BQ24257_REG_1, 4, 5),
126 	[F_FAULT]		= REG_FIELD(BQ24257_REG_1, 0, 3),
127 	/* REG 2 */
128 	[F_RESET]		= REG_FIELD(BQ24257_REG_2, 7, 7),
129 	[F_IILIMIT]		= REG_FIELD(BQ24257_REG_2, 4, 6),
130 	[F_EN_STAT]		= REG_FIELD(BQ24257_REG_2, 3, 3),
131 	[F_EN_TERM]		= REG_FIELD(BQ24257_REG_2, 2, 2),
132 	[F_CE]			= REG_FIELD(BQ24257_REG_2, 1, 1),
133 	[F_HZ_MODE]		= REG_FIELD(BQ24257_REG_2, 0, 0),
134 	/* REG 3 */
135 	[F_VBAT]		= REG_FIELD(BQ24257_REG_3, 2, 7),
136 	[F_USB_DET]		= REG_FIELD(BQ24257_REG_3, 0, 1),
137 	/* REG 4 */
138 	[F_ICHG]		= REG_FIELD(BQ24257_REG_4, 3, 7),
139 	[F_ITERM]		= REG_FIELD(BQ24257_REG_4, 0, 2),
140 	/* REG 5 */
141 	[F_LOOP_STATUS]		= REG_FIELD(BQ24257_REG_5, 6, 7),
142 	[F_LOW_CHG]		= REG_FIELD(BQ24257_REG_5, 5, 5),
143 	[F_DPDM_EN]		= REG_FIELD(BQ24257_REG_5, 4, 4),
144 	[F_CE_STATUS]		= REG_FIELD(BQ24257_REG_5, 3, 3),
145 	[F_VINDPM]		= REG_FIELD(BQ24257_REG_5, 0, 2),
146 	/* REG 6 */
147 	[F_X2_TMR_EN]		= REG_FIELD(BQ24257_REG_6, 7, 7),
148 	[F_TMR]			= REG_FIELD(BQ24257_REG_6, 5, 6),
149 	[F_SYSOFF]		= REG_FIELD(BQ24257_REG_6, 4, 4),
150 	[F_TS_EN]		= REG_FIELD(BQ24257_REG_6, 3, 3),
151 	[F_TS_STAT]		= REG_FIELD(BQ24257_REG_6, 0, 2),
152 	/* REG 7 */
153 	[F_VOVP]		= REG_FIELD(BQ24257_REG_7, 5, 7),
154 	[F_CLR_VDP]		= REG_FIELD(BQ24257_REG_7, 4, 4),
155 	[F_FORCE_BATDET]	= REG_FIELD(BQ24257_REG_7, 3, 3),
156 	[F_FORCE_PTM]		= REG_FIELD(BQ24257_REG_7, 2, 2)
157 };
158 
159 static const u32 bq24257_vbat_map[] = {
160 	3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
161 	3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
162 	3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
163 	3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
164 	4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
165 	4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
166 };
167 
168 #define BQ24257_VBAT_MAP_SIZE		ARRAY_SIZE(bq24257_vbat_map)
169 
170 static const u32 bq24257_ichg_map[] = {
171 	500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
172 	950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
173 	1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
174 	1750000, 1800000, 1850000, 1900000, 1950000, 2000000
175 };
176 
177 #define BQ24257_ICHG_MAP_SIZE		ARRAY_SIZE(bq24257_ichg_map)
178 
179 static const u32 bq24257_iterm_map[] = {
180 	50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
181 };
182 
183 #define BQ24257_ITERM_MAP_SIZE		ARRAY_SIZE(bq24257_iterm_map)
184 
185 static const u32 bq24257_iilimit_map[] = {
186 	100000, 150000, 500000, 900000, 1500000, 2000000
187 };
188 
189 #define BQ24257_IILIMIT_MAP_SIZE	ARRAY_SIZE(bq24257_iilimit_map)
190 
191 static const u32 bq24257_vovp_map[] = {
192 	6000000, 6500000, 7000000, 8000000, 9000000, 9500000, 10000000,
193 	10500000
194 };
195 
196 #define BQ24257_VOVP_MAP_SIZE		ARRAY_SIZE(bq24257_vovp_map)
197 
198 static const u32 bq24257_vindpm_map[] = {
199 	4200000, 4280000, 4360000, 4440000, 4520000, 4600000, 4680000,
200 	4760000
201 };
202 
203 #define BQ24257_VINDPM_MAP_SIZE		ARRAY_SIZE(bq24257_vindpm_map)
204 
205 static int bq24257_field_read(struct bq24257_device *bq,
206 			      enum bq24257_fields field_id)
207 {
208 	int ret;
209 	int val;
210 
211 	ret = regmap_field_read(bq->rmap_fields[field_id], &val);
212 	if (ret < 0)
213 		return ret;
214 
215 	return val;
216 }
217 
218 static int bq24257_field_write(struct bq24257_device *bq,
219 			       enum bq24257_fields field_id, u8 val)
220 {
221 	return regmap_field_write(bq->rmap_fields[field_id], val);
222 }
223 
224 static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
225 {
226 	u8 idx;
227 
228 	for (idx = 1; idx < map_size; idx++)
229 		if (value < map[idx])
230 			break;
231 
232 	return idx - 1;
233 }
234 
235 enum bq24257_status {
236 	STATUS_READY,
237 	STATUS_CHARGE_IN_PROGRESS,
238 	STATUS_CHARGE_DONE,
239 	STATUS_FAULT,
240 };
241 
242 enum bq24257_fault {
243 	FAULT_NORMAL,
244 	FAULT_INPUT_OVP,
245 	FAULT_INPUT_UVLO,
246 	FAULT_SLEEP,
247 	FAULT_BAT_TS,
248 	FAULT_BAT_OVP,
249 	FAULT_TS,
250 	FAULT_TIMER,
251 	FAULT_NO_BAT,
252 	FAULT_ISET,
253 	FAULT_INPUT_LDO_LOW,
254 };
255 
256 static int bq24257_get_input_current_limit(struct bq24257_device *bq,
257 					   union power_supply_propval *val)
258 {
259 	int ret;
260 
261 	ret = bq24257_field_read(bq, F_IILIMIT);
262 	if (ret < 0)
263 		return ret;
264 
265 	/*
266 	 * The "External ILIM" and "Production & Test" modes are not exposed
267 	 * through this driver and not being covered by the lookup table.
268 	 * Should such a mode have become active let's return an error rather
269 	 * than exceeding the bounds of the lookup table and returning
270 	 * garbage.
271 	 */
272 	if (ret >= BQ24257_IILIMIT_MAP_SIZE)
273 		return -ENODATA;
274 
275 	val->intval = bq24257_iilimit_map[ret];
276 
277 	return 0;
278 }
279 
280 static int bq24257_set_input_current_limit(struct bq24257_device *bq,
281 					const union power_supply_propval *val)
282 {
283 	/*
284 	 * Address the case where the user manually sets an input current limit
285 	 * while the charger auto-detection mechanism is active. In this
286 	 * case we want to abort and go straight to the user-specified value.
287 	 */
288 	if (bq->iilimit_autoset_enable)
289 		cancel_delayed_work_sync(&bq->iilimit_setup_work);
290 
291 	return bq24257_field_write(bq, F_IILIMIT,
292 				   bq24257_find_idx(val->intval,
293 						    bq24257_iilimit_map,
294 						    BQ24257_IILIMIT_MAP_SIZE));
295 }
296 
297 static int bq24257_power_supply_get_property(struct power_supply *psy,
298 					     enum power_supply_property psp,
299 					     union power_supply_propval *val)
300 {
301 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
302 	struct bq24257_state state;
303 
304 	mutex_lock(&bq->lock);
305 	state = bq->state;
306 	mutex_unlock(&bq->lock);
307 
308 	switch (psp) {
309 	case POWER_SUPPLY_PROP_STATUS:
310 		if (!state.power_good)
311 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
312 		else if (state.status == STATUS_READY)
313 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
314 		else if (state.status == STATUS_CHARGE_IN_PROGRESS)
315 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
316 		else if (state.status == STATUS_CHARGE_DONE)
317 			val->intval = POWER_SUPPLY_STATUS_FULL;
318 		else
319 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
320 		break;
321 
322 	case POWER_SUPPLY_PROP_MANUFACTURER:
323 		val->strval = BQ24257_MANUFACTURER;
324 		break;
325 
326 	case POWER_SUPPLY_PROP_MODEL_NAME:
327 		val->strval = bq->info->name;
328 		break;
329 
330 	case POWER_SUPPLY_PROP_ONLINE:
331 		val->intval = state.power_good;
332 		break;
333 
334 	case POWER_SUPPLY_PROP_HEALTH:
335 		switch (state.fault) {
336 		case FAULT_NORMAL:
337 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
338 			break;
339 
340 		case FAULT_INPUT_OVP:
341 		case FAULT_BAT_OVP:
342 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
343 			break;
344 
345 		case FAULT_TS:
346 		case FAULT_BAT_TS:
347 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
348 			break;
349 
350 		case FAULT_TIMER:
351 			val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
352 			break;
353 
354 		default:
355 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
356 			break;
357 		}
358 
359 		break;
360 
361 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
362 		val->intval = bq24257_ichg_map[bq->init_data.ichg];
363 		break;
364 
365 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
366 		val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
367 		break;
368 
369 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
370 		val->intval = bq24257_vbat_map[bq->init_data.vbat];
371 		break;
372 
373 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
374 		val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
375 		break;
376 
377 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
378 		val->intval = bq24257_iterm_map[bq->init_data.iterm];
379 		break;
380 
381 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
382 		return bq24257_get_input_current_limit(bq, val);
383 
384 	default:
385 		return -EINVAL;
386 	}
387 
388 	return 0;
389 }
390 
391 static int bq24257_power_supply_set_property(struct power_supply *psy,
392 					enum power_supply_property prop,
393 					const union power_supply_propval *val)
394 {
395 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
396 
397 	switch (prop) {
398 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
399 		return bq24257_set_input_current_limit(bq, val);
400 	default:
401 		return -EINVAL;
402 	}
403 }
404 
405 static int bq24257_power_supply_property_is_writeable(struct power_supply *psy,
406 					enum power_supply_property psp)
407 {
408 	switch (psp) {
409 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
410 		return true;
411 	default:
412 		return false;
413 	}
414 }
415 
416 static int bq24257_get_chip_state(struct bq24257_device *bq,
417 				  struct bq24257_state *state)
418 {
419 	int ret;
420 
421 	ret = bq24257_field_read(bq, F_STAT);
422 	if (ret < 0)
423 		return ret;
424 
425 	state->status = ret;
426 
427 	ret = bq24257_field_read(bq, F_FAULT);
428 	if (ret < 0)
429 		return ret;
430 
431 	state->fault = ret;
432 
433 	if (bq->pg)
434 		state->power_good = !gpiod_get_value_cansleep(bq->pg);
435 	else
436 		/*
437 		 * If we have a chip without a dedicated power-good GPIO or
438 		 * some other explicit bit that would provide this information
439 		 * assume the power is good if there is no supply related
440 		 * fault - and not good otherwise. There is a possibility for
441 		 * other errors to mask that power in fact is not good but this
442 		 * is probably the best we can do here.
443 		 */
444 		switch (state->fault) {
445 		case FAULT_INPUT_OVP:
446 		case FAULT_INPUT_UVLO:
447 		case FAULT_INPUT_LDO_LOW:
448 			state->power_good = false;
449 			break;
450 		default:
451 			state->power_good = true;
452 		}
453 
454 	return 0;
455 }
456 
457 static bool bq24257_state_changed(struct bq24257_device *bq,
458 				  struct bq24257_state *new_state)
459 {
460 	int ret;
461 
462 	mutex_lock(&bq->lock);
463 	ret = (bq->state.status != new_state->status ||
464 	       bq->state.fault != new_state->fault ||
465 	       bq->state.power_good != new_state->power_good);
466 	mutex_unlock(&bq->lock);
467 
468 	return ret;
469 }
470 
471 enum bq24257_loop_status {
472 	LOOP_STATUS_NONE,
473 	LOOP_STATUS_IN_DPM,
474 	LOOP_STATUS_IN_CURRENT_LIMIT,
475 	LOOP_STATUS_THERMAL,
476 };
477 
478 enum bq24257_in_ilimit {
479 	IILIMIT_100,
480 	IILIMIT_150,
481 	IILIMIT_500,
482 	IILIMIT_900,
483 	IILIMIT_1500,
484 	IILIMIT_2000,
485 	IILIMIT_EXT,
486 	IILIMIT_NONE,
487 };
488 
489 enum bq24257_vovp {
490 	VOVP_6000,
491 	VOVP_6500,
492 	VOVP_7000,
493 	VOVP_8000,
494 	VOVP_9000,
495 	VOVP_9500,
496 	VOVP_10000,
497 	VOVP_10500
498 };
499 
500 enum bq24257_vindpm {
501 	VINDPM_4200,
502 	VINDPM_4280,
503 	VINDPM_4360,
504 	VINDPM_4440,
505 	VINDPM_4520,
506 	VINDPM_4600,
507 	VINDPM_4680,
508 	VINDPM_4760
509 };
510 
511 enum bq24257_port_type {
512 	PORT_TYPE_DCP,		/* Dedicated Charging Port */
513 	PORT_TYPE_CDP,		/* Charging Downstream Port */
514 	PORT_TYPE_SDP,		/* Standard Downstream Port */
515 	PORT_TYPE_NON_STANDARD,
516 };
517 
518 enum bq24257_safety_timer {
519 	SAFETY_TIMER_45,
520 	SAFETY_TIMER_360,
521 	SAFETY_TIMER_540,
522 	SAFETY_TIMER_NONE,
523 };
524 
525 static int bq24257_iilimit_autoset(struct bq24257_device *bq)
526 {
527 	int loop_status;
528 	int iilimit;
529 	int port_type;
530 	int ret;
531 	const u8 new_iilimit[] = {
532 		[PORT_TYPE_DCP] = IILIMIT_2000,
533 		[PORT_TYPE_CDP] = IILIMIT_2000,
534 		[PORT_TYPE_SDP] = IILIMIT_500,
535 		[PORT_TYPE_NON_STANDARD] = IILIMIT_500
536 	};
537 
538 	ret = bq24257_field_read(bq, F_LOOP_STATUS);
539 	if (ret < 0)
540 		goto error;
541 
542 	loop_status = ret;
543 
544 	ret = bq24257_field_read(bq, F_IILIMIT);
545 	if (ret < 0)
546 		goto error;
547 
548 	iilimit = ret;
549 
550 	/*
551 	 * All USB ports should be able to handle 500mA. If not, DPM will lower
552 	 * the charging current to accommodate the power source. No need to set
553 	 * a lower IILIMIT value.
554 	 */
555 	if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
556 		return 0;
557 
558 	ret = bq24257_field_read(bq, F_USB_DET);
559 	if (ret < 0)
560 		goto error;
561 
562 	port_type = ret;
563 
564 	ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
565 	if (ret < 0)
566 		goto error;
567 
568 	ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
569 	if (ret < 0)
570 		goto error;
571 
572 	ret = bq24257_field_write(bq, F_CLR_VDP, 1);
573 	if (ret < 0)
574 		goto error;
575 
576 	dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
577 		port_type, loop_status, new_iilimit[port_type]);
578 
579 	return 0;
580 
581 error:
582 	dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
583 	return ret;
584 }
585 
586 static void bq24257_iilimit_setup_work(struct work_struct *work)
587 {
588 	struct bq24257_device *bq = container_of(work, struct bq24257_device,
589 						 iilimit_setup_work.work);
590 
591 	bq24257_iilimit_autoset(bq);
592 }
593 
594 static void bq24257_handle_state_change(struct bq24257_device *bq,
595 					struct bq24257_state *new_state)
596 {
597 	int ret;
598 	struct bq24257_state old_state;
599 
600 	mutex_lock(&bq->lock);
601 	old_state = bq->state;
602 	mutex_unlock(&bq->lock);
603 
604 	/*
605 	 * Handle BQ2425x state changes observing whether the D+/D- based input
606 	 * current limit autoset functionality is enabled.
607 	 */
608 	if (!new_state->power_good) {
609 		dev_dbg(bq->dev, "Power removed\n");
610 		if (bq->iilimit_autoset_enable) {
611 			cancel_delayed_work_sync(&bq->iilimit_setup_work);
612 
613 			/* activate D+/D- port detection algorithm */
614 			ret = bq24257_field_write(bq, F_DPDM_EN, 1);
615 			if (ret < 0)
616 				goto error;
617 		}
618 		/*
619 		 * When power is removed always return to the default input
620 		 * current limit as configured during probe.
621 		 */
622 		ret = bq24257_field_write(bq, F_IILIMIT, bq->init_data.iilimit);
623 		if (ret < 0)
624 			goto error;
625 	} else if (!old_state.power_good) {
626 		dev_dbg(bq->dev, "Power inserted\n");
627 
628 		if (bq->iilimit_autoset_enable)
629 			/* configure input current limit */
630 			schedule_delayed_work(&bq->iilimit_setup_work,
631 				      msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
632 	} else if (new_state->fault == FAULT_NO_BAT) {
633 		dev_warn(bq->dev, "Battery removed\n");
634 	} else if (new_state->fault == FAULT_TIMER) {
635 		dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
636 	}
637 
638 	return;
639 
640 error:
641 	dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
642 }
643 
644 static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
645 {
646 	int ret;
647 	struct bq24257_device *bq = private;
648 	struct bq24257_state state;
649 
650 	ret = bq24257_get_chip_state(bq, &state);
651 	if (ret < 0)
652 		return IRQ_HANDLED;
653 
654 	if (!bq24257_state_changed(bq, &state))
655 		return IRQ_HANDLED;
656 
657 	dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
658 		state.status, state.fault, state.power_good);
659 
660 	bq24257_handle_state_change(bq, &state);
661 
662 	mutex_lock(&bq->lock);
663 	bq->state = state;
664 	mutex_unlock(&bq->lock);
665 
666 	power_supply_changed(bq->charger);
667 
668 	return IRQ_HANDLED;
669 }
670 
671 static int bq24257_hw_init(struct bq24257_device *bq)
672 {
673 	int ret;
674 	int i;
675 	struct bq24257_state state;
676 
677 	const struct {
678 		int field;
679 		u32 value;
680 	} init_data[] = {
681 		{F_ICHG, bq->init_data.ichg},
682 		{F_VBAT, bq->init_data.vbat},
683 		{F_ITERM, bq->init_data.iterm},
684 		{F_VOVP, bq->init_data.vovp},
685 		{F_VINDPM, bq->init_data.vindpm},
686 	};
687 
688 	/*
689 	 * Disable the watchdog timer to prevent the IC from going back to
690 	 * default settings after 50 seconds of I2C inactivity.
691 	 */
692 	ret = bq24257_field_write(bq, F_WD_EN, 0);
693 	if (ret < 0)
694 		return ret;
695 
696 	/* configure the charge currents and voltages */
697 	for (i = 0; i < ARRAY_SIZE(init_data); i++) {
698 		ret = bq24257_field_write(bq, init_data[i].field,
699 					  init_data[i].value);
700 		if (ret < 0)
701 			return ret;
702 	}
703 
704 	ret = bq24257_get_chip_state(bq, &state);
705 	if (ret < 0)
706 		return ret;
707 
708 	mutex_lock(&bq->lock);
709 	bq->state = state;
710 	mutex_unlock(&bq->lock);
711 
712 	if (!bq->iilimit_autoset_enable) {
713 		dev_dbg(bq->dev, "manually setting iilimit = %u\n",
714 			bq->init_data.iilimit);
715 
716 		/* program fixed input current limit */
717 		ret = bq24257_field_write(bq, F_IILIMIT,
718 					  bq->init_data.iilimit);
719 		if (ret < 0)
720 			return ret;
721 	} else if (!state.power_good)
722 		/* activate D+/D- detection algorithm */
723 		ret = bq24257_field_write(bq, F_DPDM_EN, 1);
724 	else if (state.fault != FAULT_NO_BAT)
725 		ret = bq24257_iilimit_autoset(bq);
726 
727 	return ret;
728 }
729 
730 static enum power_supply_property bq24257_power_supply_props[] = {
731 	POWER_SUPPLY_PROP_MANUFACTURER,
732 	POWER_SUPPLY_PROP_MODEL_NAME,
733 	POWER_SUPPLY_PROP_STATUS,
734 	POWER_SUPPLY_PROP_ONLINE,
735 	POWER_SUPPLY_PROP_HEALTH,
736 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
737 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
738 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
739 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
740 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
741 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
742 };
743 
744 static char *bq24257_charger_supplied_to[] = {
745 	"main-battery",
746 };
747 
748 static const struct power_supply_desc bq24257_power_supply_desc = {
749 	.name = "bq24257-charger",
750 	.type = POWER_SUPPLY_TYPE_USB,
751 	.properties = bq24257_power_supply_props,
752 	.num_properties = ARRAY_SIZE(bq24257_power_supply_props),
753 	.get_property = bq24257_power_supply_get_property,
754 	.set_property = bq24257_power_supply_set_property,
755 	.property_is_writeable = bq24257_power_supply_property_is_writeable,
756 };
757 
758 static ssize_t bq24257_show_ovp_voltage(struct device *dev,
759 					struct device_attribute *attr,
760 					char *buf)
761 {
762 	struct power_supply *psy = dev_get_drvdata(dev);
763 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
764 
765 	return sysfs_emit(buf, "%u\n", bq24257_vovp_map[bq->init_data.vovp]);
766 }
767 
768 static ssize_t bq24257_show_in_dpm_voltage(struct device *dev,
769 					   struct device_attribute *attr,
770 					   char *buf)
771 {
772 	struct power_supply *psy = dev_get_drvdata(dev);
773 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
774 
775 	return sysfs_emit(buf, "%u\n", bq24257_vindpm_map[bq->init_data.vindpm]);
776 }
777 
778 static ssize_t bq24257_sysfs_show_enable(struct device *dev,
779 					 struct device_attribute *attr,
780 					 char *buf)
781 {
782 	struct power_supply *psy = dev_get_drvdata(dev);
783 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
784 	int ret;
785 
786 	if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
787 		ret = bq24257_field_read(bq, F_HZ_MODE);
788 	else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
789 		ret = bq24257_field_read(bq, F_SYSOFF);
790 	else
791 		return -EINVAL;
792 
793 	if (ret < 0)
794 		return ret;
795 
796 	return sysfs_emit(buf, "%d\n", ret);
797 }
798 
799 static ssize_t bq24257_sysfs_set_enable(struct device *dev,
800 					struct device_attribute *attr,
801 					const char *buf,
802 					size_t count)
803 {
804 	struct power_supply *psy = dev_get_drvdata(dev);
805 	struct bq24257_device *bq = power_supply_get_drvdata(psy);
806 	long val;
807 	int ret;
808 
809 	if (kstrtol(buf, 10, &val) < 0)
810 		return -EINVAL;
811 
812 	if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
813 		ret = bq24257_field_write(bq, F_HZ_MODE, (bool)val);
814 	else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
815 		ret = bq24257_field_write(bq, F_SYSOFF, (bool)val);
816 	else
817 		return -EINVAL;
818 
819 	if (ret < 0)
820 		return ret;
821 
822 	return count;
823 }
824 
825 static DEVICE_ATTR(ovp_voltage, S_IRUGO, bq24257_show_ovp_voltage, NULL);
826 static DEVICE_ATTR(in_dpm_voltage, S_IRUGO, bq24257_show_in_dpm_voltage, NULL);
827 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
828 		   bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
829 static DEVICE_ATTR(sysoff_enable, S_IWUSR | S_IRUGO,
830 		   bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
831 
832 static struct attribute *bq24257_charger_sysfs_attrs[] = {
833 	&dev_attr_ovp_voltage.attr,
834 	&dev_attr_in_dpm_voltage.attr,
835 	&dev_attr_high_impedance_enable.attr,
836 	&dev_attr_sysoff_enable.attr,
837 	NULL,
838 };
839 
840 ATTRIBUTE_GROUPS(bq24257_charger_sysfs);
841 
842 static int bq24257_power_supply_init(struct bq24257_device *bq)
843 {
844 	struct power_supply_config psy_cfg = { .drv_data = bq, };
845 
846 	psy_cfg.attr_grp = bq24257_charger_sysfs_groups;
847 	psy_cfg.supplied_to = bq24257_charger_supplied_to;
848 	psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
849 
850 	bq->charger = devm_power_supply_register(bq->dev,
851 						 &bq24257_power_supply_desc,
852 						 &psy_cfg);
853 
854 	return PTR_ERR_OR_ZERO(bq->charger);
855 }
856 
857 static void bq24257_pg_gpio_probe(struct bq24257_device *bq)
858 {
859 	bq->pg = devm_gpiod_get_optional(bq->dev, BQ24257_PG_GPIO, GPIOD_IN);
860 
861 	if (PTR_ERR(bq->pg) == -EPROBE_DEFER) {
862 		dev_info(bq->dev, "probe retry requested for PG pin\n");
863 		return;
864 	} else if (IS_ERR(bq->pg)) {
865 		dev_err(bq->dev, "error probing PG pin\n");
866 		bq->pg = NULL;
867 		return;
868 	}
869 
870 	if (bq->pg)
871 		dev_dbg(bq->dev, "probed PG pin = %d\n", desc_to_gpio(bq->pg));
872 }
873 
874 static int bq24257_fw_probe(struct bq24257_device *bq)
875 {
876 	int ret;
877 	u32 property;
878 
879 	/* Required properties */
880 	ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
881 	if (ret < 0)
882 		return ret;
883 
884 	bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
885 					      BQ24257_ICHG_MAP_SIZE);
886 
887 	ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
888 				       &property);
889 	if (ret < 0)
890 		return ret;
891 
892 	bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
893 					      BQ24257_VBAT_MAP_SIZE);
894 
895 	ret = device_property_read_u32(bq->dev, "ti,termination-current",
896 				       &property);
897 	if (ret < 0)
898 		return ret;
899 
900 	bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
901 					       BQ24257_ITERM_MAP_SIZE);
902 
903 	/* Optional properties. If not provided use reasonable default. */
904 	ret = device_property_read_u32(bq->dev, "ti,current-limit",
905 				       &property);
906 	if (ret < 0) {
907 		bq->iilimit_autoset_enable = true;
908 
909 		/*
910 		 * Explicitly set a default value which will be needed for
911 		 * devices that don't support the automatic setting of the input
912 		 * current limit through the charger type detection mechanism.
913 		 */
914 		bq->init_data.iilimit = IILIMIT_500;
915 	} else
916 		bq->init_data.iilimit =
917 				bq24257_find_idx(property,
918 						 bq24257_iilimit_map,
919 						 BQ24257_IILIMIT_MAP_SIZE);
920 
921 	ret = device_property_read_u32(bq->dev, "ti,ovp-voltage",
922 				       &property);
923 	if (ret < 0)
924 		bq->init_data.vovp = VOVP_6500;
925 	else
926 		bq->init_data.vovp = bq24257_find_idx(property,
927 						      bq24257_vovp_map,
928 						      BQ24257_VOVP_MAP_SIZE);
929 
930 	ret = device_property_read_u32(bq->dev, "ti,in-dpm-voltage",
931 				       &property);
932 	if (ret < 0)
933 		bq->init_data.vindpm = VINDPM_4360;
934 	else
935 		bq->init_data.vindpm =
936 				bq24257_find_idx(property,
937 						 bq24257_vindpm_map,
938 						 BQ24257_VINDPM_MAP_SIZE);
939 
940 	return 0;
941 }
942 
943 static int bq24257_probe(struct i2c_client *client)
944 {
945 	struct i2c_adapter *adapter = client->adapter;
946 	struct device *dev = &client->dev;
947 	struct bq24257_device *bq;
948 	int ret;
949 	int i;
950 
951 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
952 		dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
953 		return -ENODEV;
954 	}
955 
956 	bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
957 	if (!bq)
958 		return -ENOMEM;
959 
960 	bq->client = client;
961 	bq->dev = dev;
962 
963 	bq->info = i2c_get_match_data(client);
964 	if (!bq->info)
965 		return dev_err_probe(dev, -ENODEV, "Failed to match device\n");
966 
967 	mutex_init(&bq->lock);
968 
969 	bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
970 	if (IS_ERR(bq->rmap)) {
971 		dev_err(dev, "failed to allocate register map\n");
972 		return PTR_ERR(bq->rmap);
973 	}
974 
975 	for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
976 		const struct reg_field *reg_fields = bq24257_reg_fields;
977 
978 		bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
979 							     reg_fields[i]);
980 		if (IS_ERR(bq->rmap_fields[i])) {
981 			dev_err(dev, "cannot allocate regmap field\n");
982 			return PTR_ERR(bq->rmap_fields[i]);
983 		}
984 	}
985 
986 	i2c_set_clientdata(client, bq);
987 
988 	if (!dev->platform_data) {
989 		ret = bq24257_fw_probe(bq);
990 		if (ret < 0) {
991 			dev_err(dev, "Cannot read device properties.\n");
992 			return ret;
993 		}
994 	} else {
995 		return -ENODEV;
996 	}
997 
998 	/*
999 	 * The BQ24250 doesn't support the D+/D- based charger type detection
1000 	 * used for the automatic setting of the input current limit setting so
1001 	 * explicitly disable that feature.
1002 	 */
1003 	if (bq->info->chip == BQ24250)
1004 		bq->iilimit_autoset_enable = false;
1005 
1006 	if (bq->iilimit_autoset_enable)
1007 		INIT_DELAYED_WORK(&bq->iilimit_setup_work,
1008 				  bq24257_iilimit_setup_work);
1009 
1010 	/*
1011 	 * The BQ24250 doesn't have a dedicated Power Good (PG) pin so let's
1012 	 * not probe for it and instead use a SW-based approach to determine
1013 	 * the PG state. We also use a SW-based approach for all other devices
1014 	 * if the PG pin is either not defined or can't be probed.
1015 	 */
1016 	if (bq->info->chip != BQ24250)
1017 		bq24257_pg_gpio_probe(bq);
1018 
1019 	if (PTR_ERR(bq->pg) == -EPROBE_DEFER)
1020 		return PTR_ERR(bq->pg);
1021 	else if (!bq->pg)
1022 		dev_info(bq->dev, "using SW-based power-good detection\n");
1023 
1024 	/* reset all registers to defaults */
1025 	ret = bq24257_field_write(bq, F_RESET, 1);
1026 	if (ret < 0)
1027 		return ret;
1028 
1029 	/*
1030 	 * Put the RESET bit back to 0, in cache. For some reason the HW always
1031 	 * returns 1 on this bit, so this is the only way to avoid resetting the
1032 	 * chip every time we update another field in this register.
1033 	 */
1034 	ret = bq24257_field_write(bq, F_RESET, 0);
1035 	if (ret < 0)
1036 		return ret;
1037 
1038 	ret = bq24257_hw_init(bq);
1039 	if (ret < 0) {
1040 		dev_err(dev, "Cannot initialize the chip.\n");
1041 		return ret;
1042 	}
1043 
1044 	ret = bq24257_power_supply_init(bq);
1045 	if (ret < 0) {
1046 		dev_err(dev, "Failed to register power supply\n");
1047 		return ret;
1048 	}
1049 
1050 	ret = devm_request_threaded_irq(dev, client->irq, NULL,
1051 					bq24257_irq_handler_thread,
1052 					IRQF_TRIGGER_FALLING |
1053 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1054 					bq->info->name, bq);
1055 	if (ret) {
1056 		dev_err(dev, "Failed to request IRQ #%d\n", client->irq);
1057 		return ret;
1058 	}
1059 
1060 	return 0;
1061 }
1062 
1063 static void bq24257_remove(struct i2c_client *client)
1064 {
1065 	struct bq24257_device *bq = i2c_get_clientdata(client);
1066 
1067 	if (bq->iilimit_autoset_enable)
1068 		cancel_delayed_work_sync(&bq->iilimit_setup_work);
1069 
1070 	bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
1071 }
1072 
1073 #ifdef CONFIG_PM_SLEEP
1074 static int bq24257_suspend(struct device *dev)
1075 {
1076 	struct bq24257_device *bq = dev_get_drvdata(dev);
1077 	int ret = 0;
1078 
1079 	if (bq->iilimit_autoset_enable)
1080 		cancel_delayed_work_sync(&bq->iilimit_setup_work);
1081 
1082 	/* reset all registers to default (and activate standalone mode) */
1083 	ret = bq24257_field_write(bq, F_RESET, 1);
1084 	if (ret < 0)
1085 		dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
1086 
1087 	return ret;
1088 }
1089 
1090 static int bq24257_resume(struct device *dev)
1091 {
1092 	int ret;
1093 	struct bq24257_device *bq = dev_get_drvdata(dev);
1094 
1095 	ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
1096 	if (ret < 0)
1097 		return ret;
1098 
1099 	ret = bq24257_field_write(bq, F_RESET, 0);
1100 	if (ret < 0)
1101 		return ret;
1102 
1103 	ret = bq24257_hw_init(bq);
1104 	if (ret < 0) {
1105 		dev_err(bq->dev, "Cannot init chip after resume.\n");
1106 		return ret;
1107 	}
1108 
1109 	/* signal userspace, maybe state changed while suspended */
1110 	power_supply_changed(bq->charger);
1111 
1112 	return 0;
1113 }
1114 #endif
1115 
1116 static const struct dev_pm_ops bq24257_pm = {
1117 	SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
1118 };
1119 
1120 static const struct bq2425x_chip_info bq24250_info = {
1121 	.name = "bq24250",
1122 	.chip = BQ24250,
1123 };
1124 
1125 static const struct bq2425x_chip_info bq24251_info = {
1126 	.name = "bq24251",
1127 	.chip = BQ24251,
1128 };
1129 
1130 static const struct bq2425x_chip_info bq24257_info = {
1131 	.name = "bq24257",
1132 	.chip = BQ24257,
1133 };
1134 
1135 static const struct i2c_device_id bq24257_i2c_ids[] = {
1136 	{ "bq24250", (kernel_ulong_t)&bq24250_info },
1137 	{ "bq24251", (kernel_ulong_t)&bq24251_info },
1138 	{ "bq24257", (kernel_ulong_t)&bq24257_info },
1139 	{}
1140 };
1141 MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
1142 
1143 static const struct of_device_id bq24257_of_match[] __maybe_unused = {
1144 	{ .compatible = "ti,bq24250", &bq24250_info },
1145 	{ .compatible = "ti,bq24251", &bq24251_info },
1146 	{ .compatible = "ti,bq24257", &bq24257_info },
1147 	{}
1148 };
1149 MODULE_DEVICE_TABLE(of, bq24257_of_match);
1150 
1151 #ifdef CONFIG_ACPI
1152 static const struct acpi_device_id bq24257_acpi_match[] = {
1153 	{ "BQ242500", (kernel_ulong_t)&bq24250_info },
1154 	{ "BQ242510", (kernel_ulong_t)&bq24251_info },
1155 	{ "BQ242570", (kernel_ulong_t)&bq24257_info },
1156 	{}
1157 };
1158 MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
1159 #endif
1160 
1161 static struct i2c_driver bq24257_driver = {
1162 	.driver = {
1163 		.name = "bq24257-charger",
1164 		.of_match_table = of_match_ptr(bq24257_of_match),
1165 		.acpi_match_table = ACPI_PTR(bq24257_acpi_match),
1166 		.pm = &bq24257_pm,
1167 	},
1168 	.probe = bq24257_probe,
1169 	.remove = bq24257_remove,
1170 	.id_table = bq24257_i2c_ids,
1171 };
1172 module_i2c_driver(bq24257_driver);
1173 
1174 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1175 MODULE_DESCRIPTION("bq24257 charger driver");
1176 MODULE_LICENSE("GPL");
1177