mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <ahmad@a3f.at>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <ahmad@a3f.at>
Subject: [PATCH] sandbox: add Xterm escape sequence backed LED driver
Date: Tue,  9 Mar 2021 09:01:31 +0100	[thread overview]
Message-ID: <20210309080131.902862-1-ahmad@a3f.at> (raw)

There is value in testing LED trigger conditions in sandbox,
especially with the coming bthread work that should replace pollers,
like the one used by the heartbeat trigger.

To make this testable on sandbox as well, add a simple LED driver.
It supports 5 brightness levels, which are reflected as dots in
the terminal title. This requires a Xterm compatible terminal,
but the LED is inactive by default, so other terminals aren't
affected if they lack support.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 arch/sandbox/board/Makefile  |  1 +
 arch/sandbox/board/led.c     | 67 ++++++++++++++++++++++++++++++++++++
 arch/sandbox/dts/sandbox.dts |  4 +++
 3 files changed, 72 insertions(+)
 create mode 100644 arch/sandbox/board/led.c

diff --git a/arch/sandbox/board/Makefile b/arch/sandbox/board/Makefile
index c504c967decd..ffb1dbc21ebf 100644
--- a/arch/sandbox/board/Makefile
+++ b/arch/sandbox/board/Makefile
@@ -7,5 +7,6 @@ obj-y += dtb.o
 obj-y += power.o
 obj-y += dev-random.o
 obj-y += watchdog.o
+obj-$(CONFIG_LED) += led.o
 
 extra-y += barebox.lds
diff --git a/arch/sandbox/board/led.c b/arch/sandbox/board/led.c
new file mode 100644
index 000000000000..b7ab81112baf
--- /dev/null
+++ b/arch/sandbox/board/led.c
@@ -0,0 +1,67 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <common.h>
+#include <init.h>
+#include <led.h>
+#include <mach/linux.h>
+#include <of.h>
+
+static struct sandbox_led {
+	struct led led;
+	bool active;
+} sandbox_led;
+
+static inline void terminal_puts(const char *s)
+{
+	linux_write(1, s, strlen(s));
+}
+
+static void sandbox_led_set(struct led *led, unsigned int brightness)
+{
+	terminal_puts("\x1b]2;barebox ");
+	while (brightness--)
+		terminal_puts(".");
+	terminal_puts("\a");
+
+	sandbox_led.active = true;
+}
+
+static int sandbox_led_of_probe(struct device_d *dev)
+{
+	struct device_node *np = dev->device_node;
+	int ret;
+
+	if (sandbox_led.led.set)
+		return -EBUSY;
+
+	sandbox_led.led.name = xstrdup(np->name);
+	sandbox_led.led.max_value = 5;
+	sandbox_led.led.set = sandbox_led_set;
+
+	ret = led_register(&sandbox_led.led);
+	if (ret)
+		return ret;
+
+	led_of_parse_trigger(&sandbox_led.led, np);
+
+	return 0;
+}
+
+static void sandbox_led_of_remove(struct device_d *dev)
+{
+	if (sandbox_led.active)
+		sandbox_led_set(NULL, 0);
+}
+
+static struct of_device_id sandbox_led_of_ids[] = {
+	{ .compatible = "barebox,sandbox-led", },
+	{ }
+};
+
+static struct driver_d sandbox_led_of_driver = {
+	.name  = "sandbox-led",
+	.probe = sandbox_led_of_probe,
+	.remove = sandbox_led_of_remove,
+	.of_compatible = sandbox_led_of_ids,
+};
+device_platform_driver(sandbox_led_of_driver);
diff --git a/arch/sandbox/dts/sandbox.dts b/arch/sandbox/dts/sandbox.dts
index ef1fa7b8661f..e99986bb9062 100644
--- a/arch/sandbox/dts/sandbox.dts
+++ b/arch/sandbox/dts/sandbox.dts
@@ -98,4 +98,8 @@ barebox,sandbox-watchdog
 	sound {
 		compatible = "barebox,sandbox-sound";
 	};
+
+	led {
+		compatible = "barebox,sandbox-led";
+	};
 };
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


             reply	other threads:[~2021-03-09  8:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-09  8:01 Ahmad Fatoum [this message]
2021-03-15  9:27 ` Sascha Hauer

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=20210309080131.902862-1-ahmad@a3f.at \
    --to=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    /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