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)
51 #define AXP813_BC_EN BIT(0)
54 * Note do not raise the debounce time, we must report Vusb high within
55 * 100ms otherwise we get Vbus errors in musb.
57 #define DEBOUNCE_TIME msecs_to_jiffies(50)
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
;
70 static irqreturn_t
axp20x_usb_power_irq(int irq
, void *devid
)
72 struct axp20x_usb_power
*power
= devid
;
74 power_supply_changed(power
->supply
);
79 static void axp20x_usb_power_poll_vbus(struct work_struct
*work
)
81 struct axp20x_usb_power
*power
=
82 container_of(work
, struct axp20x_usb_power
, vbus_detect
.work
);
86 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
90 val
&= (AXP20X_PWR_STATUS_VBUS_PRESENT
| AXP20X_PWR_STATUS_VBUS_USED
);
91 if (val
!= power
->old_status
)
92 power_supply_changed(power
->supply
);
94 power
->old_status
= val
;
97 mod_delayed_work(system_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
100 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power
*power
)
102 if (power
->axp20x_id
>= AXP221_ID
)
108 static int axp20x_get_current_max(struct axp20x_usb_power
*power
, int *val
)
111 int ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
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 */
123 case AXP20X_VBUS_CLIMIT_500mA
:
126 case AXP20X_VBUS_CLIMIT_900mA
:
129 case AXP20X_VBUS_CLIMIT_NONE
:
137 static int axp813_get_current_max(struct axp20x_usb_power
*power
, int *val
)
140 int ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
145 switch (v
& AXP20X_VBUS_CLIMIT_MASK
) {
146 case AXP813_VBUS_CLIMIT_900mA
:
149 case AXP813_VBUS_CLIMIT_1500mA
:
152 case AXP813_VBUS_CLIMIT_2000mA
:
155 case AXP813_VBUS_CLIMIT_2500mA
:
162 static int axp20x_usb_power_get_property(struct power_supply
*psy
,
163 enum power_supply_property psp
, union power_supply_propval
*val
)
165 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
166 unsigned int input
, v
;
170 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
171 ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
175 val
->intval
= AXP20X_VBUS_VHOLD_uV(v
);
177 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
178 if (IS_ENABLED(CONFIG_AXP20X_ADC
)) {
179 ret
= iio_read_channel_processed(power
->vbus_v
,
185 * IIO framework gives mV but Power Supply framework
192 ret
= axp20x_read_variable_width(power
->regmap
,
193 AXP20X_VBUS_V_ADC_H
, 12);
197 val
->intval
= ret
* 1700; /* 1 step = 1.7 mV */
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
,
211 * IIO framework gives mA but Power Supply framework
218 ret
= axp20x_read_variable_width(power
->regmap
,
219 AXP20X_VBUS_I_ADC_H
, 12);
223 val
->intval
= ret
* 375; /* 1 step = 0.375 mA */
229 /* All the properties below need the input-status reg value */
230 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &input
);
235 case POWER_SUPPLY_PROP_HEALTH
:
236 if (!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
)) {
237 val
->intval
= POWER_SUPPLY_HEALTH_UNKNOWN
;
241 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
243 if (power
->axp20x_id
== AXP202_ID
) {
244 ret
= regmap_read(power
->regmap
,
245 AXP20X_USB_OTG_STATUS
, &v
);
249 if (!(v
& AXP20X_USB_STATUS_VBUS_VALID
))
251 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
254 case POWER_SUPPLY_PROP_PRESENT
:
255 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
);
257 case POWER_SUPPLY_PROP_ONLINE
:
258 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_USED
);
267 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power
*power
,
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
);
293 static int axp813_usb_power_set_current_max(struct axp20x_usb_power
*power
,
300 return regmap_update_bits(power
->regmap
,
301 AXP20X_VBUS_IPSOUT_MGMT
,
302 AXP20X_VBUS_CLIMIT_MASK
,
303 AXP813_VBUS_CLIMIT_900mA
);
307 val
= (intval
- 1000000) / 500000;
308 return regmap_update_bits(power
->regmap
,
309 AXP20X_VBUS_IPSOUT_MGMT
,
310 AXP20X_VBUS_CLIMIT_MASK
, val
);
318 static int axp20x_usb_power_set_current_max(struct axp20x_usb_power
*power
,
325 if (power
->axp20x_id
== AXP221_ID
)
330 val
= (900000 - intval
) / 400000;
331 return regmap_update_bits(power
->regmap
,
332 AXP20X_VBUS_IPSOUT_MGMT
,
333 AXP20X_VBUS_CLIMIT_MASK
, val
);
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
)
345 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
348 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
349 return axp20x_usb_power_set_voltage_min(power
, val
->intval
);
351 case POWER_SUPPLY_PROP_CURRENT_MAX
:
352 if (power
->axp20x_id
== AXP813_ID
)
353 return axp813_usb_power_set_current_max(power
,
355 return axp20x_usb_power_set_current_max(power
, val
->intval
);
364 static int axp20x_usb_power_prop_writeable(struct power_supply
*psy
,
365 enum power_supply_property psp
)
367 return psp
== POWER_SUPPLY_PROP_VOLTAGE_MIN
||
368 psp
== POWER_SUPPLY_PROP_CURRENT_MAX
;
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
,
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
,
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
,
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
,
409 static int configure_iio_channels(struct platform_device
*pdev
,
410 struct axp20x_usb_power
*power
)
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
);
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
);
429 static int configure_adc_registers(struct axp20x_usb_power
*power
)
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
);
439 static int axp20x_usb_power_probe(struct platform_device
*pdev
)
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
;
452 if (!of_device_is_available(pdev
->dev
.of_node
))
456 dev_err(&pdev
->dev
, "Parent drvdata not set\n");
460 power
= devm_kzalloc(&pdev
->dev
, sizeof(*power
), GFP_KERNEL
);
464 platform_set_drvdata(pdev
, power
);
465 power
->axp20x_id
= (enum axp20x_variants
)of_device_get_match_data(
468 power
->np
= pdev
->dev
.of_node
;
469 power
->regmap
= axp20x
->regmap
;
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
);
479 if (IS_ENABLED(CONFIG_AXP20X_ADC
))
480 ret
= configure_iio_channels(pdev
, power
);
482 ret
= configure_adc_registers(power
);
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
;
495 dev_err(&pdev
->dev
, "Unsupported AXP variant: %ld\n",
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
);
506 psy_cfg
.of_node
= pdev
->dev
.of_node
;
507 psy_cfg
.drv_data
= power
;
509 power
->supply
= devm_power_supply_register(&pdev
->dev
, usb_power_desc
,
511 if (IS_ERR(power
->supply
))
512 return PTR_ERR(power
->supply
);
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
]);
518 dev_warn(&pdev
->dev
, "No IRQ for %s: %d\n",
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
);
526 dev_warn(&pdev
->dev
, "Error requesting %s IRQ: %d\n",
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);
537 static int axp20x_usb_power_remove(struct platform_device
*pdev
)
539 struct axp20x_usb_power
*power
= platform_get_drvdata(pdev
);
541 cancel_delayed_work_sync(&power
->vbus_detect
);
546 static const struct of_device_id axp20x_usb_power_match
[] = {
548 .compatible
= "x-powers,axp202-usb-power-supply",
549 .data
= (void *)AXP202_ID
,
551 .compatible
= "x-powers,axp221-usb-power-supply",
552 .data
= (void *)AXP221_ID
,
554 .compatible
= "x-powers,axp223-usb-power-supply",
555 .data
= (void *)AXP223_ID
,
557 .compatible
= "x-powers,axp813-usb-power-supply",
558 .data
= (void *)AXP813_ID
,
559 }, { /* sentinel */ }
561 MODULE_DEVICE_TABLE(of
, axp20x_usb_power_match
);
563 static struct platform_driver axp20x_usb_power_driver
= {
564 .probe
= axp20x_usb_power_probe
,
565 .remove
= axp20x_usb_power_remove
,
568 .of_match_table
= axp20x_usb_power_match
,
572 module_platform_driver(axp20x_usb_power_driver
);
574 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
575 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
576 MODULE_LICENSE("GPL");