U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v9 13/14] dm: Enable gpio command to support driver model
Date: Wed, 26 Feb 2014 15:59:26 -0700	[thread overview]
Message-ID: <1393455567-13378-14-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1393455567-13378-1-git-send-email-sjg@chromium.org>

Now that named GPIO banks are supported, along with a way of obtaining
the status of a GPIO (input or output), we can provide an enhanced
GPIO command for driver model. Where the driver provides its own operation
for obtaining the GPIO state, this is used, otherwise a generic version
is sufficient.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v9:
- Update comment about GPIO command conversion from numbers to uclass

Changes in v8: None
Changes in v7: None
Changes in v6: None
Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 common/cmd_gpio.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 116 insertions(+), 13 deletions(-)

diff --git a/common/cmd_gpio.c b/common/cmd_gpio.c
index 47eee89..778aa5f 100644
--- a/common/cmd_gpio.c
+++ b/common/cmd_gpio.c
@@ -8,7 +8,7 @@
 
 #include <common.h>
 #include <command.h>
-
+#include <dm.h>
 #include <asm/gpio.h>
 
 #ifndef name_to_gpio
@@ -22,25 +22,115 @@ enum gpio_cmd {
 	GPIO_TOGGLE,
 };
 
+#if defined(CONFIG_DM_GPIO) && !defined(gpio_status)
+static const char * const gpio_function[] = {
+	"input",
+	"output",
+	"unknown",
+};
+
+static void show_gpio(struct device *dev, const char *bank_name, int offset)
+{
+	struct dm_gpio_ops *ops = gpio_get_ops(dev);
+	char buf[80];
+	int ret;
+
+	*buf = '\0';
+	if (ops->get_state) {
+		ret = ops->get_state(dev, offset, buf, sizeof(buf));
+		if (ret) {
+			puts("<unknown>");
+			return;
+		}
+	} else {
+		int func =  GPIOF_UNKNOWN;
+		int ret;
+
+		if (ops->get_function) {
+			ret = ops->get_function(dev, offset);
+			if (ret >= 0 && ret < ARRAY_SIZE(gpio_function))
+				func = ret;
+		}
+		sprintf(buf, "%s%u: %8s %d", bank_name, offset,
+			gpio_function[func], ops->get_value(dev, offset));
+	}
+
+	puts(buf);
+	puts("\n");
+}
+
+static int do_gpio_status(const char *gpio_name)
+{
+	struct device *dev;
+	int newline = 0;
+	int ret;
+
+	if (gpio_name && !*gpio_name)
+		gpio_name = NULL;
+	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
+	     dev;
+	     ret = uclass_next_device(&dev)) {
+		const char *bank_name;
+		int num_bits;
+
+		bank_name = gpio_get_bank_info(dev, &num_bits);
+
+		if (!gpio_name || !bank_name ||
+		    !strncmp(gpio_name, bank_name, strlen(bank_name))) {
+			const char *p = NULL;
+			int offset;
+
+			if (bank_name) {
+				if (newline)
+					putc('\n');
+				printf("Bank %s:\n", bank_name);
+			}
+			newline = 1;
+			if (gpio_name && bank_name) {
+				p = gpio_name + strlen(bank_name);
+				offset = simple_strtoul(p, NULL, 10);
+				show_gpio(dev, bank_name, offset);
+			} else {
+				for (offset = 0; offset < num_bits; offset++)
+					show_gpio(dev, bank_name, offset);
+			}
+		}
+	}
+
+	return ret;
+}
+#endif
+
 static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	int gpio;
+	unsigned int gpio;
 	enum gpio_cmd sub_cmd;
 	ulong value;
-	const char *str_cmd, *str_gpio;
+	const char *str_cmd, *str_gpio = NULL;
+#ifdef CONFIG_DM_GPIO
+	int ret;
+#endif
 
+	if (argc < 2)
+ show_usage:
+		return CMD_RET_USAGE;
+	str_cmd = argv[1];
+	if (argc > 2)
+		str_gpio = argv[2];
+	if (!strcmp(str_cmd, "status")) {
+		/* Support deprecated gpio_status() */
 #ifdef gpio_status
-	if (argc == 2 && !strcmp(argv[1], "status")) {
 		gpio_status();
 		return 0;
-	}
+#elif defined(CONFIG_DM_GPIO)
+		return cmd_process_error(cmdtp, do_gpio_status(str_gpio));
+#else
+		goto show_usage;
 #endif
+	}
 
-	if (argc != 3)
- show_usage:
-		return CMD_RET_USAGE;
-	str_cmd = argv[1];
-	str_gpio = argv[2];
+	if (!str_gpio)
+		goto show_usage;
 
 	/* parse the behavior */
 	switch (*str_cmd) {
@@ -51,11 +141,23 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		default:  goto show_usage;
 	}
 
+#if defined(CONFIG_DM_GPIO)
+	/*
+	 * TODO(sjg at chromium.org): For now we must fit into the existing GPIO
+	 * framework, so we look up the name here and convert it to a GPIO number.
+	 * Once all GPIO drivers are converted to driver model, we can change the
+	 * code here to use the GPIO uclass interface instead of the numbered
+	 * GPIO compatibility layer.
+	 */
+	ret = gpio_lookup_name(str_gpio, NULL, NULL, &gpio);
+	if (ret)
+		return cmd_process_error(cmdtp, ret);
+#else
 	/* turn the gpio name into a gpio number */
 	gpio = name_to_gpio(str_gpio);
 	if (gpio < 0)
 		goto show_usage;
-
+#endif
 	/* grab the pin before we tweak it */
 	if (gpio_request(gpio, "cmd_gpio")) {
 		printf("gpio: requesting pin %u failed\n", gpio);
@@ -84,6 +186,7 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 }
 
 U_BOOT_CMD(gpio, 3, 0, do_gpio,
-	"input/set/clear/toggle gpio pins",
+	"query and control gpio pins",
 	"<input|set|clear|toggle> <pin>\n"
-	"    - input/set/clear/toggle the specified pin");
+	"    - input/set/clear/toggle the specified pin\n"
+	"gpio status [<bank> | <pin>]");
-- 
1.9.0.rc1.175.g0b1dcb5

  parent reply	other threads:[~2014-02-26 22:59 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-26 22:59 [U-Boot] [PATCH v9 0/14] Driver model implementation, tests, demo and GPIO Simon Glass
2014-02-26 22:59 ` [U-Boot] [PATCH v9 01/14] sandbox: Build a device tree file for sandbox Simon Glass
2014-03-04 19:16   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 02/14] Add cmd_process_error() to report and process errors Simon Glass
2014-03-04 19:16   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 03/14] yaffs: Remove private list implementation Simon Glass
2014-03-04 19:16   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 04/14] dm: Add README for driver model Simon Glass
2014-03-04 19:16   ` [U-Boot] [U-Boot,v9,04/14] " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 05/14] dm: Add base driver model support Simon Glass
2014-03-04 19:16   ` [U-Boot] [U-Boot,v9,05/14] " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 06/14] sandbox: config: Enable driver model Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 07/14] dm: Set up driver model after relocation Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 08/14] dm: Add basic tests Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot,v9,08/14] " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 09/14] dm: Add a 'dm' command for testing Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot,v9,09/14] " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 10/14] dm: Add a demonstration/example driver Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 11/14] dm: Add GPIO support and tests Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot,v9,11/14] " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 12/14] sandbox: Convert GPIOs to use driver model Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 22:59 ` Simon Glass [this message]
2014-03-04 19:17   ` [U-Boot] [U-Boot, v9, 13/14] dm: Enable gpio command to support " Tom Rini
2014-02-26 22:59 ` [U-Boot] [PATCH v9 14/14] dm: Remove old driver model documentation Simon Glass
2014-03-04 19:17   ` [U-Boot] [U-Boot, v9, " Tom Rini
2014-02-26 23:01 ` [U-Boot] [PATCH v9 0/14] Driver model implementation, tests, demo and GPIO Simon Glass

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=1393455567-13378-14-git-send-email-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.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