[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <61b2f38c-ef37-41ec-9102-61903f2f044b@roeck-us.net>
Date: Mon, 19 May 2025 06:22:33 -0700
From: Guenter Roeck <linux@...ck-us.net>
To: Yikai Tsai <yikai.tsai.wiwynn@...il.com>, patrick@...cx.xyz,
Carsten Spieß <mail@...sten-spiess.de>,
Jean Delvare <jdelvare@...e.com>
Cc: linux-hwmon@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v1 1/1] hwmon: (isl28022) Fix current reading calculation
On 5/18/25 23:16, Yikai Tsai wrote:
> According to the ISL28022 datasheet, bit15 of the current register is
> representing -32768. Fix the calculation to properly handle this bit,
> ensuring correct measurements for negative values.
>
> Signed-off-by: Yikai Tsai <yikai.tsai.wiwynn@...il.com>
> ---
> drivers/hwmon/isl28022.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hwmon/isl28022.c b/drivers/hwmon/isl28022.c
> index 1fb9864635db..6d8cdf80c9ea 100644
> --- a/drivers/hwmon/isl28022.c
> +++ b/drivers/hwmon/isl28022.c
> @@ -161,8 +161,9 @@ static int isl28022_read_current(struct device *dev, u32 attr, long *val)
> ISL28022_REG_CURRENT, ®val);
> if (err < 0)
> return err;
> - *val = ((long)regval * 1250L * (long)data->gain) /
> - (long)data->shunt;
> + sign_bit = (regval >> 15) & 0x01;
Just use (s16)(regval) or sign_extend() [granted, the rest of the code should
have done the same]. At the very least make sure that the code compiles.
Guenter
> + *val = (((long)(((u16)regval) & 0x7FFF) - (sign_bit * 32768)) *
> + 1250L * (long)data->gain) / (long)data->shunt;
> break;
> default:
> return -EOPNOTSUPP;
Powered by blists - more mailing lists