add gpio-line-mux
Subsystems (3)
Jonas Jelonek
maintainerv7 cover add gpio-line-mux
This proposes a new type of virtual GPIO controller and corresponding driver to provide a 1-to-many mapping between virtual GPIOs and a single real GPIO in combination with a multiplexer. 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-controls'.
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 gpio-line-mux-states property.
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.
---
Changelog:
v7: - dt-bindings fixes:
- dropped unneeded reference
- adjusted presumably confusing example
- added missing colon
Link to v6:
https://lore.kernel.org/linux-gpio/[email protected]/
v6: - added count member + __counted_by attribute for gpio_mux_states
- included Reviewed-by tags
Link to v5:
https://lore.kernel.org/linux-gpio/[email protected]/
v5: - renamed "shared" to "muxed" to avoid confusion with Bartosz' work
- dropped Reviewed-by of Krzysztof due to binding change
- use GPIOD_IN in devm_gpiod_get instead of calling gpiod_direction_input explicitly afterwards
Link to v4:
https://lore.kernel.org/linux-gpio/[email protected]/
v4: - dropped useless cast (as suggested by Thomas)
- dropped unneeded locking (as suggested by Peter)
- fixed wording in commit message
- included Reviewed-by of Krzysztof
Link to v3:
https://lore.kernel.org/linux-gpio/[email protected]/
v3: - fixed dt_binding_check errors in DT schema
- as requested by Rob (for DT schema):
- removed example from gpio-mux.yaml
- added '|' to preserve formatting
- 'shared-gpio' --> 'shared-gpios'
- general fixes to DT schema
- use mux_control_select_delay (as suggested by Peter) with hopefully reasonable delay of 100us
- gpiochip ops implementation changes:
- drop '.set' implementation (as suggested by Peter)
- new '.set' implementation just returning -EOPNOTSUPP
- '.direction_output' and '.direction_input' dropped
- '.get_direction' returns fixed value for 'input'
- direction of shared gpio set to input during probe
- as suggested by Thomas
- usage of dev_err_probe
- further simplifications
Since the consensus was that this should be input-only, '.direction_output' and '.direction_input' have been dropped completely, as suggested in the docs of struct gpio_chip. '.set' is kept but returns -ENOTSUPP.
The shared GPIO is set to input during probe, thus '.direction_input' doesn't need to be implemented. '.get_direction' is kept (as suggested in docs of struct gpio_chip) but always returns GPIO_LINE_DIRECTION_IN.
Link to v2:
https://lore.kernel.org/linux-gpio/[email protected]/
v2: - as requested by Linus:
- renamed from 'gpio-split' to 'gpio-line-mux'
- added better description and examples to DT bindings
- simplified driver
- added missing parts to DT bindings
- dropped RFC tag
- renamed patchset
Link to v1 (in case it isn't linked properly due to changed title):
https://lore.kernel.org/linux-gpio/[email protected]/
---
Jonas Jelonek (2):
dt-bindings: gpio: add gpio-line-mux controller
gpio: add gpio-line-mux driver
.../bindings/gpio/gpio-line-mux.yaml | 107 +++++++++++++++ MAINTAINERS | 6 + drivers/gpio/Kconfig | 9 ++ drivers/gpio/Makefile | 1 + drivers/gpio/gpio-line-mux.c | 126 ++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-line-mux.yaml create mode 100644 drivers/gpio/gpio-line-mux.c
base-commit: 48befae0d6eee275c3e30d1cd45f39d6ba011e19
Signature · 1 line
2.48.1
Jonas Jelonek
maintainerPATCH v7 1/2 dt-bindings: gpio: add gpio-line-mux controller
Add dt-schema for a gpio-line-mux controller which exposes virtual GPIOs for a shared GPIO controlled by a multiplexer, e.g. a gpio-mux.
The gpio-line-mux 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.
--- .../bindings/gpio/gpio-line-mux.yaml | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Documentation/devicetree/bindings/gpio/gpio-line-mux.yaml
Documentation/devicetree/bindings/gpio/gpio-line-mux.yaml new file +107 -0
@@ -0,0 +1,107 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/gpio/gpio-line-mux.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: GPIO line mux + +maintainers: + - Jonas Jelonek <[email protected]> + +description: | + A GPIO controller to provide virtual GPIOs for a 1-to-many input-only mapping + backed by a single shared GPIO and a multiplexer. A simple illustrated + example is: + + +----- A + IN / + <-----o------- B + / |\ + | | +----- C + | | \ + | | +--- D + | | + M1 M0 + + MUX CONTROL + + M1 M0 IN + 0 0 A + 0 1 B + 1 0 C + 1 1 D + + This can be used in case a real GPIO is connected to multiple inputs and + controlled by a multiplexer, and another subsystem/driver does not work + directly with the multiplexer subsystem. + +properties: + compatible: + const: gpio-line-mux + + gpio-controller: true + + "#gpio-cells": + const: 2 + + gpio-line-mux-states: + description: Mux states corresponding to the virtual GPIOs. + $ref: /schemas/types.yaml#/definitions/uint32-array + + gpio-line-names: true + + mux-controls: + maxItems: 1 + description: + Phandle to the multiplexer to control access to the GPIOs. + + ngpios: false + + muxed-gpios: + maxItems: 1 + description: + GPIO which is the '1' in 1-to-many and is shared by the virtual GPIOs + and controlled via the mux. + +required: + - compatible + - gpio-controller + - gpio-line-mux-states + - mux-controls + - muxed-gpios + +additionalProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/mux/mux.h> + + sfp_gpio_mux: mux-controller-1 { + compatible = "gpio-mux"; + mux-gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>, + <&gpio0 1 GPIO_ACTIVE_HIGH>; + #mux-control-cells = <0>; + idle-state = <MUX_IDLE_AS_IS>; + }; + + sfp1_gpio: sfp-gpio-1 { + compatible = "gpio-line-mux"; + gpio-controller; + #gpio-cells = <2>; + + mux-controls = <&sfp_gpio_mux>; + muxed-gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>; + + gpio-line-mux-states = <0>, <1>, <3>; + }; + + sfp1: sfp-p1 { + compatible = "sff,sfp"; + + i2c-bus = <&sfp1_i2c>; + los-gpios = <&sfp1_gpio 0 GPIO_ACTIVE_HIGH>; + mod-def0-gpios = <&sfp1_gpio 1 GPIO_ACTIVE_LOW>; + tx-fault-gpios = <&sfp1_gpio 2 GPIO_ACTIVE_HIGH>; + };
Signature · 1 line
2.48.1
Jonas Jelonek
maintainerPATCH v7 2/2 gpio: add gpio-line-mux driver
In reply to Jonas JelonekAdd 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.
--- MAINTAINERS | 6 ++ drivers/gpio/Kconfig | 9 +++ drivers/gpio/Makefile | 1 + drivers/gpio/gpio-line-mux.c | 126 +++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 drivers/gpio/gpio-line-mux.c
MAINTAINERS +6 -0
@@ -10774,6 +10774,12 @@ S: Maintained F: Documentation/devicetree/bindings/leds/irled/gpio-ir-tx.yaml F: drivers/media/rc/gpio-ir-tx.c +GPIO LINE MUX +M: Jonas Jelonek <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/gpio/gpio-line-mux.yaml +F: drivers/gpio/gpio-line-mux.c + GPIO MOCKUP DRIVER M: Bamvor Jian Zhang <[email protected]> L: [email protected]
drivers/gpio/Kconfig +9 -0
@@ -1994,6 +1994,15 @@ config GPIO_LATCH Say yes here to enable a driver for GPIO multiplexers based on latches connected to other GPIOs. +config GPIO_LINE_MUX + tristate "GPIO line mux driver" + depends on OF_GPIO + select MULTIPLEXER + help + Say Y here to support the GPIO line mux, which can provide virtual + GPIOs backed by a shared real GPIO and a multiplexer in a 1-to-many + fashion. + config GPIO_MOCKUP tristate "GPIO Testing Driver (DEPRECATED)" select IRQ_SIM
drivers/gpio/Makefile +1 -0
@@ -90,6 +90,7 @@ obj-$(CONFIG_GPIO_IXP4XX) += gpio-ixp4xx.o obj-$(CONFIG_GPIO_JANZ_TTL) += gpio-janz-ttl.o obj-$(CONFIG_GPIO_KEMPLD) += gpio-kempld.o obj-$(CONFIG_GPIO_LATCH) += gpio-latch.o +obj-$(CONFIG_GPIO_LINE_MUX) += gpio-line-mux.o obj-$(CONFIG_GPIO_LJCA) += gpio-ljca.o obj-$(CONFIG_GPIO_LOGICVC) += gpio-logicvc.o obj-$(CONFIG_GPIO_LOONGSON1) += gpio-loongson1.o
drivers/gpio/gpio-line-mux.c new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [PATCH v7 2/2] gpio: add gpio-line-mux driver
Quoted 5 lines · Jonas Jelonek
drivers/gpio/gpio-line-mux.c new file +5
-0+struct gpio_lmux { +struct gpio_chip gc; +struct mux_control *mux; +struct gpio_desc *muxed_gpio; +
drivers/gpio/gpio-line-mux.c line 32
Quoted 10 lines
drivers/gpio/gpio-line-mux.c new file +10
-0+u32 num_gpio_mux_states; + unsigned int gpio_mux_states[] __counted_by(num_gpio_mux_states); +}; + +static int gpio_lmux_gpio_get (struct gpio_chip *gc,unsigned int offset) +{ +struct gpio_lmux *glm =gpiochip_get_data (gc); +int ret; + +if (offset > gc->ngpio)
This is off by one. Should be >=.
regards,
dan carpenter
Quoted 11 lines
drivers/gpio/gpio-line-mux.c new file +11
-0+return -EINVAL ; + + ret =mux_control_select_delay (glm->mux, glm->gpio_mux_states[offset], +MUX_SELECT_DELAY_US ); +if (ret <0 ) +return ret; + + ret =gpiod_get_raw_value_cansleep (glm->muxed_gpio); +mux_control_deselect (glm->mux); +return ret; +}
Re: [PATCH v7 2/2] gpio: add gpio-line-mux driver
Quoted 11 lines · Dan Carpenter
Quoted 9 lines · Jonas Jelonek
drivers/gpio/gpio-line-mux.c new file +9
-0+struct gpio_lmux { +struct gpio_chip gc; +struct mux_control *mux; +struct gpio_desc *muxed_gpio; + +u32 num_gpio_mux_states; + unsigned int gpio_mux_states[] __counted_by(num_gpio_mux_states); +}; +
Quoted 9 lines
drivers/gpio/gpio-line-mux.c line 32
Quoted 6 lines
drivers/gpio/gpio-line-mux.c new file +6
-0+static int gpio_lmux_gpio_get (struct gpio_chip *gc,unsigned int offset) +{ +struct gpio_lmux *glm =gpiochip_get_data (gc); +int ret; + +if (offset > gc->ngpio)This is off by one. Should be >=.
And it's not even needed, GPIO core handles it. I'll send a patch to remove it.
Bart
Bartosz Golaszewski
AppliedQuoted 16 lines · Jonas Jelonek
This proposes a new type of virtual GPIO controller and corresponding driver to provide a 1-to-many mapping between virtual GPIOs and a single real GPIO in combination with a multiplexer. 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 signalsRX_LOS,MOD_ABSandTX_FAULTshare 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 theTX_DISABLEis separate.
Quoted 1 line
[...]
Applied, thanks!
[1/2] dt-bindings: gpio: add gpio-line-mux controller
commit: 2a7618ba8698874e9871a8ec5453e0068e94d9e5
[2/2] gpio: add gpio-line-mux driver
commit: 2b03d9a40cd1fea42fd65d2b66df80edc0f374c8Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>
Linus Walleij
maintainerQuoted 4 lines · Jonas Jelonek
This proposes a new type of virtual GPIO controller and corresponding driver to provide a 1-to-many mapping between virtual GPIOs and a single real GPIO in combination with a multiplexer. Existing drivers apparently do not serve the purpose for what I need.
Thanks for persisting and doing this the right way all the way through, Jonas!
This driver will probably be helpful for other users with the same quirk in the future.
Yours,
Linus Walleij