[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251009223501.570949-3-jelonek.jonas@gmail.com>
Date: Thu, 9 Oct 2025 22:35:01 +0000
From: Jonas Jelonek <jelonek.jonas@...il.com>
To: Linus Walleij <linus.walleij@...aro.org>,
Bartosz Golaszewski <brgl@...ev.pl>,
Rob Herring <robh@...nel.org>,
Krzysztof Kozlowski <krzk+dt@...nel.org>,
Conor Dooley <conor+dt@...nel.org>
Cc: linux-gpio@...r.kernel.org,
devicetree@...r.kernel.org,
linux-kernel@...r.kernel.org,
Jonas Jelonek <jelonek.jonas@...il.com>
Subject: [RFC PATCH v1 2/2] gpio: add gpio-split driver
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@...il.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
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@...il.com>
+S: Maintained
+F: Documentation/devicetree/bindings/gpio/gpio-split.yaml
+F: drivers/gpio/gpio-split.c
+
GPIO SUBSYSTEM
M: Linus Walleij <linus.walleij@...aro.org>
M: Bartosz Golaszewski <brgl@...ev.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@...il.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@...il.com>");
+MODULE_DESCRIPTION("GPIO split driver");
+MODULE_LICENSE("GPL");
--
2.48.1
Powered by blists - more mailing lists