Linux GPIO号如何获得其值? [英] How do Linux GPIO numbers get their values?

查看:782
本文介绍了Linux GPIO号如何获得其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Linux GPIO数字如何获得其值. 例如焦耳的GPIO映射.

I am trying to understand how the Linux GPIO numbers get their values. e.g. GPIO mapping for Joule.

我尝试阅读Pinctrl子系统上的linux文档,还查看了Intel Joule中使用的GPIO驱动程序的代码: https://elixir.bootlin.com/linux/latest/source/drivers/pinctrl/intel/pinctrl-broxton.c

I tried reading linux documentation on Pinctrl Subsystem and also looked at the code of GPIO driver being used in Intel Joule : https://elixir.bootlin.com/linux/latest/source/drivers/pinctrl/intel/pinctrl-broxton.c

但是,这种方式看起来非常特定于平台.我正在寻找一些通用的行业标准.请帮忙,或引导我阅读一些好文章.

However going this way looks very platform-specific. I am looking for some generic industry standard. Please help or please direct me to some good article.

推荐答案

首先,必须获得全局系统GPIO编号(GSGN)与特定GPIO控制器之间的区别.早在GPIO描述符时代之前,就已经使用GSGN.在切换到描述符方案之后,编号方案从(半)静态GSGN移到了动态GSGN,因此对用户来说毫无意义.取而代之的是使用了引脚的标签(如果有)或一对带有相对编号的GPIO控制器句柄.这是由资源提供者(例如ACPI和设备树)决定的.如果出于某种原因,用户想要获取控制器和相对编号的对,则 libgpiod 库和工具为实现这一目标提供了可能性.

First of all, one has to get the difference between Global System GPIO number (GSGN) and relative to the certain GPIO controller. Earlier, before the era of GPIO descriptors, the GSGN had been used. After switching to the descriptor scheme the numbering scheme moves from (semi-)static GSGN to dynamic one and thus makes nonsense to the user. Instead the label of the pin, if any, or a pair of GPIO controller handle with a number relative to it became in use. This is being dictated by the resource providers, such as ACPI and Device Tree. If, by some reason, user wants to get the pair of the controller and relative number, the libgpiod library and tools gives the possibility to achieve this.

因此,到焦耳GPIO编号方案的链接确实很脆弱,用户不应该知道GSGN.有几种方法可以获取正在运行的系统上的控制器和相对编号.但通常它们都与驱动程序和ACPI表相关,并且不需要任何用户参与.

So, the link to Joule GPIO numbering scheme is really fragile, users are not suppose to know the GSGN. There are ways how to get the controller and relative number on a running system. But usually it's all related to the driver and ACPI tables and doesn't require any user involvement.

示例:

请考虑引脚UART_1_TXD(由于某种原因,该引脚在该文档中的命名错误,应为LPSS_UART1_TXD).根据 pinctrl-broxton.c 这是GPIO控制器上具有ACPI _HID INT34D1 和_UID 1 的引脚43.

Take into consideration the pin UART_1_TXD (by some reason it's named in that document wrongly, should be LPSS_UART1_TXD). According to pinctrl-broxton.c this is pin 43 on a GPIO controller with ACPI _HID INT34D1 and _UID 1.

列出所有枚举的GPIO控制器(可选步骤):

List all enumerated GPIO controllers (optional step):

# gpiodetect 
gpiochip0 [INT34D1:00] (83 lines)
gpiochip1 [INT34D1:01] (72 lines)
gpiochip2 [INT34D1:02] (42 lines)
gpiochip3 [INT34D1:03] (31 lines)
gpiochip4 [INT34D1:04] (20 lines)

使用_UID 1 查找一个:

Find one with _UID 1:

# grep -w 1 /sys/bus/acpi/devices/INT34D1\:0*/uid
/sys/bus/acpi/devices/INT34D1:00/uid:1

# gpiodetect | grep -w INT34D1:00
gpiochip0 [INT34D1:00] (83 lines)

所以,有趣的对是:gpiochip0 43.

在实际的资源提供程序中,它看起来像这样(摘自 meta-acpi 项目):

In the actual resource provider it will look like this (taken from meta-acpi project):

...
*   pin name           pin number   led
*   -----------------------------------------
*   ISH_GPIO_0_LS      35           heartbeat
*   ISH_GPIO_1_LS      33           sd-card
*   ISH_GPIO_2_LS      31           wifi
*   ISH_GPIO_3_LS      29           led-3
...
            GpioIo (
...
                "\\_SB.GPO2",               // GPIO controller
                0)                          // Must be 0
            {
                22,                         // ISH_GPIO_0_LS
                23,                         // ISH_GPIO_1_LS
                24,                         // ISH_GPIO_2_LS
                25                          // ISH_GPIO_3_LS
            }
...

在这里您看到通过完整路径(即 \ _ SB.GPO2 )对 Device 对象的引用.

Here you see the reference to the Device object thru a full path, i.e. \_SB.GPO2.

您可能会在 meta-acpi 项目中找到更多示例.

You may find more examples in meta-acpi project.

如果发生任何奇怪的情况,用户确实想要一个无意义的数字,那就是这样:

If by any weird case user really wants a non-sense number, this is the way:

# mount -t debugfs none /sys/kernel/debug/

# cat /sys/kernel/debug/pinctrl/INT34D1\:00/gpio-ranges 
GPIO ranges handled:
0: INT34D1:00 GPIOS [429 - 460] PINS [0 - 31]
32: INT34D1:00 GPIOS [461 - 492] PINS [32 - 63]
64: INT34D1:00 GPIOS [493 - 511] PINS [64 - 82]

# echo $((43-32+461))
472

有关GPIO库和子系统的更多详细信息,请参见

More details about GPIO library and subsystem can be found in GPIO in-kernel documentation.

这篇关于Linux GPIO号如何获得其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆