Skip to content

Commit d02aa18

Browse files
geertuvinodkoul
authored andcommitted
phy: Add devm_of_phy_optional_get() helper
Add an optional variant of devm_of_phy_get() that also takes care of printing real errors, so drivers no longer have to open-code this operation. Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be> Link: https://lore.kernel.org/r/4cd0069bcff424ffc5c3a102397c02370b91985b.1674584626.git.geert+renesas@glider.be Signed-off-by: Vinod Koul <vkoul@kernel.org>
1 parent 59c3d3d commit d02aa18

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Documentation/driver-api/phy/phy.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ it. This framework provides the following APIs to get a reference to the PHY.
108108
const char *string);
109109
struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
110110
const char *con_id);
111+
struct phy *devm_of_phy_optional_get(struct device *dev,
112+
struct device_node *np,
113+
const char *con_id);
111114
struct phy *devm_of_phy_get_by_index(struct device *dev,
112115
struct device_node *np,
113116
int index);
@@ -119,8 +122,8 @@ non-dt boot, it should contain the label of the PHY. The two
119122
devm_phy_get associates the device with the PHY using devres on
120123
successful PHY get. On driver detach, release function is invoked on
121124
the devres data and devres data is freed.
122-
devm_phy_optional_get should be used when the phy is optional. This
123-
function will never return -ENODEV, but instead returns NULL when
125+
The _optional_get variants should be used when the phy is optional. These
126+
functions will never return -ENODEV, but instead return NULL when
124127
the phy cannot be found.
125128
Some generic drivers, such as ehci, may use multiple phys. In this case,
126129
devm_of_phy_get or devm_of_phy_get_by_index can be used to get a phy

drivers/phy/phy-core.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,36 @@ struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
858858
}
859859
EXPORT_SYMBOL_GPL(devm_of_phy_get);
860860

861+
/**
862+
* devm_of_phy_optional_get() - lookup and obtain a reference to an optional
863+
* phy.
864+
* @dev: device that requests this phy
865+
* @np: node containing the phy
866+
* @con_id: name of the phy from device's point of view
867+
*
868+
* Gets the phy using of_phy_get(), and associates a device with it using
869+
* devres. On driver detach, release function is invoked on the devres data,
870+
* then, devres data is freed. This differs to devm_of_phy_get() in
871+
* that if the phy does not exist, it is not considered an error and
872+
* -ENODEV will not be returned. Instead the NULL phy is returned,
873+
* which can be passed to all other phy consumer calls.
874+
*/
875+
struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np,
876+
const char *con_id)
877+
{
878+
struct phy *phy = devm_of_phy_get(dev, np, con_id);
879+
880+
if (PTR_ERR(phy) == -ENODEV)
881+
phy = NULL;
882+
883+
if (IS_ERR(phy))
884+
dev_err_probe(dev, PTR_ERR(phy), "failed to get PHY %pOF:%s",
885+
np, con_id);
886+
887+
return phy;
888+
}
889+
EXPORT_SYMBOL_GPL(devm_of_phy_optional_get);
890+
861891
/**
862892
* devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
863893
* @dev: device that requests this phy

include/linux/phy/phy.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ struct phy *devm_phy_get(struct device *dev, const char *string);
254254
struct phy *devm_phy_optional_get(struct device *dev, const char *string);
255255
struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
256256
const char *con_id);
257+
struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np,
258+
const char *con_id);
257259
struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
258260
int index);
259261
void of_phy_put(struct phy *phy);
@@ -443,6 +445,13 @@ static inline struct phy *devm_of_phy_get(struct device *dev,
443445
return ERR_PTR(-ENOSYS);
444446
}
445447

448+
static inline struct phy *devm_of_phy_optional_get(struct device *dev,
449+
struct device_node *np,
450+
const char *con_id)
451+
{
452+
return NULL;
453+
}
454+
446455
static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
447456
struct device_node *np,
448457
int index)

0 commit comments

Comments
 (0)