xref: /openbmc/linux/drivers/pmdomain/qcom/rpmhpd.c (revision adb19164)
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 	unsigned int peer_enabled_corner;
620 
621 	if (pd->state_synced) {
622 		to_active_sleep(pd, corner, &this_active_corner, &this_sleep_corner);
623 	} else {
624 		/* Clamp to highest corner if sync_state hasn't happened */
625 		this_active_corner = pd->level_count - 1;
626 		this_sleep_corner = pd->level_count - 1;
627 	}
628 
629 	if (peer && peer->enabled) {
630 		peer_enabled_corner = max(peer->corner, peer->enable_corner);
631 		to_active_sleep(peer, peer_enabled_corner, &peer_active_corner,
632 				&peer_sleep_corner);
633 	}
634 
635 	active_corner = max(this_active_corner, peer_active_corner);
636 
637 	ret = rpmhpd_send_corner(pd, RPMH_ACTIVE_ONLY_STATE, active_corner,
638 				 active_corner > pd->active_corner);
639 	if (ret)
640 		return ret;
641 
642 	pd->active_corner = active_corner;
643 
644 	if (peer) {
645 		peer->active_corner = active_corner;
646 
647 		ret = rpmhpd_send_corner(pd, RPMH_WAKE_ONLY_STATE,
648 					 active_corner, false);
649 		if (ret)
650 			return ret;
651 
652 		sleep_corner = max(this_sleep_corner, peer_sleep_corner);
653 
654 		return rpmhpd_send_corner(pd, RPMH_SLEEP_STATE, sleep_corner,
655 					  false);
656 	}
657 
658 	return ret;
659 }
660 
661 static int rpmhpd_power_on(struct generic_pm_domain *domain)
662 {
663 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
664 	unsigned int corner;
665 	int ret;
666 
667 	mutex_lock(&rpmhpd_lock);
668 
669 	corner = max(pd->corner, pd->enable_corner);
670 	ret = rpmhpd_aggregate_corner(pd, corner);
671 	if (!ret)
672 		pd->enabled = true;
673 
674 	mutex_unlock(&rpmhpd_lock);
675 
676 	return ret;
677 }
678 
679 static int rpmhpd_power_off(struct generic_pm_domain *domain)
680 {
681 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
682 	int ret;
683 
684 	mutex_lock(&rpmhpd_lock);
685 
686 	ret = rpmhpd_aggregate_corner(pd, 0);
687 	if (!ret)
688 		pd->enabled = false;
689 
690 	mutex_unlock(&rpmhpd_lock);
691 
692 	return ret;
693 }
694 
695 static int rpmhpd_set_performance_state(struct generic_pm_domain *domain,
696 					unsigned int level)
697 {
698 	struct rpmhpd *pd = domain_to_rpmhpd(domain);
699 	int ret = 0, i;
700 
701 	mutex_lock(&rpmhpd_lock);
702 
703 	for (i = 0; i < pd->level_count; i++)
704 		if (level <= pd->level[i])
705 			break;
706 
707 	/*
708 	 * If the level requested is more than that supported by the
709 	 * max corner, just set it to max anyway.
710 	 */
711 	if (i == pd->level_count)
712 		i--;
713 
714 	if (pd->enabled) {
715 		/* Ensure that the domain isn't turn off */
716 		if (i < pd->enable_corner)
717 			i = pd->enable_corner;
718 
719 		ret = rpmhpd_aggregate_corner(pd, i);
720 		if (ret)
721 			goto out;
722 	}
723 
724 	pd->corner = i;
725 out:
726 	mutex_unlock(&rpmhpd_lock);
727 
728 	return ret;
729 }
730 
731 static unsigned int rpmhpd_get_performance_state(struct generic_pm_domain *genpd,
732 						 struct dev_pm_opp *opp)
733 {
734 	return dev_pm_opp_get_level(opp);
735 }
736 
737 static int rpmhpd_update_level_mapping(struct rpmhpd *rpmhpd)
738 {
739 	int i;
740 	const u16 *buf;
741 
742 	buf = cmd_db_read_aux_data(rpmhpd->res_name, &rpmhpd->level_count);
743 	if (IS_ERR(buf))
744 		return PTR_ERR(buf);
745 
746 	/* 2 bytes used for each command DB aux data entry */
747 	rpmhpd->level_count >>= 1;
748 
749 	if (rpmhpd->level_count > RPMH_ARC_MAX_LEVELS)
750 		return -EINVAL;
751 
752 	for (i = 0; i < rpmhpd->level_count; i++) {
753 		rpmhpd->level[i] = buf[i];
754 
755 		/* Remember the first corner with non-zero level */
756 		if (!rpmhpd->level[rpmhpd->enable_corner] && rpmhpd->level[i])
757 			rpmhpd->enable_corner = i;
758 
759 		/*
760 		 * The AUX data may be zero padded.  These 0 valued entries at
761 		 * the end of the map must be ignored.
762 		 */
763 		if (i > 0 && rpmhpd->level[i] == 0) {
764 			rpmhpd->level_count = i;
765 			break;
766 		}
767 		pr_debug("%s: ARC hlvl=%2d --> vlvl=%4u\n", rpmhpd->res_name, i,
768 			 rpmhpd->level[i]);
769 	}
770 
771 	return 0;
772 }
773 
774 static int rpmhpd_probe(struct platform_device *pdev)
775 {
776 	int i, ret;
777 	size_t num_pds;
778 	struct device *dev = &pdev->dev;
779 	struct genpd_onecell_data *data;
780 	struct rpmhpd **rpmhpds;
781 	const struct rpmhpd_desc *desc;
782 
783 	desc = of_device_get_match_data(dev);
784 	if (!desc)
785 		return -EINVAL;
786 
787 	rpmhpds = desc->rpmhpds;
788 	num_pds = desc->num_pds;
789 
790 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
791 	if (!data)
792 		return -ENOMEM;
793 
794 	data->domains = devm_kcalloc(dev, num_pds, sizeof(*data->domains),
795 				     GFP_KERNEL);
796 	if (!data->domains)
797 		return -ENOMEM;
798 
799 	data->num_domains = num_pds;
800 
801 	for (i = 0; i < num_pds; i++) {
802 		if (!rpmhpds[i])
803 			continue;
804 
805 		rpmhpds[i]->dev = dev;
806 		rpmhpds[i]->addr = cmd_db_read_addr(rpmhpds[i]->res_name);
807 		if (!rpmhpds[i]->addr) {
808 			dev_err(dev, "Could not find RPMh address for resource %s\n",
809 				rpmhpds[i]->res_name);
810 			return -ENODEV;
811 		}
812 
813 		ret = cmd_db_read_slave_id(rpmhpds[i]->res_name);
814 		if (ret != CMD_DB_HW_ARC) {
815 			dev_err(dev, "RPMh slave ID mismatch\n");
816 			return -EINVAL;
817 		}
818 
819 		ret = rpmhpd_update_level_mapping(rpmhpds[i]);
820 		if (ret)
821 			return ret;
822 
823 		rpmhpds[i]->pd.power_off = rpmhpd_power_off;
824 		rpmhpds[i]->pd.power_on = rpmhpd_power_on;
825 		rpmhpds[i]->pd.set_performance_state = rpmhpd_set_performance_state;
826 		rpmhpds[i]->pd.opp_to_performance_state = rpmhpd_get_performance_state;
827 		pm_genpd_init(&rpmhpds[i]->pd, NULL, true);
828 
829 		data->domains[i] = &rpmhpds[i]->pd;
830 	}
831 
832 	/* Add subdomains */
833 	for (i = 0; i < num_pds; i++) {
834 		if (!rpmhpds[i])
835 			continue;
836 		if (rpmhpds[i]->parent)
837 			pm_genpd_add_subdomain(rpmhpds[i]->parent,
838 					       &rpmhpds[i]->pd);
839 	}
840 
841 	return of_genpd_add_provider_onecell(pdev->dev.of_node, data);
842 }
843 
844 static void rpmhpd_sync_state(struct device *dev)
845 {
846 	const struct rpmhpd_desc *desc = of_device_get_match_data(dev);
847 	struct rpmhpd **rpmhpds = desc->rpmhpds;
848 	unsigned int corner;
849 	struct rpmhpd *pd;
850 	unsigned int i;
851 	int ret;
852 
853 	mutex_lock(&rpmhpd_lock);
854 	for (i = 0; i < desc->num_pds; i++) {
855 		pd = rpmhpds[i];
856 		if (!pd)
857 			continue;
858 
859 		pd->state_synced = true;
860 		if (pd->enabled)
861 			corner = max(pd->corner, pd->enable_corner);
862 		else
863 			corner = 0;
864 
865 		ret = rpmhpd_aggregate_corner(pd, corner);
866 		if (ret)
867 			dev_err(dev, "failed to sync %s\n", pd->res_name);
868 	}
869 	mutex_unlock(&rpmhpd_lock);
870 }
871 
872 static struct platform_driver rpmhpd_driver = {
873 	.driver = {
874 		.name = "qcom-rpmhpd",
875 		.of_match_table = rpmhpd_match_table,
876 		.suppress_bind_attrs = true,
877 		.sync_state = rpmhpd_sync_state,
878 	},
879 	.probe = rpmhpd_probe,
880 };
881 
882 static int __init rpmhpd_init(void)
883 {
884 	return platform_driver_register(&rpmhpd_driver);
885 }
886 core_initcall(rpmhpd_init);
887 
888 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Power Domain Driver");
889 MODULE_LICENSE("GPL v2");
890