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