All of lore.kernel.org
 help / color / mirror / Atom feed
From: Abdelnasser Hussein <abdelnasserhussein11@gmail.com>
To: jic23@kernel.org, linux-iio@vger.kernel.org
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	nicolas.ferre@microchip.com, alexandre.belloni@bootlin.com,
	claudiu.beznea@tuxon.dev, giorgitchankvetadze1997@gmail.com,
	amin.gattout@gmail.com, antoniu.miclaus@analog.com,
	gregkh@linuxfoundation.org, mripard@kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	Abdelnasser Hussein <abdelnasserhussein11@gmail.com>
Subject: [PATCH v5 8/8] iio: adc: at91_adc: convert to device-managed resources
Date: Mon, 23 Nov 2026 09:01:01 +0200	[thread overview]
Message-ID: <20261123070101.53868-9-abdelnasserhussein11@gmail.com> (raw)
In-Reply-To: <20261123070101.53868-1-abdelnasserhussein11@gmail.com>

Convert the driver to device-managed APIs so that all resources are
freed automatically on probe failure and on driver detach:
  - allocate and register the IIO triggers directly in at91_adc_probe()
  with devm_iio_trigger_alloc() and devm_iio_trigger_register(),
  dropping at91_adc_allocate_trigger(), at91_adc_trigger_init() and
  at91_adc_trigger_remove() along with their manual rollback paths.
  The st->trig array is no longer needed and is removed as well, since
  the triggers are now owned by devres
  - set up the triggered buffer with devm_iio_triggered_buffer_setup()
  directly in at91_adc_probe(), dropping at91_adc_buffer_init() and
  at91_adc_buffer_remove()
  - allocate the touchscreen input device with
  devm_input_allocate_device(), dropping at91_ts_unregister() and the
  manual input_free_device() error path in at91_ts_register()
  - register the IIO device with devm_iio_device_register()

Redundant allocation error messages ("Failed to allocate TS device!" and
"Couldn't setup the triggers.") are dropped, and at91_ts_register() now
uses dev_err_probe() to report its remaining error.  Errors from
devm_iio_trigger_register() are reported with dev_err_probe() as well.

at91_adc_probe() no longer needs any error unwinding labels and
at91_adc_remove() is removed together with the platform driver's
.remove callback.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Abdelnasser Hussein <abdelnasserhussein11@gmail.com>
---
 drivers/iio/adc/at91_adc.c | 187 +++++++------------------------------
 1 file changed, 34 insertions(+), 153 deletions(-)

diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index 62f8db630640..5163583aa4f8 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -230,7 +230,6 @@ struct at91_adc_state {
 	u32			startup_time;
 	u8			sample_hold_time;
 	bool			sleep_mode;
-	struct iio_trigger	**trig;
 	bool			use_external;
 	u32			vref_mv;
 	u32			res;		/* resolution used for conversions */
@@ -607,96 +606,6 @@ static const struct iio_trigger_ops at91_adc_trigger_ops = {
 	.set_trigger_state = &at91_adc_configure_trigger,
 };
 
-static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *idev,
-						     const struct at91_adc_trigger *trigger)
-{
-	struct iio_trigger *trig;
-	int ret;
-
-	trig = iio_trigger_alloc(idev->dev.parent, "%s-dev%d-%s", idev->name,
-				 iio_device_id(idev), trigger->name);
-	if (trig == NULL)
-		return NULL;
-
-	iio_trigger_set_drvdata(trig, idev);
-	trig->ops = &at91_adc_trigger_ops;
-
-	ret = iio_trigger_register(trig);
-	if (ret) {
-		iio_trigger_free(trig);
-		return NULL;
-	}
-
-	return trig;
-}
-
-static int at91_adc_trigger_init(struct iio_dev *idev)
-{
-	struct at91_adc_state *st = iio_priv(idev);
-	int i, ret;
-
-	st->trig = devm_kcalloc(&idev->dev,
-				st->caps->trigger_number, sizeof(*st->trig),
-				GFP_KERNEL);
-
-	if (st->trig == NULL) {
-		ret = -ENOMEM;
-		goto error_ret;
-	}
-
-	for (i = 0; i < st->caps->trigger_number; i++) {
-		if (st->caps->triggers[i].is_external && !(st->use_external))
-			continue;
-
-		st->trig[i] = at91_adc_allocate_trigger(idev,
-							st->caps->triggers + i);
-		if (st->trig[i] == NULL) {
-			dev_err(&idev->dev,
-				"Could not allocate trigger %d\n", i);
-			ret = -ENOMEM;
-			goto error_trigger;
-		}
-	}
-
-	return 0;
-
-error_trigger:
-	for (i--; i >= 0; i--) {
-		if (!st->trig[i])
-			continue;
-
-		iio_trigger_unregister(st->trig[i]);
-		iio_trigger_free(st->trig[i]);
-	}
-error_ret:
-	return ret;
-}
-
-static void at91_adc_trigger_remove(struct iio_dev *idev)
-{
-	struct at91_adc_state *st = iio_priv(idev);
-	int i;
-
-	for (i = 0; i < st->caps->trigger_number; i++) {
-		if (!st->trig[i])
-			continue;
-
-		iio_trigger_unregister(st->trig[i]);
-		iio_trigger_free(st->trig[i]);
-	}
-}
-
-static int at91_adc_buffer_init(struct iio_dev *idev)
-{
-	return iio_triggered_buffer_setup(idev, &iio_pollfunc_store_time,
-		&at91_adc_trigger_handler, NULL);
-}
-
-static void at91_adc_buffer_remove(struct iio_dev *idev)
-{
-	iio_triggered_buffer_cleanup(idev);
-}
-
 static int at91_adc_read_raw(struct iio_dev *idev,
 			     struct iio_chan_spec const *chan,
 			     int *val, int *val2, long mask)
@@ -930,17 +839,13 @@ static int at91_ts_register(struct iio_dev *idev, struct device *dev)
 {
 	struct at91_adc_state *st = iio_priv(idev);
 	struct input_dev *input;
-	int ret;
 
-	input = input_allocate_device();
-	if (!input) {
-		dev_err(&idev->dev, "Failed to allocate TS device!\n");
+	input = devm_input_allocate_device(dev);
+	if (!input)
 		return -ENOMEM;
-	}
 
 	input->name = DRIVER_NAME;
 	input->id.bustype = BUS_HOST;
-	input->dev.parent = dev;
 	input->open = atmel_ts_open;
 	input->close = atmel_ts_close;
 
@@ -954,12 +859,9 @@ static int at91_ts_register(struct iio_dev *idev, struct device *dev)
 				     0, 0);
 		input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
 	} else {
-		if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE) {
-			dev_err(dev,
-				"This touchscreen controller only support 4 wires\n");
-			ret = -EINVAL;
-			goto err;
-		}
+		if (st->touchscreen_type != ATMEL_ADC_TOUCHSCREEN_4WIRE)
+			return dev_err_probe(dev, -EINVAL,
+					     "This touchscreen controller only support 4 wires\n");
 
 		input_set_abs_params(input, ABS_X, 0, (1 << MAX_RLPOS_BITS) - 1,
 				     0, 0);
@@ -970,20 +872,7 @@ static int at91_ts_register(struct iio_dev *idev, struct device *dev)
 	st->ts_input = input;
 	input_set_drvdata(input, st);
 
-	ret = input_register_device(input);
-	if (ret)
-		goto err;
-
-	return ret;
-
-err:
-	input_free_device(input);
-	return ret;
-}
-
-static void at91_ts_unregister(struct at91_adc_state *st)
-{
-	input_unregister_device(st->ts_input);
+	return input_register_device(input);
 }
 
 static int at91_adc_probe(struct platform_device *pdev)
@@ -1142,16 +1031,34 @@ static int at91_adc_probe(struct platform_device *pdev)
 	 * trigger for classic adc.
 	 */
 	if (!st->touchscreen_type) {
-		ret = at91_adc_buffer_init(idev);
-		if (ret < 0)
+		ret = devm_iio_triggered_buffer_setup(dev, idev,
+						      &iio_pollfunc_store_time,
+						      &at91_adc_trigger_handler,
+						      NULL);
+		if (ret)
 			return dev_err_probe(dev, ret,
 					     "Couldn't initialize the buffer.\n");
 
-		ret = at91_adc_trigger_init(idev);
-		if (ret < 0) {
-			dev_err(dev, "Couldn't setup the triggers.\n");
-			at91_adc_buffer_remove(idev);
-			return ret;
+		for (unsigned int i = 0; i < st->caps->trigger_number; i++) {
+			struct iio_trigger *trig;
+
+			if (st->caps->triggers[i].is_external && !st->use_external)
+				continue;
+
+			trig = devm_iio_trigger_alloc(dev, "%s-dev%d-%s",
+						      idev->name,
+						      iio_device_id(idev),
+						      st->caps->triggers[i].name);
+			if (!trig)
+				return -ENOMEM;
+
+			iio_trigger_set_drvdata(trig, idev);
+			trig->ops = &at91_adc_trigger_ops;
+
+			ret = devm_iio_trigger_register(dev, trig);
+			if (ret)
+				return dev_err_probe(dev, ret,
+						     "Couldn't register trigger %u.\n", i);
 		}
 	} else {
 		ret = at91_ts_register(idev, dev);
@@ -1161,36 +1068,11 @@ static int at91_adc_probe(struct platform_device *pdev)
 		at91_ts_hw_init(idev, adc_clk_khz);
 	}
 
-	ret = iio_device_register(idev);
-	if (ret < 0) {
-		dev_err(dev, "Couldn't register the device.\n");
-		goto error_iio_device_register;
-	}
+	ret = devm_iio_device_register(dev, idev);
+	if (ret)
+		return dev_err_probe(dev, ret, "Couldn't register the device.\n");
 
 	return 0;
-
-error_iio_device_register:
-	if (!st->touchscreen_type) {
-		at91_adc_trigger_remove(idev);
-		at91_adc_buffer_remove(idev);
-	} else {
-		at91_ts_unregister(st);
-	}
-	return ret;
-}
-
-static void at91_adc_remove(struct platform_device *pdev)
-{
-	struct iio_dev *idev = platform_get_drvdata(pdev);
-	struct at91_adc_state *st = iio_priv(idev);
-
-	iio_device_unregister(idev);
-	if (!st->touchscreen_type) {
-		at91_adc_trigger_remove(idev);
-		at91_adc_buffer_remove(idev);
-	} else {
-		at91_ts_unregister(st);
-	}
 }
 
 static int at91_adc_suspend(struct device *dev)
@@ -1340,7 +1222,6 @@ MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
 
 static struct platform_driver at91_adc_driver = {
 	.probe = at91_adc_probe,
-	.remove = at91_adc_remove,
 	.driver = {
 		   .name = DRIVER_NAME,
 		   .of_match_table = at91_adc_dt_ids,
-- 
2.56.0


  parent reply	other threads:[~2026-11-23  7:02 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  7:00 [PATCH v5 0/8] iio: adc: at91_adc: fix NULL trigger dereference and convert to devm Abdelnasser Hussein
2026-11-23  7:00 ` [PATCH v5 1/8] iio: adc: at91_adc: skip unpopulated triggers on teardown Abdelnasser Hussein
2026-11-23  7:53   ` Joshua Crofts
2026-11-23  7:00 ` [PATCH v5 2/8] iio: adc: at91_adc: drop duplicate IRQ allocation error message Abdelnasser Hussein
2026-11-23  7:55   ` Joshua Crofts
2026-11-23  7:00 ` [PATCH v5 3/8] iio: adc: at91_adc: switch to device_get_match_data() Abdelnasser Hussein
2026-11-23  7:52   ` Joshua Crofts
2026-11-23  7:00 ` [PATCH v5 4/8] iio: adc: at91_adc: convert to device_property_*() APIs Abdelnasser Hussein
2026-11-23  8:02   ` Joshua Crofts
2026-11-23  8:58   ` Uwe Kleine-König
2026-11-23 10:02   ` Andy Shevchenko
2026-11-23  7:00 ` [PATCH v5 5/8] iio: adc: at91_adc: use local struct device pointer in probe Abdelnasser Hussein
2026-11-23  8:02   ` Joshua Crofts
2026-11-23 10:03   ` Andy Shevchenko
2026-11-23  7:00 ` [PATCH v5 6/8] iio: adc: at91_adc: pass struct device to at91_ts_register() Abdelnasser Hussein
2026-11-23  8:03   ` Joshua Crofts
2026-11-23 10:04   ` Andy Shevchenko
2026-11-23  7:01 ` [PATCH v5 7/8] iio: adc: at91_adc: use HZ_PER_KHZ instead of raw 1000 Abdelnasser Hussein
2026-11-23  8:04   ` Joshua Crofts
2026-11-23  7:01 ` Abdelnasser Hussein [this message]
2026-11-28 23:34   ` [PATCH v5 8/8] iio: adc: at91_adc: convert to device-managed resources Jonathan Cameron
2026-11-28 23:26 ` [PATCH v5 0/8] iio: adc: at91_adc: fix NULL trigger dereference and convert to devm Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123070101.53868-9-abdelnasserhussein11@gmail.com \
    --to=abdelnasserhussein11@gmail.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=amin.gattout@gmail.com \
    --cc=andy@kernel.org \
    --cc=antoniu.miclaus@analog.com \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=dlechner@baylibre.com \
    --cc=giorgitchankvetadze1997@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mripard@kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=nuno.sa@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.