From: David Lechner <dlechner@baylibre.com>
To: Jonathan Corbet <corbet@lwn.net>,
Liam Girdwood <lgirdwood@gmail.com>,
Mark Brown <broonie@kernel.org>, Jean Delvare <jdelvare@suse.com>,
Guenter Roeck <linux@roeck-us.net>,
Support Opensource <support.opensource@diasemi.com>,
Cosmin Tanislav <cosmin.tanislav@analog.com>,
Lars-Peter Clausen <lars@metafoo.de>,
Michael Hennerich <Michael.Hennerich@analog.com>,
Jonathan Cameron <jic23@kernel.org>,
Antoniu Miclaus <antoniu.miclaus@analog.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: David Lechner <dlechner@baylibre.com>,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hwmon@vger.kernel.org, linux-iio@vger.kernel.org,
linux-staging@lists.linux.dev, linux-input@vger.kernel.org
Subject: [PATCH RFC 1/7] regulator: devres: add APIs for reference supplies
Date: Wed, 27 Mar 2024 18:18:50 -0500 [thread overview]
Message-ID: <20240327-regulator-get-enable-get-votlage-v1-1-5f4517faa059@baylibre.com> (raw)
In-Reply-To: <20240327-regulator-get-enable-get-votlage-v1-0-5f4517faa059@baylibre.com>
A common use case for regulators is to supply a reference voltage to an
analog input or output device. This adds two new devres APIs to get,
enable, and get the voltage in a single call. This allows eliminating
boilerplate code in drivers that use reference supplies in this way.
devm_regulator_get_enable_get_voltage() is intended for cases where the
supply is required to provide an external reference voltage.
devm_regulator_get_optional_enable_get_voltage() is intended for cases
where the supply is optional and device typically uses an internal
reference voltage if the supply is not available.
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
Documentation/driver-api/driver-model/devres.rst | 2 +
drivers/regulator/devres.c | 83 ++++++++++++++++++++++++
include/linux/regulator/consumer.h | 14 ++++
3 files changed, 99 insertions(+)
diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
index 7be8b8dd5f00..fd954d09e13c 100644
--- a/Documentation/driver-api/driver-model/devres.rst
+++ b/Documentation/driver-api/driver-model/devres.rst
@@ -433,9 +433,11 @@ REGULATOR
devm_regulator_bulk_put()
devm_regulator_get()
devm_regulator_get_enable()
+ devm_regulator_get_enable_get_voltage()
devm_regulator_get_enable_optional()
devm_regulator_get_exclusive()
devm_regulator_get_optional()
+ devm_regulator_get_optional_enable_get_voltage()
devm_regulator_irq_helper()
devm_regulator_put()
devm_regulator_register()
diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index 90bb0d178885..e240926defc5 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -145,6 +145,89 @@ struct regulator *devm_regulator_get_optional(struct device *dev,
}
EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
+static int _devm_regulator_get_enable_get_voltage(struct device *dev,
+ const char *id,
+ bool silent_enodev)
+{
+ struct regulator *r;
+ int ret;
+
+ /*
+ * Since we need a real voltage, we use devm_regulator_get_optional()
+ * here to avoid getting a dummy regulator if the supply is not present.
+ */
+ r = devm_regulator_get_optional(dev, id);
+ if (silent_enodev && PTR_ERR(r) == -ENODEV)
+ return -ENODEV;
+ if (IS_ERR(r))
+ return dev_err_probe(dev, PTR_ERR(r),
+ "failed to get regulator '%s'\n", id);
+
+ ret = regulator_enable(r);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "failed to enable regulator '%s'\n", id);
+
+ ret = devm_add_action_or_reset(dev, regulator_action_disable, r);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "failed to add disable action for regulator '%s'\n",
+ id);
+
+ ret = regulator_get_voltage(r);
+ if (ret < 0)
+ return dev_err_probe(dev, ret,
+ "failed to get voltage for regulator '%s'\n",
+ id);
+
+ return ret;
+}
+
+/**
+ * devm_regulator_get_enable_get_voltage - Resource managed regulator get and
+ * enable that returns the voltage
+ * @dev: device to supply
+ * @id: supply name or regulator ID.
+ *
+ * Get and enable regulator for duration of the device life-time.
+ * regulator_disable() and regulator_put() are automatically called on driver
+ * detach. See regulator_get_optional(), regulator_enable(), and
+ * regulator_get_voltage() for more information.
+ *
+ * This is a convenience function for supplies that provide a reference voltage
+ * where the consumer driver just needs to know the voltage and keep the
+ * regulator enabled. Also, as a convenience, this prints error messages on
+ * failure.
+ *
+ * Returns: voltage in microvolts on success, or an error code on failure.
+ */
+int devm_regulator_get_enable_get_voltage(struct device *dev, const char *id)
+{
+ return _devm_regulator_get_enable_get_voltage(dev, id, false);
+}
+EXPORT_SYMBOL_GPL(devm_regulator_get_enable_get_voltage);
+
+/**
+ * devm_regulator_get_optional_enable_get_voltage - Resource managed regulator
+ * get and enable that returns
+ * the voltage
+ * @dev: device to supply
+ * @id: supply name or regulator ID.
+ *
+ * This function is identical to devm_regulator_get_enable_get_voltage() except
+ * that it does not print an error message in the case of -ENODEV. Callers are
+ * expected to handle -ENODEV as a special case instead of passing it on as an
+ * error.
+ *
+ * Returns: voltage in microvolts on success, or an error code on failure.
+ */
+int devm_regulator_get_optional_enable_get_voltage(struct device *dev,
+ const char *id)
+{
+ return _devm_regulator_get_enable_get_voltage(dev, id, true);
+}
+EXPORT_SYMBOL_GPL(devm_regulator_get_optional_enable_get_voltage);
+
static int devm_regulator_match(struct device *dev, void *res, void *data)
{
struct regulator **r = res;
diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h
index 4660582a3302..35596db521a0 100644
--- a/include/linux/regulator/consumer.h
+++ b/include/linux/regulator/consumer.h
@@ -164,6 +164,8 @@ struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
const char *id);
int devm_regulator_get_enable(struct device *dev, const char *id);
int devm_regulator_get_enable_optional(struct device *dev, const char *id);
+int devm_regulator_get_enable_get_voltage(struct device *dev, const char *id);
+int devm_regulator_get_optional_enable_get_voltage(struct device *dev, const char *id);
void regulator_put(struct regulator *regulator);
void devm_regulator_put(struct regulator *regulator);
@@ -329,6 +331,18 @@ static inline int devm_regulator_get_enable_optional(struct device *dev,
return -ENODEV;
}
+static inline int devm_regulator_get_enable_get_voltage(struct device *dev,
+ const char *id)
+{
+ return -ENODEV;
+}
+
+static inline int devm_regulator_get_optional_enable_get_voltage(struct device *dev,
+ const char *id)
+{
+ return -ENODEV;
+}
+
static inline struct regulator *__must_check
regulator_get_optional(struct device *dev, const char *id)
{
--
2.43.2
next prev parent reply other threads:[~2024-03-27 23:19 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-27 23:18 [PATCH RFC 0/7] regulator: new APIs for voltage reference supplies David Lechner
2024-03-27 23:18 ` David Lechner [this message]
2024-03-28 13:47 ` [PATCH RFC 1/7] regulator: devres: add APIs for " Jonathan Cameron
2024-03-28 15:54 ` David Lechner
2024-03-28 18:03 ` Dmitry Torokhov
2024-03-28 18:18 ` Mark Brown
2024-03-28 20:17 ` Dmitry Torokhov
2024-03-28 20:25 ` Mark Brown
2024-03-30 16:02 ` Jonathan Cameron
2024-03-27 23:18 ` [PATCH RFC 2/7] hwmon: (adc128d818) Use devm_regulator_get_optional_enable_get_voltage() David Lechner
2024-03-28 14:05 ` Jonathan Cameron
2024-03-27 23:18 ` [PATCH RFC 3/7] hwmon: (da9052) Use devm_regulator_get_enable_get_voltage() David Lechner
2024-03-28 14:20 ` Jonathan Cameron
2024-03-28 15:20 ` Guenter Roeck
2024-03-28 15:53 ` Jonathan Cameron
2024-03-27 23:18 ` [PATCH RFC 4/7] iio: addac: ad74115: " David Lechner
2024-03-28 13:58 ` Jonathan Cameron
2024-03-27 23:18 ` [PATCH RFC 5/7] iio: frequency: admv1013: " David Lechner
2024-03-28 13:51 ` Jonathan Cameron
2024-03-27 23:18 ` [PATCH RFC 6/7] staging: iio: impedance-analyzer: " David Lechner
2024-03-28 13:50 ` Jonathan Cameron
2024-03-27 23:18 ` [PATCH RFC 7/7] Input: mpr121: " David Lechner
2024-03-28 14:21 ` Jonathan Cameron
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=20240327-regulator-get-enable-get-votlage-v1-1-5f4517faa059@baylibre.com \
--to=dlechner@baylibre.com \
--cc=Michael.Hennerich@analog.com \
--cc=antoniu.miclaus@analog.com \
--cc=broonie@kernel.org \
--cc=corbet@lwn.net \
--cc=cosmin.tanislav@analog.com \
--cc=dmitry.torokhov@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=jdelvare@suse.com \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=lgirdwood@gmail.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-staging@lists.linux.dev \
--cc=linux@roeck-us.net \
--cc=support.opensource@diasemi.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).