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 /* SDM845 RPMH powerdomains */
68 static struct rpmhpd sdm845_ebi = {
69 .pd = { .name = "ebi", },
70 .res_name = "ebi.lvl",
73 static struct rpmhpd sdm845_lmx = {
74 .pd = { .name = "lmx", },
75 .res_name = "lmx.lvl",
78 static struct rpmhpd sdm845_lcx = {
79 .pd = { .name = "lcx", },
80 .res_name = "lcx.lvl",
83 static struct rpmhpd sdm845_gfx = {
84 .pd = { .name = "gfx", },
85 .res_name = "gfx.lvl",
88 static struct rpmhpd sdm845_mss = {
89 .pd = { .name = "mss", },
90 .res_name = "mss.lvl",
93 static struct rpmhpd sdm845_mx_ao;
94 static struct rpmhpd sdm845_mx = {
95 .pd = { .name = "mx", },
96 .peer = &sdm845_mx_ao,
100 static struct rpmhpd sdm845_mx_ao = {
101 .pd = { .name = "mx_ao", },
104 .res_name = "mx.lvl",
107 static struct rpmhpd sdm845_cx_ao;
108 static struct rpmhpd sdm845_cx = {
109 .pd = { .name = "cx", },
110 .peer = &sdm845_cx_ao,
111 .parent = &sdm845_mx.pd,
112 .res_name = "cx.lvl",
115 static struct rpmhpd sdm845_cx_ao = {
116 .pd = { .name = "cx_ao", },
119 .parent = &sdm845_mx_ao.pd,
120 .res_name = "cx.lvl",
123 static struct rpmhpd *sdm845_rpmhpds[] = {
124 [SDM845_EBI] = &sdm845_ebi,
125 [SDM845_MX] = &sdm845_mx,
126 [SDM845_MX_AO] = &sdm845_mx_ao,
127 [SDM845_CX] = &sdm845_cx,
128 [SDM845_CX_AO] = &sdm845_cx_ao,
129 [SDM845_LMX] = &sdm845_lmx,
130 [SDM845_LCX] = &sdm845_lcx,
131 [SDM845_GFX] = &sdm845_gfx,
132 [SDM845_MSS] = &sdm845_mss,
135 static const struct rpmhpd_desc sdm845_desc = {
136 .rpmhpds = sdm845_rpmhpds,
137 .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
140 /* SM8150 RPMH powerdomains */
142 static struct rpmhpd sm8150_mmcx_ao;
143 static struct rpmhpd sm8150_mmcx = {
144 .pd = { .name = "mmcx", },
145 .peer = &sm8150_mmcx_ao,
146 .res_name = "mmcx.lvl",
149 static struct rpmhpd sm8150_mmcx_ao = {
150 .pd = { .name = "mmcx_ao", },
152 .peer = &sm8150_mmcx,
153 .res_name = "mmcx.lvl",
156 static struct rpmhpd *sm8150_rpmhpds[] = {
157 [SM8150_MSS] = &sdm845_mss,
158 [SM8150_EBI] = &sdm845_ebi,
159 [SM8150_LMX] = &sdm845_lmx,
160 [SM8150_LCX] = &sdm845_lcx,
161 [SM8150_GFX] = &sdm845_gfx,
162 [SM8150_MX] = &sdm845_mx,
163 [SM8150_MX_AO] = &sdm845_mx_ao,
164 [SM8150_CX] = &sdm845_cx,
165 [SM8150_CX_AO] = &sdm845_cx_ao,
166 [SM8150_MMCX] = &sm8150_mmcx,
167 [SM8150_MMCX_AO] = &sm8150_mmcx_ao,
170 static const struct rpmhpd_desc sm8150_desc = {
171 .rpmhpds = sm8150_rpmhpds,
172 .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
175 static struct rpmhpd *sm8250_rpmhpds[] = {
176 [SM8250_CX] = &sdm845_cx,
177 [SM8250_CX_AO] = &sdm845_cx_ao,
178 [SM8250_EBI] = &sdm845_ebi,
179 [SM8250_GFX] = &sdm845_gfx,
180 [SM8250_LCX] = &sdm845_lcx,
181 [SM8250_LMX] = &sdm845_lmx,
182 [SM8250_MMCX] = &sm8150_mmcx,
183 [SM8250_MMCX_AO] = &sm8150_mmcx_ao,
184 [SM8250_MX] = &sdm845_mx,
185 [SM8250_MX_AO] = &sdm845_mx_ao,
188 static const struct rpmhpd_desc sm8250_desc = {
189 .rpmhpds = sm8250_rpmhpds,
190 .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
193 /* SC7180 RPMH powerdomains */
194 static struct rpmhpd *sc7180_rpmhpds[] = {
195 [SC7180_CX] = &sdm845_cx,
196 [SC7180_CX_AO] = &sdm845_cx_ao,
197 [SC7180_GFX] = &sdm845_gfx,
198 [SC7180_MX] = &sdm845_mx,
199 [SC7180_MX_AO] = &sdm845_mx_ao,
200 [SC7180_LMX] = &sdm845_lmx,
201 [SC7180_LCX] = &sdm845_lcx,
202 [SC7180_MSS] = &sdm845_mss,
205 static const struct rpmhpd_desc sc7180_desc = {
206 .rpmhpds = sc7180_rpmhpds,
207 .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
210 static const struct of_device_id rpmhpd_match_table[] = {
211 { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
212 { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
213 { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
214 { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
217 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
219 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
220 unsigned int corner, bool sync)
222 struct tcs_cmd cmd = {
228 * Wait for an ack only when we are increasing the
229 * perf state of the power domain
232 return rpmh_write(pd->dev, state, &cmd, 1);
234 return rpmh_write_async(pd->dev, state, &cmd, 1);
237 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
238 unsigned int *active, unsigned int *sleep)
249 * This function is used to aggregate the votes across the active only
250 * resources and its peers. The aggregated votes are sent to RPMh as
251 * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
252 * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
254 * We send ACTIVE_ONLY votes for resources without any peers. For others,
255 * which have an active only peer, all 3 votes are sent.
257 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
260 struct rpmhpd *peer = pd->peer;
261 unsigned int active_corner, sleep_corner;
262 unsigned int this_active_corner = 0, this_sleep_corner = 0;
263 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
265 to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
267 if (peer && peer->enabled)
268 to_active_sleep(peer, peer->corner, &peer_active_corner,
271 active_corner = max(this_active_corner, peer_active_corner);
273 ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
274 active_corner > pd->active_corner);
278 pd->active_corner = active_corner;
281 peer->active_corner = active_corner;
283 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
284 active_corner, false);
288 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
290 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
297 static int rpmhpd_power_on(struct generic_pm_domain *domain)
299 struct rpmhpd *pd = domain_to_rpmhpd(domain);
303 mutex_lock(&rpmhpd_lock);
305 corner = max(pd->corner, pd->enable_corner);
306 ret = rpmhpd_aggregate_corner(pd, corner);
310 mutex_unlock(&rpmhpd_lock);
315 static int rpmhpd_power_off(struct generic_pm_domain *domain)
317 struct rpmhpd *pd = domain_to_rpmhpd(domain);
320 mutex_lock(&rpmhpd_lock);
322 ret = rpmhpd_aggregate_corner(pd, 0);
326 mutex_unlock(&rpmhpd_lock);
331 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
334 struct rpmhpd *pd = domain_to_rpmhpd(domain);
337 mutex_lock(&rpmhpd_lock);
339 for (i = 0; i < pd->level_count; i++)
340 if (level <= pd->level[i])
344 * If the level requested is more than that supported by the
345 * max corner, just set it to max anyway.
347 if (i == pd->level_count)
351 /* Ensure that the domain isn't turn off */
352 if (i < pd->enable_corner)
353 i = pd->enable_corner;
355 ret = rpmhpd_aggregate_corner(pd, i);
362 mutex_unlock(&rpmhpd_lock);
367 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
368 struct dev_pm_opp *opp)
370 return dev_pm_opp_get_level(opp);
373 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
378 buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
382 /* 2 bytes used for each command DB aux data entry */
383 rpmhpd->level_count >>= 1;
385 if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
388 for (i = 0; i < rpmhpd->level_count; i++) {
389 rpmhpd->level[i] = buf[i];
391 /* Remember the first corner with non-zero level */
392 if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
393 rpmhpd->enable_corner = i;
396 * The AUX data may be zero padded. These 0 valued entries at
397 * the end of the map must be ignored.
399 if (i > 0 && rpmhpd->level[i] == 0) {
400 rpmhpd->level_count = i;
403 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
410 static int rpmhpd_probe(struct platform_device *pdev)
414 struct device *dev = &pdev->dev;
415 struct genpd_onecell_data *data;
416 struct rpmhpd **rpmhpds;
417 const struct rpmhpd_desc *desc;
419 desc = of_device_get_match_data(dev);
423 rpmhpds = desc->rpmhpds;
424 num_pds = desc->num_pds;
426 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
430 data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
435 data->num_domains = num_pds;
437 for (i = 0; i < num_pds; i++) {
439 dev_warn(dev, "rpmhpds[%d] is empty\n", i);
443 rpmhpds[i]->dev = dev;
444 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
445 if (!rpmhpds[i]->addr) {
446 dev_err(dev, "Could not find RPMh address for resource %s\n",
447 rpmhpds[i]->res_name);
451 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
452 if (ret != CMD_DB_HW_ARC) {
453 dev_err(dev, "RPMh slave ID mismatch\n");
457 ret = rpmhpd_update_level_mapping(rpmhpds[i]);
461 rpmhpds[i]->pd.power_off = rpmhpd_power_off;
462 rpmhpds[i]->pd.power_on = rpmhpd_power_on;
463 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
464 rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
465 pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
467 data->domains[i] = &rpmhpds[i]->pd;
471 for (i = 0; i < num_pds; i++) {
474 if (rpmhpds[i]->parent)
475 pm_genpd_add_subdomain(rpmhpds[i]->parent,
479 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
482 static struct platform_driver rpmhpd_driver = {
484 .name = "qcom-rpmhpd",
485 .of_match_table = rpmhpd_match_table,
486 .suppress_bind_attrs = true,
488 .probe = rpmhpd_probe,
491 static int __init rpmhpd_init(void)
493 return platform_driver_register(&rpmhpd_driver);
495 core_initcall(rpmhpd_init);
497 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
498 MODULE_LICENSE("GPL v2");