[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAMuHMdWjNZTxy8z-vkduk0Depb-6zQYMpV3DTQRBKiDaKaANTQ@mail.gmail.com>
Date: Mon, 16 Jun 2025 15:17:36 +0200
From: Geert Uytterhoeven <geert@...ux-m68k.org>
To: Guenter Roeck <linux@...ck-us.net>
Cc: Yikai Tsai <yikai.tsai.wiwynn@...il.com>, patrick@...cx.xyz,
Carsten Spieß <mail@...sten-spiess.de>,
Jean Delvare <jdelvare@...e.com>, linux-hwmon@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 1/1] hwmon: (isl28022) Fix current reading calculation
Hi Guenter,
On Mon, 16 Jun 2025 at 14:59, Guenter Roeck <linux@...ck-us.net> wrote:
> On 6/10/25 05:27, Geert Uytterhoeven wrote:
> > On Mon, 19 May 2025 at 10:48, Yikai Tsai <yikai.tsai.wiwynn@...il.com> 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>
> >
> >
> >> --- a/drivers/hwmon/isl28022.c
> >> +++ b/drivers/hwmon/isl28022.c
> >> @@ -154,6 +154,7 @@ static int isl28022_read_current(struct device *dev, u32 attr, long *val)
> >> struct isl28022_data *data = dev_get_drvdata(dev);
> >> unsigned int regval;
> >> int err;
> >> + u16 sign_bit;
> >>
> >> switch (attr) {
> >> case hwmon_curr_input:
> >> @@ -161,8 +162,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;
> >> + *val = (((long)(((u16)regval) & 0x7FFF) - (sign_bit * 32768)) *
> >> + 1250L * (long)data->gain) / (long)data->shunt;
> >
> > Isn't this complex operation to convert the 16-bit register value to
> > a two-complement signed number equivalent to a simple cast?
> >
> > (s16)regval
> >
> > isl28022_read_in() has similar code, but as the sign bit is not always
> > the MSB, it needs two additional shifts:
> >
> > ((s16)(regval << shift)) >> shift
> >
>
> Yes, I know. This could all be simplified using sign_extend32(), but I really
> wanted to be able to apply it. If someone sends me a register map, maybe I can
> write unit test code and use it to simplify the driver.
Cool, I didn't know we had a helper for that... Thanks ;-)
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@...ux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Powered by blists - more mailing lists