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/platform_device.h>
18 #include <linux/thermal.h>
20 #include "../thermal_hwmon.h"
22 /* DTS register offsets */
23 #define DTS_CFGR1_OFFSET 0x0
24 #define DTS_T0VALR1_OFFSET 0x8
25 #define DTS_RAMPVALR_OFFSET 0X10
26 #define DTS_ITR1_OFFSET 0x14
27 #define DTS_DR_OFFSET 0x1C
28 #define DTS_SR_OFFSET 0x20
29 #define DTS_ITENR_OFFSET 0x24
30 #define DTS_ICIFR_OFFSET 0x28
32 /* DTS_CFGR1 register mask definitions */
33 #define HSREF_CLK_DIV_MASK GENMASK(30, 24)
34 #define TS1_SMP_TIME_MASK GENMASK(19, 16)
35 #define TS1_INTRIG_SEL_MASK GENMASK(11, 8)
37 /* DTS_T0VALR1 register mask definitions */
38 #define TS1_T0_MASK GENMASK(17, 16)
39 #define TS1_FMT0_MASK GENMASK(15, 0)
41 /* DTS_RAMPVALR register mask definitions */
42 #define TS1_RAMP_COEFF_MASK GENMASK(15, 0)
44 /* DTS_ITR1 register mask definitions */
45 #define TS1_HITTHD_MASK GENMASK(31, 16)
46 #define TS1_LITTHD_MASK GENMASK(15, 0)
48 /* DTS_DR register mask definitions */
49 #define TS1_MFREQ_MASK GENMASK(15, 0)
51 /* DTS_ITENR register mask definitions */
52 #define ITENR_MASK (GENMASK(2, 0) | GENMASK(6, 4))
54 /* DTS_ICIFR register mask definitions */
55 #define ICIFR_MASK (GENMASK(2, 0) | GENMASK(6, 4))
57 /* Less significant bit position definitions */
59 #define TS1_HITTHD_POS 16
60 #define TS1_LITTHD_POS 0
61 #define HSREF_CLK_DIV_POS 24
63 /* DTS_CFGR1 bit definitions */
65 #define TS1_START BIT(4)
66 #define REFCLK_SEL BIT(20)
67 #define REFCLK_LSE REFCLK_SEL
68 #define Q_MEAS_OPT BIT(21)
69 #define CALIBRATION_CONTROL Q_MEAS_OPT
71 /* DTS_SR bit definitions */
72 #define TS_RDY BIT(15)
73 /* Bit definitions below are common for DTS_SR, DTS_ITENR and DTS_CIFR */
74 #define HIGH_THRESHOLD BIT(2)
75 #define LOW_THRESHOLD BIT(1)
79 #define ONE_MHZ 1000000
80 #define POLL_TIMEOUT 5000
81 #define STARTUP_TIME 40
82 #define TS1_T0_VAL0 30000 /* 30 celsius */
83 #define TS1_T0_VAL1 130000 /* 130 celsius */
85 #define SAMPLING_TIME 15
87 struct stm_thermal_sensor
{
89 struct thermal_zone_device
*th_dev
;
90 enum thermal_device_mode mode
;
92 unsigned int low_temp_enabled
;
93 unsigned int high_temp_enabled
;
96 int t0
, fmt0
, ramp_coeff
;
99 static int stm_enable_irq(struct stm_thermal_sensor
*sensor
)
103 dev_dbg(sensor
->dev
, "low:%d high:%d\n", sensor
->low_temp_enabled
,
104 sensor
->high_temp_enabled
);
106 /* Disable IT generation for low and high thresholds */
107 value
= readl_relaxed(sensor
->base
+ DTS_ITENR_OFFSET
);
108 value
&= ~(LOW_THRESHOLD
| HIGH_THRESHOLD
);
110 if (sensor
->low_temp_enabled
)
111 value
|= HIGH_THRESHOLD
;
113 if (sensor
->high_temp_enabled
)
114 value
|= LOW_THRESHOLD
;
116 /* Enable interrupts */
117 writel_relaxed(value
, sensor
->base
+ DTS_ITENR_OFFSET
);
122 static irqreturn_t
stm_thermal_irq_handler(int irq
, void *sdata
)
124 struct stm_thermal_sensor
*sensor
= sdata
;
126 dev_dbg(sensor
->dev
, "sr:%d\n",
127 readl_relaxed(sensor
->base
+ DTS_SR_OFFSET
));
129 thermal_zone_device_update(sensor
->th_dev
, THERMAL_EVENT_UNSPECIFIED
);
131 stm_enable_irq(sensor
);
133 /* Acknoledge all DTS irqs */
134 writel_relaxed(ICIFR_MASK
, sensor
->base
+ DTS_ICIFR_OFFSET
);
139 static int stm_sensor_power_on(struct stm_thermal_sensor
*sensor
)
145 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
147 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
150 * The DTS block can be enabled by setting TSx_EN bit in
151 * DTS_CFGRx register. It requires a startup time of
152 * 40μs. Use 5 ms as arbitrary timeout.
154 ret
= readl_poll_timeout(sensor
->base
+ DTS_SR_OFFSET
,
155 value
, (value
& TS_RDY
),
156 STARTUP_TIME
, POLL_TIMEOUT
);
160 /* Start continuous measuring */
161 value
= readl_relaxed(sensor
->base
+
164 writel_relaxed(value
, sensor
->base
+
167 sensor
->mode
= THERMAL_DEVICE_ENABLED
;
172 static int stm_sensor_power_off(struct stm_thermal_sensor
*sensor
)
176 sensor
->mode
= THERMAL_DEVICE_DISABLED
;
179 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
181 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
183 /* Ensure stop is taken into account */
184 usleep_range(STARTUP_TIME
, POLL_TIMEOUT
);
187 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
189 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
191 /* Ensure disable is taken into account */
192 return readl_poll_timeout(sensor
->base
+ DTS_SR_OFFSET
, value
,
194 STARTUP_TIME
, POLL_TIMEOUT
);
197 static int stm_thermal_calibration(struct stm_thermal_sensor
*sensor
)
202 /* Figure out prescaler value for PCLK during calibration */
203 clk_freq
= clk_get_rate(sensor
->clk
);
210 while (prescaler
<= clk_freq
)
214 value
= readl_relaxed(sensor
->base
+ DTS_CFGR1_OFFSET
);
216 /* Clear prescaler */
217 value
&= ~HSREF_CLK_DIV_MASK
;
219 /* Set prescaler. pclk_freq/prescaler < 1MHz */
220 value
|= (prescaler
<< HSREF_CLK_DIV_POS
);
222 /* Select PCLK as reference clock */
223 value
&= ~REFCLK_SEL
;
225 /* Set maximal sampling time for better precision */
226 value
|= TS1_SMP_TIME_MASK
;
228 /* Measure with calibration */
229 value
&= ~CALIBRATION_CONTROL
;
232 value
&= ~TS1_INTRIG_SEL_MASK
;
235 writel_relaxed(value
, sensor
->base
+ DTS_CFGR1_OFFSET
);
240 /* Fill in DTS structure with factory sensor values */
241 static int stm_thermal_read_factory_settings(struct stm_thermal_sensor
*sensor
)
243 /* Retrieve engineering calibration temperature */
244 sensor
->t0
= readl_relaxed(sensor
->base
+ DTS_T0VALR1_OFFSET
) &
247 sensor
->t0
= TS1_T0_VAL0
;
249 sensor
->t0
= TS1_T0_VAL1
;
251 /* Retrieve fmt0 and put it on Hz */
252 sensor
->fmt0
= ADJUST
* (readl_relaxed(sensor
->base
+
253 DTS_T0VALR1_OFFSET
) & TS1_FMT0_MASK
);
255 /* Retrieve ramp coefficient */
256 sensor
->ramp_coeff
= readl_relaxed(sensor
->base
+ DTS_RAMPVALR_OFFSET
) &
259 if (!sensor
->fmt0
|| !sensor
->ramp_coeff
) {
260 dev_err(sensor
->dev
, "%s: wrong setting\n", __func__
);
264 dev_dbg(sensor
->dev
, "%s: T0 = %doC, FMT0 = %dHz, RAMP_COEFF = %dHz/oC",
265 __func__
, sensor
->t0
, sensor
->fmt0
, sensor
->ramp_coeff
);
270 static int stm_thermal_calculate_threshold(struct stm_thermal_sensor
*sensor
,
275 /* Figure out the CLK_PTAT frequency for a given temperature */
276 freqM
= ((temp
- sensor
->t0
) * sensor
->ramp_coeff
) / 1000 +
279 /* Figure out the threshold sample number */
280 *th
= clk_get_rate(sensor
->clk
) * SAMPLING_TIME
/ freqM
;
284 dev_dbg(sensor
->dev
, "freqM=%d Hz, threshold=0x%x", freqM
, *th
);
289 /* Disable temperature interrupt */
290 static int stm_disable_irq(struct stm_thermal_sensor
*sensor
)
294 /* Disable IT generation */
295 value
= readl_relaxed(sensor
->base
+ DTS_ITENR_OFFSET
);
296 value
&= ~ITENR_MASK
;
297 writel_relaxed(value
, sensor
->base
+ DTS_ITENR_OFFSET
);
302 static int stm_thermal_set_trips(struct thermal_zone_device
*tz
, int low
, int high
)
304 struct stm_thermal_sensor
*sensor
= thermal_zone_device_priv(tz
);
308 dev_dbg(sensor
->dev
, "set trips %d <--> %d\n", low
, high
);
310 /* Erase threshold content */
311 itr1
= readl_relaxed(sensor
->base
+ DTS_ITR1_OFFSET
);
312 itr1
&= ~(TS1_LITTHD_MASK
| TS1_HITTHD_MASK
);
315 * Disable low-temp if "low" is too small. As per thermal framework
316 * API, we use -INT_MAX rather than INT_MIN.
319 if (low
> -INT_MAX
) {
320 sensor
->low_temp_enabled
= 1;
321 /* add 0.5 of hysteresis due to measurement error */
322 ret
= stm_thermal_calculate_threshold(sensor
, low
- 500, &th
);
326 itr1
|= (TS1_HITTHD_MASK
& (th
<< TS1_HITTHD_POS
));
328 sensor
->low_temp_enabled
= 0;
331 /* Disable high-temp if "high" is too big. */
332 if (high
< INT_MAX
) {
333 sensor
->high_temp_enabled
= 1;
334 ret
= stm_thermal_calculate_threshold(sensor
, high
, &th
);
338 itr1
|= (TS1_LITTHD_MASK
& (th
<< TS1_LITTHD_POS
));
340 sensor
->high_temp_enabled
= 0;
343 /* Write new threshod values*/
344 writel_relaxed(itr1
, sensor
->base
+ DTS_ITR1_OFFSET
);
349 /* Callback to get temperature from HW */
350 static int stm_thermal_get_temp(struct thermal_zone_device
*tz
, int *temp
)
352 struct stm_thermal_sensor
*sensor
= thermal_zone_device_priv(tz
);
356 if (sensor
->mode
!= THERMAL_DEVICE_ENABLED
)
359 /* Retrieve the number of periods sampled */
360 ret
= readl_relaxed_poll_timeout(sensor
->base
+ DTS_DR_OFFSET
, periods
,
361 (periods
& TS1_MFREQ_MASK
),
362 STARTUP_TIME
, POLL_TIMEOUT
);
366 /* Figure out the CLK_PTAT frequency */
367 freqM
= (clk_get_rate(sensor
->clk
) * SAMPLING_TIME
) / periods
;
371 /* Figure out the temperature in mili celsius */
372 *temp
= (freqM
- sensor
->fmt0
) * 1000 / sensor
->ramp_coeff
+ sensor
->t0
;
377 /* Registers DTS irq to be visible by GIC */
378 static int stm_register_irq(struct stm_thermal_sensor
*sensor
)
380 struct device
*dev
= sensor
->dev
;
381 struct platform_device
*pdev
= to_platform_device(dev
);
384 sensor
->irq
= platform_get_irq(pdev
, 0);
388 ret
= devm_request_threaded_irq(dev
, sensor
->irq
,
390 stm_thermal_irq_handler
,
392 dev
->driver
->name
, sensor
);
394 dev_err(dev
, "%s: Failed to register IRQ %d\n", __func__
,
399 dev_dbg(dev
, "%s: thermal IRQ registered", __func__
);
404 static int stm_thermal_sensor_off(struct stm_thermal_sensor
*sensor
)
408 stm_disable_irq(sensor
);
410 ret
= stm_sensor_power_off(sensor
);
414 clk_disable_unprepare(sensor
->clk
);
419 static int stm_thermal_prepare(struct stm_thermal_sensor
*sensor
)
423 ret
= clk_prepare_enable(sensor
->clk
);
427 ret
= stm_thermal_read_factory_settings(sensor
);
429 goto thermal_unprepare
;
431 ret
= stm_thermal_calibration(sensor
);
433 goto thermal_unprepare
;
438 clk_disable_unprepare(sensor
->clk
);
443 static int stm_thermal_suspend(struct device
*dev
)
445 struct stm_thermal_sensor
*sensor
= dev_get_drvdata(dev
);
447 return stm_thermal_sensor_off(sensor
);
450 static int stm_thermal_resume(struct device
*dev
)
453 struct stm_thermal_sensor
*sensor
= dev_get_drvdata(dev
);
455 ret
= stm_thermal_prepare(sensor
);
459 ret
= stm_sensor_power_on(sensor
);
463 thermal_zone_device_update(sensor
->th_dev
, THERMAL_EVENT_UNSPECIFIED
);
464 stm_enable_irq(sensor
);
469 static DEFINE_SIMPLE_DEV_PM_OPS(stm_thermal_pm_ops
,
470 stm_thermal_suspend
, stm_thermal_resume
);
472 static const struct thermal_zone_device_ops stm_tz_ops
= {
473 .get_temp
= stm_thermal_get_temp
,
474 .set_trips
= stm_thermal_set_trips
,
477 static const struct of_device_id stm_thermal_of_match
[] = {
478 { .compatible
= "st,stm32-thermal"},
481 MODULE_DEVICE_TABLE(of
, stm_thermal_of_match
);
483 static int stm_thermal_probe(struct platform_device
*pdev
)
485 struct stm_thermal_sensor
*sensor
;
489 if (!pdev
->dev
.of_node
) {
490 dev_err(&pdev
->dev
, "%s: device tree node not found\n",
495 sensor
= devm_kzalloc(&pdev
->dev
, sizeof(*sensor
), GFP_KERNEL
);
499 platform_set_drvdata(pdev
, sensor
);
501 sensor
->dev
= &pdev
->dev
;
503 base
= devm_platform_get_and_ioremap_resource(pdev
, 0, NULL
);
505 return PTR_ERR(base
);
507 /* Populate sensor */
510 sensor
->clk
= devm_clk_get(&pdev
->dev
, "pclk");
511 if (IS_ERR(sensor
->clk
)) {
512 dev_err(&pdev
->dev
, "%s: failed to fetch PCLK clock\n",
514 return PTR_ERR(sensor
->clk
);
517 stm_disable_irq(sensor
);
519 /* Clear irq flags */
520 writel_relaxed(ICIFR_MASK
, sensor
->base
+ DTS_ICIFR_OFFSET
);
522 /* Configure and enable HW sensor */
523 ret
= stm_thermal_prepare(sensor
);
525 dev_err(&pdev
->dev
, "Error prepare sensor: %d\n", ret
);
529 ret
= stm_sensor_power_on(sensor
);
531 dev_err(&pdev
->dev
, "Error power on sensor: %d\n", ret
);
535 sensor
->th_dev
= devm_thermal_of_zone_register(&pdev
->dev
, 0,
539 if (IS_ERR(sensor
->th_dev
)) {
540 dev_err(&pdev
->dev
, "%s: thermal zone sensor registering KO\n",
542 ret
= PTR_ERR(sensor
->th_dev
);
546 /* Register IRQ into GIC */
547 ret
= stm_register_irq(sensor
);
551 stm_enable_irq(sensor
);
554 * Thermal_zone doesn't enable hwmon as default,
557 ret
= thermal_add_hwmon_sysfs(sensor
->th_dev
);
561 dev_info(&pdev
->dev
, "%s: Driver initialized successfully\n",
570 static void stm_thermal_remove(struct platform_device
*pdev
)
572 struct stm_thermal_sensor
*sensor
= platform_get_drvdata(pdev
);
574 stm_thermal_sensor_off(sensor
);
575 thermal_remove_hwmon_sysfs(sensor
->th_dev
);
578 static struct platform_driver stm_thermal_driver
= {
580 .name
= "stm_thermal",
581 .pm
= pm_sleep_ptr(&stm_thermal_pm_ops
),
582 .of_match_table
= stm_thermal_of_match
,
584 .probe
= stm_thermal_probe
,
585 .remove
= stm_thermal_remove
,
587 module_platform_driver(stm_thermal_driver
);
589 MODULE_DESCRIPTION("STMicroelectronics STM32 Thermal Sensor Driver");
590 MODULE_AUTHOR("David Hernandez Sanchez <david.hernandezsanchez@st.com>");
591 MODULE_LICENSE("GPL v2");
592 MODULE_ALIAS("platform:stm_thermal");