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/devm-helpers.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/axp20x.h>
16 #include <linux/module.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 AXP192_USB_OTG_STATUS 0x04
30 #define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
31 #define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
33 #define AXP717_PWR_STATUS_VBUS_GOOD BIT(5)
35 #define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
37 #define AXP717_PMU_FAULT_VBUS BIT(5)
38 #define AXP717_PMU_FAULT_VSYS BIT(3)
40 #define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000)
41 #define AXP20X_VBUS_VHOLD_MASK GENMASK(5, 3)
42 #define AXP20X_VBUS_VHOLD_OFFSET 3
44 #define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
45 #define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
47 #define AXP717_INPUT_VOL_LIMIT_MASK GENMASK(3, 0)
48 #define AXP717_INPUT_CUR_LIMIT_MASK GENMASK(5, 0)
49 #define AXP717_ADC_DATA_MASK GENMASK(14, 0)
51 #define AXP717_ADC_EN_VBUS_VOLT BIT(2)
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
;
62 const struct power_supply_desc
*power_desc
;
63 const char * const *irq_names
;
64 unsigned int num_irq_names
;
65 const int *curr_lim_table
;
66 int curr_lim_table_size
;
67 struct reg_field curr_lim_fld
;
68 struct reg_field vbus_valid_bit
;
69 struct reg_field vbus_mon_bit
;
70 struct reg_field usb_bc_en_bit
;
71 struct reg_field usb_bc_det_fld
;
72 struct reg_field vbus_disable_bit
;
73 bool vbus_needs_polling
: 1;
74 void (*axp20x_read_vbus
)(struct work_struct
*work
);
75 int (*axp20x_cfg_iio_chan
)(struct platform_device
*pdev
,
76 struct axp20x_usb_power
*power
);
77 int (*axp20x_cfg_adc_reg
)(struct axp20x_usb_power
*power
);
80 struct axp20x_usb_power
{
82 struct regmap
*regmap
;
83 struct regmap_field
*curr_lim_fld
;
84 struct regmap_field
*vbus_valid_bit
;
85 struct regmap_field
*vbus_mon_bit
;
86 struct regmap_field
*usb_bc_en_bit
;
87 struct regmap_field
*usb_bc_det_fld
;
88 struct regmap_field
*vbus_disable_bit
;
89 struct power_supply
*supply
;
90 const struct axp_data
*axp_data
;
91 struct iio_channel
*vbus_v
;
92 struct iio_channel
*vbus_i
;
93 struct delayed_work vbus_detect
;
95 unsigned int old_status
;
97 unsigned int num_irqs
;
98 unsigned int irqs
[] __counted_by(num_irqs
);
101 static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power
*power
)
104 * Polling is only necessary while VBUS is offline. While online, a
105 * present->absent transition implies an online->offline transition
106 * and will trigger the VBUS_REMOVAL IRQ.
108 if (power
->axp_data
->vbus_needs_polling
&& !power
->online
)
114 static irqreturn_t
axp20x_usb_power_irq(int irq
, void *devid
)
116 struct axp20x_usb_power
*power
= devid
;
118 power_supply_changed(power
->supply
);
120 mod_delayed_work(system_power_efficient_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
125 static void axp20x_usb_power_poll_vbus(struct work_struct
*work
)
127 struct axp20x_usb_power
*power
=
128 container_of(work
, struct axp20x_usb_power
, vbus_detect
.work
);
132 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &val
);
136 val
&= (AXP20X_PWR_STATUS_VBUS_PRESENT
| AXP20X_PWR_STATUS_VBUS_USED
);
137 if (val
!= power
->old_status
)
138 power_supply_changed(power
->supply
);
140 if (power
->usb_bc_en_bit
&& (val
& AXP20X_PWR_STATUS_VBUS_PRESENT
) !=
141 (power
->old_status
& AXP20X_PWR_STATUS_VBUS_PRESENT
)) {
142 dev_dbg(power
->dev
, "Cable status changed, re-enabling USB BC");
143 ret
= regmap_field_write(power
->usb_bc_en_bit
, 1);
145 dev_err(power
->dev
, "failed to enable USB BC: errno %d",
149 power
->old_status
= val
;
150 power
->online
= val
& AXP20X_PWR_STATUS_VBUS_USED
;
153 if (axp20x_usb_vbus_needs_polling(power
))
154 mod_delayed_work(system_power_efficient_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
157 static void axp717_usb_power_poll_vbus(struct work_struct
*work
)
159 struct axp20x_usb_power
*power
=
160 container_of(work
, struct axp20x_usb_power
, vbus_detect
.work
);
164 ret
= regmap_read(power
->regmap
, AXP717_ON_INDICATE
, &val
);
168 val
&= AXP717_PWR_STATUS_VBUS_GOOD
;
169 if (val
!= power
->old_status
)
170 power_supply_changed(power
->supply
);
172 power
->old_status
= val
;
175 static int axp20x_get_usb_type(struct axp20x_usb_power
*power
,
176 union power_supply_propval
*val
)
181 if (!power
->usb_bc_det_fld
)
184 ret
= regmap_field_read(power
->usb_bc_det_fld
, ®
);
190 val
->intval
= POWER_SUPPLY_USB_TYPE_SDP
;
193 val
->intval
= POWER_SUPPLY_USB_TYPE_CDP
;
196 val
->intval
= POWER_SUPPLY_USB_TYPE_DCP
;
199 val
->intval
= POWER_SUPPLY_USB_TYPE_UNKNOWN
;
206 static int axp20x_usb_power_get_property(struct power_supply
*psy
,
207 enum power_supply_property psp
, union power_supply_propval
*val
)
209 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
210 unsigned int input
, v
;
214 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
215 ret
= regmap_read(power
->regmap
, AXP20X_VBUS_IPSOUT_MGMT
, &v
);
219 val
->intval
= AXP20X_VBUS_VHOLD_uV(v
);
221 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
222 if (IS_ENABLED(CONFIG_AXP20X_ADC
)) {
223 ret
= iio_read_channel_processed(power
->vbus_v
,
229 * IIO framework gives mV but Power Supply framework
236 ret
= axp20x_read_variable_width(power
->regmap
,
237 AXP20X_VBUS_V_ADC_H
, 12);
241 val
->intval
= ret
* 1700; /* 1 step = 1.7 mV */
243 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
244 ret
= regmap_field_read(power
->curr_lim_fld
, &v
);
248 if (v
< power
->axp_data
->curr_lim_table_size
)
249 val
->intval
= power
->axp_data
->curr_lim_table
[v
];
251 val
->intval
= power
->axp_data
->curr_lim_table
[
252 power
->axp_data
->curr_lim_table_size
- 1];
254 case POWER_SUPPLY_PROP_CURRENT_NOW
:
255 if (IS_ENABLED(CONFIG_AXP20X_ADC
)) {
256 ret
= iio_read_channel_processed(power
->vbus_i
,
262 * IIO framework gives mA but Power Supply framework
269 ret
= axp20x_read_variable_width(power
->regmap
,
270 AXP20X_VBUS_I_ADC_H
, 12);
274 val
->intval
= ret
* 375; /* 1 step = 0.375 mA */
277 case POWER_SUPPLY_PROP_USB_TYPE
:
278 return axp20x_get_usb_type(power
, val
);
283 /* All the properties below need the input-status reg value */
284 ret
= regmap_read(power
->regmap
, AXP20X_PWR_INPUT_STATUS
, &input
);
289 case POWER_SUPPLY_PROP_HEALTH
:
290 if (!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
)) {
291 val
->intval
= POWER_SUPPLY_HEALTH_UNKNOWN
;
295 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
297 if (power
->vbus_valid_bit
) {
298 ret
= regmap_field_read(power
->vbus_valid_bit
, &v
);
303 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
307 case POWER_SUPPLY_PROP_PRESENT
:
308 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_PRESENT
);
310 case POWER_SUPPLY_PROP_ONLINE
:
311 val
->intval
= !!(input
& AXP20X_PWR_STATUS_VBUS_USED
);
320 static int axp717_usb_power_get_property(struct power_supply
*psy
,
321 enum power_supply_property psp
, union power_supply_propval
*val
)
323 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
328 case POWER_SUPPLY_PROP_HEALTH
:
329 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
330 ret
= regmap_read(power
->regmap
, AXP717_ON_INDICATE
, &v
);
334 if (!(v
& AXP717_PWR_STATUS_VBUS_GOOD
))
335 val
->intval
= POWER_SUPPLY_HEALTH_UNKNOWN
;
337 ret
= regmap_read(power
->regmap
, AXP717_PMU_FAULT_VBUS
, &v
);
341 v
&= (AXP717_PMU_FAULT_VBUS
| AXP717_PMU_FAULT_VSYS
);
343 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
344 regmap_write(power
->regmap
, AXP717_PMU_FAULT_VBUS
, v
);
348 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
349 ret
= regmap_read(power
->regmap
, AXP717_INPUT_CUR_LIMIT_CTRL
, &v
);
353 /* 50ma step size with 100ma offset. */
354 v
&= AXP717_INPUT_CUR_LIMIT_MASK
;
355 val
->intval
= (v
* 50000) + 100000;
357 case POWER_SUPPLY_PROP_ONLINE
:
358 case POWER_SUPPLY_PROP_PRESENT
:
359 ret
= regmap_read(power
->regmap
, AXP717_ON_INDICATE
, &v
);
362 val
->intval
= !!(v
& AXP717_PWR_STATUS_VBUS_GOOD
);
364 case POWER_SUPPLY_PROP_USB_TYPE
:
365 return axp20x_get_usb_type(power
, val
);
366 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
367 ret
= regmap_read(power
->regmap
, AXP717_INPUT_VOL_LIMIT_CTRL
, &v
);
371 /* 80mv step size with 3.88v offset. */
372 v
&= AXP717_INPUT_VOL_LIMIT_MASK
;
373 val
->intval
= (v
* 80000) + 3880000;
375 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
376 if (IS_ENABLED(CONFIG_AXP20X_ADC
)) {
377 ret
= iio_read_channel_processed(power
->vbus_v
,
383 * IIO framework gives mV but Power Supply framework
390 ret
= axp20x_read_variable_width(power
->regmap
,
391 AXP717_VBUS_V_H
, 16);
395 val
->intval
= (ret
% AXP717_ADC_DATA_MASK
) * 1000;
405 static int axp20x_usb_power_set_voltage_min(struct axp20x_usb_power
*power
,
419 val
= (intval
- 4000000) / 100000;
420 return regmap_update_bits(power
->regmap
,
421 AXP20X_VBUS_IPSOUT_MGMT
,
422 AXP20X_VBUS_VHOLD_MASK
,
423 val
<< AXP20X_VBUS_VHOLD_OFFSET
);
431 static int axp717_usb_power_set_voltage_min(struct axp20x_usb_power
*power
,
436 /* Minimum value of 3.88v and maximum of 5.08v. */
437 if (intval
< 3880000 || intval
> 5080000)
440 /* step size of 80ma with 3.88v offset. */
441 val
= (intval
- 3880000) / 80000;
442 return regmap_update_bits(power
->regmap
,
443 AXP717_INPUT_VOL_LIMIT_CTRL
,
444 AXP717_INPUT_VOL_LIMIT_MASK
, val
);
447 static int axp20x_usb_power_set_input_current_limit(struct axp20x_usb_power
*power
,
452 const unsigned int max
= power
->axp_data
->curr_lim_table_size
;
457 if (power
->max_input_cur
&& (intval
> power
->max_input_cur
)) {
459 "requested current %d clamped to max current %d\n",
460 intval
, power
->max_input_cur
);
461 intval
= power
->max_input_cur
;
465 * BC1.2 detection can cause a race condition if we try to set a current
466 * limit while it's in progress. When it finishes it will overwrite the
467 * current limit we just set.
469 if (power
->usb_bc_en_bit
) {
471 "disabling BC1.2 detection because current limit was set");
472 ret
= regmap_field_write(power
->usb_bc_en_bit
, 0);
477 for (reg
= max
- 1; reg
> 0; reg
--)
478 if (power
->axp_data
->curr_lim_table
[reg
] <= intval
)
481 dev_dbg(power
->dev
, "setting input current limit reg to %d (%d uA), requested %d uA",
482 reg
, power
->axp_data
->curr_lim_table
[reg
], intval
);
484 return regmap_field_write(power
->curr_lim_fld
, reg
);
487 static int axp717_usb_power_set_input_current_limit(struct axp20x_usb_power
*power
,
492 /* Minimum value of 100mA and maximum value of 3.25A*/
493 if (intval
< 100000 || intval
> 3250000)
496 if (power
->max_input_cur
&& (intval
> power
->max_input_cur
)) {
498 "reqested current %d clamped to max current %d\n",
499 intval
, power
->max_input_cur
);
500 intval
= power
->max_input_cur
;
503 /* Minimum value of 100mA with step size of 50mA. */
504 tmp
= (intval
- 100000) / 50000;
505 return regmap_update_bits(power
->regmap
,
506 AXP717_INPUT_CUR_LIMIT_CTRL
,
507 AXP717_INPUT_CUR_LIMIT_MASK
, tmp
);
510 static int axp20x_usb_power_set_property(struct power_supply
*psy
,
511 enum power_supply_property psp
,
512 const union power_supply_propval
*val
)
514 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
517 case POWER_SUPPLY_PROP_ONLINE
:
518 if (!power
->vbus_disable_bit
)
521 return regmap_field_write(power
->vbus_disable_bit
, !val
->intval
);
523 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
524 return axp20x_usb_power_set_voltage_min(power
, val
->intval
);
526 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
527 return axp20x_usb_power_set_input_current_limit(power
, val
->intval
);
534 static int axp717_usb_power_set_property(struct power_supply
*psy
,
535 enum power_supply_property psp
,
536 const union power_supply_propval
*val
)
538 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
541 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
542 return axp717_usb_power_set_input_current_limit(power
, val
->intval
);
544 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
545 return axp717_usb_power_set_voltage_min(power
, val
->intval
);
554 static int axp20x_usb_power_prop_writeable(struct power_supply
*psy
,
555 enum power_supply_property psp
)
557 struct axp20x_usb_power
*power
= power_supply_get_drvdata(psy
);
560 * The VBUS path select flag works differently on AXP288 and newer:
561 * - On AXP20x and AXP22x, the flag enables VBUS (ignoring N_VBUSEN).
562 * - On AXP288 and AXP8xx, the flag disables VBUS (ignoring N_VBUSEN).
563 * We only expose the control on variants where it can be used to force
564 * the VBUS input offline.
566 if (psp
== POWER_SUPPLY_PROP_ONLINE
)
567 return power
->vbus_disable_bit
!= NULL
;
569 return psp
== POWER_SUPPLY_PROP_VOLTAGE_MIN
||
570 psp
== POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
;
573 static int axp717_usb_power_prop_writeable(struct power_supply
*psy
,
574 enum power_supply_property psp
)
576 return psp
== POWER_SUPPLY_PROP_VOLTAGE_MIN
||
577 psp
== POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
;
580 static int axp20x_configure_iio_channels(struct platform_device
*pdev
,
581 struct axp20x_usb_power
*power
)
583 power
->vbus_v
= devm_iio_channel_get(&pdev
->dev
, "vbus_v");
584 if (IS_ERR(power
->vbus_v
)) {
585 if (PTR_ERR(power
->vbus_v
) == -ENODEV
)
586 return -EPROBE_DEFER
;
587 return PTR_ERR(power
->vbus_v
);
590 power
->vbus_i
= devm_iio_channel_get(&pdev
->dev
, "vbus_i");
591 if (IS_ERR(power
->vbus_i
)) {
592 if (PTR_ERR(power
->vbus_i
) == -ENODEV
)
593 return -EPROBE_DEFER
;
594 return PTR_ERR(power
->vbus_i
);
600 static int axp717_configure_iio_channels(struct platform_device
*pdev
,
601 struct axp20x_usb_power
*power
)
603 power
->vbus_v
= devm_iio_channel_get(&pdev
->dev
, "vbus_v");
604 if (IS_ERR(power
->vbus_v
)) {
605 if (PTR_ERR(power
->vbus_v
) == -ENODEV
)
606 return -EPROBE_DEFER
;
607 return PTR_ERR(power
->vbus_v
);
613 static int axp20x_configure_adc_registers(struct axp20x_usb_power
*power
)
615 /* Enable vbus voltage and current measurement */
616 return regmap_update_bits(power
->regmap
, AXP20X_ADC_EN1
,
617 AXP20X_ADC_EN1_VBUS_CURR
|
618 AXP20X_ADC_EN1_VBUS_VOLT
,
619 AXP20X_ADC_EN1_VBUS_CURR
|
620 AXP20X_ADC_EN1_VBUS_VOLT
);
623 static int axp717_configure_adc_registers(struct axp20x_usb_power
*power
)
625 /* Enable vbus voltage measurement */
626 return regmap_update_bits(power
->regmap
, AXP717_ADC_CH_EN_CONTROL
,
627 AXP717_ADC_EN_VBUS_VOLT
,
628 AXP717_ADC_EN_VBUS_VOLT
);
631 static enum power_supply_property axp20x_usb_power_properties
[] = {
632 POWER_SUPPLY_PROP_HEALTH
,
633 POWER_SUPPLY_PROP_PRESENT
,
634 POWER_SUPPLY_PROP_ONLINE
,
635 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
636 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
637 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
638 POWER_SUPPLY_PROP_CURRENT_NOW
,
641 static enum power_supply_property axp22x_usb_power_properties
[] = {
642 POWER_SUPPLY_PROP_HEALTH
,
643 POWER_SUPPLY_PROP_PRESENT
,
644 POWER_SUPPLY_PROP_ONLINE
,
645 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
646 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
649 static enum power_supply_property axp717_usb_power_properties
[] = {
650 POWER_SUPPLY_PROP_HEALTH
,
651 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
652 POWER_SUPPLY_PROP_ONLINE
,
653 POWER_SUPPLY_PROP_PRESENT
,
654 POWER_SUPPLY_PROP_USB_TYPE
,
655 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
656 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
659 static enum power_supply_property axp813_usb_power_properties
[] = {
660 POWER_SUPPLY_PROP_HEALTH
,
661 POWER_SUPPLY_PROP_PRESENT
,
662 POWER_SUPPLY_PROP_ONLINE
,
663 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
664 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
665 POWER_SUPPLY_PROP_USB_TYPE
,
668 static const struct power_supply_desc axp20x_usb_power_desc
= {
669 .name
= "axp20x-usb",
670 .type
= POWER_SUPPLY_TYPE_USB
,
671 .properties
= axp20x_usb_power_properties
,
672 .num_properties
= ARRAY_SIZE(axp20x_usb_power_properties
),
673 .property_is_writeable
= axp20x_usb_power_prop_writeable
,
674 .get_property
= axp20x_usb_power_get_property
,
675 .set_property
= axp20x_usb_power_set_property
,
678 static const struct power_supply_desc axp22x_usb_power_desc
= {
679 .name
= "axp20x-usb",
680 .type
= POWER_SUPPLY_TYPE_USB
,
681 .properties
= axp22x_usb_power_properties
,
682 .num_properties
= ARRAY_SIZE(axp22x_usb_power_properties
),
683 .property_is_writeable
= axp20x_usb_power_prop_writeable
,
684 .get_property
= axp20x_usb_power_get_property
,
685 .set_property
= axp20x_usb_power_set_property
,
688 static const struct power_supply_desc axp717_usb_power_desc
= {
689 .name
= "axp20x-usb",
690 .type
= POWER_SUPPLY_TYPE_USB
,
691 .properties
= axp717_usb_power_properties
,
692 .num_properties
= ARRAY_SIZE(axp717_usb_power_properties
),
693 .property_is_writeable
= axp717_usb_power_prop_writeable
,
694 .get_property
= axp717_usb_power_get_property
,
695 .set_property
= axp717_usb_power_set_property
,
696 .usb_types
= BIT(POWER_SUPPLY_USB_TYPE_SDP
) |
697 BIT(POWER_SUPPLY_USB_TYPE_CDP
) |
698 BIT(POWER_SUPPLY_USB_TYPE_DCP
) |
699 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN
),
702 static const struct power_supply_desc axp813_usb_power_desc
= {
703 .name
= "axp20x-usb",
704 .type
= POWER_SUPPLY_TYPE_USB
,
705 .properties
= axp813_usb_power_properties
,
706 .num_properties
= ARRAY_SIZE(axp813_usb_power_properties
),
707 .property_is_writeable
= axp20x_usb_power_prop_writeable
,
708 .get_property
= axp20x_usb_power_get_property
,
709 .set_property
= axp20x_usb_power_set_property
,
710 .usb_types
= BIT(POWER_SUPPLY_USB_TYPE_SDP
) |
711 BIT(POWER_SUPPLY_USB_TYPE_CDP
) |
712 BIT(POWER_SUPPLY_USB_TYPE_DCP
) |
713 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN
),
716 static const char * const axp20x_irq_names
[] = {
723 static const char * const axp22x_irq_names
[] = {
728 static const char * const axp717_irq_names
[] = {
734 static int axp192_usb_curr_lim_table
[] = {
741 static int axp20x_usb_curr_lim_table
[] = {
748 static int axp221_usb_curr_lim_table
[] = {
755 static int axp813_usb_curr_lim_table
[] = {
767 static const struct axp_data axp192_data
= {
768 .power_desc
= &axp20x_usb_power_desc
,
769 .irq_names
= axp20x_irq_names
,
770 .num_irq_names
= ARRAY_SIZE(axp20x_irq_names
),
771 .curr_lim_table
= axp192_usb_curr_lim_table
,
772 .curr_lim_table_size
= ARRAY_SIZE(axp192_usb_curr_lim_table
),
773 .curr_lim_fld
= REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT
, 0, 1),
774 .vbus_valid_bit
= REG_FIELD(AXP192_USB_OTG_STATUS
, 2, 2),
775 .vbus_mon_bit
= REG_FIELD(AXP20X_VBUS_MON
, 3, 3),
776 .axp20x_read_vbus
= &axp20x_usb_power_poll_vbus
,
777 .axp20x_cfg_iio_chan
= axp20x_configure_iio_channels
,
778 .axp20x_cfg_adc_reg
= axp20x_configure_adc_registers
,
781 static const struct axp_data axp202_data
= {
782 .power_desc
= &axp20x_usb_power_desc
,
783 .irq_names
= axp20x_irq_names
,
784 .num_irq_names
= ARRAY_SIZE(axp20x_irq_names
),
785 .curr_lim_table
= axp20x_usb_curr_lim_table
,
786 .curr_lim_table_size
= ARRAY_SIZE(axp20x_usb_curr_lim_table
),
787 .curr_lim_fld
= REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT
, 0, 1),
788 .vbus_valid_bit
= REG_FIELD(AXP20X_USB_OTG_STATUS
, 2, 2),
789 .vbus_mon_bit
= REG_FIELD(AXP20X_VBUS_MON
, 3, 3),
790 .axp20x_read_vbus
= &axp20x_usb_power_poll_vbus
,
791 .axp20x_cfg_iio_chan
= axp20x_configure_iio_channels
,
792 .axp20x_cfg_adc_reg
= axp20x_configure_adc_registers
,
795 static const struct axp_data axp221_data
= {
796 .power_desc
= &axp22x_usb_power_desc
,
797 .irq_names
= axp22x_irq_names
,
798 .num_irq_names
= ARRAY_SIZE(axp22x_irq_names
),
799 .curr_lim_table
= axp221_usb_curr_lim_table
,
800 .curr_lim_table_size
= ARRAY_SIZE(axp221_usb_curr_lim_table
),
801 .curr_lim_fld
= REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT
, 0, 1),
802 .vbus_needs_polling
= true,
803 .axp20x_read_vbus
= &axp20x_usb_power_poll_vbus
,
804 .axp20x_cfg_iio_chan
= axp20x_configure_iio_channels
,
805 .axp20x_cfg_adc_reg
= axp20x_configure_adc_registers
,
808 static const struct axp_data axp223_data
= {
809 .power_desc
= &axp22x_usb_power_desc
,
810 .irq_names
= axp22x_irq_names
,
811 .num_irq_names
= ARRAY_SIZE(axp22x_irq_names
),
812 .curr_lim_table
= axp20x_usb_curr_lim_table
,
813 .curr_lim_table_size
= ARRAY_SIZE(axp20x_usb_curr_lim_table
),
814 .curr_lim_fld
= REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT
, 0, 1),
815 .vbus_needs_polling
= true,
816 .axp20x_read_vbus
= &axp20x_usb_power_poll_vbus
,
817 .axp20x_cfg_iio_chan
= axp20x_configure_iio_channels
,
818 .axp20x_cfg_adc_reg
= axp20x_configure_adc_registers
,
821 static const struct axp_data axp717_data
= {
822 .power_desc
= &axp717_usb_power_desc
,
823 .irq_names
= axp717_irq_names
,
824 .num_irq_names
= ARRAY_SIZE(axp717_irq_names
),
825 .curr_lim_fld
= REG_FIELD(AXP717_INPUT_CUR_LIMIT_CTRL
, 0, 5),
826 .usb_bc_en_bit
= REG_FIELD(AXP717_MODULE_EN_CONTROL_1
, 4, 4),
827 .usb_bc_det_fld
= REG_FIELD(AXP717_BC_DETECT
, 5, 7),
828 .vbus_mon_bit
= REG_FIELD(AXP717_ADC_CH_EN_CONTROL
, 2, 2),
829 .vbus_needs_polling
= false,
830 .axp20x_read_vbus
= &axp717_usb_power_poll_vbus
,
831 .axp20x_cfg_iio_chan
= axp717_configure_iio_channels
,
832 .axp20x_cfg_adc_reg
= axp717_configure_adc_registers
,
835 static const struct axp_data axp813_data
= {
836 .power_desc
= &axp813_usb_power_desc
,
837 .irq_names
= axp22x_irq_names
,
838 .num_irq_names
= ARRAY_SIZE(axp22x_irq_names
),
839 .curr_lim_table
= axp813_usb_curr_lim_table
,
840 .curr_lim_table_size
= ARRAY_SIZE(axp813_usb_curr_lim_table
),
841 .curr_lim_fld
= REG_FIELD(AXP22X_CHRG_CTRL3
, 4, 7),
842 .usb_bc_en_bit
= REG_FIELD(AXP288_BC_GLOBAL
, 0, 0),
843 .usb_bc_det_fld
= REG_FIELD(AXP288_BC_DET_STAT
, 5, 7),
844 .vbus_disable_bit
= REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT
, 7, 7),
845 .vbus_needs_polling
= true,
846 .axp20x_read_vbus
= &axp20x_usb_power_poll_vbus
,
847 .axp20x_cfg_iio_chan
= axp20x_configure_iio_channels
,
848 .axp20x_cfg_adc_reg
= axp20x_configure_adc_registers
,
851 #ifdef CONFIG_PM_SLEEP
852 static int axp20x_usb_power_suspend(struct device
*dev
)
854 struct axp20x_usb_power
*power
= dev_get_drvdata(dev
);
858 * Allow wake via VBUS_PLUGIN only.
860 * As nested threaded IRQs are not automatically disabled during
861 * suspend, we must explicitly disable the remainder of the IRQs.
863 if (device_may_wakeup(&power
->supply
->dev
))
864 enable_irq_wake(power
->irqs
[i
++]);
865 while (i
< power
->num_irqs
)
866 disable_irq(power
->irqs
[i
++]);
871 static int axp20x_usb_power_resume(struct device
*dev
)
873 struct axp20x_usb_power
*power
= dev_get_drvdata(dev
);
876 if (device_may_wakeup(&power
->supply
->dev
))
877 disable_irq_wake(power
->irqs
[i
++]);
878 while (i
< power
->num_irqs
)
879 enable_irq(power
->irqs
[i
++]);
881 mod_delayed_work(system_power_efficient_wq
, &power
->vbus_detect
, DEBOUNCE_TIME
);
887 static SIMPLE_DEV_PM_OPS(axp20x_usb_power_pm_ops
, axp20x_usb_power_suspend
,
888 axp20x_usb_power_resume
);
890 static int axp20x_regmap_field_alloc_optional(struct device
*dev
,
891 struct regmap
*regmap
,
892 struct reg_field fdesc
,
893 struct regmap_field
**fieldp
)
895 struct regmap_field
*field
;
897 if (fdesc
.reg
== 0) {
902 field
= devm_regmap_field_alloc(dev
, regmap
, fdesc
);
904 return PTR_ERR(field
);
910 /* Optionally allow users to specify a maximum charging current. */
911 static void axp20x_usb_power_parse_dt(struct device
*dev
,
912 struct axp20x_usb_power
*power
)
916 ret
= device_property_read_u32(dev
, "input-current-limit-microamp",
917 &power
->max_input_cur
);
919 dev_dbg(dev
, "%s() no input-current-limit specified\n", __func__
);
922 static int axp20x_usb_power_probe(struct platform_device
*pdev
)
924 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
925 struct power_supply_config psy_cfg
= {};
926 struct axp20x_usb_power
*power
;
927 const struct axp_data
*axp_data
;
930 if (!of_device_is_available(pdev
->dev
.of_node
))
934 dev_err(&pdev
->dev
, "Parent drvdata not set\n");
938 axp_data
= of_device_get_match_data(&pdev
->dev
);
940 power
= devm_kzalloc(&pdev
->dev
,
941 struct_size(power
, irqs
, axp_data
->num_irq_names
),
946 platform_set_drvdata(pdev
, power
);
948 power
->dev
= &pdev
->dev
;
949 power
->axp_data
= axp_data
;
950 power
->regmap
= axp20x
->regmap
;
951 power
->num_irqs
= axp_data
->num_irq_names
;
953 power
->curr_lim_fld
= devm_regmap_field_alloc(&pdev
->dev
, power
->regmap
,
954 axp_data
->curr_lim_fld
);
955 if (IS_ERR(power
->curr_lim_fld
))
956 return PTR_ERR(power
->curr_lim_fld
);
958 axp20x_usb_power_parse_dt(&pdev
->dev
, power
);
960 ret
= axp20x_regmap_field_alloc_optional(&pdev
->dev
, power
->regmap
,
961 axp_data
->vbus_valid_bit
,
962 &power
->vbus_valid_bit
);
966 ret
= axp20x_regmap_field_alloc_optional(&pdev
->dev
, power
->regmap
,
967 axp_data
->vbus_mon_bit
,
968 &power
->vbus_mon_bit
);
972 ret
= axp20x_regmap_field_alloc_optional(&pdev
->dev
, power
->regmap
,
973 axp_data
->usb_bc_en_bit
,
974 &power
->usb_bc_en_bit
);
978 ret
= axp20x_regmap_field_alloc_optional(&pdev
->dev
, power
->regmap
,
979 axp_data
->usb_bc_det_fld
,
980 &power
->usb_bc_det_fld
);
984 ret
= axp20x_regmap_field_alloc_optional(&pdev
->dev
, power
->regmap
,
985 axp_data
->vbus_disable_bit
,
986 &power
->vbus_disable_bit
);
990 ret
= devm_delayed_work_autocancel(&pdev
->dev
, &power
->vbus_detect
,
991 axp_data
->axp20x_read_vbus
);
995 if (power
->vbus_mon_bit
) {
996 /* Enable vbus valid checking */
997 ret
= regmap_field_write(power
->vbus_mon_bit
, 1);
1001 if (IS_ENABLED(CONFIG_AXP20X_ADC
))
1002 ret
= axp_data
->axp20x_cfg_iio_chan(pdev
, power
);
1004 ret
= axp_data
->axp20x_cfg_adc_reg(power
);
1010 if (power
->usb_bc_en_bit
) {
1011 /* Enable USB Battery Charging specification detection */
1012 ret
= regmap_field_write(power
->usb_bc_en_bit
, 1);
1017 psy_cfg
.of_node
= pdev
->dev
.of_node
;
1018 psy_cfg
.drv_data
= power
;
1020 power
->supply
= devm_power_supply_register(&pdev
->dev
,
1021 axp_data
->power_desc
,
1023 if (IS_ERR(power
->supply
))
1024 return PTR_ERR(power
->supply
);
1026 /* Request irqs after registering, as irqs may trigger immediately */
1027 for (i
= 0; i
< axp_data
->num_irq_names
; i
++) {
1028 irq
= platform_get_irq_byname(pdev
, axp_data
->irq_names
[i
]);
1032 power
->irqs
[i
] = regmap_irq_get_virq(axp20x
->regmap_irqc
, irq
);
1033 ret
= devm_request_any_context_irq(&pdev
->dev
, power
->irqs
[i
],
1034 axp20x_usb_power_irq
, 0,
1037 dev_err(&pdev
->dev
, "Error requesting %s IRQ: %d\n",
1038 axp_data
->irq_names
[i
], ret
);
1043 if (axp20x_usb_vbus_needs_polling(power
))
1044 queue_delayed_work(system_power_efficient_wq
, &power
->vbus_detect
, 0);
1049 static const struct of_device_id axp20x_usb_power_match
[] = {
1051 .compatible
= "x-powers,axp192-usb-power-supply",
1052 .data
= &axp192_data
,
1054 .compatible
= "x-powers,axp202-usb-power-supply",
1055 .data
= &axp202_data
,
1057 .compatible
= "x-powers,axp221-usb-power-supply",
1058 .data
= &axp221_data
,
1060 .compatible
= "x-powers,axp223-usb-power-supply",
1061 .data
= &axp223_data
,
1063 .compatible
= "x-powers,axp717-usb-power-supply",
1064 .data
= &axp717_data
,
1066 .compatible
= "x-powers,axp813-usb-power-supply",
1067 .data
= &axp813_data
,
1068 }, { /* sentinel */ }
1070 MODULE_DEVICE_TABLE(of
, axp20x_usb_power_match
);
1072 static struct platform_driver axp20x_usb_power_driver
= {
1073 .probe
= axp20x_usb_power_probe
,
1076 .of_match_table
= axp20x_usb_power_match
,
1077 .pm
= &axp20x_usb_power_pm_ops
,
1081 module_platform_driver(axp20x_usb_power_driver
);
1083 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1084 MODULE_DESCRIPTION("AXP20x PMIC USB power supply status driver");
1085 MODULE_LICENSE("GPL");