Skip to content

Commit

Permalink
Merge branch 'of_get_available_child_by_name'
Browse files Browse the repository at this point in the history
Biju Das says:

====================
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/
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Feb 7, 2025
2 parents 26db4db + 0584a91 commit 233a2b1
Showing 8 changed files with 44 additions and 34 deletions.
8 changes: 3 additions & 5 deletions drivers/net/dsa/rzn1_a5psw.c
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 1 addition & 5 deletions drivers/net/dsa/sja1105/sja1105_mdio.c
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 1 addition & 6 deletions drivers/net/ethernet/actions/owl-emac.c
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 1 addition & 6 deletions drivers/net/ethernet/ibm/emac/core.c
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 1 addition & 6 deletions drivers/net/ethernet/mediatek/mtk_eth_soc.c
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 1 addition & 6 deletions drivers/net/ethernet/mediatek/mtk_star_emac.c
Original file line number Diff line number Diff line change
@@ -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;
27 changes: 27 additions & 0 deletions drivers/of/base.c
Original file line number Diff line number Diff line change
@@ -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)
{
9 changes: 9 additions & 0 deletions include/linux/of.h
Original file line number Diff line number Diff line change
@@ -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)
{

0 comments on commit 233a2b1

Please sign in to comment.