Skip to content

Commit 9d8593f

Browse files
ppailletdlezcano
authored andcommitted
thermal: stm32: Improve temperature computing
Change the way of computing to avoid rounds by 1 or 2 degrees. Also simplify the sampling time management that is hard-coded to maximum value during probe. Signed-off-by: Pascal Paillet <[email protected]> Signed-off-by: Daniel Lezcano <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent dd4c391 commit 9d8593f

File tree

1 file changed

+14
-44
lines changed

1 file changed

+14
-44
lines changed

drivers/thermal/st/stm_thermal.c

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959

6060
/* Less significant bit position definitions */
6161
#define TS1_T0_POS 16
62-
#define TS1_SMP_TIME_POS 16
6362
#define TS1_HITTHD_POS 16
6463
#define TS1_LITTHD_POS 0
6564
#define HSREF_CLK_DIV_POS 24
@@ -83,15 +82,10 @@
8382
#define ONE_MHZ 1000000
8483
#define POLL_TIMEOUT 5000
8584
#define STARTUP_TIME 40
86-
#define TS1_T0_VAL0 30
87-
#define TS1_T0_VAL1 130
85+
#define TS1_T0_VAL0 30000 /* 30 celsius */
86+
#define TS1_T0_VAL1 130000 /* 130 celsius */
8887
#define NO_HW_TRIG 0
89-
90-
/* The Thermal Framework expects millidegrees */
91-
#define mcelsius(temp) ((temp) * 1000)
92-
93-
/* The Sensor expects oC degrees */
94-
#define celsius(temp) ((temp) / 1000)
88+
#define SAMPLING_TIME 15
9589

9690
struct stm_thermal_sensor {
9791
struct device *dev;
@@ -280,27 +274,17 @@ static int stm_thermal_calculate_threshold(struct stm_thermal_sensor *sensor,
280274
int temp, u32 *th)
281275
{
282276
int freqM;
283-
u32 sampling_time;
284-
285-
/* Retrieve the number of periods to sample */
286-
sampling_time = (readl_relaxed(sensor->base + DTS_CFGR1_OFFSET) &
287-
TS1_SMP_TIME_MASK) >> TS1_SMP_TIME_POS;
288277

289278
/* Figure out the CLK_PTAT frequency for a given temperature */
290-
freqM = ((temp - sensor->t0) * sensor->ramp_coeff)
291-
+ sensor->fmt0;
292-
293-
dev_dbg(sensor->dev, "%s: freqM for threshold = %d Hz",
294-
__func__, freqM);
279+
freqM = ((temp - sensor->t0) * sensor->ramp_coeff) / 1000 +
280+
sensor->fmt0;
295281

296282
/* Figure out the threshold sample number */
297-
*th = clk_get_rate(sensor->clk);
283+
*th = clk_get_rate(sensor->clk) * SAMPLING_TIME / freqM;
298284
if (!*th)
299285
return -EINVAL;
300286

301-
*th = *th / freqM;
302-
303-
*th *= sampling_time;
287+
dev_dbg(sensor->dev, "freqM=%d Hz, threshold=0x%x", freqM, *th);
304288

305289
return 0;
306290
}
@@ -368,40 +352,26 @@ static int stm_thermal_set_trips(void *data, int low, int high)
368352
static int stm_thermal_get_temp(void *data, int *temp)
369353
{
370354
struct stm_thermal_sensor *sensor = data;
371-
u32 sampling_time;
355+
u32 periods;
372356
int freqM, ret;
373357

374358
if (sensor->mode != THERMAL_DEVICE_ENABLED)
375359
return -EAGAIN;
376360

377-
/* Retrieve the number of samples */
378-
ret = readl_poll_timeout(sensor->base + DTS_DR_OFFSET, freqM,
379-
(freqM & TS1_MFREQ_MASK), STARTUP_TIME,
380-
POLL_TIMEOUT);
381-
361+
/* Retrieve the number of periods sampled */
362+
ret = readl_relaxed_poll_timeout(sensor->base + DTS_DR_OFFSET, periods,
363+
(periods & TS1_MFREQ_MASK),
364+
STARTUP_TIME, POLL_TIMEOUT);
382365
if (ret)
383366
return ret;
384367

385-
if (!freqM)
386-
return -ENODATA;
387-
388-
/* Retrieve the number of periods sampled */
389-
sampling_time = (readl_relaxed(sensor->base + DTS_CFGR1_OFFSET) &
390-
TS1_SMP_TIME_MASK) >> TS1_SMP_TIME_POS;
391-
392-
/* Figure out the number of samples per period */
393-
freqM /= sampling_time;
394-
395368
/* Figure out the CLK_PTAT frequency */
396-
freqM = clk_get_rate(sensor->clk) / freqM;
369+
freqM = (clk_get_rate(sensor->clk) * SAMPLING_TIME) / periods;
397370
if (!freqM)
398371
return -EINVAL;
399372

400-
dev_dbg(sensor->dev, "%s: freqM=%d\n", __func__, freqM);
401-
402373
/* Figure out the temperature in mili celsius */
403-
*temp = mcelsius(sensor->t0 + ((freqM - sensor->fmt0) /
404-
sensor->ramp_coeff));
374+
*temp = (freqM - sensor->fmt0) * 1000 / sensor->ramp_coeff + sensor->t0;
405375

406376
return 0;
407377
}

0 commit comments

Comments
 (0)