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