diff mbox series

[3/7] cmd: button: add a new 'button' command

Message ID 1594644972-2588-3-git-send-email-philippe.reynes@softathome.com
State Accepted
Commit 325141a6eab0d21c928a4c36aa9b6873bb672dab
Headers show
Series [1/7] dm: button: add an uclass for button | expand

Commit Message

Philippe REYNES July 13, 2020, 12:56 p.m. UTC
Adds a command 'button' that provides the list of buttons
supported by the board, and the state of a button.

Signed-off-by: Philippe Reynes <philippe.reynes at softathome.com>
---
 cmd/Kconfig  | 11 ++++++++
 cmd/Makefile |  1 +
 cmd/button.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100644 cmd/button.c

Comments

Simon Glass July 15, 2020, 1:05 a.m. UTC | #1
On Mon, 13 Jul 2020 at 06:56, Philippe Reynes
<philippe.reynes at softathome.com> wrote:
>
> Adds a command 'button' that provides the list of buttons
> supported by the board, and the state of a button.
>
> Signed-off-by: Philippe Reynes <philippe.reynes at softathome.com>
> ---
>  cmd/Kconfig  | 11 ++++++++
>  cmd/Makefile |  1 +
>  cmd/button.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 98 insertions(+)
>  create mode 100644 cmd/button.c

Reviewed-by: Simon Glass <sjg at chromium.org>
diff mbox series

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 192b3b2..750d26a 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1664,6 +1664,17 @@  config CMD_BLOCK_CACHE
 	  during development, but also allows the cache to be disabled when
 	  it might hurt performance (e.g. when using the ums command).
 
+config CMD_BUTTON
+	bool "button"
+	depends on BUTTON
+	default y if BUTTON
+	help
+	  Enable the 'button' command which allows to get the status of
+	  buttons supported by the board. The buttonss can be listed with
+	  'button list' and state can be known with 'button <label>'.
+	  Any button drivers can be controlled with this command, e.g.
+	  button_gpio.
+
 config CMD_CACHE
 	bool "icache or dcache"
 	help
diff --git a/cmd/Makefile b/cmd/Makefile
index 974ad48..5ded3a6 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -32,6 +32,7 @@  obj-$(CONFIG_CMD_BOOTSTAGE) += bootstage.o
 obj-$(CONFIG_CMD_BOOTZ) += bootz.o
 obj-$(CONFIG_CMD_BOOTI) += booti.o
 obj-$(CONFIG_CMD_BTRFS) += btrfs.o
+obj-$(CONFIG_CMD_BUTTON) += button.o
 obj-$(CONFIG_CMD_CACHE) += cache.o
 obj-$(CONFIG_CMD_CBFS) += cbfs.o
 obj-$(CONFIG_CMD_CLK) += clk.o
diff --git a/cmd/button.c b/cmd/button.c
new file mode 100644
index 0000000..84ad165
--- /dev/null
+++ b/cmd/button.c
@@ -0,0 +1,86 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020 Philippe Reynes <philippe.reynes at softathome.com>
+ *
+ * Based on led.c
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <button.h>
+#include <dm/uclass-internal.h>
+
+static const char *const state_label[] = {
+	[BUTTON_OFF]	= "off",
+	[BUTTON_ON]	= "on",
+};
+
+static int show_button_state(struct udevice *dev)
+{
+	int ret;
+
+	ret = button_get_state(dev);
+	if (ret >= BUTTON_COUNT)
+		ret = -EINVAL;
+	if (ret >= 0)
+		printf("%s\n", state_label[ret]);
+
+	return ret;
+}
+
+static int list_buttons(void)
+{
+	struct udevice *dev;
+	int ret;
+
+	for (uclass_find_first_device(UCLASS_BUTTON, &dev);
+	     dev;
+	     uclass_find_next_device(&dev)) {
+		struct button_uc_plat *plat = dev_get_uclass_platdata(dev);
+
+		if (!plat->label)
+			continue;
+		printf("%-15s ", plat->label);
+		if (device_active(dev)) {
+			ret = show_button_state(dev);
+			if (ret < 0)
+				printf("Error %d\n", ret);
+		} else {
+			printf("<inactive>\n");
+		}
+	}
+
+	return 0;
+}
+
+int do_button(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	const char *button_label;
+	struct udevice *dev;
+	int ret;
+
+	/* Validate arguments */
+	if (argc < 2)
+		return CMD_RET_USAGE;
+	button_label = argv[1];
+	if (strncmp(button_label, "list", 4) == 0)
+		return list_buttons();
+
+	ret = button_get_by_label(button_label, &dev);
+	if (ret) {
+		printf("Button '%s' not found (err=%d)\n", button_label, ret);
+		return CMD_RET_FAILURE;
+	}
+
+	ret = show_button_state(dev);
+
+	return 0;
+}
+
+U_BOOT_CMD(
+	button, 4, 1, do_button,
+	"manage buttons",
+	"<button_label> \tGet button state\n"
+	"button list\t\tShow a list of buttons"
+);