[PATCH 1/1] PM / devfreq: add SCMI performance protocol bridge

From: Li Mingzhe

Date: Wed Nov 18 2026 - 03:57:31 EST


Add a bridge that binds the SCMI performance protocol scmi_device and
exposes a devfreq-side API in include/linux/scmi-devfreq.h:

scmi_devfreq_ready() bound check for early probe deferral
scmi_devfreq_domain_id() DT clocks phandle -> perf domain id
scmi_devfreq_opps_add() import PERFORMANCE_DESCRIBE_LEVELS
into the device OPP table
scmi_devfreq_freq_get/set() PERFORMANCE_LEVEL_GET/SET wrappers

All functions return -EPROBE_DEFER until the bridge is bound, so
consumers (DMU/NOC devfreq, GPU and other modules) are retried
automatically. The API is generic and public so that out-of-tree
devfreq consumers can use it.

Signed-off-by: Li Mingzhe <limingzhe1839@xxxxxxxxxxxxxx>
---
drivers/devfreq/Kconfig | 9 ++
drivers/devfreq/Makefile | 1 +
drivers/devfreq/scmi_devfreq.c | 205 +++++++++++++++++++++++++++++++++
include/linux/scmi-devfreq.h | 30 +++++
4 files changed, 245 insertions(+)
create mode 100644 drivers/devfreq/scmi_devfreq.c
create mode 100644 include/linux/scmi-devfreq.h

diff --git a/drivers/devfreq/Kconfig b/drivers/devfreq/Kconfig
index 043ec397de52..19cd6e01561e 100644
--- a/drivers/devfreq/Kconfig
+++ b/drivers/devfreq/Kconfig
@@ -150,6 +150,15 @@ config ARM_SUN8I_A33_MBUS_DEVFREQ
This adds the DEVFREQ driver for the MBUS controller in some
Allwinner sun8i (A33 through H3) and sun50i (A64 and H5) SoCs.

+config ARM_SCMI_DEVFREQ
+ bool "SCMI performance protocol bridge for devfreq"
+ depends on ARM_SCMI_PROTOCOL
+ help
+ Bridge owning the SCMI performance-protocol scmi_device binding
+ on behalf of devfreq drivers whose devices are managed by SCMI
+ performance domains.The bridge must be built-in: a devfreq device
+ polling the bridge cannot survive the bridge being removed.
+
config ARM_PHYTIUM_NOC_DEVFREQ
tristate "ARM PHYTIUM NOC DEVFREQ Driver"
depends on ARCH_PHYTIUM
diff --git a/drivers/devfreq/Makefile b/drivers/devfreq/Makefile
index c57455f9b481..6dd048ac6530 100644
--- a/drivers/devfreq/Makefile
+++ b/drivers/devfreq/Makefile
@@ -12,6 +12,7 @@ obj-$(CONFIG_ARM_EXYNOS_BUS_DEVFREQ) += exynos-bus.o
obj-$(CONFIG_ARM_IMX_BUS_DEVFREQ) += imx-bus.o
obj-$(CONFIG_ARM_IMX8M_DDRC_DEVFREQ) += imx8m-ddrc.o
obj-$(CONFIG_ARM_MEDIATEK_CCI_DEVFREQ) += mtk-cci-devfreq.o
+obj-$(CONFIG_ARM_SCMI_DEVFREQ) += scmi_devfreq.o
obj-$(CONFIG_ARM_PHYTIUM_DMU_DEVFREQ) += phytium_dmu.o
obj-$(CONFIG_ARM_PHYTIUM_NOC_DEVFREQ) += phytium_noc.o
obj-$(CONFIG_ARM_RK3399_DMC_DEVFREQ) += rk3399_dmc.o
diff --git a/drivers/devfreq/scmi_devfreq.c b/drivers/devfreq/scmi_devfreq.c
new file mode 100644
index 000000000000..147668850a7c
--- /dev/null
+++ b/drivers/devfreq/scmi_devfreq.c
@@ -0,0 +1,205 @@
+// SPDX-License-Identifier: GPL-1.0
+/*
+ * Phytium SCMI devfreq bridge
+ *
+ * Single owner of the SCMI performance-device binding for the Phytium
+ * devfreq drivers (NOC/DMU), mirroring the drivers/cpufreq/scmi-cpufreq.c
+ * pattern: bind the perf-protocol scmi_device, import its ops/handle,
+ * register the dummy clock provider required by devices whose DT
+ * "clocks" phandle points at the perf protocol node, and expose a small
+ * devfreq-side API to the platform drivers.
+ *
+ * NOTE: the devfreq users must be built-in; unbinding/rebinding the
+ * scmi_device while a devfreq device is polling is not supported.
+ */
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/atomic.h>
+#include <linux/clk-provider.h>
+#include <linux/of.h>
+#include <linux/property.h>
+#include <linux/pm_opp.h>
+#include <linux/scmi_protocol.h>
+
+static const struct scmi_perf_proto_ops *perf_ops;
+static struct scmi_protocol_handle *scmi_ph;
+static atomic_t bridge_ready = ATOMIC_INIT(0);
+
+/*
+ * The devfreq nodes carry "clocks = <&scmi_perf id>" solely so that
+ * scmi_dev_domain_id() can parse the perf-domain id. The OPP core,
+ * however, treats any "clocks" phandle as a real clock reference and
+ * calls clk_get(dev, NULL) when creating the OPP table via
+ * dev_pm_opp_add() inside SCMI's device_opps_add(). Since protocol@13
+ * is never registered as a clock provider, that lookup would return
+ * -EPROBE_DEFER and brick the DT perf path.
+ *
+ * Mirror drivers/cpufreq/scmi-cpufreq.c:311-315: register a dummy
+ * clock provider on the sdev whose of_node IS the protocol@13 node so
+ * the clkspec lookup matches and clk_get() succeeds. clk_get()
+ * resolves to NULL (of_clk_hw_simple_get() just hands back the data
+ * pointer), which the OPP core treats as success. The perf path never
+ * uses clk_set_rate()/clk_get_rate(), so the placeholder clk is never
+ * touched afterwards.
+ */
+static void scmi_devfreq_setup_dummy_clk_provider(struct scmi_device *sdev)
+{
+ struct device *dev = &sdev->dev;
+ int ret;
+
+ if (!of_property_present(dev->of_node, "#clock-cells"))
+ return;
+
+ ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
+ if (ret) {
+ dev_warn(dev, "devfreq bridge: failed to add dummy clk provider: %d\n",
+ ret);
+ return;
+ }
+
+ dev_info(dev, "devfreq bridge: dummy clk provider registered on perf protocol node\n");
+}
+
+static int scmi_devfreq_scmi_probe(struct scmi_device *sdev)
+{
+ const void *ops;
+ struct scmi_protocol_handle *ph;
+
+ ops = sdev->handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph);
+ if (IS_ERR(ops)) {
+ dev_err(&sdev->dev, "devfreq bridge: devm_protocol_get failed: %ld\n",
+ PTR_ERR(ops));
+ return PTR_ERR(ops);
+ }
+
+ perf_ops = ops;
+ scmi_ph = ph;
+
+ scmi_devfreq_setup_dummy_clk_provider(sdev);
+
+ atomic_set(&bridge_ready, 1);
+ dev_info(&sdev->dev, "devfreq bridge: SCMI perf protocol acquired, ready=%d\n",
+ atomic_read(&bridge_ready));
+ return 0;
+}
+
+static void scmi_devfreq_scmi_remove(struct scmi_device *sdev)
+{
+ atomic_set(&bridge_ready, 0);
+ dev_warn(&sdev->dev, "devfreq bridge: SCMI perf protocol released, devfreq users will defer\n");
+}
+
+static const struct scmi_device_id scmi_devfreq_id_table[] = {
+ { SCMI_PROTOCOL_PERF, "devfreq" },
+ /*
+ * Claim remaining protocol@13 instances (e.g. the "cpufreq"
+ * scmi_device after a failed cpufreq registration): an unbound
+ * scmi_device on this of_node keeps fw_devlink holding the
+ * devfreq nodes in deferred probe with "supplier not ready".
+ */
+ { SCMI_PROTOCOL_PERF, NULL },
+ { },
+};
+MODULE_DEVICE_TABLE(scmi, scmi_devfreq_id_table);
+
+static struct scmi_driver scmi_devfreq_scmi_driver = {
+ .name = "phytium_devfreq_scmi",
+ .probe = scmi_devfreq_scmi_probe,
+ .remove = scmi_devfreq_scmi_remove,
+ .id_table = scmi_devfreq_id_table,
+};
+
+int scmi_devfreq_ready(void)
+{
+ return atomic_read(&bridge_ready);
+}
+EXPORT_SYMBOL_GPL(scmi_devfreq_ready);
+
+int scmi_devfreq_domain_id(struct device *dev)
+{
+ int ret;
+
+ if (!atomic_read(&bridge_ready))
+ return -EPROBE_DEFER;
+
+ ret = perf_ops->device_domain_id(dev);
+ dev_dbg(dev, "devfreq bridge: device_domain_id = %d\n", ret);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(scmi_devfreq_domain_id);
+
+int scmi_devfreq_opps_add(struct device *dev)
+{
+ int err, count;
+
+ if (!atomic_read(&bridge_ready))
+ return -EPROBE_DEFER;
+
+ err = perf_ops->device_opps_add(scmi_ph, dev);
+ count = dev_pm_opp_get_opp_count(dev);
+
+ if (err || count <= 0) {
+ dev_warn(dev, "devfreq bridge: device_opps_add ret = %d, opp_count = %d\n",
+ err, count);
+ dev_pm_opp_remove_all_dynamic(dev);
+ return err ? err : -EINVAL;
+ }
+
+ dev_info(dev, "devfreq bridge: device_opps_add ret = %d, opp_count = %d\n",
+ err, count);
+ return count;
+}
+EXPORT_SYMBOL_GPL(scmi_devfreq_opps_add);
+
+int scmi_devfreq_freq_get(u32 domain_id, unsigned long *rate)
+{
+ int err;
+
+ if (!atomic_read(&bridge_ready))
+ return -EPROBE_DEFER;
+
+ err = perf_ops->freq_get(scmi_ph, domain_id, rate, false);
+ if (err) {
+ pr_debug("devfreq bridge: freq_get failed domain=%u err=%d\n",
+ domain_id, err);
+ }
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(scmi_devfreq_freq_get);
+
+int scmi_devfreq_freq_set(u32 domain_id, unsigned long rate)
+{
+ int err;
+
+ if (!atomic_read(&bridge_ready))
+ return -EPROBE_DEFER;
+
+ err = perf_ops->freq_set(scmi_ph, domain_id, rate, false);
+ if (err) {
+ pr_warn("devfreq bridge: freq_set FAILED domain=%u rate=%lu err=%d\n",
+ domain_id, rate, err);
+ return err;
+ }
+
+ pr_info("devfreq bridge: freq_set OK domain=%u rate=%lu Hz (%lu kHz)\n",
+ domain_id, rate, rate / 1000);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(scmi_devfreq_freq_set);
+
+static int __init scmi_devfreq_init(void)
+{
+ return scmi_register(&scmi_devfreq_scmi_driver);
+}
+
+static void __exit scmi_devfreq_exit(void)
+{
+ scmi_unregister(&scmi_devfreq_scmi_driver);
+}
+module_init(scmi_devfreq_init);
+module_exit(scmi_devfreq_exit);
+
+MODULE_DESCRIPTION("Phytium SCMI devfreq bridge (NOC/DMU perf protocol owner)");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/scmi-devfreq.h b/include/linux/scmi-devfreq.h
new file mode 100644
index 000000000000..37ef417b2bc1
--- /dev/null
+++ b/include/linux/scmi-devfreq.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_SCMI_DEVFREQ_H
+#define _LINUX_SCMI_DEVFREQ_H
+
+#include <linux/device.h>
+
+/*
+ * Phytium SCMI devfreq bridge (drivers/devfreq/scmi_devfreq.c).
+ * Single owner of the perf-protocol scmi_device for the NOC/DMU
+ * devfreq drivers. All functions return -EPROBE_DEFER until the
+ * bridge's scmi_device is bound.
+ */
+
+/* Bridge readiness check (cheap, for early defer decisions). */
+int scmi_devfreq_ready(void);
+
+/* Parse the device's clocks phandle -> perf domain id. */
+int scmi_devfreq_domain_id(struct device *dev);
+
+/* Add the domain's OPP table from PERFORMANCE_DESCRIBE_LEVELS.
+ * Returns the OPP count (> 0), or a negative errno. Cleans up its own
+ * partial OPPs on failure.
+ */
+int scmi_devfreq_opps_add(struct device *dev);
+
+/* PERFORMANCE_LEVEL_GET / PERFORMANCE_LEVEL_SET wrappers. */
+int scmi_devfreq_freq_get(u32 domain_id, unsigned long *rate);
+int scmi_devfreq_freq_set(u32 domain_id, unsigned long rate);
+
+#endif /* _LINUX_SCMI_DEVFREQ_H */
--
2.43.0