From: Corentin Labbe <clabbe@baylibre.com>
To: andrew@lunn.ch, broonie@kernel.org, calvin.johnson@oss.nxp.com,
davem@davemloft.net, edumazet@google.com, hkallweit1@gmail.com,
jernej.skrabec@gmail.com, krzysztof.kozlowski+dt@linaro.org,
kuba@kernel.org, lgirdwood@gmail.com, linux@armlinux.org.uk,
pabeni@redhat.com, robh+dt@kernel.org, samuel@sholland.org,
wens@csie.org
Cc: devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-sunxi@lists.linux.dev,
netdev@vger.kernel.org, linux-sunxi@googlegroups.com,
Corentin Labbe <clabbe@baylibre.com>
Subject: [PATCH v4 1/3] regulator: Add of_regulator_bulk_get_all
Date: Tue, 15 Nov 2022 07:36:01 +0000 [thread overview]
Message-ID: <20221115073603.3425396-2-clabbe@baylibre.com> (raw)
In-Reply-To: <20221115073603.3425396-1-clabbe@baylibre.com>
It work exactly like regulator_bulk_get() but instead of working on a
provided list of names, it seek all consumers properties matching
xxx-supply.
Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
drivers/regulator/of_regulator.c | 92 ++++++++++++++++++++++++++++++
include/linux/regulator/consumer.h | 8 +++
2 files changed, 100 insertions(+)
diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c
index 0aff1c2886b5..584c92f1a317 100644
--- a/drivers/regulator/of_regulator.c
+++ b/drivers/regulator/of_regulator.c
@@ -701,3 +701,95 @@ struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
return c_rdev;
}
+
+/*
+ * Check if name is a supply name according to the '*-supply' pattern
+ * return 0 if false
+ * return length of supply name without the -supply
+ */
+static int is_supply_name(const char *name)
+{
+ int strs, i;
+
+ strs = strlen(name);
+ /* string need to be at minimum len(x-supply) */
+ if (strs < 8)
+ return 0;
+ for (i = strs - 6; i > 0; i--) {
+ /* find first '-' and check if right part is supply */
+ if (name[i] != '-')
+ continue;
+ if (strcmp(name + i + 1, "supply") != 0)
+ return 0;
+ return i;
+ }
+ return 0;
+}
+
+/*
+ * of_regulator_bulk_get_all - get multiple regulator consumers
+ *
+ * @dev: Device to supply
+ * @np: device node to search for consumers
+ * @consumers: Configuration of consumers; clients are stored here.
+ *
+ * @return number of regulators on success, an errno on failure.
+ *
+ * This helper function allows drivers to get several regulator
+ * consumers in one operation. If any of the regulators cannot be
+ * acquired then any regulators that were allocated will be freed
+ * before returning to the caller.
+ */
+int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
+ struct regulator_bulk_data **consumers)
+{
+ int num_consumers = 0;
+ struct regulator *tmp;
+ struct property *prop;
+ int i, n = 0, ret;
+ char name[64];
+
+ *consumers = NULL;
+
+ /*
+ * first pass: get numbers of xxx-supply
+ * second pass: fill consumers
+ */
+restart:
+ for_each_property_of_node(np, prop) {
+ i = is_supply_name(prop->name);
+ if (i == 0)
+ continue;
+ if (!*consumers) {
+ num_consumers++;
+ continue;
+ } else {
+ memcpy(name, prop->name, i);
+ name[i] = '\0';
+ tmp = regulator_get(dev, name);
+ if (!tmp) {
+ ret = -EINVAL;
+ goto error;
+ }
+ (*consumers)[n].consumer = tmp;
+ n++;
+ continue;
+ }
+ }
+ if (*consumers)
+ return num_consumers;
+ if (num_consumers == 0)
+ return 0;
+ *consumers = kmalloc_array(num_consumers,
+ sizeof(struct regulator_bulk_data),
+ GFP_KERNEL);
+ if (!*consumers)
+ return -ENOMEM;
+ goto restart;
+
+error:
+ while (--n >= 0)
+ regulator_put(consumers[n]->consumer);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all);
diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h
index 628a52b8e63f..39b666b40ea6 100644
--- a/include/linux/regulator/consumer.h
+++ b/include/linux/regulator/consumer.h
@@ -244,6 +244,8 @@ int regulator_disable_deferred(struct regulator *regulator, int ms);
int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
struct regulator_bulk_data *consumers);
+int __must_check of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
+ struct regulator_bulk_data **consumers);
int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
struct regulator_bulk_data *consumers);
void devm_regulator_bulk_put(struct regulator_bulk_data *consumers);
@@ -481,6 +483,12 @@ static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
return 0;
}
+static inline int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
+ struct regulator_bulk_data **consumers)
+{
+ return 0;
+}
+
static inline int regulator_bulk_enable(int num_consumers,
struct regulator_bulk_data *consumers)
{
--
2.37.4
next prev parent reply other threads:[~2022-11-15 7:36 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-15 7:36 [PATCH v4 0/3] arm64: add ethernet to orange pi 3 Corentin Labbe
2022-11-15 7:36 ` Corentin Labbe [this message]
2022-11-15 10:03 ` [PATCH v4 1/3] regulator: Add of_regulator_bulk_get_all Mark Brown
2022-11-15 10:18 ` Corentin LABBE
2022-11-15 10:34 ` Mark Brown
2022-11-15 10:42 ` Russell King (Oracle)
2022-11-15 11:16 ` Mark Brown
2022-11-15 11:38 ` Russell King (Oracle)
2022-11-15 13:48 ` Andrew Lunn
2022-11-18 16:14 ` Mark Brown
2022-11-15 7:36 ` [PATCH v4 2/3] phy: handle optional regulator for PHY Corentin Labbe
2022-11-16 1:51 ` Andrew Lunn
2022-11-16 1:58 ` Andrew Lunn
2022-11-15 7:36 ` [PATCH v4 3/3] arm64: dts: allwinner: orange-pi-3: Enable ethernet Corentin Labbe
2022-11-16 1:39 ` [PATCH v4 0/3] arm64: add ethernet to orange pi 3 Andrew Lunn
2022-11-18 16:11 ` (subset) " Mark Brown
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=20221115073603.3425396-2-clabbe@baylibre.com \
--to=clabbe@baylibre.com \
--cc=andrew@lunn.ch \
--cc=broonie@kernel.org \
--cc=calvin.johnson@oss.nxp.com \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=edumazet@google.com \
--cc=hkallweit1@gmail.com \
--cc=jernej.skrabec@gmail.com \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=kuba@kernel.org \
--cc=lgirdwood@gmail.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sunxi@googlegroups.com \
--cc=linux-sunxi@lists.linux.dev \
--cc=linux@armlinux.org.uk \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=robh+dt@kernel.org \
--cc=samuel@sholland.org \
--cc=wens@csie.org \
/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;
as well as URLs for NNTP newsgroup(s).