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 * @peer: A peer power domain in case Active only Voting is
29 * @active_only: True if it represents an Active only peer
30 * @level: An array of level (vlvl) to corner (hlvl) mappings
32 * @level_count: Number of levels supported by the power domain. max
34 * @enabled: true if the power domain is enabled
35 * @res_name: Resource name used for cmd-db lookup
36 * @addr: Resource address as looped up using resource name from
41 struct generic_pm_domain pd;
42 struct generic_pm_domain *parent;
44 const bool active_only;
46 unsigned int active_corner;
47 u32 level[RPMH_ARC_MAX_LEVELS];
55 struct rpmhpd **rpmhpds;
59 static DEFINE_MUTEX(rpmhpd_lock);
61 /* SDM845 RPMH powerdomains */
63 static struct rpmhpd sdm845_ebi = {
64 .pd = { .name = "ebi", },
65 .res_name = "ebi.lvl",
68 static struct rpmhpd sdm845_lmx = {
69 .pd = { .name = "lmx", },
70 .res_name = "lmx.lvl",
73 static struct rpmhpd sdm845_lcx = {
74 .pd = { .name = "lcx", },
75 .res_name = "lcx.lvl",
78 static struct rpmhpd sdm845_gfx = {
79 .pd = { .name = "gfx", },
80 .res_name = "gfx.lvl",
83 static struct rpmhpd sdm845_mss = {
84 .pd = { .name = "mss", },
85 .res_name = "mss.lvl",
88 static struct rpmhpd sdm845_mx_ao;
89 static struct rpmhpd sdm845_mx = {
90 .pd = { .name = "mx", },
91 .peer = &sdm845_mx_ao,
95 static struct rpmhpd sdm845_mx_ao = {
96 .pd = { .name = "mx_ao", },
102 static struct rpmhpd sdm845_cx_ao;
103 static struct rpmhpd sdm845_cx = {
104 .pd = { .name = "cx", },
105 .peer = &sdm845_cx_ao,
106 .parent = &sdm845_mx.pd,
107 .res_name = "cx.lvl",
110 static struct rpmhpd sdm845_cx_ao = {
111 .pd = { .name = "cx_ao", },
114 .parent = &sdm845_mx_ao.pd,
115 .res_name = "cx.lvl",
118 static struct rpmhpd *sdm845_rpmhpds[] = {
119 [SDM845_EBI] = &sdm845_ebi,
120 [SDM845_MX] = &sdm845_mx,
121 [SDM845_MX_AO] = &sdm845_mx_ao,
122 [SDM845_CX] = &sdm845_cx,
123 [SDM845_CX_AO] = &sdm845_cx_ao,
124 [SDM845_LMX] = &sdm845_lmx,
125 [SDM845_LCX] = &sdm845_lcx,
126 [SDM845_GFX] = &sdm845_gfx,
127 [SDM845_MSS] = &sdm845_mss,
130 static const struct rpmhpd_desc sdm845_desc = {
131 .rpmhpds = sdm845_rpmhpds,
132 .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
135 /* SM8150 RPMH powerdomains */
137 static struct rpmhpd sm8150_mmcx_ao;
138 static struct rpmhpd sm8150_mmcx = {
139 .pd = { .name = "mmcx", },
140 .peer = &sm8150_mmcx_ao,
141 .res_name = "mmcx.lvl",
144 static struct rpmhpd sm8150_mmcx_ao = {
145 .pd = { .name = "mmcx_ao", },
147 .peer = &sm8150_mmcx,
148 .res_name = "mmcx.lvl",
151 static struct rpmhpd *sm8150_rpmhpds[] = {
152 [SM8150_MSS] = &sdm845_mss,
153 [SM8150_EBI] = &sdm845_ebi,
154 [SM8150_LMX] = &sdm845_lmx,
155 [SM8150_LCX] = &sdm845_lcx,
156 [SM8150_GFX] = &sdm845_gfx,
157 [SM8150_MX] = &sdm845_mx,
158 [SM8150_MX_AO] = &sdm845_mx_ao,
159 [SM8150_CX] = &sdm845_cx,
160 [SM8150_CX_AO] = &sdm845_cx_ao,
161 [SM8150_MMCX] = &sm8150_mmcx,
162 [SM8150_MMCX_AO] = &sm8150_mmcx_ao,
165 static const struct rpmhpd_desc sm8150_desc = {
166 .rpmhpds = sm8150_rpmhpds,
167 .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
170 static struct rpmhpd *sm8250_rpmhpds[] = {
171 [SM8250_CX] = &sdm845_cx,
172 [SM8250_CX_AO] = &sdm845_cx_ao,
173 [SM8250_EBI] = &sdm845_ebi,
174 [SM8250_GFX] = &sdm845_gfx,
175 [SM8250_LCX] = &sdm845_lcx,
176 [SM8250_LMX] = &sdm845_lmx,
177 [SM8250_MMCX] = &sm8150_mmcx,
178 [SM8250_MMCX_AO] = &sm8150_mmcx_ao,
179 [SM8250_MX] = &sdm845_mx,
180 [SM8250_MX_AO] = &sdm845_mx_ao,
183 static const struct rpmhpd_desc sm8250_desc = {
184 .rpmhpds = sm8250_rpmhpds,
185 .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
188 /* SC7180 RPMH powerdomains */
189 static struct rpmhpd *sc7180_rpmhpds[] = {
190 [SC7180_CX] = &sdm845_cx,
191 [SC7180_CX_AO] = &sdm845_cx_ao,
192 [SC7180_GFX] = &sdm845_gfx,
193 [SC7180_MX] = &sdm845_mx,
194 [SC7180_MX_AO] = &sdm845_mx_ao,
195 [SC7180_LMX] = &sdm845_lmx,
196 [SC7180_LCX] = &sdm845_lcx,
197 [SC7180_MSS] = &sdm845_mss,
200 static const struct rpmhpd_desc sc7180_desc = {
201 .rpmhpds = sc7180_rpmhpds,
202 .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
205 static const struct of_device_id rpmhpd_match_table[] = {
206 { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
207 { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
208 { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
209 { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
212 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
214 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
215 unsigned int corner, bool sync)
217 struct tcs_cmd cmd = {
223 * Wait for an ack only when we are increasing the
224 * perf state of the power domain
227 return rpmh_write(pd->dev, state, &cmd, 1);
229 return rpmh_write_async(pd->dev, state, &cmd, 1);
232 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
233 unsigned int *active, unsigned int *sleep)
244 * This function is used to aggregate the votes across the active only
245 * resources and its peers. The aggregated votes are sent to RPMh as
246 * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
247 * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
249 * We send ACTIVE_ONLY votes for resources without any peers. For others,
250 * which have an active only peer, all 3 votes are sent.
252 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
255 struct rpmhpd *peer = pd->peer;
256 unsigned int active_corner, sleep_corner;
257 unsigned int this_active_corner = 0, this_sleep_corner = 0;
258 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
260 to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
262 if (peer && peer->enabled)
263 to_active_sleep(peer, peer->corner, &peer_active_corner,
266 active_corner = max(this_active_corner, peer_active_corner);
268 ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
269 active_corner > pd->active_corner);
273 pd->active_corner = active_corner;
276 peer->active_corner = active_corner;
278 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
279 active_corner, false);
283 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
285 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
292 static int rpmhpd_power_on(struct generic_pm_domain *domain)
294 struct rpmhpd *pd = domain_to_rpmhpd(domain);
297 mutex_lock(&rpmhpd_lock);
300 ret = rpmhpd_aggregate_corner(pd, pd->corner);
305 mutex_unlock(&rpmhpd_lock);
310 static int rpmhpd_power_off(struct generic_pm_domain *domain)
312 struct rpmhpd *pd = domain_to_rpmhpd(domain);
315 mutex_lock(&rpmhpd_lock);
317 ret = rpmhpd_aggregate_corner(pd, pd->level[0]);
322 mutex_unlock(&rpmhpd_lock);
327 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
330 struct rpmhpd *pd = domain_to_rpmhpd(domain);
333 mutex_lock(&rpmhpd_lock);
335 for (i = 0; i < pd->level_count; i++)
336 if (level <= pd->level[i])
340 * If the level requested is more than that supported by the
341 * max corner, just set it to max anyway.
343 if (i == pd->level_count)
347 ret = rpmhpd_aggregate_corner(pd, i);
354 mutex_unlock(&rpmhpd_lock);
359 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
360 struct dev_pm_opp *opp)
362 return dev_pm_opp_get_level(opp);
365 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
370 buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
374 /* 2 bytes used for each command DB aux data entry */
375 rpmhpd->level_count >>= 1;
377 if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
380 for (i = 0; i < rpmhpd->level_count; i++) {
381 rpmhpd->level[i] = buf[i];
384 * The AUX data may be zero padded. These 0 valued entries at
385 * the end of the map must be ignored.
387 if (i > 0 && rpmhpd->level[i] == 0) {
388 rpmhpd->level_count = i;
391 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
398 static int rpmhpd_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
403 struct genpd_onecell_data *data;
404 struct rpmhpd **rpmhpds;
405 const struct rpmhpd_desc *desc;
407 desc = of_device_get_match_data(dev);
411 rpmhpds = desc->rpmhpds;
412 num_pds = desc->num_pds;
414 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
418 data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
423 data->num_domains = num_pds;
425 for (i = 0; i < num_pds; i++) {
427 dev_warn(dev, "rpmhpds[%d] is empty\n", i);
431 rpmhpds[i]->dev = dev;
432 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
433 if (!rpmhpds[i]->addr) {
434 dev_err(dev, "Could not find RPMh address for resource %s\n",
435 rpmhpds[i]->res_name);
439 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
440 if (ret != CMD_DB_HW_ARC) {
441 dev_err(dev, "RPMh slave ID mismatch\n");
445 ret = rpmhpd_update_level_mapping(rpmhpds[i]);
449 rpmhpds[i]->pd.power_off = rpmhpd_power_off;
450 rpmhpds[i]->pd.power_on = rpmhpd_power_on;
451 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
452 rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
453 pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
455 data->domains[i] = &rpmhpds[i]->pd;
459 for (i = 0; i < num_pds; i++) {
462 if (rpmhpds[i]->parent)
463 pm_genpd_add_subdomain(rpmhpds[i]->parent,
467 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
470 static struct platform_driver rpmhpd_driver = {
472 .name = "qcom-rpmhpd",
473 .of_match_table = rpmhpd_match_table,
474 .suppress_bind_attrs = true,
476 .probe = rpmhpd_probe,
479 static int __init rpmhpd_init(void)
481 return platform_driver_register(&rpmhpd_driver);
483 core_initcall(rpmhpd_init);
485 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
486 MODULE_LICENSE("GPL v2");