thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc
Subsystems (3)
PATCH RESEND 1/1 thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc
Use clamp() to simplify bcm2835_thermal_temp2adc() and improve its readability. Explicitly cast BIT() to int to prevent a signedness error.
--- drivers/thermal/broadcom/bcm2835_thermal.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/thermal/broadcom/bcm2835_thermal.c +2 -6
@@ -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)
Quoted 4 lines · Thorsten Blum
Use
clamp()to simplifybcm2835_thermal_temp2adc()and improve its readability. Explicitly castBIT()to int to prevent a signedness error.Signed-off-by Thorsten Blum <[email protected]>
--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Quoted 21 lines · On Mon, 5 Jan 2026 13:13:03 +0100 Thorsten Blum <[email protected]>…
Use
clamp()to simplifybcm2835_thermal_temp2adc()and improve its readability. Explicitly castBIT()to int to prevent a signedness error.Signed-off-by Thorsten Blum <[email protected]>--- drivers/thermal/broadcom/bcm2835_thermal.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)drivers/thermal/broadcom/bcm2835_thermal.c +1
-0@@ -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>
drivers/thermal/broadcom/bcm2835_thermal.c static int bcm2835_thermal_temp2adc(int temp, int offset, int slope) · line 84
Quoted 10 lines
drivers/thermal/broadcom/bcm2835_thermal.c +1
-6@@ -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
Quoted 4 lines
drivers/thermal/broadcom/bcm2835_thermal.c +0
-0}static int bcm2835_thermal_get_temp (struct thermal_zone_device *tz,int *temp)
Daniel Lezcano
Applieddrivers/thermal/broadcom/bcm2835_thermal.c
Quoted 11 lines · Thorsten Blum
Use
clamp()to simplifybcm2835_thermal_temp2adc()and improve its readability. Explicitly castBIT()to int to prevent a signedness error.Signed-off-by Thorsten Blum <[email protected]>--- drivers/thermal/broadcom/bcm2835_thermal.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)drivers/thermal/broadcom/bcm2835_thermal.c +0
-1--- a/drivers/thermal/broadcom/bcm2835_thermal.c
Applied, thanks
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog