Skip to content

Commit

Permalink
hwmon: (adt7475) Make volt2reg return same reg as reg2volt input
Browse files Browse the repository at this point in the history
reg2volt returns the voltage that matches a given register value.
Converting this back the other way with volt2reg didn't return the same
register value because it used truncation instead of rounding.

This meant that values read from sysfs could not be written back to sysfs
to set back the same register value.

With this change, volt2reg will return the same value for every voltage
previously returned by reg2volt (for the set of possible input values)

Signed-off-by: Luuk Paulussen <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
cc: [email protected]
Signed-off-by: Guenter Roeck <[email protected]>
  • Loading branch information
luukp authored and groeck committed Jan 17, 2020
1 parent e51a7dd commit cf3ca18
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions drivers/hwmon/adt7475.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,10 @@ static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
long reg;

if (bypass_attn & (1 << channel))
reg = (volt * 1024) / 2250;
reg = DIV_ROUND_CLOSEST(volt * 1024, 2250);
else
reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024,
(r[0] + r[1]) * 2250);
return clamp_val(reg, 0, 1023) & (0xff << 2);
}

Expand Down

0 comments on commit cf3ca18

Please sign in to comment.