0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/clk.h>
0011 #include <linux/delay.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/nvmem-consumer.h>
0016 #include <linux/of.h>
0017 #include <linux/of_address.h>
0018 #include <linux/of_device.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/slab.h>
0021 #include <linux/io.h>
0022 #include <linux/thermal.h>
0023 #include <linux/reset.h>
0024 #include <linux/types.h>
0025
0026 #include "thermal_hwmon.h"
0027
0028
0029 #define AUXADC_CON1_SET_V 0x008
0030 #define AUXADC_CON1_CLR_V 0x00c
0031 #define AUXADC_CON2_V 0x010
0032 #define AUXADC_DATA(channel) (0x14 + (channel) * 4)
0033
0034 #define APMIXED_SYS_TS_CON1 0x604
0035
0036
0037 #define TEMP_MONCTL0 0x000
0038 #define TEMP_MONCTL1 0x004
0039 #define TEMP_MONCTL2 0x008
0040 #define TEMP_MONIDET0 0x014
0041 #define TEMP_MONIDET1 0x018
0042 #define TEMP_MSRCTL0 0x038
0043 #define TEMP_MSRCTL1 0x03c
0044 #define TEMP_AHBPOLL 0x040
0045 #define TEMP_AHBTO 0x044
0046 #define TEMP_ADCPNP0 0x048
0047 #define TEMP_ADCPNP1 0x04c
0048 #define TEMP_ADCPNP2 0x050
0049 #define TEMP_ADCPNP3 0x0b4
0050
0051 #define TEMP_ADCMUX 0x054
0052 #define TEMP_ADCEN 0x060
0053 #define TEMP_PNPMUXADDR 0x064
0054 #define TEMP_ADCMUXADDR 0x068
0055 #define TEMP_ADCENADDR 0x074
0056 #define TEMP_ADCVALIDADDR 0x078
0057 #define TEMP_ADCVOLTADDR 0x07c
0058 #define TEMP_RDCTRL 0x080
0059 #define TEMP_ADCVALIDMASK 0x084
0060 #define TEMP_ADCVOLTAGESHIFT 0x088
0061 #define TEMP_ADCWRITECTRL 0x08c
0062 #define TEMP_MSR0 0x090
0063 #define TEMP_MSR1 0x094
0064 #define TEMP_MSR2 0x098
0065 #define TEMP_MSR3 0x0B8
0066
0067 #define TEMP_SPARE0 0x0f0
0068
0069 #define TEMP_ADCPNP0_1 0x148
0070 #define TEMP_ADCPNP1_1 0x14c
0071 #define TEMP_ADCPNP2_1 0x150
0072 #define TEMP_MSR0_1 0x190
0073 #define TEMP_MSR1_1 0x194
0074 #define TEMP_MSR2_1 0x198
0075 #define TEMP_ADCPNP3_1 0x1b4
0076 #define TEMP_MSR3_1 0x1B8
0077
0078 #define PTPCORESEL 0x400
0079
0080 #define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff)
0081
0082 #define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
0083 #define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
0084
0085 #define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x) (x)
0086
0087 #define TEMP_ADCWRITECTRL_ADC_PNP_WRITE BIT(0)
0088 #define TEMP_ADCWRITECTRL_ADC_MUX_WRITE BIT(1)
0089
0090 #define TEMP_ADCVALIDMASK_VALID_HIGH BIT(5)
0091 #define TEMP_ADCVALIDMASK_VALID_POS(bit) (bit)
0092
0093
0094 #define MT8173_TS1 0
0095 #define MT8173_TS2 1
0096 #define MT8173_TS3 2
0097 #define MT8173_TS4 3
0098 #define MT8173_TSABB 4
0099
0100
0101 #define MT8173_TEMP_AUXADC_CHANNEL 11
0102
0103
0104 #define MT8173_NUM_SENSORS 5
0105
0106
0107 #define MT8173_NUM_ZONES 4
0108
0109
0110 #define MT8173_NUM_SENSORS_PER_ZONE 4
0111
0112
0113 #define MT8173_NUM_CONTROLLER 1
0114
0115
0116 #define MT8173_CALIBRATION 165
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126 #define CALIB_BUF0_VALID_V1 BIT(0)
0127 #define CALIB_BUF1_ADC_GE_V1(x) (((x) >> 22) & 0x3ff)
0128 #define CALIB_BUF0_VTS_TS1_V1(x) (((x) >> 17) & 0x1ff)
0129 #define CALIB_BUF0_VTS_TS2_V1(x) (((x) >> 8) & 0x1ff)
0130 #define CALIB_BUF1_VTS_TS3_V1(x) (((x) >> 0) & 0x1ff)
0131 #define CALIB_BUF2_VTS_TS4_V1(x) (((x) >> 23) & 0x1ff)
0132 #define CALIB_BUF2_VTS_TS5_V1(x) (((x) >> 5) & 0x1ff)
0133 #define CALIB_BUF2_VTS_TSABB_V1(x) (((x) >> 14) & 0x1ff)
0134 #define CALIB_BUF0_DEGC_CALI_V1(x) (((x) >> 1) & 0x3f)
0135 #define CALIB_BUF0_O_SLOPE_V1(x) (((x) >> 26) & 0x3f)
0136 #define CALIB_BUF0_O_SLOPE_SIGN_V1(x) (((x) >> 7) & 0x1)
0137 #define CALIB_BUF1_ID_V1(x) (((x) >> 9) & 0x1)
0138
0139
0140
0141
0142
0143 #define CALIB_BUF0_ADC_OE_V2(x) (((x) >> 22) & 0x3ff)
0144 #define CALIB_BUF0_ADC_GE_V2(x) (((x) >> 12) & 0x3ff)
0145 #define CALIB_BUF0_DEGC_CALI_V2(x) (((x) >> 6) & 0x3f)
0146 #define CALIB_BUF0_O_SLOPE_V2(x) (((x) >> 0) & 0x3f)
0147 #define CALIB_BUF1_VTS_TS1_V2(x) (((x) >> 23) & 0x1ff)
0148 #define CALIB_BUF1_VTS_TS2_V2(x) (((x) >> 14) & 0x1ff)
0149 #define CALIB_BUF1_VTS_TSABB_V2(x) (((x) >> 5) & 0x1ff)
0150 #define CALIB_BUF1_VALID_V2(x) (((x) >> 4) & 0x1)
0151 #define CALIB_BUF1_O_SLOPE_SIGN_V2(x) (((x) >> 3) & 0x1)
0152
0153 enum {
0154 VTS1,
0155 VTS2,
0156 VTS3,
0157 VTS4,
0158 VTS5,
0159 VTSABB,
0160 MAX_NUM_VTS,
0161 };
0162
0163 enum mtk_thermal_version {
0164 MTK_THERMAL_V1 = 1,
0165 MTK_THERMAL_V2,
0166 };
0167
0168
0169 #define MT2701_TS1 0
0170 #define MT2701_TS2 1
0171 #define MT2701_TSABB 2
0172
0173
0174 #define MT2701_TEMP_AUXADC_CHANNEL 11
0175
0176
0177 #define MT2701_NUM_SENSORS 3
0178
0179
0180 #define MT2701_NUM_SENSORS_PER_ZONE 3
0181
0182
0183 #define MT2701_NUM_CONTROLLER 1
0184
0185
0186 #define MT2701_CALIBRATION 165
0187
0188
0189 #define MT2712_TS1 0
0190 #define MT2712_TS2 1
0191 #define MT2712_TS3 2
0192 #define MT2712_TS4 3
0193
0194
0195 #define MT2712_TEMP_AUXADC_CHANNEL 11
0196
0197
0198 #define MT2712_NUM_SENSORS 4
0199
0200
0201 #define MT2712_NUM_SENSORS_PER_ZONE 4
0202
0203
0204 #define MT2712_NUM_CONTROLLER 1
0205
0206
0207 #define MT2712_CALIBRATION 165
0208
0209 #define MT7622_TEMP_AUXADC_CHANNEL 11
0210 #define MT7622_NUM_SENSORS 1
0211 #define MT7622_NUM_ZONES 1
0212 #define MT7622_NUM_SENSORS_PER_ZONE 1
0213 #define MT7622_TS1 0
0214 #define MT7622_NUM_CONTROLLER 1
0215
0216
0217 #define MAX_NUM_ZONES 8
0218
0219
0220 #define MT7622_CALIBRATION 165
0221
0222
0223 #define MT8183_TS1 0
0224 #define MT8183_TS2 1
0225 #define MT8183_TS3 2
0226 #define MT8183_TS4 3
0227 #define MT8183_TS5 4
0228 #define MT8183_TSABB 5
0229
0230
0231 #define MT8183_TEMP_AUXADC_CHANNEL 11
0232
0233
0234 #define MT8183_NUM_SENSORS 6
0235
0236
0237 #define MT8183_NUM_ZONES 1
0238
0239
0240 #define MT8183_NUM_SENSORS_PER_ZONE 6
0241
0242
0243 #define MT8183_NUM_CONTROLLER 2
0244
0245
0246 #define MT8183_CALIBRATION 153
0247
0248 struct mtk_thermal;
0249
0250 struct thermal_bank_cfg {
0251 unsigned int num_sensors;
0252 const int *sensors;
0253 };
0254
0255 struct mtk_thermal_bank {
0256 struct mtk_thermal *mt;
0257 int id;
0258 };
0259
0260 struct mtk_thermal_data {
0261 s32 num_banks;
0262 s32 num_sensors;
0263 s32 auxadc_channel;
0264 const int *vts_index;
0265 const int *sensor_mux_values;
0266 const int *msr;
0267 const int *adcpnp;
0268 const int cali_val;
0269 const int num_controller;
0270 const int *controller_offset;
0271 bool need_switch_bank;
0272 struct thermal_bank_cfg bank_data[MAX_NUM_ZONES];
0273 enum mtk_thermal_version version;
0274 };
0275
0276 struct mtk_thermal {
0277 struct device *dev;
0278 void __iomem *thermal_base;
0279
0280 struct clk *clk_peri_therm;
0281 struct clk *clk_auxadc;
0282
0283 struct mutex lock;
0284
0285
0286 s32 adc_ge;
0287 s32 adc_oe;
0288 s32 degc_cali;
0289 s32 o_slope;
0290 s32 o_slope_sign;
0291 s32 vts[MAX_NUM_VTS];
0292
0293 const struct mtk_thermal_data *conf;
0294 struct mtk_thermal_bank banks[MAX_NUM_ZONES];
0295 };
0296
0297
0298 static const int mt8183_bank_data[MT8183_NUM_SENSORS] = {
0299 MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB
0300 };
0301
0302 static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = {
0303 TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1
0304 };
0305
0306 static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = {
0307 TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1,
0308 TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1
0309 };
0310
0311 static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 };
0312 static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100};
0313
0314 static const int mt8183_vts_index[MT8183_NUM_SENSORS] = {
0315 VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB
0316 };
0317
0318
0319 static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
0320 { MT8173_TS2, MT8173_TS3 },
0321 { MT8173_TS2, MT8173_TS4 },
0322 { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
0323 { MT8173_TS2 },
0324 };
0325
0326 static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
0327 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
0328 };
0329
0330 static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
0331 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
0332 };
0333
0334 static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
0335 static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, };
0336
0337 static const int mt8173_vts_index[MT8173_NUM_SENSORS] = {
0338 VTS1, VTS2, VTS3, VTS4, VTSABB
0339 };
0340
0341
0342 static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
0343 MT2701_TS1, MT2701_TS2, MT2701_TSABB
0344 };
0345
0346 static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
0347 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
0348 };
0349
0350 static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
0351 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
0352 };
0353
0354 static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
0355 static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, };
0356
0357 static const int mt2701_vts_index[MT2701_NUM_SENSORS] = {
0358 VTS1, VTS2, VTS3
0359 };
0360
0361
0362 static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
0363 MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
0364 };
0365
0366 static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
0367 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
0368 };
0369
0370 static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
0371 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
0372 };
0373
0374 static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
0375 static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, };
0376
0377 static const int mt2712_vts_index[MT2712_NUM_SENSORS] = {
0378 VTS1, VTS2, VTS3, VTS4
0379 };
0380
0381
0382 static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
0383 static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
0384 static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
0385 static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
0386 static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
0387 static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402 static const struct mtk_thermal_data mt8173_thermal_data = {
0403 .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
0404 .num_banks = MT8173_NUM_ZONES,
0405 .num_sensors = MT8173_NUM_SENSORS,
0406 .vts_index = mt8173_vts_index,
0407 .cali_val = MT8173_CALIBRATION,
0408 .num_controller = MT8173_NUM_CONTROLLER,
0409 .controller_offset = mt8173_tc_offset,
0410 .need_switch_bank = true,
0411 .bank_data = {
0412 {
0413 .num_sensors = 2,
0414 .sensors = mt8173_bank_data[0],
0415 }, {
0416 .num_sensors = 2,
0417 .sensors = mt8173_bank_data[1],
0418 }, {
0419 .num_sensors = 3,
0420 .sensors = mt8173_bank_data[2],
0421 }, {
0422 .num_sensors = 1,
0423 .sensors = mt8173_bank_data[3],
0424 },
0425 },
0426 .msr = mt8173_msr,
0427 .adcpnp = mt8173_adcpnp,
0428 .sensor_mux_values = mt8173_mux_values,
0429 .version = MTK_THERMAL_V1,
0430 };
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442 static const struct mtk_thermal_data mt2701_thermal_data = {
0443 .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
0444 .num_banks = 1,
0445 .num_sensors = MT2701_NUM_SENSORS,
0446 .vts_index = mt2701_vts_index,
0447 .cali_val = MT2701_CALIBRATION,
0448 .num_controller = MT2701_NUM_CONTROLLER,
0449 .controller_offset = mt2701_tc_offset,
0450 .need_switch_bank = true,
0451 .bank_data = {
0452 {
0453 .num_sensors = 3,
0454 .sensors = mt2701_bank_data,
0455 },
0456 },
0457 .msr = mt2701_msr,
0458 .adcpnp = mt2701_adcpnp,
0459 .sensor_mux_values = mt2701_mux_values,
0460 .version = MTK_THERMAL_V1,
0461 };
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473 static const struct mtk_thermal_data mt2712_thermal_data = {
0474 .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
0475 .num_banks = 1,
0476 .num_sensors = MT2712_NUM_SENSORS,
0477 .vts_index = mt2712_vts_index,
0478 .cali_val = MT2712_CALIBRATION,
0479 .num_controller = MT2712_NUM_CONTROLLER,
0480 .controller_offset = mt2712_tc_offset,
0481 .need_switch_bank = true,
0482 .bank_data = {
0483 {
0484 .num_sensors = 4,
0485 .sensors = mt2712_bank_data,
0486 },
0487 },
0488 .msr = mt2712_msr,
0489 .adcpnp = mt2712_adcpnp,
0490 .sensor_mux_values = mt2712_mux_values,
0491 .version = MTK_THERMAL_V1,
0492 };
0493
0494
0495
0496
0497
0498 static const struct mtk_thermal_data mt7622_thermal_data = {
0499 .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
0500 .num_banks = MT7622_NUM_ZONES,
0501 .num_sensors = MT7622_NUM_SENSORS,
0502 .vts_index = mt7622_vts_index,
0503 .cali_val = MT7622_CALIBRATION,
0504 .num_controller = MT7622_NUM_CONTROLLER,
0505 .controller_offset = mt7622_tc_offset,
0506 .need_switch_bank = true,
0507 .bank_data = {
0508 {
0509 .num_sensors = 1,
0510 .sensors = mt7622_bank_data,
0511 },
0512 },
0513 .msr = mt7622_msr,
0514 .adcpnp = mt7622_adcpnp,
0515 .sensor_mux_values = mt7622_mux_values,
0516 .version = MTK_THERMAL_V2,
0517 };
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530 static const struct mtk_thermal_data mt8183_thermal_data = {
0531 .auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL,
0532 .num_banks = MT8183_NUM_ZONES,
0533 .num_sensors = MT8183_NUM_SENSORS,
0534 .vts_index = mt8183_vts_index,
0535 .cali_val = MT8183_CALIBRATION,
0536 .num_controller = MT8183_NUM_CONTROLLER,
0537 .controller_offset = mt8183_tc_offset,
0538 .need_switch_bank = false,
0539 .bank_data = {
0540 {
0541 .num_sensors = 6,
0542 .sensors = mt8183_bank_data,
0543 },
0544 },
0545
0546 .msr = mt8183_msr,
0547 .adcpnp = mt8183_adcpnp,
0548 .sensor_mux_values = mt8183_mux_values,
0549 .version = MTK_THERMAL_V1,
0550 };
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561 static int raw_to_mcelsius_v1(struct mtk_thermal *mt, int sensno, s32 raw)
0562 {
0563 s32 tmp;
0564
0565 raw &= 0xfff;
0566
0567 tmp = 203450520 << 3;
0568 tmp /= mt->conf->cali_val + mt->o_slope;
0569 tmp /= 10000 + mt->adc_ge;
0570 tmp *= raw - mt->vts[sensno] - 3350;
0571 tmp >>= 3;
0572
0573 return mt->degc_cali * 500 - tmp;
0574 }
0575
0576 static int raw_to_mcelsius_v2(struct mtk_thermal *mt, int sensno, s32 raw)
0577 {
0578 s32 format_1;
0579 s32 format_2;
0580 s32 g_oe;
0581 s32 g_gain;
0582 s32 g_x_roomt;
0583 s32 tmp;
0584
0585 if (raw == 0)
0586 return 0;
0587
0588 raw &= 0xfff;
0589 g_gain = 10000 + (((mt->adc_ge - 512) * 10000) >> 12);
0590 g_oe = mt->adc_oe - 512;
0591 format_1 = mt->vts[VTS2] + 3105 - g_oe;
0592 format_2 = (mt->degc_cali * 10) >> 1;
0593 g_x_roomt = (((format_1 * 10000) >> 12) * 10000) / g_gain;
0594
0595 tmp = (((((raw - g_oe) * 10000) >> 12) * 10000) / g_gain) - g_x_roomt;
0596 tmp = tmp * 10 * 100 / 11;
0597
0598 if (mt->o_slope_sign == 0)
0599 tmp = tmp / (165 - mt->o_slope);
0600 else
0601 tmp = tmp / (165 + mt->o_slope);
0602
0603 return (format_2 - tmp) * 100;
0604 }
0605
0606
0607
0608
0609
0610
0611
0612
0613 static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
0614 {
0615 struct mtk_thermal *mt = bank->mt;
0616 u32 val;
0617
0618 if (mt->conf->need_switch_bank) {
0619 mutex_lock(&mt->lock);
0620
0621 val = readl(mt->thermal_base + PTPCORESEL);
0622 val &= ~0xf;
0623 val |= bank->id;
0624 writel(val, mt->thermal_base + PTPCORESEL);
0625 }
0626 }
0627
0628
0629
0630
0631
0632
0633
0634 static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
0635 {
0636 struct mtk_thermal *mt = bank->mt;
0637
0638 if (mt->conf->need_switch_bank)
0639 mutex_unlock(&mt->lock);
0640 }
0641
0642
0643
0644
0645
0646
0647
0648
0649 static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
0650 {
0651 struct mtk_thermal *mt = bank->mt;
0652 const struct mtk_thermal_data *conf = mt->conf;
0653 int i, temp = INT_MIN, max = INT_MIN;
0654 u32 raw;
0655
0656 for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
0657 raw = readl(mt->thermal_base + conf->msr[i]);
0658
0659 if (mt->conf->version == MTK_THERMAL_V1) {
0660 temp = raw_to_mcelsius_v1(
0661 mt, conf->bank_data[bank->id].sensors[i], raw);
0662 } else {
0663 temp = raw_to_mcelsius_v2(
0664 mt, conf->bank_data[bank->id].sensors[i], raw);
0665 }
0666
0667
0668
0669
0670
0671
0672 if (temp > 200000)
0673 temp = 0;
0674
0675 if (temp > max)
0676 max = temp;
0677 }
0678
0679 return max;
0680 }
0681
0682 static int mtk_read_temp(void *data, int *temperature)
0683 {
0684 struct mtk_thermal *mt = data;
0685 int i;
0686 int tempmax = INT_MIN;
0687
0688 for (i = 0; i < mt->conf->num_banks; i++) {
0689 struct mtk_thermal_bank *bank = &mt->banks[i];
0690
0691 mtk_thermal_get_bank(bank);
0692
0693 tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
0694
0695 mtk_thermal_put_bank(bank);
0696 }
0697
0698 *temperature = tempmax;
0699
0700 return 0;
0701 }
0702
0703 static const struct thermal_zone_of_device_ops mtk_thermal_ops = {
0704 .get_temp = mtk_read_temp,
0705 };
0706
0707 static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
0708 u32 apmixed_phys_base, u32 auxadc_phys_base,
0709 int ctrl_id)
0710 {
0711 struct mtk_thermal_bank *bank = &mt->banks[num];
0712 const struct mtk_thermal_data *conf = mt->conf;
0713 int i;
0714
0715 int offset = mt->conf->controller_offset[ctrl_id];
0716 void __iomem *controller_base = mt->thermal_base + offset;
0717
0718 bank->id = num;
0719 bank->mt = mt;
0720
0721 mtk_thermal_get_bank(bank);
0722
0723
0724 writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
0725
0726
0727
0728
0729
0730 writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
0731 TEMP_MONCTL2_SENSOR_INTERVAL(429),
0732 controller_base + TEMP_MONCTL2);
0733
0734
0735 writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
0736 controller_base + TEMP_AHBPOLL);
0737
0738
0739 writel(0x0, controller_base + TEMP_MSRCTL0);
0740
0741
0742 writel(0xffffffff, controller_base + TEMP_AHBTO);
0743
0744
0745 writel(0x0, controller_base + TEMP_MONIDET0);
0746 writel(0x0, controller_base + TEMP_MONIDET1);
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
0762
0763
0764 writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
0765 controller_base + TEMP_ADCMUXADDR);
0766
0767 if (mt->conf->version == MTK_THERMAL_V1) {
0768
0769 writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
0770 controller_base + TEMP_PNPMUXADDR);
0771 }
0772
0773
0774 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
0775
0776
0777 writel(auxadc_phys_base + AUXADC_CON1_SET_V,
0778 controller_base + TEMP_ADCENADDR);
0779
0780
0781 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
0782 controller_base + TEMP_ADCVALIDADDR);
0783
0784
0785 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
0786 controller_base + TEMP_ADCVOLTADDR);
0787
0788
0789 writel(0x0, controller_base + TEMP_RDCTRL);
0790
0791
0792 writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
0793 controller_base + TEMP_ADCVALIDMASK);
0794
0795
0796 writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
0797
0798
0799 writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
0800 controller_base + TEMP_ADCWRITECTRL);
0801
0802 for (i = 0; i < conf->bank_data[num].num_sensors; i++)
0803 writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
0804 mt->thermal_base + conf->adcpnp[i]);
0805
0806 writel((1 << conf->bank_data[num].num_sensors) - 1,
0807 controller_base + TEMP_MONCTL0);
0808
0809 writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
0810 TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
0811 controller_base + TEMP_ADCWRITECTRL);
0812
0813 mtk_thermal_put_bank(bank);
0814 }
0815
0816 static u64 of_get_phys_base(struct device_node *np)
0817 {
0818 u64 size64;
0819 const __be32 *regaddr_p;
0820
0821 regaddr_p = of_get_address(np, 0, &size64, NULL);
0822 if (!regaddr_p)
0823 return OF_BAD_ADDR;
0824
0825 return of_translate_address(np, regaddr_p);
0826 }
0827
0828 static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf)
0829 {
0830 int i;
0831
0832 if (!(buf[0] & CALIB_BUF0_VALID_V1))
0833 return -EINVAL;
0834
0835 mt->adc_ge = CALIB_BUF1_ADC_GE_V1(buf[1]);
0836
0837 for (i = 0; i < mt->conf->num_sensors; i++) {
0838 switch (mt->conf->vts_index[i]) {
0839 case VTS1:
0840 mt->vts[VTS1] = CALIB_BUF0_VTS_TS1_V1(buf[0]);
0841 break;
0842 case VTS2:
0843 mt->vts[VTS2] = CALIB_BUF0_VTS_TS2_V1(buf[0]);
0844 break;
0845 case VTS3:
0846 mt->vts[VTS3] = CALIB_BUF1_VTS_TS3_V1(buf[1]);
0847 break;
0848 case VTS4:
0849 mt->vts[VTS4] = CALIB_BUF2_VTS_TS4_V1(buf[2]);
0850 break;
0851 case VTS5:
0852 mt->vts[VTS5] = CALIB_BUF2_VTS_TS5_V1(buf[2]);
0853 break;
0854 case VTSABB:
0855 mt->vts[VTSABB] =
0856 CALIB_BUF2_VTS_TSABB_V1(buf[2]);
0857 break;
0858 default:
0859 break;
0860 }
0861 }
0862
0863 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V1(buf[0]);
0864 if (CALIB_BUF1_ID_V1(buf[1]) &
0865 CALIB_BUF0_O_SLOPE_SIGN_V1(buf[0]))
0866 mt->o_slope = -CALIB_BUF0_O_SLOPE_V1(buf[0]);
0867 else
0868 mt->o_slope = CALIB_BUF0_O_SLOPE_V1(buf[0]);
0869
0870 return 0;
0871 }
0872
0873 static int mtk_thermal_extract_efuse_v2(struct mtk_thermal *mt, u32 *buf)
0874 {
0875 if (!CALIB_BUF1_VALID_V2(buf[1]))
0876 return -EINVAL;
0877
0878 mt->adc_oe = CALIB_BUF0_ADC_OE_V2(buf[0]);
0879 mt->adc_ge = CALIB_BUF0_ADC_GE_V2(buf[0]);
0880 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V2(buf[0]);
0881 mt->o_slope = CALIB_BUF0_O_SLOPE_V2(buf[0]);
0882 mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V2(buf[1]);
0883 mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V2(buf[1]);
0884 mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V2(buf[1]);
0885 mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2(buf[1]);
0886
0887 return 0;
0888 }
0889
0890 static int mtk_thermal_get_calibration_data(struct device *dev,
0891 struct mtk_thermal *mt)
0892 {
0893 struct nvmem_cell *cell;
0894 u32 *buf;
0895 size_t len;
0896 int i, ret = 0;
0897
0898
0899 mt->adc_ge = 512;
0900 for (i = 0; i < mt->conf->num_sensors; i++)
0901 mt->vts[i] = 260;
0902 mt->degc_cali = 40;
0903 mt->o_slope = 0;
0904
0905 cell = nvmem_cell_get(dev, "calibration-data");
0906 if (IS_ERR(cell)) {
0907 if (PTR_ERR(cell) == -EPROBE_DEFER)
0908 return PTR_ERR(cell);
0909 return 0;
0910 }
0911
0912 buf = (u32 *)nvmem_cell_read(cell, &len);
0913
0914 nvmem_cell_put(cell);
0915
0916 if (IS_ERR(buf))
0917 return PTR_ERR(buf);
0918
0919 if (len < 3 * sizeof(u32)) {
0920 dev_warn(dev, "invalid calibration data\n");
0921 ret = -EINVAL;
0922 goto out;
0923 }
0924
0925 if (mt->conf->version == MTK_THERMAL_V1)
0926 ret = mtk_thermal_extract_efuse_v1(mt, buf);
0927 else
0928 ret = mtk_thermal_extract_efuse_v2(mt, buf);
0929
0930 if (ret) {
0931 dev_info(dev, "Device not calibrated, using default calibration values\n");
0932 ret = 0;
0933 }
0934
0935 out:
0936 kfree(buf);
0937
0938 return ret;
0939 }
0940
0941 static const struct of_device_id mtk_thermal_of_match[] = {
0942 {
0943 .compatible = "mediatek,mt8173-thermal",
0944 .data = (void *)&mt8173_thermal_data,
0945 },
0946 {
0947 .compatible = "mediatek,mt2701-thermal",
0948 .data = (void *)&mt2701_thermal_data,
0949 },
0950 {
0951 .compatible = "mediatek,mt2712-thermal",
0952 .data = (void *)&mt2712_thermal_data,
0953 },
0954 {
0955 .compatible = "mediatek,mt7622-thermal",
0956 .data = (void *)&mt7622_thermal_data,
0957 },
0958 {
0959 .compatible = "mediatek,mt8183-thermal",
0960 .data = (void *)&mt8183_thermal_data,
0961 }, {
0962 },
0963 };
0964 MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
0965
0966 static void mtk_thermal_turn_on_buffer(void __iomem *apmixed_base)
0967 {
0968 int tmp;
0969
0970 tmp = readl(apmixed_base + APMIXED_SYS_TS_CON1);
0971 tmp &= ~(0x37);
0972 tmp |= 0x1;
0973 writel(tmp, apmixed_base + APMIXED_SYS_TS_CON1);
0974 udelay(200);
0975 }
0976
0977 static void mtk_thermal_release_periodic_ts(struct mtk_thermal *mt,
0978 void __iomem *auxadc_base)
0979 {
0980 int tmp;
0981
0982 writel(0x800, auxadc_base + AUXADC_CON1_SET_V);
0983 writel(0x1, mt->thermal_base + TEMP_MONCTL0);
0984 tmp = readl(mt->thermal_base + TEMP_MSRCTL1);
0985 writel((tmp & (~0x10e)), mt->thermal_base + TEMP_MSRCTL1);
0986 }
0987
0988 static int mtk_thermal_probe(struct platform_device *pdev)
0989 {
0990 int ret, i, ctrl_id;
0991 struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
0992 struct mtk_thermal *mt;
0993 struct resource *res;
0994 u64 auxadc_phys_base, apmixed_phys_base;
0995 struct thermal_zone_device *tzdev;
0996 void __iomem *apmixed_base, *auxadc_base;
0997
0998 mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
0999 if (!mt)
1000 return -ENOMEM;
1001
1002 mt->conf = of_device_get_match_data(&pdev->dev);
1003
1004 mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
1005 if (IS_ERR(mt->clk_peri_therm))
1006 return PTR_ERR(mt->clk_peri_therm);
1007
1008 mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
1009 if (IS_ERR(mt->clk_auxadc))
1010 return PTR_ERR(mt->clk_auxadc);
1011
1012 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1013 mt->thermal_base = devm_ioremap_resource(&pdev->dev, res);
1014 if (IS_ERR(mt->thermal_base))
1015 return PTR_ERR(mt->thermal_base);
1016
1017 ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
1018 if (ret)
1019 return ret;
1020
1021 mutex_init(&mt->lock);
1022
1023 mt->dev = &pdev->dev;
1024
1025 auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
1026 if (!auxadc) {
1027 dev_err(&pdev->dev, "missing auxadc node\n");
1028 return -ENODEV;
1029 }
1030
1031 auxadc_base = of_iomap(auxadc, 0);
1032 auxadc_phys_base = of_get_phys_base(auxadc);
1033
1034 of_node_put(auxadc);
1035
1036 if (auxadc_phys_base == OF_BAD_ADDR) {
1037 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1038 return -EINVAL;
1039 }
1040
1041 apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
1042 if (!apmixedsys) {
1043 dev_err(&pdev->dev, "missing apmixedsys node\n");
1044 return -ENODEV;
1045 }
1046
1047 apmixed_base = of_iomap(apmixedsys, 0);
1048 apmixed_phys_base = of_get_phys_base(apmixedsys);
1049
1050 of_node_put(apmixedsys);
1051
1052 if (apmixed_phys_base == OF_BAD_ADDR) {
1053 dev_err(&pdev->dev, "Can't get auxadc phys address\n");
1054 return -EINVAL;
1055 }
1056
1057 ret = device_reset_optional(&pdev->dev);
1058 if (ret)
1059 return ret;
1060
1061 ret = clk_prepare_enable(mt->clk_auxadc);
1062 if (ret) {
1063 dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
1064 return ret;
1065 }
1066
1067 ret = clk_prepare_enable(mt->clk_peri_therm);
1068 if (ret) {
1069 dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
1070 goto err_disable_clk_auxadc;
1071 }
1072
1073 if (mt->conf->version == MTK_THERMAL_V2) {
1074 mtk_thermal_turn_on_buffer(apmixed_base);
1075 mtk_thermal_release_periodic_ts(mt, auxadc_base);
1076 }
1077
1078 for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
1079 for (i = 0; i < mt->conf->num_banks; i++)
1080 mtk_thermal_init_bank(mt, i, apmixed_phys_base,
1081 auxadc_phys_base, ctrl_id);
1082
1083 platform_set_drvdata(pdev, mt);
1084
1085 tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mt,
1086 &mtk_thermal_ops);
1087 if (IS_ERR(tzdev)) {
1088 ret = PTR_ERR(tzdev);
1089 goto err_disable_clk_peri_therm;
1090 }
1091
1092 ret = devm_thermal_add_hwmon_sysfs(tzdev);
1093 if (ret)
1094 dev_warn(&pdev->dev, "error in thermal_add_hwmon_sysfs");
1095
1096 return 0;
1097
1098 err_disable_clk_peri_therm:
1099 clk_disable_unprepare(mt->clk_peri_therm);
1100 err_disable_clk_auxadc:
1101 clk_disable_unprepare(mt->clk_auxadc);
1102
1103 return ret;
1104 }
1105
1106 static int mtk_thermal_remove(struct platform_device *pdev)
1107 {
1108 struct mtk_thermal *mt = platform_get_drvdata(pdev);
1109
1110 clk_disable_unprepare(mt->clk_peri_therm);
1111 clk_disable_unprepare(mt->clk_auxadc);
1112
1113 return 0;
1114 }
1115
1116 static struct platform_driver mtk_thermal_driver = {
1117 .probe = mtk_thermal_probe,
1118 .remove = mtk_thermal_remove,
1119 .driver = {
1120 .name = "mtk-thermal",
1121 .of_match_table = mtk_thermal_of_match,
1122 },
1123 };
1124
1125 module_platform_driver(mtk_thermal_driver);
1126
1127 MODULE_AUTHOR("Michael Kao <michael.kao@mediatek.com>");
1128 MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>");
1129 MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>");
1130 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
1131 MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>");
1132 MODULE_DESCRIPTION("Mediatek thermal driver");
1133 MODULE_LICENSE("GPL v2");