[PATCH v8 08/12] iio: ssp_sensors: convert probe and teardown to devm-managed resources
From: Sanjay Chitroda
Date: Fri May 15 2026 - 14:19:58 EST
From: Sanjay Chitroda <sanjayembeddedse@xxxxxxxxx>
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@xxxxxxxxx>
---
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