[PATCH net-next 02/12] net: phy: Introduce phy_mux, a MII multiplexer representation

From: Maxime Chevallier

Date: Fri Dec 04 2026 - 06:07:24 EST


The implementation of combo-port and multi-PHY interfaces can be done
either by having the MAC's MII interface muxed to 2 different
destinations, or by having a PHY with 2 outputs.

Let's introduce a framework to deal with the first case: muxing the MII
bus. Here again, we can mux it with different techniques:

- A Mux block in the SoC, after the MAC, MMIO-mapped like we have on
banana Pi R4 for example

- A gpio-driven Mux, can be some high speed analog switch for example.
Here again, we have 2 options:
- The switch can be controlled by Linux
- The switch is controlled through another means, that's the case
for Turris Omnia for example, the control pin is tied to the SFP
cage's mod_def0 pin that indicates if a module is present

- No mux at all, the mux is just the MII going to 2 PHYs, one of them
needing to isolate its MII.

The plan is to support all these cases, with the uAPI interface being
phy_port as it'll also allow supporting phy-driven muxes.

Introduce the framework for the muxes, named phy_mux, for now without
users, they'll be added in the next commits.

The mux is represented in DT, and is parsed using the fwnode logic.

A mux has multiple "ports" represented by "struct phy_mux_port", that
correspond to the PHY-side MII busses, only one of these can be active
at any given time.

Signed-off-by: Maxime Chevallier <maxime.chevallier@xxxxxxxxxxx>
---
MAINTAINERS | 2 +
drivers/net/phy/Kconfig | 8 ++
drivers/net/phy/Makefile | 1 +
drivers/net/phy/phy_mux.c | 189 ++++++++++++++++++++++++++++++++++++++
include/linux/phy_mux.h | 44 +++++++++
5 files changed, 244 insertions(+)
create mode 100644 drivers/net/phy/phy_mux.c
create mode 100644 include/linux/phy_mux.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 8685ed44840a..b2b02a1dbd3a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10086,6 +10086,8 @@ M: Maxime Chevallier <maxime.chevallier@xxxxxxxxxxx>
L: netdev@xxxxxxxxxxxxxxx
S: Maintained
F: Documentation/devicetree/bindings/net/mii-mux*.yaml
+F: drivers/net/phy/phy_mux.c
+F: include/linux/phy_mux.h

EXEC & BINFMT API, ELF
M: Kees Cook <kees@xxxxxxxxxx>
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index 30e9d22c8d1a..d8a0131d2aa2 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -21,6 +21,14 @@ menuconfig PHYLIB

if PHYLIB

+config PHY_MUX
+ bool "PHY multiplexing support"
+ help
+ Some boards route several PHYs or SFP cages to the same MAC
+ through a multiplexer, only one of them being usable at a
+ time. This adds the infrastructure to represent such multiplexers
+ and to select which port carries the link.
+
config SWPHY
bool

diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
index 579066a638f0..b3ec2933fec4 100644
--- a/drivers/net/phy/Makefile
+++ b/drivers/net/phy/Makefile
@@ -5,6 +5,7 @@ libphy-y := phy.o phy-c45.o phy-core.o phy_device.o \
linkmode.o phy_link_topology.o \
phy_caps.o mdio_bus_provider.o phy_port.o \
mdio_device.o
+libphy-$(CONFIG_PHY_MUX) += phy_mux.o

ifdef CONFIG_PHYLIB
# built-in whenever PHYLIB is built-in or module
diff --git a/drivers/net/phy/phy_mux.c b/drivers/net/phy/phy_mux.c
new file mode 100644
index 000000000000..e259eb4e271a
--- /dev/null
+++ b/drivers/net/phy/phy_mux.c
@@ -0,0 +1,189 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/phy.h>
+#include <linux/phy_mux.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+#include <linux/xarray.h>
+
+static LIST_HEAD(phy_muxes);
+static DEFINE_MUTEX(phy_mux_mutex);
+
+struct phy_mux {
+ struct list_head node;
+ struct device *dev;
+
+ struct xarray ports;
+ int n_ports;
+ int max_n_ports;
+
+};
+
+/**
+ * phy_mux_alloc() - Allocate a mux
+ * @dev: The mux device
+ * @max_n_ports: How many ports the mux accepts
+ *
+ * The mux is freed with phy_mux_free().
+ *
+ * Returns: the mux, or an ERR_PTR.
+ */
+struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports)
+{
+ struct phy_mux *mux;
+
+ mux = kzalloc_obj(*mux);
+ if (!mux)
+ return ERR_PTR(-ENOMEM);
+
+ xa_init_flags(&mux->ports, XA_FLAGS_ALLOC1);
+
+ mux->dev = dev;
+ mux->max_n_ports = max_n_ports;
+
+ return mux;
+}
+EXPORT_SYMBOL_GPL(phy_mux_alloc);
+
+static void phy_mux_port_destroy(struct phy_mux_port *mux_port)
+{
+ switch (mux_port->type) {
+ case PHY_MUX_PORT_PHY:
+ fwnode_handle_put(mux_port->fwnode);
+ break;
+ }
+
+ kfree(mux_port);
+}
+
+/**
+ * phy_mux_register_port() - Add a port to a mux
+ * @mux: The mux, not registered yet
+ * @port: The port, allocated with kzalloc(). The mux owns it from then on
+ * and frees it in phy_mux_free().
+ *
+ * Returns: 0 on success, -EBUSY if the mux is full, or a negative error.
+ */
+static int phy_mux_register_port(struct phy_mux *mux,
+ struct phy_mux_port *port)
+{
+ int ret;
+
+ if (mux->n_ports >= mux->max_n_ports)
+ return -EBUSY;
+
+ ret = xa_alloc(&mux->ports, &port->id, port, xa_limit_16b, GFP_KERNEL);
+ if (ret)
+ return ret;
+
+ mux->n_ports++;
+
+ return 0;
+}
+
+/**
+ * phy_mux_fwnode_add_ports() - Create the ports described under a fwnode
+ * @mux: The mux
+ * @fwnode: The mux device's fwnode, each child being a port with a
+ * phy-handle and a phy-mode
+ *
+ * The PHYs themselves are only looked up when the mux gets attached.
+ *
+ * Returns: 0 on success or a negative error.
+ */
+int phy_mux_fwnode_add_ports(struct phy_mux *mux,
+ const struct fwnode_handle *fwnode)
+{
+ struct fwnode_handle *child, *phy_fwnode;
+ struct phy_mux_port *port;
+ int ret;
+
+ fwnode_for_each_available_child_node(fwnode, child) {
+ phy_fwnode = fwnode_get_phy_node(child);
+ if (IS_ERR(phy_fwnode)) {
+ ret = PTR_ERR(phy_fwnode);
+ goto err;
+ }
+
+ port = kzalloc_obj(*port);
+ if (!port) {
+ fwnode_handle_put(phy_fwnode);
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ port->type = PHY_MUX_PORT_PHY;
+ port->fwnode = phy_fwnode;
+ port->mux = mux;
+
+ ret = fwnode_get_phy_mode(child);
+ if (ret < 0) {
+ phy_mux_port_destroy(port);
+ goto err;
+ }
+ port->interface = ret;
+
+ ret = phy_mux_register_port(mux, port);
+ if (ret) {
+ phy_mux_port_destroy(port);
+ goto err;
+ }
+ }
+
+ return 0;
+
+err:
+ fwnode_handle_put(child);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(phy_mux_fwnode_add_ports);
+
+/**
+ * phy_mux_register() - Make the mux available to its parent
+ * @mux: The mux, with all its ports added
+ *
+ * Returns: 0 on success or a negative error.
+ */
+int phy_mux_register(struct phy_mux *mux)
+{
+ mutex_lock(&phy_mux_mutex);
+ list_add(&mux->node, &phy_muxes);
+ mutex_unlock(&phy_mux_mutex);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(phy_mux_register);
+
+/**
+ * phy_mux_unregister() - Make the mux unavailable to its parent
+ * @mux: The mux, registered
+ *
+ * The mux still has to be freed with phy_mux_free().
+ */
+void phy_mux_unregister(struct phy_mux *mux)
+{
+ mutex_lock(&phy_mux_mutex);
+ list_del(&mux->node);
+ mutex_unlock(&phy_mux_mutex);
+}
+EXPORT_SYMBOL_GPL(phy_mux_unregister);
+
+/**
+ * phy_mux_free() - Free a mux and its ports
+ * @mux: The mux, not registered
+ */
+void phy_mux_free(struct phy_mux *mux)
+{
+ struct phy_mux_port *port;
+ unsigned long id;
+
+ xa_for_each(&mux->ports, id, port)
+ phy_mux_port_destroy(port);
+
+ xa_destroy(&mux->ports);
+ kfree(mux);
+}
+EXPORT_SYMBOL_GPL(phy_mux_free);
diff --git a/include/linux/phy_mux.h b/include/linux/phy_mux.h
new file mode 100644
index 000000000000..3dde6dd57faf
--- /dev/null
+++ b/include/linux/phy_mux.h
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __PHY_MUX_H
+#define __PHY_MUX_H
+
+#include <linux/phy.h>
+
+struct device;
+struct fwnode_handle;
+struct phy_mux;
+struct phy_mux_port;
+
+enum phy_mux_port_type {
+ PHY_MUX_PORT_PHY,
+};
+
+/**
+ * struct phy_mux_port - One of the multiplexed channels of a MII multiplexer
+ * @id: Identifier of the port, unique within the mux
+ * @mux: The mux this port belongs to
+ * @type: What sits behind that port
+ * @phydev: For PHY_MUX_PORT_PHY, the PHY device behind the port, while the
+ * mux is attached
+ * @fwnode: For PHY_MUX_PORT_PHY, the firmware node of that PHY
+ * @interface: The MII mode the link runs in when this port is selected
+ */
+struct phy_mux_port {
+ u32 id;
+ struct phy_mux *mux;
+ enum phy_mux_port_type type;
+ struct phy_device *phydev;
+ struct fwnode_handle *fwnode;
+
+ phy_interface_t interface;
+};
+
+struct phy_mux *phy_mux_alloc(struct device *dev, int max_n_ports);
+int phy_mux_fwnode_add_ports(struct phy_mux *mux,
+ const struct fwnode_handle *fwnode);
+int phy_mux_register(struct phy_mux *mux);
+void phy_mux_unregister(struct phy_mux *mux);
+void phy_mux_free(struct phy_mux *mux);
+
+#endif /* __PHY_MUX_H */
--
2.56.1