xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision ddb31d43cb20222f929b0d242bdb516de51b6c23)
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 	}
94*ddb31d43SBartlomiej 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 	}
121*ddb31d43SBartlomiej Zolnierkiewicz 
12259dfa54cSAmit Daniel Kachhap 	return temp;
12359dfa54cSAmit Daniel Kachhap }
12459dfa54cSAmit Daniel Kachhap 
12559dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
12659dfa54cSAmit Daniel Kachhap {
12759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
12859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
129b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
1307ca04e58SAmit Daniel Kachhap 	unsigned int status, trim_info = 0, con;
13159dfa54cSAmit Daniel Kachhap 	unsigned int rising_threshold = 0, falling_threshold = 0;
13259dfa54cSAmit Daniel Kachhap 	int ret = 0, threshold_code, i, trigger_levs = 0;
13359dfa54cSAmit Daniel Kachhap 
13459dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
13559dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
13614a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
13714a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
13859dfa54cSAmit Daniel Kachhap 
139f4dae753SAmit Daniel Kachhap 	if (TMU_SUPPORTS(pdata, READY_STATUS)) {
140b8d582b9SAmit Daniel Kachhap 		status = readb(data->base + reg->tmu_status);
14159dfa54cSAmit Daniel Kachhap 		if (!status) {
14259dfa54cSAmit Daniel Kachhap 			ret = -EBUSY;
14359dfa54cSAmit Daniel Kachhap 			goto out;
14459dfa54cSAmit Daniel Kachhap 		}
145f4dae753SAmit Daniel Kachhap 	}
14659dfa54cSAmit Daniel Kachhap 
147f4dae753SAmit Daniel Kachhap 	if (TMU_SUPPORTS(pdata, TRIM_RELOAD))
148b8d582b9SAmit Daniel Kachhap 		__raw_writel(1, data->base + reg->triminfo_ctrl);
149b8d582b9SAmit Daniel Kachhap 
15059dfa54cSAmit Daniel Kachhap 	/* Save trimming info in order to perform calibration */
151a0395eeeSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS5440) {
152a0395eeeSAmit Daniel Kachhap 		/*
153a0395eeeSAmit Daniel Kachhap 		 * For exynos5440 soc triminfo value is swapped between TMU0 and
154a0395eeeSAmit Daniel Kachhap 		 * TMU2, so the below logic is needed.
155a0395eeeSAmit Daniel Kachhap 		 */
156a0395eeeSAmit Daniel Kachhap 		switch (data->id) {
157a0395eeeSAmit Daniel Kachhap 		case 0:
158a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base +
159a0395eeeSAmit Daniel Kachhap 			EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
160a0395eeeSAmit Daniel Kachhap 			break;
161a0395eeeSAmit Daniel Kachhap 		case 1:
162b8d582b9SAmit Daniel Kachhap 			trim_info = readl(data->base + reg->triminfo_data);
163a0395eeeSAmit Daniel Kachhap 			break;
164a0395eeeSAmit Daniel Kachhap 		case 2:
165a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base -
166a0395eeeSAmit Daniel Kachhap 			EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
167a0395eeeSAmit Daniel Kachhap 		}
168a0395eeeSAmit Daniel Kachhap 	} else {
16914a11dc7SNaveen Krishna Chatradhi 		/* On exynos5420 the triminfo register is in the shared space */
17014a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
17114a11dc7SNaveen Krishna Chatradhi 			trim_info = readl(data->base_second +
17214a11dc7SNaveen Krishna Chatradhi 							reg->triminfo_data);
17314a11dc7SNaveen Krishna Chatradhi 		else
174a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base + reg->triminfo_data);
175a0395eeeSAmit Daniel Kachhap 	}
176b8d582b9SAmit Daniel Kachhap 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
177b8d582b9SAmit Daniel Kachhap 	data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) &
178b8d582b9SAmit Daniel Kachhap 				EXYNOS_TMU_TEMP_MASK);
17959dfa54cSAmit Daniel Kachhap 
1805000806cSAmit Daniel Kachhap 	if (!data->temp_error1 ||
1815000806cSAmit Daniel Kachhap 		(pdata->min_efuse_value > data->temp_error1) ||
1825000806cSAmit Daniel Kachhap 		(data->temp_error1 > pdata->max_efuse_value))
1835000806cSAmit Daniel Kachhap 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
1845000806cSAmit Daniel Kachhap 
1855000806cSAmit Daniel Kachhap 	if (!data->temp_error2)
1865000806cSAmit Daniel Kachhap 		data->temp_error2 =
1875000806cSAmit Daniel Kachhap 			(pdata->efuse_value >> reg->triminfo_85_shift) &
1885000806cSAmit Daniel Kachhap 			EXYNOS_TMU_TEMP_MASK;
18959dfa54cSAmit Daniel Kachhap 
1907ca04e58SAmit Daniel Kachhap 	for (i = 0; i < pdata->max_trigger_level; i++) {
1917ca04e58SAmit Daniel Kachhap 		if (!pdata->trigger_levels[i])
1927ca04e58SAmit Daniel Kachhap 			continue;
1937ca04e58SAmit Daniel Kachhap 
1947ca04e58SAmit Daniel Kachhap 		/* Count trigger levels except the HW trip*/
1957ca04e58SAmit Daniel Kachhap 		if (!(pdata->trigger_type[i] == HW_TRIP))
19659dfa54cSAmit Daniel Kachhap 			trigger_levs++;
1977ca04e58SAmit Daniel Kachhap 	}
19859dfa54cSAmit Daniel Kachhap 
199c65d3473STushar Behera 	rising_threshold = readl(data->base + reg->threshold_th0);
200c65d3473STushar Behera 
20159dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210) {
20259dfa54cSAmit Daniel Kachhap 		/* Write temperature code for threshold */
20359dfa54cSAmit Daniel Kachhap 		threshold_code = temp_to_code(data, pdata->threshold);
20459dfa54cSAmit Daniel Kachhap 		writeb(threshold_code,
205b8d582b9SAmit Daniel Kachhap 			data->base + reg->threshold_temp);
20659dfa54cSAmit Daniel Kachhap 		for (i = 0; i < trigger_levs; i++)
207b8d582b9SAmit Daniel Kachhap 			writeb(pdata->trigger_levels[i], data->base +
208b8d582b9SAmit Daniel Kachhap 			reg->threshold_th0 + i * sizeof(reg->threshold_th0));
20959dfa54cSAmit Daniel Kachhap 
21074429c2fSNaveen Krishna Chatradhi 		writel(reg->intclr_rise_mask, data->base + reg->tmu_intclear);
211a0395eeeSAmit Daniel Kachhap 	} else {
21259dfa54cSAmit Daniel Kachhap 		/* Write temperature code for rising and falling threshold */
2137ca04e58SAmit Daniel Kachhap 		for (i = 0;
2147ca04e58SAmit Daniel Kachhap 		i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) {
21559dfa54cSAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
21659dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i]);
217c65d3473STushar Behera 			rising_threshold &= ~(0xff << 8 * i);
21859dfa54cSAmit Daniel Kachhap 			rising_threshold |= threshold_code << 8 * i;
21959dfa54cSAmit Daniel Kachhap 			if (pdata->threshold_falling) {
22059dfa54cSAmit Daniel Kachhap 				threshold_code = temp_to_code(data,
22159dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i] -
22259dfa54cSAmit Daniel Kachhap 						pdata->threshold_falling);
2238131a246SBartlomiej Zolnierkiewicz 				falling_threshold |= threshold_code << 8 * i;
22459dfa54cSAmit Daniel Kachhap 			}
22559dfa54cSAmit Daniel Kachhap 		}
22659dfa54cSAmit Daniel Kachhap 
22759dfa54cSAmit Daniel Kachhap 		writel(rising_threshold,
228b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th0);
22959dfa54cSAmit Daniel Kachhap 		writel(falling_threshold,
230b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th1);
23159dfa54cSAmit Daniel Kachhap 
23274429c2fSNaveen Krishna Chatradhi 		writel((reg->intclr_rise_mask << reg->intclr_rise_shift) |
23374429c2fSNaveen Krishna Chatradhi 			(reg->intclr_fall_mask << reg->intclr_fall_shift),
234b8d582b9SAmit Daniel Kachhap 				data->base + reg->tmu_intclear);
2357ca04e58SAmit Daniel Kachhap 
2367ca04e58SAmit Daniel Kachhap 		/* if last threshold limit is also present */
2377ca04e58SAmit Daniel Kachhap 		i = pdata->max_trigger_level - 1;
2387ca04e58SAmit Daniel Kachhap 		if (pdata->trigger_levels[i] &&
2397ca04e58SAmit Daniel Kachhap 				(pdata->trigger_type[i] == HW_TRIP)) {
2407ca04e58SAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
2417ca04e58SAmit Daniel Kachhap 						pdata->trigger_levels[i]);
242a0395eeeSAmit Daniel Kachhap 			if (i == EXYNOS_MAX_TRIGGER_PER_REG - 1) {
243a0395eeeSAmit Daniel Kachhap 				/* 1-4 level to be assigned in th0 reg */
244c65d3473STushar Behera 				rising_threshold &= ~(0xff << 8 * i);
2457ca04e58SAmit Daniel Kachhap 				rising_threshold |= threshold_code << 8 * i;
2467ca04e58SAmit Daniel Kachhap 				writel(rising_threshold,
2477ca04e58SAmit Daniel Kachhap 					data->base + reg->threshold_th0);
248a0395eeeSAmit Daniel Kachhap 			} else if (i == EXYNOS_MAX_TRIGGER_PER_REG) {
249a0395eeeSAmit Daniel Kachhap 				/* 5th level to be assigned in th2 reg */
250a0395eeeSAmit Daniel Kachhap 				rising_threshold =
251a0395eeeSAmit Daniel Kachhap 				threshold_code << reg->threshold_th3_l0_shift;
252a0395eeeSAmit Daniel Kachhap 				writel(rising_threshold,
253a0395eeeSAmit Daniel Kachhap 					data->base + reg->threshold_th2);
254a0395eeeSAmit Daniel Kachhap 			}
2557ca04e58SAmit Daniel Kachhap 			con = readl(data->base + reg->tmu_ctrl);
2567ca04e58SAmit Daniel Kachhap 			con |= (1 << reg->therm_trip_en_shift);
2577ca04e58SAmit Daniel Kachhap 			writel(con, data->base + reg->tmu_ctrl);
2587ca04e58SAmit Daniel Kachhap 		}
25959dfa54cSAmit Daniel Kachhap 	}
260a0395eeeSAmit Daniel Kachhap 	/*Clear the PMIN in the common TMU register*/
261a0395eeeSAmit Daniel Kachhap 	if (reg->tmu_pmin && !data->id)
2629025d563SNaveen Krishna Chatradhi 		writel(0, data->base_second + reg->tmu_pmin);
26359dfa54cSAmit Daniel Kachhap out:
26459dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
26559dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
26614a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
26714a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
26859dfa54cSAmit Daniel Kachhap 
26959dfa54cSAmit Daniel Kachhap 	return ret;
27059dfa54cSAmit Daniel Kachhap }
27159dfa54cSAmit Daniel Kachhap 
27259dfa54cSAmit Daniel Kachhap static void exynos_tmu_control(struct platform_device *pdev, bool on)
27359dfa54cSAmit Daniel Kachhap {
27459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
27559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
276b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
277d37761ecSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
27859dfa54cSAmit Daniel Kachhap 
27959dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
28059dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
28159dfa54cSAmit Daniel Kachhap 
282b8d582b9SAmit Daniel Kachhap 	con = readl(data->base + reg->tmu_ctrl);
28359dfa54cSAmit Daniel Kachhap 
28486f5362eSLukasz Majewski 	if (pdata->test_mux)
28586f5362eSLukasz Majewski 		con |= (pdata->test_mux << reg->test_mux_addr_shift);
28686f5362eSLukasz Majewski 
287d0a0ce3eSAmit Daniel Kachhap 	if (pdata->reference_voltage) {
288b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
289b8d582b9SAmit Daniel Kachhap 		con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
290d0a0ce3eSAmit Daniel Kachhap 	}
291d0a0ce3eSAmit Daniel Kachhap 
292d0a0ce3eSAmit Daniel Kachhap 	if (pdata->gain) {
293b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
294b8d582b9SAmit Daniel Kachhap 		con |= (pdata->gain << reg->buf_slope_sel_shift);
295d0a0ce3eSAmit Daniel Kachhap 	}
296d0a0ce3eSAmit Daniel Kachhap 
297d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
298b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->therm_trip_mode_mask <<
299b8d582b9SAmit Daniel Kachhap 					reg->therm_trip_mode_shift);
300b8d582b9SAmit Daniel Kachhap 		con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
30159dfa54cSAmit Daniel Kachhap 	}
30259dfa54cSAmit Daniel Kachhap 
30359dfa54cSAmit Daniel Kachhap 	if (on) {
304b8d582b9SAmit Daniel Kachhap 		con |= (1 << reg->core_en_shift);
305d0a0ce3eSAmit Daniel Kachhap 		interrupt_en =
306b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[3] << reg->inten_rise3_shift |
307b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[2] << reg->inten_rise2_shift |
308b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[1] << reg->inten_rise1_shift |
309b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[0] << reg->inten_rise0_shift;
310f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, FALLING_TRIP))
311d0a0ce3eSAmit Daniel Kachhap 			interrupt_en |=
312b8d582b9SAmit Daniel Kachhap 				interrupt_en << reg->inten_fall0_shift;
31359dfa54cSAmit Daniel Kachhap 	} else {
314b8d582b9SAmit Daniel Kachhap 		con &= ~(1 << reg->core_en_shift);
31559dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
31659dfa54cSAmit Daniel Kachhap 	}
317b8d582b9SAmit Daniel Kachhap 	writel(interrupt_en, data->base + reg->tmu_inten);
318b8d582b9SAmit Daniel Kachhap 	writel(con, data->base + reg->tmu_ctrl);
31959dfa54cSAmit Daniel Kachhap 
32059dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
32159dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
32259dfa54cSAmit Daniel Kachhap }
32359dfa54cSAmit Daniel Kachhap 
32459dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data)
32559dfa54cSAmit Daniel Kachhap {
326b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
327b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
32859dfa54cSAmit Daniel Kachhap 	u8 temp_code;
32959dfa54cSAmit Daniel Kachhap 	int temp;
33059dfa54cSAmit Daniel Kachhap 
33159dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
33259dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
33359dfa54cSAmit Daniel Kachhap 
334b8d582b9SAmit Daniel Kachhap 	temp_code = readb(data->base + reg->tmu_cur_temp);
33559dfa54cSAmit Daniel Kachhap 
336*ddb31d43SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS4210)
337*ddb31d43SBartlomiej Zolnierkiewicz 		/* temp_code should range between 75 and 175 */
338*ddb31d43SBartlomiej Zolnierkiewicz 		if (temp_code < 75 || temp_code > 175) {
339*ddb31d43SBartlomiej Zolnierkiewicz 			temp = -ENODATA;
340*ddb31d43SBartlomiej Zolnierkiewicz 			goto out;
341*ddb31d43SBartlomiej Zolnierkiewicz 		}
342*ddb31d43SBartlomiej Zolnierkiewicz 
343*ddb31d43SBartlomiej Zolnierkiewicz 	temp = code_to_temp(data, temp_code);
344*ddb31d43SBartlomiej Zolnierkiewicz out:
34559dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
34659dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
34759dfa54cSAmit Daniel Kachhap 
34859dfa54cSAmit Daniel Kachhap 	return temp;
34959dfa54cSAmit Daniel Kachhap }
35059dfa54cSAmit Daniel Kachhap 
35159dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
35259dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
35359dfa54cSAmit Daniel Kachhap {
35459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
355b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
356b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
357b8d582b9SAmit Daniel Kachhap 	unsigned int val;
35859dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
35959dfa54cSAmit Daniel Kachhap 
360f4dae753SAmit Daniel Kachhap 	if (!TMU_SUPPORTS(pdata, EMULATION))
36159dfa54cSAmit Daniel Kachhap 		goto out;
36259dfa54cSAmit Daniel Kachhap 
36359dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
36459dfa54cSAmit Daniel Kachhap 		goto out;
36559dfa54cSAmit Daniel Kachhap 
36659dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
36759dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
36859dfa54cSAmit Daniel Kachhap 
369b8d582b9SAmit Daniel Kachhap 	val = readl(data->base + reg->emul_con);
37059dfa54cSAmit Daniel Kachhap 
37159dfa54cSAmit Daniel Kachhap 	if (temp) {
37259dfa54cSAmit Daniel Kachhap 		temp /= MCELSIUS;
37359dfa54cSAmit Daniel Kachhap 
374f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
375f4dae753SAmit Daniel Kachhap 			val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift);
376f4dae753SAmit Daniel Kachhap 			val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift);
377f4dae753SAmit Daniel Kachhap 		}
378f4dae753SAmit Daniel Kachhap 		val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift);
379f4dae753SAmit Daniel Kachhap 		val |= (temp_to_code(data, temp) << reg->emul_temp_shift) |
380f4dae753SAmit Daniel Kachhap 			EXYNOS_EMUL_ENABLE;
38159dfa54cSAmit Daniel Kachhap 	} else {
382b8d582b9SAmit Daniel Kachhap 		val &= ~EXYNOS_EMUL_ENABLE;
38359dfa54cSAmit Daniel Kachhap 	}
38459dfa54cSAmit Daniel Kachhap 
385b8d582b9SAmit Daniel Kachhap 	writel(val, data->base + reg->emul_con);
38659dfa54cSAmit Daniel Kachhap 
38759dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
38859dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
38959dfa54cSAmit Daniel Kachhap 	return 0;
39059dfa54cSAmit Daniel Kachhap out:
39159dfa54cSAmit Daniel Kachhap 	return ret;
39259dfa54cSAmit Daniel Kachhap }
39359dfa54cSAmit Daniel Kachhap #else
39459dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
39559dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
39659dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/
39759dfa54cSAmit Daniel Kachhap 
39859dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
39959dfa54cSAmit Daniel Kachhap {
40059dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
40159dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
402b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
403b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
404a0395eeeSAmit Daniel Kachhap 	unsigned int val_irq, val_type;
405a0395eeeSAmit Daniel Kachhap 
40614a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
40714a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
408a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
409a0395eeeSAmit Daniel Kachhap 	if (reg->tmu_irqstatus) {
4109025d563SNaveen Krishna Chatradhi 		val_type = readl(data->base_second + reg->tmu_irqstatus);
411a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
412a0395eeeSAmit Daniel Kachhap 			goto out;
413a0395eeeSAmit Daniel Kachhap 	}
41414a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
41514a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
41659dfa54cSAmit Daniel Kachhap 
417cebe7373SAmit Daniel Kachhap 	exynos_report_trigger(data->reg_conf);
41859dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
41959dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
420b8d582b9SAmit Daniel Kachhap 
421a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
422a4463c4fSAmit Daniel Kachhap 	val_irq = readl(data->base + reg->tmu_intstat);
423a4463c4fSAmit Daniel Kachhap 	/* clear the interrupts */
424a4463c4fSAmit Daniel Kachhap 	writel(val_irq, data->base + reg->tmu_intclear);
425b8d582b9SAmit Daniel Kachhap 
42659dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
42759dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
428a0395eeeSAmit Daniel Kachhap out:
42959dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
43059dfa54cSAmit Daniel Kachhap }
43159dfa54cSAmit Daniel Kachhap 
43259dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
43359dfa54cSAmit Daniel Kachhap {
43459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
43559dfa54cSAmit Daniel Kachhap 
43659dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
43759dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
43859dfa54cSAmit Daniel Kachhap 
43959dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
44059dfa54cSAmit Daniel Kachhap }
44159dfa54cSAmit Daniel Kachhap 
44259dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
44359dfa54cSAmit Daniel Kachhap 	{
4441fe56dc1SChanwoo Choi 		.compatible = "samsung,exynos3250-tmu",
4451fe56dc1SChanwoo Choi 		.data = (void *)EXYNOS3250_TMU_DRV_DATA,
4461fe56dc1SChanwoo Choi 	},
4471fe56dc1SChanwoo Choi 	{
44859dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4210-tmu",
44959dfa54cSAmit Daniel Kachhap 		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
45059dfa54cSAmit Daniel Kachhap 	},
45159dfa54cSAmit Daniel Kachhap 	{
45259dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4412-tmu",
45314ddfaecSLukasz Majewski 		.data = (void *)EXYNOS4412_TMU_DRV_DATA,
45459dfa54cSAmit Daniel Kachhap 	},
45559dfa54cSAmit Daniel Kachhap 	{
45659dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos5250-tmu",
457e6b7991eSAmit Daniel Kachhap 		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
45859dfa54cSAmit Daniel Kachhap 	},
45990542546SAmit Daniel Kachhap 	{
460923488a5SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5260-tmu",
461923488a5SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5260_TMU_DRV_DATA,
462923488a5SNaveen Krishna Chatradhi 	},
463923488a5SNaveen Krishna Chatradhi 	{
46414a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu",
46514a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
46614a11dc7SNaveen Krishna Chatradhi 	},
46714a11dc7SNaveen Krishna Chatradhi 	{
46814a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu-ext-triminfo",
46914a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
47014a11dc7SNaveen Krishna Chatradhi 	},
47114a11dc7SNaveen Krishna Chatradhi 	{
47290542546SAmit Daniel Kachhap 		.compatible = "samsung,exynos5440-tmu",
47390542546SAmit Daniel Kachhap 		.data = (void *)EXYNOS5440_TMU_DRV_DATA,
47490542546SAmit Daniel Kachhap 	},
47559dfa54cSAmit Daniel Kachhap 	{},
47659dfa54cSAmit Daniel Kachhap };
47759dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
47859dfa54cSAmit Daniel Kachhap 
47959dfa54cSAmit Daniel Kachhap static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
480cebe7373SAmit Daniel Kachhap 			struct platform_device *pdev, int id)
48159dfa54cSAmit Daniel Kachhap {
482cebe7373SAmit Daniel Kachhap 	struct  exynos_tmu_init_data *data_table;
483cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *tmu_data;
48459dfa54cSAmit Daniel Kachhap 	const struct of_device_id *match;
48573b5b1d7SSachin Kamat 
48659dfa54cSAmit Daniel Kachhap 	match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
48759dfa54cSAmit Daniel Kachhap 	if (!match)
48859dfa54cSAmit Daniel Kachhap 		return NULL;
489cebe7373SAmit Daniel Kachhap 	data_table = (struct exynos_tmu_init_data *) match->data;
490cebe7373SAmit Daniel Kachhap 	if (!data_table || id >= data_table->tmu_count)
491cebe7373SAmit Daniel Kachhap 		return NULL;
492cebe7373SAmit Daniel Kachhap 	tmu_data = data_table->tmu_data;
493cebe7373SAmit Daniel Kachhap 	return (struct exynos_tmu_platform_data *) (tmu_data + id);
49459dfa54cSAmit Daniel Kachhap }
49559dfa54cSAmit Daniel Kachhap 
496cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
49759dfa54cSAmit Daniel Kachhap {
498cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
499cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
500cebe7373SAmit Daniel Kachhap 	struct resource res;
501498d22f6SAmit Daniel Kachhap 	int ret;
50259dfa54cSAmit Daniel Kachhap 
50373b5b1d7SSachin Kamat 	if (!data || !pdev->dev.of_node)
504cebe7373SAmit Daniel Kachhap 		return -ENODEV;
50559dfa54cSAmit Daniel Kachhap 
506498d22f6SAmit Daniel Kachhap 	/*
507498d22f6SAmit Daniel Kachhap 	 * Try enabling the regulator if found
508498d22f6SAmit Daniel Kachhap 	 * TODO: Add regulator as an SOC feature, so that regulator enable
509498d22f6SAmit Daniel Kachhap 	 * is a compulsory call.
510498d22f6SAmit Daniel Kachhap 	 */
511498d22f6SAmit Daniel Kachhap 	data->regulator = devm_regulator_get(&pdev->dev, "vtmu");
512498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator)) {
513498d22f6SAmit Daniel Kachhap 		ret = regulator_enable(data->regulator);
514498d22f6SAmit Daniel Kachhap 		if (ret) {
515498d22f6SAmit Daniel Kachhap 			dev_err(&pdev->dev, "failed to enable vtmu\n");
516498d22f6SAmit Daniel Kachhap 			return ret;
517498d22f6SAmit Daniel Kachhap 		}
518498d22f6SAmit Daniel Kachhap 	} else {
519498d22f6SAmit Daniel Kachhap 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
520498d22f6SAmit Daniel Kachhap 	}
521498d22f6SAmit Daniel Kachhap 
522cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
523cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
524cebe7373SAmit Daniel Kachhap 		data->id = 0;
525cebe7373SAmit Daniel Kachhap 
526cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
527cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
528cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
529cebe7373SAmit Daniel Kachhap 		return -ENODEV;
530cebe7373SAmit Daniel Kachhap 	}
531cebe7373SAmit Daniel Kachhap 
532cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
533cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
534cebe7373SAmit Daniel Kachhap 		return -ENODEV;
535cebe7373SAmit Daniel Kachhap 	}
536cebe7373SAmit Daniel Kachhap 
537cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
538cebe7373SAmit Daniel Kachhap 	if (!data->base) {
539cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
540cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
541cebe7373SAmit Daniel Kachhap 	}
542cebe7373SAmit Daniel Kachhap 
543cebe7373SAmit Daniel Kachhap 	pdata = exynos_get_driver_data(pdev, data->id);
54459dfa54cSAmit Daniel Kachhap 	if (!pdata) {
54559dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "No platform init data supplied.\n");
54659dfa54cSAmit Daniel Kachhap 		return -ENODEV;
54759dfa54cSAmit Daniel Kachhap 	}
548cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
549d9b6ee14SAmit Daniel Kachhap 	/*
550d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
551d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
552d9b6ee14SAmit Daniel Kachhap 	 */
5539025d563SNaveen Krishna Chatradhi 	if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE))
554d9b6ee14SAmit Daniel Kachhap 		return 0;
555d9b6ee14SAmit Daniel Kachhap 
556d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
557d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
558d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
559d9b6ee14SAmit Daniel Kachhap 	}
560d9b6ee14SAmit Daniel Kachhap 
5619025d563SNaveen Krishna Chatradhi 	data->base_second = devm_ioremap(&pdev->dev, res.start,
562d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
5639025d563SNaveen Krishna Chatradhi 	if (!data->base_second) {
564d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
565d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
566d9b6ee14SAmit Daniel Kachhap 	}
567cebe7373SAmit Daniel Kachhap 
568cebe7373SAmit Daniel Kachhap 	return 0;
569cebe7373SAmit Daniel Kachhap }
570cebe7373SAmit Daniel Kachhap 
571cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
572cebe7373SAmit Daniel Kachhap {
573cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data;
574cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
575cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *sensor_conf;
576cebe7373SAmit Daniel Kachhap 	int ret, i;
577cebe7373SAmit Daniel Kachhap 
57859dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
57959dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
5802a9675b3SJingoo Han 	if (!data)
58159dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
58259dfa54cSAmit Daniel Kachhap 
583cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
584cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
585cebe7373SAmit Daniel Kachhap 
586cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
587cebe7373SAmit Daniel Kachhap 	if (ret)
588cebe7373SAmit Daniel Kachhap 		return ret;
589cebe7373SAmit Daniel Kachhap 
590cebe7373SAmit Daniel Kachhap 	pdata = data->pdata;
59159dfa54cSAmit Daniel Kachhap 
59259dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
59359dfa54cSAmit Daniel Kachhap 
59459dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
59559dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
59659dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
59759dfa54cSAmit Daniel Kachhap 		return  PTR_ERR(data->clk);
59859dfa54cSAmit Daniel Kachhap 	}
59959dfa54cSAmit Daniel Kachhap 
60014a11dc7SNaveen Krishna Chatradhi 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
60114a11dc7SNaveen Krishna Chatradhi 	if (IS_ERR(data->clk_sec)) {
60214a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
60314a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
60414a11dc7SNaveen Krishna Chatradhi 			return PTR_ERR(data->clk_sec);
60514a11dc7SNaveen Krishna Chatradhi 		}
60614a11dc7SNaveen Krishna Chatradhi 	} else {
60714a11dc7SNaveen Krishna Chatradhi 		ret = clk_prepare(data->clk_sec);
60814a11dc7SNaveen Krishna Chatradhi 		if (ret) {
60914a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get clock\n");
61059dfa54cSAmit Daniel Kachhap 			return ret;
61114a11dc7SNaveen Krishna Chatradhi 		}
61214a11dc7SNaveen Krishna Chatradhi 	}
61314a11dc7SNaveen Krishna Chatradhi 
61414a11dc7SNaveen Krishna Chatradhi 	ret = clk_prepare(data->clk);
61514a11dc7SNaveen Krishna Chatradhi 	if (ret) {
61614a11dc7SNaveen Krishna Chatradhi 		dev_err(&pdev->dev, "Failed to get clock\n");
61714a11dc7SNaveen Krishna Chatradhi 		goto err_clk_sec;
61814a11dc7SNaveen Krishna Chatradhi 	}
61959dfa54cSAmit Daniel Kachhap 
6201fe56dc1SChanwoo Choi 	if (pdata->type == SOC_ARCH_EXYNOS3250 ||
6211fe56dc1SChanwoo Choi 	    pdata->type == SOC_ARCH_EXYNOS4210 ||
62214ddfaecSLukasz Majewski 	    pdata->type == SOC_ARCH_EXYNOS4412 ||
62314ddfaecSLukasz Majewski 	    pdata->type == SOC_ARCH_EXYNOS5250 ||
624923488a5SNaveen Krishna Chatradhi 	    pdata->type == SOC_ARCH_EXYNOS5260 ||
62514a11dc7SNaveen Krishna Chatradhi 	    pdata->type == SOC_ARCH_EXYNOS5420_TRIMINFO ||
626a0395eeeSAmit Daniel Kachhap 	    pdata->type == SOC_ARCH_EXYNOS5440)
62759dfa54cSAmit Daniel Kachhap 		data->soc = pdata->type;
62859dfa54cSAmit Daniel Kachhap 	else {
62959dfa54cSAmit Daniel Kachhap 		ret = -EINVAL;
63059dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Platform not supported\n");
63159dfa54cSAmit Daniel Kachhap 		goto err_clk;
63259dfa54cSAmit Daniel Kachhap 	}
63359dfa54cSAmit Daniel Kachhap 
63459dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
63559dfa54cSAmit Daniel Kachhap 	if (ret) {
63659dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
63759dfa54cSAmit Daniel Kachhap 		goto err_clk;
63859dfa54cSAmit Daniel Kachhap 	}
63959dfa54cSAmit Daniel Kachhap 
64059dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
64159dfa54cSAmit Daniel Kachhap 
642cebe7373SAmit Daniel Kachhap 	/* Allocate a structure to register with the exynos core thermal */
643cebe7373SAmit Daniel Kachhap 	sensor_conf = devm_kzalloc(&pdev->dev,
644cebe7373SAmit Daniel Kachhap 				sizeof(struct thermal_sensor_conf), GFP_KERNEL);
645cebe7373SAmit Daniel Kachhap 	if (!sensor_conf) {
646cebe7373SAmit Daniel Kachhap 		ret = -ENOMEM;
647cebe7373SAmit Daniel Kachhap 		goto err_clk;
648cebe7373SAmit Daniel Kachhap 	}
649cebe7373SAmit Daniel Kachhap 	sprintf(sensor_conf->name, "therm_zone%d", data->id);
650cebe7373SAmit Daniel Kachhap 	sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
651cebe7373SAmit Daniel Kachhap 	sensor_conf->write_emul_temp =
652cebe7373SAmit Daniel Kachhap 		(int (*)(void *, unsigned long))exynos_tmu_set_emulation;
653cebe7373SAmit Daniel Kachhap 	sensor_conf->driver_data = data;
654cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
655bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
656bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[3];
65759dfa54cSAmit Daniel Kachhap 
658cebe7373SAmit Daniel Kachhap 	for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
659cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_val[i] =
66059dfa54cSAmit Daniel Kachhap 			pdata->threshold + pdata->trigger_levels[i];
661cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_type[i] =
6625c3cf552SAmit Daniel Kachhap 					pdata->trigger_type[i];
6635c3cf552SAmit Daniel Kachhap 	}
66459dfa54cSAmit Daniel Kachhap 
665cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
66659dfa54cSAmit Daniel Kachhap 
667cebe7373SAmit Daniel Kachhap 	sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
66859dfa54cSAmit Daniel Kachhap 	for (i = 0; i < pdata->freq_tab_count; i++) {
669cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].freq_clip_max =
67059dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].freq_clip_max;
671cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].temp_level =
67259dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].temp_level;
67359dfa54cSAmit Daniel Kachhap 	}
674cebe7373SAmit Daniel Kachhap 	sensor_conf->dev = &pdev->dev;
675cebe7373SAmit Daniel Kachhap 	/* Register the sensor with thermal management interface */
676cebe7373SAmit Daniel Kachhap 	ret = exynos_register_thermal(sensor_conf);
67759dfa54cSAmit Daniel Kachhap 	if (ret) {
67859dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to register thermal interface\n");
67959dfa54cSAmit Daniel Kachhap 		goto err_clk;
68059dfa54cSAmit Daniel Kachhap 	}
681cebe7373SAmit Daniel Kachhap 	data->reg_conf = sensor_conf;
682cebe7373SAmit Daniel Kachhap 
683cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
684cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
685cebe7373SAmit Daniel Kachhap 	if (ret) {
686cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
687cebe7373SAmit Daniel Kachhap 		goto err_clk;
688cebe7373SAmit Daniel Kachhap 	}
68959dfa54cSAmit Daniel Kachhap 
69059dfa54cSAmit Daniel Kachhap 	return 0;
69159dfa54cSAmit Daniel Kachhap err_clk:
69259dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
69314a11dc7SNaveen Krishna Chatradhi err_clk_sec:
69414a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
69514a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
69659dfa54cSAmit Daniel Kachhap 	return ret;
69759dfa54cSAmit Daniel Kachhap }
69859dfa54cSAmit Daniel Kachhap 
69959dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
70059dfa54cSAmit Daniel Kachhap {
70159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
70259dfa54cSAmit Daniel Kachhap 
703cebe7373SAmit Daniel Kachhap 	exynos_unregister_thermal(data->reg_conf);
70459dfa54cSAmit Daniel Kachhap 
7054215688eSBartlomiej Zolnierkiewicz 	exynos_tmu_control(pdev, false);
7064215688eSBartlomiej Zolnierkiewicz 
70759dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
70814a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
70914a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
71059dfa54cSAmit Daniel Kachhap 
711498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator))
712498d22f6SAmit Daniel Kachhap 		regulator_disable(data->regulator);
713498d22f6SAmit Daniel Kachhap 
71459dfa54cSAmit Daniel Kachhap 	return 0;
71559dfa54cSAmit Daniel Kachhap }
71659dfa54cSAmit Daniel Kachhap 
71759dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
71859dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
71959dfa54cSAmit Daniel Kachhap {
72059dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
72159dfa54cSAmit Daniel Kachhap 
72259dfa54cSAmit Daniel Kachhap 	return 0;
72359dfa54cSAmit Daniel Kachhap }
72459dfa54cSAmit Daniel Kachhap 
72559dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
72659dfa54cSAmit Daniel Kachhap {
72759dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
72859dfa54cSAmit Daniel Kachhap 
72959dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
73059dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
73159dfa54cSAmit Daniel Kachhap 
73259dfa54cSAmit Daniel Kachhap 	return 0;
73359dfa54cSAmit Daniel Kachhap }
73459dfa54cSAmit Daniel Kachhap 
73559dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
73659dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
73759dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
73859dfa54cSAmit Daniel Kachhap #else
73959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
74059dfa54cSAmit Daniel Kachhap #endif
74159dfa54cSAmit Daniel Kachhap 
74259dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
74359dfa54cSAmit Daniel Kachhap 	.driver = {
74459dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
74559dfa54cSAmit Daniel Kachhap 		.owner  = THIS_MODULE,
74659dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
74773b5b1d7SSachin Kamat 		.of_match_table = exynos_tmu_match,
74859dfa54cSAmit Daniel Kachhap 	},
74959dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
75059dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
75159dfa54cSAmit Daniel Kachhap };
75259dfa54cSAmit Daniel Kachhap 
75359dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
75459dfa54cSAmit Daniel Kachhap 
75559dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
75659dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
75759dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
75859dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
759