Skip to content

Commit 2ac6b2e

Browse files
justephjic23
authored andcommitted
iio: adc: ad7380: use devm_regulator_get_enable_read_voltage()
Use devm_regulator_get_enable_read_voltage() to simplify the code. Reviewed-by: Nuno Sa <nuno.sa@analog.com> Reviewed-by: David Lechner <dlechner@baylibre.com> Signed-off-by: Julien Stephan <jstephan@baylibre.com> Link: https://patch.msgid.link/20241022-ad7380-fix-supplies-v3-2-f0cefe1b7fa6@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent fbe5956 commit 2ac6b2e

File tree

1 file changed

+21
-60
lines changed

1 file changed

+21
-60
lines changed

drivers/iio/adc/ad7380.c

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ static const struct iio_info ad7380_info = {
956956
.debugfs_reg_access = &ad7380_debugfs_reg_access,
957957
};
958958

959-
static int ad7380_init(struct ad7380_state *st, struct regulator *vref)
959+
static int ad7380_init(struct ad7380_state *st, bool external_ref_en)
960960
{
961961
int ret;
962962

@@ -968,13 +968,13 @@ static int ad7380_init(struct ad7380_state *st, struct regulator *vref)
968968
if (ret < 0)
969969
return ret;
970970

971-
/* select internal or external reference voltage */
972-
ret = regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG1,
973-
AD7380_CONFIG1_REFSEL,
974-
FIELD_PREP(AD7380_CONFIG1_REFSEL,
975-
vref ? 1 : 0));
976-
if (ret < 0)
977-
return ret;
971+
if (external_ref_en) {
972+
/* select external reference voltage */
973+
ret = regmap_set_bits(st->regmap, AD7380_REG_ADDR_CONFIG1,
974+
AD7380_CONFIG1_REFSEL);
975+
if (ret < 0)
976+
return ret;
977+
}
978978

979979
/* This is the default value after reset. */
980980
st->oversampling_ratio = 1;
@@ -987,16 +987,11 @@ static int ad7380_init(struct ad7380_state *st, struct regulator *vref)
987987
FIELD_PREP(AD7380_CONFIG2_SDO, 1));
988988
}
989989

990-
static void ad7380_regulator_disable(void *p)
991-
{
992-
regulator_disable(p);
993-
}
994-
995990
static int ad7380_probe(struct spi_device *spi)
996991
{
997992
struct iio_dev *indio_dev;
998993
struct ad7380_state *st;
999-
struct regulator *vref;
994+
bool external_ref_en;
1000995
int ret, i;
1001996

1002997
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
@@ -1009,37 +1004,17 @@ static int ad7380_probe(struct spi_device *spi)
10091004
if (!st->chip_info)
10101005
return dev_err_probe(&spi->dev, -EINVAL, "missing match data\n");
10111006

1012-
vref = devm_regulator_get_optional(&spi->dev, "refio");
1013-
if (IS_ERR(vref)) {
1014-
if (PTR_ERR(vref) != -ENODEV)
1015-
return dev_err_probe(&spi->dev, PTR_ERR(vref),
1016-
"Failed to get refio regulator\n");
1017-
1018-
vref = NULL;
1019-
}
1020-
10211007
/*
10221008
* If there is no REFIO supply, then it means that we are using
10231009
* the internal 2.5V reference, otherwise REFIO is reference voltage.
10241010
*/
1025-
if (vref) {
1026-
ret = regulator_enable(vref);
1027-
if (ret)
1028-
return ret;
1011+
ret = devm_regulator_get_enable_read_voltage(&spi->dev, "refio");
1012+
if (ret < 0 && ret != -ENODEV)
1013+
return dev_err_probe(&spi->dev, ret,
1014+
"Failed to get refio regulator\n");
10291015

1030-
ret = devm_add_action_or_reset(&spi->dev,
1031-
ad7380_regulator_disable, vref);
1032-
if (ret)
1033-
return ret;
1034-
1035-
ret = regulator_get_voltage(vref);
1036-
if (ret < 0)
1037-
return ret;
1038-
1039-
st->vref_mv = ret / 1000;
1040-
} else {
1041-
st->vref_mv = AD7380_INTERNAL_REF_MV;
1042-
}
1016+
external_ref_en = ret != -ENODEV;
1017+
st->vref_mv = external_ref_en ? ret / 1000 : AD7380_INTERNAL_REF_MV;
10431018

10441019
if (st->chip_info->num_vcm_supplies > ARRAY_SIZE(st->vcm_mv))
10451020
return dev_err_probe(&spi->dev, -EINVAL,
@@ -1050,27 +1025,13 @@ static int ad7380_probe(struct spi_device *spi)
10501025
* input pin.
10511026
*/
10521027
for (i = 0; i < st->chip_info->num_vcm_supplies; i++) {
1053-
struct regulator *vcm;
1054-
1055-
vcm = devm_regulator_get(&spi->dev,
1056-
st->chip_info->vcm_supplies[i]);
1057-
if (IS_ERR(vcm))
1058-
return dev_err_probe(&spi->dev, PTR_ERR(vcm),
1059-
"Failed to get %s regulator\n",
1060-
st->chip_info->vcm_supplies[i]);
1028+
const char *vcm = st->chip_info->vcm_supplies[i];
10611029

1062-
ret = regulator_enable(vcm);
1063-
if (ret)
1064-
return ret;
1065-
1066-
ret = devm_add_action_or_reset(&spi->dev,
1067-
ad7380_regulator_disable, vcm);
1068-
if (ret)
1069-
return ret;
1070-
1071-
ret = regulator_get_voltage(vcm);
1030+
ret = devm_regulator_get_enable_read_voltage(&spi->dev, vcm);
10721031
if (ret < 0)
1073-
return ret;
1032+
return dev_err_probe(&spi->dev, ret,
1033+
"Failed to get %s regulator\n",
1034+
vcm);
10741035

10751036
st->vcm_mv[i] = ret / 1000;
10761037
}
@@ -1135,7 +1096,7 @@ static int ad7380_probe(struct spi_device *spi)
11351096
if (ret)
11361097
return ret;
11371098

1138-
ret = ad7380_init(st, vref);
1099+
ret = ad7380_init(st, external_ref_en);
11391100
if (ret)
11401101
return ret;
11411102

0 commit comments

Comments
 (0)