1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2022 MediaTek Inc. 4 * Copyright (C) 2022 Collabora Ltd. 5 * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/bits.h> 10 #include <linux/clk.h> 11 #include <linux/completion.h> 12 #include <linux/cpu.h> 13 #include <linux/cpuidle.h> 14 #include <linux/debugfs.h> 15 #include <linux/device.h> 16 #include <linux/init.h> 17 #include <linux/interrupt.h> 18 #include <linux/kernel.h> 19 #include <linux/kthread.h> 20 #include <linux/module.h> 21 #include <linux/mutex.h> 22 #include <linux/nvmem-consumer.h> 23 #include <linux/of_address.h> 24 #include <linux/of_irq.h> 25 #include <linux/of_platform.h> 26 #include <linux/platform_device.h> 27 #include <linux/pm_domain.h> 28 #include <linux/pm_opp.h> 29 #include <linux/pm_runtime.h> 30 #include <linux/regulator/consumer.h> 31 #include <linux/reset.h> 32 #include <linux/seq_file.h> 33 #include <linux/slab.h> 34 #include <linux/spinlock.h> 35 #include <linux/thermal.h> 36 37 /* svs bank mode support */ 38 #define SVSB_MODE_ALL_DISABLE 0 39 #define SVSB_MODE_INIT01 BIT(1) 40 #define SVSB_MODE_INIT02 BIT(2) 41 #define SVSB_MODE_MON BIT(3) 42 43 /* svs bank volt flags */ 44 #define SVSB_INIT01_PD_REQ BIT(0) 45 #define SVSB_INIT01_VOLT_IGNORE BIT(1) 46 #define SVSB_INIT01_VOLT_INC_ONLY BIT(2) 47 #define SVSB_MON_VOLT_IGNORE BIT(16) 48 #define SVSB_REMOVE_DVTFIXED_VOLT BIT(24) 49 50 /* svs bank register fields and common configuration */ 51 #define SVSB_PTPCONFIG_DETMAX GENMASK(15, 0) 52 #define SVSB_DET_MAX FIELD_PREP(SVSB_PTPCONFIG_DETMAX, 0xffff) 53 #define SVSB_DET_WINDOW 0xa28 54 55 /* DESCHAR */ 56 #define SVSB_DESCHAR_FLD_MDES GENMASK(7, 0) 57 #define SVSB_DESCHAR_FLD_BDES GENMASK(15, 8) 58 59 /* TEMPCHAR */ 60 #define SVSB_TEMPCHAR_FLD_DVT_FIXED GENMASK(7, 0) 61 #define SVSB_TEMPCHAR_FLD_MTDES GENMASK(15, 8) 62 #define SVSB_TEMPCHAR_FLD_VCO GENMASK(23, 16) 63 64 /* DETCHAR */ 65 #define SVSB_DETCHAR_FLD_DCMDET GENMASK(7, 0) 66 #define SVSB_DETCHAR_FLD_DCBDET GENMASK(15, 8) 67 68 /* SVSEN (PTPEN) */ 69 #define SVSB_PTPEN_INIT01 BIT(0) 70 #define SVSB_PTPEN_MON BIT(1) 71 #define SVSB_PTPEN_INIT02 (SVSB_PTPEN_INIT01 | BIT(2)) 72 #define SVSB_PTPEN_OFF 0x0 73 74 /* FREQPCTS */ 75 #define SVSB_FREQPCTS_FLD_PCT0_4 GENMASK(7, 0) 76 #define SVSB_FREQPCTS_FLD_PCT1_5 GENMASK(15, 8) 77 #define SVSB_FREQPCTS_FLD_PCT2_6 GENMASK(23, 16) 78 #define SVSB_FREQPCTS_FLD_PCT3_7 GENMASK(31, 24) 79 80 /* INTSTS */ 81 #define SVSB_INTSTS_VAL_CLEAN 0x00ffffff 82 #define SVSB_INTSTS_F0_COMPLETE BIT(0) 83 #define SVSB_INTSTS_FLD_MONVOP GENMASK(23, 16) 84 #define SVSB_RUNCONFIG_DEFAULT 0x80000000 85 86 /* LIMITVALS */ 87 #define SVSB_LIMITVALS_FLD_DTLO GENMASK(7, 0) 88 #define SVSB_LIMITVALS_FLD_DTHI GENMASK(15, 8) 89 #define SVSB_LIMITVALS_FLD_VMIN GENMASK(23, 16) 90 #define SVSB_LIMITVALS_FLD_VMAX GENMASK(31, 24) 91 #define SVSB_VAL_DTHI 0x1 92 #define SVSB_VAL_DTLO 0xfe 93 94 /* INTEN */ 95 #define SVSB_INTEN_F0EN BIT(0) 96 #define SVSB_INTEN_DACK0UPEN BIT(8) 97 #define SVSB_INTEN_DC0EN BIT(9) 98 #define SVSB_INTEN_DC1EN BIT(10) 99 #define SVSB_INTEN_DACK0LOEN BIT(11) 100 #define SVSB_INTEN_INITPROD_OVF_EN BIT(12) 101 #define SVSB_INTEN_INITSUM_OVF_EN BIT(14) 102 #define SVSB_INTEN_MONVOPEN GENMASK(23, 16) 103 #define SVSB_INTEN_INIT0x (SVSB_INTEN_F0EN | SVSB_INTEN_DACK0UPEN | \ 104 SVSB_INTEN_DC0EN | SVSB_INTEN_DC1EN | \ 105 SVSB_INTEN_DACK0LOEN | \ 106 SVSB_INTEN_INITPROD_OVF_EN | \ 107 SVSB_INTEN_INITSUM_OVF_EN) 108 109 /* TSCALCS */ 110 #define SVSB_TSCALCS_FLD_MTS GENMASK(11, 0) 111 #define SVSB_TSCALCS_FLD_BTS GENMASK(23, 12) 112 113 /* INIT2VALS */ 114 #define SVSB_INIT2VALS_FLD_DCVOFFSETIN GENMASK(15, 0) 115 #define SVSB_INIT2VALS_FLD_AGEVOFFSETIN GENMASK(31, 16) 116 117 /* VOPS */ 118 #define SVSB_VOPS_FLD_VOP0_4 GENMASK(7, 0) 119 #define SVSB_VOPS_FLD_VOP1_5 GENMASK(15, 8) 120 #define SVSB_VOPS_FLD_VOP2_6 GENMASK(23, 16) 121 #define SVSB_VOPS_FLD_VOP3_7 GENMASK(31, 24) 122 123 /* SVS Thermal Coefficients */ 124 #define SVSB_TS_COEFF_MT8195 250460 125 #define SVSB_TS_COEFF_MT8186 204650 126 127 /* Algo helpers */ 128 #define FUSE_DATA_NOT_VALID U32_MAX 129 130 /* svs bank related setting */ 131 #define BITS8 8 132 #define MAX_OPP_ENTRIES 16 133 #define REG_BYTES 4 134 #define SVSB_DC_SIGNED_BIT BIT(15) 135 #define SVSB_DET_CLK_EN BIT(31) 136 #define SVSB_TEMP_LOWER_BOUND 0xb2 137 #define SVSB_TEMP_UPPER_BOUND 0x64 138 139 static DEFINE_SPINLOCK(svs_lock); 140 141 #ifdef CONFIG_DEBUG_FS 142 #define debug_fops_ro(name) \ 143 static int svs_##name##_debug_open(struct inode *inode, \ 144 struct file *filp) \ 145 { \ 146 return single_open(filp, svs_##name##_debug_show, \ 147 inode->i_private); \ 148 } \ 149 static const struct file_operations svs_##name##_debug_fops = { \ 150 .owner = THIS_MODULE, \ 151 .open = svs_##name##_debug_open, \ 152 .read = seq_read, \ 153 .llseek = seq_lseek, \ 154 .release = single_release, \ 155 } 156 157 #define debug_fops_rw(name) \ 158 static int svs_##name##_debug_open(struct inode *inode, \ 159 struct file *filp) \ 160 { \ 161 return single_open(filp, svs_##name##_debug_show, \ 162 inode->i_private); \ 163 } \ 164 static const struct file_operations svs_##name##_debug_fops = { \ 165 .owner = THIS_MODULE, \ 166 .open = svs_##name##_debug_open, \ 167 .read = seq_read, \ 168 .write = svs_##name##_debug_write, \ 169 .llseek = seq_lseek, \ 170 .release = single_release, \ 171 } 172 173 #define svs_dentry_data(name) {__stringify(name), &svs_##name##_debug_fops} 174 #endif 175 176 /** 177 * enum svsb_sw_id - SVS Bank Software ID 178 * @SVSB_SWID_CPU_LITTLE: CPU little cluster Bank 179 * @SVSB_SWID_CPU_BIG: CPU big cluster Bank 180 * @SVSB_SWID_CCI: Cache Coherent Interconnect Bank 181 * @SVSB_SWID_GPU: GPU Bank 182 * @SVSB_SWID_MAX: Total number of Banks 183 */ 184 enum svsb_sw_id { 185 SVSB_SWID_CPU_LITTLE, 186 SVSB_SWID_CPU_BIG, 187 SVSB_SWID_CCI, 188 SVSB_SWID_GPU, 189 SVSB_SWID_MAX 190 }; 191 192 /** 193 * enum svsb_type - SVS Bank 2-line: Type and Role 194 * @SVSB_TYPE_NONE: One-line type Bank - Global role 195 * @SVSB_TYPE_LOW: Two-line type Bank - Low bank role 196 * @SVSB_TYPE_HIGH: Two-line type Bank - High bank role 197 * @SVSB_TYPE_MAX: Total number of bank types 198 */ 199 enum svsb_type { 200 SVSB_TYPE_NONE, 201 SVSB_TYPE_LOW, 202 SVSB_TYPE_HIGH, 203 SVSB_TYPE_MAX 204 }; 205 206 /** 207 * enum svsb_phase - svs bank phase enumeration 208 * @SVSB_PHASE_ERROR: svs bank encounters unexpected condition 209 * @SVSB_PHASE_INIT01: svs bank basic init for data calibration 210 * @SVSB_PHASE_INIT02: svs bank can provide voltages to opp table 211 * @SVSB_PHASE_MON: svs bank can provide voltages with thermal effect 212 * @SVSB_PHASE_MAX: total number of svs bank phase (debug purpose) 213 * 214 * Each svs bank has its own independent phase and we enable each svs bank by 215 * running their phase orderly. However, when svs bank encounters unexpected 216 * condition, it will fire an irq (PHASE_ERROR) to inform svs software. 217 * 218 * svs bank general phase-enabled order: 219 * SVSB_PHASE_INIT01 -> SVSB_PHASE_INIT02 -> SVSB_PHASE_MON 220 */ 221 enum svsb_phase { 222 SVSB_PHASE_ERROR = 0, 223 SVSB_PHASE_INIT01, 224 SVSB_PHASE_INIT02, 225 SVSB_PHASE_MON, 226 SVSB_PHASE_MAX, 227 }; 228 229 enum svs_reg_index { 230 DESCHAR = 0, 231 TEMPCHAR, 232 DETCHAR, 233 AGECHAR, 234 DCCONFIG, 235 AGECONFIG, 236 FREQPCT30, 237 FREQPCT74, 238 LIMITVALS, 239 VBOOT, 240 DETWINDOW, 241 CONFIG, 242 TSCALCS, 243 RUNCONFIG, 244 SVSEN, 245 INIT2VALS, 246 DCVALUES, 247 AGEVALUES, 248 VOP30, 249 VOP74, 250 TEMP, 251 INTSTS, 252 INTSTSRAW, 253 INTEN, 254 CHKINT, 255 CHKSHIFT, 256 STATUS, 257 VDESIGN30, 258 VDESIGN74, 259 DVT30, 260 DVT74, 261 AGECOUNT, 262 SMSTATE0, 263 SMSTATE1, 264 CTL0, 265 DESDETSEC, 266 TEMPAGESEC, 267 CTRLSPARE0, 268 CTRLSPARE1, 269 CTRLSPARE2, 270 CTRLSPARE3, 271 CORESEL, 272 THERMINTST, 273 INTST, 274 THSTAGE0ST, 275 THSTAGE1ST, 276 THSTAGE2ST, 277 THAHBST0, 278 THAHBST1, 279 SPARE0, 280 SPARE1, 281 SPARE2, 282 SPARE3, 283 THSLPEVEB, 284 SVS_REG_MAX, 285 }; 286 287 static const u32 svs_regs_v2[] = { 288 [DESCHAR] = 0x00, 289 [TEMPCHAR] = 0x04, 290 [DETCHAR] = 0x08, 291 [AGECHAR] = 0x0c, 292 [DCCONFIG] = 0x10, 293 [AGECONFIG] = 0x14, 294 [FREQPCT30] = 0x18, 295 [FREQPCT74] = 0x1c, 296 [LIMITVALS] = 0x20, 297 [VBOOT] = 0x24, 298 [DETWINDOW] = 0x28, 299 [CONFIG] = 0x2c, 300 [TSCALCS] = 0x30, 301 [RUNCONFIG] = 0x34, 302 [SVSEN] = 0x38, 303 [INIT2VALS] = 0x3c, 304 [DCVALUES] = 0x40, 305 [AGEVALUES] = 0x44, 306 [VOP30] = 0x48, 307 [VOP74] = 0x4c, 308 [TEMP] = 0x50, 309 [INTSTS] = 0x54, 310 [INTSTSRAW] = 0x58, 311 [INTEN] = 0x5c, 312 [CHKINT] = 0x60, 313 [CHKSHIFT] = 0x64, 314 [STATUS] = 0x68, 315 [VDESIGN30] = 0x6c, 316 [VDESIGN74] = 0x70, 317 [DVT30] = 0x74, 318 [DVT74] = 0x78, 319 [AGECOUNT] = 0x7c, 320 [SMSTATE0] = 0x80, 321 [SMSTATE1] = 0x84, 322 [CTL0] = 0x88, 323 [DESDETSEC] = 0xe0, 324 [TEMPAGESEC] = 0xe4, 325 [CTRLSPARE0] = 0xf0, 326 [CTRLSPARE1] = 0xf4, 327 [CTRLSPARE2] = 0xf8, 328 [CTRLSPARE3] = 0xfc, 329 [CORESEL] = 0x300, 330 [THERMINTST] = 0x304, 331 [INTST] = 0x308, 332 [THSTAGE0ST] = 0x30c, 333 [THSTAGE1ST] = 0x310, 334 [THSTAGE2ST] = 0x314, 335 [THAHBST0] = 0x318, 336 [THAHBST1] = 0x31c, 337 [SPARE0] = 0x320, 338 [SPARE1] = 0x324, 339 [SPARE2] = 0x328, 340 [SPARE3] = 0x32c, 341 [THSLPEVEB] = 0x330, 342 }; 343 344 static const char * const svs_swid_names[SVSB_SWID_MAX] = { 345 "SVSB_CPU_LITTLE", "SVSB_CPU_BIG", "SVSB_CCI", "SVSB_GPU" 346 }; 347 348 static const char * const svs_type_names[SVSB_TYPE_MAX] = { 349 "", "_LOW", "_HIGH" 350 }; 351 352 enum svs_fusemap_dev { 353 BDEV_BDES, 354 BDEV_MDES, 355 BDEV_MTDES, 356 BDEV_DCBDET, 357 BDEV_DCMDET, 358 BDEV_MAX 359 }; 360 361 enum svs_fusemap_glb { 362 GLB_FT_PGM, 363 GLB_VMIN, 364 GLB_MAX 365 }; 366 367 struct svs_fusemap { 368 s8 index; 369 u8 ofst; 370 }; 371 372 /** 373 * struct svs_platform - svs platform control 374 * @base: svs platform register base 375 * @dev: svs platform device 376 * @main_clk: main clock for svs bank 377 * @banks: svs banks that svs platform supports 378 * @rst: svs platform reset control 379 * @efuse_max: total number of svs efuse 380 * @tefuse_max: total number of thermal efuse 381 * @regs: svs platform registers map 382 * @efuse: svs efuse data received from NVMEM framework 383 * @tefuse: thermal efuse data received from NVMEM framework 384 * @ts_coeff: thermal sensors coefficient 385 * @bank_max: total number of svs banks 386 */ 387 struct svs_platform { 388 void __iomem *base; 389 struct device *dev; 390 struct clk *main_clk; 391 struct svs_bank *banks; 392 struct reset_control *rst; 393 size_t efuse_max; 394 size_t tefuse_max; 395 const u32 *regs; 396 u32 *efuse; 397 u32 *tefuse; 398 u32 ts_coeff; 399 u16 bank_max; 400 }; 401 402 struct svs_platform_data { 403 char *name; 404 struct svs_bank *banks; 405 bool (*efuse_parsing)(struct svs_platform *svsp, const struct svs_platform_data *pdata); 406 int (*probe)(struct svs_platform *svsp); 407 const struct svs_fusemap *glb_fuse_map; 408 const u32 *regs; 409 u32 ts_coeff; 410 u16 bank_max; 411 }; 412 413 /** 414 * struct svs_bank_pdata - SVS Bank immutable config parameters 415 * @dev_fuse_map: Bank fuse map data 416 * @buck_name: Regulator name 417 * @tzone_name: Thermal zone name 418 * @age_config: Bank age configuration 419 * @ctl0: TS-x selection 420 * @dc_config: Bank dc configuration 421 * @int_st: Bank interrupt identification 422 * @turn_freq_base: Reference frequency for 2-line turn point 423 * @tzone_htemp: Thermal zone high temperature threshold 424 * @tzone_ltemp: Thermal zone low temperature threshold 425 * @volt_step: Bank voltage step 426 * @volt_base: Bank voltage base 427 * @tzone_htemp_voffset: Thermal zone high temperature voltage offset 428 * @tzone_ltemp_voffset: Thermal zone low temperature voltage offset 429 * @chk_shift: Bank chicken shift 430 * @cpu_id: CPU core ID for SVS CPU bank use only 431 * @opp_count: Bank opp count 432 * @vboot: Voltage request for bank init01 only 433 * @vco: Bank VCO value 434 * @sw_id: Bank software identification 435 * @type: SVS Bank Type (1 or 2-line) and Role (high/low) 436 * @set_freq_pct: function pointer to set bank frequency percent table 437 * @get_volts: function pointer to get bank voltages 438 */ 439 struct svs_bank_pdata { 440 const struct svs_fusemap *dev_fuse_map; 441 char *buck_name; 442 char *tzone_name; 443 u32 age_config; 444 u32 ctl0; 445 u32 dc_config; 446 u32 int_st; 447 u32 turn_freq_base; 448 u32 tzone_htemp; 449 u32 tzone_ltemp; 450 u32 volt_step; 451 u32 volt_base; 452 u16 tzone_htemp_voffset; 453 u16 tzone_ltemp_voffset; 454 u8 chk_shift; 455 u8 cpu_id; 456 u8 opp_count; 457 u8 vboot; 458 u8 vco; 459 u8 sw_id; 460 u8 type; 461 462 /* Callbacks */ 463 void (*set_freq_pct)(struct svs_platform *svsp, struct svs_bank *svsb); 464 void (*get_volts)(struct svs_platform *svsp, struct svs_bank *svsb); 465 }; 466 467 /** 468 * struct svs_bank - svs bank representation 469 * @pdata: SVS Bank immutable config parameters 470 * @dev: bank device 471 * @opp_dev: device for opp table/buck control 472 * @init_completion: the timeout completion for bank init 473 * @buck: regulator used by opp_dev 474 * @tzd: thermal zone device for getting temperature 475 * @lock: mutex lock to protect voltage update process 476 * @name: bank name 477 * @phase: bank current phase 478 * @volt_od: bank voltage overdrive 479 * @reg_data: bank register data in different phase for debug purpose 480 * @pm_runtime_enabled_count: bank pm runtime enabled count 481 * @mode_support: bank mode support 482 * @freq_base: reference frequency for bank init 483 * @opp_dfreq: default opp frequency table 484 * @opp_dvolt: default opp voltage table 485 * @freq_pct: frequency percent table for bank init 486 * @volt: bank voltage table 487 * @volt_flags: bank voltage flags 488 * @vmax: bank voltage maximum 489 * @vmin: bank voltage minimum 490 * @age_voffset_in: bank age voltage offset 491 * @dc_voffset_in: bank dc voltage offset 492 * @dvt_fixed: bank dvt fixed value 493 * @core_sel: bank selection 494 * @temp: bank temperature 495 * @bts: svs efuse data 496 * @mts: svs efuse data 497 * @bdes: svs efuse data 498 * @mdes: svs efuse data 499 * @mtdes: svs efuse data 500 * @dcbdet: svs efuse data 501 * @dcmdet: svs efuse data 502 * @turn_pt: 2-line turn point tells which opp_volt calculated by high/low bank 503 * @vbin_turn_pt: voltage bin turn point helps know which svsb_volt should be overridden 504 * 505 * Svs bank will generate suitable voltages by below general math equation 506 * and provide these voltages to opp voltage table. 507 * 508 * opp_volt[i] = (volt[i] * volt_step) + volt_base; 509 */ 510 struct svs_bank { 511 const struct svs_bank_pdata pdata; 512 struct device *dev; 513 struct device *opp_dev; 514 struct completion init_completion; 515 struct regulator *buck; 516 struct thermal_zone_device *tzd; 517 struct mutex lock; 518 int pm_runtime_enabled_count; 519 short int volt_od; 520 char *name; 521 enum svsb_phase phase; 522 u32 reg_data[SVSB_PHASE_MAX][SVS_REG_MAX]; 523 u8 mode_support; 524 u32 opp_dfreq[MAX_OPP_ENTRIES]; 525 u32 opp_dvolt[MAX_OPP_ENTRIES]; 526 u32 freq_pct[MAX_OPP_ENTRIES]; 527 u32 volt[MAX_OPP_ENTRIES]; 528 u32 volt_flags; 529 u32 freq_base; 530 u32 turn_pt; 531 u32 vbin_turn_pt; 532 u32 core_sel; 533 u32 temp; 534 u16 age_voffset_in; 535 u16 dc_voffset_in; 536 u8 dvt_fixed; 537 u8 vmax; 538 u8 vmin; 539 u16 bts; 540 u16 mts; 541 u16 bdes; 542 u16 mdes; 543 u8 mtdes; 544 u8 dcbdet; 545 u8 dcmdet; 546 }; 547 548 static u32 percent(u32 numerator, u32 denominator) 549 { 550 /* If not divide 1000, "numerator * 100" will have data overflow. */ 551 numerator /= 1000; 552 denominator /= 1000; 553 554 return DIV_ROUND_UP(numerator * 100, denominator); 555 } 556 557 static u32 svs_readl_relaxed(struct svs_platform *svsp, enum svs_reg_index rg_i) 558 { 559 return readl_relaxed(svsp->base + svsp->regs[rg_i]); 560 } 561 562 static void svs_writel_relaxed(struct svs_platform *svsp, u32 val, 563 enum svs_reg_index rg_i) 564 { 565 writel_relaxed(val, svsp->base + svsp->regs[rg_i]); 566 } 567 568 static void svs_switch_bank(struct svs_platform *svsp, struct svs_bank *svsb) 569 { 570 svs_writel_relaxed(svsp, svsb->core_sel, CORESEL); 571 } 572 573 static u32 svs_bank_volt_to_opp_volt(u32 svsb_volt, u32 svsb_volt_step, 574 u32 svsb_volt_base) 575 { 576 return (svsb_volt * svsb_volt_step) + svsb_volt_base; 577 } 578 579 static u32 svs_opp_volt_to_bank_volt(u32 opp_u_volt, u32 svsb_volt_step, 580 u32 svsb_volt_base) 581 { 582 return (opp_u_volt - svsb_volt_base) / svsb_volt_step; 583 } 584 585 static int svs_sync_bank_volts_from_opp(struct svs_bank *svsb) 586 { 587 const struct svs_bank_pdata *bdata = &svsb->pdata; 588 struct dev_pm_opp *opp; 589 u32 i, opp_u_volt; 590 591 for (i = 0; i < bdata->opp_count; i++) { 592 opp = dev_pm_opp_find_freq_exact(svsb->opp_dev, 593 svsb->opp_dfreq[i], 594 true); 595 if (IS_ERR(opp)) { 596 dev_err(svsb->dev, "cannot find freq = %u (%ld)\n", 597 svsb->opp_dfreq[i], PTR_ERR(opp)); 598 return PTR_ERR(opp); 599 } 600 601 opp_u_volt = dev_pm_opp_get_voltage(opp); 602 svsb->volt[i] = svs_opp_volt_to_bank_volt(opp_u_volt, 603 bdata->volt_step, 604 bdata->volt_base); 605 dev_pm_opp_put(opp); 606 } 607 608 return 0; 609 } 610 611 static int svs_adjust_pm_opp_volts(struct svs_bank *svsb) 612 { 613 int ret = -EPERM, tzone_temp = 0; 614 const struct svs_bank_pdata *bdata = &svsb->pdata; 615 u32 i, svsb_volt, opp_volt, temp_voffset = 0, opp_start, opp_stop; 616 617 mutex_lock(&svsb->lock); 618 619 /* 620 * 2-line bank updates its corresponding opp volts. 621 * 1-line bank updates all opp volts. 622 */ 623 if (bdata->type == SVSB_TYPE_HIGH) { 624 opp_start = 0; 625 opp_stop = svsb->turn_pt; 626 } else if (bdata->type == SVSB_TYPE_LOW) { 627 opp_start = svsb->turn_pt; 628 opp_stop = bdata->opp_count; 629 } else { 630 opp_start = 0; 631 opp_stop = bdata->opp_count; 632 } 633 634 /* Get thermal effect */ 635 if (!IS_ERR_OR_NULL(svsb->tzd)) { 636 ret = thermal_zone_get_temp(svsb->tzd, &tzone_temp); 637 if (ret || (svsb->temp > SVSB_TEMP_UPPER_BOUND && 638 svsb->temp < SVSB_TEMP_LOWER_BOUND)) { 639 dev_err(svsb->dev, "%s: %d (0x%x), run default volts\n", 640 bdata->tzone_name, ret, svsb->temp); 641 svsb->phase = SVSB_PHASE_ERROR; 642 } 643 644 if (tzone_temp >= bdata->tzone_htemp) 645 temp_voffset += bdata->tzone_htemp_voffset; 646 else if (tzone_temp <= bdata->tzone_ltemp) 647 temp_voffset += bdata->tzone_ltemp_voffset; 648 649 /* 2-line bank update all opp volts when running mon mode */ 650 if (svsb->phase == SVSB_PHASE_MON && (bdata->type == SVSB_TYPE_HIGH || 651 bdata->type == SVSB_TYPE_LOW)) { 652 opp_start = 0; 653 opp_stop = bdata->opp_count; 654 } 655 } 656 657 /* vmin <= svsb_volt (opp_volt) <= default opp voltage */ 658 for (i = opp_start; i < opp_stop; i++) { 659 switch (svsb->phase) { 660 case SVSB_PHASE_ERROR: 661 opp_volt = svsb->opp_dvolt[i]; 662 break; 663 case SVSB_PHASE_INIT01: 664 /* do nothing */ 665 goto unlock_mutex; 666 case SVSB_PHASE_INIT02: 667 case SVSB_PHASE_MON: 668 svsb_volt = max(svsb->volt[i] + temp_voffset, svsb->vmin); 669 opp_volt = svs_bank_volt_to_opp_volt(svsb_volt, 670 bdata->volt_step, 671 bdata->volt_base); 672 break; 673 default: 674 dev_err(svsb->dev, "unknown phase: %u\n", svsb->phase); 675 ret = -EINVAL; 676 goto unlock_mutex; 677 } 678 679 opp_volt = min(opp_volt, svsb->opp_dvolt[i]); 680 ret = dev_pm_opp_adjust_voltage(svsb->opp_dev, 681 svsb->opp_dfreq[i], 682 opp_volt, opp_volt, 683 svsb->opp_dvolt[i]); 684 if (ret) { 685 dev_err(svsb->dev, "set %uuV fail: %d\n", 686 opp_volt, ret); 687 goto unlock_mutex; 688 } 689 } 690 691 unlock_mutex: 692 mutex_unlock(&svsb->lock); 693 694 return ret; 695 } 696 697 static void svs_bank_disable_and_restore_default_volts(struct svs_platform *svsp, 698 struct svs_bank *svsb) 699 { 700 unsigned long flags; 701 702 if (svsb->mode_support == SVSB_MODE_ALL_DISABLE) 703 return; 704 705 spin_lock_irqsave(&svs_lock, flags); 706 svs_switch_bank(svsp, svsb); 707 svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN); 708 svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS); 709 spin_unlock_irqrestore(&svs_lock, flags); 710 711 svsb->phase = SVSB_PHASE_ERROR; 712 svs_adjust_pm_opp_volts(svsb); 713 } 714 715 #ifdef CONFIG_DEBUG_FS 716 static int svs_dump_debug_show(struct seq_file *m, void *p) 717 { 718 struct svs_platform *svsp = (struct svs_platform *)m->private; 719 struct svs_bank *svsb; 720 unsigned long svs_reg_addr; 721 u32 idx, i, j, bank_id; 722 723 for (i = 0; i < svsp->efuse_max; i++) 724 if (svsp->efuse && svsp->efuse[i]) 725 seq_printf(m, "M_HW_RES%d = 0x%08x\n", 726 i, svsp->efuse[i]); 727 728 for (i = 0; i < svsp->tefuse_max; i++) 729 if (svsp->tefuse) 730 seq_printf(m, "THERMAL_EFUSE%d = 0x%08x\n", 731 i, svsp->tefuse[i]); 732 733 for (bank_id = 0, idx = 0; idx < svsp->bank_max; idx++, bank_id++) { 734 svsb = &svsp->banks[idx]; 735 736 for (i = SVSB_PHASE_INIT01; i <= SVSB_PHASE_MON; i++) { 737 seq_printf(m, "Bank_number = %u\n", bank_id); 738 739 if (i == SVSB_PHASE_INIT01 || i == SVSB_PHASE_INIT02) 740 seq_printf(m, "mode = init%d\n", i); 741 else if (i == SVSB_PHASE_MON) 742 seq_puts(m, "mode = mon\n"); 743 else 744 seq_puts(m, "mode = error\n"); 745 746 for (j = DESCHAR; j < SVS_REG_MAX; j++) { 747 svs_reg_addr = (unsigned long)(svsp->base + 748 svsp->regs[j]); 749 seq_printf(m, "0x%08lx = 0x%08x\n", 750 svs_reg_addr, svsb->reg_data[i][j]); 751 } 752 } 753 } 754 755 return 0; 756 } 757 758 debug_fops_ro(dump); 759 760 static int svs_enable_debug_show(struct seq_file *m, void *v) 761 { 762 struct svs_bank *svsb = (struct svs_bank *)m->private; 763 764 switch (svsb->phase) { 765 case SVSB_PHASE_ERROR: 766 seq_puts(m, "disabled\n"); 767 break; 768 case SVSB_PHASE_INIT01: 769 seq_puts(m, "init1\n"); 770 break; 771 case SVSB_PHASE_INIT02: 772 seq_puts(m, "init2\n"); 773 break; 774 case SVSB_PHASE_MON: 775 seq_puts(m, "mon mode\n"); 776 break; 777 default: 778 seq_puts(m, "unknown\n"); 779 break; 780 } 781 782 return 0; 783 } 784 785 static ssize_t svs_enable_debug_write(struct file *filp, 786 const char __user *buffer, 787 size_t count, loff_t *pos) 788 { 789 struct svs_bank *svsb = file_inode(filp)->i_private; 790 struct svs_platform *svsp = dev_get_drvdata(svsb->dev); 791 int enabled, ret; 792 char *buf = NULL; 793 794 if (count >= PAGE_SIZE) 795 return -EINVAL; 796 797 buf = (char *)memdup_user_nul(buffer, count); 798 if (IS_ERR(buf)) 799 return PTR_ERR(buf); 800 801 ret = kstrtoint(buf, 10, &enabled); 802 if (ret) 803 return ret; 804 805 if (!enabled) { 806 svs_bank_disable_and_restore_default_volts(svsp, svsb); 807 svsb->mode_support = SVSB_MODE_ALL_DISABLE; 808 } 809 810 kfree(buf); 811 812 return count; 813 } 814 815 debug_fops_rw(enable); 816 817 static int svs_status_debug_show(struct seq_file *m, void *v) 818 { 819 struct svs_bank *svsb = (struct svs_bank *)m->private; 820 struct dev_pm_opp *opp; 821 int tzone_temp = 0, ret; 822 u32 i; 823 824 ret = thermal_zone_get_temp(svsb->tzd, &tzone_temp); 825 if (ret) 826 seq_printf(m, "%s: temperature ignore, vbin_turn_pt = %u, turn_pt = %u\n", 827 svsb->name, svsb->vbin_turn_pt, svsb->turn_pt); 828 else 829 seq_printf(m, "%s: temperature = %d, vbin_turn_pt = %u, turn_pt = %u\n", 830 svsb->name, tzone_temp, svsb->vbin_turn_pt, 831 svsb->turn_pt); 832 833 for (i = 0; i < svsb->pdata.opp_count; i++) { 834 opp = dev_pm_opp_find_freq_exact(svsb->opp_dev, 835 svsb->opp_dfreq[i], true); 836 if (IS_ERR(opp)) { 837 seq_printf(m, "%s: cannot find freq = %u (%ld)\n", 838 svsb->name, svsb->opp_dfreq[i], 839 PTR_ERR(opp)); 840 return PTR_ERR(opp); 841 } 842 843 seq_printf(m, "opp_freq[%02u]: %u, opp_volt[%02u]: %lu, ", 844 i, svsb->opp_dfreq[i], i, 845 dev_pm_opp_get_voltage(opp)); 846 seq_printf(m, "svsb_volt[%02u]: 0x%x, freq_pct[%02u]: %u\n", 847 i, svsb->volt[i], i, svsb->freq_pct[i]); 848 dev_pm_opp_put(opp); 849 } 850 851 return 0; 852 } 853 854 debug_fops_ro(status); 855 856 static int svs_create_debug_cmds(struct svs_platform *svsp) 857 { 858 struct svs_bank *svsb; 859 struct dentry *svs_dir, *svsb_dir, *file_entry; 860 const char *d = "/sys/kernel/debug/svs"; 861 u32 i, idx; 862 863 struct svs_dentry { 864 const char *name; 865 const struct file_operations *fops; 866 }; 867 868 struct svs_dentry svs_entries[] = { 869 svs_dentry_data(dump), 870 }; 871 872 struct svs_dentry svsb_entries[] = { 873 svs_dentry_data(enable), 874 svs_dentry_data(status), 875 }; 876 877 svs_dir = debugfs_create_dir("svs", NULL); 878 if (IS_ERR(svs_dir)) { 879 dev_err(svsp->dev, "cannot create %s: %ld\n", 880 d, PTR_ERR(svs_dir)); 881 return PTR_ERR(svs_dir); 882 } 883 884 for (i = 0; i < ARRAY_SIZE(svs_entries); i++) { 885 file_entry = debugfs_create_file(svs_entries[i].name, 0664, 886 svs_dir, svsp, 887 svs_entries[i].fops); 888 if (IS_ERR(file_entry)) { 889 dev_err(svsp->dev, "cannot create %s/%s: %ld\n", 890 d, svs_entries[i].name, PTR_ERR(file_entry)); 891 return PTR_ERR(file_entry); 892 } 893 } 894 895 for (idx = 0; idx < svsp->bank_max; idx++) { 896 svsb = &svsp->banks[idx]; 897 898 if (svsb->mode_support == SVSB_MODE_ALL_DISABLE) 899 continue; 900 901 svsb_dir = debugfs_create_dir(svsb->name, svs_dir); 902 if (IS_ERR(svsb_dir)) { 903 dev_err(svsp->dev, "cannot create %s/%s: %ld\n", 904 d, svsb->name, PTR_ERR(svsb_dir)); 905 return PTR_ERR(svsb_dir); 906 } 907 908 for (i = 0; i < ARRAY_SIZE(svsb_entries); i++) { 909 file_entry = debugfs_create_file(svsb_entries[i].name, 910 0664, svsb_dir, svsb, 911 svsb_entries[i].fops); 912 if (IS_ERR(file_entry)) { 913 dev_err(svsp->dev, "no %s/%s/%s?: %ld\n", 914 d, svsb->name, svsb_entries[i].name, 915 PTR_ERR(file_entry)); 916 return PTR_ERR(file_entry); 917 } 918 } 919 } 920 921 return 0; 922 } 923 #endif /* CONFIG_DEBUG_FS */ 924 925 static u32 interpolate(u32 f0, u32 f1, u32 v0, u32 v1, u32 fx) 926 { 927 u32 vx; 928 929 if (v0 == v1 || f0 == f1) 930 return v0; 931 932 /* *100 to have decimal fraction factor */ 933 vx = (v0 * 100) - ((((v0 - v1) * 100) / (f0 - f1)) * (f0 - fx)); 934 935 return DIV_ROUND_UP(vx, 100); 936 } 937 938 static void svs_get_bank_volts_v3(struct svs_platform *svsp, struct svs_bank *svsb) 939 { 940 const struct svs_bank_pdata *bdata = &svsb->pdata; 941 u32 i, j, *vop, vop74, vop30, turn_pt = svsb->turn_pt; 942 u32 b_sft, shift_byte = 0, opp_start = 0, opp_stop = 0; 943 u32 middle_index = (bdata->opp_count / 2); 944 945 if (svsb->phase == SVSB_PHASE_MON && 946 svsb->volt_flags & SVSB_MON_VOLT_IGNORE) 947 return; 948 949 vop74 = svs_readl_relaxed(svsp, VOP74); 950 vop30 = svs_readl_relaxed(svsp, VOP30); 951 952 /* Target is to set svsb->volt[] by algorithm */ 953 if (turn_pt < middle_index) { 954 if (bdata->type == SVSB_TYPE_HIGH) { 955 /* volt[0] ~ volt[turn_pt - 1] */ 956 for (i = 0; i < turn_pt; i++) { 957 b_sft = BITS8 * (shift_byte % REG_BYTES); 958 vop = (shift_byte < REG_BYTES) ? &vop30 : 959 &vop74; 960 svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0); 961 shift_byte++; 962 } 963 } else if (bdata->type == SVSB_TYPE_LOW) { 964 /* volt[turn_pt] + volt[j] ~ volt[opp_count - 1] */ 965 j = bdata->opp_count - 7; 966 svsb->volt[turn_pt] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, vop30); 967 shift_byte++; 968 for (i = j; i < bdata->opp_count; i++) { 969 b_sft = BITS8 * (shift_byte % REG_BYTES); 970 vop = (shift_byte < REG_BYTES) ? &vop30 : 971 &vop74; 972 svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0); 973 shift_byte++; 974 } 975 976 /* volt[turn_pt + 1] ~ volt[j - 1] by interpolate */ 977 for (i = turn_pt + 1; i < j; i++) 978 svsb->volt[i] = interpolate(svsb->freq_pct[turn_pt], 979 svsb->freq_pct[j], 980 svsb->volt[turn_pt], 981 svsb->volt[j], 982 svsb->freq_pct[i]); 983 } 984 } else { 985 if (bdata->type == SVSB_TYPE_HIGH) { 986 /* volt[0] + volt[j] ~ volt[turn_pt - 1] */ 987 j = turn_pt - 7; 988 svsb->volt[0] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, vop30); 989 shift_byte++; 990 for (i = j; i < turn_pt; i++) { 991 b_sft = BITS8 * (shift_byte % REG_BYTES); 992 vop = (shift_byte < REG_BYTES) ? &vop30 : 993 &vop74; 994 svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0); 995 shift_byte++; 996 } 997 998 /* volt[1] ~ volt[j - 1] by interpolate */ 999 for (i = 1; i < j; i++) 1000 svsb->volt[i] = interpolate(svsb->freq_pct[0], 1001 svsb->freq_pct[j], 1002 svsb->volt[0], 1003 svsb->volt[j], 1004 svsb->freq_pct[i]); 1005 } else if (bdata->type == SVSB_TYPE_LOW) { 1006 /* volt[turn_pt] ~ volt[opp_count - 1] */ 1007 for (i = turn_pt; i < bdata->opp_count; i++) { 1008 b_sft = BITS8 * (shift_byte % REG_BYTES); 1009 vop = (shift_byte < REG_BYTES) ? &vop30 : 1010 &vop74; 1011 svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0); 1012 shift_byte++; 1013 } 1014 } 1015 } 1016 1017 if (bdata->type == SVSB_TYPE_HIGH) { 1018 opp_start = 0; 1019 opp_stop = svsb->turn_pt; 1020 } else if (bdata->type == SVSB_TYPE_LOW) { 1021 opp_start = svsb->turn_pt; 1022 opp_stop = bdata->opp_count; 1023 } 1024 1025 for (i = opp_start; i < opp_stop; i++) 1026 if (svsb->volt_flags & SVSB_REMOVE_DVTFIXED_VOLT) 1027 svsb->volt[i] -= svsb->dvt_fixed; 1028 1029 /* For voltage bin support */ 1030 if (svsb->opp_dfreq[0] > svsb->freq_base) { 1031 svsb->volt[0] = svs_opp_volt_to_bank_volt(svsb->opp_dvolt[0], 1032 bdata->volt_step, 1033 bdata->volt_base); 1034 1035 /* Find voltage bin turn point */ 1036 for (i = 0; i < bdata->opp_count; i++) { 1037 if (svsb->opp_dfreq[i] <= svsb->freq_base) { 1038 svsb->vbin_turn_pt = i; 1039 break; 1040 } 1041 } 1042 1043 /* Override svs bank voltages */ 1044 for (i = 1; i < svsb->vbin_turn_pt; i++) 1045 svsb->volt[i] = interpolate(svsb->freq_pct[0], 1046 svsb->freq_pct[svsb->vbin_turn_pt], 1047 svsb->volt[0], 1048 svsb->volt[svsb->vbin_turn_pt], 1049 svsb->freq_pct[i]); 1050 } 1051 } 1052 1053 static void svs_set_bank_freq_pct_v3(struct svs_platform *svsp, struct svs_bank *svsb) 1054 { 1055 const struct svs_bank_pdata *bdata = &svsb->pdata; 1056 u32 i, j, *freq_pct, freq_pct74 = 0, freq_pct30 = 0; 1057 u32 b_sft, shift_byte = 0, turn_pt; 1058 u32 middle_index = (bdata->opp_count / 2); 1059 1060 for (i = 0; i < bdata->opp_count; i++) { 1061 if (svsb->opp_dfreq[i] <= bdata->turn_freq_base) { 1062 svsb->turn_pt = i; 1063 break; 1064 } 1065 } 1066 1067 turn_pt = svsb->turn_pt; 1068 1069 /* Target is to fill out freq_pct74 / freq_pct30 by algorithm */ 1070 if (turn_pt < middle_index) { 1071 if (bdata->type == SVSB_TYPE_HIGH) { 1072 /* 1073 * If we don't handle this situation, 1074 * SVSB_TYPE_HIGH's FREQPCT74 / FREQPCT30 would keep "0" 1075 * and this leads SVSB_TYPE_LOW to work abnormally. 1076 */ 1077 if (turn_pt == 0) 1078 freq_pct30 = svsb->freq_pct[0]; 1079 1080 /* freq_pct[0] ~ freq_pct[turn_pt - 1] */ 1081 for (i = 0; i < turn_pt; i++) { 1082 b_sft = BITS8 * (shift_byte % REG_BYTES); 1083 freq_pct = (shift_byte < REG_BYTES) ? 1084 &freq_pct30 : &freq_pct74; 1085 *freq_pct |= (svsb->freq_pct[i] << b_sft); 1086 shift_byte++; 1087 } 1088 } else if (bdata->type == SVSB_TYPE_LOW) { 1089 /* 1090 * freq_pct[turn_pt] + 1091 * freq_pct[opp_count - 7] ~ freq_pct[opp_count -1] 1092 */ 1093 freq_pct30 = svsb->freq_pct[turn_pt]; 1094 shift_byte++; 1095 j = bdata->opp_count - 7; 1096 for (i = j; i < bdata->opp_count; i++) { 1097 b_sft = BITS8 * (shift_byte % REG_BYTES); 1098 freq_pct = (shift_byte < REG_BYTES) ? 1099 &freq_pct30 : &freq_pct74; 1100 *freq_pct |= (svsb->freq_pct[i] << b_sft); 1101 shift_byte++; 1102 } 1103 } 1104 } else { 1105 if (bdata->type == SVSB_TYPE_HIGH) { 1106 /* 1107 * freq_pct[0] + 1108 * freq_pct[turn_pt - 7] ~ freq_pct[turn_pt - 1] 1109 */ 1110 freq_pct30 = svsb->freq_pct[0]; 1111 shift_byte++; 1112 j = turn_pt - 7; 1113 for (i = j; i < turn_pt; i++) { 1114 b_sft = BITS8 * (shift_byte % REG_BYTES); 1115 freq_pct = (shift_byte < REG_BYTES) ? 1116 &freq_pct30 : &freq_pct74; 1117 *freq_pct |= (svsb->freq_pct[i] << b_sft); 1118 shift_byte++; 1119 } 1120 } else if (bdata->type == SVSB_TYPE_LOW) { 1121 /* freq_pct[turn_pt] ~ freq_pct[opp_count - 1] */ 1122 for (i = turn_pt; i < bdata->opp_count; i++) { 1123 b_sft = BITS8 * (shift_byte % REG_BYTES); 1124 freq_pct = (shift_byte < REG_BYTES) ? 1125 &freq_pct30 : &freq_pct74; 1126 *freq_pct |= (svsb->freq_pct[i] << b_sft); 1127 shift_byte++; 1128 } 1129 } 1130 } 1131 1132 svs_writel_relaxed(svsp, freq_pct74, FREQPCT74); 1133 svs_writel_relaxed(svsp, freq_pct30, FREQPCT30); 1134 } 1135 1136 static void svs_get_bank_volts_v2(struct svs_platform *svsp, struct svs_bank *svsb) 1137 { 1138 const struct svs_bank_pdata *bdata = &svsb->pdata; 1139 u32 temp, i; 1140 1141 temp = svs_readl_relaxed(svsp, VOP74); 1142 svsb->volt[14] = FIELD_GET(SVSB_VOPS_FLD_VOP3_7, temp); 1143 svsb->volt[12] = FIELD_GET(SVSB_VOPS_FLD_VOP2_6, temp); 1144 svsb->volt[10] = FIELD_GET(SVSB_VOPS_FLD_VOP1_5, temp); 1145 svsb->volt[8] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, temp); 1146 1147 temp = svs_readl_relaxed(svsp, VOP30); 1148 svsb->volt[6] = FIELD_GET(SVSB_VOPS_FLD_VOP3_7, temp); 1149 svsb->volt[4] = FIELD_GET(SVSB_VOPS_FLD_VOP2_6, temp); 1150 svsb->volt[2] = FIELD_GET(SVSB_VOPS_FLD_VOP1_5, temp); 1151 svsb->volt[0] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, temp); 1152 1153 for (i = 0; i <= 12; i += 2) 1154 svsb->volt[i + 1] = interpolate(svsb->freq_pct[i], 1155 svsb->freq_pct[i + 2], 1156 svsb->volt[i], 1157 svsb->volt[i + 2], 1158 svsb->freq_pct[i + 1]); 1159 1160 svsb->volt[15] = interpolate(svsb->freq_pct[12], 1161 svsb->freq_pct[14], 1162 svsb->volt[12], 1163 svsb->volt[14], 1164 svsb->freq_pct[15]); 1165 1166 for (i = 0; i < bdata->opp_count; i++) 1167 svsb->volt[i] += svsb->volt_od; 1168 1169 /* For voltage bin support */ 1170 if (svsb->opp_dfreq[0] > svsb->freq_base) { 1171 svsb->volt[0] = svs_opp_volt_to_bank_volt(svsb->opp_dvolt[0], 1172 bdata->volt_step, 1173 bdata->volt_base); 1174 1175 /* Find voltage bin turn point */ 1176 for (i = 0; i < bdata->opp_count; i++) { 1177 if (svsb->opp_dfreq[i] <= svsb->freq_base) { 1178 svsb->vbin_turn_pt = i; 1179 break; 1180 } 1181 } 1182 1183 /* Override svs bank voltages */ 1184 for (i = 1; i < svsb->vbin_turn_pt; i++) 1185 svsb->volt[i] = interpolate(svsb->freq_pct[0], 1186 svsb->freq_pct[svsb->vbin_turn_pt], 1187 svsb->volt[0], 1188 svsb->volt[svsb->vbin_turn_pt], 1189 svsb->freq_pct[i]); 1190 } 1191 } 1192 1193 static void svs_set_bank_freq_pct_v2(struct svs_platform *svsp, struct svs_bank *svsb) 1194 { 1195 u32 freqpct74_val, freqpct30_val; 1196 1197 freqpct74_val = FIELD_PREP(SVSB_FREQPCTS_FLD_PCT0_4, svsb->freq_pct[8]) | 1198 FIELD_PREP(SVSB_FREQPCTS_FLD_PCT1_5, svsb->freq_pct[10]) | 1199 FIELD_PREP(SVSB_FREQPCTS_FLD_PCT2_6, svsb->freq_pct[12]) | 1200 FIELD_PREP(SVSB_FREQPCTS_FLD_PCT3_7, svsb->freq_pct[14]); 1201 1202 freqpct30_val = FIELD_PREP(SVSB_FREQPCTS_FLD_PCT0_4, svsb->freq_pct[0]) | 1203 FIELD_PREP(SVSB_FREQPCTS_FLD_PCT1_5, svsb->freq_pct[2]) | 1204 FIELD_PREP(SVSB_FREQPCTS_FLD_PCT2_6, svsb->freq_pct[4]) | 1205 FIELD_PREP(SVSB_FREQPCTS_FLD_PCT3_7, svsb->freq_pct[6]); 1206 1207 svs_writel_relaxed(svsp, freqpct74_val, FREQPCT74); 1208 svs_writel_relaxed(svsp, freqpct30_val, FREQPCT30); 1209 } 1210 1211 static void svs_set_bank_phase(struct svs_platform *svsp, 1212 unsigned int bank_idx, 1213 enum svsb_phase target_phase) 1214 { 1215 struct svs_bank *svsb = &svsp->banks[bank_idx]; 1216 const struct svs_bank_pdata *bdata = &svsb->pdata; 1217 u32 des_char, temp_char, det_char, limit_vals, init2vals, ts_calcs; 1218 1219 svs_switch_bank(svsp, svsb); 1220 1221 des_char = FIELD_PREP(SVSB_DESCHAR_FLD_BDES, svsb->bdes) | 1222 FIELD_PREP(SVSB_DESCHAR_FLD_MDES, svsb->mdes); 1223 svs_writel_relaxed(svsp, des_char, DESCHAR); 1224 1225 temp_char = FIELD_PREP(SVSB_TEMPCHAR_FLD_VCO, bdata->vco) | 1226 FIELD_PREP(SVSB_TEMPCHAR_FLD_MTDES, svsb->mtdes) | 1227 FIELD_PREP(SVSB_TEMPCHAR_FLD_DVT_FIXED, svsb->dvt_fixed); 1228 svs_writel_relaxed(svsp, temp_char, TEMPCHAR); 1229 1230 det_char = FIELD_PREP(SVSB_DETCHAR_FLD_DCBDET, svsb->dcbdet) | 1231 FIELD_PREP(SVSB_DETCHAR_FLD_DCMDET, svsb->dcmdet); 1232 svs_writel_relaxed(svsp, det_char, DETCHAR); 1233 1234 svs_writel_relaxed(svsp, bdata->dc_config, DCCONFIG); 1235 svs_writel_relaxed(svsp, bdata->age_config, AGECONFIG); 1236 svs_writel_relaxed(svsp, SVSB_RUNCONFIG_DEFAULT, RUNCONFIG); 1237 1238 bdata->set_freq_pct(svsp, svsb); 1239 1240 limit_vals = FIELD_PREP(SVSB_LIMITVALS_FLD_DTLO, SVSB_VAL_DTLO) | 1241 FIELD_PREP(SVSB_LIMITVALS_FLD_DTHI, SVSB_VAL_DTHI) | 1242 FIELD_PREP(SVSB_LIMITVALS_FLD_VMIN, svsb->vmin) | 1243 FIELD_PREP(SVSB_LIMITVALS_FLD_VMAX, svsb->vmax); 1244 svs_writel_relaxed(svsp, limit_vals, LIMITVALS); 1245 1246 svs_writel_relaxed(svsp, SVSB_DET_WINDOW, DETWINDOW); 1247 svs_writel_relaxed(svsp, SVSB_DET_MAX, CONFIG); 1248 svs_writel_relaxed(svsp, bdata->chk_shift, CHKSHIFT); 1249 svs_writel_relaxed(svsp, bdata->ctl0, CTL0); 1250 svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS); 1251 1252 switch (target_phase) { 1253 case SVSB_PHASE_INIT01: 1254 svs_writel_relaxed(svsp, bdata->vboot, VBOOT); 1255 svs_writel_relaxed(svsp, SVSB_INTEN_INIT0x, INTEN); 1256 svs_writel_relaxed(svsp, SVSB_PTPEN_INIT01, SVSEN); 1257 break; 1258 case SVSB_PHASE_INIT02: 1259 init2vals = FIELD_PREP(SVSB_INIT2VALS_FLD_AGEVOFFSETIN, svsb->age_voffset_in) | 1260 FIELD_PREP(SVSB_INIT2VALS_FLD_DCVOFFSETIN, svsb->dc_voffset_in); 1261 svs_writel_relaxed(svsp, SVSB_INTEN_INIT0x, INTEN); 1262 svs_writel_relaxed(svsp, init2vals, INIT2VALS); 1263 svs_writel_relaxed(svsp, SVSB_PTPEN_INIT02, SVSEN); 1264 break; 1265 case SVSB_PHASE_MON: 1266 ts_calcs = FIELD_PREP(SVSB_TSCALCS_FLD_BTS, svsb->bts) | 1267 FIELD_PREP(SVSB_TSCALCS_FLD_MTS, svsb->mts); 1268 svs_writel_relaxed(svsp, ts_calcs, TSCALCS); 1269 svs_writel_relaxed(svsp, SVSB_INTEN_MONVOPEN, INTEN); 1270 svs_writel_relaxed(svsp, SVSB_PTPEN_MON, SVSEN); 1271 break; 1272 default: 1273 dev_err(svsb->dev, "requested unknown target phase: %u\n", 1274 target_phase); 1275 break; 1276 } 1277 } 1278 1279 static inline void svs_save_bank_register_data(struct svs_platform *svsp, 1280 unsigned short bank_idx, 1281 enum svsb_phase phase) 1282 { 1283 struct svs_bank *svsb = &svsp->banks[bank_idx]; 1284 enum svs_reg_index rg_i; 1285 1286 for (rg_i = DESCHAR; rg_i < SVS_REG_MAX; rg_i++) 1287 svsb->reg_data[phase][rg_i] = svs_readl_relaxed(svsp, rg_i); 1288 } 1289 1290 static inline void svs_error_isr_handler(struct svs_platform *svsp, 1291 unsigned short bank_idx) 1292 { 1293 struct svs_bank *svsb = &svsp->banks[bank_idx]; 1294 1295 dev_err(svsb->dev, "%s: CORESEL = 0x%08x\n", 1296 __func__, svs_readl_relaxed(svsp, CORESEL)); 1297 dev_err(svsb->dev, "SVSEN = 0x%08x, INTSTS = 0x%08x\n", 1298 svs_readl_relaxed(svsp, SVSEN), 1299 svs_readl_relaxed(svsp, INTSTS)); 1300 dev_err(svsb->dev, "SMSTATE0 = 0x%08x, SMSTATE1 = 0x%08x\n", 1301 svs_readl_relaxed(svsp, SMSTATE0), 1302 svs_readl_relaxed(svsp, SMSTATE1)); 1303 dev_err(svsb->dev, "TEMP = 0x%08x\n", svs_readl_relaxed(svsp, TEMP)); 1304 1305 svs_save_bank_register_data(svsp, bank_idx, SVSB_PHASE_ERROR); 1306 1307 svsb->phase = SVSB_PHASE_ERROR; 1308 svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN); 1309 svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS); 1310 } 1311 1312 static inline void svs_init01_isr_handler(struct svs_platform *svsp, 1313 unsigned short bank_idx) 1314 { 1315 struct svs_bank *svsb = &svsp->banks[bank_idx]; 1316 u32 val; 1317 1318 dev_info(svsb->dev, "%s: VDN74~30:0x%08x~0x%08x, DC:0x%08x\n", 1319 __func__, svs_readl_relaxed(svsp, VDESIGN74), 1320 svs_readl_relaxed(svsp, VDESIGN30), 1321 svs_readl_relaxed(svsp, DCVALUES)); 1322 1323 svs_save_bank_register_data(svsp, bank_idx, SVSB_PHASE_INIT01); 1324 1325 svsb->phase = SVSB_PHASE_INIT01; 1326 val = ~(svs_readl_relaxed(svsp, DCVALUES) & GENMASK(15, 0)) + 1; 1327 svsb->dc_voffset_in = val & GENMASK(15, 0); 1328 if (svsb->volt_flags & SVSB_INIT01_VOLT_IGNORE || 1329 (svsb->dc_voffset_in & SVSB_DC_SIGNED_BIT && 1330 svsb->volt_flags & SVSB_INIT01_VOLT_INC_ONLY)) 1331 svsb->dc_voffset_in = 0; 1332 1333 svsb->age_voffset_in = svs_readl_relaxed(svsp, AGEVALUES) & 1334 GENMASK(15, 0); 1335 1336 svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN); 1337 svs_writel_relaxed(svsp, SVSB_INTSTS_F0_COMPLETE, INTSTS); 1338 svsb->core_sel &= ~SVSB_DET_CLK_EN; 1339 } 1340 1341 static inline void svs_init02_isr_handler(struct svs_platform *svsp, 1342 unsigned short bank_idx) 1343 { 1344 struct svs_bank *svsb = &svsp->banks[bank_idx]; 1345 const struct svs_bank_pdata *bdata = &svsb->pdata; 1346 1347 dev_info(svsb->dev, "%s: VOP74~30:0x%08x~0x%08x, DC:0x%08x\n", 1348 __func__, svs_readl_relaxed(svsp, VOP74), 1349 svs_readl_relaxed(svsp, VOP30), 1350 svs_readl_relaxed(svsp, DCVALUES)); 1351 1352 svs_save_bank_register_data(svsp, bank_idx, SVSB_PHASE_INIT02); 1353 1354 svsb->phase = SVSB_PHASE_INIT02; 1355 bdata->get_volts(svsp, svsb); 1356 1357 svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN); 1358 svs_writel_relaxed(svsp, SVSB_INTSTS_F0_COMPLETE, INTSTS); 1359 } 1360 1361 static inline void svs_mon_mode_isr_handler(struct svs_platform *svsp, 1362 unsigned short bank_idx) 1363 { 1364 struct svs_bank *svsb = &svsp->banks[bank_idx]; 1365 const struct svs_bank_pdata *bdata = &svsb->pdata; 1366 1367 svs_save_bank_register_data(svsp, bank_idx, SVSB_PHASE_MON); 1368 1369 svsb->phase = SVSB_PHASE_MON; 1370 bdata->get_volts(svsp, svsb); 1371 1372 svsb->temp = svs_readl_relaxed(svsp, TEMP) & GENMASK(7, 0); 1373 svs_writel_relaxed(svsp, SVSB_INTSTS_FLD_MONVOP, INTSTS); 1374 } 1375 1376 static irqreturn_t svs_isr(int irq, void *data) 1377 { 1378 struct svs_platform *svsp = data; 1379 const struct svs_bank_pdata *bdata; 1380 struct svs_bank *svsb = NULL; 1381 unsigned long flags; 1382 u32 idx, int_sts, svs_en; 1383 1384 for (idx = 0; idx < svsp->bank_max; idx++) { 1385 svsb = &svsp->banks[idx]; 1386 bdata = &svsb->pdata; 1387 WARN(!svsb, "%s: svsb(%s) is null", __func__, svsb->name); 1388 1389 spin_lock_irqsave(&svs_lock, flags); 1390 1391 /* Find out which svs bank fires interrupt */ 1392 if (bdata->int_st & svs_readl_relaxed(svsp, INTST)) { 1393 spin_unlock_irqrestore(&svs_lock, flags); 1394 continue; 1395 } 1396 1397 svs_switch_bank(svsp, svsb); 1398 int_sts = svs_readl_relaxed(svsp, INTSTS); 1399 svs_en = svs_readl_relaxed(svsp, SVSEN); 1400 1401 if (int_sts == SVSB_INTSTS_F0_COMPLETE && 1402 svs_en == SVSB_PTPEN_INIT01) 1403 svs_init01_isr_handler(svsp, idx); 1404 else if (int_sts == SVSB_INTSTS_F0_COMPLETE && 1405 svs_en == SVSB_PTPEN_INIT02) 1406 svs_init02_isr_handler(svsp, idx); 1407 else if (int_sts & SVSB_INTSTS_FLD_MONVOP) 1408 svs_mon_mode_isr_handler(svsp, idx); 1409 else 1410 svs_error_isr_handler(svsp, idx); 1411 1412 spin_unlock_irqrestore(&svs_lock, flags); 1413 break; 1414 } 1415 1416 svs_adjust_pm_opp_volts(svsb); 1417 1418 if (svsb->phase == SVSB_PHASE_INIT01 || 1419 svsb->phase == SVSB_PHASE_INIT02) 1420 complete(&svsb->init_completion); 1421 1422 return IRQ_HANDLED; 1423 } 1424 1425 static bool svs_mode_available(struct svs_platform *svsp, u8 mode) 1426 { 1427 int i; 1428 1429 for (i = 0; i < svsp->bank_max; i++) 1430 if (svsp->banks[i].mode_support & mode) 1431 return true; 1432 return false; 1433 } 1434 1435 static int svs_init01(struct svs_platform *svsp) 1436 { 1437 const struct svs_bank_pdata *bdata; 1438 struct svs_bank *svsb; 1439 unsigned long flags, time_left; 1440 bool search_done; 1441 int ret = 0, r; 1442 u32 opp_freq, opp_vboot, buck_volt, idx, i; 1443 1444 if (!svs_mode_available(svsp, SVSB_MODE_INIT01)) 1445 return 0; 1446 1447 /* Keep CPUs' core power on for svs_init01 initialization */ 1448 cpuidle_pause_and_lock(); 1449 1450 /* Svs bank init01 preparation - power enable */ 1451 for (idx = 0; idx < svsp->bank_max; idx++) { 1452 svsb = &svsp->banks[idx]; 1453 bdata = &svsb->pdata; 1454 1455 if (!(svsb->mode_support & SVSB_MODE_INIT01)) 1456 continue; 1457 1458 ret = regulator_enable(svsb->buck); 1459 if (ret) { 1460 dev_err(svsb->dev, "%s enable fail: %d\n", 1461 bdata->buck_name, ret); 1462 goto svs_init01_resume_cpuidle; 1463 } 1464 1465 /* Some buck doesn't support mode change. Show fail msg only */ 1466 ret = regulator_set_mode(svsb->buck, REGULATOR_MODE_FAST); 1467 if (ret) 1468 dev_notice(svsb->dev, "set fast mode fail: %d\n", ret); 1469 1470 if (svsb->volt_flags & SVSB_INIT01_PD_REQ) { 1471 if (!pm_runtime_enabled(svsb->opp_dev)) { 1472 pm_runtime_enable(svsb->opp_dev); 1473 svsb->pm_runtime_enabled_count++; 1474 } 1475 1476 ret = pm_runtime_resume_and_get(svsb->opp_dev); 1477 if (ret < 0) { 1478 dev_err(svsb->dev, "mtcmos on fail: %d\n", ret); 1479 goto svs_init01_resume_cpuidle; 1480 } 1481 } 1482 } 1483 1484 /* 1485 * Svs bank init01 preparation - vboot voltage adjustment 1486 * Sometimes two svs banks use the same buck. Therefore, 1487 * we have to set each svs bank to target voltage(vboot) first. 1488 */ 1489 for (idx = 0; idx < svsp->bank_max; idx++) { 1490 svsb = &svsp->banks[idx]; 1491 bdata = &svsb->pdata; 1492 1493 if (!(svsb->mode_support & SVSB_MODE_INIT01)) 1494 continue; 1495 1496 /* 1497 * Find the fastest freq that can be run at vboot and 1498 * fix to that freq until svs_init01 is done. 1499 */ 1500 search_done = false; 1501 opp_vboot = svs_bank_volt_to_opp_volt(bdata->vboot, 1502 bdata->volt_step, 1503 bdata->volt_base); 1504 1505 for (i = 0; i < bdata->opp_count; i++) { 1506 opp_freq = svsb->opp_dfreq[i]; 1507 if (!search_done && svsb->opp_dvolt[i] <= opp_vboot) { 1508 ret = dev_pm_opp_adjust_voltage(svsb->opp_dev, 1509 opp_freq, 1510 opp_vboot, 1511 opp_vboot, 1512 opp_vboot); 1513 if (ret) { 1514 dev_err(svsb->dev, 1515 "set opp %uuV vboot fail: %d\n", 1516 opp_vboot, ret); 1517 goto svs_init01_finish; 1518 } 1519 1520 search_done = true; 1521 } else { 1522 ret = dev_pm_opp_disable(svsb->opp_dev, 1523 svsb->opp_dfreq[i]); 1524 if (ret) { 1525 dev_err(svsb->dev, 1526 "opp %uHz disable fail: %d\n", 1527 svsb->opp_dfreq[i], ret); 1528 goto svs_init01_finish; 1529 } 1530 } 1531 } 1532 } 1533 1534 /* Svs bank init01 begins */ 1535 for (idx = 0; idx < svsp->bank_max; idx++) { 1536 svsb = &svsp->banks[idx]; 1537 bdata = &svsb->pdata; 1538 1539 if (!(svsb->mode_support & SVSB_MODE_INIT01)) 1540 continue; 1541 1542 opp_vboot = svs_bank_volt_to_opp_volt(bdata->vboot, 1543 bdata->volt_step, 1544 bdata->volt_base); 1545 1546 buck_volt = regulator_get_voltage(svsb->buck); 1547 if (buck_volt != opp_vboot) { 1548 dev_err(svsb->dev, 1549 "buck voltage: %uuV, expected vboot: %uuV\n", 1550 buck_volt, opp_vboot); 1551 ret = -EPERM; 1552 goto svs_init01_finish; 1553 } 1554 1555 spin_lock_irqsave(&svs_lock, flags); 1556 svs_set_bank_phase(svsp, idx, SVSB_PHASE_INIT01); 1557 spin_unlock_irqrestore(&svs_lock, flags); 1558 1559 time_left = wait_for_completion_timeout(&svsb->init_completion, 1560 msecs_to_jiffies(5000)); 1561 if (!time_left) { 1562 dev_err(svsb->dev, "init01 completion timeout\n"); 1563 ret = -EBUSY; 1564 goto svs_init01_finish; 1565 } 1566 } 1567 1568 svs_init01_finish: 1569 for (idx = 0; idx < svsp->bank_max; idx++) { 1570 svsb = &svsp->banks[idx]; 1571 bdata = &svsb->pdata; 1572 1573 if (!(svsb->mode_support & SVSB_MODE_INIT01)) 1574 continue; 1575 1576 for (i = 0; i < bdata->opp_count; i++) { 1577 r = dev_pm_opp_enable(svsb->opp_dev, 1578 svsb->opp_dfreq[i]); 1579 if (r) 1580 dev_err(svsb->dev, "opp %uHz enable fail: %d\n", 1581 svsb->opp_dfreq[i], r); 1582 } 1583 1584 if (svsb->volt_flags & SVSB_INIT01_PD_REQ) { 1585 r = pm_runtime_put_sync(svsb->opp_dev); 1586 if (r) 1587 dev_err(svsb->dev, "mtcmos off fail: %d\n", r); 1588 1589 if (svsb->pm_runtime_enabled_count > 0) { 1590 pm_runtime_disable(svsb->opp_dev); 1591 svsb->pm_runtime_enabled_count--; 1592 } 1593 } 1594 1595 r = regulator_set_mode(svsb->buck, REGULATOR_MODE_NORMAL); 1596 if (r) 1597 dev_notice(svsb->dev, "set normal mode fail: %d\n", r); 1598 1599 r = regulator_disable(svsb->buck); 1600 if (r) 1601 dev_err(svsb->dev, "%s disable fail: %d\n", 1602 bdata->buck_name, r); 1603 } 1604 1605 svs_init01_resume_cpuidle: 1606 cpuidle_resume_and_unlock(); 1607 1608 return ret; 1609 } 1610 1611 static int svs_init02(struct svs_platform *svsp) 1612 { 1613 const struct svs_bank_pdata *bdata; 1614 struct svs_bank *svsb; 1615 unsigned long flags, time_left; 1616 int ret; 1617 u32 idx; 1618 1619 if (!svs_mode_available(svsp, SVSB_MODE_INIT02)) 1620 return 0; 1621 1622 for (idx = 0; idx < svsp->bank_max; idx++) { 1623 svsb = &svsp->banks[idx]; 1624 1625 if (!(svsb->mode_support & SVSB_MODE_INIT02)) 1626 continue; 1627 1628 reinit_completion(&svsb->init_completion); 1629 spin_lock_irqsave(&svs_lock, flags); 1630 svs_set_bank_phase(svsp, idx, SVSB_PHASE_INIT02); 1631 spin_unlock_irqrestore(&svs_lock, flags); 1632 1633 time_left = wait_for_completion_timeout(&svsb->init_completion, 1634 msecs_to_jiffies(5000)); 1635 if (!time_left) { 1636 dev_err(svsb->dev, "init02 completion timeout\n"); 1637 ret = -EBUSY; 1638 goto out_of_init02; 1639 } 1640 } 1641 1642 /* 1643 * 2-line high/low bank update its corresponding opp voltages only. 1644 * Therefore, we sync voltages from opp for high/low bank voltages 1645 * consistency. 1646 */ 1647 for (idx = 0; idx < svsp->bank_max; idx++) { 1648 svsb = &svsp->banks[idx]; 1649 bdata = &svsb->pdata; 1650 1651 if (!(svsb->mode_support & SVSB_MODE_INIT02)) 1652 continue; 1653 1654 if (bdata->type == SVSB_TYPE_HIGH || bdata->type == SVSB_TYPE_LOW) { 1655 if (svs_sync_bank_volts_from_opp(svsb)) { 1656 dev_err(svsb->dev, "sync volt fail\n"); 1657 ret = -EPERM; 1658 goto out_of_init02; 1659 } 1660 } 1661 } 1662 1663 return 0; 1664 1665 out_of_init02: 1666 for (idx = 0; idx < svsp->bank_max; idx++) { 1667 svsb = &svsp->banks[idx]; 1668 svs_bank_disable_and_restore_default_volts(svsp, svsb); 1669 } 1670 1671 return ret; 1672 } 1673 1674 static void svs_mon_mode(struct svs_platform *svsp) 1675 { 1676 struct svs_bank *svsb; 1677 unsigned long flags; 1678 u32 idx; 1679 1680 for (idx = 0; idx < svsp->bank_max; idx++) { 1681 svsb = &svsp->banks[idx]; 1682 1683 if (!(svsb->mode_support & SVSB_MODE_MON)) 1684 continue; 1685 1686 spin_lock_irqsave(&svs_lock, flags); 1687 svs_set_bank_phase(svsp, idx, SVSB_PHASE_MON); 1688 spin_unlock_irqrestore(&svs_lock, flags); 1689 } 1690 } 1691 1692 static int svs_start(struct svs_platform *svsp) 1693 { 1694 int ret; 1695 1696 ret = svs_init01(svsp); 1697 if (ret) 1698 return ret; 1699 1700 ret = svs_init02(svsp); 1701 if (ret) 1702 return ret; 1703 1704 svs_mon_mode(svsp); 1705 1706 return 0; 1707 } 1708 1709 static int svs_suspend(struct device *dev) 1710 { 1711 struct svs_platform *svsp = dev_get_drvdata(dev); 1712 int ret; 1713 u32 idx; 1714 1715 for (idx = 0; idx < svsp->bank_max; idx++) { 1716 struct svs_bank *svsb = &svsp->banks[idx]; 1717 1718 svs_bank_disable_and_restore_default_volts(svsp, svsb); 1719 } 1720 1721 ret = reset_control_assert(svsp->rst); 1722 if (ret) { 1723 dev_err(svsp->dev, "cannot assert reset %d\n", ret); 1724 return ret; 1725 } 1726 1727 clk_disable_unprepare(svsp->main_clk); 1728 1729 return 0; 1730 } 1731 1732 static int svs_resume(struct device *dev) 1733 { 1734 struct svs_platform *svsp = dev_get_drvdata(dev); 1735 int ret; 1736 1737 ret = clk_prepare_enable(svsp->main_clk); 1738 if (ret) { 1739 dev_err(svsp->dev, "cannot enable main_clk, disable svs\n"); 1740 return ret; 1741 } 1742 1743 ret = reset_control_deassert(svsp->rst); 1744 if (ret) { 1745 dev_err(svsp->dev, "cannot deassert reset %d\n", ret); 1746 goto out_of_resume; 1747 } 1748 1749 ret = svs_init02(svsp); 1750 if (ret) 1751 goto svs_resume_reset_assert; 1752 1753 svs_mon_mode(svsp); 1754 1755 return 0; 1756 1757 svs_resume_reset_assert: 1758 dev_err(svsp->dev, "assert reset: %d\n", 1759 reset_control_assert(svsp->rst)); 1760 1761 out_of_resume: 1762 clk_disable_unprepare(svsp->main_clk); 1763 return ret; 1764 } 1765 1766 static int svs_bank_resource_setup(struct svs_platform *svsp) 1767 { 1768 const struct svs_bank_pdata *bdata; 1769 struct svs_bank *svsb; 1770 struct dev_pm_opp *opp; 1771 char tz_name_buf[20]; 1772 unsigned long freq; 1773 int count, ret; 1774 u32 idx, i; 1775 1776 dev_set_drvdata(svsp->dev, svsp); 1777 1778 for (idx = 0; idx < svsp->bank_max; idx++) { 1779 svsb = &svsp->banks[idx]; 1780 bdata = &svsb->pdata; 1781 1782 if (bdata->sw_id >= SVSB_SWID_MAX || bdata->type >= SVSB_TYPE_MAX) { 1783 dev_err(svsb->dev, "unknown bank sw_id or type\n"); 1784 return -EINVAL; 1785 } 1786 1787 svsb->dev = devm_kzalloc(svsp->dev, sizeof(*svsb->dev), GFP_KERNEL); 1788 if (!svsb->dev) 1789 return -ENOMEM; 1790 1791 svsb->name = devm_kasprintf(svsp->dev, GFP_KERNEL, "%s%s", 1792 svs_swid_names[bdata->sw_id], 1793 svs_type_names[bdata->type]); 1794 if (!svsb->name) 1795 return -ENOMEM; 1796 1797 ret = dev_set_name(svsb->dev, "%s", svsb->name); 1798 if (ret) 1799 return ret; 1800 1801 dev_set_drvdata(svsb->dev, svsp); 1802 1803 ret = devm_pm_opp_of_add_table(svsb->opp_dev); 1804 if (ret) { 1805 dev_err(svsb->dev, "add opp table fail: %d\n", ret); 1806 return ret; 1807 } 1808 1809 mutex_init(&svsb->lock); 1810 init_completion(&svsb->init_completion); 1811 1812 if (svsb->mode_support & SVSB_MODE_INIT01) { 1813 svsb->buck = devm_regulator_get_optional(svsb->opp_dev, 1814 bdata->buck_name); 1815 if (IS_ERR(svsb->buck)) { 1816 dev_err(svsb->dev, "cannot get \"%s-supply\"\n", 1817 bdata->buck_name); 1818 return PTR_ERR(svsb->buck); 1819 } 1820 } 1821 1822 if (!IS_ERR_OR_NULL(bdata->tzone_name)) { 1823 snprintf(tz_name_buf, ARRAY_SIZE(tz_name_buf), 1824 "%s-thermal", bdata->tzone_name); 1825 svsb->tzd = thermal_zone_get_zone_by_name(tz_name_buf); 1826 if (IS_ERR(svsb->tzd)) { 1827 dev_err(svsb->dev, "cannot get \"%s\" thermal zone\n", 1828 tz_name_buf); 1829 return PTR_ERR(svsb->tzd); 1830 } 1831 } 1832 1833 count = dev_pm_opp_get_opp_count(svsb->opp_dev); 1834 if (bdata->opp_count != count) { 1835 dev_err(svsb->dev, 1836 "opp_count not \"%u\" but get \"%d\"?\n", 1837 bdata->opp_count, count); 1838 return count; 1839 } 1840 1841 for (i = 0, freq = ULONG_MAX; i < bdata->opp_count; i++, freq--) { 1842 opp = dev_pm_opp_find_freq_floor(svsb->opp_dev, &freq); 1843 if (IS_ERR(opp)) { 1844 dev_err(svsb->dev, "cannot find freq = %ld\n", 1845 PTR_ERR(opp)); 1846 return PTR_ERR(opp); 1847 } 1848 1849 svsb->opp_dfreq[i] = freq; 1850 svsb->opp_dvolt[i] = dev_pm_opp_get_voltage(opp); 1851 svsb->freq_pct[i] = percent(svsb->opp_dfreq[i], 1852 svsb->freq_base); 1853 dev_pm_opp_put(opp); 1854 } 1855 } 1856 1857 return 0; 1858 } 1859 1860 static int svs_get_efuse_data(struct svs_platform *svsp, 1861 const char *nvmem_cell_name, 1862 u32 **svsp_efuse, size_t *svsp_efuse_max) 1863 { 1864 struct nvmem_cell *cell; 1865 1866 cell = nvmem_cell_get(svsp->dev, nvmem_cell_name); 1867 if (IS_ERR(cell)) { 1868 dev_err(svsp->dev, "no \"%s\"? %ld\n", 1869 nvmem_cell_name, PTR_ERR(cell)); 1870 return PTR_ERR(cell); 1871 } 1872 1873 *svsp_efuse = nvmem_cell_read(cell, svsp_efuse_max); 1874 if (IS_ERR(*svsp_efuse)) { 1875 nvmem_cell_put(cell); 1876 return PTR_ERR(*svsp_efuse); 1877 } 1878 1879 *svsp_efuse_max /= sizeof(u32); 1880 nvmem_cell_put(cell); 1881 1882 return 0; 1883 } 1884 1885 static u32 svs_get_fuse_val(u32 *fuse_array, const struct svs_fusemap *fmap, u8 nbits) 1886 { 1887 u32 val; 1888 1889 if (fmap->index < 0) 1890 return FUSE_DATA_NOT_VALID; 1891 1892 val = fuse_array[fmap->index] >> fmap->ofst; 1893 val &= GENMASK(nbits - 1, 0); 1894 1895 return val; 1896 } 1897 1898 static bool svs_is_available(struct svs_platform *svsp) 1899 { 1900 int i, num_populated = 0; 1901 1902 /* If at least two fuse arrays are populated, SVS is calibrated */ 1903 for (i = 0; i < svsp->efuse_max; i++) { 1904 if (svsp->efuse[i]) 1905 num_populated++; 1906 1907 if (num_populated > 1) 1908 return true; 1909 } 1910 1911 return false; 1912 } 1913 1914 static bool svs_common_parse_efuse(struct svs_platform *svsp, 1915 const struct svs_platform_data *pdata) 1916 { 1917 const struct svs_fusemap *gfmap = pdata->glb_fuse_map; 1918 struct svs_fusemap tfm = { 0, 24 }; 1919 u32 golden_temp, val; 1920 u8 ft_pgm, vmin; 1921 int i; 1922 1923 if (!svs_is_available(svsp)) 1924 return false; 1925 1926 /* Get golden temperature from SVS-Thermal calibration */ 1927 val = svs_get_fuse_val(svsp->tefuse, &tfm, 8); 1928 1929 /* If golden temp is not programmed, use the default of 50 */ 1930 golden_temp = val ? val : 50; 1931 1932 /* Parse fused SVS calibration */ 1933 ft_pgm = svs_get_fuse_val(svsp->efuse, &gfmap[GLB_FT_PGM], 8); 1934 vmin = svs_get_fuse_val(svsp->efuse, &gfmap[GLB_VMIN], 2); 1935 1936 for (i = 0; i < svsp->bank_max; i++) { 1937 struct svs_bank *svsb = &svsp->banks[i]; 1938 const struct svs_bank_pdata *bdata = &svsb->pdata; 1939 const struct svs_fusemap *dfmap = bdata->dev_fuse_map; 1940 1941 if (vmin == 1) 1942 svsb->vmin = 0x1e; 1943 1944 if (ft_pgm == 0) 1945 svsb->volt_flags |= SVSB_INIT01_VOLT_IGNORE; 1946 1947 svsb->mtdes = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_MTDES], 8); 1948 svsb->bdes = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_BDES], 8); 1949 svsb->mdes = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_MDES], 8); 1950 svsb->dcbdet = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_DCBDET], 8); 1951 svsb->dcmdet = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_DCMDET], 8); 1952 svsb->vmax += svsb->dvt_fixed; 1953 1954 svsb->mts = (svsp->ts_coeff * 2) / 1000; 1955 svsb->bts = (((500 * golden_temp + svsp->ts_coeff) / 1000) - 25) * 4; 1956 } 1957 1958 return true; 1959 } 1960 1961 static bool svs_mt8183_efuse_parsing(struct svs_platform *svsp, 1962 const struct svs_platform_data *pdata) 1963 { 1964 struct svs_bank *svsb; 1965 const struct svs_bank_pdata *bdata; 1966 int format[6], x_roomt[6], o_vtsmcu[5], o_vtsabb, tb_roomt = 0; 1967 int adc_ge_t, adc_oe_t, ge, oe, gain, degc_cali, adc_cali_en_t; 1968 int o_slope, o_slope_sign, ts_id; 1969 u32 idx, i, ft_pgm, mts, temp0, temp1, temp2; 1970 1971 for (i = 0; i < svsp->efuse_max; i++) 1972 if (svsp->efuse[i]) 1973 dev_info(svsp->dev, "M_HW_RES%d: 0x%08x\n", 1974 i, svsp->efuse[i]); 1975 1976 if (!svsp->efuse[2]) { 1977 dev_notice(svsp->dev, "svs_efuse[2] = 0x0?\n"); 1978 return false; 1979 } 1980 1981 /* Svs efuse parsing */ 1982 ft_pgm = svs_get_fuse_val(svsp->efuse, &pdata->glb_fuse_map[GLB_FT_PGM], 4); 1983 1984 for (idx = 0; idx < svsp->bank_max; idx++) { 1985 svsb = &svsp->banks[idx]; 1986 bdata = &svsb->pdata; 1987 const struct svs_fusemap *dfmap = bdata->dev_fuse_map; 1988 1989 if (ft_pgm <= 1) 1990 svsb->volt_flags |= SVSB_INIT01_VOLT_IGNORE; 1991 1992 svsb->mtdes = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_MTDES], 8); 1993 svsb->bdes = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_BDES], 8); 1994 svsb->mdes = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_MDES], 8); 1995 svsb->dcbdet = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_DCBDET], 8); 1996 svsb->dcmdet = svs_get_fuse_val(svsp->efuse, &dfmap[BDEV_DCMDET], 8); 1997 1998 switch (bdata->sw_id) { 1999 case SVSB_SWID_CPU_LITTLE: 2000 case SVSB_SWID_CCI: 2001 if (ft_pgm <= 3) 2002 svsb->volt_od += 10; 2003 else 2004 svsb->volt_od += 2; 2005 break; 2006 case SVSB_SWID_CPU_BIG: 2007 if (ft_pgm <= 3) 2008 svsb->volt_od += 15; 2009 else 2010 svsb->volt_od += 12; 2011 break; 2012 case SVSB_SWID_GPU: 2013 if (ft_pgm != FUSE_DATA_NOT_VALID && ft_pgm >= 2) { 2014 svsb->freq_base = 800000000; /* 800MHz */ 2015 svsb->dvt_fixed = 2; 2016 } 2017 break; 2018 default: 2019 dev_err(svsb->dev, "unknown sw_id: %u\n", bdata->sw_id); 2020 return false; 2021 } 2022 } 2023 2024 /* Thermal efuse parsing */ 2025 adc_ge_t = (svsp->tefuse[1] >> 22) & GENMASK(9, 0); 2026 adc_oe_t = (svsp->tefuse[1] >> 12) & GENMASK(9, 0); 2027 2028 o_vtsmcu[0] = (svsp->tefuse[0] >> 17) & GENMASK(8, 0); 2029 o_vtsmcu[1] = (svsp->tefuse[0] >> 8) & GENMASK(8, 0); 2030 o_vtsmcu[2] = svsp->tefuse[1] & GENMASK(8, 0); 2031 o_vtsmcu[3] = (svsp->tefuse[2] >> 23) & GENMASK(8, 0); 2032 o_vtsmcu[4] = (svsp->tefuse[2] >> 5) & GENMASK(8, 0); 2033 o_vtsabb = (svsp->tefuse[2] >> 14) & GENMASK(8, 0); 2034 2035 degc_cali = (svsp->tefuse[0] >> 1) & GENMASK(5, 0); 2036 adc_cali_en_t = svsp->tefuse[0] & BIT(0); 2037 o_slope_sign = (svsp->tefuse[0] >> 7) & BIT(0); 2038 2039 ts_id = (svsp->tefuse[1] >> 9) & BIT(0); 2040 if (!ts_id) { 2041 o_slope = 1534; 2042 } else { 2043 o_slope = (svsp->tefuse[0] >> 26) & GENMASK(5, 0); 2044 if (!o_slope_sign) 2045 o_slope = 1534 + o_slope * 10; 2046 else 2047 o_slope = 1534 - o_slope * 10; 2048 } 2049 2050 if (adc_cali_en_t == 0 || 2051 adc_ge_t < 265 || adc_ge_t > 758 || 2052 adc_oe_t < 265 || adc_oe_t > 758 || 2053 o_vtsmcu[0] < -8 || o_vtsmcu[0] > 484 || 2054 o_vtsmcu[1] < -8 || o_vtsmcu[1] > 484 || 2055 o_vtsmcu[2] < -8 || o_vtsmcu[2] > 484 || 2056 o_vtsmcu[3] < -8 || o_vtsmcu[3] > 484 || 2057 o_vtsmcu[4] < -8 || o_vtsmcu[4] > 484 || 2058 o_vtsabb < -8 || o_vtsabb > 484 || 2059 degc_cali < 1 || degc_cali > 63) { 2060 dev_err(svsp->dev, "bad thermal efuse, no mon mode\n"); 2061 goto remove_mt8183_svsb_mon_mode; 2062 } 2063 2064 ge = ((adc_ge_t - 512) * 10000) / 4096; 2065 oe = (adc_oe_t - 512); 2066 gain = (10000 + ge); 2067 2068 format[0] = (o_vtsmcu[0] + 3350 - oe); 2069 format[1] = (o_vtsmcu[1] + 3350 - oe); 2070 format[2] = (o_vtsmcu[2] + 3350 - oe); 2071 format[3] = (o_vtsmcu[3] + 3350 - oe); 2072 format[4] = (o_vtsmcu[4] + 3350 - oe); 2073 format[5] = (o_vtsabb + 3350 - oe); 2074 2075 for (i = 0; i < 6; i++) 2076 x_roomt[i] = (((format[i] * 10000) / 4096) * 10000) / gain; 2077 2078 temp0 = (10000 * 100000 / gain) * 15 / 18; 2079 mts = (temp0 * 10) / o_slope; 2080 2081 for (idx = 0; idx < svsp->bank_max; idx++) { 2082 svsb = &svsp->banks[idx]; 2083 bdata = &svsb->pdata; 2084 svsb->mts = mts; 2085 2086 switch (bdata->sw_id) { 2087 case SVSB_SWID_CPU_LITTLE: 2088 tb_roomt = x_roomt[3]; 2089 break; 2090 case SVSB_SWID_CPU_BIG: 2091 tb_roomt = x_roomt[4]; 2092 break; 2093 case SVSB_SWID_CCI: 2094 tb_roomt = x_roomt[3]; 2095 break; 2096 case SVSB_SWID_GPU: 2097 tb_roomt = x_roomt[1]; 2098 break; 2099 default: 2100 dev_err(svsb->dev, "unknown sw_id: %u\n", bdata->sw_id); 2101 goto remove_mt8183_svsb_mon_mode; 2102 } 2103 2104 temp0 = (degc_cali * 10 / 2); 2105 temp1 = ((10000 * 100000 / 4096 / gain) * 2106 oe + tb_roomt * 10) * 15 / 18; 2107 temp2 = temp1 * 100 / o_slope; 2108 2109 svsb->bts = (temp0 + temp2 - 250) * 4 / 10; 2110 } 2111 2112 return true; 2113 2114 remove_mt8183_svsb_mon_mode: 2115 for (idx = 0; idx < svsp->bank_max; idx++) { 2116 svsb = &svsp->banks[idx]; 2117 svsb->mode_support &= ~SVSB_MODE_MON; 2118 } 2119 2120 return true; 2121 } 2122 2123 static struct device *svs_get_subsys_device(struct svs_platform *svsp, 2124 const char *node_name) 2125 { 2126 struct platform_device *pdev; 2127 struct device_node *np; 2128 2129 np = of_find_node_by_name(NULL, node_name); 2130 if (!np) { 2131 dev_err(svsp->dev, "cannot find %s node\n", node_name); 2132 return ERR_PTR(-ENODEV); 2133 } 2134 2135 pdev = of_find_device_by_node(np); 2136 of_node_put(np); 2137 if (!pdev) { 2138 dev_err(svsp->dev, "cannot find pdev by %s\n", node_name); 2139 return ERR_PTR(-ENXIO); 2140 } 2141 2142 return &pdev->dev; 2143 } 2144 2145 static struct device *svs_add_device_link(struct svs_platform *svsp, 2146 const char *node_name) 2147 { 2148 struct device *dev; 2149 struct device_link *sup_link; 2150 2151 dev = svs_get_subsys_device(svsp, node_name); 2152 if (IS_ERR(dev)) 2153 return dev; 2154 2155 sup_link = device_link_add(svsp->dev, dev, 2156 DL_FLAG_AUTOREMOVE_CONSUMER); 2157 if (!sup_link) { 2158 dev_err(svsp->dev, "sup_link is NULL\n"); 2159 return ERR_PTR(-EINVAL); 2160 } 2161 2162 if (sup_link->supplier->links.status != DL_DEV_DRIVER_BOUND) 2163 return ERR_PTR(-EPROBE_DEFER); 2164 2165 return dev; 2166 } 2167 2168 static int svs_mt8192_platform_probe(struct svs_platform *svsp) 2169 { 2170 struct device *dev; 2171 u32 idx; 2172 2173 svsp->rst = devm_reset_control_get_optional(svsp->dev, "svs_rst"); 2174 if (IS_ERR(svsp->rst)) 2175 return dev_err_probe(svsp->dev, PTR_ERR(svsp->rst), 2176 "cannot get svs reset control\n"); 2177 2178 dev = svs_add_device_link(svsp, "thermal-sensor"); 2179 if (IS_ERR(dev)) 2180 return dev_err_probe(svsp->dev, PTR_ERR(dev), 2181 "failed to get lvts device\n"); 2182 2183 for (idx = 0; idx < svsp->bank_max; idx++) { 2184 struct svs_bank *svsb = &svsp->banks[idx]; 2185 const struct svs_bank_pdata *bdata = &svsb->pdata; 2186 2187 switch (bdata->sw_id) { 2188 case SVSB_SWID_CPU_LITTLE: 2189 case SVSB_SWID_CPU_BIG: 2190 svsb->opp_dev = get_cpu_device(bdata->cpu_id); 2191 break; 2192 case SVSB_SWID_CCI: 2193 svsb->opp_dev = svs_add_device_link(svsp, "cci"); 2194 break; 2195 case SVSB_SWID_GPU: 2196 if (bdata->type == SVSB_TYPE_LOW) 2197 svsb->opp_dev = svs_get_subsys_device(svsp, "gpu"); 2198 else 2199 svsb->opp_dev = svs_add_device_link(svsp, "gpu"); 2200 break; 2201 default: 2202 dev_err(svsb->dev, "unknown sw_id: %u\n", bdata->sw_id); 2203 return -EINVAL; 2204 } 2205 2206 if (IS_ERR(svsb->opp_dev)) 2207 return dev_err_probe(svsp->dev, PTR_ERR(svsb->opp_dev), 2208 "failed to get OPP device for bank %d\n", 2209 idx); 2210 } 2211 2212 return 0; 2213 } 2214 2215 static int svs_mt8183_platform_probe(struct svs_platform *svsp) 2216 { 2217 struct device *dev; 2218 u32 idx; 2219 2220 dev = svs_add_device_link(svsp, "thermal-sensor"); 2221 if (IS_ERR(dev)) 2222 return dev_err_probe(svsp->dev, PTR_ERR(dev), 2223 "failed to get thermal device\n"); 2224 2225 for (idx = 0; idx < svsp->bank_max; idx++) { 2226 struct svs_bank *svsb = &svsp->banks[idx]; 2227 const struct svs_bank_pdata *bdata = &svsb->pdata; 2228 2229 switch (bdata->sw_id) { 2230 case SVSB_SWID_CPU_LITTLE: 2231 case SVSB_SWID_CPU_BIG: 2232 svsb->opp_dev = get_cpu_device(bdata->cpu_id); 2233 break; 2234 case SVSB_SWID_CCI: 2235 svsb->opp_dev = svs_add_device_link(svsp, "cci"); 2236 break; 2237 case SVSB_SWID_GPU: 2238 svsb->opp_dev = svs_add_device_link(svsp, "gpu"); 2239 break; 2240 default: 2241 dev_err(svsb->dev, "unknown sw_id: %u\n", bdata->sw_id); 2242 return -EINVAL; 2243 } 2244 2245 if (IS_ERR(svsb->opp_dev)) 2246 return dev_err_probe(svsp->dev, PTR_ERR(svsb->opp_dev), 2247 "failed to get OPP device for bank %d\n", 2248 idx); 2249 } 2250 2251 return 0; 2252 } 2253 2254 static struct svs_bank svs_mt8195_banks[] = { 2255 { 2256 .pdata = (const struct svs_bank_pdata) { 2257 .sw_id = SVSB_SWID_GPU, 2258 .type = SVSB_TYPE_LOW, 2259 .set_freq_pct = svs_set_bank_freq_pct_v3, 2260 .get_volts = svs_get_bank_volts_v3, 2261 .opp_count = MAX_OPP_ENTRIES, 2262 .turn_freq_base = 640000000, 2263 .volt_step = 6250, 2264 .volt_base = 400000, 2265 .age_config = 0x555555, 2266 .dc_config = 0x1, 2267 .vco = 0x18, 2268 .chk_shift = 0x87, 2269 .int_st = BIT(0), 2270 .ctl0 = 0x00540003, 2271 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2272 { 10, 16 }, { 10, 24 }, { 10, 0 }, { 8, 0 }, { 8, 8 } 2273 } 2274 }, 2275 .mode_support = SVSB_MODE_INIT02, 2276 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT, 2277 .freq_base = 640000000, 2278 .core_sel = 0x0fff0100, 2279 .dvt_fixed = 0x1, 2280 .vmax = 0x38, 2281 .vmin = 0x14, 2282 }, 2283 { 2284 .pdata = (const struct svs_bank_pdata) { 2285 .sw_id = SVSB_SWID_GPU, 2286 .type = SVSB_TYPE_HIGH, 2287 .set_freq_pct = svs_set_bank_freq_pct_v3, 2288 .get_volts = svs_get_bank_volts_v3, 2289 .tzone_name = "gpu", 2290 .opp_count = MAX_OPP_ENTRIES, 2291 .turn_freq_base = 640000000, 2292 .volt_step = 6250, 2293 .volt_base = 400000, 2294 .age_config = 0x555555, 2295 .dc_config = 0x1, 2296 .vco = 0x18, 2297 .chk_shift = 0x87, 2298 .int_st = BIT(1), 2299 .ctl0 = 0x00540003, 2300 .tzone_htemp = 85000, 2301 .tzone_htemp_voffset = 0, 2302 .tzone_ltemp = 25000, 2303 .tzone_ltemp_voffset = 7, 2304 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2305 { 9, 16 }, { 9, 24 }, { 9, 0 }, { 8, 0 }, { 8, 8 } 2306 }, 2307 }, 2308 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT | SVSB_MON_VOLT_IGNORE, 2309 .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON, 2310 .freq_base = 880000000, 2311 .core_sel = 0x0fff0101, 2312 .dvt_fixed = 0x6, 2313 .vmax = 0x38, 2314 .vmin = 0x14, 2315 }, 2316 }; 2317 2318 static struct svs_bank svs_mt8192_banks[] = { 2319 { 2320 .pdata = (const struct svs_bank_pdata) { 2321 .sw_id = SVSB_SWID_GPU, 2322 .type = SVSB_TYPE_LOW, 2323 .set_freq_pct = svs_set_bank_freq_pct_v3, 2324 .get_volts = svs_get_bank_volts_v3, 2325 .tzone_name = "gpu", 2326 .opp_count = MAX_OPP_ENTRIES, 2327 .turn_freq_base = 688000000, 2328 .volt_step = 6250, 2329 .volt_base = 400000, 2330 .age_config = 0x555555, 2331 .dc_config = 0x1, 2332 .vco = 0x18, 2333 .chk_shift = 0x87, 2334 .int_st = BIT(0), 2335 .ctl0 = 0x00540003, 2336 .tzone_htemp = 85000, 2337 .tzone_htemp_voffset = 0, 2338 .tzone_ltemp = 25000, 2339 .tzone_ltemp_voffset = 7, 2340 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2341 { 10, 16 }, { 10, 24 }, { 10, 0 }, { 17, 0 }, { 17, 8 } 2342 } 2343 }, 2344 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT, 2345 .mode_support = SVSB_MODE_INIT02, 2346 .freq_base = 688000000, 2347 .core_sel = 0x0fff0100, 2348 .dvt_fixed = 0x1, 2349 .vmax = 0x60, 2350 .vmin = 0x1a, 2351 }, 2352 { 2353 .pdata = (const struct svs_bank_pdata) { 2354 .sw_id = SVSB_SWID_GPU, 2355 .type = SVSB_TYPE_HIGH, 2356 .set_freq_pct = svs_set_bank_freq_pct_v3, 2357 .get_volts = svs_get_bank_volts_v3, 2358 .tzone_name = "gpu", 2359 .opp_count = MAX_OPP_ENTRIES, 2360 .turn_freq_base = 688000000, 2361 .volt_step = 6250, 2362 .volt_base = 400000, 2363 .age_config = 0x555555, 2364 .dc_config = 0x1, 2365 .vco = 0x18, 2366 .chk_shift = 0x87, 2367 .int_st = BIT(1), 2368 .ctl0 = 0x00540003, 2369 .tzone_htemp = 85000, 2370 .tzone_htemp_voffset = 0, 2371 .tzone_ltemp = 25000, 2372 .tzone_ltemp_voffset = 7, 2373 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2374 { 9, 16 }, { 9, 24 }, { 17, 0 }, { 17, 16 }, { 17, 24 } 2375 } 2376 }, 2377 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT | SVSB_MON_VOLT_IGNORE, 2378 .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON, 2379 .freq_base = 902000000, 2380 .core_sel = 0x0fff0101, 2381 .dvt_fixed = 0x6, 2382 .vmax = 0x60, 2383 .vmin = 0x1a, 2384 }, 2385 }; 2386 2387 static struct svs_bank svs_mt8188_banks[] = { 2388 { 2389 .pdata = (const struct svs_bank_pdata) { 2390 .sw_id = SVSB_SWID_GPU, 2391 .type = SVSB_TYPE_LOW, 2392 .set_freq_pct = svs_set_bank_freq_pct_v3, 2393 .get_volts = svs_get_bank_volts_v3, 2394 .opp_count = MAX_OPP_ENTRIES, 2395 .turn_freq_base = 640000000, 2396 .volt_step = 6250, 2397 .volt_base = 400000, 2398 .age_config = 0x555555, 2399 .dc_config = 0x555555, 2400 .vco = 0x10, 2401 .chk_shift = 0x87, 2402 .int_st = BIT(0), 2403 .ctl0 = 0x00100003, 2404 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2405 { 5, 16 }, { 5, 24 }, { 5, 0 }, { 15, 16 }, { 15, 24 } 2406 } 2407 }, 2408 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT, 2409 .mode_support = SVSB_MODE_INIT02, 2410 .freq_base = 640000000, 2411 .core_sel = 0x0fff0000, 2412 .dvt_fixed = 0x1, 2413 .vmax = 0x38, 2414 .vmin = 0x1c, 2415 }, 2416 { 2417 .pdata = (const struct svs_bank_pdata) { 2418 .sw_id = SVSB_SWID_GPU, 2419 .type = SVSB_TYPE_HIGH, 2420 .set_freq_pct = svs_set_bank_freq_pct_v3, 2421 .get_volts = svs_get_bank_volts_v3, 2422 .tzone_name = "gpu", 2423 .opp_count = MAX_OPP_ENTRIES, 2424 .turn_freq_base = 640000000, 2425 .volt_step = 6250, 2426 .volt_base = 400000, 2427 .age_config = 0x555555, 2428 .dc_config = 0x555555, 2429 .vco = 0x10, 2430 .chk_shift = 0x87, 2431 .int_st = BIT(1), 2432 .ctl0 = 0x00100003, 2433 .tzone_htemp = 85000, 2434 .tzone_htemp_voffset = 0, 2435 .tzone_ltemp = 25000, 2436 .tzone_ltemp_voffset = 7, 2437 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2438 { 4, 16 }, { 4, 24 }, { 4, 0 }, { 14, 0 }, { 14, 8 } 2439 } 2440 }, 2441 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT | SVSB_MON_VOLT_IGNORE, 2442 .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON, 2443 .freq_base = 880000000, 2444 .core_sel = 0x0fff0001, 2445 .dvt_fixed = 0x4, 2446 .vmax = 0x38, 2447 .vmin = 0x1c, 2448 }, 2449 }; 2450 2451 static struct svs_bank svs_mt8186_banks[] = { 2452 { 2453 .pdata = (const struct svs_bank_pdata) { 2454 .sw_id = SVSB_SWID_CPU_BIG, 2455 .type = SVSB_TYPE_LOW, 2456 .set_freq_pct = svs_set_bank_freq_pct_v3, 2457 .get_volts = svs_get_bank_volts_v3, 2458 .cpu_id = 6, 2459 .opp_count = MAX_OPP_ENTRIES, 2460 .turn_freq_base = 1670000000, 2461 .volt_step = 6250, 2462 .volt_base = 400000, 2463 .age_config = 0x1, 2464 .dc_config = 0x1, 2465 .vco = 0x10, 2466 .chk_shift = 0x87, 2467 .int_st = BIT(0), 2468 .ctl0 = 0x00540003, 2469 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2470 { 3, 16 }, { 3, 24 }, { 3, 0 }, { 14, 16 }, { 14, 24 } 2471 } 2472 }, 2473 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT, 2474 .volt_od = 4, 2475 .mode_support = SVSB_MODE_INIT02, 2476 .freq_base = 1670000000, 2477 .core_sel = 0x0fff0100, 2478 .dvt_fixed = 0x3, 2479 .vmax = 0x59, 2480 .vmin = 0x20, 2481 }, 2482 { 2483 .pdata = (const struct svs_bank_pdata) { 2484 .sw_id = SVSB_SWID_CPU_BIG, 2485 .type = SVSB_TYPE_HIGH, 2486 .set_freq_pct = svs_set_bank_freq_pct_v3, 2487 .get_volts = svs_get_bank_volts_v3, 2488 .cpu_id = 6, 2489 .tzone_name = "cpu-big", 2490 .opp_count = MAX_OPP_ENTRIES, 2491 .turn_freq_base = 1670000000, 2492 .volt_step = 6250, 2493 .volt_base = 400000, 2494 .age_config = 0x1, 2495 .dc_config = 0x1, 2496 .vco = 0x10, 2497 .chk_shift = 0x87, 2498 .int_st = BIT(1), 2499 .ctl0 = 0x00540003, 2500 .tzone_htemp = 85000, 2501 .tzone_htemp_voffset = 8, 2502 .tzone_ltemp = 25000, 2503 .tzone_ltemp_voffset = 8, 2504 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2505 { 2, 16 }, { 2, 24 }, { 2, 0 }, { 13, 0 }, { 13, 8 } 2506 } 2507 }, 2508 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT | SVSB_MON_VOLT_IGNORE, 2509 .volt_od = 4, 2510 .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON, 2511 .freq_base = 2050000000, 2512 .core_sel = 0x0fff0101, 2513 .dvt_fixed = 0x6, 2514 .vmax = 0x73, 2515 .vmin = 0x20, 2516 }, 2517 { 2518 .pdata = (const struct svs_bank_pdata) { 2519 .sw_id = SVSB_SWID_CPU_LITTLE, 2520 .set_freq_pct = svs_set_bank_freq_pct_v2, 2521 .get_volts = svs_get_bank_volts_v2, 2522 .cpu_id = 0, 2523 .tzone_name = "cpu-little", 2524 .opp_count = MAX_OPP_ENTRIES, 2525 .volt_step = 6250, 2526 .volt_base = 400000, 2527 .age_config = 0x1, 2528 .dc_config = 0x1, 2529 .vco = 0x10, 2530 .chk_shift = 0x87, 2531 .int_st = BIT(2), 2532 .ctl0 = 0x3210000f, 2533 .tzone_htemp = 85000, 2534 .tzone_htemp_voffset = 8, 2535 .tzone_ltemp = 25000, 2536 .tzone_ltemp_voffset = 8, 2537 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2538 { 4, 16 }, { 4, 24 }, { 4, 0 }, { 14, 0 }, { 14, 8 } 2539 } 2540 }, 2541 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT | SVSB_MON_VOLT_IGNORE, 2542 .volt_od = 3, 2543 .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON, 2544 .freq_base = 2000000000, 2545 .core_sel = 0x0fff0102, 2546 .dvt_fixed = 0x6, 2547 .vmax = 0x65, 2548 .vmin = 0x20, 2549 }, 2550 { 2551 .pdata = (const struct svs_bank_pdata) { 2552 .sw_id = SVSB_SWID_CCI, 2553 .set_freq_pct = svs_set_bank_freq_pct_v2, 2554 .get_volts = svs_get_bank_volts_v2, 2555 .tzone_name = "cci", 2556 .opp_count = MAX_OPP_ENTRIES, 2557 .volt_step = 6250, 2558 .volt_base = 400000, 2559 .age_config = 0x1, 2560 .dc_config = 0x1, 2561 .vco = 0x10, 2562 .chk_shift = 0x87, 2563 .int_st = BIT(3), 2564 .ctl0 = 0x3210000f, 2565 .tzone_htemp = 85000, 2566 .tzone_htemp_voffset = 8, 2567 .tzone_ltemp = 25000, 2568 .tzone_ltemp_voffset = 8, 2569 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2570 { 5, 16 }, { 5, 24 }, { 5, 0 }, { 15, 16 }, { 15, 24 } 2571 } 2572 }, 2573 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT | SVSB_MON_VOLT_IGNORE, 2574 .volt_od = 3, 2575 .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON, 2576 .freq_base = 1400000000, 2577 .core_sel = 0x0fff0103, 2578 .dvt_fixed = 0x6, 2579 .vmax = 0x65, 2580 .vmin = 0x20, 2581 }, 2582 { 2583 .pdata = (const struct svs_bank_pdata) { 2584 .sw_id = SVSB_SWID_GPU, 2585 .set_freq_pct = svs_set_bank_freq_pct_v2, 2586 .get_volts = svs_get_bank_volts_v2, 2587 .tzone_name = "gpu", 2588 .opp_count = MAX_OPP_ENTRIES, 2589 .volt_step = 6250, 2590 .volt_base = 400000, 2591 .age_config = 0x555555, 2592 .dc_config = 0x1, 2593 .vco = 0x10, 2594 .chk_shift = 0x87, 2595 .int_st = BIT(4), 2596 .ctl0 = 0x00100003, 2597 .tzone_htemp = 85000, 2598 .tzone_htemp_voffset = 8, 2599 .tzone_ltemp = 25000, 2600 .tzone_ltemp_voffset = 7, 2601 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2602 { 6, 16 }, { 6, 24 }, { 6, 0 }, { 15, 8 }, { 15, 0 } 2603 } 2604 }, 2605 .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT | SVSB_MON_VOLT_IGNORE, 2606 .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON, 2607 .freq_base = 850000000, 2608 .core_sel = 0x0fff0104, 2609 .dvt_fixed = 0x4, 2610 .vmax = 0x58, 2611 .vmin = 0x20, 2612 }, 2613 }; 2614 2615 static struct svs_bank svs_mt8183_banks[] = { 2616 { 2617 .pdata = (const struct svs_bank_pdata) { 2618 .sw_id = SVSB_SWID_CPU_LITTLE, 2619 .set_freq_pct = svs_set_bank_freq_pct_v2, 2620 .get_volts = svs_get_bank_volts_v2, 2621 .cpu_id = 0, 2622 .buck_name = "proc", 2623 .opp_count = MAX_OPP_ENTRIES, 2624 .vboot = 0x30, 2625 .volt_step = 6250, 2626 .volt_base = 500000, 2627 .age_config = 0x555555, 2628 .dc_config = 0x555555, 2629 .vco = 0x10, 2630 .chk_shift = 0x77, 2631 .int_st = BIT(0), 2632 .ctl0 = 0x00010001, 2633 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2634 { 16, 0 }, { 16, 8 }, { 17, 16 }, { 16, 16 }, { 16, 24 } 2635 } 2636 }, 2637 .volt_flags = SVSB_INIT01_VOLT_INC_ONLY, 2638 .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02, 2639 .freq_base = 1989000000, 2640 .core_sel = 0x8fff0000, 2641 .dvt_fixed = 0x7, 2642 .vmax = 0x64, 2643 .vmin = 0x18, 2644 2645 }, 2646 { 2647 .pdata = (const struct svs_bank_pdata) { 2648 .sw_id = SVSB_SWID_CPU_BIG, 2649 .set_freq_pct = svs_set_bank_freq_pct_v2, 2650 .get_volts = svs_get_bank_volts_v2, 2651 .cpu_id = 4, 2652 .buck_name = "proc", 2653 .opp_count = MAX_OPP_ENTRIES, 2654 .vboot = 0x30, 2655 .volt_step = 6250, 2656 .volt_base = 500000, 2657 .age_config = 0x555555, 2658 .dc_config = 0x555555, 2659 .vco = 0x10, 2660 .chk_shift = 0x77, 2661 .int_st = BIT(1), 2662 .ctl0 = 0x00000001, 2663 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2664 { 18, 0 }, { 18, 8 }, { 17, 0 }, { 18, 16 }, { 18, 24 } 2665 } 2666 }, 2667 .volt_flags = SVSB_INIT01_VOLT_INC_ONLY, 2668 .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02, 2669 .freq_base = 1989000000, 2670 .core_sel = 0x8fff0001, 2671 .dvt_fixed = 0x7, 2672 .vmax = 0x58, 2673 .vmin = 0x10, 2674 2675 }, 2676 { 2677 .pdata = (const struct svs_bank_pdata) { 2678 .sw_id = SVSB_SWID_CCI, 2679 .set_freq_pct = svs_set_bank_freq_pct_v2, 2680 .get_volts = svs_get_bank_volts_v2, 2681 .buck_name = "proc", 2682 .opp_count = MAX_OPP_ENTRIES, 2683 .vboot = 0x30, 2684 .volt_step = 6250, 2685 .volt_base = 500000, 2686 .age_config = 0x555555, 2687 .dc_config = 0x555555, 2688 .vco = 0x10, 2689 .chk_shift = 0x77, 2690 .int_st = BIT(2), 2691 .ctl0 = 0x00100003, 2692 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2693 { 4, 0 }, { 4, 8 }, { 5, 16 }, { 4, 16 }, { 4, 24 } 2694 } 2695 }, 2696 .volt_flags = SVSB_INIT01_VOLT_INC_ONLY, 2697 .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02, 2698 .freq_base = 1196000000, 2699 .core_sel = 0x8fff0002, 2700 .dvt_fixed = 0x7, 2701 .vmax = 0x64, 2702 .vmin = 0x18, 2703 }, 2704 { 2705 .pdata = (const struct svs_bank_pdata) { 2706 .sw_id = SVSB_SWID_GPU, 2707 .set_freq_pct = svs_set_bank_freq_pct_v2, 2708 .get_volts = svs_get_bank_volts_v2, 2709 .buck_name = "mali", 2710 .tzone_name = "gpu", 2711 .opp_count = MAX_OPP_ENTRIES, 2712 .vboot = 0x30, 2713 .volt_step = 6250, 2714 .volt_base = 500000, 2715 .age_config = 0x555555, 2716 .dc_config = 0x555555, 2717 .vco = 0x10, 2718 .chk_shift = 0x77, 2719 .int_st = BIT(3), 2720 .ctl0 = 0x00050001, 2721 .tzone_htemp = 85000, 2722 .tzone_htemp_voffset = 0, 2723 .tzone_ltemp = 25000, 2724 .tzone_ltemp_voffset = 3, 2725 .dev_fuse_map = (const struct svs_fusemap[BDEV_MAX]) { 2726 { 6, 0 }, { 6, 8 }, { 5, 0 }, { 6, 16 }, { 6, 24 } 2727 } 2728 }, 2729 .volt_flags = SVSB_INIT01_PD_REQ | SVSB_INIT01_VOLT_INC_ONLY, 2730 .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02 | SVSB_MODE_MON, 2731 .freq_base = 900000000, 2732 .core_sel = 0x8fff0003, 2733 .dvt_fixed = 0x3, 2734 .vmax = 0x40, 2735 .vmin = 0x14, 2736 }, 2737 }; 2738 2739 static const struct svs_platform_data svs_mt8195_platform_data = { 2740 .name = "mt8195-svs", 2741 .banks = svs_mt8195_banks, 2742 .efuse_parsing = svs_common_parse_efuse, 2743 .probe = svs_mt8192_platform_probe, 2744 .regs = svs_regs_v2, 2745 .bank_max = ARRAY_SIZE(svs_mt8195_banks), 2746 .ts_coeff = SVSB_TS_COEFF_MT8195, 2747 .glb_fuse_map = (const struct svs_fusemap[GLB_MAX]) { 2748 { 0, 0 }, { 19, 4 } 2749 } 2750 }; 2751 2752 static const struct svs_platform_data svs_mt8192_platform_data = { 2753 .name = "mt8192-svs", 2754 .banks = svs_mt8192_banks, 2755 .efuse_parsing = svs_common_parse_efuse, 2756 .probe = svs_mt8192_platform_probe, 2757 .regs = svs_regs_v2, 2758 .bank_max = ARRAY_SIZE(svs_mt8192_banks), 2759 .ts_coeff = SVSB_TS_COEFF_MT8195, 2760 .glb_fuse_map = (const struct svs_fusemap[GLB_MAX]) { 2761 /* FT_PGM not present */ 2762 { -1, 0 }, { 19, 4 } 2763 } 2764 }; 2765 2766 static const struct svs_platform_data svs_mt8188_platform_data = { 2767 .name = "mt8188-svs", 2768 .banks = svs_mt8188_banks, 2769 .efuse_parsing = svs_common_parse_efuse, 2770 .probe = svs_mt8192_platform_probe, 2771 .regs = svs_regs_v2, 2772 .bank_max = ARRAY_SIZE(svs_mt8188_banks), 2773 .ts_coeff = SVSB_TS_COEFF_MT8195, 2774 .glb_fuse_map = (const struct svs_fusemap[GLB_MAX]) { 2775 /* FT_PGM and VMIN not present */ 2776 { -1, 0 }, { -1, 0 } 2777 } 2778 }; 2779 2780 static const struct svs_platform_data svs_mt8186_platform_data = { 2781 .name = "mt8186-svs", 2782 .banks = svs_mt8186_banks, 2783 .efuse_parsing = svs_common_parse_efuse, 2784 .probe = svs_mt8192_platform_probe, 2785 .regs = svs_regs_v2, 2786 .bank_max = ARRAY_SIZE(svs_mt8186_banks), 2787 .ts_coeff = SVSB_TS_COEFF_MT8186, 2788 .glb_fuse_map = (const struct svs_fusemap[GLB_MAX]) { 2789 /* FT_PGM and VMIN not present */ 2790 { -1, 0 }, { -1, 0 } 2791 } 2792 }; 2793 2794 static const struct svs_platform_data svs_mt8183_platform_data = { 2795 .name = "mt8183-svs", 2796 .banks = svs_mt8183_banks, 2797 .efuse_parsing = svs_mt8183_efuse_parsing, 2798 .probe = svs_mt8183_platform_probe, 2799 .regs = svs_regs_v2, 2800 .bank_max = ARRAY_SIZE(svs_mt8183_banks), 2801 .glb_fuse_map = (const struct svs_fusemap[GLB_MAX]) { 2802 /* VMIN not present */ 2803 { 0, 4 }, { -1, 0 } 2804 } 2805 }; 2806 2807 static const struct of_device_id svs_of_match[] = { 2808 { .compatible = "mediatek,mt8195-svs", .data = &svs_mt8195_platform_data }, 2809 { .compatible = "mediatek,mt8192-svs", .data = &svs_mt8192_platform_data }, 2810 { .compatible = "mediatek,mt8188-svs", .data = &svs_mt8188_platform_data }, 2811 { .compatible = "mediatek,mt8186-svs", .data = &svs_mt8186_platform_data }, 2812 { .compatible = "mediatek,mt8183-svs", .data = &svs_mt8183_platform_data }, 2813 { /* sentinel */ } 2814 }; 2815 MODULE_DEVICE_TABLE(of, svs_of_match); 2816 2817 static int svs_probe(struct platform_device *pdev) 2818 { 2819 struct svs_platform *svsp; 2820 const struct svs_platform_data *svsp_data; 2821 int ret, svsp_irq; 2822 2823 svsp_data = of_device_get_match_data(&pdev->dev); 2824 2825 svsp = devm_kzalloc(&pdev->dev, sizeof(*svsp), GFP_KERNEL); 2826 if (!svsp) 2827 return -ENOMEM; 2828 2829 svsp->dev = &pdev->dev; 2830 svsp->banks = svsp_data->banks; 2831 svsp->regs = svsp_data->regs; 2832 svsp->bank_max = svsp_data->bank_max; 2833 svsp->ts_coeff = svsp_data->ts_coeff; 2834 2835 ret = svsp_data->probe(svsp); 2836 if (ret) 2837 return ret; 2838 2839 ret = svs_get_efuse_data(svsp, "svs-calibration-data", 2840 &svsp->efuse, &svsp->efuse_max); 2841 if (ret) 2842 return dev_err_probe(&pdev->dev, ret, "Cannot read SVS calibration\n"); 2843 2844 ret = svs_get_efuse_data(svsp, "t-calibration-data", 2845 &svsp->tefuse, &svsp->tefuse_max); 2846 if (ret) { 2847 dev_err_probe(&pdev->dev, ret, "Cannot read SVS-Thermal calibration\n"); 2848 goto svs_probe_free_efuse; 2849 } 2850 2851 if (!svsp_data->efuse_parsing(svsp, svsp_data)) { 2852 ret = dev_err_probe(svsp->dev, -EINVAL, "efuse data parsing failed\n"); 2853 goto svs_probe_free_tefuse; 2854 } 2855 2856 ret = svs_bank_resource_setup(svsp); 2857 if (ret) { 2858 dev_err_probe(svsp->dev, ret, "svs bank resource setup fail\n"); 2859 goto svs_probe_free_tefuse; 2860 } 2861 2862 svsp_irq = platform_get_irq(pdev, 0); 2863 if (svsp_irq < 0) { 2864 ret = svsp_irq; 2865 goto svs_probe_free_tefuse; 2866 } 2867 2868 svsp->main_clk = devm_clk_get(svsp->dev, "main"); 2869 if (IS_ERR(svsp->main_clk)) { 2870 ret = dev_err_probe(svsp->dev, PTR_ERR(svsp->main_clk), 2871 "failed to get clock\n"); 2872 goto svs_probe_free_tefuse; 2873 } 2874 2875 ret = clk_prepare_enable(svsp->main_clk); 2876 if (ret) { 2877 dev_err_probe(svsp->dev, ret, "cannot enable main clk\n"); 2878 goto svs_probe_free_tefuse; 2879 } 2880 2881 svsp->base = of_iomap(svsp->dev->of_node, 0); 2882 if (IS_ERR_OR_NULL(svsp->base)) { 2883 ret = dev_err_probe(svsp->dev, -EINVAL, "cannot find svs register base\n"); 2884 goto svs_probe_clk_disable; 2885 } 2886 2887 ret = devm_request_threaded_irq(svsp->dev, svsp_irq, NULL, svs_isr, 2888 IRQF_ONESHOT, svsp_data->name, svsp); 2889 if (ret) { 2890 dev_err_probe(svsp->dev, ret, "register irq(%d) failed\n", svsp_irq); 2891 goto svs_probe_iounmap; 2892 } 2893 2894 ret = svs_start(svsp); 2895 if (ret) { 2896 dev_err_probe(svsp->dev, ret, "svs start fail\n"); 2897 goto svs_probe_iounmap; 2898 } 2899 2900 #ifdef CONFIG_DEBUG_FS 2901 ret = svs_create_debug_cmds(svsp); 2902 if (ret) { 2903 dev_err_probe(svsp->dev, ret, "svs create debug cmds fail\n"); 2904 goto svs_probe_iounmap; 2905 } 2906 #endif 2907 2908 return 0; 2909 2910 svs_probe_iounmap: 2911 iounmap(svsp->base); 2912 svs_probe_clk_disable: 2913 clk_disable_unprepare(svsp->main_clk); 2914 svs_probe_free_tefuse: 2915 kfree(svsp->tefuse); 2916 svs_probe_free_efuse: 2917 kfree(svsp->efuse); 2918 return ret; 2919 } 2920 2921 static DEFINE_SIMPLE_DEV_PM_OPS(svs_pm_ops, svs_suspend, svs_resume); 2922 2923 static struct platform_driver svs_driver = { 2924 .probe = svs_probe, 2925 .driver = { 2926 .name = "mtk-svs", 2927 .pm = &svs_pm_ops, 2928 .of_match_table = svs_of_match, 2929 }, 2930 }; 2931 2932 module_platform_driver(svs_driver); 2933 2934 MODULE_AUTHOR("Roger Lu <roger.lu@mediatek.com>"); 2935 MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>"); 2936 MODULE_DESCRIPTION("MediaTek SVS driver"); 2937 MODULE_LICENSE("GPL"); 2938