Skip to content

Commit 0b2cca4

Browse files
groeckgregkh
authored andcommitted
hwmon: (ina238) Correctly clamp power limits
[ Upstream commit c262357 ] ina238_write_power() was attempting to clamp the user input but was throwing away the result. Ensure that we clamp the value to the appropriate range before it is converted into a register value. Fixes: 0d9f596 ("hwmon: (ina238) Modify the calculation formula to adapt to different chips") Cc: Wenliang Yan <wenliang202407@163.com> Cc: Chris Packham <chris.packham@alliedtelesis.co.nz> Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 6bc941d commit 0b2cca4

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

drivers/hwmon/ina238.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,10 @@ static int ina238_write_power(struct device *dev, u32 attr, long val)
426426
* Unsigned postive values. Compared against the 24-bit power register,
427427
* lower 8-bits are truncated. Same conversion to/from uW as POWER
428428
* register.
429+
* The first clamp_val() is to establish a baseline to avoid overflows.
429430
*/
430-
regval = clamp_val(val, 0, LONG_MAX);
431-
regval = div_u64(val * 4 * 100 * data->rshunt, data->config->power_calculate_factor *
431+
regval = clamp_val(val, 0, LONG_MAX / 2);
432+
regval = div_u64(regval * 4 * 100 * data->rshunt, data->config->power_calculate_factor *
432433
1000ULL * INA238_FIXED_SHUNT * data->gain);
433434
regval = clamp_val(regval >> 8, 0, U16_MAX);
434435

0 commit comments

Comments
 (0)