[PATCH 2/2] serial: core: implement support for rs485-mux-gpios

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add code for handling a rs485-mux-gpio specified in device tree.

Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
---
 drivers/tty/serial/serial_core.c | 35 ++++++++++++++++++++++++++++++--
 include/linux/serial_core.h      |  1 +
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index f1348a509552..410b17ea7444 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -1402,14 +1402,20 @@ static void uart_set_rs485_termination(struct uart_port *port,
 				 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
 }
 
+static void uart_set_rs485_mux(struct uart_port *port, const struct serial_rs485 *rs485)
+{
+	gpiod_set_value_cansleep(port->rs485_mux_gpio,
+				 !!(rs485->flags & SER_RS485_ENABLED));
+}
+
 static int uart_rs485_config(struct uart_port *port)
 {
 	struct serial_rs485 *rs485 = &port->rs485;
 	unsigned long flags;
-	int ret;
+	int ret = 0;
 
 	if (!(rs485->flags & SER_RS485_ENABLED))
-		return 0;
+		goto out;
 
 	uart_sanitize_serial_rs485(port, rs485);
 	uart_set_rs485_termination(port, rs485);
@@ -1420,6 +1426,9 @@ static int uart_rs485_config(struct uart_port *port)
 	if (ret)
 		memset(rs485, 0, sizeof(*rs485));
 
+out:
+	uart_set_rs485_mux(port, rs485);
+
 	return ret;
 }
 
@@ -1457,6 +1466,14 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
 		return ret;
 	uart_sanitize_serial_rs485(port, &rs485);
 	uart_set_rs485_termination(port, &rs485);
+	/*
+	 * To avoid glitches on the transmit enable pin, the mux must
+	 * be set before calling the driver's ->rs485_config when
+	 * disabling rs485 mode, but after when enabling rs485
+	 * mode.
+	 */
+	if (!(rs485.flags & SER_RS485_ENABLED))
+		uart_set_rs485_mux(port, &rs485);
 
 	uart_port_lock_irqsave(port, &flags);
 	ret = port->rs485_config(port, &tty->termios, &rs485);
@@ -1468,6 +1485,13 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
 			port->ops->set_mctrl(port, port->mctrl);
 	}
 	uart_port_unlock_irqrestore(port, flags);
+
+	/*
+	 * The ->rs485_config might have failed. Regardless, set the
+	 * mux according to the port's effective rs485 config.
+	 */
+	uart_set_rs485_mux(port, &port->rs485);
+
 	if (ret)
 		return ret;
 
@@ -3621,6 +3645,13 @@ int uart_get_rs485_mode(struct uart_port *port)
 		return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n");
 	port->rs485_rx_during_tx_gpio = desc;
 
+	dflags = (rs485conf->flags & SER_RS485_ENABLED) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
+	desc = devm_gpiod_get_optional(dev, "rs485-mux", dflags);
+	if (IS_ERR(desc))
+		return dev_err_probe(dev, PTR_ERR(port->rs485_mux_gpio),
+				     "Cannot get rs485-mux-gpios\n");
+	port->rs485_mux_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 89f7b6c63598..943818209c49 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -584,6 +584,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_mux_gpio;	/* gpio for selecting RS485 mode */
 	struct serial_iso7816   iso7816;
 	void			*private_data;		/* generic platform data pointer */
 };
-- 
2.40.1.1.g1c60b9335d





[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]


  Powered by Linux