1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018, The Linux Foundation. All rights reserved.*/ 3 4 #include <linux/cleanup.h> 5 #include <linux/err.h> 6 #include <linux/init.h> 7 #include <linux/kernel.h> 8 #include <linux/module.h> 9 #include <linux/mutex.h> 10 #include <linux/pm_domain.h> 11 #include <linux/slab.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/pm_opp.h> 15 #include <soc/qcom/cmd-db.h> 16 #include <soc/qcom/rpmh.h> 17 #include <dt-bindings/power/qcom-rpmpd.h> 18 #include <dt-bindings/power/qcom,rpmhpd.h> 19 20 #define domain_to_rpmhpd(domain) container_of(domain, struct rpmhpd, pd) 21 22 #define RPMH_ARC_MAX_LEVELS 32 23 24 /** 25 * struct rpmhpd - top level RPMh power domain resource data structure 26 * @dev: rpmh power domain controller device 27 * @pd: generic_pm_domain corresponding to the power domain 28 * @parent: generic_pm_domain corresponding to the parent's power domain 29 * @peer: A peer power domain in case Active only Voting is 30 * supported 31 * @active_only: True if it represents an Active only peer 32 * @corner: current corner 33 * @active_corner: current active corner 34 * @enable_corner: lowest non-zero corner 35 * @level: An array of level (vlvl) to corner (hlvl) mappings 36 * derived from cmd-db 37 * @level_count: Number of levels supported by the power domain. max 38 * being 16 (0 - 15) 39 * @enabled: true if the power domain is enabled 40 * @res_name: Resource name used for cmd-db lookup 41 * @addr: Resource address as looped up using resource name from 42 * cmd-db 43 * @state_synced: Indicator that sync_state has been invoked for the rpmhpd resource 44 * @skip_retention_level: Indicate that retention level should not be used for the power domain 45 */ 46 struct rpmhpd { 47 struct device *dev; 48 struct generic_pm_domain pd; 49 struct generic_pm_domain *parent; 50 struct rpmhpd *peer; 51 const bool active_only; 52 unsigned int corner; 53 unsigned int active_corner; 54 unsigned int enable_corner; 55 u32 level[RPMH_ARC_MAX_LEVELS]; 56 size_t level_count; 57 bool enabled; 58 const char *res_name; 59 u32 addr; 60 bool state_synced; 61 bool skip_retention_level; 62 }; 63 64 struct rpmhpd_desc { 65 struct rpmhpd **rpmhpds; 66 size_t num_pds; 67 }; 68 69 static DEFINE_MUTEX(rpmhpd_lock); 70 71 /* RPMH powerdomains */ 72 73 static struct rpmhpd cx_ao; 74 static struct rpmhpd mx; 75 static struct rpmhpd mx_ao; 76 static struct rpmhpd cx = { 77 .pd = { .name = "cx", }, 78 .peer = &cx_ao, 79 .res_name = "cx.lvl", 80 }; 81 82 static struct rpmhpd cx_ao = { 83 .pd = { .name = "cx_ao", }, 84 .active_only = true, 85 .peer = &cx, 86 .res_name = "cx.lvl", 87 }; 88 89 static struct rpmhpd cx_ao_w_mx_parent; 90 static struct rpmhpd cx_w_mx_parent = { 91 .pd = { .name = "cx", }, 92 .peer = &cx_ao_w_mx_parent, 93 .parent = &mx.pd, 94 .res_name = "cx.lvl", 95 }; 96 97 static struct rpmhpd cx_ao_w_mx_parent = { 98 .pd = { .name = "cx_ao", }, 99 .active_only = true, 100 .peer = &cx_w_mx_parent, 101 .parent = &mx_ao.pd, 102 .res_name = "cx.lvl", 103 }; 104 105 static struct rpmhpd ebi = { 106 .pd = { .name = "ebi", }, 107 .res_name = "ebi.lvl", 108 }; 109 110 static struct rpmhpd gfx = { 111 .pd = { .name = "gfx", }, 112 .res_name = "gfx.lvl", 113 }; 114 115 static struct rpmhpd lcx = { 116 .pd = { .name = "lcx", }, 117 .res_name = "lcx.lvl", 118 }; 119 120 static struct rpmhpd lmx = { 121 .pd = { .name = "lmx", }, 122 .res_name = "lmx.lvl", 123 }; 124 125 static struct rpmhpd mmcx_ao; 126 static struct rpmhpd mmcx = { 127 .pd = { .name = "mmcx", }, 128 .peer = &mmcx_ao, 129 .res_name = "mmcx.lvl", 130 }; 131 132 static struct rpmhpd mmcx_ao = { 133 .pd = { .name = "mmcx_ao", }, 134 .active_only = true, 135 .peer = &mmcx, 136 .res_name = "mmcx.lvl", 137 }; 138 139 static struct rpmhpd mmcx_ao_w_cx_parent; 140 static struct rpmhpd mmcx_w_cx_parent = { 141 .pd = { .name = "mmcx", }, 142 .peer = &mmcx_ao_w_cx_parent, 143 .parent = &cx.pd, 144 .res_name = "mmcx.lvl", 145 }; 146 147 static struct rpmhpd mmcx_ao_w_cx_parent = { 148 .pd = { .name = "mmcx_ao", }, 149 .active_only = true, 150 .peer = &mmcx_w_cx_parent, 151 .parent = &cx_ao.pd, 152 .res_name = "mmcx.lvl", 153 }; 154 155 static struct rpmhpd mss = { 156 .pd = { .name = "mss", }, 157 .res_name = "mss.lvl", 158 }; 159 160 static struct rpmhpd mx_ao; 161 static struct rpmhpd mx = { 162 .pd = { .name = "mx", }, 163 .peer = &mx_ao, 164 .res_name = "mx.lvl", 165 }; 166 167 static struct rpmhpd mx_ao = { 168 .pd = { .name = "mx_ao", }, 169 .active_only = true, 170 .peer = &mx, 171 .res_name = "mx.lvl", 172 }; 173 174 static struct rpmhpd mxc_ao; 175 static struct rpmhpd mxc = { 176 .pd = { .name = "mxc", }, 177 .peer = &mxc_ao, 178 .res_name = "mxc.lvl", 179 .skip_retention_level = true, 180 }; 181 182 static struct rpmhpd mxc_ao = { 183 .pd = { .name = "mxc_ao", }, 184 .active_only = true, 185 .peer = &mxc, 186 .res_name = "mxc.lvl", 187 .skip_retention_level = true, 188 }; 189 190 static struct rpmhpd nsp = { 191 .pd = { .name = "nsp", }, 192 .res_name = "nsp.lvl", 193 }; 194 195 static struct rpmhpd nsp0 = { 196 .pd = { .name = "nsp0", }, 197 .res_name = "nsp0.lvl", 198 }; 199 200 static struct rpmhpd nsp1 = { 201 .pd = { .name = "nsp1", }, 202 .res_name = "nsp1.lvl", 203 }; 204 205 static struct rpmhpd nsp2 = { 206 .pd = { .name = "nsp2", }, 207 .res_name = "nsp2.lvl", 208 }; 209 210 static struct rpmhpd qphy = { 211 .pd = { .name = "qphy", }, 212 .res_name = "qphy.lvl", 213 }; 214 215 static struct rpmhpd gmxc = { 216 .pd = { .name = "gmxc", }, 217 .res_name = "gmxc.lvl", 218 }; 219 220 /* Milos RPMH powerdomains */ 221 static struct rpmhpd *milos_rpmhpds[] = { 222 [RPMHPD_CX] = &cx, 223 [RPMHPD_CX_AO] = &cx_ao, 224 [RPMHPD_EBI] = &ebi, 225 [RPMHPD_GFX] = &gfx, 226 [RPMHPD_LCX] = &lcx, 227 [RPMHPD_LMX] = &lmx, 228 [RPMHPD_MSS] = &mss, 229 [RPMHPD_MX] = &mx, 230 [RPMHPD_MX_AO] = &mx_ao, 231 }; 232 233 static const struct rpmhpd_desc milos_desc = { 234 .rpmhpds = milos_rpmhpds, 235 .num_pds = ARRAY_SIZE(milos_rpmhpds), 236 }; 237 238 /* SA8540P RPMH powerdomains */ 239 static struct rpmhpd *sa8540p_rpmhpds[] = { 240 [SC8280XP_CX] = &cx, 241 [SC8280XP_CX_AO] = &cx_ao, 242 [SC8280XP_EBI] = &ebi, 243 [SC8280XP_LCX] = &lcx, 244 [SC8280XP_LMX] = &lmx, 245 [SC8280XP_MMCX] = &mmcx, 246 [SC8280XP_MMCX_AO] = &mmcx_ao, 247 [SC8280XP_MX] = &mx, 248 [SC8280XP_MX_AO] = &mx_ao, 249 [SC8280XP_NSP] = &nsp, 250 }; 251 252 static const struct rpmhpd_desc sa8540p_desc = { 253 .rpmhpds = sa8540p_rpmhpds, 254 .num_pds = ARRAY_SIZE(sa8540p_rpmhpds), 255 }; 256 257 /* SA8775P RPMH power domains */ 258 static struct rpmhpd *sa8775p_rpmhpds[] = { 259 [SA8775P_CX] = &cx, 260 [SA8775P_CX_AO] = &cx_ao, 261 [SA8775P_EBI] = &ebi, 262 [SA8775P_GFX] = &gfx, 263 [SA8775P_LCX] = &lcx, 264 [SA8775P_LMX] = &lmx, 265 [SA8775P_MMCX] = &mmcx, 266 [SA8775P_MMCX_AO] = &mmcx_ao, 267 [SA8775P_MXC] = &mxc, 268 [SA8775P_MXC_AO] = &mxc_ao, 269 [SA8775P_MX] = &mx, 270 [SA8775P_MX_AO] = &mx_ao, 271 [SA8775P_NSP0] = &nsp0, 272 [SA8775P_NSP1] = &nsp1, 273 }; 274 275 static const struct rpmhpd_desc sa8775p_desc = { 276 .rpmhpds = sa8775p_rpmhpds, 277 .num_pds = ARRAY_SIZE(sa8775p_rpmhpds), 278 }; 279 280 /* SAR2130P RPMH powerdomains */ 281 static struct rpmhpd *sar2130p_rpmhpds[] = { 282 [RPMHPD_CX] = &cx, 283 [RPMHPD_CX_AO] = &cx_ao, 284 [RPMHPD_EBI] = &ebi, 285 [RPMHPD_GFX] = &gfx, 286 [RPMHPD_LCX] = &lcx, 287 [RPMHPD_LMX] = &lmx, 288 [RPMHPD_MMCX] = &mmcx_w_cx_parent, 289 [RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent, 290 [RPMHPD_MSS] = &mss, 291 [RPMHPD_MX] = &mx, 292 [RPMHPD_MX_AO] = &mx_ao, 293 [RPMHPD_MXC] = &mxc, 294 [RPMHPD_MXC_AO] = &mxc_ao, 295 [RPMHPD_NSP] = &nsp, 296 [RPMHPD_QPHY] = &qphy, 297 }; 298 299 static const struct rpmhpd_desc sar2130p_desc = { 300 .rpmhpds = sar2130p_rpmhpds, 301 .num_pds = ARRAY_SIZE(sar2130p_rpmhpds), 302 }; 303 304 /* SDM670 RPMH powerdomains */ 305 static struct rpmhpd *sdm670_rpmhpds[] = { 306 [SDM670_CX] = &cx_w_mx_parent, 307 [SDM670_CX_AO] = &cx_ao_w_mx_parent, 308 [SDM670_GFX] = &gfx, 309 [SDM670_LCX] = &lcx, 310 [SDM670_LMX] = &lmx, 311 [SDM670_MSS] = &mss, 312 [SDM670_MX] = &mx, 313 [SDM670_MX_AO] = &mx_ao, 314 }; 315 316 static const struct rpmhpd_desc sdm670_desc = { 317 .rpmhpds = sdm670_rpmhpds, 318 .num_pds = ARRAY_SIZE(sdm670_rpmhpds), 319 }; 320 321 /* SDM845 RPMH powerdomains */ 322 static struct rpmhpd *sdm845_rpmhpds[] = { 323 [SDM845_CX] = &cx_w_mx_parent, 324 [SDM845_CX_AO] = &cx_ao_w_mx_parent, 325 [SDM845_EBI] = &ebi, 326 [SDM845_GFX] = &gfx, 327 [SDM845_LCX] = &lcx, 328 [SDM845_LMX] = &lmx, 329 [SDM845_MSS] = &mss, 330 [SDM845_MX] = &mx, 331 [SDM845_MX_AO] = &mx_ao, 332 }; 333 334 static const struct rpmhpd_desc sdm845_desc = { 335 .rpmhpds = sdm845_rpmhpds, 336 .num_pds = ARRAY_SIZE(sdm845_rpmhpds), 337 }; 338 339 /* SDX55 RPMH powerdomains */ 340 static struct rpmhpd *sdx55_rpmhpds[] = { 341 [SDX55_CX] = &cx_w_mx_parent, 342 [SDX55_MSS] = &mss, 343 [SDX55_MX] = &mx, 344 }; 345 346 static const struct rpmhpd_desc sdx55_desc = { 347 .rpmhpds = sdx55_rpmhpds, 348 .num_pds = ARRAY_SIZE(sdx55_rpmhpds), 349 }; 350 351 /* SDX65 RPMH powerdomains */ 352 static struct rpmhpd *sdx65_rpmhpds[] = { 353 [SDX65_CX] = &cx_w_mx_parent, 354 [SDX65_CX_AO] = &cx_ao_w_mx_parent, 355 [SDX65_MSS] = &mss, 356 [SDX65_MX] = &mx, 357 [SDX65_MX_AO] = &mx_ao, 358 [SDX65_MXC] = &mxc, 359 }; 360 361 static const struct rpmhpd_desc sdx65_desc = { 362 .rpmhpds = sdx65_rpmhpds, 363 .num_pds = ARRAY_SIZE(sdx65_rpmhpds), 364 }; 365 366 /* SDX75 RPMH powerdomains */ 367 static struct rpmhpd *sdx75_rpmhpds[] = { 368 [RPMHPD_CX] = &cx, 369 [RPMHPD_CX_AO] = &cx_ao, 370 [RPMHPD_MSS] = &mss, 371 [RPMHPD_MX] = &mx, 372 [RPMHPD_MX_AO] = &mx_ao, 373 [RPMHPD_MXC] = &mxc, 374 }; 375 376 static const struct rpmhpd_desc sdx75_desc = { 377 .rpmhpds = sdx75_rpmhpds, 378 .num_pds = ARRAY_SIZE(sdx75_rpmhpds), 379 }; 380 381 /* SM4450 RPMH powerdomains */ 382 static struct rpmhpd *sm4450_rpmhpds[] = { 383 [RPMHPD_CX] = &cx, 384 [RPMHPD_CX_AO] = &cx_ao, 385 [RPMHPD_EBI] = &ebi, 386 [RPMHPD_LMX] = &lmx, 387 [RPMHPD_MSS] = &mss, 388 [RPMHPD_MX] = &mx, 389 }; 390 391 static const struct rpmhpd_desc sm4450_desc = { 392 .rpmhpds = sm4450_rpmhpds, 393 .num_pds = ARRAY_SIZE(sm4450_rpmhpds), 394 }; 395 396 /* SM6350 RPMH powerdomains */ 397 static struct rpmhpd *sm6350_rpmhpds[] = { 398 [SM6350_CX] = &cx_w_mx_parent, 399 [SM6350_GFX] = &gfx, 400 [SM6350_LCX] = &lcx, 401 [SM6350_LMX] = &lmx, 402 [SM6350_MSS] = &mss, 403 [SM6350_MX] = &mx, 404 }; 405 406 static const struct rpmhpd_desc sm6350_desc = { 407 .rpmhpds = sm6350_rpmhpds, 408 .num_pds = ARRAY_SIZE(sm6350_rpmhpds), 409 }; 410 411 /* SM7150 RPMH powerdomains */ 412 static struct rpmhpd *sm7150_rpmhpds[] = { 413 [RPMHPD_CX] = &cx_w_mx_parent, 414 [RPMHPD_CX_AO] = &cx_ao_w_mx_parent, 415 [RPMHPD_GFX] = &gfx, 416 [RPMHPD_LCX] = &lcx, 417 [RPMHPD_LMX] = &lmx, 418 [RPMHPD_MX] = &mx, 419 [RPMHPD_MX_AO] = &mx_ao, 420 [RPMHPD_MSS] = &mss, 421 }; 422 423 static const struct rpmhpd_desc sm7150_desc = { 424 .rpmhpds = sm7150_rpmhpds, 425 .num_pds = ARRAY_SIZE(sm7150_rpmhpds), 426 }; 427 428 /* SM8150 RPMH powerdomains */ 429 static struct rpmhpd *sm8150_rpmhpds[] = { 430 [SM8150_CX] = &cx_w_mx_parent, 431 [SM8150_CX_AO] = &cx_ao_w_mx_parent, 432 [SM8150_EBI] = &ebi, 433 [SM8150_GFX] = &gfx, 434 [SM8150_LCX] = &lcx, 435 [SM8150_LMX] = &lmx, 436 [SM8150_MMCX] = &mmcx, 437 [SM8150_MMCX_AO] = &mmcx_ao, 438 [SM8150_MSS] = &mss, 439 [SM8150_MX] = &mx, 440 [SM8150_MX_AO] = &mx_ao, 441 }; 442 443 static const struct rpmhpd_desc sm8150_desc = { 444 .rpmhpds = sm8150_rpmhpds, 445 .num_pds = ARRAY_SIZE(sm8150_rpmhpds), 446 }; 447 448 static struct rpmhpd *sa8155p_rpmhpds[] = { 449 [SA8155P_CX] = &cx_w_mx_parent, 450 [SA8155P_CX_AO] = &cx_ao_w_mx_parent, 451 [SA8155P_EBI] = &ebi, 452 [SA8155P_GFX] = &gfx, 453 [SA8155P_MSS] = &mss, 454 [SA8155P_MX] = &mx, 455 [SA8155P_MX_AO] = &mx_ao, 456 }; 457 458 static const struct rpmhpd_desc sa8155p_desc = { 459 .rpmhpds = sa8155p_rpmhpds, 460 .num_pds = ARRAY_SIZE(sa8155p_rpmhpds), 461 }; 462 463 /* SM8250 RPMH powerdomains */ 464 static struct rpmhpd *sm8250_rpmhpds[] = { 465 [RPMHPD_CX] = &cx_w_mx_parent, 466 [RPMHPD_CX_AO] = &cx_ao_w_mx_parent, 467 [RPMHPD_EBI] = &ebi, 468 [RPMHPD_GFX] = &gfx, 469 [RPMHPD_LCX] = &lcx, 470 [RPMHPD_LMX] = &lmx, 471 [RPMHPD_MMCX] = &mmcx, 472 [RPMHPD_MMCX_AO] = &mmcx_ao, 473 [RPMHPD_MX] = &mx, 474 [RPMHPD_MX_AO] = &mx_ao, 475 }; 476 477 static const struct rpmhpd_desc sm8250_desc = { 478 .rpmhpds = sm8250_rpmhpds, 479 .num_pds = ARRAY_SIZE(sm8250_rpmhpds), 480 }; 481 482 /* SM8350 Power domains */ 483 static struct rpmhpd *sm8350_rpmhpds[] = { 484 [RPMHPD_CX] = &cx_w_mx_parent, 485 [RPMHPD_CX_AO] = &cx_ao_w_mx_parent, 486 [RPMHPD_EBI] = &ebi, 487 [RPMHPD_GFX] = &gfx, 488 [RPMHPD_LCX] = &lcx, 489 [RPMHPD_LMX] = &lmx, 490 [RPMHPD_MMCX] = &mmcx, 491 [RPMHPD_MMCX_AO] = &mmcx_ao, 492 [RPMHPD_MSS] = &mss, 493 [RPMHPD_MX] = &mx, 494 [RPMHPD_MX_AO] = &mx_ao, 495 [RPMHPD_MXC] = &mxc, 496 [RPMHPD_MXC_AO] = &mxc_ao, 497 }; 498 499 static const struct rpmhpd_desc sm8350_desc = { 500 .rpmhpds = sm8350_rpmhpds, 501 .num_pds = ARRAY_SIZE(sm8350_rpmhpds), 502 }; 503 504 /* SM8450 RPMH powerdomains */ 505 static struct rpmhpd *sm8450_rpmhpds[] = { 506 [RPMHPD_CX] = &cx, 507 [RPMHPD_CX_AO] = &cx_ao, 508 [RPMHPD_EBI] = &ebi, 509 [RPMHPD_GFX] = &gfx, 510 [RPMHPD_LCX] = &lcx, 511 [RPMHPD_LMX] = &lmx, 512 [RPMHPD_MMCX] = &mmcx_w_cx_parent, 513 [RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent, 514 [RPMHPD_MSS] = &mss, 515 [RPMHPD_MX] = &mx, 516 [RPMHPD_MX_AO] = &mx_ao, 517 [RPMHPD_MXC] = &mxc, 518 [RPMHPD_MXC_AO] = &mxc_ao, 519 }; 520 521 static const struct rpmhpd_desc sm8450_desc = { 522 .rpmhpds = sm8450_rpmhpds, 523 .num_pds = ARRAY_SIZE(sm8450_rpmhpds), 524 }; 525 526 /* SM8550 RPMH powerdomains */ 527 static struct rpmhpd *sm8550_rpmhpds[] = { 528 [RPMHPD_CX] = &cx, 529 [RPMHPD_CX_AO] = &cx_ao, 530 [RPMHPD_EBI] = &ebi, 531 [RPMHPD_GFX] = &gfx, 532 [RPMHPD_LCX] = &lcx, 533 [RPMHPD_LMX] = &lmx, 534 [RPMHPD_MMCX] = &mmcx_w_cx_parent, 535 [RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent, 536 [RPMHPD_MSS] = &mss, 537 [RPMHPD_MX] = &mx, 538 [RPMHPD_MX_AO] = &mx_ao, 539 [RPMHPD_MXC] = &mxc, 540 [RPMHPD_MXC_AO] = &mxc_ao, 541 [RPMHPD_NSP] = &nsp, 542 }; 543 544 static const struct rpmhpd_desc sm8550_desc = { 545 .rpmhpds = sm8550_rpmhpds, 546 .num_pds = ARRAY_SIZE(sm8550_rpmhpds), 547 }; 548 549 /* SM8650 RPMH powerdomains */ 550 static struct rpmhpd *sm8650_rpmhpds[] = { 551 [RPMHPD_CX] = &cx, 552 [RPMHPD_CX_AO] = &cx_ao, 553 [RPMHPD_EBI] = &ebi, 554 [RPMHPD_GFX] = &gfx, 555 [RPMHPD_LCX] = &lcx, 556 [RPMHPD_LMX] = &lmx, 557 [RPMHPD_MMCX] = &mmcx_w_cx_parent, 558 [RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent, 559 [RPMHPD_MSS] = &mss, 560 [RPMHPD_MX] = &mx, 561 [RPMHPD_MX_AO] = &mx_ao, 562 [RPMHPD_MXC] = &mxc, 563 [RPMHPD_MXC_AO] = &mxc_ao, 564 [RPMHPD_NSP] = &nsp, 565 [RPMHPD_NSP2] = &nsp2, 566 }; 567 568 static const struct rpmhpd_desc sm8650_desc = { 569 .rpmhpds = sm8650_rpmhpds, 570 .num_pds = ARRAY_SIZE(sm8650_rpmhpds), 571 }; 572 573 /* SM8750 RPMH powerdomains */ 574 static struct rpmhpd *sm8750_rpmhpds[] = { 575 [RPMHPD_CX] = &cx, 576 [RPMHPD_CX_AO] = &cx_ao, 577 [RPMHPD_EBI] = &ebi, 578 [RPMHPD_GFX] = &gfx, 579 [RPMHPD_GMXC] = &gmxc, 580 [RPMHPD_LCX] = &lcx, 581 [RPMHPD_LMX] = &lmx, 582 [RPMHPD_MX] = &mx, 583 [RPMHPD_MX_AO] = &mx_ao, 584 [RPMHPD_MMCX] = &mmcx_w_cx_parent, 585 [RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent, 586 [RPMHPD_MSS] = &mss, 587 [RPMHPD_MXC] = &mxc, 588 [RPMHPD_MXC_AO] = &mxc_ao, 589 [RPMHPD_NSP] = &nsp, 590 [RPMHPD_NSP2] = &nsp2, 591 }; 592 593 static const struct rpmhpd_desc sm8750_desc = { 594 .rpmhpds = sm8750_rpmhpds, 595 .num_pds = ARRAY_SIZE(sm8750_rpmhpds), 596 }; 597 598 /* KAANAPALI RPMH powerdomains */ 599 static struct rpmhpd *kaanapali_rpmhpds[] = { 600 [RPMHPD_CX] = &cx, 601 [RPMHPD_CX_AO] = &cx_ao, 602 [RPMHPD_EBI] = &ebi, 603 [RPMHPD_GFX] = &gfx, 604 [RPMHPD_GMXC] = &gmxc, 605 [RPMHPD_LCX] = &lcx, 606 [RPMHPD_LMX] = &lmx, 607 [RPMHPD_MX] = &mx, 608 [RPMHPD_MX_AO] = &mx_ao, 609 [RPMHPD_MMCX] = &mmcx, 610 [RPMHPD_MMCX_AO] = &mmcx_ao, 611 [RPMHPD_MSS] = &mss, 612 [RPMHPD_MXC] = &mxc, 613 [RPMHPD_MXC_AO] = &mxc_ao, 614 [RPMHPD_NSP] = &nsp, 615 [RPMHPD_NSP2] = &nsp2, 616 }; 617 618 static const struct rpmhpd_desc kaanapali_desc = { 619 .rpmhpds = kaanapali_rpmhpds, 620 .num_pds = ARRAY_SIZE(kaanapali_rpmhpds), 621 }; 622 623 /* QDU1000/QRU1000 RPMH powerdomains */ 624 static struct rpmhpd *qdu1000_rpmhpds[] = { 625 [QDU1000_CX] = &cx, 626 [QDU1000_EBI] = &ebi, 627 [QDU1000_MSS] = &mss, 628 [QDU1000_MX] = &mx, 629 }; 630 631 static const struct rpmhpd_desc qdu1000_desc = { 632 .rpmhpds = qdu1000_rpmhpds, 633 .num_pds = ARRAY_SIZE(qdu1000_rpmhpds), 634 }; 635 636 /* SC7180 RPMH powerdomains */ 637 static struct rpmhpd *sc7180_rpmhpds[] = { 638 [SC7180_CX] = &cx_w_mx_parent, 639 [SC7180_CX_AO] = &cx_ao_w_mx_parent, 640 [SC7180_GFX] = &gfx, 641 [SC7180_LCX] = &lcx, 642 [SC7180_LMX] = &lmx, 643 [SC7180_MSS] = &mss, 644 [SC7180_MX] = &mx, 645 [SC7180_MX_AO] = &mx_ao, 646 }; 647 648 static const struct rpmhpd_desc sc7180_desc = { 649 .rpmhpds = sc7180_rpmhpds, 650 .num_pds = ARRAY_SIZE(sc7180_rpmhpds), 651 }; 652 653 /* SC7280 RPMH powerdomains */ 654 static struct rpmhpd *sc7280_rpmhpds[] = { 655 [SC7280_CX] = &cx, 656 [SC7280_CX_AO] = &cx_ao, 657 [SC7280_EBI] = &ebi, 658 [SC7280_GFX] = &gfx, 659 [SC7280_LCX] = &lcx, 660 [SC7280_LMX] = &lmx, 661 [SC7280_MSS] = &mss, 662 [SC7280_MX] = &mx, 663 [SC7280_MX_AO] = &mx_ao, 664 }; 665 666 static const struct rpmhpd_desc sc7280_desc = { 667 .rpmhpds = sc7280_rpmhpds, 668 .num_pds = ARRAY_SIZE(sc7280_rpmhpds), 669 }; 670 671 /* SC8180x RPMH powerdomains */ 672 static struct rpmhpd *sc8180x_rpmhpds[] = { 673 [SC8180X_CX] = &cx_w_mx_parent, 674 [SC8180X_CX_AO] = &cx_ao_w_mx_parent, 675 [SC8180X_EBI] = &ebi, 676 [SC8180X_GFX] = &gfx, 677 [SC8180X_LCX] = &lcx, 678 [SC8180X_LMX] = &lmx, 679 [SC8180X_MMCX] = &mmcx, 680 [SC8180X_MMCX_AO] = &mmcx_ao, 681 [SC8180X_MSS] = &mss, 682 [SC8180X_MX] = &mx, 683 [SC8180X_MX_AO] = &mx_ao, 684 }; 685 686 static const struct rpmhpd_desc sc8180x_desc = { 687 .rpmhpds = sc8180x_rpmhpds, 688 .num_pds = ARRAY_SIZE(sc8180x_rpmhpds), 689 }; 690 691 /* SC8280xp RPMH powerdomains */ 692 static struct rpmhpd *sc8280xp_rpmhpds[] = { 693 [SC8280XP_CX] = &cx, 694 [SC8280XP_CX_AO] = &cx_ao, 695 [SC8280XP_EBI] = &ebi, 696 [SC8280XP_GFX] = &gfx, 697 [SC8280XP_LCX] = &lcx, 698 [SC8280XP_LMX] = &lmx, 699 [SC8280XP_MMCX] = &mmcx, 700 [SC8280XP_MMCX_AO] = &mmcx_ao, 701 [SC8280XP_MX] = &mx, 702 [SC8280XP_MX_AO] = &mx_ao, 703 [SC8280XP_NSP] = &nsp, 704 [SC8280XP_QPHY] = &qphy, 705 }; 706 707 static const struct rpmhpd_desc sc8280xp_desc = { 708 .rpmhpds = sc8280xp_rpmhpds, 709 .num_pds = ARRAY_SIZE(sc8280xp_rpmhpds), 710 }; 711 712 /* Glymur RPMH powerdomains */ 713 static struct rpmhpd *glymur_rpmhpds[] = { 714 [RPMHPD_CX] = &cx, 715 [RPMHPD_CX_AO] = &cx_ao, 716 [RPMHPD_EBI] = &ebi, 717 [RPMHPD_GFX] = &gfx, 718 [RPMHPD_LCX] = &lcx, 719 [RPMHPD_LMX] = &lmx, 720 [RPMHPD_MMCX] = &mmcx, 721 [RPMHPD_MMCX_AO] = &mmcx_ao, 722 [RPMHPD_MX] = &mx, 723 [RPMHPD_MX_AO] = &mx_ao, 724 [RPMHPD_MXC] = &mxc, 725 [RPMHPD_MXC_AO] = &mxc_ao, 726 [RPMHPD_MSS] = &mss, 727 [RPMHPD_NSP] = &nsp, 728 [RPMHPD_NSP2] = &nsp2, 729 [RPMHPD_GMXC] = &gmxc, 730 }; 731 732 static const struct rpmhpd_desc glymur_desc = { 733 .rpmhpds = glymur_rpmhpds, 734 .num_pds = ARRAY_SIZE(glymur_rpmhpds), 735 }; 736 737 /* X1E80100 RPMH powerdomains */ 738 static struct rpmhpd *x1e80100_rpmhpds[] = { 739 [RPMHPD_CX] = &cx, 740 [RPMHPD_CX_AO] = &cx_ao, 741 [RPMHPD_EBI] = &ebi, 742 [RPMHPD_GFX] = &gfx, 743 [RPMHPD_LCX] = &lcx, 744 [RPMHPD_LMX] = &lmx, 745 [RPMHPD_MMCX] = &mmcx, 746 [RPMHPD_MMCX_AO] = &mmcx_ao, 747 [RPMHPD_MX] = &mx, 748 [RPMHPD_MX_AO] = &mx_ao, 749 [RPMHPD_NSP] = &nsp, 750 [RPMHPD_MXC] = &mxc, 751 [RPMHPD_GMXC] = &gmxc, 752 }; 753 754 static const struct rpmhpd_desc x1e80100_desc = { 755 .rpmhpds = x1e80100_rpmhpds, 756 .num_pds = ARRAY_SIZE(x1e80100_rpmhpds), 757 }; 758 759 /* QCS8300 RPMH power domains */ 760 static struct rpmhpd *qcs8300_rpmhpds[] = { 761 [RPMHPD_CX] = &cx, 762 [RPMHPD_CX_AO] = &cx_ao, 763 [RPMHPD_EBI] = &ebi, 764 [RPMHPD_GFX] = &gfx, 765 [RPMHPD_LCX] = &lcx, 766 [RPMHPD_LMX] = &lmx, 767 [RPMHPD_MMCX] = &mmcx_w_cx_parent, 768 [RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent, 769 [RPMHPD_MXC] = &mxc, 770 [RPMHPD_MXC_AO] = &mxc_ao, 771 [RPMHPD_MX] = &mx, 772 [RPMHPD_MX_AO] = &mx_ao, 773 [RPMHPD_NSP0] = &nsp0, 774 [RPMHPD_NSP1] = &nsp1, 775 }; 776 777 static const struct rpmhpd_desc qcs8300_desc = { 778 .rpmhpds = qcs8300_rpmhpds, 779 .num_pds = ARRAY_SIZE(qcs8300_rpmhpds), 780 }; 781 782 /* QCS615 RPMH powerdomains */ 783 static struct rpmhpd *qcs615_rpmhpds[] = { 784 [RPMHPD_CX] = &cx, 785 [RPMHPD_CX_AO] = &cx_ao, 786 }; 787 788 static const struct rpmhpd_desc qcs615_desc = { 789 .rpmhpds = qcs615_rpmhpds, 790 .num_pds = ARRAY_SIZE(qcs615_rpmhpds), 791 }; 792 793 static const struct of_device_id rpmhpd_match_table[] = { 794 { .compatible = "qcom,glymur-rpmhpd", .data = &glymur_desc }, 795 { .compatible = "qcom,kaanapali-rpmhpd", .data = &kaanapali_desc }, 796 { .compatible = "qcom,milos-rpmhpd", .data = &milos_desc }, 797 { .compatible = "qcom,qcs615-rpmhpd", .data = &qcs615_desc }, 798 { .compatible = "qcom,qcs8300-rpmhpd", .data = &qcs8300_desc }, 799 { .compatible = "qcom,qdu1000-rpmhpd", .data = &qdu1000_desc }, 800 { .compatible = "qcom,sa8155p-rpmhpd", .data = &sa8155p_desc }, 801 { .compatible = "qcom,sa8540p-rpmhpd", .data = &sa8540p_desc }, 802 { .compatible = "qcom,sa8775p-rpmhpd", .data = &sa8775p_desc }, 803 { .compatible = "qcom,sar2130p-rpmhpd", .data = &sar2130p_desc}, 804 { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc }, 805 { .compatible = "qcom,sc7280-rpmhpd", .data = &sc7280_desc }, 806 { .compatible = "qcom,sc8180x-rpmhpd", .data = &sc8180x_desc }, 807 { .compatible = "qcom,sc8280xp-rpmhpd", .data = &sc8280xp_desc }, 808 { .compatible = "qcom,sdm670-rpmhpd", .data = &sdm670_desc }, 809 { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc }, 810 { .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc}, 811 { .compatible = "qcom,sdx65-rpmhpd", .data = &sdx65_desc}, 812 { .compatible = "qcom,sdx75-rpmhpd", .data = &sdx75_desc}, 813 { .compatible = "qcom,sm4450-rpmhpd", .data = &sm4450_desc }, 814 { .compatible = "qcom,sm6350-rpmhpd", .data = &sm6350_desc }, 815 { .compatible = "qcom,sm7150-rpmhpd", .data = &sm7150_desc }, 816 { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc }, 817 { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc }, 818 { .compatible = "qcom,sm8350-rpmhpd", .data = &sm8350_desc }, 819 { .compatible = "qcom,sm8450-rpmhpd", .data = &sm8450_desc }, 820 { .compatible = "qcom,sm8550-rpmhpd", .data = &sm8550_desc }, 821 { .compatible = "qcom,sm8650-rpmhpd", .data = &sm8650_desc }, 822 { .compatible = "qcom,sm8750-rpmhpd", .data = &sm8750_desc }, 823 { .compatible = "qcom,x1e80100-rpmhpd", .data = &x1e80100_desc }, 824 { } 825 }; 826 MODULE_DEVICE_TABLE(of, rpmhpd_match_table); 827 828 static int rpmhpd_send_corner(struct rpmhpd *pd, int state, 829 unsigned int corner, bool sync) 830 { 831 struct tcs_cmd cmd = { 832 .addr = pd->addr, 833 .data = corner, 834 }; 835 836 /* 837 * Wait for an ack only when we are increasing the 838 * perf state of the power domain 839 */ 840 if (sync) 841 return rpmh_write(pd->dev, state, &cmd, 1); 842 else 843 return rpmh_write_async(pd->dev, state, &cmd, 1); 844 } 845 846 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner, 847 unsigned int *active, unsigned int *sleep) 848 { 849 *active = corner; 850 851 if (pd->active_only) 852 *sleep = 0; 853 else 854 *sleep = *active; 855 } 856 857 /* 858 * This function is used to aggregate the votes across the active only 859 * resources and its peers. The aggregated votes are sent to RPMh as 860 * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes 861 * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh 862 * on system sleep). 863 * We send ACTIVE_ONLY votes for resources without any peers. For others, 864 * which have an active only peer, all 3 votes are sent. 865 */ 866 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner) 867 { 868 int ret; 869 struct rpmhpd *peer = pd->peer; 870 unsigned int active_corner, sleep_corner; 871 unsigned int this_active_corner = 0, this_sleep_corner = 0; 872 unsigned int peer_active_corner = 0, peer_sleep_corner = 0; 873 unsigned int peer_enabled_corner; 874 875 if (pd->state_synced) { 876 to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner); 877 } else { 878 /* Clamp to highest corner if sync_state hasn't happened */ 879 this_active_corner = pd->level_count - 1; 880 this_sleep_corner = pd->level_count - 1; 881 } 882 883 if (peer && peer->enabled) { 884 peer_enabled_corner = max(peer->corner, peer->enable_corner); 885 to_active_sleep(peer, peer_enabled_corner, &peer_active_corner, 886 &peer_sleep_corner); 887 } 888 889 active_corner = max(this_active_corner, peer_active_corner); 890 891 ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner, 892 active_corner > pd->active_corner); 893 if (ret) 894 return ret; 895 896 pd->active_corner = active_corner; 897 898 if (peer) { 899 peer->active_corner = active_corner; 900 901 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE, 902 active_corner, false); 903 if (ret) 904 return ret; 905 906 sleep_corner = max(this_sleep_corner, peer_sleep_corner); 907 908 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner, 909 false); 910 } 911 912 return ret; 913 } 914 915 static int rpmhpd_power_on(struct generic_pm_domain *domain) 916 { 917 struct rpmhpd *pd = domain_to_rpmhpd(domain); 918 unsigned int corner; 919 int ret; 920 921 mutex_lock(&rpmhpd_lock); 922 923 corner = max(pd->corner, pd->enable_corner); 924 ret = rpmhpd_aggregate_corner(pd, corner); 925 if (!ret) 926 pd->enabled = true; 927 928 mutex_unlock(&rpmhpd_lock); 929 930 return ret; 931 } 932 933 static int rpmhpd_power_off(struct generic_pm_domain *domain) 934 { 935 struct rpmhpd *pd = domain_to_rpmhpd(domain); 936 int ret; 937 938 mutex_lock(&rpmhpd_lock); 939 940 ret = rpmhpd_aggregate_corner(pd, 0); 941 if (!ret) 942 pd->enabled = false; 943 944 mutex_unlock(&rpmhpd_lock); 945 946 return ret; 947 } 948 949 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain, 950 unsigned int level) 951 { 952 struct rpmhpd *pd = domain_to_rpmhpd(domain); 953 int ret, i; 954 955 guard(mutex)(&rpmhpd_lock); 956 957 for (i = 0; i < pd->level_count; i++) 958 if (level <= pd->level[i]) 959 break; 960 961 /* 962 * If the level requested is more than that supported by the 963 * max corner, just set it to max anyway. 964 */ 965 if (i == pd->level_count) 966 i--; 967 968 if (pd->enabled) { 969 /* Ensure that the domain isn't turn off */ 970 if (i < pd->enable_corner) 971 i = pd->enable_corner; 972 973 ret = rpmhpd_aggregate_corner(pd, i); 974 if (ret) 975 return ret; 976 } 977 978 pd->corner = i; 979 980 return 0; 981 } 982 983 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd) 984 { 985 int i; 986 const u16 *buf; 987 988 buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count); 989 if (IS_ERR(buf)) 990 return PTR_ERR(buf); 991 992 /* 2 bytes used for each command DB aux data entry */ 993 rpmhpd->level_count >>= 1; 994 995 if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS) 996 return -EINVAL; 997 998 for (i = 0; i < rpmhpd->level_count; i++) { 999 if (rpmhpd->skip_retention_level && buf[i] == RPMH_REGULATOR_LEVEL_RETENTION) 1000 continue; 1001 1002 rpmhpd->level[i] = buf[i]; 1003 1004 /* Remember the first corner with non-zero level */ 1005 if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i]) 1006 rpmhpd->enable_corner = i; 1007 1008 /* 1009 * The AUX data may be zero padded. These 0 valued entries at 1010 * the end of the map must be ignored. 1011 */ 1012 if (i > 0 && rpmhpd->level[i] == 0) { 1013 rpmhpd->level_count = i; 1014 break; 1015 } 1016 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i, 1017 rpmhpd->level[i]); 1018 } 1019 1020 return 0; 1021 } 1022 1023 static int rpmhpd_probe(struct platform_device *pdev) 1024 { 1025 int i, ret; 1026 size_t num_pds; 1027 struct device *dev = &pdev->dev; 1028 struct genpd_onecell_data *data; 1029 struct rpmhpd **rpmhpds; 1030 const struct rpmhpd_desc *desc; 1031 1032 desc = of_device_get_match_data(dev); 1033 if (!desc) 1034 return -EINVAL; 1035 1036 rpmhpds = desc->rpmhpds; 1037 num_pds = desc->num_pds; 1038 1039 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 1040 if (!data) 1041 return -ENOMEM; 1042 1043 data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains), 1044 GFP_KERNEL); 1045 if (!data->domains) 1046 return -ENOMEM; 1047 1048 data->num_domains = num_pds; 1049 1050 for (i = 0; i < num_pds; i++) { 1051 if (!rpmhpds[i]) 1052 continue; 1053 1054 rpmhpds[i]->dev = dev; 1055 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name); 1056 if (!rpmhpds[i]->addr) { 1057 dev_err(dev, "Could not find RPMh address for resource %s\n", 1058 rpmhpds[i]->res_name); 1059 return -ENODEV; 1060 } 1061 1062 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name); 1063 if (ret != CMD_DB_HW_ARC) { 1064 dev_err(dev, "RPMh slave ID mismatch\n"); 1065 return -EINVAL; 1066 } 1067 1068 ret = rpmhpd_update_level_mapping(rpmhpds[i]); 1069 if (ret) 1070 return ret; 1071 1072 rpmhpds[i]->pd.power_off = rpmhpd_power_off; 1073 rpmhpds[i]->pd.power_on = rpmhpd_power_on; 1074 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state; 1075 pm_genpd_init(&rpmhpds[i]->pd, NULL, true); 1076 1077 data->domains[i] = &rpmhpds[i]->pd; 1078 } 1079 1080 /* Add subdomains */ 1081 for (i = 0; i < num_pds; i++) { 1082 if (!rpmhpds[i]) 1083 continue; 1084 if (rpmhpds[i]->parent) 1085 pm_genpd_add_subdomain(rpmhpds[i]->parent, 1086 &rpmhpds[i]->pd); 1087 } 1088 1089 return of_genpd_add_provider_onecell(pdev->dev.of_node, data); 1090 } 1091 1092 static void rpmhpd_sync_state(struct device *dev) 1093 { 1094 const struct rpmhpd_desc *desc = of_device_get_match_data(dev); 1095 struct rpmhpd **rpmhpds = desc->rpmhpds; 1096 unsigned int corner; 1097 struct rpmhpd *pd; 1098 unsigned int i; 1099 int ret; 1100 1101 of_genpd_sync_state(dev->of_node); 1102 1103 mutex_lock(&rpmhpd_lock); 1104 for (i = 0; i < desc->num_pds; i++) { 1105 pd = rpmhpds[i]; 1106 if (!pd) 1107 continue; 1108 1109 pd->state_synced = true; 1110 if (pd->enabled) 1111 corner = max(pd->corner, pd->enable_corner); 1112 else 1113 corner = 0; 1114 1115 ret = rpmhpd_aggregate_corner(pd, corner); 1116 if (ret) 1117 dev_err(dev, "failed to sync %s\n", pd->res_name); 1118 } 1119 mutex_unlock(&rpmhpd_lock); 1120 } 1121 1122 static struct platform_driver rpmhpd_driver = { 1123 .driver = { 1124 .name = "qcom-rpmhpd", 1125 .of_match_table = rpmhpd_match_table, 1126 .suppress_bind_attrs = true, 1127 .sync_state = rpmhpd_sync_state, 1128 }, 1129 .probe = rpmhpd_probe, 1130 }; 1131 1132 static int __init rpmhpd_init(void) 1133 { 1134 return platform_driver_register(&rpmhpd_driver); 1135 } 1136 core_initcall(rpmhpd_init); 1137 1138 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver"); 1139 MODULE_LICENSE("GPL v2"); 1140