xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision a0395eee7ca19623f69f067cc8cfbd4385d85dad)
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>
3259dfa54cSAmit Daniel Kachhap 
3359dfa54cSAmit Daniel Kachhap #include "exynos_thermal_common.h"
340c1836a6SAmit Daniel Kachhap #include "exynos_tmu.h"
35e6b7991eSAmit Daniel Kachhap #include "exynos_tmu_data.h"
3659dfa54cSAmit Daniel Kachhap 
37cebe7373SAmit Daniel Kachhap /**
38cebe7373SAmit Daniel Kachhap  * struct exynos_tmu_data : A structure to hold the private data of the TMU
39cebe7373SAmit Daniel Kachhap 	driver
40cebe7373SAmit Daniel Kachhap  * @id: identifier of the one instance of the TMU controller.
41cebe7373SAmit Daniel Kachhap  * @pdata: pointer to the tmu platform/configuration data
42cebe7373SAmit Daniel Kachhap  * @base: base address of the single instance of the TMU controller.
43d9b6ee14SAmit Daniel Kachhap  * @base_common: base address of the common registers of the TMU controller.
44cebe7373SAmit Daniel Kachhap  * @irq: irq number of the TMU controller.
45cebe7373SAmit Daniel Kachhap  * @soc: id of the SOC type.
46cebe7373SAmit Daniel Kachhap  * @irq_work: pointer to the irq work structure.
47cebe7373SAmit Daniel Kachhap  * @lock: lock to implement synchronization.
48cebe7373SAmit Daniel Kachhap  * @clk: pointer to the clock structure.
49cebe7373SAmit Daniel Kachhap  * @temp_error1: fused value of the first point trim.
50cebe7373SAmit Daniel Kachhap  * @temp_error2: fused value of the second point trim.
51cebe7373SAmit Daniel Kachhap  * @reg_conf: pointer to structure to register with core thermal.
52cebe7373SAmit Daniel Kachhap  */
5359dfa54cSAmit Daniel Kachhap struct exynos_tmu_data {
54cebe7373SAmit Daniel Kachhap 	int id;
5559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
5659dfa54cSAmit Daniel Kachhap 	void __iomem *base;
57d9b6ee14SAmit Daniel Kachhap 	void __iomem *base_common;
5859dfa54cSAmit Daniel Kachhap 	int irq;
5959dfa54cSAmit Daniel Kachhap 	enum soc_type soc;
6059dfa54cSAmit Daniel Kachhap 	struct work_struct irq_work;
6159dfa54cSAmit Daniel Kachhap 	struct mutex lock;
6259dfa54cSAmit Daniel Kachhap 	struct clk *clk;
6359dfa54cSAmit Daniel Kachhap 	u8 temp_error1, temp_error2;
64cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *reg_conf;
6559dfa54cSAmit Daniel Kachhap };
6659dfa54cSAmit Daniel Kachhap 
6759dfa54cSAmit Daniel Kachhap /*
6859dfa54cSAmit Daniel Kachhap  * TMU treats temperature as a mapped temperature code.
6959dfa54cSAmit Daniel Kachhap  * The temperature is converted differently depending on the calibration type.
7059dfa54cSAmit Daniel Kachhap  */
7159dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
7259dfa54cSAmit Daniel Kachhap {
7359dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
7459dfa54cSAmit Daniel Kachhap 	int temp_code;
7559dfa54cSAmit Daniel Kachhap 
7659dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210)
7759dfa54cSAmit Daniel Kachhap 		/* temp should range between 25 and 125 */
7859dfa54cSAmit Daniel Kachhap 		if (temp < 25 || temp > 125) {
7959dfa54cSAmit Daniel Kachhap 			temp_code = -EINVAL;
8059dfa54cSAmit Daniel Kachhap 			goto out;
8159dfa54cSAmit Daniel Kachhap 		}
8259dfa54cSAmit Daniel Kachhap 
8359dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
8459dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
85bb34b4c8SAmit Daniel Kachhap 		temp_code = (temp - pdata->first_point_trim) *
8659dfa54cSAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) /
87bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) +
88bb34b4c8SAmit Daniel Kachhap 			data->temp_error1;
8959dfa54cSAmit Daniel Kachhap 		break;
9059dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
91bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
9259dfa54cSAmit Daniel Kachhap 		break;
9359dfa54cSAmit Daniel Kachhap 	default:
94bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + pdata->default_temp_offset;
9559dfa54cSAmit Daniel Kachhap 		break;
9659dfa54cSAmit Daniel Kachhap 	}
9759dfa54cSAmit Daniel Kachhap out:
9859dfa54cSAmit Daniel Kachhap 	return temp_code;
9959dfa54cSAmit Daniel Kachhap }
10059dfa54cSAmit Daniel Kachhap 
10159dfa54cSAmit Daniel Kachhap /*
10259dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
10359dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
10459dfa54cSAmit Daniel Kachhap  */
10559dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
10659dfa54cSAmit Daniel Kachhap {
10759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
10859dfa54cSAmit Daniel Kachhap 	int temp;
10959dfa54cSAmit Daniel Kachhap 
11059dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210)
11159dfa54cSAmit Daniel Kachhap 		/* temp_code should range between 75 and 175 */
11259dfa54cSAmit Daniel Kachhap 		if (temp_code < 75 || temp_code > 175) {
11359dfa54cSAmit Daniel Kachhap 			temp = -ENODATA;
11459dfa54cSAmit Daniel Kachhap 			goto out;
11559dfa54cSAmit Daniel Kachhap 		}
11659dfa54cSAmit Daniel Kachhap 
11759dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
11859dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
119bb34b4c8SAmit Daniel Kachhap 		temp = (temp_code - data->temp_error1) *
120bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) /
121bb34b4c8SAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) +
122bb34b4c8SAmit Daniel Kachhap 			pdata->first_point_trim;
12359dfa54cSAmit Daniel Kachhap 		break;
12459dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
125bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
12659dfa54cSAmit Daniel Kachhap 		break;
12759dfa54cSAmit Daniel Kachhap 	default:
128bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - pdata->default_temp_offset;
12959dfa54cSAmit Daniel Kachhap 		break;
13059dfa54cSAmit Daniel Kachhap 	}
13159dfa54cSAmit Daniel Kachhap out:
13259dfa54cSAmit Daniel Kachhap 	return temp;
13359dfa54cSAmit Daniel Kachhap }
13459dfa54cSAmit Daniel Kachhap 
13559dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
13659dfa54cSAmit Daniel Kachhap {
13759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
13859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
139b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
1407ca04e58SAmit Daniel Kachhap 	unsigned int status, trim_info = 0, con;
14159dfa54cSAmit Daniel Kachhap 	unsigned int rising_threshold = 0, falling_threshold = 0;
14259dfa54cSAmit Daniel Kachhap 	int ret = 0, threshold_code, i, trigger_levs = 0;
14359dfa54cSAmit Daniel Kachhap 
14459dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
14559dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
14659dfa54cSAmit Daniel Kachhap 
147f4dae753SAmit Daniel Kachhap 	if (TMU_SUPPORTS(pdata, READY_STATUS)) {
148b8d582b9SAmit Daniel Kachhap 		status = readb(data->base + reg->tmu_status);
14959dfa54cSAmit Daniel Kachhap 		if (!status) {
15059dfa54cSAmit Daniel Kachhap 			ret = -EBUSY;
15159dfa54cSAmit Daniel Kachhap 			goto out;
15259dfa54cSAmit Daniel Kachhap 		}
153f4dae753SAmit Daniel Kachhap 	}
15459dfa54cSAmit Daniel Kachhap 
155f4dae753SAmit Daniel Kachhap 	if (TMU_SUPPORTS(pdata, TRIM_RELOAD))
156b8d582b9SAmit Daniel Kachhap 		__raw_writel(1, data->base + reg->triminfo_ctrl);
157b8d582b9SAmit Daniel Kachhap 
15859dfa54cSAmit Daniel Kachhap 	/* Save trimming info in order to perform calibration */
159*a0395eeeSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS5440) {
160*a0395eeeSAmit Daniel Kachhap 		/*
161*a0395eeeSAmit Daniel Kachhap 		 * For exynos5440 soc triminfo value is swapped between TMU0 and
162*a0395eeeSAmit Daniel Kachhap 		 * TMU2, so the below logic is needed.
163*a0395eeeSAmit Daniel Kachhap 		 */
164*a0395eeeSAmit Daniel Kachhap 		switch (data->id) {
165*a0395eeeSAmit Daniel Kachhap 		case 0:
166*a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base +
167*a0395eeeSAmit Daniel Kachhap 			EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
168*a0395eeeSAmit Daniel Kachhap 			break;
169*a0395eeeSAmit Daniel Kachhap 		case 1:
170b8d582b9SAmit Daniel Kachhap 			trim_info = readl(data->base + reg->triminfo_data);
171*a0395eeeSAmit Daniel Kachhap 			break;
172*a0395eeeSAmit Daniel Kachhap 		case 2:
173*a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base -
174*a0395eeeSAmit Daniel Kachhap 			EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
175*a0395eeeSAmit Daniel Kachhap 		}
176*a0395eeeSAmit Daniel Kachhap 	} else {
177*a0395eeeSAmit Daniel Kachhap 		trim_info = readl(data->base + reg->triminfo_data);
178*a0395eeeSAmit Daniel Kachhap 	}
179b8d582b9SAmit Daniel Kachhap 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
180b8d582b9SAmit Daniel Kachhap 	data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) &
181b8d582b9SAmit Daniel Kachhap 				EXYNOS_TMU_TEMP_MASK);
18259dfa54cSAmit Daniel Kachhap 
183bb34b4c8SAmit Daniel Kachhap 	if ((pdata->min_efuse_value > data->temp_error1) ||
184bb34b4c8SAmit Daniel Kachhap 			(data->temp_error1 > pdata->max_efuse_value) ||
18559dfa54cSAmit Daniel Kachhap 			(data->temp_error2 != 0))
18659dfa54cSAmit Daniel Kachhap 		data->temp_error1 = pdata->efuse_value;
18759dfa54cSAmit Daniel Kachhap 
1887ca04e58SAmit Daniel Kachhap 	if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) {
1897ca04e58SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Invalid max trigger level\n");
1907ca04e58SAmit Daniel Kachhap 		goto out;
1917ca04e58SAmit Daniel Kachhap 	}
1927ca04e58SAmit Daniel Kachhap 
1937ca04e58SAmit Daniel Kachhap 	for (i = 0; i < pdata->max_trigger_level; i++) {
1947ca04e58SAmit Daniel Kachhap 		if (!pdata->trigger_levels[i])
1957ca04e58SAmit Daniel Kachhap 			continue;
1967ca04e58SAmit Daniel Kachhap 
1977ca04e58SAmit Daniel Kachhap 		if ((pdata->trigger_type[i] == HW_TRIP) &&
1987ca04e58SAmit Daniel Kachhap 		(!pdata->trigger_levels[pdata->max_trigger_level - 1])) {
1997ca04e58SAmit Daniel Kachhap 			dev_err(&pdev->dev, "Invalid hw trigger level\n");
2007ca04e58SAmit Daniel Kachhap 			ret = -EINVAL;
2017ca04e58SAmit Daniel Kachhap 			goto out;
2027ca04e58SAmit Daniel Kachhap 		}
2037ca04e58SAmit Daniel Kachhap 
2047ca04e58SAmit Daniel Kachhap 		/* Count trigger levels except the HW trip*/
2057ca04e58SAmit Daniel Kachhap 		if (!(pdata->trigger_type[i] == HW_TRIP))
20659dfa54cSAmit Daniel Kachhap 			trigger_levs++;
2077ca04e58SAmit Daniel Kachhap 	}
20859dfa54cSAmit Daniel Kachhap 
20959dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210) {
21059dfa54cSAmit Daniel Kachhap 		/* Write temperature code for threshold */
21159dfa54cSAmit Daniel Kachhap 		threshold_code = temp_to_code(data, pdata->threshold);
21259dfa54cSAmit Daniel Kachhap 		if (threshold_code < 0) {
21359dfa54cSAmit Daniel Kachhap 			ret = threshold_code;
21459dfa54cSAmit Daniel Kachhap 			goto out;
21559dfa54cSAmit Daniel Kachhap 		}
21659dfa54cSAmit Daniel Kachhap 		writeb(threshold_code,
217b8d582b9SAmit Daniel Kachhap 			data->base + reg->threshold_temp);
21859dfa54cSAmit Daniel Kachhap 		for (i = 0; i < trigger_levs; i++)
219b8d582b9SAmit Daniel Kachhap 			writeb(pdata->trigger_levels[i], data->base +
220b8d582b9SAmit Daniel Kachhap 			reg->threshold_th0 + i * sizeof(reg->threshold_th0));
22159dfa54cSAmit Daniel Kachhap 
222b8d582b9SAmit Daniel Kachhap 		writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);
223*a0395eeeSAmit Daniel Kachhap 	} else {
22459dfa54cSAmit Daniel Kachhap 		/* Write temperature code for rising and falling threshold */
2257ca04e58SAmit Daniel Kachhap 		for (i = 0;
2267ca04e58SAmit Daniel Kachhap 		i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) {
22759dfa54cSAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
22859dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i]);
22959dfa54cSAmit Daniel Kachhap 			if (threshold_code < 0) {
23059dfa54cSAmit Daniel Kachhap 				ret = threshold_code;
23159dfa54cSAmit Daniel Kachhap 				goto out;
23259dfa54cSAmit Daniel Kachhap 			}
23359dfa54cSAmit Daniel Kachhap 			rising_threshold |= threshold_code << 8 * i;
23459dfa54cSAmit Daniel Kachhap 			if (pdata->threshold_falling) {
23559dfa54cSAmit Daniel Kachhap 				threshold_code = temp_to_code(data,
23659dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i] -
23759dfa54cSAmit Daniel Kachhap 						pdata->threshold_falling);
23859dfa54cSAmit Daniel Kachhap 				if (threshold_code > 0)
23959dfa54cSAmit Daniel Kachhap 					falling_threshold |=
24059dfa54cSAmit Daniel Kachhap 						threshold_code << 8 * i;
24159dfa54cSAmit Daniel Kachhap 			}
24259dfa54cSAmit Daniel Kachhap 		}
24359dfa54cSAmit Daniel Kachhap 
24459dfa54cSAmit Daniel Kachhap 		writel(rising_threshold,
245b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th0);
24659dfa54cSAmit Daniel Kachhap 		writel(falling_threshold,
247b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th1);
24859dfa54cSAmit Daniel Kachhap 
249b8d582b9SAmit Daniel Kachhap 		writel((reg->inten_rise_mask << reg->inten_rise_shift) |
250b8d582b9SAmit Daniel Kachhap 			(reg->inten_fall_mask << reg->inten_fall_shift),
251b8d582b9SAmit Daniel Kachhap 				data->base + reg->tmu_intclear);
2527ca04e58SAmit Daniel Kachhap 
2537ca04e58SAmit Daniel Kachhap 		/* if last threshold limit is also present */
2547ca04e58SAmit Daniel Kachhap 		i = pdata->max_trigger_level - 1;
2557ca04e58SAmit Daniel Kachhap 		if (pdata->trigger_levels[i] &&
2567ca04e58SAmit Daniel Kachhap 				(pdata->trigger_type[i] == HW_TRIP)) {
2577ca04e58SAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
2587ca04e58SAmit Daniel Kachhap 						pdata->trigger_levels[i]);
2597ca04e58SAmit Daniel Kachhap 			if (threshold_code < 0) {
2607ca04e58SAmit Daniel Kachhap 				ret = threshold_code;
2617ca04e58SAmit Daniel Kachhap 				goto out;
2627ca04e58SAmit Daniel Kachhap 			}
263*a0395eeeSAmit Daniel Kachhap 			if (i == EXYNOS_MAX_TRIGGER_PER_REG - 1) {
264*a0395eeeSAmit Daniel Kachhap 				/* 1-4 level to be assigned in th0 reg */
2657ca04e58SAmit Daniel Kachhap 				rising_threshold |= threshold_code << 8 * i;
2667ca04e58SAmit Daniel Kachhap 				writel(rising_threshold,
2677ca04e58SAmit Daniel Kachhap 					data->base + reg->threshold_th0);
268*a0395eeeSAmit Daniel Kachhap 			} else if (i == EXYNOS_MAX_TRIGGER_PER_REG) {
269*a0395eeeSAmit Daniel Kachhap 				/* 5th level to be assigned in th2 reg */
270*a0395eeeSAmit Daniel Kachhap 				rising_threshold =
271*a0395eeeSAmit Daniel Kachhap 				threshold_code << reg->threshold_th3_l0_shift;
272*a0395eeeSAmit Daniel Kachhap 				writel(rising_threshold,
273*a0395eeeSAmit Daniel Kachhap 					data->base + reg->threshold_th2);
274*a0395eeeSAmit Daniel Kachhap 			}
2757ca04e58SAmit Daniel Kachhap 			con = readl(data->base + reg->tmu_ctrl);
2767ca04e58SAmit Daniel Kachhap 			con |= (1 << reg->therm_trip_en_shift);
2777ca04e58SAmit Daniel Kachhap 			writel(con, data->base + reg->tmu_ctrl);
2787ca04e58SAmit Daniel Kachhap 		}
27959dfa54cSAmit Daniel Kachhap 	}
280*a0395eeeSAmit Daniel Kachhap 	/*Clear the PMIN in the common TMU register*/
281*a0395eeeSAmit Daniel Kachhap 	if (reg->tmu_pmin && !data->id)
282*a0395eeeSAmit Daniel Kachhap 		writel(0, data->base_common + reg->tmu_pmin);
28359dfa54cSAmit Daniel Kachhap out:
28459dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
28559dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
28659dfa54cSAmit Daniel Kachhap 
28759dfa54cSAmit Daniel Kachhap 	return ret;
28859dfa54cSAmit Daniel Kachhap }
28959dfa54cSAmit Daniel Kachhap 
29059dfa54cSAmit Daniel Kachhap static void exynos_tmu_control(struct platform_device *pdev, bool on)
29159dfa54cSAmit Daniel Kachhap {
29259dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
29359dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
294b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
29559dfa54cSAmit Daniel Kachhap 	unsigned int con, interrupt_en;
29659dfa54cSAmit Daniel Kachhap 
29759dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
29859dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
29959dfa54cSAmit Daniel Kachhap 
300b8d582b9SAmit Daniel Kachhap 	con = readl(data->base + reg->tmu_ctrl);
30159dfa54cSAmit Daniel Kachhap 
302d0a0ce3eSAmit Daniel Kachhap 	if (pdata->reference_voltage) {
303b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
304b8d582b9SAmit Daniel Kachhap 		con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
305d0a0ce3eSAmit Daniel Kachhap 	}
306d0a0ce3eSAmit Daniel Kachhap 
307d0a0ce3eSAmit Daniel Kachhap 	if (pdata->gain) {
308b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
309b8d582b9SAmit Daniel Kachhap 		con |= (pdata->gain << reg->buf_slope_sel_shift);
310d0a0ce3eSAmit Daniel Kachhap 	}
311d0a0ce3eSAmit Daniel Kachhap 
312d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
313b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->therm_trip_mode_mask <<
314b8d582b9SAmit Daniel Kachhap 					reg->therm_trip_mode_shift);
315b8d582b9SAmit Daniel Kachhap 		con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
31659dfa54cSAmit Daniel Kachhap 	}
31759dfa54cSAmit Daniel Kachhap 
31859dfa54cSAmit Daniel Kachhap 	if (on) {
319b8d582b9SAmit Daniel Kachhap 		con |= (1 << reg->core_en_shift);
320d0a0ce3eSAmit Daniel Kachhap 		interrupt_en =
321b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[3] << reg->inten_rise3_shift |
322b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[2] << reg->inten_rise2_shift |
323b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[1] << reg->inten_rise1_shift |
324b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[0] << reg->inten_rise0_shift;
325f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, FALLING_TRIP))
326d0a0ce3eSAmit Daniel Kachhap 			interrupt_en |=
327b8d582b9SAmit Daniel Kachhap 				interrupt_en << reg->inten_fall0_shift;
32859dfa54cSAmit Daniel Kachhap 	} else {
329b8d582b9SAmit Daniel Kachhap 		con &= ~(1 << reg->core_en_shift);
33059dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
33159dfa54cSAmit Daniel Kachhap 	}
332b8d582b9SAmit Daniel Kachhap 	writel(interrupt_en, data->base + reg->tmu_inten);
333b8d582b9SAmit Daniel Kachhap 	writel(con, data->base + reg->tmu_ctrl);
33459dfa54cSAmit Daniel Kachhap 
33559dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
33659dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
33759dfa54cSAmit Daniel Kachhap }
33859dfa54cSAmit Daniel Kachhap 
33959dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data)
34059dfa54cSAmit Daniel Kachhap {
341b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
342b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
34359dfa54cSAmit Daniel Kachhap 	u8 temp_code;
34459dfa54cSAmit Daniel Kachhap 	int temp;
34559dfa54cSAmit Daniel Kachhap 
34659dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
34759dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
34859dfa54cSAmit Daniel Kachhap 
349b8d582b9SAmit Daniel Kachhap 	temp_code = readb(data->base + reg->tmu_cur_temp);
35059dfa54cSAmit Daniel Kachhap 	temp = code_to_temp(data, temp_code);
35159dfa54cSAmit Daniel Kachhap 
35259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
35359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
35459dfa54cSAmit Daniel Kachhap 
35559dfa54cSAmit Daniel Kachhap 	return temp;
35659dfa54cSAmit Daniel Kachhap }
35759dfa54cSAmit Daniel Kachhap 
35859dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
35959dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
36059dfa54cSAmit Daniel Kachhap {
36159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
362b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
363b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
364b8d582b9SAmit Daniel Kachhap 	unsigned int val;
36559dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
36659dfa54cSAmit Daniel Kachhap 
367f4dae753SAmit Daniel Kachhap 	if (!TMU_SUPPORTS(pdata, EMULATION))
36859dfa54cSAmit Daniel Kachhap 		goto out;
36959dfa54cSAmit Daniel Kachhap 
37059dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
37159dfa54cSAmit Daniel Kachhap 		goto out;
37259dfa54cSAmit Daniel Kachhap 
37359dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
37459dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
37559dfa54cSAmit Daniel Kachhap 
376b8d582b9SAmit Daniel Kachhap 	val = readl(data->base + reg->emul_con);
37759dfa54cSAmit Daniel Kachhap 
37859dfa54cSAmit Daniel Kachhap 	if (temp) {
37959dfa54cSAmit Daniel Kachhap 		temp /= MCELSIUS;
38059dfa54cSAmit Daniel Kachhap 
381f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
382f4dae753SAmit Daniel Kachhap 			val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift);
383f4dae753SAmit Daniel Kachhap 			val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift);
384f4dae753SAmit Daniel Kachhap 		}
385f4dae753SAmit Daniel Kachhap 		val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift);
386f4dae753SAmit Daniel Kachhap 		val |= (temp_to_code(data, temp) << reg->emul_temp_shift) |
387f4dae753SAmit Daniel Kachhap 			EXYNOS_EMUL_ENABLE;
38859dfa54cSAmit Daniel Kachhap 	} else {
389b8d582b9SAmit Daniel Kachhap 		val &= ~EXYNOS_EMUL_ENABLE;
39059dfa54cSAmit Daniel Kachhap 	}
39159dfa54cSAmit Daniel Kachhap 
392b8d582b9SAmit Daniel Kachhap 	writel(val, data->base + reg->emul_con);
39359dfa54cSAmit Daniel Kachhap 
39459dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
39559dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
39659dfa54cSAmit Daniel Kachhap 	return 0;
39759dfa54cSAmit Daniel Kachhap out:
39859dfa54cSAmit Daniel Kachhap 	return ret;
39959dfa54cSAmit Daniel Kachhap }
40059dfa54cSAmit Daniel Kachhap #else
40159dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
40259dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
40359dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/
40459dfa54cSAmit Daniel Kachhap 
40559dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
40659dfa54cSAmit Daniel Kachhap {
40759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
40859dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
409b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
410b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
411*a0395eeeSAmit Daniel Kachhap 	unsigned int val_irq, val_type;
412*a0395eeeSAmit Daniel Kachhap 
413*a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
414*a0395eeeSAmit Daniel Kachhap 	if (reg->tmu_irqstatus) {
415*a0395eeeSAmit Daniel Kachhap 		val_type = readl(data->base_common + reg->tmu_irqstatus);
416*a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
417*a0395eeeSAmit Daniel Kachhap 			goto out;
418*a0395eeeSAmit Daniel Kachhap 	}
41959dfa54cSAmit Daniel Kachhap 
420cebe7373SAmit Daniel Kachhap 	exynos_report_trigger(data->reg_conf);
42159dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
42259dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
423b8d582b9SAmit Daniel Kachhap 
424a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
425a4463c4fSAmit Daniel Kachhap 	val_irq = readl(data->base + reg->tmu_intstat);
426a4463c4fSAmit Daniel Kachhap 	/* clear the interrupts */
427a4463c4fSAmit Daniel Kachhap 	writel(val_irq, data->base + reg->tmu_intclear);
428b8d582b9SAmit Daniel Kachhap 
42959dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
43059dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
431*a0395eeeSAmit Daniel Kachhap out:
43259dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
43359dfa54cSAmit Daniel Kachhap }
43459dfa54cSAmit Daniel Kachhap 
43559dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
43659dfa54cSAmit Daniel Kachhap {
43759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
43859dfa54cSAmit Daniel Kachhap 
43959dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
44059dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
44159dfa54cSAmit Daniel Kachhap 
44259dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
44359dfa54cSAmit Daniel Kachhap }
44459dfa54cSAmit Daniel Kachhap 
44559dfa54cSAmit Daniel Kachhap #ifdef CONFIG_OF
44659dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
44759dfa54cSAmit Daniel Kachhap 	{
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",
453e6b7991eSAmit Daniel Kachhap 		.data = (void *)EXYNOS5250_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 	},
45959dfa54cSAmit Daniel Kachhap 	{},
46059dfa54cSAmit Daniel Kachhap };
46159dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
46259dfa54cSAmit Daniel Kachhap #endif
46359dfa54cSAmit Daniel Kachhap 
46459dfa54cSAmit Daniel Kachhap static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
465cebe7373SAmit Daniel Kachhap 			struct platform_device *pdev, int id)
46659dfa54cSAmit Daniel Kachhap {
46759dfa54cSAmit Daniel Kachhap #ifdef CONFIG_OF
468cebe7373SAmit Daniel Kachhap 	struct  exynos_tmu_init_data *data_table;
469cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *tmu_data;
47059dfa54cSAmit Daniel Kachhap 	if (pdev->dev.of_node) {
47159dfa54cSAmit Daniel Kachhap 		const struct of_device_id *match;
47259dfa54cSAmit Daniel Kachhap 		match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
47359dfa54cSAmit Daniel Kachhap 		if (!match)
47459dfa54cSAmit Daniel Kachhap 			return NULL;
475cebe7373SAmit Daniel Kachhap 		data_table = (struct exynos_tmu_init_data *) match->data;
476cebe7373SAmit Daniel Kachhap 		if (!data_table || id >= data_table->tmu_count)
477cebe7373SAmit Daniel Kachhap 			return NULL;
478cebe7373SAmit Daniel Kachhap 		tmu_data = data_table->tmu_data;
479cebe7373SAmit Daniel Kachhap 		return (struct exynos_tmu_platform_data *) (tmu_data + id);
48059dfa54cSAmit Daniel Kachhap 	}
48159dfa54cSAmit Daniel Kachhap #endif
4821cd1ecb6SAmit Daniel Kachhap 	return NULL;
48359dfa54cSAmit Daniel Kachhap }
48459dfa54cSAmit Daniel Kachhap 
485cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
48659dfa54cSAmit Daniel Kachhap {
487cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
488cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
489cebe7373SAmit Daniel Kachhap 	struct resource res;
49059dfa54cSAmit Daniel Kachhap 
491cebe7373SAmit Daniel Kachhap 	if (!data)
492cebe7373SAmit Daniel Kachhap 		return -ENODEV;
49359dfa54cSAmit Daniel Kachhap 
494cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
495cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
496cebe7373SAmit Daniel Kachhap 		data->id = 0;
497cebe7373SAmit Daniel Kachhap 
498cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
499cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
500cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
501cebe7373SAmit Daniel Kachhap 		return -ENODEV;
502cebe7373SAmit Daniel Kachhap 	}
503cebe7373SAmit Daniel Kachhap 
504cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
505cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
506cebe7373SAmit Daniel Kachhap 		return -ENODEV;
507cebe7373SAmit Daniel Kachhap 	}
508cebe7373SAmit Daniel Kachhap 
509cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
510cebe7373SAmit Daniel Kachhap 	if (!data->base) {
511cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
512cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
513cebe7373SAmit Daniel Kachhap 	}
514cebe7373SAmit Daniel Kachhap 
515cebe7373SAmit Daniel Kachhap 	pdata = exynos_get_driver_data(pdev, data->id);
51659dfa54cSAmit Daniel Kachhap 	if (!pdata) {
51759dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "No platform init data supplied.\n");
51859dfa54cSAmit Daniel Kachhap 		return -ENODEV;
51959dfa54cSAmit Daniel Kachhap 	}
520cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
521d9b6ee14SAmit Daniel Kachhap 	/*
522d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
523d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
524d9b6ee14SAmit Daniel Kachhap 	 */
525d9b6ee14SAmit Daniel Kachhap 	if (!TMU_SUPPORTS(pdata, SHARED_MEMORY))
526d9b6ee14SAmit Daniel Kachhap 		return 0;
527d9b6ee14SAmit Daniel Kachhap 
528d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
529d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
530d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
531d9b6ee14SAmit Daniel Kachhap 	}
532d9b6ee14SAmit Daniel Kachhap 
533d9b6ee14SAmit Daniel Kachhap 	data->base_common = devm_ioremap(&pdev->dev, res.start,
534d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
535d9b6ee14SAmit Daniel Kachhap 	if (!data->base) {
536d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
537d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
538d9b6ee14SAmit Daniel Kachhap 	}
539cebe7373SAmit Daniel Kachhap 
540cebe7373SAmit Daniel Kachhap 	return 0;
541cebe7373SAmit Daniel Kachhap }
542cebe7373SAmit Daniel Kachhap 
543cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
544cebe7373SAmit Daniel Kachhap {
545cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data;
546cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
547cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *sensor_conf;
548cebe7373SAmit Daniel Kachhap 	int ret, i;
549cebe7373SAmit Daniel Kachhap 
55059dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
55159dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
55259dfa54cSAmit Daniel Kachhap 	if (!data) {
55359dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to allocate driver structure\n");
55459dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
55559dfa54cSAmit Daniel Kachhap 	}
55659dfa54cSAmit Daniel Kachhap 
557cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
558cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
559cebe7373SAmit Daniel Kachhap 
560cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
561cebe7373SAmit Daniel Kachhap 	if (ret)
562cebe7373SAmit Daniel Kachhap 		return ret;
563cebe7373SAmit Daniel Kachhap 
564cebe7373SAmit Daniel Kachhap 	pdata = data->pdata;
56559dfa54cSAmit Daniel Kachhap 
56659dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
56759dfa54cSAmit Daniel Kachhap 
56859dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
56959dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
57059dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
57159dfa54cSAmit Daniel Kachhap 		return  PTR_ERR(data->clk);
57259dfa54cSAmit Daniel Kachhap 	}
57359dfa54cSAmit Daniel Kachhap 
57459dfa54cSAmit Daniel Kachhap 	ret = clk_prepare(data->clk);
57559dfa54cSAmit Daniel Kachhap 	if (ret)
57659dfa54cSAmit Daniel Kachhap 		return ret;
57759dfa54cSAmit Daniel Kachhap 
57859dfa54cSAmit Daniel Kachhap 	if (pdata->type == SOC_ARCH_EXYNOS ||
579*a0395eeeSAmit Daniel Kachhap 		pdata->type == SOC_ARCH_EXYNOS4210 ||
580*a0395eeeSAmit Daniel Kachhap 				pdata->type == SOC_ARCH_EXYNOS5440)
58159dfa54cSAmit Daniel Kachhap 		data->soc = pdata->type;
58259dfa54cSAmit Daniel Kachhap 	else {
58359dfa54cSAmit Daniel Kachhap 		ret = -EINVAL;
58459dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Platform not supported\n");
58559dfa54cSAmit Daniel Kachhap 		goto err_clk;
58659dfa54cSAmit Daniel Kachhap 	}
58759dfa54cSAmit Daniel Kachhap 
58859dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
58959dfa54cSAmit Daniel Kachhap 	if (ret) {
59059dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
59159dfa54cSAmit Daniel Kachhap 		goto err_clk;
59259dfa54cSAmit Daniel Kachhap 	}
59359dfa54cSAmit Daniel Kachhap 
59459dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
59559dfa54cSAmit Daniel Kachhap 
596cebe7373SAmit Daniel Kachhap 	/* Allocate a structure to register with the exynos core thermal */
597cebe7373SAmit Daniel Kachhap 	sensor_conf = devm_kzalloc(&pdev->dev,
598cebe7373SAmit Daniel Kachhap 				sizeof(struct thermal_sensor_conf), GFP_KERNEL);
599cebe7373SAmit Daniel Kachhap 	if (!sensor_conf) {
600cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to allocate registration struct\n");
601cebe7373SAmit Daniel Kachhap 		ret = -ENOMEM;
602cebe7373SAmit Daniel Kachhap 		goto err_clk;
603cebe7373SAmit Daniel Kachhap 	}
604cebe7373SAmit Daniel Kachhap 	sprintf(sensor_conf->name, "therm_zone%d", data->id);
605cebe7373SAmit Daniel Kachhap 	sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
606cebe7373SAmit Daniel Kachhap 	sensor_conf->write_emul_temp =
607cebe7373SAmit Daniel Kachhap 		(int (*)(void *, unsigned long))exynos_tmu_set_emulation;
608cebe7373SAmit Daniel Kachhap 	sensor_conf->driver_data = data;
609cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
610bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
611bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[3];
61259dfa54cSAmit Daniel Kachhap 
613cebe7373SAmit Daniel Kachhap 	for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
614cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_val[i] =
61559dfa54cSAmit Daniel Kachhap 			pdata->threshold + pdata->trigger_levels[i];
616cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_type[i] =
6175c3cf552SAmit Daniel Kachhap 					pdata->trigger_type[i];
6185c3cf552SAmit Daniel Kachhap 	}
61959dfa54cSAmit Daniel Kachhap 
620cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
62159dfa54cSAmit Daniel Kachhap 
622cebe7373SAmit Daniel Kachhap 	sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
62359dfa54cSAmit Daniel Kachhap 	for (i = 0; i < pdata->freq_tab_count; i++) {
624cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].freq_clip_max =
62559dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].freq_clip_max;
626cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].temp_level =
62759dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].temp_level;
62859dfa54cSAmit Daniel Kachhap 	}
629cebe7373SAmit Daniel Kachhap 	sensor_conf->dev = &pdev->dev;
630cebe7373SAmit Daniel Kachhap 	/* Register the sensor with thermal management interface */
631cebe7373SAmit Daniel Kachhap 	ret = exynos_register_thermal(sensor_conf);
63259dfa54cSAmit Daniel Kachhap 	if (ret) {
63359dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to register thermal interface\n");
63459dfa54cSAmit Daniel Kachhap 		goto err_clk;
63559dfa54cSAmit Daniel Kachhap 	}
636cebe7373SAmit Daniel Kachhap 	data->reg_conf = sensor_conf;
637cebe7373SAmit Daniel Kachhap 
638cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
639cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
640cebe7373SAmit Daniel Kachhap 	if (ret) {
641cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
642cebe7373SAmit Daniel Kachhap 		goto err_clk;
643cebe7373SAmit Daniel Kachhap 	}
64459dfa54cSAmit Daniel Kachhap 
64559dfa54cSAmit Daniel Kachhap 	return 0;
64659dfa54cSAmit Daniel Kachhap err_clk:
64759dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
64859dfa54cSAmit Daniel Kachhap 	return ret;
64959dfa54cSAmit Daniel Kachhap }
65059dfa54cSAmit Daniel Kachhap 
65159dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
65259dfa54cSAmit Daniel Kachhap {
65359dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
65459dfa54cSAmit Daniel Kachhap 
65559dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, false);
65659dfa54cSAmit Daniel Kachhap 
657cebe7373SAmit Daniel Kachhap 	exynos_unregister_thermal(data->reg_conf);
65859dfa54cSAmit Daniel Kachhap 
65959dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
66059dfa54cSAmit Daniel Kachhap 
66159dfa54cSAmit Daniel Kachhap 	return 0;
66259dfa54cSAmit Daniel Kachhap }
66359dfa54cSAmit Daniel Kachhap 
66459dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
66559dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
66659dfa54cSAmit Daniel Kachhap {
66759dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
66859dfa54cSAmit Daniel Kachhap 
66959dfa54cSAmit Daniel Kachhap 	return 0;
67059dfa54cSAmit Daniel Kachhap }
67159dfa54cSAmit Daniel Kachhap 
67259dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
67359dfa54cSAmit Daniel Kachhap {
67459dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
67559dfa54cSAmit Daniel Kachhap 
67659dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
67759dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
67859dfa54cSAmit Daniel Kachhap 
67959dfa54cSAmit Daniel Kachhap 	return 0;
68059dfa54cSAmit Daniel Kachhap }
68159dfa54cSAmit Daniel Kachhap 
68259dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
68359dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
68459dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
68559dfa54cSAmit Daniel Kachhap #else
68659dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
68759dfa54cSAmit Daniel Kachhap #endif
68859dfa54cSAmit Daniel Kachhap 
68959dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
69059dfa54cSAmit Daniel Kachhap 	.driver = {
69159dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
69259dfa54cSAmit Daniel Kachhap 		.owner  = THIS_MODULE,
69359dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
69459dfa54cSAmit Daniel Kachhap 		.of_match_table = of_match_ptr(exynos_tmu_match),
69559dfa54cSAmit Daniel Kachhap 	},
69659dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
69759dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
69859dfa54cSAmit Daniel Kachhap };
69959dfa54cSAmit Daniel Kachhap 
70059dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
70159dfa54cSAmit Daniel Kachhap 
70259dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
70359dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
70459dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
70559dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
706