mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nhat Tran <nhat.tranminhk2@gmail.com>
To: "Rafael J . Wysocki" <rafael@kernel.org>,
	Daniel Lezcano <daniel.lezcano@kernel.org>
Cc: Zhang Rui <rui.zhang@intel.com>,
	Lukasz Luba <lukasz.luba@arm.com>,
	Biju Das <biju.das.jz@bp.renesas.com>,
	Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>,
	linux-pm@vger.kernel.org, linux-renesas-soc@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Nhat Tran <nhat.tranminhk2@gmail.com>
Subject: [PATCH] thermal/drivers/rzg2l: Fix precision loss in temperature calculation
Date: Mon, 16 Nov 2026 13:29:09 +0700	[thread overview]
Message-ID: <20261116062909.36618-1-nhat.tranminhk2@gmail.com> (raw)

According to the RZ/G2L hardware user's manual (Rev.1.50, section
44.4.4 Procedure for Measuring the Temperature) the temperature is
computed as

  Tj = (DSENSOR - OTPTSUTRIM1) * (165 / (OTPTSUTRIM0 - OTPTSUTRIM1)) - 40

The driver follows the same grouping with integer arithmetic, so the
fractional part of 165000 / (calib0 - calib1) is truncated before it is
multiplied by (dsensor - calib1). Above -40 °C this always makes the
driver report a lower value than the real one, and the error grows
linearly with the temperature, so trip points are crossed later than
configured.

On an RZ/G2L SMARC EVK with OTP calibration values calib0 = 2902 and
calib1 = 586, the slope 71.24 is truncated to 71. The computed value is
313 m°C too low at 52 °C and 351 m°C too low at 63 °C, e.g. 62382 m°C
instead of 62733 m°C, which is reported as 62500 instead of 63000 after
rounding. With the default calibration values (3148, 503) the
calculated error is about 1 °C at 125 °C.

Multiply before dividing, as rzg3s_thermal already does, and use signed
arithmetic so that temperatures below -40 °C are still handled. The
intermediate value is at most 4095 * 165000, which fits in a signed
32-bit integer.

Fixes: 673c68bd4839 ("thermal/drivers: Add TSU driver for RZ/G2L")
Signed-off-by: Nhat Tran <nhat.tranminhk2@gmail.com>
---
 drivers/thermal/renesas/rzg2l_thermal.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/thermal/renesas/rzg2l_thermal.c b/drivers/thermal/renesas/rzg2l_thermal.c
index 90e2ba378a4b..3295eef12001 100644
--- a/drivers/thermal/renesas/rzg2l_thermal.c
+++ b/drivers/thermal/renesas/rzg2l_thermal.c
@@ -75,8 +75,8 @@ static inline void rzg2l_thermal_write(struct rzg2l_thermal_priv *priv, u32 reg,
 static int rzg2l_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
 {
 	struct rzg2l_thermal_priv *priv = thermal_zone_device_priv(tz);
-	u32 result = 0, dsensor, ts_code_ave;
-	int val, i;
+	u32 result = 0, ts_code_ave;
+	int dsensor, val, i;
 
 	for (i = 0; i < TS_CODE_CAP_TIMES ; i++) {
 		/*
@@ -105,8 +105,8 @@ static int rzg2l_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
 	 * Tj = (dsensor − calib1) * 165/ (calib0 − calib1) − 40
 	 * where calib0 and calib1 are the calibration values.
 	 */
-	val = ((dsensor - priv->calib1) * (MCELSIUS(165) /
-		(priv->calib0 - priv->calib1))) - MCELSIUS(40);
+	val = (dsensor - (int)priv->calib1) * MCELSIUS(165) /
+	      (int)(priv->calib0 - priv->calib1) - MCELSIUS(40);
 
 	*temp = roundup(val, RZG2L_THERMAL_GRAN);
 
-- 
2.25.1


                 reply	other threads:[~2026-11-16  6:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261116062909.36618-1-nhat.tranminhk2@gmail.com \
    --to=nhat.tranminhk2@gmail.com \
    --cc=biju.das.jz@bp.renesas.com \
    --cc=daniel.lezcano@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=prabhakar.mahadev-lad.rj@bp.renesas.com \
    --cc=rafael@kernel.org \
    --cc=rui.zhang@intel.com \
    /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

all inboxes | Powered by JetHome®