Skip to content

Commit c262357

Browse files
committed
hwmon: (ina238) Correctly clamp power limits
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 <[email protected]> Cc: Chris Packham <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent bd7e7bc commit c262357

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
@@ -517,9 +517,10 @@ static int ina238_write_power(struct device *dev, u32 attr, long val)
517517
* Unsigned postive values. Compared against the 24-bit power register,
518518
* lower 8-bits are truncated. Same conversion to/from uW as POWER
519519
* register.
520+
* The first clamp_val() is to establish a baseline to avoid overflows.
520521
*/
521-
regval = clamp_val(val, 0, LONG_MAX);
522-
regval = div_u64(val * 4 * 100 * data->rshunt, data->config->power_calculate_factor *
522+
regval = clamp_val(val, 0, LONG_MAX / 2);
523+
regval = div_u64(regval * 4 * 100 * data->rshunt, data->config->power_calculate_factor *
523524
1000ULL * INA238_FIXED_SHUNT * data->gain);
524525
regval = clamp_val(regval >> 8, 0, U16_MAX);
525526

0 commit comments

Comments
 (0)