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] regulator: devres: disable regulator on release if refcount is 1
Date: Fri, 25 Jun 2021 15:53:07 +0300	[thread overview]
Message-ID: <20210625125307.330831-1-aardelean@deviqon.com> (raw)

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);

   ...

Naturally, the first thought was to try to move this construct into
regulator core, but I remembered that a devm_regulator_enable() function
isn't all that loved.
The construct above looks like it could become a short-hand (in the form of
devm_regulator_enable()), somewhere in the regulator framework.

After going back through the previous discussions [referenced below, sorry
if I missed any], it looks like maybe an idea would be to call
regulator_disable() right before regulator_put() inside
devm_regulator_release(). But we need to call it only if the 'enable_count'
is 1.

This means that the last 'regulator_disable()' (on driver remove) becomes
optional.
If there are any unbalanced regulator_enable()/regulator_disable() calls,
the 'enable_count' won't be touched and 'regulator_put()' will print a
warning.
The condition could be made to check if 'enable_count >= 1', and the
behavior would be the same, but it's probably a good idea not to touch this
refcount if isn't 1.

The only disadvantage to this approach, is that it changes the order in the
drivers in which the register_disable() gets called, with respect to other
steps in the probe/remove order.
With this, the register_disable() will be called right before the consumer
reference is free'd.

But the other advantage is that regulator_disable() calls can be removed
in simple drivers, where the consumer reference has been requested
with devm_regulator_get().

References:
  https://lore.kernel.org/lkml/20170213023249.GA27688@dtor-ws/
  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>
---

Note: this patch applies indepently of this series:
  https://lore.kernel.org/lkml/20210625122324.327585-1-aardelean@deviqon.com/

 drivers/regulator/devres.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
index 826c29499d69..1852afc02990 100644
--- a/drivers/regulator/devres.c
+++ b/drivers/regulator/devres.c
@@ -16,7 +16,12 @@
 
 static void devm_regulator_release(struct device *dev, void *res)
 {
-	regulator_put(*(struct regulator **)res);
+	struct regulator *regulator = *(struct regulator **)res;
+
+	if (regulator->enable_count == 1)
+		regulator_disable(regulator);
+
+	regulator_put(regulator);
 }
 
 static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
-- 
2.31.1


             reply	other threads:[~2021-06-25 12:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-25 12:53 Alexandru Ardelean [this message]
2021-06-28 14:53 ` Mark Brown
2021-06-29  7:55   ` Alexandru Ardelean
2021-06-30 12:49 ` [RFC PATCH v2] regulator: devres: add devm_regulator_enable() as short-hand Alexandru Ardelean

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=20210625125307.330831-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] regulator: devres: disable regulator on release if refcount is 1' \
    /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