linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND] thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc
@ 2026-01-05 12:13 Thorsten Blum
  2026-01-05 17:34 ` Luca Ceresoli
  2026-01-05 22:27 ` David Laight
  0 siblings, 2 replies; 3+ messages in thread
From: Thorsten Blum @ 2026-01-05 12:13 UTC (permalink / raw)
  To: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	Florian Fainelli, Broadcom internal kernel review list, Ray Jui,
	Scott Branden, Luca Ceresoli, Geert Uytterhoeven
  Cc: Thorsten Blum, linux-pm, linux-rpi-kernel, linux-arm-kernel,
	linux-kernel

Use clamp() to simplify bcm2835_thermal_temp2adc() and improve its
readability. Explicitly cast BIT() to int to prevent a signedness error.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/thermal/broadcom/bcm2835_thermal.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/drivers/thermal/broadcom/bcm2835_thermal.c b/drivers/thermal/broadcom/bcm2835_thermal.c
index 685a5aee5e0d..c5105dfc6ec9 100644
--- a/drivers/thermal/broadcom/bcm2835_thermal.c
+++ b/drivers/thermal/broadcom/bcm2835_thermal.c
@@ -11,6 +11,7 @@
 #include <linux/err.h>
 #include <linux/io.h>
 #include <linux/kernel.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
@@ -80,12 +81,7 @@ static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
 	temp -= offset;
 	temp /= slope;
 
-	if (temp < 0)
-		temp = 0;
-	if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
-		temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
-
-	return temp;
+	return clamp(temp, 0, (int)BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1);
 }
 
 static int bcm2835_thermal_get_temp(struct thermal_zone_device *tz, int *temp)


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

* Re: [PATCH RESEND] thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc
  2026-01-05 12:13 [PATCH RESEND] thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc Thorsten Blum
@ 2026-01-05 17:34 ` Luca Ceresoli
  2026-01-05 22:27 ` David Laight
  1 sibling, 0 replies; 3+ messages in thread
From: Luca Ceresoli @ 2026-01-05 17:34 UTC (permalink / raw)
  To: Thorsten Blum, Rafael J. Wysocki, Daniel Lezcano, Zhang Rui,
	Lukasz Luba, Florian Fainelli,
	Broadcom internal kernel review list, Ray Jui, Scott Branden,
	Geert Uytterhoeven
  Cc: linux-pm, linux-rpi-kernel, linux-arm-kernel, linux-kernel

On Mon Jan 5, 2026 at 1:13 PM CET, Thorsten Blum wrote:
> Use clamp() to simplify bcm2835_thermal_temp2adc() and improve its
> readability. Explicitly cast BIT() to int to prevent a signedness error.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>

Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>

--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


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

* Re: [PATCH RESEND] thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc
  2026-01-05 12:13 [PATCH RESEND] thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc Thorsten Blum
  2026-01-05 17:34 ` Luca Ceresoli
@ 2026-01-05 22:27 ` David Laight
  1 sibling, 0 replies; 3+ messages in thread
From: David Laight @ 2026-01-05 22:27 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
	Florian Fainelli, Broadcom internal kernel review list, Ray Jui,
	Scott Branden, Luca Ceresoli, Geert Uytterhoeven, linux-pm,
	linux-rpi-kernel, linux-arm-kernel, linux-kernel

On Mon,  5 Jan 2026 13:13:03 +0100
Thorsten Blum <thorsten.blum@linux.dev> wrote:

> Use clamp() to simplify bcm2835_thermal_temp2adc() and improve its
> readability. Explicitly cast BIT() to int to prevent a signedness error.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  drivers/thermal/broadcom/bcm2835_thermal.c | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/thermal/broadcom/bcm2835_thermal.c b/drivers/thermal/broadcom/bcm2835_thermal.c
> index 685a5aee5e0d..c5105dfc6ec9 100644
> --- a/drivers/thermal/broadcom/bcm2835_thermal.c
> +++ b/drivers/thermal/broadcom/bcm2835_thermal.c
> @@ -11,6 +11,7 @@
>  #include <linux/err.h>
>  #include <linux/io.h>
>  #include <linux/kernel.h>
> +#include <linux/minmax.h>
>  #include <linux/module.h>
>  #include <linux/of.h>
>  #include <linux/of_address.h>
> @@ -80,12 +81,7 @@ static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
>  	temp -= offset;
>  	temp /= slope;
>  
> -	if (temp < 0)
> -		temp = 0;
> -	if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
> -		temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
> -
> -	return temp;
> +	return clamp(temp, 0, (int)BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1);

Hmmm....
I wonder if I can get 'statically_true(lo >= 0)' into the type check so that
cast isn't necessary.
	signed_val < 0 ? 0 : signed_val > unsigned_val ? unsigned_val : signed_val
is fine.
Would mean swapping the order of the tests - which shouldn't break anything.
But will need a full audit - various bits of code have relied on the order
of the comparisons.

	David

>  }
>  
>  static int bcm2835_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
> 



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

end of thread, other threads:[~2026-01-05 22:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-05 12:13 [PATCH RESEND] thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc Thorsten Blum
2026-01-05 17:34 ` Luca Ceresoli
2026-01-05 22:27 ` David Laight

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).