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 FACTOR0 10000000
75 #define FACTOR2 4297157
77 #define TEMPMON_IMX6Q 1
78 #define TEMPMON_IMX6SX 2
80 struct thermal_soc_data {
84 static struct thermal_soc_data thermal_imx6q_data = {
85 .version = TEMPMON_IMX6Q,
88 static struct thermal_soc_data thermal_imx6sx_data = {
89 .version = TEMPMON_IMX6SX,
92 struct imx_thermal_data {
93 struct cpufreq_policy *policy;
94 struct thermal_zone_device *tz;
95 struct thermal_cooling_device *cdev;
96 enum thermal_device_mode mode;
97 struct regmap *tempmon;
98 u32 c1, c2; /* See formula in imx_init_calib() */
106 struct clk *thermal_clk;
107 const struct thermal_soc_data *socdata;
108 const char *temp_grade;
111 static void imx_set_panic_temp(struct imx_thermal_data *data,
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,
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, int *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_init_calib() 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 < %d\n",
193 data->alarm_temp / 1000);
197 if (*temp != data->last_temp) {
198 dev_dbg(&tz->device, "millicelsius: %d\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, THERMAL_EVENT_UNSPECIFIED);
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, int *temp)
267 struct imx_thermal_data *data = tz->devdata;
269 *temp = data->temp_critical;
273 static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
276 struct imx_thermal_data *data = tz->devdata;
278 *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
283 static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
286 struct imx_thermal_data *data = tz->devdata;
288 /* do not allow changing critical threshold */
289 if (trip == IMX_TRIP_CRITICAL)
292 /* do not allow passive to be set higher than critical */
293 if (temp < 0 || temp > data->temp_critical)
296 data->temp_passive = temp;
298 imx_set_alarm_temp(data, temp);
303 static int imx_bind(struct thermal_zone_device *tz,
304 struct thermal_cooling_device *cdev)
308 ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
311 THERMAL_WEIGHT_DEFAULT);
314 "binding zone %s with cdev %s failed:%d\n",
315 tz->type, cdev->type, ret);
322 static int imx_unbind(struct thermal_zone_device *tz,
323 struct thermal_cooling_device *cdev)
327 ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
330 "unbinding zone %s with cdev %s failed:%d\n",
331 tz->type, cdev->type, ret);
338 static struct thermal_zone_device_ops imx_tz_ops = {
340 .unbind = imx_unbind,
341 .get_temp = imx_get_temp,
342 .get_mode = imx_get_mode,
343 .set_mode = imx_set_mode,
344 .get_trip_type = imx_get_trip_type,
345 .get_trip_temp = imx_get_trip_temp,
346 .get_crit_temp = imx_get_crit_temp,
347 .set_trip_temp = imx_set_trip_temp,
350 static int imx_init_calib(struct platform_device *pdev, u32 val)
352 struct imx_thermal_data *data = platform_get_drvdata(pdev);
356 if (val == 0 || val == ~0) {
357 dev_err(&pdev->dev, "invalid sensor calibration data\n");
362 * Sensor data layout:
363 * [31:20] - sensor value @ 25C
364 * Use universal formula now and only need sensor value @ 25C
365 * slope = 0.4297157 - (0.0015976 * 25C fuse)
368 t1 = 25; /* t1 always 25C */
371 * Derived from linear interpolation:
372 * slope = 0.4297157 - (0.0015976 * 25C fuse)
373 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
374 * (Nmeas - n1) / (Tmeas - t1) = slope
375 * We want to reduce this down to the minimum computation necessary
376 * for each temperature read. Also, we want Tmeas in millicelsius
377 * and we don't want to lose precision from integer division. So...
378 * Tmeas = (Nmeas - n1) / slope + t1
379 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
380 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
381 * Let constant c1 = (-1000 / slope)
382 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
383 * Let constant c2 = n1 *c1 + 1000 * t1
384 * milli_Tmeas = c2 - Nmeas * c1
388 do_div(temp64, FACTOR1 * n1 - FACTOR2);
390 data->c2 = n1 * data->c1 + 1000 * t1;
395 static void imx_init_temp_grade(struct platform_device *pdev, u32 val)
397 struct imx_thermal_data *data = platform_get_drvdata(pdev);
399 /* The maximum die temp is specified by the Temperature Grade */
400 switch ((val >> 6) & 0x3) {
401 case 0: /* Commercial (0 to 95C) */
402 data->temp_grade = "Commercial";
403 data->temp_max = 95000;
405 case 1: /* Extended Commercial (-20 to 105C) */
406 data->temp_grade = "Extended Commercial";
407 data->temp_max = 105000;
409 case 2: /* Industrial (-40 to 105C) */
410 data->temp_grade = "Industrial";
411 data->temp_max = 105000;
413 case 3: /* Automotive (-40 to 125C) */
414 data->temp_grade = "Automotive";
415 data->temp_max = 125000;
420 * Set the critical trip point at 5C under max
421 * Set the passive trip point at 10C under max (can change via sysfs)
423 data->temp_critical = data->temp_max - (1000 * 5);
424 data->temp_passive = data->temp_max - (1000 * 10);
427 static int imx_init_from_tempmon_data(struct platform_device *pdev)
433 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
437 dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
441 ret = regmap_read(map, OCOTP_ANA1, &val);
443 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
446 ret = imx_init_calib(pdev, val);
450 ret = regmap_read(map, OCOTP_MEM0, &val);
452 dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
455 imx_init_temp_grade(pdev, val);
460 static int imx_init_from_nvmem_cells(struct platform_device *pdev)
465 ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
468 imx_init_calib(pdev, val);
470 ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
473 imx_init_temp_grade(pdev, val);
478 static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
480 struct imx_thermal_data *data = dev;
482 disable_irq_nosync(irq);
483 data->irq_enabled = false;
485 return IRQ_WAKE_THREAD;
488 static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
490 struct imx_thermal_data *data = dev;
492 dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
493 data->alarm_temp / 1000);
495 thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
500 static const struct of_device_id of_imx_thermal_match[] = {
501 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
502 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
505 MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
507 static int imx_thermal_probe(struct platform_device *pdev)
509 struct imx_thermal_data *data;
514 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
518 map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
521 dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
526 data->socdata = of_device_get_match_data(&pdev->dev);
527 if (!data->socdata) {
528 dev_err(&pdev->dev, "no device match found\n");
532 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
533 if (data->socdata->version == TEMPMON_IMX6SX) {
534 regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
535 MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
537 * reset value of LOW ALARM is incorrect, set it to lowest
538 * value to avoid false trigger of low alarm.
540 regmap_write(map, TEMPSENSE2 + REG_SET,
541 TEMPSENSE2_LOW_VALUE_MASK);
544 data->irq = platform_get_irq(pdev, 0);
548 platform_set_drvdata(pdev, data);
550 if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
551 ret = imx_init_from_nvmem_cells(pdev);
552 if (ret == -EPROBE_DEFER)
555 dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
560 ret = imx_init_from_tempmon_data(pdev);
562 dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
567 /* Make sure sensor is in known good state for measurements */
568 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
569 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
570 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
571 regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
572 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
574 data->policy = cpufreq_cpu_get(0);
576 pr_debug("%s: CPUFreq policy not found\n", __func__);
577 return -EPROBE_DEFER;
580 data->cdev = cpufreq_cooling_register(data->policy);
581 if (IS_ERR(data->cdev)) {
582 ret = PTR_ERR(data->cdev);
584 "failed to register cpufreq cooling device: %d\n", ret);
585 cpufreq_cpu_put(data->policy);
589 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
590 if (IS_ERR(data->thermal_clk)) {
591 ret = PTR_ERR(data->thermal_clk);
592 if (ret != -EPROBE_DEFER)
594 "failed to get thermal clk: %d\n", ret);
595 cpufreq_cooling_unregister(data->cdev);
596 cpufreq_cpu_put(data->policy);
601 * Thermal sensor needs clk on to get correct value, normally
602 * we should enable its clk before taking measurement and disable
603 * clk after measurement is done, but if alarm function is enabled,
604 * hardware will auto measure the temperature periodically, so we
605 * need to keep the clk always on for alarm function.
607 ret = clk_prepare_enable(data->thermal_clk);
609 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
610 cpufreq_cooling_unregister(data->cdev);
611 cpufreq_cpu_put(data->policy);
615 data->tz = thermal_zone_device_register("imx_thermal_zone",
617 BIT(IMX_TRIP_PASSIVE), data,
621 if (IS_ERR(data->tz)) {
622 ret = PTR_ERR(data->tz);
624 "failed to register thermal zone device %d\n", ret);
625 clk_disable_unprepare(data->thermal_clk);
626 cpufreq_cooling_unregister(data->cdev);
627 cpufreq_cpu_put(data->policy);
631 dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
632 " critical:%dC passive:%dC\n", data->temp_grade,
633 data->temp_max / 1000, data->temp_critical / 1000,
634 data->temp_passive / 1000);
636 /* Enable measurements at ~ 10 Hz */
637 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
638 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
639 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
640 imx_set_alarm_temp(data, data->temp_passive);
642 if (data->socdata->version == TEMPMON_IMX6SX)
643 imx_set_panic_temp(data, data->temp_critical);
645 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
646 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
648 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
649 imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
650 0, "imx_thermal", data);
652 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
653 clk_disable_unprepare(data->thermal_clk);
654 thermal_zone_device_unregister(data->tz);
655 cpufreq_cooling_unregister(data->cdev);
656 cpufreq_cpu_put(data->policy);
660 data->irq_enabled = true;
661 data->mode = THERMAL_DEVICE_ENABLED;
666 static int imx_thermal_remove(struct platform_device *pdev)
668 struct imx_thermal_data *data = platform_get_drvdata(pdev);
669 struct regmap *map = data->tempmon;
671 /* Disable measurements */
672 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
673 if (!IS_ERR(data->thermal_clk))
674 clk_disable_unprepare(data->thermal_clk);
676 thermal_zone_device_unregister(data->tz);
677 cpufreq_cooling_unregister(data->cdev);
678 cpufreq_cpu_put(data->policy);
683 #ifdef CONFIG_PM_SLEEP
684 static int imx_thermal_suspend(struct device *dev)
686 struct imx_thermal_data *data = dev_get_drvdata(dev);
687 struct regmap *map = data->tempmon;
690 * Need to disable thermal sensor, otherwise, when thermal core
691 * try to get temperature before thermal sensor resume, a wrong
692 * temperature will be read as the thermal sensor is powered
695 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
696 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
697 data->mode = THERMAL_DEVICE_DISABLED;
698 clk_disable_unprepare(data->thermal_clk);
703 static int imx_thermal_resume(struct device *dev)
705 struct imx_thermal_data *data = dev_get_drvdata(dev);
706 struct regmap *map = data->tempmon;
709 ret = clk_prepare_enable(data->thermal_clk);
712 /* Enabled thermal sensor after resume */
713 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
714 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
715 data->mode = THERMAL_DEVICE_ENABLED;
721 static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
722 imx_thermal_suspend, imx_thermal_resume);
724 static struct platform_driver imx_thermal = {
726 .name = "imx_thermal",
727 .pm = &imx_thermal_pm_ops,
728 .of_match_table = of_imx_thermal_match,
730 .probe = imx_thermal_probe,
731 .remove = imx_thermal_remove,
733 module_platform_driver(imx_thermal);
735 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
736 MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
737 MODULE_LICENSE("GPL v2");
738 MODULE_ALIAS("platform:imx-thermal");