1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, The Linux Foundation. All rights reserved.*/
5 #include <linux/init.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/pm_domain.h>
10 #include <linux/slab.h>
12 #include <linux/of_device.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>
19 #define domain_to_rpmhpd(domain) container_of(domain, struct rpmhpd, pd)
21 #define RPMH_ARC_MAX_LEVELS 16
24 * struct rpmhpd - top level RPMh power domain resource data structure
25 * @dev: rpmh power domain controller device
26 * @pd: generic_pm_domain corrresponding to the power domain
27 * @parent: generic_pm_domain corrresponding to the parent's power domain
28 * @peer: A peer power domain in case Active only Voting is
30 * @active_only: True if it represents an Active only peer
31 * @corner: current corner
32 * @active_corner: current active corner
33 * @enable_corner: lowest non-zero corner
34 * @level: An array of level (vlvl) to corner (hlvl) mappings
36 * @level_count: Number of levels supported by the power domain. max
38 * @enabled: true if the power domain is enabled
39 * @res_name: Resource name used for cmd-db lookup
40 * @addr: Resource address as looped up using resource name from
45 struct generic_pm_domain pd;
46 struct generic_pm_domain *parent;
48 const bool active_only;
50 unsigned int active_corner;
51 unsigned int enable_corner;
52 u32 level[RPMH_ARC_MAX_LEVELS];
60 struct rpmhpd **rpmhpds;
64 static DEFINE_MUTEX(rpmhpd_lock);
66 /* RPMH powerdomains */
68 static struct rpmhpd cx_ao;
69 static struct rpmhpd mx;
70 static struct rpmhpd mx_ao;
71 static struct rpmhpd cx = {
72 .pd = { .name = "cx", },
77 static struct rpmhpd cx_ao = {
78 .pd = { .name = "cx_ao", },
84 static struct rpmhpd cx_ao_w_mx_parent;
85 static struct rpmhpd cx_w_mx_parent = {
86 .pd = { .name = "cx", },
87 .peer = &cx_ao_w_mx_parent,
92 static struct rpmhpd cx_ao_w_mx_parent = {
93 .pd = { .name = "cx_ao", },
95 .peer = &cx_w_mx_parent,
100 static struct rpmhpd ebi = {
101 .pd = { .name = "ebi", },
102 .res_name = "ebi.lvl",
105 static struct rpmhpd gfx = {
106 .pd = { .name = "gfx", },
107 .res_name = "gfx.lvl",
110 static struct rpmhpd lcx = {
111 .pd = { .name = "lcx", },
112 .res_name = "lcx.lvl",
115 static struct rpmhpd lmx = {
116 .pd = { .name = "lmx", },
117 .res_name = "lmx.lvl",
120 static struct rpmhpd mmcx_ao;
121 static struct rpmhpd mmcx = {
122 .pd = { .name = "mmcx", },
124 .res_name = "mmcx.lvl",
127 static struct rpmhpd mmcx_ao = {
128 .pd = { .name = "mmcx_ao", },
131 .res_name = "mmcx.lvl",
134 static struct rpmhpd mmcx_ao_w_cx_parent;
135 static struct rpmhpd mmcx_w_cx_parent = {
136 .pd = { .name = "mmcx", },
137 .peer = &mmcx_ao_w_cx_parent,
139 .res_name = "mmcx.lvl",
142 static struct rpmhpd mmcx_ao_w_cx_parent = {
143 .pd = { .name = "mmcx_ao", },
145 .peer = &mmcx_w_cx_parent,
147 .res_name = "mmcx.lvl",
150 static struct rpmhpd mss = {
151 .pd = { .name = "mss", },
152 .res_name = "mss.lvl",
155 static struct rpmhpd mx_ao;
156 static struct rpmhpd mx = {
157 .pd = { .name = "mx", },
159 .res_name = "mx.lvl",
162 static struct rpmhpd mx_ao = {
163 .pd = { .name = "mx_ao", },
166 .res_name = "mx.lvl",
169 static struct rpmhpd mxc_ao;
170 static struct rpmhpd mxc = {
171 .pd = { .name = "mxc", },
173 .res_name = "mxc.lvl",
176 static struct rpmhpd mxc_ao = {
177 .pd = { .name = "mxc_ao", },
180 .res_name = "mxc.lvl",
183 static struct rpmhpd nsp = {
184 .pd = { .name = "nsp", },
185 .res_name = "nsp.lvl",
188 static struct rpmhpd qphy = {
189 .pd = { .name = "qphy", },
190 .res_name = "qphy.lvl",
193 /* SA8540P RPMH powerdomains */
194 static struct rpmhpd *sa8540p_rpmhpds[] = {
196 [SC8280XP_CX_AO] = &cx_ao,
197 [SC8280XP_EBI] = &ebi,
198 [SC8280XP_GFX] = &gfx,
199 [SC8280XP_LCX] = &lcx,
200 [SC8280XP_LMX] = &lmx,
201 [SC8280XP_MMCX] = &mmcx,
202 [SC8280XP_MMCX_AO] = &mmcx_ao,
204 [SC8280XP_MX_AO] = &mx_ao,
205 [SC8280XP_NSP] = &nsp,
208 static const struct rpmhpd_desc sa8540p_desc = {
209 .rpmhpds = sa8540p_rpmhpds,
210 .num_pds = ARRAY_SIZE(sa8540p_rpmhpds),
213 /* SDM845 RPMH powerdomains */
214 static struct rpmhpd *sdm845_rpmhpds[] = {
215 [SDM845_CX] = &cx_w_mx_parent,
216 [SDM845_CX_AO] = &cx_ao_w_mx_parent,
223 [SDM845_MX_AO] = &mx_ao,
226 static const struct rpmhpd_desc sdm845_desc = {
227 .rpmhpds = sdm845_rpmhpds,
228 .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
231 /* SDX55 RPMH powerdomains */
232 static struct rpmhpd *sdx55_rpmhpds[] = {
233 [SDX55_CX] = &cx_w_mx_parent,
238 static const struct rpmhpd_desc sdx55_desc = {
239 .rpmhpds = sdx55_rpmhpds,
240 .num_pds = ARRAY_SIZE(sdx55_rpmhpds),
243 /* SDX65 RPMH powerdomains */
244 static struct rpmhpd *sdx65_rpmhpds[] = {
245 [SDX65_CX] = &cx_w_mx_parent,
246 [SDX65_CX_AO] = &cx_ao_w_mx_parent,
249 [SDX65_MX_AO] = &mx_ao,
253 static const struct rpmhpd_desc sdx65_desc = {
254 .rpmhpds = sdx65_rpmhpds,
255 .num_pds = ARRAY_SIZE(sdx65_rpmhpds),
258 /* SM6350 RPMH powerdomains */
259 static struct rpmhpd *sm6350_rpmhpds[] = {
260 [SM6350_CX] = &cx_w_mx_parent,
268 static const struct rpmhpd_desc sm6350_desc = {
269 .rpmhpds = sm6350_rpmhpds,
270 .num_pds = ARRAY_SIZE(sm6350_rpmhpds),
273 /* SM8150 RPMH powerdomains */
274 static struct rpmhpd *sm8150_rpmhpds[] = {
275 [SM8150_CX] = &cx_w_mx_parent,
276 [SM8150_CX_AO] = &cx_ao_w_mx_parent,
281 [SM8150_MMCX] = &mmcx,
282 [SM8150_MMCX_AO] = &mmcx_ao,
285 [SM8150_MX_AO] = &mx_ao,
288 static const struct rpmhpd_desc sm8150_desc = {
289 .rpmhpds = sm8150_rpmhpds,
290 .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
293 /* SM8250 RPMH powerdomains */
294 static struct rpmhpd *sm8250_rpmhpds[] = {
295 [SM8250_CX] = &cx_w_mx_parent,
296 [SM8250_CX_AO] = &cx_ao_w_mx_parent,
301 [SM8250_MMCX] = &mmcx,
302 [SM8250_MMCX_AO] = &mmcx_ao,
304 [SM8250_MX_AO] = &mx_ao,
307 static const struct rpmhpd_desc sm8250_desc = {
308 .rpmhpds = sm8250_rpmhpds,
309 .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
312 /* SM8350 Power domains */
313 static struct rpmhpd *sm8350_rpmhpds[] = {
314 [SM8350_CX] = &cx_w_mx_parent,
315 [SM8350_CX_AO] = &cx_ao_w_mx_parent,
320 [SM8350_MMCX] = &mmcx,
321 [SM8350_MMCX_AO] = &mmcx_ao,
324 [SM8350_MX_AO] = &mx_ao,
326 [SM8350_MXC_AO] = &mxc_ao,
329 static const struct rpmhpd_desc sm8350_desc = {
330 .rpmhpds = sm8350_rpmhpds,
331 .num_pds = ARRAY_SIZE(sm8350_rpmhpds),
334 /* SM8450 RPMH powerdomains */
335 static struct rpmhpd *sm8450_rpmhpds[] = {
337 [SM8450_CX_AO] = &cx_ao,
342 [SM8450_MMCX] = &mmcx_w_cx_parent,
343 [SM8450_MMCX_AO] = &mmcx_ao_w_cx_parent,
346 [SM8450_MX_AO] = &mx_ao,
348 [SM8450_MXC_AO] = &mxc_ao,
351 static const struct rpmhpd_desc sm8450_desc = {
352 .rpmhpds = sm8450_rpmhpds,
353 .num_pds = ARRAY_SIZE(sm8450_rpmhpds),
356 /* SC7180 RPMH powerdomains */
357 static struct rpmhpd *sc7180_rpmhpds[] = {
358 [SC7180_CX] = &cx_w_mx_parent,
359 [SC7180_CX_AO] = &cx_ao_w_mx_parent,
365 [SC7180_MX_AO] = &mx_ao,
368 static const struct rpmhpd_desc sc7180_desc = {
369 .rpmhpds = sc7180_rpmhpds,
370 .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
373 /* SC7280 RPMH powerdomains */
374 static struct rpmhpd *sc7280_rpmhpds[] = {
376 [SC7280_CX_AO] = &cx_ao,
383 [SC7280_MX_AO] = &mx_ao,
386 static const struct rpmhpd_desc sc7280_desc = {
387 .rpmhpds = sc7280_rpmhpds,
388 .num_pds = ARRAY_SIZE(sc7280_rpmhpds),
391 /* SC8180x RPMH powerdomains */
392 static struct rpmhpd *sc8180x_rpmhpds[] = {
393 [SC8180X_CX] = &cx_w_mx_parent,
394 [SC8180X_CX_AO] = &cx_ao_w_mx_parent,
395 [SC8180X_EBI] = &ebi,
396 [SC8180X_GFX] = &gfx,
397 [SC8180X_LCX] = &lcx,
398 [SC8180X_LMX] = &lmx,
399 [SC8180X_MMCX] = &mmcx,
400 [SC8180X_MMCX_AO] = &mmcx_ao,
401 [SC8180X_MSS] = &mss,
403 [SC8180X_MX_AO] = &mx_ao,
406 static const struct rpmhpd_desc sc8180x_desc = {
407 .rpmhpds = sc8180x_rpmhpds,
408 .num_pds = ARRAY_SIZE(sc8180x_rpmhpds),
411 /* SC8280xp RPMH powerdomains */
412 static struct rpmhpd *sc8280xp_rpmhpds[] = {
414 [SC8280XP_CX_AO] = &cx_ao,
415 [SC8280XP_EBI] = &ebi,
416 [SC8280XP_GFX] = &gfx,
417 [SC8280XP_LCX] = &lcx,
418 [SC8280XP_LMX] = &lmx,
419 [SC8280XP_MMCX] = &mmcx,
420 [SC8280XP_MMCX_AO] = &mmcx_ao,
422 [SC8280XP_MX_AO] = &mx_ao,
423 [SC8280XP_NSP] = &nsp,
424 [SC8280XP_QPHY] = &qphy,
427 static const struct rpmhpd_desc sc8280xp_desc = {
428 .rpmhpds = sc8280xp_rpmhpds,
429 .num_pds = ARRAY_SIZE(sc8280xp_rpmhpds),
432 static const struct of_device_id rpmhpd_match_table[] = {
433 { .compatible = "qcom,sa8540p-rpmhpd", .data = &sa8540p_desc },
434 { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
435 { .compatible = "qcom,sc7280-rpmhpd", .data = &sc7280_desc },
436 { .compatible = "qcom,sc8180x-rpmhpd", .data = &sc8180x_desc },
437 { .compatible = "qcom,sc8280xp-rpmhpd", .data = &sc8280xp_desc },
438 { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
439 { .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc},
440 { .compatible = "qcom,sdx65-rpmhpd", .data = &sdx65_desc},
441 { .compatible = "qcom,sm6350-rpmhpd", .data = &sm6350_desc },
442 { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
443 { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
444 { .compatible = "qcom,sm8350-rpmhpd", .data = &sm8350_desc },
445 { .compatible = "qcom,sm8450-rpmhpd", .data = &sm8450_desc },
448 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
450 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
451 unsigned int corner, bool sync)
453 struct tcs_cmd cmd = {
459 * Wait for an ack only when we are increasing the
460 * perf state of the power domain
463 return rpmh_write(pd->dev, state, &cmd, 1);
465 return rpmh_write_async(pd->dev, state, &cmd, 1);
468 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
469 unsigned int *active, unsigned int *sleep)
480 * This function is used to aggregate the votes across the active only
481 * resources and its peers. The aggregated votes are sent to RPMh as
482 * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
483 * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
485 * We send ACTIVE_ONLY votes for resources without any peers. For others,
486 * which have an active only peer, all 3 votes are sent.
488 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
491 struct rpmhpd *peer = pd->peer;
492 unsigned int active_corner, sleep_corner;
493 unsigned int this_active_corner = 0, this_sleep_corner = 0;
494 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
496 to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
498 if (peer && peer->enabled)
499 to_active_sleep(peer, peer->corner, &peer_active_corner,
502 active_corner = max(this_active_corner, peer_active_corner);
504 ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
505 active_corner > pd->active_corner);
509 pd->active_corner = active_corner;
512 peer->active_corner = active_corner;
514 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
515 active_corner, false);
519 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
521 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
528 static int rpmhpd_power_on(struct generic_pm_domain *domain)
530 struct rpmhpd *pd = domain_to_rpmhpd(domain);
534 mutex_lock(&rpmhpd_lock);
536 corner = max(pd->corner, pd->enable_corner);
537 ret = rpmhpd_aggregate_corner(pd, corner);
541 mutex_unlock(&rpmhpd_lock);
546 static int rpmhpd_power_off(struct generic_pm_domain *domain)
548 struct rpmhpd *pd = domain_to_rpmhpd(domain);
551 mutex_lock(&rpmhpd_lock);
553 ret = rpmhpd_aggregate_corner(pd, 0);
557 mutex_unlock(&rpmhpd_lock);
562 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
565 struct rpmhpd *pd = domain_to_rpmhpd(domain);
568 mutex_lock(&rpmhpd_lock);
570 for (i = 0; i < pd->level_count; i++)
571 if (level <= pd->level[i])
575 * If the level requested is more than that supported by the
576 * max corner, just set it to max anyway.
578 if (i == pd->level_count)
582 /* Ensure that the domain isn't turn off */
583 if (i < pd->enable_corner)
584 i = pd->enable_corner;
586 ret = rpmhpd_aggregate_corner(pd, i);
593 mutex_unlock(&rpmhpd_lock);
598 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
599 struct dev_pm_opp *opp)
601 return dev_pm_opp_get_level(opp);
604 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
609 buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
613 /* 2 bytes used for each command DB aux data entry */
614 rpmhpd->level_count >>= 1;
616 if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
619 for (i = 0; i < rpmhpd->level_count; i++) {
620 rpmhpd->level[i] = buf[i];
622 /* Remember the first corner with non-zero level */
623 if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
624 rpmhpd->enable_corner = i;
627 * The AUX data may be zero padded. These 0 valued entries at
628 * the end of the map must be ignored.
630 if (i > 0 && rpmhpd->level[i] == 0) {
631 rpmhpd->level_count = i;
634 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
641 static int rpmhpd_probe(struct platform_device *pdev)
645 struct device *dev = &pdev->dev;
646 struct genpd_onecell_data *data;
647 struct rpmhpd **rpmhpds;
648 const struct rpmhpd_desc *desc;
650 desc = of_device_get_match_data(dev);
654 rpmhpds = desc->rpmhpds;
655 num_pds = desc->num_pds;
657 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
661 data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
666 data->num_domains = num_pds;
668 for (i = 0; i < num_pds; i++) {
672 rpmhpds[i]->dev = dev;
673 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
674 if (!rpmhpds[i]->addr) {
675 dev_err(dev, "Could not find RPMh address for resource %s\n",
676 rpmhpds[i]->res_name);
680 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
681 if (ret != CMD_DB_HW_ARC) {
682 dev_err(dev, "RPMh slave ID mismatch\n");
686 ret = rpmhpd_update_level_mapping(rpmhpds[i]);
690 rpmhpds[i]->pd.power_off = rpmhpd_power_off;
691 rpmhpds[i]->pd.power_on = rpmhpd_power_on;
692 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
693 rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
694 pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
696 data->domains[i] = &rpmhpds[i]->pd;
700 for (i = 0; i < num_pds; i++) {
703 if (rpmhpds[i]->parent)
704 pm_genpd_add_subdomain(rpmhpds[i]->parent,
708 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
711 static struct platform_driver rpmhpd_driver = {
713 .name = "qcom-rpmhpd",
714 .of_match_table = rpmhpd_match_table,
715 .suppress_bind_attrs = true,
717 .probe = rpmhpd_probe,
720 static int __init rpmhpd_init(void)
722 return platform_driver_register(&rpmhpd_driver);
724 core_initcall(rpmhpd_init);
726 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
727 MODULE_LICENSE("GPL v2");