Commit a0d50aa9 authored by Linus Walleij's avatar Linus Walleij

Merge branch 'ib-gpio-aggregator' into devel

parents d850c6f4 d9646a48
.. SPDX-License-Identifier: GPL-2.0-only
GPIO Aggregator
===============
The GPIO Aggregator provides a mechanism to aggregate GPIOs, and expose them as
a new gpio_chip. This supports the following use cases.
Aggregating GPIOs using Sysfs
-----------------------------
GPIO controllers are exported to userspace using /dev/gpiochip* character
devices. Access control to these devices is provided by standard UNIX file
system permissions, on an all-or-nothing basis: either a GPIO controller is
accessible for a user, or it is not.
The GPIO Aggregator provides access control for a set of one or more GPIOs, by
aggregating them into a new gpio_chip, which can be assigned to a group or user
using standard UNIX file ownership and permissions. Furthermore, this
simplifies and hardens exporting GPIOs to a virtual machine, as the VM can just
grab the full GPIO controller, and no longer needs to care about which GPIOs to
grab and which not, reducing the attack surface.
Aggregated GPIO controllers are instantiated and destroyed by writing to
write-only attribute files in sysfs.
/sys/bus/platform/drivers/gpio-aggregator/
"new_device" ...
Userspace may ask the kernel to instantiate an aggregated GPIO
controller by writing a string describing the GPIOs to
aggregate to the "new_device" file, using the format
.. code-block:: none
[<gpioA>] [<gpiochipB> <offsets>] ...
Where:
"<gpioA>" ...
is a GPIO line name,
"<gpiochipB>" ...
is a GPIO chip label, and
"<offsets>" ...
is a comma-separated list of GPIO offsets and/or
GPIO offset ranges denoted by dashes.
Example: Instantiate a new GPIO aggregator by aggregating GPIO
line 19 of "e6052000.gpio" and GPIO lines 20-21 of
"e6050000.gpio" into a new gpio_chip:
.. code-block:: sh
$ echo 'e6052000.gpio 19 e6050000.gpio 20-21' > new_device
"delete_device" ...
Userspace may ask the kernel to destroy an aggregated GPIO
controller after use by writing its device name to the
"delete_device" file.
Example: Destroy the previously-created aggregated GPIO
controller, assumed to be "gpio-aggregator.0":
.. code-block:: sh
$ echo gpio-aggregator.0 > delete_device
Generic GPIO Driver
-------------------
The GPIO Aggregator can also be used as a generic driver for a simple
GPIO-operated device described in DT, without a dedicated in-kernel driver.
This is useful in industrial control, and is not unlike e.g. spidev, which
allows the user to communicate with an SPI device from userspace.
Binding a device to the GPIO Aggregator is performed either by modifying the
gpio-aggregator driver, or by writing to the "driver_override" file in Sysfs.
Example: If "door" is a GPIO-operated device described in DT, using its own
compatible value::
door {
compatible = "myvendor,mydoor";
gpios = <&gpio2 19 GPIO_ACTIVE_HIGH>,
<&gpio2 20 GPIO_ACTIVE_LOW>;
gpio-line-names = "open", "lock";
};
it can be bound to the GPIO Aggregator by either:
1. Adding its compatible value to ``gpio_aggregator_dt_ids[]``,
2. Binding manually using "driver_override":
.. code-block:: sh
$ echo gpio-aggregator > /sys/bus/platform/devices/door/driver_override
$ echo door > /sys/bus/platform/drivers/gpio-aggregator/bind
After that, a new gpiochip "door" has been created:
.. code-block:: sh
$ gpioinfo door
gpiochip12 - 2 lines:
line 0: "open" unused input active-high
line 1: "lock" unused input active-high
...@@ -7,6 +7,7 @@ gpio ...@@ -7,6 +7,7 @@ gpio
.. toctree:: .. toctree::
:maxdepth: 1 :maxdepth: 1
gpio-aggregator
sysfs sysfs
.. only:: subproject and html .. only:: subproject and html
......
...@@ -113,13 +113,15 @@ files that desire to do so need to include the following header:: ...@@ -113,13 +113,15 @@ files that desire to do so need to include the following header::
GPIOs are mapped by the means of tables of lookups, containing instances of the GPIOs are mapped by the means of tables of lookups, containing instances of the
gpiod_lookup structure. Two macros are defined to help declaring such mappings:: gpiod_lookup structure. Two macros are defined to help declaring such mappings::
GPIO_LOOKUP(chip_label, chip_hwnum, con_id, flags) GPIO_LOOKUP(key, chip_hwnum, con_id, flags)
GPIO_LOOKUP_IDX(chip_label, chip_hwnum, con_id, idx, flags) GPIO_LOOKUP_IDX(key, chip_hwnum, con_id, idx, flags)
where where
- chip_label is the label of the gpiod_chip instance providing the GPIO - key is either the label of the gpiod_chip instance providing the GPIO, or
- chip_hwnum is the hardware number of the GPIO within the chip the GPIO line name
- chip_hwnum is the hardware number of the GPIO within the chip, or U16_MAX
to indicate that key is a GPIO line name
- con_id is the name of the GPIO function from the device point of view. It - con_id is the name of the GPIO function from the device point of view. It
can be NULL, in which case it will match any function. can be NULL, in which case it will match any function.
- idx is the index of the GPIO within the function. - idx is the index of the GPIO within the function.
...@@ -135,7 +137,10 @@ where ...@@ -135,7 +137,10 @@ where
In the future, these flags might be extended to support more properties. In the future, these flags might be extended to support more properties.
Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. Note that:
1. GPIO line names are not guaranteed to be globally unique, so the first
match found will be used.
2. GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0.
A lookup table can then be defined as follows, with an empty entry defining its A lookup table can then be defined as follows, with an empty entry defining its
end. The 'dev_id' field of the table is the identifier of the device that will end. The 'dev_id' field of the table is the identifier of the device that will
......
...@@ -7228,6 +7228,13 @@ F: Documentation/firmware-guide/acpi/gpio-properties.rst ...@@ -7228,6 +7228,13 @@ F: Documentation/firmware-guide/acpi/gpio-properties.rst
F: drivers/gpio/gpiolib-acpi.c F: drivers/gpio/gpiolib-acpi.c
F: drivers/gpio/gpiolib-acpi.h F: drivers/gpio/gpiolib-acpi.h
GPIO AGGREGATOR
M: Geert Uytterhoeven <geert+renesas@glider.be>
L: linux-gpio@vger.kernel.org
S: Supported
F: Documentation/admin-guide/gpio/gpio-aggregator.rst
F: drivers/gpio/gpio-aggregator.c
GPIO IR Transmitter GPIO IR Transmitter
M: Sean Young <sean@mess.org> M: Sean Young <sean@mess.org>
L: linux-media@vger.kernel.org L: linux-media@vger.kernel.org
......
...@@ -1541,6 +1541,18 @@ config GPIO_VIPERBOARD ...@@ -1541,6 +1541,18 @@ config GPIO_VIPERBOARD
endmenu endmenu
config GPIO_AGGREGATOR
tristate "GPIO Aggregator"
help
Say yes here to enable the GPIO Aggregator, which provides a way to
aggregate existing GPIO lines into a new virtual GPIO chip.
This can serve the following purposes:
- Assign permissions for a collection of GPIO lines to a user,
- Export a collection of GPIO lines to a virtual machine,
- Provide a generic driver for a GPIO-operated device in an
industrial control context, to be operated from userspace using
the GPIO chardev interface.
config GPIO_MOCKUP config GPIO_MOCKUP
tristate "GPIO Testing Driver" tristate "GPIO Testing Driver"
select IRQ_SIM select IRQ_SIM
......
...@@ -25,6 +25,7 @@ obj-$(CONFIG_GPIO_74XX_MMIO) += gpio-74xx-mmio.o ...@@ -25,6 +25,7 @@ obj-$(CONFIG_GPIO_74XX_MMIO) += gpio-74xx-mmio.o
obj-$(CONFIG_GPIO_ADNP) += gpio-adnp.o obj-$(CONFIG_GPIO_ADNP) += gpio-adnp.o
obj-$(CONFIG_GPIO_ADP5520) += gpio-adp5520.o obj-$(CONFIG_GPIO_ADP5520) += gpio-adp5520.o
obj-$(CONFIG_GPIO_ADP5588) += gpio-adp5588.o obj-$(CONFIG_GPIO_ADP5588) += gpio-adp5588.o
obj-$(CONFIG_GPIO_AGGREGATOR) += gpio-aggregator.o
obj-$(CONFIG_GPIO_ALTERA_A10SR) += gpio-altera-a10sr.o obj-$(CONFIG_GPIO_ALTERA_A10SR) += gpio-altera-a10sr.o
obj-$(CONFIG_GPIO_ALTERA) += gpio-altera.o obj-$(CONFIG_GPIO_ALTERA) += gpio-altera.o
obj-$(CONFIG_GPIO_AMD8111) += gpio-amd8111.o obj-$(CONFIG_GPIO_AMD8111) += gpio-amd8111.o
......
This diff is collapsed.
...@@ -4622,7 +4622,7 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, ...@@ -4622,7 +4622,7 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
if (!table) if (!table)
return desc; return desc;
for (p = &table->table[0]; p->chip_label; p++) { for (p = &table->table[0]; p->key; p++) {
struct gpio_chip *gc; struct gpio_chip *gc;
/* idx must always match exactly */ /* idx must always match exactly */
...@@ -4633,18 +4633,30 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, ...@@ -4633,18 +4633,30 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
continue; continue;
gc = find_chip_by_name(p->chip_label); if (p->chip_hwnum == U16_MAX) {
desc = gpio_name_to_desc(p->key);
if (desc) {
*flags = p->flags;
return desc;
}
dev_warn(dev, "cannot find GPIO line %s, deferring\n",
p->key);
return ERR_PTR(-EPROBE_DEFER);
}
gc = find_chip_by_name(p->key);
if (!gc) { if (!gc) {
/* /*
* As the lookup table indicates a chip with * As the lookup table indicates a chip with
* p->chip_label should exist, assume it may * p->key should exist, assume it may
* still appear later and let the interested * still appear later and let the interested
* consumer be probed again or let the Deferred * consumer be probed again or let the Deferred
* Probe infrastructure handle the error. * Probe infrastructure handle the error.
*/ */
dev_warn(dev, "cannot find GPIO chip %s, deferring\n", dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
p->chip_label); p->key);
return ERR_PTR(-EPROBE_DEFER); return ERR_PTR(-EPROBE_DEFER);
} }
...@@ -4675,7 +4687,7 @@ static int platform_gpio_count(struct device *dev, const char *con_id) ...@@ -4675,7 +4687,7 @@ static int platform_gpio_count(struct device *dev, const char *con_id)
if (!table) if (!table)
return -ENOENT; return -ENOENT;
for (p = &table->table[0]; p->chip_label; p++) { for (p = &table->table[0]; p->key; p++) {
if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
(!con_id && !p->con_id)) (!con_id && !p->con_id))
count++; count++;
......
...@@ -1439,9 +1439,9 @@ static int i801_add_mux(struct i801_priv *priv) ...@@ -1439,9 +1439,9 @@ static int i801_add_mux(struct i801_priv *priv)
return -ENOMEM; return -ENOMEM;
lookup->dev_id = "i2c-mux-gpio"; lookup->dev_id = "i2c-mux-gpio";
for (i = 0; i < mux_config->n_gpios; i++) { for (i = 0; i < mux_config->n_gpios; i++) {
lookup->table[i].chip_label = mux_config->gpio_chip; lookup->table[i] = (struct gpiod_lookup)
lookup->table[i].chip_hwnum = mux_config->gpios[i]; GPIO_LOOKUP(mux_config->gpio_chip,
lookup->table[i].con_id = "mux"; mux_config->gpios[i], "mux", 0);
} }
gpiod_add_lookup_table(lookup); gpiod_add_lookup_table(lookup);
priv->lookup = lookup; priv->lookup = lookup;
......
...@@ -1145,22 +1145,14 @@ static int sm501_register_gpio_i2c_instance(struct sm501_devdata *sm, ...@@ -1145,22 +1145,14 @@ static int sm501_register_gpio_i2c_instance(struct sm501_devdata *sm,
return -ENOMEM; return -ENOMEM;
lookup->dev_id = "i2c-gpio"; lookup->dev_id = "i2c-gpio";
if (iic->pin_sda < 32) lookup->table[0] = (struct gpiod_lookup)
lookup->table[0].chip_label = "SM501-LOW"; GPIO_LOOKUP_IDX(iic->pin_sda < 32 ? "SM501-LOW" : "SM501-HIGH",
else iic->pin_sda % 32, NULL, 0,
lookup->table[0].chip_label = "SM501-HIGH"; GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN);
lookup->table[0].chip_hwnum = iic->pin_sda % 32; lookup->table[1] = (struct gpiod_lookup)
lookup->table[0].con_id = NULL; GPIO_LOOKUP_IDX(iic->pin_scl < 32 ? "SM501-LOW" : "SM501-HIGH",
lookup->table[0].idx = 0; iic->pin_scl % 32, NULL, 1,
lookup->table[0].flags = GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN; GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN);
if (iic->pin_scl < 32)
lookup->table[1].chip_label = "SM501-LOW";
else
lookup->table[1].chip_label = "SM501-HIGH";
lookup->table[1].chip_hwnum = iic->pin_scl % 32;
lookup->table[1].con_id = NULL;
lookup->table[1].idx = 1;
lookup->table[1].flags = GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN;
gpiod_add_lookup_table(lookup); gpiod_add_lookup_table(lookup);
icd = dev_get_platdata(&pdev->dev); icd = dev_get_platdata(&pdev->dev);
......
...@@ -20,8 +20,11 @@ enum gpio_lookup_flags { ...@@ -20,8 +20,11 @@ enum gpio_lookup_flags {
/** /**
* struct gpiod_lookup - lookup table * struct gpiod_lookup - lookup table
* @chip_label: name of the chip the GPIO belongs to * @key: either the name of the chip the GPIO belongs to, or the GPIO line name
* @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO * Note that GPIO line names are not guaranteed to be globally unique,
* so this will use the first match found!
* @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO, or
* U16_MAX to indicate that @key is a GPIO line name
* @con_id: name of the GPIO from the device's point of view * @con_id: name of the GPIO from the device's point of view
* @idx: index of the GPIO in case several GPIOs share the same name * @idx: index of the GPIO in case several GPIOs share the same name
* @flags: bitmask of gpio_lookup_flags GPIO_* values * @flags: bitmask of gpio_lookup_flags GPIO_* values
...@@ -30,7 +33,7 @@ enum gpio_lookup_flags { ...@@ -30,7 +33,7 @@ enum gpio_lookup_flags {
* functions using platform data. * functions using platform data.
*/ */
struct gpiod_lookup { struct gpiod_lookup {
const char *chip_label; const char *key;
u16 chip_hwnum; u16 chip_hwnum;
const char *con_id; const char *con_id;
unsigned int idx; unsigned int idx;
...@@ -63,17 +66,17 @@ struct gpiod_hog { ...@@ -63,17 +66,17 @@ struct gpiod_hog {
/* /*
* Simple definition of a single GPIO under a con_id * Simple definition of a single GPIO under a con_id
*/ */
#define GPIO_LOOKUP(_chip_label, _chip_hwnum, _con_id, _flags) \ #define GPIO_LOOKUP(_key, _chip_hwnum, _con_id, _flags) \
GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags) GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, 0, _flags)
/* /*
* Use this macro if you need to have several GPIOs under the same con_id. * Use this macro if you need to have several GPIOs under the same con_id.
* Each GPIO needs to use a different index and can be accessed using * Each GPIO needs to use a different index and can be accessed using
* gpiod_get_index() * gpiod_get_index()
*/ */
#define GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, _idx, _flags) \ #define GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, _idx, _flags) \
{ \ { \
.chip_label = _chip_label, \ .key = _key, \
.chip_hwnum = _chip_hwnum, \ .chip_hwnum = _chip_hwnum, \
.con_id = _con_id, \ .con_id = _con_id, \
.idx = _idx, \ .idx = _idx, \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment