Skip to content

Commit dc9a897

Browse files
Michal Wilczynskistorulf
authored andcommitted
pmdomain: thead: Add power-domain driver for TH1520
The T-Head TH1520 SoC contains multiple power islands that can be programmatically turned on and off using the AON (Always-On) protocol and a hardware mailbox [1]. The relevant mailbox driver has already been merged into the mainline kernel in commit 5d4d263 ("mailbox: Introduce support for T-head TH1520 Mailbox driver"); Introduce a power-domain driver for the TH1520 SoC, which is using AON firmware protocol to communicate with E902 core through the hardware mailbox. This way it can send power on/off commands to the E902 core. The interaction with AUDIO power island e.g trying to turn it OFF proved to crash the firmware running on the E902 core. Introduce the workaround to disable interacting with the power island. [1] Link: https://openbeagle.org/beaglev-ahead/beaglev-ahead/-/blob/main/docs/TH1520%20System%20User%20Manual.pdf Signed-off-by: Michal Wilczynski <m.wilczynski@samsung.com> Acked-by: Drew Fustini <drew@pdp7.com> Link: https://lore.kernel.org/r/20250311171900.1549916-5-m.wilczynski@samsung.com Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
1 parent 0c54b63 commit dc9a897

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20430,6 +20430,7 @@ F: drivers/firmware/thead,th1520-aon.c
2043020430
F: drivers/mailbox/mailbox-th1520.c
2043120431
F: drivers/net/ethernet/stmicro/stmmac/dwmac-thead.c
2043220432
F: drivers/pinctrl/pinctrl-th1520.c
20433+
F: drivers/pmdomain/thead/
2043320434
F: include/dt-bindings/clock/thead,th1520-clk-ap.h
2043420435
F: include/dt-bindings/power/thead,th1520-power.h
2043520436
F: include/linux/firmware/thead/thead,th1520-aon.h

drivers/pmdomain/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ source "drivers/pmdomain/st/Kconfig"
1616
source "drivers/pmdomain/starfive/Kconfig"
1717
source "drivers/pmdomain/sunxi/Kconfig"
1818
source "drivers/pmdomain/tegra/Kconfig"
19+
source "drivers/pmdomain/thead/Kconfig"
1920
source "drivers/pmdomain/ti/Kconfig"
2021
source "drivers/pmdomain/xilinx/Kconfig"
2122

drivers/pmdomain/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ obj-y += st/
1414
obj-y += starfive/
1515
obj-y += sunxi/
1616
obj-y += tegra/
17+
obj-y += thead/
1718
obj-y += ti/
1819
obj-y += xilinx/
1920
obj-y += core.o governor.o

drivers/pmdomain/thead/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
config TH1520_PM_DOMAINS
4+
tristate "Support TH1520 Power Domains"
5+
depends on TH1520_AON_PROTOCOL || !TH1520_AON_PROTOCOL
6+
select REGMAP_MMIO
7+
help
8+
This driver enables power domain management for the T-HEAD
9+
TH-1520 SoC. On this SoC there are number of power domains,
10+
which can be managed independently. For example GPU, NPU,
11+
and DPU reside in their own power domains which can be
12+
turned on/off.

drivers/pmdomain/thead/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
obj-$(CONFIG_TH1520_PM_DOMAINS) += th1520-pm-domains.o
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2021 Alibaba Group Holding Limited.
4+
* Copyright (c) 2024 Samsung Electronics Co., Ltd.
5+
* Author: Michal Wilczynski <m.wilczynski@samsung.com>
6+
*/
7+
8+
#include <linux/firmware/thead/thead,th1520-aon.h>
9+
#include <linux/slab.h>
10+
#include <linux/platform_device.h>
11+
#include <linux/pm_domain.h>
12+
13+
#include <dt-bindings/power/thead,th1520-power.h>
14+
15+
struct th1520_power_domain {
16+
struct th1520_aon_chan *aon_chan;
17+
struct generic_pm_domain genpd;
18+
u32 rsrc;
19+
};
20+
21+
struct th1520_power_info {
22+
const char *name;
23+
u32 rsrc;
24+
bool disabled;
25+
};
26+
27+
/*
28+
* The AUDIO power domain is marked as disabled to prevent the driver from
29+
* managing its power state. Direct AON firmware calls to control this power
30+
* island trigger a firmware bug causing system instability. Until this
31+
* firmware issue is resolved, the AUDIO power domain must remain disabled
32+
* to avoid crashes.
33+
*/
34+
static const struct th1520_power_info th1520_pd_ranges[] = {
35+
[TH1520_AUDIO_PD] = {"audio", TH1520_AON_AUDIO_PD, true },
36+
[TH1520_VDEC_PD] = { "vdec", TH1520_AON_VDEC_PD, false },
37+
[TH1520_NPU_PD] = { "npu", TH1520_AON_NPU_PD, false },
38+
[TH1520_VENC_PD] = { "venc", TH1520_AON_VENC_PD, false },
39+
[TH1520_GPU_PD] = { "gpu", TH1520_AON_GPU_PD, false },
40+
[TH1520_DSP0_PD] = { "dsp0", TH1520_AON_DSP0_PD, false },
41+
[TH1520_DSP1_PD] = { "dsp1", TH1520_AON_DSP1_PD, false }
42+
};
43+
44+
static inline struct th1520_power_domain *
45+
to_th1520_power_domain(struct generic_pm_domain *genpd)
46+
{
47+
return container_of(genpd, struct th1520_power_domain, genpd);
48+
}
49+
50+
static int th1520_pd_power_on(struct generic_pm_domain *domain)
51+
{
52+
struct th1520_power_domain *pd = to_th1520_power_domain(domain);
53+
54+
return th1520_aon_power_update(pd->aon_chan, pd->rsrc, true);
55+
}
56+
57+
static int th1520_pd_power_off(struct generic_pm_domain *domain)
58+
{
59+
struct th1520_power_domain *pd = to_th1520_power_domain(domain);
60+
61+
return th1520_aon_power_update(pd->aon_chan, pd->rsrc, false);
62+
}
63+
64+
static struct generic_pm_domain *th1520_pd_xlate(const struct of_phandle_args *spec,
65+
void *data)
66+
{
67+
struct generic_pm_domain *domain = ERR_PTR(-ENOENT);
68+
struct genpd_onecell_data *pd_data = data;
69+
unsigned int i;
70+
71+
for (i = 0; i < ARRAY_SIZE(th1520_pd_ranges); i++) {
72+
struct th1520_power_domain *pd;
73+
74+
if (th1520_pd_ranges[i].disabled)
75+
continue;
76+
77+
pd = to_th1520_power_domain(pd_data->domains[i]);
78+
if (pd->rsrc == spec->args[0]) {
79+
domain = &pd->genpd;
80+
break;
81+
}
82+
}
83+
84+
return domain;
85+
}
86+
87+
static struct th1520_power_domain *
88+
th1520_add_pm_domain(struct device *dev, const struct th1520_power_info *pi)
89+
{
90+
struct th1520_power_domain *pd;
91+
int ret;
92+
93+
pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
94+
if (!pd)
95+
return ERR_PTR(-ENOMEM);
96+
97+
pd->rsrc = pi->rsrc;
98+
pd->genpd.power_on = th1520_pd_power_on;
99+
pd->genpd.power_off = th1520_pd_power_off;
100+
pd->genpd.name = pi->name;
101+
102+
ret = pm_genpd_init(&pd->genpd, NULL, true);
103+
if (ret)
104+
return ERR_PTR(ret);
105+
106+
return pd;
107+
}
108+
109+
static void th1520_pd_init_all_off(struct generic_pm_domain **domains,
110+
struct device *dev)
111+
{
112+
int ret;
113+
int i;
114+
115+
for (i = 0; i < ARRAY_SIZE(th1520_pd_ranges); i++) {
116+
struct th1520_power_domain *pd;
117+
118+
if (th1520_pd_ranges[i].disabled)
119+
continue;
120+
121+
pd = to_th1520_power_domain(domains[i]);
122+
123+
ret = th1520_aon_power_update(pd->aon_chan, pd->rsrc, false);
124+
if (ret)
125+
dev_err(dev,
126+
"Failed to initially power down power domain %s\n",
127+
pd->genpd.name);
128+
}
129+
}
130+
131+
static int th1520_pd_probe(struct platform_device *pdev)
132+
{
133+
struct generic_pm_domain **domains;
134+
struct genpd_onecell_data *pd_data;
135+
struct th1520_aon_chan *aon_chan;
136+
struct device *dev = &pdev->dev;
137+
int i, ret;
138+
139+
aon_chan = th1520_aon_init(dev);
140+
if (IS_ERR(aon_chan))
141+
return dev_err_probe(dev, PTR_ERR(aon_chan),
142+
"Failed to get AON channel\n");
143+
144+
domains = devm_kcalloc(dev, ARRAY_SIZE(th1520_pd_ranges),
145+
sizeof(*domains), GFP_KERNEL);
146+
if (!domains) {
147+
ret = -ENOMEM;
148+
goto err_clean_aon;
149+
}
150+
151+
pd_data = devm_kzalloc(dev, sizeof(*pd_data), GFP_KERNEL);
152+
if (!pd_data) {
153+
ret = -ENOMEM;
154+
goto err_clean_aon;
155+
}
156+
157+
for (i = 0; i < ARRAY_SIZE(th1520_pd_ranges); i++) {
158+
struct th1520_power_domain *pd;
159+
160+
if (th1520_pd_ranges[i].disabled)
161+
continue;
162+
163+
pd = th1520_add_pm_domain(dev, &th1520_pd_ranges[i]);
164+
if (IS_ERR(pd)) {
165+
ret = PTR_ERR(pd);
166+
goto err_clean_genpd;
167+
}
168+
169+
pd->aon_chan = aon_chan;
170+
domains[i] = &pd->genpd;
171+
dev_dbg(dev, "added power domain %s\n", pd->genpd.name);
172+
}
173+
174+
pd_data->domains = domains;
175+
pd_data->num_domains = ARRAY_SIZE(th1520_pd_ranges);
176+
pd_data->xlate = th1520_pd_xlate;
177+
178+
/*
179+
* Initialize all power domains to off to ensure they start in a
180+
* low-power state. This allows device drivers to manage power
181+
* domains by turning them on or off as needed.
182+
*/
183+
th1520_pd_init_all_off(domains, dev);
184+
185+
ret = of_genpd_add_provider_onecell(dev->of_node, pd_data);
186+
if (ret)
187+
goto err_clean_genpd;
188+
189+
return 0;
190+
191+
err_clean_genpd:
192+
for (i--; i >= 0; i--)
193+
pm_genpd_remove(domains[i]);
194+
err_clean_aon:
195+
th1520_aon_deinit(aon_chan);
196+
197+
return ret;
198+
}
199+
200+
static const struct of_device_id th1520_pd_match[] = {
201+
{ .compatible = "thead,th1520-aon" },
202+
{ /* Sentinel */ }
203+
};
204+
MODULE_DEVICE_TABLE(of, th1520_pd_match);
205+
206+
static struct platform_driver th1520_pd_driver = {
207+
.driver = {
208+
.name = "th1520-pd",
209+
.of_match_table = th1520_pd_match,
210+
.suppress_bind_attrs = true,
211+
},
212+
.probe = th1520_pd_probe,
213+
};
214+
module_platform_driver(th1520_pd_driver);
215+
216+
MODULE_AUTHOR("Michal Wilczynski <m.wilczynski@samsung.com>");
217+
MODULE_DESCRIPTION("T-HEAD TH1520 SoC power domain controller");
218+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)