[U-Boot] [PATCH 1/2] GPIO: Extend the cmd_gpio API by gpio_{from, to}_string()

Marek Vasut marex at denx.de
Tue May 1 22:50:14 CEST 2012


The gpio_from_string() call shall parse the incoming GPIO name taken
from the command line and return the GPIO number used within U-Boot or
return -1 on error.

The gpio_to_string() on the other hand allows nicer reporting of GPIO
name in the output of cmd_gpio.

Signed-off-by: Marek Vasut <marex at denx.de>
Cc: Detlev Zundel <dzu at denx.de>
Cc: Mike Frysinger <vapier at gentoo.org>
Cc: Stefano Babic <sbabic at denx.de>
---
 common/cmd_gpio.c |   27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/common/cmd_gpio.c b/common/cmd_gpio.c
index 47eee89..12870ef 100644
--- a/common/cmd_gpio.c
+++ b/common/cmd_gpio.c
@@ -15,6 +15,22 @@
 #define name_to_gpio(name) simple_strtoul(name, NULL, 10)
 #endif
 
+int __gpio_from_string(const char *name)
+{
+	return name_to_gpio(name);
+}
+
+int gpio_from_string(const char *name)
+	__attribute__((weak, alias("__gpio_from_string")));
+
+int __gpio_to_string(int gpio, char *buf, int buflen)
+{
+	return 0;
+}
+
+int gpio_to_string(int gpio, char *buf, int buflen)
+	__attribute__((weak, alias("__gpio_from_string")));
+
 enum gpio_cmd {
 	GPIO_INPUT,
 	GPIO_SET,
@@ -27,7 +43,9 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	int gpio;
 	enum gpio_cmd sub_cmd;
 	ulong value;
-	const char *str_cmd, *str_gpio;
+	const char *str_cmd;
+	char str_gpio[16] = { 0 };
+	int ret;
 
 #ifdef gpio_status
 	if (argc == 2 && !strcmp(argv[1], "status")) {
@@ -40,7 +58,6 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  show_usage:
 		return CMD_RET_USAGE;
 	str_cmd = argv[1];
-	str_gpio = argv[2];
 
 	/* parse the behavior */
 	switch (*str_cmd) {
@@ -52,7 +69,7 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	}
 
 	/* turn the gpio name into a gpio number */
-	gpio = name_to_gpio(str_gpio);
+	gpio = gpio_from_string(argv[2]);
 	if (gpio < 0)
 		goto show_usage;
 
@@ -75,8 +92,10 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		}
 		gpio_direction_output(gpio, value);
 	}
+
+	ret = gpio_to_string(gpio, str_gpio, sizeof(str_gpio));
 	printf("gpio: pin %s (gpio %i) value is %lu\n",
-		str_gpio, gpio, value);
+		ret ? str_gpio : argv[2], gpio, value);
 
 	gpio_free(gpio);
 
-- 
1.7.10



More information about the U-Boot mailing list