From: Jaakko Koivisto <jmatko@utu.fi>
To: "Andreas Klinger" <ak@it-klinger.de>,
"Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>
Cc: <linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
Jaakko Koivisto <jmatko@utu.fi>
Subject: [PATCH v2 2/5] iio: chemical: sgp40: Replace manual locking with guard()
Date: Mon, 2 Nov 2026 11:12:55 +0200 [thread overview]
Message-ID: <20261102091258.247402-3-jmatko@utu.fi> (raw)
In-Reply-To: <20261102091258.247402-1-jmatko@utu.fi>
Replace manual mutex_lock() and mutex_unlock() with RAII-style
guard(mutex)().
Extend the lock in sgp40_measure_resistance_raw() to cover the
I2C send and receive cycle, preventing I/O-errors if the device
is currently running a command, or clobbering the output of a previous
command if a new command is issued during the sleep-period between
i2c_master_send() and i2c_master_recv().
Signed-off-by: Jaakko Koivisto <jmatko@utu.fi>
---
drivers/iio/chemical/sgp40.c | 33 +++++++++++++--------------------
1 file changed, 13 insertions(+), 20 deletions(-)
diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
index 3a239fb6847f..080d51f9f53c 100644
--- a/drivers/iio/chemical/sgp40.c
+++ b/drivers/iio/chemical/sgp40.c
@@ -148,9 +148,8 @@ static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc)
u32 exp = 0;
/* we calculate as a multiple of 16384 (2^14) */
- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);
x = ((int)resistance_raw - data->res_calibbias) * 106;
- mutex_unlock(&data->lock);
/* voc = 500 / (1 + e^x) */
exp = sgp40_exp(x, SGP40_CALC_POWER, 18);
@@ -172,7 +171,7 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
struct sgp40_tg_result tgres;
- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);
ticks = (data->rht / 10) * 65535 / 10000;
ticks16 = (u16)clamp(ticks, 0u, 65535u); /* clamp between 0 .. 100 %rH */
@@ -184,8 +183,6 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
tg.temp_ticks = cpu_to_be16(ticks16);
tg.temp_crc = crc8(sgp40_crc8_table, (u8 *)&tg.temp_ticks, 2, SGP40_CRC8_INIT);
- mutex_unlock(&data->lock);
-
ret = i2c_master_send(client, (const char *)&tg, sizeof(tg));
if (ret < 0)
return ret;
@@ -232,16 +229,16 @@ static int sgp40_read_raw(struct iio_dev *indio_dev,
*val = resistance_raw;
return IIO_VAL_INT;
- case IIO_TEMP:
- mutex_lock(&data->lock);
+ case IIO_TEMP: {
+ guard(mutex)(&data->lock);
*val = data->temp;
- mutex_unlock(&data->lock);
return IIO_VAL_INT;
- case IIO_HUMIDITYRELATIVE:
- mutex_lock(&data->lock);
+ }
+ case IIO_HUMIDITYRELATIVE: {
+ guard(mutex)(&data->lock);
*val = data->rht;
- mutex_unlock(&data->lock);
return IIO_VAL_INT;
+ }
default:
return -EINVAL;
}
@@ -263,11 +260,11 @@ static int sgp40_read_raw(struct iio_dev *indio_dev,
*val2 = ((voc % (1 << SGP40_CALC_POWER)) * 244) / (1 << (SGP40_CALC_POWER - 12));
dev_dbg(data->dev, "voc: %d val: %d.%06d\n", voc, *val, *val2);
return IIO_VAL_INT_PLUS_MICRO;
- case IIO_CHAN_INFO_CALIBBIAS:
- mutex_lock(&data->lock);
+ case IIO_CHAN_INFO_CALIBBIAS: {
+ guard(mutex)(&data->lock);
*val = data->res_calibbias;
- mutex_unlock(&data->lock);
return IIO_VAL_INT;
+ }
default:
return -EINVAL;
}
@@ -279,6 +276,8 @@ static int sgp40_write_raw(struct iio_dev *indio_dev,
{
struct sgp40_data *data = iio_priv(indio_dev);
+ guard(mutex)(&data->lock);
+
switch (mask) {
case IIO_CHAN_INFO_RAW:
switch (chan->type) {
@@ -286,17 +285,13 @@ static int sgp40_write_raw(struct iio_dev *indio_dev,
if ((val < -45000) || (val > 130000))
return -EINVAL;
- mutex_lock(&data->lock);
data->temp = val;
- mutex_unlock(&data->lock);
return 0;
case IIO_HUMIDITYRELATIVE:
if ((val < 0) || (val > 100000))
return -EINVAL;
- mutex_lock(&data->lock);
data->rht = val;
- mutex_unlock(&data->lock);
return 0;
default:
return -EINVAL;
@@ -305,9 +300,7 @@ static int sgp40_write_raw(struct iio_dev *indio_dev,
if ((val < 20000) || (val > 52768))
return -EINVAL;
- mutex_lock(&data->lock);
data->res_calibbias = val;
- mutex_unlock(&data->lock);
return 0;
}
return -EINVAL;
--
2.56.0
next prev parent reply other threads:[~2026-11-02 9:14 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 9:12 [PATCH v2 0/5] iio: chemical: sgp40: add funtions for serial number readback, self-test, and heater control Jaakko Koivisto
2026-11-02 9:12 ` [PATCH v2 1/5] iio: chemical: sgp40: Do not convert I2C errors to EIO Jaakko Koivisto
2026-11-02 9:54 ` Joshua Crofts
2026-11-02 9:12 ` Jaakko Koivisto [this message]
2026-11-02 10:10 ` [PATCH v2 2/5] iio: chemical: sgp40: Replace manual locking with guard() Joshua Crofts
2026-11-02 9:12 ` [PATCH v2 3/5] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
2026-11-02 10:20 ` Joshua Crofts
2026-11-02 9:12 ` [PATCH v2 4/5] iio: chemical: sgp40: Implement execute_self_test-command Jaakko Koivisto
2026-11-02 10:21 ` Joshua Crofts
2026-11-02 9:56 ` [PATCH v2 5/5] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto
2026-11-02 10:24 ` Joshua Crofts
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=20261102091258.247402-3-jmatko@utu.fi \
--to=jmatko@utu.fi \
--cc=ak@it-klinger.de \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--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®