WIP FPC-III support
[linux/fpc-iii.git] / drivers / soc / qcom / rpmpd.c
blob85d1207b72d7b4adeb9eb275f2ce3085dfece75b
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017-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/of.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
14 #include <linux/soc/qcom/smd-rpm.h>
16 #include <dt-bindings/power/qcom-rpmpd.h>
18 #define domain_to_rpmpd(domain) container_of(domain, struct rpmpd, pd)
20 /* Resource types:
21 * RPMPD_X is X encoded as a little-endian, lower-case, ASCII string */
22 #define RPMPD_SMPA 0x61706d73
23 #define RPMPD_LDOA 0x616f646c
24 #define RPMPD_RWCX 0x78637772
25 #define RPMPD_RWMX 0x786d7772
26 #define RPMPD_RWLC 0x636c7772
27 #define RPMPD_RWLM 0x6d6c7772
28 #define RPMPD_RWSC 0x63737772
29 #define RPMPD_RWSM 0x6d737772
31 /* Operation Keys */
32 #define KEY_CORNER 0x6e726f63 /* corn */
33 #define KEY_ENABLE 0x6e657773 /* swen */
34 #define KEY_FLOOR_CORNER 0x636676 /* vfc */
35 #define KEY_FLOOR_LEVEL 0x6c6676 /* vfl */
36 #define KEY_LEVEL 0x6c766c76 /* vlvl */
38 #define MAX_CORNER_RPMPD_STATE 6
40 #define DEFINE_RPMPD_PAIR(_platform, _name, _active, r_type, r_key, \
41 r_id) \
42 static struct rpmpd _platform##_##_active; \
43 static struct rpmpd _platform##_##_name = { \
44 .pd = { .name = #_name, }, \
45 .peer = &_platform##_##_active, \
46 .res_type = RPMPD_##r_type, \
47 .res_id = r_id, \
48 .key = KEY_##r_key, \
49 }; \
50 static struct rpmpd _platform##_##_active = { \
51 .pd = { .name = #_active, }, \
52 .peer = &_platform##_##_name, \
53 .active_only = true, \
54 .res_type = RPMPD_##r_type, \
55 .res_id = r_id, \
56 .key = KEY_##r_key, \
59 #define DEFINE_RPMPD_CORNER(_platform, _name, r_type, r_id) \
60 static struct rpmpd _platform##_##_name = { \
61 .pd = { .name = #_name, }, \
62 .res_type = RPMPD_##r_type, \
63 .res_id = r_id, \
64 .key = KEY_CORNER, \
67 #define DEFINE_RPMPD_LEVEL(_platform, _name, r_type, r_id) \
68 static struct rpmpd _platform##_##_name = { \
69 .pd = { .name = #_name, }, \
70 .res_type = RPMPD_##r_type, \
71 .res_id = r_id, \
72 .key = KEY_LEVEL, \
75 #define DEFINE_RPMPD_VFC(_platform, _name, r_type, r_id) \
76 static struct rpmpd _platform##_##_name = { \
77 .pd = { .name = #_name, }, \
78 .res_type = RPMPD_##r_type, \
79 .res_id = r_id, \
80 .key = KEY_FLOOR_CORNER, \
83 #define DEFINE_RPMPD_VFL(_platform, _name, r_type, r_id) \
84 static struct rpmpd _platform##_##_name = { \
85 .pd = { .name = #_name, }, \
86 .res_type = RPMPD_##r_type, \
87 .res_id = r_id, \
88 .key = KEY_FLOOR_LEVEL, \
91 struct rpmpd_req {
92 __le32 key;
93 __le32 nbytes;
94 __le32 value;
97 struct rpmpd {
98 struct generic_pm_domain pd;
99 struct rpmpd *peer;
100 const bool active_only;
101 unsigned int corner;
102 bool enabled;
103 const char *res_name;
104 const int res_type;
105 const int res_id;
106 struct qcom_smd_rpm *rpm;
107 unsigned int max_state;
108 __le32 key;
111 struct rpmpd_desc {
112 struct rpmpd **rpmpds;
113 size_t num_pds;
114 unsigned int max_state;
117 static DEFINE_MUTEX(rpmpd_lock);
119 /* msm8939 RPM Power Domains */
120 DEFINE_RPMPD_PAIR(msm8939, vddmd, vddmd_ao, SMPA, CORNER, 1);
121 DEFINE_RPMPD_VFC(msm8939, vddmd_vfc, SMPA, 1);
123 DEFINE_RPMPD_PAIR(msm8939, vddcx, vddcx_ao, SMPA, CORNER, 2);
124 DEFINE_RPMPD_VFC(msm8939, vddcx_vfc, SMPA, 2);
126 DEFINE_RPMPD_PAIR(msm8939, vddmx, vddmx_ao, LDOA, CORNER, 3);
128 static struct rpmpd *msm8939_rpmpds[] = {
129 [MSM8939_VDDMDCX] = &msm8939_vddmd,
130 [MSM8939_VDDMDCX_AO] = &msm8939_vddmd_ao,
131 [MSM8939_VDDMDCX_VFC] = &msm8939_vddmd_vfc,
132 [MSM8939_VDDCX] = &msm8939_vddcx,
133 [MSM8939_VDDCX_AO] = &msm8939_vddcx_ao,
134 [MSM8939_VDDCX_VFC] = &msm8939_vddcx_vfc,
135 [MSM8939_VDDMX] = &msm8939_vddmx,
136 [MSM8939_VDDMX_AO] = &msm8939_vddmx_ao,
139 static const struct rpmpd_desc msm8939_desc = {
140 .rpmpds = msm8939_rpmpds,
141 .num_pds = ARRAY_SIZE(msm8939_rpmpds),
142 .max_state = MAX_CORNER_RPMPD_STATE,
145 /* msm8916 RPM Power Domains */
146 DEFINE_RPMPD_PAIR(msm8916, vddcx, vddcx_ao, SMPA, CORNER, 1);
147 DEFINE_RPMPD_PAIR(msm8916, vddmx, vddmx_ao, LDOA, CORNER, 3);
149 DEFINE_RPMPD_VFC(msm8916, vddcx_vfc, SMPA, 1);
151 static struct rpmpd *msm8916_rpmpds[] = {
152 [MSM8916_VDDCX] = &msm8916_vddcx,
153 [MSM8916_VDDCX_AO] = &msm8916_vddcx_ao,
154 [MSM8916_VDDCX_VFC] = &msm8916_vddcx_vfc,
155 [MSM8916_VDDMX] = &msm8916_vddmx,
156 [MSM8916_VDDMX_AO] = &msm8916_vddmx_ao,
159 static const struct rpmpd_desc msm8916_desc = {
160 .rpmpds = msm8916_rpmpds,
161 .num_pds = ARRAY_SIZE(msm8916_rpmpds),
162 .max_state = MAX_CORNER_RPMPD_STATE,
165 /* msm8976 RPM Power Domains */
166 DEFINE_RPMPD_PAIR(msm8976, vddcx, vddcx_ao, SMPA, LEVEL, 2);
167 DEFINE_RPMPD_PAIR(msm8976, vddmx, vddmx_ao, SMPA, LEVEL, 6);
169 DEFINE_RPMPD_VFL(msm8976, vddcx_vfl, RWSC, 2);
170 DEFINE_RPMPD_VFL(msm8976, vddmx_vfl, RWSM, 6);
172 static struct rpmpd *msm8976_rpmpds[] = {
173 [MSM8976_VDDCX] = &msm8976_vddcx,
174 [MSM8976_VDDCX_AO] = &msm8976_vddcx_ao,
175 [MSM8976_VDDCX_VFL] = &msm8976_vddcx_vfl,
176 [MSM8976_VDDMX] = &msm8976_vddmx,
177 [MSM8976_VDDMX_AO] = &msm8976_vddmx_ao,
178 [MSM8976_VDDMX_VFL] = &msm8976_vddmx_vfl,
181 static const struct rpmpd_desc msm8976_desc = {
182 .rpmpds = msm8976_rpmpds,
183 .num_pds = ARRAY_SIZE(msm8976_rpmpds),
184 .max_state = RPM_SMD_LEVEL_TURBO_HIGH,
187 /* msm8996 RPM Power domains */
188 DEFINE_RPMPD_PAIR(msm8996, vddcx, vddcx_ao, SMPA, CORNER, 1);
189 DEFINE_RPMPD_PAIR(msm8996, vddmx, vddmx_ao, SMPA, CORNER, 2);
190 DEFINE_RPMPD_CORNER(msm8996, vddsscx, LDOA, 26);
192 DEFINE_RPMPD_VFC(msm8996, vddcx_vfc, SMPA, 1);
193 DEFINE_RPMPD_VFC(msm8996, vddsscx_vfc, LDOA, 26);
195 static struct rpmpd *msm8996_rpmpds[] = {
196 [MSM8996_VDDCX] = &msm8996_vddcx,
197 [MSM8996_VDDCX_AO] = &msm8996_vddcx_ao,
198 [MSM8996_VDDCX_VFC] = &msm8996_vddcx_vfc,
199 [MSM8996_VDDMX] = &msm8996_vddmx,
200 [MSM8996_VDDMX_AO] = &msm8996_vddmx_ao,
201 [MSM8996_VDDSSCX] = &msm8996_vddsscx,
202 [MSM8996_VDDSSCX_VFC] = &msm8996_vddsscx_vfc,
205 static const struct rpmpd_desc msm8996_desc = {
206 .rpmpds = msm8996_rpmpds,
207 .num_pds = ARRAY_SIZE(msm8996_rpmpds),
208 .max_state = MAX_CORNER_RPMPD_STATE,
211 /* msm8998 RPM Power domains */
212 DEFINE_RPMPD_PAIR(msm8998, vddcx, vddcx_ao, RWCX, LEVEL, 0);
213 DEFINE_RPMPD_VFL(msm8998, vddcx_vfl, RWCX, 0);
215 DEFINE_RPMPD_PAIR(msm8998, vddmx, vddmx_ao, RWMX, LEVEL, 0);
216 DEFINE_RPMPD_VFL(msm8998, vddmx_vfl, RWMX, 0);
218 DEFINE_RPMPD_LEVEL(msm8998, vdd_ssccx, RWSC, 0);
219 DEFINE_RPMPD_VFL(msm8998, vdd_ssccx_vfl, RWSC, 0);
221 DEFINE_RPMPD_LEVEL(msm8998, vdd_sscmx, RWSM, 0);
222 DEFINE_RPMPD_VFL(msm8998, vdd_sscmx_vfl, RWSM, 0);
224 static struct rpmpd *msm8998_rpmpds[] = {
225 [MSM8998_VDDCX] = &msm8998_vddcx,
226 [MSM8998_VDDCX_AO] = &msm8998_vddcx_ao,
227 [MSM8998_VDDCX_VFL] = &msm8998_vddcx_vfl,
228 [MSM8998_VDDMX] = &msm8998_vddmx,
229 [MSM8998_VDDMX_AO] = &msm8998_vddmx_ao,
230 [MSM8998_VDDMX_VFL] = &msm8998_vddmx_vfl,
231 [MSM8998_SSCCX] = &msm8998_vdd_ssccx,
232 [MSM8998_SSCCX_VFL] = &msm8998_vdd_ssccx_vfl,
233 [MSM8998_SSCMX] = &msm8998_vdd_sscmx,
234 [MSM8998_SSCMX_VFL] = &msm8998_vdd_sscmx_vfl,
237 static const struct rpmpd_desc msm8998_desc = {
238 .rpmpds = msm8998_rpmpds,
239 .num_pds = ARRAY_SIZE(msm8998_rpmpds),
240 .max_state = RPM_SMD_LEVEL_BINNING,
243 /* qcs404 RPM Power domains */
244 DEFINE_RPMPD_PAIR(qcs404, vddmx, vddmx_ao, RWMX, LEVEL, 0);
245 DEFINE_RPMPD_VFL(qcs404, vddmx_vfl, RWMX, 0);
247 DEFINE_RPMPD_LEVEL(qcs404, vdd_lpicx, RWLC, 0);
248 DEFINE_RPMPD_VFL(qcs404, vdd_lpicx_vfl, RWLC, 0);
250 DEFINE_RPMPD_LEVEL(qcs404, vdd_lpimx, RWLM, 0);
251 DEFINE_RPMPD_VFL(qcs404, vdd_lpimx_vfl, RWLM, 0);
253 static struct rpmpd *qcs404_rpmpds[] = {
254 [QCS404_VDDMX] = &qcs404_vddmx,
255 [QCS404_VDDMX_AO] = &qcs404_vddmx_ao,
256 [QCS404_VDDMX_VFL] = &qcs404_vddmx_vfl,
257 [QCS404_LPICX] = &qcs404_vdd_lpicx,
258 [QCS404_LPICX_VFL] = &qcs404_vdd_lpicx_vfl,
259 [QCS404_LPIMX] = &qcs404_vdd_lpimx,
260 [QCS404_LPIMX_VFL] = &qcs404_vdd_lpimx_vfl,
263 static const struct rpmpd_desc qcs404_desc = {
264 .rpmpds = qcs404_rpmpds,
265 .num_pds = ARRAY_SIZE(qcs404_rpmpds),
266 .max_state = RPM_SMD_LEVEL_BINNING,
269 /* sdm660 RPM Power domains */
270 DEFINE_RPMPD_PAIR(sdm660, vddcx, vddcx_ao, RWCX, LEVEL, 0);
271 DEFINE_RPMPD_VFL(sdm660, vddcx_vfl, RWCX, 0);
273 DEFINE_RPMPD_PAIR(sdm660, vddmx, vddmx_ao, RWMX, LEVEL, 0);
274 DEFINE_RPMPD_VFL(sdm660, vddmx_vfl, RWMX, 0);
276 DEFINE_RPMPD_LEVEL(sdm660, vdd_ssccx, RWLC, 0);
277 DEFINE_RPMPD_VFL(sdm660, vdd_ssccx_vfl, RWLC, 0);
279 DEFINE_RPMPD_LEVEL(sdm660, vdd_sscmx, RWLM, 0);
280 DEFINE_RPMPD_VFL(sdm660, vdd_sscmx_vfl, RWLM, 0);
282 static struct rpmpd *sdm660_rpmpds[] = {
283 [SDM660_VDDCX] = &sdm660_vddcx,
284 [SDM660_VDDCX_AO] = &sdm660_vddcx_ao,
285 [SDM660_VDDCX_VFL] = &sdm660_vddcx_vfl,
286 [SDM660_VDDMX] = &sdm660_vddmx,
287 [SDM660_VDDMX_AO] = &sdm660_vddmx_ao,
288 [SDM660_VDDMX_VFL] = &sdm660_vddmx_vfl,
289 [SDM660_SSCCX] = &sdm660_vdd_ssccx,
290 [SDM660_SSCCX_VFL] = &sdm660_vdd_ssccx_vfl,
291 [SDM660_SSCMX] = &sdm660_vdd_sscmx,
292 [SDM660_SSCMX_VFL] = &sdm660_vdd_sscmx_vfl,
295 static const struct rpmpd_desc sdm660_desc = {
296 .rpmpds = sdm660_rpmpds,
297 .num_pds = ARRAY_SIZE(sdm660_rpmpds),
298 .max_state = RPM_SMD_LEVEL_TURBO,
301 static const struct of_device_id rpmpd_match_table[] = {
302 { .compatible = "qcom,msm8916-rpmpd", .data = &msm8916_desc },
303 { .compatible = "qcom,msm8939-rpmpd", .data = &msm8939_desc },
304 { .compatible = "qcom,msm8976-rpmpd", .data = &msm8976_desc },
305 { .compatible = "qcom,msm8996-rpmpd", .data = &msm8996_desc },
306 { .compatible = "qcom,msm8998-rpmpd", .data = &msm8998_desc },
307 { .compatible = "qcom,qcs404-rpmpd", .data = &qcs404_desc },
308 { .compatible = "qcom,sdm660-rpmpd", .data = &sdm660_desc },
311 MODULE_DEVICE_TABLE(of, rpmpd_match_table);
313 static int rpmpd_send_enable(struct rpmpd *pd, bool enable)
315 struct rpmpd_req req = {
316 .key = KEY_ENABLE,
317 .nbytes = cpu_to_le32(sizeof(u32)),
318 .value = cpu_to_le32(enable),
321 return qcom_rpm_smd_write(pd->rpm, QCOM_SMD_RPM_ACTIVE_STATE,
322 pd->res_type, pd->res_id, &req, sizeof(req));
325 static int rpmpd_send_corner(struct rpmpd *pd, int state, unsigned int corner)
327 struct rpmpd_req req = {
328 .key = pd->key,
329 .nbytes = cpu_to_le32(sizeof(u32)),
330 .value = cpu_to_le32(corner),
333 return qcom_rpm_smd_write(pd->rpm, state, pd->res_type, pd->res_id,
334 &req, sizeof(req));
337 static void to_active_sleep(struct rpmpd *pd, unsigned int corner,
338 unsigned int *active, unsigned int *sleep)
340 *active = corner;
342 if (pd->active_only)
343 *sleep = 0;
344 else
345 *sleep = *active;
348 static int rpmpd_aggregate_corner(struct rpmpd *pd)
350 int ret;
351 struct rpmpd *peer = pd->peer;
352 unsigned int active_corner, sleep_corner;
353 unsigned int this_active_corner = 0, this_sleep_corner = 0;
354 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
356 to_active_sleep(pd, pd->corner, &this_active_corner, &this_sleep_corner);
358 if (peer && peer->enabled)
359 to_active_sleep(peer, peer->corner, &peer_active_corner,
360 &peer_sleep_corner);
362 active_corner = max(this_active_corner, peer_active_corner);
364 ret = rpmpd_send_corner(pd, QCOM_SMD_RPM_ACTIVE_STATE, active_corner);
365 if (ret)
366 return ret;
368 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
370 return rpmpd_send_corner(pd, QCOM_SMD_RPM_SLEEP_STATE, sleep_corner);
373 static int rpmpd_power_on(struct generic_pm_domain *domain)
375 int ret;
376 struct rpmpd *pd = domain_to_rpmpd(domain);
378 mutex_lock(&rpmpd_lock);
380 ret = rpmpd_send_enable(pd, true);
381 if (ret)
382 goto out;
384 pd->enabled = true;
386 if (pd->corner)
387 ret = rpmpd_aggregate_corner(pd);
389 out:
390 mutex_unlock(&rpmpd_lock);
392 return ret;
395 static int rpmpd_power_off(struct generic_pm_domain *domain)
397 int ret;
398 struct rpmpd *pd = domain_to_rpmpd(domain);
400 mutex_lock(&rpmpd_lock);
402 ret = rpmpd_send_enable(pd, false);
403 if (!ret)
404 pd->enabled = false;
406 mutex_unlock(&rpmpd_lock);
408 return ret;
411 static int rpmpd_set_performance(struct generic_pm_domain *domain,
412 unsigned int state)
414 int ret = 0;
415 struct rpmpd *pd = domain_to_rpmpd(domain);
417 if (state > pd->max_state)
418 state = pd->max_state;
420 mutex_lock(&rpmpd_lock);
422 pd->corner = state;
424 /* Always send updates for vfc and vfl */
425 if (!pd->enabled && pd->key != KEY_FLOOR_CORNER &&
426 pd->key != KEY_FLOOR_LEVEL)
427 goto out;
429 ret = rpmpd_aggregate_corner(pd);
431 out:
432 mutex_unlock(&rpmpd_lock);
434 return ret;
437 static unsigned int rpmpd_get_performance(struct generic_pm_domain *genpd,
438 struct dev_pm_opp *opp)
440 return dev_pm_opp_get_level(opp);
443 static int rpmpd_probe(struct platform_device *pdev)
445 int i;
446 size_t num;
447 struct genpd_onecell_data *data;
448 struct qcom_smd_rpm *rpm;
449 struct rpmpd **rpmpds;
450 const struct rpmpd_desc *desc;
452 rpm = dev_get_drvdata(pdev->dev.parent);
453 if (!rpm) {
454 dev_err(&pdev->dev, "Unable to retrieve handle to RPM\n");
455 return -ENODEV;
458 desc = of_device_get_match_data(&pdev->dev);
459 if (!desc)
460 return -EINVAL;
462 rpmpds = desc->rpmpds;
463 num = desc->num_pds;
465 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
466 if (!data)
467 return -ENOMEM;
469 data->domains = devm_kcalloc(&pdev->dev, num, sizeof(*data->domains),
470 GFP_KERNEL);
471 data->num_domains = num;
473 for (i = 0; i < num; i++) {
474 if (!rpmpds[i]) {
475 dev_warn(&pdev->dev, "rpmpds[] with empty entry at index=%d\n",
477 continue;
480 rpmpds[i]->rpm = rpm;
481 rpmpds[i]->max_state = desc->max_state;
482 rpmpds[i]->pd.power_off = rpmpd_power_off;
483 rpmpds[i]->pd.power_on = rpmpd_power_on;
484 rpmpds[i]->pd.set_performance_state = rpmpd_set_performance;
485 rpmpds[i]->pd.opp_to_performance_state = rpmpd_get_performance;
486 pm_genpd_init(&rpmpds[i]->pd, NULL, true);
488 data->domains[i] = &rpmpds[i]->pd;
491 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
494 static struct platform_driver rpmpd_driver = {
495 .driver = {
496 .name = "qcom-rpmpd",
497 .of_match_table = rpmpd_match_table,
498 .suppress_bind_attrs = true,
500 .probe = rpmpd_probe,
503 static int __init rpmpd_init(void)
505 return platform_driver_register(&rpmpd_driver);
507 core_initcall(rpmpd_init);
509 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPM Power Domain Driver");
510 MODULE_LICENSE("GPL v2");