[PATCH v2 3/3] serial: core: Drive the transceiver interface select GPIO

From: Tapio Reijonen

Date: Thu Nov 05 2026 - 02:13:33 EST


Multiprotocol transceivers such as the LTC2873 provide both an RS232
and an RS485 interface on the same bus pins and choose between them
with an interface select input. There is no generic way to drive that
input today, so a port wired to such a transceiver is stuck in
whichever interface the pin is strapped to.

Add the optional rs485-select-gpios property to the GPIOs acquired by
uart_get_rs485_mode() and drive it from the RS485 configuration state:
the GPIO is active while RS485 is enabled, which selects the RS485
interface, and inactive otherwise, which selects the alternative
interface, typically RS232. RS422 is a mode of SER_RS485_ENABLED and
therefore selects the RS485 interface as well.

The state is applied in uart_rs485_config() for the boot-time and
resume paths and in uart_set_rs485_config() for the ioctl path. Unlike
the termination and RX-during-TX GPIOs, which only matter while RS485
is enabled, this GPIO must also be driven when RS485 is disabled to
return the transceiver to its alternative interface. It is deasserted
when the driver rejects the boot-time configuration, restored to the
previous setting when the driver rejects an ioctl, and forgotten with
the other RS485 descriptors on failure and port removal.

Unlike the termination and RX-during-TX lines, whose level is harmless
before the port is configured, the select line decides which driver sits
on the bus pins. Driving it to the configured state at request time is
wrong either way: a port strapped for RS485 would present the
alternative interface during probe, and a port strapped for the
alternative interface would select RS485 while probe code such as the
8250 autoconfig still toggles RTS, the driver enable in RS485 mode.
Request the GPIO without changing its state and make it an output at the
level it already has, so the transceiver keeps the interface it was
strapped for until uart_rs485_config() runs after the driver has placed
RTS at its RS485 idle level. A line that cannot be read back, as on
output-only GPIO controllers, starts at the inactive level instead of
failing the probe. A port that boots with RS485 disabled is switched to
the alternative interface by uart_rs485_config() as well, after the
modem-control lines have been deasserted and without involving the
driver callback.

In RS485 mode RTS is the driver enable of the transceiver. The GPIO is
therefore driven after the driver callback when RS485 is enabled, so
RTS already sits at its RS485 idle level when the transceiver switches
over, and before the driver callback when RS485 is disabled, so the
transceiver has left RS485 mode before RTS reverts to modem control.

Drivers calling uart_get_rs485_mode() need no change to support such
transceivers.

Signed-off-by: Tapio Reijonen <tapio.reijonen@xxxxxxxxxxx>
---
drivers/tty/serial/serial_core.c | 45 +++++++++++++++++++++++++++++++++++++++-
include/linux/serial_core.h | 1 +
2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 9516f50a72139c73d70826f4bb0627aed2d3d310..6c0a6d9dcb851b6db0acd91143e651bcb33ce5ae 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1424,6 +1424,7 @@ static void uart_forget_rs485_gpios(struct uart_port *port)
{
port->rs485_term_gpio = NULL;
port->rs485_rx_during_tx_gpio = NULL;
+ port->rs485_select_gpio = NULL;
}

static void uart_set_rs485_termination(struct uart_port *port,
@@ -1446,13 +1447,22 @@ static void uart_set_rs485_rx_during_tx(struct uart_port *port,
!!(rs485->flags & SER_RS485_RX_DURING_TX));
}

+static void uart_set_rs485_select_gpio(struct uart_port *port,
+ const struct serial_rs485 *rs485)
+{
+ gpiod_set_value_cansleep(port->rs485_select_gpio,
+ !!(rs485->flags & SER_RS485_ENABLED));
+}
+
static int uart_rs485_config(struct uart_port *port)
{
struct serial_rs485 *rs485 = &port->rs485;
int ret;

- if (!(rs485->flags & SER_RS485_ENABLED))
+ if (!(rs485->flags & SER_RS485_ENABLED)) {
+ uart_set_rs485_select_gpio(port, rs485);
return 0;
+ }

uart_sanitize_serial_rs485(port, rs485);
uart_set_rs485_termination(port, rs485);
@@ -1467,6 +1477,8 @@ static int uart_rs485_config(struct uart_port *port)
gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
}

+ uart_set_rs485_select_gpio(port, rs485);
+
return ret;
}

@@ -1503,6 +1515,10 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
uart_set_rs485_termination(port, &rs485);
uart_set_rs485_rx_during_tx(port, &rs485);

+ /* Leave RS485 before RTS reverts to modem control below */
+ if (!(rs485.flags & SER_RS485_ENABLED))
+ uart_set_rs485_select_gpio(port, &rs485);
+
scoped_guard(uart_port_lock_irqsave, port) {
ret = port->rs485_config(port, &tty->termios, &rs485);
if (!ret) {
@@ -1519,9 +1535,14 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
!!(port->rs485.flags & SER_RS485_TERMINATE_BUS));
gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
!!(port->rs485.flags & SER_RS485_RX_DURING_TX));
+ uart_set_rs485_select_gpio(port, &port->rs485);
return ret;
}

+ /* Enter RS485 only once the driver has parked RTS */
+ if (rs485.flags & SER_RS485_ENABLED)
+ uart_set_rs485_select_gpio(port, &rs485);
+
if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
return -EFAULT;

@@ -3663,6 +3684,28 @@ int uart_get_rs485_mode(struct uart_port *port)
if (port->rs485_rx_during_tx_gpio)
port->rs485_supported.flags |= SER_RS485_RX_DURING_TX;

+ /*
+ * Keep the strap level until the port has been configured. A line
+ * that cannot be read back, as on output-only GPIO controllers,
+ * starts on the alternative interface.
+ */
+ desc = devm_gpiod_get_optional(dev, "rs485-select", GPIOD_ASIS);
+ if (IS_ERR(desc)) {
+ uart_forget_rs485_gpios(port);
+ return dev_err_probe(dev, PTR_ERR(desc),
+ "Cannot get rs485-select-gpios\n");
+ }
+ if (desc) {
+ ret = gpiod_get_value_cansleep(desc);
+ ret = gpiod_direction_output(desc, ret > 0);
+ if (ret) {
+ uart_forget_rs485_gpios(port);
+ return dev_err_probe(dev, ret,
+ "Cannot drive rs485-select-gpios\n");
+ }
+ }
+ port->rs485_select_gpio = desc;
+
return 0;
}
EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 4eeb8253752aa08b27829e80a259c7db65728e8e..fe90a8aada98cacf5a80f20ec2a91e249c92d0ed 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -600,6 +600,7 @@ struct uart_port {
struct serial_rs485 rs485_supported; /* Supported mask for serial_rs485 */
struct gpio_desc *rs485_term_gpio; /* enable RS485 bus termination */
struct gpio_desc *rs485_rx_during_tx_gpio; /* Output GPIO that sets the state of RS485 RX during TX */
+ struct gpio_desc *rs485_select_gpio; /* select the RS485 interface */
struct serial_iso7816 iso7816;
void *private_data; /* generic platform data pointer */
};

--
2.47.3