diff mbox series

[RFC,v1,2/2] gpio: add gpio-split driver

Message ID 20251009223501.570949-3-jelonek.jonas@gmail.com
State New
Headers show
Series add support for splitting GPIOs | expand

Commit Message

Jonas Jelonek Oct. 9, 2025, 10:35 p.m. UTC
Add a new driver which allows to split a physical GPIO into multiple
virtual GPIOs by using a multiplexer.

For now, this doesn't support advanced features like IRQs, just normal
IN and OUT functionality of GPIOs.

This can help in various usecases. One practical case is the special
hardware design of the Realtek-based XS1930-10 switch from Zyxel. It
features two SFP+ ports/cages whose signals are wired to directly to the
switch SoC. Although Realtek SoCs are short on GPIOs, there are usually
enough the fit the SFP signals without any hacks.

However, Zyxel did some weird design and connected RX_LOS, MOD_ABS and
TX_FAULT of one SFP cage onto a single GPIO line controlled by a
multiplexer (the same for the other SFP cage). The single multiplexer
controls the lines for both SFP and depending on the state, the
designated 'signal GPIO lines' are connected to one of the three SFP
signals.

Because the SFP core/driver doesn't support multiplexer but needs single
GPIOs for each of the signals, this driver fills the gap between both.
It registers a gpio_chip, provides multiple virtual GPIOs and sets the
backing multiplexer accordingly.

Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
---
 MAINTAINERS               |   6 ++
 drivers/gpio/Kconfig      |   8 ++
 drivers/gpio/Makefile     |   1 +
 drivers/gpio/gpio-split.c | 210 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 225 insertions(+)
 create mode 100644 drivers/gpio/gpio-split.c

Comments

Linus Walleij Oct. 14, 2025, 8:37 a.m. UTC | #1
Hi Jonas,

thanks for your patch!

overall I like the idea and I think we definitely need
something like this.

On Fri, Oct 10, 2025 at 12:35 AM Jonas Jelonek <jelonek.jonas@gmail.com> wrote:

> Add a new driver which allows to split a physical GPIO into multiple
> virtual GPIOs by using a multiplexer.
>
> For now, this doesn't support advanced features like IRQs, just normal
> IN and OUT functionality of GPIOs.
>
> This can help in various usecases. One practical case is the special
> hardware design of the Realtek-based XS1930-10 switch from Zyxel. It
> features two SFP+ ports/cages whose signals are wired to directly to the
> switch SoC. Although Realtek SoCs are short on GPIOs, there are usually
> enough the fit the SFP signals without any hacks.
>
> However, Zyxel did some weird design and connected RX_LOS, MOD_ABS and
> TX_FAULT of one SFP cage onto a single GPIO line controlled by a
> multiplexer (the same for the other SFP cage). The single multiplexer
> controls the lines for both SFP and depending on the state, the
> designated 'signal GPIO lines' are connected to one of the three SFP
> signals.
>
> Because the SFP core/driver doesn't support multiplexer but needs single
> GPIOs for each of the signals, this driver fills the gap between both.
> It registers a gpio_chip, provides multiple virtual GPIOs and sets the
> backing multiplexer accordingly.
>
> Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>

This can be made easier these days, reusing the
forwarder library. I think! Check if I'm right.
Make sure you use kernel v6.18-rc1 as a
baseline for your next patch iteration.

select GPIO_AGGREGATOR

#include <linux/gpio/forwarder.h>

Look into this driver for an example of forwarding
GPIO lines:
drivers/pinctrl/pinctrl-upboard.c

See
commit dca2f73cf19fedd7bc38fa6a0eb50fea63cd0214

Now that is a pin controller so it contains a lot of
irrelevant stuff. Focus on the forwarding part.

This part is maybe the most interesting:

        fwd = devm_gpiochip_fwd_alloc(dev, pctrl->pctrl_data->ngpio);

Here ngpio will be 1 for your usecase.

        if (IS_ERR(fwd))
                return dev_err_probe(dev, PTR_ERR(fwd), "Failed to
allocate the gpiochip forwarder\n");

        chip = gpiochip_fwd_get_gpiochip(fwd);
        chip->request = upboard_gpio_request;
        chip->free = upboard_gpio_free;
        chip->get_direction = upboard_gpio_get_direction;
        chip->direction_output = upboard_gpio_direction_output;
        chip->direction_input = upboard_gpio_direction_input;

        ret = gpiochip_fwd_register(fwd, pctrl);
        if (ret)
                return dev_err_probe(dev, ret, "Failed to register the
gpiochip forwarder\n");

As you can see you can override request/free/get_direction etc.

In this case you probably don't want to override these functions,
but instead override chip->get and chip->set so that these set
the mux (and delay a bit?) before reading/writing the line.

->get_multiple and ->set_multiple seems hard to implement
and should probably be overridden with functions returning
an error.

> +++ b/drivers/gpio/gpio-split.c

As mentioned I would call this gpio-line-mux.c

Yours,
Linus Walleij
Jonas Jelonek Oct. 16, 2025, 3:37 p.m. UTC | #2
Hi Linus,

On 14.10.25 10:37, Linus Walleij wrote:
> This can be made easier these days, reusing the
> forwarder library. I think! Check if I'm right.

I think this doesn't really simplify things her. As far as I can see the
GPIO forwarder is more targeted toward 1-to-1 scenarios, requiring some
(or even more?) "hackery" to fit to my 1-to-many.

> This part is maybe the most interesting:
>
>         fwd = devm_gpiochip_fwd_alloc(dev, pctrl->pctrl_data->ngpio);
>
> Here ngpio will be 1 for your usecase.

Giving ngpio=1 here makes the gpiochip only provide a single gpio. This then
needs to be the number of GPIOs defined in the DT (number of child nodes).
But in this case, the internal 'descs' is allocated accordingly and there's a
1-to-1-mapping between the external offset and internal offset. To solve this
I would have to add the same descriptor for the shared gpio multiple times.
Not sure if this is a good idea.

>> +++ b/drivers/gpio/gpio-split.c
> As mentioned I would call this gpio-line-mux.c

Sure, will be changed.

> Yours,
> Linus Walleij

Best,
Jonas
Linus Walleij Oct. 16, 2025, 10:12 p.m. UTC | #3
On Thu, Oct 16, 2025 at 5:37 PM Jonas Jelonek <jelonek.jonas@gmail.com> wrote:

> I think this doesn't really simplify things her. As far as I can see the
> GPIO forwarder is more targeted toward 1-to-1 scenarios, requiring some
> (or even more?) "hackery" to fit to my 1-to-many.

I see hm OK you worked on it so you know better what
will be most elegant here, let's see what the others say.

I'm curious what v2 will look like!

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 681fbc825805..efb7e4a338e0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -10479,6 +10479,12 @@  F:	Documentation/dev-tools/gpio-sloppy-logic-analyzer.rst
 F:	drivers/gpio/gpio-sloppy-logic-analyzer.c
 F:	tools/gpio/gpio-sloppy-logic-analyzer.sh
 
+GPIO SPLIT
+M:	Jonas Jelonek <jelonek.jonas@gmail.com>
+S:	Maintained
+F:	Documentation/devicetree/bindings/gpio/gpio-split.yaml
+F:	drivers/gpio/gpio-split.c
+
 GPIO SUBSYSTEM
 M:	Linus Walleij <linus.walleij@linaro.org>
 M:	Bartosz Golaszewski <brgl@bgdev.pl>
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 867d82b5ed63..9209bc78bd53 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -1988,6 +1988,14 @@  config GPIO_MOCKUP
 	  tools/testing/selftests/gpio/gpio-mockup.sh. Reference the usage in
 	  it.
 
+config GPIO_SPLIT
+	tristate "GPIO split driver"
+	depends on OF_GPIO
+	select MULTIPLEXER
+	help
+	  Say Y here to support splitting physical GPIOs into multiple virtual
+	  GPIOs using a multiplexer.
+
 config GPIO_VIRTIO
 	tristate "VirtIO GPIO support"
 	depends on VIRTIO
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 000fa2e397c2..813eb676e5fb 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -167,6 +167,7 @@  obj-$(CONFIG_GPIO_SLOPPY_LOGIC_ANALYZER) += gpio-sloppy-logic-analyzer.o
 obj-$(CONFIG_GPIO_SODAVILLE)		+= gpio-sodaville.o
 obj-$(CONFIG_GPIO_SPACEMIT_K1)		+= gpio-spacemit-k1.o
 obj-$(CONFIG_GPIO_SPEAR_SPICS)		+= gpio-spear-spics.o
+obj-$(CONFIG_GPIO_SPLIT)		+= gpio-split.o
 obj-$(CONFIG_GPIO_SPRD)			+= gpio-sprd.o
 obj-$(CONFIG_GPIO_STMPE)		+= gpio-stmpe.o
 obj-$(CONFIG_GPIO_STP_XWAY)		+= gpio-stp-xway.o
diff --git a/drivers/gpio/gpio-split.c b/drivers/gpio/gpio-split.c
new file mode 100644
index 000000000000..78da8bf91a0f
--- /dev/null
+++ b/drivers/gpio/gpio-split.c
@@ -0,0 +1,210 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * GPIO splitter acting as virtual gpiochip which "splits" a physical GPIO into
+ * multiple using a multiplexer (e.g. SFP signals RX_LOS, TX_FAULT, MOD_DEF0
+ * muxed on a single GPIO).
+ *
+ * Copyright (c) 2025 Jonas Jelonek <jelonek.jonas@gmail.com>
+ */
+
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/mux/consumer.h>
+#include <linux/mux/driver.h>
+#include <linux/platform_device.h>
+
+
+struct gpio_split_gpio {
+	unsigned int mux_state;
+};
+
+struct gpio_split {
+	struct gpio_chip gc;
+	struct mux_control *mux;
+	struct device *dev;
+
+	struct mutex lock;
+
+	struct gpio_desc *shared_gpio;
+	/* dynamically sized, must be last */
+	struct gpio_split_gpio gpios[];
+};
+
+DEFINE_GUARD(gpio_split, struct gpio_split *, mutex_lock(&_T->lock), mutex_unlock(&_T->lock))
+
+static int gpio_split_gpio_get(struct gpio_chip *gc, unsigned int offset)
+{
+	struct gpio_split *gs = (struct gpio_split *)gpiochip_get_data(gc);
+	struct gpio_split_gpio *gs_gpio;
+	int ret;
+
+	if (offset > gc->ngpio)
+		return -EINVAL;
+
+	guard(gpio_split)(gs);
+
+	gs_gpio = &gs->gpios[offset];
+	ret = mux_control_select(gs->mux, gs_gpio->mux_state);
+	if (ret < 0)
+		return ret;
+
+	ret = gpiod_get_raw_value_cansleep(gs->shared_gpio);
+	mux_control_deselect(gs->mux);
+	return ret;
+}
+
+static void gpio_split_gpio_set(struct gpio_chip *gc, unsigned int offset,
+				int value)
+{
+	struct gpio_split *gs = (struct gpio_split *)gpiochip_get_data(gc);
+	struct gpio_split_gpio *gs_gpio;
+	int ret;
+
+	if (offset > gc->ngpio)
+		return;
+
+	guard(gpio_split)(gs);
+
+	gs_gpio = &gs->gpios[offset];
+	ret = mux_control_select(gs->mux, gs_gpio->mux_state);
+	if (ret < 0)
+		return;
+
+	gpiod_set_raw_value_cansleep(gs->shared_gpio, value);
+	mux_control_deselect(gs->mux);
+}
+
+static int gpio_split_gpio_get_direction(struct gpio_chip *gc,
+					 unsigned int offset)
+{
+	struct gpio_split *gs = (struct gpio_split *)gpiochip_get_data(gc);
+
+	if (offset > gc->ngpio)
+		return -EINVAL;
+
+	guard(gpio_split)(gs);
+
+	return gpiod_get_direction(gs->shared_gpio);
+}
+
+static int gpio_split_gpio_direction_input(struct gpio_chip *gc,
+					   unsigned int offset)
+{
+	struct gpio_split *gs = (struct gpio_split *)gpiochip_get_data(gc);
+
+	if (offset > gc->ngpio)
+		return -EINVAL;
+
+	guard(gpio_split)(gs);
+
+	return gpiod_direction_input(gs->shared_gpio);
+}
+
+static int gpio_split_gpio_direction_output(struct gpio_chip *gc,
+					    unsigned int offset, int value)
+{
+	struct gpio_split *gs = (struct gpio_split *)gpiochip_get_data(gc);
+
+	if (offset > gc->ngpio)
+		return -EINVAL;
+
+	guard(gpio_split)(gs);
+
+	return gpiod_direction_output_raw(gs->shared_gpio, value);
+}
+
+static const struct of_device_id gpio_split_of_match[] = {
+	{ .compatible = "gpio-split" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, gpio_split_of_match);
+
+static int gpio_split_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct gpio_split *gs;
+	struct fwnode_handle *child;
+	unsigned int ngpio, size, i;
+	int ret;
+
+	ngpio = device_get_child_node_count(dev);
+	size = sizeof(*gs) + (sizeof(struct gpio_split_gpio) * ngpio);
+
+	gs = devm_kzalloc(dev, size, GFP_KERNEL);
+	if (!gs)
+		return -ENOMEM;
+
+	mutex_init(&gs->lock);
+
+	gs->dev = dev;
+	gs->gc.base = -1;
+	gs->gc.can_sleep = true;
+	gs->gc.fwnode = dev_fwnode(dev);
+	gs->gc.label = "gpio-split";
+	gs->gc.ngpio = ngpio;
+	gs->gc.owner = THIS_MODULE;
+	gs->gc.parent = dev;
+
+	gs->gc.get = gpio_split_gpio_get;
+	gs->gc.set = gpio_split_gpio_set;
+	gs->gc.get_direction = gpio_split_gpio_get_direction;
+	gs->gc.direction_input = gpio_split_gpio_direction_input;
+	gs->gc.direction_output = gpio_split_gpio_direction_output;
+
+	gs->mux = devm_mux_control_get(dev, NULL);
+	if (IS_ERR(gs->mux)) {
+		if (PTR_ERR(gs->mux) == -EPROBE_DEFER) {
+			dev_err(dev, "mux-controller not ready, deferring probe\n");
+			return -EPROBE_DEFER;
+		}
+
+		dev_err(dev, "could not get mux-controller\n");
+		return PTR_ERR(gs->mux);
+	}
+
+	gs->shared_gpio = devm_gpiod_get(dev, "shared", GPIOD_ASIS);
+	if (IS_ERR(gs->shared_gpio)) {
+		dev_err(dev, "could not get shared-gpio\n");
+		return PTR_ERR(gs->shared_gpio);
+	}
+
+	i = 0;
+	device_for_each_child_node(dev, child) {
+		struct gpio_split_gpio *gpio = &gs->gpios[i];
+		u32 mux_state;
+
+		ret = fwnode_property_read_u32(child, "mux-state", &mux_state);
+		if (ret) {
+			dev_err(dev, "gpio %u: failed to read mux-state\n", i);
+			return ret;
+		}
+
+		gpio->mux_state = (unsigned int)mux_state;
+		i++;
+	}
+
+	ret = devm_gpiochip_add_data(dev, &gs->gc, gs);
+	if (ret) {
+		dev_err(dev, "failed to add gpiochip: %d\n", ret);
+		return ret;
+	}
+
+	dev_info(dev, "providing %u virtual GPIOs for real GPIO %u\n", i,
+		 desc_to_gpio(gs->shared_gpio));
+	return 0;
+}
+
+static struct platform_driver gpio_split_driver = {
+	.driver = {
+		.name = "gpio-split",
+		.of_match_table = gpio_split_of_match,
+	},
+	.probe = gpio_split_probe,
+};
+module_platform_driver(gpio_split_driver);
+
+MODULE_AUTHOR("Jonas Jelonek <jelonek.jonas@gmail.com>");
+MODULE_DESCRIPTION("GPIO split driver");
+MODULE_LICENSE("GPL");