[RFC PATCH v2] regulator: devres: add devm_regulator_enable() as short-hand

From: Alexandru Ardelean
Date: Wed Jun 30 2021 - 08:49:42 EST


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@xxxxxxxxxxx/
[2] https://lore.kernel.org/lkml/20170213023249.GA27688@dtor-ws/
[3] https://lkml.org/lkml/2014/2/4/940

Cc: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
Cc: Guenter Roeck <linux@xxxxxxxxxxxx>
Signed-off-by: Alexandru Ardelean <aardelean@xxxxxxxxxxx>
---
.../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