xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision 5f09a5cbd14ae16e93866040fa44d930ff885650)
159dfa54cSAmit Daniel Kachhap /*
259dfa54cSAmit Daniel Kachhap  * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit)
359dfa54cSAmit Daniel Kachhap  *
43b6a1a80SLukasz Majewski  *  Copyright (C) 2014 Samsung Electronics
53b6a1a80SLukasz Majewski  *  Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
63b6a1a80SLukasz Majewski  *  Lukasz Majewski <l.majewski@samsung.com>
73b6a1a80SLukasz Majewski  *
859dfa54cSAmit Daniel Kachhap  *  Copyright (C) 2011 Samsung Electronics
959dfa54cSAmit Daniel Kachhap  *  Donggeun Kim <dg77.kim@samsung.com>
1059dfa54cSAmit Daniel Kachhap  *  Amit Daniel Kachhap <amit.kachhap@linaro.org>
1159dfa54cSAmit Daniel Kachhap  *
1259dfa54cSAmit Daniel Kachhap  * This program is free software; you can redistribute it and/or modify
1359dfa54cSAmit Daniel Kachhap  * it under the terms of the GNU General Public License as published by
1459dfa54cSAmit Daniel Kachhap  * the Free Software Foundation; either version 2 of the License, or
1559dfa54cSAmit Daniel Kachhap  * (at your option) any later version.
1659dfa54cSAmit Daniel Kachhap  *
1759dfa54cSAmit Daniel Kachhap  * This program is distributed in the hope that it will be useful,
1859dfa54cSAmit Daniel Kachhap  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1959dfa54cSAmit Daniel Kachhap  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2059dfa54cSAmit Daniel Kachhap  * GNU General Public License for more details.
2159dfa54cSAmit Daniel Kachhap  *
2259dfa54cSAmit Daniel Kachhap  * You should have received a copy of the GNU General Public License
2359dfa54cSAmit Daniel Kachhap  * along with this program; if not, write to the Free Software
2459dfa54cSAmit Daniel Kachhap  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2559dfa54cSAmit Daniel Kachhap  *
2659dfa54cSAmit Daniel Kachhap  */
2759dfa54cSAmit Daniel Kachhap 
2859dfa54cSAmit Daniel Kachhap #include <linux/clk.h>
2959dfa54cSAmit Daniel Kachhap #include <linux/io.h>
3059dfa54cSAmit Daniel Kachhap #include <linux/interrupt.h>
3159dfa54cSAmit Daniel Kachhap #include <linux/module.h>
3259dfa54cSAmit Daniel Kachhap #include <linux/of.h>
33cebe7373SAmit Daniel Kachhap #include <linux/of_address.h>
34cebe7373SAmit Daniel Kachhap #include <linux/of_irq.h>
3559dfa54cSAmit Daniel Kachhap #include <linux/platform_device.h>
36498d22f6SAmit Daniel Kachhap #include <linux/regulator/consumer.h>
3759dfa54cSAmit Daniel Kachhap 
380c1836a6SAmit Daniel Kachhap #include "exynos_tmu.h"
393b6a1a80SLukasz Majewski #include "../thermal_core.h"
402845f6ecSBartlomiej Zolnierkiewicz 
412845f6ecSBartlomiej Zolnierkiewicz /* Exynos generic registers */
422845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_TRIMINFO		0x0
432845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_CONTROL		0x20
442845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_STATUS		0x28
452845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_CURRENT_TEMP	0x40
462845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_INTEN		0x70
472845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_INTSTAT		0x74
482845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REG_INTCLEAR		0x78
492845f6ecSBartlomiej Zolnierkiewicz 
502845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TEMP_MASK		0xff
512845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REF_VOLTAGE_SHIFT	24
522845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_REF_VOLTAGE_MASK	0x1f
532845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_BUF_SLOPE_SEL_MASK	0xf
542845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT	8
552845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_CORE_EN_SHIFT	0
562845f6ecSBartlomiej Zolnierkiewicz 
572845f6ecSBartlomiej Zolnierkiewicz /* Exynos3250 specific registers */
582845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIMINFO_CON1	0x10
592845f6ecSBartlomiej Zolnierkiewicz 
602845f6ecSBartlomiej Zolnierkiewicz /* Exynos4210 specific registers */
612845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4210_TMU_REG_THRESHOLD_TEMP	0x44
622845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4210_TMU_REG_TRIG_LEVEL0	0x50
632845f6ecSBartlomiej Zolnierkiewicz 
642845f6ecSBartlomiej Zolnierkiewicz /* Exynos5250, Exynos4412, Exynos3250 specific registers */
652845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIMINFO_CON2	0x14
662845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_THD_TEMP_RISE		0x50
672845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_THD_TEMP_FALL		0x54
682845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_CON		0x80
692845f6ecSBartlomiej Zolnierkiewicz 
702845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TRIMINFO_RELOAD_ENABLE	1
712845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TRIMINFO_25_SHIFT	0
722845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TRIMINFO_85_SHIFT	8
732845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIP_MODE_SHIFT	13
742845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_TRIP_MODE_MASK	0x7
752845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_THERM_TRIP_EN_SHIFT	12
762845f6ecSBartlomiej Zolnierkiewicz 
772845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE0_SHIFT	0
782845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE1_SHIFT	4
792845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE2_SHIFT	8
802845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_RISE3_SHIFT	12
812845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_TMU_INTEN_FALL0_SHIFT	16
822845f6ecSBartlomiej Zolnierkiewicz 
832845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_TIME	0x57F0
842845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_TIME_MASK	0xffff
852845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_TIME_SHIFT	16
862845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_DATA_SHIFT	8
872845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_DATA_MASK	0xFF
882845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS_EMUL_ENABLE	0x1
892845f6ecSBartlomiej Zolnierkiewicz 
902845f6ecSBartlomiej Zolnierkiewicz /* Exynos5260 specific */
912845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_TMU_REG_INTEN		0xC0
922845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_TMU_REG_INTSTAT		0xC4
932845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_TMU_REG_INTCLEAR		0xC8
942845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5260_EMUL_CON			0x100
952845f6ecSBartlomiej Zolnierkiewicz 
962845f6ecSBartlomiej Zolnierkiewicz /* Exynos4412 specific */
972845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4412_MUX_ADDR_VALUE          6
982845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS4412_MUX_ADDR_SHIFT          20
992845f6ecSBartlomiej Zolnierkiewicz 
100488c7455SChanwoo Choi /* Exynos5433 specific registers */
101488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_CONTROL1		0x024
102488c7455SChanwoo Choi #define EXYNOS5433_TMU_SAMPLING_INTERVAL	0x02c
103488c7455SChanwoo Choi #define EXYNOS5433_TMU_COUNTER_VALUE0		0x030
104488c7455SChanwoo Choi #define EXYNOS5433_TMU_COUNTER_VALUE1		0x034
105488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_CURRENT_TEMP1	0x044
106488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_RISE3_0		0x050
107488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_RISE7_4		0x054
108488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_FALL3_0		0x060
109488c7455SChanwoo Choi #define EXYNOS5433_THD_TEMP_FALL7_4		0x064
110488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_INTEN		0x0c0
111488c7455SChanwoo Choi #define EXYNOS5433_TMU_REG_INTPEND		0x0c8
112488c7455SChanwoo Choi #define EXYNOS5433_TMU_EMUL_CON			0x110
113488c7455SChanwoo Choi #define EXYNOS5433_TMU_PD_DET_EN		0x130
114488c7455SChanwoo Choi 
115488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT	16
116488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT	23
117488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_SENSOR_ID_MASK	\
118488c7455SChanwoo Choi 			(0xf << EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT)
119488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_CALIB_SEL_MASK	BIT(23)
120488c7455SChanwoo Choi 
121488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING	0
122488c7455SChanwoo Choi #define EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING	1
123488c7455SChanwoo Choi 
124488c7455SChanwoo Choi #define EXYNOS5433_PD_DET_EN			1
125488c7455SChanwoo Choi 
1262845f6ecSBartlomiej Zolnierkiewicz /*exynos5440 specific registers*/
1272845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_TRIM		0x000
1282845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_CTRL		0x020
1292845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_DEBUG		0x040
1302845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_TEMP		0x0f0
1312845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_TH0			0x110
1322845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_TH1			0x130
1332845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_TH2			0x150
1342845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_IRQEN		0x210
1352845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_S0_7_IRQ			0x230
1362845f6ecSBartlomiej Zolnierkiewicz /* exynos5440 common registers */
1372845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_IRQ_STATUS		0x000
1382845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_PMIN			0x004
1392845f6ecSBartlomiej Zolnierkiewicz 
1402845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_INTEN_RISE0_SHIFT	0
1412845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_INTEN_RISE1_SHIFT	1
1422845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_INTEN_RISE2_SHIFT	2
1432845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_INTEN_RISE3_SHIFT	3
1442845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_INTEN_FALL0_SHIFT	4
1452845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_TMU_TH_RISE4_SHIFT		24
1462845f6ecSBartlomiej Zolnierkiewicz #define EXYNOS5440_EFUSE_SWAP_OFFSET		8
14759dfa54cSAmit Daniel Kachhap 
1486c247393SAbhilash Kesavan /* Exynos7 specific registers */
1496c247393SAbhilash Kesavan #define EXYNOS7_THD_TEMP_RISE7_6		0x50
1506c247393SAbhilash Kesavan #define EXYNOS7_THD_TEMP_FALL7_6		0x60
1516c247393SAbhilash Kesavan #define EXYNOS7_TMU_REG_INTEN			0x110
1526c247393SAbhilash Kesavan #define EXYNOS7_TMU_REG_INTPEND			0x118
1536c247393SAbhilash Kesavan #define EXYNOS7_TMU_REG_EMUL_CON		0x160
1546c247393SAbhilash Kesavan 
1556c247393SAbhilash Kesavan #define EXYNOS7_TMU_TEMP_MASK			0x1ff
1566c247393SAbhilash Kesavan #define EXYNOS7_PD_DET_EN_SHIFT			23
1576c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE0_SHIFT		0
1586c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE1_SHIFT		1
1596c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE2_SHIFT		2
1606c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE3_SHIFT		3
1616c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE4_SHIFT		4
1626c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE5_SHIFT		5
1636c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE6_SHIFT		6
1646c247393SAbhilash Kesavan #define EXYNOS7_TMU_INTEN_RISE7_SHIFT		7
1656c247393SAbhilash Kesavan #define EXYNOS7_EMUL_DATA_SHIFT			7
1666c247393SAbhilash Kesavan #define EXYNOS7_EMUL_DATA_MASK			0x1ff
1676c247393SAbhilash Kesavan 
1683b6a1a80SLukasz Majewski #define MCELSIUS	1000
169cebe7373SAmit Daniel Kachhap /**
170cebe7373SAmit Daniel Kachhap  * struct exynos_tmu_data : A structure to hold the private data of the TMU
171cebe7373SAmit Daniel Kachhap 	driver
172cebe7373SAmit Daniel Kachhap  * @id: identifier of the one instance of the TMU controller.
173cebe7373SAmit Daniel Kachhap  * @pdata: pointer to the tmu platform/configuration data
174cebe7373SAmit Daniel Kachhap  * @base: base address of the single instance of the TMU controller.
1759025d563SNaveen Krishna Chatradhi  * @base_second: base address of the common registers of the TMU controller.
176cebe7373SAmit Daniel Kachhap  * @irq: irq number of the TMU controller.
177cebe7373SAmit Daniel Kachhap  * @soc: id of the SOC type.
178cebe7373SAmit Daniel Kachhap  * @irq_work: pointer to the irq work structure.
179cebe7373SAmit Daniel Kachhap  * @lock: lock to implement synchronization.
180cebe7373SAmit Daniel Kachhap  * @clk: pointer to the clock structure.
18114a11dc7SNaveen Krishna Chatradhi  * @clk_sec: pointer to the clock structure for accessing the base_second.
1826c247393SAbhilash Kesavan  * @sclk: pointer to the clock structure for accessing the tmu special clk.
183cebe7373SAmit Daniel Kachhap  * @temp_error1: fused value of the first point trim.
184cebe7373SAmit Daniel Kachhap  * @temp_error2: fused value of the second point trim.
185498d22f6SAmit Daniel Kachhap  * @regulator: pointer to the TMU regulator structure.
186cebe7373SAmit Daniel Kachhap  * @reg_conf: pointer to structure to register with core thermal.
18772d1100bSBartlomiej Zolnierkiewicz  * @tmu_initialize: SoC specific TMU initialization method
18837f9034fSBartlomiej Zolnierkiewicz  * @tmu_control: SoC specific TMU control method
189b79985caSBartlomiej Zolnierkiewicz  * @tmu_read: SoC specific TMU temperature read method
190285d994aSBartlomiej Zolnierkiewicz  * @tmu_set_emulation: SoC specific TMU emulation setting method
191a7331f72SBartlomiej Zolnierkiewicz  * @tmu_clear_irqs: SoC specific TMU interrupts clearing method
192cebe7373SAmit Daniel Kachhap  */
19359dfa54cSAmit Daniel Kachhap struct exynos_tmu_data {
194cebe7373SAmit Daniel Kachhap 	int id;
19559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
19659dfa54cSAmit Daniel Kachhap 	void __iomem *base;
1979025d563SNaveen Krishna Chatradhi 	void __iomem *base_second;
19859dfa54cSAmit Daniel Kachhap 	int irq;
19959dfa54cSAmit Daniel Kachhap 	enum soc_type soc;
20059dfa54cSAmit Daniel Kachhap 	struct work_struct irq_work;
20159dfa54cSAmit Daniel Kachhap 	struct mutex lock;
2026c247393SAbhilash Kesavan 	struct clk *clk, *clk_sec, *sclk;
2036c247393SAbhilash Kesavan 	u16 temp_error1, temp_error2;
204498d22f6SAmit Daniel Kachhap 	struct regulator *regulator;
2053b6a1a80SLukasz Majewski 	struct thermal_zone_device *tzd;
2063b6a1a80SLukasz Majewski 
20772d1100bSBartlomiej Zolnierkiewicz 	int (*tmu_initialize)(struct platform_device *pdev);
20837f9034fSBartlomiej Zolnierkiewicz 	void (*tmu_control)(struct platform_device *pdev, bool on);
209b79985caSBartlomiej Zolnierkiewicz 	int (*tmu_read)(struct exynos_tmu_data *data);
210285d994aSBartlomiej Zolnierkiewicz 	void (*tmu_set_emulation)(struct exynos_tmu_data *data,
211285d994aSBartlomiej Zolnierkiewicz 				  unsigned long temp);
212a7331f72SBartlomiej Zolnierkiewicz 	void (*tmu_clear_irqs)(struct exynos_tmu_data *data);
21359dfa54cSAmit Daniel Kachhap };
21459dfa54cSAmit Daniel Kachhap 
2153b6a1a80SLukasz Majewski static void exynos_report_trigger(struct exynos_tmu_data *p)
2163b6a1a80SLukasz Majewski {
2173b6a1a80SLukasz Majewski 	char data[10], *envp[] = { data, NULL };
2183b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = p->tzd;
2193b6a1a80SLukasz Majewski 	unsigned long temp;
2203b6a1a80SLukasz Majewski 	unsigned int i;
2213b6a1a80SLukasz Majewski 
222eccb6014SLukasz Majewski 	if (!tz) {
223eccb6014SLukasz Majewski 		pr_err("No thermal zone device defined\n");
2243b6a1a80SLukasz Majewski 		return;
2253b6a1a80SLukasz Majewski 	}
2263b6a1a80SLukasz Majewski 
2273b6a1a80SLukasz Majewski 	thermal_zone_device_update(tz);
2283b6a1a80SLukasz Majewski 
2293b6a1a80SLukasz Majewski 	mutex_lock(&tz->lock);
2303b6a1a80SLukasz Majewski 	/* Find the level for which trip happened */
2313b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
2323b6a1a80SLukasz Majewski 		tz->ops->get_trip_temp(tz, i, &temp);
2333b6a1a80SLukasz Majewski 		if (tz->last_temperature < temp)
2343b6a1a80SLukasz Majewski 			break;
2353b6a1a80SLukasz Majewski 	}
2363b6a1a80SLukasz Majewski 
2373b6a1a80SLukasz Majewski 	snprintf(data, sizeof(data), "%u", i);
2383b6a1a80SLukasz Majewski 	kobject_uevent_env(&tz->device.kobj, KOBJ_CHANGE, envp);
2393b6a1a80SLukasz Majewski 	mutex_unlock(&tz->lock);
2403b6a1a80SLukasz Majewski }
2413b6a1a80SLukasz Majewski 
24259dfa54cSAmit Daniel Kachhap /*
24359dfa54cSAmit Daniel Kachhap  * TMU treats temperature as a mapped temperature code.
24459dfa54cSAmit Daniel Kachhap  * The temperature is converted differently depending on the calibration type.
24559dfa54cSAmit Daniel Kachhap  */
24659dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
24759dfa54cSAmit Daniel Kachhap {
24859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
24959dfa54cSAmit Daniel Kachhap 	int temp_code;
25059dfa54cSAmit Daniel Kachhap 
25159dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
25259dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
253bb34b4c8SAmit Daniel Kachhap 		temp_code = (temp - pdata->first_point_trim) *
25459dfa54cSAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) /
255bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) +
256bb34b4c8SAmit Daniel Kachhap 			data->temp_error1;
25759dfa54cSAmit Daniel Kachhap 		break;
25859dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
259bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
26059dfa54cSAmit Daniel Kachhap 		break;
26159dfa54cSAmit Daniel Kachhap 	default:
262bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + pdata->default_temp_offset;
26359dfa54cSAmit Daniel Kachhap 		break;
26459dfa54cSAmit Daniel Kachhap 	}
265ddb31d43SBartlomiej Zolnierkiewicz 
26659dfa54cSAmit Daniel Kachhap 	return temp_code;
26759dfa54cSAmit Daniel Kachhap }
26859dfa54cSAmit Daniel Kachhap 
26959dfa54cSAmit Daniel Kachhap /*
27059dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
27159dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
27259dfa54cSAmit Daniel Kachhap  */
2736c247393SAbhilash Kesavan static int code_to_temp(struct exynos_tmu_data *data, u16 temp_code)
27459dfa54cSAmit Daniel Kachhap {
27559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
27659dfa54cSAmit Daniel Kachhap 	int temp;
27759dfa54cSAmit Daniel Kachhap 
27859dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
27959dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
280bb34b4c8SAmit Daniel Kachhap 		temp = (temp_code - data->temp_error1) *
281bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) /
282bb34b4c8SAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) +
283bb34b4c8SAmit Daniel Kachhap 			pdata->first_point_trim;
28459dfa54cSAmit Daniel Kachhap 		break;
28559dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
286bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
28759dfa54cSAmit Daniel Kachhap 		break;
28859dfa54cSAmit Daniel Kachhap 	default:
289bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - pdata->default_temp_offset;
29059dfa54cSAmit Daniel Kachhap 		break;
29159dfa54cSAmit Daniel Kachhap 	}
292ddb31d43SBartlomiej Zolnierkiewicz 
29359dfa54cSAmit Daniel Kachhap 	return temp;
29459dfa54cSAmit Daniel Kachhap }
29559dfa54cSAmit Daniel Kachhap 
2968328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
297b835ced1SBartlomiej Zolnierkiewicz {
29859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
29959dfa54cSAmit Daniel Kachhap 
300b8d582b9SAmit Daniel Kachhap 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
30199d67fb9SBartlomiej Zolnierkiewicz 	data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
302b8d582b9SAmit Daniel Kachhap 				EXYNOS_TMU_TEMP_MASK);
30359dfa54cSAmit Daniel Kachhap 
3045000806cSAmit Daniel Kachhap 	if (!data->temp_error1 ||
3055000806cSAmit Daniel Kachhap 		(pdata->min_efuse_value > data->temp_error1) ||
3065000806cSAmit Daniel Kachhap 		(data->temp_error1 > pdata->max_efuse_value))
3075000806cSAmit Daniel Kachhap 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
3085000806cSAmit Daniel Kachhap 
3095000806cSAmit Daniel Kachhap 	if (!data->temp_error2)
3105000806cSAmit Daniel Kachhap 		data->temp_error2 =
31199d67fb9SBartlomiej Zolnierkiewicz 			(pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
3125000806cSAmit Daniel Kachhap 			EXYNOS_TMU_TEMP_MASK;
3138328a4b1SBartlomiej Zolnierkiewicz }
31459dfa54cSAmit Daniel Kachhap 
315fe87789cSBartlomiej Zolnierkiewicz static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling)
316fe87789cSBartlomiej Zolnierkiewicz {
3173b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
3183b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
3193b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(tz);
3203b6a1a80SLukasz Majewski 	unsigned long temp;
321fe87789cSBartlomiej Zolnierkiewicz 	int i;
322c65d3473STushar Behera 
3233b6a1a80SLukasz Majewski 	if (!trips) {
3243b6a1a80SLukasz Majewski 		pr_err("%s: Cannot get trip points from of-thermal.c!\n",
3253b6a1a80SLukasz Majewski 		       __func__);
3263b6a1a80SLukasz Majewski 		return 0;
3273b6a1a80SLukasz Majewski 	}
328fe87789cSBartlomiej Zolnierkiewicz 
3293b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
3303b6a1a80SLukasz Majewski 		if (trips[i].type == THERMAL_TRIP_CRITICAL)
3313b6a1a80SLukasz Majewski 			continue;
3323b6a1a80SLukasz Majewski 
3333b6a1a80SLukasz Majewski 		temp = trips[i].temperature / MCELSIUS;
334fe87789cSBartlomiej Zolnierkiewicz 		if (falling)
3353b6a1a80SLukasz Majewski 			temp -= (trips[i].hysteresis / MCELSIUS);
336fe87789cSBartlomiej Zolnierkiewicz 		else
337fe87789cSBartlomiej Zolnierkiewicz 			threshold &= ~(0xff << 8 * i);
338fe87789cSBartlomiej Zolnierkiewicz 
339fe87789cSBartlomiej Zolnierkiewicz 		threshold |= temp_to_code(data, temp) << 8 * i;
34059dfa54cSAmit Daniel Kachhap 	}
34159dfa54cSAmit Daniel Kachhap 
342fe87789cSBartlomiej Zolnierkiewicz 	return threshold;
343fe87789cSBartlomiej Zolnierkiewicz }
34459dfa54cSAmit Daniel Kachhap 
34559dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
34659dfa54cSAmit Daniel Kachhap {
34759dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
34872d1100bSBartlomiej Zolnierkiewicz 	int ret;
3497ca04e58SAmit Daniel Kachhap 
35059dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
35159dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
35259dfa54cSAmit Daniel Kachhap 	if (!IS_ERR(data->clk_sec))
35359dfa54cSAmit Daniel Kachhap 		clk_enable(data->clk_sec);
35472d1100bSBartlomiej Zolnierkiewicz 	ret = data->tmu_initialize(pdev);
35559dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
35659dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
35714a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
35814a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
35959dfa54cSAmit Daniel Kachhap 
36059dfa54cSAmit Daniel Kachhap 	return ret;
36159dfa54cSAmit Daniel Kachhap }
36259dfa54cSAmit Daniel Kachhap 
363d00671c3SBartlomiej Zolnierkiewicz static u32 get_con_reg(struct exynos_tmu_data *data, u32 con)
36459dfa54cSAmit Daniel Kachhap {
36559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
36659dfa54cSAmit Daniel Kachhap 
3677575983cSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS4412 ||
3687575983cSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS3250)
3697575983cSBartlomiej Zolnierkiewicz 		con |= (EXYNOS4412_MUX_ADDR_VALUE << EXYNOS4412_MUX_ADDR_SHIFT);
37086f5362eSLukasz Majewski 
37199d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
37299d67fb9SBartlomiej Zolnierkiewicz 	con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
373d0a0ce3eSAmit Daniel Kachhap 
37499d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
37599d67fb9SBartlomiej Zolnierkiewicz 	con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
376d0a0ce3eSAmit Daniel Kachhap 
377d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
378b9504a6aSBartlomiej Zolnierkiewicz 		con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
379b9504a6aSBartlomiej Zolnierkiewicz 		con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT);
38059dfa54cSAmit Daniel Kachhap 	}
38159dfa54cSAmit Daniel Kachhap 
382d00671c3SBartlomiej Zolnierkiewicz 	return con;
383d00671c3SBartlomiej Zolnierkiewicz }
384d00671c3SBartlomiej Zolnierkiewicz 
385d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on)
386d00671c3SBartlomiej Zolnierkiewicz {
387d00671c3SBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
388d00671c3SBartlomiej Zolnierkiewicz 
389d00671c3SBartlomiej Zolnierkiewicz 	mutex_lock(&data->lock);
390d00671c3SBartlomiej Zolnierkiewicz 	clk_enable(data->clk);
39137f9034fSBartlomiej Zolnierkiewicz 	data->tmu_control(pdev, on);
39259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
39359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
39459dfa54cSAmit Daniel Kachhap }
39559dfa54cSAmit Daniel Kachhap 
39672d1100bSBartlomiej Zolnierkiewicz static int exynos4210_tmu_initialize(struct platform_device *pdev)
39772d1100bSBartlomiej Zolnierkiewicz {
39872d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
3993b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
4003b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
4013b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(tz);
40272d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
4033b6a1a80SLukasz Majewski 	unsigned long reference, temp;
4043b6a1a80SLukasz Majewski 	unsigned int status;
4053b6a1a80SLukasz Majewski 
4063b6a1a80SLukasz Majewski 	if (!trips) {
4073b6a1a80SLukasz Majewski 		pr_err("%s: Cannot get trip points from of-thermal.c!\n",
4083b6a1a80SLukasz Majewski 		       __func__);
4093b6a1a80SLukasz Majewski 		ret = -ENODEV;
4103b6a1a80SLukasz Majewski 		goto out;
4113b6a1a80SLukasz Majewski 	}
41272d1100bSBartlomiej Zolnierkiewicz 
41372d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
41472d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
41572d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
41672d1100bSBartlomiej Zolnierkiewicz 		goto out;
41772d1100bSBartlomiej Zolnierkiewicz 	}
41872d1100bSBartlomiej Zolnierkiewicz 
41972d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO));
42072d1100bSBartlomiej Zolnierkiewicz 
42172d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for threshold */
4223b6a1a80SLukasz Majewski 	reference = trips[0].temperature / MCELSIUS;
4233b6a1a80SLukasz Majewski 	threshold_code = temp_to_code(data, reference);
4243b6a1a80SLukasz Majewski 	if (threshold_code < 0) {
4253b6a1a80SLukasz Majewski 		ret = threshold_code;
4263b6a1a80SLukasz Majewski 		goto out;
4273b6a1a80SLukasz Majewski 	}
42872d1100bSBartlomiej Zolnierkiewicz 	writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
42972d1100bSBartlomiej Zolnierkiewicz 
4303b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
4313b6a1a80SLukasz Majewski 		temp = trips[i].temperature / MCELSIUS;
4323b6a1a80SLukasz Majewski 		writeb(temp - reference, data->base +
43372d1100bSBartlomiej Zolnierkiewicz 		       EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
4343b6a1a80SLukasz Majewski 	}
43572d1100bSBartlomiej Zolnierkiewicz 
436a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
43772d1100bSBartlomiej Zolnierkiewicz out:
43872d1100bSBartlomiej Zolnierkiewicz 	return ret;
43972d1100bSBartlomiej Zolnierkiewicz }
44072d1100bSBartlomiej Zolnierkiewicz 
44172d1100bSBartlomiej Zolnierkiewicz static int exynos4412_tmu_initialize(struct platform_device *pdev)
44272d1100bSBartlomiej Zolnierkiewicz {
44372d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
4443b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
4453b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(data->tzd);
44672d1100bSBartlomiej Zolnierkiewicz 	unsigned int status, trim_info, con, ctrl, rising_threshold;
44772d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
4483b6a1a80SLukasz Majewski 	unsigned long crit_temp = 0;
44972d1100bSBartlomiej Zolnierkiewicz 
45072d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
45172d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
45272d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
45372d1100bSBartlomiej Zolnierkiewicz 		goto out;
45472d1100bSBartlomiej Zolnierkiewicz 	}
45572d1100bSBartlomiej Zolnierkiewicz 
45672d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS3250 ||
45772d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS4412 ||
45872d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS5250) {
45972d1100bSBartlomiej Zolnierkiewicz 		if (data->soc == SOC_ARCH_EXYNOS3250) {
46072d1100bSBartlomiej Zolnierkiewicz 			ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
46172d1100bSBartlomiej Zolnierkiewicz 			ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
46272d1100bSBartlomiej Zolnierkiewicz 			writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
46372d1100bSBartlomiej Zolnierkiewicz 		}
46472d1100bSBartlomiej Zolnierkiewicz 		ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
46572d1100bSBartlomiej Zolnierkiewicz 		ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
46672d1100bSBartlomiej Zolnierkiewicz 		writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
46772d1100bSBartlomiej Zolnierkiewicz 	}
46872d1100bSBartlomiej Zolnierkiewicz 
46972d1100bSBartlomiej Zolnierkiewicz 	/* On exynos5420 the triminfo register is in the shared space */
47072d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
47172d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO);
47272d1100bSBartlomiej Zolnierkiewicz 	else
47372d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
47472d1100bSBartlomiej Zolnierkiewicz 
47572d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
47672d1100bSBartlomiej Zolnierkiewicz 
47772d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
47872d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE);
47972d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
48072d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
48172d1100bSBartlomiej Zolnierkiewicz 	writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL);
48272d1100bSBartlomiej Zolnierkiewicz 
483a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
48472d1100bSBartlomiej Zolnierkiewicz 
48572d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
4863b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(data->tzd); i++) {
4873b6a1a80SLukasz Majewski 		if (trips[i].type == THERMAL_TRIP_CRITICAL) {
4883b6a1a80SLukasz Majewski 			crit_temp = trips[i].temperature;
4893b6a1a80SLukasz Majewski 			break;
4903b6a1a80SLukasz Majewski 		}
4913b6a1a80SLukasz Majewski 	}
4923b6a1a80SLukasz Majewski 
4933b6a1a80SLukasz Majewski 	if (i == of_thermal_get_ntrips(data->tzd)) {
4943b6a1a80SLukasz Majewski 		pr_err("%s: No CRITICAL trip point defined at of-thermal.c!\n",
4953b6a1a80SLukasz Majewski 		       __func__);
4963b6a1a80SLukasz Majewski 		ret = -EINVAL;
4973b6a1a80SLukasz Majewski 		goto out;
4983b6a1a80SLukasz Majewski 	}
4993b6a1a80SLukasz Majewski 
5003b6a1a80SLukasz Majewski 	threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
50172d1100bSBartlomiej Zolnierkiewicz 	/* 1-4 level to be assigned in th0 reg */
50272d1100bSBartlomiej Zolnierkiewicz 	rising_threshold &= ~(0xff << 8 * i);
50372d1100bSBartlomiej Zolnierkiewicz 	rising_threshold |= threshold_code << 8 * i;
50472d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
50572d1100bSBartlomiej Zolnierkiewicz 	con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
50672d1100bSBartlomiej Zolnierkiewicz 	con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
50772d1100bSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
5083b6a1a80SLukasz Majewski 
50972d1100bSBartlomiej Zolnierkiewicz out:
51072d1100bSBartlomiej Zolnierkiewicz 	return ret;
51172d1100bSBartlomiej Zolnierkiewicz }
51272d1100bSBartlomiej Zolnierkiewicz 
513488c7455SChanwoo Choi static int exynos5433_tmu_initialize(struct platform_device *pdev)
514488c7455SChanwoo Choi {
515488c7455SChanwoo Choi 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
516488c7455SChanwoo Choi 	struct exynos_tmu_platform_data *pdata = data->pdata;
517488c7455SChanwoo Choi 	struct thermal_zone_device *tz = data->tzd;
518488c7455SChanwoo Choi 	unsigned int status, trim_info;
519488c7455SChanwoo Choi 	unsigned int rising_threshold = 0, falling_threshold = 0;
520488c7455SChanwoo Choi 	unsigned long temp, temp_hist;
521488c7455SChanwoo Choi 	int ret = 0, threshold_code, i, sensor_id, cal_type;
522488c7455SChanwoo Choi 
523488c7455SChanwoo Choi 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
524488c7455SChanwoo Choi 	if (!status) {
525488c7455SChanwoo Choi 		ret = -EBUSY;
526488c7455SChanwoo Choi 		goto out;
527488c7455SChanwoo Choi 	}
528488c7455SChanwoo Choi 
529488c7455SChanwoo Choi 	trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
530488c7455SChanwoo Choi 	sanitize_temp_error(data, trim_info);
531488c7455SChanwoo Choi 
532488c7455SChanwoo Choi 	/* Read the temperature sensor id */
533488c7455SChanwoo Choi 	sensor_id = (trim_info & EXYNOS5433_TRIMINFO_SENSOR_ID_MASK)
534488c7455SChanwoo Choi 				>> EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT;
535488c7455SChanwoo Choi 	dev_info(&pdev->dev, "Temperature sensor ID: 0x%x\n", sensor_id);
536488c7455SChanwoo Choi 
537488c7455SChanwoo Choi 	/* Read the calibration mode */
538488c7455SChanwoo Choi 	writel(trim_info, data->base + EXYNOS_TMU_REG_TRIMINFO);
539488c7455SChanwoo Choi 	cal_type = (trim_info & EXYNOS5433_TRIMINFO_CALIB_SEL_MASK)
540488c7455SChanwoo Choi 				>> EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT;
541488c7455SChanwoo Choi 
542488c7455SChanwoo Choi 	switch (cal_type) {
543488c7455SChanwoo Choi 	case EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING:
544488c7455SChanwoo Choi 		pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
545488c7455SChanwoo Choi 		break;
546488c7455SChanwoo Choi 	case EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING:
547488c7455SChanwoo Choi 		pdata->cal_type = TYPE_TWO_POINT_TRIMMING;
548488c7455SChanwoo Choi 		break;
549488c7455SChanwoo Choi 	default:
550488c7455SChanwoo Choi 		pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
551488c7455SChanwoo Choi 		break;
552488c7455SChanwoo Choi 	};
553488c7455SChanwoo Choi 
554488c7455SChanwoo Choi 	dev_info(&pdev->dev, "Calibration type is %d-point calibration\n",
555488c7455SChanwoo Choi 			cal_type ?  2 : 1);
556488c7455SChanwoo Choi 
557488c7455SChanwoo Choi 	/* Write temperature code for rising and falling threshold */
558488c7455SChanwoo Choi 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
559488c7455SChanwoo Choi 		int rising_reg_offset, falling_reg_offset;
560488c7455SChanwoo Choi 		int j = 0;
561488c7455SChanwoo Choi 
562488c7455SChanwoo Choi 		switch (i) {
563488c7455SChanwoo Choi 		case 0:
564488c7455SChanwoo Choi 		case 1:
565488c7455SChanwoo Choi 		case 2:
566488c7455SChanwoo Choi 		case 3:
567488c7455SChanwoo Choi 			rising_reg_offset = EXYNOS5433_THD_TEMP_RISE3_0;
568488c7455SChanwoo Choi 			falling_reg_offset = EXYNOS5433_THD_TEMP_FALL3_0;
569488c7455SChanwoo Choi 			j = i;
570488c7455SChanwoo Choi 			break;
571488c7455SChanwoo Choi 		case 4:
572488c7455SChanwoo Choi 		case 5:
573488c7455SChanwoo Choi 		case 6:
574488c7455SChanwoo Choi 		case 7:
575488c7455SChanwoo Choi 			rising_reg_offset = EXYNOS5433_THD_TEMP_RISE7_4;
576488c7455SChanwoo Choi 			falling_reg_offset = EXYNOS5433_THD_TEMP_FALL7_4;
577488c7455SChanwoo Choi 			j = i - 4;
578488c7455SChanwoo Choi 			break;
579488c7455SChanwoo Choi 		default:
580488c7455SChanwoo Choi 			continue;
581488c7455SChanwoo Choi 		}
582488c7455SChanwoo Choi 
583488c7455SChanwoo Choi 		/* Write temperature code for rising threshold */
584488c7455SChanwoo Choi 		tz->ops->get_trip_temp(tz, i, &temp);
585488c7455SChanwoo Choi 		temp /= MCELSIUS;
586488c7455SChanwoo Choi 		threshold_code = temp_to_code(data, temp);
587488c7455SChanwoo Choi 
588488c7455SChanwoo Choi 		rising_threshold = readl(data->base + rising_reg_offset);
589488c7455SChanwoo Choi 		rising_threshold |= (threshold_code << j * 8);
590488c7455SChanwoo Choi 		writel(rising_threshold, data->base + rising_reg_offset);
591488c7455SChanwoo Choi 
592488c7455SChanwoo Choi 		/* Write temperature code for falling threshold */
593488c7455SChanwoo Choi 		tz->ops->get_trip_hyst(tz, i, &temp_hist);
594488c7455SChanwoo Choi 		temp_hist = temp - (temp_hist / MCELSIUS);
595488c7455SChanwoo Choi 		threshold_code = temp_to_code(data, temp_hist);
596488c7455SChanwoo Choi 
597488c7455SChanwoo Choi 		falling_threshold = readl(data->base + falling_reg_offset);
598488c7455SChanwoo Choi 		falling_threshold &= ~(0xff << j * 8);
599488c7455SChanwoo Choi 		falling_threshold |= (threshold_code << j * 8);
600488c7455SChanwoo Choi 		writel(falling_threshold, data->base + falling_reg_offset);
601488c7455SChanwoo Choi 	}
602488c7455SChanwoo Choi 
603488c7455SChanwoo Choi 	data->tmu_clear_irqs(data);
604488c7455SChanwoo Choi out:
605488c7455SChanwoo Choi 	return ret;
606488c7455SChanwoo Choi }
607488c7455SChanwoo Choi 
60872d1100bSBartlomiej Zolnierkiewicz static int exynos5440_tmu_initialize(struct platform_device *pdev)
60972d1100bSBartlomiej Zolnierkiewicz {
61072d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
61172d1100bSBartlomiej Zolnierkiewicz 	unsigned int trim_info = 0, con, rising_threshold;
6123b6a1a80SLukasz Majewski 	int ret = 0, threshold_code;
6133b6a1a80SLukasz Majewski 	unsigned long crit_temp = 0;
61472d1100bSBartlomiej Zolnierkiewicz 
61572d1100bSBartlomiej Zolnierkiewicz 	/*
61672d1100bSBartlomiej Zolnierkiewicz 	 * For exynos5440 soc triminfo value is swapped between TMU0 and
61772d1100bSBartlomiej Zolnierkiewicz 	 * TMU2, so the below logic is needed.
61872d1100bSBartlomiej Zolnierkiewicz 	 */
61972d1100bSBartlomiej Zolnierkiewicz 	switch (data->id) {
62072d1100bSBartlomiej Zolnierkiewicz 	case 0:
62172d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET +
62272d1100bSBartlomiej Zolnierkiewicz 				 EXYNOS5440_TMU_S0_7_TRIM);
62372d1100bSBartlomiej Zolnierkiewicz 		break;
62472d1100bSBartlomiej Zolnierkiewicz 	case 1:
62572d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM);
62672d1100bSBartlomiej Zolnierkiewicz 		break;
62772d1100bSBartlomiej Zolnierkiewicz 	case 2:
62872d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET +
62972d1100bSBartlomiej Zolnierkiewicz 				  EXYNOS5440_TMU_S0_7_TRIM);
63072d1100bSBartlomiej Zolnierkiewicz 	}
63172d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
63272d1100bSBartlomiej Zolnierkiewicz 
63372d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
63472d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0);
63572d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
63672d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0);
63772d1100bSBartlomiej Zolnierkiewicz 	writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1);
63872d1100bSBartlomiej Zolnierkiewicz 
639a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
64072d1100bSBartlomiej Zolnierkiewicz 
64172d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
6423b6a1a80SLukasz Majewski 	if (!data->tzd->ops->get_crit_temp(data->tzd, &crit_temp)) {
6433b6a1a80SLukasz Majewski 		threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
64472d1100bSBartlomiej Zolnierkiewicz 		/* 5th level to be assigned in th2 reg */
64572d1100bSBartlomiej Zolnierkiewicz 		rising_threshold =
64672d1100bSBartlomiej Zolnierkiewicz 			threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT;
64772d1100bSBartlomiej Zolnierkiewicz 		writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2);
64872d1100bSBartlomiej Zolnierkiewicz 		con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL);
64972d1100bSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
65072d1100bSBartlomiej Zolnierkiewicz 		writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
65172d1100bSBartlomiej Zolnierkiewicz 	}
65272d1100bSBartlomiej Zolnierkiewicz 	/* Clear the PMIN in the common TMU register */
65372d1100bSBartlomiej Zolnierkiewicz 	if (!data->id)
65472d1100bSBartlomiej Zolnierkiewicz 		writel(0, data->base_second + EXYNOS5440_TMU_PMIN);
65572d1100bSBartlomiej Zolnierkiewicz 	return ret;
65672d1100bSBartlomiej Zolnierkiewicz }
65772d1100bSBartlomiej Zolnierkiewicz 
6586c247393SAbhilash Kesavan static int exynos7_tmu_initialize(struct platform_device *pdev)
6596c247393SAbhilash Kesavan {
6606c247393SAbhilash Kesavan 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
6616c247393SAbhilash Kesavan 	struct thermal_zone_device *tz = data->tzd;
6626c247393SAbhilash Kesavan 	struct exynos_tmu_platform_data *pdata = data->pdata;
6636c247393SAbhilash Kesavan 	unsigned int status, trim_info;
6646c247393SAbhilash Kesavan 	unsigned int rising_threshold = 0, falling_threshold = 0;
6656c247393SAbhilash Kesavan 	int ret = 0, threshold_code, i;
6666c247393SAbhilash Kesavan 	unsigned long temp, temp_hist;
6676c247393SAbhilash Kesavan 	unsigned int reg_off, bit_off;
6686c247393SAbhilash Kesavan 
6696c247393SAbhilash Kesavan 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
6706c247393SAbhilash Kesavan 	if (!status) {
6716c247393SAbhilash Kesavan 		ret = -EBUSY;
6726c247393SAbhilash Kesavan 		goto out;
6736c247393SAbhilash Kesavan 	}
6746c247393SAbhilash Kesavan 
6756c247393SAbhilash Kesavan 	trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
6766c247393SAbhilash Kesavan 
6776c247393SAbhilash Kesavan 	data->temp_error1 = trim_info & EXYNOS7_TMU_TEMP_MASK;
6786c247393SAbhilash Kesavan 	if (!data->temp_error1 ||
6796c247393SAbhilash Kesavan 	    (pdata->min_efuse_value > data->temp_error1) ||
6806c247393SAbhilash Kesavan 	    (data->temp_error1 > pdata->max_efuse_value))
6816c247393SAbhilash Kesavan 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
6826c247393SAbhilash Kesavan 
6836c247393SAbhilash Kesavan 	/* Write temperature code for rising and falling threshold */
6846c247393SAbhilash Kesavan 	for (i = (of_thermal_get_ntrips(tz) - 1); i >= 0; i--) {
6856c247393SAbhilash Kesavan 		/*
6866c247393SAbhilash Kesavan 		 * On exynos7 there are 4 rising and 4 falling threshold
6876c247393SAbhilash Kesavan 		 * registers (0x50-0x5c and 0x60-0x6c respectively). Each
6886c247393SAbhilash Kesavan 		 * register holds the value of two threshold levels (at bit
6896c247393SAbhilash Kesavan 		 * offsets 0 and 16). Based on the fact that there are atmost
6906c247393SAbhilash Kesavan 		 * eight possible trigger levels, calculate the register and
6916c247393SAbhilash Kesavan 		 * bit offsets where the threshold levels are to be written.
6926c247393SAbhilash Kesavan 		 *
6936c247393SAbhilash Kesavan 		 * e.g. EXYNOS7_THD_TEMP_RISE7_6 (0x50)
6946c247393SAbhilash Kesavan 		 * [24:16] - Threshold level 7
6956c247393SAbhilash Kesavan 		 * [8:0] - Threshold level 6
6966c247393SAbhilash Kesavan 		 * e.g. EXYNOS7_THD_TEMP_RISE5_4 (0x54)
6976c247393SAbhilash Kesavan 		 * [24:16] - Threshold level 5
6986c247393SAbhilash Kesavan 		 * [8:0] - Threshold level 4
6996c247393SAbhilash Kesavan 		 *
7006c247393SAbhilash Kesavan 		 * and similarly for falling thresholds.
7016c247393SAbhilash Kesavan 		 *
7026c247393SAbhilash Kesavan 		 * Based on the above, calculate the register and bit offsets
7036c247393SAbhilash Kesavan 		 * for rising/falling threshold levels and populate them.
7046c247393SAbhilash Kesavan 		 */
7056c247393SAbhilash Kesavan 		reg_off = ((7 - i) / 2) * 4;
7066c247393SAbhilash Kesavan 		bit_off = ((8 - i) % 2);
7076c247393SAbhilash Kesavan 
7086c247393SAbhilash Kesavan 		tz->ops->get_trip_temp(tz, i, &temp);
7096c247393SAbhilash Kesavan 		temp /= MCELSIUS;
7106c247393SAbhilash Kesavan 
7116c247393SAbhilash Kesavan 		tz->ops->get_trip_hyst(tz, i, &temp_hist);
7126c247393SAbhilash Kesavan 		temp_hist = temp - (temp_hist / MCELSIUS);
7136c247393SAbhilash Kesavan 
7146c247393SAbhilash Kesavan 		/* Set 9-bit temperature code for rising threshold levels */
7156c247393SAbhilash Kesavan 		threshold_code = temp_to_code(data, temp);
7166c247393SAbhilash Kesavan 		rising_threshold = readl(data->base +
7176c247393SAbhilash Kesavan 			EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
7186c247393SAbhilash Kesavan 		rising_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
7196c247393SAbhilash Kesavan 		rising_threshold |= threshold_code << (16 * bit_off);
7206c247393SAbhilash Kesavan 		writel(rising_threshold,
7216c247393SAbhilash Kesavan 		       data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
7226c247393SAbhilash Kesavan 
7236c247393SAbhilash Kesavan 		/* Set 9-bit temperature code for falling threshold levels */
7246c247393SAbhilash Kesavan 		threshold_code = temp_to_code(data, temp_hist);
7256c247393SAbhilash Kesavan 		falling_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
7266c247393SAbhilash Kesavan 		falling_threshold |= threshold_code << (16 * bit_off);
7276c247393SAbhilash Kesavan 		writel(falling_threshold,
7286c247393SAbhilash Kesavan 		       data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off);
7296c247393SAbhilash Kesavan 	}
7306c247393SAbhilash Kesavan 
7316c247393SAbhilash Kesavan 	data->tmu_clear_irqs(data);
7326c247393SAbhilash Kesavan out:
7336c247393SAbhilash Kesavan 	return ret;
7346c247393SAbhilash Kesavan }
7356c247393SAbhilash Kesavan 
73637f9034fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_control(struct platform_device *pdev, bool on)
73737f9034fSBartlomiej Zolnierkiewicz {
73837f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
7393b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
74037f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
74137f9034fSBartlomiej Zolnierkiewicz 
74237f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
74337f9034fSBartlomiej Zolnierkiewicz 
74459dfa54cSAmit Daniel Kachhap 	if (on) {
74559dfa54cSAmit Daniel Kachhap 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
74659dfa54cSAmit Daniel Kachhap 		interrupt_en =
7473b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 3)
7483b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE3_SHIFT) |
7493b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 2)
7503b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE2_SHIFT) |
7513b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 1)
7523b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE1_SHIFT) |
7533b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 0)
7543b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE0_SHIFT);
7553b6a1a80SLukasz Majewski 
756e0761533SBartlomiej Zolnierkiewicz 		if (data->soc != SOC_ARCH_EXYNOS4210)
75759dfa54cSAmit Daniel Kachhap 			interrupt_en |=
75837f9034fSBartlomiej Zolnierkiewicz 				interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
75959dfa54cSAmit Daniel Kachhap 	} else {
76059dfa54cSAmit Daniel Kachhap 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
76159dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
76259dfa54cSAmit Daniel Kachhap 	}
76337f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
76437f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
76537f9034fSBartlomiej Zolnierkiewicz }
76659dfa54cSAmit Daniel Kachhap 
767488c7455SChanwoo Choi static void exynos5433_tmu_control(struct platform_device *pdev, bool on)
768488c7455SChanwoo Choi {
769488c7455SChanwoo Choi 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
770488c7455SChanwoo Choi 	struct thermal_zone_device *tz = data->tzd;
771488c7455SChanwoo Choi 	unsigned int con, interrupt_en, pd_det_en;
772488c7455SChanwoo Choi 
773488c7455SChanwoo Choi 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
774488c7455SChanwoo Choi 
775488c7455SChanwoo Choi 	if (on) {
776488c7455SChanwoo Choi 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
777488c7455SChanwoo Choi 		interrupt_en =
778488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 7)
779488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
780488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 6)
781488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
782488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 5)
783488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
784488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 4)
785488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
786488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 3)
787488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
788488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 2)
789488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
790488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 1)
791488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
792488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 0)
793488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE0_SHIFT);
794488c7455SChanwoo Choi 
795488c7455SChanwoo Choi 		interrupt_en |=
796488c7455SChanwoo Choi 			interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
797488c7455SChanwoo Choi 	} else {
798488c7455SChanwoo Choi 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
799488c7455SChanwoo Choi 		interrupt_en = 0; /* Disable all interrupts */
800488c7455SChanwoo Choi 	}
801488c7455SChanwoo Choi 
802488c7455SChanwoo Choi 	pd_det_en = on ? EXYNOS5433_PD_DET_EN : 0;
803488c7455SChanwoo Choi 
804488c7455SChanwoo Choi 	writel(pd_det_en, data->base + EXYNOS5433_TMU_PD_DET_EN);
805488c7455SChanwoo Choi 	writel(interrupt_en, data->base + EXYNOS5433_TMU_REG_INTEN);
806488c7455SChanwoo Choi 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
807488c7455SChanwoo Choi }
808488c7455SChanwoo Choi 
80937f9034fSBartlomiej Zolnierkiewicz static void exynos5440_tmu_control(struct platform_device *pdev, bool on)
81037f9034fSBartlomiej Zolnierkiewicz {
81137f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
8123b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
81337f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
81437f9034fSBartlomiej Zolnierkiewicz 
81537f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL));
81637f9034fSBartlomiej Zolnierkiewicz 
81737f9034fSBartlomiej Zolnierkiewicz 	if (on) {
81837f9034fSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
81937f9034fSBartlomiej Zolnierkiewicz 		interrupt_en =
8203b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 3)
8213b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE3_SHIFT) |
8223b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 2)
8233b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE2_SHIFT) |
8243b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 1)
8253b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE1_SHIFT) |
8263b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 0)
8273b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE0_SHIFT);
8283b6a1a80SLukasz Majewski 		interrupt_en |=
8293b6a1a80SLukasz Majewski 			interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT;
83037f9034fSBartlomiej Zolnierkiewicz 	} else {
83137f9034fSBartlomiej Zolnierkiewicz 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
83237f9034fSBartlomiej Zolnierkiewicz 		interrupt_en = 0; /* Disable all interrupts */
83337f9034fSBartlomiej Zolnierkiewicz 	}
83437f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN);
83537f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
83659dfa54cSAmit Daniel Kachhap }
83759dfa54cSAmit Daniel Kachhap 
8386c247393SAbhilash Kesavan static void exynos7_tmu_control(struct platform_device *pdev, bool on)
8396c247393SAbhilash Kesavan {
8406c247393SAbhilash Kesavan 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
8416c247393SAbhilash Kesavan 	struct thermal_zone_device *tz = data->tzd;
8426c247393SAbhilash Kesavan 	unsigned int con, interrupt_en;
8436c247393SAbhilash Kesavan 
8446c247393SAbhilash Kesavan 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
8456c247393SAbhilash Kesavan 
8466c247393SAbhilash Kesavan 	if (on) {
8476c247393SAbhilash Kesavan 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
84842b696e8SChanwoo Choi 		con |= (1 << EXYNOS7_PD_DET_EN_SHIFT);
8496c247393SAbhilash Kesavan 		interrupt_en =
8506c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 7)
8516c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
8526c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 6)
8536c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
8546c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 5)
8556c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
8566c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 4)
8576c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
8586c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 3)
8596c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
8606c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 2)
8616c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
8626c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 1)
8636c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
8646c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 0)
8656c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE0_SHIFT);
8666c247393SAbhilash Kesavan 
8676c247393SAbhilash Kesavan 		interrupt_en |=
8686c247393SAbhilash Kesavan 			interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
8696c247393SAbhilash Kesavan 	} else {
8706c247393SAbhilash Kesavan 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
87142b696e8SChanwoo Choi 		con &= ~(1 << EXYNOS7_PD_DET_EN_SHIFT);
8726c247393SAbhilash Kesavan 		interrupt_en = 0; /* Disable all interrupts */
8736c247393SAbhilash Kesavan 	}
8746c247393SAbhilash Kesavan 
8756c247393SAbhilash Kesavan 	writel(interrupt_en, data->base + EXYNOS7_TMU_REG_INTEN);
8766c247393SAbhilash Kesavan 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
8776c247393SAbhilash Kesavan }
8786c247393SAbhilash Kesavan 
8793b6a1a80SLukasz Majewski static int exynos_get_temp(void *p, long *temp)
88059dfa54cSAmit Daniel Kachhap {
8813b6a1a80SLukasz Majewski 	struct exynos_tmu_data *data = p;
8823b6a1a80SLukasz Majewski 
8834531fa16SLukasz Majewski 	if (!data || !data->tmu_read)
8843b6a1a80SLukasz Majewski 		return -EINVAL;
88559dfa54cSAmit Daniel Kachhap 
88659dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
88759dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
8883b6a1a80SLukasz Majewski 
8893b6a1a80SLukasz Majewski 	*temp = code_to_temp(data, data->tmu_read(data)) * MCELSIUS;
8903b6a1a80SLukasz Majewski 
89159dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
89259dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
89359dfa54cSAmit Daniel Kachhap 
8943b6a1a80SLukasz Majewski 	return 0;
89559dfa54cSAmit Daniel Kachhap }
89659dfa54cSAmit Daniel Kachhap 
89759dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
898154013eaSBartlomiej Zolnierkiewicz static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val,
899154013eaSBartlomiej Zolnierkiewicz 			    unsigned long temp)
900154013eaSBartlomiej Zolnierkiewicz {
901154013eaSBartlomiej Zolnierkiewicz 	if (temp) {
902154013eaSBartlomiej Zolnierkiewicz 		temp /= MCELSIUS;
903154013eaSBartlomiej Zolnierkiewicz 
904d564b55aSBartlomiej Zolnierkiewicz 		if (data->soc != SOC_ARCH_EXYNOS5440) {
905154013eaSBartlomiej Zolnierkiewicz 			val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
906154013eaSBartlomiej Zolnierkiewicz 			val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
907154013eaSBartlomiej Zolnierkiewicz 		}
9086c247393SAbhilash Kesavan 		if (data->soc == SOC_ARCH_EXYNOS7) {
9096c247393SAbhilash Kesavan 			val &= ~(EXYNOS7_EMUL_DATA_MASK <<
9106c247393SAbhilash Kesavan 				EXYNOS7_EMUL_DATA_SHIFT);
9116c247393SAbhilash Kesavan 			val |= (temp_to_code(data, temp) <<
9126c247393SAbhilash Kesavan 				EXYNOS7_EMUL_DATA_SHIFT) |
913154013eaSBartlomiej Zolnierkiewicz 				EXYNOS_EMUL_ENABLE;
914154013eaSBartlomiej Zolnierkiewicz 		} else {
9156c247393SAbhilash Kesavan 			val &= ~(EXYNOS_EMUL_DATA_MASK <<
9166c247393SAbhilash Kesavan 				EXYNOS_EMUL_DATA_SHIFT);
9176c247393SAbhilash Kesavan 			val |= (temp_to_code(data, temp) <<
9186c247393SAbhilash Kesavan 				EXYNOS_EMUL_DATA_SHIFT) |
9196c247393SAbhilash Kesavan 				EXYNOS_EMUL_ENABLE;
9206c247393SAbhilash Kesavan 		}
9216c247393SAbhilash Kesavan 	} else {
922154013eaSBartlomiej Zolnierkiewicz 		val &= ~EXYNOS_EMUL_ENABLE;
923154013eaSBartlomiej Zolnierkiewicz 	}
924154013eaSBartlomiej Zolnierkiewicz 
925154013eaSBartlomiej Zolnierkiewicz 	return val;
926154013eaSBartlomiej Zolnierkiewicz }
927154013eaSBartlomiej Zolnierkiewicz 
928285d994aSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data,
929285d994aSBartlomiej Zolnierkiewicz 					 unsigned long temp)
930285d994aSBartlomiej Zolnierkiewicz {
931285d994aSBartlomiej Zolnierkiewicz 	unsigned int val;
932285d994aSBartlomiej Zolnierkiewicz 	u32 emul_con;
933285d994aSBartlomiej Zolnierkiewicz 
934285d994aSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5260)
935285d994aSBartlomiej Zolnierkiewicz 		emul_con = EXYNOS5260_EMUL_CON;
936488c7455SChanwoo Choi 	if (data->soc == SOC_ARCH_EXYNOS5433)
937488c7455SChanwoo Choi 		emul_con = EXYNOS5433_TMU_EMUL_CON;
9386c247393SAbhilash Kesavan 	else if (data->soc == SOC_ARCH_EXYNOS7)
9396c247393SAbhilash Kesavan 		emul_con = EXYNOS7_TMU_REG_EMUL_CON;
940285d994aSBartlomiej Zolnierkiewicz 	else
941285d994aSBartlomiej Zolnierkiewicz 		emul_con = EXYNOS_EMUL_CON;
942285d994aSBartlomiej Zolnierkiewicz 
943285d994aSBartlomiej Zolnierkiewicz 	val = readl(data->base + emul_con);
944285d994aSBartlomiej Zolnierkiewicz 	val = get_emul_con_reg(data, val, temp);
945285d994aSBartlomiej Zolnierkiewicz 	writel(val, data->base + emul_con);
946285d994aSBartlomiej Zolnierkiewicz }
947285d994aSBartlomiej Zolnierkiewicz 
948285d994aSBartlomiej Zolnierkiewicz static void exynos5440_tmu_set_emulation(struct exynos_tmu_data *data,
949285d994aSBartlomiej Zolnierkiewicz 					 unsigned long temp)
950285d994aSBartlomiej Zolnierkiewicz {
951285d994aSBartlomiej Zolnierkiewicz 	unsigned int val;
952285d994aSBartlomiej Zolnierkiewicz 
953285d994aSBartlomiej Zolnierkiewicz 	val = readl(data->base + EXYNOS5440_TMU_S0_7_DEBUG);
954285d994aSBartlomiej Zolnierkiewicz 	val = get_emul_con_reg(data, val, temp);
955285d994aSBartlomiej Zolnierkiewicz 	writel(val, data->base + EXYNOS5440_TMU_S0_7_DEBUG);
956285d994aSBartlomiej Zolnierkiewicz }
957285d994aSBartlomiej Zolnierkiewicz 
95859dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
95959dfa54cSAmit Daniel Kachhap {
96059dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
96159dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
96259dfa54cSAmit Daniel Kachhap 
963ef3f80fcSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS4210)
96459dfa54cSAmit Daniel Kachhap 		goto out;
96559dfa54cSAmit Daniel Kachhap 
96659dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
96759dfa54cSAmit Daniel Kachhap 		goto out;
96859dfa54cSAmit Daniel Kachhap 
96959dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
97059dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
971285d994aSBartlomiej Zolnierkiewicz 	data->tmu_set_emulation(data, temp);
97259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
97359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
97459dfa54cSAmit Daniel Kachhap 	return 0;
97559dfa54cSAmit Daniel Kachhap out:
97659dfa54cSAmit Daniel Kachhap 	return ret;
97759dfa54cSAmit Daniel Kachhap }
97859dfa54cSAmit Daniel Kachhap #else
979285d994aSBartlomiej Zolnierkiewicz #define exynos4412_tmu_set_emulation NULL
980285d994aSBartlomiej Zolnierkiewicz #define exynos5440_tmu_set_emulation NULL
98159dfa54cSAmit Daniel Kachhap static int exynos_tmu_set_emulation(void *drv_data,	unsigned long temp)
98259dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
98359dfa54cSAmit Daniel Kachhap #endif /* CONFIG_THERMAL_EMULATION */
98459dfa54cSAmit Daniel Kachhap 
985b79985caSBartlomiej Zolnierkiewicz static int exynos4210_tmu_read(struct exynos_tmu_data *data)
986b79985caSBartlomiej Zolnierkiewicz {
987b79985caSBartlomiej Zolnierkiewicz 	int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
988b79985caSBartlomiej Zolnierkiewicz 
989b79985caSBartlomiej Zolnierkiewicz 	/* "temp_code" should range between 75 and 175 */
990b79985caSBartlomiej Zolnierkiewicz 	return (ret < 75 || ret > 175) ? -ENODATA : ret;
991b79985caSBartlomiej Zolnierkiewicz }
992b79985caSBartlomiej Zolnierkiewicz 
993b79985caSBartlomiej Zolnierkiewicz static int exynos4412_tmu_read(struct exynos_tmu_data *data)
994b79985caSBartlomiej Zolnierkiewicz {
995b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
996b79985caSBartlomiej Zolnierkiewicz }
997b79985caSBartlomiej Zolnierkiewicz 
998b79985caSBartlomiej Zolnierkiewicz static int exynos5440_tmu_read(struct exynos_tmu_data *data)
999b79985caSBartlomiej Zolnierkiewicz {
1000b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS5440_TMU_S0_7_TEMP);
1001b79985caSBartlomiej Zolnierkiewicz }
1002b79985caSBartlomiej Zolnierkiewicz 
10036c247393SAbhilash Kesavan static int exynos7_tmu_read(struct exynos_tmu_data *data)
10046c247393SAbhilash Kesavan {
10056c247393SAbhilash Kesavan 	return readw(data->base + EXYNOS_TMU_REG_CURRENT_TEMP) &
10066c247393SAbhilash Kesavan 		EXYNOS7_TMU_TEMP_MASK;
10076c247393SAbhilash Kesavan }
10086c247393SAbhilash Kesavan 
100959dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
101059dfa54cSAmit Daniel Kachhap {
101159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
101259dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
1013b835ced1SBartlomiej Zolnierkiewicz 	unsigned int val_type;
1014a0395eeeSAmit Daniel Kachhap 
101514a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
101614a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
1017a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
1018421d5d12SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5440) {
1019421d5d12SBartlomiej Zolnierkiewicz 		val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS);
1020a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
1021a0395eeeSAmit Daniel Kachhap 			goto out;
1022a0395eeeSAmit Daniel Kachhap 	}
102314a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
102414a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
102559dfa54cSAmit Daniel Kachhap 
10263b6a1a80SLukasz Majewski 	exynos_report_trigger(data);
102759dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
102859dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
1029b8d582b9SAmit Daniel Kachhap 
1030a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
1031a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
1032b8d582b9SAmit Daniel Kachhap 
103359dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
103459dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
1035a0395eeeSAmit Daniel Kachhap out:
103659dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
103759dfa54cSAmit Daniel Kachhap }
103859dfa54cSAmit Daniel Kachhap 
1039a7331f72SBartlomiej Zolnierkiewicz static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data)
1040a7331f72SBartlomiej Zolnierkiewicz {
1041a7331f72SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
1042a7331f72SBartlomiej Zolnierkiewicz 	u32 tmu_intstat, tmu_intclear;
1043a7331f72SBartlomiej Zolnierkiewicz 
1044a7331f72SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5260) {
1045a7331f72SBartlomiej Zolnierkiewicz 		tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT;
1046a7331f72SBartlomiej Zolnierkiewicz 		tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR;
10476c247393SAbhilash Kesavan 	} else if (data->soc == SOC_ARCH_EXYNOS7) {
10486c247393SAbhilash Kesavan 		tmu_intstat = EXYNOS7_TMU_REG_INTPEND;
10496c247393SAbhilash Kesavan 		tmu_intclear = EXYNOS7_TMU_REG_INTPEND;
1050488c7455SChanwoo Choi 	} else if (data->soc == SOC_ARCH_EXYNOS5433) {
1051488c7455SChanwoo Choi 		tmu_intstat = EXYNOS5433_TMU_REG_INTPEND;
1052488c7455SChanwoo Choi 		tmu_intclear = EXYNOS5433_TMU_REG_INTPEND;
1053a7331f72SBartlomiej Zolnierkiewicz 	} else {
1054a7331f72SBartlomiej Zolnierkiewicz 		tmu_intstat = EXYNOS_TMU_REG_INTSTAT;
1055a7331f72SBartlomiej Zolnierkiewicz 		tmu_intclear = EXYNOS_TMU_REG_INTCLEAR;
1056a7331f72SBartlomiej Zolnierkiewicz 	}
1057a7331f72SBartlomiej Zolnierkiewicz 
1058a7331f72SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + tmu_intstat);
1059a7331f72SBartlomiej Zolnierkiewicz 	/*
1060a7331f72SBartlomiej Zolnierkiewicz 	 * Clear the interrupts.  Please note that the documentation for
1061a7331f72SBartlomiej Zolnierkiewicz 	 * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
1062a7331f72SBartlomiej Zolnierkiewicz 	 * states that INTCLEAR register has a different placing of bits
1063a7331f72SBartlomiej Zolnierkiewicz 	 * responsible for FALL IRQs than INTSTAT register.  Exynos5420
1064a7331f72SBartlomiej Zolnierkiewicz 	 * and Exynos5440 documentation is correct (Exynos4210 doesn't
1065a7331f72SBartlomiej Zolnierkiewicz 	 * support FALL IRQs at all).
1066a7331f72SBartlomiej Zolnierkiewicz 	 */
1067a7331f72SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + tmu_intclear);
1068a7331f72SBartlomiej Zolnierkiewicz }
1069a7331f72SBartlomiej Zolnierkiewicz 
1070a7331f72SBartlomiej Zolnierkiewicz static void exynos5440_tmu_clear_irqs(struct exynos_tmu_data *data)
1071a7331f72SBartlomiej Zolnierkiewicz {
1072a7331f72SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
1073a7331f72SBartlomiej Zolnierkiewicz 
1074a7331f72SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + EXYNOS5440_TMU_S0_7_IRQ);
1075a7331f72SBartlomiej Zolnierkiewicz 	/* clear the interrupts */
1076a7331f72SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + EXYNOS5440_TMU_S0_7_IRQ);
1077a7331f72SBartlomiej Zolnierkiewicz }
1078a7331f72SBartlomiej Zolnierkiewicz 
107959dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
108059dfa54cSAmit Daniel Kachhap {
108159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
108259dfa54cSAmit Daniel Kachhap 
108359dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
108459dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
108559dfa54cSAmit Daniel Kachhap 
108659dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
108759dfa54cSAmit Daniel Kachhap }
108859dfa54cSAmit Daniel Kachhap 
108959dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
1090b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos3250-tmu", },
1091b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos4210-tmu", },
1092b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos4412-tmu", },
1093b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5250-tmu", },
1094b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5260-tmu", },
1095b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5420-tmu", },
1096b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5420-tmu-ext-triminfo", },
1097488c7455SChanwoo Choi 	{ .compatible = "samsung,exynos5433-tmu", },
1098b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5440-tmu", },
1099b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos7-tmu", },
1100b71d399cSChanwoo Choi 	{ /* sentinel */ },
110159dfa54cSAmit Daniel Kachhap };
110259dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
110359dfa54cSAmit Daniel Kachhap 
11043b6a1a80SLukasz Majewski static int exynos_of_get_soc_type(struct device_node *np)
110559dfa54cSAmit Daniel Kachhap {
11063b6a1a80SLukasz Majewski 	if (of_device_is_compatible(np, "samsung,exynos3250-tmu"))
11073b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS3250;
11083b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos4210-tmu"))
11093b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS4210;
11103b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos4412-tmu"))
11113b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS4412;
11123b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5250-tmu"))
11133b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5250;
11143b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5260-tmu"))
11153b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5260;
11163b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5420-tmu"))
11173b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5420;
11183b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np,
11193b6a1a80SLukasz Majewski 					 "samsung,exynos5420-tmu-ext-triminfo"))
11203b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5420_TRIMINFO;
1121488c7455SChanwoo Choi 	else if (of_device_is_compatible(np, "samsung,exynos5433-tmu"))
1122488c7455SChanwoo Choi 		return SOC_ARCH_EXYNOS5433;
11233b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5440-tmu"))
11243b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5440;
11256c247393SAbhilash Kesavan 	else if (of_device_is_compatible(np, "samsung,exynos7-tmu"))
11266c247393SAbhilash Kesavan 		return SOC_ARCH_EXYNOS7;
112773b5b1d7SSachin Kamat 
11283b6a1a80SLukasz Majewski 	return -EINVAL;
11293b6a1a80SLukasz Majewski }
11303b6a1a80SLukasz Majewski 
11313b6a1a80SLukasz Majewski static int exynos_of_sensor_conf(struct device_node *np,
11323b6a1a80SLukasz Majewski 				 struct exynos_tmu_platform_data *pdata)
11333b6a1a80SLukasz Majewski {
11343b6a1a80SLukasz Majewski 	u32 value;
11353b6a1a80SLukasz Majewski 	int ret;
11363b6a1a80SLukasz Majewski 
11373b6a1a80SLukasz Majewski 	of_node_get(np);
11383b6a1a80SLukasz Majewski 
11393b6a1a80SLukasz Majewski 	ret = of_property_read_u32(np, "samsung,tmu_gain", &value);
11403b6a1a80SLukasz Majewski 	pdata->gain = (u8)value;
11413b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_reference_voltage", &value);
11423b6a1a80SLukasz Majewski 	pdata->reference_voltage = (u8)value;
11433b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_noise_cancel_mode", &value);
11443b6a1a80SLukasz Majewski 	pdata->noise_cancel_mode = (u8)value;
11453b6a1a80SLukasz Majewski 
11463b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_efuse_value",
11473b6a1a80SLukasz Majewski 			     &pdata->efuse_value);
11483b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_min_efuse_value",
11493b6a1a80SLukasz Majewski 			     &pdata->min_efuse_value);
11503b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_max_efuse_value",
11513b6a1a80SLukasz Majewski 			     &pdata->max_efuse_value);
11523b6a1a80SLukasz Majewski 
11533b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_first_point_trim", &value);
11543b6a1a80SLukasz Majewski 	pdata->first_point_trim = (u8)value;
11553b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_second_point_trim", &value);
11563b6a1a80SLukasz Majewski 	pdata->second_point_trim = (u8)value;
11573b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_default_temp_offset", &value);
11583b6a1a80SLukasz Majewski 	pdata->default_temp_offset = (u8)value;
11593b6a1a80SLukasz Majewski 
11603b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_cal_type", &pdata->cal_type);
11613b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_cal_mode", &pdata->cal_mode);
11623b6a1a80SLukasz Majewski 
11633b6a1a80SLukasz Majewski 	of_node_put(np);
11643b6a1a80SLukasz Majewski 	return 0;
116559dfa54cSAmit Daniel Kachhap }
116659dfa54cSAmit Daniel Kachhap 
1167cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
116859dfa54cSAmit Daniel Kachhap {
1169cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
1170cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
1171cebe7373SAmit Daniel Kachhap 	struct resource res;
1172498d22f6SAmit Daniel Kachhap 	int ret;
117359dfa54cSAmit Daniel Kachhap 
117473b5b1d7SSachin Kamat 	if (!data || !pdev->dev.of_node)
1175cebe7373SAmit Daniel Kachhap 		return -ENODEV;
117659dfa54cSAmit Daniel Kachhap 
1177498d22f6SAmit Daniel Kachhap 	/*
1178498d22f6SAmit Daniel Kachhap 	 * Try enabling the regulator if found
1179498d22f6SAmit Daniel Kachhap 	 * TODO: Add regulator as an SOC feature, so that regulator enable
1180498d22f6SAmit Daniel Kachhap 	 * is a compulsory call.
1181498d22f6SAmit Daniel Kachhap 	 */
1182498d22f6SAmit Daniel Kachhap 	data->regulator = devm_regulator_get(&pdev->dev, "vtmu");
1183498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator)) {
1184498d22f6SAmit Daniel Kachhap 		ret = regulator_enable(data->regulator);
1185498d22f6SAmit Daniel Kachhap 		if (ret) {
1186498d22f6SAmit Daniel Kachhap 			dev_err(&pdev->dev, "failed to enable vtmu\n");
1187498d22f6SAmit Daniel Kachhap 			return ret;
1188498d22f6SAmit Daniel Kachhap 		}
1189498d22f6SAmit Daniel Kachhap 	} else {
1190498d22f6SAmit Daniel Kachhap 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
1191498d22f6SAmit Daniel Kachhap 	}
1192498d22f6SAmit Daniel Kachhap 
1193cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
1194cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
1195cebe7373SAmit Daniel Kachhap 		data->id = 0;
1196cebe7373SAmit Daniel Kachhap 
1197cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1198cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
1199cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
1200cebe7373SAmit Daniel Kachhap 		return -ENODEV;
1201cebe7373SAmit Daniel Kachhap 	}
1202cebe7373SAmit Daniel Kachhap 
1203cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
1204cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
1205cebe7373SAmit Daniel Kachhap 		return -ENODEV;
1206cebe7373SAmit Daniel Kachhap 	}
1207cebe7373SAmit Daniel Kachhap 
1208cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
1209cebe7373SAmit Daniel Kachhap 	if (!data->base) {
1210cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
1211cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
1212cebe7373SAmit Daniel Kachhap 	}
1213cebe7373SAmit Daniel Kachhap 
12143b6a1a80SLukasz Majewski 	pdata = devm_kzalloc(&pdev->dev,
12153b6a1a80SLukasz Majewski 			     sizeof(struct exynos_tmu_platform_data),
12163b6a1a80SLukasz Majewski 			     GFP_KERNEL);
12173b6a1a80SLukasz Majewski 	if (!pdata)
12183b6a1a80SLukasz Majewski 		return -ENOMEM;
121956adb9efSBartlomiej Zolnierkiewicz 
12203b6a1a80SLukasz Majewski 	exynos_of_sensor_conf(pdev->dev.of_node, pdata);
1221cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
12223b6a1a80SLukasz Majewski 	data->soc = exynos_of_get_soc_type(pdev->dev.of_node);
122356adb9efSBartlomiej Zolnierkiewicz 
122456adb9efSBartlomiej Zolnierkiewicz 	switch (data->soc) {
122556adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4210:
122656adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4210_tmu_initialize;
122756adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
122856adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4210_tmu_read;
122956adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
123056adb9efSBartlomiej Zolnierkiewicz 		break;
123156adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS3250:
123256adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4412:
123356adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5250:
123456adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5260:
123556adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420:
123656adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420_TRIMINFO:
123756adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4412_tmu_initialize;
123856adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
123956adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4412_tmu_read;
124056adb9efSBartlomiej Zolnierkiewicz 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
124156adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
124256adb9efSBartlomiej Zolnierkiewicz 		break;
1243488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS5433:
1244488c7455SChanwoo Choi 		data->tmu_initialize = exynos5433_tmu_initialize;
1245488c7455SChanwoo Choi 		data->tmu_control = exynos5433_tmu_control;
1246488c7455SChanwoo Choi 		data->tmu_read = exynos4412_tmu_read;
1247488c7455SChanwoo Choi 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
1248488c7455SChanwoo Choi 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
1249488c7455SChanwoo Choi 		break;
125056adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5440:
125156adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos5440_tmu_initialize;
125256adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos5440_tmu_control;
125356adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos5440_tmu_read;
125456adb9efSBartlomiej Zolnierkiewicz 		data->tmu_set_emulation = exynos5440_tmu_set_emulation;
125556adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos5440_tmu_clear_irqs;
125656adb9efSBartlomiej Zolnierkiewicz 		break;
12576c247393SAbhilash Kesavan 	case SOC_ARCH_EXYNOS7:
12586c247393SAbhilash Kesavan 		data->tmu_initialize = exynos7_tmu_initialize;
12596c247393SAbhilash Kesavan 		data->tmu_control = exynos7_tmu_control;
12606c247393SAbhilash Kesavan 		data->tmu_read = exynos7_tmu_read;
12616c247393SAbhilash Kesavan 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
12626c247393SAbhilash Kesavan 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12636c247393SAbhilash Kesavan 		break;
126456adb9efSBartlomiej Zolnierkiewicz 	default:
126556adb9efSBartlomiej Zolnierkiewicz 		dev_err(&pdev->dev, "Platform not supported\n");
126656adb9efSBartlomiej Zolnierkiewicz 		return -EINVAL;
126756adb9efSBartlomiej Zolnierkiewicz 	}
126856adb9efSBartlomiej Zolnierkiewicz 
1269d9b6ee14SAmit Daniel Kachhap 	/*
1270d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
1271d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
1272d9b6ee14SAmit Daniel Kachhap 	 */
127356adb9efSBartlomiej Zolnierkiewicz 	if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO &&
127456adb9efSBartlomiej Zolnierkiewicz 	    data->soc != SOC_ARCH_EXYNOS5440)
1275d9b6ee14SAmit Daniel Kachhap 		return 0;
1276d9b6ee14SAmit Daniel Kachhap 
1277d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
1278d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
1279d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
1280d9b6ee14SAmit Daniel Kachhap 	}
1281d9b6ee14SAmit Daniel Kachhap 
12829025d563SNaveen Krishna Chatradhi 	data->base_second = devm_ioremap(&pdev->dev, res.start,
1283d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
12849025d563SNaveen Krishna Chatradhi 	if (!data->base_second) {
1285d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
1286d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
1287d9b6ee14SAmit Daniel Kachhap 	}
1288cebe7373SAmit Daniel Kachhap 
1289cebe7373SAmit Daniel Kachhap 	return 0;
1290cebe7373SAmit Daniel Kachhap }
1291cebe7373SAmit Daniel Kachhap 
12923b6a1a80SLukasz Majewski static struct thermal_zone_of_device_ops exynos_sensor_ops = {
12933b6a1a80SLukasz Majewski 	.get_temp = exynos_get_temp,
12943b6a1a80SLukasz Majewski 	.set_emul_temp = exynos_tmu_set_emulation,
12953b6a1a80SLukasz Majewski };
12963b6a1a80SLukasz Majewski 
1297cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
1298cebe7373SAmit Daniel Kachhap {
1299cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
13003b6a1a80SLukasz Majewski 	struct exynos_tmu_data *data;
13013b6a1a80SLukasz Majewski 	int ret;
1302cebe7373SAmit Daniel Kachhap 
130359dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
130459dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
13052a9675b3SJingoo Han 	if (!data)
130659dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
130759dfa54cSAmit Daniel Kachhap 
1308cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
1309cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
1310cebe7373SAmit Daniel Kachhap 
13113b6a1a80SLukasz Majewski 	data->tzd = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
13123b6a1a80SLukasz Majewski 						    &exynos_sensor_ops);
13133b6a1a80SLukasz Majewski 	if (IS_ERR(data->tzd)) {
13143b6a1a80SLukasz Majewski 		pr_err("thermal: tz: %p ERROR\n", data->tzd);
13153b6a1a80SLukasz Majewski 		return PTR_ERR(data->tzd);
13163b6a1a80SLukasz Majewski 	}
1317cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
1318cebe7373SAmit Daniel Kachhap 	if (ret)
13193b6a1a80SLukasz Majewski 		goto err_sensor;
1320cebe7373SAmit Daniel Kachhap 
1321cebe7373SAmit Daniel Kachhap 	pdata = data->pdata;
132259dfa54cSAmit Daniel Kachhap 
132359dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
132459dfa54cSAmit Daniel Kachhap 
132559dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
132659dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
132759dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
13283b6a1a80SLukasz Majewski 		ret = PTR_ERR(data->clk);
13293b6a1a80SLukasz Majewski 		goto err_sensor;
133059dfa54cSAmit Daniel Kachhap 	}
133159dfa54cSAmit Daniel Kachhap 
133214a11dc7SNaveen Krishna Chatradhi 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
133314a11dc7SNaveen Krishna Chatradhi 	if (IS_ERR(data->clk_sec)) {
133414a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
133514a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
13363b6a1a80SLukasz Majewski 			ret = PTR_ERR(data->clk_sec);
13373b6a1a80SLukasz Majewski 			goto err_sensor;
133814a11dc7SNaveen Krishna Chatradhi 		}
133914a11dc7SNaveen Krishna Chatradhi 	} else {
134014a11dc7SNaveen Krishna Chatradhi 		ret = clk_prepare(data->clk_sec);
134114a11dc7SNaveen Krishna Chatradhi 		if (ret) {
134214a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get clock\n");
13433b6a1a80SLukasz Majewski 			goto err_sensor;
134414a11dc7SNaveen Krishna Chatradhi 		}
134514a11dc7SNaveen Krishna Chatradhi 	}
134614a11dc7SNaveen Krishna Chatradhi 
134714a11dc7SNaveen Krishna Chatradhi 	ret = clk_prepare(data->clk);
134814a11dc7SNaveen Krishna Chatradhi 	if (ret) {
134914a11dc7SNaveen Krishna Chatradhi 		dev_err(&pdev->dev, "Failed to get clock\n");
135014a11dc7SNaveen Krishna Chatradhi 		goto err_clk_sec;
135114a11dc7SNaveen Krishna Chatradhi 	}
135259dfa54cSAmit Daniel Kachhap 
1353488c7455SChanwoo Choi 	switch (data->soc) {
1354488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS5433:
1355488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS7:
13566c247393SAbhilash Kesavan 		data->sclk = devm_clk_get(&pdev->dev, "tmu_sclk");
13576c247393SAbhilash Kesavan 		if (IS_ERR(data->sclk)) {
13586c247393SAbhilash Kesavan 			dev_err(&pdev->dev, "Failed to get sclk\n");
13596c247393SAbhilash Kesavan 			goto err_clk;
13606c247393SAbhilash Kesavan 		} else {
13616c247393SAbhilash Kesavan 			ret = clk_prepare_enable(data->sclk);
13626c247393SAbhilash Kesavan 			if (ret) {
13636c247393SAbhilash Kesavan 				dev_err(&pdev->dev, "Failed to enable sclk\n");
13646c247393SAbhilash Kesavan 				goto err_clk;
13656c247393SAbhilash Kesavan 			}
13666c247393SAbhilash Kesavan 		}
1367488c7455SChanwoo Choi 		break;
1368488c7455SChanwoo Choi 	default:
1369488c7455SChanwoo Choi 		break;
1370488c7455SChanwoo Choi 	};
13716c247393SAbhilash Kesavan 
137259dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
137359dfa54cSAmit Daniel Kachhap 	if (ret) {
137459dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
13756c247393SAbhilash Kesavan 		goto err_sclk;
137659dfa54cSAmit Daniel Kachhap 	}
137759dfa54cSAmit Daniel Kachhap 
1378cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
1379cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
1380cebe7373SAmit Daniel Kachhap 	if (ret) {
1381cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
13826c247393SAbhilash Kesavan 		goto err_sclk;
1383cebe7373SAmit Daniel Kachhap 	}
138459dfa54cSAmit Daniel Kachhap 
13853b6a1a80SLukasz Majewski 	exynos_tmu_control(pdev, true);
138659dfa54cSAmit Daniel Kachhap 	return 0;
13876c247393SAbhilash Kesavan err_sclk:
13886c247393SAbhilash Kesavan 	clk_disable_unprepare(data->sclk);
138959dfa54cSAmit Daniel Kachhap err_clk:
139059dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
139114a11dc7SNaveen Krishna Chatradhi err_clk_sec:
139214a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
139314a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
13943b6a1a80SLukasz Majewski err_sensor:
1395*5f09a5cbSKrzysztof Kozlowski 	if (!IS_ERR_OR_NULL(data->regulator))
1396*5f09a5cbSKrzysztof Kozlowski 		regulator_disable(data->regulator);
13973b6a1a80SLukasz Majewski 	thermal_zone_of_sensor_unregister(&pdev->dev, data->tzd);
13983b6a1a80SLukasz Majewski 
139959dfa54cSAmit Daniel Kachhap 	return ret;
140059dfa54cSAmit Daniel Kachhap }
140159dfa54cSAmit Daniel Kachhap 
140259dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
140359dfa54cSAmit Daniel Kachhap {
140459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
14053b6a1a80SLukasz Majewski 	struct thermal_zone_device *tzd = data->tzd;
140659dfa54cSAmit Daniel Kachhap 
14073b6a1a80SLukasz Majewski 	thermal_zone_of_sensor_unregister(&pdev->dev, tzd);
14084215688eSBartlomiej Zolnierkiewicz 	exynos_tmu_control(pdev, false);
14094215688eSBartlomiej Zolnierkiewicz 
14106c247393SAbhilash Kesavan 	clk_disable_unprepare(data->sclk);
141159dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
141214a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
141314a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
141459dfa54cSAmit Daniel Kachhap 
1415498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator))
1416498d22f6SAmit Daniel Kachhap 		regulator_disable(data->regulator);
1417498d22f6SAmit Daniel Kachhap 
141859dfa54cSAmit Daniel Kachhap 	return 0;
141959dfa54cSAmit Daniel Kachhap }
142059dfa54cSAmit Daniel Kachhap 
142159dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
142259dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
142359dfa54cSAmit Daniel Kachhap {
142459dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
142559dfa54cSAmit Daniel Kachhap 
142659dfa54cSAmit Daniel Kachhap 	return 0;
142759dfa54cSAmit Daniel Kachhap }
142859dfa54cSAmit Daniel Kachhap 
142959dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
143059dfa54cSAmit Daniel Kachhap {
143159dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
143259dfa54cSAmit Daniel Kachhap 
143359dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
143459dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
143559dfa54cSAmit Daniel Kachhap 
143659dfa54cSAmit Daniel Kachhap 	return 0;
143759dfa54cSAmit Daniel Kachhap }
143859dfa54cSAmit Daniel Kachhap 
143959dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
144059dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
144159dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
144259dfa54cSAmit Daniel Kachhap #else
144359dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
144459dfa54cSAmit Daniel Kachhap #endif
144559dfa54cSAmit Daniel Kachhap 
144659dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
144759dfa54cSAmit Daniel Kachhap 	.driver = {
144859dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
144959dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
145073b5b1d7SSachin Kamat 		.of_match_table = exynos_tmu_match,
145159dfa54cSAmit Daniel Kachhap 	},
145259dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
145359dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
145459dfa54cSAmit Daniel Kachhap };
145559dfa54cSAmit Daniel Kachhap 
145659dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
145759dfa54cSAmit Daniel Kachhap 
145859dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
145959dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
146059dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
146159dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
1462