1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright 2013 Freescale Semiconductor, Inc.
6 #include <linux/cpufreq.h>
7 #include <linux/cpu_cooling.h>
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/thermal.h>
17 #include <linux/nvmem-consumer.h>
18 #include <linux/pm_runtime.h>
25 #define IMX6_MISC0 0x0150
26 #define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
27 #define IMX6_MISC1 0x0160
28 #define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
29 /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
30 #define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
31 #define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
33 #define IMX6_TEMPSENSE0 0x0180
34 #define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
35 #define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
36 #define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
37 #define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
38 #define IMX6_TEMPSENSE0_FINISHED (1 << 2)
39 #define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
40 #define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
42 #define IMX6_TEMPSENSE1 0x0190
43 #define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
44 #define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
46 #define OCOTP_MEM0 0x0480
47 #define OCOTP_ANA1 0x04e0
49 /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
50 #define IMX6_TEMPSENSE2 0x0290
51 #define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
52 #define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
53 #define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
54 #define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
57 #define IMX7_ANADIG_DIGPROG 0x800
58 #define IMX7_TEMPSENSE0 0x300
59 #define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
60 #define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
61 #define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
62 #define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
63 #define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
64 #define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
66 #define IMX7_TEMPSENSE1 0x310
67 #define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
68 #define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
69 #define IMX7_TEMPSENSE1_FINISHED (1 << 11)
70 #define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
71 #define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
72 #define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
73 #define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
75 /* The driver supports 1 passive trip point and 1 critical trip point */
76 enum imx_thermal_trip {
81 #define IMX_POLLING_DELAY 2000 /* millisecond */
82 #define IMX_PASSIVE_DELAY 1000
84 #define TEMPMON_IMX6Q 1
85 #define TEMPMON_IMX6SX 2
86 #define TEMPMON_IMX7D 3
88 struct thermal_soc_data {
93 u32 measure_temp_mask;
95 u32 measure_freq_ctrl;
96 u32 measure_freq_mask;
97 u32 measure_freq_shift;
101 u32 temp_value_shift;
104 u32 panic_alarm_ctrl;
105 u32 panic_alarm_mask;
106 u32 panic_alarm_shift;
110 u32 high_alarm_shift;
117 static struct thermal_trip trips[] = {
118 [IMX_TRIP_PASSIVE] = { .type = THERMAL_TRIP_PASSIVE },
119 [IMX_TRIP_CRITICAL] = { .type = THERMAL_TRIP_CRITICAL },
122 static struct thermal_soc_data thermal_imx6q_data = {
123 .version = TEMPMON_IMX6Q,
125 .sensor_ctrl = IMX6_TEMPSENSE0,
126 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
127 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
129 .measure_freq_ctrl = IMX6_TEMPSENSE1,
130 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
131 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
133 .temp_data = IMX6_TEMPSENSE0,
134 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
135 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
136 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
138 .high_alarm_ctrl = IMX6_TEMPSENSE0,
139 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
140 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
143 static struct thermal_soc_data thermal_imx6sx_data = {
144 .version = TEMPMON_IMX6SX,
146 .sensor_ctrl = IMX6_TEMPSENSE0,
147 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
148 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
150 .measure_freq_ctrl = IMX6_TEMPSENSE1,
151 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
152 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
154 .temp_data = IMX6_TEMPSENSE0,
155 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
156 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
157 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
159 .high_alarm_ctrl = IMX6_TEMPSENSE0,
160 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
161 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
163 .panic_alarm_ctrl = IMX6_TEMPSENSE2,
164 .panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
165 .panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
167 .low_alarm_ctrl = IMX6_TEMPSENSE2,
168 .low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
169 .low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
172 static struct thermal_soc_data thermal_imx7d_data = {
173 .version = TEMPMON_IMX7D,
175 .sensor_ctrl = IMX7_TEMPSENSE1,
176 .power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
177 .measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
179 .measure_freq_ctrl = IMX7_TEMPSENSE1,
180 .measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
181 .measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
183 .temp_data = IMX7_TEMPSENSE1,
184 .temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
185 .temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
186 .temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
188 .panic_alarm_ctrl = IMX7_TEMPSENSE1,
189 .panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
190 .panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
192 .high_alarm_ctrl = IMX7_TEMPSENSE0,
193 .high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
194 .high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
196 .low_alarm_ctrl = IMX7_TEMPSENSE0,
197 .low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
198 .low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
201 struct imx_thermal_data {
203 struct cpufreq_policy *policy;
204 struct thermal_zone_device *tz;
205 struct thermal_cooling_device *cdev;
206 struct regmap *tempmon;
207 u32 c1, c2; /* See formula in imx_init_calib() */
213 struct clk *thermal_clk;
214 const struct thermal_soc_data *socdata;
215 const char *temp_grade;
218 static void imx_set_panic_temp(struct imx_thermal_data *data,
221 const struct thermal_soc_data *soc_data = data->socdata;
222 struct regmap *map = data->tempmon;
225 critical_value = (data->c2 - panic_temp) / data->c1;
227 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
228 soc_data->panic_alarm_mask);
229 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
230 critical_value << soc_data->panic_alarm_shift);
233 static void imx_set_alarm_temp(struct imx_thermal_data *data,
236 struct regmap *map = data->tempmon;
237 const struct thermal_soc_data *soc_data = data->socdata;
240 data->alarm_temp = alarm_temp;
242 if (data->socdata->version == TEMPMON_IMX7D)
243 alarm_value = alarm_temp / 1000 + data->c1 - 25;
245 alarm_value = (data->c2 - alarm_temp) / data->c1;
247 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
248 soc_data->high_alarm_mask);
249 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
250 alarm_value << soc_data->high_alarm_shift);
253 static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
255 struct imx_thermal_data *data = thermal_zone_device_priv(tz);
256 const struct thermal_soc_data *soc_data = data->socdata;
257 struct regmap *map = data->tempmon;
262 ret = pm_runtime_resume_and_get(data->dev);
266 regmap_read(map, soc_data->temp_data, &val);
268 if ((val & soc_data->temp_valid_mask) == 0)
271 n_meas = (val & soc_data->temp_value_mask)
272 >> soc_data->temp_value_shift;
274 /* See imx_init_calib() for formula derivation */
275 if (data->socdata->version == TEMPMON_IMX7D)
276 *temp = (n_meas - data->c1 + 25) * 1000;
278 *temp = data->c2 - n_meas * data->c1;
280 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
281 if (data->socdata->version == TEMPMON_IMX6Q) {
282 if (data->alarm_temp == trips[IMX_TRIP_PASSIVE].temperature &&
283 *temp >= trips[IMX_TRIP_PASSIVE].temperature)
284 imx_set_alarm_temp(data, trips[IMX_TRIP_CRITICAL].temperature);
285 if (data->alarm_temp == trips[IMX_TRIP_CRITICAL].temperature &&
286 *temp < trips[IMX_TRIP_PASSIVE].temperature) {
287 imx_set_alarm_temp(data, trips[IMX_TRIP_PASSIVE].temperature);
288 dev_dbg(data->dev, "thermal alarm off: T < %d\n",
289 data->alarm_temp / 1000);
293 if (*temp != data->last_temp) {
294 dev_dbg(data->dev, "millicelsius: %d\n", *temp);
295 data->last_temp = *temp;
298 /* Reenable alarm IRQ if temperature below alarm temperature */
299 if (!data->irq_enabled && *temp < data->alarm_temp) {
300 data->irq_enabled = true;
301 enable_irq(data->irq);
304 pm_runtime_put(data->dev);
309 static int imx_change_mode(struct thermal_zone_device *tz,
310 enum thermal_device_mode mode)
312 struct imx_thermal_data *data = thermal_zone_device_priv(tz);
314 if (mode == THERMAL_DEVICE_ENABLED) {
315 pm_runtime_get(data->dev);
317 if (!data->irq_enabled) {
318 data->irq_enabled = true;
319 enable_irq(data->irq);
322 pm_runtime_put(data->dev);
324 if (data->irq_enabled) {
325 disable_irq(data->irq);
326 data->irq_enabled = false;
333 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip_id,
336 struct imx_thermal_data *data = thermal_zone_device_priv(tz);
337 struct thermal_trip trip;
340 ret = pm_runtime_resume_and_get(data->dev);
344 ret = __thermal_zone_get_trip(tz, trip_id, &trip);
348 /* do not allow changing critical threshold */
349 if (trip.type == THERMAL_TRIP_CRITICAL)
352 /* do not allow passive to be set higher than critical */
353 if (temp < 0 || temp > trips[IMX_TRIP_CRITICAL].temperature)
356 imx_set_alarm_temp(data, temp);
358 pm_runtime_put(data->dev);
363 static int imx_bind(struct thermal_zone_device *tz,
364 struct thermal_cooling_device *cdev)
366 return thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
369 THERMAL_WEIGHT_DEFAULT);
372 static int imx_unbind(struct thermal_zone_device *tz,
373 struct thermal_cooling_device *cdev)
375 return thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
378 static struct thermal_zone_device_ops imx_tz_ops = {
380 .unbind = imx_unbind,
381 .get_temp = imx_get_temp,
382 .change_mode = imx_change_mode,
383 .set_trip_temp = imx_set_trip_temp,
386 static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
388 struct imx_thermal_data *data = platform_get_drvdata(pdev);
392 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
393 dev_err(&pdev->dev, "invalid sensor calibration data\n");
398 * On i.MX7D, we only use the calibration data at 25C to get the temp,
399 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
401 if (data->socdata->version == TEMPMON_IMX7D) {
402 data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
407 * The sensor is calibrated at 25 °C (aka T1) and the value measured
408 * (aka N1) at this temperature is provided in bits [31:20] in the
409 * i.MX's OCOTP value ANA1.
410 * To find the actual temperature T, the following formula has to be used
411 * when reading value n from the sensor:
413 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
414 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
415 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
420 * T1' = 28.580661 °C
421 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
422 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
425 n1 = ocotp_ana1 >> 20;
427 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
428 temp64 *= 1000; /* to get result in °mC */
429 do_div(temp64, 15423 * n1 - 4148468);
431 data->c2 = n1 * data->c1 + 28581;
436 static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
438 struct imx_thermal_data *data = platform_get_drvdata(pdev);
440 /* The maximum die temp is specified by the Temperature Grade */
441 switch ((ocotp_mem0 >> 6) & 0x3) {
442 case 0: /* Commercial (0 to 95 °C) */
443 data->temp_grade = "Commercial";
444 data->temp_max = 95000;
446 case 1: /* Extended Commercial (-20 °C to 105 °C) */
447 data->temp_grade = "Extended Commercial";
448 data->temp_max = 105000;
450 case 2: /* Industrial (-40 °C to 105 °C) */
451 data->temp_grade = "Industrial";
452 data->temp_max = 105000;
454 case 3: /* Automotive (-40 °C to 125 °C) */
455 data->temp_grade = "Automotive";
456 data->temp_max = 125000;
461 * Set the critical trip point at 5 °C under max
462 * Set the passive trip point at 10 °C under max (changeable via sysfs)
464 trips[IMX_TRIP_PASSIVE].temperature = data->temp_max - (1000 * 10);
465 trips[IMX_TRIP_CRITICAL].temperature = data->temp_max - (1000 * 5);
468 static int imx_init_from_tempmon_data(struct platform_device *pdev)
474 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
478 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
482 ret = regmap_read(map, OCOTP_ANA1, &val);
484 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
487 ret = imx_init_calib(pdev, val);
491 ret = regmap_read(map, OCOTP_MEM0, &val);
493 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
496 imx_init_temp_grade(pdev, val);
501 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
506 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
510 ret = imx_init_calib(pdev, val);
514 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
517 imx_init_temp_grade(pdev, val);
522 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
524 struct imx_thermal_data *data = dev;
526 disable_irq_nosync(irq);
527 data->irq_enabled = false;
529 return IRQ_WAKE_THREAD;
532 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
534 struct imx_thermal_data *data = dev;
536 dev_dbg(data->dev, "THERMAL ALARM: T > %d\n", data->alarm_temp / 1000);
538 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
543 static const struct of_device_id of_imx_thermal_match[] = {
544 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
545 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
546 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
549 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
551 #ifdef CONFIG_CPU_FREQ
553 * Create cooling device in case no #cooling-cells property is available in
556 static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
558 struct device_node *np;
561 data->policy = cpufreq_cpu_get(0);
563 pr_debug("%s: CPUFreq policy not found\n", __func__);
564 return -EPROBE_DEFER;
567 np = of_get_cpu_node(data->policy->cpu, NULL);
569 if (!np || !of_property_present(np, "#cooling-cells")) {
570 data->cdev = cpufreq_cooling_register(data->policy);
571 if (IS_ERR(data->cdev)) {
572 ret = PTR_ERR(data->cdev);
573 cpufreq_cpu_put(data->policy);
582 static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
584 cpufreq_cooling_unregister(data->cdev);
585 cpufreq_cpu_put(data->policy);
590 static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
595 static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
600 static int imx_thermal_probe(struct platform_device *pdev)
602 struct imx_thermal_data *data;
607 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
611 data->dev = &pdev->dev;
613 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
616 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
621 data->socdata = of_device_get_match_data(&pdev->dev);
622 if (!data->socdata) {
623 dev_err(&pdev->dev, "no device match found\n");
627 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
628 if (data->socdata->version == TEMPMON_IMX6SX) {
629 regmap_write(map, IMX6_MISC1 + REG_CLR,
630 IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
631 | IMX6_MISC1_IRQ_TEMPPANIC);
633 * reset value of LOW ALARM is incorrect, set it to lowest
634 * value to avoid false trigger of low alarm.
636 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
637 data->socdata->low_alarm_mask);
640 data->irq = platform_get_irq(pdev, 0);
644 platform_set_drvdata(pdev, data);
646 if (of_property_present(pdev->dev.of_node, "nvmem-cells")) {
647 ret = imx_init_from_nvmem_cells(pdev);
649 return dev_err_probe(&pdev->dev, ret,
650 "failed to init from nvmem\n");
652 ret = imx_init_from_tempmon_data(pdev);
654 dev_err(&pdev->dev, "failed to init from fsl,tempmon-data\n");
659 /* Make sure sensor is in known good state for measurements */
660 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
661 data->socdata->power_down_mask);
662 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
663 data->socdata->measure_temp_mask);
664 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
665 data->socdata->measure_freq_mask);
666 if (data->socdata->version != TEMPMON_IMX7D)
667 regmap_write(map, IMX6_MISC0 + REG_SET,
668 IMX6_MISC0_REFTOP_SELBIASOFF);
669 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
670 data->socdata->power_down_mask);
672 ret = imx_thermal_register_legacy_cooling(data);
674 return dev_err_probe(&pdev->dev, ret,
675 "failed to register cpufreq cooling device\n");
677 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
678 if (IS_ERR(data->thermal_clk)) {
679 ret = PTR_ERR(data->thermal_clk);
680 if (ret != -EPROBE_DEFER)
682 "failed to get thermal clk: %d\n", ret);
687 * Thermal sensor needs clk on to get correct value, normally
688 * we should enable its clk before taking measurement and disable
689 * clk after measurement is done, but if alarm function is enabled,
690 * hardware will auto measure the temperature periodically, so we
691 * need to keep the clk always on for alarm function.
693 ret = clk_prepare_enable(data->thermal_clk);
695 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
699 data->tz = thermal_zone_device_register_with_trips("imx_thermal_zone",
702 BIT(IMX_TRIP_PASSIVE), data,
706 if (IS_ERR(data->tz)) {
707 ret = PTR_ERR(data->tz);
709 "failed to register thermal zone device %d\n", ret);
713 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
714 " critical:%dC passive:%dC\n", data->temp_grade,
715 data->temp_max / 1000, trips[IMX_TRIP_CRITICAL].temperature / 1000,
716 trips[IMX_TRIP_PASSIVE].temperature / 1000);
718 /* Enable measurements at ~ 10 Hz */
719 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
720 data->socdata->measure_freq_mask);
721 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
722 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
723 measure_freq << data->socdata->measure_freq_shift);
724 imx_set_alarm_temp(data, trips[IMX_TRIP_PASSIVE].temperature);
726 if (data->socdata->version == TEMPMON_IMX6SX)
727 imx_set_panic_temp(data, trips[IMX_TRIP_CRITICAL].temperature);
729 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
730 data->socdata->power_down_mask);
731 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
732 data->socdata->measure_temp_mask);
733 /* After power up, we need a delay before first access can be done. */
734 usleep_range(20, 50);
736 /* the core was configured and enabled just before */
737 pm_runtime_set_active(&pdev->dev);
738 pm_runtime_enable(data->dev);
740 ret = pm_runtime_resume_and_get(data->dev);
742 goto disable_runtime_pm;
744 data->irq_enabled = true;
745 ret = thermal_zone_device_enable(data->tz);
747 goto thermal_zone_unregister;
749 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
750 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
751 0, "imx_thermal", data);
753 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
754 goto thermal_zone_unregister;
757 pm_runtime_put(data->dev);
761 thermal_zone_unregister:
762 thermal_zone_device_unregister(data->tz);
764 pm_runtime_put_noidle(data->dev);
765 pm_runtime_disable(data->dev);
767 clk_disable_unprepare(data->thermal_clk);
769 imx_thermal_unregister_legacy_cooling(data);
774 static int imx_thermal_remove(struct platform_device *pdev)
776 struct imx_thermal_data *data = platform_get_drvdata(pdev);
778 pm_runtime_put_noidle(data->dev);
779 pm_runtime_disable(data->dev);
781 thermal_zone_device_unregister(data->tz);
782 imx_thermal_unregister_legacy_cooling(data);
787 static int __maybe_unused imx_thermal_suspend(struct device *dev)
789 struct imx_thermal_data *data = dev_get_drvdata(dev);
793 * Need to disable thermal sensor, otherwise, when thermal core
794 * try to get temperature before thermal sensor resume, a wrong
795 * temperature will be read as the thermal sensor is powered
796 * down. This is done in change_mode() operation called from
797 * thermal_zone_device_disable()
799 ret = thermal_zone_device_disable(data->tz);
803 return pm_runtime_force_suspend(data->dev);
806 static int __maybe_unused imx_thermal_resume(struct device *dev)
808 struct imx_thermal_data *data = dev_get_drvdata(dev);
811 ret = pm_runtime_force_resume(data->dev);
814 /* Enabled thermal sensor after resume */
815 return thermal_zone_device_enable(data->tz);
818 static int __maybe_unused imx_thermal_runtime_suspend(struct device *dev)
820 struct imx_thermal_data *data = dev_get_drvdata(dev);
821 const struct thermal_soc_data *socdata = data->socdata;
822 struct regmap *map = data->tempmon;
825 ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
826 socdata->measure_temp_mask);
830 ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
831 socdata->power_down_mask);
835 clk_disable_unprepare(data->thermal_clk);
840 static int __maybe_unused imx_thermal_runtime_resume(struct device *dev)
842 struct imx_thermal_data *data = dev_get_drvdata(dev);
843 const struct thermal_soc_data *socdata = data->socdata;
844 struct regmap *map = data->tempmon;
847 ret = clk_prepare_enable(data->thermal_clk);
851 ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
852 socdata->power_down_mask);
856 ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
857 socdata->measure_temp_mask);
862 * According to the temp sensor designers, it may require up to ~17us
863 * to complete a measurement.
865 usleep_range(20, 50);
870 static const struct dev_pm_ops imx_thermal_pm_ops = {
871 SET_SYSTEM_SLEEP_PM_OPS(imx_thermal_suspend, imx_thermal_resume)
872 SET_RUNTIME_PM_OPS(imx_thermal_runtime_suspend,
873 imx_thermal_runtime_resume, NULL)
876 static struct platform_driver imx_thermal = {
878 .name = "imx_thermal",
879 .pm = &imx_thermal_pm_ops,
880 .of_match_table = of_imx_thermal_match,
882 .probe = imx_thermal_probe,
883 .remove = imx_thermal_remove,
885 module_platform_driver(imx_thermal);
887 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
888 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
889 MODULE_LICENSE("GPL v2");
890 MODULE_ALIAS("platform:imx-thermal");