[PATCH v5 phy-next 16/27] phy: make phy_get_mode(), phy_(get|set)_bus_width() NULL tolerant

Vladimir Oltean
2026-03-19T22:34:59Z

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>
---
v2->v5: none
v1->v2: patch is new
---
drivers/phy/phy-core.c | 9 ++++++++-
include/linux/phy/phy.h | 2 ++
2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index a1aff00fba7c..0d0be494cfd7 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -624,19 +624,26 @@ 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);

void phy_set_bus_width(struct phy *phy, int bus_width)
{
- phy->attrs.bus_width = bus_width;
+ if (phy)
+ phy->attrs.bus_width = bus_width;
}
EXPORT_SYMBOL_GPL(phy_set_bus_width);

diff --git a/include/linux/phy/phy.h b/include/linux/phy/phy.h
index f208edd25afe..a7e2432ca1ae 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;
}