treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / thermal / st / stm_thermal.c
blobad9e3bf8fdf675a434aebf1b7d6f5550dd1485a8
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/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 */
61 #define TS1_T0_POS 16
62 #define TS1_HITTHD_POS 16
63 #define TS1_LITTHD_POS 0
64 #define HSREF_CLK_DIV_POS 24
66 /* DTS_CFGR1 bit definitions */
67 #define TS1_EN BIT(0)
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)
80 /* Constants */
81 #define ADJUST 100
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 */
87 #define NO_HW_TRIG 0
88 #define SAMPLING_TIME 15
90 struct stm_thermal_sensor {
91 struct device *dev;
92 struct thermal_zone_device *th_dev;
93 enum thermal_device_mode mode;
94 struct clk *clk;
95 unsigned int low_temp_enabled;
96 unsigned int high_temp_enabled;
97 int irq;
98 void __iomem *base;
99 int t0, fmt0, ramp_coeff;
102 static int stm_enable_irq(struct stm_thermal_sensor *sensor)
104 u32 value;
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);
122 return 0;
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);
139 return IRQ_HANDLED;
142 static int stm_sensor_power_on(struct stm_thermal_sensor *sensor)
144 int ret;
145 u32 value;
147 /* Enable sensor */
148 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
149 value |= TS1_EN;
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);
160 if (ret)
161 return ret;
163 /* Start continuous measuring */
164 value = readl_relaxed(sensor->base +
165 DTS_CFGR1_OFFSET);
166 value |= TS1_START;
167 writel_relaxed(value, sensor->base +
168 DTS_CFGR1_OFFSET);
170 sensor->mode = THERMAL_DEVICE_ENABLED;
172 return 0;
175 static int stm_sensor_power_off(struct stm_thermal_sensor *sensor)
177 u32 value;
179 sensor->mode = THERMAL_DEVICE_DISABLED;
181 /* Stop measuring */
182 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
183 value &= ~TS1_START;
184 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
186 /* Ensure stop is taken into account */
187 usleep_range(STARTUP_TIME, POLL_TIMEOUT);
189 /* Disable sensor */
190 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET);
191 value &= ~TS1_EN;
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,
196 !(value & TS_RDY),
197 STARTUP_TIME, POLL_TIMEOUT);
200 static int stm_thermal_calibration(struct stm_thermal_sensor *sensor)
202 u32 value, clk_freq;
203 u32 prescaler;
205 /* Figure out prescaler value for PCLK during calibration */
206 clk_freq = clk_get_rate(sensor->clk);
207 if (!clk_freq)
208 return -EINVAL;
210 prescaler = 0;
211 clk_freq /= ONE_MHZ;
212 if (clk_freq) {
213 while (prescaler <= clk_freq)
214 prescaler++;
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;
234 /* select trigger */
235 value &= ~TS1_INTRIG_SEL_MASK;
236 value |= NO_HW_TRIG;
238 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET);
240 return 0;
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) &
248 TS1_T0_MASK;
249 if (!sensor->t0)
250 sensor->t0 = TS1_T0_VAL0;
251 else
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) &
260 TS1_RAMP_COEFF_MASK;
262 if (!sensor->fmt0 || !sensor->ramp_coeff) {
263 dev_err(sensor->dev, "%s: wrong setting\n", __func__);
264 return -EINVAL;
267 dev_dbg(sensor->dev, "%s: T0 = %doC, FMT0 = %dHz, RAMP_COEFF = %dHz/oC",
268 __func__, sensor->t0, sensor->fmt0, sensor->ramp_coeff);
270 return 0;
273 static int stm_thermal_calculate_threshold(struct stm_thermal_sensor *sensor,
274 int temp, u32 *th)
276 int freqM;
278 /* Figure out the CLK_PTAT frequency for a given temperature */
279 freqM = ((temp - sensor->t0) * sensor->ramp_coeff) / 1000 +
280 sensor->fmt0;
282 /* Figure out the threshold sample number */
283 *th = clk_get_rate(sensor->clk) * SAMPLING_TIME / freqM;
284 if (!*th)
285 return -EINVAL;
287 dev_dbg(sensor->dev, "freqM=%d Hz, threshold=0x%x", freqM, *th);
289 return 0;
292 /* Disable temperature interrupt */
293 static int stm_disable_irq(struct stm_thermal_sensor *sensor)
295 u32 value;
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);
302 return 0;
305 static int stm_thermal_set_trips(void *data, int low, int high)
307 struct stm_thermal_sensor *sensor = data;
308 u32 itr1, th;
309 int ret;
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);
326 if (ret)
327 return ret;
329 itr1 |= (TS1_HITTHD_MASK & (th << TS1_HITTHD_POS));
330 } else {
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);
338 if (ret)
339 return ret;
341 itr1 |= (TS1_LITTHD_MASK & (th << TS1_LITTHD_POS));
342 } else {
343 sensor->high_temp_enabled = 0;
346 /* Write new threshod values*/
347 writel_relaxed(itr1, sensor->base + DTS_ITR1_OFFSET);
349 return 0;
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;
356 u32 periods;
357 int freqM, ret;
359 if (sensor->mode != THERMAL_DEVICE_ENABLED)
360 return -EAGAIN;
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);
366 if (ret)
367 return ret;
369 /* Figure out the CLK_PTAT frequency */
370 freqM = (clk_get_rate(sensor->clk) * SAMPLING_TIME) / periods;
371 if (!freqM)
372 return -EINVAL;
374 /* Figure out the temperature in mili celsius */
375 *temp = (freqM - sensor->fmt0) * 1000 / sensor->ramp_coeff + sensor->t0;
377 return 0;
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);
385 int ret;
387 sensor->irq = platform_get_irq(pdev, 0);
388 if (sensor->irq < 0) {
389 dev_err(dev, "%s: Unable to find IRQ\n", __func__);
390 return sensor->irq;
393 ret = devm_request_threaded_irq(dev, sensor->irq,
394 NULL,
395 stm_thermal_irq_handler,
396 IRQF_ONESHOT,
397 dev->driver->name, sensor);
398 if (ret) {
399 dev_err(dev, "%s: Failed to register IRQ %d\n", __func__,
400 sensor->irq);
401 return ret;
404 dev_dbg(dev, "%s: thermal IRQ registered", __func__);
406 return 0;
409 static int stm_thermal_sensor_off(struct stm_thermal_sensor *sensor)
411 int ret;
413 stm_disable_irq(sensor);
415 ret = stm_sensor_power_off(sensor);
416 if (ret)
417 return ret;
419 clk_disable_unprepare(sensor->clk);
421 return 0;
424 static int stm_thermal_prepare(struct stm_thermal_sensor *sensor)
426 int ret;
428 ret = clk_prepare_enable(sensor->clk);
429 if (ret)
430 return ret;
432 ret = stm_thermal_read_factory_settings(sensor);
433 if (ret)
434 goto thermal_unprepare;
436 ret = stm_thermal_calibration(sensor);
437 if (ret)
438 goto thermal_unprepare;
440 return 0;
442 thermal_unprepare:
443 clk_disable_unprepare(sensor->clk);
445 return ret;
448 #ifdef CONFIG_PM_SLEEP
449 static int stm_thermal_suspend(struct device *dev)
451 int ret;
452 struct stm_thermal_sensor *sensor = dev_get_drvdata(dev);
454 ret = stm_thermal_sensor_off(sensor);
455 if (ret)
456 return ret;
458 return 0;
461 static int stm_thermal_resume(struct device *dev)
463 int ret;
464 struct stm_thermal_sensor *sensor = dev_get_drvdata(dev);
466 ret = stm_thermal_prepare(sensor);
467 if (ret)
468 return ret;
470 ret = stm_sensor_power_on(sensor);
471 if (ret)
472 return ret;
474 thermal_zone_device_update(sensor->th_dev, THERMAL_EVENT_UNSPECIFIED);
475 stm_enable_irq(sensor);
477 return 0;
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"},
490 { /* sentinel */ }
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;
498 void __iomem *base;
499 int ret;
501 if (!pdev->dev.of_node) {
502 dev_err(&pdev->dev, "%s: device tree node not found\n",
503 __func__);
504 return -EINVAL;
507 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
508 if (!sensor)
509 return -ENOMEM;
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);
517 if (IS_ERR(base))
518 return PTR_ERR(base);
520 /* Populate sensor */
521 sensor->base = base;
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",
526 __func__);
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);
537 if (ret) {
538 dev_err(&pdev->dev, "Error prepare sensor: %d\n", ret);
539 return ret;
542 ret = stm_sensor_power_on(sensor);
543 if (ret) {
544 dev_err(&pdev->dev, "Error power on sensor: %d\n", ret);
545 return ret;
548 sensor->th_dev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0,
549 sensor,
550 &stm_tz_ops);
552 if (IS_ERR(sensor->th_dev)) {
553 dev_err(&pdev->dev, "%s: thermal zone sensor registering KO\n",
554 __func__);
555 ret = PTR_ERR(sensor->th_dev);
556 return ret;
559 /* Register IRQ into GIC */
560 ret = stm_register_irq(sensor);
561 if (ret)
562 goto err_tz;
564 stm_enable_irq(sensor);
567 * Thermal_zone doesn't enable hwmon as default,
568 * enable it here
570 sensor->th_dev->tzp->no_hwmon = false;
571 ret = thermal_add_hwmon_sysfs(sensor->th_dev);
572 if (ret)
573 goto err_tz;
575 dev_info(&pdev->dev, "%s: Driver initialized successfully\n",
576 __func__);
578 return 0;
580 err_tz:
581 thermal_zone_of_sensor_unregister(&pdev->dev, sensor->th_dev);
582 return ret;
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);
593 return 0;
596 static struct platform_driver stm_thermal_driver = {
597 .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");