The Linux Kernel Mailing List
 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, kees@kernel.org,
	nabijaczleweli@nabijaczleweli.xyz, kyungmin.park@samsung.com,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v8 08/12] iio: ssp_sensors: convert probe and teardown to devm-managed resources
Date: Fri, 15 May 2026 23:10:13 +0530	[thread overview]
Message-ID: <20260515174017.3962168-9-sanjayembedded@gmail.com> (raw)
In-Reply-To: <20260515174017.3962168-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>
---
 drivers/iio/common/ssp_sensors/ssp_dev.c | 114 +++++++++++++----------
 1 file changed, 64 insertions(+), 50 deletions(-)

diff --git a/drivers/iio/common/ssp_sensors/ssp_dev.c b/drivers/iio/common/ssp_sensors/ssp_dev.c
index 88bb8ab0cd9b..441416c612a4 100644
--- a/drivers/iio/common/ssp_sensors/ssp_dev.c
+++ b/drivers/iio/common/ssp_sensors/ssp_dev.c
@@ -199,6 +199,26 @@ static void ssp_disable_wdt_timer(struct ssp_data *data)
 	cancel_work_sync(&data->work_wdt);
 }
 
+static void ssp_disable_work_timer(void *ptr)
+{
+	struct ssp_data *data = ptr;
+
+	cancel_delayed_work_sync(&data->work_refresh);
+	ssp_disable_wdt_timer(data);
+}
+
+static void ssp_shutdown_action(void *ptr)
+{
+	struct ssp_data *data = ptr;
+
+	if (ssp_command(data, SSP_MSG2SSP_AP_STATUS_SHUTDOWN, 0) < 0)
+		dev_err(&data->spi->dev,
+			"SSP_MSG2SSP_AP_STATUS_SHUTDOWN failed\n");
+
+	ssp_disable_mcu(data);
+	ssp_clean_pending_list(data);
+}
+
 /**
  * ssp_get_sensor_delay() - gets sensor data acquisition period
  * @data:	sensorhub structure
@@ -498,9 +518,8 @@ static int ssp_probe(struct spi_device *spi)
 		return -ENODEV;
 	}
 
-	ret = mfd_add_devices(dev, PLATFORM_DEVID_NONE,
-			      sensorhub_sensor_devs,
-			      ARRAY_SIZE(sensorhub_sensor_devs), NULL, 0, NULL);
+	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE, sensorhub_sensor_devs,
+				   ARRAY_SIZE(sensorhub_sensor_devs), NULL, 0, NULL);
 	if (ret < 0) {
 		dev_err(dev, "mfd add devices fail\n");
 		return ret;
@@ -510,14 +529,16 @@ static int ssp_probe(struct spi_device *spi)
 	ret = spi_setup(spi);
 	if (ret < 0) {
 		dev_err(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(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;
@@ -530,7 +551,9 @@ static int ssp_probe(struct spi_device *spi)
 
 	data->time_syncing = true;
 
-	mutex_init(&data->pending_lock);
+	ret = devm_mutex_init(dev, &data->pending_lock);
+	if (ret < 0)
+		return ret;
 	INIT_LIST_HEAD(&data->pending_list);
 
 	atomic_set(&data->enable_refcount, 0);
@@ -540,14 +563,23 @@ 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(dev, "Irq request fail\n");
-		goto err_setup_irq;
-	}
+	/*
+	 * Register deferred callback which disable timer and work after
+	 * module unloaded.
+	 *
+	 * driver should cancel delayed refresh work, watchdog work and delete
+	 * watchdog timer once interrupt is disabled and IRQ is freed.
+	 */
+	ret = devm_add_action_or_reset(dev, ssp_disable_work_timer, data);
+	if (ret < 0)
+		return ret;
+
+	ret = devm_request_threaded_irq(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;
@@ -560,47 +592,30 @@ static int ssp_probe(struct spi_device *spi)
 		ret = ssp_initialize_mcu(data);
 		if (ret < 0) {
 			dev_err(dev, "Initialize_mcu failed\n");
-			goto err_read_reg;
+			return ret;
 		}
 	} else {
 		dev_err(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(dev);
-
-	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_disable_mcu(data);
-	ssp_clean_pending_list(data);
-
-	free_irq(data->spi->irq, data);
-	cancel_delayed_work_sync(&data->work_refresh);
-
-	ssp_disable_wdt_timer(data);
-
-	mutex_destroy(&data->comm_lock);
-	mutex_destroy(&data->pending_lock);
+	/*
+	 * Managed shutdown action for the SSP device lifecycle.
+	 *
+	 * This action unwinds state by:
+	 *  - notifying the SSP firmware of AP shutdown,
+	 *  - disabling the MCU to prevent further IRQ activity,
+	 *  - cleaning up any pending command state.
+	 *
+	 * The action is registered once the MCU has been initialized so that
+	 * both driver removal and probe failure after this point leave the
+	 * device in a quiescent state.
+	 */
+	ret = devm_add_action_or_reset(dev, ssp_shutdown_action, data);
+	if (ret < 0)
+		return ret;
 
-	mfd_remove_devices(&spi->dev);
+	return 0;
 }
 
 static int ssp_suspend(struct device *dev)
@@ -656,7 +671,6 @@ static DEFINE_SIMPLE_DEV_PM_OPS(ssp_pm_ops, ssp_suspend, ssp_resume);
 
 static struct spi_driver ssp_driver = {
 	.probe = ssp_probe,
-	.remove = ssp_remove,
 	.driver = {
 		.pm = pm_sleep_ptr(&ssp_pm_ops),
 		.of_match_table = ssp_of_match,
-- 
2.34.1


  parent reply	other threads:[~2026-05-15 17:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15 17:40 [PATCH v8 00/12] iio: ssp_sensors: driver fixes, refactor and cleanup Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 01/12] iio: ssp_sensors: cancel delayed work_refresh on remove Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 02/12] iio: ssp_sensors: factor out pending list add/remove helpers Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 03/12] iio: ssp_sensors: refactor transfer logic into helper Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 04/12] iio: ssp_sensors: factor out MCU enable/disable helpers Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 05/12] iio: ssp_sensors: use local struct device Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 06/12] iio: ssp_sensors: fix variable type and declaration order in probe() Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 07/12] iio: ssp_sensors: Drop duplicated wdt timer and work cleanup Sanjay Chitroda
2026-05-15 17:40 ` Sanjay Chitroda [this message]
2026-05-15 17:40 ` [PATCH v8 09/12] iio: ssp_sensors: use guard() to release mutexes Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 10/12] iio: ssp_sensors: Use dev_err_probe() Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 11/12] iio: ssp_sensors: return errors directly from ssp_irq_msg() Sanjay Chitroda
2026-05-15 17:40 ` [PATCH v8 12/12] 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=20260515174017.3962168-9-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=kyungmin.park@samsung.com \
    --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