Skip to contentSkip to conversation

Add of_get_available_child_by_name()

PATCH Applied Biju Das 4 participants 18 messages Feb 5, 2025 – Feb 7, 2025

Subsystems (9)
lore
Share & tools
lore

Biju Das

author
#0 lore raw

v2 cover Add of_get_available_child_by_name()

There are lot of net drivers using of_get_child_by_name() followed by of_device_is_available() to find the available child node by name for a given parent. Provide a helper for these users to simplify the code.

v1->v2:
* Make it as a series as per [1] to cover the dependency.
* Added Rb tag from Rob for patch#1 and this patch can be merged through net as it is the main user.
* Updated all the patches with patch suffix net-next
* Dropped _free() usage.

[1]
https://lore.kernel.org/all/CAL_JsqLo4uSGYMcLXN=0iSUMHdW8RaGCY+o8ThQHq3_eUTV9wQ@mail.gmail.com/

Biju Das (7):
of: base: Add of_get_available_child_by_name()
net: dsa: rzn1_a5psw: Use of_get_available_child_by_name()
net: dsa: sja1105: Use of_get_available_child_by_name()
net: ethernet: mtk-star-emac: Use of_get_available_child_by_name()
net: ethernet: mtk_eth_soc: Use of_get_available_child_by_name()
net: ethernet: actions: Use of_get_available_child_by_name()
net: ibm: emac: Use of_get_available_child_by_name()

 drivers/net/dsa/rzn1_a5psw.c                  |  8 +++---
 drivers/net/dsa/sja1105/sja1105_mdio.c        |  6 +----
 drivers/net/ethernet/actions/owl-emac.c       |  7 +----
 drivers/net/ethernet/ibm/emac/core.c          |  7 +----
 drivers/net/ethernet/mediatek/mtk_eth_soc.c   |  7 +----
 drivers/net/ethernet/mediatek/mtk_star_emac.c |  7 +----
 drivers/of/base.c                             | 27 +++++++++++++++++++
 include/linux/of.h                            |  9 +++++++
 8 files changed, 44 insertions(+), 34 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/7 of: base: Add of_get_available_child_by_name()

There are lot of drivers using of_get_child_by_name() followed by of_device_is_available() to find the available child node by name for a given parent. Provide a helper for these users to simplify the code.

---
previous v2->v2
* Added Rb tag from Rob.
v1->previous v2:
* Updated commit description.
* Updated kerneldoc comment block
* Avoided code duplication by using of_get_child_by_name().

---
 drivers/of/base.c  | 27 +++++++++++++++++++++++++++
 include/linux/of.h |  9 +++++++++
 2 files changed, 36 insertions(+)
drivers/of/base.c +27 -0
index af6c68bbb427..e37b088f1fad 100644
@@ -824,6 +824,33 @@ struct device_node *of_get_child_by_name(const struct device_node *node,
 }
 EXPORT_SYMBOL(of_get_child_by_name);
 
+/**
+ * of_get_available_child_by_name - Find the available child node by name for a given parent
+ * @node:	parent node
+ * @name:	child name to look for.
+ *
+ * This function looks for child node for given matching name and checks the
+ * device's availability for use.
+ *
+ * Return: A node pointer if found, with refcount incremented, use
+ * of_node_put() on it when done.
+ * Returns NULL if node is not found.
+ */
+struct device_node *of_get_available_child_by_name(const struct device_node *node,
+						   const char *name)
+{
+	struct device_node *child;
+
+	child = of_get_child_by_name(node, name);
+	if (child && !of_device_is_available(child)) {
+		of_node_put(child);
+		return NULL;
+	}
+
+	return child;
+}
+EXPORT_SYMBOL(of_get_available_child_by_name);
+
 struct device_node *__of_find_node_by_path(const struct device_node *parent,
 						const char *path)
 {
include/linux/of.h +9 -0
index eaf0e2a2b75c..9d6b8a61607f 100644
@@ -301,6 +301,8 @@ extern struct device_node *of_get_compatible_child(const struct device_node *par
 					const char *compatible);
 extern struct device_node *of_get_child_by_name(const struct device_node *node,
 					const char *name);
+extern struct device_node *of_get_available_child_by_name(const struct device_node *node,
+							  const char *name);
 
 /* cache lookup */
 extern struct device_node *of_find_next_cache_node(const struct device_node *);
@@ -578,6 +580,13 @@ static inline struct device_node *of_get_child_by_name(
 	return NULL;
 }
 
+static inline struct device_node *of_get_available_child_by_name(
+					const struct device_node *node,
+					const char *name)
+{
+	return NULL;
+}
+
 static inline int of_device_is_compatible(const struct device_node *device,
 					  const char *name)
 {
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/7
#2 lore raw

Re: [PATCH net-next v2 1/7] of: base: Add of_get_available_child_by_name()

Quoted 7 lines · Biju Das

There are lot of drivers using of_get_child_by_name() followed by of_device_is_available() to find the available child node by name for a given parent. Provide a helper for these users to simplify the code.

Quoted 7 lines

---
previous v2->v2
* Added Rb tag from Rob.
v1->previous v2:
* Updated commit description.
* Updated kerneldoc comment block
* Avoided code duplication by using of_get_child_by_name().

author
#3 lore raw

PATCH v2 2/7 net: dsa: rzn1_a5psw: Use of_get_available_child_by_name()

In reply to Biju Das

Simplify a5psw_probe() by using of_get_available_child_by_name().

While at it, move of_node_put(mdio) inside the if block to avoid code duplication.

---
v1->v2:
* Rebased and added patch suffix net-next.

---
 drivers/net/dsa/rzn1_a5psw.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/net/dsa/rzn1_a5psw.c +3 -5
index 66974379334a..31ea8130a495 100644
@@ -1248,18 +1248,16 @@ static int a5psw_probe(struct platform_device *pdev)
 	if (ret)
 		goto clk_disable;
 
-	mdio = of_get_child_by_name(dev->of_node, "mdio");
-	if (of_device_is_available(mdio)) {
+	mdio = of_get_available_child_by_name(dev->of_node, "mdio");
+	if (mdio) {
 		ret = a5psw_probe_mdio(a5psw, mdio);
+		of_node_put(mdio);
 		if (ret) {
-			of_node_put(mdio);
 			dev_err(dev, "Failed to register MDIO: %d\n", ret);
 			goto hclk_disable;
 		}
 	}
 
-	of_node_put(mdio);
-
 	ds = &a5psw->ds;
 	ds->dev = dev;
 	ds->num_ports = A5PSW_PORTS_NUM;
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 2/7
#4 lore raw

Re: [PATCH net-next v2 2/7] net: dsa: rzn1_a5psw: Use of_get_available_child_by_name()

Quoted 9 lines · Biju Das

Simplify a5psw_probe() by using of_get_available_child_by_name().

While at it, move of_node_put(mdio) inside the if block to avoid code duplication.

---
v1->v2:
* Rebased and added patch suffix net-next.

author
#6 lore raw

PATCH v2 3/7 net: dsa: sja1105: Use of_get_available_child_by_name()

In reply to Biju Das

Use the helper of_get_available_child_by_name() to simplify sja1105_mdiobus_register().

---
v1->v2:
* Dropped using _free().

---
 drivers/net/dsa/sja1105/sja1105_mdio.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/net/dsa/sja1105/sja1105_mdio.c +1 -5
index 84b7169f2974..8d535c033cef 100644
@@ -468,13 +468,10 @@ int sja1105_mdiobus_register(struct dsa_switch *ds)
 	if (rc)
 		return rc;
 
-	mdio_node = of_get_child_by_name(switch_node, "mdios");
+	mdio_node = of_get_available_child_by_name(switch_node, "mdios");
 	if (!mdio_node)
 		return 0;
 
-	if (!of_device_is_available(mdio_node))
-		goto out_put_mdio_node;
-
 	if (regs->mdio_100base_tx != SJA1105_RSV_ADDR) {
 		rc = sja1105_mdiobus_base_tx_register(priv, mdio_node);
 		if (rc)
@@ -487,7 +484,6 @@ int sja1105_mdiobus_register(struct dsa_switch *ds)
 			goto err_free_base_tx_mdiobus;
 	}
 
-out_put_mdio_node:
 	of_node_put(mdio_node);
 
 	return 0;
Signature · 1 line
2.43.0
author
#9 lore raw

PATCH v2 4/7 net: ethernet: mtk-star-emac: Use of_get_available_child_by_name()

In reply to Biju Das

Use the helper of_get_available_child_by_name() to simplify mtk_star_mdio_init().

---
v1->v2:
* Dropped using _free()

---
 drivers/net/ethernet/mediatek/mtk_star_emac.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/net/ethernet/mediatek/mtk_star_emac.c +1 -6
index 25989c79c92e..76f202d7f055 100644
@@ -1427,15 +1427,10 @@ static int mtk_star_mdio_init(struct net_device *ndev)
 
 	of_node = dev->of_node;
 
-	mdio_node = of_get_child_by_name(of_node, "mdio");
+	mdio_node = of_get_available_child_by_name(of_node, "mdio");
 	if (!mdio_node)
 		return -ENODEV;
 
-	if (!of_device_is_available(mdio_node)) {
-		ret = -ENODEV;
-		goto out_put_node;
-	}
-
 	priv->mii = devm_mdiobus_alloc(dev);
 	if (!priv->mii) {
 		ret = -ENOMEM;
Signature · 1 line
2.43.0
author
#11 lore raw

PATCH v2 5/7 net: ethernet: mtk_eth_soc: Use of_get_available_child_by_name()

In reply to Biju Das

Use the helper of_get_available_child_by_name() to simplify mtk_mdio_init().

---
v1->v2:
* Dropped using _free().

---
 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/net/ethernet/mediatek/mtk_eth_soc.c +1 -6
index 53485142938c..0ad965ced5ef 100644
@@ -830,17 +830,12 @@ static int mtk_mdio_init(struct mtk_eth *eth)
 	int ret;
 	u32 val;
 
-	mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
+	mii_np = of_get_available_child_by_name(eth->dev->of_node, "mdio-bus");
 	if (!mii_np) {
 		dev_err(eth->dev, "no %s child node found", "mdio-bus");
 		return -ENODEV;
 	}
 
-	if (!of_device_is_available(mii_np)) {
-		ret = -ENODEV;
-		goto err_put_node;
-	}
-
 	eth->mii_bus = devm_mdiobus_alloc(eth->dev);
 	if (!eth->mii_bus) {
 		ret = -ENOMEM;
Signature · 1 line
2.43.0
author
#13 lore raw

PATCH v2 6/7 net: ethernet: actions: Use of_get_available_child_by_name()

In reply to Biju Das

Use the helper of_get_available_child_by_name() to simplify owl_emac_mdio_init().

---
previous v2->v2:
* Dropped using _free().
v1-> previous v2:
* Dropped duplicate mdio_node declaration.

---
 drivers/net/ethernet/actions/owl-emac.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/net/ethernet/actions/owl-emac.c +1 -6
index 115f48b3342c..0a08da799255 100644
@@ -1325,15 +1325,10 @@ static int owl_emac_mdio_init(struct net_device *netdev)
 	struct device_node *mdio_node;
 	int ret;
 
-	mdio_node = of_get_child_by_name(dev->of_node, "mdio");
+	mdio_node = of_get_available_child_by_name(dev->of_node, "mdio");
 	if (!mdio_node)
 		return -ENODEV;
 
-	if (!of_device_is_available(mdio_node)) {
-		ret = -ENODEV;
-		goto err_put_node;
-	}
-
 	priv->mii = devm_mdiobus_alloc(dev);
 	if (!priv->mii) {
 		ret = -ENOMEM;
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 6/7
#14 lore raw

Re: [PATCH net-next v2 6/7] net: ethernet: actions: Use of_get_available_child_by_name()

Quoted 9 lines · Biju Das

Use the helper of_get_available_child_by_name() to simplify owl_emac_mdio_init().

---
previous v2->v2:
* Dropped using _free().
v1-> previous v2:
* Dropped duplicate mdio_node declaration.

author
#15 lore raw

PATCH v2 7/7 net: ibm: emac: Use of_get_available_child_by_name()

In reply to Biju Das

Use the helper of_get_available_child_by_name() to simplify emac_dt_mdio_probe().

---
v1->v2:
* Dropped using _free()

---
 drivers/net/ethernet/ibm/emac/core.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +1 -6
index 25b8a3556004..417dfa18daae 100644
@@ -2554,17 +2554,12 @@ static int emac_dt_mdio_probe(struct emac_instance *dev)
 	struct mii_bus *bus;
 	int res;
 
-	mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
+	mii_np = of_get_available_child_by_name(dev->ofdev->dev.of_node, "mdio");
 	if (!mii_np) {
 		dev_err(&dev->ofdev->dev, "no mdio definition found.");
 		return -ENODEV;
 	}
 
-	if (!of_device_is_available(mii_np)) {
-		res = -ENODEV;
-		goto put_node;
-	}
-
 	bus = devm_mdiobus_alloc(&dev->ofdev->dev);
 	if (!bus) {
 		res = -ENOMEM;
Signature · 1 line
2.43.0
#17 lore raw
In reply to Biju Das
This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Hello:

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

Quoted 4 lines · On Wed, 5 Feb 2025 12:42:20 +0000 you wrote:

There are lot of net drivers using of_get_child_by_name() followed by of_device_is_available() to find the available child node by name for a given parent. Provide a helper for these users to simplify the code.

Quoted 8 lines

v1->v2:
* Make it as a series as per [1] to cover the dependency.
* Added Rb tag from Rob for patch#1 and this patch can be merged through net as it is the main user.
* Updated all the patches with patch suffix net-next
* Dropped _free() usage.

[...]

Here is the summary with links:
- [net-next,v2,1/7] of: base: Add of_get_available_child_by_name()

    https://git.kernel.org/netdev/net-next/c/8d3bbe4355ad
  - [net-next,v2,2/7] net: dsa: rzn1_a5psw: Use of_get_available_child_by_name()
    https://git.kernel.org/netdev/net-next/c/46df19a8dfdf
  - [net-next,v2,3/7] net: dsa: sja1105: Use of_get_available_child_by_name()
    https://git.kernel.org/netdev/net-next/c/a76568865c15
  - [net-next,v2,4/7] net: ethernet: mtk-star-emac: Use of_get_available_child_by_name()
    https://git.kernel.org/netdev/net-next/c/876e52b2d3f4
  - [net-next,v2,5/7] net: ethernet: mtk_eth_soc: Use of_get_available_child_by_name()
    https://git.kernel.org/netdev/net-next/c/1364004b5b91
  - [net-next,v2,6/7] net: ethernet: actions: Use of_get_available_child_by_name()
    https://git.kernel.org/netdev/net-next/c/76c82eb04332
  - [net-next,v2,7/7] net: ibm: emac: Use of_get_available_child_by_name()
    https://git.kernel.org/netdev/net-next/c/0584a917a209

You are awesome, thank you!

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