linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] rs485 bus termination GPIO
@ 2020-05-18 14:45 Lukas Wunner
  2020-05-18 14:45 ` [PATCH v3 1/2] dt-bindings: serial: Add binding for " Lukas Wunner
  2020-05-18 14:45 ` [PATCH v3 2/2] serial: 8250: Support " Lukas Wunner
  0 siblings, 2 replies; 4+ messages in thread
From: Lukas Wunner @ 2020-05-18 14:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Rob Herring
  Cc: Matwey V. Kornilov, Giulio Benetti, Heiko Stuebner,
	Andy Shevchenko, Christoph Muellner, Jan Kiszka, linux-serial,
	devicetree

Define a device tree binding for an rs485 bus termination GPIO
(patch [1/2]), amend the serial core to retrieve the GPIO from
the device tree and amend the default ->rs485_config() callback
for 8250 drivers to change the GPIO on request from user space
(patch [2/2]).


Changes v2 -> v3:

* Patch [2/2]:
  * Drop unnecessary NULL pointer check preceding gpiod_set_value(). (Heiko)
  * Amend commit message to suggest moving 8250_exar.c to new binding. (Andy)
    Add Jan to Cc and Andy's R-b tag.

Changes v1 -> v2:

* Patch [2/2]:
  * Drop unnecessary devm_gpiod_put(). (Andy)
  * Use GPIOD_OUT_LOW macro for brevity. (Andy)
  * Document the rationale for disabling termination by default.
  * Drop nonsensical read of GPIO after setting its direction to out.


Link to v2:
https://lore.kernel.org/linux-serial/cover.1589285873.git.lukas@wunner.de


Lukas Wunner (2):
  dt-bindings: serial: Add binding for rs485 bus termination GPIO
  serial: 8250: Support rs485 bus termination GPIO

 .../devicetree/bindings/serial/rs485.yaml        |  4 ++++
 drivers/tty/serial/8250/8250_port.c              |  3 +++
 drivers/tty/serial/serial_core.c                 | 16 ++++++++++++++++
 include/linux/serial_core.h                      |  2 ++
 4 files changed, 25 insertions(+)

-- 
2.26.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/2] dt-bindings: serial: Add binding for rs485 bus termination GPIO
  2020-05-18 14:45 [PATCH v3 0/2] rs485 bus termination GPIO Lukas Wunner
@ 2020-05-18 14:45 ` Lukas Wunner
  2020-05-28 20:50   ` Rob Herring
  2020-05-18 14:45 ` [PATCH v3 2/2] serial: 8250: Support " Lukas Wunner
  1 sibling, 1 reply; 4+ messages in thread
From: Lukas Wunner @ 2020-05-18 14:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Rob Herring
  Cc: Matwey V. Kornilov, Giulio Benetti, Heiko Stuebner,
	Andy Shevchenko, Christoph Muellner, Jan Kiszka, linux-serial,
	devicetree

Commit e8759ad17d41 ("serial: uapi: Add support for bus termination")
introduced the ability to enable rs485 bus termination from user space.
So far the feature is only used by a single driver, 8250_exar.c, using a
hardcoded GPIO pin specific to Siemens IOT2040 products.

Provide for a more generic solution by allowing specification of an
rs485 bus termination GPIO pin in the device tree.  An upcoming commit
implements support for this pin for any 8250 driver.  The binding is
used in device trees of the "Revolution Pi" PLCs offered by KUNBUS.

[Heiko Stuebner converted the binding to YAML, hence his Signed-off-by.]

Signed-off-by: Heiko Stuebner <heiko.stuebner@theobroma-systems.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Cc: Jan Kiszka <jan.kiszka@siemens.com>
---
 Documentation/devicetree/bindings/serial/rs485.yaml | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/Documentation/devicetree/bindings/serial/rs485.yaml b/Documentation/devicetree/bindings/serial/rs485.yaml
index d4beaf11222d..a9ad17864889 100644
--- a/Documentation/devicetree/bindings/serial/rs485.yaml
+++ b/Documentation/devicetree/bindings/serial/rs485.yaml
@@ -43,3 +43,7 @@ properties:
   rs485-rx-during-tx:
    description: enables the receiving of data even while sending data.
    $ref: /schemas/types.yaml#/definitions/flag
+
+  rs485-term-gpios:
+    description: GPIO pin to enable RS485 bus termination.
+    maxItems: 1
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 2/2] serial: 8250: Support rs485 bus termination GPIO
  2020-05-18 14:45 [PATCH v3 0/2] rs485 bus termination GPIO Lukas Wunner
  2020-05-18 14:45 ` [PATCH v3 1/2] dt-bindings: serial: Add binding for " Lukas Wunner
@ 2020-05-18 14:45 ` Lukas Wunner
  1 sibling, 0 replies; 4+ messages in thread
From: Lukas Wunner @ 2020-05-18 14:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Rob Herring
  Cc: Matwey V. Kornilov, Giulio Benetti, Heiko Stuebner,
	Andy Shevchenko, Christoph Muellner, Jan Kiszka, linux-serial,
	devicetree

Commit e8759ad17d41 ("serial: uapi: Add support for bus termination")
introduced the ability to enable rs485 bus termination from user space.
So far the feature is only used by a single driver, 8250_exar.c, using a
hardcoded GPIO pin specific to Siemens IOT2040 products.

Provide for a more generic solution by allowing specification of an
rs485 bus termination GPIO pin in the device tree:  Amend the serial
core to retrieve the GPIO from the device tree (or ACPI table) and amend
the default ->rs485_config() callback for 8250 drivers to change the
GPIO on request from user space.

Perhaps 8250_exar.c can be converted to the generic approach in a
follow-up patch.

Signed-off-by: Lukas Wunner <lukas@wunner.de>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Jan Kiszka <jan.kiszka@siemens.com>
---
 drivers/tty/serial/8250/8250_port.c |  3 +++
 drivers/tty/serial/serial_core.c    | 16 ++++++++++++++++
 include/linux/serial_core.h         |  2 ++
 3 files changed, 21 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
index 4d83c85a7389..4f6ff54175ce 100644
--- a/drivers/tty/serial/8250/8250_port.c
+++ b/drivers/tty/serial/8250/8250_port.c
@@ -681,6 +681,9 @@ int serial8250_em485_config(struct uart_port *port, struct serial_rs485 *rs485)
 	memset(rs485->padding, 0, sizeof(rs485->padding));
 	port->rs485 = *rs485;
 
+	gpiod_set_value(port->rs485_term_gpio,
+			rs485->flags & SER_RS485_TERMINATE_BUS);
+
 	/*
 	 * Both serial8250_em485_init() and serial8250_em485_destroy()
 	 * are idempotent.
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 43b6682877d5..57840cf90388 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -3317,6 +3317,7 @@ int uart_get_rs485_mode(struct uart_port *port)
 	 * to get to a defined state with the following properties:
 	 */
 	rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
+			      SER_RS485_TERMINATE_BUS |
 			      SER_RS485_RTS_AFTER_SEND);
 	rs485conf->flags |= SER_RS485_RTS_ON_SEND;
 
@@ -3331,6 +3332,21 @@ int uart_get_rs485_mode(struct uart_port *port)
 		rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
 	}
 
+	/*
+	 * Disabling termination by default is the safe choice:  Else if many
+	 * bus participants enable it, no communication is possible at all.
+	 * Works fine for short cables and users may enable for longer cables.
+	 */
+	port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
+							GPIOD_OUT_LOW);
+	if (IS_ERR(port->rs485_term_gpio)) {
+		ret = PTR_ERR(port->rs485_term_gpio);
+		port->rs485_term_gpio = NULL;
+		if (ret != -EPROBE_DEFER)
+			dev_err(dev, "Cannot get rs485-term-gpios\n");
+		return ret;
+	}
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index b649a2b894e7..9fd550e7946a 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -10,6 +10,7 @@
 #include <linux/bitops.h>
 #include <linux/compiler.h>
 #include <linux/console.h>
+#include <linux/gpio/consumer.h>
 #include <linux/interrupt.h>
 #include <linux/circ_buf.h>
 #include <linux/spinlock.h>
@@ -251,6 +252,7 @@ struct uart_port {
 	struct attribute_group	*attr_group;		/* port specific attributes */
 	const struct attribute_group **tty_groups;	/* all attributes (serial core use only) */
 	struct serial_rs485     rs485;
+	struct gpio_desc	*rs485_term_gpio;	/* enable RS485 bus termination */
 	struct serial_iso7816   iso7816;
 	void			*private_data;		/* generic platform data pointer */
 };
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 1/2] dt-bindings: serial: Add binding for rs485 bus termination GPIO
  2020-05-18 14:45 ` [PATCH v3 1/2] dt-bindings: serial: Add binding for " Lukas Wunner
@ 2020-05-28 20:50   ` Rob Herring
  0 siblings, 0 replies; 4+ messages in thread
From: Rob Herring @ 2020-05-28 20:50 UTC (permalink / raw)
  To: Lukas Wunner
  Cc: devicetree, Jan Kiszka, Matwey V. Kornilov, Greg Kroah-Hartman,
	Andy Shevchenko, Christoph Muellner, Jiri Slaby, linux-serial,
	Heiko Stuebner, Giulio Benetti, Rob Herring

On Mon, 18 May 2020 16:45:01 +0200, Lukas Wunner wrote:
> Commit e8759ad17d41 ("serial: uapi: Add support for bus termination")
> introduced the ability to enable rs485 bus termination from user space.
> So far the feature is only used by a single driver, 8250_exar.c, using a
> hardcoded GPIO pin specific to Siemens IOT2040 products.
> 
> Provide for a more generic solution by allowing specification of an
> rs485 bus termination GPIO pin in the device tree.  An upcoming commit
> implements support for this pin for any 8250 driver.  The binding is
> used in device trees of the "Revolution Pi" PLCs offered by KUNBUS.
> 
> [Heiko Stuebner converted the binding to YAML, hence his Signed-off-by.]
> 
> Signed-off-by: Heiko Stuebner <heiko.stuebner@theobroma-systems.com>
> Signed-off-by: Lukas Wunner <lukas@wunner.de>
> Cc: Jan Kiszka <jan.kiszka@siemens.com>
> ---
>  Documentation/devicetree/bindings/serial/rs485.yaml | 4 ++++
>  1 file changed, 4 insertions(+)
> 

Reviewed-by: Rob Herring <robh@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-28 20:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-18 14:45 [PATCH v3 0/2] rs485 bus termination GPIO Lukas Wunner
2020-05-18 14:45 ` [PATCH v3 1/2] dt-bindings: serial: Add binding for " Lukas Wunner
2020-05-28 20:50   ` Rob Herring
2020-05-18 14:45 ` [PATCH v3 2/2] serial: 8250: Support " Lukas Wunner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).