xref: /linux/drivers/thermal/imx_thermal.c (revision 27c8f12e972d3647e9d759d7cafd4c34fa513432)
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,
335 			     const struct thermal_trip *trip, int temp)
336 {
337 	struct imx_thermal_data *data = thermal_zone_device_priv(tz);
338 	int ret;
339 
340 	ret = pm_runtime_resume_and_get(data->dev);
341 	if (ret < 0)
342 		return ret;
343 
344 	/* do not allow passive to be set higher than critical */
345 	if (temp < 0 || temp > trips[IMX_TRIP_CRITICAL].temperature)
346 		return -EINVAL;
347 
348 	imx_set_alarm_temp(data, temp);
349 	trips[IMX_TRIP_PASSIVE].temperature = temp;
350 
351 	pm_runtime_put(data->dev);
352 
353 	return 0;
354 }
355 
356 static int imx_bind(struct thermal_zone_device *tz,
357 		    struct thermal_cooling_device *cdev)
358 {
359 	return thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
360 						THERMAL_NO_LIMIT,
361 						THERMAL_NO_LIMIT,
362 						THERMAL_WEIGHT_DEFAULT);
363 }
364 
365 static int imx_unbind(struct thermal_zone_device *tz,
366 		      struct thermal_cooling_device *cdev)
367 {
368 	return thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
369 }
370 
371 static struct thermal_zone_device_ops imx_tz_ops = {
372 	.bind = imx_bind,
373 	.unbind = imx_unbind,
374 	.get_temp = imx_get_temp,
375 	.change_mode = imx_change_mode,
376 	.set_trip_temp = imx_set_trip_temp,
377 };
378 
379 static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
380 {
381 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
382 	int n1;
383 	u64 temp64;
384 
385 	if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
386 		dev_err(&pdev->dev, "invalid sensor calibration data\n");
387 		return -EINVAL;
388 	}
389 
390 	/*
391 	 * On i.MX7D, we only use the calibration data at 25C to get the temp,
392 	 * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
393 	 */
394 	if (data->socdata->version == TEMPMON_IMX7D) {
395 		data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
396 		return 0;
397 	}
398 
399 	/*
400 	 * The sensor is calibrated at 25 °C (aka T1) and the value measured
401 	 * (aka N1) at this temperature is provided in bits [31:20] in the
402 	 * i.MX's OCOTP value ANA1.
403 	 * To find the actual temperature T, the following formula has to be used
404 	 * when reading value n from the sensor:
405 	 *
406 	 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
407 	 *   = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
408 	 *   = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
409 	 *   = c2 - c1 * N
410 	 *
411 	 * with
412 	 *
413 	 *  T1' = 28.580661 °C
414 	 *   c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
415 	 *   c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
416 	 *      = T1' + N1 * c1
417 	 */
418 	n1 = ocotp_ana1 >> 20;
419 
420 	temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
421 	temp64 *= 1000; /* to get result in °mC */
422 	do_div(temp64, 15423 * n1 - 4148468);
423 	data->c1 = temp64;
424 	data->c2 = n1 * data->c1 + 28581;
425 
426 	return 0;
427 }
428 
429 static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
430 {
431 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
432 
433 	/* The maximum die temp is specified by the Temperature Grade */
434 	switch ((ocotp_mem0 >> 6) & 0x3) {
435 	case 0: /* Commercial (0 to 95 °C) */
436 		data->temp_grade = "Commercial";
437 		data->temp_max = 95000;
438 		break;
439 	case 1: /* Extended Commercial (-20 °C to 105 °C) */
440 		data->temp_grade = "Extended Commercial";
441 		data->temp_max = 105000;
442 		break;
443 	case 2: /* Industrial (-40 °C to 105 °C) */
444 		data->temp_grade = "Industrial";
445 		data->temp_max = 105000;
446 		break;
447 	case 3: /* Automotive (-40 °C to 125 °C) */
448 		data->temp_grade = "Automotive";
449 		data->temp_max = 125000;
450 		break;
451 	}
452 
453 	/*
454 	 * Set the critical trip point at 5 °C under max
455 	 * Set the passive trip point at 10 °C under max (changeable via sysfs)
456 	 */
457 	trips[IMX_TRIP_PASSIVE].temperature = data->temp_max - (1000 * 10);
458 	trips[IMX_TRIP_CRITICAL].temperature = data->temp_max - (1000 * 5);
459 }
460 
461 static int imx_init_from_tempmon_data(struct platform_device *pdev)
462 {
463 	struct regmap *map;
464 	int ret;
465 	u32 val;
466 
467 	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
468 					      "fsl,tempmon-data");
469 	if (IS_ERR(map)) {
470 		ret = PTR_ERR(map);
471 		dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
472 		return ret;
473 	}
474 
475 	ret = regmap_read(map, OCOTP_ANA1, &val);
476 	if (ret) {
477 		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
478 		return ret;
479 	}
480 	ret = imx_init_calib(pdev, val);
481 	if (ret)
482 		return ret;
483 
484 	ret = regmap_read(map, OCOTP_MEM0, &val);
485 	if (ret) {
486 		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
487 		return ret;
488 	}
489 	imx_init_temp_grade(pdev, val);
490 
491 	return 0;
492 }
493 
494 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
495 {
496 	int ret;
497 	u32 val;
498 
499 	ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
500 	if (ret)
501 		return ret;
502 
503 	ret = imx_init_calib(pdev, val);
504 	if (ret)
505 		return ret;
506 
507 	ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
508 	if (ret)
509 		return ret;
510 	imx_init_temp_grade(pdev, val);
511 
512 	return 0;
513 }
514 
515 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
516 {
517 	struct imx_thermal_data *data = dev;
518 
519 	disable_irq_nosync(irq);
520 	data->irq_enabled = false;
521 
522 	return IRQ_WAKE_THREAD;
523 }
524 
525 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
526 {
527 	struct imx_thermal_data *data = dev;
528 
529 	dev_dbg(data->dev, "THERMAL ALARM: T > %d\n", data->alarm_temp / 1000);
530 
531 	thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
532 
533 	return IRQ_HANDLED;
534 }
535 
536 static const struct of_device_id of_imx_thermal_match[] = {
537 	{ .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
538 	{ .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
539 	{ .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
540 	{ /* end */ }
541 };
542 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
543 
544 #ifdef CONFIG_CPU_FREQ
545 /*
546  * Create cooling device in case no #cooling-cells property is available in
547  * CPU node
548  */
549 static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
550 {
551 	struct device_node *np;
552 	int ret = 0;
553 
554 	data->policy = cpufreq_cpu_get(0);
555 	if (!data->policy) {
556 		pr_debug("%s: CPUFreq policy not found\n", __func__);
557 		return -EPROBE_DEFER;
558 	}
559 
560 	np = of_get_cpu_node(data->policy->cpu, NULL);
561 
562 	if (!np || !of_property_present(np, "#cooling-cells")) {
563 		data->cdev = cpufreq_cooling_register(data->policy);
564 		if (IS_ERR(data->cdev)) {
565 			ret = PTR_ERR(data->cdev);
566 			cpufreq_cpu_put(data->policy);
567 		}
568 	}
569 
570 	of_node_put(np);
571 
572 	return ret;
573 }
574 
575 static void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
576 {
577 	cpufreq_cooling_unregister(data->cdev);
578 	cpufreq_cpu_put(data->policy);
579 }
580 
581 #else
582 
583 static inline int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
584 {
585 	return 0;
586 }
587 
588 static inline void imx_thermal_unregister_legacy_cooling(struct imx_thermal_data *data)
589 {
590 }
591 #endif
592 
593 static int imx_thermal_probe(struct platform_device *pdev)
594 {
595 	struct device *dev = &pdev->dev;
596 	struct imx_thermal_data *data;
597 	struct regmap *map;
598 	int measure_freq;
599 	int ret;
600 
601 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
602 	if (!data)
603 		return -ENOMEM;
604 
605 	data->dev = dev;
606 
607 	map = syscon_regmap_lookup_by_phandle(dev->of_node, "fsl,tempmon");
608 	if (IS_ERR(map)) {
609 		ret = PTR_ERR(map);
610 		dev_err(dev, "failed to get tempmon regmap: %d\n", ret);
611 		return ret;
612 	}
613 	data->tempmon = map;
614 
615 	data->socdata = of_device_get_match_data(dev);
616 	if (!data->socdata) {
617 		dev_err(dev, "no device match found\n");
618 		return -ENODEV;
619 	}
620 
621 	/* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
622 	if (data->socdata->version == TEMPMON_IMX6SX) {
623 		regmap_write(map, IMX6_MISC1 + REG_CLR,
624 			IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
625 			| IMX6_MISC1_IRQ_TEMPPANIC);
626 		/*
627 		 * reset value of LOW ALARM is incorrect, set it to lowest
628 		 * value to avoid false trigger of low alarm.
629 		 */
630 		regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
631 			     data->socdata->low_alarm_mask);
632 	}
633 
634 	data->irq = platform_get_irq(pdev, 0);
635 	if (data->irq < 0)
636 		return data->irq;
637 
638 	platform_set_drvdata(pdev, data);
639 
640 	if (of_property_present(dev->of_node, "nvmem-cells")) {
641 		ret = imx_init_from_nvmem_cells(pdev);
642 		if (ret)
643 			return dev_err_probe(dev, ret,
644 					     "failed to init from nvmem\n");
645 	} else {
646 		ret = imx_init_from_tempmon_data(pdev);
647 		if (ret) {
648 			dev_err(dev, "failed to init from fsl,tempmon-data\n");
649 			return ret;
650 		}
651 	}
652 
653 	/* Make sure sensor is in known good state for measurements */
654 	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
655 		     data->socdata->power_down_mask);
656 	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
657 		     data->socdata->measure_temp_mask);
658 	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
659 		     data->socdata->measure_freq_mask);
660 	if (data->socdata->version != TEMPMON_IMX7D)
661 		regmap_write(map, IMX6_MISC0 + REG_SET,
662 			IMX6_MISC0_REFTOP_SELBIASOFF);
663 	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
664 		     data->socdata->power_down_mask);
665 
666 	ret = imx_thermal_register_legacy_cooling(data);
667 	if (ret)
668 		return dev_err_probe(dev, ret,
669 				     "failed to register cpufreq cooling device\n");
670 
671 	data->thermal_clk = devm_clk_get(dev, NULL);
672 	if (IS_ERR(data->thermal_clk)) {
673 		ret = dev_err_probe(dev, PTR_ERR(data->thermal_clk), "failed to get thermal clk\n");
674 		goto legacy_cleanup;
675 	}
676 
677 	/*
678 	 * Thermal sensor needs clk on to get correct value, normally
679 	 * we should enable its clk before taking measurement and disable
680 	 * clk after measurement is done, but if alarm function is enabled,
681 	 * hardware will auto measure the temperature periodically, so we
682 	 * need to keep the clk always on for alarm function.
683 	 */
684 	ret = clk_prepare_enable(data->thermal_clk);
685 	if (ret) {
686 		dev_err(dev, "failed to enable thermal clk: %d\n", ret);
687 		goto legacy_cleanup;
688 	}
689 
690 	data->tz = thermal_zone_device_register_with_trips("imx_thermal_zone",
691 							   trips,
692 							   ARRAY_SIZE(trips),
693 							   data,
694 							   &imx_tz_ops, NULL,
695 							   IMX_PASSIVE_DELAY,
696 							   IMX_POLLING_DELAY);
697 	if (IS_ERR(data->tz)) {
698 		ret = PTR_ERR(data->tz);
699 		dev_err(dev, "failed to register thermal zone device %d\n",
700 			ret);
701 		goto clk_disable;
702 	}
703 
704 	dev_info(dev, "%s CPU temperature grade - max:%dC"
705 		 " critical:%dC passive:%dC\n", data->temp_grade,
706 		 data->temp_max / 1000, trips[IMX_TRIP_CRITICAL].temperature / 1000,
707 		 trips[IMX_TRIP_PASSIVE].temperature / 1000);
708 
709 	/* Enable measurements at ~ 10 Hz */
710 	regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
711 		     data->socdata->measure_freq_mask);
712 	measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
713 	regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
714 		     measure_freq << data->socdata->measure_freq_shift);
715 	imx_set_alarm_temp(data, trips[IMX_TRIP_PASSIVE].temperature);
716 
717 	if (data->socdata->version == TEMPMON_IMX6SX)
718 		imx_set_panic_temp(data, trips[IMX_TRIP_CRITICAL].temperature);
719 
720 	regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
721 		     data->socdata->power_down_mask);
722 	regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
723 		     data->socdata->measure_temp_mask);
724 	/* After power up, we need a delay before first access can be done. */
725 	usleep_range(20, 50);
726 
727 	/* the core was configured and enabled just before */
728 	pm_runtime_set_active(dev);
729 	pm_runtime_enable(data->dev);
730 
731 	ret = pm_runtime_resume_and_get(data->dev);
732 	if (ret < 0)
733 		goto disable_runtime_pm;
734 
735 	data->irq_enabled = true;
736 	ret = thermal_zone_device_enable(data->tz);
737 	if (ret)
738 		goto thermal_zone_unregister;
739 
740 	ret = devm_request_threaded_irq(dev, data->irq,
741 			imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
742 			0, "imx_thermal", data);
743 	if (ret < 0) {
744 		dev_err(dev, "failed to request alarm irq: %d\n", ret);
745 		goto thermal_zone_unregister;
746 	}
747 
748 	pm_runtime_put(data->dev);
749 
750 	return 0;
751 
752 thermal_zone_unregister:
753 	thermal_zone_device_unregister(data->tz);
754 disable_runtime_pm:
755 	pm_runtime_put_noidle(data->dev);
756 	pm_runtime_disable(data->dev);
757 clk_disable:
758 	clk_disable_unprepare(data->thermal_clk);
759 legacy_cleanup:
760 	imx_thermal_unregister_legacy_cooling(data);
761 
762 	return ret;
763 }
764 
765 static void imx_thermal_remove(struct platform_device *pdev)
766 {
767 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
768 
769 	pm_runtime_put_noidle(data->dev);
770 	pm_runtime_disable(data->dev);
771 
772 	thermal_zone_device_unregister(data->tz);
773 	imx_thermal_unregister_legacy_cooling(data);
774 }
775 
776 static int __maybe_unused imx_thermal_suspend(struct device *dev)
777 {
778 	struct imx_thermal_data *data = dev_get_drvdata(dev);
779 	int ret;
780 
781 	/*
782 	 * Need to disable thermal sensor, otherwise, when thermal core
783 	 * try to get temperature before thermal sensor resume, a wrong
784 	 * temperature will be read as the thermal sensor is powered
785 	 * down. This is done in change_mode() operation called from
786 	 * thermal_zone_device_disable()
787 	 */
788 	ret = thermal_zone_device_disable(data->tz);
789 	if (ret)
790 		return ret;
791 
792 	return pm_runtime_force_suspend(data->dev);
793 }
794 
795 static int __maybe_unused imx_thermal_resume(struct device *dev)
796 {
797 	struct imx_thermal_data *data = dev_get_drvdata(dev);
798 	int ret;
799 
800 	ret = pm_runtime_force_resume(data->dev);
801 	if (ret)
802 		return ret;
803 	/* Enabled thermal sensor after resume */
804 	return thermal_zone_device_enable(data->tz);
805 }
806 
807 static int __maybe_unused imx_thermal_runtime_suspend(struct device *dev)
808 {
809 	struct imx_thermal_data *data = dev_get_drvdata(dev);
810 	const struct thermal_soc_data *socdata = data->socdata;
811 	struct regmap *map = data->tempmon;
812 	int ret;
813 
814 	ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
815 			   socdata->measure_temp_mask);
816 	if (ret)
817 		return ret;
818 
819 	ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
820 			   socdata->power_down_mask);
821 	if (ret)
822 		return ret;
823 
824 	clk_disable_unprepare(data->thermal_clk);
825 
826 	return 0;
827 }
828 
829 static int __maybe_unused imx_thermal_runtime_resume(struct device *dev)
830 {
831 	struct imx_thermal_data *data = dev_get_drvdata(dev);
832 	const struct thermal_soc_data *socdata = data->socdata;
833 	struct regmap *map = data->tempmon;
834 	int ret;
835 
836 	ret = clk_prepare_enable(data->thermal_clk);
837 	if (ret)
838 		return ret;
839 
840 	ret = regmap_write(map, socdata->sensor_ctrl + REG_CLR,
841 			   socdata->power_down_mask);
842 	if (ret)
843 		return ret;
844 
845 	ret = regmap_write(map, socdata->sensor_ctrl + REG_SET,
846 			   socdata->measure_temp_mask);
847 	if (ret)
848 		return ret;
849 
850 	/*
851 	 * According to the temp sensor designers, it may require up to ~17us
852 	 * to complete a measurement.
853 	 */
854 	usleep_range(20, 50);
855 
856 	return 0;
857 }
858 
859 static const struct dev_pm_ops imx_thermal_pm_ops = {
860 	SET_SYSTEM_SLEEP_PM_OPS(imx_thermal_suspend, imx_thermal_resume)
861 	SET_RUNTIME_PM_OPS(imx_thermal_runtime_suspend,
862 			   imx_thermal_runtime_resume, NULL)
863 };
864 
865 static struct platform_driver imx_thermal = {
866 	.driver = {
867 		.name	= "imx_thermal",
868 		.pm	= &imx_thermal_pm_ops,
869 		.of_match_table = of_imx_thermal_match,
870 	},
871 	.probe		= imx_thermal_probe,
872 	.remove_new	= imx_thermal_remove,
873 };
874 module_platform_driver(imx_thermal);
875 
876 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
877 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
878 MODULE_LICENSE("GPL v2");
879 MODULE_ALIAS("platform:imx-thermal");
880