1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AXP20x PMIC USB power supply status driver
5 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
6 * Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
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>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.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>
26 #define DRVNAME "axp20x-usb-power-supply"
28 #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
29 #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
31 #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
33 #define AXP20X_VBUS_PATH_SEL BIT(7)
34 #define AXP20X_VBUS_PATH_SEL_OFFSET 7
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
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
50 #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
51 #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
53 #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
55 #define AXP813_BC_EN BIT(0)
58 * Note do not raise the debounce time, we must report Vusb high within
59 * 100ms otherwise we get Vbus errors in musb.
61 #define DEBOUNCE_TIME msecs_to_jiffies(50)
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
;
72 unsigned int num_irqs
;
76 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power
*power
)
79 * Polling is only necessary while VBUS is offline. While online, a
80 * present->absent transition implies an online->offline transition
81 * and will triger the VBUS_REMOVAL IRQ.
83 if (power
->axp20x_id
>= AXP221_ID
&& !power
->online
)
89 static irqreturn_t
axp20x_usb_power_irq(int irq
, void *devid
)
91 struct axp20x_usb_power
*power
= devid
;
93 power_supply_changed(power
->supply
);
95 mod_delayed_work(system_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
100 static void axp20x_usb_power_poll_vbus(struct work_struct
*work
)
102 struct axp20x_usb_power
*power
=
103 container_of(work
, struct axp20x_usb_power
, vbus_detect
.work
);
107 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
111 val
&= (AXP20X_PWR_STATUS_VBUS_PRESENT
| AXP20X_PWR_STATUS_VBUS_USED
);
112 if (val
!= power
->old_status
)
113 power_supply_changed(power
->supply
);
115 power
->old_status
= val
;
116 power
->online
= val
& AXP20X_PWR_STATUS_VBUS_USED
;
119 if (axp20x_usb_vbus_needs_polling(power
))
120 mod_delayed_work(system_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
123 static int axp20x_get_current_max(struct axp20x_usb_power
*power
, int *val
)
126 int ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
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 */
138 case AXP20X_VBUS_CLIMIT_500mA
:
141 case AXP20X_VBUS_CLIMIT_900mA
:
144 case AXP20X_VBUS_CLIMIT_NONE
:
152 static int axp813_get_current_max(struct axp20x_usb_power
*power
, int *val
)
155 int ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
160 switch (v
& AXP20X_VBUS_CLIMIT_MASK
) {
161 case AXP813_VBUS_CLIMIT_900mA
:
164 case AXP813_VBUS_CLIMIT_1500mA
:
167 case AXP813_VBUS_CLIMIT_2000mA
:
170 case AXP813_VBUS_CLIMIT_2500mA
:
177 static int axp20x_usb_power_get_property(struct power_supply
*psy
,
178 enum power_supply_property psp
, union power_supply_propval
*val
)
180 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
181 unsigned int input
, v
;
185 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
186 ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
190 val
->intval
= AXP20X_VBUS_VHOLD_uV(v
);
192 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
193 if (IS_ENABLED(CONFIG_AXP20X_ADC
)) {
194 ret
= iio_read_channel_processed(power
->vbus_v
,
200 * IIO framework gives mV but Power Supply framework
207 ret
= axp20x_read_variable_width(power
->regmap
,
208 AXP20X_VBUS_V_ADC_H
, 12);
212 val
->intval
= ret
* 1700; /* 1 step = 1.7 mV */
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
,
226 * IIO framework gives mA but Power Supply framework
233 ret
= axp20x_read_variable_width(power
->regmap
,
234 AXP20X_VBUS_I_ADC_H
, 12);
238 val
->intval
= ret
* 375; /* 1 step = 0.375 mA */
244 /* All the properties below need the input-status reg value */
245 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &input
);
250 case POWER_SUPPLY_PROP_HEALTH
:
251 if (!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
)) {
252 val
->intval
= POWER_SUPPLY_HEALTH_UNKNOWN
;
256 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
258 if (power
->axp20x_id
== AXP202_ID
) {
259 ret
= regmap_read(power
->regmap
,
260 AXP20X_USB_OTG_STATUS
, &v
);
264 if (!(v
& AXP20X_USB_STATUS_VBUS_VALID
))
266 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
269 case POWER_SUPPLY_PROP_PRESENT
:
270 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
);
272 case POWER_SUPPLY_PROP_ONLINE
:
273 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_USED
);
282 static int axp813_usb_power_set_online(struct axp20x_usb_power
*power
,
285 int val
= !intval
<< AXP20X_VBUS_PATH_SEL_OFFSET
;
287 return regmap_update_bits(power
->regmap
,
288 AXP20X_VBUS_IPSOUT_MGMT
,
289 AXP20X_VBUS_PATH_SEL
, val
);
292 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power
*power
,
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
);
318 static int axp813_usb_power_set_current_max(struct axp20x_usb_power
*power
,
325 return regmap_update_bits(power
->regmap
,
326 AXP20X_VBUS_IPSOUT_MGMT
,
327 AXP20X_VBUS_CLIMIT_MASK
,
328 AXP813_VBUS_CLIMIT_900mA
);
332 val
= (intval
- 1000000) / 500000;
333 return regmap_update_bits(power
->regmap
,
334 AXP20X_VBUS_IPSOUT_MGMT
,
335 AXP20X_VBUS_CLIMIT_MASK
, val
);
343 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power
*power
,
350 if (power
->axp20x_id
== AXP221_ID
)
355 val
= (900000 - intval
) / 400000;
356 return regmap_update_bits(power
->regmap
,
357 AXP20X_VBUS_IPSOUT_MGMT
,
358 AXP20X_VBUS_CLIMIT_MASK
, val
);
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
)
370 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
373 case POWER_SUPPLY_PROP_ONLINE
:
374 if (power
->axp20x_id
!= AXP813_ID
)
376 return axp813_usb_power_set_online(power
, val
->intval
);
378 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
379 return axp20x_usb_power_set_voltage_min(power
, val
->intval
);
381 case POWER_SUPPLY_PROP_CURRENT_MAX
:
382 if (power
->axp20x_id
== AXP813_ID
)
383 return axp813_usb_power_set_current_max(power
,
385 return axp20x_usb_power_set_current_max(power
, val
->intval
);
394 static int axp20x_usb_power_prop_writeable(struct power_supply
*psy
,
395 enum power_supply_property psp
)
397 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
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.
406 if (psp
== POWER_SUPPLY_PROP_ONLINE
)
407 return power
->axp20x_id
== AXP813_ID
;
409 return psp
== POWER_SUPPLY_PROP_VOLTAGE_MIN
||
410 psp
== POWER_SUPPLY_PROP_CURRENT_MAX
;
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
,
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
,
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
,
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
,
451 static const char * const axp20x_irq_names
[] = {
458 static const char * const axp22x_irq_names
[] = {
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
;
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
,
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
,
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
,
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
,
498 #ifdef CONFIG_PM_SLEEP
499 static int axp20x_usb_power_suspend(struct device
*dev
)
501 struct axp20x_usb_power
*power
= dev_get_drvdata(dev
);
505 * Allow wake via VBUS_PLUGIN only.
507 * As nested threaded IRQs are not automatically disabled during
508 * suspend, we must explicitly disable the remainder of the IRQs.
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
++]);
518 static int axp20x_usb_power_resume(struct device
*dev
)
520 struct axp20x_usb_power
*power
= dev_get_drvdata(dev
);
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
++]);
528 mod_delayed_work(system_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
534 static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops
, axp20x_usb_power_suspend
,
535 axp20x_usb_power_resume
);
537 static int configure_iio_channels(struct platform_device
*pdev
,
538 struct axp20x_usb_power
*power
)
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
);
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
);
557 static int configure_adc_registers(struct axp20x_usb_power
*power
)
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
);
567 static int axp20x_usb_power_probe(struct platform_device
*pdev
)
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
;
575 if (!of_device_is_available(pdev
->dev
.of_node
))
579 dev_err(&pdev
->dev
, "Parent drvdata not set\n");
583 axp_data
= of_device_get_match_data(&pdev
->dev
);
585 power
= devm_kzalloc(&pdev
->dev
,
586 struct_size(power
, irqs
, axp_data
->num_irq_names
),
591 platform_set_drvdata(pdev
, power
);
593 power
->axp20x_id
= axp_data
->axp20x_id
;
594 power
->regmap
= axp20x
->regmap
;
595 power
->num_irqs
= axp_data
->num_irq_names
;
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
);
605 if (IS_ENABLED(CONFIG_AXP20X_ADC
))
606 ret
= configure_iio_channels(pdev
, power
);
608 ret
= configure_adc_registers(power
);
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
);
620 psy_cfg
.of_node
= pdev
->dev
.of_node
;
621 psy_cfg
.drv_data
= power
;
623 power
->supply
= devm_power_supply_register(&pdev
->dev
,
624 axp_data
->power_desc
,
626 if (IS_ERR(power
->supply
))
627 return PTR_ERR(power
->supply
);
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
]);
633 dev_err(&pdev
->dev
, "No IRQ for %s: %d\n",
634 axp_data
->irq_names
[i
], irq
);
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,
642 dev_err(&pdev
->dev
, "Error requesting %s IRQ: %d\n",
643 axp_data
->irq_names
[i
], ret
);
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);
655 static int axp20x_usb_power_remove(struct platform_device
*pdev
)
657 struct axp20x_usb_power
*power
= platform_get_drvdata(pdev
);
659 cancel_delayed_work_sync(&power
->vbus_detect
);
664 static const struct of_device_id axp20x_usb_power_match
[] = {
666 .compatible
= "x-powers,axp202-usb-power-supply",
667 .data
= &axp202_data
,
669 .compatible
= "x-powers,axp221-usb-power-supply",
670 .data
= &axp221_data
,
672 .compatible
= "x-powers,axp223-usb-power-supply",
673 .data
= &axp223_data
,
675 .compatible
= "x-powers,axp813-usb-power-supply",
676 .data
= &axp813_data
,
677 }, { /* sentinel */ }
679 MODULE_DEVICE_TABLE(of
, axp20x_usb_power_match
);
681 static struct platform_driver axp20x_usb_power_driver
= {
682 .probe
= axp20x_usb_power_probe
,
683 .remove
= axp20x_usb_power_remove
,
686 .of_match_table
= axp20x_usb_power_match
,
687 .pm
= &axp20x_usb_power_pm_ops
,
691 module_platform_driver(axp20x_usb_power_driver
);
693 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
694 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
695 MODULE_LICENSE("GPL");