Skip to contentSkip to conversation

iio: temperature: mcp9600: Fix temperature reading for negative values

PATCH Applied Dimitri Fedrau 4 participants 7 messages Apr 24, 2024 – Apr 29, 2024 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: temperature: mcp9600: Fix temperature reading for negative values

Temperature is stored as 16bit value in two's complement format. Current implementation ignores the sign bit. Make it aware of the sign bit by using sign_extend32.

Fixes 3f6b9598b6df ("iio: temperature: Add MCP9600 thermocouple EMF converter")
---
 drivers/iio/temperature/mcp9600.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/mcp9600.c +2 -1
index 46845804292b..7a3eef5d5e75 100644
@@ -52,7 +52,8 @@ static int mcp9600_read(struct mcp9600_data *data,
 
 	if (ret < 0)
 		return ret;
-	*val = ret;
+
+	*val = sign_extend32(ret, 15);
 
 	return 0;
 }
Signature · 1 line
2.39.2
#1 lore raw

Hi Dimitri,

Interesting patch this one.
I think this does apply, although, the cold junction register has for sign bits so I think we could also have a mask to clear those out.
Some code suggestions inline.

Regards,
Marcelo

drivers/iio/temperature/mcp9600.c

Quoted 14 lines · Dimitri Fedrau

Temperature is stored as 16bit value in two's complement format. Current implementation ignores the sign bit. Make it aware of the sign bit by using sign_extend32.

Fixes 3f6b9598b6df ("iio: temperature: Add MCP9600 thermocouple EMF converter")
---
 drivers/iio/temperature/mcp9600.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/mcp9600.c +0 -0
index 46845804292b..7a3eef5d5e75 100644

#define MCP9600_COLD_JUNCTION_SIGN_MSK GENMASK(15,12)
...

drivers/iio/temperature/mcp9600.c static int mcp9600_read(struct mcp9600_data *data, · line 56

Quoted 7 lines
drivers/iio/temperature/mcp9600.c +2 -1
@@ -52,7 +52,8 @@ static int mcp9600_read(struct mcp9600_data *data,
 
 	if (ret < 0)
 		return ret;
-	*val = ret;
+
+	*val = sign_extend32(ret, 15);
	if (chan->address == MCP9600_COLD_JUNCTION)
		*val &= ~MCP9600_COLD_JUNCTION_SIGN_MSK;
Quoted 7 lines
drivers/iio/temperature/mcp9600.c +0 -0
 	return 0;
 }
Signature · 1 line
2.39.2
#2 lore raw

Am Sat, Apr 27, 2024 at 05:49:18AM -0300 schrieb Marcelo Schmitt:

Quoted 7 lines

Hi Dimitri,

Interesting patch this one.
I think this does apply, although, the cold junction register has for sign bits so I think we could also have a mask to clear those out.
Some code suggestions inline.

Hi Marcelo,

the temperature bits are in two’s complement format for hot and cold junction. Equations to calculate the temperature are also the same in the datasheet. There should be no difference when handling them. I don't think we need to do anything more with the value except sign_extend it to the appropriate data type. If the sign bits aren't right, there is a bug in the chip, until then futher processing of it is unneeded. I could add a comment here if it helps.

Quoted 21 lines · Dimitri Fedrau

drivers/iio/temperature/mcp9600.c

Quoted 14 lines

Temperature is stored as 16bit value in two's complement format. Current implementation ignores the sign bit. Make it aware of the sign bit by using sign_extend32.

Fixes 3f6b9598b6df ("iio: temperature: Add MCP9600 thermocouple EMF converter")
---
 drivers/iio/temperature/mcp9600.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/mcp9600.c +0 -0
index 46845804292b..7a3eef5d5e75 100644

#define MCP9600_COLD_JUNCTION_SIGN_MSK GENMASK(15,12)
...

Quoted 2 lines
drivers/iio/temperature/mcp9600.c +0 -0
@@ -52,7 +52,8 @@ static int mcp9600_read(struct mcp9600_data *data,
 
Quoted 8 lines

drivers/iio/temperature/mcp9600.c static int mcp9600_read(struct mcp9600_data *data, · line 56

Quoted 5 lines
drivers/iio/temperature/mcp9600.c +2 -1
 	if (ret < 0)
 		return ret;
-	*val = ret;
+
+	*val = sign_extend32(ret, 15);
	if (chan->address == MCP9600_COLD_JUNCTION)
		*val &= ~MCP9600_COLD_JUNCTION_SIGN_MSK;

This won't work. Assuming int is 32-bit ret = 0xfffe and *val = -2 after sign_extends32, this would result in *val = -61442 which is wrong.

Quoted 7 lines
Quoted 7 lines
drivers/iio/temperature/mcp9600.c +0 -0
 	return 0;
 }
Signature · 1 line
2.39.2

Best regards,
Dimitri Fedrau

maintainer
#3 lore raw
Quoted 10 lines · On Sat, 27 Apr 2024 21:57:58 +0200 Dimitri Fedrau <[email protected]> wrote:

Am Sat, Apr 27, 2024 at 05:49:18AM -0300 schrieb Marcelo Schmitt:

Quoted 7 lines

Hi Dimitri,

Interesting patch this one.
I think this does apply, although, the cold junction register has for sign bits so I think we could also have a mask to clear those out.
Some code suggestions inline.

Hi Marcelo,

Quoted 7 lines

the temperature bits are in two’s complement format for hot and cold junction. Equations to calculate the temperature are also the same in the datasheet. There should be no difference when handling them. I don't think we need to do anything more with the value except sign_extend it to the appropriate data type. If the sign bits aren't right, there is a bug in the chip, until then futher processing of it is unneeded. I could add a comment here if it helps.

Agreed - by my reading the original patch is correct. Maybe it would act as cleaner 'documentation' to have the sign_extend32() for the cold junction be from bit 12 rather than 15, but I'm not sure it's worth the effort.

Andrew, would be great if you can review this fix in case we are all missing something!

Jonathan

Quoted 41 lines
Quoted 22 lines · Dimitri Fedrau

drivers/iio/temperature/mcp9600.c

Quoted 14 lines

Temperature is stored as 16bit value in two's complement format. Current implementation ignores the sign bit. Make it aware of the sign bit by using sign_extend32.

Fixes 3f6b9598b6df ("iio: temperature: Add MCP9600 thermocouple EMF converter")
---
 drivers/iio/temperature/mcp9600.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/mcp9600.c +0 -0
index 46845804292b..7a3eef5d5e75 100644

#define MCP9600_COLD_JUNCTION_SIGN_MSK GENMASK(15,12)
...

Quoted 2 lines
drivers/iio/temperature/mcp9600.c +0 -0
@@ -52,7 +52,8 @@ static int mcp9600_read(struct mcp9600_data *data,
 
Quoted 8 lines

drivers/iio/temperature/mcp9600.c static int mcp9600_read(struct mcp9600_data *data, · line 56

Quoted 5 lines
drivers/iio/temperature/mcp9600.c +2 -1
 	if (ret < 0)
 		return ret;
-	*val = ret;
+
+	*val = sign_extend32(ret, 15);  
	if (chan->address == MCP9600_COLD_JUNCTION)
		*val &= ~MCP9600_COLD_JUNCTION_SIGN_MSK;

This won't work. Assuming int is 32-bit ret = 0xfffe and *val = -2 after sign_extends32, this would result in *val = -61442 which is wrong.

Quoted 7 lines
Quoted 7 lines
drivers/iio/temperature/mcp9600.c +0 -0
 	return 0;
 }
Signature · 1 line
2.39.2

Best regards,
Dimitri Fedrau

Reviewed-by
#4 lore raw
Quoted 13 lines · Jonathan Cameron
Quoted 10 lines · On Sat, 27 Apr 2024 21:57:58 +0200 Dimitri Fedrau <[email protected]> wrote:

Am Sat, Apr 27, 2024 at 05:49:18AM -0300 schrieb Marcelo Schmitt:

Quoted 7 lines

Hi Dimitri,

Interesting patch this one.
I think this does apply, although, the cold junction register has for sign bits so I think we could also have a mask to clear those out.
Some code suggestions inline.

Hi Marcelo,

Quoted 7 lines
Quoted 7 lines

the temperature bits are in two’s complement format for hot and cold junction. Equations to calculate the temperature are also the same in the datasheet. There should be no difference when handling them. I don't think we need to do anything more with the value except sign_extend it to the appropriate data type. If the sign bits aren't right, there is a bug in the chip, until then futher processing of it is unneeded. I could add a comment here if it helps.

Ah yes, I missed the fact that other bits need to be set to make the value smaller (closer to zero) when the MSB of two's complement number is set.

Quoted 4 lines

Agreed - by my reading the original patch is correct. Maybe it would act as cleaner 'documentation' to have the sign_extend32() for the cold junction be from bit 12 rather than 15, but I'm not sure it's worth the effort.

Yes, the original patch should be correct.
Yeah, no practical difference using bit 12 or 15 to sign extend the number so probably not worth changing that.

Quoted 12 lines

Andrew, would be great if you can review this fix in case we are all missing something!

Jonathan

Quoted 6 lines
Quoted 6 lines · Dimitri Fedrau
Quoted 4 lines

Temperature is stored as 16bit value in two's complement format. Current implementation ignores the sign bit. Make it aware of the sign bit by using sign_extend32.

Quoted 36 lines
Quoted 35 lines
Quoted 16 lines

drivers/iio/temperature/mcp9600.c

Quoted 10 lines
Fixes 3f6b9598b6df ("iio: temperature: Add MCP9600 thermocouple EMF converter")
---
 drivers/iio/temperature/mcp9600.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/temperature/mcp9600.c +0 -0
index 46845804292b..7a3eef5d5e75 100644

#define MCP9600_COLD_JUNCTION_SIGN_MSK GENMASK(15,12)
...

Quoted 2 lines
drivers/iio/temperature/mcp9600.c +0 -0
@@ -52,7 +52,8 @@ static int mcp9600_read(struct mcp9600_data *data,
 
Quoted 8 lines

drivers/iio/temperature/mcp9600.c static int mcp9600_read(struct mcp9600_data *data, · line 56

Quoted 5 lines
drivers/iio/temperature/mcp9600.c +2 -1
 	if (ret < 0)
 		return ret;
-	*val = ret;
+
+	*val = sign_extend32(ret, 15);  
	if (chan->address == MCP9600_COLD_JUNCTION)
		*val &= ~MCP9600_COLD_JUNCTION_SIGN_MSK;

This won't work. Assuming int is 32-bit ret = 0xfffe and *val = -2 after sign_extends32, this would result in *val = -61442 which is wrong.

Quoted 7 lines
Quoted 7 lines
drivers/iio/temperature/mcp9600.c +0 -0
 	return 0;
 }
Signature · 1 line
2.39.2

Best regards,
Dimitri Fedrau

#5 lore raw
In reply to Jonathan Cameron

Hi all,

Quoted 6 lines · Jonathan Cameron

Agreed - by my reading the original patch is correct. Maybe it would act as cleaner 'documentation' to have the sign_extend32() for the cold junction be from bit 12 rather than 15, but I'm not sure it's worth the effort.

Andrew, would be great if you can review this fix in case we are all missing something!

I also agree that Dimitri’s original patch appears correct per the data sheet.

I think of the cold junction register as a 16 bit 2s complement signed integer, despite the limited range. I like sign extending it from bit 15 rather than from 12.

I applied Dimitri’s patch and stuck a dev board in my icebox.

Before patch:

    # cat /sys/bus/iio/devices/iio:device0/in_temp_raw 
    65222
    # cat /sys/bus/iio/devices/iio:device0/in_temp_ambient_raw
    65256

After patch:

    # cat /sys/bus/iio/devices/iio:device0/in_temp_raw
    -260
    # cat /sys/bus/iio/devices/iio:device0/in_temp_ambient_raw
    -212

Looks good to me! Thanks for the patch Dimitri, and thanks to all for the review!

Andrew

maintainer Applied
#6 lore raw
Quoted 25 lines · On Sun, 28 Apr 2024 14:46:39 -0400 Andrew Hepp <[email protected]> wrote:

Hi all,

Quoted 6 lines · Jonathan Cameron

Agreed - by my reading the original patch is correct. Maybe it would act as cleaner 'documentation' to have the sign_extend32() for the cold junction be from bit 12 rather than 15, but I'm not sure it's worth the effort.

Andrew, would be great if you can review this fix in case we are all missing something!

I also agree that Dimitri’s original patch appears correct per the data sheet.

I think of the cold junction register as a 16 bit 2s complement signed integer, despite the limited range. I like sign extending it from bit 15 rather than from 12.

I applied Dimitri’s patch and stuck a dev board in my icebox.

Before patch:

    # cat /sys/bus/iio/devices/iio:device0/in_temp_raw 
    65222
    # cat /sys/bus/iio/devices/iio:device0/in_temp_ambient_raw
    65256

After patch:

Quoted 8 lines
    # cat /sys/bus/iio/devices/iio:device0/in_temp_raw
    -260
    # cat /sys/bus/iio/devices/iio:device0/in_temp_ambient_raw
    -212

Looks good to me! Thanks for the patch Dimitri, and thanks to all for the review!

Andrew

That definitely deserves a Tested-by tag so I added one. Hope you don't mind!

Applied to the fixes-togreg branch of iio.git and marked for stable.

Thanks,

Jonathan