public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/4] iio: adc: ad799x: modernize resource management
@ 2026-03-08 18:28 Archit Anant
  2026-03-08 18:28 ` [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static Archit Anant
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Archit Anant @ 2026-03-08 18:28 UTC (permalink / raw)
  To: jic23, dlechner
  Cc: lars, Michael.Hennerich, nuno.sa, andy, linux-iio, linux-kernel,
	Archit Anant

This series modernizes the ad799x driver by converting all resource
management to the devm_ infrastructure. This results in cleaner 
probe error handling and the removal of the ad799x_remove() function.

Changes in v4:
- Simplified voltage caching to use a single 'vref_uV' variable as
suggested by David Lechner.
- Capitalized 'uV' in variable names as suggested by Andy Shevchenko.
- Removed redundant 'if (ret < 0)' check in read_raw() (dead check).
- Used devm_mutex_init() to replace manual mutex handling.
- Introduced a local 'dev' pointer in probe() to simplify function calls.
- Switched to IIO_DECLARE_BUFFER_WITH_TS() for the buffer declaration.
- Moved goto removals into the respective patches where devm conversion
occurs.

Changes in v3:
- Split the monolithic v2 patch into 4 logical commits as requested by
  Andy Shevchenko.

Changes in v2:
- Eliminated dynamic buffer allocation (feedback from David Lechner).
- Cached voltages to prevent devm/manual ordering hazards (feedback 
  from Jonathan Cameron).

Archit Anant (4):
  iio: adc: ad799x: make rx_buf static
  iio: adc: ad799x: cache regulator voltage during probe
  iio: adc: ad799x: use devm_add_action_or_reset for regulators
  iio: adc: ad799x: use devm_iio_device_register and drop remove

 drivers/iio/adc/ad799x.c | 101 +++++++++++++++++----------------------
 1 file changed, 44 insertions(+), 57 deletions(-)

-- 
2.39.5


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static
  2026-03-08 18:28 [PATCH v4 0/4] iio: adc: ad799x: modernize resource management Archit Anant
@ 2026-03-08 18:28 ` Archit Anant
  2026-03-08 21:10   ` Andy Shevchenko
  2026-03-08 18:28 ` [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe Archit Anant
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Archit Anant @ 2026-03-08 18:28 UTC (permalink / raw)
  To: jic23, dlechner
  Cc: lars, Michael.Hennerich, nuno.sa, andy, linux-iio, linux-kernel,
	Archit Anant

Currently, rx_buf is dynamically allocated using kmalloc() every time
ad799x_update_scan_mode() is called. This can lead to memory leaks if
the scan mask is updated multiple times.

Drop the dynamic allocation and replace it with a static, buffer
using IIO_DECLARE_BUFFER_WITH_TS().This eliminates the allocation
overhead, prevents leaks, and removes the need for manual kfree()
on driver removal.

Suggested-by: David Lechner <dlechner@baylibre.com>
Signed-off-by: Archit Anant <architanant5@gmail.com>
---
 drivers/iio/adc/ad799x.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index 108bb22162ef..c1a7fa37706f 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -39,6 +39,7 @@
 #include <linux/iio/triggered_buffer.h>
 
 #define AD799X_CHANNEL_SHIFT			4
+#define AD799X_MAX_CHANNELS			8
 
 /*
  * AD7991, AD7995 and AD7999 defines
@@ -133,8 +134,8 @@ struct ad799x_state {
 	unsigned int			id;
 	u16				config;
 
-	u8				*rx_buf;
 	unsigned int			transfer_size;
+	IIO_DECLARE_BUFFER_WITH_TS(__be16, rx_buf, AD799X_MAX_CHANNELS);
 };
 
 static int ad799x_write_config(struct ad799x_state *st, u16 val)
@@ -217,7 +218,7 @@ static irqreturn_t ad799x_trigger_handler(int irq, void *p)
 	}
 
 	b_sent = i2c_smbus_read_i2c_block_data(st->client,
-			cmd, st->transfer_size, st->rx_buf);
+			cmd, st->transfer_size, (u8 *)st->rx_buf);
 	if (b_sent < 0)
 		goto out;
 
@@ -234,11 +235,6 @@ static int ad799x_update_scan_mode(struct iio_dev *indio_dev,
 {
 	struct ad799x_state *st = iio_priv(indio_dev);
 
-	kfree(st->rx_buf);
-	st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
-	if (!st->rx_buf)
-		return -ENOMEM;
-
 	st->transfer_size = bitmap_weight(scan_mask,
 					  iio_get_masklength(indio_dev)) * 2;
 
@@ -895,7 +891,6 @@ static void ad799x_remove(struct i2c_client *client)
 	if (st->vref)
 		regulator_disable(st->vref);
 	regulator_disable(st->reg);
-	kfree(st->rx_buf);
 }
 
 static int ad799x_suspend(struct device *dev)
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe
  2026-03-08 18:28 [PATCH v4 0/4] iio: adc: ad799x: modernize resource management Archit Anant
  2026-03-08 18:28 ` [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static Archit Anant
@ 2026-03-08 18:28 ` Archit Anant
  2026-03-08 21:12   ` Andy Shevchenko
  2026-03-09 19:38   ` Jonathan Cameron
  2026-03-08 18:28 ` [PATCH v4 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators Archit Anant
  2026-03-08 18:28 ` [PATCH v4 4/4] iio: adc: ad799x: use devm_iio_device_register and drop remove Archit Anant
  3 siblings, 2 replies; 9+ messages in thread
From: Archit Anant @ 2026-03-08 18:28 UTC (permalink / raw)
  To: jic23, dlechner
  Cc: lars, Michael.Hennerich, nuno.sa, andy, linux-iio, linux-kernel,
	Archit Anant

Reading the regulator voltage via regulator_get_voltage() can be a slow
operation. Since the reference voltage for this ADC is not expected to
change at runtime, it is inefficient to query the regulator API every
time userspace reads the IIO_CHAN_INFO_SCALE attribute.

Determine the active reference voltage (either VREF or VCC) during
probe() and cache it in a single variable in the state structure.
This improves the performance of ad799x_read_raw(), removes the
dependency on the regulator pointers during fast-path reads, and
allows the removal of a redundant negative-value check.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Suggested-by: David Lechner <dlechner@baylibre.com>
Signed-off-by: Archit Anant <architanant5@gmail.com>
---
 drivers/iio/adc/ad799x.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index c1a7fa37706f..28f1901698a6 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -135,6 +135,9 @@ struct ad799x_state {
 	u16				config;
 
 	unsigned int			transfer_size;
+
+	int				vref_uV;
+
 	IIO_DECLARE_BUFFER_WITH_TS(__be16, rx_buf, AD799X_MAX_CHANNELS);
 };
 
@@ -302,14 +305,7 @@ static int ad799x_read_raw(struct iio_dev *indio_dev,
 			GENMASK(chan->scan_type.realbits - 1, 0);
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		if (st->vref)
-			ret = regulator_get_voltage(st->vref);
-		else
-			ret = regulator_get_voltage(st->reg);
-
-		if (ret < 0)
-			return ret;
-		*val = ret / 1000;
+		*val = st->vref_uV / 1000;
 		*val2 = chan->scan_type.realbits;
 		return IIO_VAL_FRACTIONAL_LOG2;
 	}
@@ -809,6 +805,10 @@ static int ad799x_probe(struct i2c_client *client)
 	ret = regulator_enable(st->reg);
 	if (ret)
 		return ret;
+	ret = regulator_get_voltage(st->reg);
+	if (ret < 0)
+		goto error_disable_reg;
+	st->vref_uV = ret;
 
 	/* check if an external reference is supplied */
 	if (chip_info->has_vref) {
@@ -827,6 +827,11 @@ static int ad799x_probe(struct i2c_client *client)
 			ret = regulator_enable(st->vref);
 			if (ret)
 				goto error_disable_reg;
+
+			ret = regulator_get_voltage(st->vref);
+			if (ret < 0)
+				goto error_disable_vref;
+			st->vref_uV = ret;
 		}
 	}
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v4 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators
  2026-03-08 18:28 [PATCH v4 0/4] iio: adc: ad799x: modernize resource management Archit Anant
  2026-03-08 18:28 ` [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static Archit Anant
  2026-03-08 18:28 ` [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe Archit Anant
@ 2026-03-08 18:28 ` Archit Anant
  2026-03-08 21:15   ` Andy Shevchenko
  2026-03-08 18:28 ` [PATCH v4 4/4] iio: adc: ad799x: use devm_iio_device_register and drop remove Archit Anant
  3 siblings, 1 reply; 9+ messages in thread
From: Archit Anant @ 2026-03-08 18:28 UTC (permalink / raw)
  To: jic23, dlechner
  Cc: lars, Michael.Hennerich, nuno.sa, andy, linux-iio, linux-kernel,
	Archit Anant, Andy Shevchenko

Convert the manual regulator_disable() handling to use
devm_add_action_or_reset(). This guarantees that the regulators are
disabled in the correct reverse order of allocation during device
teardown or probe failure.

As part of this conversion, introduce a local device pointer to
simplify function calls and remove redundant goto labels for regulator
cleanup, as the devm framework now handles these automatically.

Suggested-by: David Lechner <dlechner@baylibre.com>
Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Archit Anant <architanant5@gmail.com>
---
 drivers/iio/adc/ad799x.c | 46 ++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index 28f1901698a6..f5ef6ff97c89 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -773,8 +773,14 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 };
 
+static void ad799x_reg_disable(void *reg)
+{
+	regulator_disable(reg);
+}
+
 static int ad799x_probe(struct i2c_client *client)
 {
+	struct device *dev = &client->dev;
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 	int ret;
 	int extra_config = 0;
@@ -783,7 +789,7 @@ static int ad799x_probe(struct i2c_client *client)
 	const struct ad799x_chip_info *chip_info =
 		&ad799x_chip_info_tbl[id->driver_data];
 
-	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
 	if (indio_dev == NULL)
 		return -ENOMEM;
 
@@ -799,7 +805,7 @@ static int ad799x_probe(struct i2c_client *client)
 
 	/* TODO: Add pdata options for filtering and bit delay */
 
-	st->reg = devm_regulator_get(&client->dev, "vcc");
+	st->reg = devm_regulator_get(dev, "vcc");
 	if (IS_ERR(st->reg))
 		return PTR_ERR(st->reg);
 	ret = regulator_enable(st->reg);
@@ -807,31 +813,38 @@ static int ad799x_probe(struct i2c_client *client)
 		return ret;
 	ret = regulator_get_voltage(st->reg);
 	if (ret < 0)
-		goto error_disable_reg;
+		return ret;
 	st->vref_uV = ret;
 
+	ret = devm_add_action_or_reset(dev, ad799x_reg_disable, st->reg);
+	if (ret)
+		return ret;
 	/* check if an external reference is supplied */
 	if (chip_info->has_vref) {
-		st->vref = devm_regulator_get_optional(&client->dev, "vref");
+		st->vref = devm_regulator_get_optional(dev, "vref");
 		ret = PTR_ERR_OR_ZERO(st->vref);
 		if (ret) {
 			if (ret != -ENODEV)
-				goto error_disable_reg;
+				return ret;
 			st->vref = NULL;
-			dev_info(&client->dev, "Using VCC reference voltage\n");
+			dev_info(dev, "Using VCC reference voltage\n");
 		}
 
 		if (st->vref) {
-			dev_info(&client->dev, "Using external reference voltage\n");
+			dev_info(dev, "Using external reference voltage\n");
 			extra_config |= AD7991_REF_SEL;
 			ret = regulator_enable(st->vref);
 			if (ret)
-				goto error_disable_reg;
+				return ret;
 
 			ret = regulator_get_voltage(st->vref);
 			if (ret < 0)
-				goto error_disable_vref;
+				return ret;
 			st->vref_uV = ret;
+
+			ret = devm_add_action_or_reset(dev, ad799x_reg_disable, st->vref);
+			if (ret)
+				return ret;
 		}
 	}
 
@@ -846,15 +859,15 @@ static int ad799x_probe(struct i2c_client *client)
 
 	ret = ad799x_update_config(st, st->chip_config->default_config | extra_config);
 	if (ret)
-		goto error_disable_vref;
+		return ret;
 
 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
 		&ad799x_trigger_handler, NULL);
 	if (ret)
-		goto error_disable_vref;
+		return ret;
 
 	if (client->irq > 0) {
-		ret = devm_request_threaded_irq(&client->dev,
+		ret = devm_request_threaded_irq(dev,
 						client->irq,
 						NULL,
 						ad799x_event_handler,
@@ -876,11 +889,6 @@ static int ad799x_probe(struct i2c_client *client)
 
 error_cleanup_ring:
 	iio_triggered_buffer_cleanup(indio_dev);
-error_disable_vref:
-	if (st->vref)
-		regulator_disable(st->vref);
-error_disable_reg:
-	regulator_disable(st->reg);
 
 	return ret;
 }
@@ -888,14 +896,10 @@ static int ad799x_probe(struct i2c_client *client)
 static void ad799x_remove(struct i2c_client *client)
 {
 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
-	struct ad799x_state *st = iio_priv(indio_dev);
 
 	iio_device_unregister(indio_dev);
 
 	iio_triggered_buffer_cleanup(indio_dev);
-	if (st->vref)
-		regulator_disable(st->vref);
-	regulator_disable(st->reg);
 }
 
 static int ad799x_suspend(struct device *dev)
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v4 4/4] iio: adc: ad799x: use devm_iio_device_register and drop remove
  2026-03-08 18:28 [PATCH v4 0/4] iio: adc: ad799x: modernize resource management Archit Anant
                   ` (2 preceding siblings ...)
  2026-03-08 18:28 ` [PATCH v4 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators Archit Anant
@ 2026-03-08 18:28 ` Archit Anant
  3 siblings, 0 replies; 9+ messages in thread
From: Archit Anant @ 2026-03-08 18:28 UTC (permalink / raw)
  To: jic23, dlechner
  Cc: lars, Michael.Hennerich, nuno.sa, andy, linux-iio, linux-kernel,
	Archit Anant, Andy Shevchenko

Convert the driver to use the device-managed versions of
iio_device_register(), iio_triggered_buffer_setup(), and
devm_mutex_init().

By moving these remaining resources to the devm framework, the
driver no longer requires manual cleanup. This allows for the removal
of all remaining goto labels in ad799x_probe() and the complete
deletion of the ad799x_remove() function.

Suggested-by: David Lechner <dlechner@baylibre.com>
Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Archit Anant <architanant5@gmail.com>
---
 drivers/iio/adc/ad799x.c | 27 +++++----------------------
 1 file changed, 5 insertions(+), 22 deletions(-)

diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index f5ef6ff97c89..89ac0d393366 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -861,7 +861,7 @@ static int ad799x_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
-	ret = iio_triggered_buffer_setup(indio_dev, NULL,
+	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
 		&ad799x_trigger_handler, NULL);
 	if (ret)
 		return ret;
@@ -876,30 +876,14 @@ static int ad799x_probe(struct i2c_client *client)
 						client->name,
 						indio_dev);
 		if (ret)
-			goto error_cleanup_ring;
+			return ret;
 	}
 
-	mutex_init(&st->lock);
-
-	ret = iio_device_register(indio_dev);
+	ret = devm_mutex_init(dev, &st->lock);
 	if (ret)
-		goto error_cleanup_ring;
-
-	return 0;
-
-error_cleanup_ring:
-	iio_triggered_buffer_cleanup(indio_dev);
-
-	return ret;
-}
-
-static void ad799x_remove(struct i2c_client *client)
-{
-	struct iio_dev *indio_dev = i2c_get_clientdata(client);
-
-	iio_device_unregister(indio_dev);
+		return ret;
 
-	iio_triggered_buffer_cleanup(indio_dev);
+	return devm_iio_device_register(dev, indio_dev);
 }
 
 static int ad799x_suspend(struct device *dev)
@@ -969,7 +953,6 @@ static struct i2c_driver ad799x_driver = {
 		.pm = pm_sleep_ptr(&ad799x_pm_ops),
 	},
 	.probe = ad799x_probe,
-	.remove = ad799x_remove,
 	.id_table = ad799x_id,
 };
 module_i2c_driver(ad799x_driver);
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static
  2026-03-08 18:28 ` [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static Archit Anant
@ 2026-03-08 21:10   ` Andy Shevchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-03-08 21:10 UTC (permalink / raw)
  To: Archit Anant
  Cc: jic23, dlechner, lars, Michael.Hennerich, nuno.sa, andy,
	linux-iio, linux-kernel

On Sun, Mar 08, 2026 at 11:58:08PM +0530, Archit Anant wrote:
> Currently, rx_buf is dynamically allocated using kmalloc() every time
> ad799x_update_scan_mode() is called. This can lead to memory leaks if
> the scan mask is updated multiple times.
> 
> Drop the dynamic allocation and replace it with a static, buffer
> using IIO_DECLARE_BUFFER_WITH_TS().This eliminates the allocation

Missing space after period.

> overhead, prevents leaks, and removes the need for manual kfree()
> on driver removal.

Code wise LGTM.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe
  2026-03-08 18:28 ` [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe Archit Anant
@ 2026-03-08 21:12   ` Andy Shevchenko
  2026-03-09 19:38   ` Jonathan Cameron
  1 sibling, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-03-08 21:12 UTC (permalink / raw)
  To: Archit Anant
  Cc: jic23, dlechner, lars, Michael.Hennerich, nuno.sa, andy,
	linux-iio, linux-kernel

On Sun, Mar 08, 2026 at 11:58:09PM +0530, Archit Anant wrote:
> Reading the regulator voltage via regulator_get_voltage() can be a slow
> operation. Since the reference voltage for this ADC is not expected to
> change at runtime, it is inefficient to query the regulator API every
> time userspace reads the IIO_CHAN_INFO_SCALE attribute.
> 
> Determine the active reference voltage (either VREF or VCC) during
> probe() and cache it in a single variable in the state structure.
> This improves the performance of ad799x_read_raw(), removes the
> dependency on the regulator pointers during fast-path reads, and
> allows the removal of a redundant negative-value check.

...

> +		*val = st->vref_uV / 1000;

Perhaps "(MICRO / MILLI)" instead of "1000" until we have a dedicate constant?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators
  2026-03-08 18:28 ` [PATCH v4 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators Archit Anant
@ 2026-03-08 21:15   ` Andy Shevchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-03-08 21:15 UTC (permalink / raw)
  To: Archit Anant
  Cc: jic23, dlechner, lars, Michael.Hennerich, nuno.sa, andy,
	linux-iio, linux-kernel

On Sun, Mar 08, 2026 at 11:58:10PM +0530, Archit Anant wrote:
> Convert the manual regulator_disable() handling to use
> devm_add_action_or_reset(). This guarantees that the regulators are
> disabled in the correct reverse order of allocation during device
> teardown or probe failure.
> 
> As part of this conversion, introduce a local device pointer to
> simplify function calls and remove redundant goto labels for regulator
> cleanup, as the devm framework now handles these automatically.

Needs to be split to two:
- moving to managed resources (and here you introduce temporary variable to reduce the churn)
- simplifying the old code with that variable

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe
  2026-03-08 18:28 ` [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe Archit Anant
  2026-03-08 21:12   ` Andy Shevchenko
@ 2026-03-09 19:38   ` Jonathan Cameron
  1 sibling, 0 replies; 9+ messages in thread
From: Jonathan Cameron @ 2026-03-09 19:38 UTC (permalink / raw)
  To: Archit Anant
  Cc: dlechner, lars, Michael.Hennerich, nuno.sa, andy, linux-iio,
	linux-kernel

On Sun,  8 Mar 2026 23:58:09 +0530
Archit Anant <architanant5@gmail.com> wrote:

> Reading the regulator voltage via regulator_get_voltage() can be a slow
> operation. Since the reference voltage for this ADC is not expected to
> change at runtime, it is inefficient to query the regulator API every
> time userspace reads the IIO_CHAN_INFO_SCALE attribute.
> 
> Determine the active reference voltage (either VREF or VCC) during
> probe() and cache it in a single variable in the state structure.
> This improves the performance of ad799x_read_raw(), removes the
> dependency on the regulator pointers during fast-path reads, and
> allows the removal of a redundant negative-value check.
> 
> Suggested-by: Jonathan Cameron <jic23@kernel.org>
> Suggested-by: David Lechner <dlechner@baylibre.com>
> Signed-off-by: Archit Anant <architanant5@gmail.com>
This is a little more complex. See below.

> ---
>  drivers/iio/adc/ad799x.c | 21 +++++++++++++--------
>  1 file changed, 13 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
> index c1a7fa37706f..28f1901698a6 100644
> --- a/drivers/iio/adc/ad799x.c
> +++ b/drivers/iio/adc/ad799x.c
> @@ -135,6 +135,9 @@ struct ad799x_state {
>  	u16				config;
>  
>  	unsigned int			transfer_size;
> +
> +	int				vref_uV;
> +
>  	IIO_DECLARE_BUFFER_WITH_TS(__be16, rx_buf, AD799X_MAX_CHANNELS);
>  };
>  
> @@ -302,14 +305,7 @@ static int ad799x_read_raw(struct iio_dev *indio_dev,
>  			GENMASK(chan->scan_type.realbits - 1, 0);
>  		return IIO_VAL_INT;
>  	case IIO_CHAN_INFO_SCALE:
> -		if (st->vref)
> -			ret = regulator_get_voltage(st->vref);
> -		else
> -			ret = regulator_get_voltage(st->reg);
> -
> -		if (ret < 0)
> -			return ret;
> -		*val = ret / 1000;
> +		*val = st->vref_uV / 1000;
>  		*val2 = chan->scan_type.realbits;
>  		return IIO_VAL_FRACTIONAL_LOG2;
>  	}
> @@ -809,6 +805,10 @@ static int ad799x_probe(struct i2c_client *client)
>  	ret = regulator_enable(st->reg);
>  	if (ret)
>  		return ret;
> +	ret = regulator_get_voltage(st->reg);
This is broken for some cases.  If the vcc reg is a stub provided
by the regulator framework, or a regulator that we simply have no way
to read back the voltage from, then this will return an error.
Previously that was only a problem if we actually need this voltage.
i.e. we didn't have a vref.

So we need to read this voltage only if there is no vref.

> +	if (ret < 0)
> +		goto error_disable_reg;
> +	st->vref_uV = ret;
>  
>  	/* check if an external reference is supplied */
>  	if (chip_info->has_vref) {
> @@ -827,6 +827,11 @@ static int ad799x_probe(struct i2c_client *client)
>  			ret = regulator_enable(st->vref);
>  			if (ret)
>  				goto error_disable_reg;
> +
> +			ret = regulator_get_voltage(st->vref);

For this you can use
	devm_regulator_get_enable_read_voltage()
Read the comments for how that works with optional regulators.

> +			if (ret < 0)
> +				goto error_disable_vref;
> +			st->vref_uV = ret;
>  		}
>  	}
>  


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-03-09 19:38 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-08 18:28 [PATCH v4 0/4] iio: adc: ad799x: modernize resource management Archit Anant
2026-03-08 18:28 ` [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static Archit Anant
2026-03-08 21:10   ` Andy Shevchenko
2026-03-08 18:28 ` [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe Archit Anant
2026-03-08 21:12   ` Andy Shevchenko
2026-03-09 19:38   ` Jonathan Cameron
2026-03-08 18:28 ` [PATCH v4 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators Archit Anant
2026-03-08 21:15   ` Andy Shevchenko
2026-03-08 18:28 ` [PATCH v4 4/4] iio: adc: ad799x: use devm_iio_device_register and drop remove Archit Anant

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox