xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision 8328a4b1d67fa6a4e0c51c27ddb7cb61b562f33e)
159dfa54cSAmit Daniel Kachhap /*
259dfa54cSAmit Daniel Kachhap  * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit)
359dfa54cSAmit Daniel Kachhap  *
459dfa54cSAmit Daniel Kachhap  *  Copyright (C) 2011 Samsung Electronics
559dfa54cSAmit Daniel Kachhap  *  Donggeun Kim <dg77.kim@samsung.com>
659dfa54cSAmit Daniel Kachhap  *  Amit Daniel Kachhap <amit.kachhap@linaro.org>
759dfa54cSAmit Daniel Kachhap  *
859dfa54cSAmit Daniel Kachhap  * This program is free software; you can redistribute it and/or modify
959dfa54cSAmit Daniel Kachhap  * it under the terms of the GNU General Public License as published by
1059dfa54cSAmit Daniel Kachhap  * the Free Software Foundation; either version 2 of the License, or
1159dfa54cSAmit Daniel Kachhap  * (at your option) any later version.
1259dfa54cSAmit Daniel Kachhap  *
1359dfa54cSAmit Daniel Kachhap  * This program is distributed in the hope that it will be useful,
1459dfa54cSAmit Daniel Kachhap  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1559dfa54cSAmit Daniel Kachhap  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1659dfa54cSAmit Daniel Kachhap  * GNU General Public License for more details.
1759dfa54cSAmit Daniel Kachhap  *
1859dfa54cSAmit Daniel Kachhap  * You should have received a copy of the GNU General Public License
1959dfa54cSAmit Daniel Kachhap  * along with this program; if not, write to the Free Software
2059dfa54cSAmit Daniel Kachhap  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2159dfa54cSAmit Daniel Kachhap  *
2259dfa54cSAmit Daniel Kachhap  */
2359dfa54cSAmit Daniel Kachhap 
2459dfa54cSAmit Daniel Kachhap #include <linux/clk.h>
2559dfa54cSAmit Daniel Kachhap #include <linux/io.h>
2659dfa54cSAmit Daniel Kachhap #include <linux/interrupt.h>
2759dfa54cSAmit Daniel Kachhap #include <linux/module.h>
2859dfa54cSAmit Daniel Kachhap #include <linux/of.h>
29cebe7373SAmit Daniel Kachhap #include <linux/of_address.h>
30cebe7373SAmit Daniel Kachhap #include <linux/of_irq.h>
3159dfa54cSAmit Daniel Kachhap #include <linux/platform_device.h>
32498d22f6SAmit Daniel Kachhap #include <linux/regulator/consumer.h>
3359dfa54cSAmit Daniel Kachhap 
3459dfa54cSAmit Daniel Kachhap #include "exynos_thermal_common.h"
350c1836a6SAmit Daniel Kachhap #include "exynos_tmu.h"
36e6b7991eSAmit Daniel Kachhap #include "exynos_tmu_data.h"
3759dfa54cSAmit Daniel Kachhap 
38cebe7373SAmit Daniel Kachhap /**
39cebe7373SAmit Daniel Kachhap  * struct exynos_tmu_data : A structure to hold the private data of the TMU
40cebe7373SAmit Daniel Kachhap 	driver
41cebe7373SAmit Daniel Kachhap  * @id: identifier of the one instance of the TMU controller.
42cebe7373SAmit Daniel Kachhap  * @pdata: pointer to the tmu platform/configuration data
43cebe7373SAmit Daniel Kachhap  * @base: base address of the single instance of the TMU controller.
449025d563SNaveen Krishna Chatradhi  * @base_second: base address of the common registers of the TMU controller.
45cebe7373SAmit Daniel Kachhap  * @irq: irq number of the TMU controller.
46cebe7373SAmit Daniel Kachhap  * @soc: id of the SOC type.
47cebe7373SAmit Daniel Kachhap  * @irq_work: pointer to the irq work structure.
48cebe7373SAmit Daniel Kachhap  * @lock: lock to implement synchronization.
49cebe7373SAmit Daniel Kachhap  * @clk: pointer to the clock structure.
5014a11dc7SNaveen Krishna Chatradhi  * @clk_sec: pointer to the clock structure for accessing the base_second.
51cebe7373SAmit Daniel Kachhap  * @temp_error1: fused value of the first point trim.
52cebe7373SAmit Daniel Kachhap  * @temp_error2: fused value of the second point trim.
53498d22f6SAmit Daniel Kachhap  * @regulator: pointer to the TMU regulator structure.
54cebe7373SAmit Daniel Kachhap  * @reg_conf: pointer to structure to register with core thermal.
55cebe7373SAmit Daniel Kachhap  */
5659dfa54cSAmit Daniel Kachhap struct exynos_tmu_data {
57cebe7373SAmit Daniel Kachhap 	int id;
5859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
5959dfa54cSAmit Daniel Kachhap 	void __iomem *base;
609025d563SNaveen Krishna Chatradhi 	void __iomem *base_second;
6159dfa54cSAmit Daniel Kachhap 	int irq;
6259dfa54cSAmit Daniel Kachhap 	enum soc_type soc;
6359dfa54cSAmit Daniel Kachhap 	struct work_struct irq_work;
6459dfa54cSAmit Daniel Kachhap 	struct mutex lock;
6514a11dc7SNaveen Krishna Chatradhi 	struct clk *clk, *clk_sec;
6659dfa54cSAmit Daniel Kachhap 	u8 temp_error1, temp_error2;
67498d22f6SAmit Daniel Kachhap 	struct regulator *regulator;
68cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *reg_conf;
6959dfa54cSAmit Daniel Kachhap };
7059dfa54cSAmit Daniel Kachhap 
7159dfa54cSAmit Daniel Kachhap /*
7259dfa54cSAmit Daniel Kachhap  * TMU treats temperature as a mapped temperature code.
7359dfa54cSAmit Daniel Kachhap  * The temperature is converted differently depending on the calibration type.
7459dfa54cSAmit Daniel Kachhap  */
7559dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
7659dfa54cSAmit Daniel Kachhap {
7759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
7859dfa54cSAmit Daniel Kachhap 	int temp_code;
7959dfa54cSAmit Daniel Kachhap 
8059dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
8159dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
82bb34b4c8SAmit Daniel Kachhap 		temp_code = (temp - pdata->first_point_trim) *
8359dfa54cSAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) /
84bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) +
85bb34b4c8SAmit Daniel Kachhap 			data->temp_error1;
8659dfa54cSAmit Daniel Kachhap 		break;
8759dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
88bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
8959dfa54cSAmit Daniel Kachhap 		break;
9059dfa54cSAmit Daniel Kachhap 	default:
91bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + pdata->default_temp_offset;
9259dfa54cSAmit Daniel Kachhap 		break;
9359dfa54cSAmit Daniel Kachhap 	}
94ddb31d43SBartlomiej Zolnierkiewicz 
9559dfa54cSAmit Daniel Kachhap 	return temp_code;
9659dfa54cSAmit Daniel Kachhap }
9759dfa54cSAmit Daniel Kachhap 
9859dfa54cSAmit Daniel Kachhap /*
9959dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
10059dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
10159dfa54cSAmit Daniel Kachhap  */
10259dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
10359dfa54cSAmit Daniel Kachhap {
10459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
10559dfa54cSAmit Daniel Kachhap 	int temp;
10659dfa54cSAmit Daniel Kachhap 
10759dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
10859dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
109bb34b4c8SAmit Daniel Kachhap 		temp = (temp_code - data->temp_error1) *
110bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) /
111bb34b4c8SAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) +
112bb34b4c8SAmit Daniel Kachhap 			pdata->first_point_trim;
11359dfa54cSAmit Daniel Kachhap 		break;
11459dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
115bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
11659dfa54cSAmit Daniel Kachhap 		break;
11759dfa54cSAmit Daniel Kachhap 	default:
118bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - pdata->default_temp_offset;
11959dfa54cSAmit Daniel Kachhap 		break;
12059dfa54cSAmit Daniel Kachhap 	}
121ddb31d43SBartlomiej Zolnierkiewicz 
12259dfa54cSAmit Daniel Kachhap 	return temp;
12359dfa54cSAmit Daniel Kachhap }
12459dfa54cSAmit Daniel Kachhap 
125b835ced1SBartlomiej Zolnierkiewicz static void exynos_tmu_clear_irqs(struct exynos_tmu_data *data)
126b835ced1SBartlomiej Zolnierkiewicz {
127b835ced1SBartlomiej Zolnierkiewicz 	const struct exynos_tmu_registers *reg = data->pdata->registers;
128b835ced1SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
129b835ced1SBartlomiej Zolnierkiewicz 
130b835ced1SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + reg->tmu_intstat);
131b835ced1SBartlomiej Zolnierkiewicz 	/*
132b835ced1SBartlomiej Zolnierkiewicz 	 * Clear the interrupts.  Please note that the documentation for
133b835ced1SBartlomiej Zolnierkiewicz 	 * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
134b835ced1SBartlomiej Zolnierkiewicz 	 * states that INTCLEAR register has a different placing of bits
135b835ced1SBartlomiej Zolnierkiewicz 	 * responsible for FALL IRQs than INTSTAT register.  Exynos5420
136b835ced1SBartlomiej Zolnierkiewicz 	 * and Exynos5440 documentation is correct (Exynos4210 doesn't
137b835ced1SBartlomiej Zolnierkiewicz 	 * support FALL IRQs at all).
138b835ced1SBartlomiej Zolnierkiewicz 	 */
139b835ced1SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + reg->tmu_intclear);
140b835ced1SBartlomiej Zolnierkiewicz }
141b835ced1SBartlomiej Zolnierkiewicz 
142*8328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
143*8328a4b1SBartlomiej Zolnierkiewicz {
144*8328a4b1SBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
145*8328a4b1SBartlomiej Zolnierkiewicz 
146*8328a4b1SBartlomiej Zolnierkiewicz 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
147*8328a4b1SBartlomiej Zolnierkiewicz 	data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
148*8328a4b1SBartlomiej Zolnierkiewicz 				EXYNOS_TMU_TEMP_MASK);
149*8328a4b1SBartlomiej Zolnierkiewicz 
150*8328a4b1SBartlomiej Zolnierkiewicz 	if (!data->temp_error1 ||
151*8328a4b1SBartlomiej Zolnierkiewicz 		(pdata->min_efuse_value > data->temp_error1) ||
152*8328a4b1SBartlomiej Zolnierkiewicz 		(data->temp_error1 > pdata->max_efuse_value))
153*8328a4b1SBartlomiej Zolnierkiewicz 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
154*8328a4b1SBartlomiej Zolnierkiewicz 
155*8328a4b1SBartlomiej Zolnierkiewicz 	if (!data->temp_error2)
156*8328a4b1SBartlomiej Zolnierkiewicz 		data->temp_error2 =
157*8328a4b1SBartlomiej Zolnierkiewicz 			(pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
158*8328a4b1SBartlomiej Zolnierkiewicz 			EXYNOS_TMU_TEMP_MASK;
159*8328a4b1SBartlomiej Zolnierkiewicz }
160*8328a4b1SBartlomiej Zolnierkiewicz 
16159dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
16259dfa54cSAmit Daniel Kachhap {
16359dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
16459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
165b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
16656c64da7SChanwoo Choi 	unsigned int status, trim_info = 0, con, ctrl;
16759dfa54cSAmit Daniel Kachhap 	unsigned int rising_threshold = 0, falling_threshold = 0;
168ac951af5SBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
16959dfa54cSAmit Daniel Kachhap 
17059dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
17159dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
17214a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
17314a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
17459dfa54cSAmit Daniel Kachhap 
1756f02fa18SBartlomiej Zolnierkiewicz 	if (data->soc != SOC_ARCH_EXYNOS5440) {
1765d022061SBartlomiej Zolnierkiewicz 		status = readb(data->base + EXYNOS_TMU_REG_STATUS);
17759dfa54cSAmit Daniel Kachhap 		if (!status) {
17859dfa54cSAmit Daniel Kachhap 			ret = -EBUSY;
17959dfa54cSAmit Daniel Kachhap 			goto out;
18059dfa54cSAmit Daniel Kachhap 		}
181f4dae753SAmit Daniel Kachhap 	}
18259dfa54cSAmit Daniel Kachhap 
1831e04ee80SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS3250 ||
1841e04ee80SBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS4412 ||
1851e04ee80SBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS5250) {
18632f95205SBartlomiej Zolnierkiewicz 		if (data->soc == SOC_ARCH_EXYNOS3250) {
18732f95205SBartlomiej Zolnierkiewicz 			ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
18832f95205SBartlomiej Zolnierkiewicz 			ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
18932f95205SBartlomiej Zolnierkiewicz 			writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
19056c64da7SChanwoo Choi 		}
19132f95205SBartlomiej Zolnierkiewicz 		ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
19232f95205SBartlomiej Zolnierkiewicz 		ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
19332f95205SBartlomiej Zolnierkiewicz 		writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
19456c64da7SChanwoo Choi 	}
195b8d582b9SAmit Daniel Kachhap 
19659dfa54cSAmit Daniel Kachhap 	/* Save trimming info in order to perform calibration */
197a0395eeeSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS5440) {
198a0395eeeSAmit Daniel Kachhap 		/*
199a0395eeeSAmit Daniel Kachhap 		 * For exynos5440 soc triminfo value is swapped between TMU0 and
200a0395eeeSAmit Daniel Kachhap 		 * TMU2, so the below logic is needed.
201a0395eeeSAmit Daniel Kachhap 		 */
202a0395eeeSAmit Daniel Kachhap 		switch (data->id) {
203a0395eeeSAmit Daniel Kachhap 		case 0:
204a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base +
20577109411SBartlomiej Zolnierkiewicz 			EXYNOS5440_EFUSE_SWAP_OFFSET + EXYNOS5440_TMU_S0_7_TRIM);
206a0395eeeSAmit Daniel Kachhap 			break;
207a0395eeeSAmit Daniel Kachhap 		case 1:
20877109411SBartlomiej Zolnierkiewicz 			trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM);
209a0395eeeSAmit Daniel Kachhap 			break;
210a0395eeeSAmit Daniel Kachhap 		case 2:
211a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base -
21277109411SBartlomiej Zolnierkiewicz 			EXYNOS5440_EFUSE_SWAP_OFFSET + EXYNOS5440_TMU_S0_7_TRIM);
213a0395eeeSAmit Daniel Kachhap 		}
214a0395eeeSAmit Daniel Kachhap 	} else {
21514a11dc7SNaveen Krishna Chatradhi 		/* On exynos5420 the triminfo register is in the shared space */
21614a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
21714a11dc7SNaveen Krishna Chatradhi 			trim_info = readl(data->base_second +
21877109411SBartlomiej Zolnierkiewicz 						EXYNOS_TMU_REG_TRIMINFO);
21914a11dc7SNaveen Krishna Chatradhi 		else
22077109411SBartlomiej Zolnierkiewicz 			trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
221a0395eeeSAmit Daniel Kachhap 	}
222*8328a4b1SBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
22359dfa54cSAmit Daniel Kachhap 
224c65d3473STushar Behera 	rising_threshold = readl(data->base + reg->threshold_th0);
225c65d3473STushar Behera 
22659dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210) {
22759dfa54cSAmit Daniel Kachhap 		/* Write temperature code for threshold */
22859dfa54cSAmit Daniel Kachhap 		threshold_code = temp_to_code(data, pdata->threshold);
22959dfa54cSAmit Daniel Kachhap 		writeb(threshold_code,
2306b1fbbdeSBartlomiej Zolnierkiewicz 			data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
231ac951af5SBartlomiej Zolnierkiewicz 		for (i = 0; i < pdata->non_hw_trigger_levels; i++)
232b8d582b9SAmit Daniel Kachhap 			writeb(pdata->trigger_levels[i], data->base +
233b8d582b9SAmit Daniel Kachhap 			reg->threshold_th0 + i * sizeof(reg->threshold_th0));
23459dfa54cSAmit Daniel Kachhap 
235b835ced1SBartlomiej Zolnierkiewicz 		exynos_tmu_clear_irqs(data);
236a0395eeeSAmit Daniel Kachhap 	} else {
23759dfa54cSAmit Daniel Kachhap 		/* Write temperature code for rising and falling threshold */
238ac951af5SBartlomiej Zolnierkiewicz 		for (i = 0; i < pdata->non_hw_trigger_levels; i++) {
23959dfa54cSAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
24059dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i]);
241c65d3473STushar Behera 			rising_threshold &= ~(0xff << 8 * i);
24259dfa54cSAmit Daniel Kachhap 			rising_threshold |= threshold_code << 8 * i;
24323f14629SBartlomiej Zolnierkiewicz 			if (data->soc != SOC_ARCH_EXYNOS5440) {
24459dfa54cSAmit Daniel Kachhap 				threshold_code = temp_to_code(data,
24559dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i] -
24659dfa54cSAmit Daniel Kachhap 						pdata->threshold_falling);
2478131a246SBartlomiej Zolnierkiewicz 				falling_threshold |= threshold_code << 8 * i;
24859dfa54cSAmit Daniel Kachhap 			}
24959dfa54cSAmit Daniel Kachhap 		}
25059dfa54cSAmit Daniel Kachhap 
25159dfa54cSAmit Daniel Kachhap 		writel(rising_threshold,
252b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th0);
25359dfa54cSAmit Daniel Kachhap 		writel(falling_threshold,
254b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th1);
25559dfa54cSAmit Daniel Kachhap 
256b835ced1SBartlomiej Zolnierkiewicz 		exynos_tmu_clear_irqs(data);
2577ca04e58SAmit Daniel Kachhap 
2587ca04e58SAmit Daniel Kachhap 		/* if last threshold limit is also present */
2597ca04e58SAmit Daniel Kachhap 		i = pdata->max_trigger_level - 1;
2607ca04e58SAmit Daniel Kachhap 		if (pdata->trigger_levels[i] &&
2617ca04e58SAmit Daniel Kachhap 				(pdata->trigger_type[i] == HW_TRIP)) {
2627ca04e58SAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
2637ca04e58SAmit Daniel Kachhap 						pdata->trigger_levels[i]);
2642516593eSBartlomiej Zolnierkiewicz 			if (data->soc != SOC_ARCH_EXYNOS5440) {
265a0395eeeSAmit Daniel Kachhap 				/* 1-4 level to be assigned in th0 reg */
266c65d3473STushar Behera 				rising_threshold &= ~(0xff << 8 * i);
2677ca04e58SAmit Daniel Kachhap 				rising_threshold |= threshold_code << 8 * i;
2687ca04e58SAmit Daniel Kachhap 				writel(rising_threshold,
2692516593eSBartlomiej Zolnierkiewicz 					data->base + EXYNOS_THD_TEMP_RISE);
2702516593eSBartlomiej Zolnierkiewicz 			} else {
271a0395eeeSAmit Daniel Kachhap 				/* 5th level to be assigned in th2 reg */
272a0395eeeSAmit Daniel Kachhap 				rising_threshold =
2732516593eSBartlomiej Zolnierkiewicz 				threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT;
274a0395eeeSAmit Daniel Kachhap 				writel(rising_threshold,
2752516593eSBartlomiej Zolnierkiewicz 					data->base + EXYNOS5440_TMU_S0_7_TH2);
276a0395eeeSAmit Daniel Kachhap 			}
2777ca04e58SAmit Daniel Kachhap 			con = readl(data->base + reg->tmu_ctrl);
27877a37a92SBartlomiej Zolnierkiewicz 			con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
2797ca04e58SAmit Daniel Kachhap 			writel(con, data->base + reg->tmu_ctrl);
2807ca04e58SAmit Daniel Kachhap 		}
28159dfa54cSAmit Daniel Kachhap 	}
282a0395eeeSAmit Daniel Kachhap 	/*Clear the PMIN in the common TMU register*/
2830c78b4d8SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5440 && !data->id)
2840c78b4d8SBartlomiej Zolnierkiewicz 		writel(0, data->base_second + EXYNOS5440_TMU_PMIN);
28559dfa54cSAmit Daniel Kachhap out:
28659dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
28759dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
28814a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
28914a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
29059dfa54cSAmit Daniel Kachhap 
29159dfa54cSAmit Daniel Kachhap 	return ret;
29259dfa54cSAmit Daniel Kachhap }
29359dfa54cSAmit Daniel Kachhap 
29459dfa54cSAmit Daniel Kachhap static void exynos_tmu_control(struct platform_device *pdev, bool on)
29559dfa54cSAmit Daniel Kachhap {
29659dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
29759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
298b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
299d37761ecSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
30059dfa54cSAmit Daniel Kachhap 
30159dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
30259dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
30359dfa54cSAmit Daniel Kachhap 
304b8d582b9SAmit Daniel Kachhap 	con = readl(data->base + reg->tmu_ctrl);
30559dfa54cSAmit Daniel Kachhap 
30686f5362eSLukasz Majewski 	if (pdata->test_mux)
307bfb2b88cSBartlomiej Zolnierkiewicz 		con |= (pdata->test_mux << EXYNOS4412_MUX_ADDR_SHIFT);
30886f5362eSLukasz Majewski 
30999d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
31099d67fb9SBartlomiej Zolnierkiewicz 	con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
311d0a0ce3eSAmit Daniel Kachhap 
31299d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
31399d67fb9SBartlomiej Zolnierkiewicz 	con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
314d0a0ce3eSAmit Daniel Kachhap 
315d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
316b9504a6aSBartlomiej Zolnierkiewicz 		con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
317b9504a6aSBartlomiej Zolnierkiewicz 		con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT);
31859dfa54cSAmit Daniel Kachhap 	}
31959dfa54cSAmit Daniel Kachhap 
32059dfa54cSAmit Daniel Kachhap 	if (on) {
32199d67fb9SBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
322d0a0ce3eSAmit Daniel Kachhap 		interrupt_en =
323b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[3] << reg->inten_rise3_shift |
324b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[2] << reg->inten_rise2_shift |
325b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[1] << reg->inten_rise1_shift |
326b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[0] << reg->inten_rise0_shift;
327f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, FALLING_TRIP))
328d0a0ce3eSAmit Daniel Kachhap 			interrupt_en |=
329b8d582b9SAmit Daniel Kachhap 				interrupt_en << reg->inten_fall0_shift;
33059dfa54cSAmit Daniel Kachhap 	} else {
33199d67fb9SBartlomiej Zolnierkiewicz 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
33259dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
33359dfa54cSAmit Daniel Kachhap 	}
334b8d582b9SAmit Daniel Kachhap 	writel(interrupt_en, data->base + reg->tmu_inten);
335b8d582b9SAmit Daniel Kachhap 	writel(con, data->base + reg->tmu_ctrl);
33659dfa54cSAmit Daniel Kachhap 
33759dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
33859dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
33959dfa54cSAmit Daniel Kachhap }
34059dfa54cSAmit Daniel Kachhap 
34159dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data)
34259dfa54cSAmit Daniel Kachhap {
343b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
344b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
34559dfa54cSAmit Daniel Kachhap 	u8 temp_code;
34659dfa54cSAmit Daniel Kachhap 	int temp;
34759dfa54cSAmit Daniel Kachhap 
34859dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
34959dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
35059dfa54cSAmit Daniel Kachhap 
351b8d582b9SAmit Daniel Kachhap 	temp_code = readb(data->base + reg->tmu_cur_temp);
35259dfa54cSAmit Daniel Kachhap 
353ddb31d43SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS4210)
354ddb31d43SBartlomiej Zolnierkiewicz 		/* temp_code should range between 75 and 175 */
355ddb31d43SBartlomiej Zolnierkiewicz 		if (temp_code < 75 || temp_code > 175) {
356ddb31d43SBartlomiej Zolnierkiewicz 			temp = -ENODATA;
357ddb31d43SBartlomiej Zolnierkiewicz 			goto out;
358ddb31d43SBartlomiej Zolnierkiewicz 		}
359ddb31d43SBartlomiej Zolnierkiewicz 
360ddb31d43SBartlomiej Zolnierkiewicz 	temp = code_to_temp(data, temp_code);
361ddb31d43SBartlomiej Zolnierkiewicz out:
36259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
36359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
36459dfa54cSAmit Daniel Kachhap 
36559dfa54cSAmit Daniel Kachhap 	return temp;
36659dfa54cSAmit Daniel Kachhap }
36759dfa54cSAmit Daniel Kachhap 
36859dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
36959dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
37059dfa54cSAmit Daniel Kachhap {
37159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
372b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
373b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
374b8d582b9SAmit Daniel Kachhap 	unsigned int val;
37559dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
37659dfa54cSAmit Daniel Kachhap 
377f4dae753SAmit Daniel Kachhap 	if (!TMU_SUPPORTS(pdata, EMULATION))
37859dfa54cSAmit Daniel Kachhap 		goto out;
37959dfa54cSAmit Daniel Kachhap 
38059dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
38159dfa54cSAmit Daniel Kachhap 		goto out;
38259dfa54cSAmit Daniel Kachhap 
38359dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
38459dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
38559dfa54cSAmit Daniel Kachhap 
386b8d582b9SAmit Daniel Kachhap 	val = readl(data->base + reg->emul_con);
38759dfa54cSAmit Daniel Kachhap 
38859dfa54cSAmit Daniel Kachhap 	if (temp) {
38959dfa54cSAmit Daniel Kachhap 		temp /= MCELSIUS;
39059dfa54cSAmit Daniel Kachhap 
391f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
3926070c2caSBartlomiej Zolnierkiewicz 			val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
3936070c2caSBartlomiej Zolnierkiewicz 			val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
394f4dae753SAmit Daniel Kachhap 		}
3959e288d64SBartlomiej Zolnierkiewicz 		val &= ~(EXYNOS_EMUL_DATA_MASK << EXYNOS_EMUL_DATA_SHIFT);
3969e288d64SBartlomiej Zolnierkiewicz 		val |= (temp_to_code(data, temp) << EXYNOS_EMUL_DATA_SHIFT) |
397f4dae753SAmit Daniel Kachhap 			EXYNOS_EMUL_ENABLE;
39859dfa54cSAmit Daniel Kachhap 	} else {
399b8d582b9SAmit Daniel Kachhap 		val &= ~EXYNOS_EMUL_ENABLE;
40059dfa54cSAmit Daniel Kachhap 	}
40159dfa54cSAmit Daniel Kachhap 
402b8d582b9SAmit Daniel Kachhap 	writel(val, data->base + reg->emul_con);
40359dfa54cSAmit Daniel Kachhap 
40459dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
40559dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
40659dfa54cSAmit Daniel Kachhap 	return 0;
40759dfa54cSAmit Daniel Kachhap out:
40859dfa54cSAmit Daniel Kachhap 	return ret;
40959dfa54cSAmit Daniel Kachhap }
41059dfa54cSAmit Daniel Kachhap #else
41159dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
41259dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
41359dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/
41459dfa54cSAmit Daniel Kachhap 
41559dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
41659dfa54cSAmit Daniel Kachhap {
41759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
41859dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
419b835ced1SBartlomiej Zolnierkiewicz 	unsigned int val_type;
420a0395eeeSAmit Daniel Kachhap 
42114a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
42214a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
423a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
424421d5d12SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5440) {
425421d5d12SBartlomiej Zolnierkiewicz 		val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS);
426a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
427a0395eeeSAmit Daniel Kachhap 			goto out;
428a0395eeeSAmit Daniel Kachhap 	}
42914a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
43014a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
43159dfa54cSAmit Daniel Kachhap 
432cebe7373SAmit Daniel Kachhap 	exynos_report_trigger(data->reg_conf);
43359dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
43459dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
435b8d582b9SAmit Daniel Kachhap 
436a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
437b835ced1SBartlomiej Zolnierkiewicz 	exynos_tmu_clear_irqs(data);
438b8d582b9SAmit Daniel Kachhap 
43959dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
44059dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
441a0395eeeSAmit Daniel Kachhap out:
44259dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
44359dfa54cSAmit Daniel Kachhap }
44459dfa54cSAmit Daniel Kachhap 
44559dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
44659dfa54cSAmit Daniel Kachhap {
44759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
44859dfa54cSAmit Daniel Kachhap 
44959dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
45059dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
45159dfa54cSAmit Daniel Kachhap 
45259dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
45359dfa54cSAmit Daniel Kachhap }
45459dfa54cSAmit Daniel Kachhap 
45559dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
45659dfa54cSAmit Daniel Kachhap 	{
4571fe56dc1SChanwoo Choi 		.compatible = "samsung,exynos3250-tmu",
4581fe56dc1SChanwoo Choi 		.data = (void *)EXYNOS3250_TMU_DRV_DATA,
4591fe56dc1SChanwoo Choi 	},
4601fe56dc1SChanwoo Choi 	{
46159dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4210-tmu",
46259dfa54cSAmit Daniel Kachhap 		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
46359dfa54cSAmit Daniel Kachhap 	},
46459dfa54cSAmit Daniel Kachhap 	{
46559dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4412-tmu",
46614ddfaecSLukasz Majewski 		.data = (void *)EXYNOS4412_TMU_DRV_DATA,
46759dfa54cSAmit Daniel Kachhap 	},
46859dfa54cSAmit Daniel Kachhap 	{
46959dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos5250-tmu",
470e6b7991eSAmit Daniel Kachhap 		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
47159dfa54cSAmit Daniel Kachhap 	},
47290542546SAmit Daniel Kachhap 	{
473923488a5SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5260-tmu",
474923488a5SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5260_TMU_DRV_DATA,
475923488a5SNaveen Krishna Chatradhi 	},
476923488a5SNaveen Krishna Chatradhi 	{
47714a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu",
47814a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
47914a11dc7SNaveen Krishna Chatradhi 	},
48014a11dc7SNaveen Krishna Chatradhi 	{
48114a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu-ext-triminfo",
48214a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
48314a11dc7SNaveen Krishna Chatradhi 	},
48414a11dc7SNaveen Krishna Chatradhi 	{
48590542546SAmit Daniel Kachhap 		.compatible = "samsung,exynos5440-tmu",
48690542546SAmit Daniel Kachhap 		.data = (void *)EXYNOS5440_TMU_DRV_DATA,
48790542546SAmit Daniel Kachhap 	},
48859dfa54cSAmit Daniel Kachhap 	{},
48959dfa54cSAmit Daniel Kachhap };
49059dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
49159dfa54cSAmit Daniel Kachhap 
49259dfa54cSAmit Daniel Kachhap static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
493cebe7373SAmit Daniel Kachhap 			struct platform_device *pdev, int id)
49459dfa54cSAmit Daniel Kachhap {
495cebe7373SAmit Daniel Kachhap 	struct  exynos_tmu_init_data *data_table;
496cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *tmu_data;
49759dfa54cSAmit Daniel Kachhap 	const struct of_device_id *match;
49873b5b1d7SSachin Kamat 
49959dfa54cSAmit Daniel Kachhap 	match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
50059dfa54cSAmit Daniel Kachhap 	if (!match)
50159dfa54cSAmit Daniel Kachhap 		return NULL;
502cebe7373SAmit Daniel Kachhap 	data_table = (struct exynos_tmu_init_data *) match->data;
503cebe7373SAmit Daniel Kachhap 	if (!data_table || id >= data_table->tmu_count)
504cebe7373SAmit Daniel Kachhap 		return NULL;
505cebe7373SAmit Daniel Kachhap 	tmu_data = data_table->tmu_data;
506cebe7373SAmit Daniel Kachhap 	return (struct exynos_tmu_platform_data *) (tmu_data + id);
50759dfa54cSAmit Daniel Kachhap }
50859dfa54cSAmit Daniel Kachhap 
509cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
51059dfa54cSAmit Daniel Kachhap {
511cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
512cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
513cebe7373SAmit Daniel Kachhap 	struct resource res;
514498d22f6SAmit Daniel Kachhap 	int ret;
51559dfa54cSAmit Daniel Kachhap 
51673b5b1d7SSachin Kamat 	if (!data || !pdev->dev.of_node)
517cebe7373SAmit Daniel Kachhap 		return -ENODEV;
51859dfa54cSAmit Daniel Kachhap 
519498d22f6SAmit Daniel Kachhap 	/*
520498d22f6SAmit Daniel Kachhap 	 * Try enabling the regulator if found
521498d22f6SAmit Daniel Kachhap 	 * TODO: Add regulator as an SOC feature, so that regulator enable
522498d22f6SAmit Daniel Kachhap 	 * is a compulsory call.
523498d22f6SAmit Daniel Kachhap 	 */
524498d22f6SAmit Daniel Kachhap 	data->regulator = devm_regulator_get(&pdev->dev, "vtmu");
525498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator)) {
526498d22f6SAmit Daniel Kachhap 		ret = regulator_enable(data->regulator);
527498d22f6SAmit Daniel Kachhap 		if (ret) {
528498d22f6SAmit Daniel Kachhap 			dev_err(&pdev->dev, "failed to enable vtmu\n");
529498d22f6SAmit Daniel Kachhap 			return ret;
530498d22f6SAmit Daniel Kachhap 		}
531498d22f6SAmit Daniel Kachhap 	} else {
532498d22f6SAmit Daniel Kachhap 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
533498d22f6SAmit Daniel Kachhap 	}
534498d22f6SAmit Daniel Kachhap 
535cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
536cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
537cebe7373SAmit Daniel Kachhap 		data->id = 0;
538cebe7373SAmit Daniel Kachhap 
539cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
540cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
541cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
542cebe7373SAmit Daniel Kachhap 		return -ENODEV;
543cebe7373SAmit Daniel Kachhap 	}
544cebe7373SAmit Daniel Kachhap 
545cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
546cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
547cebe7373SAmit Daniel Kachhap 		return -ENODEV;
548cebe7373SAmit Daniel Kachhap 	}
549cebe7373SAmit Daniel Kachhap 
550cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
551cebe7373SAmit Daniel Kachhap 	if (!data->base) {
552cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
553cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
554cebe7373SAmit Daniel Kachhap 	}
555cebe7373SAmit Daniel Kachhap 
556cebe7373SAmit Daniel Kachhap 	pdata = exynos_get_driver_data(pdev, data->id);
55759dfa54cSAmit Daniel Kachhap 	if (!pdata) {
55859dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "No platform init data supplied.\n");
55959dfa54cSAmit Daniel Kachhap 		return -ENODEV;
56059dfa54cSAmit Daniel Kachhap 	}
561cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
562d9b6ee14SAmit Daniel Kachhap 	/*
563d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
564d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
565d9b6ee14SAmit Daniel Kachhap 	 */
5669025d563SNaveen Krishna Chatradhi 	if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE))
567d9b6ee14SAmit Daniel Kachhap 		return 0;
568d9b6ee14SAmit Daniel Kachhap 
569d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
570d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
571d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
572d9b6ee14SAmit Daniel Kachhap 	}
573d9b6ee14SAmit Daniel Kachhap 
5749025d563SNaveen Krishna Chatradhi 	data->base_second = devm_ioremap(&pdev->dev, res.start,
575d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
5769025d563SNaveen Krishna Chatradhi 	if (!data->base_second) {
577d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
578d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
579d9b6ee14SAmit Daniel Kachhap 	}
580cebe7373SAmit Daniel Kachhap 
581cebe7373SAmit Daniel Kachhap 	return 0;
582cebe7373SAmit Daniel Kachhap }
583cebe7373SAmit Daniel Kachhap 
584cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
585cebe7373SAmit Daniel Kachhap {
586cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data;
587cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
588cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *sensor_conf;
589cebe7373SAmit Daniel Kachhap 	int ret, i;
590cebe7373SAmit Daniel Kachhap 
59159dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
59259dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
5932a9675b3SJingoo Han 	if (!data)
59459dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
59559dfa54cSAmit Daniel Kachhap 
596cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
597cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
598cebe7373SAmit Daniel Kachhap 
599cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
600cebe7373SAmit Daniel Kachhap 	if (ret)
601cebe7373SAmit Daniel Kachhap 		return ret;
602cebe7373SAmit Daniel Kachhap 
603cebe7373SAmit Daniel Kachhap 	pdata = data->pdata;
60459dfa54cSAmit Daniel Kachhap 
60559dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
60659dfa54cSAmit Daniel Kachhap 
60759dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
60859dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
60959dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
61059dfa54cSAmit Daniel Kachhap 		return  PTR_ERR(data->clk);
61159dfa54cSAmit Daniel Kachhap 	}
61259dfa54cSAmit Daniel Kachhap 
61314a11dc7SNaveen Krishna Chatradhi 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
61414a11dc7SNaveen Krishna Chatradhi 	if (IS_ERR(data->clk_sec)) {
61514a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
61614a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
61714a11dc7SNaveen Krishna Chatradhi 			return PTR_ERR(data->clk_sec);
61814a11dc7SNaveen Krishna Chatradhi 		}
61914a11dc7SNaveen Krishna Chatradhi 	} else {
62014a11dc7SNaveen Krishna Chatradhi 		ret = clk_prepare(data->clk_sec);
62114a11dc7SNaveen Krishna Chatradhi 		if (ret) {
62214a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get clock\n");
62359dfa54cSAmit Daniel Kachhap 			return ret;
62414a11dc7SNaveen Krishna Chatradhi 		}
62514a11dc7SNaveen Krishna Chatradhi 	}
62614a11dc7SNaveen Krishna Chatradhi 
62714a11dc7SNaveen Krishna Chatradhi 	ret = clk_prepare(data->clk);
62814a11dc7SNaveen Krishna Chatradhi 	if (ret) {
62914a11dc7SNaveen Krishna Chatradhi 		dev_err(&pdev->dev, "Failed to get clock\n");
63014a11dc7SNaveen Krishna Chatradhi 		goto err_clk_sec;
63114a11dc7SNaveen Krishna Chatradhi 	}
63259dfa54cSAmit Daniel Kachhap 
6331fe56dc1SChanwoo Choi 	if (pdata->type == SOC_ARCH_EXYNOS3250 ||
6341fe56dc1SChanwoo Choi 	    pdata->type == SOC_ARCH_EXYNOS4210 ||
63514ddfaecSLukasz Majewski 	    pdata->type == SOC_ARCH_EXYNOS4412 ||
63614ddfaecSLukasz Majewski 	    pdata->type == SOC_ARCH_EXYNOS5250 ||
637923488a5SNaveen Krishna Chatradhi 	    pdata->type == SOC_ARCH_EXYNOS5260 ||
6381e04ee80SBartlomiej Zolnierkiewicz 	    pdata->type == SOC_ARCH_EXYNOS5420 ||
63914a11dc7SNaveen Krishna Chatradhi 	    pdata->type == SOC_ARCH_EXYNOS5420_TRIMINFO ||
640a0395eeeSAmit Daniel Kachhap 	    pdata->type == SOC_ARCH_EXYNOS5440)
64159dfa54cSAmit Daniel Kachhap 		data->soc = pdata->type;
64259dfa54cSAmit Daniel Kachhap 	else {
64359dfa54cSAmit Daniel Kachhap 		ret = -EINVAL;
64459dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Platform not supported\n");
64559dfa54cSAmit Daniel Kachhap 		goto err_clk;
64659dfa54cSAmit Daniel Kachhap 	}
64759dfa54cSAmit Daniel Kachhap 
64859dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
64959dfa54cSAmit Daniel Kachhap 	if (ret) {
65059dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
65159dfa54cSAmit Daniel Kachhap 		goto err_clk;
65259dfa54cSAmit Daniel Kachhap 	}
65359dfa54cSAmit Daniel Kachhap 
65459dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
65559dfa54cSAmit Daniel Kachhap 
656cebe7373SAmit Daniel Kachhap 	/* Allocate a structure to register with the exynos core thermal */
657cebe7373SAmit Daniel Kachhap 	sensor_conf = devm_kzalloc(&pdev->dev,
658cebe7373SAmit Daniel Kachhap 				sizeof(struct thermal_sensor_conf), GFP_KERNEL);
659cebe7373SAmit Daniel Kachhap 	if (!sensor_conf) {
660cebe7373SAmit Daniel Kachhap 		ret = -ENOMEM;
661cebe7373SAmit Daniel Kachhap 		goto err_clk;
662cebe7373SAmit Daniel Kachhap 	}
663cebe7373SAmit Daniel Kachhap 	sprintf(sensor_conf->name, "therm_zone%d", data->id);
664cebe7373SAmit Daniel Kachhap 	sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
665cebe7373SAmit Daniel Kachhap 	sensor_conf->write_emul_temp =
666cebe7373SAmit Daniel Kachhap 		(int (*)(void *, unsigned long))exynos_tmu_set_emulation;
667cebe7373SAmit Daniel Kachhap 	sensor_conf->driver_data = data;
668cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
669bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
670bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[3];
67159dfa54cSAmit Daniel Kachhap 
672cebe7373SAmit Daniel Kachhap 	for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
673cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_val[i] =
67459dfa54cSAmit Daniel Kachhap 			pdata->threshold + pdata->trigger_levels[i];
675cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_type[i] =
6765c3cf552SAmit Daniel Kachhap 					pdata->trigger_type[i];
6775c3cf552SAmit Daniel Kachhap 	}
67859dfa54cSAmit Daniel Kachhap 
679cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
68059dfa54cSAmit Daniel Kachhap 
681cebe7373SAmit Daniel Kachhap 	sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
68259dfa54cSAmit Daniel Kachhap 	for (i = 0; i < pdata->freq_tab_count; i++) {
683cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].freq_clip_max =
68459dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].freq_clip_max;
685cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].temp_level =
68659dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].temp_level;
68759dfa54cSAmit Daniel Kachhap 	}
688cebe7373SAmit Daniel Kachhap 	sensor_conf->dev = &pdev->dev;
689cebe7373SAmit Daniel Kachhap 	/* Register the sensor with thermal management interface */
690cebe7373SAmit Daniel Kachhap 	ret = exynos_register_thermal(sensor_conf);
69159dfa54cSAmit Daniel Kachhap 	if (ret) {
69259dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to register thermal interface\n");
69359dfa54cSAmit Daniel Kachhap 		goto err_clk;
69459dfa54cSAmit Daniel Kachhap 	}
695cebe7373SAmit Daniel Kachhap 	data->reg_conf = sensor_conf;
696cebe7373SAmit Daniel Kachhap 
697cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
698cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
699cebe7373SAmit Daniel Kachhap 	if (ret) {
700cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
701cebe7373SAmit Daniel Kachhap 		goto err_clk;
702cebe7373SAmit Daniel Kachhap 	}
70359dfa54cSAmit Daniel Kachhap 
70459dfa54cSAmit Daniel Kachhap 	return 0;
70559dfa54cSAmit Daniel Kachhap err_clk:
70659dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
70714a11dc7SNaveen Krishna Chatradhi err_clk_sec:
70814a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
70914a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
71059dfa54cSAmit Daniel Kachhap 	return ret;
71159dfa54cSAmit Daniel Kachhap }
71259dfa54cSAmit Daniel Kachhap 
71359dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
71459dfa54cSAmit Daniel Kachhap {
71559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
71659dfa54cSAmit Daniel Kachhap 
717cebe7373SAmit Daniel Kachhap 	exynos_unregister_thermal(data->reg_conf);
71859dfa54cSAmit Daniel Kachhap 
7194215688eSBartlomiej Zolnierkiewicz 	exynos_tmu_control(pdev, false);
7204215688eSBartlomiej Zolnierkiewicz 
72159dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
72214a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
72314a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
72459dfa54cSAmit Daniel Kachhap 
725498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator))
726498d22f6SAmit Daniel Kachhap 		regulator_disable(data->regulator);
727498d22f6SAmit Daniel Kachhap 
72859dfa54cSAmit Daniel Kachhap 	return 0;
72959dfa54cSAmit Daniel Kachhap }
73059dfa54cSAmit Daniel Kachhap 
73159dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
73259dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
73359dfa54cSAmit Daniel Kachhap {
73459dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
73559dfa54cSAmit Daniel Kachhap 
73659dfa54cSAmit Daniel Kachhap 	return 0;
73759dfa54cSAmit Daniel Kachhap }
73859dfa54cSAmit Daniel Kachhap 
73959dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
74059dfa54cSAmit Daniel Kachhap {
74159dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
74259dfa54cSAmit Daniel Kachhap 
74359dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
74459dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
74559dfa54cSAmit Daniel Kachhap 
74659dfa54cSAmit Daniel Kachhap 	return 0;
74759dfa54cSAmit Daniel Kachhap }
74859dfa54cSAmit Daniel Kachhap 
74959dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
75059dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
75159dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
75259dfa54cSAmit Daniel Kachhap #else
75359dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
75459dfa54cSAmit Daniel Kachhap #endif
75559dfa54cSAmit Daniel Kachhap 
75659dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
75759dfa54cSAmit Daniel Kachhap 	.driver = {
75859dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
75959dfa54cSAmit Daniel Kachhap 		.owner  = THIS_MODULE,
76059dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
76173b5b1d7SSachin Kamat 		.of_match_table = exynos_tmu_match,
76259dfa54cSAmit Daniel Kachhap 	},
76359dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
76459dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
76559dfa54cSAmit Daniel Kachhap };
76659dfa54cSAmit Daniel Kachhap 
76759dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
76859dfa54cSAmit Daniel Kachhap 
76959dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
77059dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
77159dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
77259dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
773