From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE532286883 for ; Thu, 9 Oct 2025 22:35:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760049322; cv=none; b=d85ex8R2XGtwvHQgli2mnxgQJffj2Qx8oA2cdAV6Qph/3Lj9sA3k4YiAvRQU+UpcvMSrd16CT/kvAgr63MuoMgLY3v+K1Js342NtdTfl59Ni3xp50D3zqkMYpGP0Iqne4dknD81tFXzlo+NcFKlmtKYuD02l5+zjHiPwfPDiDi4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760049322; c=relaxed/simple; bh=PiqFJX7DaQjelivbGJ1zkt3d8F0vNnikrdG+r1OY1/U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bRK/BJnmJQdLcnBGauzsLrs/+Oeo+F7ShYehi7AkkaD02R65m+C3lQ5SCdUFv0VFcCTUOIY+uWZQOyPiAgsLjsLcMVQ4WlBqq8cnU9bIO5gUegTtj6JMCZZkrMy6F3GBeZQza/Ic4BC8/398Dovvq3DGp2fL3Hl6EmA4bKrjwvQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HwHo41s4; arc=none smtp.client-ip=209.85.208.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HwHo41s4" Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-62ec5f750f7so2561467a12.3 for ; Thu, 09 Oct 2025 15:35:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760049319; x=1760654119; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kOyZxlvZUezvZb4UBJSZAR11hnNz49iBIAa7TvCXQCI=; b=HwHo41s4xilmqNXVQDwdBBsVN43WCmEZaW0QBBf9Mjq6lbPq2QnUixXnyXYSNvryMJ gcCwsT82YI8uV+oELEbPIO74dRgpILXj9WMePX4pE5ad4h0cELG7me2w0ut6qloH+Vw/ MO1CeTZ6P1IAIUBROgkP+vE1ksf5FrWU57GUTzxJNIss9myWYTrgHcFfm5JPkqBrM91Z STBvMDkDwJ6jpB77wwN1ZL3eT07uIzuj11Re7e80u0tTqLlEtUsqw83PiKtyWxxKRclx Y8Lr/dPGDe0ZlS5SN/2EGBuvEKrbqAwdTA7yyNT9/QkkMUDfK3QXY4rrrbf8cb6R44X8 nDng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760049319; x=1760654119; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kOyZxlvZUezvZb4UBJSZAR11hnNz49iBIAa7TvCXQCI=; b=tdz+nDCaKfnzqVAvuC3YHbfLJP9RVYKQis4mpha0C0fezVpPP3Cdo1BSK7CTAjafri wcEeeGzStzAj31ZHxXfr9i6CVXfC5bTIkeG9KP00s28WdTmtIQt+g1s2RIfgLQy2V10u 1vamrSQAvJrF3xESRCPeenbHqTW2ew3UdO064H1QgmgGqBaJR61zIunqP8HWv2CuQ0oE G6xi/1U0DPJIe8c9EvhbsMv3VIwKrYUk2Au8FNfOkaIzqrjxuuVXdpLO6sGPD/5IeJXX wqMHsDp0LiVgcHA4S4/5q80O8wZ3hIXORRSkSXfMcnV9u/tZptiMPdyBhAj0eD8Zuiuq FnAQ== X-Forwarded-Encrypted: i=1; AJvYcCX9Xs6bMOiZVve+O0SHtHt1b88jhUPOaNVhjbXYi0eJM8bZ+YJ8r2q29YO2hNHMEx7fACKLrG/APbLm@vger.kernel.org X-Gm-Message-State: AOJu0Yx+zelgT+X4DEprGygqqvETX12rnjrtBmiSf5/5+ViSRigNGI9a yR0slCVScYeGMTwELh24FW/vG2w9E8+c4Zh8U9tDBndA4p+66IDpGUPa X-Gm-Gg: ASbGncvc+MaEoFo1043iSHBSD+dEc0EGE9Yw4kGxmjSjNhGbEpY0Qr1OwsqOwMYBYRl yH/NOn6Jg+tTyKdooYhBmpWuvEcaLPFmjsQ+4k56zhJ1aQubte9IfYo+QYqkwZJjie/S/QjCjjU 8rXD5XhMsQ60vmMCuhWvgVFwH7ysQEMRnIc4LLpdGJDDyHgnA6bqJc+keC1t0Zr8My9VZ2SaEhZ kpaYFiBydVLBPEjRf6EZTqNOLXAHnOMdp8D0UuXs3iV418THA6BwTGMOOZlYSuFzzcgTAVw+0GR HsTfCFmJS8m9dtCqMnzFXpWT3ixeKlh7wbToRVO7fE/XHkRFZxzUzq71jyXFI13iIjfBDeva2d4 OycNWZp2WtqkA8rSWFAw5GTQtXYdTaPeLoFSU+sM3ZTBIa4rW2w== X-Google-Smtp-Source: AGHT+IHpvdQ3Lze478kQb743ESJRaIFc+pf9zKNzzGA7lcUz5m44CBD9OWwEjiGXY+Gi37Oo35E6PQ== X-Received: by 2002:a17:907:26c8:b0:b3f:ccac:af38 with SMTP id a640c23a62f3a-b50ac1cc387mr1058532666b.30.1760049319056; Thu, 09 Oct 2025 15:35:19 -0700 (PDT) Received: from builder.. ([2001:9e8:f121:c116:be24:11ff:fe30:5d85]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b55d5cad80bsm74383766b.16.2025.10.09.15.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Oct 2025 15:35:18 -0700 (PDT) From: Jonas Jelonek To: Linus Walleij , Bartosz Golaszewski , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-gpio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Jonas Jelonek Subject: [RFC PATCH v1 2/2] gpio: add gpio-split driver Date: Thu, 9 Oct 2025 22:35:01 +0000 Message-ID: <20251009223501.570949-3-jelonek.jonas@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20251009223501.570949-1-jelonek.jonas@gmail.com> References: <20251009223501.570949-1-jelonek.jonas@gmail.com> Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 +S: Maintained +F: Documentation/devicetree/bindings/gpio/gpio-split.yaml +F: drivers/gpio/gpio-split.c + GPIO SUBSYSTEM M: Linus Walleij M: Bartosz Golaszewski 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 + */ + +#include +#include +#include +#include +#include +#include +#include + + +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 "); +MODULE_DESCRIPTION("GPIO split driver"); +MODULE_LICENSE("GPL"); -- 2.48.1