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.
To enable GPIO in SYSFS, select the following kernel option:
Device Drivers ---> --- GPIO Support [*] /sys/class/gpio/... (sysfs interface)
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:
/sys/class/gpio/gpioXX
Through this interface, you are now able to do things like:
# Reading the pin value cat /sys/class/gpio/gpioXX/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