2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
4 * Akshay Saraswat <akshay.s@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,
26 #include <asm/arch/tmu.h>
27 #include <asm/arch/power.h>
29 #define TRIMINFO_RELOAD 1
31 #define THERM_TRIP_EN (1 << 12)
34 #define INTEN_RISE1 (1 << 4)
35 #define INTEN_RISE2 (1 << 8)
36 #define INTEN_FALL0 (1 << 16)
37 #define INTEN_FALL1 (1 << 20)
38 #define INTEN_FALL2 (1 << 24)
40 #define TRIM_INFO_MASK 0xff
42 #define INTCLEAR_RISE0 1
43 #define INTCLEAR_RISE1 (1 << 4)
44 #define INTCLEAR_RISE2 (1 << 8)
45 #define INTCLEAR_FALL0 (1 << 16)
46 #define INTCLEAR_FALL1 (1 << 20)
47 #define INTCLEAR_FALL2 (1 << 24)
48 #define INTCLEARALL (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \
49 INTCLEAR_RISE2 | INTCLEAR_FALL0 | \
50 INTCLEAR_FALL1 | INTCLEAR_FALL2)
52 /* Tmeperature threshold values for various thermal events */
53 struct temperature_params {
54 /* minimum value in temperature code range */
56 /* maximum value in temperature code range */
58 /* temperature threshold to start warning */
59 unsigned start_warning;
60 /* temperature threshold CPU tripping */
61 unsigned start_tripping;
62 /* temperature threshold for HW tripping */
63 unsigned hardware_tripping;
66 /* Pre-defined values and thresholds for calibration of current temperature */
68 /* pre-defined temperature thresholds */
69 struct temperature_params ts;
70 /* pre-defined efuse range minimum value */
71 unsigned efuse_min_value;
72 /* pre-defined efuse value for temperature calibration */
74 /* pre-defined efuse range maximum value */
75 unsigned efuse_max_value;
76 /* current temperature sensing slope */
80 /* TMU device specific details and status */
82 /* base Address for the TMU */
83 struct exynos5_tmu_reg *tmu_base;
84 /* mux Address for the TMU */
86 /* pre-defined values for calibration and thresholds */
88 /* value required for triminfo_25 calibration */
90 /* value required for triminfo_85 calibration */
92 /* Value for measured data calibration */
94 /* enum value indicating status of the TMU */
98 /* Global struct tmu_info variable to store init values */
99 static struct tmu_info gbl_info;
102 * Get current temperature code from register,
103 * then calculate and calibrate it's value
106 * Return: current temperature of the chip as sensed by TMU
108 static int get_cur_temp(struct tmu_info *info)
110 struct exynos5_tmu_reg *reg = info->tmu_base;
115 * Temperature code range between min 25 and max 125.
116 * May run more than once for first call as initial sensing
117 * has not yet happened.
119 if (info->tmu_state == TMU_STATUS_NORMAL) {
120 start = get_timer(0);
122 cur_temp = readl(®->current_temp) & 0xff;
123 } while ((cur_temp == 0) || (get_timer(start) > 100));
129 /* Calibrate current temperature */
130 cur_temp = cur_temp - info->te1 + info->dc_value;
136 * Monitors status of the TMU device and exynos temperature
138 * @param temp pointer to the current temperature value
139 * Return: enum tmu_status_t value, code indicating event to execute
141 enum tmu_status_t tmu_monitor(int *temp)
144 struct tmu_data *data = &gbl_info.data;
146 if (gbl_info.tmu_state == TMU_STATUS_INIT)
147 return TMU_STATUS_INIT;
149 /* Read current temperature of the SOC */
150 cur_temp = get_cur_temp(&gbl_info);
157 /* Temperature code lies between min 25 and max 125 */
158 if ((cur_temp >= data->ts.start_tripping) &&
159 (cur_temp <= data->ts.max_val))
160 return TMU_STATUS_TRIPPED;
162 if (cur_temp >= data->ts.start_warning)
163 return TMU_STATUS_WARNING;
165 if ((cur_temp < data->ts.start_warning) &&
166 (cur_temp >= data->ts.min_val))
167 return TMU_STATUS_NORMAL;
170 /* Temperature code does not lie between min 25 and max 125 */
171 gbl_info.tmu_state = TMU_STATUS_INIT;
172 debug("EXYNOS_TMU: Thermal reading failed\n");
173 return TMU_STATUS_INIT;
177 * Get TMU specific pre-defined values from FDT
179 * @param info pointer to the tmu_info struct
180 * @param blob FDT blob
181 * Return: int value, 0 for success
183 static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
185 #if CONFIG_IS_ENABLED(OF_CONTROL)
190 /* Get the node from FDT for TMU */
191 node = fdtdec_next_compatible(blob, 0,
192 COMPAT_SAMSUNG_EXYNOS_TMU);
194 debug("EXYNOS_TMU: No node for tmu in device tree\n");
199 * Get the pre-defined TMU specific values from FDT.
200 * All of these are expected to be correct otherwise
201 * miscalculation of register values in tmu_setup_parameters
202 * may result in misleading current temperature.
204 addr = fdtdec_get_addr(blob, node, "reg");
205 if (addr == FDT_ADDR_T_NONE) {
206 debug("%s: Missing tmu-base\n", __func__);
209 info->tmu_base = (struct exynos5_tmu_reg *)addr;
211 /* Optional field. */
212 info->tmu_mux = fdtdec_get_int(blob,
213 node, "samsung,mux", -1);
214 /* Take default value as per the user manual b(110) */
215 if (info->tmu_mux == -1)
218 info->data.ts.min_val = fdtdec_get_int(blob,
219 node, "samsung,min-temp", -1);
220 error |= (info->data.ts.min_val == -1);
221 info->data.ts.max_val = fdtdec_get_int(blob,
222 node, "samsung,max-temp", -1);
223 error |= (info->data.ts.max_val == -1);
224 info->data.ts.start_warning = fdtdec_get_int(blob,
225 node, "samsung,start-warning", -1);
226 error |= (info->data.ts.start_warning == -1);
227 info->data.ts.start_tripping = fdtdec_get_int(blob,
228 node, "samsung,start-tripping", -1);
229 error |= (info->data.ts.start_tripping == -1);
230 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
231 node, "samsung,hw-tripping", -1);
232 error |= (info->data.ts.hardware_tripping == -1);
233 info->data.efuse_min_value = fdtdec_get_int(blob,
234 node, "samsung,efuse-min-value", -1);
235 error |= (info->data.efuse_min_value == -1);
236 info->data.efuse_value = fdtdec_get_int(blob,
237 node, "samsung,efuse-value", -1);
238 error |= (info->data.efuse_value == -1);
239 info->data.efuse_max_value = fdtdec_get_int(blob,
240 node, "samsung,efuse-max-value", -1);
241 error |= (info->data.efuse_max_value == -1);
242 info->data.slope = fdtdec_get_int(blob,
243 node, "samsung,slope", -1);
244 error |= (info->data.slope == -1);
245 info->dc_value = fdtdec_get_int(blob,
246 node, "samsung,dc-value", -1);
247 error |= (info->dc_value == -1);
250 debug("fail to get tmu node properties\n");
254 /* Non DT support may never be added. Just in case */
262 * Calibrate and calculate threshold values and
263 * enable interrupt levels
265 * @param info pointer to the tmu_info struct
267 static void tmu_setup_parameters(struct tmu_info *info)
269 unsigned te_code, con;
270 unsigned warning_code, trip_code, hwtrip_code;
271 unsigned cooling_temp;
272 unsigned rising_value;
273 struct tmu_data *data = &info->data;
274 struct exynos5_tmu_reg *reg = info->tmu_base;
276 /* Must reload for reading efuse value from triminfo register */
277 writel(TRIMINFO_RELOAD, ®->triminfo_control);
279 /* Get the compensation parameter */
280 te_code = readl(®->triminfo);
281 info->te1 = te_code & TRIM_INFO_MASK;
282 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
284 if ((data->efuse_min_value > info->te1) ||
285 (info->te1 > data->efuse_max_value)
287 info->te1 = data->efuse_value;
289 /* Get RISING & FALLING Threshold value */
290 warning_code = data->ts.start_warning
291 + info->te1 - info->dc_value;
292 trip_code = data->ts.start_tripping
293 + info->te1 - info->dc_value;
294 hwtrip_code = data->ts.hardware_tripping
295 + info->te1 - info->dc_value;
299 rising_value = ((warning_code << 8) |
301 (hwtrip_code << 24));
303 /* Set interrupt level */
304 writel(rising_value, ®->threshold_temp_rise);
305 writel(cooling_temp, ®->threshold_temp_fall);
308 * Init TMU control tuning parameters
309 * [28:24] VREF - Voltage reference
310 * [15:13] THERM_TRIP_MODE - Tripping mode
311 * [12] THERM_TRIP_EN - Thermal tripping enable
312 * [11:8] BUF_SLOPE_SEL - Gain of amplifier
313 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
315 writel(data->slope, ®->tmu_control);
317 writel(INTCLEARALL, ®->intclear);
319 /* TMU core enable */
320 con = readl(®->tmu_control);
321 con |= THERM_TRIP_EN | CORE_EN | (info->tmu_mux << 20);
323 writel(con, ®->tmu_control);
325 /* Enable HW thermal trip */
326 set_hw_thermal_trip();
328 /* LEV1 LEV2 interrupt enable */
329 writel(INTEN_RISE1 | INTEN_RISE2, ®->inten);
333 * Initialize TMU device
335 * @param blob FDT blob
336 * Return: int value, 0 for success
338 int tmu_init(const void *blob)
340 gbl_info.tmu_state = TMU_STATUS_INIT;
341 if (get_tmu_fdt_values(&gbl_info, blob) < 0)
344 tmu_setup_parameters(&gbl_info);
345 gbl_info.tmu_state = TMU_STATUS_NORMAL;
347 return gbl_info.tmu_state;