Skip to contentSkip to conversation

iio: temperature: tsys02d: Fix state desynchronization on resolution write error

PATCH Superseded Salah Triki 4 participants 4 messages Sep 7 – Sep 13 IIO

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: temperature: tsys02d: Fix state desynchronization on resolution write error

In tsys02d_write_raw(), dev_data->res_index is updated before checking the return value of ms_sensors_write_resolution(). If the hardware I2C write operation fails, dev_data->res_index remains updated with the new index despite the physical sensor remaining in its previous state.

This leads to a state desynchronization where subsequent reads via tsys02d_read_raw() return an incorrect sampling frequency.

Fix this by performing the I2C write first and updating dev_data->res_index only if ms_sensors_write_resolution() succeeds.

Fixes 53bf4d067d51 ("Add tsys02d meas-spec driver support")
---
 drivers/iio/temperature/tsys02d.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/tsys02d.c +2 -1
index 3ef72347456e..f50426beacdd 100644
@@ -73,8 +73,9 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 		if (i < 0)
 			return -EINVAL;
 		mutex_lock(&dev_data->lock);
-		dev_data->res_index = i;
 		ret = ms_sensors_write_resolution(dev_data, i);
+		if (!ret)
+			dev_data->res_index = i;
 		mutex_unlock(&dev_data->lock);
 
 		return ret;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · On Mon, 7 Sep 2026 09:53:53 +0100 Salah Triki <[email protected]> wrote:

In tsys02d_write_raw(), dev_data->res_index is updated before checking the return value of ms_sensors_write_resolution(). If the hardware I2C write operation fails, dev_data->res_index remains updated with the new index despite the physical sensor remaining in its previous state.

Quoted 8 lines

This leads to a state desynchronization where subsequent reads via tsys02d_read_raw() return an incorrect sampling frequency.

Fix this by performing the I2C write first and updating dev_data->res_index only if ms_sensors_write_resolution() succeeds.

Fixes 53bf4d067d51 ("Add tsys02d meas-spec driver support")

Was this found using an LLM or manually?

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 76

Quoted 13 lines
---
 drivers/iio/temperature/tsys02d.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/tsys02d.c +0 -1
index 3ef72347456e..f50426beacdd 100644
@@ -73,8 +73,9 @@ static int tsys02d_write_raw(struct iio_dev *indio_dev,
 		if (i < 0)
 			return -EINVAL;
 		mutex_lock(&dev_data->lock);
-		dev_data->res_index = i;

+ blank line

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 78

Quoted 3 lines
drivers/iio/temperature/tsys02d.c +2 -0
 		ret = ms_sensors_write_resolution(dev_data, i);
+		if (!ret)
+			dev_data->res_index = i;

+ blank line

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 81

Quoted 3 lines
drivers/iio/temperature/tsys02d.c +0 -0
 		mutex_unlock(&dev_data->lock);
 
 		return ret;
Signature · 2 lines
Kind regards,
Joshua Crofts
#2 lore raw
Quoted 2 lines · Joshua Crofts

On Mon, 7 Sep 2026 09:53:53 +0100
Salah Triki <[email protected]> wrote:

...

Quoted 11 lines

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 78

Quoted 3 lines
drivers/iio/temperature/tsys02d.c +2 -0
 		ret = ms_sensors_write_resolution(dev_data, i);
+		if (!ret)
+			dev_data->res_index = i;

+ blank line

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 81

Quoted 3 lines
drivers/iio/temperature/tsys02d.c +0 -0
 		mutex_unlock(&dev_data->lock);
 
 		return ret;

Actually we prefer usual pattern — "check for error first". This 'if (!ret)' is confusing.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw
Quoted 12 lines · On Tue, 8 Sep 2026 13:32:44 +0300 Andy Shevchenko…
Quoted 2 lines · Joshua Crofts

On Mon, 7 Sep 2026 09:53:53 +0100
Salah Triki <[email protected]> wrote:

...

Quoted 6 lines

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 78

Quoted 3 lines
drivers/iio/temperature/tsys02d.c +2 -0
 		ret = ms_sensors_write_resolution(dev_data, i);
+		if (!ret)
+			dev_data->res_index = i;  

+ blank line

Quoted 9 lines
Quoted 5 lines

drivers/iio/temperature/tsys02d.c static int tsys02d_write_raw(struct iio_dev *indio_dev, · line 81

Quoted 3 lines
drivers/iio/temperature/tsys02d.c +0 -0
 		mutex_unlock(&dev_data->lock);
 
 		return ret;  

Actually we prefer usual pattern — "check for error first". This 'if (!ret)' is confusing.

I'd use a guard so we can just return when the error is seen.