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/device_cooling.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/module.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 TEMPSENSE0 0x0180
37 #define TEMPSENSE0_ALARM_VALUE_SHIFT 20
38 #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
39 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
40 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
41 #define TEMPSENSE0_FINISHED (1 << 2)
42 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
43 #define TEMPSENSE0_POWER_DOWN (1 << 0)
45 #define TEMPSENSE1 0x0190
46 #define TEMPSENSE1_MEASURE_FREQ 0xffff
48 #define OCOTP_ANA1 0x04e0
50 /* The driver supports 1 passive trip point and 1 critical trip point */
51 enum imx_thermal_trip {
58 * It defines the temperature in millicelsius for passive trip point
59 * that will trigger cooling action when crossed.
61 #define IMX_TEMP_PASSIVE 85000
62 #define IMX_TEMP_PASSIVE_COOL_DELTA 10000
65 * The maximum die temperature on imx parts is 105C, let's give some cushion
66 * for noise and possible temperature rise between measurements.
68 #define IMX_TEMP_CRITICAL 100000
70 #define IMX_POLLING_DELAY 2000 /* millisecond */
71 #define IMX_PASSIVE_DELAY 1000
73 #define FACTOR0 10000000
75 #define FACTOR2 4297157
77 struct imx_thermal_data {
78 struct thermal_zone_device *tz;
79 struct thermal_cooling_device *cdev[2];
80 enum thermal_device_mode mode;
81 struct regmap *tempmon;
82 u32 c1, c2; /* See formula in imx_get_sensor_data() */
83 unsigned long temp_passive;
84 unsigned long temp_critical;
85 unsigned long alarm_temp;
86 unsigned long last_temp;
89 struct clk *thermal_clk;
92 static void imx_set_alarm_temp(struct imx_thermal_data *data,
93 signed long alarm_temp)
95 struct regmap *map = data->tempmon;
98 data->alarm_temp = alarm_temp;
99 alarm_value = (data->c2 - alarm_temp) / data->c1;
100 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
101 regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
102 TEMPSENSE0_ALARM_VALUE_SHIFT);
105 static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
107 struct imx_thermal_data *data = tz->devdata;
108 struct regmap *map = data->tempmon;
113 if (data->mode == THERMAL_DEVICE_ENABLED) {
114 /* Check if a measurement is currently in progress */
115 regmap_read(map, TEMPSENSE0, &val);
116 wait = !(val & TEMPSENSE0_FINISHED);
119 * Every time we measure the temperature, we will power on the
120 * temperature sensor, enable measurements, take a reading,
121 * disable measurements, power off the temperature sensor.
123 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
124 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
130 * According to the temp sensor designers, it may require up to ~17us
131 * to complete a measurement.
134 usleep_range(20, 50);
136 regmap_read(map, TEMPSENSE0, &val);
138 if (data->mode != THERMAL_DEVICE_ENABLED) {
139 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
140 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
143 if ((val & TEMPSENSE0_FINISHED) == 0) {
144 dev_dbg(&tz->device, "temp measurement never finished\n");
148 n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
150 /* See imx_get_sensor_data() for formula derivation */
151 *temp = data->c2 - n_meas * data->c1;
153 /* Update alarm value to next higher trip point */
154 if (data->alarm_temp == data->temp_passive && *temp >= data->temp_passive)
155 imx_set_alarm_temp(data, data->temp_critical);
156 if (data->alarm_temp == data->temp_critical && *temp < data->temp_passive) {
157 imx_set_alarm_temp(data, data->temp_passive);
158 dev_dbg(&tz->device, "thermal alarm off: T < %lu\n",
159 data->alarm_temp / 1000);
162 if (*temp != data->last_temp) {
163 dev_dbg(&tz->device, "millicelsius: %ld\n", *temp);
164 data->last_temp = *temp;
167 /* Reenable alarm IRQ if temperature below alarm temperature */
168 if (!data->irq_enabled && *temp < data->alarm_temp) {
169 data->irq_enabled = true;
170 enable_irq(data->irq);
176 static int imx_get_mode(struct thermal_zone_device *tz,
177 enum thermal_device_mode *mode)
179 struct imx_thermal_data *data = tz->devdata;
186 static int imx_set_mode(struct thermal_zone_device *tz,
187 enum thermal_device_mode mode)
189 struct imx_thermal_data *data = tz->devdata;
190 struct regmap *map = data->tempmon;
192 if (mode == THERMAL_DEVICE_ENABLED) {
193 tz->polling_delay = IMX_POLLING_DELAY;
194 tz->passive_delay = IMX_PASSIVE_DELAY;
196 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
197 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
199 if (!data->irq_enabled) {
200 data->irq_enabled = true;
201 enable_irq(data->irq);
204 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
205 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
207 tz->polling_delay = 0;
208 tz->passive_delay = 0;
210 if (data->irq_enabled) {
211 disable_irq(data->irq);
212 data->irq_enabled = false;
217 thermal_zone_device_update(tz);
222 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
223 enum thermal_trip_type *type)
225 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
226 THERMAL_TRIP_CRITICAL;
230 static int imx_get_crit_temp(struct thermal_zone_device *tz,
233 struct imx_thermal_data *data = tz->devdata;
235 *temp = data->temp_critical;
239 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
242 struct imx_thermal_data *data = tz->devdata;
244 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
249 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
252 struct imx_thermal_data *data = tz->devdata;
254 if (trip == IMX_TRIP_CRITICAL)
257 if (temp > IMX_TEMP_PASSIVE)
260 data->temp_passive = temp;
262 imx_set_alarm_temp(data, temp);
267 static int imx_bind(struct thermal_zone_device *tz,
268 struct thermal_cooling_device *cdev)
272 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
277 "binding zone %s with cdev %s failed:%d\n",
278 tz->type, cdev->type, ret);
285 static int imx_unbind(struct thermal_zone_device *tz,
286 struct thermal_cooling_device *cdev)
290 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
293 "unbinding zone %s with cdev %s failed:%d\n",
294 tz->type, cdev->type, ret);
301 static int imx_get_trend(struct thermal_zone_device *tz,
302 int trip, enum thermal_trend *trend)
305 unsigned long trip_temp;
307 ret = imx_get_trip_temp(tz, trip, &trip_temp);
311 if (tz->temperature >= (trip_temp - IMX_TEMP_PASSIVE_COOL_DELTA))
312 *trend = THERMAL_TREND_RAISE_FULL;
314 *trend = THERMAL_TREND_DROP_FULL;
319 static struct thermal_zone_device_ops imx_tz_ops = {
321 .unbind = imx_unbind,
322 .get_temp = imx_get_temp,
323 .get_mode = imx_get_mode,
324 .set_mode = imx_set_mode,
325 .get_trip_type = imx_get_trip_type,
326 .get_trip_temp = imx_get_trip_temp,
327 .get_crit_temp = imx_get_crit_temp,
328 .get_trend = imx_get_trend,
329 .set_trip_temp = imx_set_trip_temp,
332 static int imx_get_sensor_data(struct platform_device *pdev)
334 struct imx_thermal_data *data = platform_get_drvdata(pdev);
341 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
345 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
349 ret = regmap_read(map, OCOTP_ANA1, &val);
351 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
355 if (val == 0 || val == ~0) {
356 dev_err(&pdev->dev, "invalid sensor calibration data\n");
361 * Sensor data layout:
362 * [31:20] - sensor value @ 25C
363 * [19:8] - sensor value of hot
364 * [7:0] - hot temperature value
365 * Use universal formula now and only need sensor value @ 25C
366 * slope = 0.4297157 - (0.0015976 * 25C fuse)
369 n2 = (val & 0xfff00) >> 8;
371 t1 = 25; /* t1 always 25C */
374 * Derived from linear interpolation:
375 * slope = 0.4297157 - (0.0015976 * 25C fuse)
376 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
377 * (Nmeas - n1) / (Tmeas - t1) = slope
378 * We want to reduce this down to the minimum computation necessary
379 * for each temperature read. Also, we want Tmeas in millicelsius
380 * and we don't want to lose precision from integer division. So...
381 * Tmeas = (Nmeas - n1) / slope + t1
382 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
383 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
384 * Let constant c1 = (-1000 / slope)
385 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
386 * Let constant c2 = n1 *c1 + 1000 * t1
387 * milli_Tmeas = c2 - Nmeas * c1
391 do_div(temp64, FACTOR1 * n1 - FACTOR2);
393 data->c2 = n1 * data->c1 + 1000 * t1;
396 * Set the default passive cooling trip point to 20 °C below the
397 * maximum die temperature. Can be changed from userspace.
399 data->temp_passive = 1000 * (t2 - 20);
402 * The maximum die temperature is t2, let's give 5 °C cushion
403 * for noise and possible temperature rise between measurements.
405 data->temp_critical = 1000 * (t2 - 5);
410 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
412 struct imx_thermal_data *data = dev;
414 disable_irq_nosync(irq);
415 data->irq_enabled = false;
417 return IRQ_WAKE_THREAD;
420 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
422 struct imx_thermal_data *data = dev;
424 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %lu\n",
425 data->alarm_temp / 1000);
427 thermal_zone_device_update(data->tz);
432 static int imx_thermal_probe(struct platform_device *pdev)
434 struct imx_thermal_data *data;
435 struct cpumask clip_cpus;
440 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
444 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
447 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
452 data->irq = platform_get_irq(pdev, 0);
456 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
457 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
458 0, "imx_thermal", data);
460 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
464 platform_set_drvdata(pdev, data);
466 ret = imx_get_sensor_data(pdev);
468 dev_err(&pdev->dev, "failed to get sensor data\n");
472 /* Make sure sensor is in known good state for measurements */
473 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
474 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
475 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
476 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
477 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
479 cpumask_set_cpu(0, &clip_cpus);
480 data->cdev[0] = cpufreq_cooling_register(&clip_cpus);
481 if (IS_ERR(data->cdev[0])) {
482 ret = PTR_ERR(data->cdev[0]);
484 "failed to register cpufreq cooling device: %d\n", ret);
488 data->cdev[1] = devfreq_cooling_register();
489 if (IS_ERR(data->cdev[1])) {
490 ret = PTR_ERR(data->cdev[1]);
492 "failed to register devfreq cooling device: %d\n", ret);
496 data->temp_passive = IMX_TEMP_PASSIVE;
497 data->temp_critical = IMX_TEMP_CRITICAL;
498 data->tz = thermal_zone_device_register("imx_thermal_zone",
500 BIT(IMX_TRIP_PASSIVE), data,
504 if (IS_ERR(data->tz)) {
505 ret = PTR_ERR(data->tz);
507 "failed to register thermal zone device %d\n", ret);
508 cpufreq_cooling_unregister(data->cdev[0]);
509 devfreq_cooling_unregister(data->cdev[1]);
513 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
514 if (IS_ERR(data->thermal_clk)) {
515 dev_warn(&pdev->dev, "failed to get thermal clk!\n");
518 * Thermal sensor needs clk on to get correct value, normally
519 * we should enable its clk before taking measurement and disable
520 * clk after measurement is done, but if alarm function is enabled,
521 * hardware will auto measure the temperature periodically, so we
522 * need to keep the clk always on for alarm function.
524 ret = clk_prepare_enable(data->thermal_clk);
526 dev_warn(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
529 /* Enable measurements at ~ 10 Hz */
530 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
531 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
532 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
533 imx_set_alarm_temp(data, data->temp_passive);
534 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
535 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
537 data->irq_enabled = true;
538 data->mode = THERMAL_DEVICE_ENABLED;
543 static int imx_thermal_remove(struct platform_device *pdev)
545 struct imx_thermal_data *data = platform_get_drvdata(pdev);
546 struct regmap *map = data->tempmon;
548 /* Disable measurements */
549 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
550 if (!IS_ERR(data->thermal_clk))
551 clk_disable_unprepare(data->thermal_clk);
553 thermal_zone_device_unregister(data->tz);
554 cpufreq_cooling_unregister(data->cdev[0]);
555 devfreq_cooling_unregister(data->cdev[1]);
560 #ifdef CONFIG_PM_SLEEP
561 static int imx_thermal_suspend(struct device *dev)
563 struct imx_thermal_data *data = dev_get_drvdata(dev);
564 struct regmap *map = data->tempmon;
567 * Need to disable thermal sensor, otherwise, when thermal core
568 * try to get temperature before thermal sensor resume, a wrong
569 * temperature will be read as the thermal sensor is powered
572 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
573 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
574 data->mode = THERMAL_DEVICE_DISABLED;
579 static int imx_thermal_resume(struct device *dev)
581 struct imx_thermal_data *data = dev_get_drvdata(dev);
582 struct regmap *map = data->tempmon;
584 /* Enabled thermal sensor after resume */
585 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
586 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
587 data->mode = THERMAL_DEVICE_ENABLED;
593 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
594 imx_thermal_suspend, imx_thermal_resume);
596 static const struct of_device_id of_imx_thermal_match[] = {
597 { .compatible = "fsl,imx6q-tempmon", },
600 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
602 static struct platform_driver imx_thermal = {
604 .name = "imx_thermal",
605 .owner = THIS_MODULE,
606 .pm = &imx_thermal_pm_ops,
607 .of_match_table = of_imx_thermal_match,
609 .probe = imx_thermal_probe,
610 .remove = imx_thermal_remove,
612 module_platform_driver(imx_thermal);
614 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
615 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
616 MODULE_LICENSE("GPL v2");
617 MODULE_ALIAS("platform:imx-thermal");