xref: /openbmc/linux/drivers/pmdomain/qcom/rpmhpd.c (revision 6db6b729)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, The Linux Foundation. All rights reserved.*/
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/slab.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
14 #include <soc/qcom/cmd-db.h>
15 #include <soc/qcom/rpmh.h>
16 #include <dt-bindings/power/qcom-rpmpd.h>
17 #include <dt-bindings/power/qcom,rpmhpd.h>
18 
19 #define domain_to_rpmhpd(domain) container_of(domain, struct rpmhpd, pd)
20 
21 #define RPMH_ARC_MAX_LEVELS	16
22 
23 /**
24  * struct rpmhpd - top level RPMh power domain resource data structure
25  * @dev:		rpmh power domain controller device
26  * @pd:			generic_pm_domain corresponding to the power domain
27  * @parent:		generic_pm_domain corresponding 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  * @enable_corner:	lowest non-zero corner
34  * @level:		An array of level (vlvl) to corner (hlvl) mappings
35  *			derived from cmd-db
36  * @level_count:	Number of levels supported by the power domain. max
37  *			being 16 (0 - 15)
38  * @enabled:		true if the power domain is enabled
39  * @res_name:		Resource name used for cmd-db lookup
40  * @addr:		Resource address as looped up using resource name from
41  *			cmd-db
42  * @state_synced:	Indicator that sync_state has been invoked for the rpmhpd resource
43  */
44 struct rpmhpd {
45 	struct device	*dev;
46 	struct generic_pm_domain pd;
47 	struct generic_pm_domain *parent;
48 	struct rpmhpd	*peer;
49 	const bool	active_only;
50 	unsigned int	corner;
51 	unsigned int	active_corner;
52 	unsigned int	enable_corner;
53 	u32		level[RPMH_ARC_MAX_LEVELS];
54 	size_t		level_count;
55 	bool		enabled;
56 	const char	*res_name;
57 	u32		addr;
58 	bool		state_synced;
59 };
60 
61 struct rpmhpd_desc {
62 	struct rpmhpd **rpmhpds;
63 	size_t num_pds;
64 };
65 
66 static DEFINE_MUTEX(rpmhpd_lock);
67 
68 /* RPMH powerdomains */
69 
70 static struct rpmhpd cx_ao;
71 static struct rpmhpd mx;
72 static struct rpmhpd mx_ao;
73 static struct rpmhpd cx = {
74 	.pd = { .name = "cx", },
75 	.peer = &cx_ao,
76 	.res_name = "cx.lvl",
77 };
78 
79 static struct rpmhpd cx_ao = {
80 	.pd = { .name = "cx_ao", },
81 	.active_only = true,
82 	.peer = &cx,
83 	.res_name = "cx.lvl",
84 };
85 
86 static struct rpmhpd cx_ao_w_mx_parent;
87 static struct rpmhpd cx_w_mx_parent = {
88 	.pd = { .name = "cx", },
89 	.peer = &cx_ao_w_mx_parent,
90 	.parent = &mx.pd,
91 	.res_name = "cx.lvl",
92 };
93 
94 static struct rpmhpd cx_ao_w_mx_parent = {
95 	.pd = { .name = "cx_ao", },
96 	.active_only = true,
97 	.peer = &cx_w_mx_parent,
98 	.parent = &mx_ao.pd,
99 	.res_name = "cx.lvl",
100 };
101 
102 static struct rpmhpd ebi = {
103 	.pd = { .name = "ebi", },
104 	.res_name = "ebi.lvl",
105 };
106 
107 static struct rpmhpd gfx = {
108 	.pd = { .name = "gfx", },
109 	.res_name = "gfx.lvl",
110 };
111 
112 static struct rpmhpd lcx = {
113 	.pd = { .name = "lcx", },
114 	.res_name = "lcx.lvl",
115 };
116 
117 static struct rpmhpd lmx = {
118 	.pd = { .name = "lmx", },
119 	.res_name = "lmx.lvl",
120 };
121 
122 static struct rpmhpd mmcx_ao;
123 static struct rpmhpd mmcx = {
124 	.pd = { .name = "mmcx", },
125 	.peer = &mmcx_ao,
126 	.res_name = "mmcx.lvl",
127 };
128 
129 static struct rpmhpd mmcx_ao = {
130 	.pd = { .name = "mmcx_ao", },
131 	.active_only = true,
132 	.peer = &mmcx,
133 	.res_name = "mmcx.lvl",
134 };
135 
136 static struct rpmhpd mmcx_ao_w_cx_parent;
137 static struct rpmhpd mmcx_w_cx_parent = {
138 	.pd = { .name = "mmcx", },
139 	.peer = &mmcx_ao_w_cx_parent,
140 	.parent = &cx.pd,
141 	.res_name = "mmcx.lvl",
142 };
143 
144 static struct rpmhpd mmcx_ao_w_cx_parent = {
145 	.pd = { .name = "mmcx_ao", },
146 	.active_only = true,
147 	.peer = &mmcx_w_cx_parent,
148 	.parent = &cx_ao.pd,
149 	.res_name = "mmcx.lvl",
150 };
151 
152 static struct rpmhpd mss = {
153 	.pd = { .name = "mss", },
154 	.res_name = "mss.lvl",
155 };
156 
157 static struct rpmhpd mx_ao;
158 static struct rpmhpd mx = {
159 	.pd = { .name = "mx", },
160 	.peer = &mx_ao,
161 	.res_name = "mx.lvl",
162 };
163 
164 static struct rpmhpd mx_ao = {
165 	.pd = { .name = "mx_ao", },
166 	.active_only = true,
167 	.peer = &mx,
168 	.res_name = "mx.lvl",
169 };
170 
171 static struct rpmhpd mxc_ao;
172 static struct rpmhpd mxc = {
173 	.pd = { .name = "mxc", },
174 	.peer = &mxc_ao,
175 	.res_name = "mxc.lvl",
176 };
177 
178 static struct rpmhpd mxc_ao = {
179 	.pd = { .name = "mxc_ao", },
180 	.active_only = true,
181 	.peer = &mxc,
182 	.res_name = "mxc.lvl",
183 };
184 
185 static struct rpmhpd nsp = {
186 	.pd = { .name = "nsp", },
187 	.res_name = "nsp.lvl",
188 };
189 
190 static struct rpmhpd nsp0 = {
191 	.pd = { .name = "nsp0", },
192 	.res_name = "nsp0.lvl",
193 };
194 
195 static struct rpmhpd nsp1 = {
196 	.pd = { .name = "nsp1", },
197 	.res_name = "nsp1.lvl",
198 };
199 
200 static struct rpmhpd qphy = {
201 	.pd = { .name = "qphy", },
202 	.res_name = "qphy.lvl",
203 };
204 
205 /* SA8540P RPMH powerdomains */
206 static struct rpmhpd *sa8540p_rpmhpds[] = {
207 	[SC8280XP_CX] = &cx,
208 	[SC8280XP_CX_AO] = &cx_ao,
209 	[SC8280XP_EBI] = &ebi,
210 	[SC8280XP_GFX] = &gfx,
211 	[SC8280XP_LCX] = &lcx,
212 	[SC8280XP_LMX] = &lmx,
213 	[SC8280XP_MMCX] = &mmcx,
214 	[SC8280XP_MMCX_AO] = &mmcx_ao,
215 	[SC8280XP_MX] = &mx,
216 	[SC8280XP_MX_AO] = &mx_ao,
217 	[SC8280XP_NSP] = &nsp,
218 };
219 
220 static const struct rpmhpd_desc sa8540p_desc = {
221 	.rpmhpds = sa8540p_rpmhpds,
222 	.num_pds = ARRAY_SIZE(sa8540p_rpmhpds),
223 };
224 
225 /* SA8775P RPMH power domains */
226 static struct rpmhpd *sa8775p_rpmhpds[] = {
227 	[SA8775P_CX] = &cx,
228 	[SA8775P_CX_AO] = &cx_ao,
229 	[SA8775P_EBI] = &ebi,
230 	[SA8775P_GFX] = &gfx,
231 	[SA8775P_LCX] = &lcx,
232 	[SA8775P_LMX] = &lmx,
233 	[SA8775P_MMCX] = &mmcx,
234 	[SA8775P_MMCX_AO] = &mmcx_ao,
235 	[SA8775P_MXC] = &mxc,
236 	[SA8775P_MXC_AO] = &mxc_ao,
237 	[SA8775P_MX] = &mx,
238 	[SA8775P_MX_AO] = &mx_ao,
239 	[SA8775P_NSP0] = &nsp0,
240 	[SA8775P_NSP1] = &nsp1,
241 };
242 
243 static const struct rpmhpd_desc sa8775p_desc = {
244 	.rpmhpds = sa8775p_rpmhpds,
245 	.num_pds = ARRAY_SIZE(sa8775p_rpmhpds),
246 };
247 
248 /* SDM670 RPMH powerdomains */
249 static struct rpmhpd *sdm670_rpmhpds[] = {
250 	[SDM670_CX] = &cx_w_mx_parent,
251 	[SDM670_CX_AO] = &cx_ao_w_mx_parent,
252 	[SDM670_GFX] = &gfx,
253 	[SDM670_LCX] = &lcx,
254 	[SDM670_LMX] = &lmx,
255 	[SDM670_MSS] = &mss,
256 	[SDM670_MX] = &mx,
257 	[SDM670_MX_AO] = &mx_ao,
258 };
259 
260 static const struct rpmhpd_desc sdm670_desc = {
261 	.rpmhpds = sdm670_rpmhpds,
262 	.num_pds = ARRAY_SIZE(sdm670_rpmhpds),
263 };
264 
265 /* SDM845 RPMH powerdomains */
266 static struct rpmhpd *sdm845_rpmhpds[] = {
267 	[SDM845_CX] = &cx_w_mx_parent,
268 	[SDM845_CX_AO] = &cx_ao_w_mx_parent,
269 	[SDM845_EBI] = &ebi,
270 	[SDM845_GFX] = &gfx,
271 	[SDM845_LCX] = &lcx,
272 	[SDM845_LMX] = &lmx,
273 	[SDM845_MSS] = &mss,
274 	[SDM845_MX] = &mx,
275 	[SDM845_MX_AO] = &mx_ao,
276 };
277 
278 static const struct rpmhpd_desc sdm845_desc = {
279 	.rpmhpds = sdm845_rpmhpds,
280 	.num_pds = ARRAY_SIZE(sdm845_rpmhpds),
281 };
282 
283 /* SDX55 RPMH powerdomains */
284 static struct rpmhpd *sdx55_rpmhpds[] = {
285 	[SDX55_CX] = &cx_w_mx_parent,
286 	[SDX55_MSS] = &mss,
287 	[SDX55_MX] = &mx,
288 };
289 
290 static const struct rpmhpd_desc sdx55_desc = {
291 	.rpmhpds = sdx55_rpmhpds,
292 	.num_pds = ARRAY_SIZE(sdx55_rpmhpds),
293 };
294 
295 /* SDX65 RPMH powerdomains */
296 static struct rpmhpd *sdx65_rpmhpds[] = {
297 	[SDX65_CX] = &cx_w_mx_parent,
298 	[SDX65_CX_AO] = &cx_ao_w_mx_parent,
299 	[SDX65_MSS] = &mss,
300 	[SDX65_MX] = &mx,
301 	[SDX65_MX_AO] = &mx_ao,
302 	[SDX65_MXC] = &mxc,
303 };
304 
305 static const struct rpmhpd_desc sdx65_desc = {
306 	.rpmhpds = sdx65_rpmhpds,
307 	.num_pds = ARRAY_SIZE(sdx65_rpmhpds),
308 };
309 
310 /* SDX75 RPMH powerdomains */
311 static struct rpmhpd *sdx75_rpmhpds[] = {
312 	[RPMHPD_CX] = &cx,
313 	[RPMHPD_CX_AO] = &cx_ao,
314 	[RPMHPD_MSS] = &mss,
315 	[RPMHPD_MX] = &mx,
316 	[RPMHPD_MX_AO] = &mx_ao,
317 	[RPMHPD_MXC] = &mxc,
318 };
319 
320 static const struct rpmhpd_desc sdx75_desc = {
321 	.rpmhpds = sdx75_rpmhpds,
322 	.num_pds = ARRAY_SIZE(sdx75_rpmhpds),
323 };
324 
325 /* SM6350 RPMH powerdomains */
326 static struct rpmhpd *sm6350_rpmhpds[] = {
327 	[SM6350_CX] = &cx_w_mx_parent,
328 	[SM6350_GFX] = &gfx,
329 	[SM6350_LCX] = &lcx,
330 	[SM6350_LMX] = &lmx,
331 	[SM6350_MSS] = &mss,
332 	[SM6350_MX] = &mx,
333 };
334 
335 static const struct rpmhpd_desc sm6350_desc = {
336 	.rpmhpds = sm6350_rpmhpds,
337 	.num_pds = ARRAY_SIZE(sm6350_rpmhpds),
338 };
339 
340 /* SM8150 RPMH powerdomains */
341 static struct rpmhpd *sm8150_rpmhpds[] = {
342 	[SM8150_CX] = &cx_w_mx_parent,
343 	[SM8150_CX_AO] = &cx_ao_w_mx_parent,
344 	[SM8150_EBI] = &ebi,
345 	[SM8150_GFX] = &gfx,
346 	[SM8150_LCX] = &lcx,
347 	[SM8150_LMX] = &lmx,
348 	[SM8150_MMCX] = &mmcx,
349 	[SM8150_MMCX_AO] = &mmcx_ao,
350 	[SM8150_MSS] = &mss,
351 	[SM8150_MX] = &mx,
352 	[SM8150_MX_AO] = &mx_ao,
353 };
354 
355 static const struct rpmhpd_desc sm8150_desc = {
356 	.rpmhpds = sm8150_rpmhpds,
357 	.num_pds = ARRAY_SIZE(sm8150_rpmhpds),
358 };
359 
360 static struct rpmhpd *sa8155p_rpmhpds[] = {
361 	[SA8155P_CX] = &cx_w_mx_parent,
362 	[SA8155P_CX_AO] = &cx_ao_w_mx_parent,
363 	[SA8155P_EBI] = &ebi,
364 	[SA8155P_GFX] = &gfx,
365 	[SA8155P_MSS] = &mss,
366 	[SA8155P_MX] = &mx,
367 	[SA8155P_MX_AO] = &mx_ao,
368 };
369 
370 static const struct rpmhpd_desc sa8155p_desc = {
371 	.rpmhpds = sa8155p_rpmhpds,
372 	.num_pds = ARRAY_SIZE(sa8155p_rpmhpds),
373 };
374 
375 /* SM8250 RPMH powerdomains */
376 static struct rpmhpd *sm8250_rpmhpds[] = {
377 	[RPMHPD_CX] = &cx_w_mx_parent,
378 	[RPMHPD_CX_AO] = &cx_ao_w_mx_parent,
379 	[RPMHPD_EBI] = &ebi,
380 	[RPMHPD_GFX] = &gfx,
381 	[RPMHPD_LCX] = &lcx,
382 	[RPMHPD_LMX] = &lmx,
383 	[RPMHPD_MMCX] = &mmcx,
384 	[RPMHPD_MMCX_AO] = &mmcx_ao,
385 	[RPMHPD_MX] = &mx,
386 	[RPMHPD_MX_AO] = &mx_ao,
387 };
388 
389 static const struct rpmhpd_desc sm8250_desc = {
390 	.rpmhpds = sm8250_rpmhpds,
391 	.num_pds = ARRAY_SIZE(sm8250_rpmhpds),
392 };
393 
394 /* SM8350 Power domains */
395 static struct rpmhpd *sm8350_rpmhpds[] = {
396 	[RPMHPD_CX] = &cx_w_mx_parent,
397 	[RPMHPD_CX_AO] = &cx_ao_w_mx_parent,
398 	[RPMHPD_EBI] = &ebi,
399 	[RPMHPD_GFX] = &gfx,
400 	[RPMHPD_LCX] = &lcx,
401 	[RPMHPD_LMX] = &lmx,
402 	[RPMHPD_MMCX] = &mmcx,
403 	[RPMHPD_MMCX_AO] = &mmcx_ao,
404 	[RPMHPD_MSS] = &mss,
405 	[RPMHPD_MX] = &mx,
406 	[RPMHPD_MX_AO] = &mx_ao,
407 	[RPMHPD_MXC] = &mxc,
408 	[RPMHPD_MXC_AO] = &mxc_ao,
409 };
410 
411 static const struct rpmhpd_desc sm8350_desc = {
412 	.rpmhpds = sm8350_rpmhpds,
413 	.num_pds = ARRAY_SIZE(sm8350_rpmhpds),
414 };
415 
416 /* SM8450 RPMH powerdomains */
417 static struct rpmhpd *sm8450_rpmhpds[] = {
418 	[RPMHPD_CX] = &cx,
419 	[RPMHPD_CX_AO] = &cx_ao,
420 	[RPMHPD_EBI] = &ebi,
421 	[RPMHPD_GFX] = &gfx,
422 	[RPMHPD_LCX] = &lcx,
423 	[RPMHPD_LMX] = &lmx,
424 	[RPMHPD_MMCX] = &mmcx_w_cx_parent,
425 	[RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent,
426 	[RPMHPD_MSS] = &mss,
427 	[RPMHPD_MX] = &mx,
428 	[RPMHPD_MX_AO] = &mx_ao,
429 	[RPMHPD_MXC] = &mxc,
430 	[RPMHPD_MXC_AO] = &mxc_ao,
431 };
432 
433 static const struct rpmhpd_desc sm8450_desc = {
434 	.rpmhpds = sm8450_rpmhpds,
435 	.num_pds = ARRAY_SIZE(sm8450_rpmhpds),
436 };
437 
438 /* SM8550 RPMH powerdomains */
439 static struct rpmhpd *sm8550_rpmhpds[] = {
440 	[RPMHPD_CX] = &cx,
441 	[RPMHPD_CX_AO] = &cx_ao,
442 	[RPMHPD_EBI] = &ebi,
443 	[RPMHPD_GFX] = &gfx,
444 	[RPMHPD_LCX] = &lcx,
445 	[RPMHPD_LMX] = &lmx,
446 	[RPMHPD_MMCX] = &mmcx_w_cx_parent,
447 	[RPMHPD_MMCX_AO] = &mmcx_ao_w_cx_parent,
448 	[RPMHPD_MSS] = &mss,
449 	[RPMHPD_MX] = &mx,
450 	[RPMHPD_MX_AO] = &mx_ao,
451 	[RPMHPD_MXC] = &mxc,
452 	[RPMHPD_MXC_AO] = &mxc_ao,
453 	[RPMHPD_NSP] = &nsp,
454 };
455 
456 static const struct rpmhpd_desc sm8550_desc = {
457 	.rpmhpds = sm8550_rpmhpds,
458 	.num_pds = ARRAY_SIZE(sm8550_rpmhpds),
459 };
460 
461 /* QDU1000/QRU1000 RPMH powerdomains */
462 static struct rpmhpd *qdu1000_rpmhpds[] = {
463 	[QDU1000_CX] = &cx,
464 	[QDU1000_EBI] = &ebi,
465 	[QDU1000_MSS] = &mss,
466 	[QDU1000_MX] = &mx,
467 };
468 
469 static const struct rpmhpd_desc qdu1000_desc = {
470 	.rpmhpds = qdu1000_rpmhpds,
471 	.num_pds = ARRAY_SIZE(qdu1000_rpmhpds),
472 };
473 
474 /* SC7180 RPMH powerdomains */
475 static struct rpmhpd *sc7180_rpmhpds[] = {
476 	[SC7180_CX] = &cx_w_mx_parent,
477 	[SC7180_CX_AO] = &cx_ao_w_mx_parent,
478 	[SC7180_GFX] = &gfx,
479 	[SC7180_LCX] = &lcx,
480 	[SC7180_LMX] = &lmx,
481 	[SC7180_MSS] = &mss,
482 	[SC7180_MX] = &mx,
483 	[SC7180_MX_AO] = &mx_ao,
484 };
485 
486 static const struct rpmhpd_desc sc7180_desc = {
487 	.rpmhpds = sc7180_rpmhpds,
488 	.num_pds = ARRAY_SIZE(sc7180_rpmhpds),
489 };
490 
491 /* SC7280 RPMH powerdomains */
492 static struct rpmhpd *sc7280_rpmhpds[] = {
493 	[SC7280_CX] = &cx,
494 	[SC7280_CX_AO] = &cx_ao,
495 	[SC7280_EBI] = &ebi,
496 	[SC7280_GFX] = &gfx,
497 	[SC7280_LCX] = &lcx,
498 	[SC7280_LMX] = &lmx,
499 	[SC7280_MSS] = &mss,
500 	[SC7280_MX] = &mx,
501 	[SC7280_MX_AO] = &mx_ao,
502 };
503 
504 static const struct rpmhpd_desc sc7280_desc = {
505 	.rpmhpds = sc7280_rpmhpds,
506 	.num_pds = ARRAY_SIZE(sc7280_rpmhpds),
507 };
508 
509 /* SC8180x RPMH powerdomains */
510 static struct rpmhpd *sc8180x_rpmhpds[] = {
511 	[SC8180X_CX] = &cx_w_mx_parent,
512 	[SC8180X_CX_AO] = &cx_ao_w_mx_parent,
513 	[SC8180X_EBI] = &ebi,
514 	[SC8180X_GFX] = &gfx,
515 	[SC8180X_LCX] = &lcx,
516 	[SC8180X_LMX] = &lmx,
517 	[SC8180X_MMCX] = &mmcx,
518 	[SC8180X_MMCX_AO] = &mmcx_ao,
519 	[SC8180X_MSS] = &mss,
520 	[SC8180X_MX] = &mx,
521 	[SC8180X_MX_AO] = &mx_ao,
522 };
523 
524 static const struct rpmhpd_desc sc8180x_desc = {
525 	.rpmhpds = sc8180x_rpmhpds,
526 	.num_pds = ARRAY_SIZE(sc8180x_rpmhpds),
527 };
528 
529 /* SC8280xp RPMH powerdomains */
530 static struct rpmhpd *sc8280xp_rpmhpds[] = {
531 	[SC8280XP_CX] = &cx,
532 	[SC8280XP_CX_AO] = &cx_ao,
533 	[SC8280XP_EBI] = &ebi,
534 	[SC8280XP_GFX] = &gfx,
535 	[SC8280XP_LCX] = &lcx,
536 	[SC8280XP_LMX] = &lmx,
537 	[SC8280XP_MMCX] = &mmcx,
538 	[SC8280XP_MMCX_AO] = &mmcx_ao,
539 	[SC8280XP_MX] = &mx,
540 	[SC8280XP_MX_AO] = &mx_ao,
541 	[SC8280XP_NSP] = &nsp,
542 	[SC8280XP_QPHY] = &qphy,
543 };
544 
545 static const struct rpmhpd_desc sc8280xp_desc = {
546 	.rpmhpds = sc8280xp_rpmhpds,
547 	.num_pds = ARRAY_SIZE(sc8280xp_rpmhpds),
548 };
549 
550 static const struct of_device_id rpmhpd_match_table[] = {
551 	{ .compatible = "qcom,qdu1000-rpmhpd", .data = &qdu1000_desc },
552 	{ .compatible = "qcom,sa8155p-rpmhpd", .data = &sa8155p_desc },
553 	{ .compatible = "qcom,sa8540p-rpmhpd", .data = &sa8540p_desc },
554 	{ .compatible = "qcom,sa8775p-rpmhpd", .data = &sa8775p_desc },
555 	{ .compatible = "qcom,sc7180-rpmhpd", .data = &sc7180_desc },
556 	{ .compatible = "qcom,sc7280-rpmhpd", .data = &sc7280_desc },
557 	{ .compatible = "qcom,sc8180x-rpmhpd", .data = &sc8180x_desc },
558 	{ .compatible = "qcom,sc8280xp-rpmhpd", .data = &sc8280xp_desc },
559 	{ .compatible = "qcom,sdm670-rpmhpd", .data = &sdm670_desc },
560 	{ .compatible = "qcom,sdm845-rpmhpd", .data = &sdm845_desc },
561 	{ .compatible = "qcom,sdx55-rpmhpd", .data = &sdx55_desc},
562 	{ .compatible = "qcom,sdx65-rpmhpd", .data = &sdx65_desc},
563 	{ .compatible = "qcom,sdx75-rpmhpd", .data = &sdx75_desc},
564 	{ .compatible = "qcom,sm6350-rpmhpd", .data = &sm6350_desc },
565 	{ .compatible = "qcom,sm8150-rpmhpd", .data = &sm8150_desc },
566 	{ .compatible = "qcom,sm8250-rpmhpd", .data = &sm8250_desc },
567 	{ .compatible = "qcom,sm8350-rpmhpd", .data = &sm8350_desc },
568 	{ .compatible = "qcom,sm8450-rpmhpd", .data = &sm8450_desc },
569 	{ .compatible = "qcom,sm8550-rpmhpd", .data = &sm8550_desc },
570 	{ }
571 };
572 MODULE_DEVICE_TABLE(of, rpmhpd_match_table);
573 
574 static int rpmhpd_send_corner(struct rpmhpd *pd, int state,
575 			      unsigned int corner, bool sync)
576 {
577 	struct tcs_cmd cmd = {
578 		.addr = pd->addr,
579 		.data = corner,
580 	};
581 
582 	/*
583 	 * Wait for an ack only when we are increasing the
584 	 * perf state of the power domain
585 	 */
586 	if (sync)
587 		return rpmh_write(pd->dev, state, &cmd, 1);
588 	else
589 		return rpmh_write_async(pd->dev, state, &cmd, 1);
590 }
591 
592 static void to_active_sleep(struct rpmhpd *pd, unsigned int corner,
593 			    unsigned int *active, unsigned int *sleep)
594 {
595 	*active = corner;
596 
597 	if (pd->active_only)
598 		*sleep = 0;
599 	else
600 		*sleep = *active;
601 }
602 
603 /*
604  * This function is used to aggregate the votes across the active only
605  * resources and its peers. The aggregated votes are sent to RPMh as
606  * ACTIVE_ONLY votes (which take effect immediately), as WAKE_ONLY votes
607  * (applied by RPMh on system wakeup) and as SLEEP votes (applied by RPMh
608  * on system sleep).
609  * We send ACTIVE_ONLY votes for resources without any peers. For others,
610  * which have an active only peer, all 3 votes are sent.
611  */
612 static int rpmhpd_aggregate_corner(struct rpmhpd *pd, unsigned int corner)
613 {
614 	int ret;
615 	struct rpmhpd *peer = pd->peer;
616 	unsigned int active_corner, sleep_corner;
617 	unsigned int this_active_corner = 0, this_sleep_corner = 0;
618 	unsigned int peer_active_corner = 0, peer_sleep_corner = 0;
619 
620 	if (pd->state_synced) {
621 		to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
622 	} else {
623 		/* Clamp to highest corner if sync_state hasn't happened */
624 		this_active_corner = pd->level_count - 1;
625 		this_sleep_corner = pd->level_count - 1;
626 	}
627 
628 	if (peer && peer->enabled)
629 		to_active_sleep(peer, peer->corner, &peer_active_corner,
630 				&peer_sleep_corner);
631 
632 	active_corner = max(this_active_corner, peer_active_corner);
633 
634 	ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
635 				 active_corner > pd->active_corner);
636 	if (ret)
637 		return ret;
638 
639 	pd->active_corner = active_corner;
640 
641 	if (peer) {
642 		peer->active_corner = active_corner;
643 
644 		ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
645 					 active_corner, false);
646 		if (ret)
647 			return ret;
648 
649 		sleep_corner = max(this_sleep_corner, peer_sleep_corner);
650 
651 		return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
652 					  false);
653 	}
654 
655 	return ret;
656 }
657 
658 static int rpmhpd_power_on(struct generic_pm_domain *domain)
659 {
660 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
661 	unsigned int corner;
662 	int ret;
663 
664 	mutex_lock(&rpmhpd_lock);
665 
666 	corner = max(pd->corner, pd->enable_corner);
667 	ret = rpmhpd_aggregate_corner(pd, corner);
668 	if (!ret)
669 		pd->enabled = true;
670 
671 	mutex_unlock(&rpmhpd_lock);
672 
673 	return ret;
674 }
675 
676 static int rpmhpd_power_off(struct generic_pm_domain *domain)
677 {
678 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
679 	int ret;
680 
681 	mutex_lock(&rpmhpd_lock);
682 
683 	ret = rpmhpd_aggregate_corner(pd, 0);
684 	if (!ret)
685 		pd->enabled = false;
686 
687 	mutex_unlock(&rpmhpd_lock);
688 
689 	return ret;
690 }
691 
692 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
693 					unsigned int level)
694 {
695 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
696 	int ret = 0, i;
697 
698 	mutex_lock(&rpmhpd_lock);
699 
700 	for (i = 0; i < pd->level_count; i++)
701 		if (level <= pd->level[i])
702 			break;
703 
704 	/*
705 	 * If the level requested is more than that supported by the
706 	 * max corner, just set it to max anyway.
707 	 */
708 	if (i == pd->level_count)
709 		i--;
710 
711 	if (pd->enabled) {
712 		/* Ensure that the domain isn't turn off */
713 		if (i < pd->enable_corner)
714 			i = pd->enable_corner;
715 
716 		ret = rpmhpd_aggregate_corner(pd, i);
717 		if (ret)
718 			goto out;
719 	}
720 
721 	pd->corner = i;
722 out:
723 	mutex_unlock(&rpmhpd_lock);
724 
725 	return ret;
726 }
727 
728 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
729 						 struct dev_pm_opp *opp)
730 {
731 	return dev_pm_opp_get_level(opp);
732 }
733 
734 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
735 {
736 	int i;
737 	const u16 *buf;
738 
739 	buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
740 	if (IS_ERR(buf))
741 		return PTR_ERR(buf);
742 
743 	/* 2 bytes used for each command DB aux data entry */
744 	rpmhpd->level_count >>= 1;
745 
746 	if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
747 		return -EINVAL;
748 
749 	for (i = 0; i < rpmhpd->level_count; i++) {
750 		rpmhpd->level[i] = buf[i];
751 
752 		/* Remember the first corner with non-zero level */
753 		if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
754 			rpmhpd->enable_corner = i;
755 
756 		/*
757 		 * The AUX data may be zero padded.  These 0 valued entries at
758 		 * the end of the map must be ignored.
759 		 */
760 		if (i > 0 && rpmhpd->level[i] == 0) {
761 			rpmhpd->level_count = i;
762 			break;
763 		}
764 		pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
765 			 rpmhpd->level[i]);
766 	}
767 
768 	return 0;
769 }
770 
771 static int rpmhpd_probe(struct platform_device *pdev)
772 {
773 	int i, ret;
774 	size_t num_pds;
775 	struct device *dev = &pdev->dev;
776 	struct genpd_onecell_data *data;
777 	struct rpmhpd **rpmhpds;
778 	const struct rpmhpd_desc *desc;
779 
780 	desc = of_device_get_match_data(dev);
781 	if (!desc)
782 		return -EINVAL;
783 
784 	rpmhpds = desc->rpmhpds;
785 	num_pds = desc->num_pds;
786 
787 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
788 	if (!data)
789 		return -ENOMEM;
790 
791 	data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
792 				     GFP_KERNEL);
793 	if (!data->domains)
794 		return -ENOMEM;
795 
796 	data->num_domains = num_pds;
797 
798 	for (i = 0; i < num_pds; i++) {
799 		if (!rpmhpds[i])
800 			continue;
801 
802 		rpmhpds[i]->dev = dev;
803 		rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
804 		if (!rpmhpds[i]->addr) {
805 			dev_err(dev, "Could not find RPMh address for resource %s\n",
806 				rpmhpds[i]->res_name);
807 			return -ENODEV;
808 		}
809 
810 		ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
811 		if (ret != CMD_DB_HW_ARC) {
812 			dev_err(dev, "RPMh slave ID mismatch\n");
813 			return -EINVAL;
814 		}
815 
816 		ret = rpmhpd_update_level_mapping(rpmhpds[i]);
817 		if (ret)
818 			return ret;
819 
820 		rpmhpds[i]->pd.power_off = rpmhpd_power_off;
821 		rpmhpds[i]->pd.power_on = rpmhpd_power_on;
822 		rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
823 		rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
824 		pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
825 
826 		data->domains[i] = &rpmhpds[i]->pd;
827 	}
828 
829 	/* Add subdomains */
830 	for (i = 0; i < num_pds; i++) {
831 		if (!rpmhpds[i])
832 			continue;
833 		if (rpmhpds[i]->parent)
834 			pm_genpd_add_subdomain(rpmhpds[i]->parent,
835 					       &rpmhpds[i]->pd);
836 	}
837 
838 	return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
839 }
840 
841 static void rpmhpd_sync_state(struct device *dev)
842 {
843 	const struct rpmhpd_desc *desc = of_device_get_match_data(dev);
844 	struct rpmhpd **rpmhpds = desc->rpmhpds;
845 	unsigned int corner;
846 	struct rpmhpd *pd;
847 	unsigned int i;
848 	int ret;
849 
850 	mutex_lock(&rpmhpd_lock);
851 	for (i = 0; i < desc->num_pds; i++) {
852 		pd = rpmhpds[i];
853 		if (!pd)
854 			continue;
855 
856 		pd->state_synced = true;
857 		if (pd->enabled)
858 			corner = max(pd->corner, pd->enable_corner);
859 		else
860 			corner = 0;
861 
862 		ret = rpmhpd_aggregate_corner(pd, corner);
863 		if (ret)
864 			dev_err(dev, "failed to sync %s\n", pd->res_name);
865 	}
866 	mutex_unlock(&rpmhpd_lock);
867 }
868 
869 static struct platform_driver rpmhpd_driver = {
870 	.driver = {
871 		.name = "qcom-rpmhpd",
872 		.of_match_table = rpmhpd_match_table,
873 		.suppress_bind_attrs = true,
874 		.sync_state = rpmhpd_sync_state,
875 	},
876 	.probe = rpmhpd_probe,
877 };
878 
879 static int __init rpmhpd_init(void)
880 {
881 	return platform_driver_register(&rpmhpd_driver);
882 }
883 core_initcall(rpmhpd_init);
884 
885 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
886 MODULE_LICENSE("GPL v2");
887