thermal: cpu_cooling: Remove unused cur_freq variable
[linux-2.6-block.git] / drivers / thermal / mtk_thermal.c
CommitLineData
a92db1c8
SH
1/*
2 * Copyright (c) 2015 MediaTek Inc.
3 * Author: Hanyi Wu <hanyi.wu@mediatek.com>
4 * Sascha Hauer <s.hauer@pengutronix.de>
b7cf0053 5 * Dawei Chien <dawei.chien@mediatek.com>
6cf7f002 6 * Louis Yu <louis.yu@mediatek.com>
a92db1c8
SH
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/clk.h>
19#include <linux/delay.h>
20#include <linux/interrupt.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/nvmem-consumer.h>
24#include <linux/of.h>
25#include <linux/of_address.h>
b7cf0053 26#include <linux/of_device.h>
a92db1c8
SH
27#include <linux/platform_device.h>
28#include <linux/slab.h>
29#include <linux/io.h>
30#include <linux/thermal.h>
31#include <linux/reset.h>
32#include <linux/types.h>
a92db1c8
SH
33
34/* AUXADC Registers */
a92db1c8
SH
35#define AUXADC_CON1_SET_V 0x008
36#define AUXADC_CON1_CLR_V 0x00c
37#define AUXADC_CON2_V 0x010
38#define AUXADC_DATA(channel) (0x14 + (channel) * 4)
a92db1c8
SH
39
40#define APMIXED_SYS_TS_CON1 0x604
41
42/* Thermal Controller Registers */
43#define TEMP_MONCTL0 0x000
44#define TEMP_MONCTL1 0x004
45#define TEMP_MONCTL2 0x008
46#define TEMP_MONIDET0 0x014
47#define TEMP_MONIDET1 0x018
48#define TEMP_MSRCTL0 0x038
49#define TEMP_AHBPOLL 0x040
50#define TEMP_AHBTO 0x044
51#define TEMP_ADCPNP0 0x048
52#define TEMP_ADCPNP1 0x04c
53#define TEMP_ADCPNP2 0x050
54#define TEMP_ADCPNP3 0x0b4
55
56#define TEMP_ADCMUX 0x054
57#define TEMP_ADCEN 0x060
58#define TEMP_PNPMUXADDR 0x064
59#define TEMP_ADCMUXADDR 0x068
60#define TEMP_ADCENADDR 0x074
61#define TEMP_ADCVALIDADDR 0x078
62#define TEMP_ADCVOLTADDR 0x07c
63#define TEMP_RDCTRL 0x080
64#define TEMP_ADCVALIDMASK 0x084
65#define TEMP_ADCVOLTAGESHIFT 0x088
66#define TEMP_ADCWRITECTRL 0x08c
67#define TEMP_MSR0 0x090
68#define TEMP_MSR1 0x094
69#define TEMP_MSR2 0x098
70#define TEMP_MSR3 0x0B8
71
72#define TEMP_SPARE0 0x0f0
73
a4ffe6b5
MK
74#define TEMP_ADCPNP0_1 0x148
75#define TEMP_ADCPNP1_1 0x14c
76#define TEMP_ADCPNP2_1 0x150
77#define TEMP_MSR0_1 0x190
78#define TEMP_MSR1_1 0x194
79#define TEMP_MSR2_1 0x198
80#define TEMP_ADCPNP3_1 0x1b4
81#define TEMP_MSR3_1 0x1B8
82
a92db1c8
SH
83#define PTPCORESEL 0x400
84
85#define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff)
86
eb4fc33e 87#define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
a92db1c8
SH
88#define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
89
90#define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x) (x)
91
92#define TEMP_ADCWRITECTRL_ADC_PNP_WRITE BIT(0)
93#define TEMP_ADCWRITECTRL_ADC_MUX_WRITE BIT(1)
94
95#define TEMP_ADCVALIDMASK_VALID_HIGH BIT(5)
96#define TEMP_ADCVALIDMASK_VALID_POS(bit) (bit)
97
b7cf0053 98/* MT8173 thermal sensors */
a92db1c8
SH
99#define MT8173_TS1 0
100#define MT8173_TS2 1
101#define MT8173_TS3 2
102#define MT8173_TS4 3
103#define MT8173_TSABB 4
104
105/* AUXADC channel 11 is used for the temperature sensors */
106#define MT8173_TEMP_AUXADC_CHANNEL 11
107
108/* The total number of temperature sensors in the MT8173 */
109#define MT8173_NUM_SENSORS 5
110
111/* The number of banks in the MT8173 */
112#define MT8173_NUM_ZONES 4
113
114/* The number of sensing points per bank */
115#define MT8173_NUM_SENSORS_PER_ZONE 4
116
bd940394
MK
117/* The number of controller in the MT8173 */
118#define MT8173_NUM_CONTROLLER 1
119
f8451476
MK
120/* The calibration coefficient of sensor */
121#define MT8173_CALIBRATION 165
122
b7cf0053 123/*
124 * Layout of the fuses providing the calibration data
a4ffe6b5
MK
125 * These macros could be used for MT8183, MT8173, MT2701, and MT2712.
126 * MT8183 has 6 sensors and needs 6 VTS calibration data.
0a068993
LY
127 * MT8173 has 5 sensors and needs 5 VTS calibration data.
128 * MT2701 has 3 sensors and needs 3 VTS calibration data.
129 * MT2712 has 4 sensors and needs 4 VTS calibration data.
b7cf0053 130 */
1d081945
MK
131#define CALIB_BUF0_VALID BIT(0)
132#define CALIB_BUF1_ADC_GE(x) (((x) >> 22) & 0x3ff)
133#define CALIB_BUF0_VTS_TS1(x) (((x) >> 17) & 0x1ff)
134#define CALIB_BUF0_VTS_TS2(x) (((x) >> 8) & 0x1ff)
135#define CALIB_BUF1_VTS_TS3(x) (((x) >> 0) & 0x1ff)
136#define CALIB_BUF2_VTS_TS4(x) (((x) >> 23) & 0x1ff)
a4ffe6b5 137#define CALIB_BUF2_VTS_TS5(x) (((x) >> 5) & 0x1ff)
1d081945
MK
138#define CALIB_BUF2_VTS_TSABB(x) (((x) >> 14) & 0x1ff)
139#define CALIB_BUF0_DEGC_CALI(x) (((x) >> 1) & 0x3f)
140#define CALIB_BUF0_O_SLOPE(x) (((x) >> 26) & 0x3f)
141#define CALIB_BUF0_O_SLOPE_SIGN(x) (((x) >> 7) & 0x1)
142#define CALIB_BUF1_ID(x) (((x) >> 9) & 0x1)
143
144enum {
145 VTS1,
146 VTS2,
147 VTS3,
148 VTS4,
a4ffe6b5 149 VTS5,
1d081945
MK
150 VTSABB,
151 MAX_NUM_VTS,
152};
a92db1c8 153
b7cf0053 154/* MT2701 thermal sensors */
155#define MT2701_TS1 0
156#define MT2701_TS2 1
157#define MT2701_TSABB 2
158
159/* AUXADC channel 11 is used for the temperature sensors */
160#define MT2701_TEMP_AUXADC_CHANNEL 11
161
162/* The total number of temperature sensors in the MT2701 */
163#define MT2701_NUM_SENSORS 3
164
b7cf0053 165/* The number of sensing points per bank */
166#define MT2701_NUM_SENSORS_PER_ZONE 3
167
bd940394
MK
168/* The number of controller in the MT2701 */
169#define MT2701_NUM_CONTROLLER 1
170
f8451476
MK
171/* The calibration coefficient of sensor */
172#define MT2701_CALIBRATION 165
173
6cf7f002
LY
174/* MT2712 thermal sensors */
175#define MT2712_TS1 0
176#define MT2712_TS2 1
177#define MT2712_TS3 2
178#define MT2712_TS4 3
179
180/* AUXADC channel 11 is used for the temperature sensors */
181#define MT2712_TEMP_AUXADC_CHANNEL 11
182
183/* The total number of temperature sensors in the MT2712 */
184#define MT2712_NUM_SENSORS 4
185
186/* The number of sensing points per bank */
187#define MT2712_NUM_SENSORS_PER_ZONE 4
188
bd940394
MK
189/* The number of controller in the MT2712 */
190#define MT2712_NUM_CONTROLLER 1
191
f8451476
MK
192/* The calibration coefficient of sensor */
193#define MT2712_CALIBRATION 165
194
3966be3c
SW
195#define MT7622_TEMP_AUXADC_CHANNEL 11
196#define MT7622_NUM_SENSORS 1
197#define MT7622_NUM_ZONES 1
198#define MT7622_NUM_SENSORS_PER_ZONE 1
199#define MT7622_TS1 0
bd940394 200#define MT7622_NUM_CONTROLLER 1
3966be3c 201
f8451476
MK
202/* The calibration coefficient of sensor */
203#define MT7622_CALIBRATION 165
204
a4ffe6b5
MK
205/* MT8183 thermal sensors */
206#define MT8183_TS1 0
207#define MT8183_TS2 1
208#define MT8183_TS3 2
209#define MT8183_TS4 3
210#define MT8183_TS5 4
211#define MT8183_TSABB 5
212
213/* AUXADC channel is used for the temperature sensors */
214#define MT8183_TEMP_AUXADC_CHANNEL 11
215
216/* The total number of temperature sensors in the MT8183 */
217#define MT8183_NUM_SENSORS 6
218
219/* The number of sensing points per bank */
220#define MT8183_NUM_SENSORS_PER_ZONE 6
221
222/* The number of controller in the MT8183 */
223#define MT8183_NUM_CONTROLLER 2
224
225/* The calibration coefficient of sensor */
226#define MT8183_CALIBRATION 153
227
a92db1c8
SH
228struct mtk_thermal;
229
b7cf0053 230struct thermal_bank_cfg {
231 unsigned int num_sensors;
232 const int *sensors;
233};
234
a92db1c8
SH
235struct mtk_thermal_bank {
236 struct mtk_thermal *mt;
237 int id;
238};
239
b7cf0053 240struct mtk_thermal_data {
241 s32 num_banks;
242 s32 num_sensors;
243 s32 auxadc_channel;
1d081945 244 const int *vts_index;
b7cf0053 245 const int *sensor_mux_values;
246 const int *msr;
247 const int *adcpnp;
f8451476 248 const int cali_val;
bd940394
MK
249 const int num_controller;
250 const int *controller_offset;
cb82aaad 251 bool need_switch_bank;
b7cf0053 252 struct thermal_bank_cfg bank_data[];
253};
254
a92db1c8
SH
255struct mtk_thermal {
256 struct device *dev;
257 void __iomem *thermal_base;
258
259 struct clk *clk_peri_therm;
260 struct clk *clk_auxadc;
eb4fc33e 261 /* lock: for getting and putting banks */
a92db1c8
SH
262 struct mutex lock;
263
264 /* Calibration values */
265 s32 adc_ge;
266 s32 degc_cali;
267 s32 o_slope;
1d081945 268 s32 vts[MAX_NUM_VTS];
a92db1c8 269
b7cf0053 270 const struct mtk_thermal_data *conf;
271 struct mtk_thermal_bank banks[];
a92db1c8
SH
272};
273
a4ffe6b5
MK
274/* MT8183 thermal sensor data */
275static const int mt8183_bank_data[MT8183_NUM_SENSORS] = {
276 MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB
277};
278
279static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = {
280 TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1
281};
282
283static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = {
284 TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1,
285 TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1
286};
287
288static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 };
289static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100};
290
291static const int mt8183_vts_index[MT8183_NUM_SENSORS] = {
292 VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB
293};
294
b7cf0053 295/* MT8173 thermal sensor data */
992edf39 296static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
b7cf0053 297 { MT8173_TS2, MT8173_TS3 },
298 { MT8173_TS2, MT8173_TS4 },
299 { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
300 { MT8173_TS2 },
a92db1c8
SH
301};
302
992edf39 303static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
05d7839a 304 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
b7cf0053 305};
a92db1c8 306
992edf39 307static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
b7cf0053 308 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
309};
310
992edf39 311static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
bd940394 312static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, };
b7cf0053 313
1d081945
MK
314static const int mt8173_vts_index[MT8173_NUM_SENSORS] = {
315 VTS1, VTS2, VTS3, VTS4, VTSABB
316};
317
b7cf0053 318/* MT2701 thermal sensor data */
992edf39 319static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
b7cf0053 320 MT2701_TS1, MT2701_TS2, MT2701_TSABB
321};
322
992edf39 323static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
b7cf0053 324 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
325};
326
992edf39 327static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
b7cf0053 328 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
329};
330
992edf39 331static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
bd940394 332static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, };
b7cf0053 333
1d081945
MK
334static const int mt2701_vts_index[MT2701_NUM_SENSORS] = {
335 VTS1, VTS2, VTS3
336};
337
6cf7f002
LY
338/* MT2712 thermal sensor data */
339static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
340 MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
341};
342
343static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
344 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
345};
346
347static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
348 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
349};
350
351static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
bd940394 352static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, };
6cf7f002 353
1d081945
MK
354static const int mt2712_vts_index[MT2712_NUM_SENSORS] = {
355 VTS1, VTS2, VTS3, VTS4
356};
357
3966be3c
SW
358/* MT7622 thermal sensor data */
359static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
360static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
361static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
362static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
1d081945 363static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
bd940394 364static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
3966be3c 365
b7cf0053 366/**
a92db1c8
SH
367 * The MT8173 thermal controller has four banks. Each bank can read up to
368 * four temperature sensors simultaneously. The MT8173 has a total of 5
369 * temperature sensors. We use each bank to measure a certain area of the
370 * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
371 * areas, hence is used in different banks.
372 *
373 * The thermal core only gets the maximum temperature of all banks, so
374 * the bank concept wouldn't be necessary here. However, the SVS (Smart
375 * Voltage Scaling) unit makes its decisions based on the same bank
376 * data, and this indeed needs the temperatures of the individual banks
377 * for making better decisions.
378 */
b7cf0053 379static const struct mtk_thermal_data mt8173_thermal_data = {
380 .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
381 .num_banks = MT8173_NUM_ZONES,
382 .num_sensors = MT8173_NUM_SENSORS,
1d081945 383 .vts_index = mt8173_vts_index,
f8451476 384 .cali_val = MT8173_CALIBRATION,
bd940394
MK
385 .num_controller = MT8173_NUM_CONTROLLER,
386 .controller_offset = mt8173_tc_offset,
cb82aaad 387 .need_switch_bank = true,
b7cf0053 388 .bank_data = {
389 {
390 .num_sensors = 2,
391 .sensors = mt8173_bank_data[0],
392 }, {
393 .num_sensors = 2,
394 .sensors = mt8173_bank_data[1],
395 }, {
396 .num_sensors = 3,
397 .sensors = mt8173_bank_data[2],
398 }, {
399 .num_sensors = 1,
400 .sensors = mt8173_bank_data[3],
401 },
a92db1c8 402 },
b7cf0053 403 .msr = mt8173_msr,
404 .adcpnp = mt8173_adcpnp,
405 .sensor_mux_values = mt8173_mux_values,
a92db1c8
SH
406};
407
b7cf0053 408/**
409 * The MT2701 thermal controller has one bank, which can read up to
410 * three temperature sensors simultaneously. The MT2701 has a total of 3
411 * temperature sensors.
412 *
413 * The thermal core only gets the maximum temperature of this one bank,
414 * so the bank concept wouldn't be necessary here. However, the SVS (Smart
415 * Voltage Scaling) unit makes its decisions based on the same bank
416 * data.
417 */
418static const struct mtk_thermal_data mt2701_thermal_data = {
419 .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
420 .num_banks = 1,
421 .num_sensors = MT2701_NUM_SENSORS,
1d081945 422 .vts_index = mt2701_vts_index,
f8451476 423 .cali_val = MT2701_CALIBRATION,
bd940394
MK
424 .num_controller = MT2701_NUM_CONTROLLER,
425 .controller_offset = mt2701_tc_offset,
cb82aaad 426 .need_switch_bank = true,
b7cf0053 427 .bank_data = {
428 {
429 .num_sensors = 3,
430 .sensors = mt2701_bank_data,
431 },
a92db1c8 432 },
b7cf0053 433 .msr = mt2701_msr,
434 .adcpnp = mt2701_adcpnp,
435 .sensor_mux_values = mt2701_mux_values,
a92db1c8
SH
436};
437
6cf7f002
LY
438/**
439 * The MT2712 thermal controller has one bank, which can read up to
440 * four temperature sensors simultaneously. The MT2712 has a total of 4
441 * temperature sensors.
442 *
443 * The thermal core only gets the maximum temperature of this one bank,
444 * so the bank concept wouldn't be necessary here. However, the SVS (Smart
445 * Voltage Scaling) unit makes its decisions based on the same bank
446 * data.
447 */
448static const struct mtk_thermal_data mt2712_thermal_data = {
449 .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
450 .num_banks = 1,
451 .num_sensors = MT2712_NUM_SENSORS,
1d081945 452 .vts_index = mt2712_vts_index,
f8451476 453 .cali_val = MT2712_CALIBRATION,
bd940394
MK
454 .num_controller = MT2712_NUM_CONTROLLER,
455 .controller_offset = mt2712_tc_offset,
cb82aaad 456 .need_switch_bank = true,
6cf7f002
LY
457 .bank_data = {
458 {
459 .num_sensors = 4,
460 .sensors = mt2712_bank_data,
461 },
462 },
463 .msr = mt2712_msr,
464 .adcpnp = mt2712_adcpnp,
465 .sensor_mux_values = mt2712_mux_values,
466};
467
3966be3c
SW
468/*
469 * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data
470 * access.
471 */
472static const struct mtk_thermal_data mt7622_thermal_data = {
473 .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
474 .num_banks = MT7622_NUM_ZONES,
475 .num_sensors = MT7622_NUM_SENSORS,
1d081945 476 .vts_index = mt7622_vts_index,
f8451476 477 .cali_val = MT7622_CALIBRATION,
bd940394
MK
478 .num_controller = MT7622_NUM_CONTROLLER,
479 .controller_offset = mt7622_tc_offset,
cb82aaad 480 .need_switch_bank = true,
3966be3c
SW
481 .bank_data = {
482 {
483 .num_sensors = 1,
484 .sensors = mt7622_bank_data,
485 },
486 },
487 .msr = mt7622_msr,
488 .adcpnp = mt7622_adcpnp,
489 .sensor_mux_values = mt7622_mux_values,
490};
491
a4ffe6b5
MK
492/**
493 * The MT8183 thermal controller has one bank for the current SW framework.
494 * The MT8183 has a total of 6 temperature sensors.
495 * There are two thermal controller to control the six sensor.
496 * The first one bind 2 sensor, and the other bind 4 sensors.
497 * The thermal core only gets the maximum temperature of all sensor, so
498 * the bank concept wouldn't be necessary here. However, the SVS (Smart
499 * Voltage Scaling) unit makes its decisions based on the same bank
500 * data, and this indeed needs the temperatures of the individual banks
501 * for making better decisions.
502 */
503
504static const struct mtk_thermal_data mt8183_thermal_data = {
505 .auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL,
506 .num_banks = MT8183_NUM_SENSORS_PER_ZONE,
507 .num_sensors = MT8183_NUM_SENSORS,
508 .vts_index = mt8183_vts_index,
509 .cali_val = MT8183_CALIBRATION,
510 .num_controller = MT8183_NUM_CONTROLLER,
511 .controller_offset = mt8183_tc_offset,
512 .need_switch_bank = false,
513 .bank_data = {
514 {
515 .num_sensors = 6,
516 .sensors = mt8183_bank_data,
517 },
518 },
519
520 .msr = mt8183_msr,
521 .adcpnp = mt8183_adcpnp,
522 .sensor_mux_values = mt8183_mux_values,
523};
524
a92db1c8
SH
525/**
526 * raw_to_mcelsius - convert a raw ADC value to mcelsius
527 * @mt: The thermal controller
528 * @raw: raw ADC value
529 *
530 * This converts the raw ADC value to mcelsius using the SoC specific
531 * calibration constants
532 */
533static int raw_to_mcelsius(struct mtk_thermal *mt, int sensno, s32 raw)
534{
535 s32 tmp;
536
537 raw &= 0xfff;
538
539 tmp = 203450520 << 3;
f8451476 540 tmp /= mt->conf->cali_val + mt->o_slope;
a92db1c8
SH
541 tmp /= 10000 + mt->adc_ge;
542 tmp *= raw - mt->vts[sensno] - 3350;
543 tmp >>= 3;
544
545 return mt->degc_cali * 500 - tmp;
546}
547
548/**
549 * mtk_thermal_get_bank - get bank
550 * @bank: The bank
551 *
552 * The bank registers are banked, we have to select a bank in the
553 * PTPCORESEL register to access it.
554 */
555static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
556{
557 struct mtk_thermal *mt = bank->mt;
558 u32 val;
559
cb82aaad
MK
560 if (mt->conf->need_switch_bank) {
561 mutex_lock(&mt->lock);
a92db1c8 562
cb82aaad
MK
563 val = readl(mt->thermal_base + PTPCORESEL);
564 val &= ~0xf;
565 val |= bank->id;
566 writel(val, mt->thermal_base + PTPCORESEL);
567 }
a92db1c8
SH
568}
569
570/**
571 * mtk_thermal_put_bank - release bank
572 * @bank: The bank
573 *
574 * release a bank previously taken with mtk_thermal_get_bank,
575 */
576static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
577{
578 struct mtk_thermal *mt = bank->mt;
579
cb82aaad
MK
580 if (mt->conf->need_switch_bank)
581 mutex_unlock(&mt->lock);
a92db1c8
SH
582}
583
584/**
585 * mtk_thermal_bank_temperature - get the temperature of a bank
586 * @bank: The bank
587 *
588 * The temperature of a bank is considered the maximum temperature of
589 * the sensors associated to the bank.
590 */
591static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
592{
593 struct mtk_thermal *mt = bank->mt;
b7cf0053 594 const struct mtk_thermal_data *conf = mt->conf;
eb4fc33e 595 int i, temp = INT_MIN, max = INT_MIN;
a92db1c8
SH
596 u32 raw;
597
b7cf0053 598 for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
eb9aecd9
MK
599 raw = readl(mt->thermal_base +
600 conf->msr[conf->bank_data[bank->id].sensors[i]]);
a92db1c8 601
b7cf0053 602 temp = raw_to_mcelsius(mt,
603 conf->bank_data[bank->id].sensors[i],
604 raw);
a92db1c8
SH
605
606 /*
607 * The first read of a sensor often contains very high bogus
608 * temperature value. Filter these out so that the system does
609 * not immediately shut down.
610 */
611 if (temp > 200000)
612 temp = 0;
613
614 if (temp > max)
615 max = temp;
616 }
617
618 return max;
619}
620
621static int mtk_read_temp(void *data, int *temperature)
622{
623 struct mtk_thermal *mt = data;
624 int i;
625 int tempmax = INT_MIN;
626
b7cf0053 627 for (i = 0; i < mt->conf->num_banks; i++) {
a92db1c8
SH
628 struct mtk_thermal_bank *bank = &mt->banks[i];
629
630 mtk_thermal_get_bank(bank);
631
632 tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
633
634 mtk_thermal_put_bank(bank);
635 }
636
637 *temperature = tempmax;
638
639 return 0;
640}
641
642static const struct thermal_zone_of_device_ops mtk_thermal_ops = {
643 .get_temp = mtk_read_temp,
644};
645
646static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
bd940394
MK
647 u32 apmixed_phys_base, u32 auxadc_phys_base,
648 int ctrl_id)
a92db1c8
SH
649{
650 struct mtk_thermal_bank *bank = &mt->banks[num];
b7cf0053 651 const struct mtk_thermal_data *conf = mt->conf;
a92db1c8
SH
652 int i;
653
bd940394
MK
654 int offset = mt->conf->controller_offset[ctrl_id];
655 void __iomem *controller_base = mt->thermal_base + offset;
656
a92db1c8
SH
657 bank->id = num;
658 bank->mt = mt;
659
660 mtk_thermal_get_bank(bank);
661
662 /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
bd940394 663 writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
a92db1c8
SH
664
665 /*
666 * filt interval is 1 * 46.540us = 46.54us,
667 * sen interval is 429 * 46.540us = 19.96ms
668 */
669 writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
670 TEMP_MONCTL2_SENSOR_INTERVAL(429),
bd940394 671 controller_base + TEMP_MONCTL2);
a92db1c8
SH
672
673 /* poll is set to 10u */
674 writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
bd940394 675 controller_base + TEMP_AHBPOLL);
a92db1c8
SH
676
677 /* temperature sampling control, 1 sample */
bd940394 678 writel(0x0, controller_base + TEMP_MSRCTL0);
a92db1c8
SH
679
680 /* exceed this polling time, IRQ would be inserted */
bd940394 681 writel(0xffffffff, controller_base + TEMP_AHBTO);
a92db1c8
SH
682
683 /* number of interrupts per event, 1 is enough */
bd940394
MK
684 writel(0x0, controller_base + TEMP_MONIDET0);
685 writel(0x0, controller_base + TEMP_MONIDET1);
a92db1c8
SH
686
687 /*
688 * The MT8173 thermal controller does not have its own ADC. Instead it
689 * uses AHB bus accesses to control the AUXADC. To do this the thermal
690 * controller has to be programmed with the physical addresses of the
691 * AUXADC registers and with the various bit positions in the AUXADC.
692 * Also the thermal controller controls a mux in the APMIXEDSYS register
693 * space.
694 */
695
696 /*
697 * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
698 * automatically by hw
699 */
bd940394 700 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
a92db1c8
SH
701
702 /* AHB address for auxadc mux selection */
703 writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
bd940394 704 controller_base + TEMP_ADCMUXADDR);
a92db1c8
SH
705
706 /* AHB address for pnp sensor mux selection */
707 writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
bd940394 708 controller_base + TEMP_PNPMUXADDR);
a92db1c8
SH
709
710 /* AHB value for auxadc enable */
bd940394 711 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
a92db1c8
SH
712
713 /* AHB address for auxadc enable (channel 0 immediate mode selected) */
714 writel(auxadc_phys_base + AUXADC_CON1_SET_V,
bd940394 715 controller_base + TEMP_ADCENADDR);
a92db1c8
SH
716
717 /* AHB address for auxadc valid bit */
b7cf0053 718 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
bd940394 719 controller_base + TEMP_ADCVALIDADDR);
a92db1c8
SH
720
721 /* AHB address for auxadc voltage output */
b7cf0053 722 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
bd940394 723 controller_base + TEMP_ADCVOLTADDR);
a92db1c8
SH
724
725 /* read valid & voltage are at the same register */
bd940394 726 writel(0x0, controller_base + TEMP_RDCTRL);
a92db1c8
SH
727
728 /* indicate where the valid bit is */
729 writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
bd940394 730 controller_base + TEMP_ADCVALIDMASK);
a92db1c8
SH
731
732 /* no shift */
bd940394 733 writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
a92db1c8
SH
734
735 /* enable auxadc mux write transaction */
736 writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
bd940394 737 controller_base + TEMP_ADCWRITECTRL);
a92db1c8 738
b7cf0053 739 for (i = 0; i < conf->bank_data[num].num_sensors; i++)
740 writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
eb9aecd9
MK
741 mt->thermal_base +
742 conf->adcpnp[conf->bank_data[num].sensors[i]]);
a92db1c8 743
b7cf0053 744 writel((1 << conf->bank_data[num].num_sensors) - 1,
bd940394 745 controller_base + TEMP_MONCTL0);
a92db1c8 746
eb4fc33e
EV
747 writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
748 TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
bd940394 749 controller_base + TEMP_ADCWRITECTRL);
a92db1c8
SH
750
751 mtk_thermal_put_bank(bank);
752}
753
754static u64 of_get_phys_base(struct device_node *np)
755{
756 u64 size64;
757 const __be32 *regaddr_p;
758
759 regaddr_p = of_get_address(np, 0, &size64, NULL);
760 if (!regaddr_p)
761 return OF_BAD_ADDR;
762
763 return of_translate_address(np, regaddr_p);
764}
765
eb4fc33e
EV
766static int mtk_thermal_get_calibration_data(struct device *dev,
767 struct mtk_thermal *mt)
a92db1c8
SH
768{
769 struct nvmem_cell *cell;
770 u32 *buf;
771 size_t len;
772 int i, ret = 0;
773
774 /* Start with default values */
775 mt->adc_ge = 512;
b7cf0053 776 for (i = 0; i < mt->conf->num_sensors; i++)
a92db1c8
SH
777 mt->vts[i] = 260;
778 mt->degc_cali = 40;
779 mt->o_slope = 0;
780
781 cell = nvmem_cell_get(dev, "calibration-data");
782 if (IS_ERR(cell)) {
783 if (PTR_ERR(cell) == -EPROBE_DEFER)
784 return PTR_ERR(cell);
785 return 0;
786 }
787
788 buf = (u32 *)nvmem_cell_read(cell, &len);
789
790 nvmem_cell_put(cell);
791
792 if (IS_ERR(buf))
793 return PTR_ERR(buf);
794
795 if (len < 3 * sizeof(u32)) {
796 dev_warn(dev, "invalid calibration data\n");
797 ret = -EINVAL;
798 goto out;
799 }
800
1d081945
MK
801 if (buf[0] & CALIB_BUF0_VALID) {
802 mt->adc_ge = CALIB_BUF1_ADC_GE(buf[1]);
803
804 for (i = 0; i < mt->conf->num_sensors; i++) {
805 switch (mt->conf->vts_index[i]) {
806 case VTS1:
807 mt->vts[VTS1] = CALIB_BUF0_VTS_TS1(buf[0]);
808 break;
809 case VTS2:
810 mt->vts[VTS2] = CALIB_BUF0_VTS_TS2(buf[0]);
811 break;
812 case VTS3:
813 mt->vts[VTS3] = CALIB_BUF1_VTS_TS3(buf[1]);
814 break;
815 case VTS4:
816 mt->vts[VTS4] = CALIB_BUF2_VTS_TS4(buf[2]);
817 break;
a4ffe6b5
MK
818 case VTS5:
819 mt->vts[VTS5] = CALIB_BUF2_VTS_TS5(buf[2]);
820 break;
1d081945
MK
821 case VTSABB:
822 mt->vts[VTSABB] = CALIB_BUF2_VTS_TSABB(buf[2]);
823 break;
824 default:
825 break;
826 }
827 }
828
829 mt->degc_cali = CALIB_BUF0_DEGC_CALI(buf[0]);
830 if (CALIB_BUF1_ID(buf[1]) &
831 CALIB_BUF0_O_SLOPE_SIGN(buf[0]))
832 mt->o_slope = -CALIB_BUF0_O_SLOPE(buf[0]);
0a068993 833 else
1d081945 834 mt->o_slope = CALIB_BUF0_O_SLOPE(buf[0]);
a92db1c8
SH
835 } else {
836 dev_info(dev, "Device not calibrated, using default calibration values\n");
837 }
838
839out:
840 kfree(buf);
841
842 return ret;
843}
844
b7cf0053 845static const struct of_device_id mtk_thermal_of_match[] = {
846 {
847 .compatible = "mediatek,mt8173-thermal",
848 .data = (void *)&mt8173_thermal_data,
849 },
850 {
851 .compatible = "mediatek,mt2701-thermal",
852 .data = (void *)&mt2701_thermal_data,
6cf7f002
LY
853 },
854 {
855 .compatible = "mediatek,mt2712-thermal",
856 .data = (void *)&mt2712_thermal_data,
3966be3c
SW
857 },
858 {
859 .compatible = "mediatek,mt7622-thermal",
860 .data = (void *)&mt7622_thermal_data,
a4ffe6b5
MK
861 },
862 {
863 .compatible = "mediatek,mt8183-thermal",
864 .data = (void *)&mt8183_thermal_data,
b7cf0053 865 }, {
866 },
867};
868MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
869
a92db1c8
SH
870static int mtk_thermal_probe(struct platform_device *pdev)
871{
bd940394 872 int ret, i, ctrl_id;
a92db1c8
SH
873 struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
874 struct mtk_thermal *mt;
875 struct resource *res;
876 u64 auxadc_phys_base, apmixed_phys_base;
1f6b0889 877 struct thermal_zone_device *tzdev;
a92db1c8
SH
878
879 mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
880 if (!mt)
881 return -ENOMEM;
882
9efc58df 883 mt->conf = of_device_get_match_data(&pdev->dev);
b7cf0053 884
a92db1c8
SH
885 mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
886 if (IS_ERR(mt->clk_peri_therm))
887 return PTR_ERR(mt->clk_peri_therm);
888
889 mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
890 if (IS_ERR(mt->clk_auxadc))
891 return PTR_ERR(mt->clk_auxadc);
892
893 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
894 mt->thermal_base = devm_ioremap_resource(&pdev->dev, res);
895 if (IS_ERR(mt->thermal_base))
896 return PTR_ERR(mt->thermal_base);
897
898 ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
899 if (ret)
900 return ret;
901
902 mutex_init(&mt->lock);
903
904 mt->dev = &pdev->dev;
905
906 auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
907 if (!auxadc) {
908 dev_err(&pdev->dev, "missing auxadc node\n");
909 return -ENODEV;
910 }
911
912 auxadc_phys_base = of_get_phys_base(auxadc);
913
914 of_node_put(auxadc);
915
916 if (auxadc_phys_base == OF_BAD_ADDR) {
917 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
918 return -EINVAL;
919 }
920
921 apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
922 if (!apmixedsys) {
923 dev_err(&pdev->dev, "missing apmixedsys node\n");
924 return -ENODEV;
925 }
926
927 apmixed_phys_base = of_get_phys_base(apmixedsys);
928
929 of_node_put(apmixedsys);
930
931 if (apmixed_phys_base == OF_BAD_ADDR) {
932 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
933 return -EINVAL;
934 }
935
6760f3f7
LY
936 ret = device_reset(&pdev->dev);
937 if (ret)
938 return ret;
939
a92db1c8
SH
940 ret = clk_prepare_enable(mt->clk_auxadc);
941 if (ret) {
942 dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
943 return ret;
944 }
945
a92db1c8
SH
946 ret = clk_prepare_enable(mt->clk_peri_therm);
947 if (ret) {
948 dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
949 goto err_disable_clk_auxadc;
950 }
951
bd940394
MK
952 for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
953 for (i = 0; i < mt->conf->num_banks; i++)
954 mtk_thermal_init_bank(mt, i, apmixed_phys_base,
955 auxadc_phys_base, ctrl_id);
a92db1c8
SH
956
957 platform_set_drvdata(pdev, mt);
958
1f6b0889
AL
959 tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mt,
960 &mtk_thermal_ops);
961 if (IS_ERR(tzdev)) {
962 ret = PTR_ERR(tzdev);
963 goto err_disable_clk_peri_therm;
964 }
a92db1c8
SH
965
966 return 0;
967
1f6b0889
AL
968err_disable_clk_peri_therm:
969 clk_disable_unprepare(mt->clk_peri_therm);
a92db1c8
SH
970err_disable_clk_auxadc:
971 clk_disable_unprepare(mt->clk_auxadc);
972
973 return ret;
974}
975
976static int mtk_thermal_remove(struct platform_device *pdev)
977{
978 struct mtk_thermal *mt = platform_get_drvdata(pdev);
979
a92db1c8
SH
980 clk_disable_unprepare(mt->clk_peri_therm);
981 clk_disable_unprepare(mt->clk_auxadc);
982
983 return 0;
984}
985
a92db1c8
SH
986static struct platform_driver mtk_thermal_driver = {
987 .probe = mtk_thermal_probe,
988 .remove = mtk_thermal_remove,
989 .driver = {
f45ce7ee 990 .name = "mtk-thermal",
a92db1c8
SH
991 .of_match_table = mtk_thermal_of_match,
992 },
993};
994
995module_platform_driver(mtk_thermal_driver);
996
a4ffe6b5 997MODULE_AUTHOR("Michael Kao <michael.kao@mediatek.com>");
6cf7f002 998MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>");
b7cf0053 999MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>");
9ebfb4e0 1000MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
a92db1c8
SH
1001MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>");
1002MODULE_DESCRIPTION("Mediatek thermal driver");
1003MODULE_LICENSE("GPL v2");