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/cpu_cooling.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
21 #include <linux/of_device.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>
33 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
35 #define MISC1_IRQ_TEMPHIGH (1 << 29)
36 /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
37 #define MISC1_IRQ_TEMPLOW (1 << 28)
38 #define MISC1_IRQ_TEMPPANIC (1 << 27)
40 #define TEMPSENSE0 0x0180
41 #define TEMPSENSE0_ALARM_VALUE_SHIFT 20
42 #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
43 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
44 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
45 #define TEMPSENSE0_FINISHED (1 << 2)
46 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
47 #define TEMPSENSE0_POWER_DOWN (1 << 0)
49 #define TEMPSENSE1 0x0190
50 #define TEMPSENSE1_MEASURE_FREQ 0xffff
51 /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
52 #define TEMPSENSE2 0x0290
53 #define TEMPSENSE2_LOW_VALUE_SHIFT 0
54 #define TEMPSENSE2_LOW_VALUE_MASK 0xfff
55 #define TEMPSENSE2_PANIC_VALUE_SHIFT 16
56 #define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
58 #define OCOTP_ANA1 0x04e0
60 /* The driver supports 1 passive trip point and 1 critical trip point */
61 enum imx_thermal_trip
{
68 * It defines the temperature in millicelsius for passive trip point
69 * that will trigger cooling action when crossed.
71 #define IMX_TEMP_PASSIVE 85000
73 #define IMX_POLLING_DELAY 2000 /* millisecond */
74 #define IMX_PASSIVE_DELAY 1000
76 #define FACTOR0 10000000
78 #define FACTOR2 4297157
80 #define TEMPMON_IMX6Q 1
81 #define TEMPMON_IMX6SX 2
83 struct thermal_soc_data
{
87 static struct thermal_soc_data thermal_imx6q_data
= {
88 .version
= TEMPMON_IMX6Q
,
91 static struct thermal_soc_data thermal_imx6sx_data
= {
92 .version
= TEMPMON_IMX6SX
,
95 struct imx_thermal_data
{
96 struct thermal_zone_device
*tz
;
97 struct thermal_cooling_device
*cdev
;
98 enum thermal_device_mode mode
;
99 struct regmap
*tempmon
;
100 u32 c1
, c2
; /* See formula in imx_get_sensor_data() */
101 unsigned long temp_passive
;
102 unsigned long temp_critical
;
103 unsigned long alarm_temp
;
104 unsigned long last_temp
;
107 struct clk
*thermal_clk
;
108 const struct thermal_soc_data
*socdata
;
111 static void imx_set_panic_temp(struct imx_thermal_data
*data
,
112 signed long panic_temp
)
114 struct regmap
*map
= data
->tempmon
;
117 critical_value
= (data
->c2
- panic_temp
) / data
->c1
;
118 regmap_write(map
, TEMPSENSE2
+ REG_CLR
, TEMPSENSE2_PANIC_VALUE_MASK
);
119 regmap_write(map
, TEMPSENSE2
+ REG_SET
, critical_value
<<
120 TEMPSENSE2_PANIC_VALUE_SHIFT
);
123 static void imx_set_alarm_temp(struct imx_thermal_data
*data
,
124 signed long alarm_temp
)
126 struct regmap
*map
= data
->tempmon
;
129 data
->alarm_temp
= alarm_temp
;
130 alarm_value
= (data
->c2
- alarm_temp
) / data
->c1
;
131 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_ALARM_VALUE_MASK
);
132 regmap_write(map
, TEMPSENSE0
+ REG_SET
, alarm_value
<<
133 TEMPSENSE0_ALARM_VALUE_SHIFT
);
136 static int imx_get_temp(struct thermal_zone_device
*tz
, unsigned long *temp
)
138 struct imx_thermal_data
*data
= tz
->devdata
;
139 struct regmap
*map
= data
->tempmon
;
144 if (data
->mode
== THERMAL_DEVICE_ENABLED
) {
145 /* Check if a measurement is currently in progress */
146 regmap_read(map
, TEMPSENSE0
, &val
);
147 wait
= !(val
& TEMPSENSE0_FINISHED
);
150 * Every time we measure the temperature, we will power on the
151 * temperature sensor, enable measurements, take a reading,
152 * disable measurements, power off the temperature sensor.
154 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
155 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
161 * According to the temp sensor designers, it may require up to ~17us
162 * to complete a measurement.
165 usleep_range(20, 50);
167 regmap_read(map
, TEMPSENSE0
, &val
);
169 if (data
->mode
!= THERMAL_DEVICE_ENABLED
) {
170 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
171 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
174 if ((val
& TEMPSENSE0_FINISHED
) == 0) {
175 dev_dbg(&tz
->device
, "temp measurement never finished\n");
179 n_meas
= (val
& TEMPSENSE0_TEMP_CNT_MASK
) >> TEMPSENSE0_TEMP_CNT_SHIFT
;
181 /* See imx_get_sensor_data() for formula derivation */
182 *temp
= data
->c2
- n_meas
* data
->c1
;
184 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
185 if (data
->socdata
->version
== TEMPMON_IMX6Q
) {
186 if (data
->alarm_temp
== data
->temp_passive
&&
187 *temp
>= data
->temp_passive
)
188 imx_set_alarm_temp(data
, data
->temp_critical
);
189 if (data
->alarm_temp
== data
->temp_critical
&&
190 *temp
< data
->temp_passive
) {
191 imx_set_alarm_temp(data
, data
->temp_passive
);
192 dev_dbg(&tz
->device
, "thermal alarm off: T < %lu\n",
193 data
->alarm_temp
/ 1000);
197 if (*temp
!= data
->last_temp
) {
198 dev_dbg(&tz
->device
, "millicelsius: %ld\n", *temp
);
199 data
->last_temp
= *temp
;
202 /* Reenable alarm IRQ if temperature below alarm temperature */
203 if (!data
->irq_enabled
&& *temp
< data
->alarm_temp
) {
204 data
->irq_enabled
= true;
205 enable_irq(data
->irq
);
211 static int imx_get_mode(struct thermal_zone_device
*tz
,
212 enum thermal_device_mode
*mode
)
214 struct imx_thermal_data
*data
= tz
->devdata
;
221 static int imx_set_mode(struct thermal_zone_device
*tz
,
222 enum thermal_device_mode mode
)
224 struct imx_thermal_data
*data
= tz
->devdata
;
225 struct regmap
*map
= data
->tempmon
;
227 if (mode
== THERMAL_DEVICE_ENABLED
) {
228 tz
->polling_delay
= IMX_POLLING_DELAY
;
229 tz
->passive_delay
= IMX_PASSIVE_DELAY
;
231 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
232 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
234 if (!data
->irq_enabled
) {
235 data
->irq_enabled
= true;
236 enable_irq(data
->irq
);
239 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
240 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
242 tz
->polling_delay
= 0;
243 tz
->passive_delay
= 0;
245 if (data
->irq_enabled
) {
246 disable_irq(data
->irq
);
247 data
->irq_enabled
= false;
252 thermal_zone_device_update(tz
);
257 static int imx_get_trip_type(struct thermal_zone_device
*tz
, int trip
,
258 enum thermal_trip_type
*type
)
260 *type
= (trip
== IMX_TRIP_PASSIVE
) ? THERMAL_TRIP_PASSIVE
:
261 THERMAL_TRIP_CRITICAL
;
265 static int imx_get_crit_temp(struct thermal_zone_device
*tz
,
268 struct imx_thermal_data
*data
= tz
->devdata
;
270 *temp
= data
->temp_critical
;
274 static int imx_get_trip_temp(struct thermal_zone_device
*tz
, int trip
,
277 struct imx_thermal_data
*data
= tz
->devdata
;
279 *temp
= (trip
== IMX_TRIP_PASSIVE
) ? data
->temp_passive
:
284 static int imx_set_trip_temp(struct thermal_zone_device
*tz
, int trip
,
287 struct imx_thermal_data
*data
= tz
->devdata
;
289 if (trip
== IMX_TRIP_CRITICAL
)
292 if (temp
> IMX_TEMP_PASSIVE
)
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
,
312 "binding zone %s with cdev %s failed:%d\n",
313 tz
->type
, cdev
->type
, ret
);
320 static int imx_unbind(struct thermal_zone_device
*tz
,
321 struct thermal_cooling_device
*cdev
)
325 ret
= thermal_zone_unbind_cooling_device(tz
, IMX_TRIP_PASSIVE
, cdev
);
328 "unbinding zone %s with cdev %s failed:%d\n",
329 tz
->type
, cdev
->type
, ret
);
336 static struct thermal_zone_device_ops imx_tz_ops
= {
338 .unbind
= imx_unbind
,
339 .get_temp
= imx_get_temp
,
340 .get_mode
= imx_get_mode
,
341 .set_mode
= imx_set_mode
,
342 .get_trip_type
= imx_get_trip_type
,
343 .get_trip_temp
= imx_get_trip_temp
,
344 .get_crit_temp
= imx_get_crit_temp
,
345 .set_trip_temp
= imx_set_trip_temp
,
348 static int imx_get_sensor_data(struct platform_device
*pdev
)
350 struct imx_thermal_data
*data
= platform_get_drvdata(pdev
);
357 map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
361 dev_err(&pdev
->dev
, "failed to get sensor regmap: %d\n", ret
);
365 ret
= regmap_read(map
, OCOTP_ANA1
, &val
);
367 dev_err(&pdev
->dev
, "failed to read sensor data: %d\n", ret
);
371 if (val
== 0 || val
== ~0) {
372 dev_err(&pdev
->dev
, "invalid sensor calibration data\n");
377 * Sensor data layout:
378 * [31:20] - sensor value @ 25C
379 * Use universal formula now and only need sensor value @ 25C
380 * slope = 0.4297157 - (0.0015976 * 25C fuse)
383 t1
= 25; /* t1 always 25C */
386 * Derived from linear interpolation:
387 * slope = 0.4297157 - (0.0015976 * 25C fuse)
388 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
389 * (Nmeas - n1) / (Tmeas - t1) = slope
390 * We want to reduce this down to the minimum computation necessary
391 * for each temperature read. Also, we want Tmeas in millicelsius
392 * and we don't want to lose precision from integer division. So...
393 * Tmeas = (Nmeas - n1) / slope + t1
394 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
395 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
396 * Let constant c1 = (-1000 / slope)
397 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
398 * Let constant c2 = n1 *c1 + 1000 * t1
399 * milli_Tmeas = c2 - Nmeas * c1
403 do_div(temp64
, FACTOR1
* n1
- FACTOR2
);
405 data
->c2
= n1
* data
->c1
+ 1000 * t1
;
408 * Set the default passive cooling trip point,
409 * can be changed from userspace.
411 data
->temp_passive
= IMX_TEMP_PASSIVE
;
414 * The maximum die temperature set to 20 C higher than
417 data
->temp_critical
= 1000 * 20 + data
->temp_passive
;
422 static irqreturn_t
imx_thermal_alarm_irq(int irq
, void *dev
)
424 struct imx_thermal_data
*data
= dev
;
426 disable_irq_nosync(irq
);
427 data
->irq_enabled
= false;
429 return IRQ_WAKE_THREAD
;
432 static irqreturn_t
imx_thermal_alarm_irq_thread(int irq
, void *dev
)
434 struct imx_thermal_data
*data
= dev
;
436 dev_dbg(&data
->tz
->device
, "THERMAL ALARM: T > %lu\n",
437 data
->alarm_temp
/ 1000);
439 thermal_zone_device_update(data
->tz
);
444 static const struct of_device_id of_imx_thermal_match
[] = {
445 { .compatible
= "fsl,imx6q-tempmon", .data
= &thermal_imx6q_data
, },
446 { .compatible
= "fsl,imx6sx-tempmon", .data
= &thermal_imx6sx_data
, },
449 MODULE_DEVICE_TABLE(of
, of_imx_thermal_match
);
451 static int imx_thermal_probe(struct platform_device
*pdev
)
453 const struct of_device_id
*of_id
=
454 of_match_device(of_imx_thermal_match
, &pdev
->dev
);
455 struct imx_thermal_data
*data
;
460 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
464 map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
, "fsl,tempmon");
467 dev_err(&pdev
->dev
, "failed to get tempmon regmap: %d\n", ret
);
472 data
->socdata
= of_id
->data
;
474 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
475 if (data
->socdata
->version
== TEMPMON_IMX6SX
) {
476 regmap_write(map
, MISC1
+ REG_CLR
, MISC1_IRQ_TEMPHIGH
|
477 MISC1_IRQ_TEMPLOW
| MISC1_IRQ_TEMPPANIC
);
479 * reset value of LOW ALARM is incorrect, set it to lowest
480 * value to avoid false trigger of low alarm.
482 regmap_write(map
, TEMPSENSE2
+ REG_SET
,
483 TEMPSENSE2_LOW_VALUE_MASK
);
486 data
->irq
= platform_get_irq(pdev
, 0);
490 ret
= devm_request_threaded_irq(&pdev
->dev
, data
->irq
,
491 imx_thermal_alarm_irq
, imx_thermal_alarm_irq_thread
,
492 0, "imx_thermal", data
);
494 dev_err(&pdev
->dev
, "failed to request alarm irq: %d\n", ret
);
498 platform_set_drvdata(pdev
, data
);
500 ret
= imx_get_sensor_data(pdev
);
502 dev_err(&pdev
->dev
, "failed to get sensor data\n");
506 /* Make sure sensor is in known good state for measurements */
507 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
508 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
509 regmap_write(map
, TEMPSENSE1
+ REG_CLR
, TEMPSENSE1_MEASURE_FREQ
);
510 regmap_write(map
, MISC0
+ REG_SET
, MISC0_REFTOP_SELBIASOFF
);
511 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
513 data
->cdev
= cpufreq_cooling_register(cpu_present_mask
);
514 if (IS_ERR(data
->cdev
)) {
515 ret
= PTR_ERR(data
->cdev
);
516 if (ret
!= -EPROBE_DEFER
)
518 "failed to register cpufreq cooling device: %d\n",
523 data
->thermal_clk
= devm_clk_get(&pdev
->dev
, NULL
);
524 if (IS_ERR(data
->thermal_clk
)) {
525 ret
= PTR_ERR(data
->thermal_clk
);
526 if (ret
!= -EPROBE_DEFER
)
528 "failed to get thermal clk: %d\n", ret
);
529 cpufreq_cooling_unregister(data
->cdev
);
534 * Thermal sensor needs clk on to get correct value, normally
535 * we should enable its clk before taking measurement and disable
536 * clk after measurement is done, but if alarm function is enabled,
537 * hardware will auto measure the temperature periodically, so we
538 * need to keep the clk always on for alarm function.
540 ret
= clk_prepare_enable(data
->thermal_clk
);
542 dev_err(&pdev
->dev
, "failed to enable thermal clk: %d\n", ret
);
543 cpufreq_cooling_unregister(data
->cdev
);
547 data
->tz
= thermal_zone_device_register("imx_thermal_zone",
549 BIT(IMX_TRIP_PASSIVE
), data
,
553 if (IS_ERR(data
->tz
)) {
554 ret
= PTR_ERR(data
->tz
);
556 "failed to register thermal zone device %d\n", ret
);
557 clk_disable_unprepare(data
->thermal_clk
);
558 cpufreq_cooling_unregister(data
->cdev
);
562 /* Enable measurements at ~ 10 Hz */
563 regmap_write(map
, TEMPSENSE1
+ REG_CLR
, TEMPSENSE1_MEASURE_FREQ
);
564 measure_freq
= DIV_ROUND_UP(32768, 10); /* 10 Hz */
565 regmap_write(map
, TEMPSENSE1
+ REG_SET
, measure_freq
);
566 imx_set_alarm_temp(data
, data
->temp_passive
);
568 if (data
->socdata
->version
== TEMPMON_IMX6SX
)
569 imx_set_panic_temp(data
, data
->temp_critical
);
571 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
572 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
574 data
->irq_enabled
= true;
575 data
->mode
= THERMAL_DEVICE_ENABLED
;
580 static int imx_thermal_remove(struct platform_device
*pdev
)
582 struct imx_thermal_data
*data
= platform_get_drvdata(pdev
);
583 struct regmap
*map
= data
->tempmon
;
585 /* Disable measurements */
586 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
587 if (!IS_ERR(data
->thermal_clk
))
588 clk_disable_unprepare(data
->thermal_clk
);
590 thermal_zone_device_unregister(data
->tz
);
591 cpufreq_cooling_unregister(data
->cdev
);
596 #ifdef CONFIG_PM_SLEEP
597 static int imx_thermal_suspend(struct device
*dev
)
599 struct imx_thermal_data
*data
= dev_get_drvdata(dev
);
600 struct regmap
*map
= data
->tempmon
;
603 * Need to disable thermal sensor, otherwise, when thermal core
604 * try to get temperature before thermal sensor resume, a wrong
605 * temperature will be read as the thermal sensor is powered
608 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
609 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
610 data
->mode
= THERMAL_DEVICE_DISABLED
;
611 clk_disable_unprepare(data
->thermal_clk
);
616 static int imx_thermal_resume(struct device
*dev
)
618 struct imx_thermal_data
*data
= dev_get_drvdata(dev
);
619 struct regmap
*map
= data
->tempmon
;
621 clk_prepare_enable(data
->thermal_clk
);
622 /* Enabled thermal sensor after resume */
623 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
624 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
625 data
->mode
= THERMAL_DEVICE_ENABLED
;
631 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops
,
632 imx_thermal_suspend
, imx_thermal_resume
);
634 static struct platform_driver imx_thermal
= {
636 .name
= "imx_thermal",
637 .pm
= &imx_thermal_pm_ops
,
638 .of_match_table
= of_imx_thermal_match
,
640 .probe
= imx_thermal_probe
,
641 .remove
= imx_thermal_remove
,
643 module_platform_driver(imx_thermal
);
645 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
646 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
647 MODULE_LICENSE("GPL v2");
648 MODULE_ALIAS("platform:imx-thermal");