接到个小需求,按开机键3s后机器开机,要实现这样的功能就需要某个GPIO输出高电平或者低电平去支持硬件电路的实现。所以,就总结下在Linux平台怎么去控制GPIO。

控制GPIO可分为在内核态和用户态控制,在内核态可以写个驱动或者直接在uboot里直接控制。在用户态控制就比较简单,可以通过sysfs子系统控制GPIO。

一、.用户空间控制GPIO

在/sys/class/gpio/下有个export文件,向export文件写入要操作的GPIO号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction和value等,direction控制GPIO输入或者输出模式,而value可控制GPIO的状态或者读取状态。

/sys/class/gpio/目录下各个文件说明:
/sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号;
/sys/class/gpio/unexport 用于通知系统取消导出;
/sys/class/gpio/gpioX/direction文件,可以写入in(设置输入方向)或out(设置输出方向);
/sys/class/gpio/gpioX/value文件是可以读写GPIO状态;
/sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息,包括每个寄存器控制引脚的起始编号,寄存器名称,引脚总数;
其中X表示具体的引脚编号。

举个例子:
要设置编号为7的GPIO,使其输出高电平

echo 7 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio7/direction
echo 1 > /sys/class/gpio/gpio7/value

查看各个GPIO使用的情况

[root@eufy ~]$ cat /sys/kernel/debug/gpio
GPIOs 0-31, platform/pinctrl, gpio0:
 gpio-2   (                    |reset               ) out hi    
 gpio-5   (                    |spk-ctl             ) out lo    
 gpio-7   (                    |sysfs            ) out hi    

GPIOs 32-63, platform/pinctrl, gpio1:
 gpio-42  (                    |sysfs               ) out hi    

GPIOs 64-95, platform/pinctrl, gpio2:

GPIOs 96-127, platform/pinctrl, gpio3:

GPIOs 128-159, platform/pinctrl, gpio4:

如下,编号为7的GPIO就设置成了输出高电平

gpio-7   (                    |sysfs            ) out hi

二、内核空间控制GPIO

内核提供的API介绍

1.向内核申请指定IO

int gpio_request(unsigned gpio, const char *label)

功能:向内核申请指定IO,所申请的IO口会被内核记录。 已经被申请的IO,在没有释放前再次申请将会失败。
参数:
gpio:申请IO口编号;
label:申请者的名字,随便。
返回值:成功:0;失败:负数

2.释放向内核已经申请的指定IO

void gpio_free(unsigned gpio)

功能:释放向内核已经申请的指定IO。所释放的IO口会在内核记录消除。
释放的io可以再次被申请。
参数:所要释放的io口编号。
此函数要和 gpio_request配对使用。

3.设置gpio为输出功能

int gpio_direction_output(unsigned gpio, int value)

功能:指定IO口gpio设置为输出功能,并且输出指定电平value,1代表高电平,0代表低电平。
参数:gpio 指定哪个IO口,这里是io的编号,跟申请的IO编号一致
Value 指定输出电平,高还是低。
返回值:成功返回0;失败返回-1

4.读取gpio电平状态

int gpio_direction_input(unsigned gpio)

功能:读取指定IO口gpio电平状态
参数:gpio的IO口的编号
返回:高电平返回1;低电平返回0。

5.获取IO口的状态函数

int gpio_get_value(unsigned int gpio)

功能:获取IO口的状态
参数:gpio 要获取的IO口编号
返回:IO口状态(高或者低)

6.设置IO口的状态函数

void gpio_set_value(unsigned int gpio, int value)

功能:设置IO口的状态
参数:gpio 要获取的IO口编号 ,
value:要设置的值 ,0或1
返回:无

7.判断IO是否可以休眠函数 (比较少用)

int gpio_cansleep(unsigned int gpio)

功能:判断IO是否可以休眠
参数:gpio要获取的IO口编号
返回:非0,可以休眠;0,不可休眠
8.通过Io口编号转换成对应的外部中断编号函数(很常用 )

int gpio_to_irq(unsigned int gpio)

功能:通过Io口编号转换成对应的外部中断编号
参数:gpio 要获取的IO口编号
返回:成功:中断编号; 失败:- EINVAL

举例,同上述功能一致:

#define GPIO0_A7 7
ret = gpio_request(GPIO0_A7, "gpio0_a7");
if (!ret) {
	printk("request for gpio0_a7 failed:%d\n", ret);
    	return 0;
}
gpio_direction_output(GPIO0_A7,1);

查看GPIO编号为7的状态:

[root@eufy ~]$ cat /sys/kernel/debug/gpio 
GPIOs 0-31, platform/pinctrl, gpio0:
 gpio-2   (                    |reset               ) out hi    
 gpio-5   (                    |spk-ctl             ) out lo    
 gpio-7   (                    |gpio0_a7            ) out hi    

GPIOs 32-63, platform/pinctrl, gpio1:
 gpio-42  (                    |sysfs               ) out hi    

GPIOs 64-95, platform/pinctrl, gpio2:

GPIOs 96-127, platform/pinctrl, gpio3:

GPIOs 128-159, platform/pinctrl, gpio4:

三、查看GPIO全部信息

从上面的介绍来看,不管是在用户态还是在内核态设置GPIO的状态时使用的都是具体的GPIO编号,而有时Linux的CPU原理图是以GPIO0A3形式呈现的,这就有个转换的过程。
查看所有的GPIO信息

[root@eufy /oem]$ cat /sys/kernel/debug/pinctrl/pinctrl/pinmux-pins 
Pinmux settings per pin
Format: pin (name): mux_owner gpio_owner hog?
pin 0 (gpio0-0): wireless-wlan (GPIO UNCLAIMED) function wireless-wlan group wifi-wake-host
pin 1 (gpio0-1): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 2 (gpio0-2): (MUX UNCLAIMED) gpio0:2
pin 3 (gpio0-3): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 4 (gpio0-4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 5 (gpio0-5): (MUX UNCLAIMED) gpio0:5
pin 6 (gpio0-6): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 7 (gpio0-7): (MUX UNCLAIMED) gpio0:7
pin 8 (gpio0-8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 9 (gpio0-9): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 10 (gpio0-10): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 11 (gpio0-11): ff050000.i2c (GPIO UNCLAIMED) function i2c1 group i2c1-xfer
pin 12 (gpio0-12): ff050000.i2c (GPIO UNCLAIMED) function i2c1 group i2c1-xfer
pin 13 (gpio0-13): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 14 (gpio0-14): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 15 (gpio0-15): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 16 (gpio0-16): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 17 (gpio0-17): ff0d0000.serial (GPIO UNCLAIMED) function uart3-m1 group uart3m1-xfer
pin 18 (gpio0-18): ff0d0000.serial (GPIO UNCLAIMED) function uart3-m1 group uart3m1-xfer
pin 19 (gpio0-19): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 20 (gpio0-20): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 21 (gpio0-21): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 22 (gpio0-22): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 23 (gpio0-23): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 24 (gpio0-24): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 25 (gpio0-25): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 26 (gpio0-26): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 27 (gpio0-27): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 28 (gpio0-28): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 29 (gpio0-29): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 30 (gpio0-30): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 31 (gpio0-31): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 32 (gpio1-0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 33 (gpio1-1): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 34 (gpio1-2): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 35 (gpio1-3): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 36 (gpio1-4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 37 (gpio1-5): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 38 (gpio1-6): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 39 (gpio1-7): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 40 (gpio1-8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 41 (gpio1-9): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 42 (gpio1-10): (MUX UNCLAIMED) gpio1:42
pin 43 (gpio1-11): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 44 (gpio1-12): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 45 (gpio1-13): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 46 (gpio1-14): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 47 (gpio1-15): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 48 (gpio1-16): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 49 (gpio1-17): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 50 (gpio1-18): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 51 (gpio1-19): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 52 (gpio1-20): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 53 (gpio1-21): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 54 (gpio1-22): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 55 (gpio1-23): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 56 (gpio1-24): ff0b0000.serial (GPIO UNCLAIMED) function uart1 group uart1-xfer
pin 57 (gpio1-25): ff0b0000.serial (GPIO UNCLAIMED) function uart1 group uart1-xfer
pin 58 (gpio1-26): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 59 (gpio1-27): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 60 (gpio1-28): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 61 (gpio1-29): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 62 (gpio1-30): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 63 (gpio1-31): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 64 (gpio2-0): ff0a0000.serial (GPIO UNCLAIMED) function uart0 group uart0-xfer
pin 65 (gpio2-1): ff0a0000.serial (GPIO UNCLAIMED) function uart0 group uart0-xfer
pin 66 (gpio2-2): ff060000.i2c (GPIO UNCLAIMED) function i2c2 group i2c2-xfer
pin 67 (gpio2-3): ff060000.i2c (GPIO UNCLAIMED) function i2c2 group i2c2-xfer
pin 68 (gpio2-4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 69 (gpio2-5): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 70 (gpio2-6): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 71 (gpio2-7): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 72 (gpio2-8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 73 (gpio2-9): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 74 (gpio2-10): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 75 (gpio2-11): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 76 (gpio2-12): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 77 (gpio2-13): (MUX UNCLAIMED) gpio2:77
pin 78 (gpio2-14): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 79 (gpio2-15): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 80 (gpio2-16): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 81 (gpio2-17): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 82 (gpio2-18): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 83 (gpio2-19): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 84 (gpio2-20): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 85 (gpio2-21): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 86 (gpio2-22): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 87 (gpio2-23): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 88 (gpio2-24): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 89 (gpio2-25): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 90 (gpio2-26): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 91 (gpio2-27): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 92 (gpio2-28): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 93 (gpio2-29): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 94 (gpio2-30): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 95 (gpio2-31): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 96 (gpio3-0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 97 (gpio3-1): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 98 (gpio3-2): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 99 (gpio3-3): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 100 (gpio3-4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 101 (gpio3-5): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 102 (gpio3-6): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 103 (gpio3-7): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 104 (gpio3-8): (MUX UNCLAIMED) gpio3:104
pin 105 (gpio3-9): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 106 (gpio3-10): ff130000.spi (GPIO UNCLAIMED) function spi1 group spi1-miso
pin 107 (gpio3-11): ff130000.spi (GPIO UNCLAIMED) function spi1 group spi1-clk
pin 108 (gpio3-12): ff130000.spi (GPIO UNCLAIMED) function spi1 group spi1-mosi
pin 109 (gpio3-13): ff130000.spi (GPIO UNCLAIMED) function spi1 group spi1-csn0
pin 110 (gpio3-14): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 111 (gpio3-15): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 112 (gpio3-16): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 113 (gpio3-17): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 114 (gpio3-18): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 115 (gpio3-19): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 116 (gpio3-20): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 117 (gpio3-21): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 118 (gpio3-22): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 119 (gpio3-23): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 120 (gpio3-24): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 121 (gpio3-25): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 122 (gpio3-26): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 123 (gpio3-27): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 124 (gpio3-28): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 125 (gpio3-29): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 126 (gpio3-30): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 127 (gpio3-31): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 128 (gpio4-0): ff4a0000.dwmmc (GPIO UNCLAIMED) function sdio group sdio-bus4
pin 129 (gpio4-1): ff4a0000.dwmmc (GPIO UNCLAIMED) function sdio group sdio-bus4
pin 130 (gpio4-2): ff4a0000.dwmmc (GPIO UNCLAIMED) function sdio group sdio-bus4
pin 131 (gpio4-3): ff4a0000.dwmmc (GPIO UNCLAIMED) function sdio group sdio-bus4
pin 132 (gpio4-4): ff4a0000.dwmmc (GPIO UNCLAIMED) function sdio group sdio-cmd
pin 133 (gpio4-5): ff4a0000.dwmmc (GPIO UNCLAIMED) function sdio group sdio-clk
pin 134 (gpio4-6): ff0e0000.serial (GPIO UNCLAIMED) function uart4 group uart4-cts
pin 135 (gpio4-7): wireless-bluetooth gpio4:135 function uart4 group uart4-rts
pin 136 (gpio4-8): ff0e0000.serial (GPIO UNCLAIMED) function uart4 group uart4-xfer
pin 137 (gpio4-9): ff0e0000.serial (GPIO UNCLAIMED) function uart4 group uart4-xfer
pin 138 (gpio4-10): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 139 (gpio4-11): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 140 (gpio4-12): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 141 (gpio4-13): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 142 (gpio4-14): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 143 (gpio4-15): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 144 (gpio4-16): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 145 (gpio4-17): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 146 (gpio4-18): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 147 (gpio4-19): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 148 (gpio4-20): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 149 (gpio4-21): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 150 (gpio4-22): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 151 (gpio4-23): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 152 (gpio4-24): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 153 (gpio4-25): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 154 (gpio4-26): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 155 (gpio4-27): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 156 (gpio4-28): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 157 (gpio4-29): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 158 (gpio4-30): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 159 (gpio4-31): (MUX UNCLAIMED) (GPIO UNCLAIMED)

注意,上面的pin 7 (gpio0-7): (MUX UNCLAIMED) gpio0:7就是例子的gpio0_a7脚(对应原理图上的),编号就是7。换算关系如下(rk3308b为例):

计算gpion_xy的编号:
n=(0,1,2,3,4......)
x=(a,b,c,d)
y=(1,2,3,4,5,6......31)
pin=n*32+(x-1)*8+y

例如:gpio4_b0
pin=4*32+(2-1)*8+0=104
备注:x=(a,b,c,d…)<===> (1,2,3,4…)