1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
4 * Author: David Hernandez Sanchez <david.hernandezsanchez@st.com> for
9 #include <linux/clk-provider.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/thermal.h>
22 #include "../thermal_core.h"
23 #include "../thermal_hwmon.h"
25 /* DTS register offsets */
26 #define DTS_CFGR1_OFFSET 0x0
27 #define DTS_T0VALR1_OFFSET 0x8
28 #define DTS_RAMPVALR_OFFSET 0X10
29 #define DTS_ITR1_OFFSET 0x14
30 #define DTS_DR_OFFSET 0x1C
31 #define DTS_SR_OFFSET 0x20
32 #define DTS_ITENR_OFFSET 0x24
33 #define DTS_ICIFR_OFFSET 0x28
35 /* DTS_CFGR1 register mask definitions */
36 #define HSREF_CLK_DIV_MASK GENMASK(30, 24)
37 #define TS1_SMP_TIME_MASK GENMASK(19, 16)
38 #define TS1_INTRIG_SEL_MASK GENMASK(11, 8)
40 /* DTS_T0VALR1 register mask definitions */
41 #define TS1_T0_MASK GENMASK(17, 16)
42 #define TS1_FMT0_MASK GENMASK(15, 0)
44 /* DTS_RAMPVALR register mask definitions */
45 #define TS1_RAMP_COEFF_MASK GENMASK(15, 0)
47 /* DTS_ITR1 register mask definitions */
48 #define TS1_HITTHD_MASK GENMASK(31, 16)
49 #define TS1_LITTHD_MASK GENMASK(15, 0)
51 /* DTS_DR register mask definitions */
52 #define TS1_MFREQ_MASK GENMASK(15, 0)
54 /* DTS_ITENR register mask definitions */
55 #define ITENR_MASK (GENMASK(2, 0) | GENMASK(6, 4))
57 /* DTS_ICIFR register mask definitions */
58 #define ICIFR_MASK (GENMASK(2, 0) | GENMASK(6, 4))
60 /* Less significant bit position definitions */
62 #define TS1_HITTHD_POS 16
63 #define TS1_LITTHD_POS 0
64 #define HSREF_CLK_DIV_POS 24
66 /* DTS_CFGR1 bit definitions */
68 #define TS1_START BIT(4)
69 #define REFCLK_SEL BIT(20)
70 #define REFCLK_LSE REFCLK_SEL
71 #define Q_MEAS_OPT BIT(21)
72 #define CALIBRATION_CONTROL Q_MEAS_OPT
74 /* DTS_SR bit definitions */
75 #define TS_RDY BIT(15)
76 /* Bit definitions below are common for DTS_SR, DTS_ITENR and DTS_CIFR */
77 #define HIGH_THRESHOLD BIT(2)
78 #define LOW_THRESHOLD BIT(1)
82 #define ONE_MHZ 1000000
83 #define POLL_TIMEOUT 5000
84 #define STARTUP_TIME 40
85 #define TS1_T0_VAL0 30000 /* 30 celsius */
86 #define TS1_T0_VAL1 130000 /* 130 celsius */
88 #define SAMPLING_TIME 15
90 struct stm_thermal_sensor
{
92 struct thermal_zone_device
*th_dev
;
93 enum thermal_device_mode mode
;
95 unsigned int low_temp_enabled
;
96 unsigned int high_temp_enabled
;
99 int t0
, fmt0
, ramp_coeff
;
102 static int stm_enable_irq(struct stm_thermal_sensor
*sensor
)
106 dev_dbg(sensor
->dev
, "low:%d high:%d\n", sensor
->low_temp_enabled
,
107 sensor
->high_temp_enabled
);
109 /* Disable IT generation for low and high thresholds */
110 value
= readl_relaxed(sensor
->base
+ DTS_ITENR_OFFSET
);
111 value
&= ~(LOW_THRESHOLD
| HIGH_THRESHOLD
);
113 if (sensor
->low_temp_enabled
)
114 value
|= HIGH_THRESHOLD
;
116 if (sensor
->high_temp_enabled
)
117 value
|= LOW_THRESHOLD
;
119 /* Enable interrupts */
120 writel_relaxed(value
, sensor
->base
+ DTS_ITENR_OFFSET
);
125 static irqreturn_t
stm_thermal_irq_handler(int irq
, void *sdata
)
127 struct stm_thermal_sensor
*sensor
= sdata
;
129 dev_dbg(sensor
->dev
, "sr:%d\n",
130 readl_relaxed(sensor
->base
+ DTS_SR_OFFSET
));
132 thermal_zone_device_update(sensor
->th_dev
, THERMAL_EVENT_UNSPECIFIED
);
134 stm_enable_irq(sensor
);
136 /* Acknoledge all DTS irqs */
137 writel_relaxed(ICIFR_MASK
, sensor
->base
+ DTS_ICIFR_OFFSET
);
142 static int stm_sensor_power_on(struct stm_thermal_sensor
*sensor
)
148 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
150 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
153 * The DTS block can be enabled by setting TSx_EN bit in
154 * DTS_CFGRx register. It requires a startup time of
155 * 40μs. Use 5 ms as arbitrary timeout.
157 ret
= readl_poll_timeout(sensor
->base
+ DTS_SR_OFFSET
,
158 value
, (value
& TS_RDY
),
159 STARTUP_TIME
, POLL_TIMEOUT
);
163 /* Start continuous measuring */
164 value
= readl_relaxed(sensor
->base
+
167 writel_relaxed(value
, sensor
->base
+
170 sensor
->mode
= THERMAL_DEVICE_ENABLED
;
175 static int stm_sensor_power_off(struct stm_thermal_sensor
*sensor
)
179 sensor
->mode
= THERMAL_DEVICE_DISABLED
;
182 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
184 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
186 /* Ensure stop is taken into account */
187 usleep_range(STARTUP_TIME
, POLL_TIMEOUT
);
190 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
192 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
194 /* Ensure disable is taken into account */
195 return readl_poll_timeout(sensor
->base
+ DTS_SR_OFFSET
, value
,
197 STARTUP_TIME
, POLL_TIMEOUT
);
200 static int stm_thermal_calibration(struct stm_thermal_sensor
*sensor
)
205 /* Figure out prescaler value for PCLK during calibration */
206 clk_freq
= clk_get_rate(sensor
->clk
);
213 while (prescaler
<= clk_freq
)
217 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
219 /* Clear prescaler */
220 value
&= ~HSREF_CLK_DIV_MASK
;
222 /* Set prescaler. pclk_freq/prescaler < 1MHz */
223 value
|= (prescaler
<< HSREF_CLK_DIV_POS
);
225 /* Select PCLK as reference clock */
226 value
&= ~REFCLK_SEL
;
228 /* Set maximal sampling time for better precision */
229 value
|= TS1_SMP_TIME_MASK
;
231 /* Measure with calibration */
232 value
&= ~CALIBRATION_CONTROL
;
235 value
&= ~TS1_INTRIG_SEL_MASK
;
238 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
243 /* Fill in DTS structure with factory sensor values */
244 static int stm_thermal_read_factory_settings(struct stm_thermal_sensor
*sensor
)
246 /* Retrieve engineering calibration temperature */
247 sensor
->t0
= readl_relaxed(sensor
->base
+ DTS_T0VALR1_OFFSET
) &
250 sensor
->t0
= TS1_T0_VAL0
;
252 sensor
->t0
= TS1_T0_VAL1
;
254 /* Retrieve fmt0 and put it on Hz */
255 sensor
->fmt0
= ADJUST
* (readl_relaxed(sensor
->base
+
256 DTS_T0VALR1_OFFSET
) & TS1_FMT0_MASK
);
258 /* Retrieve ramp coefficient */
259 sensor
->ramp_coeff
= readl_relaxed(sensor
->base
+ DTS_RAMPVALR_OFFSET
) &
262 if (!sensor
->fmt0
|| !sensor
->ramp_coeff
) {
263 dev_err(sensor
->dev
, "%s: wrong setting\n", __func__
);
267 dev_dbg(sensor
->dev
, "%s: T0 = %doC, FMT0 = %dHz, RAMP_COEFF = %dHz/oC",
268 __func__
, sensor
->t0
, sensor
->fmt0
, sensor
->ramp_coeff
);
273 static int stm_thermal_calculate_threshold(struct stm_thermal_sensor
*sensor
,
278 /* Figure out the CLK_PTAT frequency for a given temperature */
279 freqM
= ((temp
- sensor
->t0
) * sensor
->ramp_coeff
) / 1000 +
282 /* Figure out the threshold sample number */
283 *th
= clk_get_rate(sensor
->clk
) * SAMPLING_TIME
/ freqM
;
287 dev_dbg(sensor
->dev
, "freqM=%d Hz, threshold=0x%x", freqM
, *th
);
292 /* Disable temperature interrupt */
293 static int stm_disable_irq(struct stm_thermal_sensor
*sensor
)
297 /* Disable IT generation */
298 value
= readl_relaxed(sensor
->base
+ DTS_ITENR_OFFSET
);
299 value
&= ~ITENR_MASK
;
300 writel_relaxed(value
, sensor
->base
+ DTS_ITENR_OFFSET
);
305 static int stm_thermal_set_trips(void *data
, int low
, int high
)
307 struct stm_thermal_sensor
*sensor
= data
;
311 dev_dbg(sensor
->dev
, "set trips %d <--> %d\n", low
, high
);
313 /* Erase threshold content */
314 itr1
= readl_relaxed(sensor
->base
+ DTS_ITR1_OFFSET
);
315 itr1
&= ~(TS1_LITTHD_MASK
| TS1_HITTHD_MASK
);
318 * Disable low-temp if "low" is too small. As per thermal framework
319 * API, we use -INT_MAX rather than INT_MIN.
322 if (low
> -INT_MAX
) {
323 sensor
->low_temp_enabled
= 1;
324 /* add 0.5 of hysteresis due to measurement error */
325 ret
= stm_thermal_calculate_threshold(sensor
, low
- 500, &th
);
329 itr1
|= (TS1_HITTHD_MASK
& (th
<< TS1_HITTHD_POS
));
331 sensor
->low_temp_enabled
= 0;
334 /* Disable high-temp if "high" is too big. */
335 if (high
< INT_MAX
) {
336 sensor
->high_temp_enabled
= 1;
337 ret
= stm_thermal_calculate_threshold(sensor
, high
, &th
);
341 itr1
|= (TS1_LITTHD_MASK
& (th
<< TS1_LITTHD_POS
));
343 sensor
->high_temp_enabled
= 0;
346 /* Write new threshod values*/
347 writel_relaxed(itr1
, sensor
->base
+ DTS_ITR1_OFFSET
);
352 /* Callback to get temperature from HW */
353 static int stm_thermal_get_temp(void *data
, int *temp
)
355 struct stm_thermal_sensor
*sensor
= data
;
359 if (sensor
->mode
!= THERMAL_DEVICE_ENABLED
)
362 /* Retrieve the number of periods sampled */
363 ret
= readl_relaxed_poll_timeout(sensor
->base
+ DTS_DR_OFFSET
, periods
,
364 (periods
& TS1_MFREQ_MASK
),
365 STARTUP_TIME
, POLL_TIMEOUT
);
369 /* Figure out the CLK_PTAT frequency */
370 freqM
= (clk_get_rate(sensor
->clk
) * SAMPLING_TIME
) / periods
;
374 /* Figure out the temperature in mili celsius */
375 *temp
= (freqM
- sensor
->fmt0
) * 1000 / sensor
->ramp_coeff
+ sensor
->t0
;
380 /* Registers DTS irq to be visible by GIC */
381 static int stm_register_irq(struct stm_thermal_sensor
*sensor
)
383 struct device
*dev
= sensor
->dev
;
384 struct platform_device
*pdev
= to_platform_device(dev
);
387 sensor
->irq
= platform_get_irq(pdev
, 0);
388 if (sensor
->irq
< 0) {
389 dev_err(dev
, "%s: Unable to find IRQ\n", __func__
);
393 ret
= devm_request_threaded_irq(dev
, sensor
->irq
,
395 stm_thermal_irq_handler
,
397 dev
->driver
->name
, sensor
);
399 dev_err(dev
, "%s: Failed to register IRQ %d\n", __func__
,
404 dev_dbg(dev
, "%s: thermal IRQ registered", __func__
);
409 static int stm_thermal_sensor_off(struct stm_thermal_sensor
*sensor
)
413 stm_disable_irq(sensor
);
415 ret
= stm_sensor_power_off(sensor
);
419 clk_disable_unprepare(sensor
->clk
);
424 static int stm_thermal_prepare(struct stm_thermal_sensor
*sensor
)
428 ret
= clk_prepare_enable(sensor
->clk
);
432 ret
= stm_thermal_read_factory_settings(sensor
);
434 goto thermal_unprepare
;
436 ret
= stm_thermal_calibration(sensor
);
438 goto thermal_unprepare
;
443 clk_disable_unprepare(sensor
->clk
);
448 #ifdef CONFIG_PM_SLEEP
449 static int stm_thermal_suspend(struct device
*dev
)
452 struct stm_thermal_sensor
*sensor
= dev_get_drvdata(dev
);
454 ret
= stm_thermal_sensor_off(sensor
);
461 static int stm_thermal_resume(struct device
*dev
)
464 struct stm_thermal_sensor
*sensor
= dev_get_drvdata(dev
);
466 ret
= stm_thermal_prepare(sensor
);
470 ret
= stm_sensor_power_on(sensor
);
474 thermal_zone_device_update(sensor
->th_dev
, THERMAL_EVENT_UNSPECIFIED
);
475 stm_enable_irq(sensor
);
479 #endif /* CONFIG_PM_SLEEP */
481 SIMPLE_DEV_PM_OPS(stm_thermal_pm_ops
, stm_thermal_suspend
, stm_thermal_resume
);
483 static const struct thermal_zone_of_device_ops stm_tz_ops
= {
484 .get_temp
= stm_thermal_get_temp
,
485 .set_trips
= stm_thermal_set_trips
,
488 static const struct of_device_id stm_thermal_of_match
[] = {
489 { .compatible
= "st,stm32-thermal"},
492 MODULE_DEVICE_TABLE(of
, stm_thermal_of_match
);
494 static int stm_thermal_probe(struct platform_device
*pdev
)
496 struct stm_thermal_sensor
*sensor
;
497 struct resource
*res
;
501 if (!pdev
->dev
.of_node
) {
502 dev_err(&pdev
->dev
, "%s: device tree node not found\n",
507 sensor
= devm_kzalloc(&pdev
->dev
, sizeof(*sensor
), GFP_KERNEL
);
511 platform_set_drvdata(pdev
, sensor
);
513 sensor
->dev
= &pdev
->dev
;
515 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
516 base
= devm_ioremap_resource(&pdev
->dev
, res
);
518 return PTR_ERR(base
);
520 /* Populate sensor */
523 sensor
->clk
= devm_clk_get(&pdev
->dev
, "pclk");
524 if (IS_ERR(sensor
->clk
)) {
525 dev_err(&pdev
->dev
, "%s: failed to fetch PCLK clock\n",
527 return PTR_ERR(sensor
->clk
);
530 stm_disable_irq(sensor
);
532 /* Clear irq flags */
533 writel_relaxed(ICIFR_MASK
, sensor
->base
+ DTS_ICIFR_OFFSET
);
535 /* Configure and enable HW sensor */
536 ret
= stm_thermal_prepare(sensor
);
538 dev_err(&pdev
->dev
, "Error prepare sensor: %d\n", ret
);
542 ret
= stm_sensor_power_on(sensor
);
544 dev_err(&pdev
->dev
, "Error power on sensor: %d\n", ret
);
548 sensor
->th_dev
= devm_thermal_zone_of_sensor_register(&pdev
->dev
, 0,
552 if (IS_ERR(sensor
->th_dev
)) {
553 dev_err(&pdev
->dev
, "%s: thermal zone sensor registering KO\n",
555 ret
= PTR_ERR(sensor
->th_dev
);
559 /* Register IRQ into GIC */
560 ret
= stm_register_irq(sensor
);
564 stm_enable_irq(sensor
);
567 * Thermal_zone doesn't enable hwmon as default,
570 sensor
->th_dev
->tzp
->no_hwmon
= false;
571 ret
= thermal_add_hwmon_sysfs(sensor
->th_dev
);
575 dev_info(&pdev
->dev
, "%s: Driver initialized successfully\n",
581 thermal_zone_of_sensor_unregister(&pdev
->dev
, sensor
->th_dev
);
585 static int stm_thermal_remove(struct platform_device
*pdev
)
587 struct stm_thermal_sensor
*sensor
= platform_get_drvdata(pdev
);
589 stm_thermal_sensor_off(sensor
);
590 thermal_remove_hwmon_sysfs(sensor
->th_dev
);
591 thermal_zone_of_sensor_unregister(&pdev
->dev
, sensor
->th_dev
);
596 static struct platform_driver stm_thermal_driver
= {
598 .name
= "stm_thermal",
599 .pm
= &stm_thermal_pm_ops
,
600 .of_match_table
= stm_thermal_of_match
,
602 .probe
= stm_thermal_probe
,
603 .remove
= stm_thermal_remove
,
605 module_platform_driver(stm_thermal_driver
);
607 MODULE_DESCRIPTION("STMicroelectronics STM32 Thermal Sensor Driver");
608 MODULE_AUTHOR("David Hernandez Sanchez <david.hernandezsanchez@st.com>");
609 MODULE_LICENSE("GPL v2");
610 MODULE_ALIAS("platform:stm_thermal");