rk3288 gpio


原文链接: rk3288 gpio

linux/gpio.txt 中文文档
write: /sys/class/gpio/export: device or resource busy kidoman/embd
Configuration
The RK3288 bank/bit notation for GPIOs must be formed as "GPIO_"
RK3288 GIPIO 管脚计算公式: 32 x (gpio_bank) + gpio_bit - 8
Group GPIO0 only 24 gpios,So each GPIO Num subtracts 8.

gpio_bank : cat /sys/kernel/debug/gpio

GPIO0 0-23, platform/pinctrl, gpio0: gpiochip0
GPIO1 24-55, platform/pinctrl, gpio1: gpiochip24
GPIO2 56-87, platform/pinctrl, gpio2: gpiochip56
GPIO3 88-119, platform/pinctrl, gpio3: gpiochip88
GPIO4 120-151, platform/pinctrl, gpio4: gpiochip120
GPIO5 152-183, platform/pinctrl, gpio5: gpiochip152
GPIO6 184-215, platform/pinctrl, gpio6: gpiochip184
GPIO7 216-247, platform/pinctrl, gpio7: gpiochip216
GPIO8 248-263, platform/pinctrl, gpio8: gpiochip248

gpio_bit : 0-7 8-15 16-23 24-31

E.g. GPIO2_A4 => 56+4= 60

RSB-4680

GPIO Number GPIO formed Numeric Representation
GPIO0 GPIO7_A3 219
GPIO1 GPIO7_A4 220
GPIO2 GPIO7_A5 221
GPIO3 GPIO7_C5 237
GPIO4 GPIO8_A2 250
GPIO5 GPIO8_A3 251
GPIO6 GPIO8_A0 248
GPIO7 GPIO8_A1 249

Export GPIO then you can use control GPIO from userr space through sysfs
/sys/class/gpio/gpio0/

"direction" ... 读取得到 "in" 或 "out"。这个值通常运行写入。
    写入"out" 时,其引脚的默认输出为低电平。为了确保无故障运行,
    "low" 或 "high" 的电平值应该写入 GPIO 的配置,作为初始输出值。

    注意:如果内核不支持改变 GPIO 的方向,或者在导出时内核代码没有
    明确允许用户空间可以重新配置 GPIO 方向,那么这个属性将不存在。

"value" ... 读取得到 0 (低电平) 或 1 (高电平)。如果 GPIO 配置为
    输出,这个值允许写操作。任何非零值都以高电平看待。

    如果引脚可以配置为中断信号,且如果已经配置了产生中断的模式
    (见"edge"的描述),你可以对这个文件使用轮询操作(poll(2)),
    且轮询操作会在任何中断触发时返回。如果你使用轮询操作(poll(2)),
    请在 events 中设置 POLLPRI 和 POLLERR。如果你使用轮询操作
    (select(2)),请在 exceptfds 设置你期望的文件描述符。在
    轮询操作(poll(2))返回之后,既可以通过 lseek(2)操作读取
    sysfs 文件的开始部分,也可以关闭这个文件并重新打开它来读取数据。

"edge" ... 读取得到“none”、“rising”、“falling”或者“both”。
    将这些字符串写入这个文件可以选择沿触发模式,会使得轮询操作
    (select(2))在"value"文件中返回。

    这个文件仅有在这个引脚可以配置为可产生中断输入引脚时,才存在。

"active_low" ... 读取得到 0 (假) 或 1 (真)。写入任何非零值可以
    翻转这个属性的(读写)值。已存在或之后通过"edge"属性设置了"rising"
    和 "falling" 沿触发模式的轮询操作(poll(2))将会遵循这个设置。

Export GPIO
echo 219 > /sys/class/gpio/export

Set GPIO direction to in/out
echo "out" > /sys/class/gpio/gpio219/direction
"in" :
"out" :
Set GPIO value 0/1 if GPIO pin define is output
echo 1 > /sys/class/gpio/gpio219/value

Used as IRQ signal
Note:You have to configure GPIO to input

echo "rising" > /sys/class/gpio/gpio219/edge
rising: Trigger on rising edge
falling: Trigger on falling edge
both: Trigger on both edges
none: Disable interrupt on both edges

Unexport GPIO0
echo 219 > /sys/class/gpio/unexport
Test
GPIO 219 and GPIO 220 are taken as an example:

Connect GPIO 219 and GPIO 220
Export GPIO 219 and GPIO 220

echo 219 > /sys/class/gpio/export

echo 220 > /sys/class/gpio/export

Set GPIO 219 to output

echo "out" > /sys/class/gpio/gpio219/direction

Set GPIO 220 to input

echo "in" > /sys/class/gpio/gpio220/direction

Change GPIO 219 to 1 and read GPIO 220 value

echo 1 > /sys/class/gpio/gpio219/value

cat /sys/class/gpio/gpio220/value

1
Change GPIO 219 to 0 and read GPIO 220 value

echo 0 > /sys/class/gpio/gpio219/value

cat /sys/class/gpio/gpio220/value

0

检测 rk3288 可导出引脚

for i in {1..250}; do echo $i>export;done
export gpio134 gpio172 gpio203 gpio238 gpio45 gpio79
gpio1 gpio135 gpio173 gpio204 gpio239 gpio46 gpio8
gpio100 gpio136 gpio174 gpio205 gpio24 gpio47 gpio80
gpio101 gpio137 gpio175 gpio206 gpio240 gpio48 gpio81
gpio103 gpio138 gpio176 gpio207 gpio241 gpio49 gpio82
gpio104 gpio140 gpio177 gpio208 gpio242 gpio50 gpio83
gpio105 gpio141 gpio178 gpio209 gpio243 gpio51 gpio84
gpio106 gpio142 gpio179 gpio21 gpio244 gpio52 gpio85
gpio107 gpio143 gpio180 gpio210 gpio245 gpio53 gpio86
gpio108 gpio144 gpio181 gpio211 gpio246 gpio54 gpio87
gpio109 gpio145 gpio182 gpio212 gpio247 gpio55 gpio88
gpio110 gpio147 gpio183 gpio213 gpio248 gpio56 gpio89
gpio111 gpio15 gpio184 gpio214 gpio25 gpio57 gpio9
gpio112 gpio150 gpio185 gpio215 gpio26 gpio58 gpio90
gpio113 gpio152 gpio186 gpio216 gpio27 gpio59 gpio91
gpio114 gpio153 gpio187 gpio217 gpio28 gpio6 gpio92
gpio115 gpio154 gpio188 gpio218 gpio29 gpio60 gpio93
gpio116 gpio155 gpio189 gpio22 gpio3 gpio61 gpio94
gpio117 gpio156 gpio19 gpio220 gpio30 gpio62 gpio95
gpio118 gpio157 gpio190 gpio221 gpio31 gpio63 gpio96
gpio119 gpio158 gpio191 gpio223 gpio32 gpio64 gpio97
gpio120 gpio159 gpio192 gpio224 gpio33 gpio65 gpio98
gpio121 gpio16 gpio193 gpio225 gpio34 gpio66 gpio99
gpio122 gpio160 gpio194 gpio226 gpio35 gpio67 gpiochip0
gpio123 gpio161 gpio195 gpio228 gpio36 gpio68 gpiochip120
gpio124 gpio162 gpio196 gpio229 gpio37 gpio69 gpiochip152
gpio125 gpio163 gpio197 gpio23 gpio38 gpio70 gpiochip184
gpio126 gpio164 gpio198 gpio230 gpio39 gpio72 gpiochip216
gpio128 gpio166 gpio199 gpio232 gpio4 gpio73 gpiochip24
gpio129 gpio167 gpio2 gpio233 gpio40 gpio74 gpiochip248
gpio130 gpio168 gpio20 gpio234 gpio41 gpio75 gpiochip56
gpio131 gpio17 gpio200 gpio235 gpio42 gpio76 gpiochip88
gpio132 gpio170 gpio201 gpio236 gpio43 gpio77 unexport
gpio133 gpio171 gpio202 gpio237 gpio44 gpio78

比如我要操作GPIO8_A6作为高电平输出有效, 那么有以下问题:

  1. 对应的gpio number是多少呢?
  2. 如何设置成输出
  3. 如何输出高电平

问题1:
可以通过/sys/kernel/debug/gpio查询信息:
root@rk3288:/sys/kernel/debug # cat gpio
//snip
GPIOs 184-215, platform/ff770000.pinctrl, gpio6:
gpio-193 (? ) in hi
gpio-194 (? ) in hi

GPIOs 216-247, platform/ff770000.pinctrl, gpio7:
gpio-218 (enable ) out hi
gpio-219 (lcd_en ) in hi
gpio-220 (lcd_cs ) in hi
gpio-221 (gslX680 wake pin ) out hi
gpio-222 (gslX680 irq pin ) out lo
gpio-223 (headset_gpio ) in hi
gpio-233 (? ) in hi
gpio-234 (? ) in hi

GPIOs 248-279, platform/ff770000.pinctrl, gpio8:

GPIOs 280-311, platform/ff770000.pinctrl, gpio15:

可以看到gpio8是以nubmer为248开始, 那么GPIO8_A6就是 248 + 6 = 254
接下来就可以导出gpio了
root@rk3288:/sys/class/gpio # echo 254 > export
root@rk3288:/sys/class/gpio # ls
export
gpio254
......

问题2
root@rk3288:/sys/class/gpio/gpio254 # echo out > direction
root@rk3288:/sys/class/gpio/gpio254 # cat direction
out

问题3:
root@rk3288:/sys/class/gpio/gpio254 # echo 1 > value
root@rk3288:/sys/class/gpio/gpio254 # cat value
1

GPIOs 0-23, platform/pinctrl, gpio0:
 gpio-5   (                    |GPIO Key Power      ) in  hi
 gpio-7   (                    |irq-gpio            ) in  hi
 gpio-10  (                    |hp_ctl_gpio         ) out lo
 gpio-11  (                    |vcc28_dvp           ) out hi
 gpio-12  (                    |vcc_otg_5v          ) out hi
 gpio-13  (                    |vcc_sys_5v          ) out hi
 gpio-14  (                    |vcc_host_5v         ) out hi
 gpio-18  (                    |reset-gpio          ) out hi

GPIOs 24-55, platform/pinctrl, gpio1:
 gpio-48  (                    |sysfs               ) in  lo

GPIOs 56-87, platform/pinctrl, gpio2:
 gpio-71  (                    |?                   ) in  hi

GPIOs 88-119, platform/pinctrl, gpio3:
 gpio-102 (                    |power-gpio          ) out hi

GPIOs 120-151, platform/pinctrl, gpio4:
 gpio-127 (                    |mdio-reset          ) out hi
 gpio-139 (                    |bt_default_rts      ) out lo
 gpio-146 (                    |bt_default_wake     ) out hi
 gpio-148 (                    |reset               ) out hi
 gpio-149 (                    |bt_default_reset    ) out hi
 gpio-151 (                    |bt_default_wake_host) out hi

GPIOs 152-183, platform/pinctrl, gpio5:
 gpio-165 (                    |cs-gpio             ) out hi
 gpio-169 (                    |enable              ) out hi

GPIOs 184-215, platform/pinctrl, gpio6:

GPIOs 216-247, platform/pinctrl, gpio7:
 gpio-219 (                    |vcc_lcd             ) out hi
 gpio-222 (                    |?                   ) out hi
 gpio-227 (                    |vcc_sd              ) out hi
 gpio-231 (                    |?                   ) in  lo

GPIOs 248-263, platform/pinctrl, gpio8:
 gpio-249 (                    |?                   ) out hi
 gpio-250 (                    |?                   ) out lo
`