1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright 2013 Freescale Semiconductor, Inc.
7 #include <linux/cpufreq.h>
8 #include <linux/cpu_cooling.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/thermal.h>
23 #include <linux/types.h>
24 #include <linux/nvmem-consumer.h>
31 #define IMX6_MISC0 0x0150
32 #define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
33 #define IMX6_MISC1 0x0160
34 #define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
35 /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
36 #define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
37 #define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
39 #define IMX6_TEMPSENSE0 0x0180
40 #define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
41 #define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
42 #define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
43 #define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
44 #define IMX6_TEMPSENSE0_FINISHED (1 << 2)
45 #define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
46 #define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
48 #define IMX6_TEMPSENSE1 0x0190
49 #define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
50 #define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
52 #define OCOTP_MEM0 0x0480
53 #define OCOTP_ANA1 0x04e0
55 /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
56 #define IMX6_TEMPSENSE2 0x0290
57 #define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
58 #define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
59 #define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
60 #define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
63 #define IMX7_ANADIG_DIGPROG 0x800
64 #define IMX7_TEMPSENSE0 0x300
65 #define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
66 #define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
67 #define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
68 #define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
69 #define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
70 #define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
72 #define IMX7_TEMPSENSE1 0x310
73 #define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
74 #define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
75 #define IMX7_TEMPSENSE1_FINISHED (1 << 11)
76 #define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
77 #define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
78 #define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
79 #define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
81 /* The driver supports 1 passive trip point and 1 critical trip point */
82 enum imx_thermal_trip {
88 #define IMX_POLLING_DELAY 2000 /* millisecond */
89 #define IMX_PASSIVE_DELAY 1000
91 #define TEMPMON_IMX6Q 1
92 #define TEMPMON_IMX6SX 2
93 #define TEMPMON_IMX7D 3
95 struct thermal_soc_data {
100 u32 measure_temp_mask;
102 u32 measure_freq_ctrl;
103 u32 measure_freq_mask;
104 u32 measure_freq_shift;
108 u32 temp_value_shift;
111 u32 panic_alarm_ctrl;
112 u32 panic_alarm_mask;
113 u32 panic_alarm_shift;
117 u32 high_alarm_shift;
124 static struct thermal_soc_data thermal_imx6q_data = {
125 .version = TEMPMON_IMX6Q,
127 .sensor_ctrl = IMX6_TEMPSENSE0,
128 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
129 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
131 .measure_freq_ctrl = IMX6_TEMPSENSE1,
132 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
133 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
135 .temp_data = IMX6_TEMPSENSE0,
136 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
137 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
138 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
140 .high_alarm_ctrl = IMX6_TEMPSENSE0,
141 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
142 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
145 static struct thermal_soc_data thermal_imx6sx_data = {
146 .version = TEMPMON_IMX6SX,
148 .sensor_ctrl = IMX6_TEMPSENSE0,
149 .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
150 .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
152 .measure_freq_ctrl = IMX6_TEMPSENSE1,
153 .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
154 .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
156 .temp_data = IMX6_TEMPSENSE0,
157 .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
158 .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
159 .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
161 .high_alarm_ctrl = IMX6_TEMPSENSE0,
162 .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
163 .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
165 .panic_alarm_ctrl = IMX6_TEMPSENSE2,
166 .panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
167 .panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
169 .low_alarm_ctrl = IMX6_TEMPSENSE2,
170 .low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
171 .low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
174 static struct thermal_soc_data thermal_imx7d_data = {
175 .version = TEMPMON_IMX7D,
177 .sensor_ctrl = IMX7_TEMPSENSE1,
178 .power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
179 .measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
181 .measure_freq_ctrl = IMX7_TEMPSENSE1,
182 .measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
183 .measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
185 .temp_data = IMX7_TEMPSENSE1,
186 .temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
187 .temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
188 .temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
190 .panic_alarm_ctrl = IMX7_TEMPSENSE1,
191 .panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
192 .panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
194 .high_alarm_ctrl = IMX7_TEMPSENSE0,
195 .high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
196 .high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
198 .low_alarm_ctrl = IMX7_TEMPSENSE0,
199 .low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
200 .low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
203 struct imx_thermal_data {
204 struct cpufreq_policy *policy;
205 struct thermal_zone_device *tz;
206 struct thermal_cooling_device *cdev;
207 enum thermal_device_mode mode;
208 struct regmap *tempmon;
209 u32 c1, c2; /* See formula in imx_init_calib() */
217 struct clk *thermal_clk;
218 const struct thermal_soc_data *socdata;
219 const char *temp_grade;
222 static void imx_set_panic_temp(struct imx_thermal_data *data,
225 const struct thermal_soc_data *soc_data = data->socdata;
226 struct regmap *map = data->tempmon;
229 critical_value = (data->c2 - panic_temp) / data->c1;
231 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
232 soc_data->panic_alarm_mask);
233 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
234 critical_value << soc_data->panic_alarm_shift);
237 static void imx_set_alarm_temp(struct imx_thermal_data *data,
240 struct regmap *map = data->tempmon;
241 const struct thermal_soc_data *soc_data = data->socdata;
244 data->alarm_temp = alarm_temp;
246 if (data->socdata->version == TEMPMON_IMX7D)
247 alarm_value = alarm_temp / 1000 + data->c1 - 25;
249 alarm_value = (data->c2 - alarm_temp) / data->c1;
251 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
252 soc_data->high_alarm_mask);
253 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
254 alarm_value << soc_data->high_alarm_shift);
257 static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
259 struct imx_thermal_data *data = tz->devdata;
260 const struct thermal_soc_data *soc_data = data->socdata;
261 struct regmap *map = data->tempmon;
266 if (data->mode == THERMAL_DEVICE_ENABLED) {
267 /* Check if a measurement is currently in progress */
268 regmap_read(map, soc_data->temp_data, &val);
269 wait = !(val & soc_data->temp_valid_mask);
272 * Every time we measure the temperature, we will power on the
273 * temperature sensor, enable measurements, take a reading,
274 * disable measurements, power off the temperature sensor.
276 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
277 soc_data->power_down_mask);
278 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
279 soc_data->measure_temp_mask);
285 * According to the temp sensor designers, it may require up to ~17us
286 * to complete a measurement.
289 usleep_range(20, 50);
291 regmap_read(map, soc_data->temp_data, &val);
293 if (data->mode != THERMAL_DEVICE_ENABLED) {
294 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
295 soc_data->measure_temp_mask);
296 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
297 soc_data->power_down_mask);
300 if ((val & soc_data->temp_valid_mask) == 0) {
301 dev_dbg(&tz->device, "temp measurement never finished\n");
305 n_meas = (val & soc_data->temp_value_mask)
306 >> soc_data->temp_value_shift;
308 /* See imx_init_calib() for formula derivation */
309 if (data->socdata->version == TEMPMON_IMX7D)
310 *temp = (n_meas - data->c1 + 25) * 1000;
312 *temp = data->c2 - n_meas * data->c1;
314 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
315 if (data->socdata->version == TEMPMON_IMX6Q) {
316 if (data->alarm_temp == data->temp_passive &&
317 *temp >= data->temp_passive)
318 imx_set_alarm_temp(data, data->temp_critical);
319 if (data->alarm_temp == data->temp_critical &&
320 *temp < data->temp_passive) {
321 imx_set_alarm_temp(data, data->temp_passive);
322 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
323 data->alarm_temp / 1000);
327 if (*temp != data->last_temp) {
328 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
329 data->last_temp = *temp;
332 /* Reenable alarm IRQ if temperature below alarm temperature */
333 if (!data->irq_enabled && *temp < data->alarm_temp) {
334 data->irq_enabled = true;
335 enable_irq(data->irq);
341 static int imx_get_mode(struct thermal_zone_device *tz,
342 enum thermal_device_mode *mode)
344 struct imx_thermal_data *data = tz->devdata;
351 static int imx_set_mode(struct thermal_zone_device *tz,
352 enum thermal_device_mode mode)
354 struct imx_thermal_data *data = tz->devdata;
355 struct regmap *map = data->tempmon;
356 const struct thermal_soc_data *soc_data = data->socdata;
358 if (mode == THERMAL_DEVICE_ENABLED) {
359 tz->polling_delay = IMX_POLLING_DELAY;
360 tz->passive_delay = IMX_PASSIVE_DELAY;
362 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
363 soc_data->power_down_mask);
364 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
365 soc_data->measure_temp_mask);
367 if (!data->irq_enabled) {
368 data->irq_enabled = true;
369 enable_irq(data->irq);
372 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
373 soc_data->measure_temp_mask);
374 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
375 soc_data->power_down_mask);
377 tz->polling_delay = 0;
378 tz->passive_delay = 0;
380 if (data->irq_enabled) {
381 disable_irq(data->irq);
382 data->irq_enabled = false;
387 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
392 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
393 enum thermal_trip_type *type)
395 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
396 THERMAL_TRIP_CRITICAL;
400 static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
402 struct imx_thermal_data *data = tz->devdata;
404 *temp = data->temp_critical;
408 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
411 struct imx_thermal_data *data = tz->devdata;
413 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
418 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
421 struct imx_thermal_data *data = tz->devdata;
423 /* do not allow changing critical threshold */
424 if (trip == IMX_TRIP_CRITICAL)
427 /* do not allow passive to be set higher than critical */
428 if (temp < 0 || temp > data->temp_critical)
431 data->temp_passive = temp;
433 imx_set_alarm_temp(data, temp);
438 static int imx_bind(struct thermal_zone_device *tz,
439 struct thermal_cooling_device *cdev)
443 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
446 THERMAL_WEIGHT_DEFAULT);
449 "binding zone %s with cdev %s failed:%d\n",
450 tz->type, cdev->type, ret);
457 static int imx_unbind(struct thermal_zone_device *tz,
458 struct thermal_cooling_device *cdev)
462 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
465 "unbinding zone %s with cdev %s failed:%d\n",
466 tz->type, cdev->type, ret);
473 static struct thermal_zone_device_ops imx_tz_ops = {
475 .unbind = imx_unbind,
476 .get_temp = imx_get_temp,
477 .get_mode = imx_get_mode,
478 .set_mode = imx_set_mode,
479 .get_trip_type = imx_get_trip_type,
480 .get_trip_temp = imx_get_trip_temp,
481 .get_crit_temp = imx_get_crit_temp,
482 .set_trip_temp = imx_set_trip_temp,
485 static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
487 struct imx_thermal_data *data = platform_get_drvdata(pdev);
491 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
492 dev_err(&pdev->dev, "invalid sensor calibration data\n");
497 * On i.MX7D, we only use the calibration data at 25C to get the temp,
498 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
500 if (data->socdata->version == TEMPMON_IMX7D) {
501 data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
506 * The sensor is calibrated at 25 °C (aka T1) and the value measured
507 * (aka N1) at this temperature is provided in bits [31:20] in the
508 * i.MX's OCOTP value ANA1.
509 * To find the actual temperature T, the following formula has to be used
510 * when reading value n from the sensor:
512 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
513 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
514 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
519 * T1' = 28.580661 °C
520 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
521 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
524 n1 = ocotp_ana1 >> 20;
526 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
527 temp64 *= 1000; /* to get result in °mC */
528 do_div(temp64, 15423 * n1 - 4148468);
530 data->c2 = n1 * data->c1 + 28581;
535 static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
537 struct imx_thermal_data *data = platform_get_drvdata(pdev);
539 /* The maximum die temp is specified by the Temperature Grade */
540 switch ((ocotp_mem0 >> 6) & 0x3) {
541 case 0: /* Commercial (0 to 95 °C) */
542 data->temp_grade = "Commercial";
543 data->temp_max = 95000;
545 case 1: /* Extended Commercial (-20 °C to 105 °C) */
546 data->temp_grade = "Extended Commercial";
547 data->temp_max = 105000;
549 case 2: /* Industrial (-40 °C to 105 °C) */
550 data->temp_grade = "Industrial";
551 data->temp_max = 105000;
553 case 3: /* Automotive (-40 °C to 125 °C) */
554 data->temp_grade = "Automotive";
555 data->temp_max = 125000;
560 * Set the critical trip point at 5 °C under max
561 * Set the passive trip point at 10 °C under max (changeable via sysfs)
563 data->temp_critical = data->temp_max - (1000 * 5);
564 data->temp_passive = data->temp_max - (1000 * 10);
567 static int imx_init_from_tempmon_data(struct platform_device *pdev)
573 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
577 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
581 ret = regmap_read(map, OCOTP_ANA1, &val);
583 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
586 ret = imx_init_calib(pdev, val);
590 ret = regmap_read(map, OCOTP_MEM0, &val);
592 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
595 imx_init_temp_grade(pdev, val);
600 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
605 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
609 ret = imx_init_calib(pdev, val);
613 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
616 imx_init_temp_grade(pdev, val);
621 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
623 struct imx_thermal_data *data = dev;
625 disable_irq_nosync(irq);
626 data->irq_enabled = false;
628 return IRQ_WAKE_THREAD;
631 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
633 struct imx_thermal_data *data = dev;
635 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
636 data->alarm_temp / 1000);
638 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
643 static const struct of_device_id of_imx_thermal_match[] = {
644 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
645 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
646 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
649 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
652 * Create cooling device in case no #cooling-cells property is available in
655 static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
657 struct device_node *np = of_get_cpu_node(data->policy->cpu, NULL);
660 if (!np || !of_find_property(np, "#cooling-cells", NULL)) {
661 data->cdev = cpufreq_cooling_register(data->policy);
662 if (IS_ERR(data->cdev)) {
663 ret = PTR_ERR(data->cdev);
664 cpufreq_cpu_put(data->policy);
672 static int imx_thermal_probe(struct platform_device *pdev)
674 struct imx_thermal_data *data;
679 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
683 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
686 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
691 data->socdata = of_device_get_match_data(&pdev->dev);
692 if (!data->socdata) {
693 dev_err(&pdev->dev, "no device match found\n");
697 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
698 if (data->socdata->version == TEMPMON_IMX6SX) {
699 regmap_write(map, IMX6_MISC1 + REG_CLR,
700 IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
701 | IMX6_MISC1_IRQ_TEMPPANIC);
703 * reset value of LOW ALARM is incorrect, set it to lowest
704 * value to avoid false trigger of low alarm.
706 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
707 data->socdata->low_alarm_mask);
710 data->irq = platform_get_irq(pdev, 0);
714 platform_set_drvdata(pdev, data);
716 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
717 ret = imx_init_from_nvmem_cells(pdev);
718 if (ret == -EPROBE_DEFER)
721 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
726 ret = imx_init_from_tempmon_data(pdev);
728 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
733 /* Make sure sensor is in known good state for measurements */
734 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
735 data->socdata->power_down_mask);
736 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
737 data->socdata->measure_temp_mask);
738 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
739 data->socdata->measure_freq_mask);
740 if (data->socdata->version != TEMPMON_IMX7D)
741 regmap_write(map, IMX6_MISC0 + REG_SET,
742 IMX6_MISC0_REFTOP_SELBIASOFF);
743 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
744 data->socdata->power_down_mask);
746 data->policy = cpufreq_cpu_get(0);
748 pr_debug("%s: CPUFreq policy not found\n", __func__);
749 return -EPROBE_DEFER;
752 ret = imx_thermal_register_legacy_cooling(data);
755 "failed to register cpufreq cooling device: %d\n", ret);
759 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
760 if (IS_ERR(data->thermal_clk)) {
761 ret = PTR_ERR(data->thermal_clk);
762 if (ret != -EPROBE_DEFER)
764 "failed to get thermal clk: %d\n", ret);
765 cpufreq_cooling_unregister(data->cdev);
766 cpufreq_cpu_put(data->policy);
771 * Thermal sensor needs clk on to get correct value, normally
772 * we should enable its clk before taking measurement and disable
773 * clk after measurement is done, but if alarm function is enabled,
774 * hardware will auto measure the temperature periodically, so we
775 * need to keep the clk always on for alarm function.
777 ret = clk_prepare_enable(data->thermal_clk);
779 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
780 cpufreq_cooling_unregister(data->cdev);
781 cpufreq_cpu_put(data->policy);
785 data->tz = thermal_zone_device_register("imx_thermal_zone",
787 BIT(IMX_TRIP_PASSIVE), data,
791 if (IS_ERR(data->tz)) {
792 ret = PTR_ERR(data->tz);
794 "failed to register thermal zone device %d\n", ret);
795 clk_disable_unprepare(data->thermal_clk);
796 cpufreq_cooling_unregister(data->cdev);
797 cpufreq_cpu_put(data->policy);
801 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
802 " critical:%dC passive:%dC\n", data->temp_grade,
803 data->temp_max / 1000, data->temp_critical / 1000,
804 data->temp_passive / 1000);
806 /* Enable measurements at ~ 10 Hz */
807 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
808 data->socdata->measure_freq_mask);
809 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
810 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
811 measure_freq << data->socdata->measure_freq_shift);
812 imx_set_alarm_temp(data, data->temp_passive);
814 if (data->socdata->version == TEMPMON_IMX6SX)
815 imx_set_panic_temp(data, data->temp_critical);
817 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
818 data->socdata->power_down_mask);
819 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
820 data->socdata->measure_temp_mask);
822 data->irq_enabled = true;
823 data->mode = THERMAL_DEVICE_ENABLED;
825 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
826 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
827 0, "imx_thermal", data);
829 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
830 clk_disable_unprepare(data->thermal_clk);
831 thermal_zone_device_unregister(data->tz);
832 cpufreq_cooling_unregister(data->cdev);
833 cpufreq_cpu_put(data->policy);
840 static int imx_thermal_remove(struct platform_device *pdev)
842 struct imx_thermal_data *data = platform_get_drvdata(pdev);
843 struct regmap *map = data->tempmon;
845 /* Disable measurements */
846 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
847 data->socdata->power_down_mask);
848 if (!IS_ERR(data->thermal_clk))
849 clk_disable_unprepare(data->thermal_clk);
851 thermal_zone_device_unregister(data->tz);
852 cpufreq_cooling_unregister(data->cdev);
853 cpufreq_cpu_put(data->policy);
858 #ifdef CONFIG_PM_SLEEP
859 static int imx_thermal_suspend(struct device *dev)
861 struct imx_thermal_data *data = dev_get_drvdata(dev);
862 struct regmap *map = data->tempmon;
865 * Need to disable thermal sensor, otherwise, when thermal core
866 * try to get temperature before thermal sensor resume, a wrong
867 * temperature will be read as the thermal sensor is powered
870 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
871 data->socdata->measure_temp_mask);
872 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
873 data->socdata->power_down_mask);
874 data->mode = THERMAL_DEVICE_DISABLED;
875 clk_disable_unprepare(data->thermal_clk);
880 static int imx_thermal_resume(struct device *dev)
882 struct imx_thermal_data *data = dev_get_drvdata(dev);
883 struct regmap *map = data->tempmon;
886 ret = clk_prepare_enable(data->thermal_clk);
889 /* Enabled thermal sensor after resume */
890 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
891 data->socdata->power_down_mask);
892 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
893 data->socdata->measure_temp_mask);
894 data->mode = THERMAL_DEVICE_ENABLED;
900 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
901 imx_thermal_suspend, imx_thermal_resume);
903 static struct platform_driver imx_thermal = {
905 .name = "imx_thermal",
906 .pm = &imx_thermal_pm_ops,
907 .of_match_table = of_imx_thermal_match,
909 .probe = imx_thermal_probe,
910 .remove = imx_thermal_remove,
912 module_platform_driver(imx_thermal);
914 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
915 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
916 MODULE_LICENSE("GPL v2");
917 MODULE_ALIAS("platform:imx-thermal");