diff mbox series

[RFC,net-next,v2,04/11] net: mdio: fwnode: convert fwnode_mdiobus_register() for fwnode

Message ID 20220331092533.348626-5-clement.leger@bootlin.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series add fwnode based mdiobus registration | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 8
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 9 this patch: 6
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 150 lines checked
netdev/kdoc fail Errors and warnings before: 1 this patch: 2
netdev/source_inline success Was 0 now: 0

Commit Message

Clément Léger March 31, 2022, 9:25 a.m. UTC
First version was added as a simple copy of of_mdiobus_register() to
allow compiling and being able to see the diff of modifications that
are going to be done. This commits convert the code that was imported
to handle a fwnode_handle properly.

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
---
 drivers/net/mdio/fwnode_mdio.c | 76 ++++++++++++++++++++++------------
 1 file changed, 50 insertions(+), 26 deletions(-)
diff mbox series

Patch

diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
index 38c873c49ecf..319cccd0edeb 100644
--- a/drivers/net/mdio/fwnode_mdio.c
+++ b/drivers/net/mdio/fwnode_mdio.c
@@ -146,10 +146,9 @@  int fwnode_mdiobus_register_phy(struct mii_bus *bus,
 }
 EXPORT_SYMBOL(fwnode_mdiobus_register_phy);
 
-static int of_mdiobus_register_device(struct mii_bus *mdio,
-				      struct device_node *child, u32 addr)
+static int fwnode_mdiobus_register_device(struct mii_bus *mdio,
+					  struct fwnode_handle *child, u32 addr)
 {
-	struct fwnode_handle *fwnode = of_fwnode_handle(child);
 	struct mdio_device *mdiodev;
 	int rc;
 
@@ -160,18 +159,18 @@  static int of_mdiobus_register_device(struct mii_bus *mdio,
 	/* Associate the OF node with the device structure so it
 	 * can be looked up later.
 	 */
-	fwnode_handle_get(fwnode);
-	device_set_node(&mdiodev->dev, fwnode);
+	fwnode_handle_get(child);
+	device_set_node(&mdiodev->dev, child);
 
 	/* All data is now stored in the mdiodev struct; register it. */
 	rc = mdio_device_register(mdiodev);
 	if (rc) {
 		mdio_device_free(mdiodev);
-		of_node_put(child);
+		fwnode_handle_put(child);
 		return rc;
 	}
 
-	dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n",
+	dev_dbg(&mdio->dev, "registered mdio device %pfwP at address %i\n",
 		child, addr);
 	return 0;
 }
@@ -186,26 +185,51 @@  static int of_mdiobus_register_device(struct mii_bus *mdio,
  * A device which is not a phy is expected to have a compatible string
  * indicating what sort of device it is.
  */
-bool fwnode_mdiobus_child_is_phy(struct device_node *child)
+bool fwnode_mdiobus_child_is_phy(struct fwnode_handle *child)
 {
 	u32 phy_id;
 
-	if (of_get_phy_id(child, &phy_id) != -EINVAL)
+	if (fwnode_get_phy_id(child, &phy_id) != -EINVAL)
 		return true;
 
-	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
+	if (fwnode_property_match_string(child, "compatible",
+					 "ethernet-phy-ieee802.3-c45") >= 0)
 		return true;
 
-	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
+	if (fwnode_property_match_string(child, "compatible",
+					 "ethernet-phy-ieee802.3-c22") >= 0)
 		return true;
 
-	if (!of_find_property(child, "compatible", NULL))
+	if (!fwnode_property_present(child, "compatible"))
 		return true;
 
 	return false;
 }
 EXPORT_SYMBOL(fwnode_mdiobus_child_is_phy);
 
+int fwnode_mdio_parse_addr(struct device *dev,
+			   const struct fwnode_handle *fwnode)
+{
+	u32 addr;
+	int ret;
+
+	ret = fwnode_property_read_u32(fwnode, "reg", &addr);
+	if (ret < 0) {
+		dev_err(dev, "%pfwP has invalid PHY address\n", fwnode);
+		return ret;
+	}
+
+	/* A PHY must have a reg property in the range [0-31] */
+	if (addr >= PHY_MAX_ADDR) {
+		dev_err(dev, "%pfwP PHY address %i is too large\n",
+			fwnode, addr);
+		return -EINVAL;
+	}
+
+	return addr;
+}
+EXPORT_SYMBOL(fwnode_mdio_parse_addr);
+
 /**
  * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  * @mdio: pointer to mii_bus structure
@@ -214,29 +238,31 @@  EXPORT_SYMBOL(fwnode_mdiobus_child_is_phy);
  * This function registers the mii_bus structure and registers a phy_device
  * for each child node of @np.
  */
-int fwnode_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
+int fwnode_mdiobus_register(struct mii_bus *mdio, struct fwnode_handle *fwnode)
 {
-	struct device_node *child;
+	struct fwnode_handle *child;
 	int addr, rc;
 
-	if (!np)
+	if (!fwnode)
 		return mdiobus_register(mdio);
 
 	/* Do not continue if the node is disabled */
-	if (!of_device_is_available(np))
+	if (!fwnode_device_is_available(fwnode))
 		return -ENODEV;
 
 	/* Mask out all PHYs from auto probing.  Instead the PHYs listed in
 	 * the device tree are populated after the bus has been registered */
 	mdio->phy_mask = ~0;
 
-	device_set_node(&mdio->dev, of_fwnode_handle(np));
+	device_set_node(&mdio->dev, fwnode);
 
 	/* Get bus level PHY reset GPIO details */
 	mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
-	of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
+	fwnode_property_read_u32(fwnode, "reset-delay-us",
+				 &mdio->reset_delay_us);
 	mdio->reset_post_delay_us = 0;
-	of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us);
+	fwnode_property_read_u32(fwnode, "reset-post-delay-us",
+				 &mdio->reset_post_delay_us);
 
 	/* Register the MDIO bus */
 	rc = mdiobus_register(mdio);
@@ -244,17 +270,15 @@  int fwnode_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
 		return rc;
 
 	/* Loop over the child nodes and register a phy_device for each phy */
-	for_each_available_child_of_node(np, child) {
-		addr = of_mdio_parse_addr(&mdio->dev, child);
+	fwnode_for_each_available_child_node(fwnode, child) {
+		addr = fwnode_mdio_parse_addr(&mdio->dev, child);
 		if (addr < 0)
 			continue;
 
-		if (of_mdiobus_child_is_phy(child))
-			rc = fwnode_mdiobus_register_phy(mdio,
-							 of_fwnode_handle(child),
-							 addr);
+		if (fwnode_mdiobus_child_is_phy(child))
+			rc = fwnode_mdiobus_register_phy(mdio, child, addr);
 		else
-			rc = of_mdiobus_register_device(mdio, child, addr);
+			rc = fwnode_mdiobus_register_device(mdio, child, addr);
 
 		if (rc == -ENODEV)
 			dev_err(&mdio->dev,