Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Geert Uytterhoeven and committed by
Vinod Koul
d02aa181 59c3d3d0

+44 -2
+5 -2
Documentation/driver-api/phy/phy.rst
··· 108 108 const char *string); 109 109 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, 110 110 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); 111 114 struct phy *devm_of_phy_get_by_index(struct device *dev, 112 115 struct device_node *np, 113 116 int index); ··· 122 119 devm_phy_get associates the device with the PHY using devres on 123 120 successful PHY get. On driver detach, release function is invoked on 124 121 the devres data and devres data is freed. 125 - devm_phy_optional_get should be used when the phy is optional. This 126 - function will never return -ENODEV, but instead returns NULL when 122 + The _optional_get variants should be used when the phy is optional. These 123 + functions will never return -ENODEV, but instead return NULL when 127 124 the phy cannot be found. 128 125 Some generic drivers, such as ehci, may use multiple phys. In this case, 129 126 devm_of_phy_get or devm_of_phy_get_by_index can be used to get a phy
+30
drivers/phy/phy-core.c
··· 859 859 EXPORT_SYMBOL_GPL(devm_of_phy_get); 860 860 861 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 + 891 + /** 862 892 * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index. 863 893 * @dev: device that requests this phy 864 894 * @np: node containing the phy
+9
include/linux/phy/phy.h
··· 254 254 struct phy *devm_phy_optional_get(struct device *dev, const char *string); 255 255 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np, 256 256 const char *con_id); 257 + struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np, 258 + const char *con_id); 257 259 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np, 258 260 int index); 259 261 void of_phy_put(struct phy *phy); ··· 443 441 const char *con_id) 444 442 { 445 443 return ERR_PTR(-ENOSYS); 444 + } 445 + 446 + static inline struct phy *devm_of_phy_optional_get(struct device *dev, 447 + struct device_node *np, 448 + const char *con_id) 449 + { 450 + return NULL; 446 451 } 447 452 448 453 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,