From: Yikai Tsai yikai.tsai.wiwynn@gmail.com
[ Upstream commit b2446a16dbf2347a07af0cf994ca36576d94df77 ]
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 Signed-off-by: Sasha Levin sashal@kernel.org ---
Now let me check for similar current register handling issues in other hwmon drivers. Let me look at the commit to understand the exact issue:
Looking at the commit changes, I can see that:
**Before (broken):** ```c *val = ((long)regval * 1250L * (long)data->gain) / (long)data->shunt; ```
**After (fixed):** ```c u16 sign_bit; sign_bit = (regval >> 15) & 0x01; *val = (((long)(((u16)regval) & 0x7FFF) - (sign_bit * 32768)) * 1250L * (long)data->gain) / (long)data->shunt; ```
The issue is that the ISL28022 current register is a 16-bit two's complement signed integer where bit 15 represents the sign bit (equivalent to -32768), but the original code was treating it as an unsigned value.
Let me analyze based on the established criteria:
**Analysis:**
**YES - This commit should be backported to stable kernel trees.**
**Detailed Analysis:**
1. **Bug Fix Nature**: This is a clear bug fix that addresses incorrect current readings for negative values. According to the ISL28022 datasheet mentioned in the commit, the current register (04h) is a 16-bit two's complement value where bit 15 represents -32768, but the driver was treating it as unsigned.
2. **User Impact**: This bug significantly affects users who rely on accurate current measurements, especially when current flows in the negative direction. The bug would cause: - Incorrect reporting of negative currents as large positive values - Complete failure to detect reverse current flow - Invalid power calculations that depend on current readings
3. **Code Analysis**: - **Problem**: Lines 164-165 in `/home/sasha/linux/drivers/hwmon/isl28022.c:164-165` treat `regval` as unsigned, causing negative currents to be interpreted as large positive values - **Solution**: The fix properly implements sign extension by extracting bit 15 as the sign bit and manually computing the two's complement value: `(value & 0x7FFF) - (sign_bit * 32768)` - **Consistency**: The fix follows the exact same pattern already used in the shunt voltage reading function (`isl28022_read_in`) at lines 120-122, showing this is the established correct approach in this driver
4. **Risk Assessment**: - **Minimal Risk**: The change is very small and localized to the current reading calculation - **Well-Tested Pattern**: Uses the identical sign handling logic already proven correct in the shunt voltage function - **No Side Effects**: Only affects current register interpretation, doesn't change any other functionality
5. **Driver Context**: - The ISL28022 driver was introduced in October 2024 (commit 39671a14df4f), making it relatively new - The driver is hardware-specific and contained within the hwmon subsystem - This follows the same pattern as other successful backports shown in the examples
6. **Comparison to Similar Commits**: This commit is nearly identical to the reference examples that were marked for backporting: - **Similar Commit #1** (ina2xx): Fixed sign extension for current register by adding `(s16)` cast - **Similar Commit #3** (tmp513): Fixed sign extension with `sign_extend32(regval, 15)` and type casting - **Similar Commit #4** (nct7904): Fixed sign extension by converting to `s8` - **Similar Commit #5** (ina3221): Fixed shunt sum register bit shifting
7. **Critical Subsystem**: Hardware monitoring is critical for system health monitoring, thermal management, and power management. Incorrect current readings could lead to improper system behavior or failure to detect dangerous conditions.
**Conclusion**: This is a textbook stable tree candidate - it fixes a clear functional bug with minimal risk, follows established patterns, and addresses a real user-impacting issue in hardware monitoring functionality.
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 1fb9864635db9..1b4fb0824d6c0 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;