mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jirislaby@kernel.org>
Cc: devicetree@vger.kernel.org,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Lukas Wunner <lukas@wunner.de>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org
Subject: [PATCH 2/2] serial: core: implement support for rs485-mux-gpios
Date: Mon, 20 Nov 2023 16:10:55 +0100	[thread overview]
Message-ID: <20231120151056.148450-3-linux@rasmusvillemoes.dk> (raw)
In-Reply-To: <20231120151056.148450-1-linux@rasmusvillemoes.dk>

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

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 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


  parent reply	other threads:[~2023-11-20 15:11 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 15:10 [PATCH 0/2] serial: add rs485-mux-gpio dt binding and support Rasmus Villemoes
2023-11-20 15:10 ` [PATCH 1/2] dt-bindings: serial: rs485: add rs485-mux-gpios binding Rasmus Villemoes
2023-11-21  7:52   ` Krzysztof Kozlowski
2023-11-21  8:27     ` Rasmus Villemoes
2023-11-21  8:34       ` Krzysztof Kozlowski
2023-11-21  9:28         ` Rasmus Villemoes
2023-11-22 18:00           ` Krzysztof Kozlowski
2023-11-22 14:53   ` Lukas Wunner
2023-11-23 10:07     ` Rasmus Villemoes
2023-11-23 10:38       ` Lukas Wunner
2023-11-23 13:48         ` Rasmus Villemoes
2023-11-25 23:40     ` Lino Sanfilippo
2023-11-27 12:14       ` Christoph Niedermaier
2023-12-06 15:42         ` Lino Sanfilippo
2023-12-07 12:35           ` Andy Shevchenko
2023-12-09 11:24             ` Lino Sanfilippo
2023-12-09 11:47           ` Lino Sanfilippo
2023-12-11 13:07             ` Andy Shevchenko
2023-12-14  8:52               ` Lino Sanfilippo
2023-12-14 10:24               ` Crescent CY Hsieh
2023-12-14 13:41                 ` Christoph Niedermaier
2023-12-14 14:04                   ` Lino Sanfilippo
2023-12-14 14:50                     ` Christoph Niedermaier
2023-12-15 22:13                       ` Lino Sanfilippo
2023-12-18  9:08                         ` Christoph Niedermaier
2023-12-21 15:53                   ` Lukas Wunner
2023-12-23 12:49                     ` Christoph Niedermaier
2023-12-23 13:40                       ` Lino Sanfilippo
2023-12-24 10:11                         ` Christoph Niedermaier
2023-12-28 23:25                         ` Lukas Wunner
2023-11-20 15:10 ` Rasmus Villemoes [this message]
2023-11-20 23:28   ` [PATCH 2/2] serial: core: implement support for rs485-mux-gpios Lino Sanfilippo
2023-11-21 10:49   ` Dan Carpenter
2023-11-22 15:10   ` Lukas Wunner
2023-12-04  5:00   ` Dan Carpenter
2023-11-22 14:57 ` [PATCH 0/2] serial: add rs485-mux-gpio dt binding and support Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231120151056.148450-3-linux@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=lukas@wunner.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®