From: Inochi Amaoto <inochiama@gmail.com>
To: "Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Damien Le Moal" <dlemoal@kernel.org>,
"Niklas Cassel" <cassel@kernel.org>,
"Frank Li" <Frank.Li@nxp.com>,
"Sascha Hauer" <s.hauer@pengutronix.de>,
"Pengutronix Kernel Team" <kernel@pengutronix.de>,
"Fabio Estevam" <festevam@gmail.com>,
"Aksh Garg" <a-garg7@ti.com>,
"Lorenzo Pieralisi" <lpieralisi@kernel.org>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
"Manivannan Sadhasivam" <mani@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Vinod Koul" <vkoul@kernel.org>,
"Neil Armstrong" <neil.armstrong@linaro.org>,
"Vladimir Oltean" <olteanv@gmail.com>,
"Rhys Tumelty" <rhys@tumelty.co.uk>,
"Inochi Amaoto" <inochiama@gmail.com>,
"Qiang Yu" <qiang.yu@oss.qualcomm.com>,
"Manikandan K Pillai" <mpillai@cadence.com>,
"vladimir.oltean@nxp.com" <vladimir.oltean@nxp.com>,
"Hans Zhang" <18255117159@163.com>, "Yao Zi" <me@ziyao.cc>
Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-ide@vger.kernel.org, imx@lists.linux.dev,
linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org,
linux-phy@lists.infradead.org, Yixun Lan <dlan@gentoo.org>,
Longbin Li <looong.bin@gmail.com>
Subject: [PATCH v6 5/8] phy: core: Add managed phy bulk data helper functions
Date: Mon, 30 Nov 2026 14:36:04 +0800 [thread overview]
Message-ID: <20261130063607.294823-6-inochiama@gmail.com> (raw)
In-Reply-To: <20261130063607.294823-1-inochiama@gmail.com>
Add device managed variants of the phy bulk helper functions. So
the driver can benefit from automatically managed phy handles.
Signed-off-by: Inochi Amaoto <inochiama@gmail.com>
---
drivers/phy/phy-core.c | 320 ++++++++++++++++++++++++++++++++++++++++
include/linux/phy/phy.h | 31 ++++
2 files changed, 351 insertions(+)
diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index 6991e5359afb..eeba1b899c20 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -1100,6 +1100,326 @@ struct phy *devm_phy_get_by_of_node(struct device *dev, struct device_node *np)
}
EXPORT_SYMBOL_GPL(devm_phy_get_by_of_node);
+/**
+ * of_phy_get_count() - Get the number of phys of a device node
+ * @np: device_node for which to get the phy
+ *
+ * Return: the phy count if successful, %0 if no phy handle is found,
+ * negative error value if error occurs.
+ */
+static int of_phy_get_count(const struct device_node *np)
+{
+ int count;
+
+ count = of_count_phandle_with_args(np, "phys", "#phy-cells");
+
+ if (count == -ENOENT)
+ return 0;
+
+ return count;
+}
+
+/**
+ * phy_bulk_put() - release a set of PHYs
+ * @dev: device that acquired the PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data with PHYs set
+ *
+ * Releases the PHY references in reverse order and clears the PHY pointer in
+ * each entry. The caller owns the phys array and is responsible for freeing it
+ * if necessary.
+ */
+static void phy_bulk_put(struct device *dev, int num_phys,
+ struct phy_bulk_data *phys)
+{
+ while (num_phys--) {
+ phy_put(dev, phys[num_phys].phy);
+ phys[num_phys].phy = NULL;
+ }
+}
+
+/**
+ * of_phy_bulk_put() - release a set of PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data with PHYs set
+ *
+ * Releases the PHY references in reverse order and clears the PHY pointer in
+ * each entry. The caller owns the phys array and is responsible for freeing it
+ * if necessary.
+ */
+static void of_phy_bulk_put(int num_phys, struct phy_bulk_data *phys)
+{
+ while (num_phys--) {
+ of_phy_put(phys[num_phys].phy);
+ phys[num_phys].phy = NULL;
+ }
+}
+
+static int of_phy_bulk_get_by_index(struct device_node *np, int num_phys,
+ struct phy_bulk_data *phys)
+{
+ int ret = 0;
+ int i;
+
+ for (i = 0; i < num_phys; i++) {
+ phys[i].id = NULL;
+ phys[i].phy = NULL;
+ }
+
+ mutex_lock(&phy_provider_mutex);
+
+ for (i = 0; i < num_phys; i++) {
+ of_property_read_string_index(np, "phy-names", i, &phys[i].id);
+
+ phys[i].phy = _of_phy_get_by_index(np, i);
+
+ if (IS_ERR(phys[i].phy)) {
+ ret = PTR_ERR(phys[i].phy);
+ break;
+ }
+ }
+
+ mutex_unlock(&phy_provider_mutex);
+
+ if (ret)
+ of_phy_bulk_put(i, phys);
+
+ return ret;
+}
+
+/**
+ * phy_bulk_get() - lookup and obtain references to multiple PHYs
+ * @dev: device that requests the PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data with PHY names set
+ *
+ * Gets each PHY using phy_get(). This supports both device tree lookups and
+ * non-device-tree lookups registered with phy_create_lookup(). The caller must
+ * call phy_bulk_put() to release the PHY references.
+ *
+ * Return: %0 if successful, a negative error code otherwise
+ */
+static int phy_bulk_get(struct device *dev, int num_phys,
+ struct phy_bulk_data *phys)
+{
+ int ret, i;
+
+ for (i = 0; i < num_phys; i++)
+ phys[i].phy = NULL;
+
+ for (i = 0; i < num_phys; i++) {
+ phys[i].phy = phy_get(dev, phys[i].id);
+
+ ret = PTR_ERR_OR_ZERO(phys[i].phy);
+ if (ret) {
+ phys[i].phy = NULL;
+
+ dev_err_probe(dev, ret, "Failed to get phy: (%s)\n",
+ phys[i].id);
+ goto err;
+ }
+ }
+
+ return 0;
+
+err:
+ phy_bulk_put(dev, i, phys);
+
+ return ret;
+}
+
+/**
+ * of_phy_bulk_get_all() - obtain all PHYs from a device node
+ * @np: device node containing the PHY references
+ * @phys: pointer to store the allocated array of struct phy_bulk_data
+ *
+ * Gets every PHY referenced by the phys property in index order. PHY names are
+ * read from phy-names when present.
+ *
+ * Return: the number of PHYs on success, %0 if no PHYs are found, or a
+ * negative error code otherwise
+ */
+static int of_phy_bulk_get_all(struct device_node *np,
+ struct phy_bulk_data **phys)
+{
+ struct phy_bulk_data *phy_bulk;
+ int num_phys;
+ int ret;
+
+ num_phys = of_phy_get_count(np);
+ if (num_phys <= 0)
+ return num_phys;
+
+ phy_bulk = kmalloc_objs(*phy_bulk, num_phys);
+ if (!phy_bulk)
+ return -ENOMEM;
+
+ ret = of_phy_bulk_get_by_index(np, num_phys, phy_bulk);
+ if (ret) {
+ kfree(phy_bulk);
+ return ret;
+ }
+
+ *phys = phy_bulk;
+
+ return num_phys;
+}
+
+/**
+ * phy_bulk_put_all() - release and free PHYs
+ * @dev: device that acquired the PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data to release and free
+ */
+static void phy_bulk_put_all(struct device *dev, int num_phys,
+ struct phy_bulk_data *phys)
+{
+ if (IS_ERR_OR_NULL(phys))
+ return;
+
+ phy_bulk_put(dev, num_phys, phys);
+ kfree(phys);
+}
+
+struct phy_bulk_devres {
+ struct phy_bulk_data *phys;
+ int num_phys;
+};
+
+static void devm_phy_bulk_release(struct device *dev, void *res)
+{
+ struct phy_bulk_devres *devres = res;
+
+ phy_bulk_put(dev, devres->num_phys, devres->phys);
+}
+
+/**
+ * devm_phy_bulk_get() - managed lookup of multiple PHYs
+ * @dev: device that requests the PHYs
+ * @num_phys: number of entries in the phys array
+ * @phys: array of struct phy_bulk_data with PHY names set
+ *
+ * Gets the PHYs using phy_bulk_get() and associates the references with
+ * @dev. The references are automatically released on driver detach.
+ *
+ * Return: %0 if successful, a negative error code otherwise
+ */
+int devm_phy_bulk_get(struct device *dev, int num_phys,
+ struct phy_bulk_data *phys)
+{
+ struct phy_bulk_devres *devres;
+ int ret;
+
+ devres = devres_alloc(devm_phy_bulk_release, sizeof(*devres),
+ GFP_KERNEL);
+ if (!devres)
+ return -ENOMEM;
+
+ ret = phy_bulk_get(dev, num_phys, phys);
+ if (ret) {
+ devres_free(devres);
+ return ret;
+ }
+
+ devres->phys = phys;
+ devres->num_phys = num_phys;
+ devres_add(dev, devres);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(devm_phy_bulk_get);
+
+static int devm_phy_bulk_match(struct device *dev, void *res, void *match_data)
+{
+ struct phy_bulk_devres *devres = res;
+
+ return devres->phys == match_data;
+}
+
+static void devm_phy_bulk_release_all(struct device *dev, void *res)
+{
+ struct phy_bulk_devres *devres = res;
+
+ phy_bulk_put_all(dev, devres->num_phys, devres->phys);
+}
+
+/**
+ * devm_phy_bulk_get_all() - managed lookup of all PHYs requested by a device
+ * @dev: device that requests the PHYs
+ * @phys: pointer to store the allocated array of struct phy_bulk_data
+ *
+ * The function calls devm_of_phy_bulk_get_all() directly, but sets the
+ * @np parameter to dev_of_node(dev).
+ *
+ * Return: the number of PHYs on success, %0 if no PHYs are found, or a
+ * negative error code otherwise
+ */
+int devm_phy_bulk_get_all(struct device *dev, struct phy_bulk_data **phys)
+{
+ return devm_of_phy_bulk_get_all(dev, dev_of_node(dev), phys);
+}
+EXPORT_SYMBOL_GPL(devm_phy_bulk_get_all);
+
+/**
+ * devm_of_phy_bulk_get_all() - managed lookup of all PHYs from a device node
+ * @dev: device that requests the PHYs
+ * @np: device node containing the PHY references
+ * @phys: pointer to store the allocated array of struct phy_bulk_data
+ *
+ * Gets all PHYs from the specified device node, associates the allocated array
+ * and PHY references with @dev, and creates a device link for each PHY. They
+ * are automatically released on driver detach.
+ *
+ * Return: the number of PHYs on success, %0 if no PHYs are found, or a
+ * negative error code otherwise
+ */
+int devm_of_phy_bulk_get_all(struct device *dev, struct device_node *np,
+ struct phy_bulk_data **phys)
+{
+ struct phy_bulk_devres *devres;
+ int ret;
+
+ devres = devres_alloc(devm_phy_bulk_release_all, sizeof(*devres),
+ GFP_KERNEL);
+ if (!devres)
+ return -ENOMEM;
+
+ ret = of_phy_bulk_get_all(np, &devres->phys);
+ if (ret > 0) {
+ for (int i = 0; i < ret; i++)
+ phy_add_device_link(dev, devres->phys[i].phy);
+ *phys = devres->phys;
+ devres->num_phys = ret;
+ devres_add(dev, devres);
+ } else {
+ devres_free(devres);
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(devm_of_phy_bulk_get_all);
+
+/**
+ * devm_phy_bulk_put_all() - release the multiple PHY
+ * @dev: device that wants to release this phy
+ * @phy: the phy bulk data returned by devm_phy_bulk_get_all()
+ *
+ * destroys the devres associated with this phy bulk data and invokes
+ * phy_bulk_put to release the phy.
+ */
+void devm_phy_bulk_put_all(struct device *dev, struct phy_bulk_data *phy)
+{
+ int r;
+
+ if (!phy)
+ return;
+
+ r = devres_release(dev, devm_phy_bulk_release_all,
+ devm_phy_bulk_match, phy);
+ dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
+}
+EXPORT_SYMBOL_GPL(devm_phy_bulk_put_all);
+
/**
* phy_bulk_init() - initialize multiple PHYs
* @num_phys: number of entries in the phys array
diff --git a/include/linux/phy/phy.h b/include/linux/phy/phy.h
index c170d9aeeeb4..a51b3c283efd 100644
--- a/include/linux/phy/phy.h
+++ b/include/linux/phy/phy.h
@@ -326,6 +326,13 @@ void devm_of_phy_provider_unregister(struct device *dev,
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 devm_phy_bulk_get(struct device *dev, int num_phys,
+ struct phy_bulk_data *phys);
+int devm_phy_bulk_get_all(struct device *dev, struct phy_bulk_data **phys);
+int devm_of_phy_bulk_get_all(struct device *dev, struct device_node *np,
+ struct phy_bulk_data **phys);
+void devm_phy_bulk_put_all(struct device *dev, struct phy_bulk_data *phy);
+
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);
@@ -603,6 +610,30 @@ 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 devm_phy_bulk_get(struct device *dev, int num_phys,
+ struct phy_bulk_data *phys)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline int devm_of_phy_bulk_get_all(struct device *dev,
+ struct device_node *np,
+ struct phy_bulk_data **phys)
+{
+ return 0;
+}
+
+static inline int devm_phy_bulk_get_all(struct device *dev,
+ struct phy_bulk_data **phys)
+{
+ return 0;
+}
+
+static inline void devm_phy_bulk_put_all(struct device *dev,
+ struct phy_bulk_data *phy)
+{
+}
+
static inline int phy_bulk_init(int num_phys, struct phy_bulk_data *phys)
{
return 0;
--
2.56.1
next prev parent reply other threads:[~2026-11-30 6:38 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 6:35 [PATCH v6 0/8] phy: core: Add phy bulk helpers support Inochi Amaoto
2026-11-30 6:36 ` [PATCH v6 1/8] phy: core: Add common helper to add phy phandle device link Inochi Amaoto
2026-11-30 6:36 ` [PATCH v6 2/8] phy: core: Add common helper for get phy phandle by index Inochi Amaoto
2026-11-30 6:36 ` [PATCH v6 3/8] phy: core: Make phy_put() ignore NULL and error pointers Inochi Amaoto
2026-11-30 6:36 ` [PATCH v6 4/8] phy: core: Add phy bulk data helper functions Inochi Amaoto
2026-11-30 6:36 ` Inochi Amaoto [this message]
2026-11-30 6:36 ` [PATCH v6 6/8] doc: phy: Document some bulk " Inochi Amaoto
2026-11-30 6:36 ` [PATCH v6 7/8] PCI: cadence: Use phy bulk data api to simplify the logic Inochi Amaoto
2026-11-30 6:36 ` [PATCH v6 8/8] ata: ahci_imx: Use bulk PHY APIs for calibration PHYs Inochi Amaoto
2026-11-30 7:14 ` Damien Le Moal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130063607.294823-6-inochiama@gmail.com \
--to=inochiama@gmail.com \
--cc=18255117159@163.com \
--cc=Frank.Li@nxp.com \
--cc=a-garg7@ti.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bhelgaas@google.com \
--cc=cassel@kernel.org \
--cc=corbet@lwn.net \
--cc=dlan@gentoo.org \
--cc=dlemoal@kernel.org \
--cc=festevam@gmail.com \
--cc=imx@lists.linux.dev \
--cc=kernel@pengutronix.de \
--cc=kwilczynski@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-ide@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-phy@lists.infradead.org \
--cc=looong.bin@gmail.com \
--cc=lpieralisi@kernel.org \
--cc=mani@kernel.org \
--cc=me@ziyao.cc \
--cc=mpillai@cadence.com \
--cc=neil.armstrong@linaro.org \
--cc=olteanv@gmail.com \
--cc=qiang.yu@oss.qualcomm.com \
--cc=rdunlap@infradead.org \
--cc=rhys@tumelty.co.uk \
--cc=robh@kernel.org \
--cc=s.hauer@pengutronix.de \
--cc=skhan@linuxfoundation.org \
--cc=vkoul@kernel.org \
--cc=vladimir.oltean@nxp.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox