Skip to contentSkip to conversation

iio: adc: ad7192: Correct reference voltage

PATCH Superseded [email protected] 2 participants 2 messages Sep 23, 2023 – Sep 24, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: ad7192: Correct reference voltage

From: Alisa-Dariana Roman <[email protected]>

The avdd and the reference voltage are two different sources but the reference voltage was assigned according to the avdd supply.

Add vref regulator structure and set the reference voltage according to the vref supply from the devicetree.

In case vref supply is missing, reference voltage is set according to the avdd supply for compatibility with old devicetrees.

Fixes b581f748cce0 ("staging: iio: adc: ad7192: move out of staging")
---
 drivers/iio/adc/ad7192.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
drivers/iio/adc/ad7192.c +27 -4
index 69d1103b9508..c414fed60dd3 100644
@@ -177,6 +177,7 @@ struct ad7192_chip_info {
 struct ad7192_state {
 	const struct ad7192_chip_info	*chip_info;
 	struct regulator		*avdd;
+	struct regulator		*vref;
 	struct clk			*mclk;
 	u16				int_vref_mv;
 	u32				fclk;
@@ -1008,10 +1009,32 @@ static int ad7192_probe(struct spi_device *spi)
 	if (ret)
 		return dev_err_probe(&spi->dev, ret, "Failed to enable specified DVdd supply\n");
 
-	ret = regulator_get_voltage(st->avdd);
-	if (ret < 0) {
-		dev_err(&spi->dev, "Device tree error, reference voltage undefined\n");
-		return ret;
+	st->vref = devm_regulator_get_optional(&spi->dev, "vref");
+	if (IS_ERR(st->vref)) {
+		if (PTR_ERR(st->vref) != -ENODEV)
+			return PTR_ERR(st->vref);
+
+		ret = regulator_get_voltage(st->avdd);
+		if (ret < 0) {
+			dev_err(&spi->dev, "Device tree error, AVdd voltage undefined\n");
+			return ret;
+		}
+	} else {
+		ret = regulator_enable(st->vref);
+		if (ret) {
+			dev_err(&spi->dev, "Failed to enable specified Vref supply\n");
+			return ret;
+		}
+
+		ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->vref);
+		if (ret)
+			return ret;
+
+		ret = regulator_get_voltage(st->vref);
+		if (ret < 0) {
+			dev_err(&spi->dev, "Device tree error, Vref voltage undefined\n");
+			return ret;
+		}
 	}
 	st->int_vref_mv = ret / 1000;
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 8 lines · On Sun, 24 Sep 2023 01:58:27 +0300 [email protected] wrote:

From: Alisa-Dariana Roman <[email protected]>

The avdd and the reference voltage are two different sources but the reference voltage was assigned according to the avdd supply.

Add vref regulator structure and set the reference voltage according to the vref supply from the devicetree.

Quoted 6 lines

In case vref supply is missing, reference voltage is set according to the avdd supply for compatibility with old devicetrees.

Fixes b581f748cce0 ("staging: iio: adc: ad7192: move out of staging")

Looks good but I'd prefer dev_err_probe() for the error messages. Obviously that wasn't true for the existing one but as we are touching the code good to clean it up.

Thanks,

Jonathan

Quoted 22 lines
---
 drivers/iio/adc/ad7192.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
drivers/iio/adc/ad7192.c +1 -2
index 69d1103b9508..c414fed60dd3 100644
@@ -177,6 +177,7 @@ struct ad7192_chip_info {
 struct ad7192_state {
 	const struct ad7192_chip_info	*chip_info;
 	struct regulator		*avdd;
+	struct regulator		*vref;
 	struct clk			*mclk;
 	u16				int_vref_mv;
 	u32				fclk;
@@ -1008,10 +1009,32 @@ static int ad7192_probe(struct spi_device *spi)
 	if (ret)
 		return dev_err_probe(&spi->dev, ret, "Failed to enable specified DVdd supply\n");
 
-	ret = regulator_get_voltage(st->avdd);
-	if (ret < 0) {

drivers/iio/adc/ad7192.c static int ad7192_probe(struct spi_device *spi) · line 1019

Quoted 10 lines
drivers/iio/adc/ad7192.c +8 -2
-		dev_err(&spi->dev, "Device tree error, reference voltage undefined\n");
-		return ret;
+	st->vref = devm_regulator_get_optional(&spi->dev, "vref");
+	if (IS_ERR(st->vref)) {
+		if (PTR_ERR(st->vref) != -ENODEV)
+			return PTR_ERR(st->vref);
+
+		ret = regulator_get_voltage(st->avdd);
+		if (ret < 0) {
+			dev_err(&spi->dev, "Device tree error, AVdd voltage undefined\n");

dev_err_probe()

drivers/iio/adc/ad7192.c static int ad7192_probe(struct spi_device *spi) · line 1025

Quoted 6 lines
drivers/iio/adc/ad7192.c +6 -0
+			return ret;
+		}
+	} else {
+		ret = regulator_enable(st->vref);
+		if (ret) {
+			dev_err(&spi->dev, "Failed to enable specified Vref supply\n");

dev_err_probe()

drivers/iio/adc/ad7192.c static int ad7192_probe(struct spi_device *spi) · line 1035

Quoted 10 lines
drivers/iio/adc/ad7192.c +10 -0
+			return ret;
+		}
+
+		ret = devm_add_action_or_reset(&spi->dev, ad7192_reg_disable, st->vref);
+		if (ret)
+			return ret;
+
+		ret = regulator_get_voltage(st->vref);
+		if (ret < 0) {
+			dev_err(&spi->dev, "Device tree error, Vref voltage undefined\n");

dev_err_probe()

Quoted 5 lines
drivers/iio/adc/ad7192.c +2 -0
+			return ret;
+		}
 	}
 	st->int_vref_mv = ret / 1000;