]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - drivers/power/exynos-tmu.c
Exynos5: TMU: Add driver for Thermal Management Unit
[karo-tx-uboot.git] / drivers / power / exynos-tmu.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3  *      http://www.samsung.com
4  * Akshay Saraswat <akshay.s@samsung.com>
5  *
6  * EXYNOS - Thermal Management Unit
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  */
19
20 #include <common.h>
21 #include <errno.h>
22 #include <fdtdec.h>
23 #include <tmu.h>
24 #include <asm/arch/tmu.h>
25
26 #define TRIMINFO_RELOAD         1
27 #define CORE_EN                 1
28
29 #define INTEN_RISE0             1
30 #define INTEN_RISE1             (1 << 4)
31 #define INTEN_RISE2             (1 << 8)
32 #define INTEN_FALL0             (1 << 16)
33 #define INTEN_FALL1             (1 << 20)
34 #define INTEN_FALL2             (1 << 24)
35
36 #define TRIM_INFO_MASK          0xff
37
38 #define INTCLEAR_RISE0          1
39 #define INTCLEAR_RISE1          (1 << 4)
40 #define INTCLEAR_RISE2          (1 << 8)
41 #define INTCLEAR_FALL0          (1 << 16)
42 #define INTCLEAR_FALL1          (1 << 20)
43 #define INTCLEAR_FALL2          (1 << 24)
44 #define INTCLEARALL             (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \
45                                  INTCLEAR_RISE2 | INTCLEAR_FALL0 | \
46                                  INTCLEAR_FALL1 | INTCLEAR_FALL2)
47
48 /* Tmeperature threshold values for various thermal events */
49 struct temperature_params {
50         /* minimum value in temperature code range */
51         unsigned int min_val;
52         /* maximum value in temperature code range */
53         unsigned int max_val;
54         /* temperature threshold to start warning */
55         unsigned int start_warning;
56         /* temperature threshold CPU tripping */
57         unsigned int start_tripping;
58 };
59
60 /* Pre-defined values and thresholds for calibration of current temperature */
61 struct tmu_data {
62         /* pre-defined temperature thresholds */
63         struct temperature_params ts;
64         /* pre-defined efuse range minimum value */
65         unsigned int efuse_min_value;
66         /* pre-defined efuse value for temperature calibration */
67         unsigned int efuse_value;
68         /* pre-defined efuse range maximum value */
69         unsigned int efuse_max_value;
70         /* current temperature sensing slope */
71         unsigned int slope;
72 };
73
74 /* TMU device specific details and status */
75 struct tmu_info {
76         /* base Address for the TMU */
77         unsigned tmu_base;
78         /* pre-defined values for calibration and thresholds */
79         struct tmu_data data;
80         /* value required for triminfo_25 calibration */
81         unsigned int te1;
82         /* value required for triminfo_85 calibration */
83         unsigned int te2;
84         /* Value for measured data calibration */
85         int dc_value;
86         /* enum value indicating status of the TMU */
87         int tmu_state;
88 };
89
90 /* Global struct tmu_info variable to store init values */
91 static struct tmu_info gbl_info;
92
93 /*
94  * Get current temperature code from register,
95  * then calculate and calibrate it's value
96  * in degree celsius.
97  *
98  * @return      current temperature of the chip as sensed by TMU
99  */
100 static int get_cur_temp(struct tmu_info *info)
101 {
102         int cur_temp;
103         struct exynos5_tmu_reg *reg = (struct exynos5_tmu_reg *)info->tmu_base;
104
105         /*
106          * Temperature code range between min 25 and max 125.
107          * May run more than once for first call as initial sensing
108          * has not yet happened.
109          */
110         do {
111                 cur_temp = readl(&reg->current_temp) & 0xff;
112         } while (cur_temp == 0 && info->tmu_state == TMU_STATUS_NORMAL);
113
114         /* Calibrate current temperature */
115         cur_temp = cur_temp - info->te1 + info->dc_value;
116
117         return cur_temp;
118 }
119
120 /*
121  * Monitors status of the TMU device and exynos temperature
122  *
123  * @param temp  pointer to the current temperature value
124  * @return      enum tmu_status_t value, code indicating event to execute
125  */
126 enum tmu_status_t tmu_monitor(int *temp)
127 {
128         int cur_temp;
129         struct tmu_data *data = &gbl_info.data;
130
131         if (gbl_info.tmu_state == TMU_STATUS_INIT)
132                 return TMU_STATUS_INIT;
133
134         /* Read current temperature of the SOC */
135         cur_temp = get_cur_temp(&gbl_info);
136         *temp = cur_temp;
137
138         /* Temperature code lies between min 25 and max 125 */
139         if (cur_temp >= data->ts.start_tripping &&
140                         cur_temp <= data->ts.max_val) {
141                 return TMU_STATUS_TRIPPED;
142         } else if (cur_temp >= data->ts.start_warning) {
143                 return TMU_STATUS_WARNING;
144         } else if (cur_temp < data->ts.start_warning &&
145                         cur_temp >= data->ts.min_val) {
146                 return TMU_STATUS_NORMAL;
147         } else {
148                 /* Temperature code does not lie between min 25 and max 125 */
149                 gbl_info.tmu_state = TMU_STATUS_INIT;
150                 debug("EXYNOS_TMU: Thermal reading failed\n");
151                 return TMU_STATUS_INIT;
152         }
153 }
154
155 /*
156  * Get TMU specific pre-defined values from FDT
157  *
158  * @param info  pointer to the tmu_info struct
159  * @param blob  FDT blob
160  * @return      int value, 0 for success
161  */
162 static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
163 {
164 #ifdef CONFIG_OF_CONTROL
165         int node;
166         int error = 0;
167
168         /* Get the node from FDT for TMU */
169         node = fdtdec_next_compatible(blob, 0,
170                                       COMPAT_SAMSUNG_EXYNOS_TMU);
171         if (node < 0) {
172                 debug("EXYNOS_TMU: No node for tmu in device tree\n");
173                 return -1;
174         }
175
176         /*
177          * Get the pre-defined TMU specific values from FDT.
178          * All of these are expected to be correct otherwise
179          * miscalculation of register values in tmu_setup_parameters
180          * may result in misleading current temperature.
181          */
182         info->tmu_base = fdtdec_get_addr(blob, node, "reg");
183         if (info->tmu_base == FDT_ADDR_T_NONE) {
184                 debug("%s: Missing tmu-base\n", __func__);
185                 return -1;
186         }
187         info->data.ts.min_val = fdtdec_get_int(blob,
188                                 node, "samsung,min-temp", -1);
189         error |= info->data.ts.min_val;
190         info->data.ts.max_val = fdtdec_get_int(blob,
191                                 node, "samsung,max-temp", -1);
192         error |= info->data.ts.max_val;
193         info->data.ts.start_warning = fdtdec_get_int(blob,
194                                 node, "samsung,start-warning", -1);
195         error |= info->data.ts.start_warning;
196         info->data.ts.start_tripping = fdtdec_get_int(blob,
197                                 node, "samsung,start-tripping", -1);
198         error |= info->data.ts.start_tripping;
199         info->data.efuse_min_value = fdtdec_get_int(blob,
200                                 node, "samsung,efuse-min-value", -1);
201         error |= info->data.efuse_min_value;
202         info->data.efuse_value = fdtdec_get_int(blob,
203                                 node, "samsung,efuse-value", -1);
204         error |= info->data.efuse_value;
205         info->data.efuse_max_value = fdtdec_get_int(blob,
206                                 node, "samsung,efuse-max-value", -1);
207         error |= info->data.efuse_max_value;
208         info->data.slope = fdtdec_get_int(blob,
209                                 node, "samsung,slope", -1);
210         error |= info->data.slope;
211         info->dc_value = fdtdec_get_int(blob,
212                                 node, "samsung,dc-value", -1);
213         error |= info->dc_value;
214
215         if (error == -1) {
216                 debug("fail to get tmu node properties\n");
217                 return -1;
218         }
219 #endif
220
221         return 0;
222 }
223
224 /*
225  * Calibrate and calculate threshold values and
226  * enable interrupt levels
227  *
228  * @param       info pointer to the tmu_info struct
229  */
230 static void tmu_setup_parameters(struct tmu_info *info)
231 {
232         unsigned int te_code, con;
233         unsigned int warning_code, trip_code;
234         unsigned int cooling_temp;
235         unsigned int rising_value;
236         struct tmu_data *data = &info->data;
237         struct exynos5_tmu_reg *reg = (struct exynos5_tmu_reg *)info->tmu_base;
238
239         /* Must reload for reading efuse value from triminfo register */
240         writel(TRIMINFO_RELOAD, &reg->triminfo_control);
241
242         /* Get the compensation parameter */
243         te_code = readl(&reg->triminfo);
244         info->te1 = te_code & TRIM_INFO_MASK;
245         info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
246
247         if ((data->efuse_min_value > info->te1) ||
248                         (info->te1 > data->efuse_max_value)
249                         ||  (info->te2 != 0))
250                 info->te1 = data->efuse_value;
251
252         /* Get RISING & FALLING Threshold value */
253         warning_code = data->ts.start_warning
254                         + info->te1 - info->dc_value;
255         trip_code = data->ts.start_tripping
256                         + info->te1 - info->dc_value;
257         cooling_temp = 0;
258
259         rising_value = ((warning_code << 8) | (trip_code << 16));
260
261         /* Set interrupt level */
262         writel(rising_value, &reg->threshold_temp_rise);
263         writel(cooling_temp, &reg->threshold_temp_fall);
264
265         /*
266          * Init TMU control tuning parameters
267          * [28:24] VREF - Voltage reference
268          * [15:13] THERM_TRIP_MODE - Tripping mode
269          * [12] THERM_TRIP_EN - Thermal tripping enable
270          * [11:8] BUF_SLOPE_SEL - Gain of amplifier
271          * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
272          */
273         writel(data->slope, &reg->tmu_control);
274
275         writel(INTCLEARALL, &reg->intclear);
276
277         /* TMU core enable */
278         con = readl(&reg->tmu_control);
279         con |= CORE_EN;
280
281         writel(con, &reg->tmu_control);
282
283         /* LEV0 LEV1 LEV2 interrupt enable */
284         writel(INTEN_RISE0 | INTEN_RISE1 | INTEN_RISE2, &reg->inten);
285 }
286
287 /*
288  * Initialize TMU device
289  *
290  * @param blob  FDT blob
291  * @return      int value, 0 for success
292  */
293 int tmu_init(const void *blob)
294 {
295         gbl_info.tmu_state = TMU_STATUS_INIT;
296         if (get_tmu_fdt_values(&gbl_info, blob) < 0)
297                 goto ret;
298
299         tmu_setup_parameters(&gbl_info);
300         gbl_info.tmu_state = TMU_STATUS_NORMAL;
301 ret:
302
303         return gbl_info.tmu_state;
304 }