1 // 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 #include "../thermal_hwmon.h" 27 28 /* AUXADC Registers */ 29 #define AUXADC_CON1_SET_V 0x008 30 #define AUXADC_CON1_CLR_V 0x00c 31 #define AUXADC_CON2_V 0x010 32 #define AUXADC_DATA(channel) (0x14 + (channel) * 4) 33 34 #define APMIXED_SYS_TS_CON0 0x600 35 #define APMIXED_SYS_TS_CON1 0x604 36 37 /* Thermal Controller Registers */ 38 #define TEMP_MONCTL0 0x000 39 #define TEMP_MONCTL1 0x004 40 #define TEMP_MONCTL2 0x008 41 #define TEMP_MONIDET0 0x014 42 #define TEMP_MONIDET1 0x018 43 #define TEMP_MSRCTL0 0x038 44 #define TEMP_MSRCTL1 0x03c 45 #define TEMP_AHBPOLL 0x040 46 #define TEMP_AHBTO 0x044 47 #define TEMP_ADCPNP0 0x048 48 #define TEMP_ADCPNP1 0x04c 49 #define TEMP_ADCPNP2 0x050 50 #define TEMP_ADCPNP3 0x0b4 51 52 #define TEMP_ADCMUX 0x054 53 #define TEMP_ADCEN 0x060 54 #define TEMP_PNPMUXADDR 0x064 55 #define TEMP_ADCMUXADDR 0x068 56 #define TEMP_ADCENADDR 0x074 57 #define TEMP_ADCVALIDADDR 0x078 58 #define TEMP_ADCVOLTADDR 0x07c 59 #define TEMP_RDCTRL 0x080 60 #define TEMP_ADCVALIDMASK 0x084 61 #define TEMP_ADCVOLTAGESHIFT 0x088 62 #define TEMP_ADCWRITECTRL 0x08c 63 #define TEMP_MSR0 0x090 64 #define TEMP_MSR1 0x094 65 #define TEMP_MSR2 0x098 66 #define TEMP_MSR3 0x0B8 67 68 #define TEMP_SPARE0 0x0f0 69 70 #define TEMP_ADCPNP0_1 0x148 71 #define TEMP_ADCPNP1_1 0x14c 72 #define TEMP_ADCPNP2_1 0x150 73 #define TEMP_MSR0_1 0x190 74 #define TEMP_MSR1_1 0x194 75 #define TEMP_MSR2_1 0x198 76 #define TEMP_ADCPNP3_1 0x1b4 77 #define TEMP_MSR3_1 0x1B8 78 79 #define PTPCORESEL 0x400 80 81 #define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff) 82 83 #define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16) 84 #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 94 /* MT8173 thermal sensors */ 95 #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 113 /* The number of controller in the MT8173 */ 114 #define MT8173_NUM_CONTROLLER 1 115 116 /* The calibration coefficient of sensor */ 117 #define MT8173_CALIBRATION 165 118 119 /* 120 * Layout of the fuses providing the calibration data 121 * These macros could be used for MT8183, MT8173, MT2701, and MT2712. 122 * MT8183 has 6 sensors and needs 6 VTS calibration data. 123 * MT8173 has 5 sensors and needs 5 VTS calibration data. 124 * MT2701 has 3 sensors and needs 3 VTS calibration data. 125 * MT2712 has 4 sensors and needs 4 VTS calibration data. 126 */ 127 #define CALIB_BUF0_VALID_V1 BIT(0) 128 #define CALIB_BUF1_ADC_GE_V1(x) (((x) >> 22) & 0x3ff) 129 #define CALIB_BUF0_VTS_TS1_V1(x) (((x) >> 17) & 0x1ff) 130 #define CALIB_BUF0_VTS_TS2_V1(x) (((x) >> 8) & 0x1ff) 131 #define CALIB_BUF1_VTS_TS3_V1(x) (((x) >> 0) & 0x1ff) 132 #define CALIB_BUF2_VTS_TS4_V1(x) (((x) >> 23) & 0x1ff) 133 #define CALIB_BUF2_VTS_TS5_V1(x) (((x) >> 5) & 0x1ff) 134 #define CALIB_BUF2_VTS_TSABB_V1(x) (((x) >> 14) & 0x1ff) 135 #define CALIB_BUF0_DEGC_CALI_V1(x) (((x) >> 1) & 0x3f) 136 #define CALIB_BUF0_O_SLOPE_V1(x) (((x) >> 26) & 0x3f) 137 #define CALIB_BUF0_O_SLOPE_SIGN_V1(x) (((x) >> 7) & 0x1) 138 #define CALIB_BUF1_ID_V1(x) (((x) >> 9) & 0x1) 139 140 /* 141 * Layout of the fuses providing the calibration data 142 * These macros could be used for MT7622. 143 */ 144 #define CALIB_BUF0_ADC_OE_V2(x) (((x) >> 22) & 0x3ff) 145 #define CALIB_BUF0_ADC_GE_V2(x) (((x) >> 12) & 0x3ff) 146 #define CALIB_BUF0_DEGC_CALI_V2(x) (((x) >> 6) & 0x3f) 147 #define CALIB_BUF0_O_SLOPE_V2(x) (((x) >> 0) & 0x3f) 148 #define CALIB_BUF1_VTS_TS1_V2(x) (((x) >> 23) & 0x1ff) 149 #define CALIB_BUF1_VTS_TS2_V2(x) (((x) >> 14) & 0x1ff) 150 #define CALIB_BUF1_VTS_TSABB_V2(x) (((x) >> 5) & 0x1ff) 151 #define CALIB_BUF1_VALID_V2(x) (((x) >> 4) & 0x1) 152 #define CALIB_BUF1_O_SLOPE_SIGN_V2(x) (((x) >> 3) & 0x1) 153 154 /* 155 * Layout of the fuses providing the calibration data 156 * These macros can be used for MT7981 and MT7986. 157 */ 158 #define CALIB_BUF0_ADC_GE_V3(x) (((x) >> 0) & 0x3ff) 159 #define CALIB_BUF0_DEGC_CALI_V3(x) (((x) >> 20) & 0x3f) 160 #define CALIB_BUF0_O_SLOPE_V3(x) (((x) >> 26) & 0x3f) 161 #define CALIB_BUF1_VTS_TS1_V3(x) (((x) >> 0) & 0x1ff) 162 #define CALIB_BUF1_VTS_TS2_V3(x) (((x) >> 21) & 0x1ff) 163 #define CALIB_BUF1_VTS_TSABB_V3(x) (((x) >> 9) & 0x1ff) 164 #define CALIB_BUF1_VALID_V3(x) (((x) >> 18) & 0x1) 165 #define CALIB_BUF1_O_SLOPE_SIGN_V3(x) (((x) >> 19) & 0x1) 166 #define CALIB_BUF1_ID_V3(x) (((x) >> 20) & 0x1) 167 168 enum { 169 VTS1, 170 VTS2, 171 VTS3, 172 VTS4, 173 VTS5, 174 VTSABB, 175 MAX_NUM_VTS, 176 }; 177 178 enum mtk_thermal_version { 179 MTK_THERMAL_V1 = 1, 180 MTK_THERMAL_V2, 181 MTK_THERMAL_V3, 182 }; 183 184 /* MT2701 thermal sensors */ 185 #define MT2701_TS1 0 186 #define MT2701_TS2 1 187 #define MT2701_TSABB 2 188 189 /* AUXADC channel 11 is used for the temperature sensors */ 190 #define MT2701_TEMP_AUXADC_CHANNEL 11 191 192 /* The total number of temperature sensors in the MT2701 */ 193 #define MT2701_NUM_SENSORS 3 194 195 /* The number of sensing points per bank */ 196 #define MT2701_NUM_SENSORS_PER_ZONE 3 197 198 /* The number of controller in the MT2701 */ 199 #define MT2701_NUM_CONTROLLER 1 200 201 /* The calibration coefficient of sensor */ 202 #define MT2701_CALIBRATION 165 203 204 /* MT2712 thermal sensors */ 205 #define MT2712_TS1 0 206 #define MT2712_TS2 1 207 #define MT2712_TS3 2 208 #define MT2712_TS4 3 209 210 /* AUXADC channel 11 is used for the temperature sensors */ 211 #define MT2712_TEMP_AUXADC_CHANNEL 11 212 213 /* The total number of temperature sensors in the MT2712 */ 214 #define MT2712_NUM_SENSORS 4 215 216 /* The number of sensing points per bank */ 217 #define MT2712_NUM_SENSORS_PER_ZONE 4 218 219 /* The number of controller in the MT2712 */ 220 #define MT2712_NUM_CONTROLLER 1 221 222 /* The calibration coefficient of sensor */ 223 #define MT2712_CALIBRATION 165 224 225 #define MT7622_TEMP_AUXADC_CHANNEL 11 226 #define MT7622_NUM_SENSORS 1 227 #define MT7622_NUM_ZONES 1 228 #define MT7622_NUM_SENSORS_PER_ZONE 1 229 #define MT7622_TS1 0 230 #define MT7622_NUM_CONTROLLER 1 231 232 /* The maximum number of banks */ 233 #define MAX_NUM_ZONES 8 234 235 /* The calibration coefficient of sensor */ 236 #define MT7622_CALIBRATION 165 237 238 /* MT8183 thermal sensors */ 239 #define MT8183_TS1 0 240 #define MT8183_TS2 1 241 #define MT8183_TS3 2 242 #define MT8183_TS4 3 243 #define MT8183_TS5 4 244 #define MT8183_TSABB 5 245 246 /* AUXADC channel is used for the temperature sensors */ 247 #define MT8183_TEMP_AUXADC_CHANNEL 11 248 249 /* The total number of temperature sensors in the MT8183 */ 250 #define MT8183_NUM_SENSORS 6 251 252 /* The number of banks in the MT8183 */ 253 #define MT8183_NUM_ZONES 1 254 255 /* The number of sensing points per bank */ 256 #define MT8183_NUM_SENSORS_PER_ZONE 6 257 258 /* The number of controller in the MT8183 */ 259 #define MT8183_NUM_CONTROLLER 2 260 261 /* The calibration coefficient of sensor */ 262 #define MT8183_CALIBRATION 153 263 264 /* AUXADC channel 11 is used for the temperature sensors */ 265 #define MT7986_TEMP_AUXADC_CHANNEL 11 266 267 /* The total number of temperature sensors in the MT7986 */ 268 #define MT7986_NUM_SENSORS 1 269 270 /* The number of banks in the MT7986 */ 271 #define MT7986_NUM_ZONES 1 272 273 /* The number of sensing points per bank */ 274 #define MT7986_NUM_SENSORS_PER_ZONE 1 275 276 /* MT7986 thermal sensors */ 277 #define MT7986_TS1 0 278 279 /* The number of controller in the MT7986 */ 280 #define MT7986_NUM_CONTROLLER 1 281 282 /* The calibration coefficient of sensor */ 283 #define MT7986_CALIBRATION 165 284 285 /* MT8365 */ 286 #define MT8365_TEMP_AUXADC_CHANNEL 11 287 #define MT8365_CALIBRATION 164 288 #define MT8365_NUM_CONTROLLER 1 289 #define MT8365_NUM_BANKS 1 290 #define MT8365_NUM_SENSORS 3 291 #define MT8365_NUM_SENSORS_PER_ZONE 3 292 #define MT8365_TS1 0 293 #define MT8365_TS2 1 294 #define MT8365_TS3 2 295 296 struct mtk_thermal; 297 298 struct thermal_bank_cfg { 299 unsigned int num_sensors; 300 const int *sensors; 301 }; 302 303 struct mtk_thermal_bank { 304 struct mtk_thermal *mt; 305 int id; 306 }; 307 308 struct mtk_thermal_data { 309 s32 num_banks; 310 s32 num_sensors; 311 s32 auxadc_channel; 312 const int *vts_index; 313 const int *sensor_mux_values; 314 const int *msr; 315 const int *adcpnp; 316 const int cali_val; 317 const int num_controller; 318 const int *controller_offset; 319 bool need_switch_bank; 320 struct thermal_bank_cfg bank_data[MAX_NUM_ZONES]; 321 enum mtk_thermal_version version; 322 u32 apmixed_buffer_ctl_reg; 323 u32 apmixed_buffer_ctl_mask; 324 u32 apmixed_buffer_ctl_set; 325 }; 326 327 struct mtk_thermal { 328 struct device *dev; 329 void __iomem *thermal_base; 330 331 struct clk *clk_peri_therm; 332 struct clk *clk_auxadc; 333 /* lock: for getting and putting banks */ 334 struct mutex lock; 335 336 /* Calibration values */ 337 s32 adc_ge; 338 s32 adc_oe; 339 s32 degc_cali; 340 s32 o_slope; 341 s32 o_slope_sign; 342 s32 vts[MAX_NUM_VTS]; 343 344 const struct mtk_thermal_data *conf; 345 struct mtk_thermal_bank banks[MAX_NUM_ZONES]; 346 347 int (*raw_to_mcelsius)(struct mtk_thermal *mt, int sensno, s32 raw); 348 }; 349 350 /* MT8183 thermal sensor data */ 351 static const int mt8183_bank_data[MT8183_NUM_SENSORS] = { 352 MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB 353 }; 354 355 static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = { 356 TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1 357 }; 358 359 static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = { 360 TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1, 361 TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1 362 }; 363 364 static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 }; 365 static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100}; 366 367 static const int mt8183_vts_index[MT8183_NUM_SENSORS] = { 368 VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB 369 }; 370 371 /* MT8173 thermal sensor data */ 372 static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = { 373 { MT8173_TS2, MT8173_TS3 }, 374 { MT8173_TS2, MT8173_TS4 }, 375 { MT8173_TS1, MT8173_TS2, MT8173_TSABB }, 376 { MT8173_TS2 }, 377 }; 378 379 static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = { 380 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3 381 }; 382 383 static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = { 384 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3 385 }; 386 387 static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 }; 388 static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, }; 389 390 static const int mt8173_vts_index[MT8173_NUM_SENSORS] = { 391 VTS1, VTS2, VTS3, VTS4, VTSABB 392 }; 393 394 /* MT2701 thermal sensor data */ 395 static const int mt2701_bank_data[MT2701_NUM_SENSORS] = { 396 MT2701_TS1, MT2701_TS2, MT2701_TSABB 397 }; 398 399 static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = { 400 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2 401 }; 402 403 static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = { 404 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2 405 }; 406 407 static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 }; 408 static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, }; 409 410 static const int mt2701_vts_index[MT2701_NUM_SENSORS] = { 411 VTS1, VTS2, VTS3 412 }; 413 414 /* MT2712 thermal sensor data */ 415 static const int mt2712_bank_data[MT2712_NUM_SENSORS] = { 416 MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4 417 }; 418 419 static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = { 420 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3 421 }; 422 423 static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = { 424 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3 425 }; 426 427 static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 }; 428 static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, }; 429 430 static const int mt2712_vts_index[MT2712_NUM_SENSORS] = { 431 VTS1, VTS2, VTS3, VTS4 432 }; 433 434 /* MT7622 thermal sensor data */ 435 static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, }; 436 static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, }; 437 static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, }; 438 static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, }; 439 static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 }; 440 static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, }; 441 442 /* MT7986 thermal sensor data */ 443 static const int mt7986_bank_data[MT7986_NUM_SENSORS] = { MT7986_TS1, }; 444 static const int mt7986_msr[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, }; 445 static const int mt7986_adcpnp[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, }; 446 static const int mt7986_mux_values[MT7986_NUM_SENSORS] = { 0, }; 447 static const int mt7986_vts_index[MT7986_NUM_SENSORS] = { VTS1 }; 448 static const int mt7986_tc_offset[MT7986_NUM_CONTROLLER] = { 0x0, }; 449 450 /* MT8365 thermal sensor data */ 451 static const int mt8365_bank_data[MT8365_NUM_SENSORS] = { 452 MT8365_TS1, MT8365_TS2, MT8365_TS3 453 }; 454 455 static const int mt8365_msr[MT8365_NUM_SENSORS_PER_ZONE] = { 456 TEMP_MSR0, TEMP_MSR1, TEMP_MSR2 457 }; 458 459 static const int mt8365_adcpnp[MT8365_NUM_SENSORS_PER_ZONE] = { 460 TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2 461 }; 462 463 static const int mt8365_mux_values[MT8365_NUM_SENSORS] = { 0, 1, 2 }; 464 static const int mt8365_tc_offset[MT8365_NUM_CONTROLLER] = { 0 }; 465 466 static const int mt8365_vts_index[MT8365_NUM_SENSORS] = { VTS1, VTS2, VTS3 }; 467 468 /* 469 * The MT8173 thermal controller has four banks. Each bank can read up to 470 * four temperature sensors simultaneously. The MT8173 has a total of 5 471 * temperature sensors. We use each bank to measure a certain area of the 472 * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple 473 * areas, hence is used in different banks. 474 * 475 * The thermal core only gets the maximum temperature of all banks, so 476 * the bank concept wouldn't be necessary here. However, the SVS (Smart 477 * Voltage Scaling) unit makes its decisions based on the same bank 478 * data, and this indeed needs the temperatures of the individual banks 479 * for making better decisions. 480 */ 481 static const struct mtk_thermal_data mt8173_thermal_data = { 482 .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL, 483 .num_banks = MT8173_NUM_ZONES, 484 .num_sensors = MT8173_NUM_SENSORS, 485 .vts_index = mt8173_vts_index, 486 .cali_val = MT8173_CALIBRATION, 487 .num_controller = MT8173_NUM_CONTROLLER, 488 .controller_offset = mt8173_tc_offset, 489 .need_switch_bank = true, 490 .bank_data = { 491 { 492 .num_sensors = 2, 493 .sensors = mt8173_bank_data[0], 494 }, { 495 .num_sensors = 2, 496 .sensors = mt8173_bank_data[1], 497 }, { 498 .num_sensors = 3, 499 .sensors = mt8173_bank_data[2], 500 }, { 501 .num_sensors = 1, 502 .sensors = mt8173_bank_data[3], 503 }, 504 }, 505 .msr = mt8173_msr, 506 .adcpnp = mt8173_adcpnp, 507 .sensor_mux_values = mt8173_mux_values, 508 .version = MTK_THERMAL_V1, 509 }; 510 511 /* 512 * The MT2701 thermal controller has one bank, which can read up to 513 * three temperature sensors simultaneously. The MT2701 has a total of 3 514 * temperature sensors. 515 * 516 * The thermal core only gets the maximum temperature of this one bank, 517 * so the bank concept wouldn't be necessary here. However, the SVS (Smart 518 * Voltage Scaling) unit makes its decisions based on the same bank 519 * data. 520 */ 521 static const struct mtk_thermal_data mt2701_thermal_data = { 522 .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL, 523 .num_banks = 1, 524 .num_sensors = MT2701_NUM_SENSORS, 525 .vts_index = mt2701_vts_index, 526 .cali_val = MT2701_CALIBRATION, 527 .num_controller = MT2701_NUM_CONTROLLER, 528 .controller_offset = mt2701_tc_offset, 529 .need_switch_bank = true, 530 .bank_data = { 531 { 532 .num_sensors = 3, 533 .sensors = mt2701_bank_data, 534 }, 535 }, 536 .msr = mt2701_msr, 537 .adcpnp = mt2701_adcpnp, 538 .sensor_mux_values = mt2701_mux_values, 539 .version = MTK_THERMAL_V1, 540 }; 541 542 /* 543 * The MT8365 thermal controller has one bank, which can read up to 544 * four temperature sensors simultaneously. The MT8365 has a total of 3 545 * temperature sensors. 546 * 547 * The thermal core only gets the maximum temperature of this one bank, 548 * so the bank concept wouldn't be necessary here. However, the SVS (Smart 549 * Voltage Scaling) unit makes its decisions based on the same bank 550 * data. 551 */ 552 static const struct mtk_thermal_data mt8365_thermal_data = { 553 .auxadc_channel = MT8365_TEMP_AUXADC_CHANNEL, 554 .num_banks = MT8365_NUM_BANKS, 555 .num_sensors = MT8365_NUM_SENSORS, 556 .vts_index = mt8365_vts_index, 557 .cali_val = MT8365_CALIBRATION, 558 .num_controller = MT8365_NUM_CONTROLLER, 559 .controller_offset = mt8365_tc_offset, 560 .need_switch_bank = false, 561 .bank_data = { 562 { 563 .num_sensors = MT8365_NUM_SENSORS, 564 .sensors = mt8365_bank_data 565 }, 566 }, 567 .msr = mt8365_msr, 568 .adcpnp = mt8365_adcpnp, 569 .sensor_mux_values = mt8365_mux_values, 570 .version = MTK_THERMAL_V1, 571 .apmixed_buffer_ctl_reg = APMIXED_SYS_TS_CON0, 572 .apmixed_buffer_ctl_mask = (u32) ~GENMASK(29, 28), 573 .apmixed_buffer_ctl_set = 0, 574 }; 575 576 /* 577 * The MT2712 thermal controller has one bank, which can read up to 578 * four temperature sensors simultaneously. The MT2712 has a total of 4 579 * temperature sensors. 580 * 581 * The thermal core only gets the maximum temperature of this one bank, 582 * so the bank concept wouldn't be necessary here. However, the SVS (Smart 583 * Voltage Scaling) unit makes its decisions based on the same bank 584 * data. 585 */ 586 static const struct mtk_thermal_data mt2712_thermal_data = { 587 .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL, 588 .num_banks = 1, 589 .num_sensors = MT2712_NUM_SENSORS, 590 .vts_index = mt2712_vts_index, 591 .cali_val = MT2712_CALIBRATION, 592 .num_controller = MT2712_NUM_CONTROLLER, 593 .controller_offset = mt2712_tc_offset, 594 .need_switch_bank = true, 595 .bank_data = { 596 { 597 .num_sensors = 4, 598 .sensors = mt2712_bank_data, 599 }, 600 }, 601 .msr = mt2712_msr, 602 .adcpnp = mt2712_adcpnp, 603 .sensor_mux_values = mt2712_mux_values, 604 .version = MTK_THERMAL_V1, 605 }; 606 607 /* 608 * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data 609 * access. 610 */ 611 static const struct mtk_thermal_data mt7622_thermal_data = { 612 .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL, 613 .num_banks = MT7622_NUM_ZONES, 614 .num_sensors = MT7622_NUM_SENSORS, 615 .vts_index = mt7622_vts_index, 616 .cali_val = MT7622_CALIBRATION, 617 .num_controller = MT7622_NUM_CONTROLLER, 618 .controller_offset = mt7622_tc_offset, 619 .need_switch_bank = true, 620 .bank_data = { 621 { 622 .num_sensors = 1, 623 .sensors = mt7622_bank_data, 624 }, 625 }, 626 .msr = mt7622_msr, 627 .adcpnp = mt7622_adcpnp, 628 .sensor_mux_values = mt7622_mux_values, 629 .version = MTK_THERMAL_V2, 630 .apmixed_buffer_ctl_reg = APMIXED_SYS_TS_CON1, 631 .apmixed_buffer_ctl_mask = GENMASK(31, 6) | BIT(3), 632 .apmixed_buffer_ctl_set = BIT(0), 633 }; 634 635 /* 636 * The MT8183 thermal controller has one bank for the current SW framework. 637 * The MT8183 has a total of 6 temperature sensors. 638 * There are two thermal controller to control the six sensor. 639 * The first one bind 2 sensor, and the other bind 4 sensors. 640 * The thermal core only gets the maximum temperature of all sensor, so 641 * the bank concept wouldn't be necessary here. However, the SVS (Smart 642 * Voltage Scaling) unit makes its decisions based on the same bank 643 * data, and this indeed needs the temperatures of the individual banks 644 * for making better decisions. 645 */ 646 static const struct mtk_thermal_data mt8183_thermal_data = { 647 .auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL, 648 .num_banks = MT8183_NUM_ZONES, 649 .num_sensors = MT8183_NUM_SENSORS, 650 .vts_index = mt8183_vts_index, 651 .cali_val = MT8183_CALIBRATION, 652 .num_controller = MT8183_NUM_CONTROLLER, 653 .controller_offset = mt8183_tc_offset, 654 .need_switch_bank = false, 655 .bank_data = { 656 { 657 .num_sensors = 6, 658 .sensors = mt8183_bank_data, 659 }, 660 }, 661 662 .msr = mt8183_msr, 663 .adcpnp = mt8183_adcpnp, 664 .sensor_mux_values = mt8183_mux_values, 665 .version = MTK_THERMAL_V1, 666 }; 667 668 /* 669 * MT7986 uses AUXADC Channel 11 for raw data access. 670 */ 671 static const struct mtk_thermal_data mt7986_thermal_data = { 672 .auxadc_channel = MT7986_TEMP_AUXADC_CHANNEL, 673 .num_banks = MT7986_NUM_ZONES, 674 .num_sensors = MT7986_NUM_SENSORS, 675 .vts_index = mt7986_vts_index, 676 .cali_val = MT7986_CALIBRATION, 677 .num_controller = MT7986_NUM_CONTROLLER, 678 .controller_offset = mt7986_tc_offset, 679 .need_switch_bank = true, 680 .bank_data = { 681 { 682 .num_sensors = 1, 683 .sensors = mt7986_bank_data, 684 }, 685 }, 686 .msr = mt7986_msr, 687 .adcpnp = mt7986_adcpnp, 688 .sensor_mux_values = mt7986_mux_values, 689 .version = MTK_THERMAL_V3, 690 }; 691 692 /** 693 * raw_to_mcelsius_v1 - convert a raw ADC value to mcelsius 694 * @mt: The thermal controller 695 * @sensno: sensor number 696 * @raw: raw ADC value 697 * 698 * This converts the raw ADC value to mcelsius using the SoC specific 699 * calibration constants 700 */ 701 static int raw_to_mcelsius_v1(struct mtk_thermal *mt, int sensno, s32 raw) 702 { 703 s32 tmp; 704 705 raw &= 0xfff; 706 707 tmp = 203450520 << 3; 708 tmp /= mt->conf->cali_val + mt->o_slope; 709 tmp /= 10000 + mt->adc_ge; 710 tmp *= raw - mt->vts[sensno] - 3350; 711 tmp >>= 3; 712 713 return mt->degc_cali * 500 - tmp; 714 } 715 716 static int raw_to_mcelsius_v2(struct mtk_thermal *mt, int sensno, s32 raw) 717 { 718 s32 format_1; 719 s32 format_2; 720 s32 g_oe; 721 s32 g_gain; 722 s32 g_x_roomt; 723 s32 tmp; 724 725 if (raw == 0) 726 return 0; 727 728 raw &= 0xfff; 729 g_gain = 10000 + (((mt->adc_ge - 512) * 10000) >> 12); 730 g_oe = mt->adc_oe - 512; 731 format_1 = mt->vts[VTS2] + 3105 - g_oe; 732 format_2 = (mt->degc_cali * 10) >> 1; 733 g_x_roomt = (((format_1 * 10000) >> 12) * 10000) / g_gain; 734 735 tmp = (((((raw - g_oe) * 10000) >> 12) * 10000) / g_gain) - g_x_roomt; 736 tmp = tmp * 10 * 100 / 11; 737 738 if (mt->o_slope_sign == 0) 739 tmp = tmp / (165 - mt->o_slope); 740 else 741 tmp = tmp / (165 + mt->o_slope); 742 743 return (format_2 - tmp) * 100; 744 } 745 746 static int raw_to_mcelsius_v3(struct mtk_thermal *mt, int sensno, s32 raw) 747 { 748 s32 tmp; 749 750 if (raw == 0) 751 return 0; 752 753 raw &= 0xfff; 754 tmp = 100000 * 15 / 16 * 10000; 755 tmp /= 4096 - 512 + mt->adc_ge; 756 tmp /= 1490; 757 tmp *= raw - mt->vts[sensno] - 2900; 758 759 return mt->degc_cali * 500 - tmp; 760 } 761 762 /** 763 * mtk_thermal_get_bank - get bank 764 * @bank: The bank 765 * 766 * The bank registers are banked, we have to select a bank in the 767 * PTPCORESEL register to access it. 768 */ 769 static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank) 770 { 771 struct mtk_thermal *mt = bank->mt; 772 u32 val; 773 774 if (mt->conf->need_switch_bank) { 775 mutex_lock(&mt->lock); 776 777 val = readl(mt->thermal_base + PTPCORESEL); 778 val &= ~0xf; 779 val |= bank->id; 780 writel(val, mt->thermal_base + PTPCORESEL); 781 } 782 } 783 784 /** 785 * mtk_thermal_put_bank - release bank 786 * @bank: The bank 787 * 788 * release a bank previously taken with mtk_thermal_get_bank, 789 */ 790 static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank) 791 { 792 struct mtk_thermal *mt = bank->mt; 793 794 if (mt->conf->need_switch_bank) 795 mutex_unlock(&mt->lock); 796 } 797 798 /** 799 * mtk_thermal_bank_temperature - get the temperature of a bank 800 * @bank: The bank 801 * 802 * The temperature of a bank is considered the maximum temperature of 803 * the sensors associated to the bank. 804 */ 805 static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank) 806 { 807 struct mtk_thermal *mt = bank->mt; 808 const struct mtk_thermal_data *conf = mt->conf; 809 int i, temp = INT_MIN, max = INT_MIN; 810 u32 raw; 811 812 for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) { 813 raw = readl(mt->thermal_base + conf->msr[i]); 814 815 temp = mt->raw_to_mcelsius( 816 mt, conf->bank_data[bank->id].sensors[i], raw); 817 818 819 if (temp > max) 820 max = temp; 821 } 822 823 return max; 824 } 825 826 static int mtk_read_temp(struct thermal_zone_device *tz, int *temperature) 827 { 828 struct mtk_thermal *mt = thermal_zone_device_priv(tz); 829 int i; 830 int tempmax = INT_MIN; 831 832 for (i = 0; i < mt->conf->num_banks; i++) { 833 struct mtk_thermal_bank *bank = &mt->banks[i]; 834 835 mtk_thermal_get_bank(bank); 836 837 tempmax = max(tempmax, mtk_thermal_bank_temperature(bank)); 838 839 mtk_thermal_put_bank(bank); 840 } 841 842 *temperature = tempmax; 843 844 return 0; 845 } 846 847 static const struct thermal_zone_device_ops mtk_thermal_ops = { 848 .get_temp = mtk_read_temp, 849 }; 850 851 static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num, 852 u32 apmixed_phys_base, u32 auxadc_phys_base, 853 int ctrl_id) 854 { 855 struct mtk_thermal_bank *bank = &mt->banks[num]; 856 const struct mtk_thermal_data *conf = mt->conf; 857 int i; 858 859 int offset = mt->conf->controller_offset[ctrl_id]; 860 void __iomem *controller_base = mt->thermal_base + offset; 861 862 bank->id = num; 863 bank->mt = mt; 864 865 mtk_thermal_get_bank(bank); 866 867 /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */ 868 writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1); 869 870 /* 871 * filt interval is 1 * 46.540us = 46.54us, 872 * sen interval is 429 * 46.540us = 19.96ms 873 */ 874 writel(TEMP_MONCTL2_FILTER_INTERVAL(1) | 875 TEMP_MONCTL2_SENSOR_INTERVAL(429), 876 controller_base + TEMP_MONCTL2); 877 878 /* poll is set to 10u */ 879 writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768), 880 controller_base + TEMP_AHBPOLL); 881 882 /* temperature sampling control, 1 sample */ 883 writel(0x0, controller_base + TEMP_MSRCTL0); 884 885 /* exceed this polling time, IRQ would be inserted */ 886 writel(0xffffffff, controller_base + TEMP_AHBTO); 887 888 /* number of interrupts per event, 1 is enough */ 889 writel(0x0, controller_base + TEMP_MONIDET0); 890 writel(0x0, controller_base + TEMP_MONIDET1); 891 892 /* 893 * The MT8173 thermal controller does not have its own ADC. Instead it 894 * uses AHB bus accesses to control the AUXADC. To do this the thermal 895 * controller has to be programmed with the physical addresses of the 896 * AUXADC registers and with the various bit positions in the AUXADC. 897 * Also the thermal controller controls a mux in the APMIXEDSYS register 898 * space. 899 */ 900 901 /* 902 * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0) 903 * automatically by hw 904 */ 905 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX); 906 907 /* AHB address for auxadc mux selection */ 908 writel(auxadc_phys_base + AUXADC_CON1_CLR_V, 909 controller_base + TEMP_ADCMUXADDR); 910 911 if (mt->conf->version == MTK_THERMAL_V1) { 912 /* AHB address for pnp sensor mux selection */ 913 writel(apmixed_phys_base + APMIXED_SYS_TS_CON1, 914 controller_base + TEMP_PNPMUXADDR); 915 } 916 917 /* AHB value for auxadc enable */ 918 writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN); 919 920 /* AHB address for auxadc enable (channel 0 immediate mode selected) */ 921 writel(auxadc_phys_base + AUXADC_CON1_SET_V, 922 controller_base + TEMP_ADCENADDR); 923 924 /* AHB address for auxadc valid bit */ 925 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel), 926 controller_base + TEMP_ADCVALIDADDR); 927 928 /* AHB address for auxadc voltage output */ 929 writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel), 930 controller_base + TEMP_ADCVOLTADDR); 931 932 /* read valid & voltage are at the same register */ 933 writel(0x0, controller_base + TEMP_RDCTRL); 934 935 /* indicate where the valid bit is */ 936 writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12), 937 controller_base + TEMP_ADCVALIDMASK); 938 939 /* no shift */ 940 writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT); 941 942 /* enable auxadc mux write transaction */ 943 writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE, 944 controller_base + TEMP_ADCWRITECTRL); 945 946 for (i = 0; i < conf->bank_data[num].num_sensors; i++) 947 writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]], 948 mt->thermal_base + conf->adcpnp[i]); 949 950 writel((1 << conf->bank_data[num].num_sensors) - 1, 951 controller_base + TEMP_MONCTL0); 952 953 writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE | 954 TEMP_ADCWRITECTRL_ADC_MUX_WRITE, 955 controller_base + TEMP_ADCWRITECTRL); 956 957 mtk_thermal_put_bank(bank); 958 } 959 960 static u64 of_get_phys_base(struct device_node *np) 961 { 962 u64 size64; 963 const __be32 *regaddr_p; 964 965 regaddr_p = of_get_address(np, 0, &size64, NULL); 966 if (!regaddr_p) 967 return OF_BAD_ADDR; 968 969 return of_translate_address(np, regaddr_p); 970 } 971 972 static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf) 973 { 974 int i; 975 976 if (!(buf[0] & CALIB_BUF0_VALID_V1)) 977 return -EINVAL; 978 979 mt->adc_ge = CALIB_BUF1_ADC_GE_V1(buf[1]); 980 981 for (i = 0; i < mt->conf->num_sensors; i++) { 982 switch (mt->conf->vts_index[i]) { 983 case VTS1: 984 mt->vts[VTS1] = CALIB_BUF0_VTS_TS1_V1(buf[0]); 985 break; 986 case VTS2: 987 mt->vts[VTS2] = CALIB_BUF0_VTS_TS2_V1(buf[0]); 988 break; 989 case VTS3: 990 mt->vts[VTS3] = CALIB_BUF1_VTS_TS3_V1(buf[1]); 991 break; 992 case VTS4: 993 mt->vts[VTS4] = CALIB_BUF2_VTS_TS4_V1(buf[2]); 994 break; 995 case VTS5: 996 mt->vts[VTS5] = CALIB_BUF2_VTS_TS5_V1(buf[2]); 997 break; 998 case VTSABB: 999 mt->vts[VTSABB] = 1000 CALIB_BUF2_VTS_TSABB_V1(buf[2]); 1001 break; 1002 default: 1003 break; 1004 } 1005 } 1006 1007 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V1(buf[0]); 1008 if (CALIB_BUF1_ID_V1(buf[1]) & 1009 CALIB_BUF0_O_SLOPE_SIGN_V1(buf[0])) 1010 mt->o_slope = -CALIB_BUF0_O_SLOPE_V1(buf[0]); 1011 else 1012 mt->o_slope = CALIB_BUF0_O_SLOPE_V1(buf[0]); 1013 1014 return 0; 1015 } 1016 1017 static int mtk_thermal_extract_efuse_v2(struct mtk_thermal *mt, u32 *buf) 1018 { 1019 if (!CALIB_BUF1_VALID_V2(buf[1])) 1020 return -EINVAL; 1021 1022 mt->adc_oe = CALIB_BUF0_ADC_OE_V2(buf[0]); 1023 mt->adc_ge = CALIB_BUF0_ADC_GE_V2(buf[0]); 1024 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V2(buf[0]); 1025 mt->o_slope = CALIB_BUF0_O_SLOPE_V2(buf[0]); 1026 mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V2(buf[1]); 1027 mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V2(buf[1]); 1028 mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V2(buf[1]); 1029 mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2(buf[1]); 1030 1031 return 0; 1032 } 1033 1034 static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf) 1035 { 1036 if (!CALIB_BUF1_VALID_V3(buf[1])) 1037 return -EINVAL; 1038 1039 mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]); 1040 mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]); 1041 mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]); 1042 mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]); 1043 mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]); 1044 mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]); 1045 mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]); 1046 1047 if (CALIB_BUF1_ID_V3(buf[1]) == 0) 1048 mt->o_slope = 0; 1049 1050 return 0; 1051 } 1052 1053 static int mtk_thermal_get_calibration_data(struct device *dev, 1054 struct mtk_thermal *mt) 1055 { 1056 struct nvmem_cell *cell; 1057 u32 *buf; 1058 size_t len; 1059 int i, ret = 0; 1060 1061 /* Start with default values */ 1062 mt->adc_ge = 512; 1063 mt->adc_oe = 512; 1064 for (i = 0; i < mt->conf->num_sensors; i++) 1065 mt->vts[i] = 260; 1066 mt->degc_cali = 40; 1067 mt->o_slope = 0; 1068 1069 cell = nvmem_cell_get(dev, "calibration-data"); 1070 if (IS_ERR(cell)) { 1071 if (PTR_ERR(cell) == -EPROBE_DEFER) 1072 return PTR_ERR(cell); 1073 return 0; 1074 } 1075 1076 buf = (u32 *)nvmem_cell_read(cell, &len); 1077 1078 nvmem_cell_put(cell); 1079 1080 if (IS_ERR(buf)) 1081 return PTR_ERR(buf); 1082 1083 if (len < 3 * sizeof(u32)) { 1084 dev_warn(dev, "invalid calibration data\n"); 1085 ret = -EINVAL; 1086 goto out; 1087 } 1088 1089 switch (mt->conf->version) { 1090 case MTK_THERMAL_V1: 1091 ret = mtk_thermal_extract_efuse_v1(mt, buf); 1092 break; 1093 case MTK_THERMAL_V2: 1094 ret = mtk_thermal_extract_efuse_v2(mt, buf); 1095 break; 1096 case MTK_THERMAL_V3: 1097 ret = mtk_thermal_extract_efuse_v3(mt, buf); 1098 break; 1099 default: 1100 ret = -EINVAL; 1101 break; 1102 } 1103 1104 if (ret) { 1105 dev_info(dev, "Device not calibrated, using default calibration values\n"); 1106 ret = 0; 1107 } 1108 1109 out: 1110 kfree(buf); 1111 1112 return ret; 1113 } 1114 1115 static const struct of_device_id mtk_thermal_of_match[] = { 1116 { 1117 .compatible = "mediatek,mt8173-thermal", 1118 .data = (void *)&mt8173_thermal_data, 1119 }, 1120 { 1121 .compatible = "mediatek,mt2701-thermal", 1122 .data = (void *)&mt2701_thermal_data, 1123 }, 1124 { 1125 .compatible = "mediatek,mt2712-thermal", 1126 .data = (void *)&mt2712_thermal_data, 1127 }, 1128 { 1129 .compatible = "mediatek,mt7622-thermal", 1130 .data = (void *)&mt7622_thermal_data, 1131 }, 1132 { 1133 .compatible = "mediatek,mt7986-thermal", 1134 .data = (void *)&mt7986_thermal_data, 1135 }, 1136 { 1137 .compatible = "mediatek,mt8183-thermal", 1138 .data = (void *)&mt8183_thermal_data, 1139 }, 1140 { 1141 .compatible = "mediatek,mt8365-thermal", 1142 .data = (void *)&mt8365_thermal_data, 1143 }, { 1144 }, 1145 }; 1146 MODULE_DEVICE_TABLE(of, mtk_thermal_of_match); 1147 1148 static void mtk_thermal_turn_on_buffer(struct mtk_thermal *mt, 1149 void __iomem *apmixed_base) 1150 { 1151 u32 tmp; 1152 1153 if (!mt->conf->apmixed_buffer_ctl_reg) 1154 return; 1155 1156 tmp = readl(apmixed_base + mt->conf->apmixed_buffer_ctl_reg); 1157 tmp &= mt->conf->apmixed_buffer_ctl_mask; 1158 tmp |= mt->conf->apmixed_buffer_ctl_set; 1159 writel(tmp, apmixed_base + mt->conf->apmixed_buffer_ctl_reg); 1160 udelay(200); 1161 } 1162 1163 static void mtk_thermal_release_periodic_ts(struct mtk_thermal *mt, 1164 void __iomem *auxadc_base) 1165 { 1166 int tmp; 1167 1168 writel(0x800, auxadc_base + AUXADC_CON1_SET_V); 1169 writel(0x1, mt->thermal_base + TEMP_MONCTL0); 1170 tmp = readl(mt->thermal_base + TEMP_MSRCTL1); 1171 writel((tmp & (~0x10e)), mt->thermal_base + TEMP_MSRCTL1); 1172 } 1173 1174 static int mtk_thermal_probe(struct platform_device *pdev) 1175 { 1176 int ret, i, ctrl_id; 1177 struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node; 1178 struct mtk_thermal *mt; 1179 u64 auxadc_phys_base, apmixed_phys_base; 1180 struct thermal_zone_device *tzdev; 1181 void __iomem *apmixed_base, *auxadc_base; 1182 1183 mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL); 1184 if (!mt) 1185 return -ENOMEM; 1186 1187 mt->conf = of_device_get_match_data(&pdev->dev); 1188 1189 mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm"); 1190 if (IS_ERR(mt->clk_peri_therm)) 1191 return PTR_ERR(mt->clk_peri_therm); 1192 1193 mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc"); 1194 if (IS_ERR(mt->clk_auxadc)) 1195 return PTR_ERR(mt->clk_auxadc); 1196 1197 mt->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); 1198 if (IS_ERR(mt->thermal_base)) 1199 return PTR_ERR(mt->thermal_base); 1200 1201 ret = mtk_thermal_get_calibration_data(&pdev->dev, mt); 1202 if (ret) 1203 return ret; 1204 1205 mutex_init(&mt->lock); 1206 1207 mt->dev = &pdev->dev; 1208 1209 auxadc = of_parse_phandle(np, "mediatek,auxadc", 0); 1210 if (!auxadc) { 1211 dev_err(&pdev->dev, "missing auxadc node\n"); 1212 return -ENODEV; 1213 } 1214 1215 auxadc_base = of_iomap(auxadc, 0); 1216 auxadc_phys_base = of_get_phys_base(auxadc); 1217 1218 of_node_put(auxadc); 1219 1220 if (auxadc_phys_base == OF_BAD_ADDR) { 1221 dev_err(&pdev->dev, "Can't get auxadc phys address\n"); 1222 return -EINVAL; 1223 } 1224 1225 apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0); 1226 if (!apmixedsys) { 1227 dev_err(&pdev->dev, "missing apmixedsys node\n"); 1228 return -ENODEV; 1229 } 1230 1231 apmixed_base = of_iomap(apmixedsys, 0); 1232 apmixed_phys_base = of_get_phys_base(apmixedsys); 1233 1234 of_node_put(apmixedsys); 1235 1236 if (apmixed_phys_base == OF_BAD_ADDR) { 1237 dev_err(&pdev->dev, "Can't get auxadc phys address\n"); 1238 return -EINVAL; 1239 } 1240 1241 ret = device_reset_optional(&pdev->dev); 1242 if (ret) 1243 return ret; 1244 1245 ret = clk_prepare_enable(mt->clk_auxadc); 1246 if (ret) { 1247 dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret); 1248 return ret; 1249 } 1250 1251 ret = clk_prepare_enable(mt->clk_peri_therm); 1252 if (ret) { 1253 dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret); 1254 goto err_disable_clk_auxadc; 1255 } 1256 1257 mtk_thermal_turn_on_buffer(mt, apmixed_base); 1258 1259 if (mt->conf->version != MTK_THERMAL_V2) 1260 mtk_thermal_release_periodic_ts(mt, auxadc_base); 1261 1262 if (mt->conf->version == MTK_THERMAL_V1) 1263 mt->raw_to_mcelsius = raw_to_mcelsius_v1; 1264 else if (mt->conf->version == MTK_THERMAL_V2) 1265 mt->raw_to_mcelsius = raw_to_mcelsius_v2; 1266 else 1267 mt->raw_to_mcelsius = raw_to_mcelsius_v3; 1268 1269 for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++) 1270 for (i = 0; i < mt->conf->num_banks; i++) 1271 mtk_thermal_init_bank(mt, i, apmixed_phys_base, 1272 auxadc_phys_base, ctrl_id); 1273 1274 platform_set_drvdata(pdev, mt); 1275 1276 /* Delay for thermal banks to be ready */ 1277 msleep(30); 1278 1279 tzdev = devm_thermal_of_zone_register(&pdev->dev, 0, mt, 1280 &mtk_thermal_ops); 1281 if (IS_ERR(tzdev)) { 1282 ret = PTR_ERR(tzdev); 1283 goto err_disable_clk_peri_therm; 1284 } 1285 1286 ret = devm_thermal_add_hwmon_sysfs(&pdev->dev, tzdev); 1287 if (ret) 1288 dev_warn(&pdev->dev, "error in thermal_add_hwmon_sysfs"); 1289 1290 return 0; 1291 1292 err_disable_clk_peri_therm: 1293 clk_disable_unprepare(mt->clk_peri_therm); 1294 err_disable_clk_auxadc: 1295 clk_disable_unprepare(mt->clk_auxadc); 1296 1297 return ret; 1298 } 1299 1300 static int mtk_thermal_remove(struct platform_device *pdev) 1301 { 1302 struct mtk_thermal *mt = platform_get_drvdata(pdev); 1303 1304 clk_disable_unprepare(mt->clk_peri_therm); 1305 clk_disable_unprepare(mt->clk_auxadc); 1306 1307 return 0; 1308 } 1309 1310 static struct platform_driver mtk_thermal_driver = { 1311 .probe = mtk_thermal_probe, 1312 .remove = mtk_thermal_remove, 1313 .driver = { 1314 .name = "mtk-thermal", 1315 .of_match_table = mtk_thermal_of_match, 1316 }, 1317 }; 1318 1319 module_platform_driver(mtk_thermal_driver); 1320 1321 MODULE_AUTHOR("Michael Kao <michael.kao@mediatek.com>"); 1322 MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>"); 1323 MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>"); 1324 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); 1325 MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>"); 1326 MODULE_DESCRIPTION("Mediatek thermal driver"); 1327 MODULE_LICENSE("GPL v2"); 1328