2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
6 * EXYNOS - Thermal Management Unit
8 * See file CREDITS for list of people who contributed to this
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,
25 #include <asm/arch/tmu.h>
26 #include <asm/arch/power.h>
28 #define TRIMINFO_RELOAD 1
30 #define THERM_TRIP_EN (1 << 12)
33 #define INTEN_RISE1 (1 << 4)
34 #define INTEN_RISE2 (1 << 8)
35 #define INTEN_FALL0 (1 << 16)
36 #define INTEN_FALL1 (1 << 20)
37 #define INTEN_FALL2 (1 << 24)
39 #define TRIM_INFO_MASK 0xff
41 #define INTCLEAR_RISE0 1
42 #define INTCLEAR_RISE1 (1 << 4)
43 #define INTCLEAR_RISE2 (1 << 8)
44 #define INTCLEAR_FALL0 (1 << 16)
45 #define INTCLEAR_FALL1 (1 << 20)
46 #define INTCLEAR_FALL2 (1 << 24)
47 #define INTCLEARALL (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \
48 INTCLEAR_RISE2 | INTCLEAR_FALL0 | \
49 INTCLEAR_FALL1 | INTCLEAR_FALL2)
51 /* Tmeperature threshold values for various thermal events */
52 struct temperature_params {
53 /* minimum value in temperature code range */
55 /* maximum value in temperature code range */
57 /* temperature threshold to start warning */
58 unsigned start_warning;
59 /* temperature threshold CPU tripping */
60 unsigned start_tripping;
61 /* temperature threshold for HW tripping */
62 unsigned hardware_tripping;
65 /* Pre-defined values and thresholds for calibration of current temperature */
67 /* pre-defined temperature thresholds */
68 struct temperature_params ts;
69 /* pre-defined efuse range minimum value */
70 unsigned efuse_min_value;
71 /* pre-defined efuse value for temperature calibration */
73 /* pre-defined efuse range maximum value */
74 unsigned efuse_max_value;
75 /* current temperature sensing slope */
79 /* TMU device specific details and status */
81 /* base Address for the TMU */
82 struct exynos5_tmu_reg *tmu_base;
83 /* mux Address for the TMU */
85 /* pre-defined values for calibration and thresholds */
87 /* value required for triminfo_25 calibration */
89 /* value required for triminfo_85 calibration */
91 /* Value for measured data calibration */
93 /* enum value indicating status of the TMU */
97 /* Global struct tmu_info variable to store init values */
98 static struct tmu_info gbl_info;
101 * Get current temperature code from register,
102 * then calculate and calibrate it's value
105 * @return current temperature of the chip as sensed by TMU
107 static int get_cur_temp(struct tmu_info *info)
109 struct exynos5_tmu_reg *reg = info->tmu_base;
114 * Temperature code range between min 25 and max 125.
115 * May run more than once for first call as initial sensing
116 * has not yet happened.
118 if (info->tmu_state == TMU_STATUS_NORMAL) {
119 start = get_timer(0);
121 cur_temp = readl(®->current_temp) & 0xff;
122 } while ((cur_temp == 0) || (get_timer(start) > 100));
128 /* Calibrate current temperature */
129 cur_temp = cur_temp - info->te1 + info->dc_value;
135 * Monitors status of the TMU device and exynos temperature
137 * @param temp pointer to the current temperature value
138 * @return enum tmu_status_t value, code indicating event to execute
140 enum tmu_status_t tmu_monitor(int *temp)
143 struct tmu_data *data = &gbl_info.data;
145 if (gbl_info.tmu_state == TMU_STATUS_INIT)
146 return TMU_STATUS_INIT;
148 /* Read current temperature of the SOC */
149 cur_temp = get_cur_temp(&gbl_info);
156 /* Temperature code lies between min 25 and max 125 */
157 if ((cur_temp >= data->ts.start_tripping) &&
158 (cur_temp <= data->ts.max_val))
159 return TMU_STATUS_TRIPPED;
161 if (cur_temp >= data->ts.start_warning)
162 return TMU_STATUS_WARNING;
164 if ((cur_temp < data->ts.start_warning) &&
165 (cur_temp >= data->ts.min_val))
166 return TMU_STATUS_NORMAL;
169 /* Temperature code does not lie between min 25 and max 125 */
170 gbl_info.tmu_state = TMU_STATUS_INIT;
171 debug("EXYNOS_TMU: Thermal reading failed\n");
172 return TMU_STATUS_INIT;
176 * Get TMU specific pre-defined values from FDT
178 * @param info pointer to the tmu_info struct
179 * @param blob FDT blob
180 * @return int value, 0 for success
182 static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
184 #if CONFIG_IS_ENABLED(OF_CONTROL)
189 /* Get the node from FDT for TMU */
190 node = fdtdec_next_compatible(blob, 0,
191 COMPAT_SAMSUNG_EXYNOS_TMU);
193 debug("EXYNOS_TMU: No node for tmu in device tree\n");
198 * Get the pre-defined TMU specific values from FDT.
199 * All of these are expected to be correct otherwise
200 * miscalculation of register values in tmu_setup_parameters
201 * may result in misleading current temperature.
203 addr = fdtdec_get_addr(blob, node, "reg");
204 if (addr == FDT_ADDR_T_NONE) {
205 debug("%s: Missing tmu-base\n", __func__);
208 info->tmu_base = (struct exynos5_tmu_reg *)addr;
210 /* Optional field. */
211 info->tmu_mux = fdtdec_get_int(blob,
212 node, "samsung,mux", -1);
213 /* Take default value as per the user manual b(110) */
214 if (info->tmu_mux == -1)
217 info->data.ts.min_val = fdtdec_get_int(blob,
218 node, "samsung,min-temp", -1);
219 error |= (info->data.ts.min_val == -1);
220 info->data.ts.max_val = fdtdec_get_int(blob,
221 node, "samsung,max-temp", -1);
222 error |= (info->data.ts.max_val == -1);
223 info->data.ts.start_warning = fdtdec_get_int(blob,
224 node, "samsung,start-warning", -1);
225 error |= (info->data.ts.start_warning == -1);
226 info->data.ts.start_tripping = fdtdec_get_int(blob,
227 node, "samsung,start-tripping", -1);
228 error |= (info->data.ts.start_tripping == -1);
229 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
230 node, "samsung,hw-tripping", -1);
231 error |= (info->data.ts.hardware_tripping == -1);
232 info->data.efuse_min_value = fdtdec_get_int(blob,
233 node, "samsung,efuse-min-value", -1);
234 error |= (info->data.efuse_min_value == -1);
235 info->data.efuse_value = fdtdec_get_int(blob,
236 node, "samsung,efuse-value", -1);
237 error |= (info->data.efuse_value == -1);
238 info->data.efuse_max_value = fdtdec_get_int(blob,
239 node, "samsung,efuse-max-value", -1);
240 error |= (info->data.efuse_max_value == -1);
241 info->data.slope = fdtdec_get_int(blob,
242 node, "samsung,slope", -1);
243 error |= (info->data.slope == -1);
244 info->dc_value = fdtdec_get_int(blob,
245 node, "samsung,dc-value", -1);
246 error |= (info->dc_value == -1);
249 debug("fail to get tmu node properties\n");
253 /* Non DT support may never be added. Just in case */
261 * Calibrate and calculate threshold values and
262 * enable interrupt levels
264 * @param info pointer to the tmu_info struct
266 static void tmu_setup_parameters(struct tmu_info *info)
268 unsigned te_code, con;
269 unsigned warning_code, trip_code, hwtrip_code;
270 unsigned cooling_temp;
271 unsigned rising_value;
272 struct tmu_data *data = &info->data;
273 struct exynos5_tmu_reg *reg = info->tmu_base;
275 /* Must reload for reading efuse value from triminfo register */
276 writel(TRIMINFO_RELOAD, ®->triminfo_control);
278 /* Get the compensation parameter */
279 te_code = readl(®->triminfo);
280 info->te1 = te_code & TRIM_INFO_MASK;
281 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
283 if ((data->efuse_min_value > info->te1) ||
284 (info->te1 > data->efuse_max_value)
286 info->te1 = data->efuse_value;
288 /* Get RISING & FALLING Threshold value */
289 warning_code = data->ts.start_warning
290 + info->te1 - info->dc_value;
291 trip_code = data->ts.start_tripping
292 + info->te1 - info->dc_value;
293 hwtrip_code = data->ts.hardware_tripping
294 + info->te1 - info->dc_value;
298 rising_value = ((warning_code << 8) |
300 (hwtrip_code << 24));
302 /* Set interrupt level */
303 writel(rising_value, ®->threshold_temp_rise);
304 writel(cooling_temp, ®->threshold_temp_fall);
307 * Init TMU control tuning parameters
308 * [28:24] VREF - Voltage reference
309 * [15:13] THERM_TRIP_MODE - Tripping mode
310 * [12] THERM_TRIP_EN - Thermal tripping enable
311 * [11:8] BUF_SLOPE_SEL - Gain of amplifier
312 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
314 writel(data->slope, ®->tmu_control);
316 writel(INTCLEARALL, ®->intclear);
318 /* TMU core enable */
319 con = readl(®->tmu_control);
320 con |= THERM_TRIP_EN | CORE_EN | (info->tmu_mux << 20);
322 writel(con, ®->tmu_control);
324 /* Enable HW thermal trip */
325 set_hw_thermal_trip();
327 /* LEV1 LEV2 interrupt enable */
328 writel(INTEN_RISE1 | INTEN_RISE2, ®->inten);
332 * Initialize TMU device
334 * @param blob FDT blob
335 * @return int value, 0 for success
337 int tmu_init(const void *blob)
339 gbl_info.tmu_state = TMU_STATUS_INIT;
340 if (get_tmu_fdt_values(&gbl_info, blob) < 0)
343 tmu_setup_parameters(&gbl_info);
344 gbl_info.tmu_state = TMU_STATUS_NORMAL;
346 return gbl_info.tmu_state;