xref: /linux/drivers/thermal/samsung/exynos_tmu.c (revision e3ed36499bc95658c28557c0f4a6364f30e51bd0)
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>
32fee88e2bSMaciej Purski #include <linux/of_device.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 
168718b4ca1SBartlomiej Zolnierkiewicz #define EXYNOS_FIRST_POINT_TRIM			25
169718b4ca1SBartlomiej Zolnierkiewicz #define EXYNOS_SECOND_POINT_TRIM		85
170718b4ca1SBartlomiej Zolnierkiewicz 
17109d29426SBartlomiej Zolnierkiewicz #define EXYNOS_NOISE_CANCEL_MODE		4
17209d29426SBartlomiej Zolnierkiewicz 
1733b6a1a80SLukasz Majewski #define MCELSIUS	1000
174cebe7373SAmit Daniel Kachhap /**
175cebe7373SAmit Daniel Kachhap  * struct exynos_tmu_data : A structure to hold the private data of the TMU
176cebe7373SAmit Daniel Kachhap 	driver
177cebe7373SAmit Daniel Kachhap  * @id: identifier of the one instance of the TMU controller.
178cebe7373SAmit Daniel Kachhap  * @pdata: pointer to the tmu platform/configuration data
179cebe7373SAmit Daniel Kachhap  * @base: base address of the single instance of the TMU controller.
1809025d563SNaveen Krishna Chatradhi  * @base_second: base address of the common registers of the TMU controller.
181cebe7373SAmit Daniel Kachhap  * @irq: irq number of the TMU controller.
182cebe7373SAmit Daniel Kachhap  * @soc: id of the SOC type.
183cebe7373SAmit Daniel Kachhap  * @irq_work: pointer to the irq work structure.
184cebe7373SAmit Daniel Kachhap  * @lock: lock to implement synchronization.
185cebe7373SAmit Daniel Kachhap  * @clk: pointer to the clock structure.
18614a11dc7SNaveen Krishna Chatradhi  * @clk_sec: pointer to the clock structure for accessing the base_second.
1876c247393SAbhilash Kesavan  * @sclk: pointer to the clock structure for accessing the tmu special clk.
188*e3ed3649SBartlomiej Zolnierkiewicz  * @efuse_value: SoC defined fuse value
189*e3ed3649SBartlomiej Zolnierkiewicz  * @min_efuse_value: minimum valid trimming data
190*e3ed3649SBartlomiej Zolnierkiewicz  * @max_efuse_value: maximum valid trimming data
191cebe7373SAmit Daniel Kachhap  * @temp_error1: fused value of the first point trim.
192cebe7373SAmit Daniel Kachhap  * @temp_error2: fused value of the second point trim.
193498d22f6SAmit Daniel Kachhap  * @regulator: pointer to the TMU regulator structure.
194cebe7373SAmit Daniel Kachhap  * @reg_conf: pointer to structure to register with core thermal.
1953a3a5f15SKrzysztof Kozlowski  * @ntrip: number of supported trip points.
1960eb875d8SMarek Szyprowski  * @enabled: current status of TMU device
19772d1100bSBartlomiej Zolnierkiewicz  * @tmu_initialize: SoC specific TMU initialization method
19837f9034fSBartlomiej Zolnierkiewicz  * @tmu_control: SoC specific TMU control method
199b79985caSBartlomiej Zolnierkiewicz  * @tmu_read: SoC specific TMU temperature read method
200285d994aSBartlomiej Zolnierkiewicz  * @tmu_set_emulation: SoC specific TMU emulation setting method
201a7331f72SBartlomiej Zolnierkiewicz  * @tmu_clear_irqs: SoC specific TMU interrupts clearing method
202cebe7373SAmit Daniel Kachhap  */
20359dfa54cSAmit Daniel Kachhap struct exynos_tmu_data {
204cebe7373SAmit Daniel Kachhap 	int id;
20559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
20659dfa54cSAmit Daniel Kachhap 	void __iomem *base;
2079025d563SNaveen Krishna Chatradhi 	void __iomem *base_second;
20859dfa54cSAmit Daniel Kachhap 	int irq;
20959dfa54cSAmit Daniel Kachhap 	enum soc_type soc;
21059dfa54cSAmit Daniel Kachhap 	struct work_struct irq_work;
21159dfa54cSAmit Daniel Kachhap 	struct mutex lock;
2126c247393SAbhilash Kesavan 	struct clk *clk, *clk_sec, *sclk;
213*e3ed3649SBartlomiej Zolnierkiewicz 	u32 efuse_value;
214*e3ed3649SBartlomiej Zolnierkiewicz 	u32 min_efuse_value;
215*e3ed3649SBartlomiej Zolnierkiewicz 	u32 max_efuse_value;
2166c247393SAbhilash Kesavan 	u16 temp_error1, temp_error2;
217498d22f6SAmit Daniel Kachhap 	struct regulator *regulator;
2183b6a1a80SLukasz Majewski 	struct thermal_zone_device *tzd;
2193a3a5f15SKrzysztof Kozlowski 	unsigned int ntrip;
2200eb875d8SMarek Szyprowski 	bool enabled;
2213b6a1a80SLukasz Majewski 
22272d1100bSBartlomiej Zolnierkiewicz 	int (*tmu_initialize)(struct platform_device *pdev);
22337f9034fSBartlomiej Zolnierkiewicz 	void (*tmu_control)(struct platform_device *pdev, bool on);
224b79985caSBartlomiej Zolnierkiewicz 	int (*tmu_read)(struct exynos_tmu_data *data);
22517e8351aSSascha Hauer 	void (*tmu_set_emulation)(struct exynos_tmu_data *data, int temp);
226a7331f72SBartlomiej Zolnierkiewicz 	void (*tmu_clear_irqs)(struct exynos_tmu_data *data);
22759dfa54cSAmit Daniel Kachhap };
22859dfa54cSAmit Daniel Kachhap 
2293b6a1a80SLukasz Majewski static void exynos_report_trigger(struct exynos_tmu_data *p)
2303b6a1a80SLukasz Majewski {
2313b6a1a80SLukasz Majewski 	char data[10], *envp[] = { data, NULL };
2323b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = p->tzd;
23317e8351aSSascha Hauer 	int temp;
2343b6a1a80SLukasz Majewski 	unsigned int i;
2353b6a1a80SLukasz Majewski 
236eccb6014SLukasz Majewski 	if (!tz) {
237eccb6014SLukasz Majewski 		pr_err("No thermal zone device defined\n");
2383b6a1a80SLukasz Majewski 		return;
2393b6a1a80SLukasz Majewski 	}
2403b6a1a80SLukasz Majewski 
2410e70f466SSrinivas Pandruvada 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
2423b6a1a80SLukasz Majewski 
2433b6a1a80SLukasz Majewski 	mutex_lock(&tz->lock);
2443b6a1a80SLukasz Majewski 	/* Find the level for which trip happened */
2453b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
2463b6a1a80SLukasz Majewski 		tz->ops->get_trip_temp(tz, i, &temp);
2473b6a1a80SLukasz Majewski 		if (tz->last_temperature < temp)
2483b6a1a80SLukasz Majewski 			break;
2493b6a1a80SLukasz Majewski 	}
2503b6a1a80SLukasz Majewski 
2513b6a1a80SLukasz Majewski 	snprintf(data, sizeof(data), "%u", i);
2523b6a1a80SLukasz Majewski 	kobject_uevent_env(&tz->device.kobj, KOBJ_CHANGE, envp);
2533b6a1a80SLukasz Majewski 	mutex_unlock(&tz->lock);
2543b6a1a80SLukasz Majewski }
2553b6a1a80SLukasz Majewski 
25659dfa54cSAmit Daniel Kachhap /*
25759dfa54cSAmit Daniel Kachhap  * TMU treats temperature as a mapped temperature code.
25859dfa54cSAmit Daniel Kachhap  * The temperature is converted differently depending on the calibration type.
25959dfa54cSAmit Daniel Kachhap  */
26059dfa54cSAmit Daniel Kachhap static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
26159dfa54cSAmit Daniel Kachhap {
26259dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
26359dfa54cSAmit Daniel Kachhap 
2649c933b1bSBartlomiej Zolnierkiewicz 	if (pdata->cal_type == TYPE_ONE_POINT_TRIMMING)
265718b4ca1SBartlomiej Zolnierkiewicz 		return temp + data->temp_error1 - EXYNOS_FIRST_POINT_TRIM;
2669c933b1bSBartlomiej Zolnierkiewicz 
267718b4ca1SBartlomiej Zolnierkiewicz 	return (temp - EXYNOS_FIRST_POINT_TRIM) *
26859dfa54cSAmit Daniel Kachhap 		(data->temp_error2 - data->temp_error1) /
269718b4ca1SBartlomiej Zolnierkiewicz 		(EXYNOS_SECOND_POINT_TRIM - EXYNOS_FIRST_POINT_TRIM) +
270bb34b4c8SAmit Daniel Kachhap 		data->temp_error1;
27159dfa54cSAmit Daniel Kachhap }
27259dfa54cSAmit Daniel Kachhap 
27359dfa54cSAmit Daniel Kachhap /*
27459dfa54cSAmit Daniel Kachhap  * Calculate a temperature value from a temperature code.
27559dfa54cSAmit Daniel Kachhap  * The unit of the temperature is degree Celsius.
27659dfa54cSAmit Daniel Kachhap  */
2776c247393SAbhilash Kesavan static int code_to_temp(struct exynos_tmu_data *data, u16 temp_code)
27859dfa54cSAmit Daniel Kachhap {
27959dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata = data->pdata;
28059dfa54cSAmit Daniel Kachhap 
2819c933b1bSBartlomiej Zolnierkiewicz 	if (pdata->cal_type == TYPE_ONE_POINT_TRIMMING)
282718b4ca1SBartlomiej Zolnierkiewicz 		return temp_code - data->temp_error1 + EXYNOS_FIRST_POINT_TRIM;
2839c933b1bSBartlomiej Zolnierkiewicz 
2849c933b1bSBartlomiej Zolnierkiewicz 	return (temp_code - data->temp_error1) *
285718b4ca1SBartlomiej Zolnierkiewicz 		(EXYNOS_SECOND_POINT_TRIM - EXYNOS_FIRST_POINT_TRIM) /
286bb34b4c8SAmit Daniel Kachhap 		(data->temp_error2 - data->temp_error1) +
287718b4ca1SBartlomiej Zolnierkiewicz 		EXYNOS_FIRST_POINT_TRIM;
28859dfa54cSAmit Daniel Kachhap }
28959dfa54cSAmit Daniel Kachhap 
2908328a4b1SBartlomiej Zolnierkiewicz static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
291b835ced1SBartlomiej Zolnierkiewicz {
292b8d582b9SAmit Daniel Kachhap 	data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
29399d67fb9SBartlomiej Zolnierkiewicz 	data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
294b8d582b9SAmit Daniel Kachhap 				EXYNOS_TMU_TEMP_MASK);
29559dfa54cSAmit Daniel Kachhap 
2965000806cSAmit Daniel Kachhap 	if (!data->temp_error1 ||
297*e3ed3649SBartlomiej Zolnierkiewicz 	    (data->min_efuse_value > data->temp_error1) ||
298*e3ed3649SBartlomiej Zolnierkiewicz 	    (data->temp_error1 > data->max_efuse_value))
299*e3ed3649SBartlomiej Zolnierkiewicz 		data->temp_error1 = data->efuse_value & EXYNOS_TMU_TEMP_MASK;
3005000806cSAmit Daniel Kachhap 
3015000806cSAmit Daniel Kachhap 	if (!data->temp_error2)
3025000806cSAmit Daniel Kachhap 		data->temp_error2 =
303*e3ed3649SBartlomiej Zolnierkiewicz 			(data->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
3045000806cSAmit Daniel Kachhap 			EXYNOS_TMU_TEMP_MASK;
3058328a4b1SBartlomiej Zolnierkiewicz }
30659dfa54cSAmit Daniel Kachhap 
307fe87789cSBartlomiej Zolnierkiewicz static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling)
308fe87789cSBartlomiej Zolnierkiewicz {
3093b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
3103b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
3113b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(tz);
3123b6a1a80SLukasz Majewski 	unsigned long temp;
313fe87789cSBartlomiej Zolnierkiewicz 	int i;
314c65d3473STushar Behera 
3153b6a1a80SLukasz Majewski 	if (!trips) {
3163b6a1a80SLukasz Majewski 		pr_err("%s: Cannot get trip points from of-thermal.c!\n",
3173b6a1a80SLukasz Majewski 		       __func__);
3183b6a1a80SLukasz Majewski 		return 0;
3193b6a1a80SLukasz Majewski 	}
320fe87789cSBartlomiej Zolnierkiewicz 
3213b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
3223b6a1a80SLukasz Majewski 		if (trips[i].type == THERMAL_TRIP_CRITICAL)
3233b6a1a80SLukasz Majewski 			continue;
3243b6a1a80SLukasz Majewski 
3253b6a1a80SLukasz Majewski 		temp = trips[i].temperature / MCELSIUS;
326fe87789cSBartlomiej Zolnierkiewicz 		if (falling)
3273b6a1a80SLukasz Majewski 			temp -= (trips[i].hysteresis / MCELSIUS);
328fe87789cSBartlomiej Zolnierkiewicz 		else
329fe87789cSBartlomiej Zolnierkiewicz 			threshold &= ~(0xff << 8 * i);
330fe87789cSBartlomiej Zolnierkiewicz 
331fe87789cSBartlomiej Zolnierkiewicz 		threshold |= temp_to_code(data, temp) << 8 * i;
33259dfa54cSAmit Daniel Kachhap 	}
33359dfa54cSAmit Daniel Kachhap 
334fe87789cSBartlomiej Zolnierkiewicz 	return threshold;
335fe87789cSBartlomiej Zolnierkiewicz }
33659dfa54cSAmit Daniel Kachhap 
33759dfa54cSAmit Daniel Kachhap static int exynos_tmu_initialize(struct platform_device *pdev)
33859dfa54cSAmit Daniel Kachhap {
33959dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
34072d1100bSBartlomiej Zolnierkiewicz 	int ret;
3417ca04e58SAmit Daniel Kachhap 
3423a3a5f15SKrzysztof Kozlowski 	if (of_thermal_get_ntrips(data->tzd) > data->ntrip) {
3433a3a5f15SKrzysztof Kozlowski 		dev_info(&pdev->dev,
3443a3a5f15SKrzysztof Kozlowski 			 "More trip points than supported by this TMU.\n");
3453a3a5f15SKrzysztof Kozlowski 		dev_info(&pdev->dev,
3463a3a5f15SKrzysztof Kozlowski 			 "%d trip points should be configured in polling mode.\n",
3473a3a5f15SKrzysztof Kozlowski 			 (of_thermal_get_ntrips(data->tzd) - data->ntrip));
3483a3a5f15SKrzysztof Kozlowski 	}
3493a3a5f15SKrzysztof Kozlowski 
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 
377b9504a6aSBartlomiej Zolnierkiewicz 	con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
37809d29426SBartlomiej Zolnierkiewicz 	con |= (EXYNOS_NOISE_CANCEL_MODE << EXYNOS_TMU_TRIP_MODE_SHIFT);
37959dfa54cSAmit Daniel Kachhap 
380d00671c3SBartlomiej Zolnierkiewicz 	return con;
381d00671c3SBartlomiej Zolnierkiewicz }
382d00671c3SBartlomiej Zolnierkiewicz 
383d00671c3SBartlomiej Zolnierkiewicz static void exynos_tmu_control(struct platform_device *pdev, bool on)
384d00671c3SBartlomiej Zolnierkiewicz {
385d00671c3SBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
386d00671c3SBartlomiej Zolnierkiewicz 
387d00671c3SBartlomiej Zolnierkiewicz 	mutex_lock(&data->lock);
388d00671c3SBartlomiej Zolnierkiewicz 	clk_enable(data->clk);
38937f9034fSBartlomiej Zolnierkiewicz 	data->tmu_control(pdev, on);
3900eb875d8SMarek Szyprowski 	data->enabled = on;
39159dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
39259dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
39359dfa54cSAmit Daniel Kachhap }
39459dfa54cSAmit Daniel Kachhap 
39572d1100bSBartlomiej Zolnierkiewicz static int exynos4210_tmu_initialize(struct platform_device *pdev)
39672d1100bSBartlomiej Zolnierkiewicz {
39772d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
3983b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
3993b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
4003b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(tz);
40172d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
4023b6a1a80SLukasz Majewski 	unsigned long reference, temp;
4033b6a1a80SLukasz Majewski 	unsigned int status;
4043b6a1a80SLukasz Majewski 
4053b6a1a80SLukasz Majewski 	if (!trips) {
4063b6a1a80SLukasz Majewski 		pr_err("%s: Cannot get trip points from of-thermal.c!\n",
4073b6a1a80SLukasz Majewski 		       __func__);
4083b6a1a80SLukasz Majewski 		ret = -ENODEV;
4093b6a1a80SLukasz Majewski 		goto out;
4103b6a1a80SLukasz Majewski 	}
41172d1100bSBartlomiej Zolnierkiewicz 
41272d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
41372d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
41472d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
41572d1100bSBartlomiej Zolnierkiewicz 		goto out;
41672d1100bSBartlomiej Zolnierkiewicz 	}
41772d1100bSBartlomiej Zolnierkiewicz 
41872d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO));
41972d1100bSBartlomiej Zolnierkiewicz 
42072d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for threshold */
4213b6a1a80SLukasz Majewski 	reference = trips[0].temperature / MCELSIUS;
4223b6a1a80SLukasz Majewski 	threshold_code = temp_to_code(data, reference);
4233b6a1a80SLukasz Majewski 	if (threshold_code < 0) {
4243b6a1a80SLukasz Majewski 		ret = threshold_code;
4253b6a1a80SLukasz Majewski 		goto out;
4263b6a1a80SLukasz Majewski 	}
42772d1100bSBartlomiej Zolnierkiewicz 	writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
42872d1100bSBartlomiej Zolnierkiewicz 
4293b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
4303b6a1a80SLukasz Majewski 		temp = trips[i].temperature / MCELSIUS;
4313b6a1a80SLukasz Majewski 		writeb(temp - reference, data->base +
43272d1100bSBartlomiej Zolnierkiewicz 		       EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
4333b6a1a80SLukasz Majewski 	}
43472d1100bSBartlomiej Zolnierkiewicz 
435a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
43672d1100bSBartlomiej Zolnierkiewicz out:
43772d1100bSBartlomiej Zolnierkiewicz 	return ret;
43872d1100bSBartlomiej Zolnierkiewicz }
43972d1100bSBartlomiej Zolnierkiewicz 
44072d1100bSBartlomiej Zolnierkiewicz static int exynos4412_tmu_initialize(struct platform_device *pdev)
44172d1100bSBartlomiej Zolnierkiewicz {
44272d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
4433b6a1a80SLukasz Majewski 	const struct thermal_trip * const trips =
4443b6a1a80SLukasz Majewski 		of_thermal_get_trip_points(data->tzd);
44572d1100bSBartlomiej Zolnierkiewicz 	unsigned int status, trim_info, con, ctrl, rising_threshold;
44672d1100bSBartlomiej Zolnierkiewicz 	int ret = 0, threshold_code, i;
4473b6a1a80SLukasz Majewski 	unsigned long crit_temp = 0;
44872d1100bSBartlomiej Zolnierkiewicz 
44972d1100bSBartlomiej Zolnierkiewicz 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
45072d1100bSBartlomiej Zolnierkiewicz 	if (!status) {
45172d1100bSBartlomiej Zolnierkiewicz 		ret = -EBUSY;
45272d1100bSBartlomiej Zolnierkiewicz 		goto out;
45372d1100bSBartlomiej Zolnierkiewicz 	}
45472d1100bSBartlomiej Zolnierkiewicz 
45572d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS3250 ||
45672d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS4412 ||
45772d1100bSBartlomiej Zolnierkiewicz 	    data->soc == SOC_ARCH_EXYNOS5250) {
45872d1100bSBartlomiej Zolnierkiewicz 		if (data->soc == SOC_ARCH_EXYNOS3250) {
45972d1100bSBartlomiej Zolnierkiewicz 			ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
46072d1100bSBartlomiej Zolnierkiewicz 			ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
46172d1100bSBartlomiej Zolnierkiewicz 			writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
46272d1100bSBartlomiej Zolnierkiewicz 		}
46372d1100bSBartlomiej Zolnierkiewicz 		ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
46472d1100bSBartlomiej Zolnierkiewicz 		ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
46572d1100bSBartlomiej Zolnierkiewicz 		writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
46672d1100bSBartlomiej Zolnierkiewicz 	}
46772d1100bSBartlomiej Zolnierkiewicz 
46872d1100bSBartlomiej Zolnierkiewicz 	/* On exynos5420 the triminfo register is in the shared space */
46972d1100bSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
47072d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO);
47172d1100bSBartlomiej Zolnierkiewicz 	else
47272d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
47372d1100bSBartlomiej Zolnierkiewicz 
47472d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
47572d1100bSBartlomiej Zolnierkiewicz 
47672d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
47772d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE);
47872d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
47972d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
48072d1100bSBartlomiej Zolnierkiewicz 	writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL);
48172d1100bSBartlomiej Zolnierkiewicz 
482a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
48372d1100bSBartlomiej Zolnierkiewicz 
48472d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
4853b6a1a80SLukasz Majewski 	for (i = 0; i < of_thermal_get_ntrips(data->tzd); i++) {
4863b6a1a80SLukasz Majewski 		if (trips[i].type == THERMAL_TRIP_CRITICAL) {
4873b6a1a80SLukasz Majewski 			crit_temp = trips[i].temperature;
4883b6a1a80SLukasz Majewski 			break;
4893b6a1a80SLukasz Majewski 		}
4903b6a1a80SLukasz Majewski 	}
4913b6a1a80SLukasz Majewski 
4923b6a1a80SLukasz Majewski 	if (i == of_thermal_get_ntrips(data->tzd)) {
4933b6a1a80SLukasz Majewski 		pr_err("%s: No CRITICAL trip point defined at of-thermal.c!\n",
4943b6a1a80SLukasz Majewski 		       __func__);
4953b6a1a80SLukasz Majewski 		ret = -EINVAL;
4963b6a1a80SLukasz Majewski 		goto out;
4973b6a1a80SLukasz Majewski 	}
4983b6a1a80SLukasz Majewski 
4993b6a1a80SLukasz Majewski 	threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
50072d1100bSBartlomiej Zolnierkiewicz 	/* 1-4 level to be assigned in th0 reg */
50172d1100bSBartlomiej Zolnierkiewicz 	rising_threshold &= ~(0xff << 8 * i);
50272d1100bSBartlomiej Zolnierkiewicz 	rising_threshold |= threshold_code << 8 * i;
50372d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
50472d1100bSBartlomiej Zolnierkiewicz 	con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
50572d1100bSBartlomiej Zolnierkiewicz 	con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
50672d1100bSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
5073b6a1a80SLukasz Majewski 
50872d1100bSBartlomiej Zolnierkiewicz out:
50972d1100bSBartlomiej Zolnierkiewicz 	return ret;
51072d1100bSBartlomiej Zolnierkiewicz }
51172d1100bSBartlomiej Zolnierkiewicz 
512488c7455SChanwoo Choi static int exynos5433_tmu_initialize(struct platform_device *pdev)
513488c7455SChanwoo Choi {
514488c7455SChanwoo Choi 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
515488c7455SChanwoo Choi 	struct exynos_tmu_platform_data *pdata = data->pdata;
516488c7455SChanwoo Choi 	struct thermal_zone_device *tz = data->tzd;
517488c7455SChanwoo Choi 	unsigned int status, trim_info;
518488c7455SChanwoo Choi 	unsigned int rising_threshold = 0, falling_threshold = 0;
51917e8351aSSascha Hauer 	int temp, temp_hist;
520488c7455SChanwoo Choi 	int ret = 0, threshold_code, i, sensor_id, cal_type;
521488c7455SChanwoo Choi 
522488c7455SChanwoo Choi 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
523488c7455SChanwoo Choi 	if (!status) {
524488c7455SChanwoo Choi 		ret = -EBUSY;
525488c7455SChanwoo Choi 		goto out;
526488c7455SChanwoo Choi 	}
527488c7455SChanwoo Choi 
528488c7455SChanwoo Choi 	trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
529488c7455SChanwoo Choi 	sanitize_temp_error(data, trim_info);
530488c7455SChanwoo Choi 
531488c7455SChanwoo Choi 	/* Read the temperature sensor id */
532488c7455SChanwoo Choi 	sensor_id = (trim_info & EXYNOS5433_TRIMINFO_SENSOR_ID_MASK)
533488c7455SChanwoo Choi 				>> EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT;
534488c7455SChanwoo Choi 	dev_info(&pdev->dev, "Temperature sensor ID: 0x%x\n", sensor_id);
535488c7455SChanwoo Choi 
536488c7455SChanwoo Choi 	/* Read the calibration mode */
537488c7455SChanwoo Choi 	writel(trim_info, data->base + EXYNOS_TMU_REG_TRIMINFO);
538488c7455SChanwoo Choi 	cal_type = (trim_info & EXYNOS5433_TRIMINFO_CALIB_SEL_MASK)
539488c7455SChanwoo Choi 				>> EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT;
540488c7455SChanwoo Choi 
541488c7455SChanwoo Choi 	switch (cal_type) {
542488c7455SChanwoo Choi 	case EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING:
543488c7455SChanwoo Choi 		pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
544488c7455SChanwoo Choi 		break;
545488c7455SChanwoo Choi 	case EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING:
546488c7455SChanwoo Choi 		pdata->cal_type = TYPE_TWO_POINT_TRIMMING;
547488c7455SChanwoo Choi 		break;
548488c7455SChanwoo Choi 	default:
549488c7455SChanwoo Choi 		pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
550488c7455SChanwoo Choi 		break;
551baba1ebbSKrzysztof Kozlowski 	}
552488c7455SChanwoo Choi 
553488c7455SChanwoo Choi 	dev_info(&pdev->dev, "Calibration type is %d-point calibration\n",
554488c7455SChanwoo Choi 			cal_type ?  2 : 1);
555488c7455SChanwoo Choi 
556488c7455SChanwoo Choi 	/* Write temperature code for rising and falling threshold */
557488c7455SChanwoo Choi 	for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
558488c7455SChanwoo Choi 		int rising_reg_offset, falling_reg_offset;
559488c7455SChanwoo Choi 		int j = 0;
560488c7455SChanwoo Choi 
561488c7455SChanwoo Choi 		switch (i) {
562488c7455SChanwoo Choi 		case 0:
563488c7455SChanwoo Choi 		case 1:
564488c7455SChanwoo Choi 		case 2:
565488c7455SChanwoo Choi 		case 3:
566488c7455SChanwoo Choi 			rising_reg_offset = EXYNOS5433_THD_TEMP_RISE3_0;
567488c7455SChanwoo Choi 			falling_reg_offset = EXYNOS5433_THD_TEMP_FALL3_0;
568488c7455SChanwoo Choi 			j = i;
569488c7455SChanwoo Choi 			break;
570488c7455SChanwoo Choi 		case 4:
571488c7455SChanwoo Choi 		case 5:
572488c7455SChanwoo Choi 		case 6:
573488c7455SChanwoo Choi 		case 7:
574488c7455SChanwoo Choi 			rising_reg_offset = EXYNOS5433_THD_TEMP_RISE7_4;
575488c7455SChanwoo Choi 			falling_reg_offset = EXYNOS5433_THD_TEMP_FALL7_4;
576488c7455SChanwoo Choi 			j = i - 4;
577488c7455SChanwoo Choi 			break;
578488c7455SChanwoo Choi 		default:
579488c7455SChanwoo Choi 			continue;
580488c7455SChanwoo Choi 		}
581488c7455SChanwoo Choi 
582488c7455SChanwoo Choi 		/* Write temperature code for rising threshold */
583488c7455SChanwoo Choi 		tz->ops->get_trip_temp(tz, i, &temp);
584488c7455SChanwoo Choi 		temp /= MCELSIUS;
585488c7455SChanwoo Choi 		threshold_code = temp_to_code(data, temp);
586488c7455SChanwoo Choi 
587488c7455SChanwoo Choi 		rising_threshold = readl(data->base + rising_reg_offset);
588488c7455SChanwoo Choi 		rising_threshold |= (threshold_code << j * 8);
589488c7455SChanwoo Choi 		writel(rising_threshold, data->base + rising_reg_offset);
590488c7455SChanwoo Choi 
591488c7455SChanwoo Choi 		/* Write temperature code for falling threshold */
592488c7455SChanwoo Choi 		tz->ops->get_trip_hyst(tz, i, &temp_hist);
593488c7455SChanwoo Choi 		temp_hist = temp - (temp_hist / MCELSIUS);
594488c7455SChanwoo Choi 		threshold_code = temp_to_code(data, temp_hist);
595488c7455SChanwoo Choi 
596488c7455SChanwoo Choi 		falling_threshold = readl(data->base + falling_reg_offset);
597488c7455SChanwoo Choi 		falling_threshold &= ~(0xff << j * 8);
598488c7455SChanwoo Choi 		falling_threshold |= (threshold_code << j * 8);
599488c7455SChanwoo Choi 		writel(falling_threshold, data->base + falling_reg_offset);
600488c7455SChanwoo Choi 	}
601488c7455SChanwoo Choi 
602488c7455SChanwoo Choi 	data->tmu_clear_irqs(data);
603488c7455SChanwoo Choi out:
604488c7455SChanwoo Choi 	return ret;
605488c7455SChanwoo Choi }
606488c7455SChanwoo Choi 
60772d1100bSBartlomiej Zolnierkiewicz static int exynos5440_tmu_initialize(struct platform_device *pdev)
60872d1100bSBartlomiej Zolnierkiewicz {
60972d1100bSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
61072d1100bSBartlomiej Zolnierkiewicz 	unsigned int trim_info = 0, con, rising_threshold;
611e35dbb4dSKrzysztof Kozlowski 	int threshold_code;
61217e8351aSSascha Hauer 	int crit_temp = 0;
61372d1100bSBartlomiej Zolnierkiewicz 
61472d1100bSBartlomiej Zolnierkiewicz 	/*
61572d1100bSBartlomiej Zolnierkiewicz 	 * For exynos5440 soc triminfo value is swapped between TMU0 and
61672d1100bSBartlomiej Zolnierkiewicz 	 * TMU2, so the below logic is needed.
61772d1100bSBartlomiej Zolnierkiewicz 	 */
61872d1100bSBartlomiej Zolnierkiewicz 	switch (data->id) {
61972d1100bSBartlomiej Zolnierkiewicz 	case 0:
62072d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET +
62172d1100bSBartlomiej Zolnierkiewicz 				 EXYNOS5440_TMU_S0_7_TRIM);
62272d1100bSBartlomiej Zolnierkiewicz 		break;
62372d1100bSBartlomiej Zolnierkiewicz 	case 1:
62472d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM);
62572d1100bSBartlomiej Zolnierkiewicz 		break;
62672d1100bSBartlomiej Zolnierkiewicz 	case 2:
62772d1100bSBartlomiej Zolnierkiewicz 		trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET +
62872d1100bSBartlomiej Zolnierkiewicz 				  EXYNOS5440_TMU_S0_7_TRIM);
62972d1100bSBartlomiej Zolnierkiewicz 	}
63072d1100bSBartlomiej Zolnierkiewicz 	sanitize_temp_error(data, trim_info);
63172d1100bSBartlomiej Zolnierkiewicz 
63272d1100bSBartlomiej Zolnierkiewicz 	/* Write temperature code for rising and falling threshold */
63372d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0);
63472d1100bSBartlomiej Zolnierkiewicz 	rising_threshold = get_th_reg(data, rising_threshold, false);
63572d1100bSBartlomiej Zolnierkiewicz 	writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0);
63672d1100bSBartlomiej Zolnierkiewicz 	writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1);
63772d1100bSBartlomiej Zolnierkiewicz 
638a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
63972d1100bSBartlomiej Zolnierkiewicz 
64072d1100bSBartlomiej Zolnierkiewicz 	/* if last threshold limit is also present */
6413b6a1a80SLukasz Majewski 	if (!data->tzd->ops->get_crit_temp(data->tzd, &crit_temp)) {
6423b6a1a80SLukasz Majewski 		threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
64372d1100bSBartlomiej Zolnierkiewicz 		/* 5th level to be assigned in th2 reg */
64472d1100bSBartlomiej Zolnierkiewicz 		rising_threshold =
64572d1100bSBartlomiej Zolnierkiewicz 			threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT;
64672d1100bSBartlomiej Zolnierkiewicz 		writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2);
64772d1100bSBartlomiej Zolnierkiewicz 		con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL);
64872d1100bSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
64972d1100bSBartlomiej Zolnierkiewicz 		writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
65072d1100bSBartlomiej Zolnierkiewicz 	}
65172d1100bSBartlomiej Zolnierkiewicz 	/* Clear the PMIN in the common TMU register */
65272d1100bSBartlomiej Zolnierkiewicz 	if (!data->id)
65372d1100bSBartlomiej Zolnierkiewicz 		writel(0, data->base_second + EXYNOS5440_TMU_PMIN);
654e35dbb4dSKrzysztof Kozlowski 
655e35dbb4dSKrzysztof Kozlowski 	return 0;
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 	unsigned int status, trim_info;
6636c247393SAbhilash Kesavan 	unsigned int rising_threshold = 0, falling_threshold = 0;
6646c247393SAbhilash Kesavan 	int ret = 0, threshold_code, i;
66517e8351aSSascha Hauer 	int temp, temp_hist;
6666c247393SAbhilash Kesavan 	unsigned int reg_off, bit_off;
6676c247393SAbhilash Kesavan 
6686c247393SAbhilash Kesavan 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
6696c247393SAbhilash Kesavan 	if (!status) {
6706c247393SAbhilash Kesavan 		ret = -EBUSY;
6716c247393SAbhilash Kesavan 		goto out;
6726c247393SAbhilash Kesavan 	}
6736c247393SAbhilash Kesavan 
6746c247393SAbhilash Kesavan 	trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
6756c247393SAbhilash Kesavan 
6766c247393SAbhilash Kesavan 	data->temp_error1 = trim_info & EXYNOS7_TMU_TEMP_MASK;
6776c247393SAbhilash Kesavan 	if (!data->temp_error1 ||
678*e3ed3649SBartlomiej Zolnierkiewicz 	    (data->min_efuse_value > data->temp_error1) ||
679*e3ed3649SBartlomiej Zolnierkiewicz 	    (data->temp_error1 > data->max_efuse_value))
680*e3ed3649SBartlomiej Zolnierkiewicz 		data->temp_error1 = data->efuse_value & EXYNOS_TMU_TEMP_MASK;
6816c247393SAbhilash Kesavan 
6826c247393SAbhilash Kesavan 	/* Write temperature code for rising and falling threshold */
6836c247393SAbhilash Kesavan 	for (i = (of_thermal_get_ntrips(tz) - 1); i >= 0; i--) {
6846c247393SAbhilash Kesavan 		/*
6856c247393SAbhilash Kesavan 		 * On exynos7 there are 4 rising and 4 falling threshold
6866c247393SAbhilash Kesavan 		 * registers (0x50-0x5c and 0x60-0x6c respectively). Each
6876c247393SAbhilash Kesavan 		 * register holds the value of two threshold levels (at bit
6886c247393SAbhilash Kesavan 		 * offsets 0 and 16). Based on the fact that there are atmost
6896c247393SAbhilash Kesavan 		 * eight possible trigger levels, calculate the register and
6906c247393SAbhilash Kesavan 		 * bit offsets where the threshold levels are to be written.
6916c247393SAbhilash Kesavan 		 *
6926c247393SAbhilash Kesavan 		 * e.g. EXYNOS7_THD_TEMP_RISE7_6 (0x50)
6936c247393SAbhilash Kesavan 		 * [24:16] - Threshold level 7
6946c247393SAbhilash Kesavan 		 * [8:0] - Threshold level 6
6956c247393SAbhilash Kesavan 		 * e.g. EXYNOS7_THD_TEMP_RISE5_4 (0x54)
6966c247393SAbhilash Kesavan 		 * [24:16] - Threshold level 5
6976c247393SAbhilash Kesavan 		 * [8:0] - Threshold level 4
6986c247393SAbhilash Kesavan 		 *
6996c247393SAbhilash Kesavan 		 * and similarly for falling thresholds.
7006c247393SAbhilash Kesavan 		 *
7016c247393SAbhilash Kesavan 		 * Based on the above, calculate the register and bit offsets
7026c247393SAbhilash Kesavan 		 * for rising/falling threshold levels and populate them.
7036c247393SAbhilash Kesavan 		 */
7046c247393SAbhilash Kesavan 		reg_off = ((7 - i) / 2) * 4;
7056c247393SAbhilash Kesavan 		bit_off = ((8 - i) % 2);
7066c247393SAbhilash Kesavan 
7076c247393SAbhilash Kesavan 		tz->ops->get_trip_temp(tz, i, &temp);
7086c247393SAbhilash Kesavan 		temp /= MCELSIUS;
7096c247393SAbhilash Kesavan 
7106c247393SAbhilash Kesavan 		tz->ops->get_trip_hyst(tz, i, &temp_hist);
7116c247393SAbhilash Kesavan 		temp_hist = temp - (temp_hist / MCELSIUS);
7126c247393SAbhilash Kesavan 
7136c247393SAbhilash Kesavan 		/* Set 9-bit temperature code for rising threshold levels */
7146c247393SAbhilash Kesavan 		threshold_code = temp_to_code(data, temp);
7156c247393SAbhilash Kesavan 		rising_threshold = readl(data->base +
7166c247393SAbhilash Kesavan 			EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
7176c247393SAbhilash Kesavan 		rising_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
7186c247393SAbhilash Kesavan 		rising_threshold |= threshold_code << (16 * bit_off);
7196c247393SAbhilash Kesavan 		writel(rising_threshold,
7206c247393SAbhilash Kesavan 		       data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
7216c247393SAbhilash Kesavan 
7226c247393SAbhilash Kesavan 		/* Set 9-bit temperature code for falling threshold levels */
7236c247393SAbhilash Kesavan 		threshold_code = temp_to_code(data, temp_hist);
7246c247393SAbhilash Kesavan 		falling_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
7256c247393SAbhilash Kesavan 		falling_threshold |= threshold_code << (16 * bit_off);
7266c247393SAbhilash Kesavan 		writel(falling_threshold,
7276c247393SAbhilash Kesavan 		       data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off);
7286c247393SAbhilash Kesavan 	}
7296c247393SAbhilash Kesavan 
7306c247393SAbhilash Kesavan 	data->tmu_clear_irqs(data);
7316c247393SAbhilash Kesavan out:
7326c247393SAbhilash Kesavan 	return ret;
7336c247393SAbhilash Kesavan }
7346c247393SAbhilash Kesavan 
73537f9034fSBartlomiej Zolnierkiewicz static void exynos4210_tmu_control(struct platform_device *pdev, bool on)
73637f9034fSBartlomiej Zolnierkiewicz {
73737f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
7383b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
73937f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
74037f9034fSBartlomiej Zolnierkiewicz 
74137f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
74237f9034fSBartlomiej Zolnierkiewicz 
74359dfa54cSAmit Daniel Kachhap 	if (on) {
74459dfa54cSAmit Daniel Kachhap 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
74559dfa54cSAmit Daniel Kachhap 		interrupt_en =
7463b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 3)
7473b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE3_SHIFT) |
7483b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 2)
7493b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE2_SHIFT) |
7503b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 1)
7513b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE1_SHIFT) |
7523b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 0)
7533b6a1a80SLukasz Majewski 			 << EXYNOS_TMU_INTEN_RISE0_SHIFT);
7543b6a1a80SLukasz Majewski 
755e0761533SBartlomiej Zolnierkiewicz 		if (data->soc != SOC_ARCH_EXYNOS4210)
75659dfa54cSAmit Daniel Kachhap 			interrupt_en |=
75737f9034fSBartlomiej Zolnierkiewicz 				interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
75859dfa54cSAmit Daniel Kachhap 	} else {
75959dfa54cSAmit Daniel Kachhap 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
76059dfa54cSAmit Daniel Kachhap 		interrupt_en = 0; /* Disable all interrupts */
76159dfa54cSAmit Daniel Kachhap 	}
76237f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
76337f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
76437f9034fSBartlomiej Zolnierkiewicz }
76559dfa54cSAmit Daniel Kachhap 
766488c7455SChanwoo Choi static void exynos5433_tmu_control(struct platform_device *pdev, bool on)
767488c7455SChanwoo Choi {
768488c7455SChanwoo Choi 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
769488c7455SChanwoo Choi 	struct thermal_zone_device *tz = data->tzd;
770488c7455SChanwoo Choi 	unsigned int con, interrupt_en, pd_det_en;
771488c7455SChanwoo Choi 
772488c7455SChanwoo Choi 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
773488c7455SChanwoo Choi 
774488c7455SChanwoo Choi 	if (on) {
775488c7455SChanwoo Choi 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
776488c7455SChanwoo Choi 		interrupt_en =
777488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 7)
778488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
779488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 6)
780488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
781488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 5)
782488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
783488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 4)
784488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
785488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 3)
786488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
787488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 2)
788488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
789488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 1)
790488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
791488c7455SChanwoo Choi 			(of_thermal_is_trip_valid(tz, 0)
792488c7455SChanwoo Choi 			<< EXYNOS7_TMU_INTEN_RISE0_SHIFT);
793488c7455SChanwoo Choi 
794488c7455SChanwoo Choi 		interrupt_en |=
795488c7455SChanwoo Choi 			interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
796488c7455SChanwoo Choi 	} else {
797488c7455SChanwoo Choi 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
798488c7455SChanwoo Choi 		interrupt_en = 0; /* Disable all interrupts */
799488c7455SChanwoo Choi 	}
800488c7455SChanwoo Choi 
801488c7455SChanwoo Choi 	pd_det_en = on ? EXYNOS5433_PD_DET_EN : 0;
802488c7455SChanwoo Choi 
803488c7455SChanwoo Choi 	writel(pd_det_en, data->base + EXYNOS5433_TMU_PD_DET_EN);
804488c7455SChanwoo Choi 	writel(interrupt_en, data->base + EXYNOS5433_TMU_REG_INTEN);
805488c7455SChanwoo Choi 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
806488c7455SChanwoo Choi }
807488c7455SChanwoo Choi 
80837f9034fSBartlomiej Zolnierkiewicz static void exynos5440_tmu_control(struct platform_device *pdev, bool on)
80937f9034fSBartlomiej Zolnierkiewicz {
81037f9034fSBartlomiej Zolnierkiewicz 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
8113b6a1a80SLukasz Majewski 	struct thermal_zone_device *tz = data->tzd;
81237f9034fSBartlomiej Zolnierkiewicz 	unsigned int con, interrupt_en;
81337f9034fSBartlomiej Zolnierkiewicz 
81437f9034fSBartlomiej Zolnierkiewicz 	con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL));
81537f9034fSBartlomiej Zolnierkiewicz 
81637f9034fSBartlomiej Zolnierkiewicz 	if (on) {
81737f9034fSBartlomiej Zolnierkiewicz 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
81837f9034fSBartlomiej Zolnierkiewicz 		interrupt_en =
8193b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 3)
8203b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE3_SHIFT) |
8213b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 2)
8223b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE2_SHIFT) |
8233b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 1)
8243b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE1_SHIFT) |
8253b6a1a80SLukasz Majewski 			(of_thermal_is_trip_valid(tz, 0)
8263b6a1a80SLukasz Majewski 			 << EXYNOS5440_TMU_INTEN_RISE0_SHIFT);
8273b6a1a80SLukasz Majewski 		interrupt_en |=
8283b6a1a80SLukasz Majewski 			interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT;
82937f9034fSBartlomiej Zolnierkiewicz 	} else {
83037f9034fSBartlomiej Zolnierkiewicz 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
83137f9034fSBartlomiej Zolnierkiewicz 		interrupt_en = 0; /* Disable all interrupts */
83237f9034fSBartlomiej Zolnierkiewicz 	}
83337f9034fSBartlomiej Zolnierkiewicz 	writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN);
83437f9034fSBartlomiej Zolnierkiewicz 	writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
83559dfa54cSAmit Daniel Kachhap }
83659dfa54cSAmit Daniel Kachhap 
8376c247393SAbhilash Kesavan static void exynos7_tmu_control(struct platform_device *pdev, bool on)
8386c247393SAbhilash Kesavan {
8396c247393SAbhilash Kesavan 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
8406c247393SAbhilash Kesavan 	struct thermal_zone_device *tz = data->tzd;
8416c247393SAbhilash Kesavan 	unsigned int con, interrupt_en;
8426c247393SAbhilash Kesavan 
8436c247393SAbhilash Kesavan 	con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
8446c247393SAbhilash Kesavan 
8456c247393SAbhilash Kesavan 	if (on) {
8466c247393SAbhilash Kesavan 		con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
84742b696e8SChanwoo Choi 		con |= (1 << EXYNOS7_PD_DET_EN_SHIFT);
8486c247393SAbhilash Kesavan 		interrupt_en =
8496c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 7)
8506c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
8516c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 6)
8526c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
8536c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 5)
8546c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
8556c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 4)
8566c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
8576c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 3)
8586c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
8596c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 2)
8606c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
8616c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 1)
8626c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
8636c247393SAbhilash Kesavan 			(of_thermal_is_trip_valid(tz, 0)
8646c247393SAbhilash Kesavan 			<< EXYNOS7_TMU_INTEN_RISE0_SHIFT);
8656c247393SAbhilash Kesavan 
8666c247393SAbhilash Kesavan 		interrupt_en |=
8676c247393SAbhilash Kesavan 			interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
8686c247393SAbhilash Kesavan 	} else {
8696c247393SAbhilash Kesavan 		con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
87042b696e8SChanwoo Choi 		con &= ~(1 << EXYNOS7_PD_DET_EN_SHIFT);
8716c247393SAbhilash Kesavan 		interrupt_en = 0; /* Disable all interrupts */
8726c247393SAbhilash Kesavan 	}
8736c247393SAbhilash Kesavan 
8746c247393SAbhilash Kesavan 	writel(interrupt_en, data->base + EXYNOS7_TMU_REG_INTEN);
8756c247393SAbhilash Kesavan 	writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
8766c247393SAbhilash Kesavan }
8776c247393SAbhilash Kesavan 
87817e8351aSSascha Hauer static int exynos_get_temp(void *p, int *temp)
87959dfa54cSAmit Daniel Kachhap {
8803b6a1a80SLukasz Majewski 	struct exynos_tmu_data *data = p;
88108d725cdSMarek Szyprowski 	int value, ret = 0;
8823b6a1a80SLukasz Majewski 
8830eb875d8SMarek Szyprowski 	if (!data || !data->tmu_read || !data->enabled)
8843b6a1a80SLukasz Majewski 		return -EINVAL;
88559dfa54cSAmit Daniel Kachhap 
88659dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
88759dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
8883b6a1a80SLukasz Majewski 
88908d725cdSMarek Szyprowski 	value = data->tmu_read(data);
89008d725cdSMarek Szyprowski 	if (value < 0)
89108d725cdSMarek Szyprowski 		ret = value;
89208d725cdSMarek Szyprowski 	else
89308d725cdSMarek Szyprowski 		*temp = code_to_temp(data, value) * MCELSIUS;
8943b6a1a80SLukasz Majewski 
89559dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
89659dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
89759dfa54cSAmit Daniel Kachhap 
89808d725cdSMarek Szyprowski 	return ret;
89959dfa54cSAmit Daniel Kachhap }
90059dfa54cSAmit Daniel Kachhap 
90159dfa54cSAmit Daniel Kachhap #ifdef CONFIG_THERMAL_EMULATION
902154013eaSBartlomiej Zolnierkiewicz static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val,
90317e8351aSSascha Hauer 			    int temp)
904154013eaSBartlomiej Zolnierkiewicz {
905154013eaSBartlomiej Zolnierkiewicz 	if (temp) {
906154013eaSBartlomiej Zolnierkiewicz 		temp /= MCELSIUS;
907154013eaSBartlomiej Zolnierkiewicz 
908d564b55aSBartlomiej Zolnierkiewicz 		if (data->soc != SOC_ARCH_EXYNOS5440) {
909154013eaSBartlomiej Zolnierkiewicz 			val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
910154013eaSBartlomiej Zolnierkiewicz 			val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
911154013eaSBartlomiej Zolnierkiewicz 		}
9126c247393SAbhilash Kesavan 		if (data->soc == SOC_ARCH_EXYNOS7) {
9136c247393SAbhilash Kesavan 			val &= ~(EXYNOS7_EMUL_DATA_MASK <<
9146c247393SAbhilash Kesavan 				EXYNOS7_EMUL_DATA_SHIFT);
9156c247393SAbhilash Kesavan 			val |= (temp_to_code(data, temp) <<
9166c247393SAbhilash Kesavan 				EXYNOS7_EMUL_DATA_SHIFT) |
917154013eaSBartlomiej Zolnierkiewicz 				EXYNOS_EMUL_ENABLE;
918154013eaSBartlomiej Zolnierkiewicz 		} else {
9196c247393SAbhilash Kesavan 			val &= ~(EXYNOS_EMUL_DATA_MASK <<
9206c247393SAbhilash Kesavan 				EXYNOS_EMUL_DATA_SHIFT);
9216c247393SAbhilash Kesavan 			val |= (temp_to_code(data, temp) <<
9226c247393SAbhilash Kesavan 				EXYNOS_EMUL_DATA_SHIFT) |
9236c247393SAbhilash Kesavan 				EXYNOS_EMUL_ENABLE;
9246c247393SAbhilash Kesavan 		}
9256c247393SAbhilash Kesavan 	} else {
926154013eaSBartlomiej Zolnierkiewicz 		val &= ~EXYNOS_EMUL_ENABLE;
927154013eaSBartlomiej Zolnierkiewicz 	}
928154013eaSBartlomiej Zolnierkiewicz 
929154013eaSBartlomiej Zolnierkiewicz 	return val;
930154013eaSBartlomiej Zolnierkiewicz }
931154013eaSBartlomiej Zolnierkiewicz 
932285d994aSBartlomiej Zolnierkiewicz static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data,
93317e8351aSSascha Hauer 					 int temp)
934285d994aSBartlomiej Zolnierkiewicz {
935285d994aSBartlomiej Zolnierkiewicz 	unsigned int val;
936285d994aSBartlomiej Zolnierkiewicz 	u32 emul_con;
937285d994aSBartlomiej Zolnierkiewicz 
938285d994aSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5260)
939285d994aSBartlomiej Zolnierkiewicz 		emul_con = EXYNOS5260_EMUL_CON;
940b28fec13SSudip Mukherjee 	else if (data->soc == SOC_ARCH_EXYNOS5433)
941488c7455SChanwoo Choi 		emul_con = EXYNOS5433_TMU_EMUL_CON;
9426c247393SAbhilash Kesavan 	else if (data->soc == SOC_ARCH_EXYNOS7)
9436c247393SAbhilash Kesavan 		emul_con = EXYNOS7_TMU_REG_EMUL_CON;
944285d994aSBartlomiej Zolnierkiewicz 	else
945285d994aSBartlomiej Zolnierkiewicz 		emul_con = EXYNOS_EMUL_CON;
946285d994aSBartlomiej Zolnierkiewicz 
947285d994aSBartlomiej Zolnierkiewicz 	val = readl(data->base + emul_con);
948285d994aSBartlomiej Zolnierkiewicz 	val = get_emul_con_reg(data, val, temp);
949285d994aSBartlomiej Zolnierkiewicz 	writel(val, data->base + emul_con);
950285d994aSBartlomiej Zolnierkiewicz }
951285d994aSBartlomiej Zolnierkiewicz 
952285d994aSBartlomiej Zolnierkiewicz static void exynos5440_tmu_set_emulation(struct exynos_tmu_data *data,
95317e8351aSSascha Hauer 					 int temp)
954285d994aSBartlomiej Zolnierkiewicz {
955285d994aSBartlomiej Zolnierkiewicz 	unsigned int val;
956285d994aSBartlomiej Zolnierkiewicz 
957285d994aSBartlomiej Zolnierkiewicz 	val = readl(data->base + EXYNOS5440_TMU_S0_7_DEBUG);
958285d994aSBartlomiej Zolnierkiewicz 	val = get_emul_con_reg(data, val, temp);
959285d994aSBartlomiej Zolnierkiewicz 	writel(val, data->base + EXYNOS5440_TMU_S0_7_DEBUG);
960285d994aSBartlomiej Zolnierkiewicz }
961285d994aSBartlomiej Zolnierkiewicz 
96217e8351aSSascha Hauer static int exynos_tmu_set_emulation(void *drv_data, int temp)
96359dfa54cSAmit Daniel Kachhap {
96459dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = drv_data;
96559dfa54cSAmit Daniel Kachhap 	int ret = -EINVAL;
96659dfa54cSAmit Daniel Kachhap 
967ef3f80fcSBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS4210)
96859dfa54cSAmit Daniel Kachhap 		goto out;
96959dfa54cSAmit Daniel Kachhap 
97059dfa54cSAmit Daniel Kachhap 	if (temp && temp < MCELSIUS)
97159dfa54cSAmit Daniel Kachhap 		goto out;
97259dfa54cSAmit Daniel Kachhap 
97359dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
97459dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
975285d994aSBartlomiej Zolnierkiewicz 	data->tmu_set_emulation(data, temp);
97659dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
97759dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
97859dfa54cSAmit Daniel Kachhap 	return 0;
97959dfa54cSAmit Daniel Kachhap out:
98059dfa54cSAmit Daniel Kachhap 	return ret;
98159dfa54cSAmit Daniel Kachhap }
98259dfa54cSAmit Daniel Kachhap #else
983285d994aSBartlomiej Zolnierkiewicz #define exynos4412_tmu_set_emulation NULL
984285d994aSBartlomiej Zolnierkiewicz #define exynos5440_tmu_set_emulation NULL
98517e8351aSSascha Hauer static int exynos_tmu_set_emulation(void *drv_data, int temp)
98659dfa54cSAmit Daniel Kachhap 	{ return -EINVAL; }
98759dfa54cSAmit Daniel Kachhap #endif /* CONFIG_THERMAL_EMULATION */
98859dfa54cSAmit Daniel Kachhap 
989b79985caSBartlomiej Zolnierkiewicz static int exynos4210_tmu_read(struct exynos_tmu_data *data)
990b79985caSBartlomiej Zolnierkiewicz {
991b79985caSBartlomiej Zolnierkiewicz 	int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
992b79985caSBartlomiej Zolnierkiewicz 
993b79985caSBartlomiej Zolnierkiewicz 	/* "temp_code" should range between 75 and 175 */
994b79985caSBartlomiej Zolnierkiewicz 	return (ret < 75 || ret > 175) ? -ENODATA : ret;
995b79985caSBartlomiej Zolnierkiewicz }
996b79985caSBartlomiej Zolnierkiewicz 
997b79985caSBartlomiej Zolnierkiewicz static int exynos4412_tmu_read(struct exynos_tmu_data *data)
998b79985caSBartlomiej Zolnierkiewicz {
999b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
1000b79985caSBartlomiej Zolnierkiewicz }
1001b79985caSBartlomiej Zolnierkiewicz 
1002b79985caSBartlomiej Zolnierkiewicz static int exynos5440_tmu_read(struct exynos_tmu_data *data)
1003b79985caSBartlomiej Zolnierkiewicz {
1004b79985caSBartlomiej Zolnierkiewicz 	return readb(data->base + EXYNOS5440_TMU_S0_7_TEMP);
1005b79985caSBartlomiej Zolnierkiewicz }
1006b79985caSBartlomiej Zolnierkiewicz 
10076c247393SAbhilash Kesavan static int exynos7_tmu_read(struct exynos_tmu_data *data)
10086c247393SAbhilash Kesavan {
10096c247393SAbhilash Kesavan 	return readw(data->base + EXYNOS_TMU_REG_CURRENT_TEMP) &
10106c247393SAbhilash Kesavan 		EXYNOS7_TMU_TEMP_MASK;
10116c247393SAbhilash Kesavan }
10126c247393SAbhilash Kesavan 
101359dfa54cSAmit Daniel Kachhap static void exynos_tmu_work(struct work_struct *work)
101459dfa54cSAmit Daniel Kachhap {
101559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = container_of(work,
101659dfa54cSAmit Daniel Kachhap 			struct exynos_tmu_data, irq_work);
1017b835ced1SBartlomiej Zolnierkiewicz 	unsigned int val_type;
1018a0395eeeSAmit Daniel Kachhap 
101914a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
102014a11dc7SNaveen Krishna Chatradhi 		clk_enable(data->clk_sec);
1021a0395eeeSAmit Daniel Kachhap 	/* Find which sensor generated this interrupt */
1022421d5d12SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5440) {
1023421d5d12SBartlomiej Zolnierkiewicz 		val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS);
1024a0395eeeSAmit Daniel Kachhap 		if (!((val_type >> data->id) & 0x1))
1025a0395eeeSAmit Daniel Kachhap 			goto out;
1026a0395eeeSAmit Daniel Kachhap 	}
102714a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
102814a11dc7SNaveen Krishna Chatradhi 		clk_disable(data->clk_sec);
102959dfa54cSAmit Daniel Kachhap 
10303b6a1a80SLukasz Majewski 	exynos_report_trigger(data);
103159dfa54cSAmit Daniel Kachhap 	mutex_lock(&data->lock);
103259dfa54cSAmit Daniel Kachhap 	clk_enable(data->clk);
1033b8d582b9SAmit Daniel Kachhap 
1034a4463c4fSAmit Daniel Kachhap 	/* TODO: take action based on particular interrupt */
1035a7331f72SBartlomiej Zolnierkiewicz 	data->tmu_clear_irqs(data);
1036b8d582b9SAmit Daniel Kachhap 
103759dfa54cSAmit Daniel Kachhap 	clk_disable(data->clk);
103859dfa54cSAmit Daniel Kachhap 	mutex_unlock(&data->lock);
1039a0395eeeSAmit Daniel Kachhap out:
104059dfa54cSAmit Daniel Kachhap 	enable_irq(data->irq);
104159dfa54cSAmit Daniel Kachhap }
104259dfa54cSAmit Daniel Kachhap 
1043a7331f72SBartlomiej Zolnierkiewicz static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data)
1044a7331f72SBartlomiej Zolnierkiewicz {
1045a7331f72SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
1046a7331f72SBartlomiej Zolnierkiewicz 	u32 tmu_intstat, tmu_intclear;
1047a7331f72SBartlomiej Zolnierkiewicz 
1048a7331f72SBartlomiej Zolnierkiewicz 	if (data->soc == SOC_ARCH_EXYNOS5260) {
1049a7331f72SBartlomiej Zolnierkiewicz 		tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT;
1050a7331f72SBartlomiej Zolnierkiewicz 		tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR;
10516c247393SAbhilash Kesavan 	} else if (data->soc == SOC_ARCH_EXYNOS7) {
10526c247393SAbhilash Kesavan 		tmu_intstat = EXYNOS7_TMU_REG_INTPEND;
10536c247393SAbhilash Kesavan 		tmu_intclear = EXYNOS7_TMU_REG_INTPEND;
1054488c7455SChanwoo Choi 	} else if (data->soc == SOC_ARCH_EXYNOS5433) {
1055488c7455SChanwoo Choi 		tmu_intstat = EXYNOS5433_TMU_REG_INTPEND;
1056488c7455SChanwoo Choi 		tmu_intclear = EXYNOS5433_TMU_REG_INTPEND;
1057a7331f72SBartlomiej Zolnierkiewicz 	} else {
1058a7331f72SBartlomiej Zolnierkiewicz 		tmu_intstat = EXYNOS_TMU_REG_INTSTAT;
1059a7331f72SBartlomiej Zolnierkiewicz 		tmu_intclear = EXYNOS_TMU_REG_INTCLEAR;
1060a7331f72SBartlomiej Zolnierkiewicz 	}
1061a7331f72SBartlomiej Zolnierkiewicz 
1062a7331f72SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + tmu_intstat);
1063a7331f72SBartlomiej Zolnierkiewicz 	/*
1064a7331f72SBartlomiej Zolnierkiewicz 	 * Clear the interrupts.  Please note that the documentation for
1065a7331f72SBartlomiej Zolnierkiewicz 	 * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
1066a7331f72SBartlomiej Zolnierkiewicz 	 * states that INTCLEAR register has a different placing of bits
1067a7331f72SBartlomiej Zolnierkiewicz 	 * responsible for FALL IRQs than INTSTAT register.  Exynos5420
1068a7331f72SBartlomiej Zolnierkiewicz 	 * and Exynos5440 documentation is correct (Exynos4210 doesn't
1069a7331f72SBartlomiej Zolnierkiewicz 	 * support FALL IRQs at all).
1070a7331f72SBartlomiej Zolnierkiewicz 	 */
1071a7331f72SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + tmu_intclear);
1072a7331f72SBartlomiej Zolnierkiewicz }
1073a7331f72SBartlomiej Zolnierkiewicz 
1074a7331f72SBartlomiej Zolnierkiewicz static void exynos5440_tmu_clear_irqs(struct exynos_tmu_data *data)
1075a7331f72SBartlomiej Zolnierkiewicz {
1076a7331f72SBartlomiej Zolnierkiewicz 	unsigned int val_irq;
1077a7331f72SBartlomiej Zolnierkiewicz 
1078a7331f72SBartlomiej Zolnierkiewicz 	val_irq = readl(data->base + EXYNOS5440_TMU_S0_7_IRQ);
1079a7331f72SBartlomiej Zolnierkiewicz 	/* clear the interrupts */
1080a7331f72SBartlomiej Zolnierkiewicz 	writel(val_irq, data->base + EXYNOS5440_TMU_S0_7_IRQ);
1081a7331f72SBartlomiej Zolnierkiewicz }
1082a7331f72SBartlomiej Zolnierkiewicz 
108359dfa54cSAmit Daniel Kachhap static irqreturn_t exynos_tmu_irq(int irq, void *id)
108459dfa54cSAmit Daniel Kachhap {
108559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = id;
108659dfa54cSAmit Daniel Kachhap 
108759dfa54cSAmit Daniel Kachhap 	disable_irq_nosync(irq);
108859dfa54cSAmit Daniel Kachhap 	schedule_work(&data->irq_work);
108959dfa54cSAmit Daniel Kachhap 
109059dfa54cSAmit Daniel Kachhap 	return IRQ_HANDLED;
109159dfa54cSAmit Daniel Kachhap }
109259dfa54cSAmit Daniel Kachhap 
109359dfa54cSAmit Daniel Kachhap static const struct of_device_id exynos_tmu_match[] = {
1094fee88e2bSMaciej Purski 	{
1095fee88e2bSMaciej Purski 		.compatible = "samsung,exynos3250-tmu",
1096fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS3250,
1097fee88e2bSMaciej Purski 	}, {
1098fee88e2bSMaciej Purski 		.compatible = "samsung,exynos4210-tmu",
1099fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS4210,
1100fee88e2bSMaciej Purski 	}, {
1101fee88e2bSMaciej Purski 		.compatible = "samsung,exynos4412-tmu",
1102fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS4412,
1103fee88e2bSMaciej Purski 	}, {
1104fee88e2bSMaciej Purski 		.compatible = "samsung,exynos5250-tmu",
1105fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS5250,
1106fee88e2bSMaciej Purski 	}, {
1107fee88e2bSMaciej Purski 		.compatible = "samsung,exynos5260-tmu",
1108fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS5260,
1109fee88e2bSMaciej Purski 	}, {
1110fee88e2bSMaciej Purski 		.compatible = "samsung,exynos5420-tmu",
1111fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS5420,
1112fee88e2bSMaciej Purski 	}, {
1113fee88e2bSMaciej Purski 		.compatible = "samsung,exynos5420-tmu-ext-triminfo",
1114fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS5420_TRIMINFO,
1115fee88e2bSMaciej Purski 	}, {
1116fee88e2bSMaciej Purski 		.compatible = "samsung,exynos5433-tmu",
1117fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS5433,
1118fee88e2bSMaciej Purski 	}, {
1119fee88e2bSMaciej Purski 		.compatible = "samsung,exynos5440-tmu",
1120fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS5440,
1121fee88e2bSMaciej Purski 	}, {
1122fee88e2bSMaciej Purski 		.compatible = "samsung,exynos7-tmu",
1123fee88e2bSMaciej Purski 		.data = (const void *)SOC_ARCH_EXYNOS7,
1124fee88e2bSMaciej Purski 	},
1125fee88e2bSMaciej Purski 	{ },
112659dfa54cSAmit Daniel Kachhap };
112759dfa54cSAmit Daniel Kachhap MODULE_DEVICE_TABLE(of, exynos_tmu_match);
112859dfa54cSAmit Daniel Kachhap 
11293b6a1a80SLukasz Majewski static int exynos_of_sensor_conf(struct device_node *np,
11303b6a1a80SLukasz Majewski 				 struct exynos_tmu_platform_data *pdata)
11313b6a1a80SLukasz Majewski {
11323b6a1a80SLukasz Majewski 	u32 value;
11333b6a1a80SLukasz Majewski 	int ret;
11343b6a1a80SLukasz Majewski 
11353b6a1a80SLukasz Majewski 	of_node_get(np);
11363b6a1a80SLukasz Majewski 
11373b6a1a80SLukasz Majewski 	ret = of_property_read_u32(np, "samsung,tmu_gain", &value);
11383b6a1a80SLukasz Majewski 	pdata->gain = (u8)value;
11393b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_reference_voltage", &value);
11403b6a1a80SLukasz Majewski 	pdata->reference_voltage = (u8)value;
11413b6a1a80SLukasz Majewski 
11423b6a1a80SLukasz Majewski 	of_property_read_u32(np, "samsung,tmu_cal_type", &pdata->cal_type);
11433b6a1a80SLukasz Majewski 
11443b6a1a80SLukasz Majewski 	of_node_put(np);
11453b6a1a80SLukasz Majewski 	return 0;
114659dfa54cSAmit Daniel Kachhap }
114759dfa54cSAmit Daniel Kachhap 
1148cebe7373SAmit Daniel Kachhap static int exynos_map_dt_data(struct platform_device *pdev)
114959dfa54cSAmit Daniel Kachhap {
1150cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
1151cebe7373SAmit Daniel Kachhap 	struct exynos_tmu_platform_data *pdata;
1152cebe7373SAmit Daniel Kachhap 	struct resource res;
115359dfa54cSAmit Daniel Kachhap 
115473b5b1d7SSachin Kamat 	if (!data || !pdev->dev.of_node)
1155cebe7373SAmit Daniel Kachhap 		return -ENODEV;
115659dfa54cSAmit Daniel Kachhap 
1157cebe7373SAmit Daniel Kachhap 	data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
1158cebe7373SAmit Daniel Kachhap 	if (data->id < 0)
1159cebe7373SAmit Daniel Kachhap 		data->id = 0;
1160cebe7373SAmit Daniel Kachhap 
1161cebe7373SAmit Daniel Kachhap 	data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1162cebe7373SAmit Daniel Kachhap 	if (data->irq <= 0) {
1163cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get IRQ\n");
1164cebe7373SAmit Daniel Kachhap 		return -ENODEV;
1165cebe7373SAmit Daniel Kachhap 	}
1166cebe7373SAmit Daniel Kachhap 
1167cebe7373SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
1168cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 0\n");
1169cebe7373SAmit Daniel Kachhap 		return -ENODEV;
1170cebe7373SAmit Daniel Kachhap 	}
1171cebe7373SAmit Daniel Kachhap 
1172cebe7373SAmit Daniel Kachhap 	data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
1173cebe7373SAmit Daniel Kachhap 	if (!data->base) {
1174cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
1175cebe7373SAmit Daniel Kachhap 		return -EADDRNOTAVAIL;
1176cebe7373SAmit Daniel Kachhap 	}
1177cebe7373SAmit Daniel Kachhap 
11783b6a1a80SLukasz Majewski 	pdata = devm_kzalloc(&pdev->dev,
11793b6a1a80SLukasz Majewski 			     sizeof(struct exynos_tmu_platform_data),
11803b6a1a80SLukasz Majewski 			     GFP_KERNEL);
11813b6a1a80SLukasz Majewski 	if (!pdata)
11823b6a1a80SLukasz Majewski 		return -ENOMEM;
118356adb9efSBartlomiej Zolnierkiewicz 
11843b6a1a80SLukasz Majewski 	exynos_of_sensor_conf(pdev->dev.of_node, pdata);
1185cebe7373SAmit Daniel Kachhap 	data->pdata = pdata;
1186fee88e2bSMaciej Purski 	data->soc = (enum soc_type)of_device_get_match_data(&pdev->dev);
118756adb9efSBartlomiej Zolnierkiewicz 
118856adb9efSBartlomiej Zolnierkiewicz 	switch (data->soc) {
118956adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4210:
119056adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4210_tmu_initialize;
119156adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
119256adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4210_tmu_read;
119356adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
11943a3a5f15SKrzysztof Kozlowski 		data->ntrip = 4;
1195*e3ed3649SBartlomiej Zolnierkiewicz 		data->efuse_value = 55;
1196*e3ed3649SBartlomiej Zolnierkiewicz 		data->min_efuse_value = 40;
1197*e3ed3649SBartlomiej Zolnierkiewicz 		data->max_efuse_value = 100;
119856adb9efSBartlomiej Zolnierkiewicz 		break;
119956adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS3250:
120056adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS4412:
120156adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5250:
120256adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5260:
120356adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420:
120456adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5420_TRIMINFO:
120556adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos4412_tmu_initialize;
120656adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos4210_tmu_control;
120756adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos4412_tmu_read;
120856adb9efSBartlomiej Zolnierkiewicz 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
120956adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12103a3a5f15SKrzysztof Kozlowski 		data->ntrip = 4;
1211*e3ed3649SBartlomiej Zolnierkiewicz 		data->efuse_value = 55;
1212*e3ed3649SBartlomiej Zolnierkiewicz 		if (data->soc != SOC_ARCH_EXYNOS5420 &&
1213*e3ed3649SBartlomiej Zolnierkiewicz 		    data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO)
1214*e3ed3649SBartlomiej Zolnierkiewicz 			data->min_efuse_value = 40;
1215*e3ed3649SBartlomiej Zolnierkiewicz 		else
1216*e3ed3649SBartlomiej Zolnierkiewicz 			data->min_efuse_value = 0;
1217*e3ed3649SBartlomiej Zolnierkiewicz 		data->max_efuse_value = 100;
121856adb9efSBartlomiej Zolnierkiewicz 		break;
1219488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS5433:
1220488c7455SChanwoo Choi 		data->tmu_initialize = exynos5433_tmu_initialize;
1221488c7455SChanwoo Choi 		data->tmu_control = exynos5433_tmu_control;
1222488c7455SChanwoo Choi 		data->tmu_read = exynos4412_tmu_read;
1223488c7455SChanwoo Choi 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
1224488c7455SChanwoo Choi 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12253a3a5f15SKrzysztof Kozlowski 		data->ntrip = 8;
1226*e3ed3649SBartlomiej Zolnierkiewicz 		data->efuse_value = 75;
1227*e3ed3649SBartlomiej Zolnierkiewicz 		data->min_efuse_value = 40;
1228*e3ed3649SBartlomiej Zolnierkiewicz 		data->max_efuse_value = 150;
1229488c7455SChanwoo Choi 		break;
123056adb9efSBartlomiej Zolnierkiewicz 	case SOC_ARCH_EXYNOS5440:
123156adb9efSBartlomiej Zolnierkiewicz 		data->tmu_initialize = exynos5440_tmu_initialize;
123256adb9efSBartlomiej Zolnierkiewicz 		data->tmu_control = exynos5440_tmu_control;
123356adb9efSBartlomiej Zolnierkiewicz 		data->tmu_read = exynos5440_tmu_read;
123456adb9efSBartlomiej Zolnierkiewicz 		data->tmu_set_emulation = exynos5440_tmu_set_emulation;
123556adb9efSBartlomiej Zolnierkiewicz 		data->tmu_clear_irqs = exynos5440_tmu_clear_irqs;
12363a3a5f15SKrzysztof Kozlowski 		data->ntrip = 4;
1237*e3ed3649SBartlomiej Zolnierkiewicz 		data->efuse_value = 0x5d2d;
1238*e3ed3649SBartlomiej Zolnierkiewicz 		data->min_efuse_value = 16;
1239*e3ed3649SBartlomiej Zolnierkiewicz 		data->max_efuse_value = 76;
124056adb9efSBartlomiej Zolnierkiewicz 		break;
12416c247393SAbhilash Kesavan 	case SOC_ARCH_EXYNOS7:
12426c247393SAbhilash Kesavan 		data->tmu_initialize = exynos7_tmu_initialize;
12436c247393SAbhilash Kesavan 		data->tmu_control = exynos7_tmu_control;
12446c247393SAbhilash Kesavan 		data->tmu_read = exynos7_tmu_read;
12456c247393SAbhilash Kesavan 		data->tmu_set_emulation = exynos4412_tmu_set_emulation;
12466c247393SAbhilash Kesavan 		data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
12473a3a5f15SKrzysztof Kozlowski 		data->ntrip = 8;
1248*e3ed3649SBartlomiej Zolnierkiewicz 		data->efuse_value = 75;
1249*e3ed3649SBartlomiej Zolnierkiewicz 		data->min_efuse_value = 15;
1250*e3ed3649SBartlomiej Zolnierkiewicz 		data->max_efuse_value = 100;
12516c247393SAbhilash Kesavan 		break;
125256adb9efSBartlomiej Zolnierkiewicz 	default:
125356adb9efSBartlomiej Zolnierkiewicz 		dev_err(&pdev->dev, "Platform not supported\n");
125456adb9efSBartlomiej Zolnierkiewicz 		return -EINVAL;
125556adb9efSBartlomiej Zolnierkiewicz 	}
125656adb9efSBartlomiej Zolnierkiewicz 
1257d9b6ee14SAmit Daniel Kachhap 	/*
1258d9b6ee14SAmit Daniel Kachhap 	 * Check if the TMU shares some registers and then try to map the
1259d9b6ee14SAmit Daniel Kachhap 	 * memory of common registers.
1260d9b6ee14SAmit Daniel Kachhap 	 */
126156adb9efSBartlomiej Zolnierkiewicz 	if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO &&
126256adb9efSBartlomiej Zolnierkiewicz 	    data->soc != SOC_ARCH_EXYNOS5440)
1263d9b6ee14SAmit Daniel Kachhap 		return 0;
1264d9b6ee14SAmit Daniel Kachhap 
1265d9b6ee14SAmit Daniel Kachhap 	if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
1266d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "failed to get Resource 1\n");
1267d9b6ee14SAmit Daniel Kachhap 		return -ENODEV;
1268d9b6ee14SAmit Daniel Kachhap 	}
1269d9b6ee14SAmit Daniel Kachhap 
12709025d563SNaveen Krishna Chatradhi 	data->base_second = devm_ioremap(&pdev->dev, res.start,
1271d9b6ee14SAmit Daniel Kachhap 					resource_size(&res));
12729025d563SNaveen Krishna Chatradhi 	if (!data->base_second) {
1273d9b6ee14SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to ioremap memory\n");
1274d9b6ee14SAmit Daniel Kachhap 		return -ENOMEM;
1275d9b6ee14SAmit Daniel Kachhap 	}
1276cebe7373SAmit Daniel Kachhap 
1277cebe7373SAmit Daniel Kachhap 	return 0;
1278cebe7373SAmit Daniel Kachhap }
1279cebe7373SAmit Daniel Kachhap 
1280c3c04d9dSJulia Lawall static const struct thermal_zone_of_device_ops exynos_sensor_ops = {
12813b6a1a80SLukasz Majewski 	.get_temp = exynos_get_temp,
12823b6a1a80SLukasz Majewski 	.set_emul_temp = exynos_tmu_set_emulation,
12833b6a1a80SLukasz Majewski };
12843b6a1a80SLukasz Majewski 
1285cebe7373SAmit Daniel Kachhap static int exynos_tmu_probe(struct platform_device *pdev)
1286cebe7373SAmit Daniel Kachhap {
12873b6a1a80SLukasz Majewski 	struct exynos_tmu_data *data;
12883b6a1a80SLukasz Majewski 	int ret;
1289cebe7373SAmit Daniel Kachhap 
129059dfa54cSAmit Daniel Kachhap 	data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
129159dfa54cSAmit Daniel Kachhap 					GFP_KERNEL);
12922a9675b3SJingoo Han 	if (!data)
129359dfa54cSAmit Daniel Kachhap 		return -ENOMEM;
129459dfa54cSAmit Daniel Kachhap 
1295cebe7373SAmit Daniel Kachhap 	platform_set_drvdata(pdev, data);
1296cebe7373SAmit Daniel Kachhap 	mutex_init(&data->lock);
1297cebe7373SAmit Daniel Kachhap 
1298824ead03SKrzysztof Kozlowski 	/*
1299824ead03SKrzysztof Kozlowski 	 * Try enabling the regulator if found
1300824ead03SKrzysztof Kozlowski 	 * TODO: Add regulator as an SOC feature, so that regulator enable
1301824ead03SKrzysztof Kozlowski 	 * is a compulsory call.
1302824ead03SKrzysztof Kozlowski 	 */
13034d3583cdSJavier Martinez Canillas 	data->regulator = devm_regulator_get_optional(&pdev->dev, "vtmu");
1304824ead03SKrzysztof Kozlowski 	if (!IS_ERR(data->regulator)) {
1305824ead03SKrzysztof Kozlowski 		ret = regulator_enable(data->regulator);
1306824ead03SKrzysztof Kozlowski 		if (ret) {
1307824ead03SKrzysztof Kozlowski 			dev_err(&pdev->dev, "failed to enable vtmu\n");
1308824ead03SKrzysztof Kozlowski 			return ret;
13093b6a1a80SLukasz Majewski 		}
1310824ead03SKrzysztof Kozlowski 	} else {
1311ccb361d2SJavier Martinez Canillas 		if (PTR_ERR(data->regulator) == -EPROBE_DEFER)
1312ccb361d2SJavier Martinez Canillas 			return -EPROBE_DEFER;
1313824ead03SKrzysztof Kozlowski 		dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
1314824ead03SKrzysztof Kozlowski 	}
1315824ead03SKrzysztof Kozlowski 
1316cebe7373SAmit Daniel Kachhap 	ret = exynos_map_dt_data(pdev);
1317cebe7373SAmit Daniel Kachhap 	if (ret)
13183b6a1a80SLukasz Majewski 		goto err_sensor;
1319cebe7373SAmit Daniel Kachhap 
132059dfa54cSAmit Daniel Kachhap 	INIT_WORK(&data->irq_work, exynos_tmu_work);
132159dfa54cSAmit Daniel Kachhap 
132259dfa54cSAmit Daniel Kachhap 	data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
132359dfa54cSAmit Daniel Kachhap 	if (IS_ERR(data->clk)) {
132459dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to get clock\n");
13253b6a1a80SLukasz Majewski 		ret = PTR_ERR(data->clk);
13263b6a1a80SLukasz Majewski 		goto err_sensor;
132759dfa54cSAmit Daniel Kachhap 	}
132859dfa54cSAmit Daniel Kachhap 
132914a11dc7SNaveen Krishna Chatradhi 	data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
133014a11dc7SNaveen Krishna Chatradhi 	if (IS_ERR(data->clk_sec)) {
133114a11dc7SNaveen Krishna Chatradhi 		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
133214a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get triminfo clock\n");
13333b6a1a80SLukasz Majewski 			ret = PTR_ERR(data->clk_sec);
13343b6a1a80SLukasz Majewski 			goto err_sensor;
133514a11dc7SNaveen Krishna Chatradhi 		}
133614a11dc7SNaveen Krishna Chatradhi 	} else {
133714a11dc7SNaveen Krishna Chatradhi 		ret = clk_prepare(data->clk_sec);
133814a11dc7SNaveen Krishna Chatradhi 		if (ret) {
133914a11dc7SNaveen Krishna Chatradhi 			dev_err(&pdev->dev, "Failed to get clock\n");
13403b6a1a80SLukasz Majewski 			goto err_sensor;
134114a11dc7SNaveen Krishna Chatradhi 		}
134214a11dc7SNaveen Krishna Chatradhi 	}
134314a11dc7SNaveen Krishna Chatradhi 
134414a11dc7SNaveen Krishna Chatradhi 	ret = clk_prepare(data->clk);
134514a11dc7SNaveen Krishna Chatradhi 	if (ret) {
134614a11dc7SNaveen Krishna Chatradhi 		dev_err(&pdev->dev, "Failed to get clock\n");
134714a11dc7SNaveen Krishna Chatradhi 		goto err_clk_sec;
134814a11dc7SNaveen Krishna Chatradhi 	}
134959dfa54cSAmit Daniel Kachhap 
1350488c7455SChanwoo Choi 	switch (data->soc) {
1351488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS5433:
1352488c7455SChanwoo Choi 	case SOC_ARCH_EXYNOS7:
13536c247393SAbhilash Kesavan 		data->sclk = devm_clk_get(&pdev->dev, "tmu_sclk");
13546c247393SAbhilash Kesavan 		if (IS_ERR(data->sclk)) {
13556c247393SAbhilash Kesavan 			dev_err(&pdev->dev, "Failed to get sclk\n");
13566c247393SAbhilash Kesavan 			goto err_clk;
13576c247393SAbhilash Kesavan 		} else {
13586c247393SAbhilash Kesavan 			ret = clk_prepare_enable(data->sclk);
13596c247393SAbhilash Kesavan 			if (ret) {
13606c247393SAbhilash Kesavan 				dev_err(&pdev->dev, "Failed to enable sclk\n");
13616c247393SAbhilash Kesavan 				goto err_clk;
13626c247393SAbhilash Kesavan 			}
13636c247393SAbhilash Kesavan 		}
1364488c7455SChanwoo Choi 		break;
1365488c7455SChanwoo Choi 	default:
1366488c7455SChanwoo Choi 		break;
1367baba1ebbSKrzysztof Kozlowski 	}
13686c247393SAbhilash Kesavan 
13699e4249b4SKrzysztof Kozlowski 	/*
13709e4249b4SKrzysztof Kozlowski 	 * data->tzd must be registered before calling exynos_tmu_initialize(),
13719e4249b4SKrzysztof Kozlowski 	 * requesting irq and calling exynos_tmu_control().
13729e4249b4SKrzysztof Kozlowski 	 */
13739e4249b4SKrzysztof Kozlowski 	data->tzd = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
13749e4249b4SKrzysztof Kozlowski 						    &exynos_sensor_ops);
13759e4249b4SKrzysztof Kozlowski 	if (IS_ERR(data->tzd)) {
13769e4249b4SKrzysztof Kozlowski 		ret = PTR_ERR(data->tzd);
13779e4249b4SKrzysztof Kozlowski 		dev_err(&pdev->dev, "Failed to register sensor: %d\n", ret);
13789e4249b4SKrzysztof Kozlowski 		goto err_sclk;
13799e4249b4SKrzysztof Kozlowski 	}
138059dfa54cSAmit Daniel Kachhap 
138159dfa54cSAmit Daniel Kachhap 	ret = exynos_tmu_initialize(pdev);
138259dfa54cSAmit Daniel Kachhap 	if (ret) {
138359dfa54cSAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to initialize TMU\n");
13849e4249b4SKrzysztof Kozlowski 		goto err_thermal;
138559dfa54cSAmit Daniel Kachhap 	}
138659dfa54cSAmit Daniel Kachhap 
1387cebe7373SAmit Daniel Kachhap 	ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
1388cebe7373SAmit Daniel Kachhap 		IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
1389cebe7373SAmit Daniel Kachhap 	if (ret) {
1390cebe7373SAmit Daniel Kachhap 		dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
13919e4249b4SKrzysztof Kozlowski 		goto err_thermal;
1392cebe7373SAmit Daniel Kachhap 	}
139359dfa54cSAmit Daniel Kachhap 
13943b6a1a80SLukasz Majewski 	exynos_tmu_control(pdev, true);
139559dfa54cSAmit Daniel Kachhap 	return 0;
13969e4249b4SKrzysztof Kozlowski 
13979e4249b4SKrzysztof Kozlowski err_thermal:
13989e4249b4SKrzysztof Kozlowski 	thermal_zone_of_sensor_unregister(&pdev->dev, data->tzd);
13996c247393SAbhilash Kesavan err_sclk:
14006c247393SAbhilash Kesavan 	clk_disable_unprepare(data->sclk);
140159dfa54cSAmit Daniel Kachhap err_clk:
140259dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
140314a11dc7SNaveen Krishna Chatradhi err_clk_sec:
140414a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
140514a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
14063b6a1a80SLukasz Majewski err_sensor:
1407bfa26838SKrzysztof Kozlowski 	if (!IS_ERR(data->regulator))
14085f09a5cbSKrzysztof Kozlowski 		regulator_disable(data->regulator);
14093b6a1a80SLukasz Majewski 
141059dfa54cSAmit Daniel Kachhap 	return ret;
141159dfa54cSAmit Daniel Kachhap }
141259dfa54cSAmit Daniel Kachhap 
141359dfa54cSAmit Daniel Kachhap static int exynos_tmu_remove(struct platform_device *pdev)
141459dfa54cSAmit Daniel Kachhap {
141559dfa54cSAmit Daniel Kachhap 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
14163b6a1a80SLukasz Majewski 	struct thermal_zone_device *tzd = data->tzd;
141759dfa54cSAmit Daniel Kachhap 
14183b6a1a80SLukasz Majewski 	thermal_zone_of_sensor_unregister(&pdev->dev, tzd);
14194215688eSBartlomiej Zolnierkiewicz 	exynos_tmu_control(pdev, false);
14204215688eSBartlomiej Zolnierkiewicz 
14216c247393SAbhilash Kesavan 	clk_disable_unprepare(data->sclk);
142259dfa54cSAmit Daniel Kachhap 	clk_unprepare(data->clk);
142314a11dc7SNaveen Krishna Chatradhi 	if (!IS_ERR(data->clk_sec))
142414a11dc7SNaveen Krishna Chatradhi 		clk_unprepare(data->clk_sec);
142559dfa54cSAmit Daniel Kachhap 
1426498d22f6SAmit Daniel Kachhap 	if (!IS_ERR(data->regulator))
1427498d22f6SAmit Daniel Kachhap 		regulator_disable(data->regulator);
1428498d22f6SAmit Daniel Kachhap 
142959dfa54cSAmit Daniel Kachhap 	return 0;
143059dfa54cSAmit Daniel Kachhap }
143159dfa54cSAmit Daniel Kachhap 
143259dfa54cSAmit Daniel Kachhap #ifdef CONFIG_PM_SLEEP
143359dfa54cSAmit Daniel Kachhap static int exynos_tmu_suspend(struct device *dev)
143459dfa54cSAmit Daniel Kachhap {
143559dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(to_platform_device(dev), false);
143659dfa54cSAmit Daniel Kachhap 
143759dfa54cSAmit Daniel Kachhap 	return 0;
143859dfa54cSAmit Daniel Kachhap }
143959dfa54cSAmit Daniel Kachhap 
144059dfa54cSAmit Daniel Kachhap static int exynos_tmu_resume(struct device *dev)
144159dfa54cSAmit Daniel Kachhap {
144259dfa54cSAmit Daniel Kachhap 	struct platform_device *pdev = to_platform_device(dev);
144359dfa54cSAmit Daniel Kachhap 
144459dfa54cSAmit Daniel Kachhap 	exynos_tmu_initialize(pdev);
144559dfa54cSAmit Daniel Kachhap 	exynos_tmu_control(pdev, true);
144659dfa54cSAmit Daniel Kachhap 
144759dfa54cSAmit Daniel Kachhap 	return 0;
144859dfa54cSAmit Daniel Kachhap }
144959dfa54cSAmit Daniel Kachhap 
145059dfa54cSAmit Daniel Kachhap static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
145159dfa54cSAmit Daniel Kachhap 			 exynos_tmu_suspend, exynos_tmu_resume);
145259dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	(&exynos_tmu_pm)
145359dfa54cSAmit Daniel Kachhap #else
145459dfa54cSAmit Daniel Kachhap #define EXYNOS_TMU_PM	NULL
145559dfa54cSAmit Daniel Kachhap #endif
145659dfa54cSAmit Daniel Kachhap 
145759dfa54cSAmit Daniel Kachhap static struct platform_driver exynos_tmu_driver = {
145859dfa54cSAmit Daniel Kachhap 	.driver = {
145959dfa54cSAmit Daniel Kachhap 		.name   = "exynos-tmu",
146059dfa54cSAmit Daniel Kachhap 		.pm     = EXYNOS_TMU_PM,
146173b5b1d7SSachin Kamat 		.of_match_table = exynos_tmu_match,
146259dfa54cSAmit Daniel Kachhap 	},
146359dfa54cSAmit Daniel Kachhap 	.probe = exynos_tmu_probe,
146459dfa54cSAmit Daniel Kachhap 	.remove	= exynos_tmu_remove,
146559dfa54cSAmit Daniel Kachhap };
146659dfa54cSAmit Daniel Kachhap 
146759dfa54cSAmit Daniel Kachhap module_platform_driver(exynos_tmu_driver);
146859dfa54cSAmit Daniel Kachhap 
146959dfa54cSAmit Daniel Kachhap MODULE_DESCRIPTION("EXYNOS TMU Driver");
147059dfa54cSAmit Daniel Kachhap MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
147159dfa54cSAmit Daniel Kachhap MODULE_LICENSE("GPL");
147259dfa54cSAmit Daniel Kachhap MODULE_ALIAS("platform:exynos-tmu");
1473