Skip to contentSkip to conversation

gpio: improve support for shared GPIOs

PATCH Superseded Bartosz Golaszewski 7 participants 34 messages Oct 22, 2025 – Nov 18, 2025

Subsystems (9)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover gpio: improve support for shared GPIOs

Problem statement: GPIOs are implemented as a strictly exclusive resource in the kernel but there are lots of platforms on which single pin is shared by multiple devices which don't communicate so need some way of properly sharing access to a GPIO. What we have now is the GPIOD_FLAGS_BIT_NONEXCLUSIVE flag which was introduced as a hack and doesn't do any locking or arbitration of access - it literally just hand the same GPIO descriptor to all interested users.

The proposed solution is composed of three major parts: the high-level, shared GPIO proxy driver that arbitrates access to the shared pin and exposes a regular GPIO chip interface to consumers, a low-level shared GPIOLIB module that scans firmware nodes and creates auxiliary devices that attach to the proxy driver and finally a set of core GPIOLIB changes that plug the former into the GPIO lookup path.

The changes are implemented in a way that allows to seamlessly compile out any code related to sharing GPIOs for systems that don't need it.

The practical use-case for this are the powerdown GPIOs shared by speakers on Qualcomm db845c platform, however I have also extensively tested it using gpio-virtuser on arm64 qemu with various DT configurations.

I'm Cc'ing some people that may help with reviewing/be interested in
this: OF maintainers (because the main target are OF systems initially), Mark Brown because most users of GPIOD_FLAGS_BIT_NONEXCLUSIVE live in audio or regulator drivers and one of the goals of this series is dropping the hand-crafted GPIO enable counting via struct regulator_enable_gpio in regulator core), Andy and Mika because I'd like to also cover ACPI (even though I don't know about any ACPI platform that would need this at the moment, I think it makes sense to make the solution complete), Dmitry (same thing but for software nodes), Mani (because you have a somewhat related use-case for the PERST# signal and I'd like to hear your input on whether this is something you can use or maybe it needs a separate, implicit gpio-perst driver similar to what Krzysztof did for reset-gpios) and Greg (because I mentioned this to you last week in person and I also use the auxiliary bus for the proxy devices).

Merging strategy: patches 1-6 should go through the GPIO tree and then ARM-SoC, ASoC and regulator trees can pull these changes from an immutable branch and apply the remaining patches.

---
Changes in v2:
- Fix a memory leak in error path in gpiolib-shared
- Drop the gpio-wcd934x fix that already went upstream
- Free resources used during scanning by GPIOs that turned out to be
unique
- Rework the OF property scanning
- Add patches making the regulator subsystem aware of shared GPIOs
managed by GPIOLIB
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Bartosz Golaszewski (10):
string: provide strends()
gpiolib: define GPIOD_FLAG_SHARED
gpiolib: implement low-level, shared GPIO support
gpio: shared-proxy: implement the shared GPIO proxy driver
gpiolib: support shared GPIOs in core subsystem code
gpio: provide gpiod_is_shared()
arm64: select HAVE_SHARED_GPIOS for ARCH_QCOM
ASoC: wsa881x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup
ASoC: wsa883x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup
regulator: make the subsystem aware of shared GPIOs

 arch/arm64/Kconfig.platforms     |   1 +
 drivers/gpio/Kconfig             |  17 ++
 drivers/gpio/Makefile            |   2 +
 drivers/gpio/gpio-shared-proxy.c | 329 ++++++++++++++++++++++++
 drivers/gpio/gpiolib-shared.c    | 530 +++++++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib-shared.h    |  71 ++++++
 drivers/gpio/gpiolib.c           |  70 +++++-
 drivers/gpio/gpiolib.h           |   2 +
 drivers/regulator/core.c         |   8 +
 include/linux/gpio/consumer.h    |   9 +
 include/linux/string.h           |   2 +
 lib/string.c                     |  19 ++
 lib/tests/string_kunit.c         |  13 +
 sound/soc/codecs/wsa881x.c       |   3 +-
 sound/soc/codecs/wsa883x.c       |   7 +-
 15 files changed, 1067 insertions(+), 16 deletions(-)

---
base-commit: 304d18863e6e62a8f2d0350ce0a59596e2e42768
change-id: 20250908-gpio-shared-67ec352884b6

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

PATCH v2 1/10 string: provide strends()

From: Bartosz Golaszewski <[email protected]>

Implement a function for checking if a string ends with a different string and add its kunit test cases.

---
 include/linux/string.h   |  2 ++
 lib/string.c             | 19 +++++++++++++++++++
 lib/tests/string_kunit.c | 13 +++++++++++++
 3 files changed, 34 insertions(+)
include/linux/string.h +2 -0
index fdd3442c6bcbd786e177b6e87358e1065a0ffafc..16149404fc3aed535c094b9550f7bd7c9b44a0c9 100644
@@ -562,4 +562,6 @@ static inline bool strstarts(const char *str, const char *prefix)
 	return strncmp(str, prefix, strlen(prefix)) == 0;
 }
 
+bool strends(const char *str, const char *suffix);
+
 #endif /* _LINUX_STRING_H_ */
lib/string.c +19 -0
index b632c71df1a5061256f556c40a30587cf45dce18..ac18313d9c52b38132f0bfdd952cdec6b1354d76 100644
@@ -880,3 +880,22 @@ void *memchr_inv(const void *start, int c, size_t bytes)
 	return check_bytes8(start, value, bytes % 8);
 }
 EXPORT_SYMBOL(memchr_inv);
+
+/**
+ * strends - Check if a string ends with another string.
+ * @str - NULL-terminated string to check against @suffix
+ * @suffix - NULL-terminated string defining the suffix to look for in @str
+ *
+ * Returns:
+ * True if @str ends with @suffix. False in all other cases.
+ */
+bool strends(const char *str, const char *suffix)
+{
+	unsigned int str_len = strlen(str), suffix_len = strlen(suffix);
+
+	if (str_len < suffix_len)
+		return false;
+
+	return !(strcmp(str + str_len - suffix_len, suffix));
+}
+EXPORT_SYMBOL(strends);
lib/tests/string_kunit.c +13 -0
index 0ed7448a26d3aa0fe9e2a6a894d4c49c2c0b86e0..f9a8e557ba7734c9848d58ff986407d8000f52ee 100644
@@ -602,6 +602,18 @@ static void string_test_memtostr(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, dest[7], '\0');
 }
 
+static void string_test_strends(struct kunit *test)
+{
+	KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar"));
+	KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar"));
+	KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar"));
+	KUNIT_EXPECT_TRUE(test, strends("foobar", ""));
+	KUNIT_EXPECT_FALSE(test, strends("bar", "foobar"));
+	KUNIT_EXPECT_FALSE(test, strends("", "foo"));
+	KUNIT_EXPECT_FALSE(test, strends("foobar", "ba"));
+	KUNIT_EXPECT_TRUE(test, strends("", ""));
+}
+
 static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_test_memset16),
 	KUNIT_CASE(string_test_memset32),
@@ -623,6 +635,7 @@ static struct kunit_case string_test_cases[] = {
 	KUNIT_CASE(string_test_strlcat),
 	KUNIT_CASE(string_test_strtomem),
 	KUNIT_CASE(string_test_memtostr),
+	KUNIT_CASE(string_test_strends),
 	{}
 };
 
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH v2 01/10] string: provide strends()

Quoted 3 lines · Bartosz Golaszewski

Implement a function for checking if a string ends with a different string and add its kunit test cases.

...

include/linux/string.h static inline bool strstarts(const char *str, const char *prefix) · line 565

Quoted 7 lines
include/linux/string.h +1 -0
@@ -562,4 +562,6 @@ static inline bool strstarts(const char *str, const char *prefix)
        return strncmp(str, prefix, strlen(prefix)) == 0;
 }
 
+bool strends(const char *str, const char *suffix);

Why not static inline as strstarts()?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v2 01/10] string: provide strends()

Quoted 14 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski

Implement a function for checking if a string ends with a different string and add its kunit test cases.

...

Quoted 6 lines
include/linux/string.h +0 -0
@@ -562,4 +562,6 @@ static inline bool strstarts(const char *str, const char *prefix)
        return strncmp(str, prefix, strlen(prefix)) == 0;
 }
 
Quoted 4 lines

include/linux/string.h static inline bool strstarts(const char *str, const char *prefix) · line 565

Quoted 1 line
include/linux/string.h +1 -0
+bool strends(const char *str, const char *suffix);

Why not static inline as strstarts()?

Because it's not a oneliner.

Bartosz

#4 lore raw

Re: [PATCH v2 01/10] string: provide strends()

Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Wed, Oct 22, 2025 at 4:11 PM Bartosz Golaszewski <[email protected]> wrote:

...

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

static inline bool strstarts(const char *str, const char *prefix)

        return strncmp(str, prefix, strlen(prefix)) == 0;

}

+bool strends(const char *str, const char *suffix);

Why not static inline as strstarts()?

Because it's not a oneliner.

So, and how does it answer the question? What is the obstacle here that it may not be a static inline few-liner?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v2 01/10] string: provide strends()

In reply to Bartosz Golaszewski
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Implement a function for checking if a string ends with a different string and add its kunit test cases.

...

lib/tests/string_kunit.c static void string_test_memtostr(struct kunit *test) · line 615

Quoted 11 lines
lib/tests/string_kunit.c +11 -0
+static void string_test_strends(struct kunit *test)
+{
+	KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar"));
+	KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar"));
+	KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar"));
+	KUNIT_EXPECT_TRUE(test, strends("foobar", ""));
+	KUNIT_EXPECT_FALSE(test, strends("bar", "foobar"));
+	KUNIT_EXPECT_FALSE(test, strends("", "foo"));
+	KUNIT_EXPECT_FALSE(test, strends("foobar", "ba"));
+	KUNIT_EXPECT_TRUE(test, strends("", ""));
+}

Have you checked the binary file? If you want this to be properly implemented, generate the suffix. (Actually making the function static inline makes my point really visible)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v2 01/10] string: provide strends()

Quoted 9 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Implement a function for checking if a string ends with a different string and add its kunit test cases.

...

Quoted 16 lines

lib/tests/string_kunit.c static void string_test_memtostr(struct kunit *test) · line 615

Quoted 11 lines
lib/tests/string_kunit.c +11 -0
+static void string_test_strends(struct kunit *test)
+{
+     KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar"));
+     KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar"));
+     KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar"));
+     KUNIT_EXPECT_TRUE(test, strends("foobar", ""));
+     KUNIT_EXPECT_FALSE(test, strends("bar", "foobar"));
+     KUNIT_EXPECT_FALSE(test, strends("", "foo"));
+     KUNIT_EXPECT_FALSE(test, strends("foobar", "ba"));
+     KUNIT_EXPECT_TRUE(test, strends("", ""));
+}

Have you checked the binary file? If you want this to be properly implemented, generate the suffix. (Actually making the function static inline makes my point really visible)

Andy, this is bikeshedding. This is literally the least important piece of this series. It doesn't matter for the big picture whether this is inlined or not.

Bartosz

#7 lore raw

Re: [PATCH v2 01/10] string: provide strends()

Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Wed, Oct 22, 2025 at 03:10:40PM +0200, Bartosz Golaszewski wrote:

...

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
lib/tests/string_kunit.c +7 -0
+static void string_test_strends(struct kunit *test)
+{
+     KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar"));
+     KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar"));
+     KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar"));
+     KUNIT_EXPECT_TRUE(test, strends("foobar", ""));
+     KUNIT_EXPECT_FALSE(test, strends("bar", "foobar"));
Quoted 8 lines

lib/tests/string_kunit.c static void string_test_memtostr(struct kunit *test) · line 615

Quoted 4 lines
lib/tests/string_kunit.c +4 -0
+     KUNIT_EXPECT_FALSE(test, strends("", "foo"));
+     KUNIT_EXPECT_FALSE(test, strends("foobar", "ba"));
+     KUNIT_EXPECT_TRUE(test, strends("", ""));
+}

Have you checked the binary file? If you want this to be properly implemented, generate the suffix. (Actually making the function static inline makes my point really visible)

Quoted 3 lines

Andy, this is bikeshedding. This is literally the least important piece of this series. It doesn't matter for the big picture whether this is inlined or not.

It's definitely not a bikeshedding. I try to keep a bit consistency here and I don't see the point of bloating a kernel (binary as well) for the function that just a couple of lines with simple basic calls.

Also note that with inlined version strlen() for string literals will be calculated at _compile-time_! This is clear benefit.

Really, library code is not as simple as dropping something to somewhere...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

Re: [PATCH v2 01/10] string: provide strends()

Quoted 28 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Wed, Oct 22, 2025 at 03:10:40PM +0200, Bartosz Golaszewski wrote:

...

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
lib/tests/string_kunit.c +7 -0
+static void string_test_strends(struct kunit *test)
+{
+     KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar"));
+     KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar"));
+     KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar"));
+     KUNIT_EXPECT_TRUE(test, strends("foobar", ""));
+     KUNIT_EXPECT_FALSE(test, strends("bar", "foobar"));
Quoted 8 lines

lib/tests/string_kunit.c static void string_test_memtostr(struct kunit *test) · line 615

Quoted 4 lines
lib/tests/string_kunit.c +4 -0
+     KUNIT_EXPECT_FALSE(test, strends("", "foo"));
+     KUNIT_EXPECT_FALSE(test, strends("foobar", "ba"));
+     KUNIT_EXPECT_TRUE(test, strends("", ""));
+}

Have you checked the binary file? If you want this to be properly implemented, generate the suffix. (Actually making the function static inline makes my point really visible)

Quoted 3 lines

Andy, this is bikeshedding. This is literally the least important piece of this series. It doesn't matter for the big picture whether this is inlined or not.

Quoted 9 lines

It's definitely not a bikeshedding. I try to keep a bit consistency here and I don't see the point of bloating a kernel (binary as well) for the function that just a couple of lines with simple basic calls.

Also note that with inlined version strlen() for string literals will be calculated at _compile-time_! This is clear benefit.

Really, library code is not as simple as dropping something to somewhere...

Ok, whatever I'll make it static inline.

Bart

#9 lore raw

PATCH v2 2/10 gpiolib: define GPIOD_FLAG_SHARED

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Define a new GPIO descriptor flag for marking pins that are shared by multiple consumer. This flag will be used in several places so we need to do it in advance and separately from other changes.

---
 drivers/gpio/gpiolib.h | 1 +
 1 file changed, 1 insertion(+)
drivers/gpio/gpiolib.h +1 -0
index b4c5369f8a3314244424d0c90ba006f7568b314e..ae9aa145ca055c08dad55a537567a27de57f7066 100644
@@ -204,6 +204,7 @@ struct gpio_desc {
 #define GPIOD_FLAG_EDGE_FALLING		17 /* GPIO CDEV detects falling edge events */
 #define GPIOD_FLAG_EVENT_CLOCK_REALTIME	18 /* GPIO CDEV reports REALTIME timestamps in events */
 #define GPIOD_FLAG_EVENT_CLOCK_HTE	19 /* GPIO CDEV reports hardware timestamps in events */
+#define GPIOD_FLAG_SHARED		20 /* GPIO is shared by multiple consumers */
 
 	/* Connection label */
 	struct gpio_desc_label __rcu *label;
Signature · 1 line
2.48.1
#10 lore raw

PATCH v2 3/10 gpiolib: implement low-level, shared GPIO support

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This module scans the device tree (for now only OF nodes are supported but care is taken to make other fwnode implementations easy to integrate) and determines which GPIO lines are shared by multiple users. It stores that information in memory. When the GPIO chip exposing shared lines is registered, the shared GPIO descriptors it exposes are marked as shared and virtual "proxy" devices that mediate access to the shared lines are created. When a consumer of a shared GPIO looks it up, its fwnode lookup is redirected to a just-in-time machine lookup that points to this proxy device.

This code can be compiled out on platforms which don't use shared GPIOs.

---
 drivers/gpio/Kconfig          |   8 +
 drivers/gpio/Makefile         |   1 +
 drivers/gpio/gpiolib-shared.c | 512 ++++++++++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib-shared.h |  71 ++++++
 4 files changed, 592 insertions(+)
drivers/gpio/Kconfig +8 -0
index 7ee3afbc2b05da4d7470e609a0311ecc38727b00..679a7385a9776eef96a86ca4f429ee83ac939362 100644
@@ -6,6 +6,9 @@
 config GPIOLIB_LEGACY
 	def_bool y
 
+config HAVE_SHARED_GPIOS
+	bool
+
 menuconfig GPIOLIB
 	bool "GPIO Support"
 	help
@@ -42,6 +45,11 @@ config GPIOLIB_IRQCHIP
 	select IRQ_DOMAIN
 	bool
 
+config GPIO_SHARED
+	def_bool y
+	depends on HAVE_SHARED_GPIOS || COMPILE_TEST
+	select AUXILIARY_BUS
+
 config OF_GPIO_MM_GPIOCHIP
 	bool
 	help
drivers/gpio/Makefile +1 -0
index ec296fa14bfdb360c27b4578826354762f01f074..f702f7e27e5b4017e7eab3019dae4ec912d534f8 100644
@@ -12,6 +12,7 @@ obj-$(CONFIG_GPIO_SYSFS)	+= gpiolib-sysfs.o
 obj-$(CONFIG_GPIO_ACPI)		+= gpiolib-acpi.o
 gpiolib-acpi-y			:= gpiolib-acpi-core.o gpiolib-acpi-quirks.o
 obj-$(CONFIG_GPIOLIB)		+= gpiolib-swnode.o
+obj-$(CONFIG_GPIO_SHARED)	+= gpiolib-shared.o
 
 # Device drivers. Generally keep list sorted alphabetically
 obj-$(CONFIG_GPIO_REGMAP)	+= gpio-regmap.o
drivers/gpio/gpiolib-shared.c new file +512 -0

512 lines not on this page · raw message

drivers/gpio/gpiolib-shared.h new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.48.1
#11 lore raw

Re: [PATCH v2 03/10] gpiolib: implement low-level, shared GPIO support

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This module scans the device tree (for now only OF nodes are supported but care is taken to make other fwnode implementations easy to integrate) and determines which GPIO lines are shared by multiple users. It stores that information in memory. When the GPIO chip exposing shared lines is registered, the shared GPIO descriptors it exposes are marked as shared and virtual "proxy" devices that mediate access to the shared lines are created. When a consumer of a shared GPIO looks it up, its fwnode lookup is redirected to a just-in-time machine lookup that points to this proxy device.

This code can be compiled out on platforms which don't use shared GPIOs.

...

drivers/gpio/gpiolib-shared.c line 101

Quoted 5 lines
drivers/gpio/gpiolib-shared.c new file +4 -0
+		if (!strends(prop->name, "-gpios") &&
+		    !strends(prop->name, "-gpio") &&
 
+		    strcmp(prop->name, "gpios") != 0 &&
+		    strcmp(prop->name, "gpio") != 0)

We have gpio_suffixes for a reason (also refer to for_each_gpio_property_name() implementation, and yes I understand the difference, this is just a reference for an example of use of the existing list of suffixes).

Quoted 1 line
(quoted diff) +1 -0
+			continue;

...

drivers/gpio/gpiolib-shared.c line 169

Quoted 8 lines
drivers/gpio/gpiolib-shared.c new file +8 -0
+			if (strends(prop->name, "gpios"))
+				suffix = "-gpios";
+			else if (strends(prop->name, "gpio"))
+				suffix = "-gpio";
+			else
+				suffix = NULL;
+			if (!suffix)
+				continue;

In a similar way.

drivers/gpio/gpiolib-shared.c line 172

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+			/* We only set con_id if there's actually one. */
+			if (strcmp(prop->name, "gpios") && strcmp(prop->name, "gpio")) {

And again...

Quoted 9 lines
drivers/gpio/gpiolib-shared.c new file +9 -0
+				ref->con_id = kstrdup(prop->name, GFP_KERNEL);
+				if (!ref->con_id)
+					return -ENOMEM;
+
+				con_id_len = strlen(ref->con_id);
+				suffix_len = strlen(suffix);
+
+				ref->con_id[con_id_len - suffix_len] = '\0';
+			}

...

drivers/gpio/gpiolib-shared.c line 230

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+	adev->dev.parent = gdev->dev.parent;
+	/* No need to dev->release() anything. */

And is it okay?

See drivers/base/core.c:2567

WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",

...

drivers/gpio/gpiolib-shared.c line 243

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+	pr_debug("Created an auxiliary GPIO proxy %s for GPIO device %s\n",
+		 dev_name(&adev->dev), gpio_device_get_label(gdev));

Are you expecting dev_name() to be NULL in some cases? Otherwise why is this not a dev_dbg() call?

Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}

...

drivers/gpio/gpiolib-shared.c line 273

Quoted 4 lines
drivers/gpio/gpiolib-shared.c new file +4 -0
+			char *key __free(kfree) =
+				kasprintf(GFP_KERNEL,
+					  KBUILD_MODNAME ".proxy.%u",
+					  ref->adev.id);

This looks awful. Just allow a bit longer line

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+			if (!key)
+				return -ENOMEM;

...

drivers/gpio/gpiolib-shared.c line 300

Quoted 6 lines
drivers/gpio/gpiolib-shared.c new file +6 -0
+static void gpio_shared_remove_adev(struct auxiliary_device *adev)
+{
+	lockdep_assert_held(&gpio_shared_lock);
+
+	auxiliary_device_uninit(adev);
+	auxiliary_device_delete(adev);

_destroy() ? Esp. taking into account the (wrong?) ordering.

Quoted 1 line

+}

...

drivers/gpio/gpiolib-shared.c line 321

Quoted 1 line
drivers/gpio/gpiolib-shared.c new file +1 -0
+		set_bit(GPIOD_FLAG_SHARED, flags);

Do you need this to be atomic?

drivers/gpio/gpiolib-shared.c line 327

Quoted 6 lines
drivers/gpio/gpiolib-shared.c new file +6 -0
+		/*
+		 * Shared GPIOs are not requested via the normal path. Make
+		 * them inaccessible to anyone even before we register the
+		 * chip.
+		 */
+		set_bit(GPIOD_FLAG_REQUESTED, flags);

Ditto.

...

drivers/gpio/gpiolib-shared.c

Quoted 10 lines
drivers/gpio/gpiolib-shared.c new file +9 -0
+struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev)
+{
+	struct auxiliary_device *adev = to_auxiliary_dev(dev);
+	struct gpio_shared_desc *shared_desc;
+	struct gpio_shared_entry *entry;
+	struct gpio_shared_ref *ref;
+	struct gpio_device *gdev;
+	int ret;
 
+ +	scoped_guard(mutex, &gpio_shared_lock) {

Much better to split the below to a helper and make it run under a scoped_guard() here or call a guard()() there.

Quoted 45 lines
drivers/gpio/gpiolib-shared.c new file +45 -0
+		list_for_each_entry(entry, &gpio_shared_list, list) {
+			list_for_each_entry(ref, &entry->refs, list) {
+				if (adev != &ref->adev)
+					continue;
+
+				if (entry->shared_desc) {
+					kref_get(&entry->ref);
+					shared_desc = entry->shared_desc;
+					break;
+				}
+
+				shared_desc = kzalloc(sizeof(*shared_desc), GFP_KERNEL);
+				if (!shared_desc)
+					return ERR_PTR(-ENOMEM);
+
+				gdev = gpio_device_find_by_fwnode(entry->fwnode);
+				if (!gdev) {
+					kfree(shared_desc);
+					return ERR_PTR(-EPROBE_DEFER);
+				}
+
+				shared_desc->desc = &gdev->descs[entry->offset];
+				shared_desc->can_sleep = gpiod_cansleep(shared_desc->desc);
+				if (shared_desc->can_sleep)
+					mutex_init(&shared_desc->mutex);
+				else
+					spin_lock_init(&shared_desc->spinlock);
+
+				kref_init(&entry->ref);
+				entry->shared_desc = shared_desc;
+
+				pr_debug("Device %s acquired a reference to the shared GPIO %u owned by %s\n",
+					 dev_name(dev), desc_to_gpio(shared_desc->desc),
+					 gpio_device_get_label(gdev));
+				break;
+			}
+		}
+	}
+
+	ret = devm_add_action_or_reset(dev, gpiod_shared_put, entry);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return shared_desc;
+}

...

Quoted 4 lines

+/*
+ * This is only called if gpio_shared_init() fails so it's in fact __init and
+ * not __exit.
+ */

Fine. Have you checked if there are any section mismatch warnings during kernel (post)build?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH v2 03/10] gpiolib: implement low-level, shared GPIO support

Quoted 21 lines · Andy Shevchenko
Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This module scans the device tree (for now only OF nodes are supported but care is taken to make other fwnode implementations easy to integrate) and determines which GPIO lines are shared by multiple users. It stores that information in memory. When the GPIO chip exposing shared lines is registered, the shared GPIO descriptors it exposes are marked as shared and virtual "proxy" devices that mediate access to the shared lines are created. When a consumer of a shared GPIO looks it up, its fwnode lookup is redirected to a just-in-time machine lookup that points to this proxy device.

This code can be compiled out on platforms which don't use shared GPIOs.

...

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+             if (!strends(prop->name, "-gpios") &&
+                 !strends(prop->name, "-gpio") &&
Quoted 7 lines

drivers/gpio/gpiolib-shared.c line 101

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+                 strcmp(prop->name, "gpios") != 0 &&
+                 strcmp(prop->name, "gpio") != 0)

We have gpio_suffixes for a reason (also refer to for_each_gpio_property_name() implementation, and yes I understand the difference, this is just a reference for an example of use of the existing list of suffixes).

And how would you use them here - when you also need the hyphen - without multiple dynamic allocations instead of static strings?

Quoted 10 lines

drivers/gpio/gpiolib-shared.c line 230

Quoted 3 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+     adev->dev.parent = gdev->dev.parent;
+     /* No need to dev->release() anything. */

And is it okay?

See drivers/base/core.c:2567

WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",

Huh... you're not wrong but I haven't seen this warning. Do people just use empty functions in this case?

Quoted 8 lines

...

drivers/gpio/gpiolib-shared.c line 243

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+     pr_debug("Created an auxiliary GPIO proxy %s for GPIO device %s\n",
+              dev_name(&adev->dev), gpio_device_get_label(gdev));

Are you expecting dev_name() to be NULL in some cases? Otherwise why is this not a dev_dbg() call?

It's the low-level code saying: "I created device X for Y", not "Hey, here's X, I'm here for Y".

Quoted 4 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+     return 0;
+}
Quoted 9 lines

...

drivers/gpio/gpiolib-shared.c line 273

Quoted 4 lines
drivers/gpio/gpiolib-shared.c new file +4 -0
+                     char *key __free(kfree) =
+                             kasprintf(GFP_KERNEL,
+                                       KBUILD_MODNAME ".proxy.%u",
+                                       ref->adev.id);

This looks awful. Just allow a bit longer line

Ok

Quoted 5 lines
Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+                     if (!key)
+                             return -ENOMEM;

...

Quoted 9 lines

drivers/gpio/gpiolib-shared.c line 300

Quoted 6 lines
drivers/gpio/gpiolib-shared.c new file +6 -0
+static void gpio_shared_remove_adev(struct auxiliary_device *adev)
+{
+     lockdep_assert_held(&gpio_shared_lock);
+
+     auxiliary_device_uninit(adev);
+     auxiliary_device_delete(adev);

_destroy() ? Esp. taking into account the (wrong?) ordering.

You're right about the order but if you _add() then you should _delete(). You typically _destroy() if you earlier _create().

Quoted 8 lines
Quoted 1 line

+}

...

Quoted 1 line

+ set_bit(GPIOD_FLAG_SHARED, flags);

Do you need this to be atomic?

Quoted 9 lines

drivers/gpio/gpiolib-shared.c line 327

Quoted 6 lines
drivers/gpio/gpiolib-shared.c new file +6 -0
+             /*
+              * Shared GPIOs are not requested via the normal path. Make
+              * them inaccessible to anyone even before we register the
+              * chip.
+              */
+             set_bit(GPIOD_FLAG_REQUESTED, flags);

Ditto.

Ok

Quoted 11 lines

...

Quoted 8 lines
drivers/gpio/gpiolib-shared.c new file +8 -0
+struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev)
+{
+     struct auxiliary_device *adev = to_auxiliary_dev(dev);
+     struct gpio_shared_desc *shared_desc;
+     struct gpio_shared_entry *entry;
+     struct gpio_shared_ref *ref;
+     struct gpio_device *gdev;
+     int ret;
Quoted 5 lines
Quoted 1 line

+ + scoped_guard(mutex, &gpio_shared_lock) {

Much better to split the below to a helper and make it run under a scoped_guard() here or call a guard()() there.

I'm not following, please rephrase.

Quoted 48 lines
Quoted 45 lines
drivers/gpio/gpiolib-shared.c new file +45 -0
+             list_for_each_entry(entry, &gpio_shared_list, list) {
+                     list_for_each_entry(ref, &entry->refs, list) {
+                             if (adev != &ref->adev)
+                                     continue;
+
+                             if (entry->shared_desc) {
+                                     kref_get(&entry->ref);
+                                     shared_desc = entry->shared_desc;
+                                     break;
+                             }
+
+                             shared_desc = kzalloc(sizeof(*shared_desc), GFP_KERNEL);
+                             if (!shared_desc)
+                                     return ERR_PTR(-ENOMEM);
+
+                             gdev = gpio_device_find_by_fwnode(entry->fwnode);
+                             if (!gdev) {
+                                     kfree(shared_desc);
+                                     return ERR_PTR(-EPROBE_DEFER);
+                             }
+
+                             shared_desc->desc = &gdev->descs[entry->offset];
+                             shared_desc->can_sleep = gpiod_cansleep(shared_desc->desc);
+                             if (shared_desc->can_sleep)
+                                     mutex_init(&shared_desc->mutex);
+                             else
+                                     spin_lock_init(&shared_desc->spinlock);
+
+                             kref_init(&entry->ref);
+                             entry->shared_desc = shared_desc;
+
+                             pr_debug("Device %s acquired a reference to the shared GPIO %u owned by %s\n",
+                                      dev_name(dev), desc_to_gpio(shared_desc->desc),
+                                      gpio_device_get_label(gdev));
+                             break;
+                     }
+             }
+     }
+
+     ret = devm_add_action_or_reset(dev, gpiod_shared_put, entry);
+     if (ret)
+             return ERR_PTR(ret);
+
+     return shared_desc;
+}

...

Quoted 8 lines
Quoted 4 lines

+/*
+ * This is only called if gpio_shared_init() fails so it's in fact __init and
+ * not __exit.
+ */

Fine. Have you checked if there are any section mismatch warnings during kernel (post)build?

Yes, there are none.

Bartosz

#13 lore raw

Re: [PATCH v2 03/10] gpiolib: implement low-level, shared GPIO support

Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Wed, Oct 22, 2025 at 03:10:42PM +0200, Bartosz Golaszewski wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
drivers/gpio/gpiolib-shared.c new file +4 -0
+             if (!strends(prop->name, "-gpios") &&
+                 !strends(prop->name, "-gpio") &&
 
+                 strcmp(prop->name, "gpios") != 0 &&
+                 strcmp(prop->name, "gpio") != 0)
Quoted 6 lines
Quoted 3 lines

We have gpio_suffixes for a reason (also refer to for_each_gpio_property_name() implementation, and yes I understand the difference, this is just a reference for an example of use of the existing list of suffixes).

And how would you use them here - when you also need the hyphen - without multiple dynamic allocations instead of static strings?

Something like

	char suffix[6];
	bool found = false;

	for_each_gpio_property_name(suffix, "")
		found = found || strends();
	for_each_gpio_property_name(suffix, NULL)
		found = found || (strcmp() == 0);
	if (!found)
		continue;

Of course with more thinking this may be optimized to avoid snprintf() (probably with a new helper macro or so).

But see my next reply, I found something more interesting.

...

Quoted 10 lines
Quoted 7 lines
Quoted 1 line

+ /* No need to dev->release() anything. */

And is it okay?

See drivers/base/core.c:2567

WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",

Huh... you're not wrong but I haven't seen this warning. Do people just use empty functions in this case?

I dunno. Maybe something applies a default release in you case? Can you investigate that?

...

Quoted 8 lines
Quoted 5 lines

drivers/gpio/gpiolib-shared.c line 243

Quoted 2 lines
drivers/gpio/gpiolib-shared.c new file +2 -0
+     pr_debug("Created an auxiliary GPIO proxy %s for GPIO device %s\n",
+              dev_name(&adev->dev), gpio_device_get_label(gdev));

Are you expecting dev_name() to be NULL in some cases? Otherwise why is this not a dev_dbg() call?

It's the low-level code saying: "I created device X for Y", not "Hey, here's X, I'm here for Y".

OK.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+     return 0;
+}

...

Quoted 10 lines
Quoted 9 lines

drivers/gpio/gpiolib-shared.c line 300

Quoted 6 lines
drivers/gpio/gpiolib-shared.c new file +6 -0
+static void gpio_shared_remove_adev(struct auxiliary_device *adev)
+{
+     lockdep_assert_held(&gpio_shared_lock);
+
+     auxiliary_device_uninit(adev);
+     auxiliary_device_delete(adev);

_destroy() ? Esp. taking into account the (wrong?) ordering.

Quoted 2 lines

You're right about the order but if you _add() then you should _delete(). You typically _destroy() if you earlier _create().

Maybe, but as we noticed above my suggestion would make the code less error prone to begin with.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
drivers/gpio/gpiolib-shared.c new file +6 -0
+     struct auxiliary_device *adev = to_auxiliary_dev(dev);
+     struct gpio_shared_desc *shared_desc;
+     struct gpio_shared_entry *entry;
+     struct gpio_shared_ref *ref;
+     struct gpio_device *gdev;
+     int ret;
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

+ + scoped_guard(mutex, &gpio_shared_lock) {

Much better to split the below to a helper and make it run under a scoped_guard() here or call a guard()() there.

I'm not following, please rephrase.

	scoped_guard() {
		call_my_new_helper_which_is_easier to read();
	}

	ret = devm_add_...

OR

call_my_new_helper_which_is_easier to read()

{
	guard()()

	...
}
	call_my_new_helper_which_is_easier to read();

	ret = devm_add_...
Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
drivers/gpio/gpiolib-shared.c new file +45 -0
+             list_for_each_entry(entry, &gpio_shared_list, list) {
+                     list_for_each_entry(ref, &entry->refs, list) {
+                             if (adev != &ref->adev)
+                                     continue;
+
+                             if (entry->shared_desc) {
+                                     kref_get(&entry->ref);
+                                     shared_desc = entry->shared_desc;
+                                     break;
+                             }
+
+                             shared_desc = kzalloc(sizeof(*shared_desc), GFP_KERNEL);
+                             if (!shared_desc)
+                                     return ERR_PTR(-ENOMEM);
+
+                             gdev = gpio_device_find_by_fwnode(entry->fwnode);
+                             if (!gdev) {
+                                     kfree(shared_desc);
+                                     return ERR_PTR(-EPROBE_DEFER);
+                             }
+
+                             shared_desc->desc = &gdev->descs[entry->offset];
+                             shared_desc->can_sleep = gpiod_cansleep(shared_desc->desc);
+                             if (shared_desc->can_sleep)
+                                     mutex_init(&shared_desc->mutex);
+                             else
+                                     spin_lock_init(&shared_desc->spinlock);
+
+                             kref_init(&entry->ref);
+                             entry->shared_desc = shared_desc;
+
+                             pr_debug("Device %s acquired a reference to the shared GPIO %u owned by %s\n",
+                                      dev_name(dev), desc_to_gpio(shared_desc->desc),
+                                      gpio_device_get_label(gdev));
+                             break;
+                     }
+             }
+     }
+
+     ret = devm_add_action_or_reset(dev, gpiod_shared_put, entry);
+     if (ret)
+             return ERR_PTR(ret);
+
+     return shared_desc;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw

Re: [PATCH v2 03/10] gpiolib: implement low-level, shared GPIO support

Quoted 18 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Wed, Oct 22, 2025 at 03:10:42PM +0200, Bartosz Golaszewski wrote:

...

Quoted 10 lines
Quoted 9 lines

drivers/gpio/gpiolib-shared.c line 101

Quoted 5 lines
drivers/gpio/gpiolib-shared.c new file +4 -0
+             if (!strends(prop->name, "-gpios") &&
+                 !strends(prop->name, "-gpio") &&
 
+                 strcmp(prop->name, "gpios") != 0 &&
+                 strcmp(prop->name, "gpio") != 0)

We have gpio_suffixes for a reason (also refer to for_each_gpio_property_name() implementation, and yes I understand the difference, this is just a reference for an example of use of the existing list of suffixes).

Quoted 6 lines
Quoted 2 lines

And how would you use them here - when you also need the hyphen - without multiple dynamic allocations instead of static strings?

Something like

        char suffix[6];

Well that is quite fragile, isn't it? Not only does it require 7 characters but if we ever add a "-gpios+1" suffix, it will not work correctly. At some point you just have to open-code these things for better readability. I doubt you save any code with this.

Quoted 9 lines
        bool found = false;

        for_each_gpio_property_name(suffix, "")
                found = found || strends();
        for_each_gpio_property_name(suffix, NULL)
                found = found || (strcmp() == 0);
        if (!found)
                continue;
Quoted 5 lines

Of course with more thinking this may be optimized to avoid snprintf() (probably with a new helper macro or so).

But see my next reply, I found something more interesting.

I must be missing it. I don't know what you're referring to.

Quoted 8 lines

...

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

+ /* No need to dev->release() anything. */

And is it okay?

See drivers/base/core.c:2567

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

WARN(1, KERN_ERR "Device '%s' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",

Huh... you're not wrong but I haven't seen this warning. Do people just use empty functions in this case?

I dunno. Maybe something applies a default release in you case? Can you investigate that?

Ah, this only happens when the release is triggered, not at registration. If I force a release, I see it alright.

Bart

#15 lore raw

PATCH v2 4/10 gpio: shared-proxy: implement the shared GPIO proxy driver

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add a virtual GPIO proxy driver which arbitrates access to a single shared GPIO by multiple users. It works together with the core shared GPIO support from GPIOLIB and functions by acquiring a reference to a shared GPIO descriptor exposed by gpiolib-shared and making sure that the state of the GPIO stays consistent.

In general: if there's only one user at the moment: allow it to do anything as if this was a normal GPIO (in essence: just propagate calls to the underlying real hardware driver). If there are more users: don't allow to change the direction set by the initial user, allow to change configuration options but warn about possible conflicts and finally:
treat the output-high value as a reference counted, logical "GPIO enabled" setting, meaning: the GPIO value is set to high when the first user requests it to be high and back to low once the last user stops "voting" for high.

---
 drivers/gpio/Kconfig             |   9 ++
 drivers/gpio/Makefile            |   1 +
 drivers/gpio/gpio-shared-proxy.c | 329 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 339 insertions(+)
drivers/gpio/Kconfig +9 -0
index 679a7385a9776eef96a86ca4f429ee83ac939362..de8e4febf344fc19a633cd7efe8412fe12166fb8 100644
@@ -2025,6 +2025,15 @@ config GPIO_SIM
 	  This enables the GPIO simulator - a configfs-based GPIO testing
 	  driver.
 
+config GPIO_SHARED_PROXY
+	tristate "Proxy driver for non-exclusive GPIOs"
+	default m
+	depends on GPIO_SHARED || COMPILE_TEST
+	select AUXILIARY_BUS
+	help
+	  This enables the GPIO shared proxy driver - an abstraction layer
+	  for GPIO pins that are shared by multiple devices.
+
 endmenu
 
 menu "GPIO Debugging utilities"
drivers/gpio/Makefile +1 -0
index f702f7e27e5b4017e7eab3019dae4ec912d534f8..d0020bc70b84f6fb9949165070c21725a60f47e2 100644
@@ -160,6 +160,7 @@ obj-$(CONFIG_ARCH_SA1100)		+= gpio-sa1100.o
 obj-$(CONFIG_GPIO_SAMA5D2_PIOBU)	+= gpio-sama5d2-piobu.o
 obj-$(CONFIG_GPIO_SCH311X)		+= gpio-sch311x.o
 obj-$(CONFIG_GPIO_SCH)			+= gpio-sch.o
+obj-$(CONFIG_GPIO_SHARED_PROXY)		+= gpio-shared-proxy.o
 obj-$(CONFIG_GPIO_SIFIVE)		+= gpio-sifive.o
 obj-$(CONFIG_GPIO_SIM)			+= gpio-sim.o
 obj-$(CONFIG_GPIO_SIOX)			+= gpio-siox.o
drivers/gpio/gpio-shared-proxy.c new file +329 -0

329 lines not on this page · raw message

Signature · 1 line
2.48.1
#16 lore raw

Re: [PATCH v2 04/10] gpio: shared-proxy: implement the shared GPIO proxy driver

Quoted 7 lines · Bartosz Golaszewski

Add a virtual GPIO proxy driver which arbitrates access to a single shared GPIO by multiple users. It works together with the core shared GPIO support from GPIOLIB and functions by acquiring a reference to a shared GPIO descriptor exposed by gpiolib-shared and making sure that the state of the GPIO stays consistent.

Quoted 9 lines

In general: if there's only one user at the moment: allow it to do anything as if this was a normal GPIO (in essence: just propagate calls to the underlying real hardware driver). If there are more users: don't allow to change the direction set by the initial user, allow to change configuration options but warn about possible conflicts and finally:
treat the output-high value as a reference counted, logical "GPIO enabled" setting, meaning: the GPIO value is set to high when the first user requests it to be high and back to low once the last user stops "voting" for high.

I have two Q:s about the design:
1) why can't the value be counted on the struct gpio_desc level?
2) can gpio-aggregator facilities be reused (to some extent)?

...

drivers/gpio/gpio-shared-proxy.c line 11

Quoted 6 lines
drivers/gpio/gpio-shared-proxy.c new file +6 -0
+#include <linux/auxiliary_bus.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/module.h>

+ types.h

Quoted 1 line

+#include "gpiolib-shared.h"

...

drivers/gpio/gpio-shared-proxy.c line 78

Quoted 7 lines
drivers/gpio/gpio-shared-proxy.c new file +7 -0
+out:
+	if (shared_desc->highcnt)
+		dev_dbg(proxy->dev,
+			"Voted for value '%s', effective value is 'high', number of votes for 'high': %u\n",
+			value ? "high" : "low", shared_desc->highcnt);
+	else
+		dev_dbg(proxy->dev, "Voted for value 'low', effective value is 'low'\n");

You can unify and maybe save a few bytes here and there by doing something like this:

	const char *tmp; // name is a placeholder

	tmp = str_high_low(shared_desc->highcnt);
	dev_dbg(proxy->dev,
		"Voted for value '%s', effective value is '%s', number of votes for '%s': %u\n",
		str_high_low(value), tmp, tmp, shared_desc->highcnt);

...

drivers/gpio/gpio-shared-proxy.c line 182

Quoted 3 lines
drivers/gpio/gpio-shared-proxy.c new file +3 -0
+		dev_dbg(proxy->dev,
+			"Only one user of this shared GPIO, allowing to set direction to output with value '%s'\n",
+			value ? "high" : "low");

str_high_low() ?

Quoted 14 lines
drivers/gpio/gpio-shared-proxy.c new file +14 -0
+		ret = gpiod_direction_output(desc, value);
+		if (ret)
+			return ret;
+
+		if (value) {
+			proxy->voted_high = true;
+			shared_desc->highcnt = 1;
+		} else {
+			proxy->voted_high = false;
+			shared_desc->highcnt = 0;
+		}
+
+		return 0;
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#17 lore raw

Re: [PATCH v2 04/10] gpio: shared-proxy: implement the shared GPIO proxy driver

Quoted 19 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

Add a virtual GPIO proxy driver which arbitrates access to a single shared GPIO by multiple users. It works together with the core shared GPIO support from GPIOLIB and functions by acquiring a reference to a shared GPIO descriptor exposed by gpiolib-shared and making sure that the state of the GPIO stays consistent.

Quoted 9 lines

In general: if there's only one user at the moment: allow it to do anything as if this was a normal GPIO (in essence: just propagate calls to the underlying real hardware driver). If there are more users: don't allow to change the direction set by the initial user, allow to change configuration options but warn about possible conflicts and finally:
treat the output-high value as a reference counted, logical "GPIO enabled" setting, meaning: the GPIO value is set to high when the first user requests it to be high and back to low once the last user stops "voting" for high.

Quoted 2 lines

I have two Q:s about the design:
1) why can't the value be counted on the struct gpio_desc level?

No, I specifically tried to limit the impact on users not needing this to a minimum.

Quoted 1 line

2) can gpio-aggregator facilities be reused (to some extent)?

I don't see how but if you have a precise idea, let me know.

Quoted 27 lines

...

drivers/gpio/gpio-shared-proxy.c line 11

Quoted 6 lines
drivers/gpio/gpio-shared-proxy.c new file +6 -0
+#include <linux/auxiliary_bus.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/module.h>

+ types.h

Quoted 1 line

+#include "gpiolib-shared.h"

...

drivers/gpio/gpio-shared-proxy.c line 78

Quoted 7 lines
drivers/gpio/gpio-shared-proxy.c new file +7 -0
+out:
+     if (shared_desc->highcnt)
+             dev_dbg(proxy->dev,
+                     "Voted for value '%s', effective value is 'high', number of votes for 'high': %u\n",
+                     value ? "high" : "low", shared_desc->highcnt);
+     else
+             dev_dbg(proxy->dev, "Voted for value 'low', effective value is 'low'\n");

You can unify and maybe save a few bytes here and there by doing something like this:

Quoted 7 lines
        const char *tmp; // name is a placeholder

        tmp = str_high_low(shared_desc->highcnt);
        dev_dbg(proxy->dev,
                "Voted for value '%s', effective value is '%s', number of votes for '%s': %u\n",
                str_high_low(value), tmp, tmp, shared_desc->highcnt);

This doesn't make sense, we don't "vote for low". We only vote for high. It's not: which option gets the most votes, it's: if there's at least one vote for high, then we go high.

Bart

#18 lore raw

PATCH v2 5/10 gpiolib: support shared GPIOs in core subsystem code

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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.

---
 drivers/gpio/gpiolib.c | 50 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 9 deletions(-)
drivers/gpio/gpiolib.c +41 -9
index ba5df8a233fe75e16bae615f7f7c8591066c056e..03fd60e787fcf5846519fb2db9a701770ed15d6b 100644
@@ -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);
@@ -4646,11 +4655,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");
@@ -4673,14 +4700,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;
 	}
 
Signature · 1 line
2.48.1
#19 lore raw

PATCH v2 6/10 gpio: provide gpiod_is_shared()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide an interface allowing consumers to check if a GPIO descriptor represents a GPIO that can potentially be shared by multiple consumers at the same time. This is exposed to allow subsystems that already work around the limitations of the current non-exclusive GPIO handling in some ways, to gradually convert to relying on the new shared GPIO feature of GPIOLIB.

Extend the gpiolib-shared module to mark the GPIO shared proxy descriptors with a flag checked by the new interface.

---
 drivers/gpio/gpiolib-shared.c | 18 ++++++++++++++++++
 drivers/gpio/gpiolib.c        | 20 ++++++++++++++++++++
 drivers/gpio/gpiolib.h        |  1 +
 include/linux/gpio/consumer.h |  9 +++++++++
 4 files changed, 48 insertions(+)
drivers/gpio/gpiolib-shared.c +18 -0
index d2087d0df4ab7bcd23d3736e341c0f57cd748af4..b5c33bccff0765eb940fedd5d678c47a05f1a25d 100644
@@ -309,6 +309,24 @@ int gpio_device_setup_shared(struct gpio_device *gdev)
 
 	guard(mutex)(&gpio_shared_lock);
 
+	list_for_each_entry(entry, &gpio_shared_list, list) {
+		list_for_each_entry(ref, &entry->refs, list) {
+			if (gdev->dev.parent == &ref->adev.dev) {
+				/*
+				 * This is a shared GPIO proxy. Mark its
+				 * descriptor as such and return here.
+				 */
+				set_bit(GPIOD_FLAG_SHARED_PROXY,
+					&gdev->descs[0].flags);
+				return 0;
+			}
+		}
+	}
+
+	/*
+	 * This is not a shared GPIO proxy but it still may be the device
+	 * exposing shared pins. Find them and create the proxy devices.
+	 */
 	list_for_each_entry(entry, &gpio_shared_list, list) {
 		if (!device_match_fwnode(&gdev->dev, entry->fwnode))
 			continue;
drivers/gpio/gpiolib.c +20 -0
index 03fd60e787fcf5846519fb2db9a701770ed15d6b..d689065471a1269ac0671b964c67670df5c95dd7 100644
@@ -3984,6 +3984,26 @@ int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
 }
 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
 
+/**
+ * gpiod_is_shared() - check if this GPIO can be shared by multiple consumers
+ * @desc: GPIO to inspect
+ *
+ * Returns:
+ * True if this GPIO can be shared by multiple consumers at once. False if it's
+ * a regular, exclusive GPIO.
+ *
+ * Note:
+ * This function returning true does not mean that this GPIO is currently being
+ * shared. It means the GPIO core has registered the fact that the firmware
+ * configuration indicates that it can be shared by multiple consumers and is
+ * in charge of arbitrating the access.
+ */
+bool gpiod_is_shared(const struct gpio_desc *desc)
+{
+	return test_bit(GPIOD_FLAG_SHARED_PROXY, &desc->flags);
+}
+EXPORT_SYMBOL_GPL(gpiod_is_shared);
+
 /**
  * gpiod_to_irq() - return the IRQ corresponding to a GPIO
  * @desc: gpio whose IRQ will be returned (already requested)
drivers/gpio/gpiolib.h +1 -0
index ae9aa145ca055c08dad55a537567a27de57f7066..8deed8a7d1128f01f0696af040d06f5fe140c7f8 100644
@@ -205,6 +205,7 @@ struct gpio_desc {
 #define GPIOD_FLAG_EVENT_CLOCK_REALTIME	18 /* GPIO CDEV reports REALTIME timestamps in events */
 #define GPIOD_FLAG_EVENT_CLOCK_HTE	19 /* GPIO CDEV reports hardware timestamps in events */
 #define GPIOD_FLAG_SHARED		20 /* GPIO is shared by multiple consumers */
+#define GPIOD_FLAG_SHARED_PROXY		21 /* GPIO is a virtual proxy to a physically shared pin. */
 
 	/* Connection label */
 	struct gpio_desc_label __rcu *label;
include/linux/gpio/consumer.h +9 -0
index 00df68c514051434e6fa67dc2307c6a8ce4ce3df..a8acb7c0b5af5066fd05e533468fc28616c68d78 100644
@@ -167,6 +167,8 @@ int gpiod_cansleep(const struct gpio_desc *desc);
 int gpiod_to_irq(const struct gpio_desc *desc);
 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
 
+bool gpiod_is_shared(const struct gpio_desc *desc);
+
 /* Convert between the old gpio_ and new gpiod_ interfaces */
 struct gpio_desc *gpio_to_desc(unsigned gpio);
 int desc_to_gpio(const struct gpio_desc *desc);
@@ -520,6 +522,13 @@ static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
 	return -EINVAL;
 }
 
+static inline bool gpiod_is_shared(const struct gpio_desc *desc)
+{
+	/* GPIO can never have been requested */
+	WARN_ON(desc);
+	return false;
+}
+
 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
 {
 	return NULL;
Signature · 1 line
2.48.1
Reviewed-by on v2 6/10
#20 lore raw

Re: [PATCH v2 06/10] gpio: provide gpiod_is_shared()

Quoted 9 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide an interface allowing consumers to check if a GPIO descriptor represents a GPIO that can potentially be shared by multiple consumers at the same time. This is exposed to allow subsystems that already work around the limitations of the current non-exclusive GPIO handling in some ways, to gradually convert to relying on the new shared GPIO feature of GPIOLIB.

Quoted 4 lines

Extend the gpiolib-shared module to mark the GPIO shared proxy descriptors with a flag checked by the new interface.

Yours,
Linus Walleij

#21 lore raw

Re: [PATCH v2 06/10] gpio: provide gpiod_is_shared()

Quoted 12 lines · Linus Walleij
Quoted 9 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide an interface allowing consumers to check if a GPIO descriptor represents a GPIO that can potentially be shared by multiple consumers at the same time. This is exposed to allow subsystems that already work around the limitations of the current non-exclusive GPIO handling in some ways, to gradually convert to relying on the new shared GPIO feature of GPIOLIB.

Quoted 7 lines
Quoted 4 lines

Extend the gpiolib-shared module to mark the GPIO shared proxy descriptors with a flag checked by the new interface.

I think you wanted to leave this under v3?

Bart

#22 lore raw

Re: [PATCH v2 06/10] gpio: provide gpiod_is_shared()

Quoted 17 lines · Bartosz Golaszewski
Quoted 15 lines · Linus Walleij
Quoted 12 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide an interface allowing consumers to check if a GPIO descriptor represents a GPIO that can potentially be shared by multiple consumers at the same time. This is exposed to allow subsystems that already work around the limitations of the current non-exclusive GPIO handling in some ways, to gradually convert to relying on the new shared GPIO feature of GPIOLIB.

Extend the gpiolib-shared module to mark the GPIO shared proxy descriptors with a flag checked by the new interface.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I think you wanted to leave this under v3?

Yeah probably, a bit messy in my inbox!

Yours,
Linus Walleij

#23 lore raw

PATCH v2 7/10 arm64: select HAVE_SHARED_GPIOS for ARCH_QCOM

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Some qualcomm platforms use shared GPIOs. Enable support for them by selecting the Kconfig switch provided by GPIOLIB.

---
 arch/arm64/Kconfig.platforms | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/Kconfig.platforms +1 -0
index 13173795c43d4f28e2d47acc700f80a165d44671..3dbff0261f0add0516d8cb3fd0f29e277af94f20 100644
@@ -316,6 +316,7 @@ config ARCH_QCOM
 	select GPIOLIB
 	select PINCTRL
 	select HAVE_PWRCTRL if PCI
+	select HAVE_SHARED_GPIOS
 	help
 	  This enables support for the ARMv8 based Qualcomm chipsets.
 
Signature · 1 line
2.48.1
#24 lore raw

PATCH v2 8/10 ASoC: wsa881x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This driver is only used on Qualcomm platforms which now select HAVE_SHARED_GPIOS so this flag can be dropped.

---
 sound/soc/codecs/wsa881x.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
sound/soc/codecs/wsa881x.c +1 -2
index 636e59abc3772fc0b333873a329b65f4213c3ef3..92a1e3bb8371e178571a6c1ed6f1185fe6c2e757 100644
@@ -1112,8 +1112,7 @@ static int wsa881x_probe(struct sdw_slave *pdev,
 	if (!wsa881x)
 		return -ENOMEM;
 
-	wsa881x->sd_n = devm_gpiod_get_optional(dev, "powerdown",
-						GPIOD_FLAGS_BIT_NONEXCLUSIVE);
+	wsa881x->sd_n = devm_gpiod_get_optional(dev, "powerdown", 0);
 	if (IS_ERR(wsa881x->sd_n))
 		return dev_err_probe(dev, PTR_ERR(wsa881x->sd_n),
 				     "Shutdown Control GPIO not found\n");
Signature · 1 line
2.48.1
maintainer Acked-by on v2 8/10
#25 lore raw

Re: [PATCH v2 08/10] ASoC: wsa881x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup

Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This driver is only used on Qualcomm platforms which now select HAVE_SHARED_GPIOS so this flag can be dropped.

#26 lore raw

Re: [PATCH v2 08/10] ASoC: wsa881x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup

In reply to Bartosz Golaszewski
Quoted 6 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This driver is only used on Qualcomm platforms which now select HAVE_SHARED_GPIOS so this flag can be dropped.

I tested the whole series on db845 RB3 board, looks ok.

Thanks,
Alexey

#27 lore raw

PATCH v2 9/10 ASoC: wsa883x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This driver is only used on Qualcomm platforms which now select HAVE_SHARED_GPIOS so this flag can be dropped.

---
 sound/soc/codecs/wsa883x.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
sound/soc/codecs/wsa883x.c +2 -5
index ca4520ade79aa2c9377449e29cdd33e9e8dd28c5..ba60b4de6cab7740cd8aa70c89f6e03e1dc2dd12 100644
@@ -1572,13 +1572,10 @@ static int wsa883x_get_reset(struct device *dev, struct wsa883x_priv *wsa883x)
 	if (IS_ERR(wsa883x->sd_reset))
 		return dev_err_probe(dev, PTR_ERR(wsa883x->sd_reset),
 				     "Failed to get reset\n");
-	/*
-	 * if sd_reset: NULL, so use the backwards compatible way for powerdown-gpios,
-	 * which does not handle sharing GPIO properly.
-	 */
+
+	 /* if sd_reset: NULL, so use the backwards compatible way for powerdown-gpios */
 	if (!wsa883x->sd_reset) {
 		wsa883x->sd_n = devm_gpiod_get_optional(dev, "powerdown",
-							GPIOD_FLAGS_BIT_NONEXCLUSIVE |
 							GPIOD_OUT_HIGH);
 		if (IS_ERR(wsa883x->sd_n))
 			return dev_err_probe(dev, PTR_ERR(wsa883x->sd_n),
Signature · 1 line
2.48.1
maintainer Acked-by on v2 9/10
#28 lore raw

Re: [PATCH v2 09/10] ASoC: wsa883x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup

Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This driver is only used on Qualcomm platforms which now select HAVE_SHARED_GPIOS so this flag can be dropped.

#29 lore raw

PATCH v2 10/10 regulator: make the subsystem aware of shared GPIOs

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIOLIB is now aware of shared GPIOs and - for platforms where access to such pins is managed internally - we don't need to keep track of the enable count.

Once all users in the kernel switch to using the new mechanism, we'll be able to drop the internal counting of users from the regulator code.

---
 drivers/regulator/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/regulator/core.c +8 -0
index dd7b10e768c06c83d2a4fb5dfd0fce8d796c9185..f9690f0689fda85123d07967f3c87105829e330c 100644
@@ -2616,6 +2616,13 @@ static int regulator_ena_gpio_request(struct regulator_dev *rdev,
 
 	mutex_lock(&regulator_list_mutex);
 
+	if (gpiod_is_shared(gpiod))
+		/*
+		 * The sharing of this GPIO pin is managed internally by
+		 * GPIOLIB. We don't need to keep track of its enable count.
+		 */
+		goto skip_compare;
+
 	list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
 		if (gpiod_is_equal(pin->gpiod, gpiod)) {
 			rdev_dbg(rdev, "GPIO is already used\n");
@@ -2628,6 +2635,7 @@ static int regulator_ena_gpio_request(struct regulator_dev *rdev,
 		return -ENOMEM;
 	}
 
+skip_compare:
 	pin = new_pin;
 	new_pin = NULL;
 
Signature · 1 line
2.48.1
maintainer Acked-by on v2 10/10
#30 lore raw

Re: [PATCH v2 10/10] regulator: make the subsystem aware of shared GPIOs

Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

GPIOLIB is now aware of shared GPIOs and - for platforms where access to such pins is managed internally - we don't need to keep track of the enable count.

Once all users in the kernel switch to using the new mechanism, we'll be able to drop the internal counting of users from the regulator code.

#31 lore raw
In reply to Bartosz Golaszewski
Quoted 7 lines · Bartosz Golaszewski

Problem statement: GPIOs are implemented as a strictly exclusive resource in the kernel but there are lots of platforms on which single pin is shared by multiple devices which don't communicate so need some way of properly sharing access to a GPIO. What we have now is the GPIOD_FLAGS_BIT_NONEXCLUSIVE flag which was introduced as a hack and doesn't do any locking or arbitration of access - it literally just hand the same GPIO descriptor to all interested users.

I had few stabs on this in the past, all got somehow derailed, one example was:
https://lkml.org/lkml/2019/10/30/311

Quoted 81 lines

The proposed solution is composed of three major parts: the high-level, shared GPIO proxy driver that arbitrates access to the shared pin and exposes a regular GPIO chip interface to consumers, a low-level shared GPIOLIB module that scans firmware nodes and creates auxiliary devices that attach to the proxy driver and finally a set of core GPIOLIB changes that plug the former into the GPIO lookup path.

The changes are implemented in a way that allows to seamlessly compile out any code related to sharing GPIOs for systems that don't need it.

The practical use-case for this are the powerdown GPIOs shared by speakers on Qualcomm db845c platform, however I have also extensively tested it using gpio-virtuser on arm64 qemu with various DT configurations.

I'm Cc'ing some people that may help with reviewing/be interested in
this: OF maintainers (because the main target are OF systems initially), Mark Brown because most users of GPIOD_FLAGS_BIT_NONEXCLUSIVE live in audio or regulator drivers and one of the goals of this series is dropping the hand-crafted GPIO enable counting via struct regulator_enable_gpio in regulator core), Andy and Mika because I'd like to also cover ACPI (even though I don't know about any ACPI platform that would need this at the moment, I think it makes sense to make the solution complete), Dmitry (same thing but for software nodes), Mani (because you have a somewhat related use-case for the PERST# signal and I'd like to hear your input on whether this is something you can use or maybe it needs a separate, implicit gpio-perst driver similar to what Krzysztof did for reset-gpios) and Greg (because I mentioned this to you last week in person and I also use the auxiliary bus for the proxy devices).

Merging strategy: patches 1-6 should go through the GPIO tree and then ARM-SoC, ASoC and regulator trees can pull these changes from an immutable branch and apply the remaining patches.

---
Changes in v2:
- Fix a memory leak in error path in gpiolib-shared
- Drop the gpio-wcd934x fix that already went upstream
- Free resources used during scanning by GPIOs that turned out to be
unique
- Rework the OF property scanning
- Add patches making the regulator subsystem aware of shared GPIOs
managed by GPIOLIB
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Bartosz Golaszewski (10):
string: provide strends()
gpiolib: define GPIOD_FLAG_SHARED
gpiolib: implement low-level, shared GPIO support
gpio: shared-proxy: implement the shared GPIO proxy driver
gpiolib: support shared GPIOs in core subsystem code
gpio: provide gpiod_is_shared()
arm64: select HAVE_SHARED_GPIOS for ARCH_QCOM
ASoC: wsa881x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup
ASoC: wsa883x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup
regulator: make the subsystem aware of shared GPIOs

 arch/arm64/Kconfig.platforms     |   1 +
 drivers/gpio/Kconfig             |  17 ++
 drivers/gpio/Makefile            |   2 +
 drivers/gpio/gpio-shared-proxy.c | 329 ++++++++++++++++++++++++
 drivers/gpio/gpiolib-shared.c    | 530 +++++++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib-shared.h    |  71 ++++++
 drivers/gpio/gpiolib.c           |  70 +++++-
 drivers/gpio/gpiolib.h           |   2 +
 drivers/regulator/core.c         |   8 +
 include/linux/gpio/consumer.h    |   9 +
 include/linux/string.h           |   2 +
 lib/string.c                     |  19 ++
 lib/tests/string_kunit.c         |  13 +
 sound/soc/codecs/wsa881x.c       |   3 +-
 sound/soc/codecs/wsa883x.c       |   7 +-
 15 files changed, 1067 insertions(+), 16 deletions(-)

---
base-commit: 304d18863e6e62a8f2d0350ce0a59596e2e42768
change-id: 20250908-gpio-shared-67ec352884b6

Best regards,

Signature · 1 line
Péter
#32 lore raw
Quoted 12 lines · Péter Ujfalusi
Quoted 7 lines · Bartosz Golaszewski

Problem statement: GPIOs are implemented as a strictly exclusive resource in the kernel but there are lots of platforms on which single pin is shared by multiple devices which don't communicate so need some way of properly sharing access to a GPIO. What we have now is the GPIOD_FLAGS_BIT_NONEXCLUSIVE flag which was introduced as a hack and doesn't do any locking or arbitration of access - it literally just hand the same GPIO descriptor to all interested users.

Quoted 4 lines

I had few stabs on this in the past, all got somehow derailed, one example was:
https://lkml.org/lkml/2019/10/30/311

The main issue I see with this approach is adding an actual device node for the shared GPIO which is now not accepted in DT bindings. We only create nodes for actual HW components. All the information is already in the device-tree, we just need to scan it which is what I'm trying to do here.

Bartosz

#33 lore raw
Quoted 14 lines · Bartosz Golaszewski
Quoted 12 lines · Péter Ujfalusi
Quoted 7 lines · Bartosz Golaszewski

Problem statement: GPIOs are implemented as a strictly exclusive resource in the kernel but there are lots of platforms on which single pin is shared by multiple devices which don't communicate so need some way of properly sharing access to a GPIO. What we have now is the GPIOD_FLAGS_BIT_NONEXCLUSIVE flag which was introduced as a hack and doesn't do any locking or arbitration of access - it literally just hand the same GPIO descriptor to all interested users.

Quoted 8 lines
Quoted 4 lines

I had few stabs on this in the past, all got somehow derailed, one example was:
https://lkml.org/lkml/2019/10/30/311

The main issue I see with this approach is adding an actual device node for the shared GPIO which is now not accepted in DT bindings. We only create nodes for actual HW components.

Right, that policy came later, true.

All the information is

Quoted 2 lines

already in the device-tree, we just need to scan it which is what I'm trying to do here.

I had a prototype later without the sofware-node which worked for the use case I had, but over the years I dropped it, it was a bit of hassle to roll it for nothing.

One can argue that the shared-gpio node is describing the solder blob where the GPIO line is split and routed to two different component. With the approach one can handle cases where the level is inverted by a passive component for one of the device for example - unfortunately I have seen such a board marvel as well.

The device's binding will tell _how_ it expects the GPIO's active level, which might or might not match with the real level of the GPIO line and if one of the device's branch have an inverter, that is going to be interesting to work out in conjunction with the other devices non inverted 'direct' line to the same GPIO.

Never the less, it is great that someone is trying to get this supported!

Quoted 2 lines

Bartosz

Signature · 1 line
Péter