xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision 56adb9efeb8115d2a3a807d84a0d037398588911)
1 /*
2  * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit)
3  *
4  *  Copyright (C) 2011 Samsung Electronics
5  *  Donggeun Kim <dg77.kim@samsung.com>
6  *  Amit Daniel Kachhap <amit.kachhap@linaro.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 
24 #include <linux/clk.h>
25 #include <linux/io.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/of.h>
29 #include <linux/of_address.h>
30 #include <linux/of_irq.h>
31 #include <linux/platform_device.h>
32 #include <linux/regulator/consumer.h>
33 
34 #include "exynos_thermal_common.h"
35 #include "exynos_tmu.h"
36 #include "exynos_tmu_data.h"
37 
38 /**
39  * struct exynos_tmu_data : A structure to hold the private data of the TMU
40 	driver
41  * @id: identifier of the one instance of the TMU controller.
42  * @pdata: pointer to the tmu platform/configuration data
43  * @base: base address of the single instance of the TMU controller.
44  * @base_second: base address of the common registers of the TMU controller.
45  * @irq: irq number of the TMU controller.
46  * @soc: id of the SOC type.
47  * @irq_work: pointer to the irq work structure.
48  * @lock: lock to implement synchronization.
49  * @clk: pointer to the clock structure.
50  * @clk_sec: pointer to the clock structure for accessing the base_second.
51  * @temp_error1: fused value of the first point trim.
52  * @temp_error2: fused value of the second point trim.
53  * @regulator: pointer to the TMU regulator structure.
54  * @reg_conf: pointer to structure to register with core thermal.
55  * @tmu_initialize: SoC specific TMU initialization method
56  * @tmu_control: SoC specific TMU control method
57  * @tmu_read: SoC specific TMU temperature read method
58  * @tmu_set_emulation: SoC specific TMU emulation setting method
59  * @tmu_clear_irqs: SoC specific TMU interrupts clearing method
60  */
61 struct exynos_tmu_data {
62 	int id;
63 	struct exynos_tmu_platform_data *pdata;
64 	void __iomem *base;
65 	void __iomem *base_second;
66 	int irq;
67 	enum soc_type soc;
68 	struct work_struct irq_work;
69 	struct mutex lock;
70 	struct clk *clk, *clk_sec;
71 	u8 temp_error1, temp_error2;
72 	struct regulator *regulator;
73 	struct thermal_sensor_conf *reg_conf;
74 	int (*tmu_initialize)(struct platform_device *pdev);
75 	void (*tmu_control)(struct platform_device *pdev, bool on);
76 	int (*tmu_read)(struct exynos_tmu_data *data);
77 	void (*tmu_set_emulation)(struct exynos_tmu_data *data,
78 				  unsigned long temp);
79 	void (*tmu_clear_irqs)(struct exynos_tmu_data *data);
80 };
81 
82 /*
83  * TMU treats temperature as a mapped temperature code.
84  * The temperature is converted differently depending on the calibration type.
85  */
86 static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
87 {
88 	struct exynos_tmu_platform_data *pdata = data->pdata;
89 	int temp_code;
90 
91 	switch (pdata->cal_type) {
92 	case TYPE_TWO_POINT_TRIMMING:
93 		temp_code = (temp - pdata->first_point_trim) *
94 			(data->temp_error2 - data->temp_error1) /
95 			(pdata->second_point_trim - pdata->first_point_trim) +
96 			data->temp_error1;
97 		break;
98 	case TYPE_ONE_POINT_TRIMMING:
99 		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
100 		break;
101 	default:
102 		temp_code = temp + pdata->default_temp_offset;
103 		break;
104 	}
105 
106 	return temp_code;
107 }
108 
109 /*
110  * Calculate a temperature value from a temperature code.
111  * The unit of the temperature is degree Celsius.
112  */
113 static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
114 {
115 	struct exynos_tmu_platform_data *pdata = data->pdata;
116 	int temp;
117 
118 	switch (pdata->cal_type) {
119 	case TYPE_TWO_POINT_TRIMMING:
120 		temp = (temp_code - data->temp_error1) *
121 			(pdata->second_point_trim - pdata->first_point_trim) /
122 			(data->temp_error2 - data->temp_error1) +
123 			pdata->first_point_trim;
124 		break;
125 	case TYPE_ONE_POINT_TRIMMING:
126 		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
127 		break;
128 	default:
129 		temp = temp_code - pdata->default_temp_offset;
130 		break;
131 	}
132 
133 	return temp;
134 }
135 
136 static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
137 {
138 	struct exynos_tmu_platform_data *pdata = data->pdata;
139 
140 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
141 	data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
142 				EXYNOS_TMU_TEMP_MASK);
143 
144 	if (!data->temp_error1 ||
145 		(pdata->min_efuse_value > data->temp_error1) ||
146 		(data->temp_error1 > pdata->max_efuse_value))
147 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
148 
149 	if (!data->temp_error2)
150 		data->temp_error2 =
151 			(pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
152 			EXYNOS_TMU_TEMP_MASK;
153 }
154 
155 static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling)
156 {
157 	struct exynos_tmu_platform_data *pdata = data->pdata;
158 	int i;
159 
160 	for (i = 0; i < pdata->non_hw_trigger_levels; i++) {
161 		u8 temp = pdata->trigger_levels[i];
162 
163 		if (falling)
164 			temp -= pdata->threshold_falling;
165 		else
166 			threshold &= ~(0xff << 8 * i);
167 
168 		threshold |= temp_to_code(data, temp) << 8 * i;
169 	}
170 
171 	return threshold;
172 }
173 
174 static int exynos_tmu_initialize(struct platform_device *pdev)
175 {
176 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
177 	int ret;
178 
179 	mutex_lock(&data->lock);
180 	clk_enable(data->clk);
181 	if (!IS_ERR(data->clk_sec))
182 		clk_enable(data->clk_sec);
183 	ret = data->tmu_initialize(pdev);
184 	clk_disable(data->clk);
185 	mutex_unlock(&data->lock);
186 	if (!IS_ERR(data->clk_sec))
187 		clk_disable(data->clk_sec);
188 
189 	return ret;
190 }
191 
192 static u32 get_con_reg(struct exynos_tmu_data *data, u32 con)
193 {
194 	struct exynos_tmu_platform_data *pdata = data->pdata;
195 
196 	if (pdata->test_mux)
197 		con |= (pdata->test_mux << EXYNOS4412_MUX_ADDR_SHIFT);
198 
199 	con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
200 	con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
201 
202 	con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
203 	con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
204 
205 	if (pdata->noise_cancel_mode) {
206 		con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
207 		con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT);
208 	}
209 
210 	return con;
211 }
212 
213 static void exynos_tmu_control(struct platform_device *pdev, bool on)
214 {
215 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
216 
217 	mutex_lock(&data->lock);
218 	clk_enable(data->clk);
219 	data->tmu_control(pdev, on);
220 	clk_disable(data->clk);
221 	mutex_unlock(&data->lock);
222 }
223 
224 static int exynos4210_tmu_initialize(struct platform_device *pdev)
225 {
226 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
227 	struct exynos_tmu_platform_data *pdata = data->pdata;
228 	unsigned int status;
229 	int ret = 0, threshold_code, i;
230 
231 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
232 	if (!status) {
233 		ret = -EBUSY;
234 		goto out;
235 	}
236 
237 	sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO));
238 
239 	/* Write temperature code for threshold */
240 	threshold_code = temp_to_code(data, pdata->threshold);
241 	writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
242 
243 	for (i = 0; i < pdata->non_hw_trigger_levels; i++)
244 		writeb(pdata->trigger_levels[i], data->base +
245 		       EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
246 
247 	data->tmu_clear_irqs(data);
248 out:
249 	return ret;
250 }
251 
252 static int exynos4412_tmu_initialize(struct platform_device *pdev)
253 {
254 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
255 	struct exynos_tmu_platform_data *pdata = data->pdata;
256 	unsigned int status, trim_info, con, ctrl, rising_threshold;
257 	int ret = 0, threshold_code, i;
258 
259 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
260 	if (!status) {
261 		ret = -EBUSY;
262 		goto out;
263 	}
264 
265 	if (data->soc == SOC_ARCH_EXYNOS3250 ||
266 	    data->soc == SOC_ARCH_EXYNOS4412 ||
267 	    data->soc == SOC_ARCH_EXYNOS5250) {
268 		if (data->soc == SOC_ARCH_EXYNOS3250) {
269 			ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
270 			ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
271 			writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
272 		}
273 		ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
274 		ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
275 		writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
276 	}
277 
278 	/* On exynos5420 the triminfo register is in the shared space */
279 	if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
280 		trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO);
281 	else
282 		trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
283 
284 	sanitize_temp_error(data, trim_info);
285 
286 	/* Write temperature code for rising and falling threshold */
287 	rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE);
288 	rising_threshold = get_th_reg(data, rising_threshold, false);
289 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
290 	writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL);
291 
292 	data->tmu_clear_irqs(data);
293 
294 	/* if last threshold limit is also present */
295 	i = pdata->max_trigger_level - 1;
296 	if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) {
297 		threshold_code = temp_to_code(data, pdata->trigger_levels[i]);
298 		/* 1-4 level to be assigned in th0 reg */
299 		rising_threshold &= ~(0xff << 8 * i);
300 		rising_threshold |= threshold_code << 8 * i;
301 		writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
302 		con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
303 		con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
304 		writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
305 	}
306 out:
307 	return ret;
308 }
309 
310 static int exynos5440_tmu_initialize(struct platform_device *pdev)
311 {
312 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
313 	struct exynos_tmu_platform_data *pdata = data->pdata;
314 	unsigned int trim_info = 0, con, rising_threshold;
315 	int ret = 0, threshold_code, i;
316 
317 	/*
318 	 * For exynos5440 soc triminfo value is swapped between TMU0 and
319 	 * TMU2, so the below logic is needed.
320 	 */
321 	switch (data->id) {
322 	case 0:
323 		trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET +
324 				 EXYNOS5440_TMU_S0_7_TRIM);
325 		break;
326 	case 1:
327 		trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM);
328 		break;
329 	case 2:
330 		trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET +
331 				  EXYNOS5440_TMU_S0_7_TRIM);
332 	}
333 	sanitize_temp_error(data, trim_info);
334 
335 	/* Write temperature code for rising and falling threshold */
336 	rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0);
337 	rising_threshold = get_th_reg(data, rising_threshold, false);
338 	writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0);
339 	writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1);
340 
341 	data->tmu_clear_irqs(data);
342 
343 	/* if last threshold limit is also present */
344 	i = pdata->max_trigger_level - 1;
345 	if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) {
346 		threshold_code = temp_to_code(data, pdata->trigger_levels[i]);
347 		/* 5th level to be assigned in th2 reg */
348 		rising_threshold =
349 			threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT;
350 		writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2);
351 		con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL);
352 		con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
353 		writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
354 	}
355 	/* Clear the PMIN in the common TMU register */
356 	if (!data->id)
357 		writel(0, data->base_second + EXYNOS5440_TMU_PMIN);
358 	return ret;
359 }
360 
361 static void exynos4210_tmu_control(struct platform_device *pdev, bool on)
362 {
363 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
364 	struct exynos_tmu_platform_data *pdata = data->pdata;
365 	unsigned int con, interrupt_en;
366 
367 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
368 
369 	if (on) {
370 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
371 		interrupt_en =
372 			pdata->trigger_enable[3] << EXYNOS_TMU_INTEN_RISE3_SHIFT |
373 			pdata->trigger_enable[2] << EXYNOS_TMU_INTEN_RISE2_SHIFT |
374 			pdata->trigger_enable[1] << EXYNOS_TMU_INTEN_RISE1_SHIFT |
375 			pdata->trigger_enable[0] << EXYNOS_TMU_INTEN_RISE0_SHIFT;
376 		if (data->soc != SOC_ARCH_EXYNOS4210)
377 			interrupt_en |=
378 				interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
379 	} else {
380 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
381 		interrupt_en = 0; /* Disable all interrupts */
382 	}
383 	writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
384 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
385 }
386 
387 static void exynos5440_tmu_control(struct platform_device *pdev, bool on)
388 {
389 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
390 	struct exynos_tmu_platform_data *pdata = data->pdata;
391 	unsigned int con, interrupt_en;
392 
393 	con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL));
394 
395 	if (on) {
396 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
397 		interrupt_en =
398 			pdata->trigger_enable[3] << EXYNOS5440_TMU_INTEN_RISE3_SHIFT |
399 			pdata->trigger_enable[2] << EXYNOS5440_TMU_INTEN_RISE2_SHIFT |
400 			pdata->trigger_enable[1] << EXYNOS5440_TMU_INTEN_RISE1_SHIFT |
401 			pdata->trigger_enable[0] << EXYNOS5440_TMU_INTEN_RISE0_SHIFT;
402 		interrupt_en |= interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT;
403 	} else {
404 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
405 		interrupt_en = 0; /* Disable all interrupts */
406 	}
407 	writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN);
408 	writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
409 }
410 
411 static int exynos_tmu_read(struct exynos_tmu_data *data)
412 {
413 	int ret;
414 
415 	mutex_lock(&data->lock);
416 	clk_enable(data->clk);
417 	ret = data->tmu_read(data);
418 	if (ret >= 0)
419 		ret = code_to_temp(data, ret);
420 	clk_disable(data->clk);
421 	mutex_unlock(&data->lock);
422 
423 	return ret;
424 }
425 
426 #ifdef CONFIG_THERMAL_EMULATION
427 static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val,
428 			    unsigned long temp)
429 {
430 	if (temp) {
431 		temp /= MCELSIUS;
432 
433 		if (data->soc != SOC_ARCH_EXYNOS5440) {
434 			val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
435 			val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
436 		}
437 		val &= ~(EXYNOS_EMUL_DATA_MASK << EXYNOS_EMUL_DATA_SHIFT);
438 		val |= (temp_to_code(data, temp) << EXYNOS_EMUL_DATA_SHIFT) |
439 			EXYNOS_EMUL_ENABLE;
440 	} else {
441 		val &= ~EXYNOS_EMUL_ENABLE;
442 	}
443 
444 	return val;
445 }
446 
447 static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data,
448 					 unsigned long temp)
449 {
450 	unsigned int val;
451 	u32 emul_con;
452 
453 	if (data->soc == SOC_ARCH_EXYNOS5260)
454 		emul_con = EXYNOS5260_EMUL_CON;
455 	else
456 		emul_con = EXYNOS_EMUL_CON;
457 
458 	val = readl(data->base + emul_con);
459 	val = get_emul_con_reg(data, val, temp);
460 	writel(val, data->base + emul_con);
461 }
462 
463 static void exynos5440_tmu_set_emulation(struct exynos_tmu_data *data,
464 					 unsigned long temp)
465 {
466 	unsigned int val;
467 
468 	val = readl(data->base + EXYNOS5440_TMU_S0_7_DEBUG);
469 	val = get_emul_con_reg(data, val, temp);
470 	writel(val, data->base + EXYNOS5440_TMU_S0_7_DEBUG);
471 }
472 
473 static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
474 {
475 	struct exynos_tmu_data *data = drv_data;
476 	int ret = -EINVAL;
477 
478 	if (data->soc == SOC_ARCH_EXYNOS4210)
479 		goto out;
480 
481 	if (temp && temp < MCELSIUS)
482 		goto out;
483 
484 	mutex_lock(&data->lock);
485 	clk_enable(data->clk);
486 	data->tmu_set_emulation(data, temp);
487 	clk_disable(data->clk);
488 	mutex_unlock(&data->lock);
489 	return 0;
490 out:
491 	return ret;
492 }
493 #else
494 #define exynos4412_tmu_set_emulation NULL
495 #define exynos5440_tmu_set_emulation NULL
496 static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
497 	{ return -EINVAL; }
498 #endif/*CONFIG_THERMAL_EMULATION*/
499 
500 static int exynos4210_tmu_read(struct exynos_tmu_data *data)
501 {
502 	int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
503 
504 	/* "temp_code" should range between 75 and 175 */
505 	return (ret < 75 || ret > 175) ? -ENODATA : ret;
506 }
507 
508 static int exynos4412_tmu_read(struct exynos_tmu_data *data)
509 {
510 	return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
511 }
512 
513 static int exynos5440_tmu_read(struct exynos_tmu_data *data)
514 {
515 	return readb(data->base + EXYNOS5440_TMU_S0_7_TEMP);
516 }
517 
518 static void exynos_tmu_work(struct work_struct *work)
519 {
520 	struct exynos_tmu_data *data = container_of(work,
521 			struct exynos_tmu_data, irq_work);
522 	unsigned int val_type;
523 
524 	if (!IS_ERR(data->clk_sec))
525 		clk_enable(data->clk_sec);
526 	/* Find which sensor generated this interrupt */
527 	if (data->soc == SOC_ARCH_EXYNOS5440) {
528 		val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS);
529 		if (!((val_type >> data->id) & 0x1))
530 			goto out;
531 	}
532 	if (!IS_ERR(data->clk_sec))
533 		clk_disable(data->clk_sec);
534 
535 	exynos_report_trigger(data->reg_conf);
536 	mutex_lock(&data->lock);
537 	clk_enable(data->clk);
538 
539 	/* TODO: take action based on particular interrupt */
540 	data->tmu_clear_irqs(data);
541 
542 	clk_disable(data->clk);
543 	mutex_unlock(&data->lock);
544 out:
545 	enable_irq(data->irq);
546 }
547 
548 static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data)
549 {
550 	unsigned int val_irq;
551 	u32 tmu_intstat, tmu_intclear;
552 
553 	if (data->soc == SOC_ARCH_EXYNOS5260) {
554 		tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT;
555 		tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR;
556 	} else {
557 		tmu_intstat = EXYNOS_TMU_REG_INTSTAT;
558 		tmu_intclear = EXYNOS_TMU_REG_INTCLEAR;
559 	}
560 
561 	val_irq = readl(data->base + tmu_intstat);
562 	/*
563 	 * Clear the interrupts.  Please note that the documentation for
564 	 * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
565 	 * states that INTCLEAR register has a different placing of bits
566 	 * responsible for FALL IRQs than INTSTAT register.  Exynos5420
567 	 * and Exynos5440 documentation is correct (Exynos4210 doesn't
568 	 * support FALL IRQs at all).
569 	 */
570 	writel(val_irq, data->base + tmu_intclear);
571 }
572 
573 static void exynos5440_tmu_clear_irqs(struct exynos_tmu_data *data)
574 {
575 	unsigned int val_irq;
576 
577 	val_irq = readl(data->base + EXYNOS5440_TMU_S0_7_IRQ);
578 	/* clear the interrupts */
579 	writel(val_irq, data->base + EXYNOS5440_TMU_S0_7_IRQ);
580 }
581 
582 static irqreturn_t exynos_tmu_irq(int irq, void *id)
583 {
584 	struct exynos_tmu_data *data = id;
585 
586 	disable_irq_nosync(irq);
587 	schedule_work(&data->irq_work);
588 
589 	return IRQ_HANDLED;
590 }
591 
592 static const struct of_device_id exynos_tmu_match[] = {
593 	{
594 		.compatible = "samsung,exynos3250-tmu",
595 		.data = (void *)EXYNOS3250_TMU_DRV_DATA,
596 	},
597 	{
598 		.compatible = "samsung,exynos4210-tmu",
599 		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
600 	},
601 	{
602 		.compatible = "samsung,exynos4412-tmu",
603 		.data = (void *)EXYNOS4412_TMU_DRV_DATA,
604 	},
605 	{
606 		.compatible = "samsung,exynos5250-tmu",
607 		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
608 	},
609 	{
610 		.compatible = "samsung,exynos5260-tmu",
611 		.data = (void *)EXYNOS5260_TMU_DRV_DATA,
612 	},
613 	{
614 		.compatible = "samsung,exynos5420-tmu",
615 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
616 	},
617 	{
618 		.compatible = "samsung,exynos5420-tmu-ext-triminfo",
619 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
620 	},
621 	{
622 		.compatible = "samsung,exynos5440-tmu",
623 		.data = (void *)EXYNOS5440_TMU_DRV_DATA,
624 	},
625 	{},
626 };
627 MODULE_DEVICE_TABLE(of, exynos_tmu_match);
628 
629 static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
630 			struct platform_device *pdev, int id)
631 {
632 	struct  exynos_tmu_init_data *data_table;
633 	struct exynos_tmu_platform_data *tmu_data;
634 	const struct of_device_id *match;
635 
636 	match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
637 	if (!match)
638 		return NULL;
639 	data_table = (struct exynos_tmu_init_data *) match->data;
640 	if (!data_table || id >= data_table->tmu_count)
641 		return NULL;
642 	tmu_data = data_table->tmu_data;
643 	return (struct exynos_tmu_platform_data *) (tmu_data + id);
644 }
645 
646 static int exynos_map_dt_data(struct platform_device *pdev)
647 {
648 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
649 	struct exynos_tmu_platform_data *pdata;
650 	struct resource res;
651 	int ret;
652 
653 	if (!data || !pdev->dev.of_node)
654 		return -ENODEV;
655 
656 	/*
657 	 * Try enabling the regulator if found
658 	 * TODO: Add regulator as an SOC feature, so that regulator enable
659 	 * is a compulsory call.
660 	 */
661 	data->regulator = devm_regulator_get(&pdev->dev, "vtmu");
662 	if (!IS_ERR(data->regulator)) {
663 		ret = regulator_enable(data->regulator);
664 		if (ret) {
665 			dev_err(&pdev->dev, "failed to enable vtmu\n");
666 			return ret;
667 		}
668 	} else {
669 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
670 	}
671 
672 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
673 	if (data->id < 0)
674 		data->id = 0;
675 
676 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
677 	if (data->irq <= 0) {
678 		dev_err(&pdev->dev, "failed to get IRQ\n");
679 		return -ENODEV;
680 	}
681 
682 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
683 		dev_err(&pdev->dev, "failed to get Resource 0\n");
684 		return -ENODEV;
685 	}
686 
687 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
688 	if (!data->base) {
689 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
690 		return -EADDRNOTAVAIL;
691 	}
692 
693 	pdata = exynos_get_driver_data(pdev, data->id);
694 	if (!pdata) {
695 		dev_err(&pdev->dev, "No platform init data supplied.\n");
696 		return -ENODEV;
697 	}
698 
699 	data->pdata = pdata;
700 	data->soc = pdata->type;
701 
702 	switch (data->soc) {
703 	case SOC_ARCH_EXYNOS4210:
704 		data->tmu_initialize = exynos4210_tmu_initialize;
705 		data->tmu_control = exynos4210_tmu_control;
706 		data->tmu_read = exynos4210_tmu_read;
707 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
708 		break;
709 	case SOC_ARCH_EXYNOS3250:
710 	case SOC_ARCH_EXYNOS4412:
711 	case SOC_ARCH_EXYNOS5250:
712 	case SOC_ARCH_EXYNOS5260:
713 	case SOC_ARCH_EXYNOS5420:
714 	case SOC_ARCH_EXYNOS5420_TRIMINFO:
715 		data->tmu_initialize = exynos4412_tmu_initialize;
716 		data->tmu_control = exynos4210_tmu_control;
717 		data->tmu_read = exynos4412_tmu_read;
718 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
719 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
720 		break;
721 	case SOC_ARCH_EXYNOS5440:
722 		data->tmu_initialize = exynos5440_tmu_initialize;
723 		data->tmu_control = exynos5440_tmu_control;
724 		data->tmu_read = exynos5440_tmu_read;
725 		data->tmu_set_emulation = exynos5440_tmu_set_emulation;
726 		data->tmu_clear_irqs = exynos5440_tmu_clear_irqs;
727 		break;
728 	default:
729 		dev_err(&pdev->dev, "Platform not supported\n");
730 		return -EINVAL;
731 	}
732 
733 	/*
734 	 * Check if the TMU shares some registers and then try to map the
735 	 * memory of common registers.
736 	 */
737 	if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO &&
738 	    data->soc != SOC_ARCH_EXYNOS5440)
739 		return 0;
740 
741 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
742 		dev_err(&pdev->dev, "failed to get Resource 1\n");
743 		return -ENODEV;
744 	}
745 
746 	data->base_second = devm_ioremap(&pdev->dev, res.start,
747 					resource_size(&res));
748 	if (!data->base_second) {
749 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
750 		return -ENOMEM;
751 	}
752 
753 	return 0;
754 }
755 
756 static int exynos_tmu_probe(struct platform_device *pdev)
757 {
758 	struct exynos_tmu_data *data;
759 	struct exynos_tmu_platform_data *pdata;
760 	struct thermal_sensor_conf *sensor_conf;
761 	int ret, i;
762 
763 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
764 					GFP_KERNEL);
765 	if (!data)
766 		return -ENOMEM;
767 
768 	platform_set_drvdata(pdev, data);
769 	mutex_init(&data->lock);
770 
771 	ret = exynos_map_dt_data(pdev);
772 	if (ret)
773 		return ret;
774 
775 	pdata = data->pdata;
776 
777 	INIT_WORK(&data->irq_work, exynos_tmu_work);
778 
779 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
780 	if (IS_ERR(data->clk)) {
781 		dev_err(&pdev->dev, "Failed to get clock\n");
782 		return  PTR_ERR(data->clk);
783 	}
784 
785 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
786 	if (IS_ERR(data->clk_sec)) {
787 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
788 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
789 			return PTR_ERR(data->clk_sec);
790 		}
791 	} else {
792 		ret = clk_prepare(data->clk_sec);
793 		if (ret) {
794 			dev_err(&pdev->dev, "Failed to get clock\n");
795 			return ret;
796 		}
797 	}
798 
799 	ret = clk_prepare(data->clk);
800 	if (ret) {
801 		dev_err(&pdev->dev, "Failed to get clock\n");
802 		goto err_clk_sec;
803 	}
804 
805 	ret = exynos_tmu_initialize(pdev);
806 	if (ret) {
807 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
808 		goto err_clk;
809 	}
810 
811 	exynos_tmu_control(pdev, true);
812 
813 	/* Allocate a structure to register with the exynos core thermal */
814 	sensor_conf = devm_kzalloc(&pdev->dev,
815 				sizeof(struct thermal_sensor_conf), GFP_KERNEL);
816 	if (!sensor_conf) {
817 		ret = -ENOMEM;
818 		goto err_clk;
819 	}
820 	sprintf(sensor_conf->name, "therm_zone%d", data->id);
821 	sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
822 	sensor_conf->write_emul_temp =
823 		(int (*)(void *, unsigned long))exynos_tmu_set_emulation;
824 	sensor_conf->driver_data = data;
825 	sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
826 			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
827 			pdata->trigger_enable[3];
828 
829 	for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
830 		sensor_conf->trip_data.trip_val[i] =
831 			pdata->threshold + pdata->trigger_levels[i];
832 		sensor_conf->trip_data.trip_type[i] =
833 					pdata->trigger_type[i];
834 	}
835 
836 	sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
837 
838 	sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
839 	for (i = 0; i < pdata->freq_tab_count; i++) {
840 		sensor_conf->cooling_data.freq_data[i].freq_clip_max =
841 					pdata->freq_tab[i].freq_clip_max;
842 		sensor_conf->cooling_data.freq_data[i].temp_level =
843 					pdata->freq_tab[i].temp_level;
844 	}
845 	sensor_conf->dev = &pdev->dev;
846 	/* Register the sensor with thermal management interface */
847 	ret = exynos_register_thermal(sensor_conf);
848 	if (ret) {
849 		dev_err(&pdev->dev, "Failed to register thermal interface\n");
850 		goto err_clk;
851 	}
852 	data->reg_conf = sensor_conf;
853 
854 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
855 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
856 	if (ret) {
857 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
858 		goto err_clk;
859 	}
860 
861 	return 0;
862 err_clk:
863 	clk_unprepare(data->clk);
864 err_clk_sec:
865 	if (!IS_ERR(data->clk_sec))
866 		clk_unprepare(data->clk_sec);
867 	return ret;
868 }
869 
870 static int exynos_tmu_remove(struct platform_device *pdev)
871 {
872 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
873 
874 	exynos_unregister_thermal(data->reg_conf);
875 
876 	exynos_tmu_control(pdev, false);
877 
878 	clk_unprepare(data->clk);
879 	if (!IS_ERR(data->clk_sec))
880 		clk_unprepare(data->clk_sec);
881 
882 	if (!IS_ERR(data->regulator))
883 		regulator_disable(data->regulator);
884 
885 	return 0;
886 }
887 
888 #ifdef CONFIG_PM_SLEEP
889 static int exynos_tmu_suspend(struct device *dev)
890 {
891 	exynos_tmu_control(to_platform_device(dev), false);
892 
893 	return 0;
894 }
895 
896 static int exynos_tmu_resume(struct device *dev)
897 {
898 	struct platform_device *pdev = to_platform_device(dev);
899 
900 	exynos_tmu_initialize(pdev);
901 	exynos_tmu_control(pdev, true);
902 
903 	return 0;
904 }
905 
906 static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
907 			 exynos_tmu_suspend, exynos_tmu_resume);
908 #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
909 #else
910 #define EXYNOS_TMU_PM	NULL
911 #endif
912 
913 static struct platform_driver exynos_tmu_driver = {
914 	.driver = {
915 		.name   = "exynos-tmu",
916 		.owner  = THIS_MODULE,
917 		.pm     = EXYNOS_TMU_PM,
918 		.of_match_table = exynos_tmu_match,
919 	},
920 	.probe = exynos_tmu_probe,
921 	.remove	= exynos_tmu_remove,
922 };
923 
924 module_platform_driver(exynos_tmu_driver);
925 
926 MODULE_DESCRIPTION("EXYNOS TMU Driver");
927 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
928 MODULE_LICENSE("GPL");
929 MODULE_ALIAS("platform:exynos-tmu");
930