[PATCH v6 4/8] phy: core: Add phy bulk data helper functions
From: Inochi Amaoto
Date: Mon Nov 30 2026 - 01:36:52 EST
Add several helper functions that allow drivers to operate the many
PHYs in one operation. This can relieve the driver owners' life who
needs to handle many phys, as well as each phy error handling.
Signed-off-by: Inochi Amaoto <inochiama@xxxxxxxxx>
---
drivers/phy/phy-core.c | 120 ++++++++++++++++++++++++++++++++++++++++
include/linux/phy/phy.h | 38 +++++++++++++
2 files changed, 158 insertions(+)
diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index 5004d28b167a..6991e5359afb 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -1100,6 +1100,126 @@ struct phy *devm_phy_get_by_of_node(struct device *dev, struct device_node *np)
}
EXPORT_SYMBOL_GPL(devm_phy_get_by_of_node);
+/**
+ * phy_bulk_init() - initialize multiple PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data to initialize
+ *
+ * Initializes the PHYs in array order. If an initialization fails, all PHYs
+ * initialized by this call are exited in reverse order.
+ *
+ * Return: %0 if successful, a negative error code otherwise
+ */
+int phy_bulk_init(int num_phys, struct phy_bulk_data *phys)
+{
+ int ret, i;
+
+ for (i = 0; i < num_phys; i++) {
+ ret = phy_init(phys[i].phy);
+ if (ret)
+ goto err;
+ }
+
+ return 0;
+
+err:
+ while (i--)
+ phy_exit(phys[i].phy);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(phy_bulk_init);
+
+/**
+ * phy_bulk_exit() - exit multiple PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data to exit
+ *
+ * Exits the PHYs in reverse array order. If error occurs, all exited
+ * phys are inited again.
+ *
+ * Return: %0 if successful, or negative error code if one fails.
+ */
+int phy_bulk_exit(int num_phys, struct phy_bulk_data *phys)
+{
+ int ret, i;
+
+ for (i = num_phys - 1; i >= 0; i--) {
+ ret = phy_exit(phys[i].phy);
+ if (ret)
+ goto err;
+ }
+
+ return 0;
+
+err:
+ while (++i < num_phys)
+ phy_init(phys[i].phy);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(phy_bulk_exit);
+
+/**
+ * phy_bulk_power_on() - power on multiple PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data to power on
+ *
+ * Powers on the PHYs in array order. If a power-on operation fails, all PHYs
+ * powered on by this call are powered off in reverse order.
+ *
+ * Return: %0 if successful, or negative error code if one fails.
+ */
+int phy_bulk_power_on(int num_phys, struct phy_bulk_data *phys)
+{
+ int ret, i;
+
+ for (i = 0; i < num_phys; i++) {
+ ret = phy_power_on(phys[i].phy);
+ if (ret)
+ goto err;
+ }
+
+ return 0;
+
+err:
+ while (i--)
+ phy_power_off(phys[i].phy);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(phy_bulk_power_on);
+
+/**
+ * phy_bulk_power_off() - power off multiple PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data to power off
+ *
+ * Power off the PHYs in reverse order. If a powers off fails, already
+ * power-off phys are powered on again.
+ *
+ * Return: %0 if successful, the first negative error code otherwise
+ */
+int phy_bulk_power_off(int num_phys, struct phy_bulk_data *phys)
+{
+ int ret, i;
+
+ for (i = num_phys - 1; i >= 0; i--) {
+ ret = phy_power_off(phys[i].phy);
+ if (ret)
+ goto err;
+ }
+
+ return 0;
+
+err:
+ while (++i < num_phys)
+ phy_power_on(phys[i].phy);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(phy_bulk_power_off);
+
/**
* phy_create() - create a new phy
* @dev: device that is creating the new phy
diff --git a/include/linux/phy/phy.h b/include/linux/phy/phy.h
index c480d6cb42c5..c170d9aeeeb4 100644
--- a/include/linux/phy/phy.h
+++ b/include/linux/phy/phy.h
@@ -83,6 +83,18 @@ union phy_configure_opts {
struct phy_configure_opts_hdmi hdmi;
};
+/**
+ * struct phy_bulk_data - Data used for bulk phy operations.
+ *
+ * @id: phy consumer ID
+ */
+struct phy_bulk_data {
+ const char *id;
+
+ /* private: Internal use */
+ struct phy *phy;
+};
+
/**
* struct phy_ops - set of function pointers for performing phy operations
* @init: operation to be performed for initializing phy
@@ -313,6 +325,12 @@ void devm_of_phy_provider_unregister(struct device *dev,
struct phy_provider *phy_provider);
int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
+
+int phy_bulk_init(int num_phys, struct phy_bulk_data *phys);
+int phy_bulk_exit(int num_phys, struct phy_bulk_data *phys);
+int phy_bulk_power_on(int num_phys, struct phy_bulk_data *phys);
+int phy_bulk_power_off(int num_phys, struct phy_bulk_data *phys);
+
#else
static inline int phy_pm_runtime_get(struct phy *phy)
{
@@ -584,6 +602,26 @@ phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
}
static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
const char *dev_id) { }
+
+static inline int phy_bulk_init(int num_phys, struct phy_bulk_data *phys)
+{
+ return 0;
+}
+
+static inline int phy_bulk_exit(int num_phys, struct phy_bulk_data *phys)
+{
+ return 0;
+}
+
+static inline int phy_bulk_power_on(int num_phys, struct phy_bulk_data *phys)
+{
+ return 0;
+}
+
+static inline int phy_bulk_power_off(int num_phys, struct phy_bulk_data *phys)
+{
+ return 0;
+}
#endif
#endif /* __DRIVERS_PHY_H */
--
2.56.1