首页 > 系统相关 > Linux驱动开发十六.input系统——3.系统自带的input驱动

Linux驱动开发十六.input系统——3.系统自带的input驱动

时间:2022-08-28 23:34:58浏览次数:45  
标签:keys button Linux 驱动 input gpio 设备

前面两章我们通过input子系统构建了一个按键类型的输入设备的驱动,其实Linux的内核还提供了一套基于GPIO的按键驱动程序,和LED设备一样,我们只需要在编译内核的过程中进行配置然后在设备树中定义好设备节点就可以直接使用了。

配置内核

在使用内核提供的input子系统驱动前要将驱动使能,可以按照下面说明中的路径进行配置

 从上面的介绍可以看到,这个配置在.config里时CONFIG_KEYBOARD_GPIO,在该配置被使能以后,.config文件下的这个配置值就会变成y

并且可以从定位下面哪行Defined看到具体的配置(drivers/input/keyboard/Kconfig)

 

 并且在drivers/input/keyboard路径下的Makefile里,可以看到我们配置的CONFIG_KEYBOARD_GPIO对应的规则文件

 

 也就是说我们需要关注的就是gpio_keys.c这个文件。

从这个gpio_keys.c文件里,我们可以看出来,其实这个驱动也是一个platform驱动,和platform_dev设备的匹配规则也可以在文件中找到

 

上面就是platform_driver的定义,在没有设备树的情况下我们可以直接定义一个platform设备,设备的name属性的值就是gpio-keys。如果在设备树下,匹配的规则要去找of_match_table里面的gpio_keys_of_match变量

 

也就是说,我们这个设备树的节点必须是gpio-keys。

设备树配置

设备树的配置我们可以先看下参考文档Documentation/devicetree/bindings/input/gpio-keys.txt

Device-Tree bindings for input/gpio_keys.c keyboard driver

Required properties:
    - compatible = "gpio-keys";

Optional properties:
    - autorepeat: Boolean, Enable auto repeat feature of Linux input
      subsystem.

Each button (key) is represented as a sub-node of "gpio-keys":
Subnode properties:

    - gpios: OF device-tree gpio specification.
    - interrupts: the interrupt line for that input.
    - label: Descriptive name of the key.
    - linux,code: Keycode to emit.

Note that either "interrupts" or "gpios" properties can be omitted, but not
both at the same time. Specifying both properties is allowed.

Optional subnode-properties:
    - linux,input-type: Specify event type this button/key generates.
      If not specified defaults to <1> == EV_KEY.
    - debounce-interval: Debouncing interval time in milliseconds.
      If not specified defaults to 5.
    - gpio-key,wakeup: Boolean, button can wake-up the system.
    - linux,can-disable: Boolean, indicates that button is connected
      to dedicated (not shared) interrupt which can be disabled to
      suppress events from the button.

Example nodes:

    gpio_keys {
            compatible = "gpio-keys";
            #address-cells = <1>;
            #size-cells = <0>;
            autorepeat;
            button@21 {
                label = "GPIO Key UP";
                linux,code = <v103>;
                gpios = <&gpio1 0 1>;
            };
            button@22 {
                label = "GPIO Key DOWN";
                linux,code = <108>;
                interrupts = <1 IRQ_TYPE_LEVEL_HIGH 7>;
            };
            ...

上面的范例已经给的很清楚了,我们只需要按照上面的套路,去完善设备树就可以了。                                                                                                                                                                                                                                                                                                         

gpio_keys {
    compatible = "gpio-keys";
    #address-cells = <1>;
    #cell-cells = <0>;
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_key>;

    key0 {
        label = "GPIO KEY Enter";
        linux,code = <KEY_ENTER>;
        gpios = <&gpio1 18 GPIO_ACTIVE_LOW>;
    };
};

我们把按键的键值模拟成回车键,因为在下面我们要做的驱动是LCD屏幕,但是屏幕在一定时间后会熄屏,在没有键盘的时候就需要用到按键模仿回车键去重新点亮屏幕。

编译设备树,用新的设备树文件启动开发板,可以在dev/input路径下看到有两个event文件,具体哪个文件是咱们的按键设备不一定,只能通过hexdump挨个试验一下

 

运行指令以后按下按键,看看有没有信息打印出来。如果有数据打印,就是这个文件。KEY_ENTER的值是28

#define KEY_ENTER        28

换算下来就是0x1C,和打印出来的数据一致(倒数第3列)。

这样就用到了内核自带的按键驱动。但是现在还不能像回车键一样使用按键,后面再看看吧!

标签:keys,button,Linux,驱动,input,gpio,设备
From: https://www.cnblogs.com/yinsedeyinse/p/16632591.html

相关文章

  • Linux学习笔记3——vi和vim编辑器
    Linux学习笔记3——vi和vim编辑器一、vi和vim编辑器:vi和vim是Linux中的文本编辑器,用来在Linux中创建、查看或编辑文本文件,就好像window系统的记事本一样,但是不能查看图片......
  • Linux学习笔记2——目录结构
    Linux学习笔记2——目录结构一、Linux的目录结构:Linux只有一个根目录:/层级式的目录结构:1)root:该目录为系统管理员目录,root是具有超级权限的用户。2)bin->usr/bin:存放系......
  • Linux Kernel in a Nutshell - 7
    CustomizingaKernel原文链接我的博客以·问题·做关键字搜索,还有问题构建你自己的Linux内核版本最困难的部分,应该就是确定哪一个驱动以及配置选项是你的设备需要的......
  • Linux Kernel in a Nutshell - 8
    KernelConfigurationRecipes原文链接我的博客前面介绍了重新配置内核的机制,本章介绍制作自己的内核通常会遇到的那些问题,并给出对应指令来处理它。DisksLinux内核......
  • Linux修改主机静态IP
    通过VIM编辑器打开主机配置文件夹vim/etc/sysconfig/network-scripts/ifcfg-ens33修改IP地址为静态地址BOOTPROTO="static"添加静态IP地址和网关IP地址......
  • Linux上安装并启动tomcat
    1、下载tomcat安装包官网链接:https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.57/bin/一般选择 2、将tomcat上传到Linux服务器网上有很多文件传输工具,我使用......
  • 2.Linux相关基础操作
    1.用户操作1.添加用户useraddaaa2.指定目录useradd-d/home/aaaaaa3.删除用户userdelaa4.删除用户及目录userdel-raaa5.查看用户信息idaa6.切换用户su......
  • 3.Linux更新数据源
    在一个没有安装vim等命令的Linux环境中,没办法更新数据源,没办法软件的安装等的解决方案:编辑数据源vi/etc/apt/sources.list删除全部内容并修改为debhttp://mirrors......
  • Linux上安装jdk 1.8
    1、下载jdk1.8这里贴个oracle官网链接 https://www.oracle.com/java/technologies/downloads/ 里面的jdk版本基本都有2、将压缩包上传到Linux服务器中我使用的是wi......
  • linux-grep管道命令
    grep命令文件过滤分割与合并grep(globalsearchregularexpression(RE)andprintouttheline,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正......