xref: /openbmc/linux/drivers/thermal/imx_thermal.c (revision bf070bb0)
1 /*
2  * Copyright 2013 Freescale Semiconductor, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/cpufreq.h>
12 #include <linux/cpu_cooling.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/kernel.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 #include <linux/thermal.h>
27 #include <linux/types.h>
28 #include <linux/nvmem-consumer.h>
29 
30 #define REG_SET		0x4
31 #define REG_CLR		0x8
32 #define REG_TOG		0xc
33 
34 #define MISC0				0x0150
35 #define MISC0_REFTOP_SELBIASOFF		(1 << 3)
36 #define MISC1				0x0160
37 #define MISC1_IRQ_TEMPHIGH		(1 << 29)
38 /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
39 #define MISC1_IRQ_TEMPLOW		(1 << 28)
40 #define MISC1_IRQ_TEMPPANIC		(1 << 27)
41 
42 #define TEMPSENSE0			0x0180
43 #define TEMPSENSE0_ALARM_VALUE_SHIFT	20
44 #define TEMPSENSE0_ALARM_VALUE_MASK	(0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
45 #define TEMPSENSE0_TEMP_CNT_SHIFT	8
46 #define TEMPSENSE0_TEMP_CNT_MASK	(0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
47 #define TEMPSENSE0_FINISHED		(1 << 2)
48 #define TEMPSENSE0_MEASURE_TEMP		(1 << 1)
49 #define TEMPSENSE0_POWER_DOWN		(1 << 0)
50 
51 #define TEMPSENSE1			0x0190
52 #define TEMPSENSE1_MEASURE_FREQ		0xffff
53 /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
54 #define TEMPSENSE2			0x0290
55 #define TEMPSENSE2_LOW_VALUE_SHIFT	0
56 #define TEMPSENSE2_LOW_VALUE_MASK	0xfff
57 #define TEMPSENSE2_PANIC_VALUE_SHIFT	16
58 #define TEMPSENSE2_PANIC_VALUE_MASK	0xfff0000
59 
60 #define OCOTP_MEM0			0x0480
61 #define OCOTP_ANA1			0x04e0
62 
63 /* The driver supports 1 passive trip point and 1 critical trip point */
64 enum imx_thermal_trip {
65 	IMX_TRIP_PASSIVE,
66 	IMX_TRIP_CRITICAL,
67 	IMX_TRIP_NUM,
68 };
69 
70 #define IMX_POLLING_DELAY		2000 /* millisecond */
71 #define IMX_PASSIVE_DELAY		1000
72 
73 #define FACTOR0				10000000
74 #define FACTOR1				15976
75 #define FACTOR2				4297157
76 
77 #define TEMPMON_IMX6Q			1
78 #define TEMPMON_IMX6SX			2
79 
80 struct thermal_soc_data {
81 	u32 version;
82 };
83 
84 static struct thermal_soc_data thermal_imx6q_data = {
85 	.version = TEMPMON_IMX6Q,
86 };
87 
88 static struct thermal_soc_data thermal_imx6sx_data = {
89 	.version = TEMPMON_IMX6SX,
90 };
91 
92 struct imx_thermal_data {
93 	struct cpufreq_policy *policy;
94 	struct thermal_zone_device *tz;
95 	struct thermal_cooling_device *cdev;
96 	enum thermal_device_mode mode;
97 	struct regmap *tempmon;
98 	u32 c1, c2; /* See formula in imx_init_calib() */
99 	int temp_passive;
100 	int temp_critical;
101 	int temp_max;
102 	int alarm_temp;
103 	int last_temp;
104 	bool irq_enabled;
105 	int irq;
106 	struct clk *thermal_clk;
107 	const struct thermal_soc_data *socdata;
108 	const char *temp_grade;
109 };
110 
111 static void imx_set_panic_temp(struct imx_thermal_data *data,
112 			       int panic_temp)
113 {
114 	struct regmap *map = data->tempmon;
115 	int critical_value;
116 
117 	critical_value = (data->c2 - panic_temp) / data->c1;
118 	regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK);
119 	regmap_write(map, TEMPSENSE2 + REG_SET, critical_value <<
120 			TEMPSENSE2_PANIC_VALUE_SHIFT);
121 }
122 
123 static void imx_set_alarm_temp(struct imx_thermal_data *data,
124 			       int alarm_temp)
125 {
126 	struct regmap *map = data->tempmon;
127 	int alarm_value;
128 
129 	data->alarm_temp = alarm_temp;
130 	alarm_value = (data->c2 - alarm_temp) / data->c1;
131 	regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
132 	regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
133 			TEMPSENSE0_ALARM_VALUE_SHIFT);
134 }
135 
136 static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
137 {
138 	struct imx_thermal_data *data = tz->devdata;
139 	struct regmap *map = data->tempmon;
140 	unsigned int n_meas;
141 	bool wait;
142 	u32 val;
143 
144 	if (data->mode == THERMAL_DEVICE_ENABLED) {
145 		/* Check if a measurement is currently in progress */
146 		regmap_read(map, TEMPSENSE0, &val);
147 		wait = !(val & TEMPSENSE0_FINISHED);
148 	} else {
149 		/*
150 		 * Every time we measure the temperature, we will power on the
151 		 * temperature sensor, enable measurements, take a reading,
152 		 * disable measurements, power off the temperature sensor.
153 		 */
154 		regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
155 		regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
156 
157 		wait = true;
158 	}
159 
160 	/*
161 	 * According to the temp sensor designers, it may require up to ~17us
162 	 * to complete a measurement.
163 	 */
164 	if (wait)
165 		usleep_range(20, 50);
166 
167 	regmap_read(map, TEMPSENSE0, &val);
168 
169 	if (data->mode != THERMAL_DEVICE_ENABLED) {
170 		regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
171 		regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
172 	}
173 
174 	if ((val & TEMPSENSE0_FINISHED) == 0) {
175 		dev_dbg(&tz->device, "temp measurement never finished\n");
176 		return -EAGAIN;
177 	}
178 
179 	n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
180 
181 	/* See imx_init_calib() for formula derivation */
182 	*temp = data->c2 - n_meas * data->c1;
183 
184 	/* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
185 	if (data->socdata->version == TEMPMON_IMX6Q) {
186 		if (data->alarm_temp == data->temp_passive &&
187 			*temp >= data->temp_passive)
188 			imx_set_alarm_temp(data, data->temp_critical);
189 		if (data->alarm_temp == data->temp_critical &&
190 			*temp < data->temp_passive) {
191 			imx_set_alarm_temp(data, data->temp_passive);
192 			dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
193 				data->alarm_temp / 1000);
194 		}
195 	}
196 
197 	if (*temp != data->last_temp) {
198 		dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
199 		data->last_temp = *temp;
200 	}
201 
202 	/* Reenable alarm IRQ if temperature below alarm temperature */
203 	if (!data->irq_enabled && *temp < data->alarm_temp) {
204 		data->irq_enabled = true;
205 		enable_irq(data->irq);
206 	}
207 
208 	return 0;
209 }
210 
211 static int imx_get_mode(struct thermal_zone_device *tz,
212 			enum thermal_device_mode *mode)
213 {
214 	struct imx_thermal_data *data = tz->devdata;
215 
216 	*mode = data->mode;
217 
218 	return 0;
219 }
220 
221 static int imx_set_mode(struct thermal_zone_device *tz,
222 			enum thermal_device_mode mode)
223 {
224 	struct imx_thermal_data *data = tz->devdata;
225 	struct regmap *map = data->tempmon;
226 
227 	if (mode == THERMAL_DEVICE_ENABLED) {
228 		tz->polling_delay = IMX_POLLING_DELAY;
229 		tz->passive_delay = IMX_PASSIVE_DELAY;
230 
231 		regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
232 		regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
233 
234 		if (!data->irq_enabled) {
235 			data->irq_enabled = true;
236 			enable_irq(data->irq);
237 		}
238 	} else {
239 		regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
240 		regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
241 
242 		tz->polling_delay = 0;
243 		tz->passive_delay = 0;
244 
245 		if (data->irq_enabled) {
246 			disable_irq(data->irq);
247 			data->irq_enabled = false;
248 		}
249 	}
250 
251 	data->mode = mode;
252 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
253 
254 	return 0;
255 }
256 
257 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
258 			     enum thermal_trip_type *type)
259 {
260 	*type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
261 					     THERMAL_TRIP_CRITICAL;
262 	return 0;
263 }
264 
265 static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
266 {
267 	struct imx_thermal_data *data = tz->devdata;
268 
269 	*temp = data->temp_critical;
270 	return 0;
271 }
272 
273 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
274 			     int *temp)
275 {
276 	struct imx_thermal_data *data = tz->devdata;
277 
278 	*temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
279 					     data->temp_critical;
280 	return 0;
281 }
282 
283 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
284 			     int temp)
285 {
286 	struct imx_thermal_data *data = tz->devdata;
287 
288 	/* do not allow changing critical threshold */
289 	if (trip == IMX_TRIP_CRITICAL)
290 		return -EPERM;
291 
292 	/* do not allow passive to be set higher than critical */
293 	if (temp < 0 || temp > data->temp_critical)
294 		return -EINVAL;
295 
296 	data->temp_passive = temp;
297 
298 	imx_set_alarm_temp(data, temp);
299 
300 	return 0;
301 }
302 
303 static int imx_bind(struct thermal_zone_device *tz,
304 		    struct thermal_cooling_device *cdev)
305 {
306 	int ret;
307 
308 	ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
309 					       THERMAL_NO_LIMIT,
310 					       THERMAL_NO_LIMIT,
311 					       THERMAL_WEIGHT_DEFAULT);
312 	if (ret) {
313 		dev_err(&tz->device,
314 			"binding zone %s with cdev %s failed:%d\n",
315 			tz->type, cdev->type, ret);
316 		return ret;
317 	}
318 
319 	return 0;
320 }
321 
322 static int imx_unbind(struct thermal_zone_device *tz,
323 		      struct thermal_cooling_device *cdev)
324 {
325 	int ret;
326 
327 	ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
328 	if (ret) {
329 		dev_err(&tz->device,
330 			"unbinding zone %s with cdev %s failed:%d\n",
331 			tz->type, cdev->type, ret);
332 		return ret;
333 	}
334 
335 	return 0;
336 }
337 
338 static struct thermal_zone_device_ops imx_tz_ops = {
339 	.bind = imx_bind,
340 	.unbind = imx_unbind,
341 	.get_temp = imx_get_temp,
342 	.get_mode = imx_get_mode,
343 	.set_mode = imx_set_mode,
344 	.get_trip_type = imx_get_trip_type,
345 	.get_trip_temp = imx_get_trip_temp,
346 	.get_crit_temp = imx_get_crit_temp,
347 	.set_trip_temp = imx_set_trip_temp,
348 };
349 
350 static int imx_init_calib(struct platform_device *pdev, u32 val)
351 {
352 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
353 	int t1, n1;
354 	u64 temp64;
355 
356 	if (val == 0 || val == ~0) {
357 		dev_err(&pdev->dev, "invalid sensor calibration data\n");
358 		return -EINVAL;
359 	}
360 
361 	/*
362 	 * Sensor data layout:
363 	 *   [31:20] - sensor value @ 25C
364 	 * Use universal formula now and only need sensor value @ 25C
365 	 * slope = 0.4297157 - (0.0015976 * 25C fuse)
366 	 */
367 	n1 = val >> 20;
368 	t1 = 25; /* t1 always 25C */
369 
370 	/*
371 	 * Derived from linear interpolation:
372 	 * slope = 0.4297157 - (0.0015976 * 25C fuse)
373 	 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
374 	 * (Nmeas - n1) / (Tmeas - t1) = slope
375 	 * We want to reduce this down to the minimum computation necessary
376 	 * for each temperature read.  Also, we want Tmeas in millicelsius
377 	 * and we don't want to lose precision from integer division. So...
378 	 * Tmeas = (Nmeas - n1) / slope + t1
379 	 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
380 	 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
381 	 * Let constant c1 = (-1000 / slope)
382 	 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
383 	 * Let constant c2 = n1 *c1 + 1000 * t1
384 	 * milli_Tmeas = c2 - Nmeas * c1
385 	 */
386 	temp64 = FACTOR0;
387 	temp64 *= 1000;
388 	do_div(temp64, FACTOR1 * n1 - FACTOR2);
389 	data->c1 = temp64;
390 	data->c2 = n1 * data->c1 + 1000 * t1;
391 
392 	return 0;
393 }
394 
395 static void imx_init_temp_grade(struct platform_device *pdev, u32 val)
396 {
397 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
398 
399 	/* The maximum die temp is specified by the Temperature Grade */
400 	switch ((val >> 6) & 0x3) {
401 	case 0: /* Commercial (0 to 95C) */
402 		data->temp_grade = "Commercial";
403 		data->temp_max = 95000;
404 		break;
405 	case 1: /* Extended Commercial (-20 to 105C) */
406 		data->temp_grade = "Extended Commercial";
407 		data->temp_max = 105000;
408 		break;
409 	case 2: /* Industrial (-40 to 105C) */
410 		data->temp_grade = "Industrial";
411 		data->temp_max = 105000;
412 		break;
413 	case 3: /* Automotive (-40 to 125C) */
414 		data->temp_grade = "Automotive";
415 		data->temp_max = 125000;
416 		break;
417 	}
418 
419 	/*
420 	 * Set the critical trip point at 5C under max
421 	 * Set the passive trip point at 10C under max (can change via sysfs)
422 	 */
423 	data->temp_critical = data->temp_max - (1000 * 5);
424 	data->temp_passive = data->temp_max - (1000 * 10);
425 }
426 
427 static int imx_init_from_tempmon_data(struct platform_device *pdev)
428 {
429 	struct regmap *map;
430 	int ret;
431 	u32 val;
432 
433 	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
434 					      "fsl,tempmon-data");
435 	if (IS_ERR(map)) {
436 		ret = PTR_ERR(map);
437 		dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
438 		return ret;
439 	}
440 
441 	ret = regmap_read(map, OCOTP_ANA1, &val);
442 	if (ret) {
443 		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
444 		return ret;
445 	}
446 	ret = imx_init_calib(pdev, val);
447 	if (ret)
448 		return ret;
449 
450 	ret = regmap_read(map, OCOTP_MEM0, &val);
451 	if (ret) {
452 		dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
453 		return ret;
454 	}
455 	imx_init_temp_grade(pdev, val);
456 
457 	return 0;
458 }
459 
460 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
461 {
462 	int ret;
463 	u32 val;
464 
465 	ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
466 	if (ret)
467 		return ret;
468 	imx_init_calib(pdev, val);
469 
470 	ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
471 	if (ret)
472 		return ret;
473 	imx_init_temp_grade(pdev, val);
474 
475 	return 0;
476 }
477 
478 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
479 {
480 	struct imx_thermal_data *data = dev;
481 
482 	disable_irq_nosync(irq);
483 	data->irq_enabled = false;
484 
485 	return IRQ_WAKE_THREAD;
486 }
487 
488 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
489 {
490 	struct imx_thermal_data *data = dev;
491 
492 	dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
493 		data->alarm_temp / 1000);
494 
495 	thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
496 
497 	return IRQ_HANDLED;
498 }
499 
500 static const struct of_device_id of_imx_thermal_match[] = {
501 	{ .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
502 	{ .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
503 	{ /* end */ }
504 };
505 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
506 
507 static int imx_thermal_probe(struct platform_device *pdev)
508 {
509 	struct imx_thermal_data *data;
510 	struct regmap *map;
511 	int measure_freq;
512 	int ret;
513 
514 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
515 	if (!data)
516 		return -ENOMEM;
517 
518 	map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
519 	if (IS_ERR(map)) {
520 		ret = PTR_ERR(map);
521 		dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
522 		return ret;
523 	}
524 	data->tempmon = map;
525 
526 	data->socdata = of_device_get_match_data(&pdev->dev);
527 	if (!data->socdata) {
528 		dev_err(&pdev->dev, "no device match found\n");
529 		return -ENODEV;
530 	}
531 
532 	/* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
533 	if (data->socdata->version == TEMPMON_IMX6SX) {
534 		regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
535 			MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
536 		/*
537 		 * reset value of LOW ALARM is incorrect, set it to lowest
538 		 * value to avoid false trigger of low alarm.
539 		 */
540 		regmap_write(map, TEMPSENSE2 + REG_SET,
541 			TEMPSENSE2_LOW_VALUE_MASK);
542 	}
543 
544 	data->irq = platform_get_irq(pdev, 0);
545 	if (data->irq < 0)
546 		return data->irq;
547 
548 	platform_set_drvdata(pdev, data);
549 
550 	if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
551 		ret = imx_init_from_nvmem_cells(pdev);
552 		if (ret == -EPROBE_DEFER)
553 			return ret;
554 		if (ret) {
555 			dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
556 				ret);
557 			return ret;
558 		}
559 	} else {
560 		ret = imx_init_from_tempmon_data(pdev);
561 		if (ret) {
562 			dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
563 			return ret;
564 		}
565 	}
566 
567 	/* Make sure sensor is in known good state for measurements */
568 	regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
569 	regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
570 	regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
571 	regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
572 	regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
573 
574 	data->policy = cpufreq_cpu_get(0);
575 	if (!data->policy) {
576 		pr_debug("%s: CPUFreq policy not found\n", __func__);
577 		return -EPROBE_DEFER;
578 	}
579 
580 	data->cdev = cpufreq_cooling_register(data->policy);
581 	if (IS_ERR(data->cdev)) {
582 		ret = PTR_ERR(data->cdev);
583 		dev_err(&pdev->dev,
584 			"failed to register cpufreq cooling device: %d\n", ret);
585 		cpufreq_cpu_put(data->policy);
586 		return ret;
587 	}
588 
589 	data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
590 	if (IS_ERR(data->thermal_clk)) {
591 		ret = PTR_ERR(data->thermal_clk);
592 		if (ret != -EPROBE_DEFER)
593 			dev_err(&pdev->dev,
594 				"failed to get thermal clk: %d\n", ret);
595 		cpufreq_cooling_unregister(data->cdev);
596 		cpufreq_cpu_put(data->policy);
597 		return ret;
598 	}
599 
600 	/*
601 	 * Thermal sensor needs clk on to get correct value, normally
602 	 * we should enable its clk before taking measurement and disable
603 	 * clk after measurement is done, but if alarm function is enabled,
604 	 * hardware will auto measure the temperature periodically, so we
605 	 * need to keep the clk always on for alarm function.
606 	 */
607 	ret = clk_prepare_enable(data->thermal_clk);
608 	if (ret) {
609 		dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
610 		cpufreq_cooling_unregister(data->cdev);
611 		cpufreq_cpu_put(data->policy);
612 		return ret;
613 	}
614 
615 	data->tz = thermal_zone_device_register("imx_thermal_zone",
616 						IMX_TRIP_NUM,
617 						BIT(IMX_TRIP_PASSIVE), data,
618 						&imx_tz_ops, NULL,
619 						IMX_PASSIVE_DELAY,
620 						IMX_POLLING_DELAY);
621 	if (IS_ERR(data->tz)) {
622 		ret = PTR_ERR(data->tz);
623 		dev_err(&pdev->dev,
624 			"failed to register thermal zone device %d\n", ret);
625 		clk_disable_unprepare(data->thermal_clk);
626 		cpufreq_cooling_unregister(data->cdev);
627 		cpufreq_cpu_put(data->policy);
628 		return ret;
629 	}
630 
631 	dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
632 		 " critical:%dC passive:%dC\n", data->temp_grade,
633 		 data->temp_max / 1000, data->temp_critical / 1000,
634 		 data->temp_passive / 1000);
635 
636 	/* Enable measurements at ~ 10 Hz */
637 	regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
638 	measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
639 	regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
640 	imx_set_alarm_temp(data, data->temp_passive);
641 
642 	if (data->socdata->version == TEMPMON_IMX6SX)
643 		imx_set_panic_temp(data, data->temp_critical);
644 
645 	regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
646 	regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
647 
648 	ret = devm_request_threaded_irq(&pdev->dev, data->irq,
649 			imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
650 			0, "imx_thermal", data);
651 	if (ret < 0) {
652 		dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
653 		clk_disable_unprepare(data->thermal_clk);
654 		thermal_zone_device_unregister(data->tz);
655 		cpufreq_cooling_unregister(data->cdev);
656 		cpufreq_cpu_put(data->policy);
657 		return ret;
658 	}
659 
660 	data->irq_enabled = true;
661 	data->mode = THERMAL_DEVICE_ENABLED;
662 
663 	return 0;
664 }
665 
666 static int imx_thermal_remove(struct platform_device *pdev)
667 {
668 	struct imx_thermal_data *data = platform_get_drvdata(pdev);
669 	struct regmap *map = data->tempmon;
670 
671 	/* Disable measurements */
672 	regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
673 	if (!IS_ERR(data->thermal_clk))
674 		clk_disable_unprepare(data->thermal_clk);
675 
676 	thermal_zone_device_unregister(data->tz);
677 	cpufreq_cooling_unregister(data->cdev);
678 	cpufreq_cpu_put(data->policy);
679 
680 	return 0;
681 }
682 
683 #ifdef CONFIG_PM_SLEEP
684 static int imx_thermal_suspend(struct device *dev)
685 {
686 	struct imx_thermal_data *data = dev_get_drvdata(dev);
687 	struct regmap *map = data->tempmon;
688 
689 	/*
690 	 * Need to disable thermal sensor, otherwise, when thermal core
691 	 * try to get temperature before thermal sensor resume, a wrong
692 	 * temperature will be read as the thermal sensor is powered
693 	 * down.
694 	 */
695 	regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
696 	regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
697 	data->mode = THERMAL_DEVICE_DISABLED;
698 	clk_disable_unprepare(data->thermal_clk);
699 
700 	return 0;
701 }
702 
703 static int imx_thermal_resume(struct device *dev)
704 {
705 	struct imx_thermal_data *data = dev_get_drvdata(dev);
706 	struct regmap *map = data->tempmon;
707 	int ret;
708 
709 	ret = clk_prepare_enable(data->thermal_clk);
710 	if (ret)
711 		return ret;
712 	/* Enabled thermal sensor after resume */
713 	regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
714 	regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
715 	data->mode = THERMAL_DEVICE_ENABLED;
716 
717 	return 0;
718 }
719 #endif
720 
721 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
722 			 imx_thermal_suspend, imx_thermal_resume);
723 
724 static struct platform_driver imx_thermal = {
725 	.driver = {
726 		.name	= "imx_thermal",
727 		.pm	= &imx_thermal_pm_ops,
728 		.of_match_table = of_imx_thermal_match,
729 	},
730 	.probe		= imx_thermal_probe,
731 	.remove		= imx_thermal_remove,
732 };
733 module_platform_driver(imx_thermal);
734 
735 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
736 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
737 MODULE_LICENSE("GPL v2");
738 MODULE_ALIAS("platform:imx-thermal");
739