linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: (adt7475) Make volt2reg return same reg as reg2volt input
@ 2019-12-05 22:54 Luuk Paulussen
  2019-12-05 22:57 ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Luuk Paulussen @ 2019-12-05 22:54 UTC (permalink / raw)
  To: Jean Delvare, Guenter Roeck; +Cc: linux-hwmon, linux-kernel, Luuk Paulussen

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 <luuk.paulussen@alliedtelesis.co.nz>
---
 drivers/hwmon/adt7475.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
index 6c64d50c9aae..5eed7dd2f16d 100644
--- a/drivers/hwmon/adt7475.c
+++ b/drivers/hwmon/adt7475.c
@@ -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);
 }
 
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: (adt7475) Make volt2reg return same reg as reg2volt input
  2019-12-05 22:54 [PATCH] hwmon: (adt7475) Make volt2reg return same reg as reg2volt input Luuk Paulussen
@ 2019-12-05 22:57 ` Guenter Roeck
  2019-12-05 23:16   ` [PATCH v2] " Luuk Paulussen
  0 siblings, 1 reply; 4+ messages in thread
From: Guenter Roeck @ 2019-12-05 22:57 UTC (permalink / raw)
  To: Luuk Paulussen; +Cc: Jean Delvare, linux-hwmon, linux-kernel

On Fri, Dec 06, 2019 at 11:54:30AM +1300, Luuk Paulussen wrote:
> 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 <luuk.paulussen@alliedtelesis.co.nz>
> ---
>  drivers/hwmon/adt7475.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
> index 6c64d50c9aae..5eed7dd2f16d 100644
> --- a/drivers/hwmon/adt7475.c
> +++ b/drivers/hwmon/adt7475.c
> @@ -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);

Unnecessary ( )

>  	else
> -		reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
> +		reg = DIV_ROUND_CLOSEST((volt * r[1] * 1024),
> +					((r[0] + r[1]) * 2250));

More unnecessary ( )

Otherwise good catch.

Guenter

>  	return clamp_val(reg, 0, 1023) & (0xff << 2);
>  }
>  
> -- 
> 2.24.0
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] hwmon: (adt7475) Make volt2reg return same reg as reg2volt input
  2019-12-05 22:57 ` Guenter Roeck
@ 2019-12-05 23:16   ` Luuk Paulussen
  2019-12-06 14:37     ` Guenter Roeck
  0 siblings, 1 reply; 4+ messages in thread
From: Luuk Paulussen @ 2019-12-05 23:16 UTC (permalink / raw)
  To: Jean Delvare, Guenter Roeck; +Cc: linux-hwmon, linux-kernel, Luuk Paulussen

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 <luuk.paulussen@alliedtelesis.co.nz>
---
 changes in v2:
 - remove unnecessary braces.
 drivers/hwmon/adt7475.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
index 6c64d50c9aae..01c2eeb02aa9 100644
--- a/drivers/hwmon/adt7475.c
+++ b/drivers/hwmon/adt7475.c
@@ -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);
 }
 
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] hwmon: (adt7475) Make volt2reg return same reg as reg2volt input
  2019-12-05 23:16   ` [PATCH v2] " Luuk Paulussen
@ 2019-12-06 14:37     ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2019-12-06 14:37 UTC (permalink / raw)
  To: Luuk Paulussen; +Cc: Jean Delvare, linux-hwmon, linux-kernel

On Fri, Dec 06, 2019 at 12:16:59PM +1300, Luuk Paulussen wrote:
> 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 <luuk.paulussen@alliedtelesis.co.nz>

Applied.

Thanks,
Guenter

> ---
>  changes in v2:
>  - remove unnecessary braces.
>  drivers/hwmon/adt7475.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c
> index 6c64d50c9aae..01c2eeb02aa9 100644
> --- a/drivers/hwmon/adt7475.c
> +++ b/drivers/hwmon/adt7475.c
> @@ -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);
>  }
>  

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-06 14:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-05 22:54 [PATCH] hwmon: (adt7475) Make volt2reg return same reg as reg2volt input Luuk Paulussen
2019-12-05 22:57 ` Guenter Roeck
2019-12-05 23:16   ` [PATCH v2] " Luuk Paulussen
2019-12-06 14:37     ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).