Skip to contentSkip to conversation

iio: adc: ad799x: modernize resource management

PATCH Superseded Archit Anant 3 participants 9 messages Mar 8 – Mar 9

Subsystems (2)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover iio: adc: ad799x: modernize resource management

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(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v4 1/4 iio: adc: ad799x: make rx_buf static

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.

---
 drivers/iio/adc/ad799x.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/iio/adc/ad799x.c +3 -8
index 108bb22162ef..c1a7fa37706f 100644
@@ -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)
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH v4 1/4] iio: adc: ad799x: make rx_buf static

Quoted 6 lines · 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

Missing space after period.

Quoted 2 lines

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

Code wise LGTM.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH v4 2/4 iio: adc: ad799x: cache regulator voltage during probe

In reply to 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.

---
 drivers/iio/adc/ad799x.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/iio/adc/ad799x.c +13 -8
index c1a7fa37706f..28f1901698a6 100644
@@ -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;
 		}
 	}
 
Signature · 1 line
2.39.5
#4 lore raw

Re: [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe

Quoted 10 lines · 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.

...

drivers/iio/adc/ad799x.c static int ad799x_read_raw(struct iio_dev *indio_dev, · line 308

Quoted 1 line
drivers/iio/adc/ad799x.c +1 -0
+		*val = st->vref_uV / 1000;

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

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH v4 2/4] iio: adc: ad799x: cache regulator voltage during probe

In reply to Archit Anant
Quoted 11 lines · On Sun, 8 Mar 2026 23:58:09 +0530 Archit Anant <[email protected]> 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.

Quoted 3 lines

This is a little more complex. See below.

Quoted 35 lines
---
 drivers/iio/adc/ad799x.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/iio/adc/ad799x.c +4 -8
index c1a7fa37706f..28f1901698a6 100644
@@ -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;
 	}

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

Quoted 4 lines
drivers/iio/adc/ad799x.c +1 -0
@@ -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.

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

Quoted 12 lines
drivers/iio/adc/ad799x.c +5 -0
+	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.

Quoted 6 lines
drivers/iio/adc/ad799x.c +3 -0
+			if (ret < 0)
+				goto error_disable_vref;
+			st->vref_uV = ret;
 		}
 	}
 
#6 lore raw

PATCH v4 3/4 iio: adc: ad799x: use devm_add_action_or_reset for regulators

In reply to Archit Anant

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.

---
 drivers/iio/adc/ad799x.c | 46 ++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 21 deletions(-)
drivers/iio/adc/ad799x.c +25 -21
index 28f1901698a6..f5ef6ff97c89 100644
@@ -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)
Signature · 1 line
2.39.5
#7 lore raw

Re: [PATCH v4 3/4] iio: adc: ad799x: use devm_add_action_or_reset for regulators

Quoted 8 lines · Archit Anant

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

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

PATCH v4 4/4 iio: adc: ad799x: use devm_iio_device_register and drop remove

In reply to Archit Anant

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.

---
 drivers/iio/adc/ad799x.c | 27 +++++----------------------
 1 file changed, 5 insertions(+), 22 deletions(-)
drivers/iio/adc/ad799x.c +5 -22
index f5ef6ff97c89..89ac0d393366 100644
@@ -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);
Signature · 1 line
2.39.5