[PATCH v7 7/9] iio: ssp_sensors: convert probe and teardown to devm-managed resources

Sanjay Chitroda
2026-04-26T09:17:50Z

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 v7:
- Following input from Jonathan, added comment to describe the managed action
- Keep it single line for devm_add_..() API definition
- v6 change: https://lore.kernel.org/all/20260415050749.3858046-6-sanjayembedded@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
- v5 change: https://lore.kernel.org/all/20260406080852.2727453-5-sanjayembedded@gmail.com/
---
drivers/iio/common/ssp_sensors/ssp_dev.c | 117 +++++++++++++----------
1 file changed, 65 insertions(+), 52 deletions(-)

diff --git a/drivers/iio/common/ssp_sensors/ssp_dev.c b/drivers/iio/common/ssp_sensors/ssp_dev.c
index 29b17118ba2a..5415efc7bf48 100644
--- a/drivers/iio/common/ssp_sensors/ssp_dev.c
+++ b/drivers/iio/common/ssp_sensors/ssp_dev.c
@@ -204,6 +204,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
@@ -502,9 +522,9 @@ 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;
@@ -514,14 +534,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;
@@ -534,7 +556,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);
@@ -544,14 +568,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;
@@ -564,49 +597,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);
-
- dev_err(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_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)
@@ -662,7 +676,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