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/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/axp20x.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_device.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 AXP20X_PWR_STATUS_VBUS_PRESENT	BIT(5)
29 #define AXP20X_PWR_STATUS_VBUS_USED	BIT(4)
30 
31 #define AXP20X_USB_STATUS_VBUS_VALID	BIT(2)
32 
33 #define AXP20X_VBUS_PATH_SEL		BIT(7)
34 #define AXP20X_VBUS_PATH_SEL_OFFSET	7
35 
36 #define AXP20X_VBUS_VHOLD_uV(b)		(4000000 + (((b) >> 3) & 7) * 100000)
37 #define AXP20X_VBUS_VHOLD_MASK		GENMASK(5, 3)
38 #define AXP20X_VBUS_VHOLD_OFFSET	3
39 #define AXP20X_VBUS_CLIMIT_MASK		3
40 #define AXP20X_VBUS_CLIMIT_900mA	0
41 #define AXP20X_VBUS_CLIMIT_500mA	1
42 #define AXP20X_VBUS_CLIMIT_100mA	2
43 #define AXP20X_VBUS_CLIMIT_NONE		3
44 
45 #define AXP813_VBUS_CLIMIT_900mA	0
46 #define AXP813_VBUS_CLIMIT_1500mA	1
47 #define AXP813_VBUS_CLIMIT_2000mA	2
48 #define AXP813_VBUS_CLIMIT_2500mA	3
49 
50 #define AXP20X_ADC_EN1_VBUS_CURR	BIT(2)
51 #define AXP20X_ADC_EN1_VBUS_VOLT	BIT(3)
52 
53 #define AXP20X_VBUS_MON_VBUS_VALID	BIT(3)
54 
55 #define AXP813_BC_EN		BIT(0)
56 
57 /*
58  * Note do not raise the debounce time, we must report Vusb high within
59  * 100ms otherwise we get Vbus errors in musb.
60  */
61 #define DEBOUNCE_TIME			msecs_to_jiffies(50)
62 
63 struct axp20x_usb_power {
64 	struct regmap *regmap;
65 	struct power_supply *supply;
66 	enum axp20x_variants axp20x_id;
67 	struct iio_channel *vbus_v;
68 	struct iio_channel *vbus_i;
69 	struct delayed_work vbus_detect;
70 	unsigned int old_status;
71 	unsigned int online;
72 	unsigned int num_irqs;
73 	unsigned int irqs[];
74 };
75 
76 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power)
77 {
78 	/*
79 	 * Polling is only necessary while VBUS is offline. While online, a
80 	 * present->absent transition implies an online->offline transition
81 	 * and will trigger the VBUS_REMOVAL IRQ.
82 	 */
83 	if (power->axp20x_id >= AXP221_ID && !power->online)
84 		return true;
85 
86 	return false;
87 }
88 
89 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
90 {
91 	struct axp20x_usb_power *power = devid;
92 
93 	power_supply_changed(power->supply);
94 
95 	mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
96 
97 	return IRQ_HANDLED;
98 }
99 
100 static void axp20x_usb_power_poll_vbus(struct work_struct *work)
101 {
102 	struct axp20x_usb_power *power =
103 		container_of(work, struct axp20x_usb_power, vbus_detect.work);
104 	unsigned int val;
105 	int ret;
106 
107 	ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val);
108 	if (ret)
109 		goto out;
110 
111 	val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED);
112 	if (val != power->old_status)
113 		power_supply_changed(power->supply);
114 
115 	power->old_status = val;
116 	power->online = val & AXP20X_PWR_STATUS_VBUS_USED;
117 
118 out:
119 	if (axp20x_usb_vbus_needs_polling(power))
120 		mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
121 }
122 
123 static int axp20x_get_current_max(struct axp20x_usb_power *power, int *val)
124 {
125 	unsigned int v;
126 	int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
127 
128 	if (ret)
129 		return ret;
130 
131 	switch (v & AXP20X_VBUS_CLIMIT_MASK) {
132 	case AXP20X_VBUS_CLIMIT_100mA:
133 		if (power->axp20x_id == AXP221_ID)
134 			*val = -1; /* No 100mA limit */
135 		else
136 			*val = 100000;
137 		break;
138 	case AXP20X_VBUS_CLIMIT_500mA:
139 		*val = 500000;
140 		break;
141 	case AXP20X_VBUS_CLIMIT_900mA:
142 		*val = 900000;
143 		break;
144 	case AXP20X_VBUS_CLIMIT_NONE:
145 		*val = -1;
146 		break;
147 	}
148 
149 	return 0;
150 }
151 
152 static int axp813_get_current_max(struct axp20x_usb_power *power, int *val)
153 {
154 	unsigned int v;
155 	int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
156 
157 	if (ret)
158 		return ret;
159 
160 	switch (v & AXP20X_VBUS_CLIMIT_MASK) {
161 	case AXP813_VBUS_CLIMIT_900mA:
162 		*val = 900000;
163 		break;
164 	case AXP813_VBUS_CLIMIT_1500mA:
165 		*val = 1500000;
166 		break;
167 	case AXP813_VBUS_CLIMIT_2000mA:
168 		*val = 2000000;
169 		break;
170 	case AXP813_VBUS_CLIMIT_2500mA:
171 		*val = 2500000;
172 		break;
173 	}
174 	return 0;
175 }
176 
177 static int axp20x_usb_power_get_property(struct power_supply *psy,
178 	enum power_supply_property psp, union power_supply_propval *val)
179 {
180 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
181 	unsigned int input, v;
182 	int ret;
183 
184 	switch (psp) {
185 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
186 		ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
187 		if (ret)
188 			return ret;
189 
190 		val->intval = AXP20X_VBUS_VHOLD_uV(v);
191 		return 0;
192 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
193 		if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
194 			ret = iio_read_channel_processed(power->vbus_v,
195 							 &val->intval);
196 			if (ret)
197 				return ret;
198 
199 			/*
200 			 * IIO framework gives mV but Power Supply framework
201 			 * gives uV.
202 			 */
203 			val->intval *= 1000;
204 			return 0;
205 		}
206 
207 		ret = axp20x_read_variable_width(power->regmap,
208 						 AXP20X_VBUS_V_ADC_H, 12);
209 		if (ret < 0)
210 			return ret;
211 
212 		val->intval = ret * 1700; /* 1 step = 1.7 mV */
213 		return 0;
214 	case POWER_SUPPLY_PROP_CURRENT_MAX:
215 		if (power->axp20x_id == AXP813_ID)
216 			return axp813_get_current_max(power, &val->intval);
217 		return axp20x_get_current_max(power, &val->intval);
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 	default:
241 		break;
242 	}
243 
244 	/* All the properties below need the input-status reg value */
245 	ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
246 	if (ret)
247 		return ret;
248 
249 	switch (psp) {
250 	case POWER_SUPPLY_PROP_HEALTH:
251 		if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
252 			val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
253 			break;
254 		}
255 
256 		val->intval = POWER_SUPPLY_HEALTH_GOOD;
257 
258 		if (power->axp20x_id == AXP202_ID) {
259 			ret = regmap_read(power->regmap,
260 					  AXP20X_USB_OTG_STATUS, &v);
261 			if (ret)
262 				return ret;
263 
264 			if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
265 				val->intval =
266 					POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
267 		}
268 		break;
269 	case POWER_SUPPLY_PROP_PRESENT:
270 		val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
271 		break;
272 	case POWER_SUPPLY_PROP_ONLINE:
273 		val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
274 		break;
275 	default:
276 		return -EINVAL;
277 	}
278 
279 	return 0;
280 }
281 
282 static int axp813_usb_power_set_online(struct axp20x_usb_power *power,
283 				       int intval)
284 {
285 	int val = !intval << AXP20X_VBUS_PATH_SEL_OFFSET;
286 
287 	return regmap_update_bits(power->regmap,
288 				  AXP20X_VBUS_IPSOUT_MGMT,
289 				  AXP20X_VBUS_PATH_SEL, val);
290 }
291 
292 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
293 					    int intval)
294 {
295 	int val;
296 
297 	switch (intval) {
298 	case 4000000:
299 	case 4100000:
300 	case 4200000:
301 	case 4300000:
302 	case 4400000:
303 	case 4500000:
304 	case 4600000:
305 	case 4700000:
306 		val = (intval - 4000000) / 100000;
307 		return regmap_update_bits(power->regmap,
308 					  AXP20X_VBUS_IPSOUT_MGMT,
309 					  AXP20X_VBUS_VHOLD_MASK,
310 					  val << AXP20X_VBUS_VHOLD_OFFSET);
311 	default:
312 		return -EINVAL;
313 	}
314 
315 	return -EINVAL;
316 }
317 
318 static int axp813_usb_power_set_current_max(struct axp20x_usb_power *power,
319 					    int intval)
320 {
321 	int val;
322 
323 	switch (intval) {
324 	case 900000:
325 		return regmap_update_bits(power->regmap,
326 					  AXP20X_VBUS_IPSOUT_MGMT,
327 					  AXP20X_VBUS_CLIMIT_MASK,
328 					  AXP813_VBUS_CLIMIT_900mA);
329 	case 1500000:
330 	case 2000000:
331 	case 2500000:
332 		val = (intval - 1000000) / 500000;
333 		return regmap_update_bits(power->regmap,
334 					  AXP20X_VBUS_IPSOUT_MGMT,
335 					  AXP20X_VBUS_CLIMIT_MASK, val);
336 	default:
337 		return -EINVAL;
338 	}
339 
340 	return -EINVAL;
341 }
342 
343 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
344 					    int intval)
345 {
346 	int val;
347 
348 	switch (intval) {
349 	case 100000:
350 		if (power->axp20x_id == AXP221_ID)
351 			return -EINVAL;
352 		/* fall through */
353 	case 500000:
354 	case 900000:
355 		val = (900000 - intval) / 400000;
356 		return regmap_update_bits(power->regmap,
357 					  AXP20X_VBUS_IPSOUT_MGMT,
358 					  AXP20X_VBUS_CLIMIT_MASK, val);
359 	default:
360 		return -EINVAL;
361 	}
362 
363 	return -EINVAL;
364 }
365 
366 static int axp20x_usb_power_set_property(struct power_supply *psy,
367 					 enum power_supply_property psp,
368 					 const union power_supply_propval *val)
369 {
370 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
371 
372 	switch (psp) {
373 	case POWER_SUPPLY_PROP_ONLINE:
374 		if (power->axp20x_id != AXP813_ID)
375 			return -EINVAL;
376 		return axp813_usb_power_set_online(power, val->intval);
377 
378 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
379 		return axp20x_usb_power_set_voltage_min(power, val->intval);
380 
381 	case POWER_SUPPLY_PROP_CURRENT_MAX:
382 		if (power->axp20x_id == AXP813_ID)
383 			return axp813_usb_power_set_current_max(power,
384 								val->intval);
385 		return axp20x_usb_power_set_current_max(power, val->intval);
386 
387 	default:
388 		return -EINVAL;
389 	}
390 
391 	return -EINVAL;
392 }
393 
394 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
395 					   enum power_supply_property psp)
396 {
397 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
398 
399 	/*
400 	 * The VBUS path select flag works differently on on AXP288 and newer:
401 	 *  - On AXP20x and AXP22x, the flag enables VBUS (ignoring N_VBUSEN).
402 	 *  - On AXP288 and AXP8xx, the flag disables VBUS (ignoring N_VBUSEN).
403 	 * We only expose the control on variants where it can be used to force
404 	 * the VBUS input offline.
405 	 */
406 	if (psp == POWER_SUPPLY_PROP_ONLINE)
407 		return power->axp20x_id == AXP813_ID;
408 
409 	return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
410 	       psp == POWER_SUPPLY_PROP_CURRENT_MAX;
411 }
412 
413 static enum power_supply_property axp20x_usb_power_properties[] = {
414 	POWER_SUPPLY_PROP_HEALTH,
415 	POWER_SUPPLY_PROP_PRESENT,
416 	POWER_SUPPLY_PROP_ONLINE,
417 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
418 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
419 	POWER_SUPPLY_PROP_CURRENT_MAX,
420 	POWER_SUPPLY_PROP_CURRENT_NOW,
421 };
422 
423 static enum power_supply_property axp22x_usb_power_properties[] = {
424 	POWER_SUPPLY_PROP_HEALTH,
425 	POWER_SUPPLY_PROP_PRESENT,
426 	POWER_SUPPLY_PROP_ONLINE,
427 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
428 	POWER_SUPPLY_PROP_CURRENT_MAX,
429 };
430 
431 static const struct power_supply_desc axp20x_usb_power_desc = {
432 	.name = "axp20x-usb",
433 	.type = POWER_SUPPLY_TYPE_USB,
434 	.properties = axp20x_usb_power_properties,
435 	.num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
436 	.property_is_writeable = axp20x_usb_power_prop_writeable,
437 	.get_property = axp20x_usb_power_get_property,
438 	.set_property = axp20x_usb_power_set_property,
439 };
440 
441 static const struct power_supply_desc axp22x_usb_power_desc = {
442 	.name = "axp20x-usb",
443 	.type = POWER_SUPPLY_TYPE_USB,
444 	.properties = axp22x_usb_power_properties,
445 	.num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
446 	.property_is_writeable = axp20x_usb_power_prop_writeable,
447 	.get_property = axp20x_usb_power_get_property,
448 	.set_property = axp20x_usb_power_set_property,
449 };
450 
451 static const char * const axp20x_irq_names[] = {
452 	"VBUS_PLUGIN",
453 	"VBUS_REMOVAL",
454 	"VBUS_VALID",
455 	"VBUS_NOT_VALID",
456 };
457 
458 static const char * const axp22x_irq_names[] = {
459 	"VBUS_PLUGIN",
460 	"VBUS_REMOVAL",
461 };
462 
463 struct axp_data {
464 	const struct power_supply_desc	*power_desc;
465 	const char * const		*irq_names;
466 	unsigned int			num_irq_names;
467 	enum axp20x_variants		axp20x_id;
468 };
469 
470 static const struct axp_data axp202_data = {
471 	.power_desc	= &axp20x_usb_power_desc,
472 	.irq_names	= axp20x_irq_names,
473 	.num_irq_names	= ARRAY_SIZE(axp20x_irq_names),
474 	.axp20x_id	= AXP202_ID,
475 };
476 
477 static const struct axp_data axp221_data = {
478 	.power_desc	= &axp22x_usb_power_desc,
479 	.irq_names	= axp22x_irq_names,
480 	.num_irq_names	= ARRAY_SIZE(axp22x_irq_names),
481 	.axp20x_id	= AXP221_ID,
482 };
483 
484 static const struct axp_data axp223_data = {
485 	.power_desc	= &axp22x_usb_power_desc,
486 	.irq_names	= axp22x_irq_names,
487 	.num_irq_names	= ARRAY_SIZE(axp22x_irq_names),
488 	.axp20x_id	= AXP223_ID,
489 };
490 
491 static const struct axp_data axp813_data = {
492 	.power_desc	= &axp22x_usb_power_desc,
493 	.irq_names	= axp22x_irq_names,
494 	.num_irq_names	= ARRAY_SIZE(axp22x_irq_names),
495 	.axp20x_id	= AXP813_ID,
496 };
497 
498 #ifdef CONFIG_PM_SLEEP
499 static int axp20x_usb_power_suspend(struct device *dev)
500 {
501 	struct axp20x_usb_power *power = dev_get_drvdata(dev);
502 	int i = 0;
503 
504 	/*
505 	 * Allow wake via VBUS_PLUGIN only.
506 	 *
507 	 * As nested threaded IRQs are not automatically disabled during
508 	 * suspend, we must explicitly disable the remainder of the IRQs.
509 	 */
510 	if (device_may_wakeup(&power->supply->dev))
511 		enable_irq_wake(power->irqs[i++]);
512 	while (i < power->num_irqs)
513 		disable_irq(power->irqs[i++]);
514 
515 	return 0;
516 }
517 
518 static int axp20x_usb_power_resume(struct device *dev)
519 {
520 	struct axp20x_usb_power *power = dev_get_drvdata(dev);
521 	int i = 0;
522 
523 	if (device_may_wakeup(&power->supply->dev))
524 		disable_irq_wake(power->irqs[i++]);
525 	while (i < power->num_irqs)
526 		enable_irq(power->irqs[i++]);
527 
528 	mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
529 
530 	return 0;
531 }
532 #endif
533 
534 static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops, axp20x_usb_power_suspend,
535 						  axp20x_usb_power_resume);
536 
537 static int configure_iio_channels(struct platform_device *pdev,
538 				  struct axp20x_usb_power *power)
539 {
540 	power->vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
541 	if (IS_ERR(power->vbus_v)) {
542 		if (PTR_ERR(power->vbus_v) == -ENODEV)
543 			return -EPROBE_DEFER;
544 		return PTR_ERR(power->vbus_v);
545 	}
546 
547 	power->vbus_i = devm_iio_channel_get(&pdev->dev, "vbus_i");
548 	if (IS_ERR(power->vbus_i)) {
549 		if (PTR_ERR(power->vbus_i) == -ENODEV)
550 			return -EPROBE_DEFER;
551 		return PTR_ERR(power->vbus_i);
552 	}
553 
554 	return 0;
555 }
556 
557 static int configure_adc_registers(struct axp20x_usb_power *power)
558 {
559 	/* Enable vbus voltage and current measurement */
560 	return regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
561 				  AXP20X_ADC_EN1_VBUS_CURR |
562 				  AXP20X_ADC_EN1_VBUS_VOLT,
563 				  AXP20X_ADC_EN1_VBUS_CURR |
564 				  AXP20X_ADC_EN1_VBUS_VOLT);
565 }
566 
567 static int axp20x_usb_power_probe(struct platform_device *pdev)
568 {
569 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
570 	struct power_supply_config psy_cfg = {};
571 	struct axp20x_usb_power *power;
572 	const struct axp_data *axp_data;
573 	int i, irq, ret;
574 
575 	if (!of_device_is_available(pdev->dev.of_node))
576 		return -ENODEV;
577 
578 	if (!axp20x) {
579 		dev_err(&pdev->dev, "Parent drvdata not set\n");
580 		return -EINVAL;
581 	}
582 
583 	axp_data = of_device_get_match_data(&pdev->dev);
584 
585 	power = devm_kzalloc(&pdev->dev,
586 			     struct_size(power, irqs, axp_data->num_irq_names),
587 			     GFP_KERNEL);
588 	if (!power)
589 		return -ENOMEM;
590 
591 	platform_set_drvdata(pdev, power);
592 
593 	power->axp20x_id = axp_data->axp20x_id;
594 	power->regmap = axp20x->regmap;
595 	power->num_irqs = axp_data->num_irq_names;
596 
597 	if (power->axp20x_id == AXP202_ID) {
598 		/* Enable vbus valid checking */
599 		ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
600 					 AXP20X_VBUS_MON_VBUS_VALID,
601 					 AXP20X_VBUS_MON_VBUS_VALID);
602 		if (ret)
603 			return ret;
604 
605 		if (IS_ENABLED(CONFIG_AXP20X_ADC))
606 			ret = configure_iio_channels(pdev, power);
607 		else
608 			ret = configure_adc_registers(power);
609 
610 		if (ret)
611 			return ret;
612 	}
613 
614 	if (power->axp20x_id == AXP813_ID) {
615 		/* Enable USB Battery Charging specification detection */
616 		regmap_update_bits(axp20x->regmap, AXP288_BC_GLOBAL,
617 				   AXP813_BC_EN, AXP813_BC_EN);
618 	}
619 
620 	psy_cfg.of_node = pdev->dev.of_node;
621 	psy_cfg.drv_data = power;
622 
623 	power->supply = devm_power_supply_register(&pdev->dev,
624 						   axp_data->power_desc,
625 						   &psy_cfg);
626 	if (IS_ERR(power->supply))
627 		return PTR_ERR(power->supply);
628 
629 	/* Request irqs after registering, as irqs may trigger immediately */
630 	for (i = 0; i < axp_data->num_irq_names; i++) {
631 		irq = platform_get_irq_byname(pdev, axp_data->irq_names[i]);
632 		if (irq < 0) {
633 			dev_err(&pdev->dev, "No IRQ for %s: %d\n",
634 				axp_data->irq_names[i], irq);
635 			return irq;
636 		}
637 		power->irqs[i] = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
638 		ret = devm_request_any_context_irq(&pdev->dev, power->irqs[i],
639 						   axp20x_usb_power_irq, 0,
640 						   DRVNAME, power);
641 		if (ret < 0) {
642 			dev_err(&pdev->dev, "Error requesting %s IRQ: %d\n",
643 				axp_data->irq_names[i], ret);
644 			return ret;
645 		}
646 	}
647 
648 	INIT_DELAYED_WORK(&power->vbus_detect, axp20x_usb_power_poll_vbus);
649 	if (axp20x_usb_vbus_needs_polling(power))
650 		queue_delayed_work(system_wq, &power->vbus_detect, 0);
651 
652 	return 0;
653 }
654 
655 static int axp20x_usb_power_remove(struct platform_device *pdev)
656 {
657 	struct axp20x_usb_power *power = platform_get_drvdata(pdev);
658 
659 	cancel_delayed_work_sync(&power->vbus_detect);
660 
661 	return 0;
662 }
663 
664 static const struct of_device_id axp20x_usb_power_match[] = {
665 	{
666 		.compatible = "x-powers,axp202-usb-power-supply",
667 		.data = &axp202_data,
668 	}, {
669 		.compatible = "x-powers,axp221-usb-power-supply",
670 		.data = &axp221_data,
671 	}, {
672 		.compatible = "x-powers,axp223-usb-power-supply",
673 		.data = &axp223_data,
674 	}, {
675 		.compatible = "x-powers,axp813-usb-power-supply",
676 		.data = &axp813_data,
677 	}, { /* sentinel */ }
678 };
679 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
680 
681 static struct platform_driver axp20x_usb_power_driver = {
682 	.probe = axp20x_usb_power_probe,
683 	.remove = axp20x_usb_power_remove,
684 	.driver = {
685 		.name		= DRVNAME,
686 		.of_match_table	= axp20x_usb_power_match,
687 		.pm		= &axp20x_usb_power_pm_ops,
688 	},
689 };
690 
691 module_platform_driver(axp20x_usb_power_driver);
692 
693 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
694 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
695 MODULE_LICENSE("GPL");
696