xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision b79985ca74b25926cb2d88b60805092c5456bf43)
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.
5572d1100bSBartlomiej Zolnierkiewicz  * @tmu_initialize: SoC specific TMU initialization method
5637f9034fSBartlomiej Zolnierkiewicz  * @tmu_control: SoC specific TMU control method
57*b79985caSBartlomiej Zolnierkiewicz  * @tmu_read: SoC specific TMU temperature read method
58cebe7373SAmit Daniel Kachhap  */
5959dfa54cSAmit Daniel Kachhap struct exynos_tmu_data {
60cebe7373SAmit Daniel Kachhap 	int id;
6159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
6259dfa54cSAmit Daniel Kachhap 	void __iomem *base;
639025d563SNaveen Krishna Chatradhi 	void __iomem *base_second;
6459dfa54cSAmit Daniel Kachhap 	int irq;
6559dfa54cSAmit Daniel Kachhap 	enum soc_type soc;
6659dfa54cSAmit Daniel Kachhap 	struct work_struct irq_work;
6759dfa54cSAmit Daniel Kachhap 	struct mutex lock;
6814a11dc7SNaveen Krishna Chatradhi 	struct clk *clk, *clk_sec;
6959dfa54cSAmit Daniel Kachhap 	u8 temp_error1, temp_error2;
70498d22f6SAmit Daniel Kachhap 	struct regulator *regulator;
71cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *reg_conf;
7272d1100bSBartlomiej Zolnierkiewicz 	int (*tmu_initialize)(struct platform_device *pdev);
7337f9034fSBartlomiej Zolnierkiewicz 	void (*tmu_control)(struct platform_device *pdev, bool on);
74*b79985caSBartlomiej Zolnierkiewicz 	int (*tmu_read)(struct exynos_tmu_data *data);
7559dfa54cSAmit Daniel Kachhap };
7659dfa54cSAmit Daniel Kachhap 
7759dfa54cSAmit Daniel Kachhap /*
7859dfa54cSAmit Daniel Kachhap  * TMU treats temperature as a mapped temperature code.
7959dfa54cSAmit Daniel Kachhap  * The temperature is converted differently depending on the calibration type.
8059dfa54cSAmit Daniel Kachhap  */
8159dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
8259dfa54cSAmit Daniel Kachhap {
8359dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
8459dfa54cSAmit Daniel Kachhap 	int temp_code;
8559dfa54cSAmit Daniel Kachhap 
8659dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
8759dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
88bb34b4c8SAmit Daniel Kachhap 		temp_code = (temp - pdata->first_point_trim) *
8959dfa54cSAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) /
90bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) +
91bb34b4c8SAmit Daniel Kachhap 			data->temp_error1;
9259dfa54cSAmit Daniel Kachhap 		break;
9359dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
94bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
9559dfa54cSAmit Daniel Kachhap 		break;
9659dfa54cSAmit Daniel Kachhap 	default:
97bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + pdata->default_temp_offset;
9859dfa54cSAmit Daniel Kachhap 		break;
9959dfa54cSAmit Daniel Kachhap 	}
100ddb31d43SBartlomiej Zolnierkiewicz 
10159dfa54cSAmit Daniel Kachhap 	return temp_code;
10259dfa54cSAmit Daniel Kachhap }
10359dfa54cSAmit Daniel Kachhap 
10459dfa54cSAmit Daniel Kachhap /*
10559dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
10659dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
10759dfa54cSAmit Daniel Kachhap  */
10859dfa54cSAmit Daniel Kachhap static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
10959dfa54cSAmit Daniel Kachhap {
11059dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
11159dfa54cSAmit Daniel Kachhap 	int temp;
11259dfa54cSAmit Daniel Kachhap 
11359dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
11459dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
115bb34b4c8SAmit Daniel Kachhap 		temp = (temp_code - data->temp_error1) *
116bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) /
117bb34b4c8SAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) +
118bb34b4c8SAmit Daniel Kachhap 			pdata->first_point_trim;
11959dfa54cSAmit Daniel Kachhap 		break;
12059dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
121bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
12259dfa54cSAmit Daniel Kachhap 		break;
12359dfa54cSAmit Daniel Kachhap 	default:
124bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - pdata->default_temp_offset;
12559dfa54cSAmit Daniel Kachhap 		break;
12659dfa54cSAmit Daniel Kachhap 	}
127ddb31d43SBartlomiej Zolnierkiewicz 
12859dfa54cSAmit Daniel Kachhap 	return temp;
12959dfa54cSAmit Daniel Kachhap }
13059dfa54cSAmit Daniel Kachhap 
131b835ced1SBartlomiej Zolnierkiewicz static void exynos_tmu_clear_irqs(struct exynos_tmu_data *data)
132b835ced1SBartlomiej Zolnierkiewicz {
133b835ced1SBartlomiej Zolnierkiewicz 	const struct exynos_tmu_registers *reg = data->pdata->registers;
134b835ced1SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
135b835ced1SBartlomiej Zolnierkiewicz 
136b835ced1SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + reg->tmu_intstat);
137b835ced1SBartlomiej Zolnierkiewicz 	/*
138b835ced1SBartlomiej Zolnierkiewicz 	 * Clear the interrupts.  Please note that the documentation for
139b835ced1SBartlomiej Zolnierkiewicz 	 * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
140b835ced1SBartlomiej Zolnierkiewicz 	 * states that INTCLEAR register has a different placing of bits
141b835ced1SBartlomiej Zolnierkiewicz 	 * responsible for FALL IRQs than INTSTAT register.  Exynos5420
142b835ced1SBartlomiej Zolnierkiewicz 	 * and Exynos5440 documentation is correct (Exynos4210 doesn't
143b835ced1SBartlomiej Zolnierkiewicz 	 * support FALL IRQs at all).
144b835ced1SBartlomiej Zolnierkiewicz 	 */
145b835ced1SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + reg->tmu_intclear);
146b835ced1SBartlomiej Zolnierkiewicz }
147b835ced1SBartlomiej Zolnierkiewicz 
1488328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
1498328a4b1SBartlomiej Zolnierkiewicz {
1508328a4b1SBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
1518328a4b1SBartlomiej Zolnierkiewicz 
1528328a4b1SBartlomiej Zolnierkiewicz 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
1538328a4b1SBartlomiej Zolnierkiewicz 	data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
1548328a4b1SBartlomiej Zolnierkiewicz 				EXYNOS_TMU_TEMP_MASK);
1558328a4b1SBartlomiej Zolnierkiewicz 
1568328a4b1SBartlomiej Zolnierkiewicz 	if (!data->temp_error1 ||
1578328a4b1SBartlomiej Zolnierkiewicz 		(pdata->min_efuse_value > data->temp_error1) ||
1588328a4b1SBartlomiej Zolnierkiewicz 		(data->temp_error1 > pdata->max_efuse_value))
1598328a4b1SBartlomiej Zolnierkiewicz 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
1608328a4b1SBartlomiej Zolnierkiewicz 
1618328a4b1SBartlomiej Zolnierkiewicz 	if (!data->temp_error2)
1628328a4b1SBartlomiej Zolnierkiewicz 		data->temp_error2 =
1638328a4b1SBartlomiej Zolnierkiewicz 			(pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
1648328a4b1SBartlomiej Zolnierkiewicz 			EXYNOS_TMU_TEMP_MASK;
1658328a4b1SBartlomiej Zolnierkiewicz }
1668328a4b1SBartlomiej Zolnierkiewicz 
167fe87789cSBartlomiej Zolnierkiewicz static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling)
168fe87789cSBartlomiej Zolnierkiewicz {
169fe87789cSBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
170fe87789cSBartlomiej Zolnierkiewicz 	int i;
171fe87789cSBartlomiej Zolnierkiewicz 
172fe87789cSBartlomiej Zolnierkiewicz 	for (i = 0; i < pdata->non_hw_trigger_levels; i++) {
173fe87789cSBartlomiej Zolnierkiewicz 		u8 temp = pdata->trigger_levels[i];
174fe87789cSBartlomiej Zolnierkiewicz 
175fe87789cSBartlomiej Zolnierkiewicz 		if (falling)
176fe87789cSBartlomiej Zolnierkiewicz 			temp -= pdata->threshold_falling;
177fe87789cSBartlomiej Zolnierkiewicz 		else
178fe87789cSBartlomiej Zolnierkiewicz 			threshold &= ~(0xff << 8 * i);
179fe87789cSBartlomiej Zolnierkiewicz 
180fe87789cSBartlomiej Zolnierkiewicz 		threshold |= temp_to_code(data, temp) << 8 * i;
181fe87789cSBartlomiej Zolnierkiewicz 	}
182fe87789cSBartlomiej Zolnierkiewicz 
183fe87789cSBartlomiej Zolnierkiewicz 	return threshold;
184fe87789cSBartlomiej Zolnierkiewicz }
185fe87789cSBartlomiej Zolnierkiewicz 
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);
18972d1100bSBartlomiej Zolnierkiewicz 	int ret;
19059dfa54cSAmit Daniel Kachhap 
19159dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
19259dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
19314a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
19414a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
19572d1100bSBartlomiej Zolnierkiewicz 	ret = data->tmu_initialize(pdev);
19659dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
19759dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
19814a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
19914a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
20059dfa54cSAmit Daniel Kachhap 
20159dfa54cSAmit Daniel Kachhap 	return ret;
20259dfa54cSAmit Daniel Kachhap }
20359dfa54cSAmit Daniel Kachhap 
204d00671c3SBartlomiej Zolnierkiewicz static u32 get_con_reg(struct exynos_tmu_data *data, u32 con)
20559dfa54cSAmit Daniel Kachhap {
20659dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
20759dfa54cSAmit Daniel Kachhap 
20886f5362eSLukasz Majewski 	if (pdata->test_mux)
209bfb2b88cSBartlomiej Zolnierkiewicz 		con |= (pdata->test_mux << EXYNOS4412_MUX_ADDR_SHIFT);
21086f5362eSLukasz Majewski 
21199d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
21299d67fb9SBartlomiej Zolnierkiewicz 	con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
213d0a0ce3eSAmit Daniel Kachhap 
21499d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
21599d67fb9SBartlomiej Zolnierkiewicz 	con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
216d0a0ce3eSAmit Daniel Kachhap 
217d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
218b9504a6aSBartlomiej Zolnierkiewicz 		con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
219b9504a6aSBartlomiej Zolnierkiewicz 		con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT);
22059dfa54cSAmit Daniel Kachhap 	}
22159dfa54cSAmit Daniel Kachhap 
222d00671c3SBartlomiej Zolnierkiewicz 	return con;
223d00671c3SBartlomiej Zolnierkiewicz }
224d00671c3SBartlomiej Zolnierkiewicz 
225d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on)
226d00671c3SBartlomiej Zolnierkiewicz {
227d00671c3SBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
228d00671c3SBartlomiej Zolnierkiewicz 
229d00671c3SBartlomiej Zolnierkiewicz 	mutex_lock(&data->lock);
230d00671c3SBartlomiej Zolnierkiewicz 	clk_enable(data->clk);
23137f9034fSBartlomiej Zolnierkiewicz 	data->tmu_control(pdev, on);
23259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
23359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
23459dfa54cSAmit Daniel Kachhap }
23559dfa54cSAmit Daniel Kachhap 
23672d1100bSBartlomiej Zolnierkiewicz static int exynos4210_tmu_initialize(struct platform_device *pdev)
23772d1100bSBartlomiej Zolnierkiewicz {
23872d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
23972d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
24072d1100bSBartlomiej Zolnierkiewicz 	unsigned int status;
24172d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
24272d1100bSBartlomiej Zolnierkiewicz 
24372d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
24472d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
24572d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
24672d1100bSBartlomiej Zolnierkiewicz 		goto out;
24772d1100bSBartlomiej Zolnierkiewicz 	}
24872d1100bSBartlomiej Zolnierkiewicz 
24972d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO));
25072d1100bSBartlomiej Zolnierkiewicz 
25172d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for threshold */
25272d1100bSBartlomiej Zolnierkiewicz 	threshold_code = temp_to_code(data, pdata->threshold);
25372d1100bSBartlomiej Zolnierkiewicz 	writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
25472d1100bSBartlomiej Zolnierkiewicz 
25572d1100bSBartlomiej Zolnierkiewicz 	for (i = 0; i < pdata->non_hw_trigger_levels; i++)
25672d1100bSBartlomiej Zolnierkiewicz 		writeb(pdata->trigger_levels[i], data->base +
25772d1100bSBartlomiej Zolnierkiewicz 		       EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
25872d1100bSBartlomiej Zolnierkiewicz 
25972d1100bSBartlomiej Zolnierkiewicz 	exynos_tmu_clear_irqs(data);
26072d1100bSBartlomiej Zolnierkiewicz out:
26172d1100bSBartlomiej Zolnierkiewicz 	return ret;
26272d1100bSBartlomiej Zolnierkiewicz }
26372d1100bSBartlomiej Zolnierkiewicz 
26472d1100bSBartlomiej Zolnierkiewicz static int exynos4412_tmu_initialize(struct platform_device *pdev)
26572d1100bSBartlomiej Zolnierkiewicz {
26672d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
26772d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
26872d1100bSBartlomiej Zolnierkiewicz 	unsigned int status, trim_info, con, ctrl, rising_threshold;
26972d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
27072d1100bSBartlomiej Zolnierkiewicz 
27172d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
27272d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
27372d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
27472d1100bSBartlomiej Zolnierkiewicz 		goto out;
27572d1100bSBartlomiej Zolnierkiewicz 	}
27672d1100bSBartlomiej Zolnierkiewicz 
27772d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS3250 ||
27872d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS4412 ||
27972d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS5250) {
28072d1100bSBartlomiej Zolnierkiewicz 		if (data->soc == SOC_ARCH_EXYNOS3250) {
28172d1100bSBartlomiej Zolnierkiewicz 			ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
28272d1100bSBartlomiej Zolnierkiewicz 			ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
28372d1100bSBartlomiej Zolnierkiewicz 			writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
28472d1100bSBartlomiej Zolnierkiewicz 		}
28572d1100bSBartlomiej Zolnierkiewicz 		ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
28672d1100bSBartlomiej Zolnierkiewicz 		ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
28772d1100bSBartlomiej Zolnierkiewicz 		writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
28872d1100bSBartlomiej Zolnierkiewicz 	}
28972d1100bSBartlomiej Zolnierkiewicz 
29072d1100bSBartlomiej Zolnierkiewicz 	/* On exynos5420 the triminfo register is in the shared space */
29172d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
29272d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO);
29372d1100bSBartlomiej Zolnierkiewicz 	else
29472d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
29572d1100bSBartlomiej Zolnierkiewicz 
29672d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
29772d1100bSBartlomiej Zolnierkiewicz 
29872d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
29972d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE);
30072d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
30172d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
30272d1100bSBartlomiej Zolnierkiewicz 	writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL);
30372d1100bSBartlomiej Zolnierkiewicz 
30472d1100bSBartlomiej Zolnierkiewicz 	exynos_tmu_clear_irqs(data);
30572d1100bSBartlomiej Zolnierkiewicz 
30672d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
30772d1100bSBartlomiej Zolnierkiewicz 	i = pdata->max_trigger_level - 1;
30872d1100bSBartlomiej Zolnierkiewicz 	if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) {
30972d1100bSBartlomiej Zolnierkiewicz 		threshold_code = temp_to_code(data, pdata->trigger_levels[i]);
31072d1100bSBartlomiej Zolnierkiewicz 		/* 1-4 level to be assigned in th0 reg */
31172d1100bSBartlomiej Zolnierkiewicz 		rising_threshold &= ~(0xff << 8 * i);
31272d1100bSBartlomiej Zolnierkiewicz 		rising_threshold |= threshold_code << 8 * i;
31372d1100bSBartlomiej Zolnierkiewicz 		writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
31472d1100bSBartlomiej Zolnierkiewicz 		con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
31572d1100bSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
31672d1100bSBartlomiej Zolnierkiewicz 		writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
31772d1100bSBartlomiej Zolnierkiewicz 	}
31872d1100bSBartlomiej Zolnierkiewicz out:
31972d1100bSBartlomiej Zolnierkiewicz 	return ret;
32072d1100bSBartlomiej Zolnierkiewicz }
32172d1100bSBartlomiej Zolnierkiewicz 
32272d1100bSBartlomiej Zolnierkiewicz static int exynos5440_tmu_initialize(struct platform_device *pdev)
32372d1100bSBartlomiej Zolnierkiewicz {
32472d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
32572d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
32672d1100bSBartlomiej Zolnierkiewicz 	unsigned int trim_info = 0, con, rising_threshold;
32772d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
32872d1100bSBartlomiej Zolnierkiewicz 
32972d1100bSBartlomiej Zolnierkiewicz 	/*
33072d1100bSBartlomiej Zolnierkiewicz 	 * For exynos5440 soc triminfo value is swapped between TMU0 and
33172d1100bSBartlomiej Zolnierkiewicz 	 * TMU2, so the below logic is needed.
33272d1100bSBartlomiej Zolnierkiewicz 	 */
33372d1100bSBartlomiej Zolnierkiewicz 	switch (data->id) {
33472d1100bSBartlomiej Zolnierkiewicz 	case 0:
33572d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET +
33672d1100bSBartlomiej Zolnierkiewicz 				 EXYNOS5440_TMU_S0_7_TRIM);
33772d1100bSBartlomiej Zolnierkiewicz 		break;
33872d1100bSBartlomiej Zolnierkiewicz 	case 1:
33972d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM);
34072d1100bSBartlomiej Zolnierkiewicz 		break;
34172d1100bSBartlomiej Zolnierkiewicz 	case 2:
34272d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET +
34372d1100bSBartlomiej Zolnierkiewicz 				  EXYNOS5440_TMU_S0_7_TRIM);
34472d1100bSBartlomiej Zolnierkiewicz 	}
34572d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
34672d1100bSBartlomiej Zolnierkiewicz 
34772d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
34872d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0);
34972d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
35072d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0);
35172d1100bSBartlomiej Zolnierkiewicz 	writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1);
35272d1100bSBartlomiej Zolnierkiewicz 
35372d1100bSBartlomiej Zolnierkiewicz 	exynos_tmu_clear_irqs(data);
35472d1100bSBartlomiej Zolnierkiewicz 
35572d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
35672d1100bSBartlomiej Zolnierkiewicz 	i = pdata->max_trigger_level - 1;
35772d1100bSBartlomiej Zolnierkiewicz 	if (pdata->trigger_levels[i] && pdata->trigger_type[i] == HW_TRIP) {
35872d1100bSBartlomiej Zolnierkiewicz 		threshold_code = temp_to_code(data, pdata->trigger_levels[i]);
35972d1100bSBartlomiej Zolnierkiewicz 		/* 5th level to be assigned in th2 reg */
36072d1100bSBartlomiej Zolnierkiewicz 		rising_threshold =
36172d1100bSBartlomiej Zolnierkiewicz 			threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT;
36272d1100bSBartlomiej Zolnierkiewicz 		writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2);
36372d1100bSBartlomiej Zolnierkiewicz 		con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL);
36472d1100bSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
36572d1100bSBartlomiej Zolnierkiewicz 		writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
36672d1100bSBartlomiej Zolnierkiewicz 	}
36772d1100bSBartlomiej Zolnierkiewicz 	/* Clear the PMIN in the common TMU register */
36872d1100bSBartlomiej Zolnierkiewicz 	if (!data->id)
36972d1100bSBartlomiej Zolnierkiewicz 		writel(0, data->base_second + EXYNOS5440_TMU_PMIN);
37072d1100bSBartlomiej Zolnierkiewicz 	return ret;
37172d1100bSBartlomiej Zolnierkiewicz }
37272d1100bSBartlomiej Zolnierkiewicz 
37337f9034fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_control(struct platform_device *pdev, bool on)
37437f9034fSBartlomiej Zolnierkiewicz {
37537f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
37637f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
37737f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
37837f9034fSBartlomiej Zolnierkiewicz 
37937f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
38037f9034fSBartlomiej Zolnierkiewicz 
38137f9034fSBartlomiej Zolnierkiewicz 	if (on) {
38237f9034fSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
38337f9034fSBartlomiej Zolnierkiewicz 		interrupt_en =
38437f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[3] << EXYNOS_TMU_INTEN_RISE3_SHIFT |
38537f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[2] << EXYNOS_TMU_INTEN_RISE2_SHIFT |
38637f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[1] << EXYNOS_TMU_INTEN_RISE1_SHIFT |
38737f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[0] << EXYNOS_TMU_INTEN_RISE0_SHIFT;
38837f9034fSBartlomiej Zolnierkiewicz 		if (TMU_SUPPORTS(pdata, FALLING_TRIP))
38937f9034fSBartlomiej Zolnierkiewicz 			interrupt_en |=
39037f9034fSBartlomiej Zolnierkiewicz 				interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
39137f9034fSBartlomiej Zolnierkiewicz 	} else {
39237f9034fSBartlomiej Zolnierkiewicz 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
39337f9034fSBartlomiej Zolnierkiewicz 		interrupt_en = 0; /* Disable all interrupts */
39437f9034fSBartlomiej Zolnierkiewicz 	}
39537f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
39637f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
39737f9034fSBartlomiej Zolnierkiewicz }
39837f9034fSBartlomiej Zolnierkiewicz 
39937f9034fSBartlomiej Zolnierkiewicz static void exynos5440_tmu_control(struct platform_device *pdev, bool on)
40037f9034fSBartlomiej Zolnierkiewicz {
40137f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
40237f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_platform_data *pdata = data->pdata;
40337f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
40437f9034fSBartlomiej Zolnierkiewicz 
40537f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL));
40637f9034fSBartlomiej Zolnierkiewicz 
40737f9034fSBartlomiej Zolnierkiewicz 	if (on) {
40837f9034fSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
40937f9034fSBartlomiej Zolnierkiewicz 		interrupt_en =
41037f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[3] << EXYNOS5440_TMU_INTEN_RISE3_SHIFT |
41137f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[2] << EXYNOS5440_TMU_INTEN_RISE2_SHIFT |
41237f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[1] << EXYNOS5440_TMU_INTEN_RISE1_SHIFT |
41337f9034fSBartlomiej Zolnierkiewicz 			pdata->trigger_enable[0] << EXYNOS5440_TMU_INTEN_RISE0_SHIFT;
41437f9034fSBartlomiej Zolnierkiewicz 		if (TMU_SUPPORTS(pdata, FALLING_TRIP))
41537f9034fSBartlomiej Zolnierkiewicz 			interrupt_en |=
41637f9034fSBartlomiej Zolnierkiewicz 				interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT;
41737f9034fSBartlomiej Zolnierkiewicz 	} else {
41837f9034fSBartlomiej Zolnierkiewicz 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
41937f9034fSBartlomiej Zolnierkiewicz 		interrupt_en = 0; /* Disable all interrupts */
42037f9034fSBartlomiej Zolnierkiewicz 	}
42137f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN);
42237f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
42337f9034fSBartlomiej Zolnierkiewicz }
42437f9034fSBartlomiej Zolnierkiewicz 
42559dfa54cSAmit Daniel Kachhap static int exynos_tmu_read(struct exynos_tmu_data *data)
42659dfa54cSAmit Daniel Kachhap {
427*b79985caSBartlomiej Zolnierkiewicz 	int ret;
42859dfa54cSAmit Daniel Kachhap 
42959dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
43059dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
431*b79985caSBartlomiej Zolnierkiewicz 	ret = data->tmu_read(data);
432*b79985caSBartlomiej Zolnierkiewicz 	if (ret >= 0)
433*b79985caSBartlomiej Zolnierkiewicz 		ret = code_to_temp(data, ret);
43459dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
43559dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
43659dfa54cSAmit Daniel Kachhap 
437*b79985caSBartlomiej Zolnierkiewicz 	return ret;
43859dfa54cSAmit Daniel Kachhap }
43959dfa54cSAmit Daniel Kachhap 
44059dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
44159dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
44259dfa54cSAmit Daniel Kachhap {
44359dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
444b8d582b9SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
445b8d582b9SAmit Daniel Kachhap 	const struct exynos_tmu_registers *reg = pdata->registers;
446b8d582b9SAmit Daniel Kachhap 	unsigned int val;
44759dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
44859dfa54cSAmit Daniel Kachhap 
449f4dae753SAmit Daniel Kachhap 	if (!TMU_SUPPORTS(pdata, EMULATION))
45059dfa54cSAmit Daniel Kachhap 		goto out;
45159dfa54cSAmit Daniel Kachhap 
45259dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
45359dfa54cSAmit Daniel Kachhap 		goto out;
45459dfa54cSAmit Daniel Kachhap 
45559dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
45659dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
45759dfa54cSAmit Daniel Kachhap 
458b8d582b9SAmit Daniel Kachhap 	val = readl(data->base + reg->emul_con);
45959dfa54cSAmit Daniel Kachhap 
46059dfa54cSAmit Daniel Kachhap 	if (temp) {
46159dfa54cSAmit Daniel Kachhap 		temp /= MCELSIUS;
46259dfa54cSAmit Daniel Kachhap 
463f4dae753SAmit Daniel Kachhap 		if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
4646070c2caSBartlomiej Zolnierkiewicz 			val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
4656070c2caSBartlomiej Zolnierkiewicz 			val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
466f4dae753SAmit Daniel Kachhap 		}
4679e288d64SBartlomiej Zolnierkiewicz 		val &= ~(EXYNOS_EMUL_DATA_MASK << EXYNOS_EMUL_DATA_SHIFT);
4689e288d64SBartlomiej Zolnierkiewicz 		val |= (temp_to_code(data, temp) << EXYNOS_EMUL_DATA_SHIFT) |
469f4dae753SAmit Daniel Kachhap 			EXYNOS_EMUL_ENABLE;
47059dfa54cSAmit Daniel Kachhap 	} else {
471b8d582b9SAmit Daniel Kachhap 		val &= ~EXYNOS_EMUL_ENABLE;
47259dfa54cSAmit Daniel Kachhap 	}
47359dfa54cSAmit Daniel Kachhap 
474b8d582b9SAmit Daniel Kachhap 	writel(val, data->base + reg->emul_con);
47559dfa54cSAmit Daniel Kachhap 
47659dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
47759dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
47859dfa54cSAmit Daniel Kachhap 	return 0;
47959dfa54cSAmit Daniel Kachhap out:
48059dfa54cSAmit Daniel Kachhap 	return ret;
48159dfa54cSAmit Daniel Kachhap }
48259dfa54cSAmit Daniel Kachhap #else
48359dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
48459dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
48559dfa54cSAmit Daniel Kachhap #endif/*CONFIG_THERMAL_EMULATION*/
48659dfa54cSAmit Daniel Kachhap 
487*b79985caSBartlomiej Zolnierkiewicz static int exynos4210_tmu_read(struct exynos_tmu_data *data)
488*b79985caSBartlomiej Zolnierkiewicz {
489*b79985caSBartlomiej Zolnierkiewicz 	int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
490*b79985caSBartlomiej Zolnierkiewicz 
491*b79985caSBartlomiej Zolnierkiewicz 	/* "temp_code" should range between 75 and 175 */
492*b79985caSBartlomiej Zolnierkiewicz 	return (ret < 75 || ret > 175) ? -ENODATA : ret;
493*b79985caSBartlomiej Zolnierkiewicz }
494*b79985caSBartlomiej Zolnierkiewicz 
495*b79985caSBartlomiej Zolnierkiewicz static int exynos4412_tmu_read(struct exynos_tmu_data *data)
496*b79985caSBartlomiej Zolnierkiewicz {
497*b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
498*b79985caSBartlomiej Zolnierkiewicz }
499*b79985caSBartlomiej Zolnierkiewicz 
500*b79985caSBartlomiej Zolnierkiewicz static int exynos5440_tmu_read(struct exynos_tmu_data *data)
501*b79985caSBartlomiej Zolnierkiewicz {
502*b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS5440_TMU_S0_7_TEMP);
503*b79985caSBartlomiej Zolnierkiewicz }
504*b79985caSBartlomiej Zolnierkiewicz 
50559dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
50659dfa54cSAmit Daniel Kachhap {
50759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
50859dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
509b835ced1SBartlomiej Zolnierkiewicz 	unsigned int val_type;
510a0395eeeSAmit Daniel Kachhap 
51114a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
51214a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
513a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
514421d5d12SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5440) {
515421d5d12SBartlomiej Zolnierkiewicz 		val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS);
516a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
517a0395eeeSAmit Daniel Kachhap 			goto out;
518a0395eeeSAmit Daniel Kachhap 	}
51914a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
52014a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
52159dfa54cSAmit Daniel Kachhap 
522cebe7373SAmit Daniel Kachhap 	exynos_report_trigger(data->reg_conf);
52359dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
52459dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
525b8d582b9SAmit Daniel Kachhap 
526a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
527b835ced1SBartlomiej Zolnierkiewicz 	exynos_tmu_clear_irqs(data);
528b8d582b9SAmit Daniel Kachhap 
52959dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
53059dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
531a0395eeeSAmit Daniel Kachhap out:
53259dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
53359dfa54cSAmit Daniel Kachhap }
53459dfa54cSAmit Daniel Kachhap 
53559dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
53659dfa54cSAmit Daniel Kachhap {
53759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
53859dfa54cSAmit Daniel Kachhap 
53959dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
54059dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
54159dfa54cSAmit Daniel Kachhap 
54259dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
54359dfa54cSAmit Daniel Kachhap }
54459dfa54cSAmit Daniel Kachhap 
54559dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
54659dfa54cSAmit Daniel Kachhap 	{
5471fe56dc1SChanwoo Choi 		.compatible = "samsung,exynos3250-tmu",
5481fe56dc1SChanwoo Choi 		.data = (void *)EXYNOS3250_TMU_DRV_DATA,
5491fe56dc1SChanwoo Choi 	},
5501fe56dc1SChanwoo Choi 	{
55159dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4210-tmu",
55259dfa54cSAmit Daniel Kachhap 		.data = (void *)EXYNOS4210_TMU_DRV_DATA,
55359dfa54cSAmit Daniel Kachhap 	},
55459dfa54cSAmit Daniel Kachhap 	{
55559dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos4412-tmu",
55614ddfaecSLukasz Majewski 		.data = (void *)EXYNOS4412_TMU_DRV_DATA,
55759dfa54cSAmit Daniel Kachhap 	},
55859dfa54cSAmit Daniel Kachhap 	{
55959dfa54cSAmit Daniel Kachhap 		.compatible = "samsung,exynos5250-tmu",
560e6b7991eSAmit Daniel Kachhap 		.data = (void *)EXYNOS5250_TMU_DRV_DATA,
56159dfa54cSAmit Daniel Kachhap 	},
56290542546SAmit Daniel Kachhap 	{
563923488a5SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5260-tmu",
564923488a5SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5260_TMU_DRV_DATA,
565923488a5SNaveen Krishna Chatradhi 	},
566923488a5SNaveen Krishna Chatradhi 	{
56714a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu",
56814a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
56914a11dc7SNaveen Krishna Chatradhi 	},
57014a11dc7SNaveen Krishna Chatradhi 	{
57114a11dc7SNaveen Krishna Chatradhi 		.compatible = "samsung,exynos5420-tmu-ext-triminfo",
57214a11dc7SNaveen Krishna Chatradhi 		.data = (void *)EXYNOS5420_TMU_DRV_DATA,
57314a11dc7SNaveen Krishna Chatradhi 	},
57414a11dc7SNaveen Krishna Chatradhi 	{
57590542546SAmit Daniel Kachhap 		.compatible = "samsung,exynos5440-tmu",
57690542546SAmit Daniel Kachhap 		.data = (void *)EXYNOS5440_TMU_DRV_DATA,
57790542546SAmit Daniel Kachhap 	},
57859dfa54cSAmit Daniel Kachhap 	{},
57959dfa54cSAmit Daniel Kachhap };
58059dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
58159dfa54cSAmit Daniel Kachhap 
58259dfa54cSAmit Daniel Kachhap static inline struct  exynos_tmu_platform_data *exynos_get_driver_data(
583cebe7373SAmit Daniel Kachhap 			struct platform_device *pdev, int id)
58459dfa54cSAmit Daniel Kachhap {
585cebe7373SAmit Daniel Kachhap 	struct  exynos_tmu_init_data *data_table;
586cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *tmu_data;
58759dfa54cSAmit Daniel Kachhap 	const struct of_device_id *match;
58873b5b1d7SSachin Kamat 
58959dfa54cSAmit Daniel Kachhap 	match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
59059dfa54cSAmit Daniel Kachhap 	if (!match)
59159dfa54cSAmit Daniel Kachhap 		return NULL;
592cebe7373SAmit Daniel Kachhap 	data_table = (struct exynos_tmu_init_data *) match->data;
593cebe7373SAmit Daniel Kachhap 	if (!data_table || id >= data_table->tmu_count)
594cebe7373SAmit Daniel Kachhap 		return NULL;
595cebe7373SAmit Daniel Kachhap 	tmu_data = data_table->tmu_data;
596cebe7373SAmit Daniel Kachhap 	return (struct exynos_tmu_platform_data *) (tmu_data + id);
59759dfa54cSAmit Daniel Kachhap }
59859dfa54cSAmit Daniel Kachhap 
599cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
60059dfa54cSAmit Daniel Kachhap {
601cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
602cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
603cebe7373SAmit Daniel Kachhap 	struct resource res;
604498d22f6SAmit Daniel Kachhap 	int ret;
60559dfa54cSAmit Daniel Kachhap 
60673b5b1d7SSachin Kamat 	if (!data || !pdev->dev.of_node)
607cebe7373SAmit Daniel Kachhap 		return -ENODEV;
60859dfa54cSAmit Daniel Kachhap 
609498d22f6SAmit Daniel Kachhap 	/*
610498d22f6SAmit Daniel Kachhap 	 * Try enabling the regulator if found
611498d22f6SAmit Daniel Kachhap 	 * TODO: Add regulator as an SOC feature, so that regulator enable
612498d22f6SAmit Daniel Kachhap 	 * is a compulsory call.
613498d22f6SAmit Daniel Kachhap 	 */
614498d22f6SAmit Daniel Kachhap 	data->regulator = devm_regulator_get(&pdev->dev, "vtmu");
615498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator)) {
616498d22f6SAmit Daniel Kachhap 		ret = regulator_enable(data->regulator);
617498d22f6SAmit Daniel Kachhap 		if (ret) {
618498d22f6SAmit Daniel Kachhap 			dev_err(&pdev->dev, "failed to enable vtmu\n");
619498d22f6SAmit Daniel Kachhap 			return ret;
620498d22f6SAmit Daniel Kachhap 		}
621498d22f6SAmit Daniel Kachhap 	} else {
622498d22f6SAmit Daniel Kachhap 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
623498d22f6SAmit Daniel Kachhap 	}
624498d22f6SAmit Daniel Kachhap 
625cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
626cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
627cebe7373SAmit Daniel Kachhap 		data->id = 0;
628cebe7373SAmit Daniel Kachhap 
629cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
630cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
631cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
632cebe7373SAmit Daniel Kachhap 		return -ENODEV;
633cebe7373SAmit Daniel Kachhap 	}
634cebe7373SAmit Daniel Kachhap 
635cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
636cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
637cebe7373SAmit Daniel Kachhap 		return -ENODEV;
638cebe7373SAmit Daniel Kachhap 	}
639cebe7373SAmit Daniel Kachhap 
640cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
641cebe7373SAmit Daniel Kachhap 	if (!data->base) {
642cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
643cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
644cebe7373SAmit Daniel Kachhap 	}
645cebe7373SAmit Daniel Kachhap 
646cebe7373SAmit Daniel Kachhap 	pdata = exynos_get_driver_data(pdev, data->id);
64759dfa54cSAmit Daniel Kachhap 	if (!pdata) {
64859dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "No platform init data supplied.\n");
64959dfa54cSAmit Daniel Kachhap 		return -ENODEV;
65059dfa54cSAmit Daniel Kachhap 	}
651cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
652d9b6ee14SAmit Daniel Kachhap 	/*
653d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
654d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
655d9b6ee14SAmit Daniel Kachhap 	 */
6569025d563SNaveen Krishna Chatradhi 	if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE))
657d9b6ee14SAmit Daniel Kachhap 		return 0;
658d9b6ee14SAmit Daniel Kachhap 
659d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
660d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
661d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
662d9b6ee14SAmit Daniel Kachhap 	}
663d9b6ee14SAmit Daniel Kachhap 
6649025d563SNaveen Krishna Chatradhi 	data->base_second = devm_ioremap(&pdev->dev, res.start,
665d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
6669025d563SNaveen Krishna Chatradhi 	if (!data->base_second) {
667d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
668d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
669d9b6ee14SAmit Daniel Kachhap 	}
670cebe7373SAmit Daniel Kachhap 
671cebe7373SAmit Daniel Kachhap 	return 0;
672cebe7373SAmit Daniel Kachhap }
673cebe7373SAmit Daniel Kachhap 
674cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
675cebe7373SAmit Daniel Kachhap {
676cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data;
677cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
678cebe7373SAmit Daniel Kachhap 	struct thermal_sensor_conf *sensor_conf;
679cebe7373SAmit Daniel Kachhap 	int ret, i;
680cebe7373SAmit Daniel Kachhap 
68159dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
68259dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
6832a9675b3SJingoo Han 	if (!data)
68459dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
68559dfa54cSAmit Daniel Kachhap 
686cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
687cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
688cebe7373SAmit Daniel Kachhap 
689cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
690cebe7373SAmit Daniel Kachhap 	if (ret)
691cebe7373SAmit Daniel Kachhap 		return ret;
692cebe7373SAmit Daniel Kachhap 
693cebe7373SAmit Daniel Kachhap 	pdata = data->pdata;
69459dfa54cSAmit Daniel Kachhap 
69559dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
69659dfa54cSAmit Daniel Kachhap 
69759dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
69859dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
69959dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
70059dfa54cSAmit Daniel Kachhap 		return  PTR_ERR(data->clk);
70159dfa54cSAmit Daniel Kachhap 	}
70259dfa54cSAmit Daniel Kachhap 
70314a11dc7SNaveen Krishna Chatradhi 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
70414a11dc7SNaveen Krishna Chatradhi 	if (IS_ERR(data->clk_sec)) {
70514a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
70614a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
70714a11dc7SNaveen Krishna Chatradhi 			return PTR_ERR(data->clk_sec);
70814a11dc7SNaveen Krishna Chatradhi 		}
70914a11dc7SNaveen Krishna Chatradhi 	} else {
71014a11dc7SNaveen Krishna Chatradhi 		ret = clk_prepare(data->clk_sec);
71114a11dc7SNaveen Krishna Chatradhi 		if (ret) {
71214a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get clock\n");
71359dfa54cSAmit Daniel Kachhap 			return ret;
71414a11dc7SNaveen Krishna Chatradhi 		}
71514a11dc7SNaveen Krishna Chatradhi 	}
71614a11dc7SNaveen Krishna Chatradhi 
71714a11dc7SNaveen Krishna Chatradhi 	ret = clk_prepare(data->clk);
71814a11dc7SNaveen Krishna Chatradhi 	if (ret) {
71914a11dc7SNaveen Krishna Chatradhi 		dev_err(&pdev->dev, "Failed to get clock\n");
72014a11dc7SNaveen Krishna Chatradhi 		goto err_clk_sec;
72114a11dc7SNaveen Krishna Chatradhi 	}
72259dfa54cSAmit Daniel Kachhap 
72359dfa54cSAmit Daniel Kachhap 	data->soc = pdata->type;
72472d1100bSBartlomiej Zolnierkiewicz 
72572d1100bSBartlomiej Zolnierkiewicz 	switch (data->soc) {
72672d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4210:
72772d1100bSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4210_tmu_initialize;
72837f9034fSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
729*b79985caSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4210_tmu_read;
73072d1100bSBartlomiej Zolnierkiewicz 		break;
73172d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS3250:
73272d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4412:
73372d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5250:
73472d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5260:
73572d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420:
73672d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420_TRIMINFO:
73772d1100bSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4412_tmu_initialize;
73837f9034fSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
739*b79985caSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4412_tmu_read;
74072d1100bSBartlomiej Zolnierkiewicz 		break;
74172d1100bSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5440:
74272d1100bSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos5440_tmu_initialize;
74337f9034fSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos5440_tmu_control;
744*b79985caSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos5440_tmu_read;
74572d1100bSBartlomiej Zolnierkiewicz 		break;
74672d1100bSBartlomiej Zolnierkiewicz 	default:
74759dfa54cSAmit Daniel Kachhap 		ret = -EINVAL;
74859dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Platform not supported\n");
74959dfa54cSAmit Daniel Kachhap 		goto err_clk;
75059dfa54cSAmit Daniel Kachhap 	}
75159dfa54cSAmit Daniel Kachhap 
75259dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
75359dfa54cSAmit Daniel Kachhap 	if (ret) {
75459dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
75559dfa54cSAmit Daniel Kachhap 		goto err_clk;
75659dfa54cSAmit Daniel Kachhap 	}
75759dfa54cSAmit Daniel Kachhap 
75859dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
75959dfa54cSAmit Daniel Kachhap 
760cebe7373SAmit Daniel Kachhap 	/* Allocate a structure to register with the exynos core thermal */
761cebe7373SAmit Daniel Kachhap 	sensor_conf = devm_kzalloc(&pdev->dev,
762cebe7373SAmit Daniel Kachhap 				sizeof(struct thermal_sensor_conf), GFP_KERNEL);
763cebe7373SAmit Daniel Kachhap 	if (!sensor_conf) {
764cebe7373SAmit Daniel Kachhap 		ret = -ENOMEM;
765cebe7373SAmit Daniel Kachhap 		goto err_clk;
766cebe7373SAmit Daniel Kachhap 	}
767cebe7373SAmit Daniel Kachhap 	sprintf(sensor_conf->name, "therm_zone%d", data->id);
768cebe7373SAmit Daniel Kachhap 	sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
769cebe7373SAmit Daniel Kachhap 	sensor_conf->write_emul_temp =
770cebe7373SAmit Daniel Kachhap 		(int (*)(void *, unsigned long))exynos_tmu_set_emulation;
771cebe7373SAmit Daniel Kachhap 	sensor_conf->driver_data = data;
772cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
773bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[1] + pdata->trigger_enable[2]+
774bb34b4c8SAmit Daniel Kachhap 			pdata->trigger_enable[3];
77559dfa54cSAmit Daniel Kachhap 
776cebe7373SAmit Daniel Kachhap 	for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
777cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_val[i] =
77859dfa54cSAmit Daniel Kachhap 			pdata->threshold + pdata->trigger_levels[i];
779cebe7373SAmit Daniel Kachhap 		sensor_conf->trip_data.trip_type[i] =
7805c3cf552SAmit Daniel Kachhap 					pdata->trigger_type[i];
7815c3cf552SAmit Daniel Kachhap 	}
78259dfa54cSAmit Daniel Kachhap 
783cebe7373SAmit Daniel Kachhap 	sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
78459dfa54cSAmit Daniel Kachhap 
785cebe7373SAmit Daniel Kachhap 	sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
78659dfa54cSAmit Daniel Kachhap 	for (i = 0; i < pdata->freq_tab_count; i++) {
787cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].freq_clip_max =
78859dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].freq_clip_max;
789cebe7373SAmit Daniel Kachhap 		sensor_conf->cooling_data.freq_data[i].temp_level =
79059dfa54cSAmit Daniel Kachhap 					pdata->freq_tab[i].temp_level;
79159dfa54cSAmit Daniel Kachhap 	}
792cebe7373SAmit Daniel Kachhap 	sensor_conf->dev = &pdev->dev;
793cebe7373SAmit Daniel Kachhap 	/* Register the sensor with thermal management interface */
794cebe7373SAmit Daniel Kachhap 	ret = exynos_register_thermal(sensor_conf);
79559dfa54cSAmit Daniel Kachhap 	if (ret) {
79659dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to register thermal interface\n");
79759dfa54cSAmit Daniel Kachhap 		goto err_clk;
79859dfa54cSAmit Daniel Kachhap 	}
799cebe7373SAmit Daniel Kachhap 	data->reg_conf = sensor_conf;
800cebe7373SAmit Daniel Kachhap 
801cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
802cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
803cebe7373SAmit Daniel Kachhap 	if (ret) {
804cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
805cebe7373SAmit Daniel Kachhap 		goto err_clk;
806cebe7373SAmit Daniel Kachhap 	}
80759dfa54cSAmit Daniel Kachhap 
80859dfa54cSAmit Daniel Kachhap 	return 0;
80959dfa54cSAmit Daniel Kachhap err_clk:
81059dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
81114a11dc7SNaveen Krishna Chatradhi err_clk_sec:
81214a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
81314a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
81459dfa54cSAmit Daniel Kachhap 	return ret;
81559dfa54cSAmit Daniel Kachhap }
81659dfa54cSAmit Daniel Kachhap 
81759dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
81859dfa54cSAmit Daniel Kachhap {
81959dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
82059dfa54cSAmit Daniel Kachhap 
821cebe7373SAmit Daniel Kachhap 	exynos_unregister_thermal(data->reg_conf);
82259dfa54cSAmit Daniel Kachhap 
8234215688eSBartlomiej Zolnierkiewicz 	exynos_tmu_control(pdev, false);
8244215688eSBartlomiej Zolnierkiewicz 
82559dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
82614a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
82714a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
82859dfa54cSAmit Daniel Kachhap 
829498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator))
830498d22f6SAmit Daniel Kachhap 		regulator_disable(data->regulator);
831498d22f6SAmit Daniel Kachhap 
83259dfa54cSAmit Daniel Kachhap 	return 0;
83359dfa54cSAmit Daniel Kachhap }
83459dfa54cSAmit Daniel Kachhap 
83559dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
83659dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
83759dfa54cSAmit Daniel Kachhap {
83859dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
83959dfa54cSAmit Daniel Kachhap 
84059dfa54cSAmit Daniel Kachhap 	return 0;
84159dfa54cSAmit Daniel Kachhap }
84259dfa54cSAmit Daniel Kachhap 
84359dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
84459dfa54cSAmit Daniel Kachhap {
84559dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
84659dfa54cSAmit Daniel Kachhap 
84759dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
84859dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
84959dfa54cSAmit Daniel Kachhap 
85059dfa54cSAmit Daniel Kachhap 	return 0;
85159dfa54cSAmit Daniel Kachhap }
85259dfa54cSAmit Daniel Kachhap 
85359dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
85459dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
85559dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
85659dfa54cSAmit Daniel Kachhap #else
85759dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
85859dfa54cSAmit Daniel Kachhap #endif
85959dfa54cSAmit Daniel Kachhap 
86059dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
86159dfa54cSAmit Daniel Kachhap 	.driver = {
86259dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
86359dfa54cSAmit Daniel Kachhap 		.owner  = THIS_MODULE,
86459dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
86573b5b1d7SSachin Kamat 		.of_match_table = exynos_tmu_match,
86659dfa54cSAmit Daniel Kachhap 	},
86759dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
86859dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
86959dfa54cSAmit Daniel Kachhap };
87059dfa54cSAmit Daniel Kachhap 
87159dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
87259dfa54cSAmit Daniel Kachhap 
87359dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
87459dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
87559dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
87659dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
877