iio: ssp_sensors: improve resource cleanup
v6 cover iio: ssp_sensors: improve resource cleanup
From: Sanjay Chitroda <[email protected]>
Hi all,
This patch series improves resource cleanup and error handling in the SSP IIO SPI driver by adopting the recently introduced cleanup helpers.
The changes focus on making probe/remove paths more robust and easier to reason about by reducing manual unwind logic and ensuring that locks and dynamically allocated resources are released consistently across all exit paths.
Key highlights of this series:
- Reuse embedded rx buffer for SPI transfer instead of allocating new DMA memory in interrupt context for write transaction between AP <-> HUB.
- Replace dynamic resource lifetime management with device‑managed (devm_) APIs to simplify cleanup and error paths.
- Refactor common helper API, improving readability and reducing duplicatation.
- Address minor codestyle warnings and use dev_err_probe in probe path.
Changes in v6:
- 0005: Drop remove() path using devm action with input from David
- 0007: Replace dynamic memory allocation with embedded fixed size struct buffer
- 0004: new: drop duplication in remove()
- 0006: new: Use dev_err_probe in driver probe()
Changes in v5:
- Drop usage of guard() helpers to avoid mixing mutex_lock() with guard()(), based on reviewer feedback.
- 0003: Refactor shared helper API, reducing duplication.
- 0004: Convert resource allocation and teardown to devm_ managed APIs to simplify error handling and probe/remove paths.
Changes in v4:
- Update sequence and make checkpatch style fix in base change
- Split WARNING and CHECK change with input of Andy as 0001 and 0002
- 0003: Use guard() instead of scoped_guard()
- 0004: Use preallocated buffer to stash memory allocation
No functional behavior changes are intended.
Testing:
- Compiled with W=1
- Build-tested on QEMU x86_64
Based on:
<linux-v7.0-rc5>
Feedback and reviews are very welcome.
Thanks,
Sanjay Chitroda
Sanjay Chitroda (7):
iio: ssp_sensors: cleanup codestyle warning
iio: ssp_sensors: cleanup codestyle check
iio: ssp_sensors: factor out pending list add/remove helper(s)
iio: ssp_sensors: drop duplicated wdt timer and work cleanup
iio: ssp_sensors: convert probe and teardown to devm-managed resources
iio: ssp_sensors: Use dev_err_probe
iio: ssp_sensors: reuse embedded RX buffer for SPI transfers
drivers/iio/common/ssp_sensors/ssp.h | 3 + drivers/iio/common/ssp_sensors/ssp_dev.c | 119 ++++++++++------------- drivers/iio/common/ssp_sensors/ssp_spi.c | 83 +++++++--------- 3 files changed, 88 insertions(+), 117 deletions(-)
Signature · 1 line
2.34.1
PATCH v6 1/7 iio: ssp_sensors: cleanup codestyle warning
From: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE: drivers/iio/common/ssp_sensors/ssp_spi.c
WARNING: Prefer __packed over __attribute__((__packed__)) +} __attribute__((__packed__));
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +1 -1
@@ -29,7 +29,7 @@ struct ssp_msg_header {__le16 length;__le16 options;__le32 data; -} __attribute__((__packed__)); +} __packed;struct ssp_msg {u16 length;
Signature · 1 line
2.34.1
PATCH v6 2/7 iio: ssp_sensors: cleanup codestyle check
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE: drivers/iio/common/ssp_sensors/ssp_spi.c
CHECK: Macro argument '...' may be better as '(...)' to avoid precedence issues
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +3 -3
@@ -6,7 +6,7 @@#include "ssp.h" #define SSP_DEV (&data->spi->dev) -#define SSP_GET_MESSAGE_TYPE (data) (data & (3 << SSP_RW)) +#define SSP_GET_MESSAGE_TYPE (data) ((data) & (3 << SSP_RW))/* * SSP -> AP Instruction
@@ -119,9 +119,9 @@ static inline void ssp_get_buffer(struct ssp_msg *m, unsigned int offset, }#define SSP_GET_BUFFER_AT_INDEX (m, index) \ - (m->buffer[SSP_HEADER_SIZE_ALIGNED + index]) + ((m)->buffer[SSP_HEADER_SIZE_ALIGNED + (index)])#define SSP_SET_BUFFER_AT_INDEX (m, index, val) \ - (m->buffer[SSP_HEADER_SIZE_ALIGNED + index] = val) + ((m)->buffer[SSP_HEADER_SIZE_ALIGNED + (index)] = val)static void ssp_clean_msg (struct ssp_msg *m) {
Signature · 1 line
2.34.1
PATCH v6 3/7 iio: ssp_sensors: factor out pending list add/remove helper(s)
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
The SSP SPI transfer path manipulates the pending message list in multiple places, each time open-coding the same locking and list operations.
Re-factor the pending list add and delete logic into small helper functions and drop use_no_irq variable to avoid duplication and simplify transfer flow to follow.
No functional change intended.
---
Changes in v6:
- Include tag for the suggestion of helper functions
- Drop completely use_no_irq variable with review comment from Andy
- Link to v5: https://lore.kernel.org/all/[email protected]/
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 55 +++++++++++++----------- 1 file changed, 30 insertions(+), 25 deletions(-)
drivers/iio/common/ssp_sensors/ssp_spi.c +30 -25
@@ -174,15 +174,32 @@ static int ssp_check_lines(struct ssp_data *data, bool state)return 0 ; } +static inline void ssp_pending_add (struct ssp_data *data, +struct ssp_msg *msg) +{ +if (msg->length ==0 ) +return ; + +mutex_lock (&data->pending_lock); +list_add_tail (&msg->list, &data->pending_list); +mutex_unlock (&data->pending_lock); +} + +static inline void ssp_pending_del (struct ssp_data *data, +struct ssp_msg *msg) +{ +if (msg->length ==0 ) +return ; + +mutex_lock (&data->pending_lock); +list_del (&msg->list); +mutex_unlock (&data->pending_lock); +} +static int ssp_do_transfer (struct ssp_data *data,struct ssp_msg *msg,struct completion *done,int timeout) {int status; -/* -* check if this is a short one way message or the whole transfer has -* second part after an interrupt -*/ -const bool use_no_irq = msg->length ==0 ;if (data->shut_down)return -EPERM ;
@@ -202,35 +219,23 @@ static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg, goto _error_locked; } -if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_add_tail (&msg->list, &data->pending_list); -mutex_unlock (&data->pending_lock); - } +ssp_pending_add (data, msg); status =ssp_check_lines (data, true);if (status <0 ) { -if (!use_no_irq) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); - } +ssp_pending_del (data, msg); goto _error_locked; }mutex_unlock (&data->comm_lock); -if (!use_no_irq && done) -if (wait_for_completion_timeout (done, -msecs_to_jiffies (timeout)) == -0 ) { -mutex_lock (&data->pending_lock); -list_del (&msg->list); -mutex_unlock (&data->pending_lock); +if (msg->length && done && + !wait_for_completion_timeout (done,msecs_to_jiffies (timeout))) { +ssp_pending_del (data, msg); - data->timeout_cnt++; -return -ETIMEDOUT ; - } + data->timeout_cnt++; +return -ETIMEDOUT ; + }return 0 ;
Signature · 1 line
2.34.1
PATCH v6 4/7 iio: ssp_sensors: drop duplicated wdt timer and work cleanup
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
The SSP remove path cleans up the watchdog timer and associated work both via ssp_disable_wdt_timer() and through explicit timer and work teardown.
Remove the duplicated timer_delete_sync() and cancel_work_sync() calls and rely on ssp_disable_wdt_timer() as the single cleanup helper to simplify the remove path.
No functional change intended.
--- drivers/iio/common/ssp_sensors/ssp_dev.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/iio/common/ssp_sensors/ssp_dev.c +1 -4
@@ -585,14 +585,11 @@ static void ssp_remove(struct spi_device *spi) "SSP_MSG2SSP_AP_STATUS_SHUTDOWN failed\n");ssp_enable_mcu (data, false); -ssp_disable_wdt_timer (data); -ssp_clean_pending_list (data);free_irq (data->spi->irq, data); -timer_delete_sync (&data->wdt_timer); -cancel_work_sync (&data->work_wdt); +ssp_disable_wdt_timer (data);mutex_destroy (&data->comm_lock);mutex_destroy (&data->pending_lock);
Signature · 1 line
2.34.1
PATCH v6 5/7 iio: ssp_sensors: convert probe and teardown to devm-managed resources
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
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.
---
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/[email protected]/
---
drivers/iio/common/ssp_sensors/ssp_dev.c | 88 ++++++++++-------------- 1 file changed, 38 insertions(+), 50 deletions(-)
drivers/iio/common/ssp_sensors/ssp_dev.c +38 -50
@@ -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)
Signature · 1 line
2.34.1
PATCH v6 6/7 iio: ssp_sensors: Use dev_err_probe
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
dev_err_probe() makes error code handling simpler and handle deferred probe nicely (avoid spamming logs).
--- drivers/iio/common/ssp_sensors/ssp_dev.c | 36 ++++++++++-------------- 1 file changed, 15 insertions(+), 21 deletions(-)
drivers/iio/common/ssp_sensors/ssp_dev.c +15 -21
@@ -496,25 +496,21 @@ static int ssp_probe(struct spi_device *spi)struct ssp_data *data; data =ssp_parse_dt (&spi->dev); -if (!data) { -dev_err (&spi->dev,"Failed to find platform data\n" ); -return -ENODEV ; - } +if (!data) +return dev_err_probe (&spi->dev, -ENODEV , +"Failed to find platform data\n" ); 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; - } +ARRAY_SIZE (sensorhub_sensor_devs), +NULL ,0 ,NULL ); +if (ret <0 ) +return dev_err_probe (&spi->dev, ret,"mfd add devices fail\n" ); spi->mode =SPI_MODE_1 ; ret =spi_setup (spi); -if (ret <0 ) { -dev_err (&spi->dev,"Failed to setup spi\n" ); -return ret; - } +if (ret <0 ) +return dev_err_probe (&spi->dev, ret,"Failed to setup spi\n" ); data->fw_dl_state =SSP_FW_DL_STATE_NONE ; data->spi = spi;
@@ -573,14 +569,12 @@ static int ssp_probe(struct spi_device *spi) 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" ); -return ret; - } - }else { -dev_err (&spi->dev,"Firmware version not supported\n" ); -return -EPERM ; - } +if (ret <0 ) +return dev_err_probe (&spi->dev, ret, +"Initialize_mcu failed\n" ); + }else +return dev_err_probe (&spi->dev, -EPERM , +"Firmware version not supported\n" );return 0 ; }
Signature · 1 line
2.34.1
PATCH v6 7/7 iio: ssp_sensors: reuse embedded RX buffer for SPI transfers
In reply to Sanjay ChitrodaFrom: Sanjay Chitroda <[email protected]>
Avoid allocating a temporary DMA buffer in the interrupt context when handling hub-to-AP and AP-to-hub SPI write messages.
Replace the dynamically allocated RX buffer with a fixed-size, preallocated buffer embedded in the driver structure and reused for all SPI receive operations. This removes memory allocation from the IRQ path, simplifies lifetime management.
No functional change intended.
---
Changes in v6:
- Replace dynamically allocated RX buffer with embedded fixed-size buffer
- Fix struct layout to satisfy DMA alignment constraints comment from David
- Link to v5: https://lore.kernel.org/all/[email protected]/
Changes in v5:
- Rebase change on top of latest v5 patch series.
Changes in v4:
- Use preallocated buffer and stash a buffer that gets reused each time instead of a fresh allocation.
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- prepare series to have all respective cleanup API support for the ssp_sensors following input from Andy Shevchenko
- Link to v2 https://lore.kernel.org/all/[email protected]/
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/[email protected]/
--- drivers/iio/common/ssp_sensors/ssp.h | 3 +++ drivers/iio/common/ssp_sensors/ssp_spi.c | 20 ++------------------ 2 files changed, 5 insertions(+), 18 deletions(-)
drivers/iio/common/ssp_sensors/ssp.h +3 -0
@@ -174,6 +174,7 @@ struct ssp_sensorhub_info {* @pending_list: pending list for messages queued to be sent/read * @sensor_devs: registered IIO devices table * @enable_refcount: enable reference count for wdt (watchdog timer) +* @rx_buf: buffer to receive SPI data * @header_buffer: cache aligned buffer for packet header */ struct ssp_data {
@@ -221,6 +222,8 @@ struct ssp_data {struct iio_dev *sensor_devs[SSP_SENSOR_MAX ];atomic_t enable_refcount; +u8 rx_buf[SSP_DATA_PACKET_SIZE ]; +__le16 header_buffer[SSP_HEADER_BUFFER_SIZE /sizeof (__le16)]__aligned(IIO_DMA_MINALIGN) ; };
drivers/iio/common/ssp_sensors/ssp_spi.c +2 -18
@@ -336,7 +336,7 @@ static int ssp_parse_dataframe(struct ssp_data *data, char *dataframe, int len)/* threaded irq */ int ssp_irq_msg (struct ssp_data *data) { -char *buffer; +char *buffer = data->rx_buf;u8 msg_type;int ret;u16 length, msg_options;
@@ -380,19 +380,12 @@ int ssp_irq_msg(struct ssp_data *data)* but the slave should not send such ones - it is to * check but let's handle this */ - buffer =kmalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) { - ret = -ENOMEM ; - goto _unlock; - }/* got dead packet so it is always an error */ ret =spi_read (data->spi, buffer, length);if (ret >=0 ) ret = -EPROTO ; -kfree (buffer); -dev_err (SSP_DEV ,"No match error %x\n" , msg_options);
@@ -425,22 +418,13 @@ int ssp_irq_msg(struct ssp_data *data)mutex_unlock (&data->pending_lock);break ;case SSP_HUB2AP_WRITE : - buffer =kzalloc (length,GFP_KERNEL |GFP_DMA ); -if (!buffer) -return -ENOMEM ; - ret =spi_read (data->spi, buffer, length);if (ret <0 ) {dev_err (SSP_DEV ,"spi read fail\n" ); -kfree (buffer);break ; } - ret =ssp_parse_dataframe (data, buffer, length); - -kfree (buffer); -break ; - +return ssp_parse_dataframe (data, buffer, length);default :dev_err (SSP_DEV ,"unknown msg type\n" );return -EPROTO ;
Signature · 1 line
2.34.1
Re: [PATCH v6 2/7] iio: ssp_sensors: cleanup codestyle check
In reply to Sanjay ChitrodaQuoted 5 lines · Sanjay Chitroda
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.cCHECK: Macro argument '...' may be better as '(...)' to avoid precedence issues
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v6 3/7] iio: ssp_sensors: factor out pending list add/remove helper(s)
In reply to Sanjay ChitrodaQuoted 9 lines · Sanjay Chitroda
The SSP SPI transfer path manipulates the pending message list in multiple places, each time open-coding the same locking and list operations.
Re-factor the pending list add and delete logic into small helper functions and drop
use_no_irqvariable to avoid duplication and simplify transfer flow to follow.No functional change intended.
after addressing below nit-picks.
...
Review
Quoted 2 lines
(quoted diff) +2
-0+static inline void ssp_pending_add(struct ssp_data *data, + struct ssp_msg *msg)
It's perfectly a single line (we have limit 80, so up to and including 80 is allowed).
static inline void ssp_pending_add(struct ssp_data *data, struct ssp_msg *msg)
(it's only 78 here).
...
drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_check_lines(struct ssp_data *data, bool state) · line 189
Quoted 2 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +2
-0+static inline void ssp_pending_del (struct ssp_data *data, +struct ssp_msg *msg)
Ditto.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v6 3/7] iio: ssp_sensors: factor out pending list add/remove helper(s)
In reply to Sanjay ChitrodaQuoted 9 lines · Sanjay Chitroda
The SSP SPI transfer path manipulates the pending message list in multiple places, each time open-coding the same locking and list operations.
Re-factor the pending list add and delete logic into small helper functions and drop
use_no_irqvariable to avoid duplication and simplify transfer flow to follow.No functional change intended.
...
drivers/iio/common/ssp_sensors/ssp_spi.c static int ssp_check_lines(struct ssp_data *data, bool state) · line 184
Quoted 4 lines
drivers/iio/common/ssp_sensors/ssp_spi.c +0
-4-/* -* check if this is a short one way message or the whole transfer has -* second part after an interrupt -*/
One more thing, please, move this comment to the same condition in ssp_pending_add(). The ssp_pending_del() can survive without it as they are close to each other. Or you can add a short one to refer to the comment in ssp_pending_add().
While moving, fix the style as well.
/* * Check if this is a short one way message or the whole transfer has * second part after an interrupt. */
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v6 4/7] iio: ssp_sensors: drop duplicated wdt timer and work cleanup
In reply to Sanjay ChitrodaQuoted 9 lines · Sanjay Chitroda
The SSP remove path cleans up the watchdog timer and associated work both via
ssp_disable_wdt_timer()and through explicit timer and work teardown.Remove the duplicated
timer_delete_sync()andcancel_work_sync()calls and rely onssp_disable_wdt_timer()as the single cleanup helper to simplify the remove path.No functional change intended.
...
drivers/iio/common/ssp_sensors/ssp_dev.c static void ssp_remove(struct spi_device *spi) · line 592
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +1
-4ssp_enable_mcu (data, false); -ssp_disable_wdt_timer (data); -ssp_clean_pending_list (data);free_irq (data->spi->irq, data); -timer_delete_sync (&data->wdt_timer); -cancel_work_sync (&data->work_wdt); +ssp_disable_wdt_timer (data);
Hmm... I'm not sure it doesn't change the behaviour. First of all, the difference is that IRQ handler may be involved in the timer and/or work.
This patch needs much better investigation conducted and commit message provided with explanation of all possible race conditions and why they don't happen. Also I am not sure if this dup is a mistake or deliberate action (again, it's all related to the timings of IRQ handler, timer job, and work and how they are relies on each other data/flow).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v6 5/7] iio: ssp_sensors: convert probe and teardown to devm-managed resources
In reply to Sanjay ChitrodaQuoted 13 lines · Sanjay Chitroda
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.
...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 551
Quoted 2 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +2
-0+ ret =devm_add_action_or_reset (&spi->dev, + ssp_disable_wdt_timer, data);
It's perfectly a single line of 79 characters (we allow 80 in strict mode and 100 in relaxed and you seems stick with the former, which is fine).
ret = devm_add_action_or_reset(&spi->dev, ssp_disable_wdt_timer, data);
...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 569
Quoted 2 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +2
-0+ ret =devm_add_action_or_reset (&spi->dev, + ssp_shutdown_action, data);
Ditto.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v6 6/7] iio: ssp_sensors: Use dev_err_probe
In reply to Sanjay ChitrodaQuoted 2 lines · Sanjay Chitroda
dev_err_probe()makes error code handling simpler and handle deferred probe nicely (avoid spamming logs).
...
Quoted 1 line
struct
ssp_data*data;
While at it, introduce a temporary variable
struct device *dev = &spi->dev;
and reuse it below. Note, you may add a new patch to reuse it in the lines that are not related to this patch but may utilise the local variable as well.
Actually, you can introduce it earlier in the series, exempli gratia when you moved probe to use managed resources.
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 501
Quoted 8 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +3
-4data =ssp_parse_dt (&spi->dev); -if (!data) { -dev_err (&spi->dev,"Failed to find platform data\n" ); -return -ENODEV ; - } +if (!data) +return dev_err_probe (&spi->dev, -ENODEV , +"Failed to find platform data\n" );
return dev_err_probe(dev, -ENODEV, "Failed to find platform data\n");
(And you can leave it one line as trailing string literals are fine to be
longer 80 even in strict mode. You may even double check with checkpatch, it won't complain.)
...and so on...
...
Quoted 6 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +0
-4if (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" ); -return ret; - }
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 577
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +6
-4- }else { -dev_err (&spi->dev,"Firmware version not supported\n" ); -return -EPERM ; - } +if (ret <0 ) +return dev_err_probe (&spi->dev, ret, +"Initialize_mcu failed\n" ); + }else +return dev_err_probe (&spi->dev, -EPERM , +"Firmware version not supported\n" );
I would now negate the conditional and drop redundant 'else'. Also note the missing {} in the 'else' branch as per Coding Style.
if (data->fw_dl_state != SSP_FW_DL_STATE_NONE) return dev_err_probe(dev, -EPERM, "Firmware version not supported\n"); ret = ssp_initialize_mcu(data); if (ret < 0) return dev_err_probe(dev, ret, "Initialize_mcu failed\n");
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v6 4/7] iio: ssp_sensors: drop duplicated wdt timer and work cleanup
In reply to Andy ShevchenkoQuoted 28 lines · Andy Shevchenko
Quoted 9 lines · Sanjay Chitroda
The SSP remove path cleans up the watchdog timer and associated work both via
ssp_disable_wdt_timer()and through explicit timer and work teardown.Remove the duplicated
timer_delete_sync()andcancel_work_sync()calls and rely onssp_disable_wdt_timer()as the single cleanup helper to simplify the remove path.No functional change intended.
...
drivers/iio/common/ssp_sensors/ssp_dev.c static void ssp_remove(struct spi_device *spi) · line 592
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +1
-4ssp_enable_mcu (data, false); -ssp_disable_wdt_timer (data); -ssp_clean_pending_list (data);free_irq (data->spi->irq, data); -timer_delete_sync (&data->wdt_timer); -cancel_work_sync (&data->work_wdt); +ssp_disable_wdt_timer (data);Hmm... I'm not sure it doesn't change the behaviour. First of all, the difference is that IRQ handler may be involved in the timer and/or work.
Quoted 6 lines
This patch needs much better investigation conducted and commit message provided with explanation of all possible race conditions and why they don't happen. Also I am not sure if this dup is a mistake or deliberate action (again, it's all related to the timings of IRQ handler, timer job, and work and how they are relies on each other data/flow).
Thank you Andy for the input.
You are right patch has changed teardown ordering by moving watchdog stop after free_irq(), so "no functional change" is wrong.
I will send next series change by removing duplicate timer/work cleanup after free_irq().
Race analysis,
- wdt_timer can queue work_wdt
- free_irq() handle IRQ, but does not cancel timer/work. Therefore timer/work needs to explicitly cancelled before IRQ release.
Along with change I will also add analysis in commit message.
Quoted 1 line
Re: [PATCH v6 5/7] iio: ssp_sensors: convert probe and teardown to devm-managed resources
In reply to Andy ShevchenkoQuoted 16 lines · Andy Shevchenko
Quoted 9 lines · Sanjay Chitroda
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.Quoted 4 lines
This simplifies the probe path by removing goto-based error handling, eliminates the remove callback entirely.
No functional change intended.
Quoted 8 lines
...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 551
Quoted 2 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +2
-0+ ret =devm_add_action_or_reset (&spi->dev, + ssp_disable_wdt_timer, data);It's perfectly a single line of 79 characters (we allow 80 in strict mode and 100 in relaxed and you seems stick with the former, which is fine).
Agree, will cross confirm and rectify in next series.
Quoted 9 lines
ret = devm_add_action_or_reset(&spi->dev, ssp_disable_wdt_timer, data);...
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 569
Quoted 2 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +2
-0+ ret =devm_add_action_or_reset (&spi->dev, + ssp_shutdown_action, data);Ditto.
Re: [PATCH v6 6/7] iio: ssp_sensors: Use dev_err_probe
In reply to Andy ShevchenkoQuoted 11 lines · Andy Shevchenko
Quoted 2 lines · Sanjay Chitroda
dev_err_probe()makes error code handling simpler and handle deferred probe nicely (avoid spamming logs)....
Quoted 1 line
struct
ssp_data*data;While at it, introduce a temporary variable
Quoted 8 lines
struct device *dev = &spi->dev;and reuse it below. Note, you may add a new patch to reuse it in the lines that are not related to this patch but may utilise the local variable as well.
Actually, you can introduce it earlier in the series, exempli gratia when you moved probe to use managed resources.
Thank you for the input.
I will add a change to use dev, before devm changes in next series.
Quoted 11 lines
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 501
Quoted 8 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +3
-4data =ssp_parse_dt (&spi->dev); -if (!data) { -dev_err (&spi->dev,"Failed to find platform data\n" ); -return -ENODEV ; - } +if (!data) +return dev_err_probe (&spi->dev, -ENODEV , +"Failed to find platform data\n" );return dev_err_probe(dev, -ENODEV, "Failed to find platform data\n");
Quoted 6 lines
(And you can leave it one line as trailing string literals are fine to be longer 80 even in strict mode. You may even double check with checkpatch, it won't complain.)
...and so on...
Yes, I'm bit obsessed with 72.
I am considering all new changes with possible single line since last week, again thank you for drawing my attention.
Quoted 22 lines
...
Quoted 6 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +0
-4if (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" ); -return ret; - }drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 577
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +6
-4- }else { -dev_err (&spi->dev,"Firmware version not supported\n" ); -return -EPERM ; - } +if (ret <0 ) +return dev_err_probe (&spi->dev, ret, +"Initialize_mcu failed\n" ); + }else +return dev_err_probe (&spi->dev, -EPERM , +"Firmware version not supported\n" );I would now negate the conditional and drop redundant 'else'. Also note the missing {} in the 'else' branch as per Coding Style.
Quoted 7 lines
if (data->fw_dl_state != SSP_FW_DL_STATE_NONE) return dev_err_probe(dev, -EPERM, "Firmware version not supported\n"); ret = ssp_initialize_mcu(data); if (ret < 0) return dev_err_probe(dev, ret, "Initialize_mcu failed\n");
Noted, {} to match with if block style.
I will send changes in next series, drop redundant else and simplify the logic.
Jonathan Cameron
maintainerRe: [PATCH v6 1/7] iio: ssp_sensors: cleanup codestyle warning
In reply to Sanjay ChitrodaQuoted 10 lines · On Wed, 15 Apr 2026 10:37:43 +0530 Sanjay Chitroda…
From: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.cWARNING: Prefer
__packedover __attribute__((__packed__)) +} __attribute__((__packed__));Signed-off-by Sanjay Chitroda <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>
In interests of cutting down on how many patches are in flight I'm going to pick up at least the first few patches from your series. If another version is needed for others don't resend the ones I've picked up.
Applied this one.
thanks,
Quoted 17 lines
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/common/ssp_sensors/ssp_spi.c +1
-1@@ -29,7 +29,7 @@ struct ssp_msg_header {__le16 length;__le16 options;__le32 data; -} __attribute__((__packed__)); +} __packed;struct ssp_msg {u16 length;
Re: [PATCH v6 2/7] iio: ssp_sensors: cleanup codestyle check
In reply to Andy ShevchenkoQuoted 10 lines · On Wed, 15 Apr 2026 12:14:44 +0300 Andy Shevchenko…
Quoted 5 lines · Sanjay Chitroda
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.cCHECK: Macro argument '...' may be better as '(...)' to avoid precedence issues
Reviewed-by Andy Shevchenko <[email protected]>
Applied.
Jonathan Cameron
maintainerRe: [PATCH v6 5/7] iio: ssp_sensors: convert probe and teardown to devm-managed resources
In reply to Sanjay ChitrodaQuoted 11 lines · On Wed, 15 Apr 2026 10:37:47 +0530 Sanjay Chitroda…
From: Sanjay Chitroda <[email protected]>
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.
Quoted 6 lines
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 <[email protected]>
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
Quoted 16 lines
---
Changes in v6:
- Drop remove path completely usingdevm_actionwith review comment from David
- Convert MFD device to manage resource along with mutex and IRQ
- Link to v5: https://lore.kernel.org/all/[email protected]/
---drivers/iio/common/ssp_sensors/ssp_dev.c | 88 ++++++++++-------------- 1 file changed, 38 insertions(+), 50 deletions(-)drivers/iio/common/ssp_sensors/ssp_dev.c +0
-0@@ -194,6 +194,16 @@ static void ssp_disable_wdt_timer(struct ssp_data *data)cancel_work_sync (&data->work_wdt);
drivers/iio/common/ssp_sensors/ssp_dev.c static void ssp_disable_wdt_timer(struct ssp_data *data) · line 204
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +8
-0} +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.
Quoted 75 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +23
-14+} +/** * 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;
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 569
Quoted 5 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +2
-0@@ -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.
Quoted 55 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +5
-36+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)
Re: [PATCH v6 1/7] iio: ssp_sensors: cleanup codestyle warning
In reply to Jonathan CameronQuoted 13 lines · Jonathan Cameron
Quoted 10 lines · On Wed, 15 Apr 2026 10:37:43 +0530 Sanjay Chitroda…
From: Sanjay Chitroda <[email protected]>
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.cWARNING: Prefer
__packedover __attribute__((__packed__)) +} __attribute__((__packed__));Signed-off-by Sanjay Chitroda <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>
Quoted 9 lines
In interests of cutting down on how many patches are in flight I'm going to pick up at least the first few patches from your series. If another version is needed for others don't resend the ones I've picked up.
Applied this one.
thanks,
Understood, thanks.
This will also reduce overhead to maintain patches which are already good and reviewed by.
Quoted 18 lines
Quoted 17 lines
--- drivers/iio/common/ssp_sensors/ssp_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/common/ssp_sensors/ssp_spi.c +1
-1@@ -29,7 +29,7 @@ struct ssp_msg_header {__le16 length;__le16 options;__le32 data; -} __attribute__((__packed__)); +} __packed;struct ssp_msg {u16 length;
Re: [PATCH v6 2/7] iio: ssp_sensors: cleanup codestyle check
In reply to Jonathan CameronQuoted 8 lines · Jonathan Cameron
Quoted 5 lines · On Wed, 15 Apr 2026 12:14:44 +0300 Andy Shevchenko…
Quoted 3 lines · Sanjay Chitroda
Reported by checkpatch:
FILE:drivers/iio/common/ssp_sensors/ssp_spi.c
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
CHECK: Macro argument '...' may be better as '(...)' to avoid precedence issues
Reviewed-by Andy Shevchenko <[email protected]>Applied.
Thanks, will take care.
Re: [PATCH v6 5/7] iio: ssp_sensors: convert probe and teardown to devm-managed resources
In reply to Jonathan CameronQuoted 22 lines · Jonathan Cameron
Quoted 11 lines · On Wed, 15 Apr 2026 10:37:47 +0530 Sanjay Chitroda…
From: Sanjay Chitroda <[email protected]>
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.Quoted 6 lines
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 <[email protected]>Hi Sanjay.
Quoted 9 lines
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.
Quoted 109 lines
Quoted 16 lines
---
Changes in v6:
- Drop remove path completely usingdevm_actionwith review comment from David
- Convert MFD device to manage resource along with mutex and IRQ
- Link to v5: https://lore.kernel.org/all/[email protected]/
---drivers/iio/common/ssp_sensors/ssp_dev.c | 88 ++++++++++-------------- 1 file changed, 38 insertions(+), 50 deletions(-)drivers/iio/common/ssp_sensors/ssp_dev.c +0
-0@@ -194,6 +194,16 @@ static void ssp_disable_wdt_timer(struct ssp_data *data)cancel_work_sync (&data->work_wdt);drivers/iio/common/ssp_sensors/ssp_dev.c static void ssp_disable_wdt_timer(struct ssp_data *data) · line 204
Quoted 10 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +8
-0} +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.
Quoted 78 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +23
-14+} +/** * 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);
Quoted 8 lines
drivers/iio/common/ssp_sensors/ssp_dev.c static int ssp_probe(struct spi_device *spi) · line 569
Quoted 2 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +2
-0+ 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.
Quoted 57 lines
Quoted 56 lines
drivers/iio/common/ssp_sensors/ssp_dev.c +5
-36+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)