xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision d0a0ce3e77c795258d47f9163e92d5031d0c5221)
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>
2959dfa54cSAmit Daniel Kachhap #include <linux/platform_device.h>
3059dfa54cSAmit Daniel Kachhap 
3159dfa54cSAmit Daniel Kachhap #include "exynos_thermal_common.h"
320c1836a6SAmit Daniel Kachhap #include "exynos_tmu.h"
33e6b7991eSAmit Daniel Kachhap #include "exynos_tmu_data.h"
3459dfa54cSAmit Daniel Kachhap 
3559dfa54cSAmit Daniel Kachhap /* Exynos generic registers */
3659dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_TRIMINFO		0x0
3759dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_CONTROL		0x20
3859dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_STATUS		0x28
3959dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_CURRENT_TEMP	0x40
4059dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_INTEN		0x70
4159dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_INTSTAT		0x74
4259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REG_INTCLEAR		0x78
4359dfa54cSAmit Daniel Kachhap 
4459dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_TRIM_TEMP_MASK	0xff
4559dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_GAIN_SHIFT		8
46*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_GAIN_MASK		0xf
4759dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_REF_VOLTAGE_SHIFT	24
48*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_REF_VOLTAGE_MASK	0x1f
49*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_BUF_SLOPE_SEL_MASK	0xf
50*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT	8
51*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_CORE_EN_SHIFT	0
5259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET	50
5359dfa54cSAmit Daniel Kachhap 
5459dfa54cSAmit Daniel Kachhap /* Exynos4210 specific registers */
5559dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_THRESHOLD_TEMP	0x44
5659dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL0	0x50
5759dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL1	0x54
5859dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL2	0x58
5959dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_TRIG_LEVEL3	0x5C
6059dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP0	0x60
6159dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP1	0x64
6259dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP2	0x68
6359dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_REG_PAST_TEMP3	0x6C
6459dfa54cSAmit Daniel Kachhap 
6559dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL0_MASK	0x1
6659dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL1_MASK	0x10
6759dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL2_MASK	0x100
6859dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL3_MASK	0x1000
69*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS4210_TMU_TRIG_LEVEL_MASK	0x1111
7059dfa54cSAmit Daniel Kachhap #define EXYNOS4210_TMU_INTCLEAR_VAL	0x1111
7159dfa54cSAmit Daniel Kachhap 
7259dfa54cSAmit Daniel Kachhap /* Exynos5250 and Exynos4412 specific registers */
7359dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_TRIMINFO_CON	0x14
7459dfa54cSAmit Daniel Kachhap #define EXYNOS_THD_TEMP_RISE		0x50
7559dfa54cSAmit Daniel Kachhap #define EXYNOS_THD_TEMP_FALL		0x54
7659dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_CON		0x80
7759dfa54cSAmit Daniel Kachhap 
7859dfa54cSAmit Daniel Kachhap #define EXYNOS_TRIMINFO_RELOAD		0x1
79*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TRIMINFO_SHIFT		0x0
80*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_RISE_INT_MASK	0x111
81*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_RISE_INT_SHIFT	0
82*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_FALL_INT_MASK	0x111
83*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_FALL_INT_SHIFT	12
8459dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_CLEAR_RISE_INT	0x111
8559dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_CLEAR_FALL_INT	(0x111 << 12)
8659dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_TRIP_MODE_SHIFT	13
87*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_TRIP_MODE_MASK	0x7
88*d0a0ce3eSAmit Daniel Kachhap 
89*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_INTEN_RISE0_SHIFT	0
90*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_INTEN_RISE1_SHIFT	4
91*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_INTEN_RISE2_SHIFT	8
92*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_INTEN_RISE3_SHIFT	12
93*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_INTEN_FALL0_SHIFT	16
94*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_INTEN_FALL1_SHIFT	20
95*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_TMU_INTEN_FALL2_SHIFT	24
9659dfa54cSAmit Daniel Kachhap 
9759dfa54cSAmit Daniel Kachhap #define EFUSE_MIN_VALUE 40
9859dfa54cSAmit Daniel Kachhap #define EFUSE_MAX_VALUE 100
9959dfa54cSAmit Daniel Kachhap 
10059dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
10159dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_TIME	0x57F0
102*d0a0ce3eSAmit Daniel Kachhap #define EXYNOS_EMUL_TIME_MASK	0xffff
10359dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_TIME_SHIFT	16
10459dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_DATA_SHIFT	8
10559dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_DATA_MASK	0xFF
10659dfa54cSAmit Daniel Kachhap #define EXYNOS_EMUL_ENABLE	0x1
10759dfa54cSAmit Daniel Kachhap #endif /* CONFIG_THERMAL_EMULATION */
10859dfa54cSAmit Daniel Kachhap 
10959dfa54cSAmit Daniel Kachhap struct exynos_tmu_data {
11059dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
11159dfa54cSAmit Daniel Kachhap 	struct resource *mem;
11259dfa54cSAmit Daniel Kachhap 	void __iomem *base;
11359dfa54cSAmit Daniel Kachhap 	int irq;
11459dfa54cSAmit Daniel Kachhap 	enum soc_type soc;
11559dfa54cSAmit Daniel Kachhap 	struct work_struct irq_work;
11659dfa54cSAmit Daniel Kachhap 	struct mutex lock;
11759dfa54cSAmit Daniel Kachhap 	struct clk *clk;
11859dfa54cSAmit Daniel Kachhap 	u8 temp_error1, temp_error2;
11959dfa54cSAmit Daniel Kachhap };
12059dfa54cSAmit Daniel Kachhap 
12159dfa54cSAmit Daniel Kachhap /*
12259dfa54cSAmit Daniel Kachhap  * TMU treats temperature as a mapped temperature code.
12359dfa54cSAmit Daniel Kachhap  * The temperature is converted differently depending on the calibration type.
12459dfa54cSAmit Daniel Kachhap  */
12559dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
12659dfa54cSAmit Daniel Kachhap {
12759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
12859dfa54cSAmit Daniel Kachhap 	int temp_code;
12959dfa54cSAmit Daniel Kachhap 
13059dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210)
13159dfa54cSAmit Daniel Kachhap 		/* temp should range between 25 and 125 */
13259dfa54cSAmit Daniel Kachhap 		if (temp < 25 || temp > 125) {
13359dfa54cSAmit Daniel Kachhap 			temp_code = -EINVAL;
13459dfa54cSAmit Daniel Kachhap 			goto out;
13559dfa54cSAmit Daniel Kachhap 		}
13659dfa54cSAmit Daniel Kachhap 
13759dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
13859dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
13959dfa54cSAmit Daniel Kachhap 		temp_code = (temp - 25) *
14059dfa54cSAmit Daniel Kachhap 		    (data->temp_error2 - data->temp_error1) /
14159dfa54cSAmit Daniel Kachhap 		    (85 - 25) + data->temp_error1;
14259dfa54cSAmit Daniel Kachhap 		break;
14359dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
14459dfa54cSAmit Daniel Kachhap 		temp_code = temp + data->temp_error1 - 25;
14559dfa54cSAmit Daniel Kachhap 		break;
14659dfa54cSAmit Daniel Kachhap 	default:
14759dfa54cSAmit Daniel Kachhap 		temp_code = temp + EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
14859dfa54cSAmit Daniel Kachhap 		break;
14959dfa54cSAmit Daniel Kachhap 	}
15059dfa54cSAmit Daniel Kachhap out:
15159dfa54cSAmit Daniel Kachhap 	return temp_code;
15259dfa54cSAmit Daniel Kachhap }
15359dfa54cSAmit Daniel Kachhap 
15459dfa54cSAmit Daniel Kachhap /*
15559dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
15659dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
15759dfa54cSAmit Daniel Kachhap  */
15859dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
15959dfa54cSAmit Daniel Kachhap {
16059dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
16159dfa54cSAmit Daniel Kachhap 	int temp;
16259dfa54cSAmit Daniel Kachhap 
16359dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210)
16459dfa54cSAmit Daniel Kachhap 		/* temp_code should range between 75 and 175 */
16559dfa54cSAmit Daniel Kachhap 		if (temp_code < 75 || temp_code > 175) {
16659dfa54cSAmit Daniel Kachhap 			temp = -ENODATA;
16759dfa54cSAmit Daniel Kachhap 			goto out;
16859dfa54cSAmit Daniel Kachhap 		}
16959dfa54cSAmit Daniel Kachhap 
17059dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
17159dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
17259dfa54cSAmit Daniel Kachhap 		temp = (temp_code - data->temp_error1) * (85 - 25) /
17359dfa54cSAmit Daniel Kachhap 		    (data->temp_error2 - data->temp_error1) + 25;
17459dfa54cSAmit Daniel Kachhap 		break;
17559dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
17659dfa54cSAmit Daniel Kachhap 		temp = temp_code - data->temp_error1 + 25;
17759dfa54cSAmit Daniel Kachhap 		break;
17859dfa54cSAmit Daniel Kachhap 	default:
17959dfa54cSAmit Daniel Kachhap 		temp = temp_code - EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
18059dfa54cSAmit Daniel Kachhap 		break;
18159dfa54cSAmit Daniel Kachhap 	}
18259dfa54cSAmit Daniel Kachhap out:
18359dfa54cSAmit Daniel Kachhap 	return temp;
18459dfa54cSAmit Daniel Kachhap }
18559dfa54cSAmit Daniel Kachhap 
18659dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
18759dfa54cSAmit Daniel Kachhap {
18859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
18959dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
19059dfa54cSAmit Daniel Kachhap 	unsigned int status, trim_info;
19159dfa54cSAmit Daniel Kachhap 	unsigned int rising_threshold = 0, falling_threshold = 0;
19259dfa54cSAmit Daniel Kachhap 	int ret = 0, threshold_code, i, trigger_levs = 0;
19359dfa54cSAmit Daniel Kachhap 
19459dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
19559dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
19659dfa54cSAmit Daniel Kachhap 
19759dfa54cSAmit Daniel Kachhap 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
19859dfa54cSAmit Daniel Kachhap 	if (!status) {
19959dfa54cSAmit Daniel Kachhap 		ret = -EBUSY;
20059dfa54cSAmit Daniel Kachhap 		goto out;
20159dfa54cSAmit Daniel Kachhap 	}
20259dfa54cSAmit Daniel Kachhap 
20359dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS) {
20459dfa54cSAmit Daniel Kachhap 		__raw_writel(EXYNOS_TRIMINFO_RELOAD,
20559dfa54cSAmit Daniel Kachhap 				data->base + EXYNOS_TMU_TRIMINFO_CON);
20659dfa54cSAmit Daniel Kachhap 	}
20759dfa54cSAmit Daniel Kachhap 	/* Save trimming info in order to perform calibration */
20859dfa54cSAmit Daniel Kachhap 	trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
20959dfa54cSAmit Daniel Kachhap 	data->temp_error1 = trim_info & EXYNOS_TMU_TRIM_TEMP_MASK;
21059dfa54cSAmit Daniel Kachhap 	data->temp_error2 = ((trim_info >> 8) & EXYNOS_TMU_TRIM_TEMP_MASK);
21159dfa54cSAmit Daniel Kachhap 
21259dfa54cSAmit Daniel Kachhap 	if ((EFUSE_MIN_VALUE > data->temp_error1) ||
21359dfa54cSAmit Daniel Kachhap 			(data->temp_error1 > EFUSE_MAX_VALUE) ||
21459dfa54cSAmit Daniel Kachhap 			(data->temp_error2 != 0))
21559dfa54cSAmit Daniel Kachhap 		data->temp_error1 = pdata->efuse_value;
21659dfa54cSAmit Daniel Kachhap 
21759dfa54cSAmit Daniel Kachhap 	/* Count trigger levels to be enabled */
21859dfa54cSAmit Daniel Kachhap 	for (i = 0; i < MAX_THRESHOLD_LEVS; i++)
21959dfa54cSAmit Daniel Kachhap 		if (pdata->trigger_levels[i])
22059dfa54cSAmit Daniel Kachhap 			trigger_levs++;
22159dfa54cSAmit Daniel Kachhap 
22259dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210) {
22359dfa54cSAmit Daniel Kachhap 		/* Write temperature code for threshold */
22459dfa54cSAmit Daniel Kachhap 		threshold_code = temp_to_code(data, pdata->threshold);
22559dfa54cSAmit Daniel Kachhap 		if (threshold_code < 0) {
22659dfa54cSAmit Daniel Kachhap 			ret = threshold_code;
22759dfa54cSAmit Daniel Kachhap 			goto out;
22859dfa54cSAmit Daniel Kachhap 		}
22959dfa54cSAmit Daniel Kachhap 		writeb(threshold_code,
23059dfa54cSAmit Daniel Kachhap 			data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
23159dfa54cSAmit Daniel Kachhap 		for (i = 0; i < trigger_levs; i++)
23259dfa54cSAmit Daniel Kachhap 			writeb(pdata->trigger_levels[i],
23359dfa54cSAmit Daniel Kachhap 			data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
23459dfa54cSAmit Daniel Kachhap 
23559dfa54cSAmit Daniel Kachhap 		writel(EXYNOS4210_TMU_INTCLEAR_VAL,
23659dfa54cSAmit Daniel Kachhap 			data->base + EXYNOS_TMU_REG_INTCLEAR);
23759dfa54cSAmit Daniel Kachhap 	} else if (data->soc == SOC_ARCH_EXYNOS) {
23859dfa54cSAmit Daniel Kachhap 		/* Write temperature code for rising and falling threshold */
23959dfa54cSAmit Daniel Kachhap 		for (i = 0; i < trigger_levs; i++) {
24059dfa54cSAmit Daniel Kachhap 			threshold_code = temp_to_code(data,
24159dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i]);
24259dfa54cSAmit Daniel Kachhap 			if (threshold_code < 0) {
24359dfa54cSAmit Daniel Kachhap 				ret = threshold_code;
24459dfa54cSAmit Daniel Kachhap 				goto out;
24559dfa54cSAmit Daniel Kachhap 			}
24659dfa54cSAmit Daniel Kachhap 			rising_threshold |= threshold_code << 8 * i;
24759dfa54cSAmit Daniel Kachhap 			if (pdata->threshold_falling) {
24859dfa54cSAmit Daniel Kachhap 				threshold_code = temp_to_code(data,
24959dfa54cSAmit Daniel Kachhap 						pdata->trigger_levels[i] -
25059dfa54cSAmit Daniel Kachhap 						pdata->threshold_falling);
25159dfa54cSAmit Daniel Kachhap 				if (threshold_code > 0)
25259dfa54cSAmit Daniel Kachhap 					falling_threshold |=
25359dfa54cSAmit Daniel Kachhap 						threshold_code << 8 * i;
25459dfa54cSAmit Daniel Kachhap 			}
25559dfa54cSAmit Daniel Kachhap 		}
25659dfa54cSAmit Daniel Kachhap 
25759dfa54cSAmit Daniel Kachhap 		writel(rising_threshold,
25859dfa54cSAmit Daniel Kachhap 				data->base + EXYNOS_THD_TEMP_RISE);
25959dfa54cSAmit Daniel Kachhap 		writel(falling_threshold,
26059dfa54cSAmit Daniel Kachhap 				data->base + EXYNOS_THD_TEMP_FALL);
26159dfa54cSAmit Daniel Kachhap 
26259dfa54cSAmit Daniel Kachhap 		writel(EXYNOS_TMU_CLEAR_RISE_INT | EXYNOS_TMU_CLEAR_FALL_INT,
26359dfa54cSAmit Daniel Kachhap 				data->base + EXYNOS_TMU_REG_INTCLEAR);
26459dfa54cSAmit Daniel Kachhap 	}
26559dfa54cSAmit Daniel Kachhap out:
26659dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
26759dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
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;
27659dfa54cSAmit Daniel Kachhap 	unsigned int con, interrupt_en;
27759dfa54cSAmit Daniel Kachhap 
27859dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
27959dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
28059dfa54cSAmit Daniel Kachhap 
281*d0a0ce3eSAmit Daniel Kachhap 	con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
28259dfa54cSAmit Daniel Kachhap 
283*d0a0ce3eSAmit Daniel Kachhap 	if (pdata->reference_voltage) {
284*d0a0ce3eSAmit Daniel Kachhap 		con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK <<
285*d0a0ce3eSAmit Daniel Kachhap 				EXYNOS_TMU_REF_VOLTAGE_SHIFT);
286*d0a0ce3eSAmit Daniel Kachhap 		con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
287*d0a0ce3eSAmit Daniel Kachhap 	}
288*d0a0ce3eSAmit Daniel Kachhap 
289*d0a0ce3eSAmit Daniel Kachhap 	if (pdata->gain) {
290*d0a0ce3eSAmit Daniel Kachhap 		con &= ~(EXYNOS_TMU_GAIN_MASK << EXYNOS_TMU_GAIN_SHIFT);
291*d0a0ce3eSAmit Daniel Kachhap 		con |= (pdata->gain << EXYNOS_TMU_GAIN_SHIFT);
292*d0a0ce3eSAmit Daniel Kachhap 	}
293*d0a0ce3eSAmit Daniel Kachhap 
294*d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
295*d0a0ce3eSAmit Daniel Kachhap 		con &= ~(EXYNOS_TMU_TRIP_MODE_MASK <<
296*d0a0ce3eSAmit Daniel Kachhap 					EXYNOS_TMU_TRIP_MODE_SHIFT);
297*d0a0ce3eSAmit Daniel Kachhap 		con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT);
29859dfa54cSAmit Daniel Kachhap 	}
29959dfa54cSAmit Daniel Kachhap 
30059dfa54cSAmit Daniel Kachhap 	if (on) {
301*d0a0ce3eSAmit Daniel Kachhap 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
302*d0a0ce3eSAmit Daniel Kachhap 		interrupt_en =
303*d0a0ce3eSAmit Daniel Kachhap 		pdata->trigger_level3_en << EXYNOS_TMU_INTEN_RISE3_SHIFT |
304*d0a0ce3eSAmit Daniel Kachhap 		pdata->trigger_level2_en << EXYNOS_TMU_INTEN_RISE2_SHIFT |
305*d0a0ce3eSAmit Daniel Kachhap 		pdata->trigger_level1_en << EXYNOS_TMU_INTEN_RISE1_SHIFT |
306*d0a0ce3eSAmit Daniel Kachhap 		pdata->trigger_level0_en << EXYNOS_TMU_INTEN_RISE0_SHIFT;
30759dfa54cSAmit Daniel Kachhap 		if (pdata->threshold_falling)
308*d0a0ce3eSAmit Daniel Kachhap 			interrupt_en |=
309*d0a0ce3eSAmit Daniel Kachhap 				interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
31059dfa54cSAmit Daniel Kachhap 	} else {
311*d0a0ce3eSAmit Daniel Kachhap 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
31259dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
31359dfa54cSAmit Daniel Kachhap 	}
31459dfa54cSAmit Daniel Kachhap 	writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
31559dfa54cSAmit Daniel Kachhap 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
31659dfa54cSAmit Daniel Kachhap 
31759dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
31859dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
31959dfa54cSAmit Daniel Kachhap }
32059dfa54cSAmit Daniel Kachhap 
32159dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data)
32259dfa54cSAmit Daniel Kachhap {
32359dfa54cSAmit Daniel Kachhap 	u8 temp_code;
32459dfa54cSAmit Daniel Kachhap 	int temp;
32559dfa54cSAmit Daniel Kachhap 
32659dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
32759dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
32859dfa54cSAmit Daniel Kachhap 
32959dfa54cSAmit Daniel Kachhap 	temp_code = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
33059dfa54cSAmit Daniel Kachhap 	temp = code_to_temp(data, temp_code);
33159dfa54cSAmit Daniel Kachhap 
33259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
33359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
33459dfa54cSAmit Daniel Kachhap 
33559dfa54cSAmit Daniel Kachhap 	return temp;
33659dfa54cSAmit Daniel Kachhap }
33759dfa54cSAmit Daniel Kachhap 
33859dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
33959dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
34059dfa54cSAmit Daniel Kachhap {
34159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
34259dfa54cSAmit Daniel Kachhap 	unsigned int reg;
34359dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
34459dfa54cSAmit Daniel Kachhap 
34559dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS4210)
34659dfa54cSAmit Daniel Kachhap 		goto out;
34759dfa54cSAmit Daniel Kachhap 
34859dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
34959dfa54cSAmit Daniel Kachhap 		goto out;
35059dfa54cSAmit Daniel Kachhap 
35159dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
35259dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
35359dfa54cSAmit Daniel Kachhap 
35459dfa54cSAmit Daniel Kachhap 	reg = readl(data->base + EXYNOS_EMUL_CON);
35559dfa54cSAmit Daniel Kachhap 
35659dfa54cSAmit Daniel Kachhap 	if (temp) {
35759dfa54cSAmit Daniel Kachhap 		temp /= MCELSIUS;
35859dfa54cSAmit Daniel Kachhap 
35959dfa54cSAmit Daniel Kachhap 		reg = (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT) |
36059dfa54cSAmit Daniel Kachhap 			(temp_to_code(data, temp)
36159dfa54cSAmit Daniel Kachhap 			 << EXYNOS_EMUL_DATA_SHIFT) | EXYNOS_EMUL_ENABLE;
36259dfa54cSAmit Daniel Kachhap 	} else {
36359dfa54cSAmit Daniel Kachhap 		reg &= ~EXYNOS_EMUL_ENABLE;
36459dfa54cSAmit Daniel Kachhap 	}
36559dfa54cSAmit Daniel Kachhap 
36659dfa54cSAmit Daniel Kachhap 	writel(reg, data->base + EXYNOS_EMUL_CON);
36759dfa54cSAmit Daniel Kachhap 
36859dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
36959dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
37059dfa54cSAmit Daniel Kachhap 	return 0;
37159dfa54cSAmit Daniel Kachhap out:
37259dfa54cSAmit Daniel Kachhap 	return ret;
37359dfa54cSAmit Daniel Kachhap }
37459dfa54cSAmit Daniel Kachhap #else
37559dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
37659dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
37759dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/
37859dfa54cSAmit Daniel Kachhap 
37959dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
38059dfa54cSAmit Daniel Kachhap {
38159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
38259dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
38359dfa54cSAmit Daniel Kachhap 
38459dfa54cSAmit Daniel Kachhap 	exynos_report_trigger();
38559dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
38659dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
38759dfa54cSAmit Daniel Kachhap 	if (data->soc == SOC_ARCH_EXYNOS)
38859dfa54cSAmit Daniel Kachhap 		writel(EXYNOS_TMU_CLEAR_RISE_INT |
38959dfa54cSAmit Daniel Kachhap 				EXYNOS_TMU_CLEAR_FALL_INT,
39059dfa54cSAmit Daniel Kachhap 				data->base + EXYNOS_TMU_REG_INTCLEAR);
39159dfa54cSAmit Daniel Kachhap 	else
39259dfa54cSAmit Daniel Kachhap 		writel(EXYNOS4210_TMU_INTCLEAR_VAL,
39359dfa54cSAmit Daniel Kachhap 				data->base + EXYNOS_TMU_REG_INTCLEAR);
39459dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
39559dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
39659dfa54cSAmit Daniel Kachhap 
39759dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
39859dfa54cSAmit Daniel Kachhap }
39959dfa54cSAmit Daniel Kachhap 
40059dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
40159dfa54cSAmit Daniel Kachhap {
40259dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
40359dfa54cSAmit Daniel Kachhap 
40459dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
40559dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
40659dfa54cSAmit Daniel Kachhap 
40759dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
40859dfa54cSAmit Daniel Kachhap }
40959dfa54cSAmit Daniel Kachhap static struct thermal_sensor_conf exynos_sensor_conf = {
41059dfa54cSAmit Daniel Kachhap 	.name			= "exynos-therm",
41159dfa54cSAmit Daniel Kachhap 	.read_temperature	= (int (*)(void *))exynos_tmu_read,
41259dfa54cSAmit Daniel Kachhap 	.write_emul_temp	= exynos_tmu_set_emulation,
41359dfa54cSAmit Daniel Kachhap };
41459dfa54cSAmit Daniel Kachhap 
41559dfa54cSAmit Daniel Kachhap #ifdef CONFIG_OF
41659dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
41759dfa54cSAmit Daniel Kachhap 	{
41859dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4210-tmu",
41959dfa54cSAmit Daniel Kachhap 		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
42059dfa54cSAmit Daniel Kachhap 	},
42159dfa54cSAmit Daniel Kachhap 	{
42259dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4412-tmu",
423e6b7991eSAmit Daniel Kachhap 		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
42459dfa54cSAmit Daniel Kachhap 	},
42559dfa54cSAmit Daniel Kachhap 	{
42659dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos5250-tmu",
427e6b7991eSAmit Daniel Kachhap 		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
42859dfa54cSAmit Daniel Kachhap 	},
42959dfa54cSAmit Daniel Kachhap 	{},
43059dfa54cSAmit Daniel Kachhap };
43159dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
43259dfa54cSAmit Daniel Kachhap #endif
43359dfa54cSAmit Daniel Kachhap 
43459dfa54cSAmit Daniel Kachhap static struct platform_device_id exynos_tmu_driver_ids[] = {
43559dfa54cSAmit Daniel Kachhap 	{
43659dfa54cSAmit Daniel Kachhap 		.name		= "exynos4210-tmu",
43759dfa54cSAmit Daniel Kachhap 		.driver_data    = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
43859dfa54cSAmit Daniel Kachhap 	},
43959dfa54cSAmit Daniel Kachhap 	{
44059dfa54cSAmit Daniel Kachhap 		.name		= "exynos5250-tmu",
441e6b7991eSAmit Daniel Kachhap 		.driver_data    = (kernel_ulong_t)EXYNOS5250_TMU_DRV_DATA,
44259dfa54cSAmit Daniel Kachhap 	},
44359dfa54cSAmit Daniel Kachhap 	{ },
44459dfa54cSAmit Daniel Kachhap };
44559dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids);
44659dfa54cSAmit Daniel Kachhap 
44759dfa54cSAmit Daniel Kachhap static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
44859dfa54cSAmit Daniel Kachhap 			struct platform_device *pdev)
44959dfa54cSAmit Daniel Kachhap {
45059dfa54cSAmit Daniel Kachhap #ifdef CONFIG_OF
45159dfa54cSAmit Daniel Kachhap 	if (pdev->dev.of_node) {
45259dfa54cSAmit Daniel Kachhap 		const struct of_device_id *match;
45359dfa54cSAmit Daniel Kachhap 		match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
45459dfa54cSAmit Daniel Kachhap 		if (!match)
45559dfa54cSAmit Daniel Kachhap 			return NULL;
45659dfa54cSAmit Daniel Kachhap 		return (struct exynos_tmu_platform_data *) match->data;
45759dfa54cSAmit Daniel Kachhap 	}
45859dfa54cSAmit Daniel Kachhap #endif
45959dfa54cSAmit Daniel Kachhap 	return (struct exynos_tmu_platform_data *)
46059dfa54cSAmit Daniel Kachhap 			platform_get_device_id(pdev)->driver_data;
46159dfa54cSAmit Daniel Kachhap }
46259dfa54cSAmit Daniel Kachhap 
46359dfa54cSAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
46459dfa54cSAmit Daniel Kachhap {
46559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data;
46659dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
46759dfa54cSAmit Daniel Kachhap 	int ret, i;
46859dfa54cSAmit Daniel Kachhap 
46959dfa54cSAmit Daniel Kachhap 	if (!pdata)
47059dfa54cSAmit Daniel Kachhap 		pdata = exynos_get_driver_data(pdev);
47159dfa54cSAmit Daniel Kachhap 
47259dfa54cSAmit Daniel Kachhap 	if (!pdata) {
47359dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "No platform init data supplied.\n");
47459dfa54cSAmit Daniel Kachhap 		return -ENODEV;
47559dfa54cSAmit Daniel Kachhap 	}
47659dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
47759dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
47859dfa54cSAmit Daniel Kachhap 	if (!data) {
47959dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to allocate driver structure\n");
48059dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
48159dfa54cSAmit Daniel Kachhap 	}
48259dfa54cSAmit Daniel Kachhap 
48359dfa54cSAmit Daniel Kachhap 	data->irq = platform_get_irq(pdev, 0);
48459dfa54cSAmit Daniel Kachhap 	if (data->irq < 0) {
48559dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get platform irq\n");
48659dfa54cSAmit Daniel Kachhap 		return data->irq;
48759dfa54cSAmit Daniel Kachhap 	}
48859dfa54cSAmit Daniel Kachhap 
48959dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
49059dfa54cSAmit Daniel Kachhap 
49159dfa54cSAmit Daniel Kachhap 	data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
49259dfa54cSAmit Daniel Kachhap 	data->base = devm_ioremap_resource(&pdev->dev, data->mem);
49359dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->base))
49459dfa54cSAmit Daniel Kachhap 		return PTR_ERR(data->base);
49559dfa54cSAmit Daniel Kachhap 
49659dfa54cSAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
49759dfa54cSAmit Daniel Kachhap 		IRQF_TRIGGER_RISING, "exynos-tmu", data);
49859dfa54cSAmit Daniel Kachhap 	if (ret) {
49959dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
50059dfa54cSAmit Daniel Kachhap 		return ret;
50159dfa54cSAmit Daniel Kachhap 	}
50259dfa54cSAmit Daniel Kachhap 
50359dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
50459dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
50559dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
50659dfa54cSAmit Daniel Kachhap 		return  PTR_ERR(data->clk);
50759dfa54cSAmit Daniel Kachhap 	}
50859dfa54cSAmit Daniel Kachhap 
50959dfa54cSAmit Daniel Kachhap 	ret = clk_prepare(data->clk);
51059dfa54cSAmit Daniel Kachhap 	if (ret)
51159dfa54cSAmit Daniel Kachhap 		return ret;
51259dfa54cSAmit Daniel Kachhap 
51359dfa54cSAmit Daniel Kachhap 	if (pdata->type == SOC_ARCH_EXYNOS ||
51459dfa54cSAmit Daniel Kachhap 				pdata->type == SOC_ARCH_EXYNOS4210)
51559dfa54cSAmit Daniel Kachhap 		data->soc = pdata->type;
51659dfa54cSAmit Daniel Kachhap 	else {
51759dfa54cSAmit Daniel Kachhap 		ret = -EINVAL;
51859dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Platform not supported\n");
51959dfa54cSAmit Daniel Kachhap 		goto err_clk;
52059dfa54cSAmit Daniel Kachhap 	}
52159dfa54cSAmit Daniel Kachhap 
52259dfa54cSAmit Daniel Kachhap 	data->pdata = pdata;
52359dfa54cSAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
52459dfa54cSAmit Daniel Kachhap 	mutex_init(&data->lock);
52559dfa54cSAmit Daniel Kachhap 
52659dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
52759dfa54cSAmit Daniel Kachhap 	if (ret) {
52859dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
52959dfa54cSAmit Daniel Kachhap 		goto err_clk;
53059dfa54cSAmit Daniel Kachhap 	}
53159dfa54cSAmit Daniel Kachhap 
53259dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
53359dfa54cSAmit Daniel Kachhap 
53459dfa54cSAmit Daniel Kachhap 	/* Register the sensor with thermal management interface */
53559dfa54cSAmit Daniel Kachhap 	(&exynos_sensor_conf)->private_data = data;
53659dfa54cSAmit Daniel Kachhap 	exynos_sensor_conf.trip_data.trip_count = pdata->trigger_level0_en +
53759dfa54cSAmit Daniel Kachhap 			pdata->trigger_level1_en + pdata->trigger_level2_en +
53859dfa54cSAmit Daniel Kachhap 			pdata->trigger_level3_en;
53959dfa54cSAmit Daniel Kachhap 
54059dfa54cSAmit Daniel Kachhap 	for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
54159dfa54cSAmit Daniel Kachhap 		exynos_sensor_conf.trip_data.trip_val[i] =
54259dfa54cSAmit Daniel Kachhap 			pdata->threshold + pdata->trigger_levels[i];
54359dfa54cSAmit Daniel Kachhap 
54459dfa54cSAmit Daniel Kachhap 	exynos_sensor_conf.trip_data.trigger_falling = pdata->threshold_falling;
54559dfa54cSAmit Daniel Kachhap 
54659dfa54cSAmit Daniel Kachhap 	exynos_sensor_conf.cooling_data.freq_clip_count =
54759dfa54cSAmit Daniel Kachhap 						pdata->freq_tab_count;
54859dfa54cSAmit Daniel Kachhap 	for (i = 0; i < pdata->freq_tab_count; i++) {
54959dfa54cSAmit Daniel Kachhap 		exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
55059dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].freq_clip_max;
55159dfa54cSAmit Daniel Kachhap 		exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
55259dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].temp_level;
55359dfa54cSAmit Daniel Kachhap 	}
55459dfa54cSAmit Daniel Kachhap 
55559dfa54cSAmit Daniel Kachhap 	ret = exynos_register_thermal(&exynos_sensor_conf);
55659dfa54cSAmit Daniel Kachhap 	if (ret) {
55759dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to register thermal interface\n");
55859dfa54cSAmit Daniel Kachhap 		goto err_clk;
55959dfa54cSAmit Daniel Kachhap 	}
56059dfa54cSAmit Daniel Kachhap 
56159dfa54cSAmit Daniel Kachhap 	return 0;
56259dfa54cSAmit Daniel Kachhap err_clk:
56359dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
56459dfa54cSAmit Daniel Kachhap 	return ret;
56559dfa54cSAmit Daniel Kachhap }
56659dfa54cSAmit Daniel Kachhap 
56759dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
56859dfa54cSAmit Daniel Kachhap {
56959dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
57059dfa54cSAmit Daniel Kachhap 
57159dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, false);
57259dfa54cSAmit Daniel Kachhap 
57359dfa54cSAmit Daniel Kachhap 	exynos_unregister_thermal();
57459dfa54cSAmit Daniel Kachhap 
57559dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
57659dfa54cSAmit Daniel Kachhap 
57759dfa54cSAmit Daniel Kachhap 	return 0;
57859dfa54cSAmit Daniel Kachhap }
57959dfa54cSAmit Daniel Kachhap 
58059dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
58159dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
58259dfa54cSAmit Daniel Kachhap {
58359dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
58459dfa54cSAmit Daniel Kachhap 
58559dfa54cSAmit Daniel Kachhap 	return 0;
58659dfa54cSAmit Daniel Kachhap }
58759dfa54cSAmit Daniel Kachhap 
58859dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
58959dfa54cSAmit Daniel Kachhap {
59059dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
59159dfa54cSAmit Daniel Kachhap 
59259dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
59359dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
59459dfa54cSAmit Daniel Kachhap 
59559dfa54cSAmit Daniel Kachhap 	return 0;
59659dfa54cSAmit Daniel Kachhap }
59759dfa54cSAmit Daniel Kachhap 
59859dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
59959dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
60059dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
60159dfa54cSAmit Daniel Kachhap #else
60259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
60359dfa54cSAmit Daniel Kachhap #endif
60459dfa54cSAmit Daniel Kachhap 
60559dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
60659dfa54cSAmit Daniel Kachhap 	.driver = {
60759dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
60859dfa54cSAmit Daniel Kachhap 		.owner  = THIS_MODULE,
60959dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
61059dfa54cSAmit Daniel Kachhap 		.of_match_table = of_match_ptr(exynos_tmu_match),
61159dfa54cSAmit Daniel Kachhap 	},
61259dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
61359dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
61459dfa54cSAmit Daniel Kachhap 	.id_table = exynos_tmu_driver_ids,
61559dfa54cSAmit Daniel Kachhap };
61659dfa54cSAmit Daniel Kachhap 
61759dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
61859dfa54cSAmit Daniel Kachhap 
61959dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
62059dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
62159dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
62259dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
623