Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / soc / qcom / rpmhpd.c
blob7ce06356d24c9dadae50b0fd38d2aa25b52735d5
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, The Linux Foundation. All rights reserved.*/
4 #include <linux/err.h>
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>
11 #include <linux/of.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
23 /**
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
29 * supported
30 * @active_only: True if it represents an Active only peer
31 * @corner: current corner
32 * @active_corner: current active corner
33 * @level: An array of level (vlvl) to corner (hlvl) mappings
34 * derived from cmd-db
35 * @level_count: Number of levels supported by the power domain. max
36 * being 16 (0 - 15)
37 * @enabled: true if the power domain is enabled
38 * @res_name: Resource name used for cmd-db lookup
39 * @addr: Resource address as looped up using resource name from
40 * cmd-db
42 struct rpmhpd {
43 struct device *dev;
44 struct generic_pm_domain pd;
45 struct generic_pm_domain *parent;
46 struct rpmhpd *peer;
47 const bool active_only;
48 unsigned int corner;
49 unsigned int active_corner;
50 u32 level[RPMH_ARC_MAX_LEVELS];
51 size_t level_count;
52 bool enabled;
53 const char *res_name;
54 u32 addr;
57 struct rpmhpd_desc {
58 struct rpmhpd **rpmhpds;
59 size_t num_pds;
62 static DEFINE_MUTEX(rpmhpd_lock);
64 /* SDM845 RPMH powerdomains */
66 static struct rpmhpd sdm845_ebi = {
67 .pd = { .name = "ebi", },
68 .res_name = "ebi.lvl",
71 static struct rpmhpd sdm845_lmx = {
72 .pd = { .name = "lmx", },
73 .res_name = "lmx.lvl",
76 static struct rpmhpd sdm845_lcx = {
77 .pd = { .name = "lcx", },
78 .res_name = "lcx.lvl",
81 static struct rpmhpd sdm845_gfx = {
82 .pd = { .name = "gfx", },
83 .res_name = "gfx.lvl",
86 static struct rpmhpd sdm845_mss = {
87 .pd = { .name = "mss", },
88 .res_name = "mss.lvl",
91 static struct rpmhpd sdm845_mx_ao;
92 static struct rpmhpd sdm845_mx = {
93 .pd = { .name = "mx", },
94 .peer = &sdm845_mx_ao,
95 .res_name = "mx.lvl",
98 static struct rpmhpd sdm845_mx_ao = {
99 .pd = { .name = "mx_ao", },
100 .active_only = true,
101 .peer = &sdm845_mx,
102 .res_name = "mx.lvl",
105 static struct rpmhpd sdm845_cx_ao;
106 static struct rpmhpd sdm845_cx = {
107 .pd = { .name = "cx", },
108 .peer = &sdm845_cx_ao,
109 .parent = &sdm845_mx.pd,
110 .res_name = "cx.lvl",
113 static struct rpmhpd sdm845_cx_ao = {
114 .pd = { .name = "cx_ao", },
115 .active_only = true,
116 .peer = &sdm845_cx,
117 .parent = &sdm845_mx_ao.pd,
118 .res_name = "cx.lvl",
121 static struct rpmhpd *sdm845_rpmhpds[] = {
122 [SDM845_EBI] = &sdm845_ebi,
123 [SDM845_MX] = &sdm845_mx,
124 [SDM845_MX_AO] = &sdm845_mx_ao,
125 [SDM845_CX] = &sdm845_cx,
126 [SDM845_CX_AO] = &sdm845_cx_ao,
127 [SDM845_LMX] = &sdm845_lmx,
128 [SDM845_LCX] = &sdm845_lcx,
129 [SDM845_GFX] = &sdm845_gfx,
130 [SDM845_MSS] = &sdm845_mss,
133 static const struct rpmhpd_desc sdm845_desc = {
134 .rpmhpds = sdm845_rpmhpds,
135 .num_pds = ARRAY_SIZE(sdm845_rpmhpds),
138 /* SDX55 RPMH powerdomains */
139 static struct rpmhpd *sdx55_rpmhpds[] = {
140 [SDX55_MSS] = &sdm845_mss,
141 [SDX55_MX] = &sdm845_mx,
142 [SDX55_CX] = &sdm845_cx,
145 static const struct rpmhpd_desc sdx55_desc = {
146 .rpmhpds = sdx55_rpmhpds,
147 .num_pds = ARRAY_SIZE(sdx55_rpmhpds),
150 /* SM8150 RPMH powerdomains */
152 static struct rpmhpd sm8150_mmcx_ao;
153 static struct rpmhpd sm8150_mmcx = {
154 .pd = { .name = "mmcx", },
155 .peer = &sm8150_mmcx_ao,
156 .res_name = "mmcx.lvl",
159 static struct rpmhpd sm8150_mmcx_ao = {
160 .pd = { .name = "mmcx_ao", },
161 .active_only = true,
162 .peer = &sm8150_mmcx,
163 .res_name = "mmcx.lvl",
166 static struct rpmhpd *sm8150_rpmhpds[] = {
167 [SM8150_MSS] = &sdm845_mss,
168 [SM8150_EBI] = &sdm845_ebi,
169 [SM8150_LMX] = &sdm845_lmx,
170 [SM8150_LCX] = &sdm845_lcx,
171 [SM8150_GFX] = &sdm845_gfx,
172 [SM8150_MX] = &sdm845_mx,
173 [SM8150_MX_AO] = &sdm845_mx_ao,
174 [SM8150_CX] = &sdm845_cx,
175 [SM8150_CX_AO] = &sdm845_cx_ao,
176 [SM8150_MMCX] = &sm8150_mmcx,
177 [SM8150_MMCX_AO] = &sm8150_mmcx_ao,
180 static const struct rpmhpd_desc sm8150_desc = {
181 .rpmhpds = sm8150_rpmhpds,
182 .num_pds = ARRAY_SIZE(sm8150_rpmhpds),
185 static struct rpmhpd *sm8250_rpmhpds[] = {
186 [SM8250_CX] = &sdm845_cx,
187 [SM8250_CX_AO] = &sdm845_cx_ao,
188 [SM8250_EBI] = &sdm845_ebi,
189 [SM8250_GFX] = &sdm845_gfx,
190 [SM8250_LCX] = &sdm845_lcx,
191 [SM8250_LMX] = &sdm845_lmx,
192 [SM8250_MMCX] = &sm8150_mmcx,
193 [SM8250_MMCX_AO] = &sm8150_mmcx_ao,
194 [SM8250_MX] = &sdm845_mx,
195 [SM8250_MX_AO] = &sdm845_mx_ao,
198 static const struct rpmhpd_desc sm8250_desc = {
199 .rpmhpds = sm8250_rpmhpds,
200 .num_pds = ARRAY_SIZE(sm8250_rpmhpds),
203 /* SC7180 RPMH powerdomains */
204 static struct rpmhpd *sc7180_rpmhpds[] = {
205 [SC7180_CX] = &sdm845_cx,
206 [SC7180_CX_AO] = &sdm845_cx_ao,
207 [SC7180_GFX] = &sdm845_gfx,
208 [SC7180_MX] = &sdm845_mx,
209 [SC7180_MX_AO] = &sdm845_mx_ao,
210 [SC7180_LMX] = &sdm845_lmx,
211 [SC7180_LCX] = &sdm845_lcx,
212 [SC7180_MSS] = &sdm845_mss,
215 static const struct rpmhpd_desc sc7180_desc = {
216 .rpmhpds = sc7180_rpmhpds,
217 .num_pds = ARRAY_SIZE(sc7180_rpmhpds),
220 static const struct of_device_id rpmhpd_match_table[] = {
221 { .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
222 { .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
223 { .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc},
224 { .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
225 { .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
228 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
230 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
231 unsigned int corner, bool sync)
233 struct tcs_cmd cmd = {
234 .addr = pd->addr,
235 .data = corner,
239 * Wait for an ack only when we are increasing the
240 * perf state of the power domain
242 if (sync)
243 return rpmh_write(pd->dev, state, &cmd, 1);
244 else
245 return rpmh_write_async(pd->dev, state, &cmd, 1);
248 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
249 unsigned int *active, unsigned int *sleep)
251 *active = corner;
253 if (pd->active_only)
254 *sleep = 0;
255 else
256 *sleep = *active;
260 * This function is used to aggregate the votes across the active only
261 * resources and its peers. The aggregated votes are sent to RPMh as
262 * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
263 * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
264 * on system sleep).
265 * We send ACTIVE_ONLY votes for resources without any peers. For others,
266 * which have an active only peer, all 3 votes are sent.
268 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
270 int ret;
271 struct rpmhpd *peer = pd->peer;
272 unsigned int active_corner, sleep_corner;
273 unsigned int this_active_corner = 0, this_sleep_corner = 0;
274 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
276 to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
278 if (peer && peer->enabled)
279 to_active_sleep(peer, peer->corner, &peer_active_corner,
280 &peer_sleep_corner);
282 active_corner = max(this_active_corner, peer_active_corner);
284 ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
285 active_corner > pd->active_corner);
286 if (ret)
287 return ret;
289 pd->active_corner = active_corner;
291 if (peer) {
292 peer->active_corner = active_corner;
294 ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
295 active_corner, false);
296 if (ret)
297 return ret;
299 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
301 return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
302 false);
305 return ret;
308 static int rpmhpd_power_on(struct generic_pm_domain *domain)
310 struct rpmhpd *pd = domain_to_rpmhpd(domain);
311 int ret = 0;
313 mutex_lock(&rpmhpd_lock);
315 if (pd->corner)
316 ret = rpmhpd_aggregate_corner(pd, pd->corner);
318 if (!ret)
319 pd->enabled = true;
321 mutex_unlock(&rpmhpd_lock);
323 return ret;
326 static int rpmhpd_power_off(struct generic_pm_domain *domain)
328 struct rpmhpd *pd = domain_to_rpmhpd(domain);
329 int ret = 0;
331 mutex_lock(&rpmhpd_lock);
333 ret = rpmhpd_aggregate_corner(pd, pd->level[0]);
335 if (!ret)
336 pd->enabled = false;
338 mutex_unlock(&rpmhpd_lock);
340 return ret;
343 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
344 unsigned int level)
346 struct rpmhpd *pd = domain_to_rpmhpd(domain);
347 int ret = 0, i;
349 mutex_lock(&rpmhpd_lock);
351 for (i = 0; i < pd->level_count; i++)
352 if (level <= pd->level[i])
353 break;
356 * If the level requested is more than that supported by the
357 * max corner, just set it to max anyway.
359 if (i == pd->level_count)
360 i--;
362 if (pd->enabled) {
363 ret = rpmhpd_aggregate_corner(pd, i);
364 if (ret)
365 goto out;
368 pd->corner = i;
369 out:
370 mutex_unlock(&rpmhpd_lock);
372 return ret;
375 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
376 struct dev_pm_opp *opp)
378 return dev_pm_opp_get_level(opp);
381 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
383 int i;
384 const u16 *buf;
386 buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
387 if (IS_ERR(buf))
388 return PTR_ERR(buf);
390 /* 2 bytes used for each command DB aux data entry */
391 rpmhpd->level_count >>= 1;
393 if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
394 return -EINVAL;
396 for (i = 0; i < rpmhpd->level_count; i++) {
397 rpmhpd->level[i] = buf[i];
400 * The AUX data may be zero padded. These 0 valued entries at
401 * the end of the map must be ignored.
403 if (i > 0 && rpmhpd->level[i] == 0) {
404 rpmhpd->level_count = i;
405 break;
407 pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
408 rpmhpd->level[i]);
411 return 0;
414 static int rpmhpd_probe(struct platform_device *pdev)
416 int i, ret;
417 size_t num_pds;
418 struct device *dev = &pdev->dev;
419 struct genpd_onecell_data *data;
420 struct rpmhpd **rpmhpds;
421 const struct rpmhpd_desc *desc;
423 desc = of_device_get_match_data(dev);
424 if (!desc)
425 return -EINVAL;
427 rpmhpds = desc->rpmhpds;
428 num_pds = desc->num_pds;
430 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
431 if (!data)
432 return -ENOMEM;
434 data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
435 GFP_KERNEL);
436 if (!data->domains)
437 return -ENOMEM;
439 data->num_domains = num_pds;
441 for (i = 0; i < num_pds; i++) {
442 if (!rpmhpds[i]) {
443 dev_warn(dev, "rpmhpds[%d] is empty\n", i);
444 continue;
447 rpmhpds[i]->dev = dev;
448 rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
449 if (!rpmhpds[i]->addr) {
450 dev_err(dev, "Could not find RPMh address for resource %s\n",
451 rpmhpds[i]->res_name);
452 return -ENODEV;
455 ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
456 if (ret != CMD_DB_HW_ARC) {
457 dev_err(dev, "RPMh slave ID mismatch\n");
458 return -EINVAL;
461 ret = rpmhpd_update_level_mapping(rpmhpds[i]);
462 if (ret)
463 return ret;
465 rpmhpds[i]->pd.power_off = rpmhpd_power_off;
466 rpmhpds[i]->pd.power_on = rpmhpd_power_on;
467 rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
468 rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
469 pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
471 data->domains[i] = &rpmhpds[i]->pd;
474 /* Add subdomains */
475 for (i = 0; i < num_pds; i++) {
476 if (!rpmhpds[i])
477 continue;
478 if (rpmhpds[i]->parent)
479 pm_genpd_add_subdomain(rpmhpds[i]->parent,
480 &rpmhpds[i]->pd);
483 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
486 static struct platform_driver rpmhpd_driver = {
487 .driver = {
488 .name = "qcom-rpmhpd",
489 .of_match_table = rpmhpd_match_table,
490 .suppress_bind_attrs = true,
492 .probe = rpmhpd_probe,
495 static int __init rpmhpd_init(void)
497 return platform_driver_register(&rpmhpd_driver);
499 core_initcall(rpmhpd_init);
501 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
502 MODULE_LICENSE("GPL v2");