thermal/drivers/rzg2l: Fix precision loss in temperature calculation
PATCH 1/1 thermal/drivers/rzg2l: Fix precision loss in temperature calculation
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.
--- drivers/thermal/renesas/rzg2l_thermal.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/thermal/renesas/rzg2l_thermal.c +4 -4
@@ -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 );
Signature · 1 line
2.25.1