Skip to contentSkip to conversation

Add generic GPIO support to x86

PATCH RESEND In review Florian Fainelli 8 participants 15 messages Oct 18, 2007 – Feb 23, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/4 Add generic GPIO support to x86

This patch adds the generic GPIO support to the x86 architecture. We do the same as for MIPS, we let the machine override the gpio callbacks and provide defaults one in mach-generic.

---
 arch/i386/Kconfig                   |    4 ++++
 include/asm-x86/gpio.h              |    6 ++++++
 include/asm-x86/mach-generic/gpio.h |   15 +++++++++++++++
 3 files changed, 25 insertions(+), 0 deletions(-)
 create mode 100644 include/asm-x86/gpio.h
 create mode 100644 include/asm-x86/mach-generic/gpio.h

---

arch/i386/Kconfig +4 -0
index bf9aafa..501fd6d 100644
@@ -79,6 +79,10 @@ config GENERIC_BUG
 	default y
 	depends on BUG
 
+config GENERIC_GPIO
+	bool
+	default n
+
 config GENERIC_HWEIGHT
 	bool
 	default y
include/asm-x86/gpio.h new file +6 -0
new file mode 100644
index 0000000..ff87fca
@@ -0,0 +1,6 @@
+#ifndef _ASM_I386_GPIO_H
+#define _ASM_I386_GPIO_H
+
+#include <gpio.h>
+
+#endif /* _ASM_I386_GPIO_H */
include/asm-x86/mach-generic/gpio.h new file +15 -0
new file mode 100644
index 0000000..5305dcb
@@ -0,0 +1,15 @@
+#ifndef __ASM_MACH_GENERIC_GPIO_H
+#define __ASM_MACH_GENERIC_GPIO_H
+
+int gpio_request(unsigned gpio, const char *label);
+void gpio_free(unsigned gpio);
+int gpio_direction_input(unsigned gpio);
+int gpio_direction_output(unsigned gpio, int value);
+int gpio_get_value(unsigned gpio);
+void gpio_set_value(unsigned gpio, int value);
+int gpio_to_irq(unsigned gpio);
+int irq_to_gpio(unsigned irq);
+
+#include <asm-generic/gpio.h>           /* cansleep wrappers */
+
+#endif /* __ASM_MACH_GENERIC_GPIO_H */
#1 lore raw
Quoted 5 lines · On Thu, 18 Oct 2007 15:51:24 +0200 Florian Fainelli…

This patch adds the generic GPIO support to the x86 architecture. We do the same as for MIPS, we let the machine override the gpio callbacks and provide defaults one in mach-generic.

[...]

include/asm-x86/mach-generic/gpio.h line 11

Quoted 9 lines
include/asm-x86/mach-generic/gpio.h new file +9 -0
+
+int gpio_request(unsigned gpio, const char *label);
+void gpio_free(unsigned gpio);
+int gpio_direction_input(unsigned gpio);
+int gpio_direction_output(unsigned gpio, int value);
+int gpio_get_value(unsigned gpio);
+void gpio_set_value(unsigned gpio, int value);
+int gpio_to_irq(unsigned gpio);
+int irq_to_gpio(unsigned irq);

While I certainly would like to see a generic GPIO API, this one isn't really useful for geode GPIOs. It would be nice to have one that did work for us as well. Unfortunately, I haven't had the chance to give much thought to this problem yet.

#2 lore raw

Hi Andres,

Le jeudi 18 octobre 2007, Andres Salomon a écrit :

Quoted 4 lines

While I certainly would like to see a generic GPIO API, this one isn't really useful for geode GPIOs. It would be nice to have one that did work for us as well. Unfortunately, I haven't had the chance to give much thought to this problem yet.

This one was discussed mostly on the ARM mailing-list and finally made his way to the mainline kernel. Though it lacks some functions to change for instance a entire GPIO line and not a single bit, it is used on ARM and MIPS systems so I would conform with this one for now because it is used by at least two or more architectures.

#3 lore raw
Quoted 8 lines · Florian Fainelli

Hi Andres,

Le jeudi 18 octobre 2007, Andres Salomon a écrit :

Quoted 4 lines

While I certainly would like to see a generic GPIO API, this one isn't really useful for geode GPIOs. It would be nice to have one that did work for us as well. Unfortunately, I haven't had the chance to give much thought to this problem yet.

Quoted 5 lines

This one was discussed mostly on the ARM mailing-list and finally made his way to the mainline kernel. Though it lacks some functions to change for instance a entire GPIO line and not a single bit, it is used on ARM and MIPS systems so I would conform with this one for now because it is used by at least two or more architectures.

Its reasonable to expect that the API will expand over time as it is thrust into new situations. There is nothing wrong with the existing API, it does an admirable job of simple GPIO frobbing. But on the Geode, GPIOs can do much more then just simple input and output. We can cause events, use input filtering for debouncing, set various drain options, and more. And these are not bullet points from the datasheet that we want to implement for completeness; these functions are actually being used right now in the kernel on real hardware. Just because other architectures haven't found a need to expand the API doesn't mean that we shouldn't expand it now. And bear in mind, the Geode isn't unique - I'll bet beers that there are MIPS and ARM architectures that have these features too, and they would use the API if it existed.

Jordan

#4 lore raw
Quoted 5 lines · On Fri, 19 Oct 2007 21:32:08 +0000 (UTC) Jordan Crouse <[email protected]>…
Quoted 5 lines

This one was discussed mostly on the ARM mailing-list and finally made his way to the mainline kernel. Though it lacks some functions to change for instance a entire GPIO line and not a single bit, it is used on ARM and MIPS systems so I would conform with this one for now because it is used by at least two or more architectures.

Make that four: AVR32 and Blackfin use the generic GPIO API too.

Quoted 6 lines

Its reasonable to expect that the API will expand over time as it is thrust into new situations. There is nothing wrong with the existing API, it does an admirable job of simple GPIO frobbing. But on the Geode, GPIOs can do much more then just simple input and output. We can cause events, use input filtering for debouncing, set various drain options, and more.

There are many other architectures that can do things like that. But since there are lots of variations in this kind of functionality from architecture to architecture (and even chip to chip), it has been excluded from the generic GPIO API.

Quoted 3 lines

And these are not bullet points from the
datasheet that we want to implement for completeness; these functions are actually being used right now in the kernel on real hardware.

How is it used? In many cases, you can get away with simply configuring the port with the required features like debouncing, pullup, drain options, etc. in the platform setup code where you can freely use platform-specific port configuration calls.

Quoted 5 lines

Just because other architectures haven't found a need to expand the API doesn't mean that we shouldn't expand it now. And bear in mind, the Geode isn't unique - I'll bet beers that there are MIPS and ARM architectures that have these features too, and they would use the API if it existed.

These features are certainly being utilized in the platform setup code on various platforms. I guess the current API might be too simple for drivers that need to reconfigure the port on the fly, but I haven't really seen any such drivers so far.

For example, the generic i2c-gpio driver supports three different ways of driving the i2c lines depending on how the port was configured by the platform.

Håvard

#5 lore raw
In reply to Florian Fainelli
Quoted 8 lines · On Fri, 19 Oct 2007 14:01:56 +0200 Florian Fainelli…

Hi Andres,

Le jeudi 18 octobre 2007, Andres Salomon a écrit :

Quoted 4 lines

While I certainly would like to see a generic GPIO API, this one isn't really useful for geode GPIOs. It would be nice to have one that did work for us as well. Unfortunately, I haven't had the chance to give much thought to this problem yet.

Quoted 5 lines

This one was discussed mostly on the ARM mailing-list and finally made his way to the mainline kernel. Though it lacks some functions to change for instance a entire GPIO line and not a single bit, it is used on ARM and MIPS systems so I would conform with this one for now because it is used by at least two or more architectures.

How does being used on MIPS and ARM make it suitable for x86? If you're arguing that other x86 platforms conform to it, and it might be useful for them; sure, I'll buy that. That doesn't seem to currently be the case, though.

#6 lore raw

Hi Andres,

Le lundi 22 octobre 2007, Andres Salomon a écrit :

Quoted 4 lines

How does being used on MIPS and ARM make it suitable for x86? If you're arguing that other x86 platforms conform to it, and it might be useful for them; sure, I'll buy that. That doesn't seem to currently be the case, though.

The purpose of the API, as defines itself is meant to be generic and architecture neutral, and the few functions it defines are.

Since I needed some GPIO helpers for the RDC R-321x SoC, I picked up this one. Now if you want to propose another one go ahead, and I will rebase my code on top of yours.

Signature · 2 lines
Cordialement, Florian Fainelli
------------------------------
#7 lore raw
In reply to Andres Salomon
Quoted 17 lines · Andres Salomon
Quoted 8 lines · On Fri, 19 Oct 2007 14:01:56 +0200 Florian Fainelli…

Hi Andres,

Le jeudi 18 octobre 2007, Andres Salomon a écrit :

Quoted 4 lines

While I certainly would like to see a generic GPIO API, this one isn't really useful for geode GPIOs. It would be nice to have one that did work for us as well. Unfortunately, I haven't had the chance to give much thought to this problem yet.

Quoted 5 lines

This one was discussed mostly on the ARM mailing-list and finally made his way to the mainline kernel. Though it lacks some functions to change for instance a entire GPIO line and not a single bit, it is used on ARM and MIPS systems so I would conform with this one for now because it is used by at least two or more architectures.

Quoted 4 lines

How does being used on MIPS and ARM make it suitable for x86? If you're arguing that other x86 platforms conform to it, and it might be useful for them; sure, I'll buy that. That doesn't seem to currently be the case, though.

It might be useful for everyone to check, whether the existing GPIO functionality can be extended, reworked to match the needs of Geode as well. Extending / reworking an existing interface is definitely better than adding a new incompatible one.

tglx

#8 lore raw

Hi,

Le jeudi 25 octobre 2007, Thomas Gleixner a écrit :

Quoted 4 lines

It might be useful for everyone to check, whether the existing GPIO functionality can be extended, reworked to match the needs of Geode as well. Extending / reworking an existing interface is definitely better than adding a new incompatible one.

Of course. Just because Geode is the only x86 potential user of such an API does not mean it should impose its design. Remember that this patch is part of a serie to add support for the RDC R-321x SoC, which becomes another x86-family user.

Looking at what Geode can do, and what RDC can do, I guess there are only few functions that are generic, all the drain, multiplexing, direction things are only available on the Geode board.
------------------------------

#9 lore raw
Quoted 5 lines · Florian Fainelli
Quoted 4 lines

It might be useful for everyone to check, whether the existing GPIO functionality can be extended, reworked to match the needs of Geode as well. Extending / reworking an existing interface is definitely better than adding a new incompatible one.

Quoted 8 lines

Of course. Just because Geode is the only x86 potential user of such an API does not mean it should impose its design. Remember that this patch is part of a serie to add support for the RDC R-321x SoC, which becomes another x86-family user.

Looking at what Geode can do, and what RDC can do, I guess there are only few functions that are generic, all the drain, multiplexing, direction things are only available on the Geode board.

It does not matter whether Geode has 500 functions and RDC has 5. It matters that we do _not_ want two different APIs which deal with the same thing, where one can obviously be a subset of the other. So either we can extend / rework the stuff that both parties are happy or we are not going to merge either of them.

tglx

#10 lore raw
In reply to Florian Fainelli
Quoted 52 lines · On Thu, 18 Oct 2007 15:51:24 +0200 Florian Fainelli…

This patch adds the generic GPIO support to the x86 architecture. We do the same as for MIPS, we let the machine override the gpio callbacks and provide defaults one in mach-generic.

---
 arch/i386/Kconfig                   |    4 ++++
 include/asm-x86/gpio.h              |    6 ++++++
 include/asm-x86/mach-generic/gpio.h |   15 +++++++++++++++
 3 files changed, 25 insertions(+), 0 deletions(-)
 create mode 100644 include/asm-x86/gpio.h
 create mode 100644 include/asm-x86/mach-generic/gpio.h

---

arch/i386/Kconfig +4 -0
index bf9aafa..501fd6d 100644
@@ -79,6 +79,10 @@ config GENERIC_BUG
 	default y
 	depends on BUG
 
+config GENERIC_GPIO
+	bool
+	default n
+
 config GENERIC_HWEIGHT
 	bool
 	default y
include/asm-x86/gpio.h new file +6 -0
new file mode 100644
index 0000000..ff87fca
@@ -0,0 +1,6 @@
+#ifndef _ASM_I386_GPIO_H
+#define _ASM_I386_GPIO_H
+
+#include <gpio.h>
+
+#endif /* _ASM_I386_GPIO_H */
include/asm-x86/mach-generic/gpio.h new file +5 -0
new file mode 100644
index 0000000..5305dcb
@@ -0,0 +1,15 @@
+#ifndef __ASM_MACH_GENERIC_GPIO_H
+#define __ASM_MACH_GENERIC_GPIO_H
+
+int gpio_request(unsigned gpio, const char *label);
+void gpio_free(unsigned gpio);

include/asm-x86/mach-generic/gpio.h line 15

Quoted 10 lines
include/asm-x86/mach-generic/gpio.h new file +10 -0
+int gpio_direction_input(unsigned gpio);
+int gpio_direction_output(unsigned gpio, int value);
+int gpio_get_value(unsigned gpio);
+void gpio_set_value(unsigned gpio, int value);
+int gpio_to_irq(unsigned gpio);
+int irq_to_gpio(unsigned irq);
+
+#include <asm-generic/gpio.h>           /* cansleep wrappers */
+
+#endif /* __ASM_MACH_GENERIC_GPIO_H */

There's a new driver in git-dvb which does a plain old

#include <asm/gpio.h>

and it explodes on i386 allmodconfig with:

In file included from drivers/media/video/mt9m001.c:20:
include/asm/gpio.h:4:18: error: gpio.h: No such file or directory

I don't even know how this was supposed to work. What does "#include <gpio.h>" give us? Is the kbuild system supposed to have added -Iinclude/asm/mach-generic? It obviously didn't.

Someone please fix.

It would be more modern to have a <linux/gpio.h> which takes care of cruddy details, but it's getting too late for that.

#11 lore raw
Quoted 1 line · Andrew Morton

Someone please fix.

This is what I was using purely for test builds ... most x86 hardware actually has no GPIOs, and I sure don't have any of the "unusual" x86 platforms here, so I'd not want to see this version merge. Someone more clued in on how x86 handles what ARM does with e.g. <asm/arch/...> should make a better patch.

(And my vote is to **NOT** use the "-I..." magic that PowerPC uses, that's just confusing as all get-out. And not needed.)

Quoted 2 lines

It would be more modern to have a <linux/gpio.h> which takes care of cruddy details, but it's getting too late for that.

That could be added eventually ... if the platform has GPIO support it could include the <asm/gpio.h> else it could define all the calls as error-returning inlines.

- Dave

========= CUT HERE
DEBUG ONLY -- make X86_PC use gpiolib.

It's not clear to me how the various x86-ish platforms should be made to work here, since there seems to be no convention that each platform type has its own <asm/arch/...> subdir.

---
 arch/x86/Kconfig       |    2 ++
 include/asm-x86/gpio.h |   19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/Kconfig +2 -0
@@ -228,6 +228,8 @@ choice
 
 config X86_PC
 	bool "PC-compatible"
+	select GENERIC_GPIO
+	select HAVE_GPIO_LIB
 	help
 	  Choose this option if your computer is a standard PC or compatible.
 
include/asm-x86/gpio.h +18 -1
@@ -1,6 +1,23 @@
 #ifndef _ASM_I386_GPIO_H
 #define _ASM_I386_GPIO_H
 
-#include <gpio.h>
+// #include <gpio.h>
+
+#include <asm-generic/gpio.h>           /* cansleep wrappers */
+
+#define gpio_get_value	__gpio_get_value
+#define gpio_set_value	__gpio_set_value
+#define gpio_cansleep	__gpio_cansleep
+
+static inline int gpio_to_irq(unsigned gpio)
+{
+	return -ENOSYS;
+}
+
+static inline int irq_to_gpio(unsigned irq)
+{
+	return -EINVAL;
+}
+
 
 #endif /* _ASM_I386_GPIO_H */
#12 lore raw
In reply to Andrew Morton
Quoted 2 lines · Andrew Morton

It would be more modern to have a <linux/gpio.h> which takes care of cruddy details, but it's getting too late for that.

Sort of like this? For drivers that don't want to list themselves in Kconfig as depending on GENERIC_GPIO (e.g. one NAND driver I heard about), this lets them use <linux/gpio.h> ... existing code can't break, and it won't hurt if new code uses this.

- Dave

============== CUT HERE
Add a <linux/gpio.h> defining fail/warn stubs for GPIO calls on platforms that don't support the GPIO programming interface. It includes the arch-specific interface glue otherwise.

---
 include/linux/gpio.h |   93 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
include/linux/gpio.h new file +93 -0
@@ -0,0 +1,93 @@
+#ifndef __LINUX_GPIO_H
+#define __LINUX_GPIO_H
+
+#ifdef CONFIG_GENERIC_GPIO
+#include <asm/gpio.h>
+
+#else
+
+/*
+ * Some platforms don't support the GPIO programming interface.
+ *
+ * In case some driver uses it anyway (it should normally have
+ * depended on GENERIC_GPIO), these routines help the compiler
+ * optimize out much GPIO-related code ... or trigger a runtime
+ * warning when something is wrongly called.
+ */
+
+static inline int gpio_is_valid(int number)
+{
+	return 0;
+}
+
+static inline int gpio_request(unsigned gpio, const char *label)
+{
+	return -EINVAL;
+}
+
+static inline void gpio_free(unsigned gpio)
+{
+	/* GPIO can never have been requested */
+	WARN_ON(1);
+}
+
+static inline int gpio_direction_input(unsigned gpio)
+{
+	return -EINVAL;
+}
+
+static inline int gpio_direction_output(unsigned gpio, int value)
+{
+	return -EINVAL;
+}
+
+static inline int gpio_get_value(unsigned gpio)
+{
+	/* GPIO can never have been requested or set as {in,out}put */
+	WARN_ON(1);
+	return 0;
+}
+
+static inline void gpio_set_value(unsigned gpio, int value)
+{
+	/* GPIO can never have been requested or set as output */
+	WARN_ON(1);
+}
+
+static int gpio_cansleep(unsigned gpio)
+{
+	/* GPIO can never have been requested or set as {in,out}put */
+	WARN_ON(1);
+	return 0;
+}
+
+static inline int gpio_get_value_cansleep(unsigned gpio)
+{
+	/* GPIO can never have been requested or set as {in,out}put */
+	WARN_ON(1);
+	return 0;
+}
+
+static inline void gpio_set_value_cansleep(unsigned gpio, int value)
+{
+	/* GPIO can never have been requested or set as output */
+	WARN_ON(1);
+}
+
+static inline int gpio_to_irq(unsigned gpio)
+{
+	/* GPIO can never have been requested or set as input */
+	WARN_ON(1);
+	return -EINVAL;
+}
+
+static inline int irq_to_gpio(unsigned irq)
+{
+	/* irq can never have been returned from gpio_to_irq() */
+	WARN_ON(1);
+	return -EINVAL;
+}
+
+#endif
+
+#endif /* __LINUX_GPIO_H */
#13 lore raw
Quoted 40 lines · David Brownell
Quoted 2 lines · Andrew Morton

It would be more modern to have a <linux/gpio.h> which takes care of cruddy details, but it's getting too late for that.

Sort of like this? For drivers that don't want to list themselves in Kconfig as depending on GENERIC_GPIO (e.g. one NAND driver I heard about), this lets them use <linux/gpio.h> ... existing code can't break, and it won't hurt if new code uses this.

- Dave

============== CUT HERE
Add a <linux/gpio.h> defining fail/warn stubs for GPIO calls on platforms that don't support the GPIO programming interface. It includes the arch-specific interface glue otherwise.

---
 include/linux/gpio.h |   93 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
include/linux/gpio.h new file +15 -0
@@ -0,0 +1,93 @@
+#ifndef __LINUX_GPIO_H
+#define __LINUX_GPIO_H
+
+#ifdef CONFIG_GENERIC_GPIO
+#include <asm/gpio.h>
+
+#else
+
+/*
+ * Some platforms don't support the GPIO programming interface.
+ *
+ * In case some driver uses it anyway (it should normally have
+ * depended on GENERIC_GPIO), these routines help the compiler
+ * optimize out much GPIO-related code ... or trigger a runtime
+ * warning when something is wrongly called.

include/linux/gpio.h line 25

Quoted 10 lines
include/linux/gpio.h new file +10 -0
+ */
+
+static inline int gpio_is_valid(int number)
+{
+	return 0;
+}
+
+static inline int gpio_request(unsigned gpio, const char *label)
+{
+	return -EINVAL;

Could we return -ENOSYS instead, so drivers will able to distinguish between "something really failed" and "there is no gpio support, fallback to other methods"? Without this, the notorious NAND driver will hardly benefit from this change. ;-)

Signature · 4 lines
Anton Vorontsov
email: [email protected]
backup email: [email protected]
irc://irc.freenode.net/bd2
#14 lore raw
Quoted 8 lines · Anton Vorontsov

include/linux/gpio.h line 25

Quoted 3 lines
include/linux/gpio.h new file +3 -0
+static inline int gpio_request(unsigned gpio, const char *label)
+{
+     return -EINVAL;

Could we return -ENOSYS instead, so drivers will able to distinguish between "something really failed" and "there is no gpio support, fallback to other methods"? Without this, the notorious NAND driver will hardly benefit from this change. ;-)

Sure; that'll be gpio_request() and the gpio_direction_*() calls only though, since those are the initial setup calls drivers make.

- Dave