buttons.c驱动源码:
// 参考drivers\input\keyboard\gpio_keys.c
#include "linux/module.h"
#include "linux/version.h"
#include "linux/module.h"
#include "linux/init.h"
#include "linux/fs.h"
#include "linux/interrupt.h"
#include "linux/irq.h"
#include "linux/sched.h"
#include "linux/pm.h"
#include "linux/slab.h"
#include "linux/sysctl.h"
#include "linux/proc_fs.h"
#include "linux/delay.h"
#include "linux/platform_device.h"
#include "linux/input.h"
#include "linux/gpio_keys.h"
#include "linux/workqueue.h"
#include "linux/gpio.h"
struct pin_desc{
int irq;
char *name;
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pins_desc[4] = {
{IRQ_EINT(19), "K4", S5PV210_GPH2(3), KEY_L},
{IRQ_EINT(24), "K5", S5PV210_GPH3(0), KEY_S},
{IRQ_EINT(25), "K6", S5PV210_GPH3(1), KEY_ENTER},
{IRQ_EINT(26), "K7", S5PV210_GPH3(2), KEY_LEFTSHIFT},
};
static struct input_dev *buttons_dev;
static struct pin_desc *irq_pd;
static struct timer_list buttons_timer;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
// 10ms后启动定时器
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static void buttons_timer_function(unsigned long data)
{
struct pin_desc * pindesc = irq_pd;
unsigned int pinval;
if (!pindesc)
return;
pinval = gpio_get_value(pindesc->pin);
if (pinval)
{
// 松开 : 最后一个参数: 0-松开, 1-按下
input_event(buttons_dev, EV_KEY, pindesc->key_val, 0);
input_sync(buttons_dev);
}
else
{
// 按下
input_event(buttons_dev, EV_KEY, pindesc->key_val, 1);
input_sync(buttons_dev);
}
}
static int buttons_init(void)
{
int i;
// 1. 分配一个input_dev结构体
buttons_dev = input_allocate_device();;
// 2. 设置
// 2.1 能产生哪类事件
set_bit(EV_KEY, buttons_dev->evbit);
set_bit(EV_REP, buttons_dev->evbit);
// 2.2 能产生这类操作里的哪些事件: L,S,ENTER,LEFTSHIT
set_bit(KEY_L, buttons_dev->keybit);
set_bit(KEY_S, buttons_dev->keybit);
set_bit(KEY_ENTER, buttons_dev->keybit);
set_bit(KEY_LEFTSHIFT, buttons_dev->keybit);
// 3. 注册
input_register_device(buttons_dev);
// 4. 硬件相关的操作
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
add_timer(&buttons_timer);
for (i = 0; i < 4; i++)
{
request_irq(pins_desc[i].irq, buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING,
pins_desc[i].name, &pins_desc[i]);
}
return 0;
}
static void buttons_exit(void)
{
int i;
for (i = 0; i < 4; i++)
{
free_irq(pins_desc[i].irq, &pins_desc[i]);
}
del_timer(&buttons_timer);
input_unregister_device(buttons_dev);
input_free_device(buttons_dev);
}
module_init(buttons_init);
module_exit(buttons_exit);
MODULE_LICENSE("GPL");
上一篇:Tiny210按键分层分离(总线-驱动-设备模型)
下一篇:Tiny210按键阻塞访问驱动
推荐阅读最新更新时间:2024-03-16 16:16