pmdomain: thead: Add power-domain driver for TH1520
authorMichal Wilczynski <m.wilczynski@samsung.com>
Tue, 11 Mar 2025 17:18:59 +0000 (18:18 +0100)
committerUlf Hansson <ulf.hansson@linaro.org>
Thu, 13 Mar 2025 13:42:06 +0000 (14:42 +0100)
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 5d4d263e1c6b ("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>
MAINTAINERS
drivers/pmdomain/Kconfig
drivers/pmdomain/Makefile
drivers/pmdomain/thead/Kconfig [new file with mode: 0644]
drivers/pmdomain/thead/Makefile [new file with mode: 0644]
drivers/pmdomain/thead/th1520-pm-domains.c [new file with mode: 0644]

index 838d2c354aea7e1d6f3f5780fbc2e845585798c1..7550435e940a72b2b0e6c303be6c8e5f1ae16509 100644 (file)
@@ -20430,6 +20430,7 @@ F:      drivers/firmware/thead,th1520-aon.c
 F:     drivers/mailbox/mailbox-th1520.c
 F:     drivers/net/ethernet/stmicro/stmmac/dwmac-thead.c
 F:     drivers/pinctrl/pinctrl-th1520.c
+F:     drivers/pmdomain/thead/
 F:     include/dt-bindings/clock/thead,th1520-clk-ap.h
 F:     include/dt-bindings/power/thead,th1520-power.h
 F:     include/linux/firmware/thead/thead,th1520-aon.h
index 23c64851a5b07bc655090adc691a639155956058..91f04ace35d4b024fafdf6af4e26a179640eb82f 100644 (file)
@@ -16,6 +16,7 @@ source "drivers/pmdomain/st/Kconfig"
 source "drivers/pmdomain/starfive/Kconfig"
 source "drivers/pmdomain/sunxi/Kconfig"
 source "drivers/pmdomain/tegra/Kconfig"
+source "drivers/pmdomain/thead/Kconfig"
 source "drivers/pmdomain/ti/Kconfig"
 source "drivers/pmdomain/xilinx/Kconfig"
 
index a68ece2f4c68a28bfc5495a532336b369e8a4ff3..7030f44a49df9e91b1c9d1b6d12690a6248671fb 100644 (file)
@@ -14,6 +14,7 @@ obj-y                                 += st/
 obj-y                                  += starfive/
 obj-y                                  += sunxi/
 obj-y                                  += tegra/
+obj-y                                  += thead/
 obj-y                                  += ti/
 obj-y                                  += xilinx/
 obj-y                                  += core.o governor.o
diff --git a/drivers/pmdomain/thead/Kconfig b/drivers/pmdomain/thead/Kconfig
new file mode 100644 (file)
index 0000000..c7a1ac0
--- /dev/null
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+config TH1520_PM_DOMAINS
+       tristate "Support TH1520 Power Domains"
+       depends on TH1520_AON_PROTOCOL || !TH1520_AON_PROTOCOL
+       select REGMAP_MMIO
+       help
+         This driver enables power domain management for the T-HEAD
+         TH-1520 SoC. On this SoC there are number of power domains,
+         which can be managed independently. For example GPU, NPU,
+         and DPU reside in their own power domains which can be
+         turned on/off.
diff --git a/drivers/pmdomain/thead/Makefile b/drivers/pmdomain/thead/Makefile
new file mode 100644 (file)
index 0000000..adfdf54
--- /dev/null
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_TH1520_PM_DOMAINS)                += th1520-pm-domains.o
diff --git a/drivers/pmdomain/thead/th1520-pm-domains.c b/drivers/pmdomain/thead/th1520-pm-domains.c
new file mode 100644 (file)
index 0000000..f702e20
--- /dev/null
@@ -0,0 +1,218 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2021 Alibaba Group Holding Limited.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
+ * Author: Michal Wilczynski <m.wilczynski@samsung.com>
+ */
+
+#include <linux/firmware/thead/thead,th1520-aon.h>
+#include <linux/slab.h>
+#include <linux/platform_device.h>
+#include <linux/pm_domain.h>
+
+#include <dt-bindings/power/thead,th1520-power.h>
+
+struct th1520_power_domain {
+       struct th1520_aon_chan *aon_chan;
+       struct generic_pm_domain genpd;
+       u32 rsrc;
+};
+
+struct th1520_power_info {
+       const char *name;
+       u32 rsrc;
+       bool disabled;
+};
+
+/*
+ * The AUDIO power domain is marked as disabled to prevent the driver from
+ * managing its power state. Direct AON firmware calls to control this power
+ * island trigger a firmware bug causing system instability. Until this
+ * firmware issue is resolved, the AUDIO power domain must remain disabled
+ * to avoid crashes.
+ */
+static const struct th1520_power_info th1520_pd_ranges[] = {
+       [TH1520_AUDIO_PD] = {"audio", TH1520_AON_AUDIO_PD, true },
+       [TH1520_VDEC_PD] = { "vdec", TH1520_AON_VDEC_PD, false },
+       [TH1520_NPU_PD] = { "npu", TH1520_AON_NPU_PD, false },
+       [TH1520_VENC_PD] = { "venc", TH1520_AON_VENC_PD, false },
+       [TH1520_GPU_PD] = { "gpu", TH1520_AON_GPU_PD, false },
+       [TH1520_DSP0_PD] = { "dsp0", TH1520_AON_DSP0_PD, false },
+       [TH1520_DSP1_PD] = { "dsp1", TH1520_AON_DSP1_PD, false }
+};
+
+static inline struct th1520_power_domain *
+to_th1520_power_domain(struct generic_pm_domain *genpd)
+{
+       return container_of(genpd, struct th1520_power_domain, genpd);
+}
+
+static int th1520_pd_power_on(struct generic_pm_domain *domain)
+{
+       struct th1520_power_domain *pd = to_th1520_power_domain(domain);
+
+       return th1520_aon_power_update(pd->aon_chan, pd->rsrc, true);
+}
+
+static int th1520_pd_power_off(struct generic_pm_domain *domain)
+{
+       struct th1520_power_domain *pd = to_th1520_power_domain(domain);
+
+       return th1520_aon_power_update(pd->aon_chan, pd->rsrc, false);
+}
+
+static struct generic_pm_domain *th1520_pd_xlate(const struct of_phandle_args *spec,
+                                                void *data)
+{
+       struct generic_pm_domain *domain = ERR_PTR(-ENOENT);
+       struct genpd_onecell_data *pd_data = data;
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(th1520_pd_ranges); i++) {
+               struct th1520_power_domain *pd;
+
+               if (th1520_pd_ranges[i].disabled)
+                       continue;
+
+               pd = to_th1520_power_domain(pd_data->domains[i]);
+               if (pd->rsrc == spec->args[0]) {
+                       domain = &pd->genpd;
+                       break;
+               }
+       }
+
+       return domain;
+}
+
+static struct th1520_power_domain *
+th1520_add_pm_domain(struct device *dev, const struct th1520_power_info *pi)
+{
+       struct th1520_power_domain *pd;
+       int ret;
+
+       pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
+       if (!pd)
+               return ERR_PTR(-ENOMEM);
+
+       pd->rsrc = pi->rsrc;
+       pd->genpd.power_on = th1520_pd_power_on;
+       pd->genpd.power_off = th1520_pd_power_off;
+       pd->genpd.name = pi->name;
+
+       ret = pm_genpd_init(&pd->genpd, NULL, true);
+       if (ret)
+               return ERR_PTR(ret);
+
+       return pd;
+}
+
+static void th1520_pd_init_all_off(struct generic_pm_domain **domains,
+                                  struct device *dev)
+{
+       int ret;
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(th1520_pd_ranges); i++) {
+               struct th1520_power_domain *pd;
+
+               if (th1520_pd_ranges[i].disabled)
+                       continue;
+
+               pd = to_th1520_power_domain(domains[i]);
+
+               ret = th1520_aon_power_update(pd->aon_chan, pd->rsrc, false);
+               if (ret)
+                       dev_err(dev,
+                               "Failed to initially power down power domain %s\n",
+                               pd->genpd.name);
+       }
+}
+
+static int th1520_pd_probe(struct platform_device *pdev)
+{
+       struct generic_pm_domain **domains;
+       struct genpd_onecell_data *pd_data;
+       struct th1520_aon_chan *aon_chan;
+       struct device *dev = &pdev->dev;
+       int i, ret;
+
+       aon_chan = th1520_aon_init(dev);
+       if (IS_ERR(aon_chan))
+               return dev_err_probe(dev, PTR_ERR(aon_chan),
+                                    "Failed to get AON channel\n");
+
+       domains = devm_kcalloc(dev, ARRAY_SIZE(th1520_pd_ranges),
+                              sizeof(*domains), GFP_KERNEL);
+       if (!domains) {
+               ret = -ENOMEM;
+               goto err_clean_aon;
+       }
+
+       pd_data = devm_kzalloc(dev, sizeof(*pd_data), GFP_KERNEL);
+       if (!pd_data) {
+               ret = -ENOMEM;
+               goto err_clean_aon;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(th1520_pd_ranges); i++) {
+               struct th1520_power_domain *pd;
+
+               if (th1520_pd_ranges[i].disabled)
+                       continue;
+
+               pd = th1520_add_pm_domain(dev, &th1520_pd_ranges[i]);
+               if (IS_ERR(pd)) {
+                       ret = PTR_ERR(pd);
+                       goto err_clean_genpd;
+               }
+
+               pd->aon_chan = aon_chan;
+               domains[i] = &pd->genpd;
+               dev_dbg(dev, "added power domain %s\n", pd->genpd.name);
+       }
+
+       pd_data->domains = domains;
+       pd_data->num_domains = ARRAY_SIZE(th1520_pd_ranges);
+       pd_data->xlate = th1520_pd_xlate;
+
+       /*
+        * Initialize all power domains to off to ensure they start in a
+        * low-power state. This allows device drivers to manage power
+        * domains by turning them on or off as needed.
+        */
+       th1520_pd_init_all_off(domains, dev);
+
+       ret = of_genpd_add_provider_onecell(dev->of_node, pd_data);
+       if (ret)
+               goto err_clean_genpd;
+
+       return 0;
+
+err_clean_genpd:
+       for (i--; i >= 0; i--)
+               pm_genpd_remove(domains[i]);
+err_clean_aon:
+       th1520_aon_deinit(aon_chan);
+
+       return ret;
+}
+
+static const struct of_device_id th1520_pd_match[] = {
+       { .compatible = "thead,th1520-aon" },
+       { /* Sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, th1520_pd_match);
+
+static struct platform_driver th1520_pd_driver = {
+       .driver = {
+               .name = "th1520-pd",
+               .of_match_table = th1520_pd_match,
+               .suppress_bind_attrs = true,
+       },
+       .probe = th1520_pd_probe,
+};
+module_platform_driver(th1520_pd_driver);
+
+MODULE_AUTHOR("Michal Wilczynski <m.wilczynski@samsung.com>");
+MODULE_DESCRIPTION("T-HEAD TH1520 SoC power domain controller");
+MODULE_LICENSE("GPL");