public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sanjay Chitroda <sanjayembeddedse@gmail.com>
To: jic23@kernel.org
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	sanjayembeddedse@gmail.com, tglx@kernel.org,
	christophe.jaillet@wanadoo.fr, mingo@kernel.org,
	nabijaczleweli@nabijaczleweli.xyz, kees@kernel.org,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v6 5/7] iio: ssp_sensors: convert probe and teardown to devm-managed resources
Date: Wed, 15 Apr 2026 10:37:47 +0530	[thread overview]
Message-ID: <20260415050749.3858046-6-sanjayembedded@gmail.com> (raw)
In-Reply-To: <20260415050749.3858046-1-sanjayembedded@gmail.com>

From: Sanjay Chitroda <sanjayembeddedse@gmail.com>

Convert SSP driver resource management to use devm-managed helpers
and devm actions, tying the lifetime of all resources to the device.

Mutex initialization, IRQ registration, work items, timers, and MFD
resource are now managed using devm APIs. Cleanup logic previously
handled explicitly in probe error paths and the remove callback is
replaced with devm_add_action_or_reset(), ensuring correct teardown
ordering and consistent behaviour on probe failure and device unbind.

This simplifies the probe path by removing goto-based error handling,
eliminates the remove callback entirely.

No functional change intended.

Signed-off-by: Sanjay Chitroda <sanjayembeddedse@gmail.com>
---
Changes in v6:
- Drop remove path completely using devm_action with review comment from David
- Convert MFD device to manage resource along with mutex and IRQ
- Link to v5: https://lore.kernel.org/all/20260406080852.2727453-1-sanjayembedded@gmail.com/
---

 drivers/iio/common/ssp_sensors/ssp_dev.c | 88 ++++++++++--------------
 1 file changed, 38 insertions(+), 50 deletions(-)

diff --git a/drivers/iio/common/ssp_sensors/ssp_dev.c b/drivers/iio/common/ssp_sensors/ssp_dev.c
index cb4c26a6b76c..94ecc794f926 100644
--- a/drivers/iio/common/ssp_sensors/ssp_dev.c
+++ b/drivers/iio/common/ssp_sensors/ssp_dev.c
@@ -194,6 +194,16 @@ static void ssp_disable_wdt_timer(struct ssp_data *data)
 	cancel_work_sync(&data->work_wdt);
 }
 
+static void ssp_shutdown_action(struct ssp_data *data)
+{
+	if (ssp_command(data, SSP_MSG2SSP_AP_STATUS_SHUTDOWN, 0) < 0)
+		dev_err(&data->spi->dev,
+			"SSP_MSG2SSP_AP_STATUS_SHUTDOWN failed\n");
+
+	ssp_enable_mcu(data, false);
+	ssp_clean_pending_list(data);
+}
+
 /**
  * ssp_get_sensor_delay() - gets sensor data acquisition period
  * @data:	sensorhub structure
@@ -491,9 +501,9 @@ static int ssp_probe(struct spi_device *spi)
 		return -ENODEV;
 	}
 
-	ret = mfd_add_devices(&spi->dev, PLATFORM_DEVID_NONE,
-			      sensorhub_sensor_devs,
-			      ARRAY_SIZE(sensorhub_sensor_devs), NULL, 0, NULL);
+	ret = devm_mfd_add_devices(&spi->dev, PLATFORM_DEVID_NONE,
+				   sensorhub_sensor_devs,
+				   ARRAY_SIZE(sensorhub_sensor_devs), NULL, 0, NULL);
 	if (ret < 0) {
 		dev_err(&spi->dev, "mfd add devices fail\n");
 		return ret;
@@ -503,14 +513,16 @@ static int ssp_probe(struct spi_device *spi)
 	ret = spi_setup(spi);
 	if (ret < 0) {
 		dev_err(&spi->dev, "Failed to setup spi\n");
-		goto err_setup_spi;
+		return ret;
 	}
 
 	data->fw_dl_state = SSP_FW_DL_STATE_NONE;
 	data->spi = spi;
 	spi_set_drvdata(spi, data);
 
-	mutex_init(&data->comm_lock);
+	ret = devm_mutex_init(&spi->dev, &data->comm_lock);
+	if (ret < 0)
+		return ret;
 
 	for (i = 0; i < SSP_SENSOR_MAX; ++i) {
 		data->delay_buf[i] = SSP_DEFAULT_POLLING_DELAY;
@@ -523,7 +535,9 @@ static int ssp_probe(struct spi_device *spi)
 
 	data->time_syncing = true;
 
-	mutex_init(&data->pending_lock);
+	ret = devm_mutex_init(&spi->dev, &data->pending_lock);
+	if (ret < 0)
+		return ret;
 	INIT_LIST_HEAD(&data->pending_list);
 
 	atomic_set(&data->enable_refcount, 0);
@@ -533,14 +547,17 @@ static int ssp_probe(struct spi_device *spi)
 
 	timer_setup(&data->wdt_timer, ssp_wdt_timer_func, 0);
 
-	ret = request_threaded_irq(data->spi->irq, NULL,
-				   ssp_irq_thread_fn,
-				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
-				   "SSP_Int", data);
-	if (ret < 0) {
-		dev_err(&spi->dev, "Irq request fail\n");
-		goto err_setup_irq;
-	}
+	ret = devm_add_action_or_reset(&spi->dev,
+				       ssp_disable_wdt_timer, data);
+	if (ret)
+		return ret;
+
+	ret = devm_request_threaded_irq(&spi->dev, data->spi->irq, NULL,
+					ssp_irq_thread_fn,
+					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
+					"SSP_Int", data);
+	if (ret < 0)
+		return ret;
 
 	/* Let's start with enabled one so irq balance could be ok */
 	data->shut_down = false;
@@ -548,53 +565,24 @@ static int ssp_probe(struct spi_device *spi)
 	/* just to avoid unbalanced irq set wake up */
 	enable_irq_wake(data->spi->irq);
 
+	ret = devm_add_action_or_reset(&spi->dev,
+				       ssp_shutdown_action, data);
+	if (ret)
+		return ret;
+
 	data->fw_dl_state = ssp_check_fwbl(data);
 	if (data->fw_dl_state == SSP_FW_DL_STATE_NONE) {
 		ret = ssp_initialize_mcu(data);
 		if (ret < 0) {
 			dev_err(&spi->dev, "Initialize_mcu failed\n");
-			goto err_read_reg;
+			return ret;
 		}
 	} else {
 		dev_err(&spi->dev, "Firmware version not supported\n");
-		ret = -EPERM;
-		goto err_read_reg;
+		return -EPERM;
 	}
 
 	return 0;
-
-err_read_reg:
-	free_irq(data->spi->irq, data);
-err_setup_irq:
-	mutex_destroy(&data->pending_lock);
-	mutex_destroy(&data->comm_lock);
-err_setup_spi:
-	mfd_remove_devices(&spi->dev);
-
-	dev_err(&spi->dev, "Probe failed!\n");
-
-	return ret;
-}
-
-static void ssp_remove(struct spi_device *spi)
-{
-	struct ssp_data *data = spi_get_drvdata(spi);
-
-	if (ssp_command(data, SSP_MSG2SSP_AP_STATUS_SHUTDOWN, 0) < 0)
-		dev_err(&data->spi->dev,
-			"SSP_MSG2SSP_AP_STATUS_SHUTDOWN failed\n");
-
-	ssp_enable_mcu(data, false);
-	ssp_clean_pending_list(data);
-
-	free_irq(data->spi->irq, data);
-
-	ssp_disable_wdt_timer(data);
-
-	mutex_destroy(&data->comm_lock);
-	mutex_destroy(&data->pending_lock);
-
-	mfd_remove_devices(&spi->dev);
 }
 
 static int ssp_suspend(struct device *dev)
-- 
2.34.1


  parent reply	other threads:[~2026-04-15  5:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-15  5:07 [PATCH v6 0/7] iio: ssp_sensors: improve resource cleanup Sanjay Chitroda
2026-04-15  5:07 ` [PATCH v6 1/7] iio: ssp_sensors: cleanup codestyle warning Sanjay Chitroda
2026-04-15  5:07 ` [PATCH v6 2/7] iio: ssp_sensors: cleanup codestyle check Sanjay Chitroda
2026-04-15  9:14   ` Andy Shevchenko
2026-04-15  5:07 ` [PATCH v6 3/7] iio: ssp_sensors: factor out pending list add/remove helper(s) Sanjay Chitroda
2026-04-15  9:22   ` Andy Shevchenko
2026-04-15  9:25   ` Andy Shevchenko
2026-04-15  5:07 ` [PATCH v6 4/7] iio: ssp_sensors: drop duplicated wdt timer and work cleanup Sanjay Chitroda
2026-04-15  9:29   ` Andy Shevchenko
2026-04-15  5:07 ` Sanjay Chitroda [this message]
2026-04-15  9:33   ` [PATCH v6 5/7] iio: ssp_sensors: convert probe and teardown to devm-managed resources Andy Shevchenko
2026-04-15  5:07 ` [PATCH v6 6/7] iio: ssp_sensors: Use dev_err_probe Sanjay Chitroda
2026-04-15  9:39   ` Andy Shevchenko
2026-04-15  5:07 ` [PATCH v6 7/7] iio: ssp_sensors: reuse embedded RX buffer for SPI transfers Sanjay Chitroda

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=20260415050749.3858046-6-sanjayembedded@gmail.com \
    --to=sanjayembeddedse@gmail.com \
    --cc=andy@kernel.org \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=nabijaczleweli@nabijaczleweli.xyz \
    --cc=nuno.sa@analog.com \
    --cc=tglx@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox