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)
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