gpio键盘的原理
时间:2013-11-07 阅读:1499
gpio键盘
1,定义gpio键盘的设备数据结构platform_device
#define RX51_GPIO_CAMERA_LENS_COVER 110
#define RX51_GPIO_CAMERA_FOCUS 68
#define RX51_GPIO_CAMERA_CAPTURE 69
#define RX51_GPIO_KEYPAD_SLIDE 71
#define RX51_GPIO_LOCK_BUTTON 113
#define RX51_GPIO_PROXIMITY 89
#define RX51_GPIO_DEBOUNCE_TIMEOUT 10
static struct gpio_keys_button rx51_gpio_keys[] = {
{
.desc = "Camera Lens Cover",
.type = EV_SW,
.code = SW_CAMERA_LENS_COVER,
.gpio = RX51_GPIO_CAMERA_LENS_COVER,
.active_low = 1,
.debounce_interval = RX51_GPIO_DEBOUNCE_TIMEOUT,
}, {
.desc = "Camera Focus",
.type = EV_KEY,
.code = KEY_CAMERA_FOCUS,
.gpio = RX51_GPIO_CAMERA_FOCUS,
.active_low = 1,
.debounce_interval = RX51_GPIO_DEBOUNCE_TIMEOUT,
}, {
.desc = "Camera Capture",
.type = EV_KEY,
.code = KEY_CAMERA,
.gpio = RX51_GPIO_CAMERA_CAPTURE,
.active_low = 1,
.debounce_interval = RX51_GPIO_DEBOUNCE_TIMEOUT,
}, {
.desc = "Lock Button",
.type = EV_KEY,
.code = KEY_SCREENLOCK,
.gpio = RX51_GPIO_LOCK_BUTTON,
.active_low = 1,
.debounce_interval = RX51_GPIO_DEBOUNCE_TIMEOUT,
}, {
.desc = "Keypad Slide",
.type = EV_SW,
.code = SW_KEYPAD_SLIDE,
.gpio = RX51_GPIO_KEYPAD_SLIDE,
.active_low = 1,
.debounce_interval = RX51_GPIO_DEBOUNCE_TIMEOUT,
}, {
.desc = "Proximity Sensor",
.type = EV_SW,
.code = SW_FRONT_PROXIMITY,
.gpio = RX51_GPIO_PROXIMITY,
.active_low = 0,
.debounce_interval = RX51_GPIO_DEBOUNCE_TIMEOUT,
}
};
static struct gpio_keys_platform_data rx51_gpio_keys_data = {
.buttons = rx51_gpio_keys,
.nbuttons = ARRAY_SIZE(rx51_gpio_keys),
};
static struct platform_device rx51_gpio_keys_device = {
.name = "gpio-keys",
.id = -1,
.dev = {
.platform_data = &rx51_gpio_keys_data,
},
};
2,定义gpio键盘的驱动数据结构platform_driver
static struct platform_driver gpio_keys_device_driver = {
.probe = gpio_keys_probe,
.remove = __devexit_p(gpio_keys_remove),
.driver = {
.name = "gpio-keys",
.owner = THIS_MODULE,
#Ifdef CONFIG_PM
.pm = &gpio_keys_pm_ops,
#Endif
}
};
3,注册平台设备
static void __init rx51_add_gpio_keys(void)
{
platform_device_register(&rx51_gpio_keys_device);
}
platform_driver 和platform_device的.name = "gpio-keys",都是一样。device和driver的名字一样就回调用probe函数
gpio_keys_probe();
probe函数的具体实现
static int __devinit gpio_keys_probe(struct platform_device *pdev)
{
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
struct gpio_keys_drvdata *ddata;
struct input_dev *input;
int i, error;
int wakeup = 0;
ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
pdata->nbuttons * sizeof(struct gpio_button_data),
GFP_KERNEL);
input = input_allocate_device();
if (!ddata || !input) {
error = -ENOMEM;
goto fail1;
}
platform_set_drvdata(pdev, ddata);
input->name = pdev->name;
input->phys = "gpio-keys/input0";
input->dev.parent = &pdev->dev;
input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100;
/* Enable auto repeat feature of Linux input subsystem */
if (pdata->rep)
__set_bit(EV_REP, input->evbit);
ddata->input = input;
for (i = 0; i < pdata->nbuttons; i++) {
struct gpio_keys_button *button = &pdata->buttons[i];
struct gpio_button_data *bdata = &ddata->data[i];
int irq;
unsigned int type = button->type ?: EV_KEY;
bdata->input = input;
bdata->button = button;
setup_timer(&bdata->timer,
gpio_keys_timer, (unsigned long)bdata);
INIT_WORK(&bdata->work, gpio_keys_report_event);
error = gpio_request(button->gpio, button->desc ?: "gpio_keys");
if (error < 0) {
pr_err("gpio-keys: failed to request GPIO %d,"
" error %d/n", button->gpio, error);
goto fail2;
}
error = gpio_direction_input(button->gpio);
if (error < 0) {
pr_err("gpio-keys: failed to configure input"
" direction for GPIO %d, error %d/n",
button->gpio, error);
gpio_free(button->gpio);
goto fail2;
}
irq = gpio_to_irq(button->gpio);
if (irq < 0) {
error = irq;
pr_err("gpio-keys: Unable to get irq number"
" for GPIO %d, error %d/n",
button->gpio, error);
gpio_free(button->gpio);
goto fail2;
}
error = request_irq(irq, gpio_keys_isr,
IRQF_SHARED |
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
button->desc ? button->desc : "gpio_keys",
bdata);
if (error) {
pr_err("gpio-keys: Unable to claim irq %d; error %d/n",
irq, error);
gpio_free(button->gpio);
goto fail2;
}
if (button->wakeup)
wakeup = 1;
input_set_capability(input, type, button->code);
}
error = input_register_device(input);
if (error) {
pr_err("gpio-keys: Unable to register input device, "
"error: %d/n", error);
goto fail2;
}
device_init_wakeup(&pdev->dev, wakeup);
return 0;
fail2:
while (--i >= 0) {
free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
if (pdata->buttons[i].debounce_interval)
del_timer_sync(&ddata->data[i].timer);
cancel_work_sync(&ddata->data[i].work);
gpio_free(pdata->buttons[i].gpio);
}
platform_set_drvdata(pdev, NULL);
fail1:
input_free_device(input);
kfree(ddata);
return error;
}
static int __devexit gpio_keys_remove(struct platform_device *pdev)
{
struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
struct input_dev *input = ddata->input;
int i;
device_init_wakeup(&pdev->dev, 0);
for (i = 0; i < pdata->nbuttons; i++) {
int irq = gpio_to_irq(pdata->buttons[i].gpio);
free_irq(irq, &ddata->data[i]);
if (pdata->buttons[i].debounce_interval)
del_timer_sync(&ddata->data[i].timer);
cancel_work_sync(&ddata->data[i].work);
gpio_free(pdata->buttons[i].gpio);
}
input_unregister_device(input);
return 0;
0,初始化工作队列INIT_WORK(&bdata->work, gpio_keys_report_event);
1,主要是分配一个input设备 input_allocate_device();
2,按键中断请求
3,设置gpio为输入,gpio_direction_input(button->gpio);
4, 获取gpio的中断号 irq = gpio_to_irq(button->gpio);
5,中断申请 error = request_irq(irq, gpio_keys_isr,
IRQF_SHARED |
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
button->desc ? button->desc : "gpio_keys",
bdata);
6, 初始化是否可以唤醒
if (button->wakeup)
wakeup = 1;
7,初始化按键的类型和按键码 input_set_capability(input, type, button->code);
8,注册input设备error = input_register_device(input);
9,初始化唤醒源 device_init_wakeup(&pdev->dev, wakeup);
4,注册成功后,当有按键按下时就调用中断回调函数
static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
{
struct gpio_button_data *bdata = dev_id;
struct gpio_keys_button *button = bdata->button;
BUG_ON(irq != gpio_to_irq(button->gpio));
if (button->debounce_interval)
mod_timer(&bdata->timer,
jiffies + msecs_to_jiffies(button->debounce_interval));
else
schedule_work(&bdata->work);
return IRQ_HANDLED;
}
zui终执行工作队列的线程函数gpio_keys_report_event
static void gpio_keys_report_event(struct work_struct *work)
{
struct gpio_button_data *bdata =
container_of(work, struct gpio_button_data, work);
struct gpio_keys_button *button = bdata->button;
struct input_dev *input = bdata->input;
unsigned int type = button->type ?: EV_KEY;
int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
input_event(input, type, button->code, !!state);
input_sync(input);
}
根据gpio的状态为按键按下还是弹开,上报按键键码的事件sendevent
如果CPU是在睡眠状态,按键又定义了可以唤醒功能,也就是在board文件定义了(.wakeup = 0,),那么CPU睡眠之前的时候执行suspend函数
static int gpio_keys_suspend(struct device *dev)
{
struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
int i;
if (device_may_wakeup(dev)) {
for (i = 0; i < ddata->n_buttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i];
if (bdata->button->wakeup)
enable_irq_wake(bdata->irq);
}
}
return 0;
}
static inline bool device_may_wakeup(struct device *dev)
{
return dev->power.can_wakeup && !!dev->power.wakeup;
}
通过enable_irq_wake(bdata->irq);设置对应的GPIO允许唤醒功能。当CPU睡眠后,对按键按下去,触发CPU唤醒,执行相应的resume函数
static int gpio_keys_resume(struct device *dev)
{
struct gpio_keys_drvdata *ddata = dev_get_drvdata(dev);
int i;
for (i = 0; i < ddata->n_buttons; i++) {
struct gpio_button_data *bdata = &ddata->data[i];
if (bdata->button->wakeup && device_may_wakeup(dev))
disable_irq_wake(bdata->irq);
if (gpio_is_valid(bdata->button->gpio))
gpio_keys_gpio_report_event(bdata);
}
input_sync(ddata->input);
return 0;
}
先关闭相应的GPIO的唤醒,判断GPIO是否合法,如果合法就执行gpio_keys_gpio_report_event(bdata);上报按键事件给上层。
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 = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
if (type == EV_ABS) {
if (state)
input_event(input, type, button->code, button->value);
} else {
input_event(input, type, button->code, !!state);
}
input_sync(input);
}
gpio_keys_gpio_report_event(bdata);函数的功能是通过读board文件的配置表是否为按键,如果为按键就配置type为EV_KEY,通过gpio_get_value_cansleep(button->gpio)
读GPIO的电平和board文件的GPIO键盘配置表的button->active_low做异或运算zui为上报的事件状态,也就是如果低电平有效,那么board文件的GPIO键盘配置表的button->active_low 定义为1,如果按键按下去,(gpio_get_value_cansleep(button->gpio)读出GPIO的电平为0,那么(gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low;
的结果为0和1做异或运算,结果为1. 由于在board文件定义事件类型为按键type = EV_KEY,所以执行input_event(input, type, button->code, !!state);
上报按键事件给上层了,紧跟着这些input_sync(input);上报同步事件。完成了按键按下的事件的上报。系统处于工作状态,如果按键弹开就这些中断服务程序了。
static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
{
struct gpio_button_data *bdata = dev_id;
BUG_ON(irq != bdata->irq);
if (bdata->timer_debounce)
mod_timer(&bdata->timer,
jiffies + msecs_to_jiffies(bdata->timer_debounce));
else
schedule_work(&bdata->work);
return IRQ_HANDLED;
}
中断服务程序处理过程是判断board文件定义的键盘数据结构是否有防抖时间初始化bdata->timer_debounce,如果有定义防抖时间就设置定时器的定时时间bdata->timer_debounce,启动定时器bdata->timer。定时器的初始化为
setup_timer(&bdata->timer, gpio_keys_gpio_timer, (unsigned long)bdata);
gpio_keys_gpio_timer为定时器的。
当定时器的时间到了,就执行回调函数
static void gpio_keys_gpio_timer(unsigned long _data)
{
struct gpio_button_data *bdata = (struct gpio_button_data *)_data;
schedule_work(&bdata->work);
}
回调函数干的活就是调度工作队列schedule_work(&bdata->work);
工作队列的初始化为
INIT_WORK(&bdata->work, gpio_keys_gpio_work_func);
gpio_keys_gpio_work_func为工作队列&bdata->work的执行函数
所以调度工作队列schedule_work(&bdata->work)后,将gpio_keys_gpio_work_func放人工作队列执行。
gpio_keys_gpio_work_func的实现为
static void gpio_keys_gpio_work_func(struct work_struct *work)
{
struct gpio_button_data *bdata =
container_of(work, struct gpio_button_data, work);
gpio_keys_gpio_report_event(bdata);
}
也就是这些gpio_keys_gpio_report_event(bdata);
和上面唤醒的时候执行一样,对GPIO的电平,设置状态,上报事件。