Although you can develop your own driver to control GPIOs inside kernel space, there is a much simpler way for accessing GPIOs from user space. When timing requirements are not an issue, you are able to use GPIO-SYSFS.
SYSFS is a virtual file system that exports some kernel internal framework functionalities to user space and GPIO is one of the frameworks that can have functionalities exported through SYSFS.
The GPIO-SYSFS feature is available in all mainline kernels from 2.6.27 onwards.
Configuring Kernel to export GPIO through SYSFS
To enable GPIO in SYSFS, select the following kernel option:
Device Drivers --->
--- GPIO Support
[*] /sys/class/gpio/... (sysfs interface)
If you are using i.MX233 or i.MX28, after recompiling the kernel, do not forget to generate boot streams again, because this is not automatic even in ltib.
Be sure that the pins you will try to use are really accessible as GPIO pins and were not requested by the kernel (gpio_request). If pin was gpio_request'ed, you will need to gpio_export the same pin inside the kernel in order to have it accessible through SYSFS. If pin is not set as GPIO by default, you will need to set IO MUX in the proper file inside <kernel>/arch/arm/mach-XXX.
Accessing GPIO in user space
After enabling GPIO-SYSFS feature, you can boot your device with the new kernel to make some tests.
First you need to export the GPIO you want to test to the user space:
echo XX > /sys/class/gpio/export
XX shall be determined by the following algorithm:
GPIOA_[B] is the GPIO you want to export, where "A" is the GPIO bank and "B" is the offset of the pin in the bank.
if the first available GPIO bank is 0 // (iMX.28, for example)
XX = A*32 + B;
else // first GPIO bank is 1
XX = (A-1)*32 + B;
After exporting a GPIO pin, you shall be able to see the GPIO interface exported to:
Through this interface, you are now able to do things like:
# Reading the pin value
# Changing pin direction
echo in > /sys/class/gpio/gpioXX/direction
echo out > /sys/class/gpio/gpioXX/direction
# Toggling GPIO output level
echo 0 > /sys/class/gpio/gpioXX/value
echo 1 > /sys/class/gpio/gpioXX/value
It is important to note that through the GPIO virtual filesystem it is only possible to deal with one GPIO pin at a time (per command).