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

Michał Mirosław posted 9 patches 2 years, 5 months ago
There is a newer version of this series
[PATCH 5/9] regulator/core: regulator_ena_gpio_ctrl: pull in ena_gpio state handling
Posted by Michał Mirosław 2 years, 5 months ago
Deduplicate `ena_gpio_state` handling by pulling it into
regulator_ena_gpio_ctrl().

Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl>
---
 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

Re: [PATCH 5/9] regulator/core: regulator_ena_gpio_ctrl: pull in ena_gpio state handling
Posted by Mark Brown 2 years, 5 months ago
On Wed, Aug 30, 2023 at 11:38:56PM +0200, Michał Mirosław wrote:

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

The goal isn't to write the minimum number of lines possible - this just
makes the logic harder to follow and for bonus points isn't obviously
related to the chnages described in changelog.
Re: [PATCH 5/9] regulator/core: regulator_ena_gpio_ctrl: pull in ena_gpio state handling
Posted by Michał Mirosław 2 years, 5 months ago
On Thu, Aug 31, 2023 at 12:13:16PM +0100, Mark Brown wrote:
> On Wed, Aug 30, 2023 at 11:38:56PM +0200, Michał Mirosław wrote:
> 
> > -		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) {
> 
> The goal isn't to write the minimum number of lines possible - this just
> makes the logic harder to follow and for bonus points isn't obviously
> related to the chnages described in changelog.

I see that I missed this fragment when splitting patches. I'll resend
without this part if the other ones are good to go.

Best Regards
Michał Mirosław