【OK210试用体验】之button驱动 - 在线问答 - 电子技术论坛 - 最好最受欢迎电子论坛!

【OK210试用体验】之button驱动

罗崇军 ( 楼主 ) 2015-9-6 21:08:19  显示全部楼层
开学这俩天总是比较忙,今天差不多都忙完了,抽时间发个贴,防止被踢。今天要给大家发的是按键的驱动。
          硬件平台:OK210;
           os :Linux2.6.35.7
            驱动类型:button
驱动程序如下:
  1. /*
  2. * Driver for keys on GPIO lines capable of generating interrupts.
  3. *
  4. * Copyright 2005 Phil Blundell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */

  10. #include

  11. #include
  12. #include
  13. #include
  14. #include
  15. #include
  16. #include
  17. #include
  18. #include
  19. #include
  20. #include
  21. #include
  22. #include


  23. #include
  24. #include

  25. #include

  26. struct gpio_button_data {
  27.         struct gpio_keys_button *button;
  28.         struct input_dev *input;
  29.         struct timer_list timer;
  30. };

  31. struct gpio_keys_drvdata {
  32.         struct input_dev *input;
  33.         struct gpio_button_data data[0];
  34. };
  35. struct s3c_gpio_key{
  36.         int pin;
  37.         int eint;
  38.         int eintcfg;
  39.         int inputcfg;
  40. };
  41. struct s3c_gpio_key s3c_gpio_keys[]=
  42. {       
  43.         {
  44.                 .pin = S5PV210_GPH0(0),
  45.                 .eintcfg = 0X0f<<0,      
  46.                 .inputcfg = 0<<0,
  47.                 .eint = IRQ_EINT0,
  48.         },
  49.         {
  50.                 .pin = S5PV210_GPH0(1),
  51.                 .eintcfg = 0X0f<<4,      
  52.                 .inputcfg = 0<<4,
  53.                 .eint = IRQ_EINT1,
  54.         },
  55.         { //lhh add
  56.                 .pin = S5PV210_GPH0(2),
  57.                 .eintcfg = 0X0f<<8,      
  58.                 .inputcfg = 0<<8,
  59.                 .eint = IRQ_EINT2,
  60.         },
  61.         {
  62.                 .pin = S5PV210_GPH0(3),
  63.                 .eintcfg = 0X0f<<12,      
  64.                 .inputcfg = 0<<12,                  
  65.                 .eint = IRQ_EINT3,
  66.         },
  67.         {
  68.                 .pin = S5PV210_GPH0(4),  
  69.                 .eintcfg = 0X0f<<16,      
  70.                 .inputcfg = 0<<16,                          
  71.                 .eint = IRQ_EINT4,
  72.         },               
  73.         {
  74.                 .pin = S5PV210_GPH0(5),
  75.                 .eintcfg = 0X0f<<20,      
  76.                 .inputcfg = 0<<20,
  77.                 .eint = IRQ_EINT5,
  78.         },
  79.         {
  80.                 .pin = S5PV210_GPH2(6),  
  81.                 .eintcfg = 0X0f<<24,      
  82.                 .inputcfg = 0<<24,                          
  83.                 .eint = IRQ_EINT16_31,
  84.         },               
  85.         {
  86.                 .pin = S5PV210_GPH2(7),
  87.                 .eintcfg = 0X0f<<28,      
  88.                 .inputcfg = 0<<28,
  89.                 .eint = IRQ_EINT16_31,
  90.         },

  91. };

  92. static void gpio_keys_report_event(struct gpio_button_data *bdata)
  93. {
  94.         struct gpio_keys_button *button = bdata->button;
  95.         struct input_dev *input = bdata->input;
  96.         unsigned int type = button->type ?: EV_KEY;

  97.         int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
  98.         input_event(input, type, button->code, !!state);
  99.         input_sync(input);
  100.         printk("***************button enter code: %d*********************n",button->code);
  101. }

  102. static void gpio_check_button(unsigned long _data)
  103. {
  104.         struct gpio_button_data *data = (struct gpio_button_data *)_data;

  105.         gpio_keys_report_event(data);
  106. }

  107. static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
  108. {
  109.         struct gpio_button_data *bdata = dev_id;
  110.         struct gpio_keys_button *button = bdata->button;
  111.         int i;
  112. //        BUG_ON(irq != gpio_to_irq(button->gpio));

  113.         //add by Figo
  114.         for(i=0;i<(sizeof(s3c_gpio_keys)/sizeof(struct s3c_gpio_key));i++)
  115.         {
  116.                 s3c_gpio_cfgpin(s3c_gpio_keys[i].pin,s3c_gpio_keys[i].inputcfg);
  117.         }
  118. //        if(button->code == 102 || button->code == 106)
  119. //                s3c_gpio_cfgpin(button->gpio,S3C_GPIO_INPUT);
  120. //        else
  121. //                s3c_gpio_cfgpin(button->gpio,0);
  122.                
  123. //        printk("The code is %dn",button->code);
  124.         if (button->debounce_interval)
  125.                 mod_timer(&bdata->timer,
  126.                         jiffies + msecs_to_jiffies(button->debounce_interval));
  127.         else
  128.                 gpio_keys_report_event(bdata);

  129.         //add by Figo
  130.         for(i=0;i<(sizeof(s3c_gpio_keys)/sizeof(struct  s3c_gpio_key));i++)
  131.         {
  132.                 s3c_gpio_cfgpin(s3c_gpio_keys[i].pin,s3c_gpio_keys[i].eintcfg);
  133.         }
  134. //        if(button->code == 102 || button->code == 106)
  135. //                s3c_gpio_cfgpin(button->gpio,S3C_GPIO_SFN(3));
  136. //        else
  137. //                s3c_gpio_cfgpin(button->gpio,2);

  138.         return IRQ_HANDLED;
  139. }

  140. static int __devinit gpio_keys_probe(struct platform_device *pdev)
  141. {
  142.         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  143.         struct gpio_keys_drvdata *ddata;
  144.         struct input_dev *input;
  145.         int i, error,irq;
  146.         int wakeup = 0;

  147.         printk("1111111111111111111111111111111111111111111111111111111111n");
  148.         ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
  149.                         pdata->nbuttons * sizeof(struct gpio_button_data),
  150.                         GFP_KERNEL);
  151.         input = input_allocate_device();
  152.         if (!ddata || !input) {
  153.                 error = -ENOMEM;
  154.                 goto fail1;
  155.         }

  156.         platform_set_drvdata(pdev, ddata);

  157.         input->name = pdev->name;
  158.         input->phys = "gpio-keys/input0";
  159.         input->dev.parent = &pdev->dev;

  160.         input->id.bustype = BUS_HOST;
  161.         input->id.vendor = 0x0001;
  162.         input->id.product = 0x0001;
  163.         input->id.version = 0x0100;

  164.         ddata->input = input;

  165.         for (i = 0; i < pdata->nbuttons; i++) {
  166.                 struct gpio_keys_button *button = &pdata->buttons[i];
  167.                 struct gpio_button_data *bdata = &ddata->data[i];
  168.                 int irq;
  169.                 unsigned int type = button->type ?: EV_KEY;

  170.                 bdata->input = input;
  171.                 bdata->button = button;
  172.                 setup_timer(&bdata->timer,
  173.                             gpio_check_button, (unsigned long)bdata);

  174.                 error = gpio_request(button->gpio, button->desc ?: "gpio_keys");

  175.                 if (error < 0) {
  176.                         pr_err("gpio-keys: failed to request GPIO %d,"
  177.                                 " error %dn", button->gpio, error);
  178.                         goto fail2;
  179.                 }

  180.                 error = gpio_direction_input(button->gpio);
  181.                 if (error < 0) {
  182.                         pr_err("gpio-keys: failed to configure input"
  183.                                 " direction for GPIO %d, error %dn",
  184.                                 button->gpio, error);
  185.                         gpio_free(button->gpio);
  186.                         goto fail2;
  187.                 }
  188. #if 1
  189. //                s3c_gpio_cfgpin(s3c_gpio_keys[i].pin,s3c_gpio_keys[i].eintcfg);
  190.                
  191.                 if(IRQ_EINT16_31 == s3c_gpio_keys[i].eint)
  192.                 {
  193.                         irq = gpio_to_irq(button->gpio);
  194.                         error = request_irq(irq, gpio_keys_isr,
  195.                                                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_SHARED,
  196.                                                 /*button->desc ? button->desc : "gpio_keys",*/"MENU_TEST",
  197.                                                 bdata);
  198.                         if (error) {
  199.                                 pr_err("gpio-keys: Unable to claim irq %d; error %d index %dn",
  200.                                         irq, error,i);
  201.                                 gpio_free(button->gpio);
  202.                                 goto fail2;
  203.                         }
  204.                 }
  205.                 else
  206.                 {
  207.                         error = request_irq(s3c_gpio_keys[i].eint, gpio_keys_isr,
  208.                                                 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
  209.                                                 IRQF_TRIGGER_FALLING,
  210.                                                 button->desc ? button->desc : "gpio_keys",
  211.                                                 bdata);
  212.                         if (error) {
  213.                                 pr_err("gpio-keys: Unable to claim irq %d; error %dn",
  214.                                         irq, error);
  215.                                 gpio_free(button->gpio);
  216.                                 goto fail2;
  217.                         }

  218.                 }
  219.                
  220. #else
  221.                 error = request_irq(interrupt_group[i], gpio_keys_isr,
  222.                                     IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
  223.                                         IRQF_TRIGGER_FALLING,
  224.                                     button->desc ? button->desc : "gpio_keys",
  225.                                     bdata);
  226.                 if (error) {
  227.                         pr_err("gpio-keys: Unable to claim irq %d; error %d;index %dn",
  228.                                 interrupt_group[i], error,i);
  229.                         gpio_free(button->gpio);
  230.                         goto fail2;
  231.                 }
  232. #endif
  233.                 if (button->wakeup)
  234.                         wakeup = 1;

  235.                 input_set_capability(input, type, button->code);
  236.         }

  237.         error = input_register_device(input);
  238.         if (error) {
  239.                 pr_err("gpio-keys: Unable to register input device, "
  240.                         "error: %dn", error);
  241.                 goto fail2;
  242.         }

  243.         device_init_wakeup(&pdev->dev, wakeup);

  244.         return 0;

  245. fail2:
  246.         while (--i >= 0) {
  247.                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
  248.                 if (pdata->buttons[i].debounce_interval)
  249.                         del_timer_sync(&ddata->data[i].timer);
  250.                 gpio_free(pdata->buttons[i].gpio);
  251.         }

  252.         platform_set_drvdata(pdev, NULL);
  253. fail1:
  254.         input_free_device(input);
  255.         kfree(ddata);

  256.         return error;
  257. }

  258. static int __devexit gpio_keys_remove(struct platform_device *pdev)
  259. {
  260.         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  261.         struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
  262.         struct input_dev *input = ddata->input;
  263.         int i;

  264.         device_init_wakeup(&pdev->dev, 0);

  265.         for (i = 0; i < pdata->nbuttons; i++) {
  266.                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
  267.                 free_irq(irq, &ddata->data[i]);
  268.                 if (pdata->buttons[i].debounce_interval)
  269.                         del_timer_sync(&ddata->data[i].timer);
  270.                 gpio_free(pdata->buttons[i].gpio);
  271.         }

  272.         input_unregister_device(input);

  273.         return 0;
  274. }


  275. #ifdef CONFIG_PM
  276. static int gpio_keys_suspend(struct device *dev)
  277. {
  278.         struct platform_device *pdev = to_platform_device(dev);
  279.         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  280.         int i;

  281.         if (device_may_wakeup(&pdev->dev)) {

  282.                 for (i = 0; i < pdata->nbuttons; i++) {

  283.                         struct gpio_keys_button *button = &pdata->buttons[i];

  284.                         if (button->wakeup) {

  285.                                 int irq = gpio_to_irq(button->gpio);

  286. //                                enable_irq_wake(irq);
  287.                                 enable_irq_wake(s3c_gpio_keys[i].eint);

  288.                         }
  289.                 }
  290.         }

  291.         return 0;
  292. }

  293. static int gpio_keys_resume(struct device *dev)
  294. {
  295.         struct platform_device *pdev = to_platform_device(dev);
  296.         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
  297.         int i;

  298.         if (device_may_wakeup(&pdev->dev)) {
  299.                 for (i = 0; i < pdata->nbuttons; i++) {
  300.                         struct gpio_keys_button *button = &pdata->buttons[i];
  301.                         if (button->wakeup) {
  302.                                 int irq = gpio_to_irq(button->gpio);
  303.         //                        disable_irq_wake(irq);
  304.                                 disable_irq_wake(s3c_gpio_keys[i].eint);

  305.                         }
  306.                 }
  307.         }

  308.         return 0;
  309. }

  310. static const struct dev_pm_ops gpio_keys_pm_ops = {
  311.         .suspend        = gpio_keys_suspend,
  312.         .resume                = gpio_keys_resume,
  313. };
  314. #endif

  315. static struct platform_driver gpio_keys_device_driver = {
  316.         .probe                = gpio_keys_probe,
  317.         .remove                = __devexit_p(gpio_keys_remove),

  318.         .driver                = {
  319.                 .name        = "gpio-keys",
  320.                 .owner        = THIS_MODULE,
  321. #ifdef CONFIG_PM
  322.                 .pm        = &gpio_keys_pm_ops,
  323. #endif
  324.         }
  325.        
  326. };

  327. static int __init gpio_keys_init(void)
  328. {
  329.         printk("gpio-keys initn");
  330.         return platform_driver_register(&gpio_keys_device_driver);
  331. }

  332. static void __exit gpio_keys_exit(void)
  333. {
  334.         platform_driver_unregister(&gpio_keys_device_driver);
  335. }

  336. module_init(gpio_keys_init);
  337. module_exit(gpio_keys_exit);

  338. MODULE_LICENSE("GPL");
  339. MODULE_AUTHOR("Phil Blundell ");
  340. MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
  341. MODULE_ALIAS("platform:gpio-keys");

复制代码
makefile文件如下
  1. all:
  2.         make -C $(SRC_PATH)/kernel M=`pwd` modules
  3.         cp key_drv.ko $(SRC_PATH)/system/modules -av
  4. clean:
  5.         make -C $(SRC_PATH)/kernel M=`pwd` modules clean
  6.         rm -rf modules.order *.o *.ko *.mod.* Module*

  7. obj-m        += key_drv.o
复制代码
由于这个驱动比较简单,应用程序就不写了,我自己也没有验证,好像有点不负责任,到以后做项目需要的时候再慢慢调吧!



1个回复

mrbushy 发表于 2015-9-13 12:37:33
不错的分享..........
回复

举报 使用道具

您需要登录后才可以回帖 登录 | 注册

本版积分规则


关闭

站长推荐上一条 /6 下一条

小黑屋|手机版|Archiver|电子发烧友 ( 湘ICP备2023018690号 )

GMT+8, 2024-3-29 22:42 , Processed in 0.552802 second(s), Total 61, Slave 43 queries .

Powered by 电子发烧友网

© 2015 bbs.elecfans.com

微信扫描
快速回复 返回顶部 返回列表