1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  * Author: Hanyi Wu <hanyi.wu@mediatek.com>
5  *         Sascha Hauer <s.hauer@pengutronix.de>
6  *         Dawei Chien <dawei.chien@mediatek.com>
7  *         Louis Yu <louis.yu@mediatek.com>
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-consumer.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/io.h>
22 #include <linux/thermal.h>
23 #include <linux/reset.h>
24 #include <linux/types.h>
25 
26 #include "../thermal_hwmon.h"
27 
28 /* AUXADC Registers */
29 #define AUXADC_CON1_SET_V	0x008
30 #define AUXADC_CON1_CLR_V	0x00c
31 #define AUXADC_CON2_V		0x010
32 #define AUXADC_DATA(channel)	(0x14 + (channel) * 4)
33 
34 #define APMIXED_SYS_TS_CON1	0x604
35 
36 /* Thermal Controller Registers */
37 #define TEMP_MONCTL0		0x000
38 #define TEMP_MONCTL1		0x004
39 #define TEMP_MONCTL2		0x008
40 #define TEMP_MONIDET0		0x014
41 #define TEMP_MONIDET1		0x018
42 #define TEMP_MSRCTL0		0x038
43 #define TEMP_MSRCTL1		0x03c
44 #define TEMP_AHBPOLL		0x040
45 #define TEMP_AHBTO		0x044
46 #define TEMP_ADCPNP0		0x048
47 #define TEMP_ADCPNP1		0x04c
48 #define TEMP_ADCPNP2		0x050
49 #define TEMP_ADCPNP3		0x0b4
50 
51 #define TEMP_ADCMUX		0x054
52 #define TEMP_ADCEN		0x060
53 #define TEMP_PNPMUXADDR		0x064
54 #define TEMP_ADCMUXADDR		0x068
55 #define TEMP_ADCENADDR		0x074
56 #define TEMP_ADCVALIDADDR	0x078
57 #define TEMP_ADCVOLTADDR	0x07c
58 #define TEMP_RDCTRL		0x080
59 #define TEMP_ADCVALIDMASK	0x084
60 #define TEMP_ADCVOLTAGESHIFT	0x088
61 #define TEMP_ADCWRITECTRL	0x08c
62 #define TEMP_MSR0		0x090
63 #define TEMP_MSR1		0x094
64 #define TEMP_MSR2		0x098
65 #define TEMP_MSR3		0x0B8
66 
67 #define TEMP_SPARE0		0x0f0
68 
69 #define TEMP_ADCPNP0_1          0x148
70 #define TEMP_ADCPNP1_1          0x14c
71 #define TEMP_ADCPNP2_1          0x150
72 #define TEMP_MSR0_1             0x190
73 #define TEMP_MSR1_1             0x194
74 #define TEMP_MSR2_1             0x198
75 #define TEMP_ADCPNP3_1          0x1b4
76 #define TEMP_MSR3_1             0x1B8
77 
78 #define PTPCORESEL		0x400
79 
80 #define TEMP_MONCTL1_PERIOD_UNIT(x)	((x) & 0x3ff)
81 
82 #define TEMP_MONCTL2_FILTER_INTERVAL(x)	(((x) & 0x3ff) << 16)
83 #define TEMP_MONCTL2_SENSOR_INTERVAL(x)	((x) & 0x3ff)
84 
85 #define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x)	(x)
86 
87 #define TEMP_ADCWRITECTRL_ADC_PNP_WRITE		BIT(0)
88 #define TEMP_ADCWRITECTRL_ADC_MUX_WRITE		BIT(1)
89 
90 #define TEMP_ADCVALIDMASK_VALID_HIGH		BIT(5)
91 #define TEMP_ADCVALIDMASK_VALID_POS(bit)	(bit)
92 
93 /* MT8173 thermal sensors */
94 #define MT8173_TS1	0
95 #define MT8173_TS2	1
96 #define MT8173_TS3	2
97 #define MT8173_TS4	3
98 #define MT8173_TSABB	4
99 
100 /* AUXADC channel 11 is used for the temperature sensors */
101 #define MT8173_TEMP_AUXADC_CHANNEL	11
102 
103 /* The total number of temperature sensors in the MT8173 */
104 #define MT8173_NUM_SENSORS		5
105 
106 /* The number of banks in the MT8173 */
107 #define MT8173_NUM_ZONES		4
108 
109 /* The number of sensing points per bank */
110 #define MT8173_NUM_SENSORS_PER_ZONE	4
111 
112 /* The number of controller in the MT8173 */
113 #define MT8173_NUM_CONTROLLER		1
114 
115 /* The calibration coefficient of sensor  */
116 #define MT8173_CALIBRATION	165
117 
118 /*
119  * Layout of the fuses providing the calibration data
120  * These macros could be used for MT8183, MT8173, MT2701, and MT2712.
121  * MT8183 has 6 sensors and needs 6 VTS calibration data.
122  * MT8173 has 5 sensors and needs 5 VTS calibration data.
123  * MT2701 has 3 sensors and needs 3 VTS calibration data.
124  * MT2712 has 4 sensors and needs 4 VTS calibration data.
125  */
126 #define CALIB_BUF0_VALID_V1		BIT(0)
127 #define CALIB_BUF1_ADC_GE_V1(x)		(((x) >> 22) & 0x3ff)
128 #define CALIB_BUF0_VTS_TS1_V1(x)	(((x) >> 17) & 0x1ff)
129 #define CALIB_BUF0_VTS_TS2_V1(x)	(((x) >> 8) & 0x1ff)
130 #define CALIB_BUF1_VTS_TS3_V1(x)	(((x) >> 0) & 0x1ff)
131 #define CALIB_BUF2_VTS_TS4_V1(x)	(((x) >> 23) & 0x1ff)
132 #define CALIB_BUF2_VTS_TS5_V1(x)	(((x) >> 5) & 0x1ff)
133 #define CALIB_BUF2_VTS_TSABB_V1(x)	(((x) >> 14) & 0x1ff)
134 #define CALIB_BUF0_DEGC_CALI_V1(x)	(((x) >> 1) & 0x3f)
135 #define CALIB_BUF0_O_SLOPE_V1(x)	(((x) >> 26) & 0x3f)
136 #define CALIB_BUF0_O_SLOPE_SIGN_V1(x)	(((x) >> 7) & 0x1)
137 #define CALIB_BUF1_ID_V1(x)		(((x) >> 9) & 0x1)
138 
139 /*
140  * Layout of the fuses providing the calibration data
141  * These macros could be used for MT7622.
142  */
143 #define CALIB_BUF0_ADC_OE_V2(x)		(((x) >> 22) & 0x3ff)
144 #define CALIB_BUF0_ADC_GE_V2(x)		(((x) >> 12) & 0x3ff)
145 #define CALIB_BUF0_DEGC_CALI_V2(x)	(((x) >> 6) & 0x3f)
146 #define CALIB_BUF0_O_SLOPE_V2(x)	(((x) >> 0) & 0x3f)
147 #define CALIB_BUF1_VTS_TS1_V2(x)	(((x) >> 23) & 0x1ff)
148 #define CALIB_BUF1_VTS_TS2_V2(x)	(((x) >> 14) & 0x1ff)
149 #define CALIB_BUF1_VTS_TSABB_V2(x)	(((x) >> 5) & 0x1ff)
150 #define CALIB_BUF1_VALID_V2(x)		(((x) >> 4) & 0x1)
151 #define CALIB_BUF1_O_SLOPE_SIGN_V2(x)	(((x) >> 3) & 0x1)
152 
153 /*
154  * Layout of the fuses providing the calibration data
155  * These macros can be used for MT7981 and MT7986.
156  */
157 #define CALIB_BUF0_ADC_GE_V3(x)		(((x) >> 0) & 0x3ff)
158 #define CALIB_BUF0_DEGC_CALI_V3(x)	(((x) >> 20) & 0x3f)
159 #define CALIB_BUF0_O_SLOPE_V3(x)	(((x) >> 26) & 0x3f)
160 #define CALIB_BUF1_VTS_TS1_V3(x)	(((x) >> 0) & 0x1ff)
161 #define CALIB_BUF1_VTS_TS2_V3(x)	(((x) >> 21) & 0x1ff)
162 #define CALIB_BUF1_VTS_TSABB_V3(x)	(((x) >> 9) & 0x1ff)
163 #define CALIB_BUF1_VALID_V3(x)		(((x) >> 18) & 0x1)
164 #define CALIB_BUF1_O_SLOPE_SIGN_V3(x)	(((x) >> 19) & 0x1)
165 #define CALIB_BUF1_ID_V3(x)		(((x) >> 20) & 0x1)
166 
167 enum {
168 	VTS1,
169 	VTS2,
170 	VTS3,
171 	VTS4,
172 	VTS5,
173 	VTSABB,
174 	MAX_NUM_VTS,
175 };
176 
177 enum mtk_thermal_version {
178 	MTK_THERMAL_V1 = 1,
179 	MTK_THERMAL_V2,
180 	MTK_THERMAL_V3,
181 };
182 
183 /* MT2701 thermal sensors */
184 #define MT2701_TS1	0
185 #define MT2701_TS2	1
186 #define MT2701_TSABB	2
187 
188 /* AUXADC channel 11 is used for the temperature sensors */
189 #define MT2701_TEMP_AUXADC_CHANNEL	11
190 
191 /* The total number of temperature sensors in the MT2701 */
192 #define MT2701_NUM_SENSORS	3
193 
194 /* The number of sensing points per bank */
195 #define MT2701_NUM_SENSORS_PER_ZONE	3
196 
197 /* The number of controller in the MT2701 */
198 #define MT2701_NUM_CONTROLLER		1
199 
200 /* The calibration coefficient of sensor  */
201 #define MT2701_CALIBRATION	165
202 
203 /* MT2712 thermal sensors */
204 #define MT2712_TS1	0
205 #define MT2712_TS2	1
206 #define MT2712_TS3	2
207 #define MT2712_TS4	3
208 
209 /* AUXADC channel 11 is used for the temperature sensors */
210 #define MT2712_TEMP_AUXADC_CHANNEL	11
211 
212 /* The total number of temperature sensors in the MT2712 */
213 #define MT2712_NUM_SENSORS	4
214 
215 /* The number of sensing points per bank */
216 #define MT2712_NUM_SENSORS_PER_ZONE	4
217 
218 /* The number of controller in the MT2712 */
219 #define MT2712_NUM_CONTROLLER		1
220 
221 /* The calibration coefficient of sensor  */
222 #define MT2712_CALIBRATION	165
223 
224 #define MT7622_TEMP_AUXADC_CHANNEL	11
225 #define MT7622_NUM_SENSORS		1
226 #define MT7622_NUM_ZONES		1
227 #define MT7622_NUM_SENSORS_PER_ZONE	1
228 #define MT7622_TS1	0
229 #define MT7622_NUM_CONTROLLER		1
230 
231 /* The maximum number of banks */
232 #define MAX_NUM_ZONES		8
233 
234 /* The calibration coefficient of sensor  */
235 #define MT7622_CALIBRATION	165
236 
237 /* MT8183 thermal sensors */
238 #define MT8183_TS1	0
239 #define MT8183_TS2	1
240 #define MT8183_TS3	2
241 #define MT8183_TS4	3
242 #define MT8183_TS5	4
243 #define MT8183_TSABB	5
244 
245 /* AUXADC channel  is used for the temperature sensors */
246 #define MT8183_TEMP_AUXADC_CHANNEL	11
247 
248 /* The total number of temperature sensors in the MT8183 */
249 #define MT8183_NUM_SENSORS	6
250 
251 /* The number of banks in the MT8183 */
252 #define MT8183_NUM_ZONES               1
253 
254 /* The number of sensing points per bank */
255 #define MT8183_NUM_SENSORS_PER_ZONE	 6
256 
257 /* The number of controller in the MT8183 */
258 #define MT8183_NUM_CONTROLLER		2
259 
260 /* The calibration coefficient of sensor  */
261 #define MT8183_CALIBRATION	153
262 
263 /* AUXADC channel 11 is used for the temperature sensors */
264 #define MT7986_TEMP_AUXADC_CHANNEL	11
265 
266 /* The total number of temperature sensors in the MT7986 */
267 #define MT7986_NUM_SENSORS		1
268 
269 /* The number of banks in the MT7986 */
270 #define MT7986_NUM_ZONES		1
271 
272 /* The number of sensing points per bank */
273 #define MT7986_NUM_SENSORS_PER_ZONE	1
274 
275 /* MT7986 thermal sensors */
276 #define MT7986_TS1			0
277 
278 /* The number of controller in the MT7986 */
279 #define MT7986_NUM_CONTROLLER		1
280 
281 /* The calibration coefficient of sensor  */
282 #define MT7986_CALIBRATION		165
283 
284 struct mtk_thermal;
285 
286 struct thermal_bank_cfg {
287 	unsigned int num_sensors;
288 	const int *sensors;
289 };
290 
291 struct mtk_thermal_bank {
292 	struct mtk_thermal *mt;
293 	int id;
294 };
295 
296 struct mtk_thermal_data {
297 	s32 num_banks;
298 	s32 num_sensors;
299 	s32 auxadc_channel;
300 	const int *vts_index;
301 	const int *sensor_mux_values;
302 	const int *msr;
303 	const int *adcpnp;
304 	const int cali_val;
305 	const int num_controller;
306 	const int *controller_offset;
307 	bool need_switch_bank;
308 	struct thermal_bank_cfg bank_data[MAX_NUM_ZONES];
309 	enum mtk_thermal_version version;
310 };
311 
312 struct mtk_thermal {
313 	struct device *dev;
314 	void __iomem *thermal_base;
315 
316 	struct clk *clk_peri_therm;
317 	struct clk *clk_auxadc;
318 	/* lock: for getting and putting banks */
319 	struct mutex lock;
320 
321 	/* Calibration values */
322 	s32 adc_ge;
323 	s32 adc_oe;
324 	s32 degc_cali;
325 	s32 o_slope;
326 	s32 o_slope_sign;
327 	s32 vts[MAX_NUM_VTS];
328 
329 	const struct mtk_thermal_data *conf;
330 	struct mtk_thermal_bank banks[MAX_NUM_ZONES];
331 
332 	int (*raw_to_mcelsius)(struct mtk_thermal *mt, int sensno, s32 raw);
333 };
334 
335 /* MT8183 thermal sensor data */
336 static const int mt8183_bank_data[MT8183_NUM_SENSORS] = {
337 	MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB
338 };
339 
340 static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = {
341 	TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1
342 };
343 
344 static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = {
345 	TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1,
346 	TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1
347 };
348 
349 static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 };
350 static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100};
351 
352 static const int mt8183_vts_index[MT8183_NUM_SENSORS] = {
353 	VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB
354 };
355 
356 /* MT8173 thermal sensor data */
357 static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
358 	{ MT8173_TS2, MT8173_TS3 },
359 	{ MT8173_TS2, MT8173_TS4 },
360 	{ MT8173_TS1, MT8173_TS2, MT8173_TSABB },
361 	{ MT8173_TS2 },
362 };
363 
364 static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
365 	TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
366 };
367 
368 static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
369 	TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
370 };
371 
372 static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
373 static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, };
374 
375 static const int mt8173_vts_index[MT8173_NUM_SENSORS] = {
376 	VTS1, VTS2, VTS3, VTS4, VTSABB
377 };
378 
379 /* MT2701 thermal sensor data */
380 static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
381 	MT2701_TS1, MT2701_TS2, MT2701_TSABB
382 };
383 
384 static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
385 	TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
386 };
387 
388 static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
389 	TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
390 };
391 
392 static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
393 static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, };
394 
395 static const int mt2701_vts_index[MT2701_NUM_SENSORS] = {
396 	VTS1, VTS2, VTS3
397 };
398 
399 /* MT2712 thermal sensor data */
400 static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
401 	MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
402 };
403 
404 static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
405 	TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
406 };
407 
408 static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
409 	TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
410 };
411 
412 static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
413 static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, };
414 
415 static const int mt2712_vts_index[MT2712_NUM_SENSORS] = {
416 	VTS1, VTS2, VTS3, VTS4
417 };
418 
419 /* MT7622 thermal sensor data */
420 static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
421 static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
422 static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
423 static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
424 static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
425 static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
426 
427 /* MT7986 thermal sensor data */
428 static const int mt7986_bank_data[MT7986_NUM_SENSORS] = { MT7986_TS1, };
429 static const int mt7986_msr[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
430 static const int mt7986_adcpnp[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
431 static const int mt7986_mux_values[MT7986_NUM_SENSORS] = { 0, };
432 static const int mt7986_vts_index[MT7986_NUM_SENSORS] = { VTS1 };
433 static const int mt7986_tc_offset[MT7986_NUM_CONTROLLER] = { 0x0, };
434 
435 /*
436  * The MT8173 thermal controller has four banks. Each bank can read up to
437  * four temperature sensors simultaneously. The MT8173 has a total of 5
438  * temperature sensors. We use each bank to measure a certain area of the
439  * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
440  * areas, hence is used in different banks.
441  *
442  * The thermal core only gets the maximum temperature of all banks, so
443  * the bank concept wouldn't be necessary here. However, the SVS (Smart
444  * Voltage Scaling) unit makes its decisions based on the same bank
445  * data, and this indeed needs the temperatures of the individual banks
446  * for making better decisions.
447  */
448 static const struct mtk_thermal_data mt8173_thermal_data = {
449 	.auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
450 	.num_banks = MT8173_NUM_ZONES,
451 	.num_sensors = MT8173_NUM_SENSORS,
452 	.vts_index = mt8173_vts_index,
453 	.cali_val = MT8173_CALIBRATION,
454 	.num_controller = MT8173_NUM_CONTROLLER,
455 	.controller_offset = mt8173_tc_offset,
456 	.need_switch_bank = true,
457 	.bank_data = {
458 		{
459 			.num_sensors = 2,
460 			.sensors = mt8173_bank_data[0],
461 		}, {
462 			.num_sensors = 2,
463 			.sensors = mt8173_bank_data[1],
464 		}, {
465 			.num_sensors = 3,
466 			.sensors = mt8173_bank_data[2],
467 		}, {
468 			.num_sensors = 1,
469 			.sensors = mt8173_bank_data[3],
470 		},
471 	},
472 	.msr = mt8173_msr,
473 	.adcpnp = mt8173_adcpnp,
474 	.sensor_mux_values = mt8173_mux_values,
475 	.version = MTK_THERMAL_V1,
476 };
477 
478 /*
479  * The MT2701 thermal controller has one bank, which can read up to
480  * three temperature sensors simultaneously. The MT2701 has a total of 3
481  * temperature sensors.
482  *
483  * The thermal core only gets the maximum temperature of this one bank,
484  * so the bank concept wouldn't be necessary here. However, the SVS (Smart
485  * Voltage Scaling) unit makes its decisions based on the same bank
486  * data.
487  */
488 static const struct mtk_thermal_data mt2701_thermal_data = {
489 	.auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
490 	.num_banks = 1,
491 	.num_sensors = MT2701_NUM_SENSORS,
492 	.vts_index = mt2701_vts_index,
493 	.cali_val = MT2701_CALIBRATION,
494 	.num_controller = MT2701_NUM_CONTROLLER,
495 	.controller_offset = mt2701_tc_offset,
496 	.need_switch_bank = true,
497 	.bank_data = {
498 		{
499 			.num_sensors = 3,
500 			.sensors = mt2701_bank_data,
501 		},
502 	},
503 	.msr = mt2701_msr,
504 	.adcpnp = mt2701_adcpnp,
505 	.sensor_mux_values = mt2701_mux_values,
506 	.version = MTK_THERMAL_V1,
507 };
508 
509 /*
510  * The MT2712 thermal controller has one bank, which can read up to
511  * four temperature sensors simultaneously. The MT2712 has a total of 4
512  * temperature sensors.
513  *
514  * The thermal core only gets the maximum temperature of this one bank,
515  * so the bank concept wouldn't be necessary here. However, the SVS (Smart
516  * Voltage Scaling) unit makes its decisions based on the same bank
517  * data.
518  */
519 static const struct mtk_thermal_data mt2712_thermal_data = {
520 	.auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
521 	.num_banks = 1,
522 	.num_sensors = MT2712_NUM_SENSORS,
523 	.vts_index = mt2712_vts_index,
524 	.cali_val = MT2712_CALIBRATION,
525 	.num_controller = MT2712_NUM_CONTROLLER,
526 	.controller_offset = mt2712_tc_offset,
527 	.need_switch_bank = true,
528 	.bank_data = {
529 		{
530 			.num_sensors = 4,
531 			.sensors = mt2712_bank_data,
532 		},
533 	},
534 	.msr = mt2712_msr,
535 	.adcpnp = mt2712_adcpnp,
536 	.sensor_mux_values = mt2712_mux_values,
537 	.version = MTK_THERMAL_V1,
538 };
539 
540 /*
541  * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data
542  * access.
543  */
544 static const struct mtk_thermal_data mt7622_thermal_data = {
545 	.auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
546 	.num_banks = MT7622_NUM_ZONES,
547 	.num_sensors = MT7622_NUM_SENSORS,
548 	.vts_index = mt7622_vts_index,
549 	.cali_val = MT7622_CALIBRATION,
550 	.num_controller = MT7622_NUM_CONTROLLER,
551 	.controller_offset = mt7622_tc_offset,
552 	.need_switch_bank = true,
553 	.bank_data = {
554 		{
555 			.num_sensors = 1,
556 			.sensors = mt7622_bank_data,
557 		},
558 	},
559 	.msr = mt7622_msr,
560 	.adcpnp = mt7622_adcpnp,
561 	.sensor_mux_values = mt7622_mux_values,
562 	.version = MTK_THERMAL_V2,
563 };
564 
565 /*
566  * The MT8183 thermal controller has one bank for the current SW framework.
567  * The MT8183 has a total of 6 temperature sensors.
568  * There are two thermal controller to control the six sensor.
569  * The first one bind 2 sensor, and the other bind 4 sensors.
570  * The thermal core only gets the maximum temperature of all sensor, so
571  * the bank concept wouldn't be necessary here. However, the SVS (Smart
572  * Voltage Scaling) unit makes its decisions based on the same bank
573  * data, and this indeed needs the temperatures of the individual banks
574  * for making better decisions.
575  */
576 static const struct mtk_thermal_data mt8183_thermal_data = {
577 	.auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL,
578 	.num_banks = MT8183_NUM_ZONES,
579 	.num_sensors = MT8183_NUM_SENSORS,
580 	.vts_index = mt8183_vts_index,
581 	.cali_val = MT8183_CALIBRATION,
582 	.num_controller = MT8183_NUM_CONTROLLER,
583 	.controller_offset = mt8183_tc_offset,
584 	.need_switch_bank = false,
585 	.bank_data = {
586 		{
587 			.num_sensors = 6,
588 			.sensors = mt8183_bank_data,
589 		},
590 	},
591 
592 	.msr = mt8183_msr,
593 	.adcpnp = mt8183_adcpnp,
594 	.sensor_mux_values = mt8183_mux_values,
595 	.version = MTK_THERMAL_V1,
596 };
597 
598 /*
599  * MT7986 uses AUXADC Channel 11 for raw data access.
600  */
601 static const struct mtk_thermal_data mt7986_thermal_data = {
602 	.auxadc_channel = MT7986_TEMP_AUXADC_CHANNEL,
603 	.num_banks = MT7986_NUM_ZONES,
604 	.num_sensors = MT7986_NUM_SENSORS,
605 	.vts_index = mt7986_vts_index,
606 	.cali_val = MT7986_CALIBRATION,
607 	.num_controller = MT7986_NUM_CONTROLLER,
608 	.controller_offset = mt7986_tc_offset,
609 	.need_switch_bank = true,
610 	.bank_data = {
611 		{
612 			.num_sensors = 1,
613 			.sensors = mt7986_bank_data,
614 		},
615 	},
616 	.msr = mt7986_msr,
617 	.adcpnp = mt7986_adcpnp,
618 	.sensor_mux_values = mt7986_mux_values,
619 	.version = MTK_THERMAL_V3,
620 };
621 
622 /**
623  * raw_to_mcelsius_v1 - convert a raw ADC value to mcelsius
624  * @mt:	The thermal controller
625  * @sensno:	sensor number
626  * @raw:	raw ADC value
627  *
628  * This converts the raw ADC value to mcelsius using the SoC specific
629  * calibration constants
630  */
631 static int raw_to_mcelsius_v1(struct mtk_thermal *mt, int sensno, s32 raw)
632 {
633 	s32 tmp;
634 
635 	raw &= 0xfff;
636 
637 	tmp = 203450520 << 3;
638 	tmp /= mt->conf->cali_val + mt->o_slope;
639 	tmp /= 10000 + mt->adc_ge;
640 	tmp *= raw - mt->vts[sensno] - 3350;
641 	tmp >>= 3;
642 
643 	return mt->degc_cali * 500 - tmp;
644 }
645 
646 static int raw_to_mcelsius_v2(struct mtk_thermal *mt, int sensno, s32 raw)
647 {
648 	s32 format_1;
649 	s32 format_2;
650 	s32 g_oe;
651 	s32 g_gain;
652 	s32 g_x_roomt;
653 	s32 tmp;
654 
655 	if (raw == 0)
656 		return 0;
657 
658 	raw &= 0xfff;
659 	g_gain = 10000 + (((mt->adc_ge - 512) * 10000) >> 12);
660 	g_oe = mt->adc_oe - 512;
661 	format_1 = mt->vts[VTS2] + 3105 - g_oe;
662 	format_2 = (mt->degc_cali * 10) >> 1;
663 	g_x_roomt = (((format_1 * 10000) >> 12) * 10000) / g_gain;
664 
665 	tmp = (((((raw - g_oe) * 10000) >> 12) * 10000) / g_gain) - g_x_roomt;
666 	tmp = tmp * 10 * 100 / 11;
667 
668 	if (mt->o_slope_sign == 0)
669 		tmp = tmp / (165 - mt->o_slope);
670 	else
671 		tmp = tmp / (165 + mt->o_slope);
672 
673 	return (format_2 - tmp) * 100;
674 }
675 
676 static int raw_to_mcelsius_v3(struct mtk_thermal *mt, int sensno, s32 raw)
677 {
678 	s32 tmp;
679 
680 	if (raw == 0)
681 		return 0;
682 
683 	raw &= 0xfff;
684 	tmp = 100000 * 15 / 16 * 10000;
685 	tmp /= 4096 - 512 + mt->adc_ge;
686 	tmp /= 1490;
687 	tmp *= raw - mt->vts[sensno] - 2900;
688 
689 	return mt->degc_cali * 500 - tmp;
690 }
691 
692 /**
693  * mtk_thermal_get_bank - get bank
694  * @bank:	The bank
695  *
696  * The bank registers are banked, we have to select a bank in the
697  * PTPCORESEL register to access it.
698  */
699 static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
700 {
701 	struct mtk_thermal *mt = bank->mt;
702 	u32 val;
703 
704 	if (mt->conf->need_switch_bank) {
705 		mutex_lock(&mt->lock);
706 
707 		val = readl(mt->thermal_base + PTPCORESEL);
708 		val &= ~0xf;
709 		val |= bank->id;
710 		writel(val, mt->thermal_base + PTPCORESEL);
711 	}
712 }
713 
714 /**
715  * mtk_thermal_put_bank - release bank
716  * @bank:	The bank
717  *
718  * release a bank previously taken with mtk_thermal_get_bank,
719  */
720 static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
721 {
722 	struct mtk_thermal *mt = bank->mt;
723 
724 	if (mt->conf->need_switch_bank)
725 		mutex_unlock(&mt->lock);
726 }
727 
728 /**
729  * mtk_thermal_bank_temperature - get the temperature of a bank
730  * @bank:	The bank
731  *
732  * The temperature of a bank is considered the maximum temperature of
733  * the sensors associated to the bank.
734  */
735 static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
736 {
737 	struct mtk_thermal *mt = bank->mt;
738 	const struct mtk_thermal_data *conf = mt->conf;
739 	int i, temp = INT_MIN, max = INT_MIN;
740 	u32 raw;
741 
742 	for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
743 		raw = readl(mt->thermal_base + conf->msr[i]);
744 
745 		temp = mt->raw_to_mcelsius(
746 			mt, conf->bank_data[bank->id].sensors[i], raw);
747 
748 
749 		/*
750 		 * The first read of a sensor often contains very high bogus
751 		 * temperature value. Filter these out so that the system does
752 		 * not immediately shut down.
753 		 */
754 		if (temp > 200000)
755 			temp = 0;
756 
757 		if (temp > max)
758 			max = temp;
759 	}
760 
761 	return max;
762 }
763 
764 static int mtk_read_temp(struct thermal_zone_device *tz, int *temperature)
765 {
766 	struct mtk_thermal *mt = tz->devdata;
767 	int i;
768 	int tempmax = INT_MIN;
769 
770 	for (i = 0; i < mt->conf->num_banks; i++) {
771 		struct mtk_thermal_bank *bank = &mt->banks[i];
772 
773 		mtk_thermal_get_bank(bank);
774 
775 		tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
776 
777 		mtk_thermal_put_bank(bank);
778 	}
779 
780 	*temperature = tempmax;
781 
782 	return 0;
783 }
784 
785 static const struct thermal_zone_device_ops mtk_thermal_ops = {
786 	.get_temp = mtk_read_temp,
787 };
788 
789 static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
790 				  u32 apmixed_phys_base, u32 auxadc_phys_base,
791 				  int ctrl_id)
792 {
793 	struct mtk_thermal_bank *bank = &mt->banks[num];
794 	const struct mtk_thermal_data *conf = mt->conf;
795 	int i;
796 
797 	int offset = mt->conf->controller_offset[ctrl_id];
798 	void __iomem *controller_base = mt->thermal_base + offset;
799 
800 	bank->id = num;
801 	bank->mt = mt;
802 
803 	mtk_thermal_get_bank(bank);
804 
805 	/* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
806 	writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
807 
808 	/*
809 	 * filt interval is 1 * 46.540us = 46.54us,
810 	 * sen interval is 429 * 46.540us = 19.96ms
811 	 */
812 	writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
813 			TEMP_MONCTL2_SENSOR_INTERVAL(429),
814 			controller_base + TEMP_MONCTL2);
815 
816 	/* poll is set to 10u */
817 	writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
818 	       controller_base + TEMP_AHBPOLL);
819 
820 	/* temperature sampling control, 1 sample */
821 	writel(0x0, controller_base + TEMP_MSRCTL0);
822 
823 	/* exceed this polling time, IRQ would be inserted */
824 	writel(0xffffffff, controller_base + TEMP_AHBTO);
825 
826 	/* number of interrupts per event, 1 is enough */
827 	writel(0x0, controller_base + TEMP_MONIDET0);
828 	writel(0x0, controller_base + TEMP_MONIDET1);
829 
830 	/*
831 	 * The MT8173 thermal controller does not have its own ADC. Instead it
832 	 * uses AHB bus accesses to control the AUXADC. To do this the thermal
833 	 * controller has to be programmed with the physical addresses of the
834 	 * AUXADC registers and with the various bit positions in the AUXADC.
835 	 * Also the thermal controller controls a mux in the APMIXEDSYS register
836 	 * space.
837 	 */
838 
839 	/*
840 	 * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
841 	 * automatically by hw
842 	 */
843 	writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
844 
845 	/* AHB address for auxadc mux selection */
846 	writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
847 	       controller_base + TEMP_ADCMUXADDR);
848 
849 	if (mt->conf->version == MTK_THERMAL_V1) {
850 		/* AHB address for pnp sensor mux selection */
851 		writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
852 		       controller_base + TEMP_PNPMUXADDR);
853 	}
854 
855 	/* AHB value for auxadc enable */
856 	writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
857 
858 	/* AHB address for auxadc enable (channel 0 immediate mode selected) */
859 	writel(auxadc_phys_base + AUXADC_CON1_SET_V,
860 	       controller_base + TEMP_ADCENADDR);
861 
862 	/* AHB address for auxadc valid bit */
863 	writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
864 	       controller_base + TEMP_ADCVALIDADDR);
865 
866 	/* AHB address for auxadc voltage output */
867 	writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
868 	       controller_base + TEMP_ADCVOLTADDR);
869 
870 	/* read valid & voltage are at the same register */
871 	writel(0x0, controller_base + TEMP_RDCTRL);
872 
873 	/* indicate where the valid bit is */
874 	writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
875 	       controller_base + TEMP_ADCVALIDMASK);
876 
877 	/* no shift */
878 	writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
879 
880 	/* enable auxadc mux write transaction */
881 	writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
882 		controller_base + TEMP_ADCWRITECTRL);
883 
884 	for (i = 0; i < conf->bank_data[num].num_sensors; i++)
885 		writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
886 		       mt->thermal_base + conf->adcpnp[i]);
887 
888 	writel((1 << conf->bank_data[num].num_sensors) - 1,
889 	       controller_base + TEMP_MONCTL0);
890 
891 	writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
892 	       TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
893 	       controller_base + TEMP_ADCWRITECTRL);
894 
895 	mtk_thermal_put_bank(bank);
896 }
897 
898 static u64 of_get_phys_base(struct device_node *np)
899 {
900 	u64 size64;
901 	const __be32 *regaddr_p;
902 
903 	regaddr_p = of_get_address(np, 0, &size64, NULL);
904 	if (!regaddr_p)
905 		return OF_BAD_ADDR;
906 
907 	return of_translate_address(np, regaddr_p);
908 }
909 
910 static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf)
911 {
912 	int i;
913 
914 	if (!(buf[0] & CALIB_BUF0_VALID_V1))
915 		return -EINVAL;
916 
917 	mt->adc_ge = CALIB_BUF1_ADC_GE_V1(buf[1]);
918 
919 	for (i = 0; i < mt->conf->num_sensors; i++) {
920 		switch (mt->conf->vts_index[i]) {
921 		case VTS1:
922 			mt->vts[VTS1] = CALIB_BUF0_VTS_TS1_V1(buf[0]);
923 			break;
924 		case VTS2:
925 			mt->vts[VTS2] = CALIB_BUF0_VTS_TS2_V1(buf[0]);
926 			break;
927 		case VTS3:
928 			mt->vts[VTS3] = CALIB_BUF1_VTS_TS3_V1(buf[1]);
929 			break;
930 		case VTS4:
931 			mt->vts[VTS4] = CALIB_BUF2_VTS_TS4_V1(buf[2]);
932 			break;
933 		case VTS5:
934 			mt->vts[VTS5] = CALIB_BUF2_VTS_TS5_V1(buf[2]);
935 			break;
936 		case VTSABB:
937 			mt->vts[VTSABB] =
938 				CALIB_BUF2_VTS_TSABB_V1(buf[2]);
939 			break;
940 		default:
941 			break;
942 		}
943 	}
944 
945 	mt->degc_cali = CALIB_BUF0_DEGC_CALI_V1(buf[0]);
946 	if (CALIB_BUF1_ID_V1(buf[1]) &
947 	    CALIB_BUF0_O_SLOPE_SIGN_V1(buf[0]))
948 		mt->o_slope = -CALIB_BUF0_O_SLOPE_V1(buf[0]);
949 	else
950 		mt->o_slope = CALIB_BUF0_O_SLOPE_V1(buf[0]);
951 
952 	return 0;
953 }
954 
955 static int mtk_thermal_extract_efuse_v2(struct mtk_thermal *mt, u32 *buf)
956 {
957 	if (!CALIB_BUF1_VALID_V2(buf[1]))
958 		return -EINVAL;
959 
960 	mt->adc_oe = CALIB_BUF0_ADC_OE_V2(buf[0]);
961 	mt->adc_ge = CALIB_BUF0_ADC_GE_V2(buf[0]);
962 	mt->degc_cali = CALIB_BUF0_DEGC_CALI_V2(buf[0]);
963 	mt->o_slope = CALIB_BUF0_O_SLOPE_V2(buf[0]);
964 	mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V2(buf[1]);
965 	mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V2(buf[1]);
966 	mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V2(buf[1]);
967 	mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2(buf[1]);
968 
969 	return 0;
970 }
971 
972 static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf)
973 {
974 	if (!CALIB_BUF1_VALID_V3(buf[1]))
975 		return -EINVAL;
976 
977 	mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]);
978 	mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]);
979 	mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]);
980 	mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]);
981 	mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]);
982 	mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]);
983 	mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]);
984 
985 	if (CALIB_BUF1_ID_V3(buf[1]) == 0)
986 		mt->o_slope = 0;
987 
988 	return 0;
989 }
990 
991 static int mtk_thermal_get_calibration_data(struct device *dev,
992 					    struct mtk_thermal *mt)
993 {
994 	struct nvmem_cell *cell;
995 	u32 *buf;
996 	size_t len;
997 	int i, ret = 0;
998 
999 	/* Start with default values */
1000 	mt->adc_ge = 512;
1001 	mt->adc_oe = 512;
1002 	for (i = 0; i < mt->conf->num_sensors; i++)
1003 		mt->vts[i] = 260;
1004 	mt->degc_cali = 40;
1005 	mt->o_slope = 0;
1006 
1007 	cell = nvmem_cell_get(dev, "calibration-data");
1008 	if (IS_ERR(cell)) {
1009 		if (PTR_ERR(cell) == -EPROBE_DEFER)
1010 			return PTR_ERR(cell);
1011 		return 0;
1012 	}
1013 
1014 	buf = (u32 *)nvmem_cell_read(cell, &len);
1015 
1016 	nvmem_cell_put(cell);
1017 
1018 	if (IS_ERR(buf))
1019 		return PTR_ERR(buf);
1020 
1021 	if (len < 3 * sizeof(u32)) {
1022 		dev_warn(dev, "invalid calibration data\n");
1023 		ret = -EINVAL;
1024 		goto out;
1025 	}
1026 
1027 	switch (mt->conf->version) {
1028 	case MTK_THERMAL_V1:
1029 		ret = mtk_thermal_extract_efuse_v1(mt, buf);
1030 		break;
1031 	case MTK_THERMAL_V2:
1032 		ret = mtk_thermal_extract_efuse_v2(mt, buf);
1033 		break;
1034 	case MTK_THERMAL_V3:
1035 		ret = mtk_thermal_extract_efuse_v3(mt, buf);
1036 		break;
1037 	default:
1038 		ret = -EINVAL;
1039 		break;
1040 	}
1041 
1042 	if (ret) {
1043 		dev_info(dev, "Device not calibrated, using default calibration values\n");
1044 		ret = 0;
1045 	}
1046 
1047 out:
1048 	kfree(buf);
1049 
1050 	return ret;
1051 }
1052 
1053 static const struct of_device_id mtk_thermal_of_match[] = {
1054 	{
1055 		.compatible = "mediatek,mt8173-thermal",
1056 		.data = (void *)&mt8173_thermal_data,
1057 	},
1058 	{
1059 		.compatible = "mediatek,mt2701-thermal",
1060 		.data = (void *)&mt2701_thermal_data,
1061 	},
1062 	{
1063 		.compatible = "mediatek,mt2712-thermal",
1064 		.data = (void *)&mt2712_thermal_data,
1065 	},
1066 	{
1067 		.compatible = "mediatek,mt7622-thermal",
1068 		.data = (void *)&mt7622_thermal_data,
1069 	},
1070 	{
1071 		.compatible = "mediatek,mt7986-thermal",
1072 		.data = (void *)&mt7986_thermal_data,
1073 	},
1074 	{
1075 		.compatible = "mediatek,mt8183-thermal",
1076 		.data = (void *)&mt8183_thermal_data,
1077 	}, {
1078 	},
1079 };
1080 MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
1081 
1082 static void mtk_thermal_turn_on_buffer(void __iomem *apmixed_base)
1083 {
1084 	int tmp;
1085 
1086 	tmp = readl(apmixed_base + APMIXED_SYS_TS_CON1);
1087 	tmp &= ~(0x37);
1088 	tmp |= 0x1;
1089 	writel(tmp, apmixed_base + APMIXED_SYS_TS_CON1);
1090 	udelay(200);
1091 }
1092 
1093 static void mtk_thermal_release_periodic_ts(struct mtk_thermal *mt,
1094 					    void __iomem *auxadc_base)
1095 {
1096 	int tmp;
1097 
1098 	writel(0x800, auxadc_base + AUXADC_CON1_SET_V);
1099 	writel(0x1, mt->thermal_base + TEMP_MONCTL0);
1100 	tmp = readl(mt->thermal_base + TEMP_MSRCTL1);
1101 	writel((tmp & (~0x10e)), mt->thermal_base + TEMP_MSRCTL1);
1102 }
1103 
1104 static int mtk_thermal_probe(struct platform_device *pdev)
1105 {
1106 	int ret, i, ctrl_id;
1107 	struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
1108 	struct mtk_thermal *mt;
1109 	u64 auxadc_phys_base, apmixed_phys_base;
1110 	struct thermal_zone_device *tzdev;
1111 	void __iomem *apmixed_base, *auxadc_base;
1112 
1113 	mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
1114 	if (!mt)
1115 		return -ENOMEM;
1116 
1117 	mt->conf = of_device_get_match_data(&pdev->dev);
1118 
1119 	mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
1120 	if (IS_ERR(mt->clk_peri_therm))
1121 		return PTR_ERR(mt->clk_peri_therm);
1122 
1123 	mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
1124 	if (IS_ERR(mt->clk_auxadc))
1125 		return PTR_ERR(mt->clk_auxadc);
1126 
1127 	mt->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
1128 	if (IS_ERR(mt->thermal_base))
1129 		return PTR_ERR(mt->thermal_base);
1130 
1131 	ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
1132 	if (ret)
1133 		return ret;
1134 
1135 	mutex_init(&mt->lock);
1136 
1137 	mt->dev = &pdev->dev;
1138 
1139 	auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
1140 	if (!auxadc) {
1141 		dev_err(&pdev->dev, "missing auxadc node\n");
1142 		return -ENODEV;
1143 	}
1144 
1145 	auxadc_base = of_iomap(auxadc, 0);
1146 	auxadc_phys_base = of_get_phys_base(auxadc);
1147 
1148 	of_node_put(auxadc);
1149 
1150 	if (auxadc_phys_base == OF_BAD_ADDR) {
1151 		dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1152 		return -EINVAL;
1153 	}
1154 
1155 	apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
1156 	if (!apmixedsys) {
1157 		dev_err(&pdev->dev, "missing apmixedsys node\n");
1158 		return -ENODEV;
1159 	}
1160 
1161 	apmixed_base = of_iomap(apmixedsys, 0);
1162 	apmixed_phys_base = of_get_phys_base(apmixedsys);
1163 
1164 	of_node_put(apmixedsys);
1165 
1166 	if (apmixed_phys_base == OF_BAD_ADDR) {
1167 		dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1168 		return -EINVAL;
1169 	}
1170 
1171 	ret = device_reset_optional(&pdev->dev);
1172 	if (ret)
1173 		return ret;
1174 
1175 	ret = clk_prepare_enable(mt->clk_auxadc);
1176 	if (ret) {
1177 		dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
1178 		return ret;
1179 	}
1180 
1181 	ret = clk_prepare_enable(mt->clk_peri_therm);
1182 	if (ret) {
1183 		dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
1184 		goto err_disable_clk_auxadc;
1185 	}
1186 
1187 	if (mt->conf->version != MTK_THERMAL_V1) {
1188 		mtk_thermal_turn_on_buffer(apmixed_base);
1189 		mtk_thermal_release_periodic_ts(mt, auxadc_base);
1190 	}
1191 
1192 	if (mt->conf->version == MTK_THERMAL_V1)
1193 		mt->raw_to_mcelsius = raw_to_mcelsius_v1;
1194 	else if (mt->conf->version == MTK_THERMAL_V2)
1195 		mt->raw_to_mcelsius = raw_to_mcelsius_v2;
1196 	else
1197 		mt->raw_to_mcelsius = raw_to_mcelsius_v3;
1198 
1199 	for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
1200 		for (i = 0; i < mt->conf->num_banks; i++)
1201 			mtk_thermal_init_bank(mt, i, apmixed_phys_base,
1202 					      auxadc_phys_base, ctrl_id);
1203 
1204 	platform_set_drvdata(pdev, mt);
1205 
1206 	tzdev = devm_thermal_of_zone_register(&pdev->dev, 0, mt,
1207 					      &mtk_thermal_ops);
1208 	if (IS_ERR(tzdev)) {
1209 		ret = PTR_ERR(tzdev);
1210 		goto err_disable_clk_peri_therm;
1211 	}
1212 
1213 	ret = devm_thermal_add_hwmon_sysfs(tzdev);
1214 	if (ret)
1215 		dev_warn(&pdev->dev, "error in thermal_add_hwmon_sysfs");
1216 
1217 	return 0;
1218 
1219 err_disable_clk_peri_therm:
1220 	clk_disable_unprepare(mt->clk_peri_therm);
1221 err_disable_clk_auxadc:
1222 	clk_disable_unprepare(mt->clk_auxadc);
1223 
1224 	return ret;
1225 }
1226 
1227 static int mtk_thermal_remove(struct platform_device *pdev)
1228 {
1229 	struct mtk_thermal *mt = platform_get_drvdata(pdev);
1230 
1231 	clk_disable_unprepare(mt->clk_peri_therm);
1232 	clk_disable_unprepare(mt->clk_auxadc);
1233 
1234 	return 0;
1235 }
1236 
1237 static struct platform_driver mtk_thermal_driver = {
1238 	.probe = mtk_thermal_probe,
1239 	.remove = mtk_thermal_remove,
1240 	.driver = {
1241 		.name = "mtk-thermal",
1242 		.of_match_table = mtk_thermal_of_match,
1243 	},
1244 };
1245 
1246 module_platform_driver(mtk_thermal_driver);
1247 
1248 MODULE_AUTHOR("Michael Kao <michael.kao@mediatek.com>");
1249 MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>");
1250 MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>");
1251 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1252 MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>");
1253 MODULE_DESCRIPTION("Mediatek thermal driver");
1254 MODULE_LICENSE("GPL v2");
1255