3.2.2.8. GPIO

GPIO Driver Overview

The GPIO Driver enables the GPIO controllers available on the device. The driver configures the GPIO hardware and interfaces and makes them available to user space as character device or other device drivers that need to access pins. For example, a MMC/SD driver may need to read a GPIO as in input to determine if a card is present. The H/W GPIO controllers available will vary by SoC and system configuration.

Overview

The GPIO controllers allow interaction with GPIO pins for input/output and interrupt generation.

../../../../_images/GPIO_driver_diagram.png

User Layer

The GPIO driver can be used in user space via Linux CLI. This section provides examples, command-line-tools, and guidance for using GPIOs in user space.

Note

Since Linux 4.8, the GPIO sysfs interface is deprecated. User space should use the character device instead.

Example: Using GPIOs with the libgpiod library in user space to toggle a GPIO pin, which is connected to an LED on the SK-AM62B-P1 board:

  1. Locate Table 2-23 under 2.6.16.3 User Test LEDs in the AM62x Starter Kit User’s Guide (Rev. C): https://www.ti.com/lit/pdf/spruj40.

    • This example uses LED “LD11”, AKA the IO_EXP_TEST_LED. It is connected to GPIO U70.24(P27), AKA pin 27 of the GPIO Port Expander on the SK-AM62B-P1 board:

    ../../../../_images/sk-am62b-p1-top.png
  2. Detect every available gpiochip:

    $ gpiodetect
    gpiochip0 [600000.gpio] (92 lines)
    gpiochip1 [601000.gpio] (52 lines)
    gpiochip2 [1-0022] (24 lines)
    
    • This shows 92+52+24=168 total GPIO lines available across 3 GPIO chips: 0, 1, and 2.

  3. Read info for every available gpiochip:

    $ gpioinfo
    gpiochip0 - 92 lines:
             line   0:       unnamed                 input
             line   1:       unnamed                 input
             {...}
             line  91:       unnamed                 input
    gpiochip1 - 52 lines:
             line   0:       unnamed                 input
             line   1:       unnamed                 input
             {...}
             line  51:       unnamed                 input
    gpiochip2 - 24 lines:
             line   0:       "GPIO_CPSW2_RST"        input
             line   1:       "GPIO_CPSW1_RST"        input
             {...}
             line  23:       "IO_EXP_TEST_LED"       input
    
    • This should result in a large output, 168+ lines in this case. It outputs all GPIO lines available on every GPIO chip, as well as their names and input/output directions. The above output is truncated.

    • For more info on gpioinfo, see its man page: https://libgpiod.readthedocs.io/en/latest/gpioinfo.html.

    • To see info for a specific GPIO chip, use the ‘-c’ flag and the GPIO chip number.

  4. Read info for gpiochip2 (for this example):

    $ gpioinfo -c 2
    gpiochip2 - 24 lines:
             line   0:       "GPIO_CPSW2_RST"        input
             line   1:       "GPIO_CPSW1_RST"        input
             line   2:       "PRU_DETECT"            input
             line   3:       "MMC1_SD_EN"            output consumer="regulator-3"
             line   4:       "VPP_LDO_EN"            input
             line   5:       "EXP_PS_3V3_En"         input
             line   6:       "EXP_PS_5V0_En"         input
             line   7:       "EXP_HAT_DETECT"        input
             line   8:       "GPIO_AUD_RSTn"         input
             line   9:       "GPIO_eMMC_RSTn"        input
             line  10:       "UART1_FET_BUF_EN"      input
             line  11:       "WL_LT_EN"              input
             line  12:       "GPIO_HDMI_RSTn"        input
             line  13:       "CSI_GPIO1"             input
             line  14:       "CSI_GPIO2"             input
             line  15:       "PRU_3V3_EN"            input
             line  16:       "HDMI_INTn"             input consumer="interrupt"
             line  17:       "PD_I2C_IRQ"            input
             line  18:       "MCASP1_FET_EN"         input
             line  19:       "MCASP1_BUF_BT_EN"      input
             line  20:       "MCASP1_FET_SEL"        input
             line  21:       "UART1_FET_SEL"         input
             line  22:       "TSINT#"                input
             line  23:       "IO_EXP_TEST_LED"       output
    
    • We see that IO_EXP_TEST_LED is connected to GPIO2_23 on the SK-AM62B-P1 board.

  5. Read the value of the GPIO pin:

    $ gpioget --numeric -c 2 23
    0
    
    • Use variations of gpioget to read the value of the GPIO pin:

      $ gpioget -c 2 23
      "23"=inactive
      $ gpioget IO_EXP_TEST_LED
      "IO_EXP_TEST_LED"=inactive
      $ gpioget --numeric IO_EXP_TEST_LED
      0
      
    • For more info on gpioget, see its man page: https://libgpiod.readthedocs.io/en/latest/gpioget.html.

  6. Set the GPIO pin to turn on the LED

    $ gpioset -c 2 23=1
    ^C
    $ gpioset -c 2 23=0
    ^C
    
    • Note:

    • This should have turned the active-high LED “LD11” on the SK-AM62B-P1 on and off.

    • Use variations of gpioset to set the value of the GPIO pin:

      $ gpioset IO_EXP_TEST_LED=1
      ^C
      $ gpioset IO_EXP_TEST_LED=0
      ^C
      
    • This should have also turned the active-high LED “LD11” on the SK-AM62B-P1 on and off.

    • Example: Toggle GPIO2_23 (blink “LD11”) at 10Hz, then turn it off:

      $ gpioset --toggle 50ms IO_EXP_TEST_LED=1
      ^C
      $ gpioset IO_EXP_TEST_LED=0
      ^C
      
    • For more info on gpioset, see its man page: https://libgpiod.readthedocs.io/en/latest/gpioset.html.

Example: Using GPIOs on the Raspberry Pi Compatible 40-pin Header on the SK-AM62B-P1 board:

../../../../_images/sk-am62b-p1-top-2.png
  1. Locate Table 2-25 under 2.6.17.2 User Expansion Connector in the AM62x Starter Kit User’s Guide (Rev. C): https://www.ti.com/lit/pdf/spruj40.

  2. Identify the desired GPIO pin number and pin multiplexed signal. For example, GPIO1_25 corresponds to physical pin 8 on the 40-pin header.

  3. Use libgpiod mentioned in the previous example to control the GPIO pin, ensuring that the GPIO pin is not being used already.

Additional Resources

Consuming Drivers

The GPIO Driver can also be easily leveraged by other drivers to “consume” a GPIO.

  • For an example of a driver using a GPIO pin, examine this entry in a dts file for how the MMC/SD interface could use a GPIO as a card detect pin:

    • ti-processor-sdk-linux-am62xx-evm-<sdk version>/board-support/ti-linux-kernel-<kernel version>+git-ti/arch/arm/boot/dts/ti/omap/am335x-bone-common.dtsi.


Features

  • Access GPIO from user space as input or output

  • Leverage GPIO from another “consumer” driver