imx6: imx: thermal: use CPU temperature grade info for thresholds
[openwrt.git] / target / linux / imx6 / patches-4.3 / 045-imx-thermal-use-cpu-temperature-grade-info-for-thresholds
1 Index: linux-4.3/drivers/thermal/imx_thermal.c
2 ===================================================================
3 --- linux-4.3.orig/drivers/thermal/imx_thermal.c        2015-11-01 16:05:25.000000000 -0800
4 +++ linux-4.3/drivers/thermal/imx_thermal.c     2015-12-18 10:39:44.915158318 -0800
5 @@ -55,6 +55,7 @@
6  #define TEMPSENSE2_PANIC_VALUE_SHIFT   16
7  #define TEMPSENSE2_PANIC_VALUE_MASK    0xfff0000
8  
9 +#define OCOTP_MEM0                     0x0480
10  #define OCOTP_ANA1                     0x04e0
11  
12  /* The driver supports 1 passive trip point and 1 critical trip point */
13 @@ -64,12 +65,6 @@
14         IMX_TRIP_NUM,
15  };
16  
17 -/*
18 - * It defines the temperature in millicelsius for passive trip point
19 - * that will trigger cooling action when crossed.
20 - */
21 -#define IMX_TEMP_PASSIVE               85000
22 -
23  #define IMX_POLLING_DELAY              2000 /* millisecond */
24  #define IMX_PASSIVE_DELAY              1000
25  
26 @@ -100,12 +95,14 @@
27         u32 c1, c2; /* See formula in imx_get_sensor_data() */
28         int temp_passive;
29         int temp_critical;
30 +       unsigned long temp_max;
31         int alarm_temp;
32         int last_temp;
33         bool irq_enabled;
34         int irq;
35         struct clk *thermal_clk;
36         const struct thermal_soc_data *socdata;
37 +       const char *temp_grade;
38  };
39  
40  static void imx_set_panic_temp(struct imx_thermal_data *data,
41 @@ -285,10 +282,12 @@
42  {
43         struct imx_thermal_data *data = tz->devdata;
44  
45 +       /* do not allow changing critical threshold */
46         if (trip == IMX_TRIP_CRITICAL)
47                 return -EPERM;
48  
49 -       if (temp > IMX_TEMP_PASSIVE)
50 +       /* do not allow passive to be set higher than critical */
51 +       if (temp < 0 || temp > data->temp_critical)
52                 return -EINVAL;
53  
54         data->temp_passive = temp;
55 @@ -404,17 +403,39 @@
56         data->c1 = temp64;
57         data->c2 = n1 * data->c1 + 1000 * t1;
58  
59 -       /*
60 -        * Set the default passive cooling trip point,
61 -        * can be changed from userspace.
62 -        */
63 -       data->temp_passive = IMX_TEMP_PASSIVE;
64 +       /* use OTP for thermal grade */
65 +       ret = regmap_read(map, OCOTP_MEM0, &val);
66 +       if (ret) {
67 +               dev_err(&pdev->dev, "failed to read temp grade: %d\n", ret);
68 +               return ret;
69 +       }
70 +
71 +       /* The maximum die temp is specified by the Temperature Grade */
72 +       switch ((val >> 6) & 0x3) {
73 +       case 0: /* Commercial (0 to 95C) */
74 +               data->temp_grade = "Commercial";
75 +               data->temp_max = 95000;
76 +               break;
77 +       case 1: /* Extended Commercial (-20 to 105C) */
78 +               data->temp_grade = "Extended Commercial";
79 +               data->temp_max = 105000;
80 +               break;
81 +       case 2: /* Industrial (-40 to 105C) */
82 +               data->temp_grade = "Industrial";
83 +               data->temp_max = 105000;
84 +               break;
85 +       case 3: /* Automotive (-40 to 125C) */
86 +               data->temp_grade = "Automotive";
87 +               data->temp_max = 125000;
88 +               break;
89 +       }
90  
91         /*
92 -        * The maximum die temperature set to 20 C higher than
93 -        * IMX_TEMP_PASSIVE.
94 +        * Set the critical trip point at 5C under max
95 +        * Set the passive trip point at 10C under max (can change via sysfs)
96          */
97 -       data->temp_critical = 1000 * 20 + data->temp_passive;
98 +       data->temp_critical = data->temp_max - (1000 * 5);
99 +       data->temp_passive = data->temp_max - (1000 * 10);
100  
101         return 0;
102  }
103 @@ -559,6 +580,11 @@
104                 return ret;
105         }
106  
107 +       dev_info(&pdev->dev, "%s CPU temperature grade - max:%ldC"
108 +                " critical:%ldC passive:%ldC\n", data->temp_grade,
109 +                data->temp_max / 1000, data->temp_critical / 1000,
110 +                data->temp_passive / 1000);
111 +
112         /* Enable measurements at ~ 10 Hz */
113         regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
114         measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */