From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91023606A320 for ; Mon, 30 Nov 2026 06:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=tINOY4V6; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=cDUkfVqi DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=w83FoNztfLFmFCGsNZkmXmurOJ2MxNTu67SKXaZP7D4=; b=tINOY4V6YshWcYtPQ4e8brfa14 h9DUql4wO/pLcHsfQx96lPY2lSyz8rdFHVeGaq476YvPFzRbMuu35brHAS7AcLjdI/2gAlV646HZY 5wqbuhETEa06BKABvWB0frB5PsZOACmtfJ/zMmK45EUHjIkaj1olVNgWfjKrpsOSVufHpmoC8hJ+V LpFhLAt898WMJ9YI6ObZhwvBSzb40fjH//BbX/BodgrwBAQ2SahQu08ruyGMW/qShC8qcJd3U9u9f LH/lhL4dNZn3uSJ1ODfvK9DQ6hUjQEte0REBFlYLE9d7iZnTM7I3EUdxJoElQXvKIjLaAcGq9st5Q Ht++69og==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXv0e-00000003Rmr-2psu; Mon, 30 Nov 2026 06:37:09 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXv0J-00000003RUp-38JL for linux-phy@lists.infradead.org; Mon, 30 Nov 2026 06:36:50 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2f02f04a426so78817705ad.1 for ; Sun, 29 Nov 2026 22:36:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796020607; x=1796625407; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=PMODU/a4drTttagbJjUGVbF4ocpDzJoY0N1xV4BnGjA=; b=cDUkfVqikOjr+3+V6ZXE1EAKmXnXHd6WdBR5W4q0bOXyA5Ef1VD/pEY7OnZVqC9BpQ 9lmCOcaVVI2dHjvWCI/4/8q5CATw/adj06o+M54yHCV9wL/0tZzVl6loJ//UKd3BBFaS SBWAvtw21JePUr+gfp8q+oT7okMK8y32KpDvIBwbgUxBJuU7boHZUW4m2uEz10H40qvG uLWI4Pi+mw9K6WZ9AN5LyFQSfin3PK7Pj8W7jXVKkrqFgI55cGaYPhAqmKsc753LiYbE wKf2roaydQCFonwe85S9NYXywOr/sjg+NRtLKwOu0EKnJ15HwEXv1c2HeSX5dwS6RjDd J3KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796020607; x=1796625407; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=PMODU/a4drTttagbJjUGVbF4ocpDzJoY0N1xV4BnGjA=; b=kUAjF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duHL3UZRTciwxv3KBD+XICePIXXL+wMxb3ImeNwC /PiymSm1hZ20fM4sR4ReV2hWlVqkXJkrpcZmJ3joFHOSj6xS3nGR4nnLhynRpGINubLBsiiXkoa BF6bcwWLouUBZ5dNbOlf8nvOJ+c= X-Received: by 2002:a17:90b:4a88:b0:3b5:29e:11ac with SMTP id 98e67ed59e1d1-3b5029e1483mr16336003a91.37.1796020606680; Sun, 29 Nov 2026 22:36:46 -0800 (PST) Received: from localhost ([2001:19f0:8000:4ce8:5400:7ff:fe0e:e6b2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b405d135e9sm53693926a91.4.2026.11.29.22.36.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 22:36:46 -0800 (PST) From: Inochi Amaoto To: Andy Shevchenko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Damien Le Moal , Niklas Cassel , Frank Li , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Aksh Garg , Lorenzo Pieralisi , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Manivannan Sadhasivam , Rob Herring , Bjorn Helgaas , Vinod Koul , Neil Armstrong , Vladimir Oltean , Rhys Tumelty , Inochi Amaoto , Qiang Yu , Manikandan K Pillai , "vladimir.oltean@nxp.com" , Hans Zhang <18255117159@163.com>, Yao Zi 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 , Longbin Li Subject: [PATCH v6 5/8] phy: core: Add managed phy bulk data helper functions Date: Mon, 30 Nov 2026 14:36:04 +0800 Message-ID: <20261130063607.294823-6-inochiama@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261130063607.294823-1-inochiama@gmail.com> References: <20261130063607.294823-1-inochiama@gmail.com> X-Archived-By: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261129_223648_772741_98AF9893 X-CRM114-Status: GOOD ( 21.60 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-phy@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux Phy Mailing list List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-phy" X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- 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 -- linux-phy mailing list linux-phy@lists.infradead.org https://lists.infradead.org/mailman/listinfo/linux-phy