[PATCH v4 4/6] serial: 8250: set rx-enable gpio state when rs485 is disabled

Farouk Bouabid
2024-01-26T14:58:57Z

Add the possibility to de-activate rx-enable gpio when rs485 is disabled.
This defines the state of RE or DE/RE signal when em485 is disabled.

Signed-off-by: Farouk Bouabid <farouk.bouabid@theobroma-systems.com>
---
drivers/tty/serial/8250/8250_port.c | 4 ++++
drivers/tty/serial/serial_core.c | 6 ++++++
include/linux/serial_core.h | 1 +
3 files changed, 11 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
index 54d8f809b81e..fbd0212d2397 100644
--- a/drivers/tty/serial/8250/8250_port.c
+++ b/drivers/tty/serial/8250/8250_port.c
@@ -582,12 +582,16 @@ static int serial8250_em485_init(struct uart_8250_port *p)
*/
void serial8250_em485_destroy(struct uart_8250_port *p)
{
+ struct uart_port *port = &p->port;
+
if (!p->em485)
return;

hrtimer_cancel(&p->em485->start_tx_timer);
hrtimer_cancel(&p->em485->stop_tx_timer);

+ gpiod_set_value(port->rs485_re_gpio, !port->rs485_re_gpio_inactive_when_rs485_disabled);
+
kfree(p->em485);
p->em485 = NULL;
}
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 47aeece985f3..6460646cf83c 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -3663,6 +3663,12 @@ 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;

+ port->rs485_re_gpio_inactive_when_rs485_disabled = device_property_read_bool(dev,
+ "rs485-rx-enable-inactive-when-rs485-disabled");
+
+ port->rs485_re_gpio = devm_gpiod_get_optional(dev, "rs485-rx-enable",
+ port->rs485_re_gpio_inactive_when_rs485_disabled ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH);
+
if (IS_ERR(port->rs485_re_gpio)) {
ret = PTR_ERR(port->rs485_re_gpio);
port->rs485_re_gpio = NULL;
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 364583203a24..fa5a92b56360 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -585,6 +585,7 @@ struct uart_port {
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_re_gpio; /* gpio RS485 receive enable */
+ bool rs485_re_gpio_inactive_when_rs485_disabled;
struct serial_iso7816 iso7816;
void *private_data; /* generic platform data pointer */
};