Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of Contents

Table of Contents

Defining GPIO in Device-tree Source File

Every single device has a node in device-tree source (dts) file in Linux. Property pinctrl-0 (or pinctrl-1, pinctrl-2,… if a device has more states) is used to point at pin configuration node within pin controller (node pinctrl@f8800080 in SP7350). Pin configuration nodes (sub-nodes in node pinctrl@f8800080 ) define the actual pins assignment.

...

Code Block
languagec
uart0_test_pins: pinmux_uart0_test-pins {
    function = "GPIO";
    pins = "GPIO0", GPIO90";
    output-enable;
};

Manipulating GPIO in Linux Kernel (driver)

In above example, property test1-gpios define one GPIO pin to GPIO0 of pin controller and property test2-gpios define one GPIO pin to GPIO90 of pin controller, respectively. Linux driver can use function devm_gpiod_get to get GPIO descriptor with the property. Use gpiod_set_value to set value to GPIO or use gpiod_get_value to get value from GPIO. Refer to the following C example code.

Code Block
languagec
struct gpio_desc *test1_gpio;
struct gpio_desc *test2_gpio;
:
:
// Get test1_gpio.
test1_gpio = devm_gpiod_get(&pdev->dev, "test1", GPIOD_OUT_HIGH);
if (!IS_ERR(test1_gpio)) {
    printk(KERN_INFO "test1_gpio is at GPIO[%d].\n", desc_to_gpio(test1_gpio));
}

// Get test2_gpio.
test2_gpio = devm_gpiod_get(&pdev->dev, "test2", GPIOD_OUT_LOW);
if (!IS_ERR(test2_gpio)) {
    printk(KERN_INFO "test2_gpio is at GPIO[%d].\n", desc_to_gpio(test2_gpio));
}
:
:
gpiod_set_value(test1_gpio, 0);
gpiod_set_value(test2_gpio, 1);
:

Manipulating GPIO in Linux User-space (using sysfs)

Users can setup and access GPIO using sysfs interface. Path of gpio of sysfs is /sys/class/gpio. Enter the folder and use ls command to list contents:

...

where exportand unexportare write-only control interface. gpiochip0is a GPIO controller.

Example 1: Setup GPIO0 as Output Pin.

First, use echo command to export GPIO0:

...

Code Block
languagebash
/sys/class/gpio # echo 1 > GPIO0/value
/sys/class/gpio # cat GPIO0/value
1
/sys/class/gpio # echo 0 > GPIO0/value
/sys/class/gpio # cat GPIO0/value
0
/sys/class/gpio #

Example 2:Setup GPIO0 as Input Pin.

Code Block
languagebash
/sys/class/gpio # echo 0 > export
/sys/class/gpio # ls
GPIO0 		export     gpiochip0  unexport
/sys/class/gpio # echo in > GPIO0/direction
/sys/class/gpio # cat GPIO0/direction
in
/sys/class/gpio # cat GPIO0/value
1
/sys/class/gpio #

...