Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / thermal / st / stm_thermal.c
blob6e90eb9f414dfc4f243806714c4647a78e59dbff
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
4 * Author: David Hernandez Sanchez <david.hernandezsanchez@st.com> for
5 * STMicroelectronics.
6 */
8 #include <linux/clk.h>
9 #include <linux/clk-provider.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/of.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 */
58 #define TS1_T0_POS 16
59 #define TS1_HITTHD_POS 16
60 #define TS1_LITTHD_POS 0
61 #define HSREF_CLK_DIV_POS 24
63 /* DTS_CFGR1 bit definitions */
64 #define TS1_EN BIT(0)
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)
77 /* Constants */
78 #define ADJUST 100
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 */
84 #define NO_HW_TRIG 0
85 #define SAMPLING_TIME 15
87 struct stm_thermal_sensor {
88 struct device *dev;
89 struct thermal_zone_device *th_dev;
90 enum thermal_device_mode mode;
91 struct clk *clk;
92 unsigned int low_temp_enabled;
93 unsigned int high_temp_enabled;
94 int irq;
95 void __iomem *base;
96 int t0, fmt0, ramp_coeff;
99 static int stm_enable_irq(struct stm_thermal_sensor *sensor)
101 u32 value;
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);
119 return 0;
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);
136 return IRQ_HANDLED;
139 static int stm_sensor_power_on(struct stm_thermal_sensor *sensor)
141 int ret;
142 u32 value;
144 /* Enable sensor */
145 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
146 value |= TS1_EN;
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);
157 if (ret)
158 return ret;
160 /* Start continuous measuring */
161 value = readl_relaxed(sensor->base +
162 DTS_CFGR1_OFFSET);
163 value |= TS1_START;
164 writel_relaxed(value, sensor->base +
165 DTS_CFGR1_OFFSET);
167 sensor->mode = THERMAL_DEVICE_ENABLED;
169 return 0;
172 static int stm_sensor_power_off(struct stm_thermal_sensor *sensor)
174 u32 value;
176 sensor->mode = THERMAL_DEVICE_DISABLED;
178 /* Stop measuring */
179 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
180 value &= ~TS1_START;
181 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
183 /* Ensure stop is taken into account */
184 usleep_range(STARTUP_TIME, POLL_TIMEOUT);
186 /* Disable sensor */
187 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
188 value &= ~TS1_EN;
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,
193 !(value & TS_RDY),
194 STARTUP_TIME, POLL_TIMEOUT);
197 static int stm_thermal_calibration(struct stm_thermal_sensor *sensor)
199 u32 value, clk_freq;
200 u32 prescaler;
202 /* Figure out prescaler value for PCLK during calibration */
203 clk_freq = clk_get_rate(sensor->clk);
204 if (!clk_freq)
205 return -EINVAL;
207 prescaler = 0;
208 clk_freq /= ONE_MHZ;
209 if (clk_freq) {
210 while (prescaler <= clk_freq)
211 prescaler++;
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;
231 /* select trigger */
232 value &= ~TS1_INTRIG_SEL_MASK;
233 value |= NO_HW_TRIG;
235 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
237 return 0;
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) &
245 TS1_T0_MASK;
246 if (!sensor->t0)
247 sensor->t0 = TS1_T0_VAL0;
248 else
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) &
257 TS1_RAMP_COEFF_MASK;
259 if (!sensor->fmt0 || !sensor->ramp_coeff) {
260 dev_err(sensor->dev, "%s: wrong setting\n", __func__);
261 return -EINVAL;
264 dev_dbg(sensor->dev, "%s: T0 = %doC, FMT0 = %dHz, RAMP_COEFF = %dHz/oC",
265 __func__, sensor->t0, sensor->fmt0, sensor->ramp_coeff);
267 return 0;
270 static int stm_thermal_calculate_threshold(struct stm_thermal_sensor *sensor,
271 int temp, u32 *th)
273 int freqM;
275 /* Figure out the CLK_PTAT frequency for a given temperature */
276 freqM = ((temp - sensor->t0) * sensor->ramp_coeff) / 1000 +
277 sensor->fmt0;
279 /* Figure out the threshold sample number */
280 *th = clk_get_rate(sensor->clk) * SAMPLING_TIME / freqM;
281 if (!*th)
282 return -EINVAL;
284 dev_dbg(sensor->dev, "freqM=%d Hz, threshold=0x%x", freqM, *th);
286 return 0;
289 /* Disable temperature interrupt */
290 static int stm_disable_irq(struct stm_thermal_sensor *sensor)
292 u32 value;
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);
299 return 0;
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);
305 u32 itr1, th;
306 int ret;
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);
323 if (ret)
324 return ret;
326 itr1 |= (TS1_HITTHD_MASK & (th << TS1_HITTHD_POS));
327 } else {
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);
335 if (ret)
336 return ret;
338 itr1 |= (TS1_LITTHD_MASK & (th << TS1_LITTHD_POS));
339 } else {
340 sensor->high_temp_enabled = 0;
343 /* Write new threshod values*/
344 writel_relaxed(itr1, sensor->base + DTS_ITR1_OFFSET);
346 return 0;
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);
353 u32 periods;
354 int freqM, ret;
356 if (sensor->mode != THERMAL_DEVICE_ENABLED)
357 return -EAGAIN;
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);
363 if (ret)
364 return ret;
366 /* Figure out the CLK_PTAT frequency */
367 freqM = (clk_get_rate(sensor->clk) * SAMPLING_TIME) / periods;
368 if (!freqM)
369 return -EINVAL;
371 /* Figure out the temperature in mili celsius */
372 *temp = (freqM - sensor->fmt0) * 1000 / sensor->ramp_coeff + sensor->t0;
374 return 0;
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);
382 int ret;
384 sensor->irq = platform_get_irq(pdev, 0);
385 if (sensor->irq < 0)
386 return sensor->irq;
388 ret = devm_request_threaded_irq(dev, sensor->irq,
389 NULL,
390 stm_thermal_irq_handler,
391 IRQF_ONESHOT,
392 dev->driver->name, sensor);
393 if (ret) {
394 dev_err(dev, "%s: Failed to register IRQ %d\n", __func__,
395 sensor->irq);
396 return ret;
399 dev_dbg(dev, "%s: thermal IRQ registered", __func__);
401 return 0;
404 static int stm_thermal_sensor_off(struct stm_thermal_sensor *sensor)
406 int ret;
408 stm_disable_irq(sensor);
410 ret = stm_sensor_power_off(sensor);
411 if (ret)
412 return ret;
414 clk_disable_unprepare(sensor->clk);
416 return 0;
419 static int stm_thermal_prepare(struct stm_thermal_sensor *sensor)
421 int ret;
423 ret = clk_prepare_enable(sensor->clk);
424 if (ret)
425 return ret;
427 ret = stm_thermal_read_factory_settings(sensor);
428 if (ret)
429 goto thermal_unprepare;
431 ret = stm_thermal_calibration(sensor);
432 if (ret)
433 goto thermal_unprepare;
435 return 0;
437 thermal_unprepare:
438 clk_disable_unprepare(sensor->clk);
440 return ret;
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)
452 int ret;
453 struct stm_thermal_sensor *sensor = dev_get_drvdata(dev);
455 ret = stm_thermal_prepare(sensor);
456 if (ret)
457 return ret;
459 ret = stm_sensor_power_on(sensor);
460 if (ret)
461 return ret;
463 thermal_zone_device_update(sensor->th_dev, THERMAL_EVENT_UNSPECIFIED);
464 stm_enable_irq(sensor);
466 return 0;
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"},
479 { /* sentinel */ }
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;
486 void __iomem *base;
487 int ret;
489 if (!pdev->dev.of_node) {
490 dev_err(&pdev->dev, "%s: device tree node not found\n",
491 __func__);
492 return -EINVAL;
495 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
496 if (!sensor)
497 return -ENOMEM;
499 platform_set_drvdata(pdev, sensor);
501 sensor->dev = &pdev->dev;
503 base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
504 if (IS_ERR(base))
505 return PTR_ERR(base);
507 /* Populate sensor */
508 sensor->base = base;
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",
513 __func__);
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);
524 if (ret) {
525 dev_err(&pdev->dev, "Error prepare sensor: %d\n", ret);
526 return ret;
529 ret = stm_sensor_power_on(sensor);
530 if (ret) {
531 dev_err(&pdev->dev, "Error power on sensor: %d\n", ret);
532 return ret;
535 sensor->th_dev = devm_thermal_of_zone_register(&pdev->dev, 0,
536 sensor,
537 &stm_tz_ops);
539 if (IS_ERR(sensor->th_dev)) {
540 dev_err(&pdev->dev, "%s: thermal zone sensor registering KO\n",
541 __func__);
542 ret = PTR_ERR(sensor->th_dev);
543 return ret;
546 /* Register IRQ into GIC */
547 ret = stm_register_irq(sensor);
548 if (ret)
549 goto err_tz;
551 stm_enable_irq(sensor);
554 * Thermal_zone doesn't enable hwmon as default,
555 * enable it here
557 ret = thermal_add_hwmon_sysfs(sensor->th_dev);
558 if (ret)
559 goto err_tz;
561 dev_info(&pdev->dev, "%s: Driver initialized successfully\n",
562 __func__);
564 return 0;
566 err_tz:
567 return ret;
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 = {
579 .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");