2250242_en-US

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

2250242_en-US

2250242_en-US

How to use a GPIO(libgpiod) with gpioget/gpioset, on i.MX8Q as an example

difference between gpioget and "cat /sys/class/gpio" in GPIO control:

  • gpioget:

    This is a command-line utility provided by the libgpiod library. It is designed to interact with GPIO lines through the modern Linux GPIO character device interface (/dev/gpiochipN).

  • /sys/class/gpio:

    This refers to the legacy sysfs interface for GPIO control in Linux. While widely used in the past, this interface has been officially deprecated in favor of the libgpiod character device interface since Linux kernel version 6.0 and later.


Below is an example of libgpiod tested on i.MX8Q-MEK, L6.12.3 BSP.

Device tree settings:

We use GPIO1-05 and GPIO1-06 in this example. can put GPIO pinctrls in a specific group like “gpio_test”, or under certain driver like “gpio-keys”

    gpio_test: gpio-test {
        pinctrl-names = "default", "sleep";
        pinctrl-0 = <&pinctrl_gpio_test_default>;
        pinctrl-1 = <&pinctrl_gpio_test_sleep>;
        status = "okay";
    };

    gpio-keys {
        compatible = "gpio-keys";
        pinctrl-names = "default", "sleep";
        pinctrl-0 = <&pinctrl_gpio_example_default>;
        pinctrl-1 = <&pinctrl_gpio_example_sleep>;
        key_wakeup2{
            label = "wakeup_key";
            gpios = <&lsio_gpio2 1 GPIO_ACTIVE_LOW>;
            linux,code = ;
            wakeup-source;
        };

    pinctrl_gpio_test_default: gpiotestgrp_default {
        fsl,pins = <
            IMX8QM_LVDS0_I2C0_SDA_LSIO_GPIO1_IO07       0x06000061
            IMX8QM_QSPI1A_DATA0_LSIO_GPIO4_IO26         0x06000041
            IMX8QM_MIPI_CSI1_I2C0_SCL_LSIO_GPIO2_IO00   0x06000021
            IMX8QM_LVDS0_I2C0_SCL_LSIO_GPIO1_IO06       0x06000021
        >;
    };

    pinctrl_gpio_test_sleep: gpiotestgrp_sleep {
        fsl,pins = <
            IMX8QM_LVDS0_I2C0_SDA_LSIO_GPIO1_IO07       0x07800061
            IMX8QM_QSPI1A_DATA0_LSIO_GPIO4_IO26         0x07800041
            IMX8QM_MIPI_CSI1_I2C0_SCL_LSIO_GPIO2_IO00   0x07800021
            IMX8QM_LVDS0_I2C0_SCL_LSIO_GPIO1_IO06       0x07800021
        >;
    };

    pinctrl_gpio_example_default: gpioexamplegrp_default {
        fsl,pins = <
            IMX8QM_LVDS0_GPIO01_LSIO_GPIO1_IO05         0x06000021
            IMX8QM_MIPI_CSI1_I2C0_SDA_LSIO_GPIO2_IO01   0x06000021
        >;
    };

    pinctrl_gpio_example_sleep: gpioexamplegrp_sleep {
        fsl,pins = <
            IMX8QM_LVDS0_GPIO01_LSIO_GPIO1_IO05         0x07800021
            IMX8QM_MIPI_CSI1_I2C0_SDA_LSIO_GPIO2_IO01   0x07800021
         >;
    };

Kernel commands to test GPIO function:

gpiodetect and gpioinfo command:

root@imx8qmmek:~# gpiodetect
gpiochip0 [5d080000.gpio] (32 lines)
gpiochip1 [5d090000.gpio] (32 lines)
gpiochip2 [5d0a0000.gpio] (32 lines)
gpiochip3 [5d0b0000.gpio] (32 lines)
gpiochip4 [5d0c0000.gpio] (32 lines)
gpiochip5 [5d0d0000.gpio] (32 lines)
gpiochip6 [5d0e0000.gpio] (32 lines)
gpiochip7 [5d0f0000.gpio] (32 lines)
root@imx8qmmek:~# gpioinfo -c 1
gpiochip1 - 32 lines:
        line   0:       unnamed                 input
        line   1:       unnamed                 input
        line   2:       unnamed                 input
        line   3:       unnamed                 input
        line   4:       unnamed                 input
        line   5:       unnamed                 input
        line   6:       unnamed                 output
        line   7:       unnamed                 output
        line   8:       unnamed                 input
        line   9:       unnamed                 input
        line  10:       unnamed                 input
        line  11:       unnamed                 input
        line  12:       unnamed                 input
        line  13:       unnamed                 output consumer=regulator-pcie
        line  14:       unnamed                 input
        line  15:       unnamed                 input
        line  16:       unnamed                 input
        line  17:       unnamed                 input
        line  18:       unnamed                 input
        line  19:       unnamed                 input
        line  20:       unnamed                 input
        line  21:       unnamed                 input
        line  22:       unnamed                 input
        line  23:       unnamed                 input
        line  24:       unnamed                 input
        line  25:       unnamed                 input
        line  26:       unnamed                 input
        line  27:       unnamed                 input
        line  28:       unnamed                 input
        line  29:       unnamed                 input
        line  30:       unnamed                 input
        line  31:       unnamed                 input

use cat /sys/kernel/debug/gpio to show current GPIO settings before GPIO1-05 and GPIO1-06 are set:

root@imx8qmmek:~# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 512-543, parent: platform/5d080000.gpio, 5d080000.gpio:
 gpio-526 (                    |scl                 ) out lo
 gpio-527 (                    |sda                 ) in  lo

gpiochip1: GPIOs 544-575, parent: platform/5d090000.gpio, 5d090000.gpio:
 gpio-557 (                    |regulator-pcie      ) out hi

gpiochip2: GPIOs 576-607, parent: platform/5d0a0000.gpio, 5d0a0000.gpio:
 gpio-577 (                    |wakeup_key          ) in  hi ACTIVE LOW

gpiochip3: GPIOs 608-639, parent: platform/5d0b0000.gpio, 5d0b0000.gpio:
 gpio-618 (                    |spi1 CS0            ) out hi ACTIVE LOW

gpiochip4: GPIOs 640-671, parent: platform/5d0c0000.gpio, 5d0c0000.gpio:
 gpio-641 (                    |enable              ) out hi ACTIVE LOW
 gpio-643 (                    |regulator-usbotg1-vb) out lo
 gpio-647 (                    |usdhc2-vmmc         ) out hi
 gpio-667 (                    |enable              ) out lo ACTIVE LOW
 gpio-668 (                    |host-wake           ) in  hi ACTIVE LOW
 gpio-669 (                    |PCIe reset          ) out hi ACTIVE LOW

gpiochip5: GPIOs 672-703, parent: platform/5d0d0000.gpio, 5d0d0000.gpio:
 gpio-673 (                    |mux                 ) out hi
 gpio-693 (                    |wp                  ) in  lo
 gpio-694 (                    |cd                  ) in  lo ACTIVE LOW

gpiochip6: GPIOs 704-735, parent: platform/5d0e0000.gpio, 5d0e0000.gpio:

gpiochip7: GPIOs 736-767, parent: platform/5d0f0000.gpio, 5d0f0000.gpio:

use gpioset command to set GPIO output, can also change GPIO direction(input → output)

#set gpiochip1-5 and 1-6
root@imx8qmmek:~# gpioset -c gpiochip1 6=1 &
[1] 700
root@imx8qmmek:~# gpioset -c gpiochip1 5=1 &
[2] 702

check the changes from the commands above with cat /sys/kernel/debug/gpio and gpioinfo -c 1 :

root@imx8qmmek:~# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 512-543, parent: platform/5d080000.gpio, 5d080000.gpio:
 gpio-526 (                    |scl                 ) out lo
 gpio-527 (                    |sda                 ) in  lo

gpiochip1: GPIOs 544-575, parent: platform/5d090000.gpio, 5d090000.gpio:
 gpio-549 (                    |gpioset             ) out hi
 gpio-550 (                    |gpioset             ) out hi
 gpio-557 (                    |regulator-pcie      ) out hi

gpiochip2: GPIOs 576-607, parent: platform/5d0a0000.gpio, 5d0a0000.gpio:
 gpio-577 (                    |wakeup_key          ) in  hi ACTIVE LOW

gpiochip3: GPIOs 608-639, parent: platform/5d0b0000.gpio, 5d0b0000.gpio:
 gpio-618 (                    |spi1 CS0            ) out hi ACTIVE LOW

gpiochip4: GPIOs 640-671, parent: platform/5d0c0000.gpio, 5d0c0000.gpio:
 gpio-641 (                    |enable              ) out hi ACTIVE LOW
 gpio-643 (                    |regulator-usbotg1-vb) out lo
 gpio-647 (                    |usdhc2-vmmc         ) out hi
 gpio-667 (                    |enable              ) out lo ACTIVE LOW
 gpio-668 (                    |host-wake           ) in  hi ACTIVE LOW
 gpio-669 (                    |PCIe reset          ) out hi ACTIVE LOW

gpiochip5: GPIOs 672-703, parent: platform/5d0d0000.gpio, 5d0d0000.gpio:
 gpio-673 (                    |mux                 ) out hi
 gpio-693 (                    |wp                  ) in  lo
 gpio-694 (                    |cd                  ) in  lo ACTIVE LOW

gpiochip6: GPIOs 704-735, parent: platform/5d0e0000.gpio, 5d0e0000.gpio:

gpiochip7: GPIOs 736-767, parent: platform/5d0f0000.gpio, 5d0f0000.gpio:
root@imx8qmmek:~# gpioinfo -c 1
gpiochip1 - 32 lines:
        line   0:       unnamed                 input
        line   1:       unnamed                 input
        line   2:       unnamed                 input
        line   3:       unnamed                 input
        line   4:       unnamed                 input
        line   5:       unnamed                 output consumer=gpioset
        line   6:       unnamed                 output consumer=gpioset
        line   7:       unnamed                 output
        line   8:       unnamed                 input
        line   9:       unnamed                 input
        line  10:       unnamed                 input
        line  11:       unnamed                 input
        line  12:       unnamed                 input
        line  13:       unnamed                 output consumer=regulator-pcie

 


How to enable legacy GPIO SYSFS(default disabled on L6 kernel and above)

refer to the comment of this commit:

https://github.com/nxp-imx/linux-imx/commit/3b4feb21158f873269ff3fbe2fe8d23a88d64b24

commit 3b4feb21158f873269ff3fbe2fe8d23a88d64b24
Author: Linus Walleij 
Date:   Tue Nov 10 15:27:24 2020 +0100

    gpio: sysfs: Enforce character device

    If users select sysfs support they get the character device
    as well so that end-users cannot complain that they
    "only have sysfs on my system". They should have the
    character device at all times.

    If someone is in so dire need of stripping out the
    character device while still enabling the sysfs ABI they
    can very well patch the kernel.

    Also only show this obsolete option to expert users.

    Signed-off-by: Linus Walleij 
    Link: <>

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 5d4de5cd6759..4dd566f7ea39 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -59,8 +59,9 @@ config DEBUG_GPIO
          that are most common when setting up new platforms or boards.

 config GPIO_SYSFS
-       bool "/sys/class/gpio/... (sysfs interface)"
+       bool "/sys/class/gpio/... (sysfs interface)" if EXPERT
        depends on SYSFS
+       select GPIO_CDEV # We need to encourage the new ABI
        help
          Say Y here to add the legacy sysfs interface for GPIOs.

To enable GPIO_SYSFS, from the patch. The best way is CONFIG_EXPERT = y, then enable CONFIG_GPIO_SYSFS=Y. not need to revert the patch.

SYSFS, as legacy interface for GPIO control in Linux. While widely used in the past, this interface has been officially deprecated in favor of the libgpiod character device interface since Linux kernel version 6.0 and later.

i.MX Processors
タグ(1)
評価なし
バージョン履歴
最終更新日:
‎11-27-2025 12:39 PM
更新者: