Re: [PATCH v6 5/7] iio: ssp_sensors: convert probe and teardown to devm-managed resources

From: Sanjay Chitroda

Date: Sat Apr 25 2026 - 07:05:19 EST




On 19 April 2026 11:09:18 pm IST, Jonathan Cameron <jic23@xxxxxxxxxx> wrote:
>On Wed, 15 Apr 2026 10:37:47 +0530
>Sanjay Chitroda <sanjayembeddedse@xxxxxxxxx> wrote:
>
>> 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>
>Hi Sanjay.
>
>Devm conversions often get complex and end up raising questions
>that weren't as obvious when doing an explicit remove() callback.
>
>One of those inline.
>
>Thanks
>
>Jonathan
>

Hi Jonathan,

Thanks for the review.
>> ---
>> 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@xxxxxxxxx/
>> ---
>>
>> 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);
>Where this is called below, it's not obvious to me what particular
>actions it is undoing. So I think you need comments on that somewhere.
>
>General alarm bell was that it was undoing multiple things.
>
>> +}
>> +
>> /**
>> * 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);
>
>See above. At minimum this needs some explanatory comments on why
>the stuff in there needs to be called from this point onwards.
>Before this patch it wasn't called if there was an error in the next
>few lines.
>
The intent of the devm action is to define the point at which SSP MCU can become active and start IRQ, watchdog, pending command completion.

As of now, this is handle explicit at remove path, but probe failures after MCU enable may leave the device partially active.

I will add comments explaining this part in next series.
>
>> + 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)
>