public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Charles Keepax <ckeepax@opensource.cirrus.com>
To: <broonie@kernel.org>
Cc: <linus.walleij@linaro.org>, <lgirdwood@gmail.com>,
	<m.szyprowski@samsung.com>, <linux-kernel@vger.kernel.org>,
	<patches@opensource.cirrus.com>
Subject: [PATCH 2/3] regulator: Only free GPIOs if the core requested them
Date: Thu, 22 Nov 2018 17:30:14 +0000	[thread overview]
Message-ID: <20181122173015.23905-2-ckeepax@opensource.cirrus.com> (raw)
In-Reply-To: <20181122173015.23905-1-ckeepax@opensource.cirrus.com>

Currently, the regulator core will take ownership of any GPIO passed
into it. Makes end driver code fairly error prone as the normal devm_
patterns of allocation don't work. Update the regulator core to only
free the GPIO if it requested it, this allows the drivers to manage the
GPIO lifetime as they normally would.

Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
---
 drivers/regulator/core.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index dbe2f2e6e6254..9da7d27c7145e 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -83,6 +83,7 @@ struct regulator_enable_gpio {
 	u32 enable_count;	/* a number of enabled shared GPIO */
 	u32 request_count;	/* a number of requested shared GPIO */
 	unsigned int ena_gpio_invert:1;
+	unsigned int locally_requested:1;
 };
 
 /*
@@ -2233,19 +2234,20 @@ static int regulator_ena_gpio_request(struct regulator_dev *rdev,
 		}
 	}
 
+	pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
+	if (pin == NULL)
+		return -ENOMEM;
+
 	if (!config->ena_gpiod) {
 		ret = gpio_request_one(config->ena_gpio,
 				       GPIOF_DIR_OUT | config->ena_gpio_flags,
 				       rdev_get_name(rdev));
-		if (ret)
+		if (ret) {
+			kfree(pin);
 			return ret;
-	}
+		}
 
-	pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
-	if (pin == NULL) {
-		if (!config->ena_gpiod)
-			gpio_free(config->ena_gpio);
-		return -ENOMEM;
+		pin->locally_requested = 1;
 	}
 
 	pin->gpiod = gpiod;
@@ -2270,7 +2272,8 @@ static void regulator_ena_gpio_free(struct regulator_dev *rdev)
 		if (pin->gpiod == rdev->ena_pin->gpiod) {
 			if (pin->request_count <= 1) {
 				pin->request_count = 0;
-				gpiod_put(pin->gpiod);
+				if (pin->locally_requested)
+					gpiod_put(pin->gpiod);
 				list_del(&pin->list);
 				kfree(pin);
 				rdev->ena_pin = NULL;
-- 
2.11.0


  reply	other threads:[~2018-11-22 17:30 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20181122173037epcas1p39fb96bb168427d58a74a085f42a0ba84@epcas1p3.samsung.com>
2018-11-22 17:30 ` [PATCH 1/3] regulator: wm8994: Revert back to using devres Charles Keepax
2018-11-22 17:30   ` Charles Keepax [this message]
2018-11-22 22:25     ` [PATCH 2/3] regulator: Only free GPIOs if the core requested them Linus Walleij
2018-11-23  9:24     ` Marek Szyprowski
2018-11-23 14:25     ` Mark Brown
2018-11-30 22:15       ` Linus Walleij
2018-11-22 17:30   ` [PATCH 3/3] gpio: Add reference counting for non-exclusive GPIOs Charles Keepax
2018-11-23  9:25     ` Marek Szyprowski
2018-11-23  9:40     ` Linus Walleij
2018-11-23 10:57       ` Charles Keepax
2018-11-23 13:25         ` Mark Brown
2018-11-26 13:00           ` Charles Keepax
2018-11-26 14:09             ` Mark Brown
2018-11-26 14:30               ` Charles Keepax
2018-11-26 14:54                 ` Mark Brown
2018-11-26 16:53                   ` Charles Keepax
2018-11-26 21:53           ` Linus Walleij
2018-11-27  9:18             ` Charles Keepax
2018-11-27 10:50             ` Linus Walleij
2018-11-27 13:30             ` Mark Brown
2018-11-23  9:24   ` [PATCH 1/3] regulator: wm8994: Revert back to using devres Marek Szyprowski

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=20181122173015.23905-2-ckeepax@opensource.cirrus.com \
    --to=ckeepax@opensource.cirrus.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=patches@opensource.cirrus.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox