linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Kees Cook <kees@kernel.org>, Mika Westerberg <westeri@kernel.org>,
	 Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	 Linus Walleij <linus.walleij@linaro.org>,
	 Manivannan Sadhasivam <mani@kernel.org>,
	Rob Herring <robh@kernel.org>,
	 Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>,
	Saravana Kannan <saravanak@google.com>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Andy Shevchenko <andy@kernel.org>,
	Bartosz Golaszewski <brgl@bgdev.pl>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	 Srinivas Kandagatla <srini@kernel.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	 Mark Brown <broonie@kernel.org>,
	Jaroslav Kysela <perex@perex.cz>,  Takashi Iwai <tiwai@suse.com>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-gpio@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	 linux-sound@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: [PATCH RFC 6/9] gpiolib: support shared GPIOs in core subsystem code
Date: Wed, 24 Sep 2025 16:51:34 +0200	[thread overview]
Message-ID: <20250924-gpio-shared-v1-6-775e7efeb1a3@linaro.org> (raw)
In-Reply-To: <20250924-gpio-shared-v1-0-775e7efeb1a3@linaro.org>

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

As the final step in adding official support for shared GPIOs, enable
the previously added elements in core GPIO subsystem code. Set-up shared
GPIOs when adding a GPIO chip, tear it down on removal and check if a
GPIO descriptor looked up during the firmware-node stage is shared and
fall-back to machine lookup in this case.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpiolib.c | 50 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 9 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 9952e412da505c12ceddfefd9cfd778aad3bd5e6..1e4c991797126d0d91233bbe7c3ae3831bf77d6c 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -37,6 +37,7 @@
 #include "gpiolib-acpi.h"
 #include "gpiolib-cdev.h"
 #include "gpiolib-of.h"
+#include "gpiolib-shared.h"
 #include "gpiolib-swnode.h"
 #include "gpiolib-sysfs.h"
 #include "gpiolib.h"
@@ -1200,6 +1201,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	if (ret)
 		goto err_remove_irqchip_mask;
 
+	ret = gpio_device_setup_shared(gdev);
+	if (ret)
+		goto err_remove_irqchip;
+
 	/*
 	 * By first adding the chardev, and then adding the device,
 	 * we get a device node entry in sysfs under
@@ -1211,10 +1216,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	if (gpiolib_initialized) {
 		ret = gpiochip_setup_dev(gdev);
 		if (ret)
-			goto err_remove_irqchip;
+			goto err_teardown_shared;
 	}
+
 	return 0;
 
+err_teardown_shared:
+	gpio_device_teardown_shared(gdev);
 err_remove_irqchip:
 	gpiochip_irqchip_remove(gc);
 err_remove_irqchip_mask:
@@ -1283,6 +1291,7 @@ void gpiochip_remove(struct gpio_chip *gc)
 	/* Numb the device, cancelling all outstanding operations */
 	rcu_assign_pointer(gdev->chip, NULL);
 	synchronize_srcu(&gdev->srcu);
+	gpio_device_teardown_shared(gdev);
 	gpiochip_irqchip_remove(gc);
 	acpi_gpiochip_remove(gc);
 	of_gpiochip_remove(gc);
@@ -4652,11 +4661,29 @@ struct gpio_desc *gpiod_find_and_request(struct device *consumer,
 	scoped_guard(srcu, &gpio_devices_srcu) {
 		desc = gpiod_fwnode_lookup(fwnode, consumer, con_id, idx,
 					   &flags, &lookupflags);
+		if (!IS_ERR_OR_NULL(desc) &&
+		    test_bit(GPIOD_FLAG_SHARED, &desc->flags)) {
+			/*
+			 * We're dealing with a GPIO shared by multiple
+			 * consumers. This is the moment to add the machine
+			 * lookup table for the proxy device as previously
+			 * we only knew the consumer's fwnode.
+			 */
+			ret = gpio_shared_add_proxy_lookup(consumer, lookupflags);
+			if (ret)
+				return ERR_PTR(ret);
+
+			/* Trigger platform lookup for shared GPIO proxy. */
+			desc = ERR_PTR(-ENOENT);
+			/* Trigger it even for fwnode-only gpiod_get(). */
+			platform_lookup_allowed = true;
+		}
+
 		if (gpiod_not_found(desc) && platform_lookup_allowed) {
 			/*
 			 * Either we are not using DT or ACPI, or their lookup
-			 * did not return a result. In that case, use platform
-			 * lookup as a fallback.
+			 * did not return a result or this is a shared GPIO. In
+			 * that case, use platform lookup as a fallback.
 			 */
 			dev_dbg(consumer,
 				"using lookup tables for GPIO lookup\n");
@@ -4679,14 +4706,19 @@ struct gpio_desc *gpiod_find_and_request(struct device *consumer,
 			return ERR_PTR(ret);
 
 		/*
-		 * This happens when there are several consumers for
-		 * the same GPIO line: we just return here without
-		 * further initialization. It is a bit of a hack.
-		 * This is necessary to support fixed regulators.
+		 * This happens when there are several consumers for the same
+		 * GPIO line: we just return here without further
+		 * initialization. It's a hack introduced long ago to support
+		 * fixed regulators. We now have a better solution with
+		 * automated scanning where affected platforms just need to
+		 * select the provided Kconfig option.
 		 *
-		 * FIXME: Make this more sane and safe.
+		 * FIXME: Remove the GPIOD_FLAGS_BIT_NONEXCLUSIVE flag after
+		 * making sure all platforms use the new mechanism.
 		 */
-		dev_info(consumer, "nonexclusive access to GPIO for %s\n", name);
+		dev_info(consumer,
+			 "nonexclusive access to GPIO for %s, consider updating your code to using gpio-shared-proxy\n",
+			 name);
 		return desc;
 	}
 

-- 
2.48.1



  parent reply	other threads:[~2025-09-24 14:52 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-24 14:51 [PATCH RFC 0/9] gpio: improve support for shared GPIOs Bartosz Golaszewski
2025-09-24 14:51 ` [PATCH RFC 1/9] gpio: wcd934x: mark the GPIO controller as sleeping Bartosz Golaszewski
2025-09-24 14:51 ` [PATCH RFC 2/9] string: provide strends() Bartosz Golaszewski
2025-09-24 14:51 ` [PATCH RFC 3/9] gpiolib: define GPIOD_FLAG_SHARED Bartosz Golaszewski
2025-09-24 14:51 ` [PATCH RFC 4/9] gpiolib: implement low-level, shared GPIO support Bartosz Golaszewski
2025-09-24 14:51 ` [PATCH RFC 5/9] gpio: shared-proxy: implement the shared GPIO proxy driver Bartosz Golaszewski
2025-09-24 14:51 ` Bartosz Golaszewski [this message]
2025-09-24 14:51 ` [PATCH RFC 7/9] arm64: select HAVE_SHARED_GPIOS for ARCH_QCOM Bartosz Golaszewski
2025-09-24 14:51 ` [PATCH RFC 8/9] ASoC: wsa881x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup Bartosz Golaszewski
2025-09-24 14:51 ` [PATCH RFC 9/9] ASoC: wsa883x: " Bartosz Golaszewski
2025-09-24 18:25 ` [PATCH RFC 0/9] gpio: improve support for shared GPIOs Dmitry Torokhov
2025-09-24 18:58   ` Bartosz Golaszewski
2025-10-06 15:43   ` Manivannan Sadhasivam
2025-10-06 16:10     ` Bartosz Golaszewski
2025-10-06 21:52       ` Srinivas Kandagatla
2025-10-06 22:09       ` Manivannan Sadhasivam
2025-10-07 12:29         ` Bartosz Golaszewski
2025-10-21  1:53           ` Manivannan Sadhasivam
2025-10-21 12:06             ` Bartosz Golaszewski
2025-10-21 12:19               ` Manivannan Sadhasivam
2025-10-21 12:22                 ` Bartosz Golaszewski
2025-10-21 12:53                   ` Manivannan Sadhasivam
2025-10-21 14:02                     ` Bartosz Golaszewski
     [not found]                     ` <CAGb2v65acHoO5025ZN7DhX0xVQf6JyHmUK3CB9UhnmTDDHq6vg@mail.gmail.com>
2025-11-05  9:31                       ` PCIe link training and pwrctrl sequence Manivannan Sadhasivam
2025-10-21 15:56               ` [PATCH RFC 0/9] gpio: improve support for shared GPIOs Andy Shevchenko
2025-10-01  8:49 ` Linus Walleij
2025-10-01 10:53   ` Linus Walleij
2025-10-01 13:04     ` Bartosz Golaszewski
2025-10-04 13:31 ` Srinivas Kandagatla
2025-10-06 11:55   ` Bartosz Golaszewski
2025-10-06 21:55     ` Srinivas Kandagatla
2025-10-07 13:13       ` Bartosz Golaszewski
2025-10-07 13:18         ` Mark Brown
2025-10-07 13:21           ` Bartosz Golaszewski
2025-10-06 12:19   ` Mark Brown
2025-10-07 12:32     ` Srinivas Kandagatla
2025-10-09 10:12 ` (subset) " Bartosz Golaszewski
2025-10-17 17:26 ` Bartosz Golaszewski
2025-10-17 17:32   ` Mark Brown
2025-10-20  9:41     ` Bartosz Golaszewski
2025-10-21 14:08       ` Mark Brown
2025-10-21 14:42         ` Bartosz Golaszewski
2025-10-21 15:02           ` Mark Brown

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=20250924-gpio-shared-v1-6-775e7efeb1a3@linaro.org \
    --to=brgl@bgdev.pl \
    --cc=akpm@linux-foundation.org \
    --cc=andy@kernel.org \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=broonie@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=conor+dt@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=kees@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=mani@kernel.org \
    --cc=perex@perex.cz \
    --cc=robh@kernel.org \
    --cc=saravanak@google.com \
    --cc=srini@kernel.org \
    --cc=tiwai@suse.com \
    --cc=westeri@kernel.org \
    --cc=will@kernel.org \
    /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).