Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

gpio: add gpio-line-mux driver

Add a new driver which provides a 1-to-many mapping for a single real
GPIO using a multiplexer. Each virtual GPIO corresponds to a multiplexer
state which, if set for the multiplexer, connects the real GPIO to the
corresponding virtual GPIO.

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 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.

Due to several practical issues, this is input-only and doesn't support
IRQs.

Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
Reviewed-by: Thomas Richard <thomas.richard@bootlin.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20251227180134.1262138-3-jelonek.jonas@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

authored by

Jonas Jelonek and committed by
Bartosz Golaszewski
2b03d9a4 2a7618ba

+142
+6
MAINTAINERS
··· 10774 10774 F: Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.yaml 10775 10775 F: drivers/media/rc/gpio-ir-tx.c 10776 10776 10777 + GPIO LINE MUX 10778 + M: Jonas Jelonek <jelonek.jonas@gmail.com> 10779 + S: Maintained 10780 + F: Documentation/devicetree/bindings/gpio/gpio-line-mux.yaml 10781 + F: drivers/gpio/gpio-line-mux.c 10782 + 10777 10783 GPIO MOCKUP DRIVER 10778 10784 M: Bamvor Jian Zhang <bamv2005@gmail.com> 10779 10785 L: linux-gpio@vger.kernel.org
+9
drivers/gpio/Kconfig
··· 1994 1994 Say yes here to enable a driver for GPIO multiplexers based on latches 1995 1995 connected to other GPIOs. 1996 1996 1997 + config GPIO_LINE_MUX 1998 + tristate "GPIO line mux driver" 1999 + depends on OF_GPIO 2000 + select MULTIPLEXER 2001 + help 2002 + Say Y here to support the GPIO line mux, which can provide virtual 2003 + GPIOs backed by a shared real GPIO and a multiplexer in a 1-to-many 2004 + fashion. 2005 + 1997 2006 config GPIO_MOCKUP 1998 2007 tristate "GPIO Testing Driver (DEPRECATED)" 1999 2008 select IRQ_SIM
+1
drivers/gpio/Makefile
··· 90 90 obj-$(CONFIG_GPIO_JANZ_TTL) += gpio-janz-ttl.o 91 91 obj-$(CONFIG_GPIO_KEMPLD) += gpio-kempld.o 92 92 obj-$(CONFIG_GPIO_LATCH) += gpio-latch.o 93 + obj-$(CONFIG_GPIO_LINE_MUX) += gpio-line-mux.o 93 94 obj-$(CONFIG_GPIO_LJCA) += gpio-ljca.o 94 95 obj-$(CONFIG_GPIO_LOGICVC) += gpio-logicvc.o 95 96 obj-$(CONFIG_GPIO_LOONGSON1) += gpio-loongson1.o
+126
drivers/gpio/gpio-line-mux.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * GPIO line mux which acts as virtual gpiochip and provides a 1-to-many 4 + * mapping between virtual GPIOs and a real GPIO + multiplexer. 5 + * 6 + * Copyright (c) 2025 Jonas Jelonek <jelonek.jonas@gmail.com> 7 + */ 8 + 9 + #include <linux/gpio/consumer.h> 10 + #include <linux/gpio/driver.h> 11 + #include <linux/mod_devicetable.h> 12 + #include <linux/mutex.h> 13 + #include <linux/mux/consumer.h> 14 + #include <linux/platform_device.h> 15 + 16 + #define MUX_SELECT_DELAY_US 100 17 + 18 + struct gpio_lmux { 19 + struct gpio_chip gc; 20 + struct mux_control *mux; 21 + struct gpio_desc *muxed_gpio; 22 + 23 + u32 num_gpio_mux_states; 24 + unsigned int gpio_mux_states[] __counted_by(num_gpio_mux_states); 25 + }; 26 + 27 + static int gpio_lmux_gpio_get(struct gpio_chip *gc, unsigned int offset) 28 + { 29 + struct gpio_lmux *glm = gpiochip_get_data(gc); 30 + int ret; 31 + 32 + if (offset > gc->ngpio) 33 + return -EINVAL; 34 + 35 + ret = mux_control_select_delay(glm->mux, glm->gpio_mux_states[offset], 36 + MUX_SELECT_DELAY_US); 37 + if (ret < 0) 38 + return ret; 39 + 40 + ret = gpiod_get_raw_value_cansleep(glm->muxed_gpio); 41 + mux_control_deselect(glm->mux); 42 + return ret; 43 + } 44 + 45 + static int gpio_lmux_gpio_set(struct gpio_chip *gc, unsigned int offset, 46 + int value) 47 + { 48 + return -EOPNOTSUPP; 49 + } 50 + 51 + static int gpio_lmux_gpio_get_direction(struct gpio_chip *gc, 52 + unsigned int offset) 53 + { 54 + return GPIO_LINE_DIRECTION_IN; 55 + } 56 + 57 + static int gpio_lmux_probe(struct platform_device *pdev) 58 + { 59 + struct device *dev = &pdev->dev; 60 + struct gpio_lmux *glm; 61 + unsigned int ngpio; 62 + size_t size; 63 + int ret; 64 + 65 + ngpio = device_property_count_u32(dev, "gpio-line-mux-states"); 66 + if (!ngpio) 67 + return -EINVAL; 68 + 69 + size = struct_size(glm, gpio_mux_states, ngpio); 70 + glm = devm_kzalloc(dev, size, GFP_KERNEL); 71 + if (!glm) 72 + return -ENOMEM; 73 + 74 + glm->gc.base = -1; 75 + glm->gc.can_sleep = true; 76 + glm->gc.fwnode = dev_fwnode(dev); 77 + glm->gc.label = dev_name(dev); 78 + glm->gc.ngpio = ngpio; 79 + glm->gc.owner = THIS_MODULE; 80 + glm->gc.parent = dev; 81 + 82 + glm->gc.get = gpio_lmux_gpio_get; 83 + glm->gc.set = gpio_lmux_gpio_set; 84 + glm->gc.get_direction = gpio_lmux_gpio_get_direction; 85 + 86 + glm->mux = devm_mux_control_get(dev, NULL); 87 + if (IS_ERR(glm->mux)) 88 + return dev_err_probe(dev, PTR_ERR(glm->mux), 89 + "could not get mux controller\n"); 90 + 91 + glm->muxed_gpio = devm_gpiod_get(dev, "muxed", GPIOD_IN); 92 + if (IS_ERR(glm->muxed_gpio)) 93 + return dev_err_probe(dev, PTR_ERR(glm->muxed_gpio), 94 + "could not get muxed-gpio\n"); 95 + 96 + glm->num_gpio_mux_states = ngpio; 97 + ret = device_property_read_u32_array(dev, "gpio-line-mux-states", 98 + &glm->gpio_mux_states[0], ngpio); 99 + if (ret) 100 + return dev_err_probe(dev, ret, "could not get mux states\n"); 101 + 102 + ret = devm_gpiochip_add_data(dev, &glm->gc, glm); 103 + if (ret) 104 + return dev_err_probe(dev, ret, "failed to add gpiochip\n"); 105 + 106 + return 0; 107 + } 108 + 109 + static const struct of_device_id gpio_lmux_of_match[] = { 110 + { .compatible = "gpio-line-mux" }, 111 + { } 112 + }; 113 + MODULE_DEVICE_TABLE(of, gpio_lmux_of_match); 114 + 115 + static struct platform_driver gpio_lmux_driver = { 116 + .driver = { 117 + .name = "gpio-line-mux", 118 + .of_match_table = gpio_lmux_of_match, 119 + }, 120 + .probe = gpio_lmux_probe, 121 + }; 122 + module_platform_driver(gpio_lmux_driver); 123 + 124 + MODULE_AUTHOR("Jonas Jelonek <jelonek.jonas@gmail.com>"); 125 + MODULE_DESCRIPTION("GPIO line mux driver"); 126 + MODULE_LICENSE("GPL");