xref: /linux/drivers/power/supply/axp20x_usb_power.c (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * AXP20x PMIC USB power supply status driver
4  *
5  * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6  * Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/device.h>
11 #include <linux/devm-helpers.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/axp20x.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/power_supply.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/iio/consumer.h>
24 #include <linux/workqueue.h>
25 
26 #define DRVNAME "axp20x-usb-power-supply"
27 
28 #define AXP192_USB_OTG_STATUS		0x04
29 
30 #define AXP20X_PWR_STATUS_VBUS_PRESENT	BIT(5)
31 #define AXP20X_PWR_STATUS_VBUS_USED	BIT(4)
32 
33 #define AXP20X_USB_STATUS_VBUS_VALID	BIT(2)
34 
35 #define AXP20X_VBUS_VHOLD_uV(b)		(4000000 + (((b) >> 3) & 7) * 100000)
36 #define AXP20X_VBUS_VHOLD_MASK		GENMASK(5, 3)
37 #define AXP20X_VBUS_VHOLD_OFFSET	3
38 
39 #define AXP20X_ADC_EN1_VBUS_CURR	BIT(2)
40 #define AXP20X_ADC_EN1_VBUS_VOLT	BIT(3)
41 
42 /*
43  * Note do not raise the debounce time, we must report Vusb high within
44  * 100ms otherwise we get Vbus errors in musb.
45  */
46 #define DEBOUNCE_TIME			msecs_to_jiffies(50)
47 
48 struct axp_data {
49 	const struct power_supply_desc	*power_desc;
50 	const char * const		*irq_names;
51 	unsigned int			num_irq_names;
52 	const int			*curr_lim_table;
53 	int				curr_lim_table_size;
54 	struct reg_field		curr_lim_fld;
55 	struct reg_field		vbus_valid_bit;
56 	struct reg_field		vbus_mon_bit;
57 	struct reg_field		usb_bc_en_bit;
58 	struct reg_field		usb_bc_det_fld;
59 	struct reg_field		vbus_disable_bit;
60 	bool				vbus_needs_polling: 1;
61 };
62 
63 struct axp20x_usb_power {
64 	struct device *dev;
65 	struct regmap *regmap;
66 	struct regmap_field *curr_lim_fld;
67 	struct regmap_field *vbus_valid_bit;
68 	struct regmap_field *vbus_mon_bit;
69 	struct regmap_field *usb_bc_en_bit;
70 	struct regmap_field *usb_bc_det_fld;
71 	struct regmap_field *vbus_disable_bit;
72 	struct power_supply *supply;
73 	const struct axp_data *axp_data;
74 	struct iio_channel *vbus_v;
75 	struct iio_channel *vbus_i;
76 	struct delayed_work vbus_detect;
77 	unsigned int old_status;
78 	unsigned int online;
79 	unsigned int num_irqs;
80 	unsigned int irqs[] __counted_by(num_irqs);
81 };
82 
83 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power)
84 {
85 	/*
86 	 * Polling is only necessary while VBUS is offline. While online, a
87 	 * present->absent transition implies an online->offline transition
88 	 * and will trigger the VBUS_REMOVAL IRQ.
89 	 */
90 	if (power->axp_data->vbus_needs_polling && !power->online)
91 		return true;
92 
93 	return false;
94 }
95 
96 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
97 {
98 	struct axp20x_usb_power *power = devid;
99 
100 	power_supply_changed(power->supply);
101 
102 	mod_delayed_work(system_power_efficient_wq, &power->vbus_detect, DEBOUNCE_TIME);
103 
104 	return IRQ_HANDLED;
105 }
106 
107 static void axp20x_usb_power_poll_vbus(struct work_struct *work)
108 {
109 	struct axp20x_usb_power *power =
110 		container_of(work, struct axp20x_usb_power, vbus_detect.work);
111 	unsigned int val;
112 	int ret;
113 
114 	ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val);
115 	if (ret)
116 		goto out;
117 
118 	val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED);
119 	if (val != power->old_status)
120 		power_supply_changed(power->supply);
121 
122 	if (power->usb_bc_en_bit && (val & AXP20X_PWR_STATUS_VBUS_PRESENT) !=
123 		(power->old_status & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
124 		dev_dbg(power->dev, "Cable status changed, re-enabling USB BC");
125 		ret = regmap_field_write(power->usb_bc_en_bit, 1);
126 		if (ret)
127 			dev_err(power->dev, "failed to enable USB BC: errno %d",
128 				ret);
129 	}
130 
131 	power->old_status = val;
132 	power->online = val & AXP20X_PWR_STATUS_VBUS_USED;
133 
134 out:
135 	if (axp20x_usb_vbus_needs_polling(power))
136 		mod_delayed_work(system_power_efficient_wq, &power->vbus_detect, DEBOUNCE_TIME);
137 }
138 
139 static int axp20x_get_usb_type(struct axp20x_usb_power *power,
140 			       union power_supply_propval *val)
141 {
142 	unsigned int reg;
143 	int ret;
144 
145 	if (!power->usb_bc_det_fld)
146 		return -EINVAL;
147 
148 	ret = regmap_field_read(power->usb_bc_det_fld, &reg);
149 	if (ret)
150 		return ret;
151 
152 	switch (reg) {
153 	case 1:
154 		val->intval = POWER_SUPPLY_USB_TYPE_SDP;
155 		break;
156 	case 2:
157 		val->intval = POWER_SUPPLY_USB_TYPE_CDP;
158 		break;
159 	case 3:
160 		val->intval = POWER_SUPPLY_USB_TYPE_DCP;
161 		break;
162 	default:
163 		val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
164 		break;
165 	}
166 
167 	return 0;
168 }
169 
170 static int axp20x_usb_power_get_property(struct power_supply *psy,
171 	enum power_supply_property psp, union power_supply_propval *val)
172 {
173 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
174 	unsigned int input, v;
175 	int ret;
176 
177 	switch (psp) {
178 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
179 		ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
180 		if (ret)
181 			return ret;
182 
183 		val->intval = AXP20X_VBUS_VHOLD_uV(v);
184 		return 0;
185 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
186 		if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
187 			ret = iio_read_channel_processed(power->vbus_v,
188 							 &val->intval);
189 			if (ret)
190 				return ret;
191 
192 			/*
193 			 * IIO framework gives mV but Power Supply framework
194 			 * gives uV.
195 			 */
196 			val->intval *= 1000;
197 			return 0;
198 		}
199 
200 		ret = axp20x_read_variable_width(power->regmap,
201 						 AXP20X_VBUS_V_ADC_H, 12);
202 		if (ret < 0)
203 			return ret;
204 
205 		val->intval = ret * 1700; /* 1 step = 1.7 mV */
206 		return 0;
207 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
208 		ret = regmap_field_read(power->curr_lim_fld, &v);
209 		if (ret)
210 			return ret;
211 
212 		if (v < power->axp_data->curr_lim_table_size)
213 			val->intval = power->axp_data->curr_lim_table[v];
214 		else
215 			val->intval = power->axp_data->curr_lim_table[
216 				power->axp_data->curr_lim_table_size - 1];
217 		return 0;
218 	case POWER_SUPPLY_PROP_CURRENT_NOW:
219 		if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
220 			ret = iio_read_channel_processed(power->vbus_i,
221 							 &val->intval);
222 			if (ret)
223 				return ret;
224 
225 			/*
226 			 * IIO framework gives mA but Power Supply framework
227 			 * gives uA.
228 			 */
229 			val->intval *= 1000;
230 			return 0;
231 		}
232 
233 		ret = axp20x_read_variable_width(power->regmap,
234 						 AXP20X_VBUS_I_ADC_H, 12);
235 		if (ret < 0)
236 			return ret;
237 
238 		val->intval = ret * 375; /* 1 step = 0.375 mA */
239 		return 0;
240 
241 	case POWER_SUPPLY_PROP_USB_TYPE:
242 		return axp20x_get_usb_type(power, val);
243 	default:
244 		break;
245 	}
246 
247 	/* All the properties below need the input-status reg value */
248 	ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
249 	if (ret)
250 		return ret;
251 
252 	switch (psp) {
253 	case POWER_SUPPLY_PROP_HEALTH:
254 		if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
255 			val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
256 			break;
257 		}
258 
259 		val->intval = POWER_SUPPLY_HEALTH_GOOD;
260 
261 		if (power->vbus_valid_bit) {
262 			ret = regmap_field_read(power->vbus_valid_bit, &v);
263 			if (ret)
264 				return ret;
265 
266 			if (v == 0)
267 				val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
268 		}
269 
270 		break;
271 	case POWER_SUPPLY_PROP_PRESENT:
272 		val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
273 		break;
274 	case POWER_SUPPLY_PROP_ONLINE:
275 		val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
276 		break;
277 	default:
278 		return -EINVAL;
279 	}
280 
281 	return 0;
282 }
283 
284 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
285 					    int intval)
286 {
287 	int val;
288 
289 	switch (intval) {
290 	case 4000000:
291 	case 4100000:
292 	case 4200000:
293 	case 4300000:
294 	case 4400000:
295 	case 4500000:
296 	case 4600000:
297 	case 4700000:
298 		val = (intval - 4000000) / 100000;
299 		return regmap_update_bits(power->regmap,
300 					  AXP20X_VBUS_IPSOUT_MGMT,
301 					  AXP20X_VBUS_VHOLD_MASK,
302 					  val << AXP20X_VBUS_VHOLD_OFFSET);
303 	default:
304 		return -EINVAL;
305 	}
306 
307 	return -EINVAL;
308 }
309 
310 static int axp20x_usb_power_set_input_current_limit(struct axp20x_usb_power *power,
311 						    int intval)
312 {
313 	int ret;
314 	unsigned int reg;
315 	const unsigned int max = power->axp_data->curr_lim_table_size;
316 
317 	if (intval == -1)
318 		return -EINVAL;
319 
320 	/*
321 	 * BC1.2 detection can cause a race condition if we try to set a current
322 	 * limit while it's in progress. When it finishes it will overwrite the
323 	 * current limit we just set.
324 	 */
325 	if (power->usb_bc_en_bit) {
326 		dev_dbg(power->dev,
327 			"disabling BC1.2 detection because current limit was set");
328 		ret = regmap_field_write(power->usb_bc_en_bit, 0);
329 		if (ret)
330 			return ret;
331 	}
332 
333 	for (reg = max - 1; reg > 0; reg--)
334 		if (power->axp_data->curr_lim_table[reg] <= intval)
335 			break;
336 
337 	dev_dbg(power->dev, "setting input current limit reg to %d (%d uA), requested %d uA",
338 		reg, power->axp_data->curr_lim_table[reg], intval);
339 
340 	return regmap_field_write(power->curr_lim_fld, reg);
341 }
342 
343 static int axp20x_usb_power_set_property(struct power_supply *psy,
344 					 enum power_supply_property psp,
345 					 const union power_supply_propval *val)
346 {
347 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
348 
349 	switch (psp) {
350 	case POWER_SUPPLY_PROP_ONLINE:
351 		if (!power->vbus_disable_bit)
352 			return -EINVAL;
353 
354 		return regmap_field_write(power->vbus_disable_bit, !val->intval);
355 
356 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
357 		return axp20x_usb_power_set_voltage_min(power, val->intval);
358 
359 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
360 		return axp20x_usb_power_set_input_current_limit(power, val->intval);
361 
362 	default:
363 		return -EINVAL;
364 	}
365 
366 	return -EINVAL;
367 }
368 
369 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
370 					   enum power_supply_property psp)
371 {
372 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
373 
374 	/*
375 	 * The VBUS path select flag works differently on AXP288 and newer:
376 	 *  - On AXP20x and AXP22x, the flag enables VBUS (ignoring N_VBUSEN).
377 	 *  - On AXP288 and AXP8xx, the flag disables VBUS (ignoring N_VBUSEN).
378 	 * We only expose the control on variants where it can be used to force
379 	 * the VBUS input offline.
380 	 */
381 	if (psp == POWER_SUPPLY_PROP_ONLINE)
382 		return power->vbus_disable_bit != NULL;
383 
384 	return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
385 	       psp == POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT;
386 }
387 
388 static enum power_supply_property axp20x_usb_power_properties[] = {
389 	POWER_SUPPLY_PROP_HEALTH,
390 	POWER_SUPPLY_PROP_PRESENT,
391 	POWER_SUPPLY_PROP_ONLINE,
392 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
393 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
394 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
395 	POWER_SUPPLY_PROP_CURRENT_NOW,
396 };
397 
398 static enum power_supply_property axp22x_usb_power_properties[] = {
399 	POWER_SUPPLY_PROP_HEALTH,
400 	POWER_SUPPLY_PROP_PRESENT,
401 	POWER_SUPPLY_PROP_ONLINE,
402 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
403 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
404 };
405 
406 static enum power_supply_property axp813_usb_power_properties[] = {
407 	POWER_SUPPLY_PROP_HEALTH,
408 	POWER_SUPPLY_PROP_PRESENT,
409 	POWER_SUPPLY_PROP_ONLINE,
410 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
411 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
412 	POWER_SUPPLY_PROP_USB_TYPE,
413 };
414 
415 static enum power_supply_usb_type axp813_usb_types[] = {
416 	POWER_SUPPLY_USB_TYPE_SDP,
417 	POWER_SUPPLY_USB_TYPE_DCP,
418 	POWER_SUPPLY_USB_TYPE_CDP,
419 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
420 };
421 
422 static const struct power_supply_desc axp20x_usb_power_desc = {
423 	.name = "axp20x-usb",
424 	.type = POWER_SUPPLY_TYPE_USB,
425 	.properties = axp20x_usb_power_properties,
426 	.num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
427 	.property_is_writeable = axp20x_usb_power_prop_writeable,
428 	.get_property = axp20x_usb_power_get_property,
429 	.set_property = axp20x_usb_power_set_property,
430 };
431 
432 static const struct power_supply_desc axp22x_usb_power_desc = {
433 	.name = "axp20x-usb",
434 	.type = POWER_SUPPLY_TYPE_USB,
435 	.properties = axp22x_usb_power_properties,
436 	.num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
437 	.property_is_writeable = axp20x_usb_power_prop_writeable,
438 	.get_property = axp20x_usb_power_get_property,
439 	.set_property = axp20x_usb_power_set_property,
440 };
441 
442 static const struct power_supply_desc axp813_usb_power_desc = {
443 	.name = "axp20x-usb",
444 	.type = POWER_SUPPLY_TYPE_USB,
445 	.properties = axp813_usb_power_properties,
446 	.num_properties = ARRAY_SIZE(axp813_usb_power_properties),
447 	.property_is_writeable = axp20x_usb_power_prop_writeable,
448 	.get_property = axp20x_usb_power_get_property,
449 	.set_property = axp20x_usb_power_set_property,
450 	.usb_types = axp813_usb_types,
451 	.num_usb_types = ARRAY_SIZE(axp813_usb_types),
452 };
453 
454 static const char * const axp20x_irq_names[] = {
455 	"VBUS_PLUGIN",
456 	"VBUS_REMOVAL",
457 	"VBUS_VALID",
458 	"VBUS_NOT_VALID",
459 };
460 
461 static const char * const axp22x_irq_names[] = {
462 	"VBUS_PLUGIN",
463 	"VBUS_REMOVAL",
464 };
465 
466 static int axp192_usb_curr_lim_table[] = {
467 	-1,
468 	-1,
469 	500000,
470 	100000,
471 };
472 
473 static int axp20x_usb_curr_lim_table[] = {
474 	900000,
475 	500000,
476 	100000,
477 	-1,
478 };
479 
480 static int axp221_usb_curr_lim_table[] = {
481 	900000,
482 	500000,
483 	-1,
484 	-1,
485 };
486 
487 static int axp813_usb_curr_lim_table[] = {
488 	100000,
489 	500000,
490 	900000,
491 	1500000,
492 	2000000,
493 	2500000,
494 	3000000,
495 	3500000,
496 	4000000,
497 };
498 
499 static const struct axp_data axp192_data = {
500 	.power_desc	= &axp20x_usb_power_desc,
501 	.irq_names	= axp20x_irq_names,
502 	.num_irq_names	= ARRAY_SIZE(axp20x_irq_names),
503 	.curr_lim_table = axp192_usb_curr_lim_table,
504 	.curr_lim_table_size = ARRAY_SIZE(axp192_usb_curr_lim_table),
505 	.curr_lim_fld   = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1),
506 	.vbus_valid_bit = REG_FIELD(AXP192_USB_OTG_STATUS, 2, 2),
507 	.vbus_mon_bit   = REG_FIELD(AXP20X_VBUS_MON, 3, 3),
508 };
509 
510 static const struct axp_data axp202_data = {
511 	.power_desc	= &axp20x_usb_power_desc,
512 	.irq_names	= axp20x_irq_names,
513 	.num_irq_names	= ARRAY_SIZE(axp20x_irq_names),
514 	.curr_lim_table = axp20x_usb_curr_lim_table,
515 	.curr_lim_table_size = ARRAY_SIZE(axp20x_usb_curr_lim_table),
516 	.curr_lim_fld   = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1),
517 	.vbus_valid_bit = REG_FIELD(AXP20X_USB_OTG_STATUS, 2, 2),
518 	.vbus_mon_bit   = REG_FIELD(AXP20X_VBUS_MON, 3, 3),
519 };
520 
521 static const struct axp_data axp221_data = {
522 	.power_desc	= &axp22x_usb_power_desc,
523 	.irq_names	= axp22x_irq_names,
524 	.num_irq_names	= ARRAY_SIZE(axp22x_irq_names),
525 	.curr_lim_table = axp221_usb_curr_lim_table,
526 	.curr_lim_table_size = ARRAY_SIZE(axp221_usb_curr_lim_table),
527 	.curr_lim_fld   = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1),
528 	.vbus_needs_polling = true,
529 };
530 
531 static const struct axp_data axp223_data = {
532 	.power_desc	= &axp22x_usb_power_desc,
533 	.irq_names	= axp22x_irq_names,
534 	.num_irq_names	= ARRAY_SIZE(axp22x_irq_names),
535 	.curr_lim_table = axp20x_usb_curr_lim_table,
536 	.curr_lim_table_size = ARRAY_SIZE(axp20x_usb_curr_lim_table),
537 	.curr_lim_fld   = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1),
538 	.vbus_needs_polling = true,
539 };
540 
541 static const struct axp_data axp813_data = {
542 	.power_desc	= &axp813_usb_power_desc,
543 	.irq_names	= axp22x_irq_names,
544 	.num_irq_names	= ARRAY_SIZE(axp22x_irq_names),
545 	.curr_lim_table = axp813_usb_curr_lim_table,
546 	.curr_lim_table_size = ARRAY_SIZE(axp813_usb_curr_lim_table),
547 	.curr_lim_fld	= REG_FIELD(AXP22X_CHRG_CTRL3, 4, 7),
548 	.usb_bc_en_bit	= REG_FIELD(AXP288_BC_GLOBAL, 0, 0),
549 	.usb_bc_det_fld = REG_FIELD(AXP288_BC_DET_STAT, 5, 7),
550 	.vbus_disable_bit = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 7, 7),
551 	.vbus_needs_polling = true,
552 };
553 
554 #ifdef CONFIG_PM_SLEEP
555 static int axp20x_usb_power_suspend(struct device *dev)
556 {
557 	struct axp20x_usb_power *power = dev_get_drvdata(dev);
558 	int i = 0;
559 
560 	/*
561 	 * Allow wake via VBUS_PLUGIN only.
562 	 *
563 	 * As nested threaded IRQs are not automatically disabled during
564 	 * suspend, we must explicitly disable the remainder of the IRQs.
565 	 */
566 	if (device_may_wakeup(&power->supply->dev))
567 		enable_irq_wake(power->irqs[i++]);
568 	while (i < power->num_irqs)
569 		disable_irq(power->irqs[i++]);
570 
571 	return 0;
572 }
573 
574 static int axp20x_usb_power_resume(struct device *dev)
575 {
576 	struct axp20x_usb_power *power = dev_get_drvdata(dev);
577 	int i = 0;
578 
579 	if (device_may_wakeup(&power->supply->dev))
580 		disable_irq_wake(power->irqs[i++]);
581 	while (i < power->num_irqs)
582 		enable_irq(power->irqs[i++]);
583 
584 	mod_delayed_work(system_power_efficient_wq, &power->vbus_detect, DEBOUNCE_TIME);
585 
586 	return 0;
587 }
588 #endif
589 
590 static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops, axp20x_usb_power_suspend,
591 						  axp20x_usb_power_resume);
592 
593 static int configure_iio_channels(struct platform_device *pdev,
594 				  struct axp20x_usb_power *power)
595 {
596 	power->vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
597 	if (IS_ERR(power->vbus_v)) {
598 		if (PTR_ERR(power->vbus_v) == -ENODEV)
599 			return -EPROBE_DEFER;
600 		return PTR_ERR(power->vbus_v);
601 	}
602 
603 	power->vbus_i = devm_iio_channel_get(&pdev->dev, "vbus_i");
604 	if (IS_ERR(power->vbus_i)) {
605 		if (PTR_ERR(power->vbus_i) == -ENODEV)
606 			return -EPROBE_DEFER;
607 		return PTR_ERR(power->vbus_i);
608 	}
609 
610 	return 0;
611 }
612 
613 static int configure_adc_registers(struct axp20x_usb_power *power)
614 {
615 	/* Enable vbus voltage and current measurement */
616 	return regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
617 				  AXP20X_ADC_EN1_VBUS_CURR |
618 				  AXP20X_ADC_EN1_VBUS_VOLT,
619 				  AXP20X_ADC_EN1_VBUS_CURR |
620 				  AXP20X_ADC_EN1_VBUS_VOLT);
621 }
622 
623 static int axp20x_regmap_field_alloc_optional(struct device *dev,
624 					      struct regmap *regmap,
625 					      struct reg_field fdesc,
626 					      struct regmap_field **fieldp)
627 {
628 	struct regmap_field *field;
629 
630 	if (fdesc.reg == 0) {
631 		*fieldp = NULL;
632 		return 0;
633 	}
634 
635 	field = devm_regmap_field_alloc(dev, regmap, fdesc);
636 	if (IS_ERR(field))
637 		return PTR_ERR(field);
638 
639 	*fieldp = field;
640 	return 0;
641 }
642 
643 static int axp20x_usb_power_probe(struct platform_device *pdev)
644 {
645 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
646 	struct power_supply_config psy_cfg = {};
647 	struct axp20x_usb_power *power;
648 	const struct axp_data *axp_data;
649 	int i, irq, ret;
650 
651 	if (!of_device_is_available(pdev->dev.of_node))
652 		return -ENODEV;
653 
654 	if (!axp20x) {
655 		dev_err(&pdev->dev, "Parent drvdata not set\n");
656 		return -EINVAL;
657 	}
658 
659 	axp_data = of_device_get_match_data(&pdev->dev);
660 
661 	power = devm_kzalloc(&pdev->dev,
662 			     struct_size(power, irqs, axp_data->num_irq_names),
663 			     GFP_KERNEL);
664 	if (!power)
665 		return -ENOMEM;
666 
667 	platform_set_drvdata(pdev, power);
668 
669 	power->dev = &pdev->dev;
670 	power->axp_data = axp_data;
671 	power->regmap = axp20x->regmap;
672 	power->num_irqs = axp_data->num_irq_names;
673 
674 	power->curr_lim_fld = devm_regmap_field_alloc(&pdev->dev, power->regmap,
675 						      axp_data->curr_lim_fld);
676 	if (IS_ERR(power->curr_lim_fld))
677 		return PTR_ERR(power->curr_lim_fld);
678 
679 	ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap,
680 						 axp_data->vbus_valid_bit,
681 						 &power->vbus_valid_bit);
682 	if (ret)
683 		return ret;
684 
685 	ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap,
686 						 axp_data->vbus_mon_bit,
687 						 &power->vbus_mon_bit);
688 	if (ret)
689 		return ret;
690 
691 	ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap,
692 						 axp_data->usb_bc_en_bit,
693 						 &power->usb_bc_en_bit);
694 	if (ret)
695 		return ret;
696 
697 	ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap,
698 						 axp_data->usb_bc_det_fld,
699 						 &power->usb_bc_det_fld);
700 	if (ret)
701 		return ret;
702 
703 	ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap,
704 						 axp_data->vbus_disable_bit,
705 						 &power->vbus_disable_bit);
706 	if (ret)
707 		return ret;
708 
709 	ret = devm_delayed_work_autocancel(&pdev->dev, &power->vbus_detect,
710 					   axp20x_usb_power_poll_vbus);
711 	if (ret)
712 		return ret;
713 
714 	if (power->vbus_mon_bit) {
715 		/* Enable vbus valid checking */
716 		ret = regmap_field_write(power->vbus_mon_bit, 1);
717 		if (ret)
718 			return ret;
719 
720 		if (IS_ENABLED(CONFIG_AXP20X_ADC))
721 			ret = configure_iio_channels(pdev, power);
722 		else
723 			ret = configure_adc_registers(power);
724 
725 		if (ret)
726 			return ret;
727 	}
728 
729 	if (power->usb_bc_en_bit) {
730 		/* Enable USB Battery Charging specification detection */
731 		ret = regmap_field_write(power->usb_bc_en_bit, 1);
732 		if (ret)
733 			return ret;
734 	}
735 
736 	psy_cfg.of_node = pdev->dev.of_node;
737 	psy_cfg.drv_data = power;
738 
739 	power->supply = devm_power_supply_register(&pdev->dev,
740 						   axp_data->power_desc,
741 						   &psy_cfg);
742 	if (IS_ERR(power->supply))
743 		return PTR_ERR(power->supply);
744 
745 	/* Request irqs after registering, as irqs may trigger immediately */
746 	for (i = 0; i < axp_data->num_irq_names; i++) {
747 		irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]);
748 		if (irq < 0)
749 			return irq;
750 
751 		power->irqs[i] = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
752 		ret = devm_request_any_context_irq(&pdev->dev, power->irqs[i],
753 						   axp20x_usb_power_irq, 0,
754 						   DRVNAME, power);
755 		if (ret < 0) {
756 			dev_err(&pdev->dev, "Error requesting %s IRQ: %d\n",
757 				axp_data->irq_names[i], ret);
758 			return ret;
759 		}
760 	}
761 
762 	if (axp20x_usb_vbus_needs_polling(power))
763 		queue_delayed_work(system_power_efficient_wq, &power->vbus_detect, 0);
764 
765 	return 0;
766 }
767 
768 static const struct of_device_id axp20x_usb_power_match[] = {
769 	{
770 		.compatible = "x-powers,axp192-usb-power-supply",
771 		.data = &axp192_data,
772 	}, {
773 		.compatible = "x-powers,axp202-usb-power-supply",
774 		.data = &axp202_data,
775 	}, {
776 		.compatible = "x-powers,axp221-usb-power-supply",
777 		.data = &axp221_data,
778 	}, {
779 		.compatible = "x-powers,axp223-usb-power-supply",
780 		.data = &axp223_data,
781 	}, {
782 		.compatible = "x-powers,axp813-usb-power-supply",
783 		.data = &axp813_data,
784 	}, { /* sentinel */ }
785 };
786 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
787 
788 static struct platform_driver axp20x_usb_power_driver = {
789 	.probe = axp20x_usb_power_probe,
790 	.driver = {
791 		.name		= DRVNAME,
792 		.of_match_table	= axp20x_usb_power_match,
793 		.pm		= &axp20x_usb_power_pm_ops,
794 	},
795 };
796 
797 module_platform_driver(axp20x_usb_power_driver);
798 
799 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
800 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
801 MODULE_LICENSE("GPL");
802