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,
24 #include <asm/arch/tmu.h>
25 #include <asm/arch/power.h>
27 #define TRIMINFO_RELOAD 1
29 #define THERM_TRIP_EN (1 << 12)
32 #define INTEN_RISE1 (1 << 4)
33 #define INTEN_RISE2 (1 << 8)
34 #define INTEN_FALL0 (1 << 16)
35 #define INTEN_FALL1 (1 << 20)
36 #define INTEN_FALL2 (1 << 24)
38 #define TRIM_INFO_MASK 0xff
40 #define INTCLEAR_RISE0 1
41 #define INTCLEAR_RISE1 (1 << 4)
42 #define INTCLEAR_RISE2 (1 << 8)
43 #define INTCLEAR_FALL0 (1 << 16)
44 #define INTCLEAR_FALL1 (1 << 20)
45 #define INTCLEAR_FALL2 (1 << 24)
46 #define INTCLEARALL (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \
47 INTCLEAR_RISE2 | INTCLEAR_FALL0 | \
48 INTCLEAR_FALL1 | INTCLEAR_FALL2)
50 /* Tmeperature threshold values for various thermal events */
51 struct temperature_params {
52 /* minimum value in temperature code range */
54 /* maximum value in temperature code range */
56 /* temperature threshold to start warning */
57 unsigned int start_warning;
58 /* temperature threshold CPU tripping */
59 unsigned int start_tripping;
60 /* temperature threshold for HW tripping */
61 unsigned int hardware_tripping;
64 /* Pre-defined values and thresholds for calibration of current temperature */
66 /* pre-defined temperature thresholds */
67 struct temperature_params ts;
68 /* pre-defined efuse range minimum value */
69 unsigned int efuse_min_value;
70 /* pre-defined efuse value for temperature calibration */
71 unsigned int efuse_value;
72 /* pre-defined efuse range maximum value */
73 unsigned int efuse_max_value;
74 /* current temperature sensing slope */
78 /* TMU device specific details and status */
80 /* base Address for the TMU */
82 /* pre-defined values for calibration and thresholds */
84 /* value required for triminfo_25 calibration */
86 /* value required for triminfo_85 calibration */
88 /* Value for measured data calibration */
90 /* enum value indicating status of the TMU */
94 /* Global struct tmu_info variable to store init values */
95 static struct tmu_info gbl_info;
98 * Get current temperature code from register,
99 * then calculate and calibrate it's value
102 * @return current temperature of the chip as sensed by TMU
104 static int get_cur_temp(struct tmu_info *info)
107 struct exynos5_tmu_reg *reg = (struct exynos5_tmu_reg *)info->tmu_base;
110 * Temperature code range between min 25 and max 125.
111 * May run more than once for first call as initial sensing
112 * has not yet happened.
115 cur_temp = readl(®->current_temp) & 0xff;
116 } while (cur_temp == 0 && info->tmu_state == TMU_STATUS_NORMAL);
118 /* Calibrate current temperature */
119 cur_temp = cur_temp - info->te1 + info->dc_value;
125 * Monitors status of the TMU device and exynos temperature
127 * @param temp pointer to the current temperature value
128 * @return enum tmu_status_t value, code indicating event to execute
130 enum tmu_status_t tmu_monitor(int *temp)
133 struct tmu_data *data = &gbl_info.data;
135 if (gbl_info.tmu_state == TMU_STATUS_INIT)
136 return TMU_STATUS_INIT;
138 /* Read current temperature of the SOC */
139 cur_temp = get_cur_temp(&gbl_info);
142 /* Temperature code lies between min 25 and max 125 */
143 if (cur_temp >= data->ts.start_tripping &&
144 cur_temp <= data->ts.max_val) {
145 return TMU_STATUS_TRIPPED;
146 } else if (cur_temp >= data->ts.start_warning) {
147 return TMU_STATUS_WARNING;
148 } else if (cur_temp < data->ts.start_warning &&
149 cur_temp >= data->ts.min_val) {
150 return TMU_STATUS_NORMAL;
152 /* Temperature code does not lie between min 25 and max 125 */
153 gbl_info.tmu_state = TMU_STATUS_INIT;
154 debug("EXYNOS_TMU: Thermal reading failed\n");
155 return TMU_STATUS_INIT;
160 * Get TMU specific pre-defined values from FDT
162 * @param info pointer to the tmu_info struct
163 * @param blob FDT blob
164 * @return int value, 0 for success
166 static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
168 #ifdef CONFIG_OF_CONTROL
172 /* Get the node from FDT for TMU */
173 node = fdtdec_next_compatible(blob, 0,
174 COMPAT_SAMSUNG_EXYNOS_TMU);
176 debug("EXYNOS_TMU: No node for tmu in device tree\n");
181 * Get the pre-defined TMU specific values from FDT.
182 * All of these are expected to be correct otherwise
183 * miscalculation of register values in tmu_setup_parameters
184 * may result in misleading current temperature.
186 info->tmu_base = fdtdec_get_addr(blob, node, "reg");
187 if (info->tmu_base == FDT_ADDR_T_NONE) {
188 debug("%s: Missing tmu-base\n", __func__);
191 info->data.ts.min_val = fdtdec_get_int(blob,
192 node, "samsung,min-temp", -1);
193 error |= info->data.ts.min_val;
194 info->data.ts.max_val = fdtdec_get_int(blob,
195 node, "samsung,max-temp", -1);
196 error |= info->data.ts.max_val;
197 info->data.ts.start_warning = fdtdec_get_int(blob,
198 node, "samsung,start-warning", -1);
199 error |= info->data.ts.start_warning;
200 info->data.ts.start_tripping = fdtdec_get_int(blob,
201 node, "samsung,start-tripping", -1);
202 error |= info->data.ts.start_tripping;
203 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
204 node, "samsung,hw-tripping", -1);
205 error |= info->data.ts.hardware_tripping;
206 info->data.efuse_min_value = fdtdec_get_int(blob,
207 node, "samsung,efuse-min-value", -1);
208 error |= info->data.efuse_min_value;
209 info->data.efuse_value = fdtdec_get_int(blob,
210 node, "samsung,efuse-value", -1);
211 error |= info->data.efuse_value;
212 info->data.efuse_max_value = fdtdec_get_int(blob,
213 node, "samsung,efuse-max-value", -1);
214 error |= info->data.efuse_max_value;
215 info->data.slope = fdtdec_get_int(blob,
216 node, "samsung,slope", -1);
217 error |= info->data.slope;
218 info->dc_value = fdtdec_get_int(blob,
219 node, "samsung,dc-value", -1);
220 error |= info->dc_value;
223 debug("fail to get tmu node properties\n");
232 * Calibrate and calculate threshold values and
233 * enable interrupt levels
235 * @param info pointer to the tmu_info struct
237 static void tmu_setup_parameters(struct tmu_info *info)
239 unsigned int te_code, con;
240 unsigned int warning_code, trip_code, hwtrip_code;
241 unsigned int cooling_temp;
242 unsigned int rising_value;
243 struct tmu_data *data = &info->data;
244 struct exynos5_tmu_reg *reg = (struct exynos5_tmu_reg *)info->tmu_base;
246 /* Must reload for reading efuse value from triminfo register */
247 writel(TRIMINFO_RELOAD, ®->triminfo_control);
249 /* Get the compensation parameter */
250 te_code = readl(®->triminfo);
251 info->te1 = te_code & TRIM_INFO_MASK;
252 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
254 if ((data->efuse_min_value > info->te1) ||
255 (info->te1 > data->efuse_max_value)
257 info->te1 = data->efuse_value;
259 /* Get RISING & FALLING Threshold value */
260 warning_code = data->ts.start_warning
261 + info->te1 - info->dc_value;
262 trip_code = data->ts.start_tripping
263 + info->te1 - info->dc_value;
264 hwtrip_code = data->ts.hardware_tripping
265 + info->te1 - info->dc_value;
269 rising_value = ((warning_code << 8) |
271 (hwtrip_code << 24));
273 /* Set interrupt level */
274 writel(rising_value, ®->threshold_temp_rise);
275 writel(cooling_temp, ®->threshold_temp_fall);
278 * Init TMU control tuning parameters
279 * [28:24] VREF - Voltage reference
280 * [15:13] THERM_TRIP_MODE - Tripping mode
281 * [12] THERM_TRIP_EN - Thermal tripping enable
282 * [11:8] BUF_SLOPE_SEL - Gain of amplifier
283 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
285 writel(data->slope, ®->tmu_control);
287 writel(INTCLEARALL, ®->intclear);
289 /* TMU core enable */
290 con = readl(®->tmu_control);
291 con |= THERM_TRIP_EN | CORE_EN;
293 writel(con, ®->tmu_control);
295 /* Enable HW thermal trip */
296 set_hw_thermal_trip();
298 /* LEV1 LEV2 interrupt enable */
299 writel(INTEN_RISE1 | INTEN_RISE2, ®->inten);
303 * Initialize TMU device
305 * @param blob FDT blob
306 * @return int value, 0 for success
308 int tmu_init(const void *blob)
310 gbl_info.tmu_state = TMU_STATUS_INIT;
311 if (get_tmu_fdt_values(&gbl_info, blob) < 0)
314 tmu_setup_parameters(&gbl_info);
315 gbl_info.tmu_state = TMU_STATUS_NORMAL;
318 return gbl_info.tmu_state;