Skip to contentSkip to conversation

serial: imx: add rx and tx led trigger

PATCH New Uwe Kleine-König 3 participants 11 messages Jul 4, 2016 – Aug 31, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

---
 drivers/tty/serial/imx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
drivers/tty/serial/imx.c +57 -0
index 621e488cbb12..2b6ba3b8bdd5 100644
@@ -39,6 +39,7 @@
 #include <linux/of_device.h>
 #include <linux/io.h>
 #include <linux/dma-mapping.h>
+#include <linux/leds.h>
 
 #include <asm/irq.h>
 #include <linux/platform_data/serial-imx.h>
@@ -227,6 +228,8 @@ struct imx_port {
 	wait_queue_head_t	dma_wait;
 	unsigned int            saved_reg[10];
 	bool			context_saved;
+	struct led_trigger	led_trigger_rx;
+	struct led_trigger	led_trigger_tx;
 };
 
 struct imx_port_ucrs {
@@ -293,6 +296,10 @@ static inline int is_imx6q_uart(struct imx_port *sport)
 {
 	return sport->devdata->devtype == IMX6Q_UART;
 }
+
+static unsigned long led_delay = 50;
+module_param(led_delay, ulong, 0644);
+
 /*
  * Save and restore functions for UCR1, UCR2 and UCR3 registers
  */
@@ -426,6 +433,9 @@ static inline void imx_transmit_buffer(struct imx_port *sport)
 		return;
 	}
 
+	led_trigger_blink_oneshot(&sport->led_trigger_tx,
+				  &led_delay, &led_delay, 1);
+
 	if (sport->dma_is_enabled) {
 		/*
 		 * We've just sent a X-char Ensure the TX DMA is enabled
@@ -635,6 +645,9 @@ static irqreturn_t imx_rxint(int irq, void *dev_id)
 	struct tty_port *port = &sport->port.state->port;
 	unsigned long flags, temp;
 
+	led_trigger_blink_oneshot(&sport->led_trigger_rx,
+				  &led_delay, &led_delay, 1);
+
 	spin_lock_irqsave(&sport->port.lock, flags);
 
 	while (readl(sport->port.membase + USR2) & USR2_RDR) {
@@ -708,6 +721,9 @@ static void imx_dma_rxint(struct imx_port *sport)
 	unsigned long temp;
 	unsigned long flags;
 
+	led_trigger_blink_oneshot(&sport->led_trigger_rx,
+				  &led_delay, &led_delay, 1);
+
 	spin_lock_irqsave(&sport->port.lock, flags);
 
 	temp = readl(sport->port.membase + USR2);
@@ -2002,6 +2018,42 @@ static void serial_imx_probe_pdata(struct imx_port *sport,
 		sport->have_rtscts = 1;
 }
 
+static void imx_register_led_trigger(struct device *dev, struct imx_port *sport)
+{
+#ifdef CONFIG_LEDS_TRIGGERS
+	int ret;
+	char *name;
+
+	name = devm_kasprintf(dev, GFP_KERNEL, "ttymxc%d-rx|ttymxc%d-tx",
+			      sport->port.line, sport->port.line);
+	if (!name) {
+		dev_warn(dev, "failed to allocate led trigger name\n");
+		return;
+	}
+
+	sport->led_trigger_rx.name = name;
+
+	name = strchr(name, '|');
+	*name = '\0';
+
+	sport->led_trigger_tx.name = name + 1;
+
+	ret = led_trigger_register(&sport->led_trigger_rx);
+	if (ret) {
+		dev_warn(dev, "failed to register rx led trigger\n");
+		goto err_register_rx;
+	}
+
+	ret = led_trigger_register(&sport->led_trigger_tx);
+	if (ret) {
+		dev_warn(dev, "failed to register tx led trigger\n");
+		led_trigger_unregister(&sport->led_trigger_rx);
+err_register_rx:
+		devm_kfree(dev, name);
+	}
+#endif
+}
+
 static int serial_imx_probe(struct platform_device *pdev)
 {
 	struct imx_port *sport;
@@ -2065,6 +2117,8 @@ static int serial_imx_probe(struct platform_device *pdev)
 
 	sport->port.uartclk = clk_get_rate(sport->clk_per);
 
+	imx_register_led_trigger(&pdev->dev, sport);
+
 	/* For register access, we only need to enable the ipg clock. */
 	ret = clk_prepare_enable(sport->clk_ipg);
 	if (ret)
@@ -2110,6 +2164,9 @@ static int serial_imx_remove(struct platform_device *pdev)
 {
 	struct imx_port *sport = platform_get_drvdata(pdev);
 
+#ifdef CONFIG_LEDS_TRIGGERS
+	led_trigger_unregister(&sport->led_trigger_rx);
+#endif
 	return uart_remove_one_port(&imx_reg, &sport->port);
 }
 
Signature · 1 line
2.8.1
#1 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Quoted 7 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

---
 drivers/tty/serial/imx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

What is specific to this driver here? Could this be done in the core tty code instead?

Arnd

#2 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Quoted 11 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

---
 drivers/tty/serial/imx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

What is specific to this driver here? Could this be done in the core tty code instead?

The core doesn't notice when the driver starts to push out characters.

I also have a patch in my queue that adds support for rts delaying in rs485 mode. While the delay between start_tx being called and the first char leaving the hardware might not be big enough to care in rs232 mode, with a big delay_rts_before_send it might matter.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#3 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Quoted 13 lines · CEST Uwe Kleine-K?nig
Quoted 11 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

---
 drivers/tty/serial/imx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

What is specific to this driver here? Could this be done in the core tty code instead?

Quoted 6 lines

The core doesn't notice when the driver starts to push out characters.

I also have a patch in my queue that adds support for rts delaying in rs485 mode. While the delay between start_tx being called and the first char leaving the hardware might not be big enough to care in rs232 mode, with a big delay_rts_before_send it might matter.

Right, that makes sense. It still seems odd to have this just in one driver, and your changelog above doesn't really explain what the blinkenlights are actually good for.

I'm sure you had something useful in mind, can you elaborate?

If this is something we may want to do on other platforms as well, we should perhaps not hardwire the name of the imx tty device in the led trigger name.

Arnd

#4 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Quoted 21 lines · Arnd Bergmann
Quoted 13 lines · CEST Uwe Kleine-K?nig
Quoted 11 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

---
 drivers/tty/serial/imx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

What is specific to this driver here? Could this be done in the core tty code instead?

Quoted 6 lines

The core doesn't notice when the driver starts to push out characters.

I also have a patch in my queue that adds support for rts delaying in rs485 mode. While the delay between start_tx being called and the first char leaving the hardware might not be big enough to care in rs232 mode, with a big delay_rts_before_send it might matter.

Quoted 5 lines

Right, that makes sense. It still seems odd to have this just in one driver, and your changelog above doesn't really explain what the blinkenlights are actually good for.

I'm sure you had something useful in mind, can you elaborate?

I don't know the exact motivation. $customer wants to see when there is traffic on the serial line. Is there a better motivation needed? If so, what was the motivation for the mmc led trigger (which btw also used the host's device name as trigger name).

Quoted 3 lines

If this is something we may want to do on other platforms as well, we should perhaps not hardwire the name of the imx tty device in the led trigger name.

I cannot follow. If we have several serial lines and a trigger for each of them, they must get different names. Using the device's name to distinguish them seems like a good and obvious idea.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#5 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Quoted 22 lines · CEST Uwe Kleine-K?nig
Quoted 21 lines · Arnd Bergmann
Quoted 19 lines · CEST Uwe Kleine-K?nig
Quoted 11 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

---
 drivers/tty/serial/imx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

What is specific to this driver here? Could this be done in the core tty code instead?

The core doesn't notice when the driver starts to push out characters.

I also have a patch in my queue that adds support for rts delaying in rs485 mode. While the delay between start_tx being called and the first char leaving the hardware might not be big enough to care in rs232 mode, with a big delay_rts_before_send it might matter.

Quoted 8 lines
Quoted 5 lines

Right, that makes sense. It still seems odd to have this just in one driver, and your changelog above doesn't really explain what the blinkenlights are actually good for.

I'm sure you had something useful in mind, can you elaborate?

I don't know the exact motivation. $customer wants to see when there is traffic on the serial line. Is there a better motivation needed?

I don't know, it depends a bit on how smart you think $customer is ;-)

Some customers know exactly what they need and why they ask for it, some others are a bit confused about their own requirements.

I can certainly think of cases where this makes sense, e.g. a modem appliance or a terminal server.

Quoted 2 lines

If so, what was the motivation for the mmc led trigger (which btw also used the host's device name as trigger name).

I didn't see that patch.

Quoted 7 lines
Quoted 3 lines

If this is something we may want to do on other platforms as well, we should perhaps not hardwire the name of the imx tty device in the led trigger name.

I cannot follow. If we have several serial lines and a trigger for each of them, they must get different names. Using the device's name to distinguish them seems like a good and obvious idea.

The main problem I see is if someone puts the name of the trigger into a dtb file, as this hardcodes the connection between the Linux driver name and numbering system with the device tree binding, which are normally separate.

If we could derive the trigger name from the "/aliases/serial%d" property in DT instead, it would get a little more portable.

Arnd

#6 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Hello Arnd,

[adding Rob Herring to Cc]

Quoted 16 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig
Quoted 6 lines · Arnd Bergmann
Quoted 5 lines · CEST Uwe Kleine-K?nig
Quoted 4 lines · Arnd Bergmann
Quoted 3 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

Quoted 7 lines
Quoted 3 lines

If this is something we may want to do on other platforms as well, we should perhaps not hardwire the name of the imx tty device in the led trigger name.

I cannot follow. If we have several serial lines and a trigger for each of them, they must get different names. Using the device's name to distinguish them seems like a good and obvious idea.

Quoted 7 lines

The main problem I see is if someone puts the name of the trigger into a dtb file, as this hardcodes the connection between the Linux driver name and numbering system with the device tree binding, which are normally separate.

If we could derive the trigger name from the "/aliases/serial%d" property in DT instead, it would get a little more portable.

Alternatively we could invent a more dtish way as aliases seem to be frowned upon [1], something like:

	led#0 {
		label = "userled";
		linux,default-trigger = &uart1, "tx";
	};

	uart1: serial at 43f90000 {
		...
		#trigger-cells = <1>;
	};

Having said that, I don't think it's a big problem if the value of "linux,default-trigger" is Linux-specific. Moreover, is a default trigger considered a hardware description?

Best regards
Uwe

[1] http://mid.gmane.org/20160705140546.GA10601 at rob-hp-laptop Not sure this is a general objection to aliases, though.

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#7 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Quoted 32 lines · CEST Uwe Kleine-K?nig

Hello Arnd,

[adding Rob Herring to Cc]

Quoted 16 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig
Quoted 6 lines · Arnd Bergmann
Quoted 5 lines · CEST Uwe Kleine-K?nig
Quoted 4 lines · Arnd Bergmann
Quoted 3 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

Quoted 7 lines
Quoted 3 lines

If this is something we may want to do on other platforms as well, we should perhaps not hardwire the name of the imx tty device in the led trigger name.

I cannot follow. If we have several serial lines and a trigger for each of them, they must get different names. Using the device's name to distinguish them seems like a good and obvious idea.

Quoted 7 lines

The main problem I see is if someone puts the name of the trigger into a dtb file, as this hardcodes the connection between the Linux driver name and numbering system with the device tree binding, which are normally separate.

If we could derive the trigger name from the "/aliases/serial%d" property in DT instead, it would get a little more portable.

Alternatively we could invent a more dtish way as aliases seem to be frowned upon [1], something like:

Quoted 9 lines
	led#0 {
		label = "userled";
		linux,default-trigger = &uart1, "tx";
	};

	uart1: serial at 43f90000 {
		...
		#trigger-cells = <1>;
	};

That looks nice, but I don't see how we could implement this in a backwards compatible way, as we don't know whether the first cell of the property is a phandle or a string.

Quoted 3 lines

Having said that, I don't think it's a big problem if the value of "linux,default-trigger" is Linux-specific. Moreover, is a default trigger considered a hardware description?

What's more important here I think is that even in Linux, the name of the uart is not always stable across versions or configurations, e.g. in on OMAP we can have either ttyO%d or ttyS%d.

Arnd

#8 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Hello,

Quoted 34 lines · Arnd Bergmann
Quoted 33 lines · CEST Uwe Kleine-K?nig
Quoted 16 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig
Quoted 6 lines · Arnd Bergmann
Quoted 5 lines · CEST Uwe Kleine-K?nig
Quoted 4 lines · Arnd Bergmann
Quoted 3 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

Quoted 7 lines
Quoted 3 lines

If this is something we may want to do on other platforms as well, we should perhaps not hardwire the name of the imx tty device in the led trigger name.

I cannot follow. If we have several serial lines and a trigger for each of them, they must get different names. Using the device's name to distinguish them seems like a good and obvious idea.

Quoted 7 lines

The main problem I see is if someone puts the name of the trigger into a dtb file, as this hardcodes the connection between the Linux driver name and numbering system with the device tree binding, which are normally separate.

If we could derive the trigger name from the "/aliases/serial%d" property in DT instead, it would get a little more portable.

Alternatively we could invent a more dtish way as aliases seem to be frowned upon [1], something like:

	led#0 {
		label = "userled";
		linux,default-trigger = &uart1, "tx";
	};
Quoted 8 lines
Quoted 4 lines
	uart1: serial at 43f90000 {
		...
		#trigger-cells = <1>;
	};

That looks nice, but I don't see how we could implement this in a backwards compatible way, as we don't know whether the first cell of the property is a phandle or a string.

If we agree, that this is OS-agnostic, we could use "default-trigger" as property name instead of "linux,default-trigger".

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-K?nig            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#9 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

Quoted 40 lines · CEST Uwe Kleine-K?nig
Quoted 39 lines · Arnd Bergmann
Quoted 37 lines · CEST Uwe Kleine-K?nig
Quoted 16 lines · Arnd Bergmann
Quoted 7 lines · CEST Uwe Kleine-K?nig
Quoted 6 lines · Arnd Bergmann
Quoted 5 lines · CEST Uwe Kleine-K?nig
Quoted 4 lines · Arnd Bergmann
Quoted 3 lines · CEST Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

Quoted 7 lines
Quoted 3 lines

If this is something we may want to do on other platforms as well, we should perhaps not hardwire the name of the imx tty device in the led trigger name.

I cannot follow. If we have several serial lines and a trigger for each of them, they must get different names. Using the device's name to distinguish them seems like a good and obvious idea.

Quoted 7 lines

The main problem I see is if someone puts the name of the trigger into a dtb file, as this hardcodes the connection between the Linux driver name and numbering system with the device tree binding, which are normally separate.

If we could derive the trigger name from the "/aliases/serial%d" property in DT instead, it would get a little more portable.

Alternatively we could invent a more dtish way as aliases seem to be frowned upon [1], something like:

    led#0 {
            label = "userled";
            linux,default-trigger = &uart1, "tx";
    };

    uart1: serial at 43f90000 {
            ...
            #trigger-cells = <1>;
    };
Quoted 6 lines
Quoted 3 lines

That looks nice, but I don't see how we could implement this in a backwards compatible way, as we don't know whether the first cell of the property is a phandle or a string.

If we agree, that this is OS-agnostic, we could use "default-trigger" as property name instead of "linux,default-trigger".

Yes, I think that can work, but why not just use linux,default-trigger="serial%d-tx" where serial%d is the alias we already have?

Arnd

#10 lore raw

PATCH 1/1 serial: imx: add rx and tx led trigger

In reply to Uwe Kleine-König
Quoted 30 lines · Uwe Kleine-K?nig

Add support for two led triggers per UART instance that blink on transmission and reception of data respectively.

---
 drivers/tty/serial/imx.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
drivers/tty/serial/imx.c +3 -0
index 621e488cbb12..2b6ba3b8bdd5 100644
@@ -39,6 +39,7 @@
 #include <linux/of_device.h>
 #include <linux/io.h>
 #include <linux/dma-mapping.h>
+#include <linux/leds.h>
 
 #include <asm/irq.h>
 #include <linux/platform_data/serial-imx.h>
@@ -227,6 +228,8 @@ struct imx_port {
 	wait_queue_head_t	dma_wait;
 	unsigned int            saved_reg[10];
 	bool			context_saved;
+	struct led_trigger	led_trigger_rx;
+	struct led_trigger	led_trigger_tx;
 };
 
 struct imx_port_ucrs {

drivers/tty/serial/imx.c static inline int is_imx6q_uart(struct imx_port *sport) · line 301

Quoted 6 lines
drivers/tty/serial/imx.c +3 -0
@@ -293,6 +296,10 @@ static inline int is_imx6q_uart(struct imx_port *sport)
 {
 	return sport->devdata->devtype == IMX6Q_UART;
 }
+
+static unsigned long led_delay = 50;
+module_param(led_delay, ulong, 0644);

I hate module parameters, and so should you, this isn't the 1990's anymore :(

And I'm with Arnd, let's make this work for all tty drivers that want to use it please.

thanks,

greg k-h