Merge tag 'gpio/shared-gpios-for-v6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux.git into gpio/for-next

Immutable branch between the GPIO, ASoC and regulator trees for v6.19-rc1

Add better support for GPIOs shared by multiple consumers.
This commit is contained in:
Bartosz Golaszewski
2025-11-17 10:37:37 +01:00
11 changed files with 1085 additions and 9 deletions
+1
View File
@@ -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.
+17
View File
@@ -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 DEBUG_GPIO
bool "Debug GPIO calls"
depends on DEBUG_KERNEL
@@ -2017,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"
+2
View File
@@ -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
@@ -160,6 +161,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
+333
View File
@@ -0,0 +1,333 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2025 Linaro Ltd.
*/
#include <linux/auxiliary_bus.h>
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/driver.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/string_choices.h>
#include <linux/types.h>
#include "gpiolib-shared.h"
struct gpio_shared_proxy_data {
struct gpio_chip gc;
struct gpio_shared_desc *shared_desc;
struct device *dev;
bool voted_high;
};
static int
gpio_shared_proxy_set_unlocked(struct gpio_shared_proxy_data *proxy,
int (*set_func)(struct gpio_desc *desc, int value),
int value)
{
struct gpio_shared_desc *shared_desc = proxy->shared_desc;
struct gpio_desc *desc = shared_desc->desc;
int ret = 0;
gpio_shared_lockdep_assert(shared_desc);
if (value) {
/* User wants to set value to high. */
if (proxy->voted_high)
/* Already voted for high, nothing to do. */
goto out;
/* Haven't voted for high yet. */
if (!shared_desc->highcnt) {
/*
* Current value is low, need to actually set value
* to high.
*/
ret = set_func(desc, 1);
if (ret)
goto out;
}
shared_desc->highcnt++;
proxy->voted_high = true;
goto out;
}
/* Desired value is low. */
if (!proxy->voted_high)
/* We didn't vote for high, nothing to do. */
goto out;
/* We previously voted for high. */
if (shared_desc->highcnt == 1) {
/* This is the last remaining vote for high, set value to low. */
ret = set_func(desc, 0);
if (ret)
goto out;
}
shared_desc->highcnt--;
proxy->voted_high = false;
out:
if (shared_desc->highcnt)
dev_dbg(proxy->dev,
"Voted for value '%s', effective value is 'high', number of votes for 'high': %u\n",
str_high_low(value), shared_desc->highcnt);
else
dev_dbg(proxy->dev, "Voted for value 'low', effective value is 'low'\n");
return ret;
}
static int gpio_shared_proxy_request(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
struct gpio_shared_desc *shared_desc = proxy->shared_desc;
guard(gpio_shared_desc_lock)(shared_desc);
proxy->shared_desc->usecnt++;
dev_dbg(proxy->dev, "Shared GPIO requested, number of users: %u\n",
proxy->shared_desc->usecnt);
return 0;
}
static void gpio_shared_proxy_free(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
struct gpio_shared_desc *shared_desc = proxy->shared_desc;
guard(gpio_shared_desc_lock)(shared_desc);
proxy->shared_desc->usecnt--;
dev_dbg(proxy->dev, "Shared GPIO freed, number of users: %u\n",
proxy->shared_desc->usecnt);
}
static int gpio_shared_proxy_set_config(struct gpio_chip *gc,
unsigned int offset, unsigned long cfg)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
struct gpio_shared_desc *shared_desc = proxy->shared_desc;
struct gpio_desc *desc = shared_desc->desc;
int ret;
guard(gpio_shared_desc_lock)(shared_desc);
if (shared_desc->usecnt > 1) {
if (shared_desc->cfg != cfg) {
dev_dbg(proxy->dev,
"Shared GPIO's configuration already set, accepting changes but users may conflict!!\n");
} else {
dev_dbg(proxy->dev, "Equal config requested, nothing to do\n");
return 0;
}
}
ret = gpiod_set_config(desc, cfg);
if (ret && ret != -ENOTSUPP)
return ret;
shared_desc->cfg = cfg;
return 0;
}
static int gpio_shared_proxy_direction_input(struct gpio_chip *gc,
unsigned int offset)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
struct gpio_shared_desc *shared_desc = proxy->shared_desc;
struct gpio_desc *desc = shared_desc->desc;
int dir;
guard(gpio_shared_desc_lock)(shared_desc);
if (shared_desc->usecnt == 1) {
dev_dbg(proxy->dev,
"Only one user of this shared GPIO, allowing to set direction to input\n");
return gpiod_direction_input(desc);
}
dir = gpiod_get_direction(desc);
if (dir < 0)
return dir;
if (dir == GPIO_LINE_DIRECTION_OUT) {
dev_dbg(proxy->dev,
"Shared GPIO's direction already set to output, refusing to change\n");
return -EPERM;
}
return 0;
}
static int gpio_shared_proxy_direction_output(struct gpio_chip *gc,
unsigned int offset, int value)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
struct gpio_shared_desc *shared_desc = proxy->shared_desc;
struct gpio_desc *desc = shared_desc->desc;
int ret, dir;
guard(gpio_shared_desc_lock)(shared_desc);
if (shared_desc->usecnt == 1) {
dev_dbg(proxy->dev,
"Only one user of this shared GPIO, allowing to set direction to output with value '%s'\n",
str_high_low(value));
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;
}
dir = gpiod_get_direction(desc);
if (dir < 0)
return dir;
if (dir == GPIO_LINE_DIRECTION_IN) {
dev_dbg(proxy->dev,
"Shared GPIO's direction already set to input, refusing to change\n");
return -EPERM;
}
return gpio_shared_proxy_set_unlocked(proxy, gpiod_direction_output, value);
}
static int gpio_shared_proxy_get(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
return gpiod_get_value(proxy->shared_desc->desc);
}
static int gpio_shared_proxy_get_cansleep(struct gpio_chip *gc,
unsigned int offset)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
return gpiod_get_value_cansleep(proxy->shared_desc->desc);
}
static int gpio_shared_proxy_do_set(struct gpio_shared_proxy_data *proxy,
int (*set_func)(struct gpio_desc *desc, int value),
int value)
{
guard(gpio_shared_desc_lock)(proxy->shared_desc);
return gpio_shared_proxy_set_unlocked(proxy, set_func, value);
}
static int gpio_shared_proxy_set(struct gpio_chip *gc, unsigned int offset,
int value)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
return gpio_shared_proxy_do_set(proxy, gpiod_set_value, value);
}
static int gpio_shared_proxy_set_cansleep(struct gpio_chip *gc,
unsigned int offset, int value)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
return gpio_shared_proxy_do_set(proxy, gpiod_set_value_cansleep, value);
}
static int gpio_shared_proxy_get_direction(struct gpio_chip *gc,
unsigned int offset)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
return gpiod_get_direction(proxy->shared_desc->desc);
}
static int gpio_shared_proxy_to_irq(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc);
return gpiod_to_irq(proxy->shared_desc->desc);
}
static int gpio_shared_proxy_probe(struct auxiliary_device *adev,
const struct auxiliary_device_id *id)
{
struct gpio_shared_proxy_data *proxy;
struct gpio_shared_desc *shared_desc;
struct device *dev = &adev->dev;
struct gpio_chip *gc;
shared_desc = devm_gpiod_shared_get(dev);
if (IS_ERR(shared_desc))
return PTR_ERR(shared_desc);
proxy = devm_kzalloc(dev, sizeof(*proxy), GFP_KERNEL);
if (!proxy)
return -ENOMEM;
proxy->shared_desc = shared_desc;
proxy->dev = dev;
gc = &proxy->gc;
gc->base = -1;
gc->ngpio = 1;
gc->label = dev_name(dev);
gc->parent = dev;
gc->owner = THIS_MODULE;
gc->can_sleep = shared_desc->can_sleep;
gc->request = gpio_shared_proxy_request;
gc->free = gpio_shared_proxy_free;
gc->set_config = gpio_shared_proxy_set_config;
gc->direction_input = gpio_shared_proxy_direction_input;
gc->direction_output = gpio_shared_proxy_direction_output;
if (gc->can_sleep) {
gc->set = gpio_shared_proxy_set_cansleep;
gc->get = gpio_shared_proxy_get_cansleep;
} else {
gc->set = gpio_shared_proxy_set;
gc->get = gpio_shared_proxy_get;
}
gc->get_direction = gpio_shared_proxy_get_direction;
gc->to_irq = gpio_shared_proxy_to_irq;
return devm_gpiochip_add_data(dev, &proxy->gc, proxy);
}
static const struct auxiliary_device_id gpio_shared_proxy_id_table[] = {
{ .name = "gpiolib_shared.proxy" },
{},
};
MODULE_DEVICE_TABLE(auxiliary, gpio_shared_proxy_id_table);
static struct auxiliary_driver gpio_shared_proxy_driver = {
.driver = {
.name = "gpio-shared-proxy",
},
.probe = gpio_shared_proxy_probe,
.id_table = gpio_shared_proxy_id_table,
};
module_auxiliary_driver(gpio_shared_proxy_driver);
MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
MODULE_DESCRIPTION("Shared GPIO mux driver.");
MODULE_LICENSE("GPL");
File diff suppressed because it is too large Load Diff
+71
View File
@@ -0,0 +1,71 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LINUX_GPIO_SHARED_H
#define __LINUX_GPIO_SHARED_H
#include <linux/cleanup.h>
#include <linux/lockdep.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
struct gpio_device;
struct gpio_desc;
struct device;
#if IS_ENABLED(CONFIG_GPIO_SHARED)
int gpio_device_setup_shared(struct gpio_device *gdev);
void gpio_device_teardown_shared(struct gpio_device *gdev);
int gpio_shared_add_proxy_lookup(struct device *consumer, unsigned long lflags);
#else
static inline int gpio_device_setup_shared(struct gpio_device *gdev)
{
return 0;
}
static inline void gpio_device_teardown_shared(struct gpio_device *gdev) { }
static inline int gpio_shared_add_proxy_lookup(struct device *consumer,
unsigned long lflags)
{
return 0;
}
#endif /* CONFIG_GPIO_SHARED */
struct gpio_shared_desc {
struct gpio_desc *desc;
bool can_sleep;
unsigned long cfg;
unsigned int usecnt;
unsigned int highcnt;
union {
struct mutex mutex;
spinlock_t spinlock;
};
};
struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev);
DEFINE_LOCK_GUARD_1(gpio_shared_desc_lock, struct gpio_shared_desc,
if (_T->lock->can_sleep)
mutex_lock(&_T->lock->mutex);
else
spin_lock_irqsave(&_T->lock->spinlock, _T->flags),
if (_T->lock->can_sleep)
mutex_unlock(&_T->lock->mutex);
else
spin_unlock_irqrestore(&_T->lock->spinlock, _T->flags),
unsigned long flags)
static inline void gpio_shared_lockdep_assert(struct gpio_shared_desc *shared_desc)
{
if (shared_desc->can_sleep)
lockdep_assert_held(&shared_desc->mutex);
else
lockdep_assert_held(&shared_desc->spinlock);
}
#endif /* __LINUX_GPIO_SHARED_H */
+61 -9
View File
@@ -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"
@@ -1213,6 +1214,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
@@ -1224,10 +1229,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:
@@ -1296,6 +1304,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);
@@ -3988,6 +3997,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)
@@ -4659,11 +4688,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");
@@ -4686,14 +4733,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
View File
@@ -204,6 +204,8 @@ 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 */
#define GPIOD_FLAG_SHARED_PROXY 21 /* GPIO is a virtual proxy to a physically shared pin. */
/* Connection label */
struct gpio_desc_label __rcu *label;
+9
View File
@@ -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);
@@ -522,6 +524,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;
+18
View File
@@ -562,4 +562,22 @@ static inline bool strstarts(const char *str, const char *prefix)
return strncmp(str, prefix, strlen(prefix)) == 0;
}
/**
* 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.
*/
static inline 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));
}
#endif /* _LINUX_STRING_H_ */
+13
View File
@@ -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),
{}
};