* [PATCH v2 0/1] hwmon: (isl28022) Fix current reading calculation @ 2025-05-19 8:40 Yikai Tsai 2025-05-19 8:40 ` [PATCH v2 1/1] " Yikai Tsai 0 siblings, 1 reply; 7+ messages in thread From: Yikai Tsai @ 2025-05-19 8:40 UTC (permalink / raw) To: patrick Cc: Yikai Tsai, Carsten Spieß, Jean Delvare, Guenter Roeck, linux-hwmon, linux-kernel Fix the driver for Renesas ISL28022 power monitor chip. 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. v2: Add the missing variable declaration v1: fix current reading calculation Yikai Tsai (1): hwmon: (isl28022) Fix current reading calculation drivers/hwmon/isl28022.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) -- 2.25.1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/1] hwmon: (isl28022) Fix current reading calculation 2025-05-19 8:40 [PATCH v2 0/1] hwmon: (isl28022) Fix current reading calculation Yikai Tsai @ 2025-05-19 8:40 ` Yikai Tsai 2025-05-20 4:56 ` Guenter Roeck 2025-06-10 12:27 ` Geert Uytterhoeven 0 siblings, 2 replies; 7+ messages in thread From: Yikai Tsai @ 2025-05-19 8:40 UTC (permalink / raw) To: patrick, Carsten Spieß, Jean Delvare, Guenter Roeck Cc: Yikai Tsai, linux-hwmon, linux-kernel 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@gmail.com> --- drivers/hwmon/isl28022.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/isl28022.c b/drivers/hwmon/isl28022.c index 1fb9864635db..1b4fb0824d6c 100644 --- 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; break; default: return -EOPNOTSUPP; -- 2.25.1 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/1] hwmon: (isl28022) Fix current reading calculation 2025-05-19 8:40 ` [PATCH v2 1/1] " Yikai Tsai @ 2025-05-20 4:56 ` Guenter Roeck 2025-06-10 12:27 ` Geert Uytterhoeven 1 sibling, 0 replies; 7+ messages in thread From: Guenter Roeck @ 2025-05-20 4:56 UTC (permalink / raw) To: Yikai Tsai Cc: patrick, Carsten Spieß, Jean Delvare, linux-hwmon, linux-kernel On Mon, May 19, 2025 at 04:40:51PM +0800, 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@gmail.com> Applied. Guenter ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/1] hwmon: (isl28022) Fix current reading calculation 2025-05-19 8:40 ` [PATCH v2 1/1] " Yikai Tsai 2025-05-20 4:56 ` Guenter Roeck @ 2025-06-10 12:27 ` Geert Uytterhoeven 2025-06-16 12:59 ` Guenter Roeck 1 sibling, 1 reply; 7+ messages in thread From: Geert Uytterhoeven @ 2025-06-10 12:27 UTC (permalink / raw) To: Yikai Tsai Cc: patrick, Carsten Spieß, Jean Delvare, Guenter Roeck, linux-hwmon, linux-kernel Hi Yikai, On Mon, 19 May 2025 at 10:48, Yikai Tsai <yikai.tsai.wiwynn@gmail.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@gmail.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 > break; > default: > return -EOPNOTSUPP; Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/1] hwmon: (isl28022) Fix current reading calculation 2025-06-10 12:27 ` Geert Uytterhoeven @ 2025-06-16 12:59 ` Guenter Roeck 2025-06-16 13:00 ` Guenter Roeck 2025-06-16 13:17 ` Geert Uytterhoeven 0 siblings, 2 replies; 7+ messages in thread From: Guenter Roeck @ 2025-06-16 12:59 UTC (permalink / raw) To: Geert Uytterhoeven, Yikai Tsai Cc: patrick, Carsten Spieß, Jean Delvare, linux-hwmon, linux-kernel On 6/10/25 05:27, Geert Uytterhoeven wrote: > Hi Yikai, > > On Mon, 19 May 2025 at 10:48, Yikai Tsai <yikai.tsai.wiwynn@gmail.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@gmail.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. Guenter ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/1] hwmon: (isl28022) Fix current reading calculation 2025-06-16 12:59 ` Guenter Roeck @ 2025-06-16 13:00 ` Guenter Roeck 2025-06-16 13:17 ` Geert Uytterhoeven 1 sibling, 0 replies; 7+ messages in thread From: Guenter Roeck @ 2025-06-16 13:00 UTC (permalink / raw) To: Geert Uytterhoeven, Yikai Tsai Cc: patrick, Carsten Spieß, Jean Delvare, linux-hwmon, linux-kernel On 6/16/25 05:59, Guenter Roeck wrote: > On 6/10/25 05:27, Geert Uytterhoeven wrote: >> Hi Yikai, >> >> On Mon, 19 May 2025 at 10:48, Yikai Tsai <yikai.tsai.wiwynn@gmail.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@gmail.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 s/map/dump/ > write unit test code and use it to simplify the driver. > > Guenter > ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/1] hwmon: (isl28022) Fix current reading calculation 2025-06-16 12:59 ` Guenter Roeck 2025-06-16 13:00 ` Guenter Roeck @ 2025-06-16 13:17 ` Geert Uytterhoeven 1 sibling, 0 replies; 7+ messages in thread From: Geert Uytterhoeven @ 2025-06-16 13:17 UTC (permalink / raw) To: Guenter Roeck Cc: Yikai Tsai, patrick, Carsten Spieß, Jean Delvare, linux-hwmon, linux-kernel Hi Guenter, On Mon, 16 Jun 2025 at 14:59, Guenter Roeck <linux@roeck-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@gmail.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@gmail.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@linux-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 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-06-16 13:17 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-05-19 8:40 [PATCH v2 0/1] hwmon: (isl28022) Fix current reading calculation Yikai Tsai 2025-05-19 8:40 ` [PATCH v2 1/1] " Yikai Tsai 2025-05-20 4:56 ` Guenter Roeck 2025-06-10 12:27 ` Geert Uytterhoeven 2025-06-16 12:59 ` Guenter Roeck 2025-06-16 13:00 ` Guenter Roeck 2025-06-16 13:17 ` Geert Uytterhoeven
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).