2 * Copyright 2013 Freescale Semiconductor, Inc.
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.
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>
18 #include <linux/kernel.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.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>
34 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
36 #define MISC1_IRQ_TEMPHIGH (1 << 29)
37 /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
38 #define MISC1_IRQ_TEMPLOW (1 << 28)
39 #define MISC1_IRQ_TEMPPANIC (1 << 27)
41 #define TEMPSENSE0 0x0180
42 #define TEMPSENSE0_ALARM_VALUE_SHIFT 20
43 #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
44 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
45 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
46 #define TEMPSENSE0_FINISHED (1 << 2)
47 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
48 #define TEMPSENSE0_POWER_DOWN (1 << 0)
50 #define TEMPSENSE1 0x0190
51 #define TEMPSENSE1_MEASURE_FREQ 0xffff
52 /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
53 #define TEMPSENSE2 0x0290
54 #define TEMPSENSE2_LOW_VALUE_SHIFT 0
55 #define TEMPSENSE2_LOW_VALUE_MASK 0xfff
56 #define TEMPSENSE2_PANIC_VALUE_SHIFT 16
57 #define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
59 #define OCOTP_MEM0 0x0480
60 #define OCOTP_ANA1 0x04e0
62 /* The driver supports 1 passive trip point and 1 critical trip point */
63 enum imx_thermal_trip
{
69 #define IMX_POLLING_DELAY 2000 /* millisecond */
70 #define IMX_PASSIVE_DELAY 1000
72 #define FACTOR0 10000000
74 #define FACTOR2 4297157
76 #define TEMPMON_IMX6Q 1
77 #define TEMPMON_IMX6SX 2
79 struct thermal_soc_data
{
83 static struct thermal_soc_data thermal_imx6q_data
= {
84 .version
= TEMPMON_IMX6Q
,
87 static struct thermal_soc_data thermal_imx6sx_data
= {
88 .version
= TEMPMON_IMX6SX
,
91 struct imx_thermal_data
{
92 struct cpufreq_policy
*policy
;
93 struct thermal_zone_device
*tz
;
94 struct thermal_cooling_device
*cdev
;
95 enum thermal_device_mode mode
;
96 struct regmap
*tempmon
;
97 u32 c1
, c2
; /* See formula in imx_get_sensor_data() */
105 struct clk
*thermal_clk
;
106 const struct thermal_soc_data
*socdata
;
107 const char *temp_grade
;
110 static void imx_set_panic_temp(struct imx_thermal_data
*data
,
113 struct regmap
*map
= data
->tempmon
;
116 critical_value
= (data
->c2
- panic_temp
) / data
->c1
;
117 regmap_write(map
, TEMPSENSE2
+ REG_CLR
, TEMPSENSE2_PANIC_VALUE_MASK
);
118 regmap_write(map
, TEMPSENSE2
+ REG_SET
, critical_value
<<
119 TEMPSENSE2_PANIC_VALUE_SHIFT
);
122 static void imx_set_alarm_temp(struct imx_thermal_data
*data
,
125 struct regmap
*map
= data
->tempmon
;
128 data
->alarm_temp
= alarm_temp
;
129 alarm_value
= (data
->c2
- alarm_temp
) / data
->c1
;
130 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_ALARM_VALUE_MASK
);
131 regmap_write(map
, TEMPSENSE0
+ REG_SET
, alarm_value
<<
132 TEMPSENSE0_ALARM_VALUE_SHIFT
);
135 static int imx_get_temp(struct thermal_zone_device
*tz
, int *temp
)
137 struct imx_thermal_data
*data
= tz
->devdata
;
138 struct regmap
*map
= data
->tempmon
;
143 if (data
->mode
== THERMAL_DEVICE_ENABLED
) {
144 /* Check if a measurement is currently in progress */
145 regmap_read(map
, TEMPSENSE0
, &val
);
146 wait
= !(val
& TEMPSENSE0_FINISHED
);
149 * Every time we measure the temperature, we will power on the
150 * temperature sensor, enable measurements, take a reading,
151 * disable measurements, power off the temperature sensor.
153 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
154 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
160 * According to the temp sensor designers, it may require up to ~17us
161 * to complete a measurement.
164 usleep_range(20, 50);
166 regmap_read(map
, TEMPSENSE0
, &val
);
168 if (data
->mode
!= THERMAL_DEVICE_ENABLED
) {
169 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
170 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
173 if ((val
& TEMPSENSE0_FINISHED
) == 0) {
174 dev_dbg(&tz
->device
, "temp measurement never finished\n");
178 n_meas
= (val
& TEMPSENSE0_TEMP_CNT_MASK
) >> TEMPSENSE0_TEMP_CNT_SHIFT
;
180 /* See imx_get_sensor_data() for formula derivation */
181 *temp
= data
->c2
- n_meas
* data
->c1
;
183 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
184 if (data
->socdata
->version
== TEMPMON_IMX6Q
) {
185 if (data
->alarm_temp
== data
->temp_passive
&&
186 *temp
>= data
->temp_passive
)
187 imx_set_alarm_temp(data
, data
->temp_critical
);
188 if (data
->alarm_temp
== data
->temp_critical
&&
189 *temp
< data
->temp_passive
) {
190 imx_set_alarm_temp(data
, data
->temp_passive
);
191 dev_dbg(&tz
->device
, "thermal alarm off: T < %d\n",
192 data
->alarm_temp
/ 1000);
196 if (*temp
!= data
->last_temp
) {
197 dev_dbg(&tz
->device
, "millicelsius: %d\n", *temp
);
198 data
->last_temp
= *temp
;
201 /* Reenable alarm IRQ if temperature below alarm temperature */
202 if (!data
->irq_enabled
&& *temp
< data
->alarm_temp
) {
203 data
->irq_enabled
= true;
204 enable_irq(data
->irq
);
210 static int imx_get_mode(struct thermal_zone_device
*tz
,
211 enum thermal_device_mode
*mode
)
213 struct imx_thermal_data
*data
= tz
->devdata
;
220 static int imx_set_mode(struct thermal_zone_device
*tz
,
221 enum thermal_device_mode mode
)
223 struct imx_thermal_data
*data
= tz
->devdata
;
224 struct regmap
*map
= data
->tempmon
;
226 if (mode
== THERMAL_DEVICE_ENABLED
) {
227 tz
->polling_delay
= IMX_POLLING_DELAY
;
228 tz
->passive_delay
= IMX_PASSIVE_DELAY
;
230 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
231 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
233 if (!data
->irq_enabled
) {
234 data
->irq_enabled
= true;
235 enable_irq(data
->irq
);
238 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
239 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
241 tz
->polling_delay
= 0;
242 tz
->passive_delay
= 0;
244 if (data
->irq_enabled
) {
245 disable_irq(data
->irq
);
246 data
->irq_enabled
= false;
251 thermal_zone_device_update(tz
, THERMAL_EVENT_UNSPECIFIED
);
256 static int imx_get_trip_type(struct thermal_zone_device
*tz
, int trip
,
257 enum thermal_trip_type
*type
)
259 *type
= (trip
== IMX_TRIP_PASSIVE
) ? THERMAL_TRIP_PASSIVE
:
260 THERMAL_TRIP_CRITICAL
;
264 static int imx_get_crit_temp(struct thermal_zone_device
*tz
, int *temp
)
266 struct imx_thermal_data
*data
= tz
->devdata
;
268 *temp
= data
->temp_critical
;
272 static int imx_get_trip_temp(struct thermal_zone_device
*tz
, int trip
,
275 struct imx_thermal_data
*data
= tz
->devdata
;
277 *temp
= (trip
== IMX_TRIP_PASSIVE
) ? data
->temp_passive
:
282 static int imx_set_trip_temp(struct thermal_zone_device
*tz
, int trip
,
285 struct imx_thermal_data
*data
= tz
->devdata
;
287 /* do not allow changing critical threshold */
288 if (trip
== IMX_TRIP_CRITICAL
)
291 /* do not allow passive to be set higher than critical */
292 if (temp
< 0 || temp
> data
->temp_critical
)
295 data
->temp_passive
= temp
;
297 imx_set_alarm_temp(data
, temp
);
302 static int imx_bind(struct thermal_zone_device
*tz
,
303 struct thermal_cooling_device
*cdev
)
307 ret
= thermal_zone_bind_cooling_device(tz
, IMX_TRIP_PASSIVE
, cdev
,
310 THERMAL_WEIGHT_DEFAULT
);
313 "binding zone %s with cdev %s failed:%d\n",
314 tz
->type
, cdev
->type
, ret
);
321 static int imx_unbind(struct thermal_zone_device
*tz
,
322 struct thermal_cooling_device
*cdev
)
326 ret
= thermal_zone_unbind_cooling_device(tz
, IMX_TRIP_PASSIVE
, cdev
);
329 "unbinding zone %s with cdev %s failed:%d\n",
330 tz
->type
, cdev
->type
, ret
);
337 static struct thermal_zone_device_ops imx_tz_ops
= {
339 .unbind
= imx_unbind
,
340 .get_temp
= imx_get_temp
,
341 .get_mode
= imx_get_mode
,
342 .set_mode
= imx_set_mode
,
343 .get_trip_type
= imx_get_trip_type
,
344 .get_trip_temp
= imx_get_trip_temp
,
345 .get_crit_temp
= imx_get_crit_temp
,
346 .set_trip_temp
= imx_set_trip_temp
,
349 static int imx_get_sensor_data(struct platform_device
*pdev
)
351 struct imx_thermal_data
*data
= platform_get_drvdata(pdev
);
358 map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
362 dev_err(&pdev
->dev
, "failed to get sensor regmap: %d\n", ret
);
366 ret
= regmap_read(map
, OCOTP_ANA1
, &val
);
368 dev_err(&pdev
->dev
, "failed to read sensor data: %d\n", ret
);
372 if (val
== 0 || val
== ~0) {
373 dev_err(&pdev
->dev
, "invalid sensor calibration data\n");
378 * Sensor data layout:
379 * [31:20] - sensor value @ 25C
380 * Use universal formula now and only need sensor value @ 25C
381 * slope = 0.4297157 - (0.0015976 * 25C fuse)
384 t1
= 25; /* t1 always 25C */
387 * Derived from linear interpolation:
388 * slope = 0.4297157 - (0.0015976 * 25C fuse)
389 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
390 * (Nmeas - n1) / (Tmeas - t1) = slope
391 * We want to reduce this down to the minimum computation necessary
392 * for each temperature read. Also, we want Tmeas in millicelsius
393 * and we don't want to lose precision from integer division. So...
394 * Tmeas = (Nmeas - n1) / slope + t1
395 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
396 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
397 * Let constant c1 = (-1000 / slope)
398 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
399 * Let constant c2 = n1 *c1 + 1000 * t1
400 * milli_Tmeas = c2 - Nmeas * c1
404 do_div(temp64
, FACTOR1
* n1
- FACTOR2
);
406 data
->c2
= n1
* data
->c1
+ 1000 * t1
;
408 /* use OTP for thermal grade */
409 ret
= regmap_read(map
, OCOTP_MEM0
, &val
);
411 dev_err(&pdev
->dev
, "failed to read temp grade: %d\n", ret
);
415 /* The maximum die temp is specified by the Temperature Grade */
416 switch ((val
>> 6) & 0x3) {
417 case 0: /* Commercial (0 to 95C) */
418 data
->temp_grade
= "Commercial";
419 data
->temp_max
= 95000;
421 case 1: /* Extended Commercial (-20 to 105C) */
422 data
->temp_grade
= "Extended Commercial";
423 data
->temp_max
= 105000;
425 case 2: /* Industrial (-40 to 105C) */
426 data
->temp_grade
= "Industrial";
427 data
->temp_max
= 105000;
429 case 3: /* Automotive (-40 to 125C) */
430 data
->temp_grade
= "Automotive";
431 data
->temp_max
= 125000;
436 * Set the critical trip point at 5C under max
437 * Set the passive trip point at 10C under max (can change via sysfs)
439 data
->temp_critical
= data
->temp_max
- (1000 * 5);
440 data
->temp_passive
= data
->temp_max
- (1000 * 10);
445 static irqreturn_t
imx_thermal_alarm_irq(int irq
, void *dev
)
447 struct imx_thermal_data
*data
= dev
;
449 disable_irq_nosync(irq
);
450 data
->irq_enabled
= false;
452 return IRQ_WAKE_THREAD
;
455 static irqreturn_t
imx_thermal_alarm_irq_thread(int irq
, void *dev
)
457 struct imx_thermal_data
*data
= dev
;
459 dev_dbg(&data
->tz
->device
, "THERMAL ALARM: T > %d\n",
460 data
->alarm_temp
/ 1000);
462 thermal_zone_device_update(data
->tz
, THERMAL_EVENT_UNSPECIFIED
);
467 static const struct of_device_id of_imx_thermal_match
[] = {
468 { .compatible
= "fsl,imx6q-tempmon", .data
= &thermal_imx6q_data
, },
469 { .compatible
= "fsl,imx6sx-tempmon", .data
= &thermal_imx6sx_data
, },
472 MODULE_DEVICE_TABLE(of
, of_imx_thermal_match
);
474 static int imx_thermal_probe(struct platform_device
*pdev
)
476 struct imx_thermal_data
*data
;
481 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
485 map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
, "fsl,tempmon");
488 dev_err(&pdev
->dev
, "failed to get tempmon regmap: %d\n", ret
);
493 data
->socdata
= of_device_get_match_data(&pdev
->dev
);
494 if (!data
->socdata
) {
495 dev_err(&pdev
->dev
, "no device match found\n");
499 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
500 if (data
->socdata
->version
== TEMPMON_IMX6SX
) {
501 regmap_write(map
, MISC1
+ REG_CLR
, MISC1_IRQ_TEMPHIGH
|
502 MISC1_IRQ_TEMPLOW
| MISC1_IRQ_TEMPPANIC
);
504 * reset value of LOW ALARM is incorrect, set it to lowest
505 * value to avoid false trigger of low alarm.
507 regmap_write(map
, TEMPSENSE2
+ REG_SET
,
508 TEMPSENSE2_LOW_VALUE_MASK
);
511 data
->irq
= platform_get_irq(pdev
, 0);
515 platform_set_drvdata(pdev
, data
);
517 ret
= imx_get_sensor_data(pdev
);
519 dev_err(&pdev
->dev
, "failed to get sensor data\n");
523 /* Make sure sensor is in known good state for measurements */
524 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
525 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
526 regmap_write(map
, TEMPSENSE1
+ REG_CLR
, TEMPSENSE1_MEASURE_FREQ
);
527 regmap_write(map
, MISC0
+ REG_SET
, MISC0_REFTOP_SELBIASOFF
);
528 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
530 data
->policy
= cpufreq_cpu_get(0);
532 pr_debug("%s: CPUFreq policy not found\n", __func__
);
533 return -EPROBE_DEFER
;
536 data
->cdev
= cpufreq_cooling_register(data
->policy
);
537 if (IS_ERR(data
->cdev
)) {
538 ret
= PTR_ERR(data
->cdev
);
540 "failed to register cpufreq cooling device: %d\n", ret
);
541 cpufreq_cpu_put(data
->policy
);
545 data
->thermal_clk
= devm_clk_get(&pdev
->dev
, NULL
);
546 if (IS_ERR(data
->thermal_clk
)) {
547 ret
= PTR_ERR(data
->thermal_clk
);
548 if (ret
!= -EPROBE_DEFER
)
550 "failed to get thermal clk: %d\n", ret
);
551 cpufreq_cooling_unregister(data
->cdev
);
552 cpufreq_cpu_put(data
->policy
);
557 * Thermal sensor needs clk on to get correct value, normally
558 * we should enable its clk before taking measurement and disable
559 * clk after measurement is done, but if alarm function is enabled,
560 * hardware will auto measure the temperature periodically, so we
561 * need to keep the clk always on for alarm function.
563 ret
= clk_prepare_enable(data
->thermal_clk
);
565 dev_err(&pdev
->dev
, "failed to enable thermal clk: %d\n", ret
);
566 cpufreq_cooling_unregister(data
->cdev
);
567 cpufreq_cpu_put(data
->policy
);
571 data
->tz
= thermal_zone_device_register("imx_thermal_zone",
573 BIT(IMX_TRIP_PASSIVE
), data
,
577 if (IS_ERR(data
->tz
)) {
578 ret
= PTR_ERR(data
->tz
);
580 "failed to register thermal zone device %d\n", ret
);
581 clk_disable_unprepare(data
->thermal_clk
);
582 cpufreq_cooling_unregister(data
->cdev
);
583 cpufreq_cpu_put(data
->policy
);
587 dev_info(&pdev
->dev
, "%s CPU temperature grade - max:%dC"
588 " critical:%dC passive:%dC\n", data
->temp_grade
,
589 data
->temp_max
/ 1000, data
->temp_critical
/ 1000,
590 data
->temp_passive
/ 1000);
592 /* Enable measurements at ~ 10 Hz */
593 regmap_write(map
, TEMPSENSE1
+ REG_CLR
, TEMPSENSE1_MEASURE_FREQ
);
594 measure_freq
= DIV_ROUND_UP(32768, 10); /* 10 Hz */
595 regmap_write(map
, TEMPSENSE1
+ REG_SET
, measure_freq
);
596 imx_set_alarm_temp(data
, data
->temp_passive
);
598 if (data
->socdata
->version
== TEMPMON_IMX6SX
)
599 imx_set_panic_temp(data
, data
->temp_critical
);
601 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
602 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
604 ret
= devm_request_threaded_irq(&pdev
->dev
, data
->irq
,
605 imx_thermal_alarm_irq
, imx_thermal_alarm_irq_thread
,
606 0, "imx_thermal", data
);
608 dev_err(&pdev
->dev
, "failed to request alarm irq: %d\n", ret
);
609 clk_disable_unprepare(data
->thermal_clk
);
610 thermal_zone_device_unregister(data
->tz
);
611 cpufreq_cooling_unregister(data
->cdev
);
612 cpufreq_cpu_put(data
->policy
);
616 data
->irq_enabled
= true;
617 data
->mode
= THERMAL_DEVICE_ENABLED
;
622 static int imx_thermal_remove(struct platform_device
*pdev
)
624 struct imx_thermal_data
*data
= platform_get_drvdata(pdev
);
625 struct regmap
*map
= data
->tempmon
;
627 /* Disable measurements */
628 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
629 if (!IS_ERR(data
->thermal_clk
))
630 clk_disable_unprepare(data
->thermal_clk
);
632 thermal_zone_device_unregister(data
->tz
);
633 cpufreq_cooling_unregister(data
->cdev
);
634 cpufreq_cpu_put(data
->policy
);
639 #ifdef CONFIG_PM_SLEEP
640 static int imx_thermal_suspend(struct device
*dev
)
642 struct imx_thermal_data
*data
= dev_get_drvdata(dev
);
643 struct regmap
*map
= data
->tempmon
;
646 * Need to disable thermal sensor, otherwise, when thermal core
647 * try to get temperature before thermal sensor resume, a wrong
648 * temperature will be read as the thermal sensor is powered
651 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
652 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
653 data
->mode
= THERMAL_DEVICE_DISABLED
;
654 clk_disable_unprepare(data
->thermal_clk
);
659 static int imx_thermal_resume(struct device
*dev
)
661 struct imx_thermal_data
*data
= dev_get_drvdata(dev
);
662 struct regmap
*map
= data
->tempmon
;
665 ret
= clk_prepare_enable(data
->thermal_clk
);
668 /* Enabled thermal sensor after resume */
669 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
670 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
671 data
->mode
= THERMAL_DEVICE_ENABLED
;
677 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops
,
678 imx_thermal_suspend
, imx_thermal_resume
);
680 static struct platform_driver imx_thermal
= {
682 .name
= "imx_thermal",
683 .pm
= &imx_thermal_pm_ops
,
684 .of_match_table
= of_imx_thermal_match
,
686 .probe
= imx_thermal_probe
,
687 .remove
= imx_thermal_remove
,
689 module_platform_driver(imx_thermal
);
691 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
692 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
693 MODULE_LICENSE("GPL v2");
694 MODULE_ALIAS("platform:imx-thermal");