Re: [PATCH v7 2/3] staging: iio: adc: ad7816: Serialize SPI operations
From: Maxwell Doose
Date: Tue Sep 15 2026 - 08:36:31 EST
On Tue Sep 15, 2026 at 2:59 AM CDT
Abdelnasser Hussein <abdelnasserhussein11@xxxxxxxxx> wrote:
> The ad7816_spi_read() and ad7816_spi_write() functions perform a
> sequence of GPIO state changes followed by an SPI transfer. If multiple
> operations occur simultaneously, the GPIO state could be changed by one
> thread while another is in the middle of a transfer, leading to a race
> condition.
>
> Introduce a mutex to serialize the operations, ensuring that the GPIO
> toggling and the SPI transfer are treated as a single atomic operation.
> The mutex is placed right after the pointers in the device state
> structure to avoid padding holes.
>
> Signed-off-by: Abdelnasser Hussein <abdelnasserhussein11@xxxxxxxxx>
> ---
> drivers/staging/iio/adc/ad7816.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
...
> @@ -47,6 +49,7 @@ struct ad7816_chip_info {
> struct gpio_desc *rdwr_pin;
> struct gpio_desc *convert_pin;
> struct gpio_desc *busy_pin;
> + struct mutex lock; /* protect device state during SPI transfers */
I'm a little on the fence about the comment but if it satisfies
checkpatch then thats one less patch later.
> u8 oti_data[AD7816_CS_MAX + 1];
> u8 channel_id; /* 0 always be temperature */
> u8 mode;
Good find!
Reviewed-by: Maxwell Doose <maxwell@xxxxxxxxxxx>
Thanks,
Max