1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 #include <linux/of_address.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm_domain.h>
10 #include <linux/bitfield.h>
11 #include <linux/regmap.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/of_device.h>
14 #include <linux/reset-controller.h>
15 #include <linux/reset.h>
16 #include <linux/clk.h>
17 #include <dt-bindings/power/meson-g12a-power.h>
18 #include <dt-bindings/power/meson-sm1-power.h>
22 #define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
23 #define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
27 #define HHI_MEM_PD_REG0 (0x40 << 2)
28 #define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
29 #define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
30 #define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
31 #define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
32 #define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
33 #define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
34 #define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
35 #define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
38 struct meson_ee_pwrc_domain
;
40 struct meson_ee_pwrc_mem_domain
{
45 struct meson_ee_pwrc_top_domain
{
46 unsigned int sleep_reg
;
47 unsigned int sleep_mask
;
49 unsigned int iso_mask
;
52 struct meson_ee_pwrc_domain_desc
{
54 unsigned int reset_names_count
;
55 unsigned int clk_names_count
;
56 struct meson_ee_pwrc_top_domain
*top_pd
;
57 unsigned int mem_pd_count
;
58 struct meson_ee_pwrc_mem_domain
*mem_pd
;
59 bool (*get_power
)(struct meson_ee_pwrc_domain
*pwrc_domain
);
62 struct meson_ee_pwrc_domain_data
{
64 struct meson_ee_pwrc_domain_desc
*domains
;
67 /* TOP Power Domains */
69 static struct meson_ee_pwrc_top_domain g12a_pwrc_vpu
= {
70 .sleep_reg
= AO_RTI_GEN_PWR_SLEEP0
,
72 .iso_reg
= AO_RTI_GEN_PWR_SLEEP0
,
76 #define SM1_EE_PD(__bit) \
78 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
79 .sleep_mask = BIT(__bit), \
80 .iso_reg = AO_RTI_GEN_PWR_ISO0, \
81 .iso_mask = BIT(__bit), \
84 static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu
= SM1_EE_PD(8);
85 static struct meson_ee_pwrc_top_domain sm1_pwrc_nna
= SM1_EE_PD(16);
86 static struct meson_ee_pwrc_top_domain sm1_pwrc_usb
= SM1_EE_PD(17);
87 static struct meson_ee_pwrc_top_domain sm1_pwrc_pci
= SM1_EE_PD(18);
88 static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d
= SM1_EE_PD(19);
90 /* Memory PD Domains */
92 #define VPU_MEMPD(__reg) \
93 { __reg, GENMASK(1, 0) }, \
94 { __reg, GENMASK(3, 2) }, \
95 { __reg, GENMASK(5, 4) }, \
96 { __reg, GENMASK(7, 6) }, \
97 { __reg, GENMASK(9, 8) }, \
98 { __reg, GENMASK(11, 10) }, \
99 { __reg, GENMASK(13, 12) }, \
100 { __reg, GENMASK(15, 14) }, \
101 { __reg, GENMASK(17, 16) }, \
102 { __reg, GENMASK(19, 18) }, \
103 { __reg, GENMASK(21, 20) }, \
104 { __reg, GENMASK(23, 22) }, \
105 { __reg, GENMASK(25, 24) }, \
106 { __reg, GENMASK(27, 26) }, \
107 { __reg, GENMASK(29, 28) }, \
108 { __reg, GENMASK(31, 30) }
110 #define VPU_HHI_MEMPD(__reg) \
113 { __reg, BIT(10) }, \
114 { __reg, BIT(11) }, \
115 { __reg, BIT(12) }, \
116 { __reg, BIT(13) }, \
117 { __reg, BIT(14) }, \
120 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu
[] = {
121 VPU_MEMPD(HHI_VPU_MEM_PD_REG0
),
122 VPU_MEMPD(HHI_VPU_MEM_PD_REG1
),
123 VPU_MEMPD(HHI_VPU_MEM_PD_REG2
),
124 VPU_HHI_MEMPD(HHI_MEM_PD_REG0
),
127 static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth
[] = {
128 { HHI_MEM_PD_REG0
, GENMASK(3, 2) },
131 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu
[] = {
132 VPU_MEMPD(HHI_VPU_MEM_PD_REG0
),
133 VPU_MEMPD(HHI_VPU_MEM_PD_REG1
),
134 VPU_MEMPD(HHI_VPU_MEM_PD_REG2
),
135 VPU_MEMPD(HHI_VPU_MEM_PD_REG3
),
136 { HHI_VPU_MEM_PD_REG4
, GENMASK(1, 0) },
137 { HHI_VPU_MEM_PD_REG4
, GENMASK(3, 2) },
138 { HHI_VPU_MEM_PD_REG4
, GENMASK(5, 4) },
139 { HHI_VPU_MEM_PD_REG4
, GENMASK(7, 6) },
140 VPU_HHI_MEMPD(HHI_MEM_PD_REG0
),
143 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna
[] = {
144 { HHI_NANOQ_MEM_PD_REG0
, 0xff },
145 { HHI_NANOQ_MEM_PD_REG1
, 0xff },
148 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb
[] = {
149 { HHI_MEM_PD_REG0
, GENMASK(31, 30) },
152 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie
[] = {
153 { HHI_MEM_PD_REG0
, GENMASK(29, 26) },
156 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d
[] = {
157 { HHI_MEM_PD_REG0
, GENMASK(25, 18) },
160 static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio
[] = {
161 { HHI_MEM_PD_REG0
, GENMASK(5, 4) },
162 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(1, 0) },
163 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(3, 2) },
164 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(5, 4) },
165 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(7, 6) },
166 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(13, 12) },
167 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(15, 14) },
168 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(17, 16) },
169 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(19, 18) },
170 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(21, 20) },
171 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(23, 22) },
172 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(25, 24) },
173 { HHI_AUDIO_MEM_PD_REG0
, GENMASK(27, 26) },
176 #define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
179 .reset_names_count = __resets, \
180 .clk_names_count = __clks, \
181 .top_pd = __top_pd, \
182 .mem_pd_count = ARRAY_SIZE(__mem), \
184 .get_power = __get_power, \
187 #define TOP_PD(__name, __top_pd, __mem, __get_power) \
190 .top_pd = __top_pd, \
191 .mem_pd_count = ARRAY_SIZE(__mem), \
193 .get_power = __get_power, \
196 #define MEM_PD(__name, __mem) \
197 TOP_PD(__name, NULL, __mem, NULL)
199 static bool pwrc_ee_get_power(struct meson_ee_pwrc_domain
*pwrc_domain
);
201 static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains
[] = {
202 [PWRC_G12A_VPU_ID
] = VPU_PD("VPU", &g12a_pwrc_vpu
, g12a_pwrc_mem_vpu
,
203 pwrc_ee_get_power
, 11, 2),
204 [PWRC_G12A_ETH_ID
] = MEM_PD("ETH", g12a_pwrc_mem_eth
),
207 static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains
[] = {
208 [PWRC_SM1_VPU_ID
] = VPU_PD("VPU", &sm1_pwrc_vpu
, sm1_pwrc_mem_vpu
,
209 pwrc_ee_get_power
, 11, 2),
210 [PWRC_SM1_NNA_ID
] = TOP_PD("NNA", &sm1_pwrc_nna
, sm1_pwrc_mem_nna
,
212 [PWRC_SM1_USB_ID
] = TOP_PD("USB", &sm1_pwrc_usb
, sm1_pwrc_mem_usb
,
214 [PWRC_SM1_PCIE_ID
] = TOP_PD("PCI", &sm1_pwrc_pci
, sm1_pwrc_mem_pcie
,
216 [PWRC_SM1_GE2D_ID
] = TOP_PD("GE2D", &sm1_pwrc_ge2d
, sm1_pwrc_mem_ge2d
,
218 [PWRC_SM1_AUDIO_ID
] = MEM_PD("AUDIO", sm1_pwrc_mem_audio
),
219 [PWRC_SM1_ETH_ID
] = MEM_PD("ETH", g12a_pwrc_mem_eth
),
222 struct meson_ee_pwrc_domain
{
223 struct generic_pm_domain base
;
225 struct meson_ee_pwrc
*pwrc
;
226 struct meson_ee_pwrc_domain_desc desc
;
227 struct clk_bulk_data
*clks
;
229 struct reset_control
*rstc
;
233 struct meson_ee_pwrc
{
234 struct regmap
*regmap_ao
;
235 struct regmap
*regmap_hhi
;
236 struct meson_ee_pwrc_domain
*domains
;
237 struct genpd_onecell_data xlate
;
240 static bool pwrc_ee_get_power(struct meson_ee_pwrc_domain
*pwrc_domain
)
244 regmap_read(pwrc_domain
->pwrc
->regmap_ao
,
245 pwrc_domain
->desc
.top_pd
->sleep_reg
, ®
);
247 return (reg
& pwrc_domain
->desc
.top_pd
->sleep_mask
);
250 static int meson_ee_pwrc_off(struct generic_pm_domain
*domain
)
252 struct meson_ee_pwrc_domain
*pwrc_domain
=
253 container_of(domain
, struct meson_ee_pwrc_domain
, base
);
256 if (pwrc_domain
->desc
.top_pd
)
257 regmap_update_bits(pwrc_domain
->pwrc
->regmap_ao
,
258 pwrc_domain
->desc
.top_pd
->sleep_reg
,
259 pwrc_domain
->desc
.top_pd
->sleep_mask
,
260 pwrc_domain
->desc
.top_pd
->sleep_mask
);
263 for (i
= 0 ; i
< pwrc_domain
->desc
.mem_pd_count
; ++i
)
264 regmap_update_bits(pwrc_domain
->pwrc
->regmap_hhi
,
265 pwrc_domain
->desc
.mem_pd
[i
].reg
,
266 pwrc_domain
->desc
.mem_pd
[i
].mask
,
267 pwrc_domain
->desc
.mem_pd
[i
].mask
);
271 if (pwrc_domain
->desc
.top_pd
)
272 regmap_update_bits(pwrc_domain
->pwrc
->regmap_ao
,
273 pwrc_domain
->desc
.top_pd
->iso_reg
,
274 pwrc_domain
->desc
.top_pd
->iso_mask
,
275 pwrc_domain
->desc
.top_pd
->iso_mask
);
277 if (pwrc_domain
->num_clks
) {
279 clk_bulk_disable_unprepare(pwrc_domain
->num_clks
,
286 static int meson_ee_pwrc_on(struct generic_pm_domain
*domain
)
288 struct meson_ee_pwrc_domain
*pwrc_domain
=
289 container_of(domain
, struct meson_ee_pwrc_domain
, base
);
292 if (pwrc_domain
->desc
.top_pd
)
293 regmap_update_bits(pwrc_domain
->pwrc
->regmap_ao
,
294 pwrc_domain
->desc
.top_pd
->sleep_reg
,
295 pwrc_domain
->desc
.top_pd
->sleep_mask
, 0);
298 for (i
= 0 ; i
< pwrc_domain
->desc
.mem_pd_count
; ++i
)
299 regmap_update_bits(pwrc_domain
->pwrc
->regmap_hhi
,
300 pwrc_domain
->desc
.mem_pd
[i
].reg
,
301 pwrc_domain
->desc
.mem_pd
[i
].mask
, 0);
305 ret
= reset_control_assert(pwrc_domain
->rstc
);
309 if (pwrc_domain
->desc
.top_pd
)
310 regmap_update_bits(pwrc_domain
->pwrc
->regmap_ao
,
311 pwrc_domain
->desc
.top_pd
->iso_reg
,
312 pwrc_domain
->desc
.top_pd
->iso_mask
, 0);
314 ret
= reset_control_deassert(pwrc_domain
->rstc
);
318 return clk_bulk_prepare_enable(pwrc_domain
->num_clks
,
322 static int meson_ee_pwrc_init_domain(struct platform_device
*pdev
,
323 struct meson_ee_pwrc
*pwrc
,
324 struct meson_ee_pwrc_domain
*dom
)
329 dom
->num_rstc
= dom
->desc
.reset_names_count
;
330 dom
->num_clks
= dom
->desc
.clk_names_count
;
333 int count
= reset_control_get_count(&pdev
->dev
);
335 if (count
!= dom
->num_rstc
)
336 dev_warn(&pdev
->dev
, "Invalid resets count %d for domain %s\n",
337 count
, dom
->desc
.name
);
339 dom
->rstc
= devm_reset_control_array_get(&pdev
->dev
, false,
341 if (IS_ERR(dom
->rstc
))
342 return PTR_ERR(dom
->rstc
);
346 int ret
= devm_clk_bulk_get_all(&pdev
->dev
, &dom
->clks
);
350 if (dom
->num_clks
!= ret
) {
351 dev_warn(&pdev
->dev
, "Invalid clocks count %d for domain %s\n",
352 ret
, dom
->desc
.name
);
357 dom
->base
.name
= dom
->desc
.name
;
358 dom
->base
.power_on
= meson_ee_pwrc_on
;
359 dom
->base
.power_off
= meson_ee_pwrc_off
;
362 * TOFIX: This is a special case for the VPU power domain, which can
363 * be enabled previously by the bootloader. In this case the VPU
364 * pipeline may be functional but no driver maybe never attach
365 * to this power domain, and if the domain is disabled it could
366 * cause system errors. This is why the pm_domain_always_on_gov
368 * For the same reason, the clocks should be enabled in case
369 * we need to power the domain off, otherwise the internal clocks
370 * prepare/enable counters won't be in sync.
372 if (dom
->num_clks
&& dom
->desc
.get_power
&& !dom
->desc
.get_power(dom
)) {
373 ret
= clk_bulk_prepare_enable(dom
->num_clks
, dom
->clks
);
377 ret
= pm_genpd_init(&dom
->base
, &pm_domain_always_on_gov
,
382 ret
= pm_genpd_init(&dom
->base
, NULL
,
383 (dom
->desc
.get_power
?
384 dom
->desc
.get_power(dom
) : true));
392 static int meson_ee_pwrc_probe(struct platform_device
*pdev
)
394 const struct meson_ee_pwrc_domain_data
*match
;
395 struct regmap
*regmap_ao
, *regmap_hhi
;
396 struct meson_ee_pwrc
*pwrc
;
399 match
= of_device_get_match_data(&pdev
->dev
);
401 dev_err(&pdev
->dev
, "failed to get match data\n");
405 pwrc
= devm_kzalloc(&pdev
->dev
, sizeof(*pwrc
), GFP_KERNEL
);
409 pwrc
->xlate
.domains
= devm_kcalloc(&pdev
->dev
, match
->count
,
410 sizeof(*pwrc
->xlate
.domains
),
412 if (!pwrc
->xlate
.domains
)
415 pwrc
->domains
= devm_kcalloc(&pdev
->dev
, match
->count
,
416 sizeof(*pwrc
->domains
), GFP_KERNEL
);
420 pwrc
->xlate
.num_domains
= match
->count
;
422 regmap_hhi
= syscon_node_to_regmap(of_get_parent(pdev
->dev
.of_node
));
423 if (IS_ERR(regmap_hhi
)) {
424 dev_err(&pdev
->dev
, "failed to get HHI regmap\n");
425 return PTR_ERR(regmap_hhi
);
428 regmap_ao
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
429 "amlogic,ao-sysctrl");
430 if (IS_ERR(regmap_ao
)) {
431 dev_err(&pdev
->dev
, "failed to get AO regmap\n");
432 return PTR_ERR(regmap_ao
);
435 pwrc
->regmap_ao
= regmap_ao
;
436 pwrc
->regmap_hhi
= regmap_hhi
;
438 platform_set_drvdata(pdev
, pwrc
);
440 for (i
= 0 ; i
< match
->count
; ++i
) {
441 struct meson_ee_pwrc_domain
*dom
= &pwrc
->domains
[i
];
443 memcpy(&dom
->desc
, &match
->domains
[i
], sizeof(dom
->desc
));
445 ret
= meson_ee_pwrc_init_domain(pdev
, pwrc
, dom
);
449 pwrc
->xlate
.domains
[i
] = &dom
->base
;
452 return of_genpd_add_provider_onecell(pdev
->dev
.of_node
, &pwrc
->xlate
);
455 static void meson_ee_pwrc_shutdown(struct platform_device
*pdev
)
457 struct meson_ee_pwrc
*pwrc
= platform_get_drvdata(pdev
);
460 for (i
= 0 ; i
< pwrc
->xlate
.num_domains
; ++i
) {
461 struct meson_ee_pwrc_domain
*dom
= &pwrc
->domains
[i
];
463 if (dom
->desc
.get_power
&& !dom
->desc
.get_power(dom
))
464 meson_ee_pwrc_off(&dom
->base
);
468 static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data
= {
469 .count
= ARRAY_SIZE(g12a_pwrc_domains
),
470 .domains
= g12a_pwrc_domains
,
473 static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data
= {
474 .count
= ARRAY_SIZE(sm1_pwrc_domains
),
475 .domains
= sm1_pwrc_domains
,
478 static const struct of_device_id meson_ee_pwrc_match_table
[] = {
480 .compatible
= "amlogic,meson-g12a-pwrc",
481 .data
= &meson_ee_g12a_pwrc_data
,
484 .compatible
= "amlogic,meson-sm1-pwrc",
485 .data
= &meson_ee_sm1_pwrc_data
,
490 static struct platform_driver meson_ee_pwrc_driver
= {
491 .probe
= meson_ee_pwrc_probe
,
492 .shutdown
= meson_ee_pwrc_shutdown
,
494 .name
= "meson_ee_pwrc",
495 .of_match_table
= meson_ee_pwrc_match_table
,
498 builtin_platform_driver(meson_ee_pwrc_driver
);