1
2
3
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>
15
16#include <dt-bindings/power/qcom-rpmpd.h>
17
18#define domain_to_rpmpd(domain) container_of(domain, struct rpmpd, pd)
19
20
21
22#define RPMPD_SMPA 0x61706d73
23#define RPMPD_LDOA 0x616f646c
24#define RPMPD_SMPB 0x62706d73
25#define RPMPD_LDOB 0x626f646c
26#define RPMPD_RWCX 0x78637772
27#define RPMPD_RWMX 0x786d7772
28#define RPMPD_RWLC 0x636c7772
29#define RPMPD_RWLM 0x6d6c7772
30#define RPMPD_RWSC 0x63737772
31#define RPMPD_RWSM 0x6d737772
32
33
34#define KEY_CORNER 0x6e726f63
35#define KEY_ENABLE 0x6e657773
36#define KEY_FLOOR_CORNER 0x636676
37#define KEY_FLOOR_LEVEL 0x6c6676
38#define KEY_LEVEL 0x6c766c76
39
40#define MAX_CORNER_RPMPD_STATE 6
41
42#define DEFINE_RPMPD_PAIR(_platform, _name, _active, r_type, r_key, \
43 r_id) \
44 static struct rpmpd _platform##_##_active; \
45 static struct rpmpd _platform##_##_name = { \
46 .pd = { .name = #_name, }, \
47 .peer = &_platform##_##_active, \
48 .res_type = RPMPD_##r_type, \
49 .res_id = r_id, \
50 .key = KEY_##r_key, \
51 }; \
52 static struct rpmpd _platform##_##_active = { \
53 .pd = { .name = #_active, }, \
54 .peer = &_platform##_##_name, \
55 .active_only = true, \
56 .res_type = RPMPD_##r_type, \
57 .res_id = r_id, \
58 .key = KEY_##r_key, \
59 }
60
61#define DEFINE_RPMPD_CORNER(_platform, _name, r_type, r_id) \
62 static struct rpmpd _platform##_##_name = { \
63 .pd = { .name = #_name, }, \
64 .res_type = RPMPD_##r_type, \
65 .res_id = r_id, \
66 .key = KEY_CORNER, \
67 }
68
69#define DEFINE_RPMPD_LEVEL(_platform, _name, r_type, r_id) \
70 static struct rpmpd _platform##_##_name = { \
71 .pd = { .name = #_name, }, \
72 .res_type = RPMPD_##r_type, \
73 .res_id = r_id, \
74 .key = KEY_LEVEL, \
75 }
76
77#define DEFINE_RPMPD_VFC(_platform, _name, r_type, r_id) \
78 static struct rpmpd _platform##_##_name = { \
79 .pd = { .name = #_name, }, \
80 .res_type = RPMPD_##r_type, \
81 .res_id = r_id, \
82 .key = KEY_FLOOR_CORNER, \
83 }
84
85#define DEFINE_RPMPD_VFL(_platform, _name, r_type, r_id) \
86 static struct rpmpd _platform##_##_name = { \
87 .pd = { .name = #_name, }, \
88 .res_type = RPMPD_##r_type, \
89 .res_id = r_id, \
90 .key = KEY_FLOOR_LEVEL, \
91 }
92
93struct rpmpd_req {
94 __le32 key;
95 __le32 nbytes;
96 __le32 value;
97};
98
99struct rpmpd {
100 struct generic_pm_domain pd;
101 struct rpmpd *peer;
102 const bool active_only;
103 unsigned int corner;
104 bool enabled;
105 const char *res_name;
106 const int res_type;
107 const int res_id;
108 struct qcom_smd_rpm *rpm;
109 unsigned int max_state;
110 __le32 key;
111};
112
113struct rpmpd_desc {
114 struct rpmpd **rpmpds;
115 size_t num_pds;
116 unsigned int max_state;
117};
118
119static DEFINE_MUTEX(rpmpd_lock);
120
121
122DEFINE_RPMPD_PAIR(msm8939, vddmd, vddmd_ao, SMPA, CORNER, 1);
123DEFINE_RPMPD_VFC(msm8939, vddmd_vfc, SMPA, 1);
124
125DEFINE_RPMPD_PAIR(msm8939, vddcx, vddcx_ao, SMPA, CORNER, 2);
126DEFINE_RPMPD_VFC(msm8939, vddcx_vfc, SMPA, 2);
127
128DEFINE_RPMPD_PAIR(msm8939, vddmx, vddmx_ao, LDOA, CORNER, 3);
129
130static struct rpmpd *msm8939_rpmpds[] = {
131 [MSM8939_VDDMDCX] = &msm8939_vddmd,
132 [MSM8939_VDDMDCX_AO] = &msm8939_vddmd_ao,
133 [MSM8939_VDDMDCX_VFC] = &msm8939_vddmd_vfc,
134 [MSM8939_VDDCX] = &msm8939_vddcx,
135 [MSM8939_VDDCX_AO] = &msm8939_vddcx_ao,
136 [MSM8939_VDDCX_VFC] = &msm8939_vddcx_vfc,
137 [MSM8939_VDDMX] = &msm8939_vddmx,
138 [MSM8939_VDDMX_AO] = &msm8939_vddmx_ao,
139};
140
141static const struct rpmpd_desc msm8939_desc = {
142 .rpmpds = msm8939_rpmpds,
143 .num_pds = ARRAY_SIZE(msm8939_rpmpds),
144 .max_state = MAX_CORNER_RPMPD_STATE,
145};
146
147
148DEFINE_RPMPD_PAIR(msm8916, vddcx, vddcx_ao, SMPA, CORNER, 1);
149DEFINE_RPMPD_PAIR(msm8916, vddmx, vddmx_ao, LDOA, CORNER, 3);
150
151DEFINE_RPMPD_VFC(msm8916, vddcx_vfc, SMPA, 1);
152
153static struct rpmpd *msm8916_rpmpds[] = {
154 [MSM8916_VDDCX] = &msm8916_vddcx,
155 [MSM8916_VDDCX_AO] = &msm8916_vddcx_ao,
156 [MSM8916_VDDCX_VFC] = &msm8916_vddcx_vfc,
157 [MSM8916_VDDMX] = &msm8916_vddmx,
158 [MSM8916_VDDMX_AO] = &msm8916_vddmx_ao,
159};
160
161static const struct rpmpd_desc msm8916_desc = {
162 .rpmpds = msm8916_rpmpds,
163 .num_pds = ARRAY_SIZE(msm8916_rpmpds),
164 .max_state = MAX_CORNER_RPMPD_STATE,
165};
166
167
168DEFINE_RPMPD_PAIR(msm8976, vddcx, vddcx_ao, SMPA, LEVEL, 2);
169DEFINE_RPMPD_PAIR(msm8976, vddmx, vddmx_ao, SMPA, LEVEL, 6);
170
171DEFINE_RPMPD_VFL(msm8976, vddcx_vfl, RWSC, 2);
172DEFINE_RPMPD_VFL(msm8976, vddmx_vfl, RWSM, 6);
173
174static struct rpmpd *msm8976_rpmpds[] = {
175 [MSM8976_VDDCX] = &msm8976_vddcx,
176 [MSM8976_VDDCX_AO] = &msm8976_vddcx_ao,
177 [MSM8976_VDDCX_VFL] = &msm8976_vddcx_vfl,
178 [MSM8976_VDDMX] = &msm8976_vddmx,
179 [MSM8976_VDDMX_AO] = &msm8976_vddmx_ao,
180 [MSM8976_VDDMX_VFL] = &msm8976_vddmx_vfl,
181};
182
183static const struct rpmpd_desc msm8976_desc = {
184 .rpmpds = msm8976_rpmpds,
185 .num_pds = ARRAY_SIZE(msm8976_rpmpds),
186 .max_state = RPM_SMD_LEVEL_TURBO_HIGH,
187};
188
189
190DEFINE_RPMPD_PAIR(msm8994, vddcx, vddcx_ao, SMPA, CORNER, 1);
191DEFINE_RPMPD_PAIR(msm8994, vddmx, vddmx_ao, SMPA, CORNER, 2);
192
193DEFINE_RPMPD_CORNER(msm8994, vddgfx, SMPB, 2);
194
195DEFINE_RPMPD_VFC(msm8994, vddcx_vfc, SMPA, 1);
196DEFINE_RPMPD_VFC(msm8994, vddgfx_vfc, SMPB, 2);
197
198static struct rpmpd *msm8994_rpmpds[] = {
199 [MSM8994_VDDCX] = &msm8994_vddcx,
200 [MSM8994_VDDCX_AO] = &msm8994_vddcx_ao,
201 [MSM8994_VDDCX_VFC] = &msm8994_vddcx_vfc,
202 [MSM8994_VDDMX] = &msm8994_vddmx,
203 [MSM8994_VDDMX_AO] = &msm8994_vddmx_ao,
204 [MSM8994_VDDGFX] = &msm8994_vddgfx,
205 [MSM8994_VDDGFX_VFC] = &msm8994_vddgfx_vfc,
206};
207
208static const struct rpmpd_desc msm8994_desc = {
209 .rpmpds = msm8994_rpmpds,
210 .num_pds = ARRAY_SIZE(msm8994_rpmpds),
211 .max_state = MAX_CORNER_RPMPD_STATE,
212};
213
214
215DEFINE_RPMPD_PAIR(msm8996, vddcx, vddcx_ao, SMPA, CORNER, 1);
216DEFINE_RPMPD_PAIR(msm8996, vddmx, vddmx_ao, SMPA, CORNER, 2);
217DEFINE_RPMPD_CORNER(msm8996, vddsscx, LDOA, 26);
218
219DEFINE_RPMPD_VFC(msm8996, vddcx_vfc, SMPA, 1);
220DEFINE_RPMPD_VFC(msm8996, vddsscx_vfc, LDOA, 26);
221
222static struct rpmpd *msm8996_rpmpds[] = {
223 [MSM8996_VDDCX] = &msm8996_vddcx,
224 [MSM8996_VDDCX_AO] = &msm8996_vddcx_ao,
225 [MSM8996_VDDCX_VFC] = &msm8996_vddcx_vfc,
226 [MSM8996_VDDMX] = &msm8996_vddmx,
227 [MSM8996_VDDMX_AO] = &msm8996_vddmx_ao,
228 [MSM8996_VDDSSCX] = &msm8996_vddsscx,
229 [MSM8996_VDDSSCX_VFC] = &msm8996_vddsscx_vfc,
230};
231
232static const struct rpmpd_desc msm8996_desc = {
233 .rpmpds = msm8996_rpmpds,
234 .num_pds = ARRAY_SIZE(msm8996_rpmpds),
235 .max_state = MAX_CORNER_RPMPD_STATE,
236};
237
238
239DEFINE_RPMPD_PAIR(msm8998, vddcx, vddcx_ao, RWCX, LEVEL, 0);
240DEFINE_RPMPD_VFL(msm8998, vddcx_vfl, RWCX, 0);
241
242DEFINE_RPMPD_PAIR(msm8998, vddmx, vddmx_ao, RWMX, LEVEL, 0);
243DEFINE_RPMPD_VFL(msm8998, vddmx_vfl, RWMX, 0);
244
245DEFINE_RPMPD_LEVEL(msm8998, vdd_ssccx, RWSC, 0);
246DEFINE_RPMPD_VFL(msm8998, vdd_ssccx_vfl, RWSC, 0);
247
248DEFINE_RPMPD_LEVEL(msm8998, vdd_sscmx, RWSM, 0);
249DEFINE_RPMPD_VFL(msm8998, vdd_sscmx_vfl, RWSM, 0);
250
251static struct rpmpd *msm8998_rpmpds[] = {
252 [MSM8998_VDDCX] = &msm8998_vddcx,
253 [MSM8998_VDDCX_AO] = &msm8998_vddcx_ao,
254 [MSM8998_VDDCX_VFL] = &msm8998_vddcx_vfl,
255 [MSM8998_VDDMX] = &msm8998_vddmx,
256 [MSM8998_VDDMX_AO] = &msm8998_vddmx_ao,
257 [MSM8998_VDDMX_VFL] = &msm8998_vddmx_vfl,
258 [MSM8998_SSCCX] = &msm8998_vdd_ssccx,
259 [MSM8998_SSCCX_VFL] = &msm8998_vdd_ssccx_vfl,
260 [MSM8998_SSCMX] = &msm8998_vdd_sscmx,
261 [MSM8998_SSCMX_VFL] = &msm8998_vdd_sscmx_vfl,
262};
263
264static const struct rpmpd_desc msm8998_desc = {
265 .rpmpds = msm8998_rpmpds,
266 .num_pds = ARRAY_SIZE(msm8998_rpmpds),
267 .max_state = RPM_SMD_LEVEL_BINNING,
268};
269
270
271DEFINE_RPMPD_PAIR(qcs404, vddmx, vddmx_ao, RWMX, LEVEL, 0);
272DEFINE_RPMPD_VFL(qcs404, vddmx_vfl, RWMX, 0);
273
274DEFINE_RPMPD_LEVEL(qcs404, vdd_lpicx, RWLC, 0);
275DEFINE_RPMPD_VFL(qcs404, vdd_lpicx_vfl, RWLC, 0);
276
277DEFINE_RPMPD_LEVEL(qcs404, vdd_lpimx, RWLM, 0);
278DEFINE_RPMPD_VFL(qcs404, vdd_lpimx_vfl, RWLM, 0);
279
280static struct rpmpd *qcs404_rpmpds[] = {
281 [QCS404_VDDMX] = &qcs404_vddmx,
282 [QCS404_VDDMX_AO] = &qcs404_vddmx_ao,
283 [QCS404_VDDMX_VFL] = &qcs404_vddmx_vfl,
284 [QCS404_LPICX] = &qcs404_vdd_lpicx,
285 [QCS404_LPICX_VFL] = &qcs404_vdd_lpicx_vfl,
286 [QCS404_LPIMX] = &qcs404_vdd_lpimx,
287 [QCS404_LPIMX_VFL] = &qcs404_vdd_lpimx_vfl,
288};
289
290static const struct rpmpd_desc qcs404_desc = {
291 .rpmpds = qcs404_rpmpds,
292 .num_pds = ARRAY_SIZE(qcs404_rpmpds),
293 .max_state = RPM_SMD_LEVEL_BINNING,
294};
295
296
297DEFINE_RPMPD_PAIR(sdm660, vddcx, vddcx_ao, RWCX, LEVEL, 0);
298DEFINE_RPMPD_VFL(sdm660, vddcx_vfl, RWCX, 0);
299
300DEFINE_RPMPD_PAIR(sdm660, vddmx, vddmx_ao, RWMX, LEVEL, 0);
301DEFINE_RPMPD_VFL(sdm660, vddmx_vfl, RWMX, 0);
302
303DEFINE_RPMPD_LEVEL(sdm660, vdd_ssccx, RWLC, 0);
304DEFINE_RPMPD_VFL(sdm660, vdd_ssccx_vfl, RWLC, 0);
305
306DEFINE_RPMPD_LEVEL(sdm660, vdd_sscmx, RWLM, 0);
307DEFINE_RPMPD_VFL(sdm660, vdd_sscmx_vfl, RWLM, 0);
308
309static struct rpmpd *sdm660_rpmpds[] = {
310 [SDM660_VDDCX] = &sdm660_vddcx,
311 [SDM660_VDDCX_AO] = &sdm660_vddcx_ao,
312 [SDM660_VDDCX_VFL] = &sdm660_vddcx_vfl,
313 [SDM660_VDDMX] = &sdm660_vddmx,
314 [SDM660_VDDMX_AO] = &sdm660_vddmx_ao,
315 [SDM660_VDDMX_VFL] = &sdm660_vddmx_vfl,
316 [SDM660_SSCCX] = &sdm660_vdd_ssccx,
317 [SDM660_SSCCX_VFL] = &sdm660_vdd_ssccx_vfl,
318 [SDM660_SSCMX] = &sdm660_vdd_sscmx,
319 [SDM660_SSCMX_VFL] = &sdm660_vdd_sscmx_vfl,
320};
321
322static const struct rpmpd_desc sdm660_desc = {
323 .rpmpds = sdm660_rpmpds,
324 .num_pds = ARRAY_SIZE(sdm660_rpmpds),
325 .max_state = RPM_SMD_LEVEL_TURBO,
326};
327
328static const struct of_device_id rpmpd_match_table[] = {
329 { .compatible = "qcom,msm8916-rpmpd", .data = &msm8916_desc },
330 { .compatible = "qcom,msm8939-rpmpd", .data = &msm8939_desc },
331 { .compatible = "qcom,msm8976-rpmpd", .data = &msm8976_desc },
332 { .compatible = "qcom,msm8994-rpmpd", .data = &msm8994_desc },
333 { .compatible = "qcom,msm8996-rpmpd", .data = &msm8996_desc },
334 { .compatible = "qcom,msm8998-rpmpd", .data = &msm8998_desc },
335 { .compatible = "qcom,qcs404-rpmpd", .data = &qcs404_desc },
336 { .compatible = "qcom,sdm660-rpmpd", .data = &sdm660_desc },
337 { }
338};
339MODULE_DEVICE_TABLE(of, rpmpd_match_table);
340
341static int rpmpd_send_enable(struct rpmpd *pd, bool enable)
342{
343 struct rpmpd_req req = {
344 .key = KEY_ENABLE,
345 .nbytes = cpu_to_le32(sizeof(u32)),
346 .value = cpu_to_le32(enable),
347 };
348
349 return qcom_rpm_smd_write(pd->rpm, QCOM_SMD_RPM_ACTIVE_STATE,
350 pd->res_type, pd->res_id, &req, sizeof(req));
351}
352
353static int rpmpd_send_corner(struct rpmpd *pd, int state, unsigned int corner)
354{
355 struct rpmpd_req req = {
356 .key = pd->key,
357 .nbytes = cpu_to_le32(sizeof(u32)),
358 .value = cpu_to_le32(corner),
359 };
360
361 return qcom_rpm_smd_write(pd->rpm, state, pd->res_type, pd->res_id,
362 &req, sizeof(req));
363};
364
365static void to_active_sleep(struct rpmpd *pd, unsigned int corner,
366 unsigned int *active, unsigned int *sleep)
367{
368 *active = corner;
369
370 if (pd->active_only)
371 *sleep = 0;
372 else
373 *sleep = *active;
374}
375
376static int rpmpd_aggregate_corner(struct rpmpd *pd)
377{
378 int ret;
379 struct rpmpd *peer = pd->peer;
380 unsigned int active_corner, sleep_corner;
381 unsigned int this_active_corner = 0, this_sleep_corner = 0;
382 unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
383
384 to_active_sleep(pd, pd->corner, &this_active_corner, &this_sleep_corner);
385
386 if (peer && peer->enabled)
387 to_active_sleep(peer, peer->corner, &peer_active_corner,
388 &peer_sleep_corner);
389
390 active_corner = max(this_active_corner, peer_active_corner);
391
392 ret = rpmpd_send_corner(pd, QCOM_SMD_RPM_ACTIVE_STATE, active_corner);
393 if (ret)
394 return ret;
395
396 sleep_corner = max(this_sleep_corner, peer_sleep_corner);
397
398 return rpmpd_send_corner(pd, QCOM_SMD_RPM_SLEEP_STATE, sleep_corner);
399}
400
401static int rpmpd_power_on(struct generic_pm_domain *domain)
402{
403 int ret;
404 struct rpmpd *pd = domain_to_rpmpd(domain);
405
406 mutex_lock(&rpmpd_lock);
407
408 ret = rpmpd_send_enable(pd, true);
409 if (ret)
410 goto out;
411
412 pd->enabled = true;
413
414 if (pd->corner)
415 ret = rpmpd_aggregate_corner(pd);
416
417out:
418 mutex_unlock(&rpmpd_lock);
419
420 return ret;
421}
422
423static int rpmpd_power_off(struct generic_pm_domain *domain)
424{
425 int ret;
426 struct rpmpd *pd = domain_to_rpmpd(domain);
427
428 mutex_lock(&rpmpd_lock);
429
430 ret = rpmpd_send_enable(pd, false);
431 if (!ret)
432 pd->enabled = false;
433
434 mutex_unlock(&rpmpd_lock);
435
436 return ret;
437}
438
439static int rpmpd_set_performance(struct generic_pm_domain *domain,
440 unsigned int state)
441{
442 int ret = 0;
443 struct rpmpd *pd = domain_to_rpmpd(domain);
444
445 if (state > pd->max_state)
446 state = pd->max_state;
447
448 mutex_lock(&rpmpd_lock);
449
450 pd->corner = state;
451
452
453 if (!pd->enabled && pd->key != KEY_FLOOR_CORNER &&
454 pd->key != KEY_FLOOR_LEVEL)
455 goto out;
456
457 ret = rpmpd_aggregate_corner(pd);
458
459out:
460 mutex_unlock(&rpmpd_lock);
461
462 return ret;
463}
464
465static unsigned int rpmpd_get_performance(struct generic_pm_domain *genpd,
466 struct dev_pm_opp *opp)
467{
468 return dev_pm_opp_get_level(opp);
469}
470
471static int rpmpd_probe(struct platform_device *pdev)
472{
473 int i;
474 size_t num;
475 struct genpd_onecell_data *data;
476 struct qcom_smd_rpm *rpm;
477 struct rpmpd **rpmpds;
478 const struct rpmpd_desc *desc;
479
480 rpm = dev_get_drvdata(pdev->dev.parent);
481 if (!rpm) {
482 dev_err(&pdev->dev, "Unable to retrieve handle to RPM\n");
483 return -ENODEV;
484 }
485
486 desc = of_device_get_match_data(&pdev->dev);
487 if (!desc)
488 return -EINVAL;
489
490 rpmpds = desc->rpmpds;
491 num = desc->num_pds;
492
493 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
494 if (!data)
495 return -ENOMEM;
496
497 data->domains = devm_kcalloc(&pdev->dev, num, sizeof(*data->domains),
498 GFP_KERNEL);
499 data->num_domains = num;
500
501 for (i = 0; i < num; i++) {
502 if (!rpmpds[i]) {
503 dev_warn(&pdev->dev, "rpmpds[] with empty entry at index=%d\n",
504 i);
505 continue;
506 }
507
508 rpmpds[i]->rpm = rpm;
509 rpmpds[i]->max_state = desc->max_state;
510 rpmpds[i]->pd.power_off = rpmpd_power_off;
511 rpmpds[i]->pd.power_on = rpmpd_power_on;
512 rpmpds[i]->pd.set_performance_state = rpmpd_set_performance;
513 rpmpds[i]->pd.opp_to_performance_state = rpmpd_get_performance;
514 pm_genpd_init(&rpmpds[i]->pd, NULL, true);
515
516 data->domains[i] = &rpmpds[i]->pd;
517 }
518
519 return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
520}
521
522static struct platform_driver rpmpd_driver = {
523 .driver = {
524 .name = "qcom-rpmpd",
525 .of_match_table = rpmpd_match_table,
526 .suppress_bind_attrs = true,
527 },
528 .probe = rpmpd_probe,
529};
530
531static int __init rpmpd_init(void)
532{
533 return platform_driver_register(&rpmpd_driver);
534}
535core_initcall(rpmpd_init);
536
537MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPM Power Domain Driver");
538MODULE_LICENSE("GPL v2");
539