Re: [PATCH v4 1/2] staging: iio: adc: ad7816: Serialize SPI read operations
From: Jonathan Cameron
Date: Sun Sep 06 2026 - 13:50:00 EST
On Sun, 6 Sep 2026 14:40:48 +0300
Abdelnasser Hussein <abdelnasserhussein11@xxxxxxxxx> wrote:
> Add a mutex to serialize SPI read operations and prevent data corruption. Used guard(mutex) and devm_mutex_init() for safer and automatic cleanup.
Check submitting patches, this is not well formatted.
> Signed-off-by: Abdelnasser Hussein <abdelnasserhussein11@xxxxxxxxx>
Alongside the comments Joshua had I'd expect to see a description of what
the race you are preventing is and if you are going to say things like
data corruption, an illustration of what gets corrupted and how.
Jonathan
> ---
> drivers/staging/iio/adc/ad7816.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7816.c
> index 0e32a2295990..ab80b3a889bb 100644
> --- a/drivers/staging/iio/adc/ad7816.c
> +++ b/drivers/staging/iio/adc/ad7816.c
> @@ -14,6 +14,8 @@
> #include <linux/list.h>
> #include <linux/spi/spi.h>
> #include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/cleanup.h>
>
> #include <linux/iio/iio.h>
> #include <linux/iio/sysfs.h>
> @@ -65,7 +67,8 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
> {
> struct spi_device *spi_dev = chip->spi_dev;
> int ret;
> - __be16 buf;
> +
> + guard(mutex)(&chip->lock);
>
> gpiod_set_value(chip->rdwr_pin, 1);
> gpiod_set_value(chip->rdwr_pin, 0);
> @@ -360,6 +363,10 @@ static int ad7816_probe(struct spi_device *spi_dev)
> return -ENOMEM;
> chip = iio_priv(indio_dev);
>
> + ret = devm_mutex_init(&spi_dev->dev, &chip->lock);
> + if (ret)
> + return ret;
> +
> chip->spi_dev = spi_dev;
> for (i = 0; i <= AD7816_CS_MAX; i++)
> chip->oti_data[i] = 203;