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>
28 #include <linux/nvmem-consumer.h>
35 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
37 #define MISC1_IRQ_TEMPHIGH (1 << 29)
38 /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
39 #define MISC1_IRQ_TEMPLOW (1 << 28)
40 #define MISC1_IRQ_TEMPPANIC (1 << 27)
42 #define TEMPSENSE0 0x0180
43 #define TEMPSENSE0_ALARM_VALUE_SHIFT 20
44 #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
45 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
46 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
47 #define TEMPSENSE0_FINISHED (1 << 2)
48 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
49 #define TEMPSENSE0_POWER_DOWN (1 << 0)
51 #define TEMPSENSE1 0x0190
52 #define TEMPSENSE1_MEASURE_FREQ 0xffff
53 /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
54 #define TEMPSENSE2 0x0290
55 #define TEMPSENSE2_LOW_VALUE_SHIFT 0
56 #define TEMPSENSE2_LOW_VALUE_MASK 0xfff
57 #define TEMPSENSE2_PANIC_VALUE_SHIFT 16
58 #define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
60 #define OCOTP_MEM0 0x0480
61 #define OCOTP_ANA1 0x04e0
63 /* The driver supports 1 passive trip point and 1 critical trip point */
64 enum imx_thermal_trip {
70 #define IMX_POLLING_DELAY 2000 /* millisecond */
71 #define IMX_PASSIVE_DELAY 1000
73 #define TEMPMON_IMX6Q 1
74 #define TEMPMON_IMX6SX 2
76 struct thermal_soc_data {
80 static struct thermal_soc_data thermal_imx6q_data = {
81 .version = TEMPMON_IMX6Q,
84 static struct thermal_soc_data thermal_imx6sx_data = {
85 .version = TEMPMON_IMX6SX,
88 struct imx_thermal_data {
89 struct cpufreq_policy *policy;
90 struct thermal_zone_device *tz;
91 struct thermal_cooling_device *cdev;
92 enum thermal_device_mode mode;
93 struct regmap *tempmon;
94 u32 c1, c2; /* See formula in imx_init_calib() */
102 struct clk *thermal_clk;
103 const struct thermal_soc_data *socdata;
104 const char *temp_grade;
107 static void imx_set_panic_temp(struct imx_thermal_data *data,
110 struct regmap *map = data->tempmon;
113 critical_value = (data->c2 - panic_temp) / data->c1;
114 regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK);
115 regmap_write(map, TEMPSENSE2 + REG_SET, critical_value <<
116 TEMPSENSE2_PANIC_VALUE_SHIFT);
119 static void imx_set_alarm_temp(struct imx_thermal_data *data,
122 struct regmap *map = data->tempmon;
125 data->alarm_temp = alarm_temp;
126 alarm_value = (data->c2 - alarm_temp) / data->c1;
127 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
128 regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
129 TEMPSENSE0_ALARM_VALUE_SHIFT);
132 static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
134 struct imx_thermal_data *data = tz->devdata;
135 struct regmap *map = data->tempmon;
140 if (data->mode == THERMAL_DEVICE_ENABLED) {
141 /* Check if a measurement is currently in progress */
142 regmap_read(map, TEMPSENSE0, &val);
143 wait = !(val & TEMPSENSE0_FINISHED);
146 * Every time we measure the temperature, we will power on the
147 * temperature sensor, enable measurements, take a reading,
148 * disable measurements, power off the temperature sensor.
150 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
151 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
157 * According to the temp sensor designers, it may require up to ~17us
158 * to complete a measurement.
161 usleep_range(20, 50);
163 regmap_read(map, TEMPSENSE0, &val);
165 if (data->mode != THERMAL_DEVICE_ENABLED) {
166 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
167 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
170 if ((val & TEMPSENSE0_FINISHED) == 0) {
171 dev_dbg(&tz->device, "temp measurement never finished\n");
175 n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
177 /* See imx_init_calib() for formula derivation */
178 *temp = data->c2 - n_meas * data->c1;
180 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
181 if (data->socdata->version == TEMPMON_IMX6Q) {
182 if (data->alarm_temp == data->temp_passive &&
183 *temp >= data->temp_passive)
184 imx_set_alarm_temp(data, data->temp_critical);
185 if (data->alarm_temp == data->temp_critical &&
186 *temp < data->temp_passive) {
187 imx_set_alarm_temp(data, data->temp_passive);
188 dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
189 data->alarm_temp / 1000);
193 if (*temp != data->last_temp) {
194 dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
195 data->last_temp = *temp;
198 /* Reenable alarm IRQ if temperature below alarm temperature */
199 if (!data->irq_enabled && *temp < data->alarm_temp) {
200 data->irq_enabled = true;
201 enable_irq(data->irq);
207 static int imx_get_mode(struct thermal_zone_device *tz,
208 enum thermal_device_mode *mode)
210 struct imx_thermal_data *data = tz->devdata;
217 static int imx_set_mode(struct thermal_zone_device *tz,
218 enum thermal_device_mode mode)
220 struct imx_thermal_data *data = tz->devdata;
221 struct regmap *map = data->tempmon;
223 if (mode == THERMAL_DEVICE_ENABLED) {
224 tz->polling_delay = IMX_POLLING_DELAY;
225 tz->passive_delay = IMX_PASSIVE_DELAY;
227 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
228 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
230 if (!data->irq_enabled) {
231 data->irq_enabled = true;
232 enable_irq(data->irq);
235 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
236 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
238 tz->polling_delay = 0;
239 tz->passive_delay = 0;
241 if (data->irq_enabled) {
242 disable_irq(data->irq);
243 data->irq_enabled = false;
248 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
253 static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
254 enum thermal_trip_type *type)
256 *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
257 THERMAL_TRIP_CRITICAL;
261 static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
263 struct imx_thermal_data *data = tz->devdata;
265 *temp = data->temp_critical;
269 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
272 struct imx_thermal_data *data = tz->devdata;
274 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
279 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
282 struct imx_thermal_data *data = tz->devdata;
284 /* do not allow changing critical threshold */
285 if (trip == IMX_TRIP_CRITICAL)
288 /* do not allow passive to be set higher than critical */
289 if (temp < 0 || temp > data->temp_critical)
292 data->temp_passive = temp;
294 imx_set_alarm_temp(data, temp);
299 static int imx_bind(struct thermal_zone_device *tz,
300 struct thermal_cooling_device *cdev)
304 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
307 THERMAL_WEIGHT_DEFAULT);
310 "binding zone %s with cdev %s failed:%d\n",
311 tz->type, cdev->type, ret);
318 static int imx_unbind(struct thermal_zone_device *tz,
319 struct thermal_cooling_device *cdev)
323 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
326 "unbinding zone %s with cdev %s failed:%d\n",
327 tz->type, cdev->type, ret);
334 static struct thermal_zone_device_ops imx_tz_ops = {
336 .unbind = imx_unbind,
337 .get_temp = imx_get_temp,
338 .get_mode = imx_get_mode,
339 .set_mode = imx_set_mode,
340 .get_trip_type = imx_get_trip_type,
341 .get_trip_temp = imx_get_trip_temp,
342 .get_crit_temp = imx_get_crit_temp,
343 .set_trip_temp = imx_set_trip_temp,
346 static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
348 struct imx_thermal_data *data = platform_get_drvdata(pdev);
352 if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
353 dev_err(&pdev->dev, "invalid sensor calibration data\n");
358 * The sensor is calibrated at 25 °C (aka T1) and the value measured
359 * (aka N1) at this temperature is provided in bits [31:20] in the
360 * i.MX's OCOTP value ANA1.
361 * To find the actual temperature T, the following formula has to be used
362 * when reading value n from the sensor:
364 * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
365 * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
366 * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
371 * T1' = 28.580661 °C
372 * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
373 * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
376 n1 = ocotp_ana1 >> 20;
378 temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
379 temp64 *= 1000; /* to get result in °mC */
380 do_div(temp64, 15423 * n1 - 4148468);
382 data->c2 = n1 * data->c1 + 28581;
387 static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
389 struct imx_thermal_data *data = platform_get_drvdata(pdev);
391 /* The maximum die temp is specified by the Temperature Grade */
392 switch ((ocotp_mem0 >> 6) & 0x3) {
393 case 0: /* Commercial (0 to 95 °C) */
394 data->temp_grade = "Commercial";
395 data->temp_max = 95000;
397 case 1: /* Extended Commercial (-20 °C to 105 °C) */
398 data->temp_grade = "Extended Commercial";
399 data->temp_max = 105000;
401 case 2: /* Industrial (-40 °C to 105 °C) */
402 data->temp_grade = "Industrial";
403 data->temp_max = 105000;
405 case 3: /* Automotive (-40 °C to 125 °C) */
406 data->temp_grade = "Automotive";
407 data->temp_max = 125000;
412 * Set the critical trip point at 5 °C under max
413 * Set the passive trip point at 10 °C under max (changeable via sysfs)
415 data->temp_critical = data->temp_max - (1000 * 5);
416 data->temp_passive = data->temp_max - (1000 * 10);
419 static int imx_init_from_tempmon_data(struct platform_device *pdev)
425 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
429 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
433 ret = regmap_read(map, OCOTP_ANA1, &val);
435 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
438 ret = imx_init_calib(pdev, val);
442 ret = regmap_read(map, OCOTP_MEM0, &val);
444 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
447 imx_init_temp_grade(pdev, val);
452 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
457 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
460 imx_init_calib(pdev, val);
462 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
465 imx_init_temp_grade(pdev, val);
470 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
472 struct imx_thermal_data *data = dev;
474 disable_irq_nosync(irq);
475 data->irq_enabled = false;
477 return IRQ_WAKE_THREAD;
480 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
482 struct imx_thermal_data *data = dev;
484 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
485 data->alarm_temp / 1000);
487 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
492 static const struct of_device_id of_imx_thermal_match[] = {
493 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
494 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
497 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
499 static int imx_thermal_probe(struct platform_device *pdev)
501 struct imx_thermal_data *data;
506 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
510 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
513 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
518 data->socdata = of_device_get_match_data(&pdev->dev);
519 if (!data->socdata) {
520 dev_err(&pdev->dev, "no device match found\n");
524 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
525 if (data->socdata->version == TEMPMON_IMX6SX) {
526 regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
527 MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
529 * reset value of LOW ALARM is incorrect, set it to lowest
530 * value to avoid false trigger of low alarm.
532 regmap_write(map, TEMPSENSE2 + REG_SET,
533 TEMPSENSE2_LOW_VALUE_MASK);
536 data->irq = platform_get_irq(pdev, 0);
540 platform_set_drvdata(pdev, data);
542 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
543 ret = imx_init_from_nvmem_cells(pdev);
544 if (ret == -EPROBE_DEFER)
547 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
552 ret = imx_init_from_tempmon_data(pdev);
554 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
559 /* Make sure sensor is in known good state for measurements */
560 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
561 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
562 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
563 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
564 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
566 data->policy = cpufreq_cpu_get(0);
568 pr_debug("%s: CPUFreq policy not found\n", __func__);
569 return -EPROBE_DEFER;
572 data->cdev = cpufreq_cooling_register(data->policy);
573 if (IS_ERR(data->cdev)) {
574 ret = PTR_ERR(data->cdev);
576 "failed to register cpufreq cooling device: %d\n", ret);
577 cpufreq_cpu_put(data->policy);
581 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
582 if (IS_ERR(data->thermal_clk)) {
583 ret = PTR_ERR(data->thermal_clk);
584 if (ret != -EPROBE_DEFER)
586 "failed to get thermal clk: %d\n", ret);
587 cpufreq_cooling_unregister(data->cdev);
588 cpufreq_cpu_put(data->policy);
593 * Thermal sensor needs clk on to get correct value, normally
594 * we should enable its clk before taking measurement and disable
595 * clk after measurement is done, but if alarm function is enabled,
596 * hardware will auto measure the temperature periodically, so we
597 * need to keep the clk always on for alarm function.
599 ret = clk_prepare_enable(data->thermal_clk);
601 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
602 cpufreq_cooling_unregister(data->cdev);
603 cpufreq_cpu_put(data->policy);
607 data->tz = thermal_zone_device_register("imx_thermal_zone",
609 BIT(IMX_TRIP_PASSIVE), data,
613 if (IS_ERR(data->tz)) {
614 ret = PTR_ERR(data->tz);
616 "failed to register thermal zone device %d\n", ret);
617 clk_disable_unprepare(data->thermal_clk);
618 cpufreq_cooling_unregister(data->cdev);
619 cpufreq_cpu_put(data->policy);
623 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
624 " critical:%dC passive:%dC\n", data->temp_grade,
625 data->temp_max / 1000, data->temp_critical / 1000,
626 data->temp_passive / 1000);
628 /* Enable measurements at ~ 10 Hz */
629 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
630 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
631 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
632 imx_set_alarm_temp(data, data->temp_passive);
634 if (data->socdata->version == TEMPMON_IMX6SX)
635 imx_set_panic_temp(data, data->temp_critical);
637 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
638 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
640 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
641 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
642 0, "imx_thermal", data);
644 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
645 clk_disable_unprepare(data->thermal_clk);
646 thermal_zone_device_unregister(data->tz);
647 cpufreq_cooling_unregister(data->cdev);
648 cpufreq_cpu_put(data->policy);
652 data->irq_enabled = true;
653 data->mode = THERMAL_DEVICE_ENABLED;
658 static int imx_thermal_remove(struct platform_device *pdev)
660 struct imx_thermal_data *data = platform_get_drvdata(pdev);
661 struct regmap *map = data->tempmon;
663 /* Disable measurements */
664 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
665 if (!IS_ERR(data->thermal_clk))
666 clk_disable_unprepare(data->thermal_clk);
668 thermal_zone_device_unregister(data->tz);
669 cpufreq_cooling_unregister(data->cdev);
670 cpufreq_cpu_put(data->policy);
675 #ifdef CONFIG_PM_SLEEP
676 static int imx_thermal_suspend(struct device *dev)
678 struct imx_thermal_data *data = dev_get_drvdata(dev);
679 struct regmap *map = data->tempmon;
682 * Need to disable thermal sensor, otherwise, when thermal core
683 * try to get temperature before thermal sensor resume, a wrong
684 * temperature will be read as the thermal sensor is powered
687 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
688 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
689 data->mode = THERMAL_DEVICE_DISABLED;
690 clk_disable_unprepare(data->thermal_clk);
695 static int imx_thermal_resume(struct device *dev)
697 struct imx_thermal_data *data = dev_get_drvdata(dev);
698 struct regmap *map = data->tempmon;
701 ret = clk_prepare_enable(data->thermal_clk);
704 /* Enabled thermal sensor after resume */
705 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
706 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
707 data->mode = THERMAL_DEVICE_ENABLED;
713 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
714 imx_thermal_suspend, imx_thermal_resume);
716 static struct platform_driver imx_thermal = {
718 .name = "imx_thermal",
719 .pm = &imx_thermal_pm_ops,
720 .of_match_table = of_imx_thermal_match,
722 .probe = imx_thermal_probe,
723 .remove = imx_thermal_remove,
725 module_platform_driver(imx_thermal);
727 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
728 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
729 MODULE_LICENSE("GPL v2");
730 MODULE_ALIAS("platform:imx-thermal");