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