linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chuhong Yuan <hslester96@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	linux-kernel@vger.kernel.org, Chuhong Yuan <hslester96@gmail.com>
Subject: [PATCH] regulator: core: Add devres versions of regulator_enable/disable
Date: Fri,  9 Aug 2019 11:03:52 +0800	[thread overview]
Message-ID: <20190809030352.8387-1-hslester96@gmail.com> (raw)

I wrote a coccinelle script to detect possible chances
of utilizing devm_() APIs to simplify the driver.
The script found 147 drivers in total and 22 of them
have be patched.

Within the 125 left ones, at least 31 of them (24.8%)
are hindered from benefiting from devm_() APIs because
of lack of a devres version of regulator_enable().

Therefore I implemented devm_regulator_enable/disable()
to make more drivers possible to use devm_() APIs.

Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
---
 drivers/regulator/devres.c | 55 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index 3ea1c170f840..507151a71fd3 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -115,6 +115,61 @@ void devm_regulator_put(struct regulator *regulator)
 }
 EXPORT_SYMBOL_GPL(devm_regulator_put);
 
+static void devm_regulator_off(struct device *dev, void *res)
+{
+	regulator_disable(*(struct regulator **)res);
+}
+
+/**
+ * devm_regulator_enable - Resource managed regulator_enable()
+ * @regulator: regulator to enable
+ *
+ * Managed regulator_enable(). Regulator enabled is automatically
+ * disabled on driver detach. See regulator_enable() for more
+ * information.
+ */
+int devm_regulator_enable(struct device *dev, struct regulator *regulator)
+{
+	struct regulator **ptr;
+	int ret;
+
+	ptr = devres_alloc(devm_regulator_off, sizeof(*ptr), GFP_KERNEL);
+	if (!ptr)
+		return -ENOMEM;
+
+	ret = regulator_enable(regulator);
+	if (!ret) {
+		*ptr = regulator;
+		devres_add(dev, ptr);
+	} else {
+		devres_free(ptr);
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(devm_regulator_enable);
+
+/**
+ * devm_regulator_disable - Resource managed regulator_disable()
+ * @regulator: regulator to disable
+ *
+ * Disable a regulator enabled by devm_regulator_enable().
+ * Normally this function will not need to be called and the
+ * resource management code will ensure that the regulator is
+ * disabled.
+ */
+void devm_regulator_disable(struct regulator *regulator)
+{
+	int rc;
+
+	rc = devres_release(regulator->dev, devm_regulator_off,
+			    devm_regulator_match, regulator);
+
+	if (rc != 0)
+		WARN_ON(rc);
+}
+EXPORT_SYMBOL_GPL(devm_regulator_disable);
+
 struct regulator_bulk_devres {
 	struct regulator_bulk_data *consumers;
 	int num_consumers;
-- 
2.20.1


             reply	other threads:[~2019-08-09  3:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-09  3:03 Chuhong Yuan [this message]
2019-08-09 15:11 ` [PATCH] regulator: core: Add devres versions of regulator_enable/disable Mark Brown
2019-08-10  1:44   ` Chuhong Yuan
2019-08-12 11:07     ` Mark Brown
2019-08-12 12:51       ` Chuhong Yuan
2019-08-12 14:19         ` Chuhong Yuan

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=20190809030352.8387-1-hslester96@gmail.com \
    --to=hslester96@gmail.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.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).