Skip to contentSkip to conversation

tmp102: Fix first temperature reading

PATCH In review Guenter Roeck 2 participants 3 messages Oct 24, 2017 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 tmp102: Fix first temperature reading

Commit 3d8f7a89a197 ("hwmon: (tmp102) Improve handling of initial read delay") reduced the initial temperature read delay and made it dependent on the chip's shutdown mode. If the chip was not in shutdown mode at probe, the read delay no longer applies.

This ignores the fact that the chip initialization changes the temperature sensor resolution, and that the temperature register values change when the resolution is changed. As a result, the reported temperature is twice as high as the real temperature until the first temperature conversion after the configuration change is complete. This can result in unexpected behavior and, worst case, in a system shutdown. To fix the problem, let's just always wait for a conversion to complete before reporting a temperature.

Fixes 3d8f7a89a197 ("hwmon: (tmp102) Improve handling of initial read delay")
Cc Ralf Goebel <[email protected]>
---
 drivers/hwmon/tmp102.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/hwmon/tmp102.c +1 -8
index 5eafbaada795..1af1932a72db 100644
@@ -268,14 +268,7 @@ static int tmp102_probe(struct i2c_client *client,
 		return err;
 	}
 
-	tmp102->ready_time = jiffies;
-	if (tmp102->config_orig & TMP102_CONF_SD) {
-		/*
-		 * Mark that we are not ready with data until the first
-		 * conversion is complete
-		 */
-		tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
-	}
+	tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
 
 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
 							 tmp102,
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw

Hi Guenter,

Quoted 33 lines · Guenter Roeck

Commit 3d8f7a89a197 ("hwmon: (tmp102) Improve handling of initial read delay") reduced the initial temperature read delay and made it dependent on the chip's shutdown mode. If the chip was not in shutdown mode at probe, the read delay no longer applies.

This ignores the fact that the chip initialization changes the temperature sensor resolution, and that the temperature register values change when the resolution is changed. As a result, the reported temperature is twice as high as the real temperature until the first temperature conversion after the configuration change is complete. This can result in unexpected behavior and, worst case, in a system shutdown. To fix the problem, let's just always wait for a conversion to complete before reporting a temperature.

Fixes 3d8f7a89a197 ("hwmon: (tmp102) Improve handling of initial read delay")
Cc Ralf Goebel <[email protected]>
---
 drivers/hwmon/tmp102.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/hwmon/tmp102.c +0 -2
index 5eafbaada795..1af1932a72db 100644
@@ -268,14 +268,7 @@ static int tmp102_probe(struct i2c_client *client,
 		return err;
 	}
 
-	tmp102->ready_time = jiffies;
-	if (tmp102->config_orig & TMP102_CONF_SD) {

drivers/hwmon/tmp102.c static int tmp102_probe(struct i2c_client *client, · line 274

Quoted 10 lines
drivers/hwmon/tmp102.c +1 -6
-		/*
-		 * Mark that we are not ready with data until the first
-		 * conversion is complete
-		 */
-		tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
-	}
+	tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
 
 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
 							 tmp102,

Thanks for the quick fix. The change itself looks good but why remove the comment, which I think still retains its value?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#2 lore raw

Hi Jean,

Quoted 44 lines · Jean Delvare

Hi Guenter,

Quoted 41 lines · Guenter Roeck

Commit 3d8f7a89a197 ("hwmon: (tmp102) Improve handling of initial read delay") reduced the initial temperature read delay and made it dependent on the chip's shutdown mode. If the chip was not in shutdown mode at probe, the read delay no longer applies.

This ignores the fact that the chip initialization changes the temperature sensor resolution, and that the temperature register values change when the resolution is changed. As a result, the reported temperature is twice as high as the real temperature until the first temperature conversion after the configuration change is complete. This can result in unexpected behavior and, worst case, in a system shutdown. To fix the problem, let's just always wait for a conversion to complete before reporting a temperature.

Fixes 3d8f7a89a197 ("hwmon: (tmp102) Improve handling of initial read delay")
Cc Ralf Goebel <[email protected]>
---
  drivers/hwmon/tmp102.c | 9 +--------
  1 file changed, 1 insertion(+), 8 deletions(-)
drivers/hwmon/tmp102.c +1 -8
index 5eafbaada795..1af1932a72db 100644
@@ -268,14 +268,7 @@ static int tmp102_probe(struct i2c_client *client,
  		return err;
  	}
 
-	tmp102->ready_time = jiffies;
-	if (tmp102->config_orig & TMP102_CONF_SD) {
-		/*
-		 * Mark that we are not ready with data until the first
-		 * conversion is complete
-		 */
-		tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
-	}
+	tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
 
Quoted 6 lines

drivers/hwmon/tmp102.c static int tmp102_probe(struct i2c_client *client, · line 274

Quoted 2 lines
drivers/hwmon/tmp102.c +0 -0
  	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  							 tmp102,

Thanks for the quick fix. The change itself looks good but why remove the comment, which I think still retains its value?

Good point. I'll add it back in before sending the patch upstream.

Thanks,
Guenter