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 16 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 /* QDU1000/QRU1000 RPMH powerdomains */ 599 static struct rpmhpd *qdu1000_rpmhpds[] = { 600 [QDU1000_CX] = &cx, 601 [QDU1000_EBI] = &ebi, 602 [QDU1000_MSS] = &mss, 603 [QDU1000_MX] = &mx, 604 }; 605 606 static const struct rpmhpd_desc qdu1000_desc = { 607 .rpmhpds = qdu1000_rpmhpds, 608 .num_pds = ARRAY_SIZE(qdu1000_rpmhpds), 609 }; 610 611 /* SC7180 RPMH powerdomains */ 612 static struct rpmhpd *sc7180_rpmhpds[] = { 613 [SC7180_CX] = &cx_w_mx_parent, 614 [SC7180_CX_AO] = &cx_ao_w_mx_parent, 615 [SC7180_GFX] = &gfx, 616 [SC7180_LCX] = &lcx, 617 [SC7180_LMX] = &lmx, 618 [SC7180_MSS] = &mss, 619 [SC7180_MX] = &mx, 620 [SC7180_MX_AO] = &mx_ao, 621 }; 622 623 static const struct rpmhpd_desc sc7180_desc = { 624 .rpmhpds = sc7180_rpmhpds, 625 .num_pds = ARRAY_SIZE(sc7180_rpmhpds), 626 }; 627 628 /* SC7280 RPMH powerdomains */ 629 static struct rpmhpd *sc7280_rpmhpds[] = { 630 [SC7280_CX] = &cx, 631 [SC7280_CX_AO] = &cx_ao, 632 [SC7280_EBI] = &ebi, 633 [SC7280_GFX] = &gfx, 634 [SC7280_LCX] = &lcx, 635 [SC7280_LMX] = &lmx, 636 [SC7280_MSS] = &mss, 637 [SC7280_MX] = &mx, 638 [SC7280_MX_AO] = &mx_ao, 639 }; 640 641 static const struct rpmhpd_desc sc7280_desc = { 642 .rpmhpds = sc7280_rpmhpds, 643 .num_pds = ARRAY_SIZE(sc7280_rpmhpds), 644 }; 645 646 /* SC8180x RPMH powerdomains */ 647 static struct rpmhpd *sc8180x_rpmhpds[] = { 648 [SC8180X_CX] = &cx_w_mx_parent, 649 [SC8180X_CX_AO] = &cx_ao_w_mx_parent, 650 [SC8180X_EBI] = &ebi, 651 [SC8180X_GFX] = &gfx, 652 [SC8180X_LCX] = &lcx, 653 [SC8180X_LMX] = &lmx, 654 [SC8180X_MMCX] = &mmcx, 655 [SC8180X_MMCX_AO] = &mmcx_ao, 656 [SC8180X_MSS] = &mss, 657 [SC8180X_MX] = &mx, 658 [SC8180X_MX_AO] = &mx_ao, 659 }; 660 661 static const struct rpmhpd_desc sc8180x_desc = { 662 .rpmhpds = sc8180x_rpmhpds, 663 .num_pds = ARRAY_SIZE(sc8180x_rpmhpds), 664 }; 665 666 /* SC8280xp RPMH powerdomains */ 667 static struct rpmhpd *sc8280xp_rpmhpds[] = { 668 [SC8280XP_CX] = &cx, 669 [SC8280XP_CX_AO] = &cx_ao, 670 [SC8280XP_EBI] = &ebi, 671 [SC8280XP_GFX] = &gfx, 672 [SC8280XP_LCX] = &lcx, 673 [SC8280XP_LMX] = &lmx, 674 [SC8280XP_MMCX] = &mmcx, 675 [SC8280XP_MMCX_AO] = &mmcx_ao, 676 [SC8280XP_MX] = &mx, 677 [SC8280XP_MX_AO] = &mx_ao, 678 [SC8280XP_NSP] = &nsp, 679 [SC8280XP_QPHY] = &qphy, 680 }; 681 682 static const struct rpmhpd_desc sc8280xp_desc = { 683 .rpmhpds = sc8280xp_rpmhpds, 684 .num_pds = ARRAY_SIZE(sc8280xp_rpmhpds), 685 }; 686 687 /* Glymur RPMH powerdomains */ 688 static struct rpmhpd *glymur_rpmhpds[] = { 689 [RPMHPD_CX] = &cx, 690 [RPMHPD_CX_AO] = &cx_ao, 691 [RPMHPD_EBI] = &ebi, 692 [RPMHPD_GFX] = &gfx, 693 [RPMHPD_LCX] = &lcx, 694 [RPMHPD_LMX] = &lmx, 695 [RPMHPD_MMCX] = &mmcx, 696 [RPMHPD_MMCX_AO] = &mmcx_ao, 697 [RPMHPD_MX] = &mx, 698 [RPMHPD_MX_AO] = &mx_ao, 699 [RPMHPD_MXC] = &mxc, 700 [RPMHPD_MXC_AO] = &mxc_ao, 701 [RPMHPD_MSS] = &mss, 702 [RPMHPD_NSP] = &nsp, 703 [RPMHPD_NSP2] = &nsp2, 704 [RPMHPD_GMXC] = &gmxc, 705 }; 706 707 static const struct rpmhpd_desc glymur_desc = { 708 .rpmhpds = glymur_rpmhpds, 709 .num_pds = ARRAY_SIZE(glymur_rpmhpds), 710 }; 711 712 /* X1E80100 RPMH powerdomains */ 713 static struct rpmhpd *x1e80100_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_NSP] = &nsp, 725 [RPMHPD_MXC] = &mxc, 726 [RPMHPD_GMXC] = &gmxc, 727 }; 728 729 static const struct rpmhpd_desc x1e80100_desc = { 730 .rpmhpds = x1e80100_rpmhpds, 731 .num_pds = ARRAY_SIZE(x1e80100_rpmhpds), 732 }; 733 734 /* QCS8300 RPMH power domains */ 735 static struct rpmhpd *qcs8300_rpmhpds[] = { 736 [RPMHPD_CX] = &cx, 737 [RPMHPD_CX_AO] = &cx_ao, 738 [RPMHPD_EBI] = &ebi, 739 [RPMHPD_GFX] = &gfx, 740 [RPMHPD_LCX] = &lcx, 741 [RPMHPD_LMX] = &lmx, 742 [RPMHPD_MMCX] = &mmcx_w_cx_parent, 743 [RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent, 744 [RPMHPD_MXC] = &mxc, 745 [RPMHPD_MXC_AO] = &mxc_ao, 746 [RPMHPD_MX] = &mx, 747 [RPMHPD_MX_AO] = &mx_ao, 748 [RPMHPD_NSP0] = &nsp0, 749 [RPMHPD_NSP1] = &nsp1, 750 }; 751 752 static const struct rpmhpd_desc qcs8300_desc = { 753 .rpmhpds = qcs8300_rpmhpds, 754 .num_pds = ARRAY_SIZE(qcs8300_rpmhpds), 755 }; 756 757 /* QCS615 RPMH powerdomains */ 758 static struct rpmhpd *qcs615_rpmhpds[] = { 759 [RPMHPD_CX] = &cx, 760 [RPMHPD_CX_AO] = &cx_ao, 761 }; 762 763 static const struct rpmhpd_desc qcs615_desc = { 764 .rpmhpds = qcs615_rpmhpds, 765 .num_pds = ARRAY_SIZE(qcs615_rpmhpds), 766 }; 767 768 static const struct of_device_id rpmhpd_match_table[] = { 769 { .compatible = "qcom,glymur-rpmhpd", .data = &glymur_desc }, 770 { .compatible = "qcom,milos-rpmhpd", .data = &milos_desc }, 771 { .compatible = "qcom,qcs615-rpmhpd", .data = &qcs615_desc }, 772 { .compatible = "qcom,qcs8300-rpmhpd", .data = &qcs8300_desc }, 773 { .compatible = "qcom,qdu1000-rpmhpd", .data = &qdu1000_desc }, 774 { .compatible = "qcom,sa8155p-rpmhpd", .data = &sa8155p_desc }, 775 { .compatible = "qcom,sa8540p-rpmhpd", .data = &sa8540p_desc }, 776 { .compatible = "qcom,sa8775p-rpmhpd", .data = &sa8775p_desc }, 777 { .compatible = "qcom,sar2130p-rpmhpd", .data = &sar2130p_desc}, 778 { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc }, 779 { .compatible = "qcom,sc7280-rpmhpd", .data = &sc7280_desc }, 780 { .compatible = "qcom,sc8180x-rpmhpd", .data = &sc8180x_desc }, 781 { .compatible = "qcom,sc8280xp-rpmhpd", .data = &sc8280xp_desc }, 782 { .compatible = "qcom,sdm670-rpmhpd", .data = &sdm670_desc }, 783 { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc }, 784 { .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc}, 785 { .compatible = "qcom,sdx65-rpmhpd", .data = &sdx65_desc}, 786 { .compatible = "qcom,sdx75-rpmhpd", .data = &sdx75_desc}, 787 { .compatible = "qcom,sm4450-rpmhpd", .data = &sm4450_desc }, 788 { .compatible = "qcom,sm6350-rpmhpd", .data = &sm6350_desc }, 789 { .compatible = "qcom,sm7150-rpmhpd", .data = &sm7150_desc }, 790 { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc }, 791 { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc }, 792 { .compatible = "qcom,sm8350-rpmhpd", .data = &sm8350_desc }, 793 { .compatible = "qcom,sm8450-rpmhpd", .data = &sm8450_desc }, 794 { .compatible = "qcom,sm8550-rpmhpd", .data = &sm8550_desc }, 795 { .compatible = "qcom,sm8650-rpmhpd", .data = &sm8650_desc }, 796 { .compatible = "qcom,sm8750-rpmhpd", .data = &sm8750_desc }, 797 { .compatible = "qcom,x1e80100-rpmhpd", .data = &x1e80100_desc }, 798 { } 799 }; 800 MODULE_DEVICE_TABLE(of, rpmhpd_match_table); 801 802 static int rpmhpd_send_corner(struct rpmhpd *pd, int state, 803 unsigned int corner, bool sync) 804 { 805 struct tcs_cmd cmd = { 806 .addr = pd->addr, 807 .data = corner, 808 }; 809 810 /* 811 * Wait for an ack only when we are increasing the 812 * perf state of the power domain 813 */ 814 if (sync) 815 return rpmh_write(pd->dev, state, &cmd, 1); 816 else 817 return rpmh_write_async(pd->dev, state, &cmd, 1); 818 } 819 820 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner, 821 unsigned int *active, unsigned int *sleep) 822 { 823 *active = corner; 824 825 if (pd->active_only) 826 *sleep = 0; 827 else 828 *sleep = *active; 829 } 830 831 /* 832 * This function is used to aggregate the votes across the active only 833 * resources and its peers. The aggregated votes are sent to RPMh as 834 * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes 835 * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh 836 * on system sleep). 837 * We send ACTIVE_ONLY votes for resources without any peers. For others, 838 * which have an active only peer, all 3 votes are sent. 839 */ 840 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner) 841 { 842 int ret; 843 struct rpmhpd *peer = pd->peer; 844 unsigned int active_corner, sleep_corner; 845 unsigned int this_active_corner = 0, this_sleep_corner = 0; 846 unsigned int peer_active_corner = 0, peer_sleep_corner = 0; 847 unsigned int peer_enabled_corner; 848 849 if (pd->state_synced) { 850 to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner); 851 } else { 852 /* Clamp to highest corner if sync_state hasn't happened */ 853 this_active_corner = pd->level_count - 1; 854 this_sleep_corner = pd->level_count - 1; 855 } 856 857 if (peer && peer->enabled) { 858 peer_enabled_corner = max(peer->corner, peer->enable_corner); 859 to_active_sleep(peer, peer_enabled_corner, &peer_active_corner, 860 &peer_sleep_corner); 861 } 862 863 active_corner = max(this_active_corner, peer_active_corner); 864 865 ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner, 866 active_corner > pd->active_corner); 867 if (ret) 868 return ret; 869 870 pd->active_corner = active_corner; 871 872 if (peer) { 873 peer->active_corner = active_corner; 874 875 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE, 876 active_corner, false); 877 if (ret) 878 return ret; 879 880 sleep_corner = max(this_sleep_corner, peer_sleep_corner); 881 882 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner, 883 false); 884 } 885 886 return ret; 887 } 888 889 static int rpmhpd_power_on(struct generic_pm_domain *domain) 890 { 891 struct rpmhpd *pd = domain_to_rpmhpd(domain); 892 unsigned int corner; 893 int ret; 894 895 mutex_lock(&rpmhpd_lock); 896 897 corner = max(pd->corner, pd->enable_corner); 898 ret = rpmhpd_aggregate_corner(pd, corner); 899 if (!ret) 900 pd->enabled = true; 901 902 mutex_unlock(&rpmhpd_lock); 903 904 return ret; 905 } 906 907 static int rpmhpd_power_off(struct generic_pm_domain *domain) 908 { 909 struct rpmhpd *pd = domain_to_rpmhpd(domain); 910 int ret; 911 912 mutex_lock(&rpmhpd_lock); 913 914 ret = rpmhpd_aggregate_corner(pd, 0); 915 if (!ret) 916 pd->enabled = false; 917 918 mutex_unlock(&rpmhpd_lock); 919 920 return ret; 921 } 922 923 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain, 924 unsigned int level) 925 { 926 struct rpmhpd *pd = domain_to_rpmhpd(domain); 927 int ret, i; 928 929 guard(mutex)(&rpmhpd_lock); 930 931 for (i = 0; i < pd->level_count; i++) 932 if (level <= pd->level[i]) 933 break; 934 935 /* 936 * If the level requested is more than that supported by the 937 * max corner, just set it to max anyway. 938 */ 939 if (i == pd->level_count) 940 i--; 941 942 if (pd->enabled) { 943 /* Ensure that the domain isn't turn off */ 944 if (i < pd->enable_corner) 945 i = pd->enable_corner; 946 947 ret = rpmhpd_aggregate_corner(pd, i); 948 if (ret) 949 return ret; 950 } 951 952 pd->corner = i; 953 954 return 0; 955 } 956 957 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd) 958 { 959 int i; 960 const u16 *buf; 961 962 buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count); 963 if (IS_ERR(buf)) 964 return PTR_ERR(buf); 965 966 /* 2 bytes used for each command DB aux data entry */ 967 rpmhpd->level_count >>= 1; 968 969 if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS) 970 return -EINVAL; 971 972 for (i = 0; i < rpmhpd->level_count; i++) { 973 if (rpmhpd->skip_retention_level && buf[i] == RPMH_REGULATOR_LEVEL_RETENTION) 974 continue; 975 976 rpmhpd->level[i] = buf[i]; 977 978 /* Remember the first corner with non-zero level */ 979 if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i]) 980 rpmhpd->enable_corner = i; 981 982 /* 983 * The AUX data may be zero padded. These 0 valued entries at 984 * the end of the map must be ignored. 985 */ 986 if (i > 0 && rpmhpd->level[i] == 0) { 987 rpmhpd->level_count = i; 988 break; 989 } 990 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i, 991 rpmhpd->level[i]); 992 } 993 994 return 0; 995 } 996 997 static int rpmhpd_probe(struct platform_device *pdev) 998 { 999 int i, ret; 1000 size_t num_pds; 1001 struct device *dev = &pdev->dev; 1002 struct genpd_onecell_data *data; 1003 struct rpmhpd **rpmhpds; 1004 const struct rpmhpd_desc *desc; 1005 1006 desc = of_device_get_match_data(dev); 1007 if (!desc) 1008 return -EINVAL; 1009 1010 rpmhpds = desc->rpmhpds; 1011 num_pds = desc->num_pds; 1012 1013 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 1014 if (!data) 1015 return -ENOMEM; 1016 1017 data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains), 1018 GFP_KERNEL); 1019 if (!data->domains) 1020 return -ENOMEM; 1021 1022 data->num_domains = num_pds; 1023 1024 for (i = 0; i < num_pds; i++) { 1025 if (!rpmhpds[i]) 1026 continue; 1027 1028 rpmhpds[i]->dev = dev; 1029 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name); 1030 if (!rpmhpds[i]->addr) { 1031 dev_err(dev, "Could not find RPMh address for resource %s\n", 1032 rpmhpds[i]->res_name); 1033 return -ENODEV; 1034 } 1035 1036 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name); 1037 if (ret != CMD_DB_HW_ARC) { 1038 dev_err(dev, "RPMh slave ID mismatch\n"); 1039 return -EINVAL; 1040 } 1041 1042 ret = rpmhpd_update_level_mapping(rpmhpds[i]); 1043 if (ret) 1044 return ret; 1045 1046 rpmhpds[i]->pd.power_off = rpmhpd_power_off; 1047 rpmhpds[i]->pd.power_on = rpmhpd_power_on; 1048 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state; 1049 pm_genpd_init(&rpmhpds[i]->pd, NULL, true); 1050 1051 data->domains[i] = &rpmhpds[i]->pd; 1052 } 1053 1054 /* Add subdomains */ 1055 for (i = 0; i < num_pds; i++) { 1056 if (!rpmhpds[i]) 1057 continue; 1058 if (rpmhpds[i]->parent) 1059 pm_genpd_add_subdomain(rpmhpds[i]->parent, 1060 &rpmhpds[i]->pd); 1061 } 1062 1063 return of_genpd_add_provider_onecell(pdev->dev.of_node, data); 1064 } 1065 1066 static void rpmhpd_sync_state(struct device *dev) 1067 { 1068 const struct rpmhpd_desc *desc = of_device_get_match_data(dev); 1069 struct rpmhpd **rpmhpds = desc->rpmhpds; 1070 unsigned int corner; 1071 struct rpmhpd *pd; 1072 unsigned int i; 1073 int ret; 1074 1075 of_genpd_sync_state(dev->of_node); 1076 1077 mutex_lock(&rpmhpd_lock); 1078 for (i = 0; i < desc->num_pds; i++) { 1079 pd = rpmhpds[i]; 1080 if (!pd) 1081 continue; 1082 1083 pd->state_synced = true; 1084 if (pd->enabled) 1085 corner = max(pd->corner, pd->enable_corner); 1086 else 1087 corner = 0; 1088 1089 ret = rpmhpd_aggregate_corner(pd, corner); 1090 if (ret) 1091 dev_err(dev, "failed to sync %s\n", pd->res_name); 1092 } 1093 mutex_unlock(&rpmhpd_lock); 1094 } 1095 1096 static struct platform_driver rpmhpd_driver = { 1097 .driver = { 1098 .name = "qcom-rpmhpd", 1099 .of_match_table = rpmhpd_match_table, 1100 .suppress_bind_attrs = true, 1101 .sync_state = rpmhpd_sync_state, 1102 }, 1103 .probe = rpmhpd_probe, 1104 }; 1105 1106 static int __init rpmhpd_init(void) 1107 { 1108 return platform_driver_register(&rpmhpd_driver); 1109 } 1110 core_initcall(rpmhpd_init); 1111 1112 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver"); 1113 MODULE_LICENSE("GPL v2"); 1114