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

From: Calvin Johnson
Date: Wed Jul 01 2020 - 02:13:28 EST


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@xxxxxxxxxxx>
---

Changes in v2: None

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

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,
--
2.17.1