Linux GPIO 编号如何获得它们的值? [英] How do Linux GPIO numbers get their values?

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

问题描述

我试图了解 Linux GPIO 编号如何获得它们的值.例如Joule 的 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 移动到动态方案,因此对用户来说毫无意义.相反,引脚的标签(如果有)或一对带有相对编号的 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.

因此,Joule 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 这是带有 ACPI _HID INT34D1 和 _UID 1 的 GPIO 控制器上的引脚 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.

So, the interesting pair is: 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
            }
...

在这里您可以通过完整路径看到对 Device 对象的引用,即 \_SB.GPO2.

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 库和子系统的更多详细信息可以在 GPIO 内核文档.

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

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

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