xref: /linux/drivers/power/supply/axp20x_usb_power.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
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/power_supply.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/iio/consumer.h>
23 #include <linux/workqueue.h>
24 
25 #define DRVNAME "axp20x-usb-power-supply"
26 
27 #define AXP20X_PWR_STATUS_VBUS_PRESENT	BIT(5)
28 #define AXP20X_PWR_STATUS_VBUS_USED	BIT(4)
29 
30 #define AXP20X_USB_STATUS_VBUS_VALID	BIT(2)
31 
32 #define AXP20X_VBUS_VHOLD_uV(b)		(4000000 + (((b) >> 3) & 7) * 100000)
33 #define AXP20X_VBUS_VHOLD_MASK		GENMASK(5, 3)
34 #define AXP20X_VBUS_VHOLD_OFFSET	3
35 #define AXP20X_VBUS_CLIMIT_MASK		3
36 #define AXP20X_VBUS_CLIMIT_900mA	0
37 #define AXP20X_VBUS_CLIMIT_500mA	1
38 #define AXP20X_VBUS_CLIMIT_100mA	2
39 #define AXP20X_VBUS_CLIMIT_NONE		3
40 
41 #define AXP813_VBUS_CLIMIT_900mA	0
42 #define AXP813_VBUS_CLIMIT_1500mA	1
43 #define AXP813_VBUS_CLIMIT_2000mA	2
44 #define AXP813_VBUS_CLIMIT_2500mA	3
45 
46 #define AXP20X_ADC_EN1_VBUS_CURR	BIT(2)
47 #define AXP20X_ADC_EN1_VBUS_VOLT	BIT(3)
48 
49 #define AXP20X_VBUS_MON_VBUS_VALID	BIT(3)
50 
51 #define AXP813_BC_EN		BIT(0)
52 
53 /*
54  * Note do not raise the debounce time, we must report Vusb high within
55  * 100ms otherwise we get Vbus errors in musb.
56  */
57 #define DEBOUNCE_TIME			msecs_to_jiffies(50)
58 
59 struct axp20x_usb_power {
60 	struct device_node *np;
61 	struct regmap *regmap;
62 	struct power_supply *supply;
63 	enum axp20x_variants axp20x_id;
64 	struct iio_channel *vbus_v;
65 	struct iio_channel *vbus_i;
66 	struct delayed_work vbus_detect;
67 	unsigned int old_status;
68 };
69 
70 static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
71 {
72 	struct axp20x_usb_power *power = devid;
73 
74 	power_supply_changed(power->supply);
75 
76 	return IRQ_HANDLED;
77 }
78 
79 static void axp20x_usb_power_poll_vbus(struct work_struct *work)
80 {
81 	struct axp20x_usb_power *power =
82 		container_of(work, struct axp20x_usb_power, vbus_detect.work);
83 	unsigned int val;
84 	int ret;
85 
86 	ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val);
87 	if (ret)
88 		goto out;
89 
90 	val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED);
91 	if (val != power->old_status)
92 		power_supply_changed(power->supply);
93 
94 	power->old_status = val;
95 
96 out:
97 	mod_delayed_work(system_wq, &power->vbus_detect, DEBOUNCE_TIME);
98 }
99 
100 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power)
101 {
102 	if (power->axp20x_id >= AXP221_ID)
103 		return true;
104 
105 	return false;
106 }
107 
108 static int axp20x_get_current_max(struct axp20x_usb_power *power, int *val)
109 {
110 	unsigned int v;
111 	int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
112 
113 	if (ret)
114 		return ret;
115 
116 	switch (v & AXP20X_VBUS_CLIMIT_MASK) {
117 	case AXP20X_VBUS_CLIMIT_100mA:
118 		if (power->axp20x_id == AXP221_ID)
119 			*val = -1; /* No 100mA limit */
120 		else
121 			*val = 100000;
122 		break;
123 	case AXP20X_VBUS_CLIMIT_500mA:
124 		*val = 500000;
125 		break;
126 	case AXP20X_VBUS_CLIMIT_900mA:
127 		*val = 900000;
128 		break;
129 	case AXP20X_VBUS_CLIMIT_NONE:
130 		*val = -1;
131 		break;
132 	}
133 
134 	return 0;
135 }
136 
137 static int axp813_get_current_max(struct axp20x_usb_power *power, int *val)
138 {
139 	unsigned int v;
140 	int ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
141 
142 	if (ret)
143 		return ret;
144 
145 	switch (v & AXP20X_VBUS_CLIMIT_MASK) {
146 	case AXP813_VBUS_CLIMIT_900mA:
147 		*val = 900000;
148 		break;
149 	case AXP813_VBUS_CLIMIT_1500mA:
150 		*val = 1500000;
151 		break;
152 	case AXP813_VBUS_CLIMIT_2000mA:
153 		*val = 2000000;
154 		break;
155 	case AXP813_VBUS_CLIMIT_2500mA:
156 		*val = 2500000;
157 		break;
158 	}
159 	return 0;
160 }
161 
162 static int axp20x_usb_power_get_property(struct power_supply *psy,
163 	enum power_supply_property psp, union power_supply_propval *val)
164 {
165 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
166 	unsigned int input, v;
167 	int ret;
168 
169 	switch (psp) {
170 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
171 		ret = regmap_read(power->regmap, AXP20X_VBUS_IPSOUT_MGMT, &v);
172 		if (ret)
173 			return ret;
174 
175 		val->intval = AXP20X_VBUS_VHOLD_uV(v);
176 		return 0;
177 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
178 		if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
179 			ret = iio_read_channel_processed(power->vbus_v,
180 							 &val->intval);
181 			if (ret)
182 				return ret;
183 
184 			/*
185 			 * IIO framework gives mV but Power Supply framework
186 			 * gives uV.
187 			 */
188 			val->intval *= 1000;
189 			return 0;
190 		}
191 
192 		ret = axp20x_read_variable_width(power->regmap,
193 						 AXP20X_VBUS_V_ADC_H, 12);
194 		if (ret < 0)
195 			return ret;
196 
197 		val->intval = ret * 1700; /* 1 step = 1.7 mV */
198 		return 0;
199 	case POWER_SUPPLY_PROP_CURRENT_MAX:
200 		if (power->axp20x_id == AXP813_ID)
201 			return axp813_get_current_max(power, &val->intval);
202 		return axp20x_get_current_max(power, &val->intval);
203 	case POWER_SUPPLY_PROP_CURRENT_NOW:
204 		if (IS_ENABLED(CONFIG_AXP20X_ADC)) {
205 			ret = iio_read_channel_processed(power->vbus_i,
206 							 &val->intval);
207 			if (ret)
208 				return ret;
209 
210 			/*
211 			 * IIO framework gives mA but Power Supply framework
212 			 * gives uA.
213 			 */
214 			val->intval *= 1000;
215 			return 0;
216 		}
217 
218 		ret = axp20x_read_variable_width(power->regmap,
219 						 AXP20X_VBUS_I_ADC_H, 12);
220 		if (ret < 0)
221 			return ret;
222 
223 		val->intval = ret * 375; /* 1 step = 0.375 mA */
224 		return 0;
225 	default:
226 		break;
227 	}
228 
229 	/* All the properties below need the input-status reg value */
230 	ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &input);
231 	if (ret)
232 		return ret;
233 
234 	switch (psp) {
235 	case POWER_SUPPLY_PROP_HEALTH:
236 		if (!(input & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
237 			val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
238 			break;
239 		}
240 
241 		val->intval = POWER_SUPPLY_HEALTH_GOOD;
242 
243 		if (power->axp20x_id == AXP202_ID) {
244 			ret = regmap_read(power->regmap,
245 					  AXP20X_USB_OTG_STATUS, &v);
246 			if (ret)
247 				return ret;
248 
249 			if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
250 				val->intval =
251 					POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
252 		}
253 		break;
254 	case POWER_SUPPLY_PROP_PRESENT:
255 		val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
256 		break;
257 	case POWER_SUPPLY_PROP_ONLINE:
258 		val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_USED);
259 		break;
260 	default:
261 		return -EINVAL;
262 	}
263 
264 	return 0;
265 }
266 
267 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power *power,
268 					    int intval)
269 {
270 	int val;
271 
272 	switch (intval) {
273 	case 4000000:
274 	case 4100000:
275 	case 4200000:
276 	case 4300000:
277 	case 4400000:
278 	case 4500000:
279 	case 4600000:
280 	case 4700000:
281 		val = (intval - 4000000) / 100000;
282 		return regmap_update_bits(power->regmap,
283 					  AXP20X_VBUS_IPSOUT_MGMT,
284 					  AXP20X_VBUS_VHOLD_MASK,
285 					  val << AXP20X_VBUS_VHOLD_OFFSET);
286 	default:
287 		return -EINVAL;
288 	}
289 
290 	return -EINVAL;
291 }
292 
293 static int axp813_usb_power_set_current_max(struct axp20x_usb_power *power,
294 					    int intval)
295 {
296 	int val;
297 
298 	switch (intval) {
299 	case 900000:
300 		return regmap_update_bits(power->regmap,
301 					  AXP20X_VBUS_IPSOUT_MGMT,
302 					  AXP20X_VBUS_CLIMIT_MASK,
303 					  AXP813_VBUS_CLIMIT_900mA);
304 	case 1500000:
305 	case 2000000:
306 	case 2500000:
307 		val = (intval - 1000000) / 500000;
308 		return regmap_update_bits(power->regmap,
309 					  AXP20X_VBUS_IPSOUT_MGMT,
310 					  AXP20X_VBUS_CLIMIT_MASK, val);
311 	default:
312 		return -EINVAL;
313 	}
314 
315 	return -EINVAL;
316 }
317 
318 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power *power,
319 					    int intval)
320 {
321 	int val;
322 
323 	switch (intval) {
324 	case 100000:
325 		if (power->axp20x_id == AXP221_ID)
326 			return -EINVAL;
327 		/* fall through */
328 	case 500000:
329 	case 900000:
330 		val = (900000 - intval) / 400000;
331 		return regmap_update_bits(power->regmap,
332 					  AXP20X_VBUS_IPSOUT_MGMT,
333 					  AXP20X_VBUS_CLIMIT_MASK, val);
334 	default:
335 		return -EINVAL;
336 	}
337 
338 	return -EINVAL;
339 }
340 
341 static int axp20x_usb_power_set_property(struct power_supply *psy,
342 					 enum power_supply_property psp,
343 					 const union power_supply_propval *val)
344 {
345 	struct axp20x_usb_power *power = power_supply_get_drvdata(psy);
346 
347 	switch (psp) {
348 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
349 		return axp20x_usb_power_set_voltage_min(power, val->intval);
350 
351 	case POWER_SUPPLY_PROP_CURRENT_MAX:
352 		if (power->axp20x_id == AXP813_ID)
353 			return axp813_usb_power_set_current_max(power,
354 								val->intval);
355 		return axp20x_usb_power_set_current_max(power, val->intval);
356 
357 	default:
358 		return -EINVAL;
359 	}
360 
361 	return -EINVAL;
362 }
363 
364 static int axp20x_usb_power_prop_writeable(struct power_supply *psy,
365 					   enum power_supply_property psp)
366 {
367 	return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN ||
368 	       psp == POWER_SUPPLY_PROP_CURRENT_MAX;
369 }
370 
371 static enum power_supply_property axp20x_usb_power_properties[] = {
372 	POWER_SUPPLY_PROP_HEALTH,
373 	POWER_SUPPLY_PROP_PRESENT,
374 	POWER_SUPPLY_PROP_ONLINE,
375 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
376 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
377 	POWER_SUPPLY_PROP_CURRENT_MAX,
378 	POWER_SUPPLY_PROP_CURRENT_NOW,
379 };
380 
381 static enum power_supply_property axp22x_usb_power_properties[] = {
382 	POWER_SUPPLY_PROP_HEALTH,
383 	POWER_SUPPLY_PROP_PRESENT,
384 	POWER_SUPPLY_PROP_ONLINE,
385 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
386 	POWER_SUPPLY_PROP_CURRENT_MAX,
387 };
388 
389 static const struct power_supply_desc axp20x_usb_power_desc = {
390 	.name = "axp20x-usb",
391 	.type = POWER_SUPPLY_TYPE_USB,
392 	.properties = axp20x_usb_power_properties,
393 	.num_properties = ARRAY_SIZE(axp20x_usb_power_properties),
394 	.property_is_writeable = axp20x_usb_power_prop_writeable,
395 	.get_property = axp20x_usb_power_get_property,
396 	.set_property = axp20x_usb_power_set_property,
397 };
398 
399 static const struct power_supply_desc axp22x_usb_power_desc = {
400 	.name = "axp20x-usb",
401 	.type = POWER_SUPPLY_TYPE_USB,
402 	.properties = axp22x_usb_power_properties,
403 	.num_properties = ARRAY_SIZE(axp22x_usb_power_properties),
404 	.property_is_writeable = axp20x_usb_power_prop_writeable,
405 	.get_property = axp20x_usb_power_get_property,
406 	.set_property = axp20x_usb_power_set_property,
407 };
408 
409 static int configure_iio_channels(struct platform_device *pdev,
410 				  struct axp20x_usb_power *power)
411 {
412 	power->vbus_v = devm_iio_channel_get(&pdev->dev, "vbus_v");
413 	if (IS_ERR(power->vbus_v)) {
414 		if (PTR_ERR(power->vbus_v) == -ENODEV)
415 			return -EPROBE_DEFER;
416 		return PTR_ERR(power->vbus_v);
417 	}
418 
419 	power->vbus_i = devm_iio_channel_get(&pdev->dev, "vbus_i");
420 	if (IS_ERR(power->vbus_i)) {
421 		if (PTR_ERR(power->vbus_i) == -ENODEV)
422 			return -EPROBE_DEFER;
423 		return PTR_ERR(power->vbus_i);
424 	}
425 
426 	return 0;
427 }
428 
429 static int configure_adc_registers(struct axp20x_usb_power *power)
430 {
431 	/* Enable vbus voltage and current measurement */
432 	return regmap_update_bits(power->regmap, AXP20X_ADC_EN1,
433 				  AXP20X_ADC_EN1_VBUS_CURR |
434 				  AXP20X_ADC_EN1_VBUS_VOLT,
435 				  AXP20X_ADC_EN1_VBUS_CURR |
436 				  AXP20X_ADC_EN1_VBUS_VOLT);
437 }
438 
439 static int axp20x_usb_power_probe(struct platform_device *pdev)
440 {
441 	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
442 	struct power_supply_config psy_cfg = {};
443 	struct axp20x_usb_power *power;
444 	static const char * const axp20x_irq_names[] = { "VBUS_PLUGIN",
445 		"VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL };
446 	static const char * const axp22x_irq_names[] = {
447 		"VBUS_PLUGIN", "VBUS_REMOVAL", NULL };
448 	const char * const *irq_names;
449 	const struct power_supply_desc *usb_power_desc;
450 	int i, irq, ret;
451 
452 	if (!of_device_is_available(pdev->dev.of_node))
453 		return -ENODEV;
454 
455 	if (!axp20x) {
456 		dev_err(&pdev->dev, "Parent drvdata not set\n");
457 		return -EINVAL;
458 	}
459 
460 	power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
461 	if (!power)
462 		return -ENOMEM;
463 
464 	platform_set_drvdata(pdev, power);
465 	power->axp20x_id = (enum axp20x_variants)of_device_get_match_data(
466 								&pdev->dev);
467 
468 	power->np = pdev->dev.of_node;
469 	power->regmap = axp20x->regmap;
470 
471 	if (power->axp20x_id == AXP202_ID) {
472 		/* Enable vbus valid checking */
473 		ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
474 					 AXP20X_VBUS_MON_VBUS_VALID,
475 					 AXP20X_VBUS_MON_VBUS_VALID);
476 		if (ret)
477 			return ret;
478 
479 		if (IS_ENABLED(CONFIG_AXP20X_ADC))
480 			ret = configure_iio_channels(pdev, power);
481 		else
482 			ret = configure_adc_registers(power);
483 
484 		if (ret)
485 			return ret;
486 
487 		usb_power_desc = &axp20x_usb_power_desc;
488 		irq_names = axp20x_irq_names;
489 	} else if (power->axp20x_id == AXP221_ID ||
490 		   power->axp20x_id == AXP223_ID ||
491 		   power->axp20x_id == AXP813_ID) {
492 		usb_power_desc = &axp22x_usb_power_desc;
493 		irq_names = axp22x_irq_names;
494 	} else {
495 		dev_err(&pdev->dev, "Unsupported AXP variant: %ld\n",
496 			axp20x->variant);
497 		return -EINVAL;
498 	}
499 
500 	if (power->axp20x_id == AXP813_ID) {
501 		/* Enable USB Battery Charging specification detection */
502 		regmap_update_bits(axp20x->regmap, AXP288_BC_GLOBAL,
503 				   AXP813_BC_EN, AXP813_BC_EN);
504 	}
505 
506 	psy_cfg.of_node = pdev->dev.of_node;
507 	psy_cfg.drv_data = power;
508 
509 	power->supply = devm_power_supply_register(&pdev->dev, usb_power_desc,
510 						   &psy_cfg);
511 	if (IS_ERR(power->supply))
512 		return PTR_ERR(power->supply);
513 
514 	/* Request irqs after registering, as irqs may trigger immediately */
515 	for (i = 0; irq_names[i]; i++) {
516 		irq = platform_get_irq_byname(pdev, irq_names[i]);
517 		if (irq < 0) {
518 			dev_warn(&pdev->dev, "No IRQ for %s: %d\n",
519 				 irq_names[i], irq);
520 			continue;
521 		}
522 		irq = regmap_irq_get_virq(axp20x->regmap_irqc, irq);
523 		ret = devm_request_any_context_irq(&pdev->dev, irq,
524 				axp20x_usb_power_irq, 0, DRVNAME, power);
525 		if (ret < 0)
526 			dev_warn(&pdev->dev, "Error requesting %s IRQ: %d\n",
527 				 irq_names[i], ret);
528 	}
529 
530 	INIT_DELAYED_WORK(&power->vbus_detect, axp20x_usb_power_poll_vbus);
531 	if (axp20x_usb_vbus_needs_polling(power))
532 		queue_delayed_work(system_wq, &power->vbus_detect, 0);
533 
534 	return 0;
535 }
536 
537 static int axp20x_usb_power_remove(struct platform_device *pdev)
538 {
539 	struct axp20x_usb_power *power = platform_get_drvdata(pdev);
540 
541 	cancel_delayed_work_sync(&power->vbus_detect);
542 
543 	return 0;
544 }
545 
546 static const struct of_device_id axp20x_usb_power_match[] = {
547 	{
548 		.compatible = "x-powers,axp202-usb-power-supply",
549 		.data = (void *)AXP202_ID,
550 	}, {
551 		.compatible = "x-powers,axp221-usb-power-supply",
552 		.data = (void *)AXP221_ID,
553 	}, {
554 		.compatible = "x-powers,axp223-usb-power-supply",
555 		.data = (void *)AXP223_ID,
556 	}, {
557 		.compatible = "x-powers,axp813-usb-power-supply",
558 		.data = (void *)AXP813_ID,
559 	}, { /* sentinel */ }
560 };
561 MODULE_DEVICE_TABLE(of, axp20x_usb_power_match);
562 
563 static struct platform_driver axp20x_usb_power_driver = {
564 	.probe = axp20x_usb_power_probe,
565 	.remove = axp20x_usb_power_remove,
566 	.driver = {
567 		.name = DRVNAME,
568 		.of_match_table = axp20x_usb_power_match,
569 	},
570 };
571 
572 module_platform_driver(axp20x_usb_power_driver);
573 
574 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
575 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
576 MODULE_LICENSE("GPL");
577