xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision 4215688e7e55bcc80a0bbbc99164c261a112e4a1)
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 
801928457eSAmit Daniel Kachhap 	if (pdata->cal_mode == HW_MODE)
811928457eSAmit Daniel Kachhap 		return temp;
821928457eSAmit Daniel Kachhap 
8359dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210)
8459dfa54cSAmit Daniel Kachhap 		/* temp should range between 25 and 125 */
8559dfa54cSAmit Daniel Kachhap 		if (temp < 25 || temp > 125) {
8659dfa54cSAmit Daniel Kachhap 			temp_code = -EINVAL;
8759dfa54cSAmit Daniel Kachhap 			goto out;
8859dfa54cSAmit Daniel Kachhap 		}
8959dfa54cSAmit Daniel Kachhap 
9059dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
9159dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
92bb34b4c8SAmit Daniel Kachhap 		temp_code = (temp - pdata->first_point_trim) *
9359dfa54cSAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) /
94bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) +
95bb34b4c8SAmit Daniel Kachhap 			data->temp_error1;
9659dfa54cSAmit Daniel Kachhap 		break;
9759dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
98bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
9959dfa54cSAmit Daniel Kachhap 		break;
10059dfa54cSAmit Daniel Kachhap 	default:
101bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + pdata->default_temp_offset;
10259dfa54cSAmit Daniel Kachhap 		break;
10359dfa54cSAmit Daniel Kachhap 	}
10459dfa54cSAmit Daniel Kachhap out:
10559dfa54cSAmit Daniel Kachhap 	return temp_code;
10659dfa54cSAmit Daniel Kachhap }
10759dfa54cSAmit Daniel Kachhap 
10859dfa54cSAmit Daniel Kachhap /*
10959dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
11059dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
11159dfa54cSAmit Daniel Kachhap  */
11259dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
11359dfa54cSAmit Daniel Kachhap {
11459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
11559dfa54cSAmit Daniel Kachhap 	int temp;
11659dfa54cSAmit Daniel Kachhap 
1171928457eSAmit Daniel Kachhap 	if (pdata->cal_mode == HW_MODE)
1181928457eSAmit Daniel Kachhap 		return temp_code;
1191928457eSAmit Daniel Kachhap 
12059dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210)
12159dfa54cSAmit Daniel Kachhap 		/* temp_code should range between 75 and 175 */
12259dfa54cSAmit Daniel Kachhap 		if (temp_code < 75 || temp_code > 175) {
12359dfa54cSAmit Daniel Kachhap 			temp = -ENODATA;
12459dfa54cSAmit Daniel Kachhap 			goto out;
12559dfa54cSAmit Daniel Kachhap 		}
12659dfa54cSAmit Daniel Kachhap 
12759dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
12859dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
129bb34b4c8SAmit Daniel Kachhap 		temp = (temp_code - data->temp_error1) *
130bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) /
131bb34b4c8SAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) +
132bb34b4c8SAmit Daniel Kachhap 			pdata->first_point_trim;
13359dfa54cSAmit Daniel Kachhap 		break;
13459dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
135bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
13659dfa54cSAmit Daniel Kachhap 		break;
13759dfa54cSAmit Daniel Kachhap 	default:
138bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - pdata->default_temp_offset;
13959dfa54cSAmit Daniel Kachhap 		break;
14059dfa54cSAmit Daniel Kachhap 	}
14159dfa54cSAmit Daniel Kachhap out:
14259dfa54cSAmit Daniel Kachhap 	return temp;
14359dfa54cSAmit Daniel Kachhap }
14459dfa54cSAmit Daniel Kachhap 
14559dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
14659dfa54cSAmit Daniel Kachhap {
14759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
14859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
149b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
1507ca04e58SAmit Daniel Kachhap 	unsigned int status, trim_info = 0, con;
15159dfa54cSAmit Daniel Kachhap 	unsigned int rising_threshold = 0, falling_threshold = 0;
15259dfa54cSAmit Daniel Kachhap 	int ret = 0, threshold_code, i, trigger_levs = 0;
15359dfa54cSAmit Daniel Kachhap 
15459dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
15559dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
15614a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
15714a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
15859dfa54cSAmit Daniel Kachhap 
159f4dae753SAmit Daniel Kachhap 	if (TMU_SUPPORTS(pdata, READY_STATUS)) {
160b8d582b9SAmit Daniel Kachhap 		status = readb(data->base + reg->tmu_status);
16159dfa54cSAmit Daniel Kachhap 		if (!status) {
16259dfa54cSAmit Daniel Kachhap 			ret = -EBUSY;
16359dfa54cSAmit Daniel Kachhap 			goto out;
16459dfa54cSAmit Daniel Kachhap 		}
165f4dae753SAmit Daniel Kachhap 	}
16659dfa54cSAmit Daniel Kachhap 
167f4dae753SAmit Daniel Kachhap 	if (TMU_SUPPORTS(pdata, TRIM_RELOAD))
168b8d582b9SAmit Daniel Kachhap 		__raw_writel(1, data->base + reg->triminfo_ctrl);
169b8d582b9SAmit Daniel Kachhap 
1701928457eSAmit Daniel Kachhap 	if (pdata->cal_mode == HW_MODE)
1711928457eSAmit Daniel Kachhap 		goto skip_calib_data;
1721928457eSAmit Daniel Kachhap 
17359dfa54cSAmit Daniel Kachhap 	/* Save trimming info in order to perform calibration */
174a0395eeeSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS5440) {
175a0395eeeSAmit Daniel Kachhap 		/*
176a0395eeeSAmit Daniel Kachhap 		 * For exynos5440 soc triminfo value is swapped between TMU0 and
177a0395eeeSAmit Daniel Kachhap 		 * TMU2, so the below logic is needed.
178a0395eeeSAmit Daniel Kachhap 		 */
179a0395eeeSAmit Daniel Kachhap 		switch (data->id) {
180a0395eeeSAmit Daniel Kachhap 		case 0:
181a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base +
182a0395eeeSAmit Daniel Kachhap 			EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
183a0395eeeSAmit Daniel Kachhap 			break;
184a0395eeeSAmit Daniel Kachhap 		case 1:
185b8d582b9SAmit Daniel Kachhap 			trim_info = readl(data->base + reg->triminfo_data);
186a0395eeeSAmit Daniel Kachhap 			break;
187a0395eeeSAmit Daniel Kachhap 		case 2:
188a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base -
189a0395eeeSAmit Daniel Kachhap 			EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
190a0395eeeSAmit Daniel Kachhap 		}
191a0395eeeSAmit Daniel Kachhap 	} else {
19214a11dc7SNaveen Krishna Chatradhi 		/* On exynos5420 the triminfo register is in the shared space */
19314a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
19414a11dc7SNaveen Krishna Chatradhi 			trim_info = readl(data->base_second +
19514a11dc7SNaveen Krishna Chatradhi 							reg->triminfo_data);
19614a11dc7SNaveen Krishna Chatradhi 		else
197a0395eeeSAmit Daniel Kachhap 			trim_info = readl(data->base + reg->triminfo_data);
198a0395eeeSAmit Daniel Kachhap 	}
199b8d582b9SAmit Daniel Kachhap 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
200b8d582b9SAmit Daniel Kachhap 	data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) &
201b8d582b9SAmit Daniel Kachhap 				EXYNOS_TMU_TEMP_MASK);
20259dfa54cSAmit Daniel Kachhap 
2035000806cSAmit Daniel Kachhap 	if (!data->temp_error1 ||
2045000806cSAmit Daniel Kachhap 		(pdata->min_efuse_value > data->temp_error1) ||
2055000806cSAmit Daniel Kachhap 		(data->temp_error1 > pdata->max_efuse_value))
2065000806cSAmit Daniel Kachhap 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
2075000806cSAmit Daniel Kachhap 
2085000806cSAmit Daniel Kachhap 	if (!data->temp_error2)
2095000806cSAmit Daniel Kachhap 		data->temp_error2 =
2105000806cSAmit Daniel Kachhap 			(pdata->efuse_value >> reg->triminfo_85_shift) &
2115000806cSAmit Daniel Kachhap 			EXYNOS_TMU_TEMP_MASK;
21259dfa54cSAmit Daniel Kachhap 
2131928457eSAmit Daniel Kachhap skip_calib_data:
2147ca04e58SAmit Daniel Kachhap 	if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) {
2157ca04e58SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Invalid max trigger level\n");
21660acb389SJulia Lawall 		ret = -EINVAL;
2177ca04e58SAmit Daniel Kachhap 		goto out;
2187ca04e58SAmit Daniel Kachhap 	}
2197ca04e58SAmit Daniel Kachhap 
2207ca04e58SAmit Daniel Kachhap 	for (i = 0; i < pdata->max_trigger_level; i++) {
2217ca04e58SAmit Daniel Kachhap 		if (!pdata->trigger_levels[i])
2227ca04e58SAmit Daniel Kachhap 			continue;
2237ca04e58SAmit Daniel Kachhap 
2247ca04e58SAmit Daniel Kachhap 		if ((pdata->trigger_type[i] == HW_TRIP) &&
2257ca04e58SAmit Daniel Kachhap 		(!pdata->trigger_levels[pdata->max_trigger_level - 1])) {
2267ca04e58SAmit Daniel Kachhap 			dev_err(&pdev->dev, "Invalid hw trigger level\n");
2277ca04e58SAmit Daniel Kachhap 			ret = -EINVAL;
2287ca04e58SAmit Daniel Kachhap 			goto out;
2297ca04e58SAmit Daniel Kachhap 		}
2307ca04e58SAmit Daniel Kachhap 
2317ca04e58SAmit Daniel Kachhap 		/* Count trigger levels except the HW trip*/
2327ca04e58SAmit Daniel Kachhap 		if (!(pdata->trigger_type[i] == HW_TRIP))
23359dfa54cSAmit Daniel Kachhap 			trigger_levs++;
2347ca04e58SAmit Daniel Kachhap 	}
23559dfa54cSAmit Daniel Kachhap 
236c65d3473STushar Behera 	rising_threshold = readl(data->base + reg->threshold_th0);
237c65d3473STushar Behera 
23859dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210) {
23959dfa54cSAmit Daniel Kachhap 		/* Write temperature code for threshold */
24059dfa54cSAmit Daniel Kachhap 		threshold_code = temp_to_code(data, pdata->threshold);
24159dfa54cSAmit Daniel Kachhap 		if (threshold_code < 0) {
24259dfa54cSAmit Daniel Kachhap 			ret = threshold_code;
24359dfa54cSAmit Daniel Kachhap 			goto out;
24459dfa54cSAmit Daniel Kachhap 		}
24559dfa54cSAmit Daniel Kachhap 		writeb(threshold_code,
246b8d582b9SAmit Daniel Kachhap 			data->base + reg->threshold_temp);
24759dfa54cSAmit Daniel Kachhap 		for (i = 0; i < trigger_levs; i++)
248b8d582b9SAmit Daniel Kachhap 			writeb(pdata->trigger_levels[i], data->base +
249b8d582b9SAmit Daniel Kachhap 			reg->threshold_th0 + i * sizeof(reg->threshold_th0));
25059dfa54cSAmit Daniel Kachhap 
25174429c2fSNaveen Krishna Chatradhi 		writel(reg->intclr_rise_mask, data->base + reg->tmu_intclear);
252a0395eeeSAmit Daniel Kachhap 	} else {
25359dfa54cSAmit Daniel Kachhap 		/* Write temperature code for rising and falling threshold */
2547ca04e58SAmit Daniel Kachhap 		for (i = 0;
2557ca04e58SAmit Daniel Kachhap 		i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) {
25659dfa54cSAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
25759dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i]);
25859dfa54cSAmit Daniel Kachhap 			if (threshold_code < 0) {
25959dfa54cSAmit Daniel Kachhap 				ret = threshold_code;
26059dfa54cSAmit Daniel Kachhap 				goto out;
26159dfa54cSAmit Daniel Kachhap 			}
262c65d3473STushar Behera 			rising_threshold &= ~(0xff << 8 * i);
26359dfa54cSAmit Daniel Kachhap 			rising_threshold |= threshold_code << 8 * i;
26459dfa54cSAmit Daniel Kachhap 			if (pdata->threshold_falling) {
26559dfa54cSAmit Daniel Kachhap 				threshold_code = temp_to_code(data,
26659dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i] -
26759dfa54cSAmit Daniel Kachhap 						pdata->threshold_falling);
26859dfa54cSAmit Daniel Kachhap 				if (threshold_code > 0)
26959dfa54cSAmit Daniel Kachhap 					falling_threshold |=
27059dfa54cSAmit Daniel Kachhap 						threshold_code << 8 * i;
27159dfa54cSAmit Daniel Kachhap 			}
27259dfa54cSAmit Daniel Kachhap 		}
27359dfa54cSAmit Daniel Kachhap 
27459dfa54cSAmit Daniel Kachhap 		writel(rising_threshold,
275b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th0);
27659dfa54cSAmit Daniel Kachhap 		writel(falling_threshold,
277b8d582b9SAmit Daniel Kachhap 				data->base + reg->threshold_th1);
27859dfa54cSAmit Daniel Kachhap 
27974429c2fSNaveen Krishna Chatradhi 		writel((reg->intclr_rise_mask << reg->intclr_rise_shift) |
28074429c2fSNaveen Krishna Chatradhi 			(reg->intclr_fall_mask << reg->intclr_fall_shift),
281b8d582b9SAmit Daniel Kachhap 				data->base + reg->tmu_intclear);
2827ca04e58SAmit Daniel Kachhap 
2837ca04e58SAmit Daniel Kachhap 		/* if last threshold limit is also present */
2847ca04e58SAmit Daniel Kachhap 		i = pdata->max_trigger_level - 1;
2857ca04e58SAmit Daniel Kachhap 		if (pdata->trigger_levels[i] &&
2867ca04e58SAmit Daniel Kachhap 				(pdata->trigger_type[i] == HW_TRIP)) {
2877ca04e58SAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
2887ca04e58SAmit Daniel Kachhap 						pdata->trigger_levels[i]);
2897ca04e58SAmit Daniel Kachhap 			if (threshold_code < 0) {
2907ca04e58SAmit Daniel Kachhap 				ret = threshold_code;
2917ca04e58SAmit Daniel Kachhap 				goto out;
2927ca04e58SAmit Daniel Kachhap 			}
293a0395eeeSAmit Daniel Kachhap 			if (i == EXYNOS_MAX_TRIGGER_PER_REG - 1) {
294a0395eeeSAmit Daniel Kachhap 				/* 1-4 level to be assigned in th0 reg */
295c65d3473STushar Behera 				rising_threshold &= ~(0xff << 8 * i);
2967ca04e58SAmit Daniel Kachhap 				rising_threshold |= threshold_code << 8 * i;
2977ca04e58SAmit Daniel Kachhap 				writel(rising_threshold,
2987ca04e58SAmit Daniel Kachhap 					data->base + reg->threshold_th0);
299a0395eeeSAmit Daniel Kachhap 			} else if (i == EXYNOS_MAX_TRIGGER_PER_REG) {
300a0395eeeSAmit Daniel Kachhap 				/* 5th level to be assigned in th2 reg */
301a0395eeeSAmit Daniel Kachhap 				rising_threshold =
302a0395eeeSAmit Daniel Kachhap 				threshold_code << reg->threshold_th3_l0_shift;
303a0395eeeSAmit Daniel Kachhap 				writel(rising_threshold,
304a0395eeeSAmit Daniel Kachhap 					data->base + reg->threshold_th2);
305a0395eeeSAmit Daniel Kachhap 			}
3067ca04e58SAmit Daniel Kachhap 			con = readl(data->base + reg->tmu_ctrl);
3077ca04e58SAmit Daniel Kachhap 			con |= (1 << reg->therm_trip_en_shift);
3087ca04e58SAmit Daniel Kachhap 			writel(con, data->base + reg->tmu_ctrl);
3097ca04e58SAmit Daniel Kachhap 		}
31059dfa54cSAmit Daniel Kachhap 	}
311a0395eeeSAmit Daniel Kachhap 	/*Clear the PMIN in the common TMU register*/
312a0395eeeSAmit Daniel Kachhap 	if (reg->tmu_pmin && !data->id)
3139025d563SNaveen Krishna Chatradhi 		writel(0, data->base_second + reg->tmu_pmin);
31459dfa54cSAmit Daniel Kachhap out:
31559dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
31659dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
31714a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
31814a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
31959dfa54cSAmit Daniel Kachhap 
32059dfa54cSAmit Daniel Kachhap 	return ret;
32159dfa54cSAmit Daniel Kachhap }
32259dfa54cSAmit Daniel Kachhap 
32359dfa54cSAmit Daniel Kachhap static void exynos_tmu_control(struct platform_device *pdev, bool on)
32459dfa54cSAmit Daniel Kachhap {
32559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
32659dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
327b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
3281928457eSAmit Daniel Kachhap 	unsigned int con, interrupt_en, cal_val;
32959dfa54cSAmit Daniel Kachhap 
33059dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
33159dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
33259dfa54cSAmit Daniel Kachhap 
333b8d582b9SAmit Daniel Kachhap 	con = readl(data->base + reg->tmu_ctrl);
33459dfa54cSAmit Daniel Kachhap 
33586f5362eSLukasz Majewski 	if (pdata->test_mux)
33686f5362eSLukasz Majewski 		con |= (pdata->test_mux << reg->test_mux_addr_shift);
33786f5362eSLukasz Majewski 
338d0a0ce3eSAmit Daniel Kachhap 	if (pdata->reference_voltage) {
339b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
340b8d582b9SAmit Daniel Kachhap 		con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
341d0a0ce3eSAmit Daniel Kachhap 	}
342d0a0ce3eSAmit Daniel Kachhap 
343d0a0ce3eSAmit Daniel Kachhap 	if (pdata->gain) {
344b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
345b8d582b9SAmit Daniel Kachhap 		con |= (pdata->gain << reg->buf_slope_sel_shift);
346d0a0ce3eSAmit Daniel Kachhap 	}
347d0a0ce3eSAmit Daniel Kachhap 
348d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
349b8d582b9SAmit Daniel Kachhap 		con &= ~(reg->therm_trip_mode_mask <<
350b8d582b9SAmit Daniel Kachhap 					reg->therm_trip_mode_shift);
351b8d582b9SAmit Daniel Kachhap 		con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
35259dfa54cSAmit Daniel Kachhap 	}
35359dfa54cSAmit Daniel Kachhap 
3541928457eSAmit Daniel Kachhap 	if (pdata->cal_mode == HW_MODE) {
3551928457eSAmit Daniel Kachhap 		con &= ~(reg->calib_mode_mask << reg->calib_mode_shift);
3561928457eSAmit Daniel Kachhap 		cal_val = 0;
3571928457eSAmit Daniel Kachhap 		switch (pdata->cal_type) {
3581928457eSAmit Daniel Kachhap 		case TYPE_TWO_POINT_TRIMMING:
3591928457eSAmit Daniel Kachhap 			cal_val = 3;
3601928457eSAmit Daniel Kachhap 			break;
3611928457eSAmit Daniel Kachhap 		case TYPE_ONE_POINT_TRIMMING_85:
3621928457eSAmit Daniel Kachhap 			cal_val = 2;
3631928457eSAmit Daniel Kachhap 			break;
3641928457eSAmit Daniel Kachhap 		case TYPE_ONE_POINT_TRIMMING_25:
3651928457eSAmit Daniel Kachhap 			cal_val = 1;
3661928457eSAmit Daniel Kachhap 			break;
3671928457eSAmit Daniel Kachhap 		case TYPE_NONE:
3681928457eSAmit Daniel Kachhap 			break;
3691928457eSAmit Daniel Kachhap 		default:
3701928457eSAmit Daniel Kachhap 			dev_err(&pdev->dev, "Invalid calibration type, using none\n");
3711928457eSAmit Daniel Kachhap 		}
3721928457eSAmit Daniel Kachhap 		con |= cal_val << reg->calib_mode_shift;
3731928457eSAmit Daniel Kachhap 	}
3741928457eSAmit Daniel Kachhap 
37559dfa54cSAmit Daniel Kachhap 	if (on) {
376b8d582b9SAmit Daniel Kachhap 		con |= (1 << reg->core_en_shift);
377d0a0ce3eSAmit Daniel Kachhap 		interrupt_en =
378b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[3] << reg->inten_rise3_shift |
379b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[2] << reg->inten_rise2_shift |
380b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[1] << reg->inten_rise1_shift |
381b8d582b9SAmit Daniel Kachhap 			pdata->trigger_enable[0] << reg->inten_rise0_shift;
382f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, FALLING_TRIP))
383d0a0ce3eSAmit Daniel Kachhap 			interrupt_en |=
384b8d582b9SAmit Daniel Kachhap 				interrupt_en << reg->inten_fall0_shift;
38559dfa54cSAmit Daniel Kachhap 	} else {
386b8d582b9SAmit Daniel Kachhap 		con &= ~(1 << reg->core_en_shift);
38759dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
38859dfa54cSAmit Daniel Kachhap 	}
389b8d582b9SAmit Daniel Kachhap 	writel(interrupt_en, data->base + reg->tmu_inten);
390b8d582b9SAmit Daniel Kachhap 	writel(con, data->base + reg->tmu_ctrl);
39159dfa54cSAmit Daniel Kachhap 
39259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
39359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
39459dfa54cSAmit Daniel Kachhap }
39559dfa54cSAmit Daniel Kachhap 
39659dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data)
39759dfa54cSAmit Daniel Kachhap {
398b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
399b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
40059dfa54cSAmit Daniel Kachhap 	u8 temp_code;
40159dfa54cSAmit Daniel Kachhap 	int temp;
40259dfa54cSAmit Daniel Kachhap 
40359dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
40459dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
40559dfa54cSAmit Daniel Kachhap 
406b8d582b9SAmit Daniel Kachhap 	temp_code = readb(data->base + reg->tmu_cur_temp);
40759dfa54cSAmit Daniel Kachhap 	temp = code_to_temp(data, temp_code);
40859dfa54cSAmit Daniel Kachhap 
40959dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
41059dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
41159dfa54cSAmit Daniel Kachhap 
41259dfa54cSAmit Daniel Kachhap 	return temp;
41359dfa54cSAmit Daniel Kachhap }
41459dfa54cSAmit Daniel Kachhap 
41559dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
41659dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
41759dfa54cSAmit Daniel Kachhap {
41859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
419b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
420b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
421b8d582b9SAmit Daniel Kachhap 	unsigned int val;
42259dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
42359dfa54cSAmit Daniel Kachhap 
424f4dae753SAmit Daniel Kachhap 	if (!TMU_SUPPORTS(pdata, EMULATION))
42559dfa54cSAmit Daniel Kachhap 		goto out;
42659dfa54cSAmit Daniel Kachhap 
42759dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
42859dfa54cSAmit Daniel Kachhap 		goto out;
42959dfa54cSAmit Daniel Kachhap 
43059dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
43159dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
43259dfa54cSAmit Daniel Kachhap 
433b8d582b9SAmit Daniel Kachhap 	val = readl(data->base + reg->emul_con);
43459dfa54cSAmit Daniel Kachhap 
43559dfa54cSAmit Daniel Kachhap 	if (temp) {
43659dfa54cSAmit Daniel Kachhap 		temp /= MCELSIUS;
43759dfa54cSAmit Daniel Kachhap 
438f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
439f4dae753SAmit Daniel Kachhap 			val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift);
440f4dae753SAmit Daniel Kachhap 			val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift);
441f4dae753SAmit Daniel Kachhap 		}
442f4dae753SAmit Daniel Kachhap 		val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift);
443f4dae753SAmit Daniel Kachhap 		val |= (temp_to_code(data, temp) << reg->emul_temp_shift) |
444f4dae753SAmit Daniel Kachhap 			EXYNOS_EMUL_ENABLE;
44559dfa54cSAmit Daniel Kachhap 	} else {
446b8d582b9SAmit Daniel Kachhap 		val &= ~EXYNOS_EMUL_ENABLE;
44759dfa54cSAmit Daniel Kachhap 	}
44859dfa54cSAmit Daniel Kachhap 
449b8d582b9SAmit Daniel Kachhap 	writel(val, data->base + reg->emul_con);
45059dfa54cSAmit Daniel Kachhap 
45159dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
45259dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
45359dfa54cSAmit Daniel Kachhap 	return 0;
45459dfa54cSAmit Daniel Kachhap out:
45559dfa54cSAmit Daniel Kachhap 	return ret;
45659dfa54cSAmit Daniel Kachhap }
45759dfa54cSAmit Daniel Kachhap #else
45859dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
45959dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
46059dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/
46159dfa54cSAmit Daniel Kachhap 
46259dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
46359dfa54cSAmit Daniel Kachhap {
46459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
46559dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
466b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
467b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
468a0395eeeSAmit Daniel Kachhap 	unsigned int val_irq, val_type;
469a0395eeeSAmit Daniel Kachhap 
47014a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
47114a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
472a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
473a0395eeeSAmit Daniel Kachhap 	if (reg->tmu_irqstatus) {
4749025d563SNaveen Krishna Chatradhi 		val_type = readl(data->base_second + reg->tmu_irqstatus);
475a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
476a0395eeeSAmit Daniel Kachhap 			goto out;
477a0395eeeSAmit Daniel Kachhap 	}
47814a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
47914a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
48059dfa54cSAmit Daniel Kachhap 
481cebe7373SAmit Daniel Kachhap 	exynos_report_trigger(data->reg_conf);
48259dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
48359dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
484b8d582b9SAmit Daniel Kachhap 
485a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
486a4463c4fSAmit Daniel Kachhap 	val_irq = readl(data->base + reg->tmu_intstat);
487a4463c4fSAmit Daniel Kachhap 	/* clear the interrupts */
488a4463c4fSAmit Daniel Kachhap 	writel(val_irq, data->base + reg->tmu_intclear);
489b8d582b9SAmit Daniel Kachhap 
49059dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
49159dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
492a0395eeeSAmit Daniel Kachhap out:
49359dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
49459dfa54cSAmit Daniel Kachhap }
49559dfa54cSAmit Daniel Kachhap 
49659dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
49759dfa54cSAmit Daniel Kachhap {
49859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
49959dfa54cSAmit Daniel Kachhap 
50059dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
50159dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
50259dfa54cSAmit Daniel Kachhap 
50359dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
50459dfa54cSAmit Daniel Kachhap }
50559dfa54cSAmit Daniel Kachhap 
50659dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
50759dfa54cSAmit Daniel Kachhap 	{
50859dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4210-tmu",
50959dfa54cSAmit Daniel Kachhap 		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
51059dfa54cSAmit Daniel Kachhap 	},
51159dfa54cSAmit Daniel Kachhap 	{
51259dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4412-tmu",
51314ddfaecSLukasz Majewski 		.data = (void *)EXYNOS4412_TMU_DRV_DATA,
51459dfa54cSAmit Daniel Kachhap 	},
51559dfa54cSAmit Daniel Kachhap 	{
51659dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos5250-tmu",
517e6b7991eSAmit Daniel Kachhap 		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
51859dfa54cSAmit Daniel Kachhap 	},
51990542546SAmit Daniel Kachhap 	{
520923488a5SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5260-tmu",
521923488a5SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5260_TMU_DRV_DATA,
522923488a5SNaveen Krishna Chatradhi 	},
523923488a5SNaveen Krishna Chatradhi 	{
52414a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu",
52514a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
52614a11dc7SNaveen Krishna Chatradhi 	},
52714a11dc7SNaveen Krishna Chatradhi 	{
52814a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu-ext-triminfo",
52914a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
53014a11dc7SNaveen Krishna Chatradhi 	},
53114a11dc7SNaveen Krishna Chatradhi 	{
53290542546SAmit Daniel Kachhap 		.compatible = "samsung,exynos5440-tmu",
53390542546SAmit Daniel Kachhap 		.data = (void *)EXYNOS5440_TMU_DRV_DATA,
53490542546SAmit Daniel Kachhap 	},
53559dfa54cSAmit Daniel Kachhap 	{},
53659dfa54cSAmit Daniel Kachhap };
53759dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
53859dfa54cSAmit Daniel Kachhap 
53959dfa54cSAmit Daniel Kachhap static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
540cebe7373SAmit Daniel Kachhap 			struct platform_device *pdev, int id)
54159dfa54cSAmit Daniel Kachhap {
542cebe7373SAmit Daniel Kachhap 	struct  exynos_tmu_init_data *data_table;
543cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *tmu_data;
54459dfa54cSAmit Daniel Kachhap 	const struct of_device_id *match;
54573b5b1d7SSachin Kamat 
54659dfa54cSAmit Daniel Kachhap 	match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
54759dfa54cSAmit Daniel Kachhap 	if (!match)
54859dfa54cSAmit Daniel Kachhap 		return NULL;
549cebe7373SAmit Daniel Kachhap 	data_table = (struct exynos_tmu_init_data *) match->data;
550cebe7373SAmit Daniel Kachhap 	if (!data_table || id >= data_table->tmu_count)
551cebe7373SAmit Daniel Kachhap 		return NULL;
552cebe7373SAmit Daniel Kachhap 	tmu_data = data_table->tmu_data;
553cebe7373SAmit Daniel Kachhap 	return (struct exynos_tmu_platform_data *) (tmu_data + id);
55459dfa54cSAmit Daniel Kachhap }
55559dfa54cSAmit Daniel Kachhap 
556cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
55759dfa54cSAmit Daniel Kachhap {
558cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
559cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
560cebe7373SAmit Daniel Kachhap 	struct resource res;
561498d22f6SAmit Daniel Kachhap 	int ret;
56259dfa54cSAmit Daniel Kachhap 
56373b5b1d7SSachin Kamat 	if (!data || !pdev->dev.of_node)
564cebe7373SAmit Daniel Kachhap 		return -ENODEV;
56559dfa54cSAmit Daniel Kachhap 
566498d22f6SAmit Daniel Kachhap 	/*
567498d22f6SAmit Daniel Kachhap 	 * Try enabling the regulator if found
568498d22f6SAmit Daniel Kachhap 	 * TODO: Add regulator as an SOC feature, so that regulator enable
569498d22f6SAmit Daniel Kachhap 	 * is a compulsory call.
570498d22f6SAmit Daniel Kachhap 	 */
571498d22f6SAmit Daniel Kachhap 	data->regulator = devm_regulator_get(&pdev->dev, "vtmu");
572498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator)) {
573498d22f6SAmit Daniel Kachhap 		ret = regulator_enable(data->regulator);
574498d22f6SAmit Daniel Kachhap 		if (ret) {
575498d22f6SAmit Daniel Kachhap 			dev_err(&pdev->dev, "failed to enable vtmu\n");
576498d22f6SAmit Daniel Kachhap 			return ret;
577498d22f6SAmit Daniel Kachhap 		}
578498d22f6SAmit Daniel Kachhap 	} else {
579498d22f6SAmit Daniel Kachhap 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
580498d22f6SAmit Daniel Kachhap 	}
581498d22f6SAmit Daniel Kachhap 
582cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
583cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
584cebe7373SAmit Daniel Kachhap 		data->id = 0;
585cebe7373SAmit Daniel Kachhap 
586cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
587cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
588cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
589cebe7373SAmit Daniel Kachhap 		return -ENODEV;
590cebe7373SAmit Daniel Kachhap 	}
591cebe7373SAmit Daniel Kachhap 
592cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
593cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
594cebe7373SAmit Daniel Kachhap 		return -ENODEV;
595cebe7373SAmit Daniel Kachhap 	}
596cebe7373SAmit Daniel Kachhap 
597cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
598cebe7373SAmit Daniel Kachhap 	if (!data->base) {
599cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
600cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
601cebe7373SAmit Daniel Kachhap 	}
602cebe7373SAmit Daniel Kachhap 
603cebe7373SAmit Daniel Kachhap 	pdata = exynos_get_driver_data(pdev, data->id);
60459dfa54cSAmit Daniel Kachhap 	if (!pdata) {
60559dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "No platform init data supplied.\n");
60659dfa54cSAmit Daniel Kachhap 		return -ENODEV;
60759dfa54cSAmit Daniel Kachhap 	}
608cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
609d9b6ee14SAmit Daniel Kachhap 	/*
610d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
611d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
612d9b6ee14SAmit Daniel Kachhap 	 */
6139025d563SNaveen Krishna Chatradhi 	if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE))
614d9b6ee14SAmit Daniel Kachhap 		return 0;
615d9b6ee14SAmit Daniel Kachhap 
616d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
617d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
618d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
619d9b6ee14SAmit Daniel Kachhap 	}
620d9b6ee14SAmit Daniel Kachhap 
6219025d563SNaveen Krishna Chatradhi 	data->base_second = devm_ioremap(&pdev->dev, res.start,
622d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
6239025d563SNaveen Krishna Chatradhi 	if (!data->base_second) {
624d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
625d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
626d9b6ee14SAmit Daniel Kachhap 	}
627cebe7373SAmit Daniel Kachhap 
628cebe7373SAmit Daniel Kachhap 	return 0;
629cebe7373SAmit Daniel Kachhap }
630cebe7373SAmit Daniel Kachhap 
631cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
632cebe7373SAmit Daniel Kachhap {
633cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data;
634cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
635cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *sensor_conf;
636cebe7373SAmit Daniel Kachhap 	int ret, i;
637cebe7373SAmit Daniel Kachhap 
63859dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
63959dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
6402a9675b3SJingoo Han 	if (!data)
64159dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
64259dfa54cSAmit Daniel Kachhap 
643cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
644cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
645cebe7373SAmit Daniel Kachhap 
646cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
647cebe7373SAmit Daniel Kachhap 	if (ret)
648cebe7373SAmit Daniel Kachhap 		return ret;
649cebe7373SAmit Daniel Kachhap 
650cebe7373SAmit Daniel Kachhap 	pdata = data->pdata;
65159dfa54cSAmit Daniel Kachhap 
65259dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
65359dfa54cSAmit Daniel Kachhap 
65459dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
65559dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
65659dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
65759dfa54cSAmit Daniel Kachhap 		return  PTR_ERR(data->clk);
65859dfa54cSAmit Daniel Kachhap 	}
65959dfa54cSAmit Daniel Kachhap 
66014a11dc7SNaveen Krishna Chatradhi 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
66114a11dc7SNaveen Krishna Chatradhi 	if (IS_ERR(data->clk_sec)) {
66214a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
66314a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
66414a11dc7SNaveen Krishna Chatradhi 			return PTR_ERR(data->clk_sec);
66514a11dc7SNaveen Krishna Chatradhi 		}
66614a11dc7SNaveen Krishna Chatradhi 	} else {
66714a11dc7SNaveen Krishna Chatradhi 		ret = clk_prepare(data->clk_sec);
66814a11dc7SNaveen Krishna Chatradhi 		if (ret) {
66914a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get clock\n");
67059dfa54cSAmit Daniel Kachhap 			return ret;
67114a11dc7SNaveen Krishna Chatradhi 		}
67214a11dc7SNaveen Krishna Chatradhi 	}
67314a11dc7SNaveen Krishna Chatradhi 
67414a11dc7SNaveen Krishna Chatradhi 	ret = clk_prepare(data->clk);
67514a11dc7SNaveen Krishna Chatradhi 	if (ret) {
67614a11dc7SNaveen Krishna Chatradhi 		dev_err(&pdev->dev, "Failed to get clock\n");
67714a11dc7SNaveen Krishna Chatradhi 		goto err_clk_sec;
67814a11dc7SNaveen Krishna Chatradhi 	}
67959dfa54cSAmit Daniel Kachhap 
68014ddfaecSLukasz Majewski 	if (pdata->type == SOC_ARCH_EXYNOS4210 ||
68114ddfaecSLukasz Majewski 	    pdata->type == SOC_ARCH_EXYNOS4412 ||
68214ddfaecSLukasz Majewski 	    pdata->type == SOC_ARCH_EXYNOS5250 ||
683923488a5SNaveen Krishna Chatradhi 	    pdata->type == SOC_ARCH_EXYNOS5260 ||
68414a11dc7SNaveen Krishna Chatradhi 	    pdata->type == SOC_ARCH_EXYNOS5420_TRIMINFO ||
685a0395eeeSAmit Daniel Kachhap 	    pdata->type == SOC_ARCH_EXYNOS5440)
68659dfa54cSAmit Daniel Kachhap 		data->soc = pdata->type;
68759dfa54cSAmit Daniel Kachhap 	else {
68859dfa54cSAmit Daniel Kachhap 		ret = -EINVAL;
68959dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Platform not supported\n");
69059dfa54cSAmit Daniel Kachhap 		goto err_clk;
69159dfa54cSAmit Daniel Kachhap 	}
69259dfa54cSAmit Daniel Kachhap 
69359dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
69459dfa54cSAmit Daniel Kachhap 	if (ret) {
69559dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
69659dfa54cSAmit Daniel Kachhap 		goto err_clk;
69759dfa54cSAmit Daniel Kachhap 	}
69859dfa54cSAmit Daniel Kachhap 
69959dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
70059dfa54cSAmit Daniel Kachhap 
701cebe7373SAmit Daniel Kachhap 	/* Allocate a structure to register with the exynos core thermal */
702cebe7373SAmit Daniel Kachhap 	sensor_conf = devm_kzalloc(&pdev->dev,
703cebe7373SAmit Daniel Kachhap 				sizeof(struct thermal_sensor_conf), GFP_KERNEL);
704cebe7373SAmit Daniel Kachhap 	if (!sensor_conf) {
705cebe7373SAmit Daniel Kachhap 		ret = -ENOMEM;
706cebe7373SAmit Daniel Kachhap 		goto err_clk;
707cebe7373SAmit Daniel Kachhap 	}
708cebe7373SAmit Daniel Kachhap 	sprintf(sensor_conf->name, "therm_zone%d", data->id);
709cebe7373SAmit Daniel Kachhap 	sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
710cebe7373SAmit Daniel Kachhap 	sensor_conf->write_emul_temp =
711cebe7373SAmit Daniel Kachhap 		(int (*)(void *, unsigned long))exynos_tmu_set_emulation;
712cebe7373SAmit Daniel Kachhap 	sensor_conf->driver_data = data;
713cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
714bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
715bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[3];
71659dfa54cSAmit Daniel Kachhap 
717cebe7373SAmit Daniel Kachhap 	for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
718cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_val[i] =
71959dfa54cSAmit Daniel Kachhap 			pdata->threshold + pdata->trigger_levels[i];
720cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_type[i] =
7215c3cf552SAmit Daniel Kachhap 					pdata->trigger_type[i];
7225c3cf552SAmit Daniel Kachhap 	}
72359dfa54cSAmit Daniel Kachhap 
724cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
72559dfa54cSAmit Daniel Kachhap 
726cebe7373SAmit Daniel Kachhap 	sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
72759dfa54cSAmit Daniel Kachhap 	for (i = 0; i < pdata->freq_tab_count; i++) {
728cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].freq_clip_max =
72959dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].freq_clip_max;
730cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].temp_level =
73159dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].temp_level;
73259dfa54cSAmit Daniel Kachhap 	}
733cebe7373SAmit Daniel Kachhap 	sensor_conf->dev = &pdev->dev;
734cebe7373SAmit Daniel Kachhap 	/* Register the sensor with thermal management interface */
735cebe7373SAmit Daniel Kachhap 	ret = exynos_register_thermal(sensor_conf);
73659dfa54cSAmit Daniel Kachhap 	if (ret) {
73759dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to register thermal interface\n");
73859dfa54cSAmit Daniel Kachhap 		goto err_clk;
73959dfa54cSAmit Daniel Kachhap 	}
740cebe7373SAmit Daniel Kachhap 	data->reg_conf = sensor_conf;
741cebe7373SAmit Daniel Kachhap 
742cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
743cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
744cebe7373SAmit Daniel Kachhap 	if (ret) {
745cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
746cebe7373SAmit Daniel Kachhap 		goto err_clk;
747cebe7373SAmit Daniel Kachhap 	}
74859dfa54cSAmit Daniel Kachhap 
74959dfa54cSAmit Daniel Kachhap 	return 0;
75059dfa54cSAmit Daniel Kachhap err_clk:
75159dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
75214a11dc7SNaveen Krishna Chatradhi err_clk_sec:
75314a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
75414a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
75559dfa54cSAmit Daniel Kachhap 	return ret;
75659dfa54cSAmit Daniel Kachhap }
75759dfa54cSAmit Daniel Kachhap 
75859dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
75959dfa54cSAmit Daniel Kachhap {
76059dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
76159dfa54cSAmit Daniel Kachhap 
762cebe7373SAmit Daniel Kachhap 	exynos_unregister_thermal(data->reg_conf);
76359dfa54cSAmit Daniel Kachhap 
764*4215688eSBartlomiej Zolnierkiewicz 	exynos_tmu_control(pdev, false);
765*4215688eSBartlomiej Zolnierkiewicz 
76659dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
76714a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
76814a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
76959dfa54cSAmit Daniel Kachhap 
770498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator))
771498d22f6SAmit Daniel Kachhap 		regulator_disable(data->regulator);
772498d22f6SAmit Daniel Kachhap 
77359dfa54cSAmit Daniel Kachhap 	return 0;
77459dfa54cSAmit Daniel Kachhap }
77559dfa54cSAmit Daniel Kachhap 
77659dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
77759dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
77859dfa54cSAmit Daniel Kachhap {
77959dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
78059dfa54cSAmit Daniel Kachhap 
78159dfa54cSAmit Daniel Kachhap 	return 0;
78259dfa54cSAmit Daniel Kachhap }
78359dfa54cSAmit Daniel Kachhap 
78459dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
78559dfa54cSAmit Daniel Kachhap {
78659dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
78759dfa54cSAmit Daniel Kachhap 
78859dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
78959dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
79059dfa54cSAmit Daniel Kachhap 
79159dfa54cSAmit Daniel Kachhap 	return 0;
79259dfa54cSAmit Daniel Kachhap }
79359dfa54cSAmit Daniel Kachhap 
79459dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
79559dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
79659dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
79759dfa54cSAmit Daniel Kachhap #else
79859dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
79959dfa54cSAmit Daniel Kachhap #endif
80059dfa54cSAmit Daniel Kachhap 
80159dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
80259dfa54cSAmit Daniel Kachhap 	.driver = {
80359dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
80459dfa54cSAmit Daniel Kachhap 		.owner  = THIS_MODULE,
80559dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
80673b5b1d7SSachin Kamat 		.of_match_table = exynos_tmu_match,
80759dfa54cSAmit Daniel Kachhap 	},
80859dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
80959dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
81059dfa54cSAmit Daniel Kachhap };
81159dfa54cSAmit Daniel Kachhap 
81259dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
81359dfa54cSAmit Daniel Kachhap 
81459dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
81559dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
81659dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
81759dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
818