blob: f73682d5bfc8e0ddedbbdde7f3bcef9b81885f38 [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{
675 s32 format_1;
676 s32 g_oe;
677 s32 g_gain;
678 s32 g_x_roomt;
679 s32 tmp;
680
681 if (raw == 0)
682 return 0;
683
684 raw &= 0xfff;
685 g_gain = 1 + ((mt->adc_ge - 512) >> 12);
686 g_oe = mt->adc_oe - 512;
687 format_1 = mt->vts[sensno] + 2900 - g_oe;
688 g_x_roomt = format_1 / (g_gain << 12);
689
690 tmp = (((raw - g_oe) >> 12) / g_gain) - g_x_roomt;
691 tmp = tmp * 15 / 18;
692 tmp = (tmp * 100000) / 149;
693 tmp = (mt->degc_cali >> 1) - tmp;
694
695 return tmp * 100;
696}
697
developeraedf4202021-06-12 11:52:43 +0800698/**
699 * mtk_thermal_get_bank - get bank
700 * @bank: The bank
701 *
702 * The bank registers are banked, we have to select a bank in the
703 * PTPCORESEL register to access it.
704 */
705static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
706{
707 struct mtk_thermal *mt = bank->mt;
708 u32 val;
709
710 if (mt->conf->need_switch_bank) {
711 mutex_lock(&mt->lock);
712
713 val = readl(mt->thermal_base + PTPCORESEL);
714 val &= ~0xf;
715 val |= bank->id;
716 writel(val, mt->thermal_base + PTPCORESEL);
717 }
718}
719
720/**
721 * mtk_thermal_put_bank - release bank
722 * @bank: The bank
723 *
724 * release a bank previously taken with mtk_thermal_get_bank,
725 */
726static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
727{
728 struct mtk_thermal *mt = bank->mt;
729
730 if (mt->conf->need_switch_bank)
731 mutex_unlock(&mt->lock);
732}
733
734/**
735 * mtk_thermal_bank_temperature - get the temperature of a bank
736 * @bank: The bank
737 *
738 * The temperature of a bank is considered the maximum temperature of
739 * the sensors associated to the bank.
740 */
741static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
742{
743 struct mtk_thermal *mt = bank->mt;
744 const struct mtk_thermal_data *conf = mt->conf;
745 int i, temp = INT_MIN, max = INT_MIN;
746 u32 raw;
747
748 for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
749 raw = readl(mt->thermal_base + conf->msr[i]);
750
751 if (mt->conf->version == MTK_THERMAL_V1) {
752 temp = raw_to_mcelsius_v1(
753 mt, conf->bank_data[bank->id].sensors[i], raw);
developer3e9ad9d2021-07-01 16:42:25 +0800754 } else if (mt->conf->version == MTK_THERMAL_V2) {
developeraedf4202021-06-12 11:52:43 +0800755 temp = raw_to_mcelsius_v2(
756 mt, conf->bank_data[bank->id].sensors[i], raw);
developer3e9ad9d2021-07-01 16:42:25 +0800757 } else {
758 temp = raw_to_mcelsius_v3(
759 mt, conf->bank_data[bank->id].sensors[i], raw);
developeraedf4202021-06-12 11:52:43 +0800760 }
761
762 /*
763 * The first read of a sensor often contains very high bogus
764 * temperature value. Filter these out so that the system does
765 * not immediately shut down.
766 */
767 if (temp > 200000)
768 temp = 0;
769
770 if (temp > max)
771 max = temp;
772 }
773
774 return max;
775}
776
777static int mtk_read_temp(void *data, int *temperature)
778{
779 struct mtk_thermal *mt = data;
780 int i;
781 int tempmax = INT_MIN;
782
783 for (i = 0; i < mt->conf->num_banks; i++) {
784 struct mtk_thermal_bank *bank = &mt->banks[i];
785
786 mtk_thermal_get_bank(bank);
787
788 tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
789
790 mtk_thermal_put_bank(bank);
791 }
792
793 *temperature = tempmax;
794
795 return 0;
796}
797
798static const struct thermal_zone_of_device_ops mtk_thermal_ops = {
799 .get_temp = mtk_read_temp,
800};
801
802static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
803 u32 apmixed_phys_base, u32 auxadc_phys_base,
804 int ctrl_id)
805{
806 struct mtk_thermal_bank *bank = &mt->banks[num];
807 const struct mtk_thermal_data *conf = mt->conf;
808 int i;
809
810 int offset = mt->conf->controller_offset[ctrl_id];
811 void __iomem *controller_base = mt->thermal_base + offset;
812
813 bank->id = num;
814 bank->mt = mt;
815
816 mtk_thermal_get_bank(bank);
817
818 /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
819 writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
820
821 /*
822 * filt interval is 1 * 46.540us = 46.54us,
823 * sen interval is 429 * 46.540us = 19.96ms
824 */
825 writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
826 TEMP_MONCTL2_SENSOR_INTERVAL(429),
827 controller_base + TEMP_MONCTL2);
828
829 /* poll is set to 10u */
830 writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
831 controller_base + TEMP_AHBPOLL);
832
833 /* temperature sampling control, 1 sample */
834 writel(0x0, controller_base + TEMP_MSRCTL0);
835
836 /* exceed this polling time, IRQ would be inserted */
837 writel(0xffffffff, controller_base + TEMP_AHBTO);
838
839 /* number of interrupts per event, 1 is enough */
840 writel(0x0, controller_base + TEMP_MONIDET0);
841 writel(0x0, controller_base + TEMP_MONIDET1);
842
843 /*
844 * The MT8173 thermal controller does not have its own ADC. Instead it
845 * uses AHB bus accesses to control the AUXADC. To do this the thermal
846 * controller has to be programmed with the physical addresses of the
847 * AUXADC registers and with the various bit positions in the AUXADC.
848 * Also the thermal controller controls a mux in the APMIXEDSYS register
849 * space.
850 */
851
852 /*
853 * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
854 * automatically by hw
855 */
856 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
857
858 /* AHB address for auxadc mux selection */
859 writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
860 controller_base + TEMP_ADCMUXADDR);
861
862 if (mt->conf->version == MTK_THERMAL_V1) {
863 /* AHB address for pnp sensor mux selection */
864 writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
865 controller_base + TEMP_PNPMUXADDR);
866 }
867
868 /* AHB value for auxadc enable */
869 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
870
871 /* AHB address for auxadc enable (channel 0 immediate mode selected) */
872 writel(auxadc_phys_base + AUXADC_CON1_SET_V,
873 controller_base + TEMP_ADCENADDR);
874
875 /* AHB address for auxadc valid bit */
876 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
877 controller_base + TEMP_ADCVALIDADDR);
878
879 /* AHB address for auxadc voltage output */
880 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
881 controller_base + TEMP_ADCVOLTADDR);
882
883 /* read valid & voltage are at the same register */
884 writel(0x0, controller_base + TEMP_RDCTRL);
885
886 /* indicate where the valid bit is */
887 writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
888 controller_base + TEMP_ADCVALIDMASK);
889
890 /* no shift */
891 writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
892
893 /* enable auxadc mux write transaction */
894 writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
895 controller_base + TEMP_ADCWRITECTRL);
896
897 for (i = 0; i < conf->bank_data[num].num_sensors; i++)
898 writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
899 mt->thermal_base + conf->adcpnp[i]);
900
901 writel((1 << conf->bank_data[num].num_sensors) - 1,
902 controller_base + TEMP_MONCTL0);
903
904 writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
905 TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
906 controller_base + TEMP_ADCWRITECTRL);
907
908 mtk_thermal_put_bank(bank);
909}
910
911static u64 of_get_phys_base(struct device_node *np)
912{
913 u64 size64;
914 const __be32 *regaddr_p;
915
916 regaddr_p = of_get_address(np, 0, &size64, NULL);
917 if (!regaddr_p)
918 return OF_BAD_ADDR;
919
920 return of_translate_address(np, regaddr_p);
921}
922
923static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf)
924{
925 int i;
926
927 if (!(buf[0] & CALIB_BUF0_VALID_V1))
928 return -EINVAL;
929
930 mt->adc_ge = CALIB_BUF1_ADC_GE_V1(buf[1]);
931
932 for (i = 0; i < mt->conf->num_sensors; i++) {
933 switch (mt->conf->vts_index[i]) {
934 case VTS1:
935 mt->vts[VTS1] = CALIB_BUF0_VTS_TS1_V1(buf[0]);
936 break;
937 case VTS2:
938 mt->vts[VTS2] = CALIB_BUF0_VTS_TS2_V1(buf[0]);
939 break;
940 case VTS3:
941 mt->vts[VTS3] = CALIB_BUF1_VTS_TS3_V1(buf[1]);
942 break;
943 case VTS4:
944 mt->vts[VTS4] = CALIB_BUF2_VTS_TS4_V1(buf[2]);
945 break;
946 case VTS5:
947 mt->vts[VTS5] = CALIB_BUF2_VTS_TS5_V1(buf[2]);
948 break;
949 case VTSABB:
950 mt->vts[VTSABB] =
951 CALIB_BUF2_VTS_TSABB_V1(buf[2]);
952 break;
953 default:
954 break;
955 }
956 }
957
958 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V1(buf[0]);
959 if (CALIB_BUF1_ID_V1(buf[1]) &
960 CALIB_BUF0_O_SLOPE_SIGN_V1(buf[0]))
961 mt->o_slope = -CALIB_BUF0_O_SLOPE_V1(buf[0]);
962 else
963 mt->o_slope = CALIB_BUF0_O_SLOPE_V1(buf[0]);
964
965 return 0;
966}
967
968static int mtk_thermal_extract_efuse_v2(struct mtk_thermal *mt, u32 *buf)
969{
970 if (!CALIB_BUF1_VALID_V2(buf[1]))
971 return -EINVAL;
972
973 mt->adc_oe = CALIB_BUF0_ADC_OE_V2(buf[0]);
974 mt->adc_ge = CALIB_BUF0_ADC_GE_V2(buf[0]);
975 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V2(buf[0]);
976 mt->o_slope = CALIB_BUF0_O_SLOPE_V2(buf[0]);
977 mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V2(buf[1]);
978 mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V2(buf[1]);
979 mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V2(buf[1]);
980 mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2(buf[1]);
981
982 return 0;
983}
984
developer3e9ad9d2021-07-01 16:42:25 +0800985static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf)
986{
987 if (!CALIB_BUF1_VALID_V3(buf[1]))
988 return -EINVAL;
989
990 mt->adc_oe = CALIB_BUF0_ADC_OE_V3(buf[0]);
991 mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]);
992 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]);
993 mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]);
994 mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]);
995 mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]);
996 mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]);
997 mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]);
998
999 if (CALIB_BUF1_ID_V3(buf[1]) == 0)
1000 mt->o_slope = 0;
1001
1002 return 0;
1003}
1004
developeraedf4202021-06-12 11:52:43 +08001005static int mtk_thermal_get_calibration_data(struct device *dev,
1006 struct mtk_thermal *mt)
1007{
1008 struct nvmem_cell *cell;
1009 u32 *buf;
1010 size_t len;
1011 int i, ret = 0;
1012
1013 /* Start with default values */
1014 mt->adc_ge = 512;
developer3e9ad9d2021-07-01 16:42:25 +08001015 mt->adc_oe = 512;
developeraedf4202021-06-12 11:52:43 +08001016 for (i = 0; i < mt->conf->num_sensors; i++)
1017 mt->vts[i] = 260;
1018 mt->degc_cali = 40;
1019 mt->o_slope = 0;
1020
1021 cell = nvmem_cell_get(dev, "calibration-data");
1022 if (IS_ERR(cell)) {
1023 if (PTR_ERR(cell) == -EPROBE_DEFER)
1024 return PTR_ERR(cell);
1025 return 0;
1026 }
1027
1028 buf = (u32 *)nvmem_cell_read(cell, &len);
1029
1030 nvmem_cell_put(cell);
1031
1032 if (IS_ERR(buf))
1033 return PTR_ERR(buf);
1034
1035 if (len < 3 * sizeof(u32)) {
1036 dev_warn(dev, "invalid calibration data\n");
1037 ret = -EINVAL;
1038 goto out;
1039 }
1040
1041 if (mt->conf->version == MTK_THERMAL_V1)
1042 ret = mtk_thermal_extract_efuse_v1(mt, buf);
developer3e9ad9d2021-07-01 16:42:25 +08001043 else if (mt->conf->version == MTK_THERMAL_V2)
developeraedf4202021-06-12 11:52:43 +08001044 ret = mtk_thermal_extract_efuse_v2(mt, buf);
developer3e9ad9d2021-07-01 16:42:25 +08001045 else
1046 ret = mtk_thermal_extract_efuse_v3(mt, buf);
developeraedf4202021-06-12 11:52:43 +08001047
1048 if (ret) {
1049 dev_info(dev, "Device not calibrated, using default calibration values\n");
1050 ret = 0;
1051 }
1052
1053out:
1054 kfree(buf);
1055
1056 return ret;
1057}
1058
1059static const struct of_device_id mtk_thermal_of_match[] = {
1060 {
1061 .compatible = "mediatek,mt8173-thermal",
1062 .data = (void *)&mt8173_thermal_data,
1063 },
1064 {
1065 .compatible = "mediatek,mt2701-thermal",
1066 .data = (void *)&mt2701_thermal_data,
1067 },
1068 {
1069 .compatible = "mediatek,mt2712-thermal",
1070 .data = (void *)&mt2712_thermal_data,
1071 },
1072 {
1073 .compatible = "mediatek,mt7622-thermal",
1074 .data = (void *)&mt7622_thermal_data,
1075 },
1076 {
1077 .compatible = "mediatek,mt8183-thermal",
1078 .data = (void *)&mt8183_thermal_data,
developer3e9ad9d2021-07-01 16:42:25 +08001079 },
1080 {
1081 .compatible = "mediatek,mt7986-thermal",
1082 .data = (void *)&mt7986_thermal_data,
developeraedf4202021-06-12 11:52:43 +08001083 }, {
1084 },
1085};
1086MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
1087
1088static void mtk_thermal_turn_on_buffer(void __iomem *apmixed_base)
1089{
1090 int tmp;
1091
1092 tmp = readl(apmixed_base + APMIXED_SYS_TS_CON1);
1093 tmp &= ~(0x37);
1094 tmp |= 0x1;
1095 writel(tmp, apmixed_base + APMIXED_SYS_TS_CON1);
1096 udelay(200);
1097}
1098
1099static void mtk_thermal_release_periodic_ts(struct mtk_thermal *mt,
1100 void __iomem *auxadc_base)
1101{
1102 int tmp;
1103
1104 writel(0x800, auxadc_base + AUXADC_CON1_SET_V);
1105 writel(0x1, mt->thermal_base + TEMP_MONCTL0);
1106 tmp = readl(mt->thermal_base + TEMP_MSRCTL1);
1107 writel((tmp & (~0x10e)), mt->thermal_base + TEMP_MSRCTL1);
1108}
1109
1110static int mtk_thermal_probe(struct platform_device *pdev)
1111{
1112 int ret, i, ctrl_id;
1113 struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
1114 struct mtk_thermal *mt;
1115 struct resource *res;
1116 u64 auxadc_phys_base, apmixed_phys_base;
1117 struct thermal_zone_device *tzdev;
1118 void __iomem *apmixed_base, *auxadc_base;
1119
1120 mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
1121 if (!mt)
1122 return -ENOMEM;
1123
1124 mt->conf = of_device_get_match_data(&pdev->dev);
1125
1126 mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
1127 if (IS_ERR(mt->clk_peri_therm))
1128 return PTR_ERR(mt->clk_peri_therm);
1129
1130 mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
1131 if (IS_ERR(mt->clk_auxadc))
1132 return PTR_ERR(mt->clk_auxadc);
1133
1134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1135 mt->thermal_base = devm_ioremap_resource(&pdev->dev, res);
1136 if (IS_ERR(mt->thermal_base))
1137 return PTR_ERR(mt->thermal_base);
1138
1139 ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
1140 if (ret)
1141 return ret;
1142
1143 mutex_init(&mt->lock);
1144
1145 mt->dev = &pdev->dev;
1146
1147 auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
1148 if (!auxadc) {
1149 dev_err(&pdev->dev, "missing auxadc node\n");
1150 return -ENODEV;
1151 }
1152
1153 auxadc_base = of_iomap(auxadc, 0);
1154 auxadc_phys_base = of_get_phys_base(auxadc);
1155
1156 of_node_put(auxadc);
1157
1158 if (auxadc_phys_base == OF_BAD_ADDR) {
1159 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1160 return -EINVAL;
1161 }
1162
1163 apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
1164 if (!apmixedsys) {
1165 dev_err(&pdev->dev, "missing apmixedsys node\n");
1166 return -ENODEV;
1167 }
1168
1169 apmixed_base = of_iomap(apmixedsys, 0);
1170 apmixed_phys_base = of_get_phys_base(apmixedsys);
1171
1172 of_node_put(apmixedsys);
1173
1174 if (apmixed_phys_base == OF_BAD_ADDR) {
1175 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1176 return -EINVAL;
1177 }
1178
1179 ret = device_reset_optional(&pdev->dev);
1180 if (ret)
1181 return ret;
1182
1183 ret = clk_prepare_enable(mt->clk_auxadc);
1184 if (ret) {
1185 dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
1186 return ret;
1187 }
1188
1189 ret = clk_prepare_enable(mt->clk_peri_therm);
1190 if (ret) {
1191 dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
1192 goto err_disable_clk_auxadc;
1193 }
1194
developer3e9ad9d2021-07-01 16:42:25 +08001195 if (mt->conf->version == MTK_THERMAL_V2 ||
1196 mt->conf->version == MTK_THERMAL_V3) {
developeraedf4202021-06-12 11:52:43 +08001197 mtk_thermal_turn_on_buffer(apmixed_base);
1198 mtk_thermal_release_periodic_ts(mt, auxadc_base);
1199 }
1200
1201 for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
1202 for (i = 0; i < mt->conf->num_banks; i++)
1203 mtk_thermal_init_bank(mt, i, apmixed_phys_base,
1204 auxadc_phys_base, ctrl_id);
1205
1206 platform_set_drvdata(pdev, mt);
1207
1208 tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mt,
1209 &mtk_thermal_ops);
1210 if (IS_ERR(tzdev)) {
1211 ret = PTR_ERR(tzdev);
1212 goto err_disable_clk_peri_therm;
1213 }
1214
1215 return 0;
1216
1217err_disable_clk_peri_therm:
1218 clk_disable_unprepare(mt->clk_peri_therm);
1219err_disable_clk_auxadc:
1220 clk_disable_unprepare(mt->clk_auxadc);
1221
1222 return ret;
1223}
1224
1225static int mtk_thermal_remove(struct platform_device *pdev)
1226{
1227 struct mtk_thermal *mt = platform_get_drvdata(pdev);
1228
1229 clk_disable_unprepare(mt->clk_peri_therm);
1230 clk_disable_unprepare(mt->clk_auxadc);
1231
1232 return 0;
1233}
1234
1235static struct platform_driver mtk_thermal_driver = {
1236 .probe = mtk_thermal_probe,
1237 .remove = mtk_thermal_remove,
1238 .driver = {
1239 .name = "mtk-thermal",
1240 .of_match_table = mtk_thermal_of_match,
1241 },
1242};
1243
1244module_platform_driver(mtk_thermal_driver);
1245
1246MODULE_AUTHOR("Michael Kao <michael.kao@mediatek.com>");
1247MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>");
1248MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>");
1249MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1250MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>");
1251MODULE_DESCRIPTION("Mediatek thermal driver");
1252MODULE_LICENSE("GPL v2");