1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017-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>
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)
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
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_8996_RPMPD_STATE 6
40 #define DEFINE_RPMPD_PAIR(_platform, _name, _active, r_type, r_key, \
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, \
50 static struct rpmpd _platform##_##_active = { \
51 .pd = { .name = #_active, }, \
52 .peer = &_platform##_##_name, \
53 .active_only = true, \
54 .res_type = RPMPD_##r_type, \
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, \
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, \
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, \
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, \
88 .key = KEY_FLOOR_LEVEL, \
98 struct generic_pm_domain pd;
100 const bool active_only;
103 const char *res_name;
106 struct qcom_smd_rpm *rpm;
107 unsigned int max_state;
112 struct rpmpd **rpmpds;
114 unsigned int max_state;
117 static DEFINE_MUTEX(rpmpd_lock);
119 /* msm8976 RPM Power Domains */
120 DEFINE_RPMPD_PAIR(msm8976, vddcx, vddcx_ao, SMPA, LEVEL, 2);
121 DEFINE_RPMPD_PAIR(msm8976, vddmx, vddmx_ao, SMPA, LEVEL, 6);
123 DEFINE_RPMPD_VFL(msm8976, vddcx_vfl, RWSC, 2);
124 DEFINE_RPMPD_VFL(msm8976, vddmx_vfl, RWSM, 6);
126 static struct rpmpd *msm8976_rpmpds[] = {
127 [MSM8976_VDDCX] = &msm8976_vddcx,
128 [MSM8976_VDDCX_AO] = &msm8976_vddcx_ao,
129 [MSM8976_VDDCX_VFL] = &msm8976_vddcx_vfl,
130 [MSM8976_VDDMX] = &msm8976_vddmx,
131 [MSM8976_VDDMX_AO] = &msm8976_vddmx_ao,
132 [MSM8976_VDDMX_VFL] = &msm8976_vddmx_vfl,
135 static const struct rpmpd_desc msm8976_desc = {
136 .rpmpds = msm8976_rpmpds,
137 .num_pds = ARRAY_SIZE(msm8976_rpmpds),
138 .max_state = RPM_SMD_LEVEL_TURBO_HIGH,
141 /* msm8996 RPM Power domains */
142 DEFINE_RPMPD_PAIR(msm8996, vddcx, vddcx_ao, SMPA, CORNER, 1);
143 DEFINE_RPMPD_PAIR(msm8996, vddmx, vddmx_ao, SMPA, CORNER, 2);
144 DEFINE_RPMPD_CORNER(msm8996, vddsscx, LDOA, 26);
146 DEFINE_RPMPD_VFC(msm8996, vddcx_vfc, SMPA, 1);
147 DEFINE_RPMPD_VFC(msm8996, vddsscx_vfc, LDOA, 26);
149 static struct rpmpd *msm8996_rpmpds[] = {
150 [MSM8996_VDDCX] = &msm8996_vddcx,
151 [MSM8996_VDDCX_AO] = &msm8996_vddcx_ao,
152 [MSM8996_VDDCX_VFC] = &msm8996_vddcx_vfc,
153 [MSM8996_VDDMX] = &msm8996_vddmx,
154 [MSM8996_VDDMX_AO] = &msm8996_vddmx_ao,
155 [MSM8996_VDDSSCX] = &msm8996_vddsscx,
156 [MSM8996_VDDSSCX_VFC] = &msm8996_vddsscx_vfc,
159 static const struct rpmpd_desc msm8996_desc = {
160 .rpmpds = msm8996_rpmpds,
161 .num_pds = ARRAY_SIZE(msm8996_rpmpds),
162 .max_state = MAX_8996_RPMPD_STATE,
165 /* msm8998 RPM Power domains */
166 DEFINE_RPMPD_PAIR(msm8998, vddcx, vddcx_ao, RWCX, LEVEL, 0);
167 DEFINE_RPMPD_VFL(msm8998, vddcx_vfl, RWCX, 0);
169 DEFINE_RPMPD_PAIR(msm8998, vddmx, vddmx_ao, RWMX, LEVEL, 0);
170 DEFINE_RPMPD_VFL(msm8998, vddmx_vfl, RWMX, 0);
172 DEFINE_RPMPD_LEVEL(msm8998, vdd_ssccx, RWSC, 0);
173 DEFINE_RPMPD_VFL(msm8998, vdd_ssccx_vfl, RWSC, 0);
175 DEFINE_RPMPD_LEVEL(msm8998, vdd_sscmx, RWSM, 0);
176 DEFINE_RPMPD_VFL(msm8998, vdd_sscmx_vfl, RWSM, 0);
178 static struct rpmpd *msm8998_rpmpds[] = {
179 [MSM8998_VDDCX] = &msm8998_vddcx,
180 [MSM8998_VDDCX_AO] = &msm8998_vddcx_ao,
181 [MSM8998_VDDCX_VFL] = &msm8998_vddcx_vfl,
182 [MSM8998_VDDMX] = &msm8998_vddmx,
183 [MSM8998_VDDMX_AO] = &msm8998_vddmx_ao,
184 [MSM8998_VDDMX_VFL] = &msm8998_vddmx_vfl,
185 [MSM8998_SSCCX] = &msm8998_vdd_ssccx,
186 [MSM8998_SSCCX_VFL] = &msm8998_vdd_ssccx_vfl,
187 [MSM8998_SSCMX] = &msm8998_vdd_sscmx,
188 [MSM8998_SSCMX_VFL] = &msm8998_vdd_sscmx_vfl,
191 static const struct rpmpd_desc msm8998_desc = {
192 .rpmpds = msm8998_rpmpds,
193 .num_pds = ARRAY_SIZE(msm8998_rpmpds),
194 .max_state = RPM_SMD_LEVEL_BINNING,
197 /* qcs404 RPM Power domains */
198 DEFINE_RPMPD_PAIR(qcs404, vddmx, vddmx_ao, RWMX, LEVEL, 0);
199 DEFINE_RPMPD_VFL(qcs404, vddmx_vfl, RWMX, 0);
201 DEFINE_RPMPD_LEVEL(qcs404, vdd_lpicx, RWLC, 0);
202 DEFINE_RPMPD_VFL(qcs404, vdd_lpicx_vfl, RWLC, 0);
204 DEFINE_RPMPD_LEVEL(qcs404, vdd_lpimx, RWLM, 0);
205 DEFINE_RPMPD_VFL(qcs404, vdd_lpimx_vfl, RWLM, 0);
207 static struct rpmpd *qcs404_rpmpds[] = {
208 [QCS404_VDDMX] = &qcs404_vddmx,
209 [QCS404_VDDMX_AO] = &qcs404_vddmx_ao,
210 [QCS404_VDDMX_VFL] = &qcs404_vddmx_vfl,
211 [QCS404_LPICX] = &qcs404_vdd_lpicx,
212 [QCS404_LPICX_VFL] = &qcs404_vdd_lpicx_vfl,
213 [QCS404_LPIMX] = &qcs404_vdd_lpimx,
214 [QCS404_LPIMX_VFL] = &qcs404_vdd_lpimx_vfl,
217 static const struct rpmpd_desc qcs404_desc = {
218 .rpmpds = qcs404_rpmpds,
219 .num_pds = ARRAY_SIZE(qcs404_rpmpds),
220 .max_state = RPM_SMD_LEVEL_BINNING,
223 static const struct of_device_id rpmpd_match_table[] = {
224 { .compatible = "qcom,msm8976-rpmpd", .data = &msm8976_desc },
225 { .compatible = "qcom,msm8996-rpmpd", .data = &msm8996_desc },
226 { .compatible = "qcom,msm8998-rpmpd", .data = &msm8998_desc },
227 { .compatible = "qcom,qcs404-rpmpd", .data = &qcs404_desc },
230 MODULE_DEVICE_TABLE(of, rpmpd_match_table);
232 static int rpmpd_send_enable(struct rpmpd *pd, bool enable)
234 struct rpmpd_req req = {
236 .nbytes = cpu_to_le32(sizeof(u32)),
237 .value = cpu_to_le32(enable),
240 return qcom_rpm_smd_write(pd->rpm, QCOM_SMD_RPM_ACTIVE_STATE,
241 pd->res_type, pd->res_id, &req, sizeof(req));
244 static int rpmpd_send_corner(struct rpmpd *pd, int state, unsigned int corner)
246 struct rpmpd_req req = {
248 .nbytes = cpu_to_le32(sizeof(u32)),
249 .value = cpu_to_le32(corner),
252 return qcom_rpm_smd_write(pd->rpm, state, pd->res_type, pd->res_id,
256 static void to_active_sleep(struct rpmpd *pd, unsigned int corner,
257 unsigned int *active, unsigned int *sleep)
267 static int rpmpd_aggregate_corner(struct rpmpd *pd)
270 struct rpmpd *peer = pd->peer;
271 unsigned int active_corner, sleep_corner;
272 unsigned int this_active_corner = 0, this_sleep_corner = 0;
273 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
275 to_active_sleep(pd, pd->corner, &this_active_corner, &this_sleep_corner);
277 if (peer && peer->enabled)
278 to_active_sleep(peer, peer->corner, &peer_active_corner,
281 active_corner = max(this_active_corner, peer_active_corner);
283 ret = rpmpd_send_corner(pd, QCOM_SMD_RPM_ACTIVE_STATE, active_corner);
287 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
289 return rpmpd_send_corner(pd, QCOM_SMD_RPM_SLEEP_STATE, sleep_corner);
292 static int rpmpd_power_on(struct generic_pm_domain *domain)
295 struct rpmpd *pd = domain_to_rpmpd(domain);
297 mutex_lock(&rpmpd_lock);
299 ret = rpmpd_send_enable(pd, true);
306 ret = rpmpd_aggregate_corner(pd);
309 mutex_unlock(&rpmpd_lock);
314 static int rpmpd_power_off(struct generic_pm_domain *domain)
317 struct rpmpd *pd = domain_to_rpmpd(domain);
319 mutex_lock(&rpmpd_lock);
321 ret = rpmpd_send_enable(pd, false);
325 mutex_unlock(&rpmpd_lock);
330 static int rpmpd_set_performance(struct generic_pm_domain *domain,
334 struct rpmpd *pd = domain_to_rpmpd(domain);
336 if (state > pd->max_state)
337 state = pd->max_state;
339 mutex_lock(&rpmpd_lock);
343 /* Always send updates for vfc and vfl */
344 if (!pd->enabled && pd->key != KEY_FLOOR_CORNER &&
345 pd->key != KEY_FLOOR_LEVEL)
348 ret = rpmpd_aggregate_corner(pd);
351 mutex_unlock(&rpmpd_lock);
356 static unsigned int rpmpd_get_performance(struct generic_pm_domain *genpd,
357 struct dev_pm_opp *opp)
359 return dev_pm_opp_get_level(opp);
362 static int rpmpd_probe(struct platform_device *pdev)
366 struct genpd_onecell_data *data;
367 struct qcom_smd_rpm *rpm;
368 struct rpmpd **rpmpds;
369 const struct rpmpd_desc *desc;
371 rpm = dev_get_drvdata(pdev->dev.parent);
373 dev_err(&pdev->dev, "Unable to retrieve handle to RPM\n");
377 desc = of_device_get_match_data(&pdev->dev);
381 rpmpds = desc->rpmpds;
384 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
388 data->domains = devm_kcalloc(&pdev->dev, num, sizeof(*data->domains),
393 data->num_domains = num;
395 for (i = 0; i < num; i++) {
397 dev_warn(&pdev->dev, "rpmpds[] with empty entry at index=%d\n",
402 rpmpds[i]->rpm = rpm;
403 rpmpds[i]->max_state = desc->max_state;
404 rpmpds[i]->pd.power_off = rpmpd_power_off;
405 rpmpds[i]->pd.power_on = rpmpd_power_on;
406 rpmpds[i]->pd.set_performance_state = rpmpd_set_performance;
407 rpmpds[i]->pd.opp_to_performance_state = rpmpd_get_performance;
408 pm_genpd_init(&rpmpds[i]->pd, NULL, true);
410 data->domains[i] = &rpmpds[i]->pd;
413 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
416 static struct platform_driver rpmpd_driver = {
418 .name = "qcom-rpmpd",
419 .of_match_table = rpmpd_match_table,
420 .suppress_bind_attrs = true,
422 .probe = rpmpd_probe,
425 static int __init rpmpd_init(void)
427 return platform_driver_register(&rpmpd_driver);
429 core_initcall(rpmpd_init);
431 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPM Power Domain Driver");
432 MODULE_LICENSE("GPL v2");