diff mbox series

[net-next,v2,1/3] net: phy: introduce find_phy_device()

Message ID 20200701061233.31120-2-calvin.johnson@oss.nxp.com
State Changes Requested
Delegated to: David Miller
Headers show
Series [net-next,v2,1/3] net: phy: introduce find_phy_device() | expand

Commit Message

Calvin Johnson July 1, 2020, 6:12 a.m. UTC
The PHYs on a mdiobus are probed and registered using mdiobus_register().
Later, for connecting these PHYs to MAC, the PHYs registered on the
mdiobus have to be referenced.

For each MAC node, a property "mdio-handle" is used to reference the
MDIO bus on which the PHYs are registered. On getting hold of the MDIO
bus, use find_phy_device() to get the PHY connected to the MAC.

Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
---

Changes in v2: None

 drivers/net/phy/phy_device.c | 25 +++++++++++++++++++++++++
 include/linux/phy.h          |  1 +
 2 files changed, 26 insertions(+)

Comments

Andy Shevchenko July 1, 2020, 10:23 a.m. UTC | #1
On Wed, Jul 1, 2020 at 9:13 AM Calvin Johnson
<calvin.johnson@oss.nxp.com> wrote:
>
> The PHYs on a mdiobus are probed and registered using mdiobus_register().
> Later, for connecting these PHYs to MAC, the PHYs registered on the
> mdiobus have to be referenced.
>
> For each MAC node, a property "mdio-handle" is used to reference the
> MDIO bus on which the PHYs are registered. On getting hold of the MDIO
> bus, use find_phy_device() to get the PHY connected to the MAC.

...

> +       struct platform_device *pdev;

This...

> +       fwnode_mdio = fwnode_find_reference(fwnode, "mdio-handle", 0);
> +       dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode_mdio);

> +       if (IS_ERR_OR_NULL(dev))

IS_ERR()?!

> +               return NULL;

> +       pdev =  to_platform_device(dev);
> +       mdio = platform_get_drvdata(pdev);

...and this can be simple:

mdio = dev_get_drvdata(dev);

> +       err = fwnode_property_read_u32(fwnode, "phy-channel", &addr);
> +       if (err < 0 || addr < 0 || addr >= PHY_MAX_ADDR)
> +               return NULL;
Andrew Lunn July 1, 2020, 1:24 p.m. UTC | #2
> +struct phy_device *find_phy_device(struct fwnode_handle *fwnode)

We should consider the naming convention. All phylib phy functions
start with phy_. We already have phy_find_first(), so maybe
phy_find_by_fwnode() to follow the pattern?

> +{
> +	struct fwnode_handle *fwnode_mdio;
> +	struct platform_device *pdev;
> +	struct mii_bus *mdio;
> +	struct device *dev;
> +	int addr;
> +	int err;
> +
> +	fwnode_mdio = fwnode_find_reference(fwnode, "mdio-handle", 0);
> +	dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode_mdio);
> +	if (IS_ERR_OR_NULL(dev))
> +		return NULL;
> +	pdev =  to_platform_device(dev);
> +	mdio = platform_get_drvdata(pdev);

That is a big assumption to make. Please take a look at the
class_find_device_by_*() functions, as used by of_mdio_find_bus(),
mdio_find_bus(), etc.

	Andrew
diff mbox series

Patch

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index eb1068a77ce1..417000197ab1 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -25,6 +25,7 @@ 
 #include <linux/netdevice.h>
 #include <linux/phy.h>
 #include <linux/phy_led_triggers.h>
+#include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/sfp.h>
 #include <linux/skbuff.h>
@@ -966,6 +967,30 @@  struct phy_device *phy_find_first(struct mii_bus *bus)
 }
 EXPORT_SYMBOL(phy_find_first);
 
+struct phy_device *find_phy_device(struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *fwnode_mdio;
+	struct platform_device *pdev;
+	struct mii_bus *mdio;
+	struct device *dev;
+	int addr;
+	int err;
+
+	fwnode_mdio = fwnode_find_reference(fwnode, "mdio-handle", 0);
+	dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode_mdio);
+	if (IS_ERR_OR_NULL(dev))
+		return NULL;
+	pdev =  to_platform_device(dev);
+	mdio = platform_get_drvdata(pdev);
+
+	err = fwnode_property_read_u32(fwnode, "phy-channel", &addr);
+	if (err < 0 || addr < 0 || addr >= PHY_MAX_ADDR)
+		return NULL;
+
+	return mdiobus_get_phy(mdio, addr);
+}
+EXPORT_SYMBOL(find_phy_device);
+
 static void phy_link_change(struct phy_device *phydev, bool up)
 {
 	struct net_device *netdev = phydev->attached_dev;
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 101a48fa6750..ef9d7ca5d7ba 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -1245,6 +1245,7 @@  int phy_sfp_probe(struct phy_device *phydev,
 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
 			      phy_interface_t interface);
 struct phy_device *phy_find_first(struct mii_bus *bus);
+struct phy_device *find_phy_device(struct fwnode_handle *fwnode);
 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 		      u32 flags, phy_interface_t interface);
 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,