Skip to contentSkip to conversation

arch-neutral GPIO calls

PATCH Kevin O'Connor 2 participants 3 messages Dec 31, 2006 – Jan 1, 2007

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

Based on earlier discussion, I'm sending a refresh of the generic GPIO patch, with several (ARM based) implementations in separate patches:

Hi Dave,

I'm very interested in seeing an abstraction for gpios. Over the last several months, I've been working on getting Linux running on my phone
- see www.handhelds.org for more information. These types of ARM based PDAs and phones are riddled with GPIOs, and there is frequent code duplication when two machines have to do similar functionality with different gpios.

Unfortunately, I fear the implementation you propose is not robust enough to handle the cases I need to handle. In particular, I think a "struct gpio" instead of "int gpio" is needed to describe a gpio pin.

Some background - on the phone I use, it has the standard PXA27x gpios, a gpio extender chip (egpio) which is connected via the memory bus, and a micro-controller attached via an i2c like bus. Other phones will commonly have the PXA27x, "egpio", and an "asic3" chip. With the sale of the PXA line, it seems Samsung and TI based ARM chips (and their corresponding GPIOs) will be on the rise.

As you know, basic functionality like setting a LED, detecting a button press, requesting an irq, etc. are common with GPIOs regardless of which platform, or which gpio chip, is in use.

The concern I have with your current implementation is that I don't see a way to flexibly add in support for additional gpio pins on a machine by machine basis. The code does do a good job of abstracting gpios based on the primary architecture (eg, PXA vs OMAP), but not on a chip basis (eg, PXA vs ASIC3).

Specifically, once the code pulls in "<asm/gpio.h>" it will get the PXA gpio code, but this will only allow access to the arch gpios, not the machine specific gpios. Also, one of the goals of the developers at handhelds.org is to have one kernel for many different phones -- from a userspace point of view they don't generally differ very much. As such, this isn't a matter of just having each "machine" override the gpio.h file at compile time; it really needs to be done at runtime.

I understand that the existing code works entirely on integers. However, I fear this is at attribute of the problem, not of the solution.

Quoted 6 lines

- Core patch, doc + <asm-arm/gpio.h> + <asm-generic/gpio.h>
- OMAP implementation
- AT91 implementation
- PXA implementation
- SA1100 implementation
- S3C2410 implementation

Your patch clearly shows that the existing implementations are using integers. I think there is a simple way to reuse the existing implementations. For example on pxa one could do something like:

============= include/linux/gpio.h

struct gpio_ops {
        int (*gpio_direction_input)(struct *gpio);
        ...
};
struct gpio {
     struct gpio_ops *ops;
};

============= arch/arm/mach-pxa/gpio.c

struct gpio pxa_gpios[120] = {
        {.ops = pxa_gpio_ops}, ...
};
int pxa_gpio_direction_input(struct *gpio) {
        int pxa_gpio = gpio - pxa_gpios;
        pxa_gpio_mode(pxa_gpio | GPIO_IN);
        return 0;
}

...

Quoted 5 lines

Other than clarifications, the main change in the doc is defining new calls safe for use with GPIOs on things like pcf8574 I2C gpio expanders; those new calls can sleep, but are otherwise the same as the spinlock-safe versions. The implementations above implement that as a wrapper (the asm-generic header) around the spinlock-safe calls.

As above, I'm confused how these expanders would work in practice. The expanders would be present on a machine by machine basis but the code seems to be implemented on an arch by arch basis. Perhaps an example would help me.

Please CC me on replies.

Cheers,
-Kevin

#1 lore raw
Quoted 6 lines · Kevin O'Connor
Quoted 2 lines

Based on earlier discussion, I'm sending a refresh of the generic GPIO patch, with several (ARM based) implementations in separate patches:

Hi Dave,

I'm very interested in seeing an abstraction for gpios.

Good! I suspect most folk who've had to work on a few different embedded Linux boards have noticed lots of needlessly-different GPIO code. Not many folk are actually seeing that as more than just ugliness though.

Quoted 2 lines

Unfortunately, I fear the implementation you propose is not robust enough to handle the cases I need to handle.

The _interface_ allows those additional GPIO controllers though. Specifically of the type you mentioned ... that's why this version defined new "cansleep" call variants.

Quoted 5 lines

The concern I have with your current implementation is that I don't see a way to flexibly add in support for additional gpio pins on a machine by machine basis. The code does do a good job of abstracting gpios based on the primary architecture (eg, PXA vs OMAP), but not on a chip basis (eg, PXA vs ASIC3).

You used the key word: implementation. The interface allows it, but such board-specific extensions haven't yet been needed; so they've not yet been implemented.

See the appended for a patch roughly along the lines of what has previously been discussed here. No interface change, just updated implementation code. And the additional implementation logic won't kick on boards that don't need it.

Note that the current implementations are a win even without yet being able to handle the board-specific external GPIO controllers. The API is clearer than chip-specific register access, and since it's arch-neutral it already solves integration problems (like having one SPI controller driver work on both AT91 and AVR).

Quoted 7 lines
Quoted 5 lines

Other than clarifications, the main change in the doc is defining new calls safe for use with GPIOs on things like pcf8574 I2C gpio expanders; those new calls can sleep, but are otherwise the same as the spinlock-safe versions. The implementations above implement that as a wrapper (the asm-generic header) around the spinlock-safe calls.

As above, I'm confused how these expanders would work in practice.

One approach: updating implementations along the lines the patch below. Other implementations could work too.

Note that I see that kind of update as happening after the first round of patches go upstream: accept the interface first, then update boards to support it ... including later the cansleep calls, on some boards.

I like the idea of first replacing the "old" GPIO accesses with ones using the new APIs ... and only then starting to convert old I2C (etc) accesses.

Quoted 3 lines

The expanders would be present on a machine by machine basis but the code seems to be implemented on an arch by arch basis. Perhaps an example would help me.

An example showing one way to implement that interface ... appended. The expanders would be board-specific, not arch/.../mach-* specific. (I try to avoid using the word "machine" there; it's too ambiguous.)

The code below just show how boards could plug in; it doesn't actually convert any boards to use that infrastructure. It should be obvious how to do that, if you've got a board needing it which works with the kernel.org GIT tree...

- Dave

================= CUT HERE
Preliminary version of support for board-specific declaring GPIO controllers, primarily for use with things like I2C access to GPIOs.

* GPIOs numbered 0..ARCH_GPIO_MAX always use internal arch/SOC specific controller access, and are spinlock-safe.

* All other GPIO numbers are allocated semi-dynamically, as part of declaring a GPIO controller, and are assumed spinlock-unsafe.

* Such external GPIO controllers provide a simple ops vector for setting direction, and accessing GPIO values.

Board-specific init code can declare the controllers, then hand out the GPIOs to code that needs them.

include/asm-generic/gpio.h +70 -1
Index: pxa/include/asm-generic/gpio.h
===================================================================
@@ -1,7 +1,51 @@
 #ifndef _ASM_GENERIC_GPIO_H
 #define _ASM_GENERIC_GPIO_H
 
-/* platforms that don't directly support access to GPIOs through I2C, SPI,
+#ifdef	CONFIG_GPIOLIB
+
+/* Boards with GPIOs accessed through I2C etc can access them through some
+ * gpio expander library code:
+ *
+ *  - Define ARCH_GPIO_MAX.  All GPIO ids accessed through such expanders
+ *    will be above this number.  Most other GPIOs are SOC-integrated, and
+ *    could be accessed by inlined direct register reads/writes.
+ *
+ *  - Board-specific setup code declares each controller and how many GPIOs
+ *    it handles.  The return value is the first GPIO associated with that
+ *    controller; other GPIOs immediately follow.  These GPIO numbers are
+ *    then handed out to the drivers which need them.
+ *
+ *  - Have the gpio management code (request, free, set direction) delegate
+ *    to gpiolib for GPIOs above ARCH_MAX; and fail gpio_to_irq() for them.
+ */
+struct gpio_ops {
+	int	(*direction_input)(void *, unsigned offset);
+	int	(*direction_output)(void *, unsigned offset);
+	int	(*get_value)(void *, unsigned offset);
+	int	(*set_value)(void *, unsigned offset, int value);
+};
+
+extern int __init gpiolib_define_controller(struct gpio_ops *, void *, int n);
+
+extern int gpiolib_request(unsigned gpio, const char *name);
+extern void gpiolib_free(unsigned gpio);
+extern int gpiolib_direction_input(unsigned gpio);
+extern int gpiolib_direction_output(unsigned gpio);
+
+
+/* for now, assume only arch/soc gpios are spinlock-safe */
+static inline int gpio_cansleep(unsigned gpio)
+{
+	return gpio <= ARCH_GPIO_MAX;
+}
+
+/* these standard gpio calls handle both kinds of GPIO */
+extern int gpio_get_value_cansleep(unsigned gpio);
+extern void gpio_set_value_cansleep(unsigned gpio, int value);
+
+#else
+
+/* boards that don't directly support access to GPIOs through I2C, SPI,
  * or other blocking infrastructure can use these wrappers.
  */
 
@@ -22,4 +66,29 @@ static inline void gpio_set_value_cansle
 	gpio_set_value(gpio, value);
 }
 
+/* having these gpiolib_*() inlines makes it easy to just kick in
+ * the library code for boards that need it
+ */
+
+static inline int gpiolib_request(unsigned gpio, const char *name)
+{
+	return -EINVAL;
+}
+
+static inline void gpiolib_free(unsigned gpio)
+{
+}
+
+static inline int gpiolib_direction_input(unsigned gpio)
+{
+	return -EINVAL;
+}
+
+static inline int gpiolib_direction_output(unsigned gpio)
+{
+	return -EINVAL;
+}
+
+#endif
+
 #endif /* _ASM_GENERIC_GPIO_H */
lib/Kconfig +6 -0
Index: pxa/lib/Kconfig
===================================================================
@@ -106,4 +106,10 @@ config IOMAP_COPY
 	depends on !UML
 	default y
 
+#
+# gpiolib support is selected on boards that need it
+#
+config GPIOLIB
+	boolean
+
 endmenu
lib/Makefile +1 -0
Index: pxa/lib/Makefile
===================================================================
@@ -57,6 +57,7 @@ obj-$(CONFIG_AUDIT_GENERIC) += audit.o
 
 obj-$(CONFIG_SWIOTLB) += swiotlb.o
 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
+obj-$(CONFIG_GPIOLIB) += gpiolib.o
 
 lib-$(CONFIG_GENERIC_BUG) += bug.o
 
lib/gpiolib.c new file +192 -0

192 lines not on this page · raw message

include/asm-arm/arch-pxa/gpio.h +6 -0
Index: pxa/include/asm-arm/arch-pxa/gpio.h
===================================================================
@@ -30,6 +30,8 @@
 
 #include <asm/arch/pxa-regs.h>
 
+#define	ARCH_GPIO_MAX	PXA_LAST_GPIO
+
 static inline int gpio_get_value(unsigned gpio);
 static inline void gpio_set_value(unsigned gpio, int value);
 
@@ -47,11 +49,15 @@ static inline void gpio_free(unsigned gp
 
 static inline int gpio_direction_input(unsigned gpio)
 {
+	if (gpio_cansleep(gpio))
+		return gpiolib_direction_input(gpio);
 	return pxa_gpio_mode(gpio | GPIO_IN);
 }
 
 static inline int gpio_direction_output(unsigned gpio)
 {
+	if (gpio_cansleep(gpio))
+		return gpiolib_direction_output(gpio);
 	return pxa_gpio_mode(gpio | GPIO_OUT);
 }
 
#2 lore raw

Hi Dave,

Quoted 6 lines · David Brownell
Quoted 5 lines

The concern I have with your current implementation is that I don't see a way to flexibly add in support for additional gpio pins on a machine by machine basis. The code does do a good job of abstracting gpios based on the primary architecture (eg, PXA vs OMAP), but not on a chip basis (eg, PXA vs ASIC3).

Quoted 8 lines

You used the key word: implementation. The interface allows it, but such board-specific extensions haven't yet been needed; so they've not yet been implemented.

See the appended for a patch roughly along the lines of what has previously been discussed here. No interface change, just updated implementation code. And the additional implementation logic won't kick on boards that don't need it.

Thanks for enlightening me on previous discussions. The interface in the "gpiolib" patch is along the lines of what I'm looking for.

However, I still feel this approach is backwards. Going from a 'struct gpio' to an 'int gpio' is easy. Attempting to go from an 'int gpio' to a 'struct gpio' is hard, as the code you provide demonstrates.

Can you help explain what the gain to using an integer over a 'struct gpio' is? I can't help but feel that fundamentally gpios are not integers and that it is a mistake to code an API around that concept.

Quoted 5 lines

Note that the current implementations are a win even without yet being able to handle the board-specific external GPIO controllers. The API is clearer than chip-specific register access, and since it's arch-neutral it already solves integration problems (like having one SPI controller driver work on both AT91 and AVR).

I agree that the code you provide is a big step up from the existing code. We also both seem to agree that more than just an arch abstraction is necessary (the cansleep stuff seems to be an acknowledgment of this).

However, I don't understand the downside to making the API sane from the start and avoiding the artificial integer namespace problems.

Quoted 3 lines

Note that I see that kind of update as happening after the first round of patches go upstream: accept the interface first, then update boards to support it ... including later the cansleep calls, on some boards.

Once many of the problems are fixed, I fear fixing the remaining will be a harder sell. Due to the difficulty of implementing kernel wide APIs, I suspect some maintainers will just stick to cramming features in the arch directory (as your patch encourages) which can lead to duplicated code and fragmented implementations.

Thanks for working on this.
-Kevin