net: phy: Introduce phy related fwnode functions
authorCalvin Johnson <calvin.johnson@oss.nxp.com>
Fri, 11 Jun 2021 10:53:49 +0000 (13:53 +0300)
committerDavid S. Miller <davem@davemloft.net>
Fri, 11 Jun 2021 20:08:52 +0000 (13:08 -0700)
Define fwnode_phy_find_device() to iterate an mdiobus and find the
phy device of the provided phy fwnode. Additionally define
device_phy_find_device() to find phy device of provided device.

Define fwnode_get_phy_node() to get phy_node using named reference.

Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
Acked-by: Grant Likely <grant.likely@arm.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/phy/phy_device.c
include/linux/phy.h

index dca454b..786f464 100644 (file)
@@ -9,6 +9,7 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/acpi.h>
 #include <linux/bitmap.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
@@ -2899,6 +2900,67 @@ struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
 EXPORT_SYMBOL(fwnode_mdio_find_device);
 
 /**
+ * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
+ *
+ * @phy_fwnode: Pointer to the phy's fwnode.
+ *
+ * If successful, returns a pointer to the phy_device with the embedded
+ * struct device refcount incremented by one, or NULL on failure.
+ */
+struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
+{
+       struct mdio_device *mdiodev;
+
+       mdiodev = fwnode_mdio_find_device(phy_fwnode);
+       if (!mdiodev)
+               return NULL;
+
+       if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
+               return to_phy_device(&mdiodev->dev);
+
+       put_device(&mdiodev->dev);
+
+       return NULL;
+}
+EXPORT_SYMBOL(fwnode_phy_find_device);
+
+/**
+ * device_phy_find_device - For the given device, get the phy_device
+ * @dev: Pointer to the given device
+ *
+ * Refer return conditions of fwnode_phy_find_device().
+ */
+struct phy_device *device_phy_find_device(struct device *dev)
+{
+       return fwnode_phy_find_device(dev_fwnode(dev));
+}
+EXPORT_SYMBOL_GPL(device_phy_find_device);
+
+/**
+ * fwnode_get_phy_node - Get the phy_node using the named reference.
+ * @fwnode: Pointer to fwnode from which phy_node has to be obtained.
+ *
+ * Refer return conditions of fwnode_find_reference().
+ * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
+ * and "phy-device" are not supported in ACPI. DT supports all the three
+ * named references to the phy node.
+ */
+struct fwnode_handle *fwnode_get_phy_node(struct fwnode_handle *fwnode)
+{
+       struct fwnode_handle *phy_node;
+
+       /* Only phy-handle is used for ACPI */
+       phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
+       if (is_acpi_node(fwnode) || !IS_ERR(phy_node))
+               return phy_node;
+       phy_node = fwnode_find_reference(fwnode, "phy", 0);
+       if (IS_ERR(phy_node))
+               phy_node = fwnode_find_reference(fwnode, "phy-device", 0);
+       return phy_node;
+}
+EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
+
+/**
  * phy_probe - probe and init a PHY device
  * @dev: device to probe and init
  *
index 7aa97f4..f9b5fb0 100644 (file)
@@ -1378,6 +1378,9 @@ struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
                                     struct phy_c45_device_ids *c45_ids);
 #if IS_ENABLED(CONFIG_PHYLIB)
 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode);
+struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode);
+struct phy_device *device_phy_find_device(struct device *dev);
+struct fwnode_handle *fwnode_get_phy_node(struct fwnode_handle *fwnode);
 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
 int phy_device_register(struct phy_device *phy);
 void phy_device_free(struct phy_device *phydev);
@@ -1389,6 +1392,23 @@ struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
 }
 
 static inline
+struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
+{
+       return NULL;
+}
+
+static inline struct phy_device *device_phy_find_device(struct device *dev)
+{
+       return NULL;
+}
+
+static inline
+struct fwnode_handle *fwnode_get_phy_node(struct fwnode_handle *fwnode)
+{
+       return NULL;
+}
+
+static inline
 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
 {
        return NULL;