xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision ccb361d2fdda8975c8bbc8a1749c31dbd62dd276)
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.
1873a3a5f15SKrzysztof Kozlowski  * @ntrip: number of supported trip points.
18872d1100bSBartlomiej Zolnierkiewicz  * @tmu_initialize: SoC specific TMU initialization method
18937f9034fSBartlomiej Zolnierkiewicz  * @tmu_control: SoC specific TMU control method
190b79985caSBartlomiej Zolnierkiewicz  * @tmu_read: SoC specific TMU temperature read method
191285d994aSBartlomiej Zolnierkiewicz  * @tmu_set_emulation: SoC specific TMU emulation setting method
192a7331f72SBartlomiej Zolnierkiewicz  * @tmu_clear_irqs: SoC specific TMU interrupts clearing method
193cebe7373SAmit Daniel Kachhap  */
19459dfa54cSAmit Daniel Kachhap struct exynos_tmu_data {
195cebe7373SAmit Daniel Kachhap 	int id;
19659dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
19759dfa54cSAmit Daniel Kachhap 	void __iomem *base;
1989025d563SNaveen Krishna Chatradhi 	void __iomem *base_second;
19959dfa54cSAmit Daniel Kachhap 	int irq;
20059dfa54cSAmit Daniel Kachhap 	enum soc_type soc;
20159dfa54cSAmit Daniel Kachhap 	struct work_struct irq_work;
20259dfa54cSAmit Daniel Kachhap 	struct mutex lock;
2036c247393SAbhilash Kesavan 	struct clk *clk, *clk_sec, *sclk;
2046c247393SAbhilash Kesavan 	u16 temp_error1, temp_error2;
205498d22f6SAmit Daniel Kachhap 	struct regulator *regulator;
2063b6a1a80SLukasz Majewski 	struct thermal_zone_device *tzd;
2073a3a5f15SKrzysztof Kozlowski 	unsigned int ntrip;
2083b6a1a80SLukasz Majewski 
20972d1100bSBartlomiej Zolnierkiewicz 	int (*tmu_initialize)(struct platform_device *pdev);
21037f9034fSBartlomiej Zolnierkiewicz 	void (*tmu_control)(struct platform_device *pdev, bool on);
211b79985caSBartlomiej Zolnierkiewicz 	int (*tmu_read)(struct exynos_tmu_data *data);
21217e8351aSSascha Hauer 	void (*tmu_set_emulation)(struct exynos_tmu_data *data, int temp);
213a7331f72SBartlomiej Zolnierkiewicz 	void (*tmu_clear_irqs)(struct exynos_tmu_data *data);
21459dfa54cSAmit Daniel Kachhap };
21559dfa54cSAmit Daniel Kachhap 
2163b6a1a80SLukasz Majewski static void exynos_report_trigger(struct exynos_tmu_data *p)
2173b6a1a80SLukasz Majewski {
2183b6a1a80SLukasz Majewski 	char data[10], *envp[] = { data, NULL };
2193b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = p->tzd;
22017e8351aSSascha Hauer 	int temp;
2213b6a1a80SLukasz Majewski 	unsigned int i;
2223b6a1a80SLukasz Majewski 
223eccb6014SLukasz Majewski 	if (!tz) {
224eccb6014SLukasz Majewski 		pr_err("No thermal zone device defined\n");
2253b6a1a80SLukasz Majewski 		return;
2263b6a1a80SLukasz Majewski 	}
2273b6a1a80SLukasz Majewski 
2283b6a1a80SLukasz Majewski 	thermal_zone_device_update(tz);
2293b6a1a80SLukasz Majewski 
2303b6a1a80SLukasz Majewski 	mutex_lock(&tz->lock);
2313b6a1a80SLukasz Majewski 	/* Find the level for which trip happened */
2323b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
2333b6a1a80SLukasz Majewski 		tz->ops->get_trip_temp(tz, i, &temp);
2343b6a1a80SLukasz Majewski 		if (tz->last_temperature < temp)
2353b6a1a80SLukasz Majewski 			break;
2363b6a1a80SLukasz Majewski 	}
2373b6a1a80SLukasz Majewski 
2383b6a1a80SLukasz Majewski 	snprintf(data, sizeof(data), "%u", i);
2393b6a1a80SLukasz Majewski 	kobject_uevent_env(&tz->device.kobj, KOBJ_CHANGE, envp);
2403b6a1a80SLukasz Majewski 	mutex_unlock(&tz->lock);
2413b6a1a80SLukasz Majewski }
2423b6a1a80SLukasz Majewski 
24359dfa54cSAmit Daniel Kachhap /*
24459dfa54cSAmit Daniel Kachhap  * TMU treats temperature as a mapped temperature code.
24559dfa54cSAmit Daniel Kachhap  * The temperature is converted differently depending on the calibration type.
24659dfa54cSAmit Daniel Kachhap  */
24759dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
24859dfa54cSAmit Daniel Kachhap {
24959dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
25059dfa54cSAmit Daniel Kachhap 	int temp_code;
25159dfa54cSAmit Daniel Kachhap 
25259dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
25359dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
254bb34b4c8SAmit Daniel Kachhap 		temp_code = (temp - pdata->first_point_trim) *
25559dfa54cSAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) /
256bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) +
257bb34b4c8SAmit Daniel Kachhap 			data->temp_error1;
25859dfa54cSAmit Daniel Kachhap 		break;
25959dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
260bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + data->temp_error1 - pdata->first_point_trim;
26159dfa54cSAmit Daniel Kachhap 		break;
26259dfa54cSAmit Daniel Kachhap 	default:
263bb34b4c8SAmit Daniel Kachhap 		temp_code = temp + pdata->default_temp_offset;
26459dfa54cSAmit Daniel Kachhap 		break;
26559dfa54cSAmit Daniel Kachhap 	}
266ddb31d43SBartlomiej Zolnierkiewicz 
26759dfa54cSAmit Daniel Kachhap 	return temp_code;
26859dfa54cSAmit Daniel Kachhap }
26959dfa54cSAmit Daniel Kachhap 
27059dfa54cSAmit Daniel Kachhap /*
27159dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
27259dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
27359dfa54cSAmit Daniel Kachhap  */
2746c247393SAbhilash Kesavan static int code_to_temp(struct exynos_tmu_data *data, u16 temp_code)
27559dfa54cSAmit Daniel Kachhap {
27659dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
27759dfa54cSAmit Daniel Kachhap 	int temp;
27859dfa54cSAmit Daniel Kachhap 
27959dfa54cSAmit Daniel Kachhap 	switch (pdata->cal_type) {
28059dfa54cSAmit Daniel Kachhap 	case TYPE_TWO_POINT_TRIMMING:
281bb34b4c8SAmit Daniel Kachhap 		temp = (temp_code - data->temp_error1) *
282bb34b4c8SAmit Daniel Kachhap 			(pdata->second_point_trim - pdata->first_point_trim) /
283bb34b4c8SAmit Daniel Kachhap 			(data->temp_error2 - data->temp_error1) +
284bb34b4c8SAmit Daniel Kachhap 			pdata->first_point_trim;
28559dfa54cSAmit Daniel Kachhap 		break;
28659dfa54cSAmit Daniel Kachhap 	case TYPE_ONE_POINT_TRIMMING:
287bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - data->temp_error1 + pdata->first_point_trim;
28859dfa54cSAmit Daniel Kachhap 		break;
28959dfa54cSAmit Daniel Kachhap 	default:
290bb34b4c8SAmit Daniel Kachhap 		temp = temp_code - pdata->default_temp_offset;
29159dfa54cSAmit Daniel Kachhap 		break;
29259dfa54cSAmit Daniel Kachhap 	}
293ddb31d43SBartlomiej Zolnierkiewicz 
29459dfa54cSAmit Daniel Kachhap 	return temp;
29559dfa54cSAmit Daniel Kachhap }
29659dfa54cSAmit Daniel Kachhap 
2978328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
298b835ced1SBartlomiej Zolnierkiewicz {
29959dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
30059dfa54cSAmit Daniel Kachhap 
301b8d582b9SAmit Daniel Kachhap 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
30299d67fb9SBartlomiej Zolnierkiewicz 	data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
303b8d582b9SAmit Daniel Kachhap 				EXYNOS_TMU_TEMP_MASK);
30459dfa54cSAmit Daniel Kachhap 
3055000806cSAmit Daniel Kachhap 	if (!data->temp_error1 ||
3065000806cSAmit Daniel Kachhap 		(pdata->min_efuse_value > data->temp_error1) ||
3075000806cSAmit Daniel Kachhap 		(data->temp_error1 > pdata->max_efuse_value))
3085000806cSAmit Daniel Kachhap 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
3095000806cSAmit Daniel Kachhap 
3105000806cSAmit Daniel Kachhap 	if (!data->temp_error2)
3115000806cSAmit Daniel Kachhap 		data->temp_error2 =
31299d67fb9SBartlomiej Zolnierkiewicz 			(pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
3135000806cSAmit Daniel Kachhap 			EXYNOS_TMU_TEMP_MASK;
3148328a4b1SBartlomiej Zolnierkiewicz }
31559dfa54cSAmit Daniel Kachhap 
316fe87789cSBartlomiej Zolnierkiewicz static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling)
317fe87789cSBartlomiej Zolnierkiewicz {
3183b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
3193b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
3203b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(tz);
3213b6a1a80SLukasz Majewski 	unsigned long temp;
322fe87789cSBartlomiej Zolnierkiewicz 	int i;
323c65d3473STushar Behera 
3243b6a1a80SLukasz Majewski 	if (!trips) {
3253b6a1a80SLukasz Majewski 		pr_err("%s: Cannot get trip points from of-thermal.c!\n",
3263b6a1a80SLukasz Majewski 		       __func__);
3273b6a1a80SLukasz Majewski 		return 0;
3283b6a1a80SLukasz Majewski 	}
329fe87789cSBartlomiej Zolnierkiewicz 
3303b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
3313b6a1a80SLukasz Majewski 		if (trips[i].type == THERMAL_TRIP_CRITICAL)
3323b6a1a80SLukasz Majewski 			continue;
3333b6a1a80SLukasz Majewski 
3343b6a1a80SLukasz Majewski 		temp = trips[i].temperature / MCELSIUS;
335fe87789cSBartlomiej Zolnierkiewicz 		if (falling)
3363b6a1a80SLukasz Majewski 			temp -= (trips[i].hysteresis / MCELSIUS);
337fe87789cSBartlomiej Zolnierkiewicz 		else
338fe87789cSBartlomiej Zolnierkiewicz 			threshold &= ~(0xff << 8 * i);
339fe87789cSBartlomiej Zolnierkiewicz 
340fe87789cSBartlomiej Zolnierkiewicz 		threshold |= temp_to_code(data, temp) << 8 * i;
34159dfa54cSAmit Daniel Kachhap 	}
34259dfa54cSAmit Daniel Kachhap 
343fe87789cSBartlomiej Zolnierkiewicz 	return threshold;
344fe87789cSBartlomiej Zolnierkiewicz }
34559dfa54cSAmit Daniel Kachhap 
34659dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
34759dfa54cSAmit Daniel Kachhap {
34859dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
34972d1100bSBartlomiej Zolnierkiewicz 	int ret;
3507ca04e58SAmit Daniel Kachhap 
3513a3a5f15SKrzysztof Kozlowski 	if (of_thermal_get_ntrips(data->tzd) > data->ntrip) {
3523a3a5f15SKrzysztof Kozlowski 		dev_info(&pdev->dev,
3533a3a5f15SKrzysztof Kozlowski 			 "More trip points than supported by this TMU.\n");
3543a3a5f15SKrzysztof Kozlowski 		dev_info(&pdev->dev,
3553a3a5f15SKrzysztof Kozlowski 			 "%d trip points should be configured in polling mode.\n",
3563a3a5f15SKrzysztof Kozlowski 			 (of_thermal_get_ntrips(data->tzd) - data->ntrip));
3573a3a5f15SKrzysztof Kozlowski 	}
3583a3a5f15SKrzysztof Kozlowski 
35959dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
36059dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
36159dfa54cSAmit Daniel Kachhap 	if (!IS_ERR(data->clk_sec))
36259dfa54cSAmit Daniel Kachhap 		clk_enable(data->clk_sec);
36372d1100bSBartlomiej Zolnierkiewicz 	ret = data->tmu_initialize(pdev);
36459dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
36559dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
36614a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
36714a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
36859dfa54cSAmit Daniel Kachhap 
36959dfa54cSAmit Daniel Kachhap 	return ret;
37059dfa54cSAmit Daniel Kachhap }
37159dfa54cSAmit Daniel Kachhap 
372d00671c3SBartlomiej Zolnierkiewicz static u32 get_con_reg(struct exynos_tmu_data *data, u32 con)
37359dfa54cSAmit Daniel Kachhap {
37459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
37559dfa54cSAmit Daniel Kachhap 
3767575983cSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS4412 ||
3777575983cSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS3250)
3787575983cSBartlomiej Zolnierkiewicz 		con |= (EXYNOS4412_MUX_ADDR_VALUE << EXYNOS4412_MUX_ADDR_SHIFT);
37986f5362eSLukasz Majewski 
38099d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
38199d67fb9SBartlomiej Zolnierkiewicz 	con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
382d0a0ce3eSAmit Daniel Kachhap 
38399d67fb9SBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
38499d67fb9SBartlomiej Zolnierkiewicz 	con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
385d0a0ce3eSAmit Daniel Kachhap 
386d0a0ce3eSAmit Daniel Kachhap 	if (pdata->noise_cancel_mode) {
387b9504a6aSBartlomiej Zolnierkiewicz 		con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
388b9504a6aSBartlomiej Zolnierkiewicz 		con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT);
38959dfa54cSAmit Daniel Kachhap 	}
39059dfa54cSAmit Daniel Kachhap 
391d00671c3SBartlomiej Zolnierkiewicz 	return con;
392d00671c3SBartlomiej Zolnierkiewicz }
393d00671c3SBartlomiej Zolnierkiewicz 
394d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on)
395d00671c3SBartlomiej Zolnierkiewicz {
396d00671c3SBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
397d00671c3SBartlomiej Zolnierkiewicz 
398d00671c3SBartlomiej Zolnierkiewicz 	mutex_lock(&data->lock);
399d00671c3SBartlomiej Zolnierkiewicz 	clk_enable(data->clk);
40037f9034fSBartlomiej Zolnierkiewicz 	data->tmu_control(pdev, on);
40159dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
40259dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
40359dfa54cSAmit Daniel Kachhap }
40459dfa54cSAmit Daniel Kachhap 
40572d1100bSBartlomiej Zolnierkiewicz static int exynos4210_tmu_initialize(struct platform_device *pdev)
40672d1100bSBartlomiej Zolnierkiewicz {
40772d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
4083b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
4093b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
4103b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(tz);
41172d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
4123b6a1a80SLukasz Majewski 	unsigned long reference, temp;
4133b6a1a80SLukasz Majewski 	unsigned int status;
4143b6a1a80SLukasz Majewski 
4153b6a1a80SLukasz Majewski 	if (!trips) {
4163b6a1a80SLukasz Majewski 		pr_err("%s: Cannot get trip points from of-thermal.c!\n",
4173b6a1a80SLukasz Majewski 		       __func__);
4183b6a1a80SLukasz Majewski 		ret = -ENODEV;
4193b6a1a80SLukasz Majewski 		goto out;
4203b6a1a80SLukasz Majewski 	}
42172d1100bSBartlomiej Zolnierkiewicz 
42272d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
42372d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
42472d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
42572d1100bSBartlomiej Zolnierkiewicz 		goto out;
42672d1100bSBartlomiej Zolnierkiewicz 	}
42772d1100bSBartlomiej Zolnierkiewicz 
42872d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO));
42972d1100bSBartlomiej Zolnierkiewicz 
43072d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for threshold */
4313b6a1a80SLukasz Majewski 	reference = trips[0].temperature / MCELSIUS;
4323b6a1a80SLukasz Majewski 	threshold_code = temp_to_code(data, reference);
4333b6a1a80SLukasz Majewski 	if (threshold_code < 0) {
4343b6a1a80SLukasz Majewski 		ret = threshold_code;
4353b6a1a80SLukasz Majewski 		goto out;
4363b6a1a80SLukasz Majewski 	}
43772d1100bSBartlomiej Zolnierkiewicz 	writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
43872d1100bSBartlomiej Zolnierkiewicz 
4393b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
4403b6a1a80SLukasz Majewski 		temp = trips[i].temperature / MCELSIUS;
4413b6a1a80SLukasz Majewski 		writeb(temp - reference, data->base +
44272d1100bSBartlomiej Zolnierkiewicz 		       EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
4433b6a1a80SLukasz Majewski 	}
44472d1100bSBartlomiej Zolnierkiewicz 
445a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
44672d1100bSBartlomiej Zolnierkiewicz out:
44772d1100bSBartlomiej Zolnierkiewicz 	return ret;
44872d1100bSBartlomiej Zolnierkiewicz }
44972d1100bSBartlomiej Zolnierkiewicz 
45072d1100bSBartlomiej Zolnierkiewicz static int exynos4412_tmu_initialize(struct platform_device *pdev)
45172d1100bSBartlomiej Zolnierkiewicz {
45272d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
4533b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
4543b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(data->tzd);
45572d1100bSBartlomiej Zolnierkiewicz 	unsigned int status, trim_info, con, ctrl, rising_threshold;
45672d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
4573b6a1a80SLukasz Majewski 	unsigned long crit_temp = 0;
45872d1100bSBartlomiej Zolnierkiewicz 
45972d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
46072d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
46172d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
46272d1100bSBartlomiej Zolnierkiewicz 		goto out;
46372d1100bSBartlomiej Zolnierkiewicz 	}
46472d1100bSBartlomiej Zolnierkiewicz 
46572d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS3250 ||
46672d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS4412 ||
46772d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS5250) {
46872d1100bSBartlomiej Zolnierkiewicz 		if (data->soc == SOC_ARCH_EXYNOS3250) {
46972d1100bSBartlomiej Zolnierkiewicz 			ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
47072d1100bSBartlomiej Zolnierkiewicz 			ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
47172d1100bSBartlomiej Zolnierkiewicz 			writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
47272d1100bSBartlomiej Zolnierkiewicz 		}
47372d1100bSBartlomiej Zolnierkiewicz 		ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
47472d1100bSBartlomiej Zolnierkiewicz 		ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
47572d1100bSBartlomiej Zolnierkiewicz 		writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
47672d1100bSBartlomiej Zolnierkiewicz 	}
47772d1100bSBartlomiej Zolnierkiewicz 
47872d1100bSBartlomiej Zolnierkiewicz 	/* On exynos5420 the triminfo register is in the shared space */
47972d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
48072d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO);
48172d1100bSBartlomiej Zolnierkiewicz 	else
48272d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
48372d1100bSBartlomiej Zolnierkiewicz 
48472d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
48572d1100bSBartlomiej Zolnierkiewicz 
48672d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
48772d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE);
48872d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
48972d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
49072d1100bSBartlomiej Zolnierkiewicz 	writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL);
49172d1100bSBartlomiej Zolnierkiewicz 
492a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
49372d1100bSBartlomiej Zolnierkiewicz 
49472d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
4953b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(data->tzd); i++) {
4963b6a1a80SLukasz Majewski 		if (trips[i].type == THERMAL_TRIP_CRITICAL) {
4973b6a1a80SLukasz Majewski 			crit_temp = trips[i].temperature;
4983b6a1a80SLukasz Majewski 			break;
4993b6a1a80SLukasz Majewski 		}
5003b6a1a80SLukasz Majewski 	}
5013b6a1a80SLukasz Majewski 
5023b6a1a80SLukasz Majewski 	if (i == of_thermal_get_ntrips(data->tzd)) {
5033b6a1a80SLukasz Majewski 		pr_err("%s: No CRITICAL trip point defined at of-thermal.c!\n",
5043b6a1a80SLukasz Majewski 		       __func__);
5053b6a1a80SLukasz Majewski 		ret = -EINVAL;
5063b6a1a80SLukasz Majewski 		goto out;
5073b6a1a80SLukasz Majewski 	}
5083b6a1a80SLukasz Majewski 
5093b6a1a80SLukasz Majewski 	threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
51072d1100bSBartlomiej Zolnierkiewicz 	/* 1-4 level to be assigned in th0 reg */
51172d1100bSBartlomiej Zolnierkiewicz 	rising_threshold &= ~(0xff << 8 * i);
51272d1100bSBartlomiej Zolnierkiewicz 	rising_threshold |= threshold_code << 8 * i;
51372d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
51472d1100bSBartlomiej Zolnierkiewicz 	con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
51572d1100bSBartlomiej Zolnierkiewicz 	con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
51672d1100bSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
5173b6a1a80SLukasz Majewski 
51872d1100bSBartlomiej Zolnierkiewicz out:
51972d1100bSBartlomiej Zolnierkiewicz 	return ret;
52072d1100bSBartlomiej Zolnierkiewicz }
52172d1100bSBartlomiej Zolnierkiewicz 
522488c7455SChanwoo Choi static int exynos5433_tmu_initialize(struct platform_device *pdev)
523488c7455SChanwoo Choi {
524488c7455SChanwoo Choi 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
525488c7455SChanwoo Choi 	struct exynos_tmu_platform_data *pdata = data->pdata;
526488c7455SChanwoo Choi 	struct thermal_zone_device *tz = data->tzd;
527488c7455SChanwoo Choi 	unsigned int status, trim_info;
528488c7455SChanwoo Choi 	unsigned int rising_threshold = 0, falling_threshold = 0;
52917e8351aSSascha Hauer 	int temp, temp_hist;
530488c7455SChanwoo Choi 	int ret = 0, threshold_code, i, sensor_id, cal_type;
531488c7455SChanwoo Choi 
532488c7455SChanwoo Choi 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
533488c7455SChanwoo Choi 	if (!status) {
534488c7455SChanwoo Choi 		ret = -EBUSY;
535488c7455SChanwoo Choi 		goto out;
536488c7455SChanwoo Choi 	}
537488c7455SChanwoo Choi 
538488c7455SChanwoo Choi 	trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
539488c7455SChanwoo Choi 	sanitize_temp_error(data, trim_info);
540488c7455SChanwoo Choi 
541488c7455SChanwoo Choi 	/* Read the temperature sensor id */
542488c7455SChanwoo Choi 	sensor_id = (trim_info & EXYNOS5433_TRIMINFO_SENSOR_ID_MASK)
543488c7455SChanwoo Choi 				>> EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT;
544488c7455SChanwoo Choi 	dev_info(&pdev->dev, "Temperature sensor ID: 0x%x\n", sensor_id);
545488c7455SChanwoo Choi 
546488c7455SChanwoo Choi 	/* Read the calibration mode */
547488c7455SChanwoo Choi 	writel(trim_info, data->base + EXYNOS_TMU_REG_TRIMINFO);
548488c7455SChanwoo Choi 	cal_type = (trim_info & EXYNOS5433_TRIMINFO_CALIB_SEL_MASK)
549488c7455SChanwoo Choi 				>> EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT;
550488c7455SChanwoo Choi 
551488c7455SChanwoo Choi 	switch (cal_type) {
552488c7455SChanwoo Choi 	case EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING:
553488c7455SChanwoo Choi 		pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
554488c7455SChanwoo Choi 		break;
555488c7455SChanwoo Choi 	case EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING:
556488c7455SChanwoo Choi 		pdata->cal_type = TYPE_TWO_POINT_TRIMMING;
557488c7455SChanwoo Choi 		break;
558488c7455SChanwoo Choi 	default:
559488c7455SChanwoo Choi 		pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
560488c7455SChanwoo Choi 		break;
561baba1ebbSKrzysztof Kozlowski 	}
562488c7455SChanwoo Choi 
563488c7455SChanwoo Choi 	dev_info(&pdev->dev, "Calibration type is %d-point calibration\n",
564488c7455SChanwoo Choi 			cal_type ?  2 : 1);
565488c7455SChanwoo Choi 
566488c7455SChanwoo Choi 	/* Write temperature code for rising and falling threshold */
567488c7455SChanwoo Choi 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
568488c7455SChanwoo Choi 		int rising_reg_offset, falling_reg_offset;
569488c7455SChanwoo Choi 		int j = 0;
570488c7455SChanwoo Choi 
571488c7455SChanwoo Choi 		switch (i) {
572488c7455SChanwoo Choi 		case 0:
573488c7455SChanwoo Choi 		case 1:
574488c7455SChanwoo Choi 		case 2:
575488c7455SChanwoo Choi 		case 3:
576488c7455SChanwoo Choi 			rising_reg_offset = EXYNOS5433_THD_TEMP_RISE3_0;
577488c7455SChanwoo Choi 			falling_reg_offset = EXYNOS5433_THD_TEMP_FALL3_0;
578488c7455SChanwoo Choi 			j = i;
579488c7455SChanwoo Choi 			break;
580488c7455SChanwoo Choi 		case 4:
581488c7455SChanwoo Choi 		case 5:
582488c7455SChanwoo Choi 		case 6:
583488c7455SChanwoo Choi 		case 7:
584488c7455SChanwoo Choi 			rising_reg_offset = EXYNOS5433_THD_TEMP_RISE7_4;
585488c7455SChanwoo Choi 			falling_reg_offset = EXYNOS5433_THD_TEMP_FALL7_4;
586488c7455SChanwoo Choi 			j = i - 4;
587488c7455SChanwoo Choi 			break;
588488c7455SChanwoo Choi 		default:
589488c7455SChanwoo Choi 			continue;
590488c7455SChanwoo Choi 		}
591488c7455SChanwoo Choi 
592488c7455SChanwoo Choi 		/* Write temperature code for rising threshold */
593488c7455SChanwoo Choi 		tz->ops->get_trip_temp(tz, i, &temp);
594488c7455SChanwoo Choi 		temp /= MCELSIUS;
595488c7455SChanwoo Choi 		threshold_code = temp_to_code(data, temp);
596488c7455SChanwoo Choi 
597488c7455SChanwoo Choi 		rising_threshold = readl(data->base + rising_reg_offset);
598488c7455SChanwoo Choi 		rising_threshold |= (threshold_code << j * 8);
599488c7455SChanwoo Choi 		writel(rising_threshold, data->base + rising_reg_offset);
600488c7455SChanwoo Choi 
601488c7455SChanwoo Choi 		/* Write temperature code for falling threshold */
602488c7455SChanwoo Choi 		tz->ops->get_trip_hyst(tz, i, &temp_hist);
603488c7455SChanwoo Choi 		temp_hist = temp - (temp_hist / MCELSIUS);
604488c7455SChanwoo Choi 		threshold_code = temp_to_code(data, temp_hist);
605488c7455SChanwoo Choi 
606488c7455SChanwoo Choi 		falling_threshold = readl(data->base + falling_reg_offset);
607488c7455SChanwoo Choi 		falling_threshold &= ~(0xff << j * 8);
608488c7455SChanwoo Choi 		falling_threshold |= (threshold_code << j * 8);
609488c7455SChanwoo Choi 		writel(falling_threshold, data->base + falling_reg_offset);
610488c7455SChanwoo Choi 	}
611488c7455SChanwoo Choi 
612488c7455SChanwoo Choi 	data->tmu_clear_irqs(data);
613488c7455SChanwoo Choi out:
614488c7455SChanwoo Choi 	return ret;
615488c7455SChanwoo Choi }
616488c7455SChanwoo Choi 
61772d1100bSBartlomiej Zolnierkiewicz static int exynos5440_tmu_initialize(struct platform_device *pdev)
61872d1100bSBartlomiej Zolnierkiewicz {
61972d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
62072d1100bSBartlomiej Zolnierkiewicz 	unsigned int trim_info = 0, con, rising_threshold;
621e35dbb4dSKrzysztof Kozlowski 	int threshold_code;
62217e8351aSSascha Hauer 	int crit_temp = 0;
62372d1100bSBartlomiej Zolnierkiewicz 
62472d1100bSBartlomiej Zolnierkiewicz 	/*
62572d1100bSBartlomiej Zolnierkiewicz 	 * For exynos5440 soc triminfo value is swapped between TMU0 and
62672d1100bSBartlomiej Zolnierkiewicz 	 * TMU2, so the below logic is needed.
62772d1100bSBartlomiej Zolnierkiewicz 	 */
62872d1100bSBartlomiej Zolnierkiewicz 	switch (data->id) {
62972d1100bSBartlomiej Zolnierkiewicz 	case 0:
63072d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET +
63172d1100bSBartlomiej Zolnierkiewicz 				 EXYNOS5440_TMU_S0_7_TRIM);
63272d1100bSBartlomiej Zolnierkiewicz 		break;
63372d1100bSBartlomiej Zolnierkiewicz 	case 1:
63472d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM);
63572d1100bSBartlomiej Zolnierkiewicz 		break;
63672d1100bSBartlomiej Zolnierkiewicz 	case 2:
63772d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET +
63872d1100bSBartlomiej Zolnierkiewicz 				  EXYNOS5440_TMU_S0_7_TRIM);
63972d1100bSBartlomiej Zolnierkiewicz 	}
64072d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
64172d1100bSBartlomiej Zolnierkiewicz 
64272d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
64372d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0);
64472d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
64572d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0);
64672d1100bSBartlomiej Zolnierkiewicz 	writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1);
64772d1100bSBartlomiej Zolnierkiewicz 
648a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
64972d1100bSBartlomiej Zolnierkiewicz 
65072d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
6513b6a1a80SLukasz Majewski 	if (!data->tzd->ops->get_crit_temp(data->tzd, &crit_temp)) {
6523b6a1a80SLukasz Majewski 		threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
65372d1100bSBartlomiej Zolnierkiewicz 		/* 5th level to be assigned in th2 reg */
65472d1100bSBartlomiej Zolnierkiewicz 		rising_threshold =
65572d1100bSBartlomiej Zolnierkiewicz 			threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT;
65672d1100bSBartlomiej Zolnierkiewicz 		writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2);
65772d1100bSBartlomiej Zolnierkiewicz 		con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL);
65872d1100bSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
65972d1100bSBartlomiej Zolnierkiewicz 		writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
66072d1100bSBartlomiej Zolnierkiewicz 	}
66172d1100bSBartlomiej Zolnierkiewicz 	/* Clear the PMIN in the common TMU register */
66272d1100bSBartlomiej Zolnierkiewicz 	if (!data->id)
66372d1100bSBartlomiej Zolnierkiewicz 		writel(0, data->base_second + EXYNOS5440_TMU_PMIN);
664e35dbb4dSKrzysztof Kozlowski 
665e35dbb4dSKrzysztof Kozlowski 	return 0;
66672d1100bSBartlomiej Zolnierkiewicz }
66772d1100bSBartlomiej Zolnierkiewicz 
6686c247393SAbhilash Kesavan static int exynos7_tmu_initialize(struct platform_device *pdev)
6696c247393SAbhilash Kesavan {
6706c247393SAbhilash Kesavan 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
6716c247393SAbhilash Kesavan 	struct thermal_zone_device *tz = data->tzd;
6726c247393SAbhilash Kesavan 	struct exynos_tmu_platform_data *pdata = data->pdata;
6736c247393SAbhilash Kesavan 	unsigned int status, trim_info;
6746c247393SAbhilash Kesavan 	unsigned int rising_threshold = 0, falling_threshold = 0;
6756c247393SAbhilash Kesavan 	int ret = 0, threshold_code, i;
67617e8351aSSascha Hauer 	int temp, temp_hist;
6776c247393SAbhilash Kesavan 	unsigned int reg_off, bit_off;
6786c247393SAbhilash Kesavan 
6796c247393SAbhilash Kesavan 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
6806c247393SAbhilash Kesavan 	if (!status) {
6816c247393SAbhilash Kesavan 		ret = -EBUSY;
6826c247393SAbhilash Kesavan 		goto out;
6836c247393SAbhilash Kesavan 	}
6846c247393SAbhilash Kesavan 
6856c247393SAbhilash Kesavan 	trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
6866c247393SAbhilash Kesavan 
6876c247393SAbhilash Kesavan 	data->temp_error1 = trim_info & EXYNOS7_TMU_TEMP_MASK;
6886c247393SAbhilash Kesavan 	if (!data->temp_error1 ||
6896c247393SAbhilash Kesavan 	    (pdata->min_efuse_value > data->temp_error1) ||
6906c247393SAbhilash Kesavan 	    (data->temp_error1 > pdata->max_efuse_value))
6916c247393SAbhilash Kesavan 		data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
6926c247393SAbhilash Kesavan 
6936c247393SAbhilash Kesavan 	/* Write temperature code for rising and falling threshold */
6946c247393SAbhilash Kesavan 	for (i = (of_thermal_get_ntrips(tz) - 1); i >= 0; i--) {
6956c247393SAbhilash Kesavan 		/*
6966c247393SAbhilash Kesavan 		 * On exynos7 there are 4 rising and 4 falling threshold
6976c247393SAbhilash Kesavan 		 * registers (0x50-0x5c and 0x60-0x6c respectively). Each
6986c247393SAbhilash Kesavan 		 * register holds the value of two threshold levels (at bit
6996c247393SAbhilash Kesavan 		 * offsets 0 and 16). Based on the fact that there are atmost
7006c247393SAbhilash Kesavan 		 * eight possible trigger levels, calculate the register and
7016c247393SAbhilash Kesavan 		 * bit offsets where the threshold levels are to be written.
7026c247393SAbhilash Kesavan 		 *
7036c247393SAbhilash Kesavan 		 * e.g. EXYNOS7_THD_TEMP_RISE7_6 (0x50)
7046c247393SAbhilash Kesavan 		 * [24:16] - Threshold level 7
7056c247393SAbhilash Kesavan 		 * [8:0] - Threshold level 6
7066c247393SAbhilash Kesavan 		 * e.g. EXYNOS7_THD_TEMP_RISE5_4 (0x54)
7076c247393SAbhilash Kesavan 		 * [24:16] - Threshold level 5
7086c247393SAbhilash Kesavan 		 * [8:0] - Threshold level 4
7096c247393SAbhilash Kesavan 		 *
7106c247393SAbhilash Kesavan 		 * and similarly for falling thresholds.
7116c247393SAbhilash Kesavan 		 *
7126c247393SAbhilash Kesavan 		 * Based on the above, calculate the register and bit offsets
7136c247393SAbhilash Kesavan 		 * for rising/falling threshold levels and populate them.
7146c247393SAbhilash Kesavan 		 */
7156c247393SAbhilash Kesavan 		reg_off = ((7 - i) / 2) * 4;
7166c247393SAbhilash Kesavan 		bit_off = ((8 - i) % 2);
7176c247393SAbhilash Kesavan 
7186c247393SAbhilash Kesavan 		tz->ops->get_trip_temp(tz, i, &temp);
7196c247393SAbhilash Kesavan 		temp /= MCELSIUS;
7206c247393SAbhilash Kesavan 
7216c247393SAbhilash Kesavan 		tz->ops->get_trip_hyst(tz, i, &temp_hist);
7226c247393SAbhilash Kesavan 		temp_hist = temp - (temp_hist / MCELSIUS);
7236c247393SAbhilash Kesavan 
7246c247393SAbhilash Kesavan 		/* Set 9-bit temperature code for rising threshold levels */
7256c247393SAbhilash Kesavan 		threshold_code = temp_to_code(data, temp);
7266c247393SAbhilash Kesavan 		rising_threshold = readl(data->base +
7276c247393SAbhilash Kesavan 			EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
7286c247393SAbhilash Kesavan 		rising_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
7296c247393SAbhilash Kesavan 		rising_threshold |= threshold_code << (16 * bit_off);
7306c247393SAbhilash Kesavan 		writel(rising_threshold,
7316c247393SAbhilash Kesavan 		       data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
7326c247393SAbhilash Kesavan 
7336c247393SAbhilash Kesavan 		/* Set 9-bit temperature code for falling threshold levels */
7346c247393SAbhilash Kesavan 		threshold_code = temp_to_code(data, temp_hist);
7356c247393SAbhilash Kesavan 		falling_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
7366c247393SAbhilash Kesavan 		falling_threshold |= threshold_code << (16 * bit_off);
7376c247393SAbhilash Kesavan 		writel(falling_threshold,
7386c247393SAbhilash Kesavan 		       data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off);
7396c247393SAbhilash Kesavan 	}
7406c247393SAbhilash Kesavan 
7416c247393SAbhilash Kesavan 	data->tmu_clear_irqs(data);
7426c247393SAbhilash Kesavan out:
7436c247393SAbhilash Kesavan 	return ret;
7446c247393SAbhilash Kesavan }
7456c247393SAbhilash Kesavan 
74637f9034fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_control(struct platform_device *pdev, bool on)
74737f9034fSBartlomiej Zolnierkiewicz {
74837f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
7493b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
75037f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
75137f9034fSBartlomiej Zolnierkiewicz 
75237f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
75337f9034fSBartlomiej Zolnierkiewicz 
75459dfa54cSAmit Daniel Kachhap 	if (on) {
75559dfa54cSAmit Daniel Kachhap 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
75659dfa54cSAmit Daniel Kachhap 		interrupt_en =
7573b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 3)
7583b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE3_SHIFT) |
7593b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 2)
7603b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE2_SHIFT) |
7613b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 1)
7623b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE1_SHIFT) |
7633b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 0)
7643b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE0_SHIFT);
7653b6a1a80SLukasz Majewski 
766e0761533SBartlomiej Zolnierkiewicz 		if (data->soc != SOC_ARCH_EXYNOS4210)
76759dfa54cSAmit Daniel Kachhap 			interrupt_en |=
76837f9034fSBartlomiej Zolnierkiewicz 				interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
76959dfa54cSAmit Daniel Kachhap 	} else {
77059dfa54cSAmit Daniel Kachhap 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
77159dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
77259dfa54cSAmit Daniel Kachhap 	}
77337f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
77437f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
77537f9034fSBartlomiej Zolnierkiewicz }
77659dfa54cSAmit Daniel Kachhap 
777488c7455SChanwoo Choi static void exynos5433_tmu_control(struct platform_device *pdev, bool on)
778488c7455SChanwoo Choi {
779488c7455SChanwoo Choi 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
780488c7455SChanwoo Choi 	struct thermal_zone_device *tz = data->tzd;
781488c7455SChanwoo Choi 	unsigned int con, interrupt_en, pd_det_en;
782488c7455SChanwoo Choi 
783488c7455SChanwoo Choi 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
784488c7455SChanwoo Choi 
785488c7455SChanwoo Choi 	if (on) {
786488c7455SChanwoo Choi 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
787488c7455SChanwoo Choi 		interrupt_en =
788488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 7)
789488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
790488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 6)
791488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
792488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 5)
793488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
794488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 4)
795488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
796488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 3)
797488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
798488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 2)
799488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
800488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 1)
801488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
802488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 0)
803488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE0_SHIFT);
804488c7455SChanwoo Choi 
805488c7455SChanwoo Choi 		interrupt_en |=
806488c7455SChanwoo Choi 			interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
807488c7455SChanwoo Choi 	} else {
808488c7455SChanwoo Choi 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
809488c7455SChanwoo Choi 		interrupt_en = 0; /* Disable all interrupts */
810488c7455SChanwoo Choi 	}
811488c7455SChanwoo Choi 
812488c7455SChanwoo Choi 	pd_det_en = on ? EXYNOS5433_PD_DET_EN : 0;
813488c7455SChanwoo Choi 
814488c7455SChanwoo Choi 	writel(pd_det_en, data->base + EXYNOS5433_TMU_PD_DET_EN);
815488c7455SChanwoo Choi 	writel(interrupt_en, data->base + EXYNOS5433_TMU_REG_INTEN);
816488c7455SChanwoo Choi 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
817488c7455SChanwoo Choi }
818488c7455SChanwoo Choi 
81937f9034fSBartlomiej Zolnierkiewicz static void exynos5440_tmu_control(struct platform_device *pdev, bool on)
82037f9034fSBartlomiej Zolnierkiewicz {
82137f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
8223b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
82337f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
82437f9034fSBartlomiej Zolnierkiewicz 
82537f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL));
82637f9034fSBartlomiej Zolnierkiewicz 
82737f9034fSBartlomiej Zolnierkiewicz 	if (on) {
82837f9034fSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
82937f9034fSBartlomiej Zolnierkiewicz 		interrupt_en =
8303b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 3)
8313b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE3_SHIFT) |
8323b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 2)
8333b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE2_SHIFT) |
8343b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 1)
8353b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE1_SHIFT) |
8363b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 0)
8373b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE0_SHIFT);
8383b6a1a80SLukasz Majewski 		interrupt_en |=
8393b6a1a80SLukasz Majewski 			interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT;
84037f9034fSBartlomiej Zolnierkiewicz 	} else {
84137f9034fSBartlomiej Zolnierkiewicz 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
84237f9034fSBartlomiej Zolnierkiewicz 		interrupt_en = 0; /* Disable all interrupts */
84337f9034fSBartlomiej Zolnierkiewicz 	}
84437f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN);
84537f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
84659dfa54cSAmit Daniel Kachhap }
84759dfa54cSAmit Daniel Kachhap 
8486c247393SAbhilash Kesavan static void exynos7_tmu_control(struct platform_device *pdev, bool on)
8496c247393SAbhilash Kesavan {
8506c247393SAbhilash Kesavan 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
8516c247393SAbhilash Kesavan 	struct thermal_zone_device *tz = data->tzd;
8526c247393SAbhilash Kesavan 	unsigned int con, interrupt_en;
8536c247393SAbhilash Kesavan 
8546c247393SAbhilash Kesavan 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
8556c247393SAbhilash Kesavan 
8566c247393SAbhilash Kesavan 	if (on) {
8576c247393SAbhilash Kesavan 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
85842b696e8SChanwoo Choi 		con |= (1 << EXYNOS7_PD_DET_EN_SHIFT);
8596c247393SAbhilash Kesavan 		interrupt_en =
8606c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 7)
8616c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
8626c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 6)
8636c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
8646c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 5)
8656c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
8666c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 4)
8676c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
8686c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 3)
8696c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
8706c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 2)
8716c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
8726c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 1)
8736c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
8746c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 0)
8756c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE0_SHIFT);
8766c247393SAbhilash Kesavan 
8776c247393SAbhilash Kesavan 		interrupt_en |=
8786c247393SAbhilash Kesavan 			interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
8796c247393SAbhilash Kesavan 	} else {
8806c247393SAbhilash Kesavan 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
88142b696e8SChanwoo Choi 		con &= ~(1 << EXYNOS7_PD_DET_EN_SHIFT);
8826c247393SAbhilash Kesavan 		interrupt_en = 0; /* Disable all interrupts */
8836c247393SAbhilash Kesavan 	}
8846c247393SAbhilash Kesavan 
8856c247393SAbhilash Kesavan 	writel(interrupt_en, data->base + EXYNOS7_TMU_REG_INTEN);
8866c247393SAbhilash Kesavan 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
8876c247393SAbhilash Kesavan }
8886c247393SAbhilash Kesavan 
88917e8351aSSascha Hauer static int exynos_get_temp(void *p, int *temp)
89059dfa54cSAmit Daniel Kachhap {
8913b6a1a80SLukasz Majewski 	struct exynos_tmu_data *data = p;
8923b6a1a80SLukasz Majewski 
8934531fa16SLukasz Majewski 	if (!data || !data->tmu_read)
8943b6a1a80SLukasz Majewski 		return -EINVAL;
89559dfa54cSAmit Daniel Kachhap 
89659dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
89759dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
8983b6a1a80SLukasz Majewski 
8993b6a1a80SLukasz Majewski 	*temp = code_to_temp(data, data->tmu_read(data)) * MCELSIUS;
9003b6a1a80SLukasz Majewski 
90159dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
90259dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
90359dfa54cSAmit Daniel Kachhap 
9043b6a1a80SLukasz Majewski 	return 0;
90559dfa54cSAmit Daniel Kachhap }
90659dfa54cSAmit Daniel Kachhap 
90759dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
908154013eaSBartlomiej Zolnierkiewicz static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val,
90917e8351aSSascha Hauer 			    int temp)
910154013eaSBartlomiej Zolnierkiewicz {
911154013eaSBartlomiej Zolnierkiewicz 	if (temp) {
912154013eaSBartlomiej Zolnierkiewicz 		temp /= MCELSIUS;
913154013eaSBartlomiej Zolnierkiewicz 
914d564b55aSBartlomiej Zolnierkiewicz 		if (data->soc != SOC_ARCH_EXYNOS5440) {
915154013eaSBartlomiej Zolnierkiewicz 			val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
916154013eaSBartlomiej Zolnierkiewicz 			val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
917154013eaSBartlomiej Zolnierkiewicz 		}
9186c247393SAbhilash Kesavan 		if (data->soc == SOC_ARCH_EXYNOS7) {
9196c247393SAbhilash Kesavan 			val &= ~(EXYNOS7_EMUL_DATA_MASK <<
9206c247393SAbhilash Kesavan 				EXYNOS7_EMUL_DATA_SHIFT);
9216c247393SAbhilash Kesavan 			val |= (temp_to_code(data, temp) <<
9226c247393SAbhilash Kesavan 				EXYNOS7_EMUL_DATA_SHIFT) |
923154013eaSBartlomiej Zolnierkiewicz 				EXYNOS_EMUL_ENABLE;
924154013eaSBartlomiej Zolnierkiewicz 		} else {
9256c247393SAbhilash Kesavan 			val &= ~(EXYNOS_EMUL_DATA_MASK <<
9266c247393SAbhilash Kesavan 				EXYNOS_EMUL_DATA_SHIFT);
9276c247393SAbhilash Kesavan 			val |= (temp_to_code(data, temp) <<
9286c247393SAbhilash Kesavan 				EXYNOS_EMUL_DATA_SHIFT) |
9296c247393SAbhilash Kesavan 				EXYNOS_EMUL_ENABLE;
9306c247393SAbhilash Kesavan 		}
9316c247393SAbhilash Kesavan 	} else {
932154013eaSBartlomiej Zolnierkiewicz 		val &= ~EXYNOS_EMUL_ENABLE;
933154013eaSBartlomiej Zolnierkiewicz 	}
934154013eaSBartlomiej Zolnierkiewicz 
935154013eaSBartlomiej Zolnierkiewicz 	return val;
936154013eaSBartlomiej Zolnierkiewicz }
937154013eaSBartlomiej Zolnierkiewicz 
938285d994aSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data,
93917e8351aSSascha Hauer 					 int temp)
940285d994aSBartlomiej Zolnierkiewicz {
941285d994aSBartlomiej Zolnierkiewicz 	unsigned int val;
942285d994aSBartlomiej Zolnierkiewicz 	u32 emul_con;
943285d994aSBartlomiej Zolnierkiewicz 
944285d994aSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5260)
945285d994aSBartlomiej Zolnierkiewicz 		emul_con = EXYNOS5260_EMUL_CON;
946b28fec13SSudip Mukherjee 	else if (data->soc == SOC_ARCH_EXYNOS5433)
947488c7455SChanwoo Choi 		emul_con = EXYNOS5433_TMU_EMUL_CON;
9486c247393SAbhilash Kesavan 	else if (data->soc == SOC_ARCH_EXYNOS7)
9496c247393SAbhilash Kesavan 		emul_con = EXYNOS7_TMU_REG_EMUL_CON;
950285d994aSBartlomiej Zolnierkiewicz 	else
951285d994aSBartlomiej Zolnierkiewicz 		emul_con = EXYNOS_EMUL_CON;
952285d994aSBartlomiej Zolnierkiewicz 
953285d994aSBartlomiej Zolnierkiewicz 	val = readl(data->base + emul_con);
954285d994aSBartlomiej Zolnierkiewicz 	val = get_emul_con_reg(data, val, temp);
955285d994aSBartlomiej Zolnierkiewicz 	writel(val, data->base + emul_con);
956285d994aSBartlomiej Zolnierkiewicz }
957285d994aSBartlomiej Zolnierkiewicz 
958285d994aSBartlomiej Zolnierkiewicz static void exynos5440_tmu_set_emulation(struct exynos_tmu_data *data,
95917e8351aSSascha Hauer 					 int temp)
960285d994aSBartlomiej Zolnierkiewicz {
961285d994aSBartlomiej Zolnierkiewicz 	unsigned int val;
962285d994aSBartlomiej Zolnierkiewicz 
963285d994aSBartlomiej Zolnierkiewicz 	val = readl(data->base + EXYNOS5440_TMU_S0_7_DEBUG);
964285d994aSBartlomiej Zolnierkiewicz 	val = get_emul_con_reg(data, val, temp);
965285d994aSBartlomiej Zolnierkiewicz 	writel(val, data->base + EXYNOS5440_TMU_S0_7_DEBUG);
966285d994aSBartlomiej Zolnierkiewicz }
967285d994aSBartlomiej Zolnierkiewicz 
96817e8351aSSascha Hauer static int exynos_tmu_set_emulation(void *drv_data, int temp)
96959dfa54cSAmit Daniel Kachhap {
97059dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
97159dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
97259dfa54cSAmit Daniel Kachhap 
973ef3f80fcSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS4210)
97459dfa54cSAmit Daniel Kachhap 		goto out;
97559dfa54cSAmit Daniel Kachhap 
97659dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
97759dfa54cSAmit Daniel Kachhap 		goto out;
97859dfa54cSAmit Daniel Kachhap 
97959dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
98059dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
981285d994aSBartlomiej Zolnierkiewicz 	data->tmu_set_emulation(data, temp);
98259dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
98359dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
98459dfa54cSAmit Daniel Kachhap 	return 0;
98559dfa54cSAmit Daniel Kachhap out:
98659dfa54cSAmit Daniel Kachhap 	return ret;
98759dfa54cSAmit Daniel Kachhap }
98859dfa54cSAmit Daniel Kachhap #else
989285d994aSBartlomiej Zolnierkiewicz #define exynos4412_tmu_set_emulation NULL
990285d994aSBartlomiej Zolnierkiewicz #define exynos5440_tmu_set_emulation NULL
99117e8351aSSascha Hauer static int exynos_tmu_set_emulation(void *drv_data, int temp)
99259dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
99359dfa54cSAmit Daniel Kachhap #endif /* CONFIG_THERMAL_EMULATION */
99459dfa54cSAmit Daniel Kachhap 
995b79985caSBartlomiej Zolnierkiewicz static int exynos4210_tmu_read(struct exynos_tmu_data *data)
996b79985caSBartlomiej Zolnierkiewicz {
997b79985caSBartlomiej Zolnierkiewicz 	int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
998b79985caSBartlomiej Zolnierkiewicz 
999b79985caSBartlomiej Zolnierkiewicz 	/* "temp_code" should range between 75 and 175 */
1000b79985caSBartlomiej Zolnierkiewicz 	return (ret < 75 || ret > 175) ? -ENODATA : ret;
1001b79985caSBartlomiej Zolnierkiewicz }
1002b79985caSBartlomiej Zolnierkiewicz 
1003b79985caSBartlomiej Zolnierkiewicz static int exynos4412_tmu_read(struct exynos_tmu_data *data)
1004b79985caSBartlomiej Zolnierkiewicz {
1005b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
1006b79985caSBartlomiej Zolnierkiewicz }
1007b79985caSBartlomiej Zolnierkiewicz 
1008b79985caSBartlomiej Zolnierkiewicz static int exynos5440_tmu_read(struct exynos_tmu_data *data)
1009b79985caSBartlomiej Zolnierkiewicz {
1010b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS5440_TMU_S0_7_TEMP);
1011b79985caSBartlomiej Zolnierkiewicz }
1012b79985caSBartlomiej Zolnierkiewicz 
10136c247393SAbhilash Kesavan static int exynos7_tmu_read(struct exynos_tmu_data *data)
10146c247393SAbhilash Kesavan {
10156c247393SAbhilash Kesavan 	return readw(data->base + EXYNOS_TMU_REG_CURRENT_TEMP) &
10166c247393SAbhilash Kesavan 		EXYNOS7_TMU_TEMP_MASK;
10176c247393SAbhilash Kesavan }
10186c247393SAbhilash Kesavan 
101959dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
102059dfa54cSAmit Daniel Kachhap {
102159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
102259dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
1023b835ced1SBartlomiej Zolnierkiewicz 	unsigned int val_type;
1024a0395eeeSAmit Daniel Kachhap 
102514a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
102614a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
1027a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
1028421d5d12SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5440) {
1029421d5d12SBartlomiej Zolnierkiewicz 		val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS);
1030a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
1031a0395eeeSAmit Daniel Kachhap 			goto out;
1032a0395eeeSAmit Daniel Kachhap 	}
103314a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
103414a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
103559dfa54cSAmit Daniel Kachhap 
10363b6a1a80SLukasz Majewski 	exynos_report_trigger(data);
103759dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
103859dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
1039b8d582b9SAmit Daniel Kachhap 
1040a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
1041a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
1042b8d582b9SAmit Daniel Kachhap 
104359dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
104459dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
1045a0395eeeSAmit Daniel Kachhap out:
104659dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
104759dfa54cSAmit Daniel Kachhap }
104859dfa54cSAmit Daniel Kachhap 
1049a7331f72SBartlomiej Zolnierkiewicz static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data)
1050a7331f72SBartlomiej Zolnierkiewicz {
1051a7331f72SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
1052a7331f72SBartlomiej Zolnierkiewicz 	u32 tmu_intstat, tmu_intclear;
1053a7331f72SBartlomiej Zolnierkiewicz 
1054a7331f72SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5260) {
1055a7331f72SBartlomiej Zolnierkiewicz 		tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT;
1056a7331f72SBartlomiej Zolnierkiewicz 		tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR;
10576c247393SAbhilash Kesavan 	} else if (data->soc == SOC_ARCH_EXYNOS7) {
10586c247393SAbhilash Kesavan 		tmu_intstat = EXYNOS7_TMU_REG_INTPEND;
10596c247393SAbhilash Kesavan 		tmu_intclear = EXYNOS7_TMU_REG_INTPEND;
1060488c7455SChanwoo Choi 	} else if (data->soc == SOC_ARCH_EXYNOS5433) {
1061488c7455SChanwoo Choi 		tmu_intstat = EXYNOS5433_TMU_REG_INTPEND;
1062488c7455SChanwoo Choi 		tmu_intclear = EXYNOS5433_TMU_REG_INTPEND;
1063a7331f72SBartlomiej Zolnierkiewicz 	} else {
1064a7331f72SBartlomiej Zolnierkiewicz 		tmu_intstat = EXYNOS_TMU_REG_INTSTAT;
1065a7331f72SBartlomiej Zolnierkiewicz 		tmu_intclear = EXYNOS_TMU_REG_INTCLEAR;
1066a7331f72SBartlomiej Zolnierkiewicz 	}
1067a7331f72SBartlomiej Zolnierkiewicz 
1068a7331f72SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + tmu_intstat);
1069a7331f72SBartlomiej Zolnierkiewicz 	/*
1070a7331f72SBartlomiej Zolnierkiewicz 	 * Clear the interrupts.  Please note that the documentation for
1071a7331f72SBartlomiej Zolnierkiewicz 	 * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
1072a7331f72SBartlomiej Zolnierkiewicz 	 * states that INTCLEAR register has a different placing of bits
1073a7331f72SBartlomiej Zolnierkiewicz 	 * responsible for FALL IRQs than INTSTAT register.  Exynos5420
1074a7331f72SBartlomiej Zolnierkiewicz 	 * and Exynos5440 documentation is correct (Exynos4210 doesn't
1075a7331f72SBartlomiej Zolnierkiewicz 	 * support FALL IRQs at all).
1076a7331f72SBartlomiej Zolnierkiewicz 	 */
1077a7331f72SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + tmu_intclear);
1078a7331f72SBartlomiej Zolnierkiewicz }
1079a7331f72SBartlomiej Zolnierkiewicz 
1080a7331f72SBartlomiej Zolnierkiewicz static void exynos5440_tmu_clear_irqs(struct exynos_tmu_data *data)
1081a7331f72SBartlomiej Zolnierkiewicz {
1082a7331f72SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
1083a7331f72SBartlomiej Zolnierkiewicz 
1084a7331f72SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + EXYNOS5440_TMU_S0_7_IRQ);
1085a7331f72SBartlomiej Zolnierkiewicz 	/* clear the interrupts */
1086a7331f72SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + EXYNOS5440_TMU_S0_7_IRQ);
1087a7331f72SBartlomiej Zolnierkiewicz }
1088a7331f72SBartlomiej Zolnierkiewicz 
108959dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
109059dfa54cSAmit Daniel Kachhap {
109159dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
109259dfa54cSAmit Daniel Kachhap 
109359dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
109459dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
109559dfa54cSAmit Daniel Kachhap 
109659dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
109759dfa54cSAmit Daniel Kachhap }
109859dfa54cSAmit Daniel Kachhap 
109959dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
1100b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos3250-tmu", },
1101b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos4210-tmu", },
1102b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos4412-tmu", },
1103b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5250-tmu", },
1104b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5260-tmu", },
1105b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5420-tmu", },
1106b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5420-tmu-ext-triminfo", },
1107488c7455SChanwoo Choi 	{ .compatible = "samsung,exynos5433-tmu", },
1108b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos5440-tmu", },
1109b71d399cSChanwoo Choi 	{ .compatible = "samsung,exynos7-tmu", },
1110b71d399cSChanwoo Choi 	{ /* sentinel */ },
111159dfa54cSAmit Daniel Kachhap };
111259dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
111359dfa54cSAmit Daniel Kachhap 
11143b6a1a80SLukasz Majewski static int exynos_of_get_soc_type(struct device_node *np)
111559dfa54cSAmit Daniel Kachhap {
11163b6a1a80SLukasz Majewski 	if (of_device_is_compatible(np, "samsung,exynos3250-tmu"))
11173b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS3250;
11183b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos4210-tmu"))
11193b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS4210;
11203b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos4412-tmu"))
11213b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS4412;
11223b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5250-tmu"))
11233b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5250;
11243b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5260-tmu"))
11253b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5260;
11263b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5420-tmu"))
11273b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5420;
11283b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np,
11293b6a1a80SLukasz Majewski 					 "samsung,exynos5420-tmu-ext-triminfo"))
11303b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5420_TRIMINFO;
1131488c7455SChanwoo Choi 	else if (of_device_is_compatible(np, "samsung,exynos5433-tmu"))
1132488c7455SChanwoo Choi 		return SOC_ARCH_EXYNOS5433;
11333b6a1a80SLukasz Majewski 	else if (of_device_is_compatible(np, "samsung,exynos5440-tmu"))
11343b6a1a80SLukasz Majewski 		return SOC_ARCH_EXYNOS5440;
11356c247393SAbhilash Kesavan 	else if (of_device_is_compatible(np, "samsung,exynos7-tmu"))
11366c247393SAbhilash Kesavan 		return SOC_ARCH_EXYNOS7;
113773b5b1d7SSachin Kamat 
11383b6a1a80SLukasz Majewski 	return -EINVAL;
11393b6a1a80SLukasz Majewski }
11403b6a1a80SLukasz Majewski 
11413b6a1a80SLukasz Majewski static int exynos_of_sensor_conf(struct device_node *np,
11423b6a1a80SLukasz Majewski 				 struct exynos_tmu_platform_data *pdata)
11433b6a1a80SLukasz Majewski {
11443b6a1a80SLukasz Majewski 	u32 value;
11453b6a1a80SLukasz Majewski 	int ret;
11463b6a1a80SLukasz Majewski 
11473b6a1a80SLukasz Majewski 	of_node_get(np);
11483b6a1a80SLukasz Majewski 
11493b6a1a80SLukasz Majewski 	ret = of_property_read_u32(np, "samsung,tmu_gain", &value);
11503b6a1a80SLukasz Majewski 	pdata->gain = (u8)value;
11513b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_reference_voltage", &value);
11523b6a1a80SLukasz Majewski 	pdata->reference_voltage = (u8)value;
11533b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_noise_cancel_mode", &value);
11543b6a1a80SLukasz Majewski 	pdata->noise_cancel_mode = (u8)value;
11553b6a1a80SLukasz Majewski 
11563b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_efuse_value",
11573b6a1a80SLukasz Majewski 			     &pdata->efuse_value);
11583b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_min_efuse_value",
11593b6a1a80SLukasz Majewski 			     &pdata->min_efuse_value);
11603b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_max_efuse_value",
11613b6a1a80SLukasz Majewski 			     &pdata->max_efuse_value);
11623b6a1a80SLukasz Majewski 
11633b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_first_point_trim", &value);
11643b6a1a80SLukasz Majewski 	pdata->first_point_trim = (u8)value;
11653b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_second_point_trim", &value);
11663b6a1a80SLukasz Majewski 	pdata->second_point_trim = (u8)value;
11673b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_default_temp_offset", &value);
11683b6a1a80SLukasz Majewski 	pdata->default_temp_offset = (u8)value;
11693b6a1a80SLukasz Majewski 
11703b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_cal_type", &pdata->cal_type);
11713b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_cal_mode", &pdata->cal_mode);
11723b6a1a80SLukasz Majewski 
11733b6a1a80SLukasz Majewski 	of_node_put(np);
11743b6a1a80SLukasz Majewski 	return 0;
117559dfa54cSAmit Daniel Kachhap }
117659dfa54cSAmit Daniel Kachhap 
1177cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
117859dfa54cSAmit Daniel Kachhap {
1179cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
1180cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
1181cebe7373SAmit Daniel Kachhap 	struct resource res;
118259dfa54cSAmit Daniel Kachhap 
118373b5b1d7SSachin Kamat 	if (!data || !pdev->dev.of_node)
1184cebe7373SAmit Daniel Kachhap 		return -ENODEV;
118559dfa54cSAmit Daniel Kachhap 
1186cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
1187cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
1188cebe7373SAmit Daniel Kachhap 		data->id = 0;
1189cebe7373SAmit Daniel Kachhap 
1190cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1191cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
1192cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
1193cebe7373SAmit Daniel Kachhap 		return -ENODEV;
1194cebe7373SAmit Daniel Kachhap 	}
1195cebe7373SAmit Daniel Kachhap 
1196cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
1197cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
1198cebe7373SAmit Daniel Kachhap 		return -ENODEV;
1199cebe7373SAmit Daniel Kachhap 	}
1200cebe7373SAmit Daniel Kachhap 
1201cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
1202cebe7373SAmit Daniel Kachhap 	if (!data->base) {
1203cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
1204cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
1205cebe7373SAmit Daniel Kachhap 	}
1206cebe7373SAmit Daniel Kachhap 
12073b6a1a80SLukasz Majewski 	pdata = devm_kzalloc(&pdev->dev,
12083b6a1a80SLukasz Majewski 			     sizeof(struct exynos_tmu_platform_data),
12093b6a1a80SLukasz Majewski 			     GFP_KERNEL);
12103b6a1a80SLukasz Majewski 	if (!pdata)
12113b6a1a80SLukasz Majewski 		return -ENOMEM;
121256adb9efSBartlomiej Zolnierkiewicz 
12133b6a1a80SLukasz Majewski 	exynos_of_sensor_conf(pdev->dev.of_node, pdata);
1214cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
12153b6a1a80SLukasz Majewski 	data->soc = exynos_of_get_soc_type(pdev->dev.of_node);
121656adb9efSBartlomiej Zolnierkiewicz 
121756adb9efSBartlomiej Zolnierkiewicz 	switch (data->soc) {
121856adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4210:
121956adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4210_tmu_initialize;
122056adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
122156adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4210_tmu_read;
122256adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12233a3a5f15SKrzysztof Kozlowski 		data->ntrip = 4;
122456adb9efSBartlomiej Zolnierkiewicz 		break;
122556adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS3250:
122656adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4412:
122756adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5250:
122856adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5260:
122956adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420:
123056adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420_TRIMINFO:
123156adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4412_tmu_initialize;
123256adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
123356adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4412_tmu_read;
123456adb9efSBartlomiej Zolnierkiewicz 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
123556adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12363a3a5f15SKrzysztof Kozlowski 		data->ntrip = 4;
123756adb9efSBartlomiej Zolnierkiewicz 		break;
1238488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS5433:
1239488c7455SChanwoo Choi 		data->tmu_initialize = exynos5433_tmu_initialize;
1240488c7455SChanwoo Choi 		data->tmu_control = exynos5433_tmu_control;
1241488c7455SChanwoo Choi 		data->tmu_read = exynos4412_tmu_read;
1242488c7455SChanwoo Choi 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
1243488c7455SChanwoo Choi 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12443a3a5f15SKrzysztof Kozlowski 		data->ntrip = 8;
1245488c7455SChanwoo Choi 		break;
124656adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5440:
124756adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos5440_tmu_initialize;
124856adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos5440_tmu_control;
124956adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos5440_tmu_read;
125056adb9efSBartlomiej Zolnierkiewicz 		data->tmu_set_emulation = exynos5440_tmu_set_emulation;
125156adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos5440_tmu_clear_irqs;
12523a3a5f15SKrzysztof Kozlowski 		data->ntrip = 4;
125356adb9efSBartlomiej Zolnierkiewicz 		break;
12546c247393SAbhilash Kesavan 	case SOC_ARCH_EXYNOS7:
12556c247393SAbhilash Kesavan 		data->tmu_initialize = exynos7_tmu_initialize;
12566c247393SAbhilash Kesavan 		data->tmu_control = exynos7_tmu_control;
12576c247393SAbhilash Kesavan 		data->tmu_read = exynos7_tmu_read;
12586c247393SAbhilash Kesavan 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
12596c247393SAbhilash Kesavan 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12603a3a5f15SKrzysztof Kozlowski 		data->ntrip = 8;
12616c247393SAbhilash Kesavan 		break;
126256adb9efSBartlomiej Zolnierkiewicz 	default:
126356adb9efSBartlomiej Zolnierkiewicz 		dev_err(&pdev->dev, "Platform not supported\n");
126456adb9efSBartlomiej Zolnierkiewicz 		return -EINVAL;
126556adb9efSBartlomiej Zolnierkiewicz 	}
126656adb9efSBartlomiej Zolnierkiewicz 
1267d9b6ee14SAmit Daniel Kachhap 	/*
1268d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
1269d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
1270d9b6ee14SAmit Daniel Kachhap 	 */
127156adb9efSBartlomiej Zolnierkiewicz 	if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO &&
127256adb9efSBartlomiej Zolnierkiewicz 	    data->soc != SOC_ARCH_EXYNOS5440)
1273d9b6ee14SAmit Daniel Kachhap 		return 0;
1274d9b6ee14SAmit Daniel Kachhap 
1275d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
1276d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
1277d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
1278d9b6ee14SAmit Daniel Kachhap 	}
1279d9b6ee14SAmit Daniel Kachhap 
12809025d563SNaveen Krishna Chatradhi 	data->base_second = devm_ioremap(&pdev->dev, res.start,
1281d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
12829025d563SNaveen Krishna Chatradhi 	if (!data->base_second) {
1283d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
1284d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
1285d9b6ee14SAmit Daniel Kachhap 	}
1286cebe7373SAmit Daniel Kachhap 
1287cebe7373SAmit Daniel Kachhap 	return 0;
1288cebe7373SAmit Daniel Kachhap }
1289cebe7373SAmit Daniel Kachhap 
12903b6a1a80SLukasz Majewski static struct thermal_zone_of_device_ops exynos_sensor_ops = {
12913b6a1a80SLukasz Majewski 	.get_temp = exynos_get_temp,
12923b6a1a80SLukasz Majewski 	.set_emul_temp = exynos_tmu_set_emulation,
12933b6a1a80SLukasz Majewski };
12943b6a1a80SLukasz Majewski 
1295cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
1296cebe7373SAmit Daniel Kachhap {
12973b6a1a80SLukasz Majewski 	struct exynos_tmu_data *data;
12983b6a1a80SLukasz Majewski 	int ret;
1299cebe7373SAmit Daniel Kachhap 
130059dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
130159dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
13022a9675b3SJingoo Han 	if (!data)
130359dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
130459dfa54cSAmit Daniel Kachhap 
1305cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
1306cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
1307cebe7373SAmit Daniel Kachhap 
1308824ead03SKrzysztof Kozlowski 	/*
1309824ead03SKrzysztof Kozlowski 	 * Try enabling the regulator if found
1310824ead03SKrzysztof Kozlowski 	 * TODO: Add regulator as an SOC feature, so that regulator enable
1311824ead03SKrzysztof Kozlowski 	 * is a compulsory call.
1312824ead03SKrzysztof Kozlowski 	 */
13134d3583cdSJavier Martinez Canillas 	data->regulator = devm_regulator_get_optional(&pdev->dev, "vtmu");
1314824ead03SKrzysztof Kozlowski 	if (!IS_ERR(data->regulator)) {
1315824ead03SKrzysztof Kozlowski 		ret = regulator_enable(data->regulator);
1316824ead03SKrzysztof Kozlowski 		if (ret) {
1317824ead03SKrzysztof Kozlowski 			dev_err(&pdev->dev, "failed to enable vtmu\n");
1318824ead03SKrzysztof Kozlowski 			return ret;
13193b6a1a80SLukasz Majewski 		}
1320824ead03SKrzysztof Kozlowski 	} else {
1321*ccb361d2SJavier Martinez Canillas 		if (PTR_ERR(data->regulator) == -EPROBE_DEFER)
1322*ccb361d2SJavier Martinez Canillas 			return -EPROBE_DEFER;
1323824ead03SKrzysztof Kozlowski 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
1324824ead03SKrzysztof Kozlowski 	}
1325824ead03SKrzysztof Kozlowski 
1326cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
1327cebe7373SAmit Daniel Kachhap 	if (ret)
13283b6a1a80SLukasz Majewski 		goto err_sensor;
1329cebe7373SAmit Daniel Kachhap 
133059dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
133159dfa54cSAmit Daniel Kachhap 
133259dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
133359dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
133459dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
13353b6a1a80SLukasz Majewski 		ret = PTR_ERR(data->clk);
13363b6a1a80SLukasz Majewski 		goto err_sensor;
133759dfa54cSAmit Daniel Kachhap 	}
133859dfa54cSAmit Daniel Kachhap 
133914a11dc7SNaveen Krishna Chatradhi 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
134014a11dc7SNaveen Krishna Chatradhi 	if (IS_ERR(data->clk_sec)) {
134114a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
134214a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
13433b6a1a80SLukasz Majewski 			ret = PTR_ERR(data->clk_sec);
13443b6a1a80SLukasz Majewski 			goto err_sensor;
134514a11dc7SNaveen Krishna Chatradhi 		}
134614a11dc7SNaveen Krishna Chatradhi 	} else {
134714a11dc7SNaveen Krishna Chatradhi 		ret = clk_prepare(data->clk_sec);
134814a11dc7SNaveen Krishna Chatradhi 		if (ret) {
134914a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get clock\n");
13503b6a1a80SLukasz Majewski 			goto err_sensor;
135114a11dc7SNaveen Krishna Chatradhi 		}
135214a11dc7SNaveen Krishna Chatradhi 	}
135314a11dc7SNaveen Krishna Chatradhi 
135414a11dc7SNaveen Krishna Chatradhi 	ret = clk_prepare(data->clk);
135514a11dc7SNaveen Krishna Chatradhi 	if (ret) {
135614a11dc7SNaveen Krishna Chatradhi 		dev_err(&pdev->dev, "Failed to get clock\n");
135714a11dc7SNaveen Krishna Chatradhi 		goto err_clk_sec;
135814a11dc7SNaveen Krishna Chatradhi 	}
135959dfa54cSAmit Daniel Kachhap 
1360488c7455SChanwoo Choi 	switch (data->soc) {
1361488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS5433:
1362488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS7:
13636c247393SAbhilash Kesavan 		data->sclk = devm_clk_get(&pdev->dev, "tmu_sclk");
13646c247393SAbhilash Kesavan 		if (IS_ERR(data->sclk)) {
13656c247393SAbhilash Kesavan 			dev_err(&pdev->dev, "Failed to get sclk\n");
13666c247393SAbhilash Kesavan 			goto err_clk;
13676c247393SAbhilash Kesavan 		} else {
13686c247393SAbhilash Kesavan 			ret = clk_prepare_enable(data->sclk);
13696c247393SAbhilash Kesavan 			if (ret) {
13706c247393SAbhilash Kesavan 				dev_err(&pdev->dev, "Failed to enable sclk\n");
13716c247393SAbhilash Kesavan 				goto err_clk;
13726c247393SAbhilash Kesavan 			}
13736c247393SAbhilash Kesavan 		}
1374488c7455SChanwoo Choi 		break;
1375488c7455SChanwoo Choi 	default:
1376488c7455SChanwoo Choi 		break;
1377baba1ebbSKrzysztof Kozlowski 	}
13786c247393SAbhilash Kesavan 
13799e4249b4SKrzysztof Kozlowski 	/*
13809e4249b4SKrzysztof Kozlowski 	 * data->tzd must be registered before calling exynos_tmu_initialize(),
13819e4249b4SKrzysztof Kozlowski 	 * requesting irq and calling exynos_tmu_control().
13829e4249b4SKrzysztof Kozlowski 	 */
13839e4249b4SKrzysztof Kozlowski 	data->tzd = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
13849e4249b4SKrzysztof Kozlowski 						    &exynos_sensor_ops);
13859e4249b4SKrzysztof Kozlowski 	if (IS_ERR(data->tzd)) {
13869e4249b4SKrzysztof Kozlowski 		ret = PTR_ERR(data->tzd);
13879e4249b4SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to register sensor: %d\n", ret);
13889e4249b4SKrzysztof Kozlowski 		goto err_sclk;
13899e4249b4SKrzysztof Kozlowski 	}
139059dfa54cSAmit Daniel Kachhap 
139159dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
139259dfa54cSAmit Daniel Kachhap 	if (ret) {
139359dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
13949e4249b4SKrzysztof Kozlowski 		goto err_thermal;
139559dfa54cSAmit Daniel Kachhap 	}
139659dfa54cSAmit Daniel Kachhap 
1397cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
1398cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
1399cebe7373SAmit Daniel Kachhap 	if (ret) {
1400cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
14019e4249b4SKrzysztof Kozlowski 		goto err_thermal;
1402cebe7373SAmit Daniel Kachhap 	}
140359dfa54cSAmit Daniel Kachhap 
14043b6a1a80SLukasz Majewski 	exynos_tmu_control(pdev, true);
140559dfa54cSAmit Daniel Kachhap 	return 0;
14069e4249b4SKrzysztof Kozlowski 
14079e4249b4SKrzysztof Kozlowski err_thermal:
14089e4249b4SKrzysztof Kozlowski 	thermal_zone_of_sensor_unregister(&pdev->dev, data->tzd);
14096c247393SAbhilash Kesavan err_sclk:
14106c247393SAbhilash Kesavan 	clk_disable_unprepare(data->sclk);
141159dfa54cSAmit Daniel Kachhap err_clk:
141259dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
141314a11dc7SNaveen Krishna Chatradhi err_clk_sec:
141414a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
141514a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
14163b6a1a80SLukasz Majewski err_sensor:
1417bfa26838SKrzysztof Kozlowski 	if (!IS_ERR(data->regulator))
14185f09a5cbSKrzysztof Kozlowski 		regulator_disable(data->regulator);
14193b6a1a80SLukasz Majewski 
142059dfa54cSAmit Daniel Kachhap 	return ret;
142159dfa54cSAmit Daniel Kachhap }
142259dfa54cSAmit Daniel Kachhap 
142359dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
142459dfa54cSAmit Daniel Kachhap {
142559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
14263b6a1a80SLukasz Majewski 	struct thermal_zone_device *tzd = data->tzd;
142759dfa54cSAmit Daniel Kachhap 
14283b6a1a80SLukasz Majewski 	thermal_zone_of_sensor_unregister(&pdev->dev, tzd);
14294215688eSBartlomiej Zolnierkiewicz 	exynos_tmu_control(pdev, false);
14304215688eSBartlomiej Zolnierkiewicz 
14316c247393SAbhilash Kesavan 	clk_disable_unprepare(data->sclk);
143259dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
143314a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
143414a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
143559dfa54cSAmit Daniel Kachhap 
1436498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator))
1437498d22f6SAmit Daniel Kachhap 		regulator_disable(data->regulator);
1438498d22f6SAmit Daniel Kachhap 
143959dfa54cSAmit Daniel Kachhap 	return 0;
144059dfa54cSAmit Daniel Kachhap }
144159dfa54cSAmit Daniel Kachhap 
144259dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
144359dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
144459dfa54cSAmit Daniel Kachhap {
144559dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
144659dfa54cSAmit Daniel Kachhap 
144759dfa54cSAmit Daniel Kachhap 	return 0;
144859dfa54cSAmit Daniel Kachhap }
144959dfa54cSAmit Daniel Kachhap 
145059dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
145159dfa54cSAmit Daniel Kachhap {
145259dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
145359dfa54cSAmit Daniel Kachhap 
145459dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
145559dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
145659dfa54cSAmit Daniel Kachhap 
145759dfa54cSAmit Daniel Kachhap 	return 0;
145859dfa54cSAmit Daniel Kachhap }
145959dfa54cSAmit Daniel Kachhap 
146059dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
146159dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
146259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
146359dfa54cSAmit Daniel Kachhap #else
146459dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
146559dfa54cSAmit Daniel Kachhap #endif
146659dfa54cSAmit Daniel Kachhap 
146759dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
146859dfa54cSAmit Daniel Kachhap 	.driver = {
146959dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
147059dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
147173b5b1d7SSachin Kamat 		.of_match_table = exynos_tmu_match,
147259dfa54cSAmit Daniel Kachhap 	},
147359dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
147459dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
147559dfa54cSAmit Daniel Kachhap };
147659dfa54cSAmit Daniel Kachhap 
147759dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
147859dfa54cSAmit Daniel Kachhap 
147959dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
148059dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
148159dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
148259dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
1483