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