add support for splitting GPIOs
Subsystems (2)
cover add support for splitting GPIOs
Hereby I propose a new type of virtual GPIO controller and corresponding driver for splitting GPIOs using a multiplexer for different usecases. Existing drivers apparently do not serve the purpose for what I need.
I came across an issue with a switch device from Zyxel which has two SFP+ cages. Most similar switches either wire up the SFP signals (RX_LOS, MOD_ABS, TX_FAULT, TX_DISABLE) directly to the SoC (if it has enough GPIOs) or two a GPIO expander (for which a driver usually exists). However, Zyxel decided to do it differently in the following way:
The signals RX_LOS, MOD_ABS and TX_FAULT share a single GPIO line to the SoC. Which one is actually connected to that GPIO line at a time is controlled by a separate multiplexer, a GPIO multiplexer in this case (which uses two other GPIOs). Only the TX_DISABLE is separate.
The SFP core/driver doesn't seem to support such a usecase for now, for each signal one needs to specify a separate GPIO like:
los-gpio = <&gpio0 0 GPIO_ACTIVE_HIGH>; mod-def0-gpio = <&gpio0 1 GPIO_ACTIVE_LOW>; ...
But for my device, I actually need to directly specify multiplexing behavior in the SFP node or provide a mux-controller with 'mux-control'.
To fill this gap, I created a dt-schema and a working driver which exactly does what is needed. It takes a phandle to a mux-controller and the 'shared' gpio, and provides several virtual GPIOs based on the children nodes defined for the gpio-split controller, each with a mux-state which is used on access.
This virtual gpio-controller can then be referenced in the '-gpio' properties of the SFP node (or other nodes depending on the usecase) as usual and do not require any modification to the SFP core/driver.
--
Jonas Jelonek (2):
dt-bindings: gpio: add gpio-split controller
gpio: add gpio-split driver
.../devicetree/bindings/gpio/gpio-split.yaml | 77 +++++++ MAINTAINERS | 6 + drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-split.c | 210 ++++++++++++++++++ 5 files changed, 302 insertions(+) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-split.yaml create mode 100644 drivers/gpio/gpio-split.c
base-commit: bc061143637532c08d9fc657eec93fdc2588068e
Signature · 1 line
2.48.1
RFC PATCH 1/2 dt-bindings: gpio: add gpio-split controller
Add dt-schema for a virtual gpio-split controller which exposes virtual GPIOs for a shared GPIO controlled by a multiplexer, e.g. a gpio-mux.
The gpio-split controller is a gpio-controller, thus has mostly the same semantics. However, it requires a mux-control to be specified upon which it will operate.
--- .../devicetree/bindings/gpio/gpio-split.yaml | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-split.yaml
Documentation/devicetree/bindings/gpio/gpio-split.yaml new file +77 -0
@@ -0,0 +1,77 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/gpio/gpio-split.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: GPIO split + +maintainers: + - Jonas Jelonek <[email protected]> + +description: + A virtual GPIO controller to provide virtual GPIOs backed by a single real + GPIO and a multiplexer. This controller may be used in case a real GPIO is + connected to multiple inputs/outputs and controlled by a multiplexer, and + another subsystem/driver is not able to work with multiplexer subsystem. + +properties: + compatible: + const: gpio-split + + gpio-controller: true + + "#gpio-cells": + const: 2 + + gpio-line-names: true + + mux-controls: + maxItems: 1 + + ngpios: false + + shared-gpio: + description: + GPIO that is shared by the virtual GPIOs and controlled via the mux. + +required: + - compatible + - gpio-controller + - mux-controls + - shared-gpio + +additionalProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/mux/mux.h> + + sfp_gpio_mux: gpio-mux { + compatible = "gpio-mux"; + mux-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>, + <&gpio0 14 GPIO_ACTIVE_HIGH>; + #mux-control-cells = <0>; + idle-state = <MUX_IDLE_AS_IS>; + }; + + sfp1_gpio: sfp-gpio-1 { + compatible = "gpio-split"; + gpio-controller; + #gpio-cells = <2>; + + mux-controls = <&sfp_gpio_mux>; + shared-gpio = <&gpio0 19 GPIO_ACTIVE_HIGH>; + + gpio-line-names = "SFP1_LOS", "SFP1_MOD_ABS", "SFP1_TX_FAULT"; + gpio-0 { + mux-state = <0>; + }; + gpio-1 { + mux-state = <1>; + }; + gpio-2 { + mux-state = <3>; + }; + };
Signature · 1 line
2.48.1
RFC PATCH 2/2 gpio: add gpio-split driver
In reply to Jonas JelonekAdd 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.
--- 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
MAINTAINERS +6 -0
@@ -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 <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/gpio/gpio-split.yaml +F: drivers/gpio/gpio-split.c + GPIO SUBSYSTEM M: Linus Walleij <[email protected]> M: Bartosz Golaszewski <[email protected]>
drivers/gpio/Kconfig +8 -0
@@ -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
drivers/gpio/Makefile +1 -0
@@ -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
drivers/gpio/gpio-split.c new file +210 -0
210 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [RFC PATCH v1 1/2] dt-bindings: gpio: add gpio-split controller
In reply to Jonas JelonekQuoted 12 lines · Jonas Jelonek
Documentation/devicetree/bindings/gpio/gpio-split.yaml new file +6
-0@@ -0,0 +1,77 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/gpio/gpio-split.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# +
Documentation/devicetree/bindings/gpio/gpio-split.yaml line 16
Quoted 10 lines
Documentation/devicetree/bindings/gpio/gpio-split.yaml new file +10
-0+title: GPIO split + +maintainers: + - Jonas Jelonek <[email protected]> + +description: + A virtual GPIO controller to provide virtual GPIOs backed by a single real + GPIO and a multiplexer. This controller may be used in case a real GPIO is + connected to multiple inputs/outputs and controlled by a multiplexer, and + another subsystem/driver is not able to work with multiplexer subsystem.
Instead, please fix the subsystem driver?
I have also doubts we actually want it in DT, because we tie the limitation of Linux drivers to bindings.
Best regards,
Krzysztof
Re: [RFC PATCH v1 1/2] dt-bindings: gpio: add gpio-split controller
Hi Krzysztof,
Quoted 1 line · Krzysztof Kozlowski
Instead, please fix the subsystem driver?
That's why I sent this as an RFC. I thought about this too but have no good idea how to represent this in DT while keeping the existing bindings.
So I just thought this small driver would be the easier way, possibly being useful for other potential usecases.
But sure, if this is the only way to go I'll have to RFC this at the SFP driver side asking for any ideas how this could be achieved.
Quoted 6 lines
I have also doubts we actually want it in DT, because we tie the limitation of Linux drivers to bindings.
Best regards,
Krzysztof
Best regards,
Jonas
Re: [RFC PATCH v1 1/2] dt-bindings: gpio: add gpio-split controller
In reply to Jonas JelonekHi Jonas,
thanks for your patch!
Including Peter Rosin (the gpio-mux author) and Geert Uytterhoeven on this review, as they have worked with similar stuff. Please include them on future postings. The result definitely need Peters ack before we can merge it.
Quoted 8 lines · Jonas Jelonek
Add dt-schema for a virtual gpio-split controller which exposes virtual GPIOs for a shared GPIO controlled by a multiplexer, e.g. a gpio-mux.
The gpio-split controller is a gpio-controller, thus has mostly the same semantics. However, it requires a mux-control to be specified upon which it will operate.
Signed-off-by Jonas Jelonek <[email protected]>
So if I understand it correctly this models a 1-to-many input-only GPIO multiplexer, we need an illustration such as
+----- A
IN /
<-----o------- B
/ |\
| | +----- C
| | \
| | +--- D
| |M1 M0
MUX CONTROL
M1 M0 INPUT 0 0 A 0 1 B 1 0 C 1 1 D
Is this correct? In that case include something like this verbatim in the bindings (feel free to copy/modify this) as it makes it much easier to understand what is going on.
That's a very minimal example of a way to turn 3 GPIO lines into 4 GPIO lines, which is a bit crazy but I'm not the one to tell vendors what to do :D
Documentation/devicetree/bindings/gpio/gpio-split.yaml line 30
Quoted 2 lines
Documentation/devicetree/bindings/gpio/gpio-split.yaml new file +2
-0+ mux-controls: + maxItems: 1
So this needs a description, it is a phandle to the gpio multiplexer (reference /schemas/mux/gpio-mux.yaml explicitly!) used by the splitter.
You should also in the same patch add an example to /schemas/mux/gpio-mux.yaml showing how this is used to muliplex GPIOs so people find this new usecase easily.
Documentation/devicetree/bindings/gpio/gpio-split.yaml line 36
Quoted 3 lines
Documentation/devicetree/bindings/gpio/gpio-split.yaml new file +3
-0+ shared-gpio: + description: + GPIO that is shared by the virtual GPIOs and controlled via the mux.
So this one is shared one-to-many, and I think the bindings overall makes sense.
Maybe "gpio-split" is a bit ambiguous?
We have io-channel-mux, so what about "gpio-line-mux" simply?
The fact that GPIO lines are used to do the muxing is just a detail since a mux is an abstract concept, it could have just as well been muxed with some I2C device for example.
Yours,
Linus Walleij
Re: [RFC PATCH v1 2/2] gpio: add gpio-split driver
In reply to Jonas JelonekHi Jonas,
thanks for your patch!
overall I like the idea and I think we definitely need something like this.
Quoted 19 lines · Jonas Jelonek
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_ABSandTX_FAULTof 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.
Quoted 6 lines
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 <[email protected]>
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 thegpiochip 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.
Quoted 1 line
+++
b/drivers/gpio/gpio-split.c
As mentioned I would call this gpio-line-mux.c
Yours,
Linus Walleij
Re: [RFC PATCH v1 1/2] dt-bindings: gpio: add gpio-split controller
In reply to Linus WalleijHi Linus,
Quoted 8 lines · Linus Walleij
Hi Jonas,
thanks for your patch!
Including Peter Rosin (the gpio-mux author) and Geert Uytterhoeven on this review, as they have worked with similar stuff. Please include them on future postings. The result definitely need Peters ack before we can merge it.
Thanks, will do.
Quoted 21 lines
So if I understand it correctly this models a 1-to-many input-only GPIO multiplexer, we need an illustration such as
+----- A IN / <-----o------- B / |\ | | +----- C | | \ | | +--- D | |M1 M0
MUX CONTROL
M1 M0 INPUT 0 0 A 0 1 B 1 0 C 1 1 D
Quoted 3 lines
Is this correct? In that case include something like this verbatim in the bindings (feel free to copy/modify this) as it makes it much easier to understand what is going on.
You nailed it. I'll include your drawing in the bindings then in the next revision. Only thing is that I just didn't limit it to being 'input-only'. I have no real usecase for this but to me there was no really obvious issue needing this to be input-only.
Quoted 3 lines
That's a very minimal example of a way to turn 3 GPIO lines into 4 GPIO lines, which is a bit crazy but I'm not the one to tell vendors what to do :D
On my device it's actually that the single GPIO mux controls the signals for both SFP cages, meaning it makes more sense in the big picture though a GPIO expander as all other vendors do would've been better IMO ^^.
Quoted 9 lines
Documentation/devicetree/bindings/gpio/gpio-split.yaml line 30
Quoted 2 lines
Documentation/devicetree/bindings/gpio/gpio-split.yaml new file +2
-0+ mux-controls: + maxItems: 1So this needs a description, it is a phandle to the gpio multiplexer (reference /schemas/mux/gpio-mux.yaml explicitly!) used by the splitter.
You should also in the same patch add an example to /schemas/mux/gpio-mux.yaml showing how this is used to muliplex GPIOs so people find this new usecase easily.
Sure, will add it.
Quoted 5 lines
Documentation/devicetree/bindings/gpio/gpio-split.yaml line 36
Quoted 3 lines
Documentation/devicetree/bindings/gpio/gpio-split.yaml new file +3
-0+ shared-gpio: + description: + GPIO that is shared by the virtual GPIOs and controlled via the mux.So this one is shared one-to-many, and I think the bindings overall makes sense.
I'll also add this hint to the description to make it clearer.
Quoted 7 lines
Maybe "gpio-split" is a bit ambiguous?
We have io-channel-mux, so what about "gpio-line-mux" simply?The fact that GPIO lines are used to do the muxing is just a detail since a mux is an abstract concept, it could have just as well been muxed with some I2C device for example.
Sure, this was just my initial idea but I'm not fixed to it. I can adjust that in the next iteration.
Quoted 2 lines
Yours,
Linus Walleij
Best,
Jonas
Re: [RFC PATCH v1 2/2] gpio: add gpio-split driver
In reply to Linus WalleijHi Linus,
Quoted 2 lines · Linus Walleij
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.
Quoted 5 lines
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.
Quoted 2 lines
Quoted 1 line
+++
b/drivers/gpio/gpio-split.cAs mentioned I would call this
gpio-line-mux.c
Sure, will be changed.
Quoted 2 lines
Yours,
Linus Walleij
Best,
Jonas
Re: [RFC PATCH v1 2/2] gpio: add gpio-split driver
Quoted 3 lines · Jonas Jelonek
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