diff mbox series

[v2,3/3] hwmon: tmp421: fix rounding for negative values

Message ID 20210923094801.23332-3-fercerpav@gmail.com (mailing list archive)
State Changes Requested
Headers show
Series [v2,1/3] hwmon: tmp421: handle I2C errors | expand

Commit Message

Paul Fertser Sept. 23, 2021, 9:48 a.m. UTC
Current 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.

Signed-off-by: Paul Fertser <fercerpav@gmail.com>
---

Changes from v1:
 - Trivial rebase

 drivers/hwmon/tmp421.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

Comments

Guenter Roeck Sept. 24, 2021, 2:21 a.m. UTC | #1
On Thu, Sep 23, 2021 at 12:48:01PM +0300, Paul Fertser wrote:
> Current 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.
> 
> Signed-off-by: Paul Fertser <fercerpav@gmail.com>

This patch should probably have a Fixes: tag.

Guenter

> ---
> 
> Changes from v1:
>  - Trivial rebase
> 
>  drivers/hwmon/tmp421.c | 24 ++++++++----------------
>  1 file changed, 8 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/hwmon/tmp421.c b/drivers/hwmon/tmp421.c
> index 9f0a4db695db..c77c9f3bbd21 100644
> --- a/drivers/hwmon/tmp421.c
> +++ b/drivers/hwmon/tmp421.c
> @@ -100,23 +100,17 @@ struct tmp421_data {
>  	s16 temp[4];
>  };
>  
> -static int temp_from_s16(s16 reg)
> +static int temp_from_raw(u16 reg, bool extended)
>  {
>  	/* Mask out status bits */
>  	int temp = reg & ~0xf;
>  
> -	return (temp * 1000 + 128) / 256;
> -}
> -
> -static int temp_from_u16(u16 reg)
> -{
> -	/* Mask out status bits */
> -	int temp = reg & ~0xf;
> -
> -	/* Add offset for extended temperature range. */
> -	temp -= 64 * 256;
> +	if (extended)
> +		temp = temp - 64 * 256;
> +	else
> +		temp = (s16)temp;
>  
> -	return (temp * 1000 + 128) / 256;
> +	return DIV_ROUND_CLOSEST(temp * 1000, 256);
>  }
>  
>  static int tmp421_update_device(struct tmp421_data *data)
> @@ -175,10 +169,8 @@ static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
>  
>  	switch (attr) {
>  	case hwmon_temp_input:
> -		if (tmp421->config & TMP421_CONFIG_RANGE)
> -			*val = temp_from_u16(tmp421->temp[channel]);
> -		else
> -			*val = temp_from_s16(tmp421->temp[channel]);
> +		*val = temp_from_raw(tmp421->temp[channel],
> +				     tmp421->config & TMP421_CONFIG_RANGE);
>  		return 0;
>  	case hwmon_temp_fault:
>  		/*
diff mbox series

Patch

diff --git a/drivers/hwmon/tmp421.c b/drivers/hwmon/tmp421.c
index 9f0a4db695db..c77c9f3bbd21 100644
--- a/drivers/hwmon/tmp421.c
+++ b/drivers/hwmon/tmp421.c
@@ -100,23 +100,17 @@  struct tmp421_data {
 	s16 temp[4];
 };
 
-static int temp_from_s16(s16 reg)
+static int temp_from_raw(u16 reg, bool extended)
 {
 	/* Mask out status bits */
 	int temp = reg & ~0xf;
 
-	return (temp * 1000 + 128) / 256;
-}
-
-static int temp_from_u16(u16 reg)
-{
-	/* Mask out status bits */
-	int temp = reg & ~0xf;
-
-	/* Add offset for extended temperature range. */
-	temp -= 64 * 256;
+	if (extended)
+		temp = temp - 64 * 256;
+	else
+		temp = (s16)temp;
 
-	return (temp * 1000 + 128) / 256;
+	return DIV_ROUND_CLOSEST(temp * 1000, 256);
 }
 
 static int tmp421_update_device(struct tmp421_data *data)
@@ -175,10 +169,8 @@  static int tmp421_read(struct device *dev, enum hwmon_sensor_types type,
 
 	switch (attr) {
 	case hwmon_temp_input:
-		if (tmp421->config & TMP421_CONFIG_RANGE)
-			*val = temp_from_u16(tmp421->temp[channel]);
-		else
-			*val = temp_from_s16(tmp421->temp[channel]);
+		*val = temp_from_raw(tmp421->temp[channel],
+				     tmp421->config & TMP421_CONFIG_RANGE);
 		return 0;
 	case hwmon_temp_fault:
 		/*