Skip to contentSkip to conversation

iio: adc: ad799x: reference voltage capability

PATCH Superseded Stefano Manni 4 participants 9 messages Aug 6, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover iio: adc: ad799x: reference voltage capability

This patch series refactors 6b104e7895ab16b9b7f466c5f2ca282b87f661e8 in order to add the capability of the chip to have an external reference voltage into the chip_info struct.
And so avoid ugly conditional checks on the chip id.

In addition the AD7994 is marked to have the external reference voltage as well.

Stefano Manni (2):
iio: adc: ad799x: add reference voltage capability to chip_info
iio: adc: ad799x: add reference voltage to ad7994

 drivers/iio/adc/ad799x.c | 45 +++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 19 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/2 iio: adc: ad799x: add reference voltage capability to chip_info

If the chip supports an external reference voltage on REFIN pin then the "vref-supply" regulator may be used.

This commit partially refactors 6b104e7895ab16b9b7f466c5f2ca282b87f661e8 to add the capability of the chip to have an external voltage reference and then remove the ugly conditional check on chip id.

---
 drivers/iio/adc/ad799x.c | 45 +++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 19 deletions(-)
drivers/iio/adc/ad799x.c +26 -19
index 9c02f9199139..955d845407b9 100644
@@ -114,11 +114,13 @@ struct ad799x_chip_config {
  * @num_channels:	number of channels
  * @noirq_config:	device configuration w/o IRQ
  * @irq_config:		device configuration w/IRQ
+ * @has_vref:		device supports external reference voltage
  */
 struct ad799x_chip_info {
 	int				num_channels;
 	const struct ad799x_chip_config	noirq_config;
 	const struct ad799x_chip_config	irq_config;
+	bool has_vref;
 };
 
 struct ad799x_state {
@@ -604,6 +606,7 @@ static const struct iio_event_spec ad799x_events[] = {
 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	[ad7991] = {
 		.num_channels = 5,
+		.has_vref = true,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
@@ -617,6 +620,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7995] = {
 		.num_channels = 5,
+		.has_vref = true,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 10),
@@ -630,6 +634,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7999] = {
 		.num_channels = 5,
+		.has_vref = true,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 8),
@@ -643,6 +648,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7992] = {
 		.num_channels = 3,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
@@ -663,6 +669,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7993] = {
 		.num_channels = 5,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 10),
@@ -687,6 +694,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7994] = {
 		.num_channels = 5,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
@@ -711,6 +719,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7997] = {
 		.num_channels = 9,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 10),
@@ -743,6 +752,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7998] = {
 		.num_channels = 9,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
@@ -809,34 +819,31 @@ static int ad799x_probe(struct i2c_client *client)
 		return ret;
 
 	/* check if an external reference is supplied */
-	st->vref = devm_regulator_get_optional(&client->dev, "vref");
-
-	if (IS_ERR(st->vref)) {
-		if (PTR_ERR(st->vref) == -ENODEV) {
-			st->vref = NULL;
-			dev_info(&client->dev, "Using VCC reference voltage\n");
-		} else {
-			ret = PTR_ERR(st->vref);
-			goto error_disable_reg;
+	if (chip_info->has_vref) {
+		st->vref = devm_regulator_get_optional(&client->dev, "vref");
+
+		if (IS_ERR(st->vref)) {
+			if (PTR_ERR(st->vref) == -ENODEV) {
+				st->vref = NULL;
+				dev_info(&client->dev, "Using VCC reference voltage\n");
+			} else {
+				ret = PTR_ERR(st->vref);
+				goto error_disable_reg;
+			}
 		}
-	}
 
-	if (st->vref) {
-		/*
-		 * Use external reference voltage if supported by hardware.
-		 * This is optional if voltage / regulator present, use VCC otherwise.
-		 */
-		if ((st->id == ad7991) || (st->id == ad7995) || (st->id == ad7999)) {
+		if (st->vref) {
 			dev_info(&client->dev, "Using external reference voltage\n");
 			extra_config |= AD7991_REF_SEL;
 			ret = regulator_enable(st->vref);
 			if (ret)
 				goto error_disable_reg;
-		} else {
-			st->vref = NULL;
-			dev_warn(&client->dev, "Supplied reference not supported\n");
 		}
 	}
+	else {
+		st->vref = NULL;
+		dev_dbg(&client->dev, "Supplied reference not supported\n");
+	}
 
 	st->client = client;
 
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH 1/2] iio: adc: ad799x: add reference voltage capability to chip_info

Quoted 9 lines · On Wed, 6 Aug 2025 11:01:57 +0200 Stefano Manni <[email protected]>…

If the chip supports an external reference voltage on REFIN pin then the "vref-supply" regulator may be used.

This commit partially refactors 6b104e7895ab16b9b7f466c5f2ca282b87f661e8 to add the capability of the chip to have an external voltage reference and then remove the ugly conditional check on chip id.

Hi Stefano,
`
A few minor things inline.

Mostly looks good to me.

Jonathan

Quoted 71 lines
---
 drivers/iio/adc/ad799x.c | 45 +++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 19 deletions(-)
drivers/iio/adc/ad799x.c +8 -0
index 9c02f9199139..955d845407b9 100644
@@ -114,11 +114,13 @@ struct ad799x_chip_config {
  * @num_channels:	number of channels
  * @noirq_config:	device configuration w/o IRQ
  * @irq_config:		device configuration w/IRQ
+ * @has_vref:		device supports external reference voltage
  */
 struct ad799x_chip_info {
 	int				num_channels;
 	const struct ad799x_chip_config	noirq_config;
 	const struct ad799x_chip_config	irq_config;
+	bool has_vref;
 };
 
 struct ad799x_state {
@@ -604,6 +606,7 @@ static const struct iio_event_spec ad799x_events[] = {
 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	[ad7991] = {
 		.num_channels = 5,
+		.has_vref = true,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
@@ -617,6 +620,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7995] = {
 		.num_channels = 5,
+		.has_vref = true,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 10),
@@ -630,6 +634,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7999] = {
 		.num_channels = 5,
+		.has_vref = true,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 8),
@@ -643,6 +648,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7992] = {
 		.num_channels = 3,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
@@ -663,6 +669,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7993] = {
 		.num_channels = 5,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 10),
@@ -687,6 +694,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7994] = {
 		.num_channels = 5,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),

drivers/iio/adc/ad799x.c static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { · line 722

Quoted 4 lines
drivers/iio/adc/ad799x.c +1 -0
@@ -711,6 +719,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7997] = {
 		.num_channels = 9,
+		.has_vref = false,

Seems like 'not' having a vref is a reasonable default, so you could just not set it at all in those cases and rely on the default being false.

Quoted 46 lines
drivers/iio/adc/ad799x.c +13 -16
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 10),
@@ -743,6 +752,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7998] = {
 		.num_channels = 9,
+		.has_vref = false,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
@@ -809,34 +819,31 @@ static int ad799x_probe(struct i2c_client *client)
 		return ret;
 
 	/* check if an external reference is supplied */
-	st->vref = devm_regulator_get_optional(&client->dev, "vref");
-
-	if (IS_ERR(st->vref)) {
-		if (PTR_ERR(st->vref) == -ENODEV) {
-			st->vref = NULL;
-			dev_info(&client->dev, "Using VCC reference voltage\n");
-		} else {
-			ret = PTR_ERR(st->vref);
-			goto error_disable_reg;
+	if (chip_info->has_vref) {
+		st->vref = devm_regulator_get_optional(&client->dev, "vref");
+
+		if (IS_ERR(st->vref)) {
+			if (PTR_ERR(st->vref) == -ENODEV) {
+				st->vref = NULL;
+				dev_info(&client->dev, "Using VCC reference voltage\n");
+			} else {
+				ret = PTR_ERR(st->vref);
+				goto error_disable_reg;
+			}
 		}
-	}
 
-	if (st->vref) {
-		/*
-		 * Use external reference voltage if supported by hardware.
-		 * This is optional if voltage / regulator present, use VCC otherwise.
-		 */
-		if ((st->id == ad7991) || (st->id == ad7995) || (st->id == ad7999)) {
+		if (st->vref) {
 			dev_info(&client->dev, "Using external reference voltage\n");

drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client) · line 843

Quoted 10 lines
drivers/iio/adc/ad799x.c +1 -3
 			extra_config |= AD7991_REF_SEL;
 			ret = regulator_enable(st->vref);
 			if (ret)
 				goto error_disable_reg;
-		} else {
-			st->vref = NULL;
-			dev_warn(&client->dev, "Supplied reference not supported\n");
 		}
 	}
+	else {
	} else {
Quoted 6 lines
drivers/iio/adc/ad799x.c +3 -0
+		st->vref = NULL;
+		dev_dbg(&client->dev, "Supplied reference not supported\n");
+	}
 
 	st->client = client;
 
reviewer
#3 lore raw

Re: [PATCH 1/2] iio: adc: ad799x: add reference voltage capability to chip_info

Quoted 3 lines · Jonathan Cameron

On Wed, 6 Aug 2025 11:01:57 +0200
Stefano Manni <[email protected]> wrote:

...

Quoted 11 lines

drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client) · line 843

Quoted 1 line
drivers/iio/adc/ad799x.c +1 -0
+	else {
	} else {

drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client) · line 848

Quoted 6 lines
drivers/iio/adc/ad799x.c +3 -0
+		st->vref = NULL;
+		dev_dbg(&client->dev, "Supplied reference not supported\n");
+	}
 
 	st->client = client;

I would just completely drop the else. st->vref is already NULL since st is zeroed at allocation and the message is no longer useful. Previously, it warned that the devicetree contained a vref-supply when the chip didn't support it. But now no message is printed in that case and this message would be printed on all chips that don't have vref-supply in the devicetree when that case didn't print anything before.

#4 lore raw

Re: [PATCH 1/2] iio: adc: ad799x: add reference voltage capability to chip_info

In reply to Stefano Manni
Quoted 7 lines · Stefano Manni

If the chip supports an external reference voltage on REFIN pin then the "vref-supply" regulator may be used.

This commit partially refactors 6b104e7895ab16b9b7f466c5f2ca282b87f661e8 to add the capability of the chip to have an external voltage reference and then remove the ugly conditional check on chip id.

Suggested-by?

...

drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client)

Quoted 4 lines
drivers/iio/adc/ad799x.c +3 -0
+	if (chip_info->has_vref) {
+		st->vref = devm_regulator_get_optional(&client->dev, "vref");
 
+

Redundant blank line.

drivers/iio/adc/ad799x.c static int ad799x_probe(struct i2c_client *client) · line 833

Quoted 9 lines
drivers/iio/adc/ad799x.c +8 -0
+		if (IS_ERR(st->vref)) {
+			if (PTR_ERR(st->vref) == -ENODEV) {
+				st->vref = NULL;
+				dev_info(&client->dev, "Using VCC reference voltage\n");
+			} else {
+				ret = PTR_ERR(st->vref);
+				goto error_disable_reg;
+			}
 		}

Can be written as

		ret = PTR_ERR_OR_ZERO(st->vref);
		if (ret) {
			if (ret != -ENODEV)
				goto error_disable_reg;
			st->vref = NULL;
			dev_info(&client->dev, "Using VCC reference voltage\n");
		}
Quoted 1 line
drivers/iio/adc/ad799x.c +0 -1
-	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

PATCH 2/2 iio: adc: ad799x: add reference voltage to ad7994

In reply to Stefano Manni
---
 drivers/iio/adc/ad799x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ad799x.c +1 -1
index 955d845407b9..21e03f0df889 100644
@@ -694,7 +694,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 	[ad7994] = {
 		.num_channels = 5,
-		.has_vref = false,
+		.has_vref = true,
 		.noirq_config = {
 			.channel = {
 				AD799X_CHANNEL(0, 12),
Signature · 1 line
2.48.1
#6 lore raw

Re: [PATCH 2/2] iio: adc: ad799x: add reference voltage to ad7994

On Wed, Aug 06, 2025 at 11:01:58AM +0200, Stefano Manni wrote:

Missing commit message.

Quoted 1 line

...

drivers/iio/adc/ad799x.c static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { · line 697

Quoted 4 lines
drivers/iio/adc/ad799x.c +1 -1
 	[ad7994] = {
 		.num_channels = 5,
-		.has_vref = false,
+		.has_vref = true,

As Jonathan said and I implied the false is default. This patch should be just a oneliner with +1 line.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#7 lore raw
In reply to Stefano Manni
Quoted 8 lines · Stefano Manni

This patch series refactors 6b104e7895ab16b9b7f466c5f2ca282b87f661e8 in order to add the capability of the chip to have an external reference voltage into the chip_info struct.
And so avoid ugly conditional checks on the chip id.

In addition the AD7994 is marked to have the external reference voltage as well.

Quoted 7 lines

Stefano Manni (2):
iio: adc: ad799x: add reference voltage capability to chip_info
iio: adc: ad799x: add reference voltage to ad7994

 drivers/iio/adc/ad799x.c | 45 +++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 19 deletions(-)

Something to keep in mind if you send more patches: this should have been [PATCH v2] with a link to (implicit) v1 [1].

[1]: https://lore.kernel.org/linux-iio/[email protected]/

#8 lore raw
In reply to Stefano Manni
Quoted 7 lines · Stefano Manni

This patch series refactors 6b104e7895ab16b9b7f466c5f2ca282b87f661e8 in order to add the capability of the chip to have an external reference voltage into the chip_info struct.
And so avoid ugly conditional checks on the chip id.

In addition the AD7994 is marked to have the external reference voltage as well.

On top what David said, missed changelog.

Signature · 2 lines
With Best Regards,
Andy Shevchenko