world leader in high performance signal processing
Trace: » gpio

General Purpose Input/Output (GPIO)

A General Purpose Input/Output (GPIO) is a flexible software-controlled digital signal. They are provided from many kinds of chip, and are familiar to Linux developers working with embedded and custom hardware. Each GPIO represents a bit connected to a particular pin, or “ball” on Ball Grid Array (BGA) packages. Board schematics show which external hardware connects to which GPIOs. Drivers can be written generically, so that board setup code passes such pin configuration data to drivers.

Often times, different aspects of the GPIO need to be controlled. This may include things such as:

  • direction: input or output
  • value: set or unset
  • polarity: high or low
  • edge: rising or falling

The GPIO framework is not for utilizing GPIOs as interrupt sources. If you need that functionality, please see the interrupts overview document.

Blackfin Interface

Depending on the Blackfin processor variant you're working with, some of the GPIO pins may be multiplexed with other peripherals which would preclude you from using the pins simultaneously as both GPIO and with a peripheral. For the exact details, please refer to the HRM for your cpu. Keep in mind that under this mux setup, the HRM will refer to the actual physical pin of the cpu as a programmable flag (pf) since it may correspond to either a GPIO or a peripheral line.

The GPIO framework will handle the port muxing details for you, so we won't cover that any further in this document.

GPIO Input Buffers

The GPIO input enable registers are used to enable the input buffers on any GPIO that is being used as an input. Leaving the input buffer disabled eliminates the need for pull-ups and pull-downs when a particular pin is not used in the system.

GPIO Value Register Pin Interpretation

The read value of a GPIO input pin depends on the configuration of the polarity and sensitivity settings.

Polar Edge Both Effect of MMR Settings
0 0 - Pin that is high reads as 1; pin that is low reads as 0
0 1 0 If rising edge occurred, pin reads as 1; otherwise, pin reads as 0
1 0 - Pin that is low reads as 1; pin that is high reads as 0
1 1 0 If falling edge occurred, pin reads as 1; otherwise, pin reads as 0
- 1 1 If any edge occurred, pin reads as 1; otherwise, pin reads as 0

Linux Framework

All of the relevant prototypes and defines for the GPIO framework can be found in the asm/gpio.h header file.

#include <asm/gpio.h>

GPIO Allocation

GPIOs allocation is handled by two functions:

int gpio_request(unsigned gpio, const char *label);
void gpio_free(unsigned gpio);

So if we wanted to allocate GPIO_PG8 in our driver module, we would simply do:

static __init int sample_module_init(void)
    int ret;
    ret = gpio_request(GPIO_PG8, "Sample Driver");
    if (ret) {
        printk(KERN_WARNING "sample_driver: unable to request GPIO_PG8\n");
        return ret;
    return 0;
static __exit void sample_module_exit(void)

GPIO Modifiers

Since you'll need to control all the aspects of the GPIO pin, there are a suite of get/set functions.

In the snippet below, we'll set the direction to output and value to 0.

    gpio_direction_output(GPIO_PG8, 1);
    gpio_set_value(GPIO_PG8, 0);

Function Reference

Here is the full reference of GPIO functions. Hopefully the names should be self-explanatory.

int gpio_is_valid(int number);
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);
void gpio_set_value(unsigned gpio, int value);
int gpio_get_value(unsigned gpio);

External Resources

For the latest and greatest GPIO framework information, please read the following files in the kernel source:

  • linux-2.6.x/Documentation/gpio.txt

Much of the information here was taken from that file.

Complete Table of Contents/Topics