[PATCH v5 2/8] phy: core: Add common helper for get phy phandle by index

From: Inochi Amaoto

Date: Tue Nov 24 2026 - 23:37:27 EST


Two phy helpers use index to get phy phandle of a device node,
add a common function for the future reuse.

Signed-off-by: Inochi Amaoto <inochiama@xxxxxxxxx>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
drivers/phy/phy-core.c | 57 ++++++++++++++++++++++++------------------
1 file changed, 33 insertions(+), 24 deletions(-)

diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index b7550ca37e66..efbd7e139dce 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -697,38 +697,55 @@ static struct phy *_of_phy_get(struct device_node *np, int index)
return phy;
}

+/**
+ * _of_phy_get_by_index() - lookup and obtain a reference to a phy using a
+ * device_node by index.
+ * @np: device_node for which to get the phy
+ * @index: index of the phy from device's point of view
+ *
+ * Returns: the phy device, after getting a refcount to it; or
+ * -ENODEV if there is no such phy. The caller is responsible for
+ * calling of_phy_put() to release that count.
+ */
+static struct phy *_of_phy_get_by_index(struct device_node *np, int index)
+{
+ struct phy *phy;
+
+ phy = _of_phy_get(np, index);
+ if (IS_ERR(phy))
+ return phy;
+
+ if (!try_module_get(phy->ops->owner)) {
+ phy = ERR_PTR(-EPROBE_DEFER);
+ return phy;
+ }
+
+ get_device(&phy->dev);
+
+ return phy;
+}
+
/**
* of_phy_get() - lookup and obtain a reference to a phy using a device_node.
* @np: device_node for which to get the phy
* @con_id: name of the phy from device's point of view
*
- * Returns: the phy driver, after getting a refcount to it; or
+ * Returns: the phy device, after getting a refcount to it; or
* -ENODEV if there is no such phy. The caller is responsible for
* calling of_phy_put() to release that count.
*/
struct phy *of_phy_get(struct device_node *np, const char *con_id)
{
- struct phy *phy = NULL;
+ struct phy *phy;
int index = 0;

if (con_id)
index = of_property_match_string(np, "phy-names", con_id);

mutex_lock(&phy_provider_mutex);
-
- phy = _of_phy_get(np, index);
- if (IS_ERR(phy))
- goto out_unlock;
-
- if (!try_module_get(phy->ops->owner)) {
- phy = ERR_PTR(-EPROBE_DEFER);
- goto out_unlock;
- }
-
- get_device(&phy->dev);
-
-out_unlock:
+ phy = _of_phy_get_by_index(np, index);
mutex_unlock(&phy_provider_mutex);
+
return phy;
}
EXPORT_SYMBOL_GPL(of_phy_get);
@@ -1001,20 +1018,12 @@ struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
return ERR_PTR(-ENOMEM);

mutex_lock(&phy_provider_mutex);
- phy = _of_phy_get(np, index);
+ phy = _of_phy_get_by_index(np, index);
if (IS_ERR(phy)) {
devres_free(ptr);
goto out_unlock;
}

- if (!try_module_get(phy->ops->owner)) {
- devres_free(ptr);
- phy = ERR_PTR(-EPROBE_DEFER);
- goto out_unlock;
- }
-
- get_device(&phy->dev);
-
*ptr = phy;
devres_add(dev, ptr);

--
2.56.1