Skip to content
Snippets Groups Projects
Commit c37d3287 authored by Paul Fertser's avatar Paul Fertser Committed by Greg Kroah-Hartman
Browse files

hwmon: (tmp421) fix rounding for negative values


[ Upstream commit 724e8af8 ]

Old code produces -24999 for 0b1110011100000000 input in standard format due to
always rounding up rather than "away from zero".

Use the common macro for division, unify and simplify the conversion code along
the way.

Fixes: 9410700b ("hwmon: Add driver for Texas Instruments TMP421/422/423 sensor chips")
Signed-off-by: default avatarPaul Fertser <fercerpav@gmail.com>
Link: https://lore.kernel.org/r/20210924093011.26083-3-fercerpav@gmail.com


Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 8a07d5ab
No related branches found
No related tags found
No related merge requests found
...@@ -100,23 +100,17 @@ struct tmp421_data { ...@@ -100,23 +100,17 @@ struct tmp421_data {
s16 temp[4]; s16 temp[4];
}; };
static int temp_from_s16(s16 reg) static int temp_from_raw(u16 reg, bool extended)
{ {
/* Mask out status bits */ /* Mask out status bits */
int temp = reg & ~0xf; int temp = reg & ~0xf;
return (temp * 1000 + 128) / 256; if (extended)
} temp = temp - 64 * 256;
else
static int temp_from_u16(u16 reg) temp = (s16)temp;
{
/* Mask out status bits */
int temp = reg & ~0xf;
/* Add offset for extended temperature range. */
temp -= 64 * 256;
return (temp * 1000 + 128) / 256; return DIV_ROUND_CLOSEST(temp * 1000, 256);
} }
static struct tmp421_data *tmp421_update_device(struct device *dev) static struct tmp421_data *tmp421_update_device(struct device *dev)
...@@ -153,10 +147,8 @@ static int tmp421_read(struct device *dev, enum hwmon_sensor_types type, ...@@ -153,10 +147,8 @@ static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
switch (attr) { switch (attr) {
case hwmon_temp_input: case hwmon_temp_input:
if (tmp421->config & TMP421_CONFIG_RANGE) *val = temp_from_raw(tmp421->temp[channel],
*val = temp_from_u16(tmp421->temp[channel]); tmp421->config & TMP421_CONFIG_RANGE);
else
*val = temp_from_s16(tmp421->temp[channel]);
return 0; return 0;
case hwmon_temp_fault: case hwmon_temp_fault:
/* /*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment