[U-Boot] [PATCH v3] M28: Added pin name support in GPIO driver

Robert Deliën Robert at delien.nl
Wed Nov 23 22:10:49 CET 2011


This patch adds pin name support in the GPIO driver. With this patch applied, the gpio command supports pins to be addressed with friendly names.

Please note:
Function gpio_name_to_pin checks for set bits getting lost in translation and returns -EINVAL if this occurs, but a correctly translated name doesn't automatically produce a valid pin. It only means the bank/pin numbers fit the designated bitfields. After successful translation, validity needs to be checked separately.

Signed-off-by: Robert Deliën <robert at delien.nl>

diff --git a/arch/arm/include/asm/arch-mx28/gpio.h b/arch/arm/include/asm/arch-mx28/gpio.h
index 5abd7b1..37d3e88 100644
--- a/arch/arm/include/asm/arch-mx28/gpio.h
+++ b/arch/arm/include/asm/arch-mx28/gpio.h
@@ -26,6 +26,9 @@
 #ifdef	CONFIG_MXS_GPIO
 void mxs_gpio_init(void);
 
+extern int gpio_name_to_pin(const char *name);
+#define gpio_name_to_pin(name)	gpio_name_to_pin(name)
+
 extern int gpio_invalid(int gp);
 #define gpio_invalid(gpio) gpio_invalid(gpio)
 
diff --git a/arch/blackfin/include/asm/gpio.h b/arch/blackfin/include/asm/gpio.h
index 224688f..8128df1 100644
--- a/arch/blackfin/include/asm/gpio.h
+++ b/arch/blackfin/include/asm/gpio.h
@@ -1,7 +1,24 @@
 /*
- * Copyright 2006-2009 Analog Devices Inc.
+ * (C) Copyright 2006-2009
+ * Analog Devices Inc.
  *
- * Licensed under the GPL-2 or later.
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
  */
 
 #ifndef __ARCH_BLACKFIN_GPIO_H__
@@ -199,7 +216,7 @@ static inline int gpio_is_valid(int number)
 
 #include <linux/ctype.h>
 
-static inline int name_to_gpio(const char *name)
+static inline int gpio_name_to_pin(const char *name)
 {
 	int port_base;
 
@@ -246,7 +263,7 @@ static inline int name_to_gpio(const char *name)
 
 	return port_base + simple_strtoul(name, NULL, 10);
 }
-#define name_to_gpio(n) name_to_gpio(n)
+#define gpio_name_to_pin(n) gpio_name_to_pin(n)
 
 #define gpio_status() bfin_gpio_labels()
 
diff --git a/common/cmd_gpio.c b/common/cmd_gpio.c
index 51e11e7..8a70d0c 100644
--- a/common/cmd_gpio.c
+++ b/common/cmd_gpio.c
@@ -1,9 +1,24 @@
 /*
- * Control GPIO pins on the fly
+ * (C) Copyright 2008-2011
+ * Analog Devices Inc.
  *
- * Copyright (c) 2008-2011 Analog Devices Inc.
+ * See file CREDITS for list of people who contributed to this
+ * project.
  *
- * Licensed under the GPL-2 or later.
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
  */
 
 #include <common.h>
@@ -11,8 +26,8 @@
 
 #include <asm/gpio.h>
 
-#ifndef name_to_gpio
-#define name_to_gpio(name) simple_strtoul(name, NULL, 10)
+#ifndef gpio_name_to_pin
+#define gpio_name_to_pin(name)	simple_strtoul(name, NULL, 10)
 #endif
 
 #ifndef gpio_invalid
@@ -41,8 +56,8 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 #endif
 
 	if (argc != 3)
- show_usage:
-		return cmd_usage(cmdtp);
+		goto show_usage;
+
 	str_cmd = argv[1];
 	str_gpio = argv[2];
 
@@ -56,9 +71,11 @@ 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);
-	if (gpio < 0)
-		goto show_usage;
+	gpio = gpio_name_to_pin(str_gpio);
+	if (gpio < 0) {
+		printf("gpio: unknown pin %s\n", str_gpio);
+		return -1;
+	}
 
 	/* check bank and pin number for validity */
 	if (gpio_invalid(gpio)) {
@@ -91,6 +108,8 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	gpio_free(gpio);
 
 	return value;
+show_usage:
+	return cmd_usage(cmdtp);
 }
 
 U_BOOT_CMD(gpio, 3, 0, do_gpio,
diff --git a/drivers/gpio/mxs_gpio.c b/drivers/gpio/mxs_gpio.c
index dabee90..8ec57d6 100644
--- a/drivers/gpio/mxs_gpio.c
+++ b/drivers/gpio/mxs_gpio.c
@@ -23,6 +23,7 @@
  * MA 02111-1307 USA
  */
 
+#include <linux/ctype.h>
 #include <common.h>
 #include <netdev.h>
 #include <asm/errno.h>
@@ -81,6 +82,46 @@ void mxs_gpio_init(void)
 	}
 }
 
+int gpio_name_to_pin(const char *name)
+{
+	int bank;
+	int pin;
+	char *endptr;
+
+	if (!name)
+		return -EINVAL;
+
+	if (name[0] >= '0' && name[0] <= '9') {
+		pin = simple_strtoul(name, &endptr, 10);
+		if (endptr[0])
+			return -EINVAL;
+		if (pin & ~(MXS_PAD_BANK_MASK | MXS_PAD_PIN_MASK))
+			return -EINVAL;
+
+		return pin;
+	}
+
+	if (tolower(name[0]) != 'b')
+		return -EINVAL;
+	name++;
+	bank = simple_strtoul(name, &endptr, 10);
+	if (bank & ~(MXS_PAD_BANK_MASK >> MXS_PAD_BANK_SHIFT))
+		return -EINVAL;
+
+	if (tolower(name[0]) != 'p')
+		return -EINVAL;
+	name = endptr + 1;
+	pin = simple_strtoul(name, &endptr, 10);
+	if (pin & ~(MXS_PAD_PIN_MASK >> MXS_PAD_PIN_SHIFT))
+		return -EINVAL;
+
+	if (endptr[0])
+		return -EINVAL;
+
+	return ((bank << MXS_PAD_BANK_SHIFT) & MXS_PAD_BANK_MASK) |
+	       ((pin  << MXS_PAD_PIN_SHIFT ) & MXS_PAD_PIN_MASK );
+}
+
 int gpio_invalid(int gp)
 {
 	if ( (gp & ~(MXS_PAD_BANK_MASK | MXS_PAD_PIN_MASK)) ||



More information about the U-Boot mailing list