Skip to contentSkip to conversation

iio: adc: ad799x: convert to fully managed resources

PATCH In review Archit Anant 3 participants 3 messages Mar 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: adc: ad799x: convert to fully managed resources

Refactor the driver to use device-managed allocations and actions, allowing for the complete removal of the ad799x_remove() function and simplifying the error paths in probe().

Key changes:
- Replace the dynamic rx_buf allocation with a fixed-size, DMA-safe buffer using IIO_DECLARE_DMA_BUFFER_WITH_TS() in the state struct. This avoids memory leaks during scan mode updates and removes the need for manual kfree().
- Cache the VCC and VREF voltages in the state structure during probe() to avoid querying the regulator API during fast-path read_raw() calls.
- Use devm_add_action_or_reset() to register regulator_disable() callbacks for both VCC and VREF regulators.
- Convert iio_triggered_buffer_setup() and iio_device_register() to their devm_ variants, ensuring safe, reverse-order unwinding.

---
Changes in v2:
- Refactored entire resource management to eliminate remove() function based on feedback from David Lechner and Jonathan Cameron.
- Cached voltages to prevent mixing manual regulator disables with devm-managed device unregistration.

 drivers/iio/adc/ad799x.c | 83 +++++++++++++++++++---------------------
 1 file changed, 40 insertions(+), 43 deletions(-)
drivers/iio/adc/ad799x.c +40 -43
index 108bb22162ef..4a3db6f0a8d8 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,12 @@ struct ad799x_state {
 	unsigned int			id;
 	u16				config;
 
-	u8				*rx_buf;
 	unsigned int			transfer_size;
+
+	int				vcc_uv;
+	int				vref_uv;
+
+	IIO_DECLARE_DMA_BUFFER_WITH_TS(__be16, rx_buf, AD799X_MAX_CHANNELS);
 };
 
 static int ad799x_write_config(struct ad799x_state *st, u16 val)
@@ -217,11 +222,11 @@ 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;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
+	iio_push_to_buffers_with_timestamp(indio_dev, &st->rx_buf,
 			iio_get_time_ns(indio_dev));
 out:
 	iio_trigger_notify_done(indio_dev->trig);
@@ -234,11 +239,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;
 
@@ -307,9 +307,9 @@ static int ad799x_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
 		if (st->vref)
-			ret = regulator_get_voltage(st->vref);
+			ret = st->vref_uv;
 		else
-			ret = regulator_get_voltage(st->reg);
+			ret = st->vcc_uv;
 
 		if (ret < 0)
 			return ret;
@@ -781,6 +781,13 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 };
 
+static void ad799x_reg_disable(void *data)
+{
+	struct regulator *reg = data;
+
+	regulator_disable(reg);
+}
+
 static int ad799x_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
@@ -813,6 +820,14 @@ static int ad799x_probe(struct i2c_client *client)
 	ret = regulator_enable(st->reg);
 	if (ret)
 		return ret;
+	ret = devm_add_action_or_reset(&client->dev, ad799x_reg_disable, st->reg);
+	if (ret)
+		return ret;
+
+	ret = regulator_get_voltage(st->reg);
+	if (ret < 0)
+		return ret;
+	st->vcc_uv = ret;
 
 	/* check if an external reference is supplied */
 	if (chip_info->has_vref) {
@@ -820,7 +835,7 @@ static int ad799x_probe(struct i2c_client *client)
 		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");
 		}
@@ -830,7 +845,14 @@ static int ad799x_probe(struct i2c_client *client)
 			extra_config |= AD7991_REF_SEL;
 			ret = regulator_enable(st->vref);
 			if (ret)
-				goto error_disable_reg;
+				return ret;
+			ret = devm_add_action_or_reset(&client->dev, ad799x_reg_disable, st->vref);
+			if (ret)
+				return ret;
+			ret = regulator_get_voltage(st->vref);
+			if (ret < 0)
+				return ret;
+			st->vref_uv = ret;
 		}
 	}
 
@@ -845,12 +867,12 @@ 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,
+	ret = devm_iio_triggered_buffer_setup(&client->dev, 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,
@@ -862,40 +884,16 @@ 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_iio_device_register(&client->dev, indio_dev);
 	if (ret)
-		goto error_cleanup_ring;
+		return ret;
 
 	return 0;
-
-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;
-}
-
-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);
-	kfree(st->rx_buf);
 }
 
 static int ad799x_suspend(struct device *dev)
@@ -965,7 +963,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
#1 lore raw
Quoted 4 lines · Archit Anant

Refactor the driver to use device-managed allocations and actions, allowing for the complete removal of the ad799x_remove() function and simplifying the error paths in probe().

Quoted 11 lines

Key changes:
- Replace the dynamic rx_buf allocation with a fixed-size, DMA-safe buffer using IIO_DECLARE_DMA_BUFFER_WITH_TS() in the state struct. This avoids memory leaks during scan mode updates and removes the need for manual kfree().
- Cache the VCC and VREF voltages in the state structure during probe() to avoid querying the regulator API during fast-path read_raw() calls.
- Use devm_add_action_or_reset() to register regulator_disable() callbacks for both VCC and VREF regulators.
- Convert iio_triggered_buffer_setup() and iio_device_register() to their devm_ variants, ensuring safe, reverse-order unwinding.

Seems like a series squashed into a single patch, please resplit.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#2 lore raw
In reply to Archit Anant
Quoted 9 lines · Archit Anant

Refactor the driver to use device-managed allocations and actions, allowing for the complete removal of the ad799x_remove() function and simplifying the error paths in probe().

Key changes:
- Replace the dynamic rx_buf allocation with a fixed-size, DMA-safe buffer using IIO_DECLARE_DMA_BUFFER_WITH_TS() in the state struct. This avoids memory leaks during scan mode updates and removes the need for manual kfree().

I agree with Andy tht this first change should be split out to a separate patch.

Quoted 4 lines

- Cache the VCC and VREF voltages in the state structure during probe() to avoid querying the regulator API during fast-path read_raw() calls.
- Use devm_add_action_or_reset() to register regulator_disable() callbacks for both VCC and VREF regulators.

And regulator stuff as another patch.

Quoted 27 lines

- Convert iio_triggered_buffer_setup() and iio_device_register() to their devm_ variants, ensuring safe, reverse-order unwinding.

---
Changes in v2:
- Refactored entire resource management to eliminate remove() function based on feedback from David Lechner and Jonathan Cameron.
- Cached voltages to prevent mixing manual regulator disables with devm-managed device unregistration.

 drivers/iio/adc/ad799x.c | 83 +++++++++++++++++++---------------------
 1 file changed, 40 insertions(+), 43 deletions(-)
drivers/iio/adc/ad799x.c +1 -0
index 108bb22162ef..4a3db6f0a8d8 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

drivers/iio/adc/ad799x.c struct ad799x_state { · line 142

Quoted 10 lines
drivers/iio/adc/ad799x.c +5 -1
@@ -133,8 +134,12 @@ struct ad799x_state {
 	unsigned int			id;
 	u16				config;
 
-	u8				*rx_buf;
 	unsigned int			transfer_size;
+
+	int				vcc_uv;
+	int				vref_uv;
+
+	IIO_DECLARE_DMA_BUFFER_WITH_TS(__be16, rx_buf, AD799X_MAX_CHANNELS);

See v1 discussion. IIO_DECLARE_DMA_BUFFER_WITH_TS() is not needed after all since this is I2C. IIO_DECLARE_BUFFER_WITH_TS() can be used instead.

Quoted 31 lines
drivers/iio/adc/ad799x.c +2 -7
 };
 
 static int ad799x_write_config(struct ad799x_state *st, u16 val)
@@ -217,11 +222,11 @@ 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;
 
-	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
+	iio_push_to_buffers_with_timestamp(indio_dev, &st->rx_buf,
 			iio_get_time_ns(indio_dev));
 out:
 	iio_trigger_notify_done(indio_dev->trig);
@@ -234,11 +239,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;
 
@@ -307,9 +307,9 @@ static int ad799x_read_raw(struct iio_dev *indio_dev,
 		return IIO_VAL_INT;

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

Quoted 10 lines
drivers/iio/adc/ad799x.c +2 -2
 	case IIO_CHAN_INFO_SCALE:
 		if (st->vref)
-			ret = regulator_get_voltage(st->vref);
+			ret = st->vref_uv;
 		else
-			ret = regulator_get_voltage(st->reg);
+			ret = st->vcc_uv;
 
 		if (ret < 0)
 			return ret;

It should not be possible for ret < 0 now.

And we should be able to avoid assigning to ret and rather assign directly to *val now.

Quoted 72 lines
drivers/iio/adc/ad799x.c +27 -5
@@ -781,6 +781,13 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
 	},
 };
 
+static void ad799x_reg_disable(void *data)
+{
+	struct regulator *reg = data;
+
+	regulator_disable(reg);
+}
+
 static int ad799x_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
@@ -813,6 +820,14 @@ static int ad799x_probe(struct i2c_client *client)
 	ret = regulator_enable(st->reg);
 	if (ret)
 		return ret;
+	ret = devm_add_action_or_reset(&client->dev, ad799x_reg_disable, st->reg);
+	if (ret)
+		return ret;
+
+	ret = regulator_get_voltage(st->reg);
+	if (ret < 0)
+		return ret;
+	st->vcc_uv = ret;
 
 	/* check if an external reference is supplied */
 	if (chip_info->has_vref) {
@@ -820,7 +835,7 @@ static int ad799x_probe(struct i2c_client *client)
 		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");
 		}
@@ -830,7 +845,14 @@ static int ad799x_probe(struct i2c_client *client)
 			extra_config |= AD7991_REF_SEL;
 			ret = regulator_enable(st->vref);
 			if (ret)
-				goto error_disable_reg;
+				return ret;
+			ret = devm_add_action_or_reset(&client->dev, ad799x_reg_disable, st->vref);
+			if (ret)
+				return ret;
+			ret = regulator_get_voltage(st->vref);
+			if (ret < 0)
+				return ret;
+			st->vref_uv = ret;
 		}
 	}
 
@@ -845,12 +867,12 @@ 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,
+	ret = devm_iio_triggered_buffer_setup(&client->dev, 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,
@@ -862,40 +884,16 @@ static int ad799x_probe(struct i2c_client *client)
 						client->name,

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

Quoted 10 lines
drivers/iio/adc/ad799x.c +2 -2
 						indio_dev);
 		if (ret)
-			goto error_cleanup_ring;
+			return ret;
 	}
 
 	mutex_init(&st->lock);
 
-	ret = iio_device_register(indio_dev);
+	ret = devm_iio_device_register(&client->dev, indio_dev);

We can just return directly here now.

Quoted 40 lines
drivers/iio/adc/ad799x.c +1 -26
 	if (ret)
-		goto error_cleanup_ring;
+		return ret;
 
 	return 0;
-
-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;
-}
-
-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);
-	kfree(st->rx_buf);
 }
 
 static int ad799x_suspend(struct device *dev)
@@ -965,7 +963,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);