linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandru Ardelean <aardelean@deviqon.com>
To: linux-kernel@vger.kernel.org
Cc: lgirdwood@gmail.com, broonie@kernel.org,
	Alexandru Ardelean <aardelean@deviqon.com>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Guenter Roeck <linux@roeck-us.net>
Subject: [RFC PATCH v2] regulator: devres: add devm_regulator_enable() as short-hand
Date: Wed, 30 Jun 2021 15:49:31 +0300	[thread overview]
Message-ID: <20210630124931.1226658-1-aardelean@deviqon.com> (raw)
In-Reply-To: <20210625125307.330831-1-aardelean@deviqon.com>

Evidently, this came about after doing a few of these types of constructs:

    static void reg_disable(void *reg)
    {
        regulator_disable(reg)
    }
    ...
    ret = regulator_enable(reg);
    if (ret)
        return ret;

    ret = devm_add_action_or_reset(dev, reg_disable, reg);
    ...

A previous proposal was done via [1], to automatically decrease the enable
refcount in devm_regulator_release() if it's 1.
A point was made that this [indeed] can cause issues with
reviewing/tracking enable refcount.

I'm not sure [at this point in time] whether a devm_regulator_enable()
function is still something that would be a good idea, given that this can
cause issues with consumers potentially mixing regulator_enable() and
devm_regulator_enable() calls. This concern was mentioned in [2].

But, there are now a number of simple drivers that are implementing the above
construct already, so it may be an idea to propose this short-hand in the
regulator framework.

References:
 [1] https://lore.kernel.org/lkml/20210625125307.330831-1-aardelean@deviqon.com/
 [2] https://lore.kernel.org/lkml/20170213023249.GA27688@dtor-ws/
 [3] https://lkml.org/lkml/2014/2/4/940

Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Alexandru Ardelean <aardelean@deviqon.com>
---
 .../driver-api/driver-model/devres.rst        |  1 +
 drivers/regulator/devres.c                    | 20 +++++++++++++++++++
 include/linux/regulator/consumer.h            |  8 ++++++++
 3 files changed, 29 insertions(+)

diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
index e0814d214048..8a92e0888cb8 100644
--- a/Documentation/driver-api/driver-model/devres.rst
+++ b/Documentation/driver-api/driver-model/devres.rst
@@ -408,6 +408,7 @@ REGULATOR
   devm_regulator_get()
   devm_regulator_put()
   devm_regulator_register()
+  devm_regulator_enable()
 
 RESET
   devm_reset_control_get()
diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index a8de0aa88bad..dfb23ac178a0 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -533,3 +533,23 @@ void *devm_regulator_irq_helper(struct device *dev,
 	return ptr;
 }
 EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);
+
+static void devm_regulator_disable(void *regulator)
+{
+	regulator_disable(regulator);
+}
+
+/**
+ * devm_regulator_enable - device-managed regulator_enable
+ * @dev: device for regulator "consumer"
+ * @regulator: regulator source
+ *
+ * This will register an action handler for disabling the regulator
+ * by reistering a regulator_disable() call via devm_add_action_or_reset().
+ * It's meant to be a short-hand for all the drivers using this construct.
+ */
+int devm_regulator_enable(struct device *dev, struct regulator *regulator)
+{
+	return devm_add_action_or_reset(dev, devm_regulator_disable, regulator);
+}
+EXPORT_SYMBOL_GPL(devm_regulator_enable);
diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h
index f72ca73631be..01f17205c567 100644
--- a/include/linux/regulator/consumer.h
+++ b/include/linux/regulator/consumer.h
@@ -236,6 +236,8 @@ void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
 
 /* regulator output control and status */
 int __must_check regulator_enable(struct regulator *regulator);
+int __must_check devm_regulator_enable(struct device *dev,
+                                       struct regulator *regulator);
 int regulator_disable(struct regulator *regulator);
 int regulator_force_disable(struct regulator *regulator);
 int regulator_is_enabled(struct regulator *regulator);
@@ -432,6 +434,12 @@ static inline int regulator_enable(struct regulator *regulator)
 	return 0;
 }
 
+static inline int devm_regulator_enable(struct device *dev,
+                                        struct regulator *regulator)
+{
+	return 0;
+}
+
 static inline int regulator_disable(struct regulator *regulator)
 {
 	return 0;
-- 
2.31.1


      parent reply	other threads:[~2021-06-30 12:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-25 12:53 [RFC PATCH] regulator: devres: disable regulator on release if refcount is 1 Alexandru Ardelean
2021-06-28 14:53 ` Mark Brown
2021-06-29  7:55   ` Alexandru Ardelean
2021-06-30 12:49 ` Alexandru Ardelean [this message]

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=20210630124931.1226658-1-aardelean@deviqon.com \
    --to=aardelean@deviqon.com \
    --cc=broonie@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --subject='Re: [RFC PATCH v2] regulator: devres: add devm_regulator_enable() as short-hand' \
    /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

This is a public inbox, see mirroring instructions
on how to clone and mirror all data and code used for this inbox