Skip to content

Commit 4573472

Browse files
committed
iio: adc: ad7124: Fix missbalanced regulator enable / disable on error.
If the devm_regulator_get() call succeeded but not the regulator_enable() then regulator_disable() would be called on a regulator that was not enabled. Fix this by moving regulator enabling / disabling over to devm_ management via devm_add_action_or_reset. Alexandru's sign-off here because he pulled Jonathan's patch into a larger set which Jonathan then applied. Fixes: b3af341 ("iio: adc: Add ad7124 support") Reviewed-by: Alexandru Ardelean <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]> Signed-off-by: Alexandru Ardelean <[email protected]> Cc: <[email protected]>
1 parent ba9c25d commit 4573472

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

drivers/iio/adc/ad7124.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,11 @@ static int ad7124_setup(struct ad7124_state *st)
850850
return ret;
851851
}
852852

853+
static void ad7124_reg_disable(void *r)
854+
{
855+
regulator_disable(r);
856+
}
857+
853858
static int ad7124_probe(struct spi_device *spi)
854859
{
855860
const struct ad7124_chip_info *info;
@@ -895,17 +900,20 @@ static int ad7124_probe(struct spi_device *spi)
895900
ret = regulator_enable(st->vref[i]);
896901
if (ret)
897902
return ret;
903+
904+
ret = devm_add_action_or_reset(&spi->dev, ad7124_reg_disable,
905+
st->vref[i]);
906+
if (ret)
907+
return ret;
898908
}
899909

900910
st->mclk = devm_clk_get(&spi->dev, "mclk");
901-
if (IS_ERR(st->mclk)) {
902-
ret = PTR_ERR(st->mclk);
903-
goto error_regulator_disable;
904-
}
911+
if (IS_ERR(st->mclk))
912+
return PTR_ERR(st->mclk);
905913

906914
ret = clk_prepare_enable(st->mclk);
907915
if (ret < 0)
908-
goto error_regulator_disable;
916+
return ret;
909917

910918
ret = ad7124_soft_reset(st);
911919
if (ret < 0)
@@ -935,11 +943,6 @@ static int ad7124_probe(struct spi_device *spi)
935943
ad_sd_cleanup_buffer_and_trigger(indio_dev);
936944
error_clk_disable_unprepare:
937945
clk_disable_unprepare(st->mclk);
938-
error_regulator_disable:
939-
for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
940-
if (!IS_ERR_OR_NULL(st->vref[i]))
941-
regulator_disable(st->vref[i]);
942-
}
943946

944947
return ret;
945948
}
@@ -948,17 +951,11 @@ static int ad7124_remove(struct spi_device *spi)
948951
{
949952
struct iio_dev *indio_dev = spi_get_drvdata(spi);
950953
struct ad7124_state *st = iio_priv(indio_dev);
951-
int i;
952954

953955
iio_device_unregister(indio_dev);
954956
ad_sd_cleanup_buffer_and_trigger(indio_dev);
955957
clk_disable_unprepare(st->mclk);
956958

957-
for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) {
958-
if (!IS_ERR_OR_NULL(st->vref[i]))
959-
regulator_disable(st->vref[i]);
960-
}
961-
962959
return 0;
963960
}
964961

0 commit comments

Comments
 (0)