linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Walleij <linus.walleij@linaro.org>
To: Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Charles Keepax <ckeepax@opensource.cirrus.com>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Linus Walleij <linus.walleij@linaro.org>
Subject: [PATCH 09/15 v4] gpio: devres: Handle nonexclusive GPIOs
Date: Thu,  6 Dec 2018 13:43:45 +0100	[thread overview]
Message-ID: <20181206124351.10155-10-linus.walleij@linaro.org> (raw)
In-Reply-To: <20181206124351.10155-1-linus.walleij@linaro.org>

When we get a nonexeclusive GPIO descriptor using managed
resources, we should only add it to the list of managed
resources once: on the first user. Augment the
devm_gpiod_get_index() and devm_gpiod_get_from_of_node()
calls to account for this by checking if the descriptor
is already resource managed before we proceed to allocate
a new resource management struct.

Cc: Mark Brown <broonie@kernel.org>
Cc: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Fixes: b0ce7b29bfcd ("regulator/gpio: Allow nonexclusive GPIO access")
Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
ChangeLog v3->v4:
- Pass a pointer to a pointer &desc instead of just the
  descriptor pointer: it is the memory address storing the
  pointer that hooks into resource management.
ChangeLog v1->v3:
- New patch.
- This fix is in response to an issue Marek saw in the fixups
  for resource-managed GPIO descriptors used with ena_gpiod
  in the regulator subsystem.
- I first thought to apply it to the GPIO tree directly, but
  since it is not a regression it is better to put it into
  the regulator tree with the rest of the patches.
---
 drivers/gpio/gpiolib-devres.c | 50 ++++++++++++++++++++++++++---------
 1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/drivers/gpio/gpiolib-devres.c b/drivers/gpio/gpiolib-devres.c
index 01959369360b..f9591b5c9748 100644
--- a/drivers/gpio/gpiolib-devres.c
+++ b/drivers/gpio/gpiolib-devres.c
@@ -98,15 +98,28 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
 	struct gpio_desc **dr;
 	struct gpio_desc *desc;
 
+	desc = gpiod_get_index(dev, con_id, idx, flags);
+	if (IS_ERR(desc))
+		return desc;
+
+	/*
+	 * For non-exclusive GPIO descriptors, check if this descriptor is
+	 * already under resource management by this device.
+	 */
+	if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
+		struct devres *dres;
+
+		dres = devres_find(dev, devm_gpiod_release,
+				   devm_gpiod_match, &desc);
+		if (dres)
+			return desc;
+	}
+
 	dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
 			  GFP_KERNEL);
-	if (!dr)
+	if (!dr) {
+		gpiod_put(desc);
 		return ERR_PTR(-ENOMEM);
-
-	desc = gpiod_get_index(dev, con_id, idx, flags);
-	if (IS_ERR(desc)) {
-		devres_free(dr);
-		return desc;
 	}
 
 	*dr = desc;
@@ -140,15 +153,28 @@ struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
 	struct gpio_desc **dr;
 	struct gpio_desc *desc;
 
+	desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
+	if (IS_ERR(desc))
+		return desc;
+
+	/*
+	 * For non-exclusive GPIO descriptors, check if this descriptor is
+	 * already under resource management by this device.
+	 */
+	if (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
+		struct devres *dres;
+
+		dres = devres_find(dev, devm_gpiod_release,
+				   devm_gpiod_match, &desc);
+		if (dres)
+			return desc;
+	}
+
 	dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
 			  GFP_KERNEL);
-	if (!dr)
+	if (!dr) {
+		gpiod_put(desc);
 		return ERR_PTR(-ENOMEM);
-
-	desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
-	if (IS_ERR(desc)) {
-		devres_free(dr);
-		return desc;
 	}
 
 	*dr = desc;
-- 
2.19.2


  parent reply	other threads:[~2018-12-06 12:44 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20181206124518epcas4p2fd2837b5bc5e0c806e93ce752599e264@epcas4p2.samsung.com>
2018-12-06 12:43 ` [PATCH 00/15 v4] Regulator ena_gpiod fixups Linus Walleij
2018-12-06 12:43   ` [PATCH 01/15 v4] regulator: core: Track dangling GPIO descriptors Linus Walleij
2018-12-06 12:43   ` [PATCH 02/15 v4] regulator: fixed: Let core handle GPIO descriptor Linus Walleij
2018-12-06 12:43   ` [PATCH 03/15 v4] regulator: lm363x: " Linus Walleij
2018-12-06 12:43   ` [PATCH 04/15 v4] regulator: lp8788-ldo: " Linus Walleij
2018-12-06 12:43   ` [PATCH 05/15 v4] regulator: max8952: " Linus Walleij
2018-12-06 12:43   ` [PATCH 06/15 v4] gpio: Export gpiod_get_from_of_node() Linus Walleij
2018-12-06 12:43   ` [PATCH 07/15 v4] regulator: max77686: Let core handle GPIO descriptor Linus Walleij
2018-12-06 12:43   ` [PATCH 08/15 v4] gpio: Enable nonexclusive gpiods from DT nodes Linus Walleij
2018-12-06 12:43   ` Linus Walleij [this message]
2018-12-06 12:43   ` [PATCH 10/15 v4] gpio: Add devm_gpiod_unhinge() Linus Walleij
2018-12-06 12:43   ` [PATCH 11/15 v4] regulator: max8973: Let core handle GPIO descriptor Linus Walleij
2018-12-06 12:43   ` [PATCH 12/15 v4] regulator: da9211: Hand over GPIO to regulator core Linus Walleij
2018-12-06 12:43   ` [PATCH 13/15 v4] regulator: s5m8767: " Linus Walleij
2018-12-06 12:43   ` [PATCH 14/15 v4] regulator: tps65090: " Linus Walleij
2018-12-06 12:43   ` [PATCH 15/15 v4] regulator: s2mps11: " Linus Walleij
2018-12-06 13:27   ` [PATCH 00/15 v4] Regulator ena_gpiod fixups Marek Szyprowski
2018-12-06 13:49     ` Charles Keepax
2018-12-06 14:41       ` Linus Walleij

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=20181206124351.10155-10-linus.walleij@linaro.org \
    --to=linus.walleij@linaro.org \
    --cc=bgolaszewski@baylibre.com \
    --cc=broonie@kernel.org \
    --cc=ckeepax@opensource.cirrus.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.szyprowski@samsung.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;
as well as URLs for NNTP newsgroup(s).