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 /* SDM845 RPMH powerdomains */
184 static struct rpmhpd *sdm845_rpmhpds[] = {
185 [SDM845_CX] = &cx_w_mx_parent,
186 [SDM845_CX_AO] = &cx_ao_w_mx_parent,
193 [SDM845_MX_AO] = &mx_ao,
196 static const struct rpmhpd_desc sdm845_desc = {
197 .rpmhpds = sdm845_rpmhpds,
198 .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
201 /* SDX55 RPMH powerdomains */
202 static struct rpmhpd *sdx55_rpmhpds[] = {
203 [SDX55_CX] = &cx_w_mx_parent,
208 static const struct rpmhpd_desc sdx55_desc = {
209 .rpmhpds = sdx55_rpmhpds,
210 .num_pds = ARRAY_SIZE(sdx55_rpmhpds),
213 /* SM6350 RPMH powerdomains */
214 static struct rpmhpd *sm6350_rpmhpds[] = {
215 [SM6350_CX] = &cx_w_mx_parent,
223 static const struct rpmhpd_desc sm6350_desc = {
224 .rpmhpds = sm6350_rpmhpds,
225 .num_pds = ARRAY_SIZE(sm6350_rpmhpds),
228 /* SM8150 RPMH powerdomains */
229 static struct rpmhpd *sm8150_rpmhpds[] = {
230 [SM8150_CX] = &cx_w_mx_parent,
231 [SM8150_CX_AO] = &cx_ao_w_mx_parent,
236 [SM8150_MMCX] = &mmcx,
237 [SM8150_MMCX_AO] = &mmcx_ao,
240 [SM8150_MX_AO] = &mx_ao,
243 static const struct rpmhpd_desc sm8150_desc = {
244 .rpmhpds = sm8150_rpmhpds,
245 .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
248 /* SM8250 RPMH powerdomains */
249 static struct rpmhpd *sm8250_rpmhpds[] = {
250 [SM8250_CX] = &cx_w_mx_parent,
251 [SM8250_CX_AO] = &cx_ao_w_mx_parent,
256 [SM8250_MMCX] = &mmcx,
257 [SM8250_MMCX_AO] = &mmcx_ao,
259 [SM8250_MX_AO] = &mx_ao,
262 static const struct rpmhpd_desc sm8250_desc = {
263 .rpmhpds = sm8250_rpmhpds,
264 .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
267 /* SM8350 Power domains */
268 static struct rpmhpd *sm8350_rpmhpds[] = {
269 [SM8350_CX] = &cx_w_mx_parent,
270 [SM8350_CX_AO] = &cx_ao_w_mx_parent,
275 [SM8350_MMCX] = &mmcx,
276 [SM8350_MMCX_AO] = &mmcx_ao,
279 [SM8350_MX_AO] = &mx_ao,
281 [SM8350_MXC_AO] = &mxc_ao,
284 static const struct rpmhpd_desc sm8350_desc = {
285 .rpmhpds = sm8350_rpmhpds,
286 .num_pds = ARRAY_SIZE(sm8350_rpmhpds),
289 /* SM8450 RPMH powerdomains */
290 static struct rpmhpd *sm8450_rpmhpds[] = {
292 [SM8450_CX_AO] = &cx_ao,
297 [SM8450_MMCX] = &mmcx_w_cx_parent,
298 [SM8450_MMCX_AO] = &mmcx_ao_w_cx_parent,
301 [SM8450_MX_AO] = &mx_ao,
303 [SM8450_MXC_AO] = &mxc_ao,
306 static const struct rpmhpd_desc sm8450_desc = {
307 .rpmhpds = sm8450_rpmhpds,
308 .num_pds = ARRAY_SIZE(sm8450_rpmhpds),
311 /* SC7180 RPMH powerdomains */
312 static struct rpmhpd *sc7180_rpmhpds[] = {
313 [SC7180_CX] = &cx_w_mx_parent,
314 [SC7180_CX_AO] = &cx_ao_w_mx_parent,
320 [SC7180_MX_AO] = &mx_ao,
323 static const struct rpmhpd_desc sc7180_desc = {
324 .rpmhpds = sc7180_rpmhpds,
325 .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
328 /* SC7280 RPMH powerdomains */
329 static struct rpmhpd *sc7280_rpmhpds[] = {
331 [SC7280_CX_AO] = &cx_ao,
338 [SC7280_MX_AO] = &mx_ao,
341 static const struct rpmhpd_desc sc7280_desc = {
342 .rpmhpds = sc7280_rpmhpds,
343 .num_pds = ARRAY_SIZE(sc7280_rpmhpds),
346 /* SC8180x RPMH powerdomains */
347 static struct rpmhpd *sc8180x_rpmhpds[] = {
348 [SC8180X_CX] = &cx_w_mx_parent,
349 [SC8180X_CX_AO] = &cx_ao_w_mx_parent,
350 [SC8180X_EBI] = &ebi,
351 [SC8180X_GFX] = &gfx,
352 [SC8180X_LCX] = &lcx,
353 [SC8180X_LMX] = &lmx,
354 [SC8180X_MMCX] = &mmcx,
355 [SC8180X_MMCX_AO] = &mmcx_ao,
356 [SC8180X_MSS] = &mss,
358 [SC8180X_MX_AO] = &mx_ao,
361 static const struct rpmhpd_desc sc8180x_desc = {
362 .rpmhpds = sc8180x_rpmhpds,
363 .num_pds = ARRAY_SIZE(sc8180x_rpmhpds),
366 static const struct of_device_id rpmhpd_match_table[] = {
367 { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
368 { .compatible = "qcom,sc7280-rpmhpd", .data = &sc7280_desc },
369 { .compatible = "qcom,sc8180x-rpmhpd", .data = &sc8180x_desc },
370 { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
371 { .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc},
372 { .compatible = "qcom,sm6350-rpmhpd", .data = &sm6350_desc },
373 { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
374 { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
375 { .compatible = "qcom,sm8350-rpmhpd", .data = &sm8350_desc },
376 { .compatible = "qcom,sm8450-rpmhpd", .data = &sm8450_desc },
379 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
381 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
382 unsigned int corner, bool sync)
384 struct tcs_cmd cmd = {
390 * Wait for an ack only when we are increasing the
391 * perf state of the power domain
394 return rpmh_write(pd->dev, state, &cmd, 1);
396 return rpmh_write_async(pd->dev, state, &cmd, 1);
399 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
400 unsigned int *active, unsigned int *sleep)
411 * This function is used to aggregate the votes across the active only
412 * resources and its peers. The aggregated votes are sent to RPMh as
413 * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
414 * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
416 * We send ACTIVE_ONLY votes for resources without any peers. For others,
417 * which have an active only peer, all 3 votes are sent.
419 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
422 struct rpmhpd *peer = pd->peer;
423 unsigned int active_corner, sleep_corner;
424 unsigned int this_active_corner = 0, this_sleep_corner = 0;
425 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
427 to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
429 if (peer && peer->enabled)
430 to_active_sleep(peer, peer->corner, &peer_active_corner,
433 active_corner = max(this_active_corner, peer_active_corner);
435 ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
436 active_corner > pd->active_corner);
440 pd->active_corner = active_corner;
443 peer->active_corner = active_corner;
445 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
446 active_corner, false);
450 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
452 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
459 static int rpmhpd_power_on(struct generic_pm_domain *domain)
461 struct rpmhpd *pd = domain_to_rpmhpd(domain);
465 mutex_lock(&rpmhpd_lock);
467 corner = max(pd->corner, pd->enable_corner);
468 ret = rpmhpd_aggregate_corner(pd, corner);
472 mutex_unlock(&rpmhpd_lock);
477 static int rpmhpd_power_off(struct generic_pm_domain *domain)
479 struct rpmhpd *pd = domain_to_rpmhpd(domain);
482 mutex_lock(&rpmhpd_lock);
484 ret = rpmhpd_aggregate_corner(pd, 0);
488 mutex_unlock(&rpmhpd_lock);
493 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
496 struct rpmhpd *pd = domain_to_rpmhpd(domain);
499 mutex_lock(&rpmhpd_lock);
501 for (i = 0; i < pd->level_count; i++)
502 if (level <= pd->level[i])
506 * If the level requested is more than that supported by the
507 * max corner, just set it to max anyway.
509 if (i == pd->level_count)
513 /* Ensure that the domain isn't turn off */
514 if (i < pd->enable_corner)
515 i = pd->enable_corner;
517 ret = rpmhpd_aggregate_corner(pd, i);
524 mutex_unlock(&rpmhpd_lock);
529 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
530 struct dev_pm_opp *opp)
532 return dev_pm_opp_get_level(opp);
535 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
540 buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
544 /* 2 bytes used for each command DB aux data entry */
545 rpmhpd->level_count >>= 1;
547 if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
550 for (i = 0; i < rpmhpd->level_count; i++) {
551 rpmhpd->level[i] = buf[i];
553 /* Remember the first corner with non-zero level */
554 if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
555 rpmhpd->enable_corner = i;
558 * The AUX data may be zero padded. These 0 valued entries at
559 * the end of the map must be ignored.
561 if (i > 0 && rpmhpd->level[i] == 0) {
562 rpmhpd->level_count = i;
565 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
572 static int rpmhpd_probe(struct platform_device *pdev)
576 struct device *dev = &pdev->dev;
577 struct genpd_onecell_data *data;
578 struct rpmhpd **rpmhpds;
579 const struct rpmhpd_desc *desc;
581 desc = of_device_get_match_data(dev);
585 rpmhpds = desc->rpmhpds;
586 num_pds = desc->num_pds;
588 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
592 data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
597 data->num_domains = num_pds;
599 for (i = 0; i < num_pds; i++) {
601 dev_warn(dev, "rpmhpds[%d] is empty\n", i);
605 rpmhpds[i]->dev = dev;
606 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
607 if (!rpmhpds[i]->addr) {
608 dev_err(dev, "Could not find RPMh address for resource %s\n",
609 rpmhpds[i]->res_name);
613 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
614 if (ret != CMD_DB_HW_ARC) {
615 dev_err(dev, "RPMh slave ID mismatch\n");
619 ret = rpmhpd_update_level_mapping(rpmhpds[i]);
623 rpmhpds[i]->pd.power_off = rpmhpd_power_off;
624 rpmhpds[i]->pd.power_on = rpmhpd_power_on;
625 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
626 rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
627 pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
629 data->domains[i] = &rpmhpds[i]->pd;
633 for (i = 0; i < num_pds; i++) {
636 if (rpmhpds[i]->parent)
637 pm_genpd_add_subdomain(rpmhpds[i]->parent,
641 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
644 static struct platform_driver rpmhpd_driver = {
646 .name = "qcom-rpmhpd",
647 .of_match_table = rpmhpd_match_table,
648 .suppress_bind_attrs = true,
650 .probe = rpmhpd_probe,
653 static int __init rpmhpd_init(void)
655 return platform_driver_register(&rpmhpd_driver);
657 core_initcall(rpmhpd_init);
659 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
660 MODULE_LICENSE("GPL v2");