[PATCH 5/9] regulator/core: regulator_ena_gpio_ctrl: pull in ena_gpio state handling

From: Michał Mirosław
Date: Wed Aug 30 2023 - 18:47:49 EST


Deduplicate `ena_gpio_state` handling by pulling it into
regulator_ena_gpio_ctrl().

Signed-off-by: Michał Mirosław <mirq-linux@xxxxxxxxxxxx>
---
drivers/regulator/core.c | 30 +++++++++++-------------------
1 file changed, 11 insertions(+), 19 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 63d16fe59e84..c8d1b12ee43b 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -2601,6 +2601,9 @@ static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
if (!pin)
return -EINVAL;

+ if (rdev->ena_gpio_state == enable)
+ return 0;
+
if (enable) {
/* Enable GPIO at initial use */
if (pin->enable_count == 0)
@@ -2608,18 +2611,14 @@ static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)

pin->enable_count++;
} else {
- if (pin->enable_count > 1) {
- pin->enable_count--;
- return 0;
- }
-
/* Disable GPIO if not used */
- if (pin->enable_count <= 1) {
+ if (pin->enable_count-- <= 1) {
gpiod_set_value_cansleep(pin->gpiod, 0);
pin->enable_count = 0;
}
}

+ rdev->ena_gpio_state = enable;
return 0;
}

@@ -2720,12 +2719,9 @@ static int _regulator_do_enable(struct regulator_dev *rdev)
}

if (rdev->ena_pin) {
- if (!rdev->ena_gpio_state) {
- ret = regulator_ena_gpio_ctrl(rdev, true);
- if (ret < 0)
- return ret;
- rdev->ena_gpio_state = 1;
- }
+ ret = regulator_ena_gpio_ctrl(rdev, true);
+ if (ret < 0)
+ return ret;
} else if (rdev->desc->ops->enable) {
ret = rdev->desc->ops->enable(rdev);
if (ret < 0)
@@ -2938,13 +2934,9 @@ static int _regulator_do_disable(struct regulator_dev *rdev)
trace_regulator_disable(rdev_get_name(rdev));

if (rdev->ena_pin) {
- if (rdev->ena_gpio_state) {
- ret = regulator_ena_gpio_ctrl(rdev, false);
- if (ret < 0)
- return ret;
- rdev->ena_gpio_state = 0;
- }
-
+ ret = regulator_ena_gpio_ctrl(rdev, false);
+ if (ret < 0)
+ return ret;
} else if (rdev->desc->ops->disable) {
ret = rdev->desc->ops->disable(rdev);
if (ret != 0)
--
2.39.2