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. EasyRepro与测试自动化( 一) 概览

    EasyRepro是一个框架,允许在特定的Dynamics 365组织上执行自动化UI测试.你可以使用它来自动化冒烟测试.回归测试和负载测试等.该框架是由开源项目Selenium构建的,Seleniu ...

  2. 开源相机管理库Aravis例程学习(六)——camera-features

    目录 简介 例程代码 函数说明 arv_camera_get_integer arv_camera_get_string 简介 本文针对官方例程中的:04-camera-features做简单的讲解. ...

  3. 钉消息Markdown语法

    支持的Markdown语法 1 标题 2 # 一级标题 3 ## 二级标题 4 ### 三级标题 5 #### 四级标题 6 ##### 五级标题 7 ###### 六级标题 8 9 引用 10 &g ...

  4. IPD、CMMI、敏捷

    华为公司早在2009年正式发文在全公司现在流程IPD.CMMI的基础上,所有产品线的软件开发团队全面推行敏捷开发.除了华为之外,不仅是互联网企业,现在凡是涉及到软件开发的企业对敏捷都不陌生,那么IPD ...

  5. 模型评测-书生浦语大模型实战营学习笔记7&大语言模型12

    大语言模型学习-12.模型评测 书生浦语大模型实战营学习笔记7 视频教程特别像广告,所以这篇博客参考了很多其他内容给大家参考,主要是下面几个页面: https://zhuanlan.zhihu.com ...

  6. Sermant在异地多活场景下的实践

    本文分享自华为云社区<Sermant在异地多活场景下的实践>,作者:华为云开源. Sermant社区在1.3.0和1.4.0版本相继推出了消息队列禁止消费插件和数据库禁写插件,分别用于解决 ...

  7. postgresql 去重&查最新一组记录 关键词partition by

  8. Python语言:散修笔记

    文章目录 前言 转义字符的使用 原字符 变量的定义 类型转换 注释 接收用户信息 运算规则 整除运算 幂运算 比较运算符 布尔运算 运算优先级 对象的布尔值 if else elif分支结构 条件表达 ...

  9. AIRIOT答疑第5期|如何使用低代码业务流引擎?

    推拉拽! AIRIOT平台业务流引擎可创建丰富的业务流程,实现从流程定义.数据处理.任务工单.消息通知.日志追踪的闭环流转.多类型节点任意组合,可视化流程日志,精准追踪流程流转.人工任务统一管理,审批 ...

  10. Ceph配置与认证授权

    目录 Ceph配置与认证授权 1. 为什么现在不采用修改配置文件的方式了呢? 2. Ceph元变量 3. 使用命令行修改配置 3.1 全部修改(使用服务名) 3.2 部分修改(修改进程) 3.3 临时 ...