Skip to contentSkip to conversation

iio: temperature: tmp117: fix calibbias cache update on I2C write failure

PATCH Superseded Salah Triki 3 participants 3 messages Aug 22

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: temperature: tmp117: fix calibbias cache update on I2C write failure

The calibbias cache (data->calibbias) was updated before the I2C write to TMP117_REG_TEMP_OFFSET was known to succeed. If the write failed, the function correctly returned an error, but the driver's internal cache had already been updated to the new value.

This causes the cache and the actual hardware register to go out of
sync: a subsequent write of the same value would be silently skipped by the early "if (off == data->calibbias) return 0;" check, since the cache matches even though the register was never successfully updated.

Update data->calibbias only after confirming the I2C write succeeded, so the cache always reflects the actual state of the device.

Fixes df041e737a38 ("iio: temperature: add driver support for ti tmp117")
---
 drivers/iio/temperature/tmp117.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/iio/temperature/tmp117.c +7 -2
index 6bc18616ad15..7d74f401bb80 100644
@@ -95,15 +95,20 @@ static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec
 {
 	struct tmp117_data *data = iio_priv(indio_dev);
 	s16 off;
+	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_CALIBBIAS:
 		off = clamp_t(int, val, S16_MIN, S16_MAX);
 		if (off == data->calibbias)
 			return 0;
+		ret = i2c_smbus_write_word_swapped(data->client, TMP117_REG_TEMP_OFFSET, off);
+
+		if (ret)
+			return ret;
+
 		data->calibbias = off;
-		return i2c_smbus_write_word_swapped(data->client,
-						TMP117_REG_TEMP_OFFSET, off);
+		return 0;
 
 	default:
 		return -EINVAL;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 26 lines · On Sat, 22 Aug 2026 01:49:28 +0100 Salah Triki <[email protected]> wrote:

The calibbias cache (data->calibbias) was updated before the I2C write to TMP117_REG_TEMP_OFFSET was known to succeed. If the write failed, the function correctly returned an error, but the driver's internal cache had already been updated to the new value.

This causes the cache and the actual hardware register to go out of
sync: a subsequent write of the same value would be silently skipped by the early "if (off == data->calibbias) return 0;" check, since the cache matches even though the register was never successfully updated.

Update data->calibbias only after confirming the I2C write succeeded, so the cache always reflects the actual state of the device.

Fixes df041e737a38 ("iio: temperature: add driver support for ti tmp117")
---
 drivers/iio/temperature/tmp117.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/iio/temperature/tmp117.c +0 -0
index 6bc18616ad15..7d74f401bb80 100644

drivers/iio/temperature/tmp117.c static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec · line 104

Quoted 10 lines
drivers/iio/temperature/tmp117.c +1 -0
@@ -95,15 +95,20 @@ static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec
 {
 	struct tmp117_data *data = iio_priv(indio_dev);
 	s16 off;
+	int ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_CALIBBIAS:
 		off = clamp_t(int, val, S16_MIN, S16_MAX);
 		if (off == data->calibbias)
 			return 0;

Add a blank line here.

drivers/iio/temperature/tmp117.c static int tmp117_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec · line 106

Quoted 2 lines
drivers/iio/temperature/tmp117.c +2 -0
+		ret = i2c_smbus_write_word_swapped(data->client, TMP117_REG_TEMP_OFFSET, off);
+

And remove the blank line here - return value checks should be grouped with the function we're checking.

Quoted 10 lines
drivers/iio/temperature/tmp117.c +4 -2
+		if (ret)
+			return ret;
+
 		data->calibbias = off;
-		return i2c_smbus_write_word_swapped(data->client,
-						TMP117_REG_TEMP_OFFSET, off);
+		return 0;
 
 	default:
 		return -EINVAL;
Signature · 2 lines
Kind regards,
Joshua Crofts
reviewer
#2 lore raw
In reply to Salah Triki
Quoted 11 lines · Salah Triki

The calibbias cache (data->calibbias) was updated before the I2C write to TMP117_REG_TEMP_OFFSET was known to succeed. If the write failed, the function correctly returned an error, but the driver's internal cache had already been updated to the new value.

This causes the cache and the actual hardware register to go out of
sync: a subsequent write of the same value would be silently skipped by the early "if (off == data->calibbias) return 0;" check, since the cache matches even though the register was never successfully updated.

Quoted 4 lines

Update data->calibbias only after confirming the I2C write succeeded, so the cache always reflects the actual state of the device.

For fixes, it is always useful to know how you found the problem. I'm guessing code inspection here.

Quoted 3 lines
Fixes df041e737a38 ("iio: temperature: add driver support for ti tmp117")

---