aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/thermal/imx_thermal.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/thermal/imx_thermal.c')
-rw-r--r--drivers/thermal/imx_thermal.c136
1 files changed, 107 insertions, 29 deletions
diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c
index 2c516f2eebed..5a1f1070b702 100644
--- a/drivers/thermal/imx_thermal.c
+++ b/drivers/thermal/imx_thermal.c
@@ -19,6 +19,7 @@
19#include <linux/mfd/syscon.h> 19#include <linux/mfd/syscon.h>
20#include <linux/module.h> 20#include <linux/module.h>
21#include <linux/of.h> 21#include <linux/of.h>
22#include <linux/of_device.h>
22#include <linux/platform_device.h> 23#include <linux/platform_device.h>
23#include <linux/regmap.h> 24#include <linux/regmap.h>
24#include <linux/slab.h> 25#include <linux/slab.h>
@@ -31,6 +32,11 @@
31 32
32#define MISC0 0x0150 33#define MISC0 0x0150
33#define MISC0_REFTOP_SELBIASOFF (1 << 3) 34#define MISC0_REFTOP_SELBIASOFF (1 << 3)
35#define MISC1 0x0160
36#define MISC1_IRQ_TEMPHIGH (1 << 29)
37/* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
38#define MISC1_IRQ_TEMPLOW (1 << 28)
39#define MISC1_IRQ_TEMPPANIC (1 << 27)
34 40
35#define TEMPSENSE0 0x0180 41#define TEMPSENSE0 0x0180
36#define TEMPSENSE0_ALARM_VALUE_SHIFT 20 42#define TEMPSENSE0_ALARM_VALUE_SHIFT 20
@@ -43,6 +49,12 @@
43 49
44#define TEMPSENSE1 0x0190 50#define TEMPSENSE1 0x0190
45#define TEMPSENSE1_MEASURE_FREQ 0xffff 51#define TEMPSENSE1_MEASURE_FREQ 0xffff
52/* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
53#define TEMPSENSE2 0x0290
54#define TEMPSENSE2_LOW_VALUE_SHIFT 0
55#define TEMPSENSE2_LOW_VALUE_MASK 0xfff
56#define TEMPSENSE2_PANIC_VALUE_SHIFT 16
57#define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
46 58
47#define OCOTP_ANA1 0x04e0 59#define OCOTP_ANA1 0x04e0
48 60
@@ -66,6 +78,21 @@ enum imx_thermal_trip {
66#define FACTOR1 15976 78#define FACTOR1 15976
67#define FACTOR2 4297157 79#define FACTOR2 4297157
68 80
81#define TEMPMON_IMX6Q 1
82#define TEMPMON_IMX6SX 2
83
84struct thermal_soc_data {
85 u32 version;
86};
87
88static struct thermal_soc_data thermal_imx6q_data = {
89 .version = TEMPMON_IMX6Q,
90};
91
92static struct thermal_soc_data thermal_imx6sx_data = {
93 .version = TEMPMON_IMX6SX,
94};
95
69struct imx_thermal_data { 96struct imx_thermal_data {
70 struct thermal_zone_device *tz; 97 struct thermal_zone_device *tz;
71 struct thermal_cooling_device *cdev; 98 struct thermal_cooling_device *cdev;
@@ -79,8 +106,21 @@ struct imx_thermal_data {
79 bool irq_enabled; 106 bool irq_enabled;
80 int irq; 107 int irq;
81 struct clk *thermal_clk; 108 struct clk *thermal_clk;
109 const struct thermal_soc_data *socdata;
82}; 110};
83 111
112static void imx_set_panic_temp(struct imx_thermal_data *data,
113 signed long panic_temp)
114{
115 struct regmap *map = data->tempmon;
116 int critical_value;
117
118 critical_value = (data->c2 - panic_temp) / data->c1;
119 regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK);
120 regmap_write(map, TEMPSENSE2 + REG_SET, critical_value <<
121 TEMPSENSE2_PANIC_VALUE_SHIFT);
122}
123
84static void imx_set_alarm_temp(struct imx_thermal_data *data, 124static void imx_set_alarm_temp(struct imx_thermal_data *data,
85 signed long alarm_temp) 125 signed long alarm_temp)
86{ 126{
@@ -142,13 +182,17 @@ static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
142 /* See imx_get_sensor_data() for formula derivation */ 182 /* See imx_get_sensor_data() for formula derivation */
143 *temp = data->c2 - n_meas * data->c1; 183 *temp = data->c2 - n_meas * data->c1;
144 184
145 /* Update alarm value to next higher trip point */ 185 /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
146 if (data->alarm_temp == data->temp_passive && *temp >= data->temp_passive) 186 if (data->socdata->version == TEMPMON_IMX6Q) {
147 imx_set_alarm_temp(data, data->temp_critical); 187 if (data->alarm_temp == data->temp_passive &&
148 if (data->alarm_temp == data->temp_critical && *temp < data->temp_passive) { 188 *temp >= data->temp_passive)
149 imx_set_alarm_temp(data, data->temp_passive); 189 imx_set_alarm_temp(data, data->temp_critical);
150 dev_dbg(&tz->device, "thermal alarm off: T < %lu\n", 190 if (data->alarm_temp == data->temp_critical &&
151 data->alarm_temp / 1000); 191 *temp < data->temp_passive) {
192 imx_set_alarm_temp(data, data->temp_passive);
193 dev_dbg(&tz->device, "thermal alarm off: T < %lu\n",
194 data->alarm_temp / 1000);
195 }
152 } 196 }
153 197
154 if (*temp != data->last_temp) { 198 if (*temp != data->last_temp) {
@@ -398,14 +442,27 @@ static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
398 return IRQ_HANDLED; 442 return IRQ_HANDLED;
399} 443}
400 444
445static const struct of_device_id of_imx_thermal_match[] = {
446 { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
447 { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
448 { /* end */ }
449};
450MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
451
401static int imx_thermal_probe(struct platform_device *pdev) 452static int imx_thermal_probe(struct platform_device *pdev)
402{ 453{
454 const struct of_device_id *of_id =
455 of_match_device(of_imx_thermal_match, &pdev->dev);
403 struct imx_thermal_data *data; 456 struct imx_thermal_data *data;
404 struct cpumask clip_cpus; 457 struct cpumask clip_cpus;
405 struct regmap *map; 458 struct regmap *map;
406 int measure_freq; 459 int measure_freq;
407 int ret; 460 int ret;
408 461
462 if (!cpufreq_get_current_driver()) {
463 dev_dbg(&pdev->dev, "no cpufreq driver!");
464 return -EPROBE_DEFER;
465 }
409 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 466 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
410 if (!data) 467 if (!data)
411 return -ENOMEM; 468 return -ENOMEM;
@@ -418,6 +475,20 @@ static int imx_thermal_probe(struct platform_device *pdev)
418 } 475 }
419 data->tempmon = map; 476 data->tempmon = map;
420 477
478 data->socdata = of_id->data;
479
480 /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
481 if (data->socdata->version == TEMPMON_IMX6SX) {
482 regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
483 MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
484 /*
485 * reset value of LOW ALARM is incorrect, set it to lowest
486 * value to avoid false trigger of low alarm.
487 */
488 regmap_write(map, TEMPSENSE2 + REG_SET,
489 TEMPSENSE2_LOW_VALUE_MASK);
490 }
491
421 data->irq = platform_get_irq(pdev, 0); 492 data->irq = platform_get_irq(pdev, 0);
422 if (data->irq < 0) 493 if (data->irq < 0)
423 return data->irq; 494 return data->irq;
@@ -454,6 +525,30 @@ static int imx_thermal_probe(struct platform_device *pdev)
454 return ret; 525 return ret;
455 } 526 }
456 527
528 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
529 if (IS_ERR(data->thermal_clk)) {
530 ret = PTR_ERR(data->thermal_clk);
531 if (ret != -EPROBE_DEFER)
532 dev_err(&pdev->dev,
533 "failed to get thermal clk: %d\n", ret);
534 cpufreq_cooling_unregister(data->cdev);
535 return ret;
536 }
537
538 /*
539 * Thermal sensor needs clk on to get correct value, normally
540 * we should enable its clk before taking measurement and disable
541 * clk after measurement is done, but if alarm function is enabled,
542 * hardware will auto measure the temperature periodically, so we
543 * need to keep the clk always on for alarm function.
544 */
545 ret = clk_prepare_enable(data->thermal_clk);
546 if (ret) {
547 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
548 cpufreq_cooling_unregister(data->cdev);
549 return ret;
550 }
551
457 data->tz = thermal_zone_device_register("imx_thermal_zone", 552 data->tz = thermal_zone_device_register("imx_thermal_zone",
458 IMX_TRIP_NUM, 553 IMX_TRIP_NUM,
459 BIT(IMX_TRIP_PASSIVE), data, 554 BIT(IMX_TRIP_PASSIVE), data,
@@ -464,31 +559,20 @@ static int imx_thermal_probe(struct platform_device *pdev)
464 ret = PTR_ERR(data->tz); 559 ret = PTR_ERR(data->tz);
465 dev_err(&pdev->dev, 560 dev_err(&pdev->dev,
466 "failed to register thermal zone device %d\n", ret); 561 "failed to register thermal zone device %d\n", ret);
562 clk_disable_unprepare(data->thermal_clk);
467 cpufreq_cooling_unregister(data->cdev); 563 cpufreq_cooling_unregister(data->cdev);
468 return ret; 564 return ret;
469 } 565 }
470 566
471 data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
472 if (IS_ERR(data->thermal_clk)) {
473 dev_warn(&pdev->dev, "failed to get thermal clk!\n");
474 } else {
475 /*
476 * Thermal sensor needs clk on to get correct value, normally
477 * we should enable its clk before taking measurement and disable
478 * clk after measurement is done, but if alarm function is enabled,
479 * hardware will auto measure the temperature periodically, so we
480 * need to keep the clk always on for alarm function.
481 */
482 ret = clk_prepare_enable(data->thermal_clk);
483 if (ret)
484 dev_warn(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
485 }
486
487 /* Enable measurements at ~ 10 Hz */ 567 /* Enable measurements at ~ 10 Hz */
488 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ); 568 regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
489 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */ 569 measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
490 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq); 570 regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
491 imx_set_alarm_temp(data, data->temp_passive); 571 imx_set_alarm_temp(data, data->temp_passive);
572
573 if (data->socdata->version == TEMPMON_IMX6SX)
574 imx_set_panic_temp(data, data->temp_critical);
575
492 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN); 576 regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
493 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP); 577 regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
494 578
@@ -550,12 +634,6 @@ static int imx_thermal_resume(struct device *dev)
550static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops, 634static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
551 imx_thermal_suspend, imx_thermal_resume); 635 imx_thermal_suspend, imx_thermal_resume);
552 636
553static const struct of_device_id of_imx_thermal_match[] = {
554 { .compatible = "fsl,imx6q-tempmon", },
555 { /* end */ }
556};
557MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
558
559static struct platform_driver imx_thermal = { 637static struct platform_driver imx_thermal = {
560 .driver = { 638 .driver = {
561 .name = "imx_thermal", 639 .name = "imx_thermal",