Linux device tree 基础知识不补充,自行查找。。。。。。
root@yex-Ubuntu:/home/work/hiSDK/hi3516ev200/hi3516/linux-4.9.y# ls Documentation/devicetree/bindings/input/gpio-keys.txt
Documentation/devicetree/bindings/input/gpio-keys.txt
1.在内核文件目录下搜索关于HI3516EV200 的dts 文件:
arch/arm/boot/dts/
hi3516ev200-demb.dtb hi3516ev200-demb.dts hi3516ev200.dtsi
有一个*.dtb文件明显设备用到了device tree
2.添加gpio6.4 在hi3516ev200-demb.dts 文件中
20 /dts-v1/;
21 #include "hi3516ev200.dtsi"
22
23 / {
24 model = "Hisilicon HI3516EV200 DEMO Board";
25 compatible = "hisilicon,hi3516ev200";
26
27 memory {
28 device_type = "memory";
29 reg = <0x40000000 0x20000000>;
30 };
31
***32 gpio-keys {
33 compatible = "gpio-keys";
34
35 call {
36 label = "gpio-call";
37 linux,code = <0x101>;
38 gpios = <&gpio_chip6 4 0>;
39 debounce-interval = <200>;
40 };
41
42 };***
43
44 };
45
46 &uart0 {
47 status = "okay";
48 };
直接编译内核镜像 uImage 后下载到target板上运行,会看到
/sdcard/bin # cat /sys/firmware/devicetree/base/gpio-keys/call/label
gpio-call
/sdcard/bin #
证明添加成功。
3.在内核驱动drivers目录下找到一个用 input 驱动模式编写的gpio 驱动模板
drivers/input/keyboard/gpio_keys.c
将其copy在某个自建目录下/home/work,并在该目录下建立Makefile 文件,编辑内容:
ifneq ($(KERNELRELEASE),)
obj-m:=gpio_keys.o
else
#generate the path
CURRENT_PATH:=$(shell pwd)
#the absolute path
#LINUX_KERNEL_PATH:=/lib/modules/$(shell uname -r)/build
LINUX_KERNEL_PATH:=/home/work/hiSDK/hi3516ev200/hi3516/linux-4.9.y
TOOLCHAIN="/opt/hisi-linux/x86-arm/arm-himix100-linux/bin/arm-himix100-linux-"
#complie object
default:
make -C $(LINUX_KERNEL_PATH) ARCH=arm CROSS_COMPILE=$(TOOLCHAIN) M=$(CURRENT_PATH) modules
clean:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
endif
4.在gpio_keys.c中几乎无需修改,只要加入DBUG 的log测试下
make 建立驱动模块
gpio_keys.ko
root@yex-Ubuntu:/home/work/hiSDK/linux/IPC_APP/drv-module/gpio# make
make -C /home/work/hiSDK/hi3516ev200/hi3516/linux-4.9.y ARCH=arm CROSS_COMPILE="/opt/hisi-linux/x86-arm/arm-himix100-linux/bin/arm-himix100-linux-" M=/home/work/hiSDK/linux/IPC_APP/drv-module/gpio modules
make[1]: Entering directory '/home/work/hiSDK/hi3516ev200/hi3516/linux-4.9.y'
CC [M] /home/work/hiSDK/linux/IPC_APP/drv-module/gpio/gpio_keys.o
Building modules, stage 2.
MODPOST 1 modules
LD [M] /home/work/hiSDK/linux/IPC_APP/drv-module/gpio/gpio_keys.ko
make[1]: Leaving directory '/home/work/hiSDK/hi3516ev200/hi3516/linux-4.9.y'
将驱动模块下载到target板上
~ # cd /sdcard/
/sdcard # cd bin/
/sdcard/bin # tftp -l gpio_keys.ko -r gpio_keys.ko -g 192.168.9.10 69
gpio_keys.ko 100% |*******************************| 16516 0:00:00 ETA
/sdcard/bin # insmod gpio_keys.ko
Debug button->desc: gpio-call, gpio: 52, code: 101, irq: 95, debounce_interval: 200
input: gpio-keys as /devices/platform/gpio-keys/input/input0
应用hexdump 命令打开/dev/input/event0后,并将gpio脚与gnd短接,将出现采集采集信号的动作信息
/sdcard/bin # hexdump /dev/input/event0
Debug gpio_keys_open
0000000 0024 0000 4d2d 0000 0001 0101 0001 0000
0000010 0024 0000 4d2d 0000 0000 0000 0000 0000
Debug gpio_keys_gpio_isr
Debug gpio_keys_gpio_work_func
0000020 0036 0000 4a09 0002 0001 0101 0000 0000
0000030 0036 0000 4a09 0002 0000 0000 0000 0000
Debug gpio_keys_gpio_isr
Debug gpio_keys_gpio_isr
Debug gpio_keys_gpio_work_func
Debug gpio_keys_gpio_isr
Debug gpio_keys_gpio_work_func
0000040 003e 0000 8285 0003 0001 0101 0001 0000
0000050 003e 0000 8285 0003 0000 0000 0000 0000
5.系统默认添加驱动
打开/drivers/input/keyboard/Makefile可找到obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o
vi drivers/input/keyboard/Makefile
查找CONFIG_KEYBOARD_GPIO (或者自添加)
vi drivers/input/keyboard/Kconfig
可以看到:KEYBOARD_GPIO 设置
203 config KEYBOARD_GPIO
204 tristate "GPIO Buttons"
205 depends on GPIOLIB || COMPILE_TEST
206 help
207 This driver implements support for buttons connected
208 to GPIO pins of various CPUs (and some other chips).
209
210 Say Y here if your device has buttons connected
211 directly to such GPIO pins. Your board-specific
212 setup logic must also provide a platform device,
213 with configuration data saying which GPIOs are used.
214
215 To compile this driver as a module, choose M here: the
216 module will be called gpio_keys.
所以打开设置
make menuconfig
Device Drivers --->
Input device support --->
[*] Keyboards --->
<*> GPIO Buttons
make uImage
更新target板目标镜像
检测
~ # hexdump /dev/input/event0
0000000 0014 0000 84e8 0006 0001 0101 0001 0000
0000010 0014 0000 84e8 0006 0000 0000 0000 0000
random: fast init done
0000020 002e 0000 394a 000a 0001 0101 0000 0000
0000030 002e 0000 394a 000a 0000 0000 0000 0000
0000040 0031 0000 f431 000e 0001 0101 0001 0000
~ # cat /proc/bus/input/devices
I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="gpio-keys"
P: Phys=gpio-keys/input0
S: Sysfs=/devices/platform/gpio-keys/input/input0
U: Uniq=
H: Handlers=event0
B: PROP=0
B: EV=3
B: KEY=2 0 0 0 0 0 0 0 0
Break your leg。。。。。。