xref: /linux/drivers/thermal/imx_thermal.c (revision 45d8b572fac3aa8b49d53c946b3685eaf78a2824)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright 2013 Freescale Semiconductor, Inc.
4 
5 #include <linux/clk.h>
6 #include <linux/cpufreq.h>
7 #include <linux/cpu_cooling.h>
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
13 #include <linux/of.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>
19 
20 #define REG_SET		0x4
21 #define REG_CLR		0x8
22 #define REG_TOG		0xc
23 
24 /* i.MX6 specific */
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)
32 
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)
41 
42 #define IMX6_TEMPSENSE1				0x0190
43 #define IMX6_TEMPSENSE1_MEASURE_FREQ		0xffff
44 #define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT	0
45 
46 #define OCOTP_MEM0			0x0480
47 #define OCOTP_ANA1			0x04e0
48 
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
55 
56 /* i.MX7 specific */
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
65 
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
74 
75 /* The driver supports 1 passive trip point and 1 critical trip point */
76 enum imx_thermal_trip {
77 	IMX_TRIP_PASSIVE,
78 	IMX_TRIP_CRITICAL,
79 };
80 
81 #define IMX_POLLING_DELAY		2000 /* millisecond */
82 #define IMX_PASSIVE_DELAY		1000
83 
84 #define TEMPMON_IMX6Q			1
85 #define TEMPMON_IMX6SX			2
86 #define TEMPMON_IMX7D			3
87 
88 struct thermal_soc_data {
89 	u32 version;
90 
91 	u32 sensor_ctrl;
92 	u32 power_down_mask;
93 	u32 measure_temp_mask;
94 
95 	u32 measure_freq_ctrl;
96 	u32 measure_freq_mask;
97 	u32 measure_freq_shift;
98 
99 	u32 temp_data;
100 	u32 temp_value_mask;
101 	u32 temp_value_shift;
102 	u32 temp_valid_mask;
103 
104 	u32 panic_alarm_ctrl;
105 	u32 panic_alarm_mask;
106 	u32 panic_alarm_shift;
107 
108 	u32 high_alarm_ctrl;
109 	u32 high_alarm_mask;
110 	u32 high_alarm_shift;
111 
112 	u32 low_alarm_ctrl;
113 	u32 low_alarm_mask;
114 	u32 low_alarm_shift;
115 };
116 
117 static struct thermal_trip trips[] = {
118 	[IMX_TRIP_PASSIVE]  = { .type = THERMAL_TRIP_PASSIVE,
119 				.flags = THERMAL_TRIP_FLAG_RW_TEMP },
120 	[IMX_TRIP_CRITICAL] = { .type = THERMAL_TRIP_CRITICAL },
121 };
122 
123 static struct thermal_soc_data thermal_imx6q_data = {
124 	.version = TEMPMON_IMX6Q,
125 
126 	.sensor_ctrl = IMX6_TEMPSENSE0,
127 	.power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
128 	.measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
129 
130 	.measure_freq_ctrl = IMX6_TEMPSENSE1,
131 	.measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
132 	.measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
133 
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,
138 
139 	.high_alarm_ctrl = IMX6_TEMPSENSE0,
140 	.high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
141 	.high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
142 };
143 
144 static struct thermal_soc_data thermal_imx6sx_data = {
145 	.version = TEMPMON_IMX6SX,
146 
147 	.sensor_ctrl = IMX6_TEMPSENSE0,
148 	.power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
149 	.measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
150 
151 	.measure_freq_ctrl = IMX6_TEMPSENSE1,
152 	.measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
153 	.measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
154 
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,
159 
160 	.high_alarm_ctrl = IMX6_TEMPSENSE0,
161 	.high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
162 	.high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
163 
164 	.panic_alarm_ctrl = IMX6_TEMPSENSE2,
165 	.panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
166 	.panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
167 
168 	.low_alarm_ctrl = IMX6_TEMPSENSE2,
169 	.low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
170 	.low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
171 };
172 
173 static struct thermal_soc_data thermal_imx7d_data = {
174 	.version = TEMPMON_IMX7D,
175 
176 	.sensor_ctrl = IMX7_TEMPSENSE1,
177 	.power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
178 	.measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
179 
180 	.measure_freq_ctrl = IMX7_TEMPSENSE1,
181 	.measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
182 	.measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
183 
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,
188 
189 	.panic_alarm_ctrl = IMX7_TEMPSENSE1,
190 	.panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
191 	.panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
192 
193 	.high_alarm_ctrl = IMX7_TEMPSENSE0,
194 	.high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
195 	.high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
196 
197 	.low_alarm_ctrl = IMX7_TEMPSENSE0,
198 	.low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
199 	.low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
200 };
201 
202 struct imx_thermal_data {
203 	struct device *dev;
204 	struct cpufreq_policy *policy;
205 	struct thermal_zone_device *tz;
206 	struct thermal_cooling_device *cdev;
207 	struct regmap *tempmon;
208 	u32 c1, c2; /* See formula in imx_init_calib() */
209 	int temp_max;
210 	int alarm_temp;
211 	int last_temp;
212 	bool irq_enabled;
213 	int irq;
214 	struct clk *thermal_clk;
215 	const struct thermal_soc_data *socdata;
216 	const char *temp_grade;
217 };
218 
219 static void imx_set_panic_temp(struct imx_thermal_data *data,
220 			       int panic_temp)
221 {
222 	const struct thermal_soc_data *soc_data = data->socdata;
223 	struct regmap *map = data->tempmon;
224 	int critical_value;
225 
226 	critical_value = (data->c2 - panic_temp) / data->c1;
227 
228 	regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
229 		     soc_data->panic_alarm_mask);
230 	regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
231 		     critical_value << soc_data->panic_alarm_shift);
232 }
233 
234 static void imx_set_alarm_temp(struct imx_thermal_data *data,
235 			       int alarm_temp)
236 {
237 	struct regmap *map = data->tempmon;
238 	const struct thermal_soc_data *soc_data = data->socdata;
239 	int alarm_value;
240 
241 	data->alarm_temp = alarm_temp;
242 
243 	if (data->socdata->version == TEMPMON_IMX7D)
244 		alarm_value = alarm_temp / 1000 + data->c1 - 25;
245 	else
246 		alarm_value = (data->c2 - alarm_temp) / data->c1;
247 
248 	regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
249 		     soc_data->high_alarm_mask);
250 	regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
251 		     alarm_value << soc_data->high_alarm_shift);
252 }
253 
254 static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
255 {
256 	struct imx_thermal_data *data = thermal_zone_device_priv(tz);
257 	const struct thermal_soc_data *soc_data = data->socdata;
258 	struct regmap *map = data->tempmon;
259 	unsigned int n_meas;
260 	u32 val;
261 	int ret;
262 
263 	ret = pm_runtime_resume_and_get(data->dev);
264 	if (ret < 0)
265 		return ret;
266 
267 	regmap_read(map, soc_data->temp_data, &val);
268 
269 	if ((val & soc_data->temp_valid_mask) == 0)
270 		return -EAGAIN;
271 
272 	n_meas = (val & soc_data->temp_value_mask)
273 		>> soc_data->temp_value_shift;
274 
275 	/* See imx_init_calib() for formula derivation */
276 	if (data->socdata->version == TEMPMON_IMX7D)
277 		*temp = (n_meas - data->c1 + 25) * 1000;
278 	else
279 		*temp = data->c2 - n_meas * data->c1;
280 
281 	/* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
282 	if (data->socdata->version == TEMPMON_IMX6Q) {
283 		if (data->alarm_temp == trips[IMX_TRIP_PASSIVE].temperature &&
284 			*temp >= trips[IMX_TRIP_PASSIVE].temperature)
285 			imx_set_alarm_temp(data, trips[IMX_TRIP_CRITICAL].temperature);
286 		if (data->alarm_temp == trips[IMX_TRIP_CRITICAL].temperature &&
287 			*temp < trips[IMX_TRIP_PASSIVE].temperature) {
288 			imx_set_alarm_temp(data, trips[IMX_TRIP_PASSIVE].temperature);
289 			dev_dbg(data->dev, "thermal alarm off: T < %d\n",
290 				data->alarm_temp / 1000);
291 		}
292 	}
293 
294 	if (*temp != data->last_temp) {
295 		dev_dbg(data->dev, "millicelsius: %d\n", *temp);
296 		data->last_temp = *temp;
297 	}
298 
299 	/* Reenable alarm IRQ if temperature below alarm temperature */
300 	if (!data->irq_enabled && *temp < data->alarm_temp) {
301 		data->irq_enabled = true;
302 		enable_irq(data->irq);
303 	}
304 
305 	pm_runtime_put(data->dev);
306 
307 	return 0;
308 }
309 
310 static int imx_change_mode(struct thermal_zone_device *tz,
311 			   enum thermal_device_mode mode)
312 {
313 	struct imx_thermal_data *data = thermal_zone_device_priv(tz);
314 
315 	if (mode == THERMAL_DEVICE_ENABLED) {
316 		pm_runtime_get(data->dev);
317 
318 		if (!data->irq_enabled) {
319 			data->irq_enabled = true;
320 			enable_irq(data->irq);
321 		}
322 	} else {
323 		pm_runtime_put(data->dev);
324 
325 		if (data->irq_enabled) {
326 			disable_irq(data->irq);
327 			data->irq_enabled = false;
328 		}
329 	}
330 
331 	return 0;
332 }
333 
334 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip_id,
335 			     int temp)
336 {
337 	struct imx_thermal_data *data = thermal_zone_device_priv(tz);
338 	struct thermal_trip trip;
339 	int ret;
340 
341 	ret = pm_runtime_resume_and_get(data->dev);
342 	if (ret < 0)
343 		return ret;
344 
345 	ret = __thermal_zone_get_trip(tz, trip_id, &trip);
346 	if (ret)
347 		return ret;
348 
349 	/* do not allow changing critical threshold */
350 	if (trip.type == THERMAL_TRIP_CRITICAL)
351 		return -EPERM;
352 
353 	/* do not allow passive to be set higher than critical */
354 	if (temp < 0 || temp > trips[IMX_TRIP_CRITICAL].temperature)
355 		return -EINVAL;
356 
357 	imx_set_alarm_temp(data, temp);
358 	trips[IMX_TRIP_PASSIVE].temperature = temp;
359 
360 	pm_runtime_put(data->dev);
361 
362 	return 0;
363 }
364 
365 static int imx_bind(struct thermal_zone_device *tz,
366 		    struct thermal_cooling_device *cdev)
367 {
368 	return thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
369 						THERMAL_NO_LIMIT,
370 						THERMAL_NO_LIMIT,
371 						THERMAL_WEIGHT_DEFAULT);
372 }
373 
374 static int imx_unbind(struct thermal_zone_device *tz,
375 		      struct thermal_cooling_device *cdev)
376 {
377 	return thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
378 }
379 
380 static struct thermal_zone_device_ops imx_tz_ops = {
381 	.bind = imx_bind,
382 	.unbind = imx_unbind,
383 	.get_temp = imx_get_temp,
384 	.change_mode = imx_change_mode,
385 	.set_trip_temp = imx_set_trip_temp,
386 };
387 
388 static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
389 {
390 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
391 	int n1;
392 	u64 temp64;
393 
394 	if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
395 		dev_err(&pdev->dev, "invalid sensor calibration data\n");
396 		return -EINVAL;
397 	}
398 
399 	/*
400 	 * On i.MX7D, we only use the calibration data at 25C to get the temp,
401 	 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
402 	 */
403 	if (data->socdata->version == TEMPMON_IMX7D) {
404 		data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
405 		return 0;
406 	}
407 
408 	/*
409 	 * The sensor is calibrated at 25 °C (aka T1) and the value measured
410 	 * (aka N1) at this temperature is provided in bits [31:20] in the
411 	 * i.MX's OCOTP value ANA1.
412 	 * To find the actual temperature T, the following formula has to be used
413 	 * when reading value n from the sensor:
414 	 *
415 	 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
416 	 *   = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
417 	 *   = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
418 	 *   = c2 - c1 * N
419 	 *
420 	 * with
421 	 *
422 	 *  T1' = 28.580661 °C
423 	 *   c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
424 	 *   c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
425 	 *      = T1' + N1 * c1
426 	 */
427 	n1 = ocotp_ana1 >> 20;
428 
429 	temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
430 	temp64 *= 1000; /* to get result in °mC */
431 	do_div(temp64, 15423 * n1 - 4148468);
432 	data->c1 = temp64;
433 	data->c2 = n1 * data->c1 + 28581;
434 
435 	return 0;
436 }
437 
438 static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
439 {
440 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
441 
442 	/* The maximum die temp is specified by the Temperature Grade */
443 	switch ((ocotp_mem0 >> 6) & 0x3) {
444 	case 0: /* Commercial (0 to 95 °C) */
445 		data->temp_grade = "Commercial";
446 		data->temp_max = 95000;
447 		break;
448 	case 1: /* Extended Commercial (-20 °C to 105 °C) */
449 		data->temp_grade = "Extended Commercial";
450 		data->temp_max = 105000;
451 		break;
452 	case 2: /* Industrial (-40 °C to 105 °C) */
453 		data->temp_grade = "Industrial";
454 		data->temp_max = 105000;
455 		break;
456 	case 3: /* Automotive (-40 °C to 125 °C) */
457 		data->temp_grade = "Automotive";
458 		data->temp_max = 125000;
459 		break;
460 	}
461 
462 	/*
463 	 * Set the critical trip point at 5 °C under max
464 	 * Set the passive trip point at 10 °C under max (changeable via sysfs)
465 	 */
466 	trips[IMX_TRIP_PASSIVE].temperature = data->temp_max - (1000 * 10);
467 	trips[IMX_TRIP_CRITICAL].temperature = data->temp_max - (1000 * 5);
468 }
469 
470 static int imx_init_from_tempmon_data(struct platform_device *pdev)
471 {
472 	struct regmap *map;
473 	int ret;
474 	u32 val;
475 
476 	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
477 					      "fsl,tempmon-data");
478 	if (IS_ERR(map)) {
479 		ret = PTR_ERR(map);
480 		dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
481 		return ret;
482 	}
483 
484 	ret = regmap_read(map, OCOTP_ANA1, &val);
485 	if (ret) {
486 		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
487 		return ret;
488 	}
489 	ret = imx_init_calib(pdev, val);
490 	if (ret)
491 		return ret;
492 
493 	ret = regmap_read(map, OCOTP_MEM0, &val);
494 	if (ret) {
495 		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
496 		return ret;
497 	}
498 	imx_init_temp_grade(pdev, val);
499 
500 	return 0;
501 }
502 
503 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
504 {
505 	int ret;
506 	u32 val;
507 
508 	ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
509 	if (ret)
510 		return ret;
511 
512 	ret = imx_init_calib(pdev, val);
513 	if (ret)
514 		return ret;
515 
516 	ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
517 	if (ret)
518 		return ret;
519 	imx_init_temp_grade(pdev, val);
520 
521 	return 0;
522 }
523 
524 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
525 {
526 	struct imx_thermal_data *data = dev;
527 
528 	disable_irq_nosync(irq);
529 	data->irq_enabled = false;
530 
531 	return IRQ_WAKE_THREAD;
532 }
533 
534 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
535 {
536 	struct imx_thermal_data *data = dev;
537 
538 	dev_dbg(data->dev, "THERMAL ALARM: T > %d\n", data->alarm_temp / 1000);
539 
540 	thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
541 
542 	return IRQ_HANDLED;
543 }
544 
545 static const struct of_device_id of_imx_thermal_match[] = {
546 	{ .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
547 	{ .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
548 	{ .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
549 	{ /* end */ }
550 };
551 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
552 
553 #ifdef CONFIG_CPU_FREQ
554 /*
555  * Create cooling device in case no #cooling-cells property is available in
556  * CPU node
557  */
558 static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
559 {
560 	struct device_node *np;
561 	int ret = 0;
562 
563 	data->policy = cpufreq_cpu_get(0);
564 	if (!data->policy) {
565 		pr_debug("%s: CPUFreq policy not found\n", __func__);
566 		return -EPROBE_DEFER;
567 	}
568 
569 	np = of_get_cpu_node(data->policy->cpu, NULL);
570 
571 	if (!np || !of_property_present(np, "#cooling-cells")) {
572 		data->cdev = cpufreq_cooling_register(data->policy);
573 		if (IS_ERR(data->cdev)) {
574 			ret = PTR_ERR(data->cdev);
575 			cpufreq_cpu_put(data->policy);
576 		}
577 	}
578 
579 	of_node_put(np);
580 
581 	return ret;
582 }
583 
584 static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
585 {
586 	cpufreq_cooling_unregister(data->cdev);
587 	cpufreq_cpu_put(data->policy);
588 }
589 
590 #else
591 
592 static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
593 {
594 	return 0;
595 }
596 
597 static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
598 {
599 }
600 #endif
601 
602 static int imx_thermal_probe(struct platform_device *pdev)
603 {
604 	struct imx_thermal_data *data;
605 	struct regmap *map;
606 	int measure_freq;
607 	int ret;
608 
609 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
610 	if (!data)
611 		return -ENOMEM;
612 
613 	data->dev = &pdev->dev;
614 
615 	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
616 	if (IS_ERR(map)) {
617 		ret = PTR_ERR(map);
618 		dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
619 		return ret;
620 	}
621 	data->tempmon = map;
622 
623 	data->socdata = of_device_get_match_data(&pdev->dev);
624 	if (!data->socdata) {
625 		dev_err(&pdev->dev, "no device match found\n");
626 		return -ENODEV;
627 	}
628 
629 	/* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
630 	if (data->socdata->version == TEMPMON_IMX6SX) {
631 		regmap_write(map, IMX6_MISC1 + REG_CLR,
632 			IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
633 			| IMX6_MISC1_IRQ_TEMPPANIC);
634 		/*
635 		 * reset value of LOW ALARM is incorrect, set it to lowest
636 		 * value to avoid false trigger of low alarm.
637 		 */
638 		regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
639 			     data->socdata->low_alarm_mask);
640 	}
641 
642 	data->irq = platform_get_irq(pdev, 0);
643 	if (data->irq < 0)
644 		return data->irq;
645 
646 	platform_set_drvdata(pdev, data);
647 
648 	if (of_property_present(pdev->dev.of_node, "nvmem-cells")) {
649 		ret = imx_init_from_nvmem_cells(pdev);
650 		if (ret)
651 			return dev_err_probe(&pdev->dev, ret,
652 					     "failed to init from nvmem\n");
653 	} else {
654 		ret = imx_init_from_tempmon_data(pdev);
655 		if (ret) {
656 			dev_err(&pdev->dev, "failed to init from fsl,tempmon-data\n");
657 			return ret;
658 		}
659 	}
660 
661 	/* Make sure sensor is in known good state for measurements */
662 	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
663 		     data->socdata->power_down_mask);
664 	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
665 		     data->socdata->measure_temp_mask);
666 	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
667 		     data->socdata->measure_freq_mask);
668 	if (data->socdata->version != TEMPMON_IMX7D)
669 		regmap_write(map, IMX6_MISC0 + REG_SET,
670 			IMX6_MISC0_REFTOP_SELBIASOFF);
671 	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
672 		     data->socdata->power_down_mask);
673 
674 	ret = imx_thermal_register_legacy_cooling(data);
675 	if (ret)
676 		return dev_err_probe(&pdev->dev, ret,
677 				     "failed to register cpufreq cooling device\n");
678 
679 	data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
680 	if (IS_ERR(data->thermal_clk)) {
681 		ret = PTR_ERR(data->thermal_clk);
682 		if (ret != -EPROBE_DEFER)
683 			dev_err(&pdev->dev,
684 				"failed to get thermal clk: %d\n", ret);
685 		goto legacy_cleanup;
686 	}
687 
688 	/*
689 	 * Thermal sensor needs clk on to get correct value, normally
690 	 * we should enable its clk before taking measurement and disable
691 	 * clk after measurement is done, but if alarm function is enabled,
692 	 * hardware will auto measure the temperature periodically, so we
693 	 * need to keep the clk always on for alarm function.
694 	 */
695 	ret = clk_prepare_enable(data->thermal_clk);
696 	if (ret) {
697 		dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
698 		goto legacy_cleanup;
699 	}
700 
701 	data->tz = thermal_zone_device_register_with_trips("imx_thermal_zone",
702 							   trips,
703 							   ARRAY_SIZE(trips),
704 							   data,
705 							   &imx_tz_ops, NULL,
706 							   IMX_PASSIVE_DELAY,
707 							   IMX_POLLING_DELAY);
708 	if (IS_ERR(data->tz)) {
709 		ret = PTR_ERR(data->tz);
710 		dev_err(&pdev->dev,
711 			"failed to register thermal zone device %d\n", ret);
712 		goto clk_disable;
713 	}
714 
715 	dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
716 		 " critical:%dC passive:%dC\n", data->temp_grade,
717 		 data->temp_max / 1000, trips[IMX_TRIP_CRITICAL].temperature / 1000,
718 		 trips[IMX_TRIP_PASSIVE].temperature / 1000);
719 
720 	/* Enable measurements at ~ 10 Hz */
721 	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
722 		     data->socdata->measure_freq_mask);
723 	measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
724 	regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
725 		     measure_freq << data->socdata->measure_freq_shift);
726 	imx_set_alarm_temp(data, trips[IMX_TRIP_PASSIVE].temperature);
727 
728 	if (data->socdata->version == TEMPMON_IMX6SX)
729 		imx_set_panic_temp(data, trips[IMX_TRIP_CRITICAL].temperature);
730 
731 	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
732 		     data->socdata->power_down_mask);
733 	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
734 		     data->socdata->measure_temp_mask);
735 	/* After power up, we need a delay before first access can be done. */
736 	usleep_range(20, 50);
737 
738 	/* the core was configured and enabled just before */
739 	pm_runtime_set_active(&pdev->dev);
740 	pm_runtime_enable(data->dev);
741 
742 	ret = pm_runtime_resume_and_get(data->dev);
743 	if (ret < 0)
744 		goto disable_runtime_pm;
745 
746 	data->irq_enabled = true;
747 	ret = thermal_zone_device_enable(data->tz);
748 	if (ret)
749 		goto thermal_zone_unregister;
750 
751 	ret = devm_request_threaded_irq(&pdev->dev, data->irq,
752 			imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
753 			0, "imx_thermal", data);
754 	if (ret < 0) {
755 		dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
756 		goto thermal_zone_unregister;
757 	}
758 
759 	pm_runtime_put(data->dev);
760 
761 	return 0;
762 
763 thermal_zone_unregister:
764 	thermal_zone_device_unregister(data->tz);
765 disable_runtime_pm:
766 	pm_runtime_put_noidle(data->dev);
767 	pm_runtime_disable(data->dev);
768 clk_disable:
769 	clk_disable_unprepare(data->thermal_clk);
770 legacy_cleanup:
771 	imx_thermal_unregister_legacy_cooling(data);
772 
773 	return ret;
774 }
775 
776 static void imx_thermal_remove(struct platform_device *pdev)
777 {
778 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
779 
780 	pm_runtime_put_noidle(data->dev);
781 	pm_runtime_disable(data->dev);
782 
783 	thermal_zone_device_unregister(data->tz);
784 	imx_thermal_unregister_legacy_cooling(data);
785 }
786 
787 static int __maybe_unused imx_thermal_suspend(struct device *dev)
788 {
789 	struct imx_thermal_data *data = dev_get_drvdata(dev);
790 	int ret;
791 
792 	/*
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()
798 	 */
799 	ret = thermal_zone_device_disable(data->tz);
800 	if (ret)
801 		return ret;
802 
803 	return pm_runtime_force_suspend(data->dev);
804 }
805 
806 static int __maybe_unused imx_thermal_resume(struct device *dev)
807 {
808 	struct imx_thermal_data *data = dev_get_drvdata(dev);
809 	int ret;
810 
811 	ret = pm_runtime_force_resume(data->dev);
812 	if (ret)
813 		return ret;
814 	/* Enabled thermal sensor after resume */
815 	return thermal_zone_device_enable(data->tz);
816 }
817 
818 static int __maybe_unused imx_thermal_runtime_suspend(struct device *dev)
819 {
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;
823 	int ret;
824 
825 	ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
826 			   socdata->measure_temp_mask);
827 	if (ret)
828 		return ret;
829 
830 	ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
831 			   socdata->power_down_mask);
832 	if (ret)
833 		return ret;
834 
835 	clk_disable_unprepare(data->thermal_clk);
836 
837 	return 0;
838 }
839 
840 static int __maybe_unused imx_thermal_runtime_resume(struct device *dev)
841 {
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;
845 	int ret;
846 
847 	ret = clk_prepare_enable(data->thermal_clk);
848 	if (ret)
849 		return ret;
850 
851 	ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
852 			   socdata->power_down_mask);
853 	if (ret)
854 		return ret;
855 
856 	ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
857 			   socdata->measure_temp_mask);
858 	if (ret)
859 		return ret;
860 
861 	/*
862 	 * According to the temp sensor designers, it may require up to ~17us
863 	 * to complete a measurement.
864 	 */
865 	usleep_range(20, 50);
866 
867 	return 0;
868 }
869 
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)
874 };
875 
876 static struct platform_driver imx_thermal = {
877 	.driver = {
878 		.name	= "imx_thermal",
879 		.pm	= &imx_thermal_pm_ops,
880 		.of_match_table = of_imx_thermal_match,
881 	},
882 	.probe		= imx_thermal_probe,
883 	.remove_new	= imx_thermal_remove,
884 };
885 module_platform_driver(imx_thermal);
886 
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");
891