From: David Laight <david.laight.linux@gmail.com>
To: Thorsten Blum <thorsten.blum@linux.dev>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
Daniel Lezcano <daniel.lezcano@linaro.org>,
Zhang Rui <rui.zhang@intel.com>,
Lukasz Luba <lukasz.luba@arm.com>,
Florian Fainelli <florian.fainelli@broadcom.com>,
Broadcom internal kernel review list
<bcm-kernel-feedback-list@broadcom.com>,
Ray Jui <rjui@broadcom.com>,
Scott Branden <sbranden@broadcom.com>,
Luca Ceresoli <luca.ceresoli@bootlin.com>,
Geert Uytterhoeven <geert+renesas@glider.be>,
linux-pm@vger.kernel.org, linux-rpi-kernel@lists.infradead.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH RESEND] thermal: broadcom: Use clamp to simplify bcm2835_thermal_temp2adc
Date: Mon, 5 Jan 2026 22:27:55 +0000 [thread overview]
Message-ID: <20260105222755.305787f7@pumpkin> (raw)
In-Reply-To: <20260105121308.1761-1-thorsten.blum@linux.dev>
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)
>
next prev parent reply other threads:[~2026-01-05 22:28 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
2026-01-21 16:48 ` Daniel Lezcano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260105222755.305787f7@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=daniel.lezcano@linaro.org \
--cc=florian.fainelli@broadcom.com \
--cc=geert+renesas@glider.be \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-rpi-kernel@lists.infradead.org \
--cc=luca.ceresoli@bootlin.com \
--cc=lukasz.luba@arm.com \
--cc=rafael@kernel.org \
--cc=rjui@broadcom.com \
--cc=rui.zhang@intel.com \
--cc=sbranden@broadcom.com \
--cc=thorsten.blum@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox