Skip to contentSkip to conversation

net: mdio: improve reset handling of mdio devices

PATCH Applied Buday Csaba 2 participants 5 messages Nov 18, 2025 – Nov 21, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover net: mdio: improve reset handling of mdio devices

This patchset refactors and slightly improves the reset handling of mdio_device.

The patches were split from a larger series, discussed previously in the links below.

The difference between v2 and v3, is that the helper function declarations have been moved to a new header file: drivers/net/phy/mdio-private.h See links for the previous versions, and for the now separate leak fix.

Buday Csaba (3):
net: mdio: move device reset functions to mdio_device.c
net: mdio: common handling of phy device reset properties
net: mdio: improve reset handling in mdio_device.c

 drivers/net/mdio/fwnode_mdio.c |  5 ----
 drivers/net/phy/mdio-private.h | 11 +++++++
 drivers/net/phy/mdio_bus.c     | 40 ++-----------------------
 drivers/net/phy/mdio_device.c  | 54 ++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 42 deletions(-)
 create mode 100644 drivers/net/phy/mdio-private.h

base-commit: c9dfb92de0738eb7fe6a591ad1642333793e8b6e

Signature · 1 line
2.39.5
#1 lore raw

PATCH v3 1/3 net: mdio: move device reset functions to mdio_device.c

The functions mdiobus_register_gpiod() and mdiobus_register_reset() handle the mdio device reset initialization, which belong to mdio_device.c.
Move them from mdio_bus.c to mdio_device.c, and rename them to match the corresponding source file: mdio_device_register_gpio() and mdio_device_register_reset().
Remove 'static' qualifiers and declare them in drivers/net/phy/mdio-private.h (new header file).

---
V2 -> V3: removed declarations from include/linux/mdio.h to
drivers/net/phy/mdio-private.h on maintainer request.
V1 -> V2: rebase, no changes

---
 drivers/net/phy/mdio-private.h | 11 +++++++++++
 drivers/net/phy/mdio_bus.c     | 32 +++-----------------------------
 drivers/net/phy/mdio_device.c  | 28 ++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 29 deletions(-)
 create mode 100644 drivers/net/phy/mdio-private.h
drivers/net/phy/mdio-private.h new file +11 -0
new file mode 100644
index 000000000..44e2e0907
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef __MDIO_PRIVATE_H
+#define __MDIO_PRIVATE_H
+
+/* MDIO internal helpers
+ */
+
+int mdio_device_register_reset(struct mdio_device *mdiodev);
+int mdio_device_register_gpiod(struct mdio_device *mdiodev);
+
+#endif /* __MDIO_PRIVATE_H */
drivers/net/phy/mdio_bus.c +3 -29
index 435424113..575b8bb5b 100644
@@ -29,37 +29,11 @@
 #include <linux/string.h>
 #include <linux/uaccess.h>
 #include <linux/unistd.h>
+#include "mdio-private.h"
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/mdio.h>
 
-static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
-{
-	/* Deassert the optional reset signal */
-	mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
-						 "reset", GPIOD_OUT_LOW);
-	if (IS_ERR(mdiodev->reset_gpio))
-		return PTR_ERR(mdiodev->reset_gpio);
-
-	if (mdiodev->reset_gpio)
-		gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
-
-	return 0;
-}
-
-static int mdiobus_register_reset(struct mdio_device *mdiodev)
-{
-	struct reset_control *reset;
-
-	reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
-	if (IS_ERR(reset))
-		return PTR_ERR(reset);
-
-	mdiodev->reset_ctrl = reset;
-
-	return 0;
-}
-
 int mdiobus_register_device(struct mdio_device *mdiodev)
 {
 	int err;
@@ -68,11 +42,11 @@ int mdiobus_register_device(struct mdio_device *mdiodev)
 		return -EBUSY;
 
 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
-		err = mdiobus_register_gpiod(mdiodev);
+		err = mdio_device_register_gpiod(mdiodev);
 		if (err)
 			return err;
 
-		err = mdiobus_register_reset(mdiodev);
+		err = mdio_device_register_reset(mdiodev);
 		if (err) {
 			gpiod_put(mdiodev->reset_gpio);
 			mdiodev->reset_gpio = NULL;
drivers/net/phy/mdio_device.c +28 -0
index f64176e0e..0e04bddd3 100644
@@ -22,6 +22,7 @@
 #include <linux/string.h>
 #include <linux/unistd.h>
 #include <linux/property.h>
+#include "mdio-private.h"
 
 void mdio_device_free(struct mdio_device *mdiodev)
 {
@@ -118,6 +119,33 @@ void mdio_device_remove(struct mdio_device *mdiodev)
 }
 EXPORT_SYMBOL(mdio_device_remove);
 
+int mdio_device_register_gpiod(struct mdio_device *mdiodev)
+{
+	/* Deassert the optional reset signal */
+	mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
+						 "reset", GPIOD_OUT_LOW);
+	if (IS_ERR(mdiodev->reset_gpio))
+		return PTR_ERR(mdiodev->reset_gpio);
+
+	if (mdiodev->reset_gpio)
+		gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
+
+	return 0;
+}
+
+int mdio_device_register_reset(struct mdio_device *mdiodev)
+{
+	struct reset_control *reset;
+
+	reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
+	if (IS_ERR(reset))
+		return PTR_ERR(reset);
+
+	mdiodev->reset_ctrl = reset;
+
+	return 0;
+}
+
 void mdio_device_reset(struct mdio_device *mdiodev, int value)
 {
 	unsigned int d;
Signature · 1 line
2.39.5
#2 lore raw

PATCH v3 2/3 net: mdio: common handling of phy device reset properties

In reply to Buday Csaba

Unify the handling of the per device reset properties for mdio_device.

Merge mdio_device_register_gpiod() and mdio_device_register_reset() into mdio_device_register_reset(), that handles both reset-controllers and reset-gpios.
Move reading of the reset firmware properties (reset-assert-us, reset-deassert-us) from fwnode_mdio.c to mdio_device_register_reset(), so all reset related initialization code is kept in one place.

Introduce mdio_device_unregister_reset() to release the associated resources.

These changes make tracking the reset properties easier. Added kernel-doc for mdio_device_register/unregister_reset().

---
V2 -> V3: moved declarations to drivers/net/phy/mdio-private.h V1 -> V2: rebase, no change (leak fix already applied to base)

---
 drivers/net/mdio/fwnode_mdio.c |  5 -----
 drivers/net/phy/mdio-private.h |  2 +-
 drivers/net/phy/mdio_bus.c     | 12 ++--------
 drivers/net/phy/mdio_device.c  | 40 ++++++++++++++++++++++++++--------
 4 files changed, 34 insertions(+), 25 deletions(-)
drivers/net/mdio/fwnode_mdio.c +0 -5
index 9b41d4697..ba7091518 100644
@@ -92,11 +92,6 @@ int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
 	if (fwnode_property_read_bool(child, "broken-turn-around"))
 		mdio->phy_ignore_ta_mask |= 1 << addr;
 
-	fwnode_property_read_u32(child, "reset-assert-us",
-				 &phy->mdio.reset_assert_delay);
-	fwnode_property_read_u32(child, "reset-deassert-us",
-				 &phy->mdio.reset_deassert_delay);
-
 	/* Associate the fwnode with the device structure so it
 	 * can be looked up later
 	 */
drivers/net/phy/mdio-private.h +1 -1
index 44e2e0907..8bc6d9088 100644
@@ -6,6 +6,6 @@
  */
 
 int mdio_device_register_reset(struct mdio_device *mdiodev);
-int mdio_device_register_gpiod(struct mdio_device *mdiodev);
+void mdio_device_unregister_reset(struct mdio_device *mdiodev);
 
 #endif /* __MDIO_PRIVATE_H */
drivers/net/phy/mdio_bus.c +2 -10
index 575b8bb5b..ef041ad66 100644
@@ -42,16 +42,9 @@ int mdiobus_register_device(struct mdio_device *mdiodev)
 		return -EBUSY;
 
 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
-		err = mdio_device_register_gpiod(mdiodev);
-		if (err)
-			return err;
-
 		err = mdio_device_register_reset(mdiodev);
-		if (err) {
-			gpiod_put(mdiodev->reset_gpio);
-			mdiodev->reset_gpio = NULL;
+		if (err)
 			return err;
-		}
 
 		/* Assert the reset signal */
 		mdio_device_reset(mdiodev, 1);
@@ -68,8 +61,7 @@ int mdiobus_unregister_device(struct mdio_device *mdiodev)
 	if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
 		return -EINVAL;
 
-	gpiod_put(mdiodev->reset_gpio);
-	reset_control_put(mdiodev->reset_ctrl);
+	mdio_device_unregister_reset(mdiodev);
 
 	mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
 
drivers/net/phy/mdio_device.c +31 -9
index 0e04bddd3..a4d9c6ccf 100644
@@ -119,8 +119,17 @@ void mdio_device_remove(struct mdio_device *mdiodev)
 }
 EXPORT_SYMBOL(mdio_device_remove);
 
-int mdio_device_register_gpiod(struct mdio_device *mdiodev)
+/**
+ * mdio_device_register_reset - Read and initialize the reset properties of
+ *				an mdio device
+ * @mdiodev: mdio_device structure
+ *
+ * Return: Zero if successful, negative error code on failure
+ */
+int mdio_device_register_reset(struct mdio_device *mdiodev)
 {
+	struct reset_control *reset;
+
 	/* Deassert the optional reset signal */
 	mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
 						 "reset", GPIOD_OUT_LOW);
@@ -130,22 +139,35 @@ int mdio_device_register_gpiod(struct mdio_device *mdiodev)
 	if (mdiodev->reset_gpio)
 		gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
 
-	return 0;
-}
-
-int mdio_device_register_reset(struct mdio_device *mdiodev)
-{
-	struct reset_control *reset;
-
 	reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
-	if (IS_ERR(reset))
+	if (IS_ERR(reset)) {
+		gpiod_put(mdiodev->reset_gpio);
+		mdiodev->reset_gpio = NULL;
 		return PTR_ERR(reset);
+	}
 
 	mdiodev->reset_ctrl = reset;
 
+	/* Read optional firmware properties */
+	fwnode_property_read_u32(dev_fwnode(&mdiodev->dev), "reset-assert-us",
+				 &mdiodev->reset_assert_delay);
+	fwnode_property_read_u32(dev_fwnode(&mdiodev->dev), "reset-deassert-us",
+				 &mdiodev->reset_deassert_delay);
+
 	return 0;
 }
 
+/**
+ * mdio_device_unregister_reset - uninitialize the reset properties of
+ *				  an mdio device
+ * @mdiodev: mdio_device structure
+ */
+void mdio_device_unregister_reset(struct mdio_device *mdiodev)
+{
+	gpiod_put(mdiodev->reset_gpio);
+	reset_control_put(mdiodev->reset_ctrl);
+}
+
 void mdio_device_reset(struct mdio_device *mdiodev, int value)
 {
 	unsigned int d;
Signature · 1 line
2.39.5
#3 lore raw

PATCH v3 3/3 net: mdio: improve reset handling in mdio_device.c

In reply to Buday Csaba

Change fwnode_property_read_u32() in mdio_device_register_reset() to device_property_read_u32(), which is more appropriate here.

Make mdio_device_unregister_reset() truly reverse mdio_device_register_reset() by setting the internal fields to their default values.

---
V2 -> V3: no changes
V1 -> V2: rebase, leak fix removed, since it is already in base

---
 drivers/net/phy/mdio_device.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/phy/mdio_device.c +6 -2
index a4d9c6ccf..fd0e16dbc 100644
@@ -149,9 +149,9 @@ int mdio_device_register_reset(struct mdio_device *mdiodev)
 	mdiodev->reset_ctrl = reset;
 
 	/* Read optional firmware properties */
-	fwnode_property_read_u32(dev_fwnode(&mdiodev->dev), "reset-assert-us",
+	device_property_read_u32(&mdiodev->dev, "reset-assert-us",
 				 &mdiodev->reset_assert_delay);
-	fwnode_property_read_u32(dev_fwnode(&mdiodev->dev), "reset-deassert-us",
+	device_property_read_u32(&mdiodev->dev, "reset-deassert-us",
 				 &mdiodev->reset_deassert_delay);
 
 	return 0;
@@ -165,7 +165,11 @@ int mdio_device_register_reset(struct mdio_device *mdiodev)
 void mdio_device_unregister_reset(struct mdio_device *mdiodev)
 {
 	gpiod_put(mdiodev->reset_gpio);
+	mdiodev->reset_gpio = NULL;
 	reset_control_put(mdiodev->reset_ctrl);
+	mdiodev->reset_ctrl = NULL;
+	mdiodev->reset_assert_delay = 0;
+	mdiodev->reset_deassert_delay = 0;
 }
 
 void mdio_device_reset(struct mdio_device *mdiodev, int value)
Signature · 1 line
2.39.5
#4 lore raw
In reply to Buday Csaba
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Tue, 18 Nov 2025 14:58:51 +0100 you wrote:

This patchset refactors and slightly improves the reset handling of mdio_device.

The patches were split from a larger series, discussed previously in the links below.

The difference between v2 and v3, is that the helper function declarations have been moved to a new header file: drivers/net/phy/mdio-private.h See links for the previous versions, and for the now separate leak fix.

[...]

Here is the summary with links:
- [net-next,v3,1/3] net: mdio: move device reset functions to mdio_device.c

    https://git.kernel.org/netdev/net-next/c/02aeff20e8f5
  - [net-next,v3,2/3] net: mdio: common handling of phy device reset properties
    https://git.kernel.org/netdev/net-next/c/acde7ad968f6
  - [net-next,v3,3/3] net: mdio: improve reset handling in mdio_device.c
    https://git.kernel.org/netdev/net-next/c/e5a440bf020e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html