Linux Driver : gpio-keys的解析

背景

在阅读高通设备树配置一个按键的时候,没有找到按键是在什么时候进行处理的。因此根据仅有的线索gpio-key.c进行分析,发现根据之前的学习积累,很快就看懂了。

介绍

gpio-keys是基于platform来实现实现的一个通用的GPIO按键驱动,对上可以提供input子系统的event。

源码位置:drivers/input/keyboard/gpio_keys.c

这个文件是硬件无关的,而硬件有关的需要我们自己来注册。

整体流程:

0、指定硬件注册

1、初始化、解析硬件属性

2、注册中断、workqueue

3、处理中断、延迟消抖

过程解析

设备树

随便拿一段设备树来作为例子。

gpio_keys {
compatible = "gpio-keys";
label = "gpio-keys";
pinctrl-names = "default";
pinctrl-0 = <&key_vol_up_default &key_sidekey1
&key_sidekey2 &key_sidekey3 &hall_dev_key>; vol_up {
label = "volume_up";
gpios = <&pm6125_gpios 5 GPIO_ACTIVE_LOW>;
linux,input-type = <1>;
linux,code = <KEY_VOLUMEUP>;
linux,can-disable;
debounce-interval = <15>;
gpio-key,wakeup;
}; userkey1 {
label = "userkey1";
gpios = <&tlmm 107 GPIO_ACTIVE_HIGH>;
linux,input-type = <1>;
linux,code = <KEY_F22>;
linux,can-disable;
debounce-interval = <15>;
gpio-key,wakeup;
};
};

注册

第一步就是在初始化时注册platform_driver:

static const struct of_device_id gpio_keys_of_match[] = {
{ .compatible = "gpio-keys", },
{ },
}; MODULE_DEVICE_TABLE(of, gpio_keys_of_match); static struct platform_driver gpio_keys_device_driver = {
.probe = gpio_keys_probe,
.driver = {
.name = "gpio-keys",
.pm = &gpio_keys_pm_ops,
.of_match_table = gpio_keys_of_match,
}
}; static int __init gpio_keys_init(void)
{
return platform_driver_register(&gpio_keys_device_driver);
}

当发现有设备匹配时(compatible = "gpio-keys"),执行gpio_keys_probe函数。

适配

在适配的时候,就会用到下面的对象。

struct gpio_keys_button_data {
struct gpio_desc *gpiod;
int last_state;
int count;
int threshold;
}; struct gpio_button_data {
const struct gpio_keys_button *button;
struct input_dev *input;
struct gpio_desc *gpiod; unsigned short *code; struct timer_list release_timer;
unsigned int release_delay; /* in msecs, for IRQ-only buttons */ struct delayed_work work;
unsigned int software_debounce; /* in msecs, for GPIO-driven buttons */ unsigned int irq;
unsigned int wakeup_trigger_type;
spinlock_t lock;
bool disabled;
bool key_pressed;
bool suspended;
}; struct gpio_keys_drvdata {
const struct gpio_keys_platform_data *pdata;
struct input_dev *input;
struct mutex disable_lock;
unsigned short *keymap;
struct gpio_button_data data[0];
};

在适配的时候,完成了设备数据的处理以及获取、中断的注册

static int gpio_keys_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
struct fwnode_handle *child = NULL;
struct gpio_keys_drvdata *ddata;
struct input_dev *input;
size_t size;
int i, error;
int wakeup = 0; if (!pdata) {
// 获取设备以及子节点的属性值,并保存下来,用于在后续的功能中使用
pdata = gpio_keys_get_devtree_pdata(dev);
if (IS_ERR(pdata))
return PTR_ERR(pdata);
} size = sizeof(struct gpio_keys_drvdata) +
pdata->nbuttons * sizeof(struct gpio_button_data);
ddata = devm_kzalloc(dev, size, GFP_KERNEL);
if (!ddata) {
return -ENOMEM;
} ddata->keymap = devm_kcalloc(dev,
pdata->nbuttons, sizeof(ddata->keymap[0]),
GFP_KERNEL);
if (!ddata->keymap)
return -ENOMEM; input = devm_input_allocate_device(dev);
if (!input) {
return -ENOMEM;
} ddata->pdata = pdata;
ddata->input = input;
mutex_init(&ddata->disable_lock); platform_set_drvdata(pdev, ddata);
input_set_drvdata(input, ddata); // 输入子系统有关
input->name = pdata->name ? : pdev->name;
input->phys = "gpio-keys/input0";
input->dev.parent = dev;
input->open = gpio_keys_open;
input->close = gpio_keys_close; input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100; input->keycode = ddata->keymap;
input->keycodesize = sizeof(ddata->keymap[0]);
input->keycodemax = pdata->nbuttons; /* Enable auto repeat feature of Linux input subsystem */
if (pdata->rep)
__set_bit(EV_REP, input->evbit); for (i = 0; i < pdata->nbuttons; i++) {
const struct gpio_keys_button *button = &pdata->buttons[i]; if (!dev_get_platdata(dev)) {
child = device_get_next_child_node(dev, child);
if (!child) {
return -EINVAL;
}
}
// 设置按键,注册中断(gpiod_to_irq)、workqueue、
// 设置input子系统有关参数(input_set_capability),还设置了定时器(timer_setup,用于消抖)。
error = gpio_keys_setup_key(pdev, input, ddata,
button, i, child);
if (error) {
fwnode_handle_put(child);
return error;
} if (button->wakeup)
wakeup = 1;
} fwnode_handle_put(child); error = devm_device_add_group(dev, &gpio_keys_attr_group);
if (error) {
return error;
} // 设为唤醒属性(用于电源管理)
device_init_wakeup(dev, wakeup); return 0;
}

中断响应

中断上半文:

static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
{
struct gpio_button_data *bdata = dev_id; if (bdata->button->wakeup) {
const struct gpio_keys_button *button = bdata->button; pm_stay_awake(bdata->input->dev.parent);
if (bdata->suspended &&
(button->type == 0 || button->type == EV_KEY)) {
/*
* Simulate wakeup key press in case the key has
* already released by the time we got interrupt
* handler to run.
*/
// 报告热键
input_report_key(bdata->input, button->code, 1);
}
}
// 延迟、消抖
mod_delayed_work(system_wq,
&bdata->work,
msecs_to_jiffies(bdata->software_debounce)); return IRQ_HANDLED;
}

中断下半文:

static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
{
const struct gpio_keys_button *button = bdata->button;
struct input_dev *input = bdata->input;
unsigned int type = button->type ?: EV_KEY;
int state; // 如果按键在超时时间内已经释放,则返回
state = gpiod_get_value_cansleep(bdata->gpiod);
if (state < 0) {
return;
} if (type == EV_ABS) {
if (state)
input_event(input, type, button->code, button->value);
} else {
input_event(input, type, *bdata->code, state);
}
input_sync(input);
} static void gpio_keys_gpio_work_func(struct work_struct *work)
{
struct gpio_button_data *bdata =
container_of(work, struct gpio_button_data, work.work); gpio_keys_gpio_report_event(bdata); if (bdata->button->wakeup)
pm_relax(bdata->input->dev.parent);
}

Linux Driver : gpio-keys的更多相关文章

  1. linux driver ------ GPIO的驱动编写和调用

    判断哪些文件被编译进内核: 1.通过 make menuconfig 查看 2.比如查看gpio类型的文件,输入 ls drivers/gpio/*.o,有生成.o文件表示被编译进内核 在编写驱动程序 ...

  2. linux 标准 GPIO 操作

    Linux 提供了GPIO 操作的 API,具体初始化及注册函数在 driver/gpio/lib_gpio.c 中实现.   #include    int gpio_request(unsigne ...

  3. Linux driver 板级文件跟踪一般方法

    /*********************************************************************************** * Linux driver ...

  4. linux下GPIO的用户层操作(sysfs)

    linux的GPIO通过sysfs为用户提供服务,下面是linux kernel里的说明文档,学习一下. GPIO Sysfs Interface for Userspace ============ ...

  5. Android(Linux)控制GPIO方法二

    前文<Android(Linux)控制GPIO的方法及实时性分析>主要使用Linux shell命令控制GPIO,该方法可在调试过程中快速确定GPIO硬件是否有问题,即对应的GPIO是否受 ...

  6. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之二

    /** ****************************************************************************** * @author    暴走的小 ...

  7. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之一

    /** ****************************************************************************** * @author    暴走的小 ...

  8. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之三

    /** ****************************************************************************** * @author    暴走的小 ...

  9. OK335xS pwm buzzer Linux driver hacking

    /**************************************************************************** * OK335xS pwm buzzer L ...

  10. linux driver开发

    1 开发linux driver时的调试思路 基本上是打印调试,原因很简单,方便.或者使用工具挂住cpu.

随机推荐

  1. vue-cli快速搭建项目的几个文件(二)

    =======ggcss样式======== :root{     --bgColor : #d3252a;     --pinkColor : #ff4e81;     --textColor :  ...

  2. JavaWeb 中 “转发”与 “重定向”的区别

    JavaWeb 中 "转发"与 "重定向"的区别 每博一文案 人生的常态,就是有聚有散,有得有失,就像山峰一样,总有高低,起伏不断. 曾经,我们是鲜衣怒马的少年 ...

  3. 【爬虫GUI】YouTube评论采集软件,突破反爬,可无限爬取!

    目录 一.背景介绍 1.1 软件说明 1.2 效果演示 二.科普知识 2.1 关于视频id 2.2 关于评论时间 三.爬虫代码 3.1 界面模块 3.2 爬虫模块 3.3 日志模块 四.获取源码及软件 ...

  4. 前端使用 Konva 实现可视化设计器(9)- 另存为SVG

    请大家动动小手,给我一个免费的 Star 吧~ 大家如果发现了 Bug,欢迎来提 Issue 哟~ github源码 gitee源码 示例地址 另存为SVG 这一章增强了另存为的能力,实现" ...

  5. vue安装tinyMCE

    目录 [参考视频] [参考文章] 官网: https://www.tiny.cloud/auth/signup/ 资源下载 tinymce 官方为 vue 项目提供了一个组件tinymce-vue n ...

  6. pageoffice6提取word指定位置(数据区域)的值

    在实际的开发过程中,经常会遇到提取Word文档中指定位置的数据保存到数据库中的需求,PageOffice客户端控件即支持在线保存Word文件,也支持Word文档中的指定位置的数据或所有的数据提交到服务 ...

  7. java启动参考

    启动参数 mvn clean package -Dmaven.test.skip=true -Ptest - java - -server - -Xms2G - -Xmx2G - -Xss256K - ...

  8. 【C#】字符串按条件替换关键字

    private string MyReplace(string json, string keyWord, string newWord, Func<string, string, bool&g ...

  9. 阿里面试:NIO为什么会导致CPU100%?

    在 Java 中总共有三种 IO 类型:BIO(Blocking I/O,阻塞I/O).NIO(Non-blocking I/O,非阻塞I/O)和 AIO(Asynchronous I/O,异步I/O ...

  10. 使用 TiDB Vector 搭建 RAG 应用 - TiDB 文档问答小助手

    本文首发至TiDB社区专栏:https://tidb.net/blog/7a8862d5 前言 继上一次<TiDB Vector抢先体验之用TiDB实现以图搜图>后,就迫不及待的想做一些更 ...