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