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/device.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/thermal.h>
22 #include <linux/types.h>
23 #include <linux/nvmem-consumer.h>
30 #define IMX6_MISC0 0x0150
31 #define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
32 #define IMX6_MISC1 0x0160
33 #define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
34 /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
35 #define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
36 #define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
38 #define IMX6_TEMPSENSE0 0x0180
39 #define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
40 #define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
41 #define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
42 #define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
43 #define IMX6_TEMPSENSE0_FINISHED (1 << 2)
44 #define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
45 #define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
47 #define IMX6_TEMPSENSE1 0x0190
48 #define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
49 #define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
51 #define OCOTP_MEM0 0x0480
52 #define OCOTP_ANA1 0x04e0
54 /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
55 #define IMX6_TEMPSENSE2 0x0290
56 #define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
57 #define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
58 #define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
59 #define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
62 #define IMX7_ANADIG_DIGPROG 0x800
63 #define IMX7_TEMPSENSE0 0x300
64 #define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
65 #define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
66 #define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
67 #define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
68 #define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
69 #define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
71 #define IMX7_TEMPSENSE1 0x310
72 #define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
73 #define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
74 #define IMX7_TEMPSENSE1_FINISHED (1 << 11)
75 #define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
76 #define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
77 #define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
78 #define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
80 /* The driver supports 1 passive trip point and 1 critical trip point */
81 enum imx_thermal_trip {
87 #define IMX_POLLING_DELAY 2000 /* millisecond */
88 #define IMX_PASSIVE_DELAY 1000
90 #define TEMPMON_IMX6Q 1
91 #define TEMPMON_IMX6SX 2
92 #define TEMPMON_IMX7D 3
94 struct thermal_soc_data {
99 u32 measure_temp_mask;
101 u32 measure_freq_ctrl;
102 u32 measure_freq_mask;
103 u32 measure_freq_shift;
107 u32 temp_value_shift;
110 u32 panic_alarm_ctrl;
111 u32 panic_alarm_mask;
112 u32 panic_alarm_shift;
116 u32 high_alarm_shift;
123 static struct thermal_soc_data thermal_imx6q_data = {
124 .version = TEMPMON_IMX6Q,
126 .sensor_ctrl = IMX6_TEMPSENSE0,
127 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
128 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
130 .measure_freq_ctrl = IMX6_TEMPSENSE1,
131 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
132 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
134 .temp_data = IMX6_TEMPSENSE0,
135 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
136 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
137 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
139 .high_alarm_ctrl = IMX6_TEMPSENSE0,
140 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
141 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
144 static struct thermal_soc_data thermal_imx6sx_data = {
145 .version = TEMPMON_IMX6SX,
147 .sensor_ctrl = IMX6_TEMPSENSE0,
148 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
149 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
151 .measure_freq_ctrl = IMX6_TEMPSENSE1,
152 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
153 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
155 .temp_data = IMX6_TEMPSENSE0,
156 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
157 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
158 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
160 .high_alarm_ctrl = IMX6_TEMPSENSE0,
161 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
162 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
164 .panic_alarm_ctrl = IMX6_TEMPSENSE2,
165 .panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
166 .panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
168 .low_alarm_ctrl = IMX6_TEMPSENSE2,
169 .low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
170 .low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
173 static struct thermal_soc_data thermal_imx7d_data = {
174 .version = TEMPMON_IMX7D,
176 .sensor_ctrl = IMX7_TEMPSENSE1,
177 .power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
178 .measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
180 .measure_freq_ctrl = IMX7_TEMPSENSE1,
181 .measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
182 .measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
184 .temp_data = IMX7_TEMPSENSE1,
185 .temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
186 .temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
187 .temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
189 .panic_alarm_ctrl = IMX7_TEMPSENSE1,
190 .panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
191 .panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
193 .high_alarm_ctrl = IMX7_TEMPSENSE0,
194 .high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
195 .high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
197 .low_alarm_ctrl = IMX7_TEMPSENSE0,
198 .low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
199 .low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
202 struct imx_thermal_data {
203 struct cpufreq_policy *policy;
204 struct thermal_zone_device *tz;
205 struct thermal_cooling_device *cdev;
206 enum thermal_device_mode mode;
207 struct regmap *tempmon;
208 u32 c1, c2; /* See formula in imx_init_calib() */
216 struct clk *thermal_clk;
217 const struct thermal_soc_data *socdata;
218 const char *temp_grade;
221 static void imx_set_panic_temp(struct imx_thermal_data *data,
224 const struct thermal_soc_data *soc_data = data->socdata;
225 struct regmap *map = data->tempmon;
228 critical_value = (data->c2 - panic_temp) / data->c1;
230 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
231 soc_data->panic_alarm_mask);
232 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
233 critical_value << soc_data->panic_alarm_shift);
236 static void imx_set_alarm_temp(struct imx_thermal_data *data,
239 struct regmap *map = data->tempmon;
240 const struct thermal_soc_data *soc_data = data->socdata;
243 data->alarm_temp = alarm_temp;
245 if (data->socdata->version == TEMPMON_IMX7D)
246 alarm_value = alarm_temp / 1000 + data->c1 - 25;
248 alarm_value = (data->c2 - alarm_temp) / data->c1;
250 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
251 soc_data->high_alarm_mask);
252 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
253 alarm_value << soc_data->high_alarm_shift);
256 static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
258 struct imx_thermal_data *data = tz->devdata;
259 const struct thermal_soc_data *soc_data = data->socdata;
260 struct regmap *map = data->tempmon;
265 if (data->mode == THERMAL_DEVICE_ENABLED) {
266 /* Check if a measurement is currently in progress */
267 regmap_read(map, soc_data->temp_data, &val);
268 wait = !(val & soc_data->temp_valid_mask);
271 * Every time we measure the temperature, we will power on the
272 * temperature sensor, enable measurements, take a reading,
273 * disable measurements, power off the temperature sensor.
275 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
276 soc_data->power_down_mask);
277 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
278 soc_data->measure_temp_mask);
284 * According to the temp sensor designers, it may require up to ~17us
285 * to complete a measurement.
288 usleep_range(20, 50);
290 regmap_read(map, soc_data->temp_data, &val);
292 if (data->mode != THERMAL_DEVICE_ENABLED) {
293 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
294 soc_data->measure_temp_mask);
295 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
296 soc_data->power_down_mask);
299 if ((val & soc_data->temp_valid_mask) == 0) {
300 dev_dbg(&tz->device, "temp measurement never finished\n");
304 n_meas = (val & soc_data->temp_value_mask)
305 >> soc_data->temp_value_shift;
307 /* See imx_init_calib() for formula derivation */
308 if (data->socdata->version == TEMPMON_IMX7D)
309 *temp = (n_meas - data->c1 + 25) * 1000;
311 *temp = data->c2 - n_meas * data->c1;
313 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
314 if (data->socdata->version == TEMPMON_IMX6Q) {
315 if (data->alarm_temp == data->temp_passive &&
316 *temp >= data->temp_passive)
317 imx_set_alarm_temp(data, data->temp_critical);
318 if (data->alarm_temp == data->temp_critical &&
319 *temp < data->temp_passive) {
320 imx_set_alarm_temp(data, data->temp_passive);
321 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
322 data->alarm_temp / 1000);
326 if (*temp != data->last_temp) {
327 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
328 data->last_temp = *temp;
331 /* Reenable alarm IRQ if temperature below alarm temperature */
332 if (!data->irq_enabled && *temp < data->alarm_temp) {
333 data->irq_enabled = true;
334 enable_irq(data->irq);
340 static int imx_get_mode(struct thermal_zone_device *tz,
341 enum thermal_device_mode *mode)
343 struct imx_thermal_data *data = tz->devdata;
350 static int imx_set_mode(struct thermal_zone_device *tz,
351 enum thermal_device_mode mode)
353 struct imx_thermal_data *data = tz->devdata;
354 struct regmap *map = data->tempmon;
355 const struct thermal_soc_data *soc_data = data->socdata;
357 if (mode == THERMAL_DEVICE_ENABLED) {
358 tz->polling_delay = IMX_POLLING_DELAY;
359 tz->passive_delay = IMX_PASSIVE_DELAY;
361 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
362 soc_data->power_down_mask);
363 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
364 soc_data->measure_temp_mask);
366 if (!data->irq_enabled) {
367 data->irq_enabled = true;
368 enable_irq(data->irq);
371 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
372 soc_data->measure_temp_mask);
373 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
374 soc_data->power_down_mask);
376 tz->polling_delay = 0;
377 tz->passive_delay = 0;
379 if (data->irq_enabled) {
380 disable_irq(data->irq);
381 data->irq_enabled = false;
386 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
391 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
392 enum thermal_trip_type *type)
394 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
395 THERMAL_TRIP_CRITICAL;
399 static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
401 struct imx_thermal_data *data = tz->devdata;
403 *temp = data->temp_critical;
407 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
410 struct imx_thermal_data *data = tz->devdata;
412 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
417 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
420 struct imx_thermal_data *data = tz->devdata;
422 /* do not allow changing critical threshold */
423 if (trip == IMX_TRIP_CRITICAL)
426 /* do not allow passive to be set higher than critical */
427 if (temp < 0 || temp > data->temp_critical)
430 data->temp_passive = temp;
432 imx_set_alarm_temp(data, temp);
437 static int imx_bind(struct thermal_zone_device *tz,
438 struct thermal_cooling_device *cdev)
442 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
445 THERMAL_WEIGHT_DEFAULT);
448 "binding zone %s with cdev %s failed:%d\n",
449 tz->type, cdev->type, ret);
456 static int imx_unbind(struct thermal_zone_device *tz,
457 struct thermal_cooling_device *cdev)
461 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
464 "unbinding zone %s with cdev %s failed:%d\n",
465 tz->type, cdev->type, ret);
472 static struct thermal_zone_device_ops imx_tz_ops = {
474 .unbind = imx_unbind,
475 .get_temp = imx_get_temp,
476 .get_mode = imx_get_mode,
477 .set_mode = imx_set_mode,
478 .get_trip_type = imx_get_trip_type,
479 .get_trip_temp = imx_get_trip_temp,
480 .get_crit_temp = imx_get_crit_temp,
481 .set_trip_temp = imx_set_trip_temp,
484 static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
486 struct imx_thermal_data *data = platform_get_drvdata(pdev);
490 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
491 dev_err(&pdev->dev, "invalid sensor calibration data\n");
496 * On i.MX7D, we only use the calibration data at 25C to get the temp,
497 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
499 if (data->socdata->version == TEMPMON_IMX7D) {
500 data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
505 * The sensor is calibrated at 25 °C (aka T1) and the value measured
506 * (aka N1) at this temperature is provided in bits [31:20] in the
507 * i.MX's OCOTP value ANA1.
508 * To find the actual temperature T, the following formula has to be used
509 * when reading value n from the sensor:
511 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
512 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
513 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
518 * T1' = 28.580661 °C
519 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
520 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
523 n1 = ocotp_ana1 >> 20;
525 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
526 temp64 *= 1000; /* to get result in °mC */
527 do_div(temp64, 15423 * n1 - 4148468);
529 data->c2 = n1 * data->c1 + 28581;
534 static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
536 struct imx_thermal_data *data = platform_get_drvdata(pdev);
538 /* The maximum die temp is specified by the Temperature Grade */
539 switch ((ocotp_mem0 >> 6) & 0x3) {
540 case 0: /* Commercial (0 to 95 °C) */
541 data->temp_grade = "Commercial";
542 data->temp_max = 95000;
544 case 1: /* Extended Commercial (-20 °C to 105 °C) */
545 data->temp_grade = "Extended Commercial";
546 data->temp_max = 105000;
548 case 2: /* Industrial (-40 °C to 105 °C) */
549 data->temp_grade = "Industrial";
550 data->temp_max = 105000;
552 case 3: /* Automotive (-40 °C to 125 °C) */
553 data->temp_grade = "Automotive";
554 data->temp_max = 125000;
559 * Set the critical trip point at 5 °C under max
560 * Set the passive trip point at 10 °C under max (changeable via sysfs)
562 data->temp_critical = data->temp_max - (1000 * 5);
563 data->temp_passive = data->temp_max - (1000 * 10);
566 static int imx_init_from_tempmon_data(struct platform_device *pdev)
572 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
576 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
580 ret = regmap_read(map, OCOTP_ANA1, &val);
582 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
585 ret = imx_init_calib(pdev, val);
589 ret = regmap_read(map, OCOTP_MEM0, &val);
591 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
594 imx_init_temp_grade(pdev, val);
599 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
604 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
607 imx_init_calib(pdev, val);
609 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
612 imx_init_temp_grade(pdev, val);
617 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
619 struct imx_thermal_data *data = dev;
621 disable_irq_nosync(irq);
622 data->irq_enabled = false;
624 return IRQ_WAKE_THREAD;
627 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
629 struct imx_thermal_data *data = dev;
631 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
632 data->alarm_temp / 1000);
634 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
639 static const struct of_device_id of_imx_thermal_match[] = {
640 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
641 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
642 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
645 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
647 static int imx_thermal_probe(struct platform_device *pdev)
649 struct imx_thermal_data *data;
654 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
658 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
661 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
666 data->socdata = of_device_get_match_data(&pdev->dev);
667 if (!data->socdata) {
668 dev_err(&pdev->dev, "no device match found\n");
672 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
673 if (data->socdata->version == TEMPMON_IMX6SX) {
674 regmap_write(map, IMX6_MISC1 + REG_CLR,
675 IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
676 | IMX6_MISC1_IRQ_TEMPPANIC);
678 * reset value of LOW ALARM is incorrect, set it to lowest
679 * value to avoid false trigger of low alarm.
681 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
682 data->socdata->low_alarm_mask);
685 data->irq = platform_get_irq(pdev, 0);
689 platform_set_drvdata(pdev, data);
691 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
692 ret = imx_init_from_nvmem_cells(pdev);
693 if (ret == -EPROBE_DEFER)
696 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
701 ret = imx_init_from_tempmon_data(pdev);
703 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
708 /* Make sure sensor is in known good state for measurements */
709 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
710 data->socdata->power_down_mask);
711 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
712 data->socdata->measure_temp_mask);
713 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
714 data->socdata->measure_freq_mask);
715 if (data->socdata->version != TEMPMON_IMX7D)
716 regmap_write(map, IMX6_MISC0 + REG_SET,
717 IMX6_MISC0_REFTOP_SELBIASOFF);
718 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
719 data->socdata->power_down_mask);
721 data->policy = cpufreq_cpu_get(0);
723 pr_debug("%s: CPUFreq policy not found\n", __func__);
724 return -EPROBE_DEFER;
727 data->cdev = cpufreq_cooling_register(data->policy);
728 if (IS_ERR(data->cdev)) {
729 ret = PTR_ERR(data->cdev);
731 "failed to register cpufreq cooling device: %d\n", ret);
732 cpufreq_cpu_put(data->policy);
736 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
737 if (IS_ERR(data->thermal_clk)) {
738 ret = PTR_ERR(data->thermal_clk);
739 if (ret != -EPROBE_DEFER)
741 "failed to get thermal clk: %d\n", ret);
742 cpufreq_cooling_unregister(data->cdev);
743 cpufreq_cpu_put(data->policy);
748 * Thermal sensor needs clk on to get correct value, normally
749 * we should enable its clk before taking measurement and disable
750 * clk after measurement is done, but if alarm function is enabled,
751 * hardware will auto measure the temperature periodically, so we
752 * need to keep the clk always on for alarm function.
754 ret = clk_prepare_enable(data->thermal_clk);
756 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
757 cpufreq_cooling_unregister(data->cdev);
758 cpufreq_cpu_put(data->policy);
762 data->tz = thermal_zone_device_register("imx_thermal_zone",
764 BIT(IMX_TRIP_PASSIVE), data,
768 if (IS_ERR(data->tz)) {
769 ret = PTR_ERR(data->tz);
771 "failed to register thermal zone device %d\n", ret);
772 clk_disable_unprepare(data->thermal_clk);
773 cpufreq_cooling_unregister(data->cdev);
774 cpufreq_cpu_put(data->policy);
778 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
779 " critical:%dC passive:%dC\n", data->temp_grade,
780 data->temp_max / 1000, data->temp_critical / 1000,
781 data->temp_passive / 1000);
783 /* Enable measurements at ~ 10 Hz */
784 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
785 data->socdata->measure_freq_mask);
786 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
787 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
788 measure_freq << data->socdata->measure_freq_shift);
789 imx_set_alarm_temp(data, data->temp_passive);
791 if (data->socdata->version == TEMPMON_IMX6SX)
792 imx_set_panic_temp(data, data->temp_critical);
794 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
795 data->socdata->power_down_mask);
796 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
797 data->socdata->measure_temp_mask);
799 data->irq_enabled = true;
800 data->mode = THERMAL_DEVICE_ENABLED;
802 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
803 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
804 0, "imx_thermal", data);
806 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
807 clk_disable_unprepare(data->thermal_clk);
808 thermal_zone_device_unregister(data->tz);
809 cpufreq_cooling_unregister(data->cdev);
810 cpufreq_cpu_put(data->policy);
817 static int imx_thermal_remove(struct platform_device *pdev)
819 struct imx_thermal_data *data = platform_get_drvdata(pdev);
820 struct regmap *map = data->tempmon;
822 /* Disable measurements */
823 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
824 data->socdata->power_down_mask);
825 if (!IS_ERR(data->thermal_clk))
826 clk_disable_unprepare(data->thermal_clk);
828 thermal_zone_device_unregister(data->tz);
829 cpufreq_cooling_unregister(data->cdev);
830 cpufreq_cpu_put(data->policy);
835 #ifdef CONFIG_PM_SLEEP
836 static int imx_thermal_suspend(struct device *dev)
838 struct imx_thermal_data *data = dev_get_drvdata(dev);
839 struct regmap *map = data->tempmon;
842 * Need to disable thermal sensor, otherwise, when thermal core
843 * try to get temperature before thermal sensor resume, a wrong
844 * temperature will be read as the thermal sensor is powered
847 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
848 data->socdata->measure_temp_mask);
849 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
850 data->socdata->power_down_mask);
851 data->mode = THERMAL_DEVICE_DISABLED;
852 clk_disable_unprepare(data->thermal_clk);
857 static int imx_thermal_resume(struct device *dev)
859 struct imx_thermal_data *data = dev_get_drvdata(dev);
860 struct regmap *map = data->tempmon;
863 ret = clk_prepare_enable(data->thermal_clk);
866 /* Enabled thermal sensor after resume */
867 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
868 data->socdata->power_down_mask);
869 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
870 data->socdata->measure_temp_mask);
871 data->mode = THERMAL_DEVICE_ENABLED;
877 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
878 imx_thermal_suspend, imx_thermal_resume);
880 static struct platform_driver imx_thermal = {
882 .name = "imx_thermal",
883 .pm = &imx_thermal_pm_ops,
884 .of_match_table = of_imx_thermal_match,
886 .probe = imx_thermal_probe,
887 .remove = imx_thermal_remove,
889 module_platform_driver(imx_thermal);
891 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
892 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
893 MODULE_LICENSE("GPL v2");
894 MODULE_ALIAS("platform:imx-thermal");