Skip to content

Commit b2446a1

Browse files
yikaitsaiwwgroeck
authored andcommitted
hwmon: (isl28022) Fix current reading calculation
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> Link: https://lore.kernel.org/r/20250519084055.3787-2-yikai.tsai.wiwynn@gmail.com Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 0ddce55 commit b2446a1

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/hwmon/isl28022.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,17 @@ static int isl28022_read_current(struct device *dev, u32 attr, long *val)
154154
struct isl28022_data *data = dev_get_drvdata(dev);
155155
unsigned int regval;
156156
int err;
157+
u16 sign_bit;
157158

158159
switch (attr) {
159160
case hwmon_curr_input:
160161
err = regmap_read(data->regmap,
161162
ISL28022_REG_CURRENT, &regval);
162163
if (err < 0)
163164
return err;
164-
*val = ((long)regval * 1250L * (long)data->gain) /
165-
(long)data->shunt;
165+
sign_bit = (regval >> 15) & 0x01;
166+
*val = (((long)(((u16)regval) & 0x7FFF) - (sign_bit * 32768)) *
167+
1250L * (long)data->gain) / (long)data->shunt;
166168
break;
167169
default:
168170
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)