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 * Use universal formula now and only need sensor value @ 25C
364 * slope = 0.4297157 - (0.0015976 * 25C fuse)
367 t1 = 25; /* t1 always 25C */
370 * Derived from linear interpolation:
371 * slope = 0.4297157 - (0.0015976 * 25C fuse)
372 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
373 * (Nmeas - n1) / (Tmeas - t1) = slope
374 * We want to reduce this down to the minimum computation necessary
375 * for each temperature read. Also, we want Tmeas in millicelsius
376 * and we don't want to lose precision from integer division. So...
377 * Tmeas = (Nmeas - n1) / slope + t1
378 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
379 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
380 * Let constant c1 = (-1000 / slope)
381 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
382 * Let constant c2 = n1 *c1 + 1000 * t1
383 * milli_Tmeas = c2 - Nmeas * c1
387 do_div(temp64, FACTOR1 * n1 - FACTOR2);
389 data->c2 = n1 * data->c1 + 1000 * t1;
392 * Set the default passive cooling trip point,
393 * can be changed from userspace.
395 data->temp_passive = IMX_TEMP_PASSIVE;
398 * The maximum die temperature set to 20 C higher than
401 data->temp_critical = 1000 * 20 + data->temp_passive;
406 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
408 struct imx_thermal_data *data = dev;
410 disable_irq_nosync(irq);
411 data->irq_enabled = false;
413 return IRQ_WAKE_THREAD;
416 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
418 struct imx_thermal_data *data = dev;
420 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %lu\n",
421 data->alarm_temp / 1000);
423 thermal_zone_device_update(data->tz);
428 static int imx_thermal_probe(struct platform_device *pdev)
430 struct imx_thermal_data *data;
431 struct cpumask clip_cpus;
436 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
440 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
443 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
448 data->irq = platform_get_irq(pdev, 0);
452 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
453 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
454 0, "imx_thermal", data);
456 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
460 platform_set_drvdata(pdev, data);
462 ret = imx_get_sensor_data(pdev);
464 dev_err(&pdev->dev, "failed to get sensor data\n");
468 /* Make sure sensor is in known good state for measurements */
469 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
470 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
471 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
472 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
473 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
475 cpumask_set_cpu(0, &clip_cpus);
476 data->cdev[0] = cpufreq_cooling_register(&clip_cpus);
477 if (IS_ERR(data->cdev[0])) {
478 ret = PTR_ERR(data->cdev[0]);
480 "failed to register cpufreq cooling device: %d\n", ret);
484 data->cdev[1] = devfreq_cooling_register();
485 if (IS_ERR(data->cdev[1])) {
486 ret = PTR_ERR(data->cdev[1]);
488 "failed to register devfreq cooling device: %d\n", ret);
492 data->temp_passive = IMX_TEMP_PASSIVE;
493 data->temp_critical = IMX_TEMP_CRITICAL;
494 data->tz = thermal_zone_device_register("imx_thermal_zone",
496 BIT(IMX_TRIP_PASSIVE), data,
500 if (IS_ERR(data->tz)) {
501 ret = PTR_ERR(data->tz);
503 "failed to register thermal zone device %d\n", ret);
504 cpufreq_cooling_unregister(data->cdev[0]);
505 devfreq_cooling_unregister(data->cdev[1]);
509 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
510 if (IS_ERR(data->thermal_clk)) {
511 dev_warn(&pdev->dev, "failed to get thermal clk!\n");
514 * Thermal sensor needs clk on to get correct value, normally
515 * we should enable its clk before taking measurement and disable
516 * clk after measurement is done, but if alarm function is enabled,
517 * hardware will auto measure the temperature periodically, so we
518 * need to keep the clk always on for alarm function.
520 ret = clk_prepare_enable(data->thermal_clk);
522 dev_warn(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
525 /* Enable measurements at ~ 10 Hz */
526 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
527 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
528 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
529 imx_set_alarm_temp(data, data->temp_passive);
530 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
531 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
533 data->irq_enabled = true;
534 data->mode = THERMAL_DEVICE_ENABLED;
539 static int imx_thermal_remove(struct platform_device *pdev)
541 struct imx_thermal_data *data = platform_get_drvdata(pdev);
542 struct regmap *map = data->tempmon;
544 /* Disable measurements */
545 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
546 if (!IS_ERR(data->thermal_clk))
547 clk_disable_unprepare(data->thermal_clk);
549 thermal_zone_device_unregister(data->tz);
550 cpufreq_cooling_unregister(data->cdev[0]);
551 devfreq_cooling_unregister(data->cdev[1]);
556 #ifdef CONFIG_PM_SLEEP
557 static int imx_thermal_suspend(struct device *dev)
559 struct imx_thermal_data *data = dev_get_drvdata(dev);
560 struct regmap *map = data->tempmon;
563 * Need to disable thermal sensor, otherwise, when thermal core
564 * try to get temperature before thermal sensor resume, a wrong
565 * temperature will be read as the thermal sensor is powered
568 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
569 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
570 data->mode = THERMAL_DEVICE_DISABLED;
575 static int imx_thermal_resume(struct device *dev)
577 struct imx_thermal_data *data = dev_get_drvdata(dev);
578 struct regmap *map = data->tempmon;
580 /* Enabled thermal sensor after resume */
581 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
582 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
583 data->mode = THERMAL_DEVICE_ENABLED;
589 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
590 imx_thermal_suspend, imx_thermal_resume);
592 static const struct of_device_id of_imx_thermal_match[] = {
593 { .compatible = "fsl,imx6q-tempmon", },
596 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
598 static struct platform_driver imx_thermal = {
600 .name = "imx_thermal",
601 .owner = THIS_MODULE,
602 .pm = &imx_thermal_pm_ops,
603 .of_match_table = of_imx_thermal_match,
605 .probe = imx_thermal_probe,
606 .remove = imx_thermal_remove,
608 module_platform_driver(imx_thermal);
610 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
611 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
612 MODULE_LICENSE("GPL v2");
613 MODULE_ALIAS("platform:imx-thermal");