blob: e35d28d4e8f8715a97f67303ff1b3b099d6a945c [file] [log] [blame]
Sascha Hauera92db1c2015-11-30 12:42:32 +01001/*
2 * Copyright (c) 2015 MediaTek Inc.
3 * Author: Hanyi Wu <hanyi.wu@mediatek.com>
4 * Sascha Hauer <s.hauer@pengutronix.de>
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +08005 * Dawei Chien <dawei.chien@mediatek.com>
Louis Yu6cf7f002017-08-01 15:28:31 +08006 * Louis Yu <louis.yu@mediatek.com>
Sascha Hauera92db1c2015-11-30 12:42:32 +01007 *
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>
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +080026#include <linux/of_device.h>
Sascha Hauera92db1c2015-11-30 12:42:32 +010027#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>
Sascha Hauera92db1c2015-11-30 12:42:32 +010033
34/* AUXADC Registers */
35#define AUXADC_CON0_V 0x000
36#define AUXADC_CON1_V 0x004
37#define AUXADC_CON1_SET_V 0x008
38#define AUXADC_CON1_CLR_V 0x00c
39#define AUXADC_CON2_V 0x010
40#define AUXADC_DATA(channel) (0x14 + (channel) * 4)
41#define AUXADC_MISC_V 0x094
42
43#define AUXADC_CON1_CHANNEL(x) BIT(x)
44
45#define APMIXED_SYS_TS_CON1 0x604
46
47/* Thermal Controller Registers */
48#define TEMP_MONCTL0 0x000
49#define TEMP_MONCTL1 0x004
50#define TEMP_MONCTL2 0x008
51#define TEMP_MONIDET0 0x014
52#define TEMP_MONIDET1 0x018
53#define TEMP_MSRCTL0 0x038
54#define TEMP_AHBPOLL 0x040
55#define TEMP_AHBTO 0x044
56#define TEMP_ADCPNP0 0x048
57#define TEMP_ADCPNP1 0x04c
58#define TEMP_ADCPNP2 0x050
59#define TEMP_ADCPNP3 0x0b4
60
61#define TEMP_ADCMUX 0x054
62#define TEMP_ADCEN 0x060
63#define TEMP_PNPMUXADDR 0x064
64#define TEMP_ADCMUXADDR 0x068
65#define TEMP_ADCENADDR 0x074
66#define TEMP_ADCVALIDADDR 0x078
67#define TEMP_ADCVOLTADDR 0x07c
68#define TEMP_RDCTRL 0x080
69#define TEMP_ADCVALIDMASK 0x084
70#define TEMP_ADCVOLTAGESHIFT 0x088
71#define TEMP_ADCWRITECTRL 0x08c
72#define TEMP_MSR0 0x090
73#define TEMP_MSR1 0x094
74#define TEMP_MSR2 0x098
75#define TEMP_MSR3 0x0B8
76
77#define TEMP_SPARE0 0x0f0
78
79#define PTPCORESEL 0x400
80
81#define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff)
82
Eduardo Valentineb4fc332016-02-18 07:43:57 -080083#define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
Sascha Hauera92db1c2015-11-30 12:42:32 +010084#define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
85
86#define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x) (x)
87
88#define TEMP_ADCWRITECTRL_ADC_PNP_WRITE BIT(0)
89#define TEMP_ADCWRITECTRL_ADC_MUX_WRITE BIT(1)
90
91#define TEMP_ADCVALIDMASK_VALID_HIGH BIT(5)
92#define TEMP_ADCVALIDMASK_VALID_POS(bit) (bit)
93
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +080094/* MT8173 thermal sensors */
Sascha Hauera92db1c2015-11-30 12:42:32 +010095#define MT8173_TS1 0
96#define MT8173_TS2 1
97#define MT8173_TS3 2
98#define MT8173_TS4 3
99#define MT8173_TSABB 4
100
101/* AUXADC channel 11 is used for the temperature sensors */
102#define MT8173_TEMP_AUXADC_CHANNEL 11
103
104/* The total number of temperature sensors in the MT8173 */
105#define MT8173_NUM_SENSORS 5
106
107/* The number of banks in the MT8173 */
108#define MT8173_NUM_ZONES 4
109
110/* The number of sensing points per bank */
111#define MT8173_NUM_SENSORS_PER_ZONE 4
112
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800113/*
114 * Layout of the fuses providing the calibration data
Louis Yu6cf7f002017-08-01 15:28:31 +0800115 * These macros could be used for both MT8173, MT2701, and MT2712.
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800116 * MT8173 has five sensors and need five VTS calibration data,
Louis Yu6cf7f002017-08-01 15:28:31 +0800117 * and MT2701 has three sensors and need three VTS calibration data,
118 * and MT2712 has four sensors and need four VTS calibration data.
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800119 */
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800120#define MT8173_CALIB_BUF0_VALID BIT(0)
121#define MT8173_CALIB_BUF1_ADC_GE(x) (((x) >> 22) & 0x3ff)
122#define MT8173_CALIB_BUF0_VTS_TS1(x) (((x) >> 17) & 0x1ff)
123#define MT8173_CALIB_BUF0_VTS_TS2(x) (((x) >> 8) & 0x1ff)
124#define MT8173_CALIB_BUF1_VTS_TS3(x) (((x) >> 0) & 0x1ff)
125#define MT8173_CALIB_BUF2_VTS_TS4(x) (((x) >> 23) & 0x1ff)
126#define MT8173_CALIB_BUF2_VTS_TSABB(x) (((x) >> 14) & 0x1ff)
127#define MT8173_CALIB_BUF0_DEGC_CALI(x) (((x) >> 1) & 0x3f)
128#define MT8173_CALIB_BUF0_O_SLOPE(x) (((x) >> 26) & 0x3f)
Sascha Hauera92db1c2015-11-30 12:42:32 +0100129
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800130/* MT2701 thermal sensors */
131#define MT2701_TS1 0
132#define MT2701_TS2 1
133#define MT2701_TSABB 2
134
135/* AUXADC channel 11 is used for the temperature sensors */
136#define MT2701_TEMP_AUXADC_CHANNEL 11
137
138/* The total number of temperature sensors in the MT2701 */
139#define MT2701_NUM_SENSORS 3
140
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800141/* The number of sensing points per bank */
142#define MT2701_NUM_SENSORS_PER_ZONE 3
143
Louis Yu6cf7f002017-08-01 15:28:31 +0800144/* MT2712 thermal sensors */
145#define MT2712_TS1 0
146#define MT2712_TS2 1
147#define MT2712_TS3 2
148#define MT2712_TS4 3
149
150/* AUXADC channel 11 is used for the temperature sensors */
151#define MT2712_TEMP_AUXADC_CHANNEL 11
152
153/* The total number of temperature sensors in the MT2712 */
154#define MT2712_NUM_SENSORS 4
155
156/* The number of sensing points per bank */
157#define MT2712_NUM_SENSORS_PER_ZONE 4
158
159#define THERMAL_NAME "mtk-thermal"
160
Sascha Hauera92db1c2015-11-30 12:42:32 +0100161struct mtk_thermal;
162
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800163struct thermal_bank_cfg {
164 unsigned int num_sensors;
165 const int *sensors;
166};
167
Sascha Hauera92db1c2015-11-30 12:42:32 +0100168struct mtk_thermal_bank {
169 struct mtk_thermal *mt;
170 int id;
171};
172
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800173struct mtk_thermal_data {
174 s32 num_banks;
175 s32 num_sensors;
176 s32 auxadc_channel;
177 const int *sensor_mux_values;
178 const int *msr;
179 const int *adcpnp;
180 struct thermal_bank_cfg bank_data[];
181};
182
Sascha Hauera92db1c2015-11-30 12:42:32 +0100183struct mtk_thermal {
184 struct device *dev;
185 void __iomem *thermal_base;
186
187 struct clk *clk_peri_therm;
188 struct clk *clk_auxadc;
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800189 /* lock: for getting and putting banks */
Sascha Hauera92db1c2015-11-30 12:42:32 +0100190 struct mutex lock;
191
192 /* Calibration values */
193 s32 adc_ge;
194 s32 degc_cali;
195 s32 o_slope;
196 s32 vts[MT8173_NUM_SENSORS];
197
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800198 const struct mtk_thermal_data *conf;
199 struct mtk_thermal_bank banks[];
Sascha Hauera92db1c2015-11-30 12:42:32 +0100200};
201
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800202/* MT8173 thermal sensor data */
Vivek Gautam992edf32016-12-28 14:16:45 +0530203static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800204 { MT8173_TS2, MT8173_TS3 },
205 { MT8173_TS2, MT8173_TS4 },
206 { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
207 { MT8173_TS2 },
Sascha Hauera92db1c2015-11-30 12:42:32 +0100208};
209
Vivek Gautam992edf32016-12-28 14:16:45 +0530210static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
Dawei Chien05d78392017-02-21 20:26:52 +0800211 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800212};
Sascha Hauera92db1c2015-11-30 12:42:32 +0100213
Vivek Gautam992edf32016-12-28 14:16:45 +0530214static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800215 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
216};
217
Vivek Gautam992edf32016-12-28 14:16:45 +0530218static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800219
220/* MT2701 thermal sensor data */
Vivek Gautam992edf32016-12-28 14:16:45 +0530221static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800222 MT2701_TS1, MT2701_TS2, MT2701_TSABB
223};
224
Vivek Gautam992edf32016-12-28 14:16:45 +0530225static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800226 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
227};
228
Vivek Gautam992edf32016-12-28 14:16:45 +0530229static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800230 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
231};
232
Vivek Gautam992edf32016-12-28 14:16:45 +0530233static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800234
Louis Yu6cf7f002017-08-01 15:28:31 +0800235/* MT2712 thermal sensor data */
236static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
237 MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
238};
239
240static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
241 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
242};
243
244static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
245 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
246};
247
248static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
249
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800250/**
Sascha Hauera92db1c2015-11-30 12:42:32 +0100251 * The MT8173 thermal controller has four banks. Each bank can read up to
252 * four temperature sensors simultaneously. The MT8173 has a total of 5
253 * temperature sensors. We use each bank to measure a certain area of the
254 * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
255 * areas, hence is used in different banks.
256 *
257 * The thermal core only gets the maximum temperature of all banks, so
258 * the bank concept wouldn't be necessary here. However, the SVS (Smart
259 * Voltage Scaling) unit makes its decisions based on the same bank
260 * data, and this indeed needs the temperatures of the individual banks
261 * for making better decisions.
262 */
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800263static const struct mtk_thermal_data mt8173_thermal_data = {
264 .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
265 .num_banks = MT8173_NUM_ZONES,
266 .num_sensors = MT8173_NUM_SENSORS,
267 .bank_data = {
268 {
269 .num_sensors = 2,
270 .sensors = mt8173_bank_data[0],
271 }, {
272 .num_sensors = 2,
273 .sensors = mt8173_bank_data[1],
274 }, {
275 .num_sensors = 3,
276 .sensors = mt8173_bank_data[2],
277 }, {
278 .num_sensors = 1,
279 .sensors = mt8173_bank_data[3],
280 },
Sascha Hauera92db1c2015-11-30 12:42:32 +0100281 },
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800282 .msr = mt8173_msr,
283 .adcpnp = mt8173_adcpnp,
284 .sensor_mux_values = mt8173_mux_values,
Sascha Hauera92db1c2015-11-30 12:42:32 +0100285};
286
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800287/**
288 * The MT2701 thermal controller has one bank, which can read up to
289 * three temperature sensors simultaneously. The MT2701 has a total of 3
290 * temperature sensors.
291 *
292 * The thermal core only gets the maximum temperature of this one bank,
293 * so the bank concept wouldn't be necessary here. However, the SVS (Smart
294 * Voltage Scaling) unit makes its decisions based on the same bank
295 * data.
296 */
297static const struct mtk_thermal_data mt2701_thermal_data = {
298 .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
299 .num_banks = 1,
300 .num_sensors = MT2701_NUM_SENSORS,
301 .bank_data = {
302 {
303 .num_sensors = 3,
304 .sensors = mt2701_bank_data,
305 },
Sascha Hauera92db1c2015-11-30 12:42:32 +0100306 },
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800307 .msr = mt2701_msr,
308 .adcpnp = mt2701_adcpnp,
309 .sensor_mux_values = mt2701_mux_values,
Sascha Hauera92db1c2015-11-30 12:42:32 +0100310};
311
312/**
Louis Yu6cf7f002017-08-01 15:28:31 +0800313 * The MT2712 thermal controller has one bank, which can read up to
314 * four temperature sensors simultaneously. The MT2712 has a total of 4
315 * temperature sensors.
316 *
317 * The thermal core only gets the maximum temperature of this one bank,
318 * so the bank concept wouldn't be necessary here. However, the SVS (Smart
319 * Voltage Scaling) unit makes its decisions based on the same bank
320 * data.
321 */
322static const struct mtk_thermal_data mt2712_thermal_data = {
323 .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
324 .num_banks = 1,
325 .num_sensors = MT2712_NUM_SENSORS,
326 .bank_data = {
327 {
328 .num_sensors = 4,
329 .sensors = mt2712_bank_data,
330 },
331 },
332 .msr = mt2712_msr,
333 .adcpnp = mt2712_adcpnp,
334 .sensor_mux_values = mt2712_mux_values,
335};
336
337/**
Sascha Hauera92db1c2015-11-30 12:42:32 +0100338 * raw_to_mcelsius - convert a raw ADC value to mcelsius
339 * @mt: The thermal controller
340 * @raw: raw ADC value
341 *
342 * This converts the raw ADC value to mcelsius using the SoC specific
343 * calibration constants
344 */
345static int raw_to_mcelsius(struct mtk_thermal *mt, int sensno, s32 raw)
346{
347 s32 tmp;
348
349 raw &= 0xfff;
350
351 tmp = 203450520 << 3;
352 tmp /= 165 + mt->o_slope;
353 tmp /= 10000 + mt->adc_ge;
354 tmp *= raw - mt->vts[sensno] - 3350;
355 tmp >>= 3;
356
357 return mt->degc_cali * 500 - tmp;
358}
359
360/**
361 * mtk_thermal_get_bank - get bank
362 * @bank: The bank
363 *
364 * The bank registers are banked, we have to select a bank in the
365 * PTPCORESEL register to access it.
366 */
367static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
368{
369 struct mtk_thermal *mt = bank->mt;
370 u32 val;
371
372 mutex_lock(&mt->lock);
373
374 val = readl(mt->thermal_base + PTPCORESEL);
375 val &= ~0xf;
376 val |= bank->id;
377 writel(val, mt->thermal_base + PTPCORESEL);
378}
379
380/**
381 * mtk_thermal_put_bank - release bank
382 * @bank: The bank
383 *
384 * release a bank previously taken with mtk_thermal_get_bank,
385 */
386static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
387{
388 struct mtk_thermal *mt = bank->mt;
389
390 mutex_unlock(&mt->lock);
391}
392
393/**
394 * mtk_thermal_bank_temperature - get the temperature of a bank
395 * @bank: The bank
396 *
397 * The temperature of a bank is considered the maximum temperature of
398 * the sensors associated to the bank.
399 */
400static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
401{
402 struct mtk_thermal *mt = bank->mt;
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800403 const struct mtk_thermal_data *conf = mt->conf;
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800404 int i, temp = INT_MIN, max = INT_MIN;
Sascha Hauera92db1c2015-11-30 12:42:32 +0100405 u32 raw;
406
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800407 for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
408 raw = readl(mt->thermal_base + conf->msr[i]);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100409
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800410 temp = raw_to_mcelsius(mt,
411 conf->bank_data[bank->id].sensors[i],
412 raw);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100413
414 /*
415 * The first read of a sensor often contains very high bogus
416 * temperature value. Filter these out so that the system does
417 * not immediately shut down.
418 */
419 if (temp > 200000)
420 temp = 0;
421
422 if (temp > max)
423 max = temp;
424 }
425
426 return max;
427}
428
429static int mtk_read_temp(void *data, int *temperature)
430{
431 struct mtk_thermal *mt = data;
432 int i;
433 int tempmax = INT_MIN;
434
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800435 for (i = 0; i < mt->conf->num_banks; i++) {
Sascha Hauera92db1c2015-11-30 12:42:32 +0100436 struct mtk_thermal_bank *bank = &mt->banks[i];
437
438 mtk_thermal_get_bank(bank);
439
440 tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
441
442 mtk_thermal_put_bank(bank);
443 }
444
445 *temperature = tempmax;
446
447 return 0;
448}
449
450static const struct thermal_zone_of_device_ops mtk_thermal_ops = {
451 .get_temp = mtk_read_temp,
452};
453
454static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800455 u32 apmixed_phys_base, u32 auxadc_phys_base)
Sascha Hauera92db1c2015-11-30 12:42:32 +0100456{
457 struct mtk_thermal_bank *bank = &mt->banks[num];
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800458 const struct mtk_thermal_data *conf = mt->conf;
Sascha Hauera92db1c2015-11-30 12:42:32 +0100459 int i;
460
461 bank->id = num;
462 bank->mt = mt;
463
464 mtk_thermal_get_bank(bank);
465
466 /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
467 writel(TEMP_MONCTL1_PERIOD_UNIT(12), mt->thermal_base + TEMP_MONCTL1);
468
469 /*
470 * filt interval is 1 * 46.540us = 46.54us,
471 * sen interval is 429 * 46.540us = 19.96ms
472 */
473 writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
474 TEMP_MONCTL2_SENSOR_INTERVAL(429),
475 mt->thermal_base + TEMP_MONCTL2);
476
477 /* poll is set to 10u */
478 writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800479 mt->thermal_base + TEMP_AHBPOLL);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100480
481 /* temperature sampling control, 1 sample */
482 writel(0x0, mt->thermal_base + TEMP_MSRCTL0);
483
484 /* exceed this polling time, IRQ would be inserted */
485 writel(0xffffffff, mt->thermal_base + TEMP_AHBTO);
486
487 /* number of interrupts per event, 1 is enough */
488 writel(0x0, mt->thermal_base + TEMP_MONIDET0);
489 writel(0x0, mt->thermal_base + TEMP_MONIDET1);
490
491 /*
492 * The MT8173 thermal controller does not have its own ADC. Instead it
493 * uses AHB bus accesses to control the AUXADC. To do this the thermal
494 * controller has to be programmed with the physical addresses of the
495 * AUXADC registers and with the various bit positions in the AUXADC.
496 * Also the thermal controller controls a mux in the APMIXEDSYS register
497 * space.
498 */
499
500 /*
501 * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
502 * automatically by hw
503 */
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800504 writel(BIT(conf->auxadc_channel), mt->thermal_base + TEMP_ADCMUX);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100505
506 /* AHB address for auxadc mux selection */
507 writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800508 mt->thermal_base + TEMP_ADCMUXADDR);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100509
510 /* AHB address for pnp sensor mux selection */
511 writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800512 mt->thermal_base + TEMP_PNPMUXADDR);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100513
514 /* AHB value for auxadc enable */
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800515 writel(BIT(conf->auxadc_channel), mt->thermal_base + TEMP_ADCEN);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100516
517 /* AHB address for auxadc enable (channel 0 immediate mode selected) */
518 writel(auxadc_phys_base + AUXADC_CON1_SET_V,
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800519 mt->thermal_base + TEMP_ADCENADDR);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100520
521 /* AHB address for auxadc valid bit */
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800522 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800523 mt->thermal_base + TEMP_ADCVALIDADDR);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100524
525 /* AHB address for auxadc voltage output */
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800526 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800527 mt->thermal_base + TEMP_ADCVOLTADDR);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100528
529 /* read valid & voltage are at the same register */
530 writel(0x0, mt->thermal_base + TEMP_RDCTRL);
531
532 /* indicate where the valid bit is */
533 writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800534 mt->thermal_base + TEMP_ADCVALIDMASK);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100535
536 /* no shift */
537 writel(0x0, mt->thermal_base + TEMP_ADCVOLTAGESHIFT);
538
539 /* enable auxadc mux write transaction */
540 writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800541 mt->thermal_base + TEMP_ADCWRITECTRL);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100542
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800543 for (i = 0; i < conf->bank_data[num].num_sensors; i++)
544 writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
545 mt->thermal_base + conf->adcpnp[i]);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100546
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800547 writel((1 << conf->bank_data[num].num_sensors) - 1,
548 mt->thermal_base + TEMP_MONCTL0);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100549
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800550 writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
551 TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
552 mt->thermal_base + TEMP_ADCWRITECTRL);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100553
554 mtk_thermal_put_bank(bank);
555}
556
557static u64 of_get_phys_base(struct device_node *np)
558{
559 u64 size64;
560 const __be32 *regaddr_p;
561
562 regaddr_p = of_get_address(np, 0, &size64, NULL);
563 if (!regaddr_p)
564 return OF_BAD_ADDR;
565
566 return of_translate_address(np, regaddr_p);
567}
568
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800569static int mtk_thermal_get_calibration_data(struct device *dev,
570 struct mtk_thermal *mt)
Sascha Hauera92db1c2015-11-30 12:42:32 +0100571{
572 struct nvmem_cell *cell;
573 u32 *buf;
574 size_t len;
575 int i, ret = 0;
576
577 /* Start with default values */
578 mt->adc_ge = 512;
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800579 for (i = 0; i < mt->conf->num_sensors; i++)
Sascha Hauera92db1c2015-11-30 12:42:32 +0100580 mt->vts[i] = 260;
581 mt->degc_cali = 40;
582 mt->o_slope = 0;
583
584 cell = nvmem_cell_get(dev, "calibration-data");
585 if (IS_ERR(cell)) {
586 if (PTR_ERR(cell) == -EPROBE_DEFER)
587 return PTR_ERR(cell);
588 return 0;
589 }
590
591 buf = (u32 *)nvmem_cell_read(cell, &len);
592
593 nvmem_cell_put(cell);
594
595 if (IS_ERR(buf))
596 return PTR_ERR(buf);
597
598 if (len < 3 * sizeof(u32)) {
599 dev_warn(dev, "invalid calibration data\n");
600 ret = -EINVAL;
601 goto out;
602 }
603
604 if (buf[0] & MT8173_CALIB_BUF0_VALID) {
605 mt->adc_ge = MT8173_CALIB_BUF1_ADC_GE(buf[1]);
606 mt->vts[MT8173_TS1] = MT8173_CALIB_BUF0_VTS_TS1(buf[0]);
607 mt->vts[MT8173_TS2] = MT8173_CALIB_BUF0_VTS_TS2(buf[0]);
608 mt->vts[MT8173_TS3] = MT8173_CALIB_BUF1_VTS_TS3(buf[1]);
609 mt->vts[MT8173_TS4] = MT8173_CALIB_BUF2_VTS_TS4(buf[2]);
610 mt->vts[MT8173_TSABB] = MT8173_CALIB_BUF2_VTS_TSABB(buf[2]);
611 mt->degc_cali = MT8173_CALIB_BUF0_DEGC_CALI(buf[0]);
612 mt->o_slope = MT8173_CALIB_BUF0_O_SLOPE(buf[0]);
613 } else {
614 dev_info(dev, "Device not calibrated, using default calibration values\n");
615 }
616
617out:
618 kfree(buf);
619
620 return ret;
621}
622
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800623static const struct of_device_id mtk_thermal_of_match[] = {
624 {
625 .compatible = "mediatek,mt8173-thermal",
626 .data = (void *)&mt8173_thermal_data,
627 },
628 {
629 .compatible = "mediatek,mt2701-thermal",
630 .data = (void *)&mt2701_thermal_data,
Louis Yu6cf7f002017-08-01 15:28:31 +0800631 },
632 {
633 .compatible = "mediatek,mt2712-thermal",
634 .data = (void *)&mt2712_thermal_data,
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800635 }, {
636 },
637};
638MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
639
Sascha Hauera92db1c2015-11-30 12:42:32 +0100640static int mtk_thermal_probe(struct platform_device *pdev)
641{
642 int ret, i;
643 struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
644 struct mtk_thermal *mt;
645 struct resource *res;
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800646 const struct of_device_id *of_id;
Sascha Hauera92db1c2015-11-30 12:42:32 +0100647 u64 auxadc_phys_base, apmixed_phys_base;
Axel Lin1f6b0882016-09-07 17:24:52 +0800648 struct thermal_zone_device *tzdev;
Sascha Hauera92db1c2015-11-30 12:42:32 +0100649
650 mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
651 if (!mt)
652 return -ENOMEM;
653
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800654 of_id = of_match_device(mtk_thermal_of_match, &pdev->dev);
655 if (of_id)
656 mt->conf = (const struct mtk_thermal_data *)of_id->data;
657
Sascha Hauera92db1c2015-11-30 12:42:32 +0100658 mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
659 if (IS_ERR(mt->clk_peri_therm))
660 return PTR_ERR(mt->clk_peri_therm);
661
662 mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
663 if (IS_ERR(mt->clk_auxadc))
664 return PTR_ERR(mt->clk_auxadc);
665
666 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
667 mt->thermal_base = devm_ioremap_resource(&pdev->dev, res);
668 if (IS_ERR(mt->thermal_base))
669 return PTR_ERR(mt->thermal_base);
670
671 ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
672 if (ret)
673 return ret;
674
675 mutex_init(&mt->lock);
676
677 mt->dev = &pdev->dev;
678
679 auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
680 if (!auxadc) {
681 dev_err(&pdev->dev, "missing auxadc node\n");
682 return -ENODEV;
683 }
684
685 auxadc_phys_base = of_get_phys_base(auxadc);
686
687 of_node_put(auxadc);
688
689 if (auxadc_phys_base == OF_BAD_ADDR) {
690 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
691 return -EINVAL;
692 }
693
694 apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
695 if (!apmixedsys) {
696 dev_err(&pdev->dev, "missing apmixedsys node\n");
697 return -ENODEV;
698 }
699
700 apmixed_phys_base = of_get_phys_base(apmixedsys);
701
702 of_node_put(apmixedsys);
703
704 if (apmixed_phys_base == OF_BAD_ADDR) {
705 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
706 return -EINVAL;
707 }
708
709 ret = clk_prepare_enable(mt->clk_auxadc);
710 if (ret) {
711 dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
712 return ret;
713 }
714
715 ret = device_reset(&pdev->dev);
716 if (ret)
717 goto err_disable_clk_auxadc;
718
719 ret = clk_prepare_enable(mt->clk_peri_therm);
720 if (ret) {
721 dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
722 goto err_disable_clk_auxadc;
723 }
724
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800725 for (i = 0; i < mt->conf->num_banks; i++)
Eduardo Valentineb4fc332016-02-18 07:43:57 -0800726 mtk_thermal_init_bank(mt, i, apmixed_phys_base,
727 auxadc_phys_base);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100728
729 platform_set_drvdata(pdev, mt);
730
Axel Lin1f6b0882016-09-07 17:24:52 +0800731 tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mt,
732 &mtk_thermal_ops);
733 if (IS_ERR(tzdev)) {
734 ret = PTR_ERR(tzdev);
735 goto err_disable_clk_peri_therm;
736 }
Sascha Hauera92db1c2015-11-30 12:42:32 +0100737
738 return 0;
739
Axel Lin1f6b0882016-09-07 17:24:52 +0800740err_disable_clk_peri_therm:
741 clk_disable_unprepare(mt->clk_peri_therm);
Sascha Hauera92db1c2015-11-30 12:42:32 +0100742err_disable_clk_auxadc:
743 clk_disable_unprepare(mt->clk_auxadc);
744
745 return ret;
746}
747
748static int mtk_thermal_remove(struct platform_device *pdev)
749{
750 struct mtk_thermal *mt = platform_get_drvdata(pdev);
751
Sascha Hauera92db1c2015-11-30 12:42:32 +0100752 clk_disable_unprepare(mt->clk_peri_therm);
753 clk_disable_unprepare(mt->clk_auxadc);
754
755 return 0;
756}
757
Sascha Hauera92db1c2015-11-30 12:42:32 +0100758static struct platform_driver mtk_thermal_driver = {
759 .probe = mtk_thermal_probe,
760 .remove = mtk_thermal_remove,
761 .driver = {
762 .name = THERMAL_NAME,
763 .of_match_table = mtk_thermal_of_match,
764 },
765};
766
767module_platform_driver(mtk_thermal_driver);
768
Louis Yu6cf7f002017-08-01 15:28:31 +0800769MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>");
dawei.chien@mediatek.comb7cf0052016-08-18 11:50:52 +0800770MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>");
Randy Dunlap9ebfb4e2016-04-19 16:45:01 -0700771MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Sascha Hauera92db1c2015-11-30 12:42:32 +0100772MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>");
773MODULE_DESCRIPTION("Mediatek thermal driver");
774MODULE_LICENSE("GPL v2");