- Driver for keys on GPIO lines capable of generating interrupts.
- *
- * Copyright 2005 Phil Blundell
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
-
- #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/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 <asm/gpio.h>
-
- struct gpio_button_data {
- struct gpio_keys_button *button;
- struct input_dev *input;
- struct timer_list timer;
- struct work_struct work;
- };
-
- struct gpio_keys_drvdata {
- struct input_dev *input;
- struct gpio_button_data data[0];
- };
-
- 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;
- ///获得按键信息,同时于active_low异或,主要还是根据上升沿下降沿判断按键状态
- input_event(input, type, button->code, !!state);
- input_sync(input);
- }
-
- static void gpio_keys_timer(unsigned long _data)
- {
- struct gpio_button_data *data = (struct gpio_button_data *)_data;
-
- schedule_work(&data->work);//调度work
- }
-
- 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) /*检测是否在platform device设置了去抖动*/
- mod_timer(&bdata->timer,
- jiffies + msecs_to_jiffies(button->debounce_interval));
- /* 延迟msecs_to_jiffies(button->debounce_interval)个jiffies后执行schedule_work()*/
- else
- schedule_work(&bdata->work);//put work task in global workqueue,调度work
-
- return IRQ_HANDLED;
- }
-
- 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;//96行到103行,初始化input_dev一些属性
- 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);
- /* 如果 设置了 __set_bit(EV_REP, input->evbit)
- 也就是重复报告,它的工作机制是这样的:
- 如果按键报告了input_event(input, type, button->code,1);
- 之后, 在250ms (可以改)后,依然没有报告 input_event(input, type, button->code,0);
- 则 input 会每隔 33ms 继续报告一次 input_event(input, type, button->code,2);
- 直到 报告了 input_event(input, type, button->code,0);
- 才停止 ,这就是我们按住一个按键不松开时,会一直打印键值的原因;*/
-
- ddata->input = input;
-
- for (i = 0; i < pdata->nbuttons; i++) {//111-165对每个按键初始化GPIO,并初始化中断
- 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);//设置timer的值,是一个宏,在<linux/timer.h>
- INIT_WORK(&bdata->work, gpio_keys_report_event);//初始化工作队列,ldk.p124
-
- error =Cbutton->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);//配置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);//将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);//注册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);释放gpio
- }
-
- input_unregister_device(input);释放input设备
-
- return 0;
- }
-
-
- #ifdef CONFIG_PM
- static int gpio_keys_suspend(struct device *dev)
- {
- struct platform_device *pdev = to_platform_device(dev);
- struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
- int i;
-
- if (device_may_wakeup(&pdev->dev)) {
- for (i = 0; i < pdata->nbuttons; i++) {
- struct gpio_keys_button *button = &pdata->buttons[i];
- if (button->wakeup) {
- int irq = gpio_to_irq(button->gpio);
- enable_irq_wake(irq);
- }
- }
- }
-
- return 0;
- }
-
- static int gpio_keys_resume(struct device *dev)
- {
- struct platform_device *pdev = to_platform_device(dev);
- struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
- int i;
-
- if (device_may_wakeup(&pdev->dev)) {
- for (i = 0; i < pdata->nbuttons; i++) {
- struct gpio_keys_button *button = &pdata->buttons[i];
- if (button->wakeup) {
- int irq = gpio_to_irq(button->gpio);
- disable_irq_wake(irq);
- }
- }
- }
-
- return 0;
- }
-
- static const struct dev_pm_ops gpio_keys_pm_ops = {
- .suspend = gpio_keys_suspend,
- .resume = gpio_keys_resume,
- };
- #endif
-
- 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
- }
- };
-
- static int __init gpio_keys_init(void)
- {
- return platform_driver_register(&gpio_keys_device_driver);
- }
-
- static void __exit gpio_keys_exit(void)
- {
- platform_driver_unregister(&gpio_keys_device_driver);
- }
-
- module_init(gpio_keys_init);
- module_exit(gpio_keys_exit);
-
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
- MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
- MODULE_ALIAS("platform:gpio-keys");
-
-
- 附:两个结构体
- 1. struct gpio_keys_button {
- 2. /*Configuration parameters */
- 3. int code; /* 输入事件代码(KEY_*, SW_*) */
- 4. int gpio; /* gpio口 */
- 5. int active_low; /* 低电平有效*/
- 6. char *desc; /* 功能描述 */
- 7. int type; /* 输入事件类型(EV_KEY, EV_SW) */
- 8. int wakeup; /* configure thebutton as a wake-up source */
- 9. int debounce_interval; /* 去抖动间隔,单位微秒*/
- 10. };
- 11.
- 12. struct gpio_keys_platform_data {
- 13. struct gpio_keys_button *buttons;
- 14. int nbuttons;
- 15. unsignedint rep:1; /* enable inputsubsystem auto repeat */
- 16. };
gpio-keys.c分析
最新推荐文章于 2024-07-09 19:43:40 发布