[PATCH v8 phy-next 19/31] phy: make phy_get_mode(), phy_get_bus_width() NULL tolerant

Vladimir Oltean
2026-05-05T10:07:53Z

The PHY API has an optional "get" which returns NULL, so it needs to
accept that NULL coming back in.

Most PHY functions do this, only the formerly static inline attribute
dereferences did not.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
v7->v8: remove phy_set_bus_width() from this change, it has become a
provider function which does not need the protection
v2->v7: none
v1->v2: patch is new
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
drivers/phy/phy-core.c | 6 ++++++
include/linux/phy/phy.h | 2 ++
2 files changed, 8 insertions(+)

diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index fe933629286b..6cd33204d2ff 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -624,12 +624,18 @@ EXPORT_SYMBOL_GPL(phy_validate);

enum phy_mode phy_get_mode(struct phy *phy)
{
+ if (!phy)
+ return PHY_MODE_INVALID;
+
return phy->attrs.mode;
}
EXPORT_SYMBOL_GPL(phy_get_mode);

int phy_get_bus_width(struct phy *phy)
{
+ if (!phy)
+ return 0;
+
return phy->attrs.bus_width;
}
EXPORT_SYMBOL_GPL(phy_get_bus_width);
diff --git a/include/linux/phy/phy.h b/include/linux/phy/phy.h
index d716e5e0584c..99d3d65da614 100644
--- a/include/linux/phy/phy.h
+++ b/include/linux/phy/phy.h
@@ -248,6 +248,8 @@ static inline int phy_notify_state(struct phy *phy, union phy_notify state)

static inline int phy_get_bus_width(struct phy *phy)
{
+ if (!phy)
+ return 0;
return -ENOSYS;
}

--
2.34.1