From: Vineet Thakran <vineet789thakran@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: linux-iio@vger.kernel.org, "Kevin Tsai" <ktsai@capellamicro.com>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Vineet Thakran" <vineet789thakran@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH 2/3] iio: light: cm3232: serialize access to the cached command register
Date: Thu, 3 Dec 2026 16:52:53 +0000 [thread overview]
Message-ID: <20261203165254.135545-3-vineet789thakran@gmail.com> (raw)
In-Reply-To: <20261203165254.135545-1-vineet789thakran@gmail.com>
The driver keeps a copy of the command register in chip->regs_cmd.
cm3232_write_als_it(), cm3232_suspend() and cm3232_resume() update it
with a read-modify-write followed by an I2C write, and
cm3232_get_lux() and cm3232_read_als_it() derive the integration time
from it, but nothing serializes these paths. The IIO core does not
serialize read_raw() and write_raw(), so concurrent sysfs accesses can
race: two integration time writes can leave regs_cmd out of sync with
the value last written to the device, and a lux read that races with
an integration time write can scale the ALS count of one integration
time by the other. calibscale is updated and used in the same way.
Add a mutex that protects calibscale, regs_cmd, regs_als and the
device register accesses, and take it in read_raw(), write_raw(),
suspend and resume.
Fixes: b84894c7f088 ("iio: Added Capella cm3232 ambient light sensor driver.")
Fixes: 1ec28ce66aa4 ("iio: light: Added PM support for Capella CM3232 ambient light sensor driver.")
Cc: stable@vger.kernel.org
Signed-off-by: Vineet Thakran <vineet789thakran@gmail.com>
---
drivers/iio/light/cm3232.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 7e81b1c8ce9c..6f0ec4ce6207 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -8,8 +8,10 @@
* IIO driver for CM3232 (7-bit I2C slave address 0x10).
*/
+#include <linux/cleanup.h>
#include <linux/i2c.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/init.h>
@@ -67,6 +69,8 @@ static const struct cm3232_als_info cm3232_als_info_default = {
struct cm3232_chip {
struct i2c_client *client;
const struct cm3232_als_info *als_info;
+ /* Protects calibscale, regs_cmd, regs_als and the device registers */
+ struct mutex lock;
int calibscale;
u8 regs_cmd;
u16 regs_als;
@@ -237,6 +241,8 @@ static int cm3232_read_raw(struct iio_dev *indio_dev,
struct cm3232_chip *chip = iio_priv(indio_dev);
int ret;
+ guard(mutex)(&chip->lock);
+
switch (mask) {
case IIO_CHAN_INFO_PROCESSED:
ret = cm3232_get_lux(chip);
@@ -260,6 +266,8 @@ static int cm3232_write_raw(struct iio_dev *indio_dev,
{
struct cm3232_chip *chip = iio_priv(indio_dev);
+ guard(mutex)(&chip->lock);
+
switch (mask) {
case IIO_CHAN_INFO_CALIBSCALE:
chip->calibscale = val;
@@ -337,6 +345,10 @@ static int cm3232_probe(struct i2c_client *client)
chip->client = client;
chip->calibscale = CM3232_CALIBSCALE_DEFAULT;
+ ret = devm_mutex_init(&client->dev, &chip->lock);
+ if (ret)
+ return ret;
+
indio_dev->channels = cm3232_channels;
indio_dev->num_channels = ARRAY_SIZE(cm3232_channels);
indio_dev->info = &cm3232_info;
@@ -377,6 +389,8 @@ static int cm3232_suspend(struct device *dev)
struct i2c_client *client = chip->client;
int ret;
+ guard(mutex)(&chip->lock);
+
chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
chip->regs_cmd);
@@ -391,6 +405,8 @@ static int cm3232_resume(struct device *dev)
struct i2c_client *client = chip->client;
int ret;
+ guard(mutex)(&chip->lock);
+
chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
chip->regs_cmd | CM3232_CMD_ALS_RESET);
--
2.43.0
next prev parent reply other threads:[~2026-12-03 16:53 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261203165254.135545-1-vineet789thakran@gmail.com>
2026-12-03 16:52 ` [PATCH 1/3] iio: light: cm3232: unregister the device before powering it down Vineet Thakran
2026-12-04 6:59 ` Joshua Crofts
2026-12-03 16:52 ` Vineet Thakran [this message]
2026-12-03 16:52 ` [PATCH 3/3] iio: light: cm3232: reject invalid calibscale values Vineet Thakran
2026-12-04 6:58 ` 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=20261203165254.135545-3-vineet789thakran@gmail.com \
--to=vineet789thakran@gmail.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=ktsai@capellamicro.com \
--cc=linux-iio@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=stable@vger.kernel.org \
/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