From: David Lechner <dlechner@baylibre.com>
To: Mohammad Amin Hosseini <moahmmad.hosseinii@gmail.com>,
linux-iio@vger.kernel.org, linux-staging@lists.linux.dev
Cc: linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org,
jic23@kernel.org, lars@metafoo.de, Michael.Hennerich@analog.com,
nuno.sa@analog.com, andy@kernel.org
Subject: Re: [PATCH] staging: iio: adc: ad7816: add mutex to serialize SPI/GPIO operations
Date: Mon, 1 Sep 2025 11:47:57 -0500 [thread overview]
Message-ID: <b6c2ac13-2781-49ba-964f-ca821b32e2a2@baylibre.com> (raw)
In-Reply-To: <20250831153741.8820-1-moahmmad.hosseinii@gmail.com>
On 8/31/25 10:37 AM, Mohammad Amin Hosseini wrote:
> From: mohammad amin hosseini <moahmmad.hosseinii@gmail.com>
>
> The ad7816 driver was accessing SPI and GPIO lines without
> synchronization, which could lead to race conditions when accessed
> concurrently from multiple contexts. This might result in corrupted
> readings or inconsistent GPIO states.
>
> Introduce an io_lock mutex in the driver structure to serialize:
> - SPI transactions in ad7816_spi_read() and ad7816_spi_write()
> - GPIO pin toggling sequences
> - Updates to device state via sysfs store functions (mode, channel, oti)
>
> The mutex ensures proper mutual exclusion and prevents race
> conditions under concurrent access.
>
> Signed-off-by: Mohammad Amin Hosseini <moahmmad.hosseinii@gmail.com>
> ---
> drivers/staging/iio/adc/ad7816.c | 29 +++++++++++++++++++----------
> 1 file changed, 19 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7816.c
> index 4774df778de9..06567d048a6d 100644
> --- a/drivers/staging/iio/adc/ad7816.c
> +++ b/drivers/staging/iio/adc/ad7816.c
> @@ -50,6 +50,7 @@ struct ad7816_chip_info {
> u8 oti_data[AD7816_CS_MAX + 1];
> u8 channel_id; /* 0 always be temperature */
> u8 mode;
> + struct mutex io_lock; /* Protects SPI transactions and GPIO toggling */
> };
>
> enum ad7816_type {
> @@ -67,13 +68,13 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
> int ret;
> __be16 buf;
>
> + mutex_lock(&chip->io_lock);
Could avoid the gotos by using:
guard(mutex)(&chip->io_lock);
> +
> gpiod_set_value(chip->rdwr_pin, 1);
> gpiod_set_value(chip->rdwr_pin, 0);
> ret = spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id));
> - if (ret < 0) {
> - dev_err(&spi_dev->dev, "SPI channel setting error\n");
> - return ret;
> - }
> + if (ret < 0)
> + goto unlock;
> gpiod_set_value(chip->rdwr_pin, 1);
>
> if (chip->mode == AD7816_PD) { /* operating mode 2 */
> @@ -92,13 +93,13 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
> gpiod_set_value(chip->rdwr_pin, 0);
> gpiod_set_value(chip->rdwr_pin, 1);
> ret = spi_read(spi_dev, &buf, sizeof(*data));
> - if (ret < 0) {
> - dev_err(&spi_dev->dev, "SPI data read error\n");
> - return ret;
> - }
> + if (ret < 0)
> + goto unlock;
>
> *data = be16_to_cpu(buf);
>
> +unlock:
> + mutex_unlock(&chip->io_lock);
> return ret;
> }
>
> @@ -107,12 +108,13 @@ static int ad7816_spi_write(struct ad7816_chip_info *chip, u8 data)
> struct spi_device *spi_dev = chip->spi_dev;
> int ret;
>
> + mutex_lock(&chip->io_lock);
> +
> gpiod_set_value(chip->rdwr_pin, 1);
> gpiod_set_value(chip->rdwr_pin, 0);
> ret = spi_write(spi_dev, &data, sizeof(data));
> - if (ret < 0)
> - dev_err(&spi_dev->dev, "SPI oti data write error\n");
>
> + mutex_unlock(&chip->io_lock);
> return ret;
> }
>
...
> @@ -363,6 +371,7 @@ static int ad7816_probe(struct spi_device *spi_dev)
> dev_set_drvdata(&spi_dev->dev, indio_dev);
>
> chip->spi_dev = spi_dev;
> + mutex_init(&chip->io_lock);
Prefer devm_mutex_init() instead.
> for (i = 0; i <= AD7816_CS_MAX; i++)
> chip->oti_data[i] = 203;
>
prev parent reply other threads:[~2025-09-01 16:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-31 15:37 Mohammad Amin Hosseini
2025-09-01 6:10 ` Greg KH
2025-09-01 16:47 ` David Lechner [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b6c2ac13-2781-49ba-964f-ca821b32e2a2@baylibre.com \
--to=dlechner@baylibre.com \
--cc=Michael.Hennerich@analog.com \
--cc=andy@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-staging@lists.linux.dev \
--cc=moahmmad.hosseinii@gmail.com \
--cc=nuno.sa@analog.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®