Skip to contentSkip to conversation

gpio: generalize for all generic gpio providers

PATCH New Mike Frysinger 2 participants 7 messages Apr 3, 2011 – Apr 12, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: generalize for all generic gpio providers

The Blackfin gpio command isn't terribly Blackfin-specific. So generalize the few pieces into two new optional helpers:
name_to_gpio() - turn a string name into a GPIO # gpio_status() - display current pin bindings (think /proc/gpio)

Once these pieces are pulled out, we can relocate the cmd_gpio.c into the common directory.

---
 arch/blackfin/cpu/Makefile       |    1 -
 arch/blackfin/cpu/cmd_gpio.c     |  118 --------------------------------------
 arch/blackfin/include/asm/gpio.h |   53 +++++++++++++++++
 common/Makefile                  |    1 +
 common/cmd_gpio.c                |   86 +++++++++++++++++++++++++++
 5 files changed, 140 insertions(+), 119 deletions(-)
 delete mode 100644 arch/blackfin/cpu/cmd_gpio.c
 create mode 100644 common/cmd_gpio.c
arch/blackfin/cpu/Makefile +0 -1
index 4a9e577..df10f1b 100644
@@ -18,7 +18,6 @@ CEXTRA   := initcode.o
 SEXTRA   := start.o
 SOBJS    := interrupt.o cache.o
 COBJS-$(CONFIG_BOOTCOUNT_LIMIT) += bootcount.o
-COBJS-$(CONFIG_CMD_GPIO) += cmd_gpio.o
 COBJS-y  += cpu.o
 COBJS-y  += gpio.o
 COBJS-y  += interrupts.o
arch/blackfin/cpu/cmd_gpio.c deleted +0 -118

118 lines not on this page · raw message

arch/blackfin/include/asm/gpio.h +53 -0

59 lines not on this page · raw message

common/Makefile +1 -0
index 167612b..2ea0075 100644
@@ -95,6 +95,7 @@ COBJS-$(CONFIG_CMD_FLASH) += cmd_flash.o
 ifdef CONFIG_FPGA
 COBJS-$(CONFIG_CMD_FPGA) += cmd_fpga.o
 endif
+COBJS-$(CONFIG_CMD_GPIO) += cmd_gpio.o
 COBJS-$(CONFIG_CMD_I2C) += cmd_i2c.o
 COBJS-$(CONFIG_CMD_IDE) += cmd_ide.o
 COBJS-$(CONFIG_CMD_IMMAP) += cmd_immap.o
common/cmd_gpio.c new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#1 lore raw

PATCH 1/1 gpio: generalize for all generic gpio providers

Am Sonntag, den 03.04.2011, 04:43 -0400 schrieb Mike Frysinger:

Quoted 9 lines

The Blackfin gpio command isn't terribly Blackfin-specific. So generalize the few pieces into two new optional helpers:
name_to_gpio() - turn a string name into a GPIO # gpio_status() - display current pin bindings (think /proc/gpio)

Once these pieces are pulled out, we can relocate the cmd_gpio.c into the common directory.

Verified on Blackfin BF561 with full port range.

Quoted 44 lines
arch/blackfin/include/asm/gpio.h +38 -0
@@ -196,6 +196,59 @@ static inline int gpio_is_valid(int number)
 	return number >= 0 && number < MAX_BLACKFIN_GPIOS;
 }
 
+#include <linux/ctype.h>
+
+static inline int name_to_gpio(const char *name)
+{
+	int port_base;
+
+	if (tolower(*name) == 'p') {
+		++name;
+
+		switch (tolower(*name)) {
+#ifdef GPIO_PA0
+		case 'a': port_base = GPIO_PA0; break;
+#endif
+#ifdef GPIO_PB0
+		case 'b': port_base = GPIO_PB0; break;
+#endif
+#ifdef GPIO_PC0
+		case 'c': port_base = GPIO_PC0; break;
+#endif
+#ifdef GPIO_PD0
+		case 'd': port_base = GPIO_PD0; break;
+#endif
+#ifdef GPIO_PE0
+		case 'e': port_base = GPIO_PE0; break;
+#endif
+#ifdef GPIO_PF0
+		case 'f': port_base = GPIO_PF0; break;
+#endif
+#ifdef GPIO_PG0
+		case 'g': port_base = GPIO_PG0; break;
+#endif
+#ifdef GPIO_PH0
+		case 'h': port_base = GPIO_PH0; break;
+#endif
+#ifdef GPIO_PI0
+		case 'i': port_base = GPIO_PI0; break;
+#endif
+#ifdef GPIO_PJ

arch/blackfin/include/asm/gpio.h static inline int gpio_is_valid(int number) · line 246

Quoted 10 lines
arch/blackfin/include/asm/gpio.h +10 -0
+		case 'j': port_base = GPIO_PJ0; break;
+#endif
+		default:  return -1;
+		}
+
+		++name;
+	} else
+		port_base = 0;
+
+	return port_base + simple_strtoul(name, NULL, 10);

Remark: Leads to an oom access when exceeding the processor number of GPIOs, e.g. PF48 on a BF561. IMHO, no problem but only a cosmetic issue, not worth adding an additional per-cpu check.
In the end, no difference to other user errors like an invalid memory address.

Signature · 5 lines
carpe noctem engineering
Ingenieurbuero fuer Hard- & Software-Entwicklung Andreas Pretzsch
Dipl.-Ing. (FH) Andreas Pretzsch          Tel. +49-(0)731-5521572
Hahnengasse 3                             Fax: +49-(0)731-5521573
89073 Ulm, Germany                        email: apr at cn-eng.de
#2 lore raw

PATCH 1/1 gpio: generalize for all generic gpio providers

Quoted 8 lines · On Monday, April 11, 2011 15:34:17 Andreas Pretzsch wrote:

Am Sonntag, den 03.04.2011, 04:43 -0400 schrieb Mike Frysinger:

arch/blackfin/include/asm/gpio.h static inline int gpio_is_valid(int number) · line 246

Quoted 1 line
arch/blackfin/include/asm/gpio.h +1 -0
+	return port_base + simple_strtoul(name, NULL, 10);

Remark: Leads to an oom access when exceeding the processor number of GPIOs, e.g. PF48 on a BF561. IMHO, no problem but only a cosmetic issue, not worth adding an additional per-cpu check.
In the end, no difference to other user errors like an invalid memory address.

err, oom ? i guess you mean oob ?

simple to fix by having the return of gpio_request() get checked ...
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20110411/942891a6/attachment.pgp

#3 lore raw

PATCH 1/1 gpio: generalize for all generic gpio providers

Am Montag, den 11.04.2011, 23:14 -0400 schrieb Mike Frysinger:

Quoted 11 lines · On Monday, April 11, 2011 15:34:17 Andreas Pretzsch wrote:
Quoted 8 lines

Am Sonntag, den 03.04.2011, 04:43 -0400 schrieb Mike Frysinger:

arch/blackfin/include/asm/gpio.h static inline int gpio_is_valid(int number) · line 246

Quoted 1 line
arch/blackfin/include/asm/gpio.h +1 -0
+	return port_base + simple_strtoul(name, NULL, 10);

Remark: Leads to an oom access when exceeding the processor number of GPIOs, e.g. PF48 on a BF561. IMHO, no problem but only a cosmetic issue, not worth adding an additional per-cpu check.
In the end, no difference to other user errors like an invalid memory address.

err, oom ? i guess you mean oob ?

bfin> gpio input PF0
gpio: pin PF0 (gpio 0) value is 1
bfin> gpio input pf47
gpio: pin pf47 (gpio 47) value is 1
bfin> gpio input pf48
DCPLB exception outside of memory map at 0x33000000 [...]
PANIC: Blackfin internal error

Quoted 1 line

simple to fix by having the return of gpio_request() get checked ...

Works, thanks. With "[U-Boot] [PATCH] gpio: check request result":

bfin> gpio input PF0
gpio: pin PF0 (gpio 0) value is 1
bfin> gpio input pf47
gpio: pin pf47 (gpio 47) value is 1
bfin> gpio input pf48
gpio: requesting pin 48 failed
bfin>

Signature · 5 lines
carpe noctem engineering
Ingenieurbuero fuer Hard- & Software-Entwicklung Andreas Pretzsch
Dipl.-Ing. (FH) Andreas Pretzsch          Tel. +49-(0)731-5521572
Hahnengasse 3                             Fax: +49-(0)731-5521573
89073 Ulm, Germany                        email: apr at cn-eng.de
#4 lore raw

PATCH 1/1 gpio: generalize for all generic gpio providers

Quoted 13 lines · On Tuesday, April 12, 2011 09:13:35 Andreas Pretzsch wrote:

Am Montag, den 11.04.2011, 23:14 -0400 schrieb Mike Frysinger:

Quoted 11 lines · On Monday, April 11, 2011 15:34:17 Andreas Pretzsch wrote:
Quoted 8 lines

Am Sonntag, den 03.04.2011, 04:43 -0400 schrieb Mike Frysinger:

arch/blackfin/include/asm/gpio.h static inline int gpio_is_valid(int number) · line 246

Quoted 1 line
arch/blackfin/include/asm/gpio.h +1 -0
+	return port_base + simple_strtoul(name, NULL, 10);

Remark: Leads to an oom access when exceeding the processor number of GPIOs, e.g. PF48 on a BF561. IMHO, no problem but only a cosmetic issue, not worth adding an additional per-cpu check.
In the end, no difference to other user errors like an invalid memory address.

err, oom ? i guess you mean oob ?

Quoted 8 lines

bfin> gpio input PF0
gpio: pin PF0 (gpio 0) value is 1
bfin> gpio input pf47
gpio: pin pf47 (gpio 47) value is 1
bfin> gpio input pf48
DCPLB exception outside of memory map at 0x33000000 [...]
PANIC: Blackfin internal error

"oom" is usually reserved for "out of memory" as in "memory exhausted" -mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20110412/b6806b24/attachment.pgp

#5 lore raw

PATCH 1/1 gpio: check request result

In reply to Mike Frysinger

Make sure the pin request passed before attempting to use it later on.

---
 common/cmd_gpio.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
common/cmd_gpio.c +4 -1
index 9c9de28..9cc790a 100644
@@ -57,7 +57,10 @@ static int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		goto show_usage;
 
 	/* grab the pin before we tweak it */
-	gpio_request(gpio, "cmd_gpio");
+	if (gpio_request(gpio, "cmd_gpio")) {
+		printf("gpio: requesting pin %u failed\n", gpio);
+		return -1;
+	}
 
 	/* finally, let's do it: set direction and exec command */
 	if (sub_cmd == GPIO_INPUT) {
Signature · 1 line
1.7.5.rc1
#6 lore raw

PATCH 1/1 gpio: check request result

Am Dienstag, den 12.04.2011, 03:03 -0400 schrieb Mike Frysinger:

Quoted 3 lines

Make sure the pin request passed before attempting to use it later on.

Verified on Blackfin BF561 with full port range.

Signature · 5 lines
carpe noctem engineering
Ingenieurbuero fuer Hard- & Software-Entwicklung Andreas Pretzsch
Dipl.-Ing. (FH) Andreas Pretzsch          Tel. +49-(0)731-5521572
Hahnengasse 3                             Fax: +49-(0)731-5521573
89073 Ulm, Germany                        email: apr at cn-eng.de