[PATCH net-next 11/12] net: phy: mux: Add the mii-mux-auto driver
From: Maxime Chevallier
Date: Fri Dec 04 2026 - 06:08:41 EST
Add a driver for the simplest kind of mux, the ones that are switched by
the hardware without any control from software, described with the
mii-mux-auto binding.
That's what we have on the Turris Omnia's WAN port, where the mux's
select line is tied to the SFP cage's mod_def0 signal.
There's not much to do in the driver: it registers the ports, and looks
for the takes-precedence property to tell the core which port the
hardware is going to switch to.
Signed-off-by: Maxime Chevallier <maxime.chevallier@xxxxxxxxxxx>
---
MAINTAINERS | 1 +
drivers/net/phy/Kconfig | 2 +
drivers/net/phy/Makefile | 1 +
drivers/net/phy/mux/Kconfig | 18 ++++++
drivers/net/phy/mux/Makefile | 3 +
drivers/net/phy/mux/mii_mux_auto.c | 99 ++++++++++++++++++++++++++++++
6 files changed, 124 insertions(+)
create mode 100644 drivers/net/phy/mux/Kconfig
create mode 100644 drivers/net/phy/mux/Makefile
create mode 100644 drivers/net/phy/mux/mii_mux_auto.c
diff --git a/MAINTAINERS b/MAINTAINERS
index b2b02a1dbd3a..59017b0f9f86 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10086,6 +10086,7 @@ M: Maxime Chevallier <maxime.chevallier@xxxxxxxxxxx>
L: netdev@xxxxxxxxxxxxxxx
S: Maintained
F: Documentation/devicetree/bindings/net/mii-mux*.yaml
+F: drivers/net/phy/mux/
F: drivers/net/phy/phy_mux.c
F: include/linux/phy_mux.h
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index d8a0131d2aa2..f6397dab2304 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -29,6 +29,8 @@ config PHY_MUX
time. This adds the infrastructure to represent such multiplexers
and to select which port carries the link.
+source "drivers/net/phy/mux/Kconfig"
+
config SWPHY
bool
diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
index b3ec2933fec4..b26b34c93dbc 100644
--- a/drivers/net/phy/Makefile
+++ b/drivers/net/phy/Makefile
@@ -21,6 +21,7 @@ obj-$(CONFIG_PHYLINK) += phylink.o
obj-$(CONFIG_PHYLIB) += libphy.o
obj-$(CONFIG_PHYLIB) += mdio_devres.o
obj-$(CONFIG_PHY_PACKAGE) += phy_package.o
+obj-$(CONFIG_PHY_MUX) += mux/
obj-$(CONFIG_NETWORK_PHY_TIMESTAMPING) += mii_timestamper.o
diff --git a/drivers/net/phy/mux/Kconfig b/drivers/net/phy/mux/Kconfig
new file mode 100644
index 000000000000..cbfd49fab2ea
--- /dev/null
+++ b/drivers/net/phy/mux/Kconfig
@@ -0,0 +1,18 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+if PHY_MUX
+
+config MII_MUX_AUTO
+ tristate "MII multiplexer switched by the hardware"
+ help
+ Support for the two-port MII multiplexers that the hardware
+ switches by itself, without any software control.
+
+ This is typically found on combo ports, where a MAC is wired to
+ either a PHY or an SFP cage depending on whether a module is
+ plugged in the cage.
+
+ To compile this driver as a module, choose M here: the module
+ will be called mii_mux_auto.
+
+endif # PHY_MUX
diff --git a/drivers/net/phy/mux/Makefile b/drivers/net/phy/mux/Makefile
new file mode 100644
index 000000000000..23e8c5029507
--- /dev/null
+++ b/drivers/net/phy/mux/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+obj-$(CONFIG_MII_MUX_AUTO) += mii_mux_auto.o
diff --git a/drivers/net/phy/mux/mii_mux_auto.c b/drivers/net/phy/mux/mii_mux_auto.c
new file mode 100644
index 000000000000..980cc2c242ba
--- /dev/null
+++ b/drivers/net/phy/mux/mii_mux_auto.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <linux/module.h>
+#include <linux/phy_mux.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+
+static int mii_mux_auto_init_port(struct phy_mux_port *port,
+ struct fwnode_handle *child)
+{
+ port->has_precedence = fwnode_property_read_bool(child,
+ "takes-precedence");
+
+ return 0;
+}
+
+static int mii_mux_auto_check_ports(struct fwnode_handle *fwnode)
+{
+ struct fwnode_handle *child;
+ int n_sfp = 0, n_phy = 0, n_prec = 0, n_sfp_prec = 0;
+
+ fwnode_for_each_available_child_node(fwnode, child) {
+ if (fwnode_property_read_bool(child, "takes-precedence"))
+ n_prec++;
+
+ if (!fwnode_property_present(child, "sfp")) {
+ n_phy++;
+ continue;
+ }
+
+ n_sfp++;
+ if (fwnode_property_read_bool(child, "takes-precedence"))
+ n_sfp_prec++;
+ }
+
+ if (n_sfp != 1 || n_phy != 1 || n_prec != 1 || n_sfp_prec != 1)
+ return -EOPNOTSUPP;
+
+ return 0;
+}
+
+static int mii_mux_auto_probe(struct platform_device *pdev)
+{
+ struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
+ struct phy_mux *mux;
+ int ret;
+
+ ret = mii_mux_auto_check_ports(fwnode);
+ if (ret)
+ return ret;
+
+ mux = phy_mux_alloc(&pdev->dev, 2);
+ if (IS_ERR(mux))
+ return PTR_ERR(mux);
+
+ ret = phy_mux_fwnode_add_ports(mux, fwnode, mii_mux_auto_init_port);
+ if (ret)
+ goto err_free;
+
+ ret = phy_mux_register(mux);
+ if (ret)
+ goto err_free;
+
+ platform_set_drvdata(pdev, mux);
+
+ return 0;
+
+err_free:
+ phy_mux_free(mux);
+ return ret;
+}
+
+static void mii_mux_auto_remove(struct platform_device *pdev)
+{
+ struct phy_mux *mux = platform_get_drvdata(pdev);
+
+ phy_mux_unregister(mux);
+ phy_mux_free(mux);
+}
+
+static const struct of_device_id mii_mux_auto_of_match[] = {
+ { .compatible = "mii-mux-auto" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, mii_mux_auto_of_match);
+
+static struct platform_driver mii_mux_auto_driver = {
+ .probe = mii_mux_auto_probe,
+ .remove = mii_mux_auto_remove,
+ .driver = {
+ .name = "mii-mux-auto",
+ .of_match_table = mii_mux_auto_of_match,
+ },
+};
+module_platform_driver(mii_mux_auto_driver);
+
+MODULE_DESCRIPTION("Ethernet MII multiplexer switched by the hardware");
+MODULE_AUTHOR("Maxime Chevallier <maxime.chevallier@xxxxxxxxxxx>");
+MODULE_LICENSE("GPL");
--
2.56.1