Skip to content

Commit 05c14ac

Browse files
Aidan MacDonaldsre
authored andcommitted
power: supply: axp20x_usb_power: Use regmap fields for VBUS monitor feature
Use regmap fields to describe the VBUS valid bit and VBUS monitor enable bit. This allows the driver to easily support other chips, eg. the AXP192, that have the VBUS valid bit in a different register. Signed-off-by: Aidan MacDonald <aidanmacdonald.0x0@gmail.com> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
1 parent 28ca77c commit 05c14ac

1 file changed

Lines changed: 45 additions & 12 deletions

File tree

drivers/power/supply/axp20x_usb_power.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
#define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
4242
#define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
4343

44-
#define AXP20X_VBUS_MON_VBUS_VALID BIT(3)
45-
4644
#define AXP813_BC_EN BIT(0)
4745

4846
/*
@@ -58,11 +56,15 @@ struct axp_data {
5856
enum axp20x_variants axp20x_id;
5957
const int *curr_lim_table;
6058
struct reg_field curr_lim_fld;
59+
struct reg_field vbus_valid_bit;
60+
struct reg_field vbus_mon_bit;
6161
};
6262

6363
struct axp20x_usb_power {
6464
struct regmap *regmap;
6565
struct regmap_field *curr_lim_fld;
66+
struct regmap_field *vbus_valid_bit;
67+
struct regmap_field *vbus_mon_bit;
6668
struct power_supply *supply;
6769
enum axp20x_variants axp20x_id;
6870
const struct axp_data *axp_data;
@@ -206,16 +208,15 @@ static int axp20x_usb_power_get_property(struct power_supply *psy,
206208

207209
val->intval = POWER_SUPPLY_HEALTH_GOOD;
208210

209-
if (power->axp20x_id == AXP202_ID) {
210-
ret = regmap_read(power->regmap,
211-
AXP20X_USB_OTG_STATUS, &v);
211+
if (power->vbus_valid_bit) {
212+
ret = regmap_field_read(power->vbus_valid_bit, &v);
212213
if (ret)
213214
return ret;
214215

215-
if (!(v & AXP20X_USB_STATUS_VBUS_VALID))
216-
val->intval =
217-
POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
216+
if (v == 0)
217+
val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
218218
}
219+
219220
break;
220221
case POWER_SUPPLY_PROP_PRESENT:
221222
val->intval = !!(input & AXP20X_PWR_STATUS_VBUS_PRESENT);
@@ -403,6 +404,8 @@ static const struct axp_data axp202_data = {
403404
.axp20x_id = AXP202_ID,
404405
.curr_lim_table = axp20x_usb_curr_lim_table,
405406
.curr_lim_fld = REG_FIELD(AXP20X_VBUS_IPSOUT_MGMT, 0, 1),
407+
.vbus_valid_bit = REG_FIELD(AXP20X_USB_OTG_STATUS, 2, 2),
408+
.vbus_mon_bit = REG_FIELD(AXP20X_VBUS_MON, 3, 3),
406409
};
407410

408411
static const struct axp_data axp221_data = {
@@ -501,6 +504,26 @@ static int configure_adc_registers(struct axp20x_usb_power *power)
501504
AXP20X_ADC_EN1_VBUS_VOLT);
502505
}
503506

507+
static int axp20x_regmap_field_alloc_optional(struct device *dev,
508+
struct regmap *regmap,
509+
struct reg_field fdesc,
510+
struct regmap_field **fieldp)
511+
{
512+
struct regmap_field *field;
513+
514+
if (fdesc.reg == 0) {
515+
*fieldp = NULL;
516+
return 0;
517+
}
518+
519+
field = devm_regmap_field_alloc(dev, regmap, fdesc);
520+
if (IS_ERR(field))
521+
return PTR_ERR(field);
522+
523+
*fieldp = field;
524+
return 0;
525+
}
526+
504527
static int axp20x_usb_power_probe(struct platform_device *pdev)
505528
{
506529
struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
@@ -537,16 +560,26 @@ static int axp20x_usb_power_probe(struct platform_device *pdev)
537560
if (IS_ERR(power->curr_lim_fld))
538561
return PTR_ERR(power->curr_lim_fld);
539562

563+
ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap,
564+
axp_data->vbus_valid_bit,
565+
&power->vbus_valid_bit);
566+
if (ret)
567+
return ret;
568+
569+
ret = axp20x_regmap_field_alloc_optional(&pdev->dev, power->regmap,
570+
axp_data->vbus_mon_bit,
571+
&power->vbus_mon_bit);
572+
if (ret)
573+
return ret;
574+
540575
ret = devm_delayed_work_autocancel(&pdev->dev, &power->vbus_detect,
541576
axp20x_usb_power_poll_vbus);
542577
if (ret)
543578
return ret;
544579

545-
if (power->axp20x_id == AXP202_ID) {
580+
if (power->vbus_mon_bit) {
546581
/* Enable vbus valid checking */
547-
ret = regmap_update_bits(power->regmap, AXP20X_VBUS_MON,
548-
AXP20X_VBUS_MON_VBUS_VALID,
549-
AXP20X_VBUS_MON_VBUS_VALID);
582+
ret = regmap_field_write(power->vbus_mon_bit, 1);
550583
if (ret)
551584
return ret;
552585

0 commit comments

Comments
 (0)