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>
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>
25 #define DRVNAME "axp20x-usb-power-supply"
27 #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
28 #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
30 #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
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
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
46 #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
47 #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
49 #define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
52 * Note do not raise the debounce time, we must report Vusb high within
53 * 100ms otherwise we get Vbus errors in musb.
55 #define DEBOUNCE_TIME msecs_to_jiffies(50)
57 struct axp20x_usb_power
{
58 struct device_node
*np
;
59 struct regmap
*regmap
;
60 struct power_supply
*supply
;
61 enum axp20x_variants axp20x_id
;
62 struct iio_channel
*vbus_v
;
63 struct iio_channel
*vbus_i
;
64 struct delayed_work vbus_detect
;
65 unsigned int old_status
;
68 static irqreturn_t
axp20x_usb_power_irq(int irq
, void *devid
)
70 struct axp20x_usb_power
*power
= devid
;
72 power_supply_changed(power
->supply
);
77 static void axp20x_usb_power_poll_vbus(struct work_struct
*work
)
79 struct axp20x_usb_power
*power
=
80 container_of(work
, struct axp20x_usb_power
, vbus_detect
.work
);
84 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
88 val
&= (AXP20X_PWR_STATUS_VBUS_PRESENT
| AXP20X_PWR_STATUS_VBUS_USED
);
89 if (val
!= power
->old_status
)
90 power_supply_changed(power
->supply
);
92 power
->old_status
= val
;
95 mod_delayed_work(system_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
98 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power
*power
)
100 if (power
->axp20x_id
>= AXP221_ID
)
106 static int axp20x_get_current_max(struct axp20x_usb_power
*power
, int *val
)
109 int ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
114 switch (v
& AXP20X_VBUS_CLIMIT_MASK
) {
115 case AXP20X_VBUS_CLIMIT_100mA
:
116 if (power
->axp20x_id
== AXP221_ID
)
117 *val
= -1; /* No 100mA limit */
121 case AXP20X_VBUS_CLIMIT_500mA
:
124 case AXP20X_VBUS_CLIMIT_900mA
:
127 case AXP20X_VBUS_CLIMIT_NONE
:
135 static int axp813_get_current_max(struct axp20x_usb_power
*power
, int *val
)
138 int ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
143 switch (v
& AXP20X_VBUS_CLIMIT_MASK
) {
144 case AXP813_VBUS_CLIMIT_900mA
:
147 case AXP813_VBUS_CLIMIT_1500mA
:
150 case AXP813_VBUS_CLIMIT_2000mA
:
153 case AXP813_VBUS_CLIMIT_2500mA
:
160 static int axp20x_usb_power_get_property(struct power_supply
*psy
,
161 enum power_supply_property psp
, union power_supply_propval
*val
)
163 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
164 unsigned int input
, v
;
168 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
169 ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
173 val
->intval
= AXP20X_VBUS_VHOLD_uV(v
);
175 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
176 if (IS_ENABLED(CONFIG_AXP20X_ADC
)) {
177 ret
= iio_read_channel_processed(power
->vbus_v
,
183 * IIO framework gives mV but Power Supply framework
190 ret
= axp20x_read_variable_width(power
->regmap
,
191 AXP20X_VBUS_V_ADC_H
, 12);
195 val
->intval
= ret
* 1700; /* 1 step = 1.7 mV */
197 case POWER_SUPPLY_PROP_CURRENT_MAX
:
198 if (power
->axp20x_id
== AXP813_ID
)
199 return axp813_get_current_max(power
, &val
->intval
);
200 return axp20x_get_current_max(power
, &val
->intval
);
201 case POWER_SUPPLY_PROP_CURRENT_NOW
:
202 if (IS_ENABLED(CONFIG_AXP20X_ADC
)) {
203 ret
= iio_read_channel_processed(power
->vbus_i
,
209 * IIO framework gives mA but Power Supply framework
216 ret
= axp20x_read_variable_width(power
->regmap
,
217 AXP20X_VBUS_I_ADC_H
, 12);
221 val
->intval
= ret
* 375; /* 1 step = 0.375 mA */
227 /* All the properties below need the input-status reg value */
228 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &input
);
233 case POWER_SUPPLY_PROP_HEALTH
:
234 if (!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
)) {
235 val
->intval
= POWER_SUPPLY_HEALTH_UNKNOWN
;
239 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
241 if (power
->axp20x_id
== AXP202_ID
) {
242 ret
= regmap_read(power
->regmap
,
243 AXP20X_USB_OTG_STATUS
, &v
);
247 if (!(v
& AXP20X_USB_STATUS_VBUS_VALID
))
249 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
252 case POWER_SUPPLY_PROP_PRESENT
:
253 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
);
255 case POWER_SUPPLY_PROP_ONLINE
:
256 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_USED
);
265 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power
*power
,
279 val
= (intval
- 4000000) / 100000;
280 return regmap_update_bits(power
->regmap
,
281 AXP20X_VBUS_IPSOUT_MGMT
,
282 AXP20X_VBUS_VHOLD_MASK
,
283 val
<< AXP20X_VBUS_VHOLD_OFFSET
);
291 static int axp813_usb_power_set_current_max(struct axp20x_usb_power
*power
,
298 return regmap_update_bits(power
->regmap
,
299 AXP20X_VBUS_IPSOUT_MGMT
,
300 AXP20X_VBUS_CLIMIT_MASK
,
301 AXP813_VBUS_CLIMIT_900mA
);
305 val
= (intval
- 1000000) / 500000;
306 return regmap_update_bits(power
->regmap
,
307 AXP20X_VBUS_IPSOUT_MGMT
,
308 AXP20X_VBUS_CLIMIT_MASK
, val
);
316 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power
*power
,
323 if (power
->axp20x_id
== AXP221_ID
)
328 val
= (900000 - intval
) / 400000;
329 return regmap_update_bits(power
->regmap
,
330 AXP20X_VBUS_IPSOUT_MGMT
,
331 AXP20X_VBUS_CLIMIT_MASK
, val
);
339 static int axp20x_usb_power_set_property(struct power_supply
*psy
,
340 enum power_supply_property psp
,
341 const union power_supply_propval
*val
)
343 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
346 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
347 return axp20x_usb_power_set_voltage_min(power
, val
->intval
);
349 case POWER_SUPPLY_PROP_CURRENT_MAX
:
350 if (power
->axp20x_id
== AXP813_ID
)
351 return axp813_usb_power_set_current_max(power
,
353 return axp20x_usb_power_set_current_max(power
, val
->intval
);
362 static int axp20x_usb_power_prop_writeable(struct power_supply
*psy
,
363 enum power_supply_property psp
)
365 return psp
== POWER_SUPPLY_PROP_VOLTAGE_MIN
||
366 psp
== POWER_SUPPLY_PROP_CURRENT_MAX
;
369 static enum power_supply_property axp20x_usb_power_properties
[] = {
370 POWER_SUPPLY_PROP_HEALTH
,
371 POWER_SUPPLY_PROP_PRESENT
,
372 POWER_SUPPLY_PROP_ONLINE
,
373 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
374 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
375 POWER_SUPPLY_PROP_CURRENT_MAX
,
376 POWER_SUPPLY_PROP_CURRENT_NOW
,
379 static enum power_supply_property axp22x_usb_power_properties
[] = {
380 POWER_SUPPLY_PROP_HEALTH
,
381 POWER_SUPPLY_PROP_PRESENT
,
382 POWER_SUPPLY_PROP_ONLINE
,
383 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
384 POWER_SUPPLY_PROP_CURRENT_MAX
,
387 static const struct power_supply_desc axp20x_usb_power_desc
= {
388 .name
= "axp20x-usb",
389 .type
= POWER_SUPPLY_TYPE_USB
,
390 .properties
= axp20x_usb_power_properties
,
391 .num_properties
= ARRAY_SIZE(axp20x_usb_power_properties
),
392 .property_is_writeable
= axp20x_usb_power_prop_writeable
,
393 .get_property
= axp20x_usb_power_get_property
,
394 .set_property
= axp20x_usb_power_set_property
,
397 static const struct power_supply_desc axp22x_usb_power_desc
= {
398 .name
= "axp20x-usb",
399 .type
= POWER_SUPPLY_TYPE_USB
,
400 .properties
= axp22x_usb_power_properties
,
401 .num_properties
= ARRAY_SIZE(axp22x_usb_power_properties
),
402 .property_is_writeable
= axp20x_usb_power_prop_writeable
,
403 .get_property
= axp20x_usb_power_get_property
,
404 .set_property
= axp20x_usb_power_set_property
,
407 static int configure_iio_channels(struct platform_device
*pdev
,
408 struct axp20x_usb_power
*power
)
410 power
->vbus_v
= devm_iio_channel_get(&pdev
->dev
, "vbus_v");
411 if (IS_ERR(power
->vbus_v
)) {
412 if (PTR_ERR(power
->vbus_v
) == -ENODEV
)
413 return -EPROBE_DEFER
;
414 return PTR_ERR(power
->vbus_v
);
417 power
->vbus_i
= devm_iio_channel_get(&pdev
->dev
, "vbus_i");
418 if (IS_ERR(power
->vbus_i
)) {
419 if (PTR_ERR(power
->vbus_i
) == -ENODEV
)
420 return -EPROBE_DEFER
;
421 return PTR_ERR(power
->vbus_i
);
427 static int configure_adc_registers(struct axp20x_usb_power
*power
)
429 /* Enable vbus voltage and current measurement */
430 return regmap_update_bits(power
->regmap
, AXP20X_ADC_EN1
,
431 AXP20X_ADC_EN1_VBUS_CURR
|
432 AXP20X_ADC_EN1_VBUS_VOLT
,
433 AXP20X_ADC_EN1_VBUS_CURR
|
434 AXP20X_ADC_EN1_VBUS_VOLT
);
437 static int axp20x_usb_power_probe(struct platform_device
*pdev
)
439 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
440 struct power_supply_config psy_cfg
= {};
441 struct axp20x_usb_power
*power
;
442 static const char * const axp20x_irq_names
[] = { "VBUS_PLUGIN",
443 "VBUS_REMOVAL", "VBUS_VALID", "VBUS_NOT_VALID", NULL
};
444 static const char * const axp22x_irq_names
[] = {
445 "VBUS_PLUGIN", "VBUS_REMOVAL", NULL
};
446 const char * const *irq_names
;
447 const struct power_supply_desc
*usb_power_desc
;
450 if (!of_device_is_available(pdev
->dev
.of_node
))
454 dev_err(&pdev
->dev
, "Parent drvdata not set\n");
458 power
= devm_kzalloc(&pdev
->dev
, sizeof(*power
), GFP_KERNEL
);
462 platform_set_drvdata(pdev
, power
);
463 power
->axp20x_id
= (enum axp20x_variants
)of_device_get_match_data(
466 power
->np
= pdev
->dev
.of_node
;
467 power
->regmap
= axp20x
->regmap
;
469 if (power
->axp20x_id
== AXP202_ID
) {
470 /* Enable vbus valid checking */
471 ret
= regmap_update_bits(power
->regmap
, AXP20X_VBUS_MON
,
472 AXP20X_VBUS_MON_VBUS_VALID
,
473 AXP20X_VBUS_MON_VBUS_VALID
);
477 if (IS_ENABLED(CONFIG_AXP20X_ADC
))
478 ret
= configure_iio_channels(pdev
, power
);
480 ret
= configure_adc_registers(power
);
485 usb_power_desc
= &axp20x_usb_power_desc
;
486 irq_names
= axp20x_irq_names
;
487 } else if (power
->axp20x_id
== AXP221_ID
||
488 power
->axp20x_id
== AXP223_ID
||
489 power
->axp20x_id
== AXP813_ID
) {
490 usb_power_desc
= &axp22x_usb_power_desc
;
491 irq_names
= axp22x_irq_names
;
493 dev_err(&pdev
->dev
, "Unsupported AXP variant: %ld\n",
498 psy_cfg
.of_node
= pdev
->dev
.of_node
;
499 psy_cfg
.drv_data
= power
;
501 power
->supply
= devm_power_supply_register(&pdev
->dev
, usb_power_desc
,
503 if (IS_ERR(power
->supply
))
504 return PTR_ERR(power
->supply
);
506 /* Request irqs after registering, as irqs may trigger immediately */
507 for (i
= 0; irq_names
[i
]; i
++) {
508 irq
= platform_get_irq_byname(pdev
, irq_names
[i
]);
510 dev_warn(&pdev
->dev
, "No IRQ for %s: %d\n",
514 irq
= regmap_irq_get_virq(axp20x
->regmap_irqc
, irq
);
515 ret
= devm_request_any_context_irq(&pdev
->dev
, irq
,
516 axp20x_usb_power_irq
, 0, DRVNAME
, power
);
518 dev_warn(&pdev
->dev
, "Error requesting %s IRQ: %d\n",
522 INIT_DELAYED_WORK(&power
->vbus_detect
, axp20x_usb_power_poll_vbus
);
523 if (axp20x_usb_vbus_needs_polling(power
))
524 queue_delayed_work(system_wq
, &power
->vbus_detect
, 0);
529 static int axp20x_usb_power_remove(struct platform_device
*pdev
)
531 struct axp20x_usb_power
*power
= platform_get_drvdata(pdev
);
533 cancel_delayed_work_sync(&power
->vbus_detect
);
538 static const struct of_device_id axp20x_usb_power_match
[] = {
540 .compatible
= "x-powers,axp202-usb-power-supply",
541 .data
= (void *)AXP202_ID
,
543 .compatible
= "x-powers,axp221-usb-power-supply",
544 .data
= (void *)AXP221_ID
,
546 .compatible
= "x-powers,axp223-usb-power-supply",
547 .data
= (void *)AXP223_ID
,
549 .compatible
= "x-powers,axp813-usb-power-supply",
550 .data
= (void *)AXP813_ID
,
551 }, { /* sentinel */ }
553 MODULE_DEVICE_TABLE(of
, axp20x_usb_power_match
);
555 static struct platform_driver axp20x_usb_power_driver
= {
556 .probe
= axp20x_usb_power_probe
,
557 .remove
= axp20x_usb_power_remove
,
560 .of_match_table
= axp20x_usb_power_match
,
564 module_platform_driver(axp20x_usb_power_driver
);
566 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
567 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
568 MODULE_LICENSE("GPL");