blob: e1886bb505542c07b2a854323e580fcd24cd3af8 [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;
317 /* lock: for getting and putting banks */
318 struct mutex lock;
319
320 /* Calibration values */
321 s32 adc_ge;
322 s32 adc_oe;
323 s32 degc_cali;
324 s32 o_slope;
325 s32 o_slope_sign;
326 s32 vts[MAX_NUM_VTS];
327
328 const struct mtk_thermal_data *conf;
329 struct mtk_thermal_bank banks[MAX_NUM_ZONES];
330};
331
332/* MT8183 thermal sensor data */
333static const int mt8183_bank_data[MT8183_NUM_SENSORS] = {
334 MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB
335};
336
337static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = {
338 TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1
339};
340
341static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = {
342 TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1,
343 TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1
344};
345
346static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 };
347static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100};
348
349static const int mt8183_vts_index[MT8183_NUM_SENSORS] = {
350 VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB
351};
352
353/* MT8173 thermal sensor data */
354static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
355 { MT8173_TS2, MT8173_TS3 },
356 { MT8173_TS2, MT8173_TS4 },
357 { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
358 { MT8173_TS2 },
359};
360
361static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
362 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
363};
364
365static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
366 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
367};
368
369static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
370static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, };
371
372static const int mt8173_vts_index[MT8173_NUM_SENSORS] = {
373 VTS1, VTS2, VTS3, VTS4, VTSABB
374};
375
376/* MT2701 thermal sensor data */
377static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
378 MT2701_TS1, MT2701_TS2, MT2701_TSABB
379};
380
381static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
382 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
383};
384
385static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
386 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
387};
388
389static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
390static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, };
391
392static const int mt2701_vts_index[MT2701_NUM_SENSORS] = {
393 VTS1, VTS2, VTS3
394};
395
396/* MT2712 thermal sensor data */
397static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
398 MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
399};
400
401static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
402 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
403};
404
405static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
406 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
407};
408
409static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
410static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, };
411
412static const int mt2712_vts_index[MT2712_NUM_SENSORS] = {
413 VTS1, VTS2, VTS3, VTS4
414};
415
416/* MT7622 thermal sensor data */
417static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
418static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
419static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
420static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
421static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
422static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
423
developer3e9ad9d2021-07-01 16:42:25 +0800424/* MT7986 thermal sensor data */
425static const int mt7986_bank_data[MT7986_NUM_SENSORS] = { MT7986_TS1, };
426static const int mt7986_msr[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
427static const int mt7986_adcpnp[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
428static const int mt7986_mux_values[MT7986_NUM_SENSORS] = { 0, };
429static const int mt7986_vts_index[MT7986_NUM_SENSORS] = { VTS1 };
430static const int mt7986_tc_offset[MT7986_NUM_CONTROLLER] = { 0x0, };
431
developeraedf4202021-06-12 11:52:43 +0800432/*
433 * The MT8173 thermal controller has four banks. Each bank can read up to
434 * four temperature sensors simultaneously. The MT8173 has a total of 5
435 * temperature sensors. We use each bank to measure a certain area of the
436 * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
437 * areas, hence is used in different banks.
438 *
439 * The thermal core only gets the maximum temperature of all banks, so
440 * the bank concept wouldn't be necessary here. However, the SVS (Smart
441 * Voltage Scaling) unit makes its decisions based on the same bank
442 * data, and this indeed needs the temperatures of the individual banks
443 * for making better decisions.
444 */
445static const struct mtk_thermal_data mt8173_thermal_data = {
446 .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
447 .num_banks = MT8173_NUM_ZONES,
448 .num_sensors = MT8173_NUM_SENSORS,
449 .vts_index = mt8173_vts_index,
450 .cali_val = MT8173_CALIBRATION,
451 .num_controller = MT8173_NUM_CONTROLLER,
452 .controller_offset = mt8173_tc_offset,
453 .need_switch_bank = true,
454 .bank_data = {
455 {
456 .num_sensors = 2,
457 .sensors = mt8173_bank_data[0],
458 }, {
459 .num_sensors = 2,
460 .sensors = mt8173_bank_data[1],
461 }, {
462 .num_sensors = 3,
463 .sensors = mt8173_bank_data[2],
464 }, {
465 .num_sensors = 1,
466 .sensors = mt8173_bank_data[3],
467 },
468 },
469 .msr = mt8173_msr,
470 .adcpnp = mt8173_adcpnp,
471 .sensor_mux_values = mt8173_mux_values,
472 .version = MTK_THERMAL_V1,
473};
474
475/*
476 * The MT2701 thermal controller has one bank, which can read up to
477 * three temperature sensors simultaneously. The MT2701 has a total of 3
478 * temperature sensors.
479 *
480 * The thermal core only gets the maximum temperature of this one bank,
481 * so the bank concept wouldn't be necessary here. However, the SVS (Smart
482 * Voltage Scaling) unit makes its decisions based on the same bank
483 * data.
484 */
485static const struct mtk_thermal_data mt2701_thermal_data = {
486 .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
487 .num_banks = 1,
488 .num_sensors = MT2701_NUM_SENSORS,
489 .vts_index = mt2701_vts_index,
490 .cali_val = MT2701_CALIBRATION,
491 .num_controller = MT2701_NUM_CONTROLLER,
492 .controller_offset = mt2701_tc_offset,
493 .need_switch_bank = true,
494 .bank_data = {
495 {
496 .num_sensors = 3,
497 .sensors = mt2701_bank_data,
498 },
499 },
500 .msr = mt2701_msr,
501 .adcpnp = mt2701_adcpnp,
502 .sensor_mux_values = mt2701_mux_values,
503 .version = MTK_THERMAL_V1,
504};
505
506/*
507 * The MT2712 thermal controller has one bank, which can read up to
508 * four temperature sensors simultaneously. The MT2712 has a total of 4
509 * temperature sensors.
510 *
511 * The thermal core only gets the maximum temperature of this one bank,
512 * so the bank concept wouldn't be necessary here. However, the SVS (Smart
513 * Voltage Scaling) unit makes its decisions based on the same bank
514 * data.
515 */
516static const struct mtk_thermal_data mt2712_thermal_data = {
517 .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
518 .num_banks = 1,
519 .num_sensors = MT2712_NUM_SENSORS,
520 .vts_index = mt2712_vts_index,
521 .cali_val = MT2712_CALIBRATION,
522 .num_controller = MT2712_NUM_CONTROLLER,
523 .controller_offset = mt2712_tc_offset,
524 .need_switch_bank = true,
525 .bank_data = {
526 {
527 .num_sensors = 4,
528 .sensors = mt2712_bank_data,
529 },
530 },
531 .msr = mt2712_msr,
532 .adcpnp = mt2712_adcpnp,
533 .sensor_mux_values = mt2712_mux_values,
534 .version = MTK_THERMAL_V1,
535};
536
537/*
538 * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data
539 * access.
540 */
541static const struct mtk_thermal_data mt7622_thermal_data = {
542 .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
543 .num_banks = MT7622_NUM_ZONES,
544 .num_sensors = MT7622_NUM_SENSORS,
545 .vts_index = mt7622_vts_index,
546 .cali_val = MT7622_CALIBRATION,
547 .num_controller = MT7622_NUM_CONTROLLER,
548 .controller_offset = mt7622_tc_offset,
549 .need_switch_bank = true,
550 .bank_data = {
551 {
552 .num_sensors = 1,
553 .sensors = mt7622_bank_data,
554 },
555 },
556 .msr = mt7622_msr,
557 .adcpnp = mt7622_adcpnp,
558 .sensor_mux_values = mt7622_mux_values,
559 .version = MTK_THERMAL_V2,
560};
561
562/*
563 * The MT8183 thermal controller has one bank for the current SW framework.
564 * The MT8183 has a total of 6 temperature sensors.
565 * There are two thermal controller to control the six sensor.
566 * The first one bind 2 sensor, and the other bind 4 sensors.
567 * The thermal core only gets the maximum temperature of all sensor, so
568 * the bank concept wouldn't be necessary here. However, the SVS (Smart
569 * Voltage Scaling) unit makes its decisions based on the same bank
570 * data, and this indeed needs the temperatures of the individual banks
571 * for making better decisions.
572 */
573static const struct mtk_thermal_data mt8183_thermal_data = {
574 .auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL,
575 .num_banks = MT8183_NUM_ZONES,
576 .num_sensors = MT8183_NUM_SENSORS,
577 .vts_index = mt8183_vts_index,
578 .cali_val = MT8183_CALIBRATION,
579 .num_controller = MT8183_NUM_CONTROLLER,
580 .controller_offset = mt8183_tc_offset,
581 .need_switch_bank = false,
582 .bank_data = {
583 {
584 .num_sensors = 6,
585 .sensors = mt8183_bank_data,
586 },
587 },
588
589 .msr = mt8183_msr,
590 .adcpnp = mt8183_adcpnp,
591 .sensor_mux_values = mt8183_mux_values,
592 .version = MTK_THERMAL_V1,
593};
594
developer3e9ad9d2021-07-01 16:42:25 +0800595/*
596 * MT7986 uses AUXADC Channel 11 for raw data access.
597 */
598static const struct mtk_thermal_data mt7986_thermal_data = {
599 .auxadc_channel = MT7986_TEMP_AUXADC_CHANNEL,
600 .num_banks = MT7986_NUM_ZONES,
601 .num_sensors = MT7986_NUM_SENSORS,
602 .vts_index = mt7986_vts_index,
603 .cali_val = MT7986_CALIBRATION,
604 .num_controller = MT7986_NUM_CONTROLLER,
605 .controller_offset = mt7986_tc_offset,
606 .need_switch_bank = true,
607 .bank_data = {
608 {
609 .num_sensors = 1,
610 .sensors = mt7986_bank_data,
611 },
612 },
613 .msr = mt7986_msr,
614 .adcpnp = mt7986_adcpnp,
615 .sensor_mux_values = mt7986_mux_values,
616 .version = MTK_THERMAL_V3,
617};
618
developeraedf4202021-06-12 11:52:43 +0800619/**
620 * raw_to_mcelsius - convert a raw ADC value to mcelsius
621 * @mt: The thermal controller
622 * @sensno: sensor number
623 * @raw: raw ADC value
624 *
625 * This converts the raw ADC value to mcelsius using the SoC specific
626 * calibration constants
627 */
628static int raw_to_mcelsius_v1(struct mtk_thermal *mt, int sensno, s32 raw)
629{
630 s32 tmp;
631
632 raw &= 0xfff;
633
634 tmp = 203450520 << 3;
635 tmp /= mt->conf->cali_val + mt->o_slope;
636 tmp /= 10000 + mt->adc_ge;
637 tmp *= raw - mt->vts[sensno] - 3350;
638 tmp >>= 3;
639
640 return mt->degc_cali * 500 - tmp;
641}
642
643static int raw_to_mcelsius_v2(struct mtk_thermal *mt, int sensno, s32 raw)
644{
645 s32 format_1;
646 s32 format_2;
647 s32 g_oe;
648 s32 g_gain;
649 s32 g_x_roomt;
650 s32 tmp;
651
652 if (raw == 0)
653 return 0;
654
655 raw &= 0xfff;
656 g_gain = 10000 + (((mt->adc_ge - 512) * 10000) >> 12);
657 g_oe = mt->adc_oe - 512;
658 format_1 = mt->vts[VTS2] + 3105 - g_oe;
659 format_2 = (mt->degc_cali * 10) >> 1;
660 g_x_roomt = (((format_1 * 10000) >> 12) * 10000) / g_gain;
661
662 tmp = (((((raw - g_oe) * 10000) >> 12) * 10000) / g_gain) - g_x_roomt;
663 tmp = tmp * 10 * 100 / 11;
664
665 if (mt->o_slope_sign == 0)
666 tmp = tmp / (165 - mt->o_slope);
667 else
668 tmp = tmp / (165 + mt->o_slope);
669
670 return (format_2 - tmp) * 100;
671}
672
developer3e9ad9d2021-07-01 16:42:25 +0800673static int raw_to_mcelsius_v3(struct mtk_thermal *mt, int sensno, s32 raw)
674{
developer3e9ad9d2021-07-01 16:42:25 +0800675 s32 tmp;
676
677 if (raw == 0)
678 return 0;
679
680 raw &= 0xfff;
developerbaf36c72021-07-19 15:44:23 +0800681 tmp = 100000 * 15 / 16 * 10000;
682 tmp /= 4096 - 512 + mt->adc_ge;
683 tmp /= 1490;
684 tmp *= raw - mt->vts[sensno] - 2900;
developer3e9ad9d2021-07-01 16:42:25 +0800685
developerbaf36c72021-07-19 15:44:23 +0800686 return mt->degc_cali * 500 - tmp;
developer3e9ad9d2021-07-01 16:42:25 +0800687}
688
developeraedf4202021-06-12 11:52:43 +0800689/**
690 * mtk_thermal_get_bank - get bank
691 * @bank: The bank
692 *
693 * The bank registers are banked, we have to select a bank in the
694 * PTPCORESEL register to access it.
695 */
696static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
697{
698 struct mtk_thermal *mt = bank->mt;
699 u32 val;
700
701 if (mt->conf->need_switch_bank) {
702 mutex_lock(&mt->lock);
703
704 val = readl(mt->thermal_base + PTPCORESEL);
705 val &= ~0xf;
706 val |= bank->id;
707 writel(val, mt->thermal_base + PTPCORESEL);
708 }
709}
710
711/**
712 * mtk_thermal_put_bank - release bank
713 * @bank: The bank
714 *
715 * release a bank previously taken with mtk_thermal_get_bank,
716 */
717static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
718{
719 struct mtk_thermal *mt = bank->mt;
720
721 if (mt->conf->need_switch_bank)
722 mutex_unlock(&mt->lock);
723}
724
725/**
726 * mtk_thermal_bank_temperature - get the temperature of a bank
727 * @bank: The bank
728 *
729 * The temperature of a bank is considered the maximum temperature of
730 * the sensors associated to the bank.
731 */
732static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
733{
734 struct mtk_thermal *mt = bank->mt;
735 const struct mtk_thermal_data *conf = mt->conf;
736 int i, temp = INT_MIN, max = INT_MIN;
737 u32 raw;
738
739 for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
740 raw = readl(mt->thermal_base + conf->msr[i]);
741
742 if (mt->conf->version == MTK_THERMAL_V1) {
743 temp = raw_to_mcelsius_v1(
744 mt, conf->bank_data[bank->id].sensors[i], raw);
developer3e9ad9d2021-07-01 16:42:25 +0800745 } else if (mt->conf->version == MTK_THERMAL_V2) {
developeraedf4202021-06-12 11:52:43 +0800746 temp = raw_to_mcelsius_v2(
747 mt, conf->bank_data[bank->id].sensors[i], raw);
developer3e9ad9d2021-07-01 16:42:25 +0800748 } else {
749 temp = raw_to_mcelsius_v3(
750 mt, conf->bank_data[bank->id].sensors[i], raw);
developeraedf4202021-06-12 11:52:43 +0800751 }
752
753 /*
754 * The first read of a sensor often contains very high bogus
755 * temperature value. Filter these out so that the system does
756 * not immediately shut down.
757 */
758 if (temp > 200000)
759 temp = 0;
760
761 if (temp > max)
762 max = temp;
763 }
764
765 return max;
766}
767
768static int mtk_read_temp(void *data, int *temperature)
769{
770 struct mtk_thermal *mt = data;
771 int i;
772 int tempmax = INT_MIN;
773
774 for (i = 0; i < mt->conf->num_banks; i++) {
775 struct mtk_thermal_bank *bank = &mt->banks[i];
776
777 mtk_thermal_get_bank(bank);
778
779 tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
780
781 mtk_thermal_put_bank(bank);
782 }
783
784 *temperature = tempmax;
785
786 return 0;
787}
788
789static const struct thermal_zone_of_device_ops mtk_thermal_ops = {
790 .get_temp = mtk_read_temp,
791};
792
793static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
794 u32 apmixed_phys_base, u32 auxadc_phys_base,
795 int ctrl_id)
796{
797 struct mtk_thermal_bank *bank = &mt->banks[num];
798 const struct mtk_thermal_data *conf = mt->conf;
799 int i;
800
801 int offset = mt->conf->controller_offset[ctrl_id];
802 void __iomem *controller_base = mt->thermal_base + offset;
803
804 bank->id = num;
805 bank->mt = mt;
806
807 mtk_thermal_get_bank(bank);
808
809 /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
810 writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
811
812 /*
813 * filt interval is 1 * 46.540us = 46.54us,
814 * sen interval is 429 * 46.540us = 19.96ms
815 */
816 writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
817 TEMP_MONCTL2_SENSOR_INTERVAL(429),
818 controller_base + TEMP_MONCTL2);
819
820 /* poll is set to 10u */
821 writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
822 controller_base + TEMP_AHBPOLL);
823
824 /* temperature sampling control, 1 sample */
825 writel(0x0, controller_base + TEMP_MSRCTL0);
826
827 /* exceed this polling time, IRQ would be inserted */
828 writel(0xffffffff, controller_base + TEMP_AHBTO);
829
830 /* number of interrupts per event, 1 is enough */
831 writel(0x0, controller_base + TEMP_MONIDET0);
832 writel(0x0, controller_base + TEMP_MONIDET1);
833
834 /*
835 * The MT8173 thermal controller does not have its own ADC. Instead it
836 * uses AHB bus accesses to control the AUXADC. To do this the thermal
837 * controller has to be programmed with the physical addresses of the
838 * AUXADC registers and with the various bit positions in the AUXADC.
839 * Also the thermal controller controls a mux in the APMIXEDSYS register
840 * space.
841 */
842
843 /*
844 * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
845 * automatically by hw
846 */
847 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
848
849 /* AHB address for auxadc mux selection */
850 writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
851 controller_base + TEMP_ADCMUXADDR);
852
853 if (mt->conf->version == MTK_THERMAL_V1) {
854 /* AHB address for pnp sensor mux selection */
855 writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
856 controller_base + TEMP_PNPMUXADDR);
857 }
858
859 /* AHB value for auxadc enable */
860 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
861
862 /* AHB address for auxadc enable (channel 0 immediate mode selected) */
863 writel(auxadc_phys_base + AUXADC_CON1_SET_V,
864 controller_base + TEMP_ADCENADDR);
865
866 /* AHB address for auxadc valid bit */
867 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
868 controller_base + TEMP_ADCVALIDADDR);
869
870 /* AHB address for auxadc voltage output */
871 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
872 controller_base + TEMP_ADCVOLTADDR);
873
874 /* read valid & voltage are at the same register */
875 writel(0x0, controller_base + TEMP_RDCTRL);
876
877 /* indicate where the valid bit is */
878 writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
879 controller_base + TEMP_ADCVALIDMASK);
880
881 /* no shift */
882 writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
883
884 /* enable auxadc mux write transaction */
885 writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
886 controller_base + TEMP_ADCWRITECTRL);
887
888 for (i = 0; i < conf->bank_data[num].num_sensors; i++)
889 writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
890 mt->thermal_base + conf->adcpnp[i]);
891
892 writel((1 << conf->bank_data[num].num_sensors) - 1,
893 controller_base + TEMP_MONCTL0);
894
895 writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
896 TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
897 controller_base + TEMP_ADCWRITECTRL);
898
899 mtk_thermal_put_bank(bank);
900}
901
902static u64 of_get_phys_base(struct device_node *np)
903{
904 u64 size64;
905 const __be32 *regaddr_p;
906
907 regaddr_p = of_get_address(np, 0, &size64, NULL);
908 if (!regaddr_p)
909 return OF_BAD_ADDR;
910
911 return of_translate_address(np, regaddr_p);
912}
913
914static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf)
915{
916 int i;
917
918 if (!(buf[0] & CALIB_BUF0_VALID_V1))
919 return -EINVAL;
920
921 mt->adc_ge = CALIB_BUF1_ADC_GE_V1(buf[1]);
922
923 for (i = 0; i < mt->conf->num_sensors; i++) {
924 switch (mt->conf->vts_index[i]) {
925 case VTS1:
926 mt->vts[VTS1] = CALIB_BUF0_VTS_TS1_V1(buf[0]);
927 break;
928 case VTS2:
929 mt->vts[VTS2] = CALIB_BUF0_VTS_TS2_V1(buf[0]);
930 break;
931 case VTS3:
932 mt->vts[VTS3] = CALIB_BUF1_VTS_TS3_V1(buf[1]);
933 break;
934 case VTS4:
935 mt->vts[VTS4] = CALIB_BUF2_VTS_TS4_V1(buf[2]);
936 break;
937 case VTS5:
938 mt->vts[VTS5] = CALIB_BUF2_VTS_TS5_V1(buf[2]);
939 break;
940 case VTSABB:
941 mt->vts[VTSABB] =
942 CALIB_BUF2_VTS_TSABB_V1(buf[2]);
943 break;
944 default:
945 break;
946 }
947 }
948
949 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V1(buf[0]);
950 if (CALIB_BUF1_ID_V1(buf[1]) &
951 CALIB_BUF0_O_SLOPE_SIGN_V1(buf[0]))
952 mt->o_slope = -CALIB_BUF0_O_SLOPE_V1(buf[0]);
953 else
954 mt->o_slope = CALIB_BUF0_O_SLOPE_V1(buf[0]);
955
956 return 0;
957}
958
959static int mtk_thermal_extract_efuse_v2(struct mtk_thermal *mt, u32 *buf)
960{
961 if (!CALIB_BUF1_VALID_V2(buf[1]))
962 return -EINVAL;
963
964 mt->adc_oe = CALIB_BUF0_ADC_OE_V2(buf[0]);
965 mt->adc_ge = CALIB_BUF0_ADC_GE_V2(buf[0]);
966 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V2(buf[0]);
967 mt->o_slope = CALIB_BUF0_O_SLOPE_V2(buf[0]);
968 mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V2(buf[1]);
969 mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V2(buf[1]);
970 mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V2(buf[1]);
971 mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2(buf[1]);
972
973 return 0;
974}
975
developer3e9ad9d2021-07-01 16:42:25 +0800976static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf)
977{
978 if (!CALIB_BUF1_VALID_V3(buf[1]))
979 return -EINVAL;
980
981 mt->adc_oe = CALIB_BUF0_ADC_OE_V3(buf[0]);
982 mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]);
983 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]);
984 mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]);
985 mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]);
986 mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]);
987 mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]);
988 mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]);
989
990 if (CALIB_BUF1_ID_V3(buf[1]) == 0)
991 mt->o_slope = 0;
992
993 return 0;
994}
995
developeraedf4202021-06-12 11:52:43 +0800996static int mtk_thermal_get_calibration_data(struct device *dev,
997 struct mtk_thermal *mt)
998{
999 struct nvmem_cell *cell;
1000 u32 *buf;
1001 size_t len;
1002 int i, ret = 0;
1003
1004 /* Start with default values */
1005 mt->adc_ge = 512;
developer3e9ad9d2021-07-01 16:42:25 +08001006 mt->adc_oe = 512;
developeraedf4202021-06-12 11:52:43 +08001007 for (i = 0; i < mt->conf->num_sensors; i++)
1008 mt->vts[i] = 260;
1009 mt->degc_cali = 40;
1010 mt->o_slope = 0;
1011
1012 cell = nvmem_cell_get(dev, "calibration-data");
1013 if (IS_ERR(cell)) {
1014 if (PTR_ERR(cell) == -EPROBE_DEFER)
1015 return PTR_ERR(cell);
1016 return 0;
1017 }
1018
1019 buf = (u32 *)nvmem_cell_read(cell, &len);
1020
1021 nvmem_cell_put(cell);
1022
1023 if (IS_ERR(buf))
1024 return PTR_ERR(buf);
1025
1026 if (len < 3 * sizeof(u32)) {
1027 dev_warn(dev, "invalid calibration data\n");
1028 ret = -EINVAL;
1029 goto out;
1030 }
1031
1032 if (mt->conf->version == MTK_THERMAL_V1)
1033 ret = mtk_thermal_extract_efuse_v1(mt, buf);
developer3e9ad9d2021-07-01 16:42:25 +08001034 else if (mt->conf->version == MTK_THERMAL_V2)
developeraedf4202021-06-12 11:52:43 +08001035 ret = mtk_thermal_extract_efuse_v2(mt, buf);
developer3e9ad9d2021-07-01 16:42:25 +08001036 else
1037 ret = mtk_thermal_extract_efuse_v3(mt, buf);
developeraedf4202021-06-12 11:52:43 +08001038
1039 if (ret) {
1040 dev_info(dev, "Device not calibrated, using default calibration values\n");
1041 ret = 0;
1042 }
1043
1044out:
1045 kfree(buf);
1046
1047 return ret;
1048}
1049
1050static const struct of_device_id mtk_thermal_of_match[] = {
1051 {
1052 .compatible = "mediatek,mt8173-thermal",
1053 .data = (void *)&mt8173_thermal_data,
1054 },
1055 {
1056 .compatible = "mediatek,mt2701-thermal",
1057 .data = (void *)&mt2701_thermal_data,
1058 },
1059 {
1060 .compatible = "mediatek,mt2712-thermal",
1061 .data = (void *)&mt2712_thermal_data,
1062 },
1063 {
1064 .compatible = "mediatek,mt7622-thermal",
1065 .data = (void *)&mt7622_thermal_data,
1066 },
1067 {
1068 .compatible = "mediatek,mt8183-thermal",
1069 .data = (void *)&mt8183_thermal_data,
developer3e9ad9d2021-07-01 16:42:25 +08001070 },
1071 {
1072 .compatible = "mediatek,mt7986-thermal",
1073 .data = (void *)&mt7986_thermal_data,
developeraedf4202021-06-12 11:52:43 +08001074 }, {
1075 },
1076};
1077MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
1078
1079static void mtk_thermal_turn_on_buffer(void __iomem *apmixed_base)
1080{
1081 int tmp;
1082
1083 tmp = readl(apmixed_base + APMIXED_SYS_TS_CON1);
1084 tmp &= ~(0x37);
1085 tmp |= 0x1;
1086 writel(tmp, apmixed_base + APMIXED_SYS_TS_CON1);
1087 udelay(200);
1088}
1089
1090static void mtk_thermal_release_periodic_ts(struct mtk_thermal *mt,
1091 void __iomem *auxadc_base)
1092{
1093 int tmp;
1094
1095 writel(0x800, auxadc_base + AUXADC_CON1_SET_V);
1096 writel(0x1, mt->thermal_base + TEMP_MONCTL0);
1097 tmp = readl(mt->thermal_base + TEMP_MSRCTL1);
1098 writel((tmp & (~0x10e)), mt->thermal_base + TEMP_MSRCTL1);
1099}
1100
1101static int mtk_thermal_probe(struct platform_device *pdev)
1102{
1103 int ret, i, ctrl_id;
1104 struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
1105 struct mtk_thermal *mt;
1106 struct resource *res;
1107 u64 auxadc_phys_base, apmixed_phys_base;
1108 struct thermal_zone_device *tzdev;
1109 void __iomem *apmixed_base, *auxadc_base;
1110
1111 mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
1112 if (!mt)
1113 return -ENOMEM;
1114
1115 mt->conf = of_device_get_match_data(&pdev->dev);
1116
1117 mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
1118 if (IS_ERR(mt->clk_peri_therm))
1119 return PTR_ERR(mt->clk_peri_therm);
1120
1121 mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
1122 if (IS_ERR(mt->clk_auxadc))
1123 return PTR_ERR(mt->clk_auxadc);
1124
1125 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1126 mt->thermal_base = devm_ioremap_resource(&pdev->dev, res);
1127 if (IS_ERR(mt->thermal_base))
1128 return PTR_ERR(mt->thermal_base);
1129
1130 ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
1131 if (ret)
1132 return ret;
1133
1134 mutex_init(&mt->lock);
1135
1136 mt->dev = &pdev->dev;
1137
1138 auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
1139 if (!auxadc) {
1140 dev_err(&pdev->dev, "missing auxadc node\n");
1141 return -ENODEV;
1142 }
1143
1144 auxadc_base = of_iomap(auxadc, 0);
1145 auxadc_phys_base = of_get_phys_base(auxadc);
1146
1147 of_node_put(auxadc);
1148
1149 if (auxadc_phys_base == OF_BAD_ADDR) {
1150 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1151 return -EINVAL;
1152 }
1153
1154 apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
1155 if (!apmixedsys) {
1156 dev_err(&pdev->dev, "missing apmixedsys node\n");
1157 return -ENODEV;
1158 }
1159
1160 apmixed_base = of_iomap(apmixedsys, 0);
1161 apmixed_phys_base = of_get_phys_base(apmixedsys);
1162
1163 of_node_put(apmixedsys);
1164
1165 if (apmixed_phys_base == OF_BAD_ADDR) {
1166 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1167 return -EINVAL;
1168 }
1169
1170 ret = device_reset_optional(&pdev->dev);
1171 if (ret)
1172 return ret;
1173
1174 ret = clk_prepare_enable(mt->clk_auxadc);
1175 if (ret) {
1176 dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
1177 return ret;
1178 }
1179
1180 ret = clk_prepare_enable(mt->clk_peri_therm);
1181 if (ret) {
1182 dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
1183 goto err_disable_clk_auxadc;
1184 }
1185
developer3e9ad9d2021-07-01 16:42:25 +08001186 if (mt->conf->version == MTK_THERMAL_V2 ||
1187 mt->conf->version == MTK_THERMAL_V3) {
developeraedf4202021-06-12 11:52:43 +08001188 mtk_thermal_turn_on_buffer(apmixed_base);
1189 mtk_thermal_release_periodic_ts(mt, auxadc_base);
1190 }
1191
1192 for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
1193 for (i = 0; i < mt->conf->num_banks; i++)
1194 mtk_thermal_init_bank(mt, i, apmixed_phys_base,
1195 auxadc_phys_base, ctrl_id);
1196
1197 platform_set_drvdata(pdev, mt);
1198
1199 tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mt,
1200 &mtk_thermal_ops);
1201 if (IS_ERR(tzdev)) {
1202 ret = PTR_ERR(tzdev);
1203 goto err_disable_clk_peri_therm;
1204 }
1205
1206 return 0;
1207
1208err_disable_clk_peri_therm:
1209 clk_disable_unprepare(mt->clk_peri_therm);
1210err_disable_clk_auxadc:
1211 clk_disable_unprepare(mt->clk_auxadc);
1212
1213 return ret;
1214}
1215
1216static int mtk_thermal_remove(struct platform_device *pdev)
1217{
1218 struct mtk_thermal *mt = platform_get_drvdata(pdev);
1219
1220 clk_disable_unprepare(mt->clk_peri_therm);
1221 clk_disable_unprepare(mt->clk_auxadc);
1222
1223 return 0;
1224}
1225
1226static struct platform_driver mtk_thermal_driver = {
1227 .probe = mtk_thermal_probe,
1228 .remove = mtk_thermal_remove,
1229 .driver = {
1230 .name = "mtk-thermal",
1231 .of_match_table = mtk_thermal_of_match,
1232 },
1233};
1234
1235module_platform_driver(mtk_thermal_driver);
1236
1237MODULE_AUTHOR("Michael Kao <michael.kao@mediatek.com>");
1238MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>");
1239MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>");
1240MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1241MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>");
1242MODULE_DESCRIPTION("Mediatek thermal driver");
1243MODULE_LICENSE("GPL v2");