Unverified Commit be2b723d authored by Mark Brown's avatar Mark Brown
Browse files

gpio: improve support for shared GPIOs

Merge series from Bartosz Golaszewski <brgl@bgdev.pl>:

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.
parents bdf96e91 7a0a8771
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -6,6 +6,9 @@
config GPIOLIB_LEGACY
	def_bool y

config HAVE_SHARED_GPIOS
	bool

menuconfig GPIOLIB
	bool "GPIO Support"
	help
@@ -50,6 +53,11 @@ config OF_GPIO_MM_GPIOCHIP
	  this symbol, but new drivers should use the generic gpio-regmap
	  infrastructure instead.

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 −0
Original line number Diff line number Diff line
@@ -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
@@ -159,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
+333 −0
Original line number Diff line number Diff line
// 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");
+558 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2025 Linaro Ltd.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/auxiliary_bus.h>
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/fwnode.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio/machine.h>
#include <linux/idr.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/lockdep.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/overflow.h>
#include <linux/printk.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/string.h>

#include "gpiolib.h"
#include "gpiolib-shared.h"

/* Represents a single reference to a GPIO pin. */
struct gpio_shared_ref {
	struct list_head list;
	/* Firmware node associated with this GPIO's consumer. */
	struct fwnode_handle *fwnode;
	/* GPIO flags this consumer uses for the request. */
	enum gpiod_flags flags;
	char *con_id;
	int dev_id;
	struct auxiliary_device adev;
	struct gpiod_lookup_table *lookup;
};

/* Represents a single GPIO pin. */
struct gpio_shared_entry {
	struct list_head list;
	/* Firmware node associated with the GPIO controller. */
	struct fwnode_handle *fwnode;
	/* Hardware offset of the GPIO within its chip. */
	unsigned int offset;
	/* Index in the property value array. */
	size_t index;
	struct gpio_shared_desc *shared_desc;
	struct kref ref;
	struct list_head refs;
};

static LIST_HEAD(gpio_shared_list);
static DEFINE_MUTEX(gpio_shared_lock);
static DEFINE_IDA(gpio_shared_ida);

static struct gpio_shared_entry *
gpio_shared_find_entry(struct fwnode_handle *controller_node,
		       unsigned int offset)
{
	struct gpio_shared_entry *entry;

	list_for_each_entry(entry, &gpio_shared_list, list) {
		if (entry->fwnode == controller_node && entry->offset == offset)
			return entry;
	}

	return NULL;
}

#if IS_ENABLED(CONFIG_OF)
static int gpio_shared_of_traverse(struct device_node *curr)
{
	struct gpio_shared_entry *entry;
	size_t con_id_len, suffix_len;
	struct fwnode_handle *fwnode;
	struct of_phandle_args args;
	struct property *prop;
	unsigned int offset;
	const char *suffix;
	int ret, count, i;

	for_each_property_of_node(curr, prop) {
		/*
		 * The standard name for a GPIO property is "foo-gpios"
		 * or "foo-gpio". Some bindings also use "gpios" or "gpio".
		 * There are some legacy device-trees which have a different
		 * naming convention and for which we have rename quirks in
		 * place in gpiolib-of.c. I don't think any of them require
		 * support for shared GPIOs so for now let's just ignore
		 * them. We can always just export the quirk list and
		 * iterate over it here.
		 */
		if (!strends(prop->name, "-gpios") &&
		    !strends(prop->name, "-gpio") &&
		    strcmp(prop->name, "gpios") != 0 &&
		    strcmp(prop->name, "gpio") != 0)
			continue;

		count = of_count_phandle_with_args(curr, prop->name,
						   "#gpio-cells");
		if (count <= 0)
			continue;

		for (i = 0; i < count; i++) {
			struct device_node *np __free(device_node) = NULL;

			ret = of_parse_phandle_with_args(curr, prop->name,
							 "#gpio-cells", i,
							 &args);
			if (ret)
				continue;

			np = args.np;

			if (!of_property_present(np, "gpio-controller"))
				continue;

			/*
			 * We support 1, 2 and 3 cell GPIO bindings in the
			 * kernel currently. There's only one old MIPS dts that
			 * has a one-cell binding but there's no associated
			 * consumer so it may as well be an error. There don't
			 * seem to be any 3-cell users of non-exclusive GPIOs,
			 * so we can skip this as well. Let's occupy ourselves
			 * with the predominant 2-cell binding with the first
			 * cell indicating the hardware offset of the GPIO and
			 * the second defining the GPIO flags of the request.
			 */
			if (args.args_count != 2)
				continue;

			fwnode = of_fwnode_handle(args.np);
			offset = args.args[0];

			entry = gpio_shared_find_entry(fwnode, offset);
			if (!entry) {
				entry = kzalloc(sizeof(*entry), GFP_KERNEL);
				if (!entry)
					return -ENOMEM;

				entry->fwnode = fwnode_handle_get(fwnode);
				entry->offset = offset;
				entry->index = count;
				INIT_LIST_HEAD(&entry->refs);

				list_add_tail(&entry->list, &gpio_shared_list);
			}

			struct gpio_shared_ref *ref __free(kfree) =
					kzalloc(sizeof(*ref), GFP_KERNEL);
			if (!ref)
				return -ENOMEM;

			ref->fwnode = fwnode_handle_get(of_fwnode_handle(curr));
			ref->flags = args.args[1];

			if (strends(prop->name, "gpios"))
				suffix = "-gpios";
			else if (strends(prop->name, "gpio"))
				suffix = "-gpio";
			else
				suffix = NULL;
			if (!suffix)
				continue;

			/* We only set con_id if there's actually one. */
			if (strcmp(prop->name, "gpios") && strcmp(prop->name, "gpio")) {
				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';
			}

			ref->dev_id = ida_alloc(&gpio_shared_ida, GFP_KERNEL);
			if (ref->dev_id < 0) {
				kfree(ref->con_id);
				return -ENOMEM;
			}

			if (!list_empty(&entry->refs))
				pr_debug("GPIO %u at %s is shared by multiple firmware nodes\n",
					 entry->offset, fwnode_get_name(entry->fwnode));

			list_add_tail(&no_free_ptr(ref)->list, &entry->refs);
		}
	}

	for_each_child_of_node_scoped(curr, child) {
		ret = gpio_shared_of_traverse(child);
		if (ret)
			return ret;
	}

	return 0;
}

static int gpio_shared_of_scan(void)
{
	return gpio_shared_of_traverse(of_root);
}
#else
static int gpio_shared_of_scan(void)
{
	return 0;
}
#endif /* CONFIG_OF */

static void gpio_shared_adev_release(struct device *dev)
{

}

static int gpio_shared_make_adev(struct gpio_device *gdev,
				 struct gpio_shared_ref *ref)
{
	struct auxiliary_device *adev = &ref->adev;
	int ret;

	lockdep_assert_held(&gpio_shared_lock);

	memset(adev, 0, sizeof(*adev));

	adev->id = ref->dev_id;
	adev->name = "proxy";
	adev->dev.parent = gdev->dev.parent;
	adev->dev.release = gpio_shared_adev_release;

	ret = auxiliary_device_init(adev);
	if (ret)
		return ret;

	ret = auxiliary_device_add(adev);
	if (ret) {
		auxiliary_device_uninit(adev);
		return ret;
	}

	pr_debug("Created an auxiliary GPIO proxy %s for GPIO device %s\n",
		 dev_name(&adev->dev), gpio_device_get_label(gdev));

	return 0;
}

int gpio_shared_add_proxy_lookup(struct device *consumer, unsigned long lflags)
{
	const char *dev_id = dev_name(consumer);
	struct gpio_shared_entry *entry;
	struct gpio_shared_ref *ref;

	struct gpiod_lookup_table *lookup __free(kfree) =
			kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
	if (!lookup)
		return -ENOMEM;

	guard(mutex)(&gpio_shared_lock);

	list_for_each_entry(entry, &gpio_shared_list, list) {
		list_for_each_entry(ref, &entry->refs, list) {
			if (!device_match_fwnode(consumer, ref->fwnode))
				continue;

			/* We've already done that on a previous request. */
			if (ref->lookup)
				return 0;

			char *key __free(kfree) =
				kasprintf(GFP_KERNEL,
					  KBUILD_MODNAME ".proxy.%u",
					  ref->adev.id);
			if (!key)
				return -ENOMEM;

			pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n",
				 dev_id, key, ref->con_id ?: "none");

			lookup->dev_id = dev_id;
			lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0,
						       ref->con_id, lflags);

			gpiod_add_lookup_table(no_free_ptr(lookup));

			return 0;
		}
	}

	/* We warn here because this can only happen if the programmer borked. */
	WARN_ON(1);
	return -ENOENT;
}

static void gpio_shared_remove_adev(struct auxiliary_device *adev)
{
	lockdep_assert_held(&gpio_shared_lock);

	auxiliary_device_uninit(adev);
	auxiliary_device_delete(adev);
}

int gpio_device_setup_shared(struct gpio_device *gdev)
{
	struct gpio_shared_entry *entry;
	struct gpio_shared_ref *ref;
	unsigned long *flags;
	int ret;

	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;

		if (list_count_nodes(&entry->refs) <= 1)
			continue;

		flags = &gdev->descs[entry->offset].flags;

		__set_bit(GPIOD_FLAG_SHARED, flags);
		/*
		 * 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);

		pr_debug("GPIO %u owned by %s is shared by multiple consumers\n",
			 entry->offset, gpio_device_get_label(gdev));

		list_for_each_entry(ref, &entry->refs, list) {
			pr_debug("Setting up a shared GPIO entry for %s\n",
				 fwnode_get_name(ref->fwnode));

			ret = gpio_shared_make_adev(gdev, ref);
			if (ret)
				return ret;
		}
	}

	return 0;
}

void gpio_device_teardown_shared(struct gpio_device *gdev)
{
	struct gpio_shared_entry *entry;
	struct gpio_shared_ref *ref;

	guard(mutex)(&gpio_shared_lock);

	list_for_each_entry(entry, &gpio_shared_list, list) {
		if (!device_match_fwnode(&gdev->dev, entry->fwnode))
			continue;

		list_for_each_entry(ref, &entry->refs, list) {
			gpiod_remove_lookup_table(ref->lookup);
			kfree(ref->lookup->table[0].key);
			kfree(ref->lookup);
			ref->lookup = NULL;
			gpio_shared_remove_adev(&ref->adev);
		}
	}
}

static void gpio_shared_release(struct kref *kref)
{
	struct gpio_shared_entry *entry =
		container_of(kref, struct gpio_shared_entry, ref);
	struct gpio_shared_desc *shared_desc = entry->shared_desc;

	guard(mutex)(&gpio_shared_lock);

	gpio_device_put(shared_desc->desc->gdev);
	if (shared_desc->can_sleep)
		mutex_destroy(&shared_desc->mutex);
	kfree(shared_desc);
	entry->shared_desc = NULL;
}

static void gpiod_shared_put(void *data)
{
	struct gpio_shared_entry *entry = data;

	lockdep_assert_not_held(&gpio_shared_lock);

	kref_put(&entry->ref, gpio_shared_release);
}

static struct gpio_shared_desc *
gpiod_shared_desc_create(struct gpio_shared_entry *entry)
{
	struct gpio_shared_desc *shared_desc;
	struct gpio_device *gdev;

	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);

	return shared_desc;
}

static struct gpio_shared_entry *gpiod_shared_find(struct auxiliary_device *adev)
{
	struct gpio_shared_desc *shared_desc;
	struct gpio_shared_entry *entry;
	struct gpio_shared_ref *ref;

	guard(mutex)(&gpio_shared_lock);

	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);
				return entry;
			}

			shared_desc = gpiod_shared_desc_create(entry);
			if (IS_ERR(shared_desc))
				return ERR_CAST(shared_desc);

			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(&adev->dev), gpio_chip_hwgpio(shared_desc->desc),
				 gpio_device_get_label(shared_desc->desc->gdev));


			return entry;
		}
	}

	return ERR_PTR(-ENOENT);
}

struct gpio_shared_desc *devm_gpiod_shared_get(struct device *dev)
{
	struct gpio_shared_entry *entry;
	int ret;

	entry = gpiod_shared_find(to_auxiliary_dev(dev));
	if (IS_ERR(entry))
		return ERR_CAST(entry);

	ret = devm_add_action_or_reset(dev, gpiod_shared_put, entry);
	if (ret)
		return ERR_PTR(ret);

	return entry->shared_desc;
}
EXPORT_SYMBOL_GPL(devm_gpiod_shared_get);

static void gpio_shared_drop_ref(struct gpio_shared_ref *ref)
{
	list_del(&ref->list);
	kfree(ref->con_id);
	ida_free(&gpio_shared_ida, ref->dev_id);
	fwnode_handle_put(ref->fwnode);
	kfree(ref);
}

static void gpio_shared_drop_entry(struct gpio_shared_entry *entry)
{
	list_del(&entry->list);
	fwnode_handle_put(entry->fwnode);
	kfree(entry);
}

/*
 * This is only called if gpio_shared_init() fails so it's in fact __init and
 * not __exit.
 */
static void __init gpio_shared_teardown(void)
{
	struct gpio_shared_entry *entry, *epos;
	struct gpio_shared_ref *ref, *rpos;

	list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) {
		list_for_each_entry_safe(ref, rpos, &entry->refs, list)
			gpio_shared_drop_ref(ref);

		gpio_shared_drop_entry(entry);
	}
}

static void gpio_shared_free_exclusive(void)
{
	struct gpio_shared_entry *entry, *epos;

	list_for_each_entry_safe(entry, epos, &gpio_shared_list, list) {
		if (list_count_nodes(&entry->refs) > 1)
			continue;

		gpio_shared_drop_ref(list_first_entry(&entry->refs,
						      struct gpio_shared_ref,
						      list));
		gpio_shared_drop_entry(entry);
	}
}

static int __init gpio_shared_init(void)
{
	int ret;

	/* Right now, we only support OF-based systems. */
	ret = gpio_shared_of_scan();
	if (ret) {
		gpio_shared_teardown();
		pr_err("Failed to scan OF nodes for shared GPIOs: %d\n", ret);
		return ret;
	}

	gpio_shared_free_exclusive();

	pr_debug("Finished scanning firmware nodes for shared GPIOs\n");
	return 0;
}
postcore_initcall(gpio_shared_init);
+71 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading