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/cpufreq.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/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 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)
44 #define TEMPSENSE1 0x0190
45 #define TEMPSENSE1_MEASURE_FREQ 0xffff
47 #define OCOTP_ANA1 0x04e0
49 /* The driver supports 1 passive trip point and 1 critical trip point */
50 enum imx_thermal_trip
{
57 * It defines the temperature in millicelsius for passive trip point
58 * that will trigger cooling action when crossed.
60 #define IMX_TEMP_PASSIVE 85000
62 #define IMX_POLLING_DELAY 2000 /* millisecond */
63 #define IMX_PASSIVE_DELAY 1000
65 #define FACTOR0 10000000
67 #define FACTOR2 4297157
69 struct 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
;
81 struct clk
*thermal_clk
;
84 static void imx_set_alarm_temp(struct imx_thermal_data
*data
,
85 signed long alarm_temp
)
87 struct regmap
*map
= data
->tempmon
;
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
);
97 static int imx_get_temp(struct thermal_zone_device
*tz
, unsigned long *temp
)
99 struct imx_thermal_data
*data
= tz
->devdata
;
100 struct regmap
*map
= data
->tempmon
;
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
);
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.
115 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
116 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
122 * According to the temp sensor designers, it may require up to ~17us
123 * to complete a measurement.
126 usleep_range(20, 50);
128 regmap_read(map
, TEMPSENSE0
, &val
);
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
);
135 if ((val
& TEMPSENSE0_FINISHED
) == 0) {
136 dev_dbg(&tz
->device
, "temp measurement never finished\n");
140 n_meas
= (val
& TEMPSENSE0_TEMP_CNT_MASK
) >> TEMPSENSE0_TEMP_CNT_SHIFT
;
142 /* See imx_get_sensor_data() for formula derivation */
143 *temp
= data
->c2
- n_meas
* data
->c1
;
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);
154 if (*temp
!= data
->last_temp
) {
155 dev_dbg(&tz
->device
, "millicelsius: %ld\n", *temp
);
156 data
->last_temp
= *temp
;
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
);
168 static int imx_get_mode(struct thermal_zone_device
*tz
,
169 enum thermal_device_mode
*mode
)
171 struct imx_thermal_data
*data
= tz
->devdata
;
178 static int imx_set_mode(struct thermal_zone_device
*tz
,
179 enum thermal_device_mode mode
)
181 struct imx_thermal_data
*data
= tz
->devdata
;
182 struct regmap
*map
= data
->tempmon
;
184 if (mode
== THERMAL_DEVICE_ENABLED
) {
185 tz
->polling_delay
= IMX_POLLING_DELAY
;
186 tz
->passive_delay
= IMX_PASSIVE_DELAY
;
188 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_POWER_DOWN
);
189 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_MEASURE_TEMP
);
191 if (!data
->irq_enabled
) {
192 data
->irq_enabled
= true;
193 enable_irq(data
->irq
);
196 regmap_write(map
, TEMPSENSE0
+ REG_CLR
, TEMPSENSE0_MEASURE_TEMP
);
197 regmap_write(map
, TEMPSENSE0
+ REG_SET
, TEMPSENSE0_POWER_DOWN
);
199 tz
->polling_delay
= 0;
200 tz
->passive_delay
= 0;
202 if (data
->irq_enabled
) {
203 disable_irq(data
->irq
);
204 data
->irq_enabled
= false;
209 thermal_zone_device_update(tz
);
214 static int imx_get_trip_type(struct thermal_zone_device
*tz
, int trip
,
215 enum thermal_trip_type
*type
)
217 *type
= (trip
== IMX_TRIP_PASSIVE
) ? THERMAL_TRIP_PASSIVE
:
218 THERMAL_TRIP_CRITICAL
;
222 static int imx_get_crit_temp(struct thermal_zone_device
*tz
,
225 struct imx_thermal_data
*data
= tz
->devdata
;
227 *temp
= data
->temp_critical
;
231 static int imx_get_trip_temp(struct thermal_zone_device
*tz
, int trip
,
234 struct imx_thermal_data
*data
= tz
->devdata
;
236 *temp
= (trip
== IMX_TRIP_PASSIVE
) ? data
->temp_passive
:
241 static int imx_set_trip_temp(struct thermal_zone_device
*tz
, int trip
,
244 struct imx_thermal_data
*data
= tz
->devdata
;
246 if (trip
== IMX_TRIP_CRITICAL
)
249 if (temp
> IMX_TEMP_PASSIVE
)
252 data
->temp_passive
= temp
;
254 imx_set_alarm_temp(data
, temp
);
259 static int imx_bind(struct thermal_zone_device
*tz
,
260 struct thermal_cooling_device
*cdev
)
264 ret
= thermal_zone_bind_cooling_device(tz
, IMX_TRIP_PASSIVE
, cdev
,
269 "binding zone %s with cdev %s failed:%d\n",
270 tz
->type
, cdev
->type
, ret
);
277 static int imx_unbind(struct thermal_zone_device
*tz
,
278 struct thermal_cooling_device
*cdev
)
282 ret
= thermal_zone_unbind_cooling_device(tz
, IMX_TRIP_PASSIVE
, cdev
);
285 "unbinding zone %s with cdev %s failed:%d\n",
286 tz
->type
, cdev
->type
, ret
);
293 static struct thermal_zone_device_ops imx_tz_ops
= {
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
,
305 static int imx_get_sensor_data(struct platform_device
*pdev
)
307 struct imx_thermal_data
*data
= platform_get_drvdata(pdev
);
314 map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
318 dev_err(&pdev
->dev
, "failed to get sensor regmap: %d\n", ret
);
322 ret
= regmap_read(map
, OCOTP_ANA1
, &val
);
324 dev_err(&pdev
->dev
, "failed to read sensor data: %d\n", ret
);
328 if (val
== 0 || val
== ~0) {
329 dev_err(&pdev
->dev
, "invalid sensor calibration data\n");
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)
342 n2
= (val
& 0xfff00) >> 8;
344 t1
= 25; /* t1 always 25C */
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
364 do_div(temp64
, FACTOR1
* n1
- FACTOR2
);
366 data
->c2
= n1
* data
->c1
+ 1000 * t1
;
369 * Set the default passive cooling trip point to 20 °C below the
370 * maximum die temperature. Can be changed from userspace.
372 data
->temp_passive
= 1000 * (t2
- 20);
375 * The maximum die temperature is t2, let's give 5 °C cushion
376 * for noise and possible temperature rise between measurements.
378 data
->temp_critical
= 1000 * (t2
- 5);
383 static irqreturn_t
imx_thermal_alarm_irq(int irq
, void *dev
)
385 struct imx_thermal_data
*data
= dev
;
387 disable_irq_nosync(irq
);
388 data
->irq_enabled
= false;
390 return IRQ_WAKE_THREAD
;
393 static irqreturn_t
imx_thermal_alarm_irq_thread(int irq
, void *dev
)
395 struct imx_thermal_data
*data
= dev
;
397 dev_dbg(&data
->tz
->device
, "THERMAL ALARM: T > %lu\n",
398 data
->alarm_temp
/ 1000);
400 thermal_zone_device_update(data
->tz
);
405 static int imx_thermal_probe(struct platform_device
*pdev
)
407 struct imx_thermal_data
*data
;
408 struct cpumask clip_cpus
;
413 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
417 map
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
, "fsl,tempmon");
420 dev_err(&pdev
->dev
, "failed to get tempmon regmap: %d\n", ret
);
425 data
->irq
= platform_get_irq(pdev
, 0);
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
);
433 dev_err(&pdev
->dev
, "failed to request alarm irq: %d\n", ret
);
437 platform_set_drvdata(pdev
, data
);
439 ret
= imx_get_sensor_data(pdev
);
441 dev_err(&pdev
->dev
, "failed to get sensor data\n");
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
);
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
);
457 "failed to register cpufreq cooling device: %d\n", ret
);
461 data
->tz
= thermal_zone_device_register("imx_thermal_zone",
463 BIT(IMX_TRIP_PASSIVE
), data
,
467 if (IS_ERR(data
->tz
)) {
468 ret
= PTR_ERR(data
->tz
);
470 "failed to register thermal zone device %d\n", ret
);
471 cpufreq_cooling_unregister(data
->cdev
);
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");
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.
486 ret
= clk_prepare_enable(data
->thermal_clk
);
488 dev_warn(&pdev
->dev
, "failed to enable thermal clk: %d\n", ret
);
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
);
499 data
->irq_enabled
= true;
500 data
->mode
= THERMAL_DEVICE_ENABLED
;
505 static int imx_thermal_remove(struct platform_device
*pdev
)
507 struct imx_thermal_data
*data
= platform_get_drvdata(pdev
);
508 struct regmap
*map
= data
->tempmon
;
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
);
515 thermal_zone_device_unregister(data
->tz
);
516 cpufreq_cooling_unregister(data
->cdev
);
521 #ifdef CONFIG_PM_SLEEP
522 static int imx_thermal_suspend(struct device
*dev
)
524 struct imx_thermal_data
*data
= dev_get_drvdata(dev
);
525 struct regmap
*map
= data
->tempmon
;
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
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
;
540 static int imx_thermal_resume(struct device
*dev
)
542 struct imx_thermal_data
*data
= dev_get_drvdata(dev
);
543 struct regmap
*map
= data
->tempmon
;
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
;
554 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops
,
555 imx_thermal_suspend
, imx_thermal_resume
);
557 static const struct of_device_id of_imx_thermal_match
[] = {
558 { .compatible
= "fsl,imx6q-tempmon", },
561 MODULE_DEVICE_TABLE(of
, of_imx_thermal_match
);
563 static struct platform_driver imx_thermal
= {
565 .name
= "imx_thermal",
566 .owner
= THIS_MODULE
,
567 .pm
= &imx_thermal_pm_ops
,
568 .of_match_table
= of_imx_thermal_match
,
570 .probe
= imx_thermal_probe
,
571 .remove
= imx_thermal_remove
,
573 module_platform_driver(imx_thermal
);
575 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
576 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
577 MODULE_LICENSE("GPL v2");
578 MODULE_ALIAS("platform:imx-thermal");