1
2
3
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/device.h>
10#include <linux/init.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/kernel.h>
14#include <linux/mfd/syscon.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
19#include <linux/regmap.h>
20#include <linux/slab.h>
21#include <linux/thermal.h>
22#include <linux/types.h>
23#include <linux/nvmem-consumer.h>
24
25#define REG_SET 0x4
26#define REG_CLR 0x8
27#define REG_TOG 0xc
28
29
30#define IMX6_MISC0 0x0150
31#define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
32#define IMX6_MISC1 0x0160
33#define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
34
35#define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
36#define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
37
38#define IMX6_TEMPSENSE0 0x0180
39#define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
40#define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
41#define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
42#define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
43#define IMX6_TEMPSENSE0_FINISHED (1 << 2)
44#define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
45#define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
46
47#define IMX6_TEMPSENSE1 0x0190
48#define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
49#define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
50
51#define OCOTP_MEM0 0x0480
52#define OCOTP_ANA1 0x04e0
53
54
55#define IMX6_TEMPSENSE2 0x0290
56#define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
57#define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
58#define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
59#define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
60
61
62#define IMX7_ANADIG_DIGPROG 0x800
63#define IMX7_TEMPSENSE0 0x300
64#define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
65#define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
66#define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
67#define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
68#define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
69#define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
70
71#define IMX7_TEMPSENSE1 0x310
72#define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
73#define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
74#define IMX7_TEMPSENSE1_FINISHED (1 << 11)
75#define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
76#define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
77#define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
78#define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
79
80
81enum imx_thermal_trip {
82 IMX_TRIP_PASSIVE,
83 IMX_TRIP_CRITICAL,
84 IMX_TRIP_NUM,
85};
86
87#define IMX_POLLING_DELAY 2000
88#define IMX_PASSIVE_DELAY 1000
89
90#define TEMPMON_IMX6Q 1
91#define TEMPMON_IMX6SX 2
92#define TEMPMON_IMX7D 3
93
94struct thermal_soc_data {
95 u32 version;
96
97 u32 sensor_ctrl;
98 u32 power_down_mask;
99 u32 measure_temp_mask;
100
101 u32 measure_freq_ctrl;
102 u32 measure_freq_mask;
103 u32 measure_freq_shift;
104
105 u32 temp_data;
106 u32 temp_value_mask;
107 u32 temp_value_shift;
108 u32 temp_valid_mask;
109
110 u32 panic_alarm_ctrl;
111 u32 panic_alarm_mask;
112 u32 panic_alarm_shift;
113
114 u32 high_alarm_ctrl;
115 u32 high_alarm_mask;
116 u32 high_alarm_shift;
117
118 u32 low_alarm_ctrl;
119 u32 low_alarm_mask;
120 u32 low_alarm_shift;
121};
122
123static 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
144static 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
173static 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
202struct imx_thermal_data {
203 struct cpufreq_policy *policy;
204 struct thermal_zone_device *tz;
205 struct thermal_cooling_device *cdev;
206 struct regmap *tempmon;
207 u32 c1, c2;
208 int temp_passive;
209 int temp_critical;
210 int temp_max;
211 int alarm_temp;
212 int last_temp;
213 bool irq_enabled;
214 int irq;
215 struct clk *thermal_clk;
216 const struct thermal_soc_data *socdata;
217 const char *temp_grade;
218};
219
220static void imx_set_panic_temp(struct imx_thermal_data *data,
221 int panic_temp)
222{
223 const struct thermal_soc_data *soc_data = data->socdata;
224 struct regmap *map = data->tempmon;
225 int critical_value;
226
227 critical_value = (data->c2 - panic_temp) / data->c1;
228
229 regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
230 soc_data->panic_alarm_mask);
231 regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
232 critical_value << soc_data->panic_alarm_shift);
233}
234
235static void imx_set_alarm_temp(struct imx_thermal_data *data,
236 int alarm_temp)
237{
238 struct regmap *map = data->tempmon;
239 const struct thermal_soc_data *soc_data = data->socdata;
240 int alarm_value;
241
242 data->alarm_temp = alarm_temp;
243
244 if (data->socdata->version == TEMPMON_IMX7D)
245 alarm_value = alarm_temp / 1000 + data->c1 - 25;
246 else
247 alarm_value = (data->c2 - alarm_temp) / data->c1;
248
249 regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
250 soc_data->high_alarm_mask);
251 regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
252 alarm_value << soc_data->high_alarm_shift);
253}
254
255static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
256{
257 struct imx_thermal_data *data = tz->devdata;
258 const struct thermal_soc_data *soc_data = data->socdata;
259 struct regmap *map = data->tempmon;
260 unsigned int n_meas;
261 bool wait;
262 u32 val;
263
264 if (thermal_zone_device_is_enabled(tz)) {
265
266 regmap_read(map, soc_data->temp_data, &val);
267 wait = !(val & soc_data->temp_valid_mask);
268 } else {
269
270
271
272
273
274 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
275 soc_data->power_down_mask);
276 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
277 soc_data->measure_temp_mask);
278
279 wait = true;
280 }
281
282
283
284
285
286 if (wait)
287 usleep_range(20, 50);
288
289 regmap_read(map, soc_data->temp_data, &val);
290
291 if (!thermal_zone_device_is_enabled(tz)) {
292 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
293 soc_data->measure_temp_mask);
294 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
295 soc_data->power_down_mask);
296 }
297
298 if ((val & soc_data->temp_valid_mask) == 0) {
299 dev_dbg(&tz->device, "temp measurement never finished\n");
300 return -EAGAIN;
301 }
302
303 n_meas = (val & soc_data->temp_value_mask)
304 >> soc_data->temp_value_shift;
305
306
307 if (data->socdata->version == TEMPMON_IMX7D)
308 *temp = (n_meas - data->c1 + 25) * 1000;
309 else
310 *temp = data->c2 - n_meas * data->c1;
311
312
313 if (data->socdata->version == TEMPMON_IMX6Q) {
314 if (data->alarm_temp == data->temp_passive &&
315 *temp >= data->temp_passive)
316 imx_set_alarm_temp(data, data->temp_critical);
317 if (data->alarm_temp == data->temp_critical &&
318 *temp < data->temp_passive) {
319 imx_set_alarm_temp(data, data->temp_passive);
320 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
321 data->alarm_temp / 1000);
322 }
323 }
324
325 if (*temp != data->last_temp) {
326 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
327 data->last_temp = *temp;
328 }
329
330
331 if (!data->irq_enabled && *temp < data->alarm_temp) {
332 data->irq_enabled = true;
333 enable_irq(data->irq);
334 }
335
336 return 0;
337}
338
339static int imx_change_mode(struct thermal_zone_device *tz,
340 enum thermal_device_mode mode)
341{
342 struct imx_thermal_data *data = tz->devdata;
343 struct regmap *map = data->tempmon;
344 const struct thermal_soc_data *soc_data = data->socdata;
345
346 if (mode == THERMAL_DEVICE_ENABLED) {
347 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
348 soc_data->power_down_mask);
349 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
350 soc_data->measure_temp_mask);
351
352 if (!data->irq_enabled) {
353 data->irq_enabled = true;
354 enable_irq(data->irq);
355 }
356 } else {
357 regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
358 soc_data->measure_temp_mask);
359 regmap_write(map, soc_data->sensor_ctrl + REG_SET,
360 soc_data->power_down_mask);
361
362 if (data->irq_enabled) {
363 disable_irq(data->irq);
364 data->irq_enabled = false;
365 }
366 }
367
368 return 0;
369}
370
371static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
372 enum thermal_trip_type *type)
373{
374 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
375 THERMAL_TRIP_CRITICAL;
376 return 0;
377}
378
379static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
380{
381 struct imx_thermal_data *data = tz->devdata;
382
383 *temp = data->temp_critical;
384 return 0;
385}
386
387static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
388 int *temp)
389{
390 struct imx_thermal_data *data = tz->devdata;
391
392 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
393 data->temp_critical;
394 return 0;
395}
396
397static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
398 int temp)
399{
400 struct imx_thermal_data *data = tz->devdata;
401
402
403 if (trip == IMX_TRIP_CRITICAL)
404 return -EPERM;
405
406
407 if (temp < 0 || temp > data->temp_critical)
408 return -EINVAL;
409
410 data->temp_passive = temp;
411
412 imx_set_alarm_temp(data, temp);
413
414 return 0;
415}
416
417static int imx_bind(struct thermal_zone_device *tz,
418 struct thermal_cooling_device *cdev)
419{
420 int ret;
421
422 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
423 THERMAL_NO_LIMIT,
424 THERMAL_NO_LIMIT,
425 THERMAL_WEIGHT_DEFAULT);
426 if (ret) {
427 dev_err(&tz->device,
428 "binding zone %s with cdev %s failed:%d\n",
429 tz->type, cdev->type, ret);
430 return ret;
431 }
432
433 return 0;
434}
435
436static int imx_unbind(struct thermal_zone_device *tz,
437 struct thermal_cooling_device *cdev)
438{
439 int ret;
440
441 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
442 if (ret) {
443 dev_err(&tz->device,
444 "unbinding zone %s with cdev %s failed:%d\n",
445 tz->type, cdev->type, ret);
446 return ret;
447 }
448
449 return 0;
450}
451
452static struct thermal_zone_device_ops imx_tz_ops = {
453 .bind = imx_bind,
454 .unbind = imx_unbind,
455 .get_temp = imx_get_temp,
456 .change_mode = imx_change_mode,
457 .get_trip_type = imx_get_trip_type,
458 .get_trip_temp = imx_get_trip_temp,
459 .get_crit_temp = imx_get_crit_temp,
460 .set_trip_temp = imx_set_trip_temp,
461};
462
463static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
464{
465 struct imx_thermal_data *data = platform_get_drvdata(pdev);
466 int n1;
467 u64 temp64;
468
469 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
470 dev_err(&pdev->dev, "invalid sensor calibration data\n");
471 return -EINVAL;
472 }
473
474
475
476
477
478 if (data->socdata->version == TEMPMON_IMX7D) {
479 data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
480 return 0;
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502 n1 = ocotp_ana1 >> 20;
503
504 temp64 = 10000000;
505 temp64 *= 1000;
506 do_div(temp64, 15423 * n1 - 4148468);
507 data->c1 = temp64;
508 data->c2 = n1 * data->c1 + 28581;
509
510 return 0;
511}
512
513static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
514{
515 struct imx_thermal_data *data = platform_get_drvdata(pdev);
516
517
518 switch ((ocotp_mem0 >> 6) & 0x3) {
519 case 0:
520 data->temp_grade = "Commercial";
521 data->temp_max = 95000;
522 break;
523 case 1:
524 data->temp_grade = "Extended Commercial";
525 data->temp_max = 105000;
526 break;
527 case 2:
528 data->temp_grade = "Industrial";
529 data->temp_max = 105000;
530 break;
531 case 3:
532 data->temp_grade = "Automotive";
533 data->temp_max = 125000;
534 break;
535 }
536
537
538
539
540
541 data->temp_critical = data->temp_max - (1000 * 5);
542 data->temp_passive = data->temp_max - (1000 * 10);
543}
544
545static int imx_init_from_tempmon_data(struct platform_device *pdev)
546{
547 struct regmap *map;
548 int ret;
549 u32 val;
550
551 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
552 "fsl,tempmon-data");
553 if (IS_ERR(map)) {
554 ret = PTR_ERR(map);
555 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
556 return ret;
557 }
558
559 ret = regmap_read(map, OCOTP_ANA1, &val);
560 if (ret) {
561 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
562 return ret;
563 }
564 ret = imx_init_calib(pdev, val);
565 if (ret)
566 return ret;
567
568 ret = regmap_read(map, OCOTP_MEM0, &val);
569 if (ret) {
570 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
571 return ret;
572 }
573 imx_init_temp_grade(pdev, val);
574
575 return 0;
576}
577
578static int imx_init_from_nvmem_cells(struct platform_device *pdev)
579{
580 int ret;
581 u32 val;
582
583 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
584 if (ret)
585 return ret;
586 imx_init_calib(pdev, val);
587
588 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
589 if (ret)
590 return ret;
591 imx_init_temp_grade(pdev, val);
592
593 return 0;
594}
595
596static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
597{
598 struct imx_thermal_data *data = dev;
599
600 disable_irq_nosync(irq);
601 data->irq_enabled = false;
602
603 return IRQ_WAKE_THREAD;
604}
605
606static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
607{
608 struct imx_thermal_data *data = dev;
609
610 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
611 data->alarm_temp / 1000);
612
613 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
614
615 return IRQ_HANDLED;
616}
617
618static const struct of_device_id of_imx_thermal_match[] = {
619 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
620 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
621 { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
622 { }
623};
624MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
625
626static int imx_thermal_probe(struct platform_device *pdev)
627{
628 struct imx_thermal_data *data;
629 struct regmap *map;
630 int measure_freq;
631 int ret;
632
633 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
634 if (!data)
635 return -ENOMEM;
636
637 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
638 if (IS_ERR(map)) {
639 ret = PTR_ERR(map);
640 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
641 return ret;
642 }
643 data->tempmon = map;
644
645 data->socdata = of_device_get_match_data(&pdev->dev);
646 if (!data->socdata) {
647 dev_err(&pdev->dev, "no device match found\n");
648 return -ENODEV;
649 }
650
651
652 if (data->socdata->version == TEMPMON_IMX6SX) {
653 regmap_write(map, IMX6_MISC1 + REG_CLR,
654 IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
655 | IMX6_MISC1_IRQ_TEMPPANIC);
656
657
658
659
660 regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
661 data->socdata->low_alarm_mask);
662 }
663
664 data->irq = platform_get_irq(pdev, 0);
665 if (data->irq < 0)
666 return data->irq;
667
668 platform_set_drvdata(pdev, data);
669
670 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
671 ret = imx_init_from_nvmem_cells(pdev);
672 if (ret == -EPROBE_DEFER)
673 return ret;
674 if (ret) {
675 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
676 ret);
677 return ret;
678 }
679 } else {
680 ret = imx_init_from_tempmon_data(pdev);
681 if (ret) {
682 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
683 return ret;
684 }
685 }
686
687
688 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
689 data->socdata->power_down_mask);
690 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
691 data->socdata->measure_temp_mask);
692 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
693 data->socdata->measure_freq_mask);
694 if (data->socdata->version != TEMPMON_IMX7D)
695 regmap_write(map, IMX6_MISC0 + REG_SET,
696 IMX6_MISC0_REFTOP_SELBIASOFF);
697 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
698 data->socdata->power_down_mask);
699
700 data->policy = cpufreq_cpu_get(0);
701 if (!data->policy) {
702 pr_debug("%s: CPUFreq policy not found\n", __func__);
703 return -EPROBE_DEFER;
704 }
705
706 data->cdev = cpufreq_cooling_register(data->policy);
707 if (IS_ERR(data->cdev)) {
708 ret = PTR_ERR(data->cdev);
709 dev_err(&pdev->dev,
710 "failed to register cpufreq cooling device: %d\n", ret);
711 cpufreq_cpu_put(data->policy);
712 return ret;
713 }
714
715 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
716 if (IS_ERR(data->thermal_clk)) {
717 ret = PTR_ERR(data->thermal_clk);
718 if (ret != -EPROBE_DEFER)
719 dev_err(&pdev->dev,
720 "failed to get thermal clk: %d\n", ret);
721 cpufreq_cooling_unregister(data->cdev);
722 cpufreq_cpu_put(data->policy);
723 return ret;
724 }
725
726
727
728
729
730
731
732
733 ret = clk_prepare_enable(data->thermal_clk);
734 if (ret) {
735 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
736 cpufreq_cooling_unregister(data->cdev);
737 cpufreq_cpu_put(data->policy);
738 return ret;
739 }
740
741 data->tz = thermal_zone_device_register("imx_thermal_zone",
742 IMX_TRIP_NUM,
743 BIT(IMX_TRIP_PASSIVE), data,
744 &imx_tz_ops, NULL,
745 IMX_PASSIVE_DELAY,
746 IMX_POLLING_DELAY);
747 if (IS_ERR(data->tz)) {
748 ret = PTR_ERR(data->tz);
749 dev_err(&pdev->dev,
750 "failed to register thermal zone device %d\n", ret);
751 clk_disable_unprepare(data->thermal_clk);
752 cpufreq_cooling_unregister(data->cdev);
753 cpufreq_cpu_put(data->policy);
754 return ret;
755 }
756
757 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
758 " critical:%dC passive:%dC\n", data->temp_grade,
759 data->temp_max / 1000, data->temp_critical / 1000,
760 data->temp_passive / 1000);
761
762
763 regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
764 data->socdata->measure_freq_mask);
765 measure_freq = DIV_ROUND_UP(32768, 10);
766 regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
767 measure_freq << data->socdata->measure_freq_shift);
768 imx_set_alarm_temp(data, data->temp_passive);
769
770 if (data->socdata->version == TEMPMON_IMX6SX)
771 imx_set_panic_temp(data, data->temp_critical);
772
773 regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
774 data->socdata->power_down_mask);
775 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
776 data->socdata->measure_temp_mask);
777
778 data->irq_enabled = true;
779 ret = thermal_zone_device_enable(data->tz);
780 if (ret)
781 goto thermal_zone_unregister;
782
783 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
784 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
785 0, "imx_thermal", data);
786 if (ret < 0) {
787 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
788 clk_disable_unprepare(data->thermal_clk);
789 thermal_zone_device_unregister(data->tz);
790 cpufreq_cooling_unregister(data->cdev);
791 cpufreq_cpu_put(data->policy);
792 return ret;
793 }
794
795 return 0;
796}
797
798static int imx_thermal_remove(struct platform_device *pdev)
799{
800 struct imx_thermal_data *data = platform_get_drvdata(pdev);
801 struct regmap *map = data->tempmon;
802
803
804 regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
805 data->socdata->power_down_mask);
806 if (!IS_ERR(data->thermal_clk))
807 clk_disable_unprepare(data->thermal_clk);
808
809 thermal_zone_device_unregister(data->tz);
810 cpufreq_cooling_unregister(data->cdev);
811 cpufreq_cpu_put(data->policy);
812
813 return 0;
814}
815
816#ifdef CONFIG_PM_SLEEP
817static int imx_thermal_suspend(struct device *dev)
818{
819 struct imx_thermal_data *data = dev_get_drvdata(dev);
820 int ret;
821
822
823
824
825
826
827
828
829 ret = thermal_zone_device_disable(data->tz);
830 if (ret)
831 return ret;
832 clk_disable_unprepare(data->thermal_clk);
833
834 return 0;
835}
836
837static int imx_thermal_resume(struct device *dev)
838{
839 struct imx_thermal_data *data = dev_get_drvdata(dev);
840 int ret;
841
842 ret = clk_prepare_enable(data->thermal_clk);
843 if (ret)
844 return ret;
845
846 ret = thermal_zone_device_enable(data->tz);
847 if (ret)
848 return ret;
849
850 return 0;
851}
852#endif
853
854static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
855 imx_thermal_suspend, imx_thermal_resume);
856
857static struct platform_driver imx_thermal = {
858 .driver = {
859 .name = "imx_thermal",
860 .pm = &imx_thermal_pm_ops,
861 .of_match_table = of_imx_thermal_match,
862 },
863 .probe = imx_thermal_probe,
864 .remove = imx_thermal_remove,
865};
866module_platform_driver(imx_thermal);
867
868MODULE_AUTHOR("Freescale Semiconductor, Inc.");
869MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
870MODULE_LICENSE("GPL v2");
871MODULE_ALIAS("platform:imx-thermal");
872