1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (c) 2019 Amlogic, Inc.
4 * Author: Jianxin Pan <jianxin.pan@amlogic.com>
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_domain.h>
13 #include <dt-bindings/power/meson-a1-power.h>
14 #include <dt-bindings/power/meson-s4-power.h>
15 #include <linux/arm-smccc.h>
16 #include <linux/firmware/meson/meson_sm.h>
17 #include <linux/module.h>
22 struct meson_secure_pwrc_domain {
23 struct generic_pm_domain base;
25 struct meson_secure_pwrc *pwrc;
28 struct meson_secure_pwrc {
29 struct meson_secure_pwrc_domain *domains;
30 struct genpd_onecell_data xlate;
31 struct meson_sm_firmware *fw;
34 struct meson_secure_pwrc_domain_desc {
38 bool (*is_off)(struct meson_secure_pwrc_domain *pwrc_domain);
41 struct meson_secure_pwrc_domain_data {
43 struct meson_secure_pwrc_domain_desc *domains;
46 static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain *pwrc_domain)
50 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_GET, &is_off,
51 pwrc_domain->index, 0, 0, 0, 0) < 0)
52 pr_err("failed to get power domain status\n");
57 static int meson_secure_pwrc_off(struct generic_pm_domain *domain)
60 struct meson_secure_pwrc_domain *pwrc_domain =
61 container_of(domain, struct meson_secure_pwrc_domain, base);
63 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
64 pwrc_domain->index, PWRC_OFF, 0, 0, 0) < 0) {
65 pr_err("failed to set power domain off\n");
72 static int meson_secure_pwrc_on(struct generic_pm_domain *domain)
75 struct meson_secure_pwrc_domain *pwrc_domain =
76 container_of(domain, struct meson_secure_pwrc_domain, base);
78 if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
79 pwrc_domain->index, PWRC_ON, 0, 0, 0) < 0) {
80 pr_err("failed to set power domain on\n");
87 #define SEC_PD(__name, __flag) \
88 [PWRC_##__name##_ID] = \
91 .index = PWRC_##__name##_ID, \
92 .is_off = pwrc_secure_is_off, \
96 static struct meson_secure_pwrc_domain_desc a1_pwrc_domains[] = {
99 /* UART should keep working in ATF after suspend and before resume */
100 SEC_PD(UART, GENPD_FLAG_ALWAYS_ON),
101 /* DMC is for DDR PHY ana/dig and DMC, and should be always on */
102 SEC_PD(DMC, GENPD_FLAG_ALWAYS_ON),
111 /* SRAMB is used as ATF runtime memory, and should be always on */
112 SEC_PD(RAMB, GENPD_FLAG_ALWAYS_ON),
117 /* NIC is for the Arm NIC-400 interconnect, and should be always on */
118 SEC_PD(NIC, GENPD_FLAG_ALWAYS_ON),
123 static struct meson_secure_pwrc_domain_desc s4_pwrc_domains[] = {
124 SEC_PD(S4_DOS_HEVC, 0),
125 SEC_PD(S4_DOS_VDEC, 0),
126 SEC_PD(S4_VPU_HDMI, 0),
127 SEC_PD(S4_USB_COMB, 0),
129 /* ETH is for ethernet online wakeup, and should be always on */
130 SEC_PD(S4_ETH, GENPD_FLAG_ALWAYS_ON),
135 static int meson_secure_pwrc_probe(struct platform_device *pdev)
138 struct device_node *sm_np;
139 struct meson_secure_pwrc *pwrc;
140 const struct meson_secure_pwrc_domain_data *match;
142 match = of_device_get_match_data(&pdev->dev);
144 dev_err(&pdev->dev, "failed to get match data\n");
148 sm_np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gxbb-sm");
150 dev_err(&pdev->dev, "no secure-monitor node\n");
154 pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
160 pwrc->fw = meson_sm_get(sm_np);
163 return -EPROBE_DEFER;
165 pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
166 sizeof(*pwrc->xlate.domains),
168 if (!pwrc->xlate.domains)
171 pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
172 sizeof(*pwrc->domains), GFP_KERNEL);
176 pwrc->xlate.num_domains = match->count;
177 platform_set_drvdata(pdev, pwrc);
179 for (i = 0 ; i < match->count ; ++i) {
180 struct meson_secure_pwrc_domain *dom = &pwrc->domains[i];
182 if (!match->domains[i].index)
186 dom->index = match->domains[i].index;
187 dom->base.name = match->domains[i].name;
188 dom->base.flags = match->domains[i].flags;
189 dom->base.power_on = meson_secure_pwrc_on;
190 dom->base.power_off = meson_secure_pwrc_off;
192 pm_genpd_init(&dom->base, NULL, match->domains[i].is_off(dom));
194 pwrc->xlate.domains[i] = &dom->base;
197 return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
200 static struct meson_secure_pwrc_domain_data meson_secure_a1_pwrc_data = {
201 .domains = a1_pwrc_domains,
202 .count = ARRAY_SIZE(a1_pwrc_domains),
205 static struct meson_secure_pwrc_domain_data meson_secure_s4_pwrc_data = {
206 .domains = s4_pwrc_domains,
207 .count = ARRAY_SIZE(s4_pwrc_domains),
210 static const struct of_device_id meson_secure_pwrc_match_table[] = {
212 .compatible = "amlogic,meson-a1-pwrc",
213 .data = &meson_secure_a1_pwrc_data,
216 .compatible = "amlogic,meson-s4-pwrc",
217 .data = &meson_secure_s4_pwrc_data,
221 MODULE_DEVICE_TABLE(of, meson_secure_pwrc_match_table);
223 static struct platform_driver meson_secure_pwrc_driver = {
224 .probe = meson_secure_pwrc_probe,
226 .name = "meson_secure_pwrc",
227 .of_match_table = meson_secure_pwrc_match_table,
230 module_platform_driver(meson_secure_pwrc_driver);
231 MODULE_LICENSE("Dual MIT/GPL");