push-switch.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Generic push-switch framework
  3. *
  4. * Copyright (C) 2006 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <asm/push-switch.h>
  15. #define DRV_NAME "push-switch"
  16. #define DRV_VERSION "0.1.1"
  17. static ssize_t switch_show(struct device *dev,
  18. struct device_attribute *attr,
  19. char *buf)
  20. {
  21. struct push_switch_platform_info *psw_info = dev->platform_data;
  22. return sprintf(buf, "%s\n", psw_info->name);
  23. }
  24. static DEVICE_ATTR(switch, S_IRUGO, switch_show, NULL);
  25. static void switch_timer(unsigned long data)
  26. {
  27. struct push_switch *psw = (struct push_switch *)data;
  28. schedule_work(&psw->work);
  29. }
  30. static void switch_work_handler(struct work_struct *work)
  31. {
  32. struct push_switch *psw = container_of(work, struct push_switch, work);
  33. struct platform_device *pdev = psw->pdev;
  34. psw->state = 0;
  35. kobject_uevent(&pdev->dev.kobj, KOBJ_CHANGE);
  36. }
  37. static int switch_drv_probe(struct platform_device *pdev)
  38. {
  39. struct push_switch_platform_info *psw_info;
  40. struct push_switch *psw;
  41. int ret, irq;
  42. psw = kzalloc(sizeof(struct push_switch), GFP_KERNEL);
  43. if (unlikely(!psw))
  44. return -ENOMEM;
  45. irq = platform_get_irq(pdev, 0);
  46. if (unlikely(irq < 0)) {
  47. ret = -ENODEV;
  48. goto err;
  49. }
  50. psw_info = pdev->dev.platform_data;
  51. BUG_ON(!psw_info);
  52. ret = request_irq(irq, psw_info->irq_handler,
  53. IRQF_DISABLED | psw_info->irq_flags,
  54. psw_info->name ? psw_info->name : DRV_NAME, pdev);
  55. if (unlikely(ret < 0))
  56. goto err;
  57. if (psw_info->name) {
  58. ret = device_create_file(&pdev->dev, &dev_attr_switch);
  59. if (unlikely(ret)) {
  60. dev_err(&pdev->dev, "Failed creating device attrs\n");
  61. ret = -EINVAL;
  62. goto err_irq;
  63. }
  64. }
  65. INIT_WORK(&psw->work, switch_work_handler);
  66. init_timer(&psw->debounce);
  67. psw->debounce.function = switch_timer;
  68. psw->debounce.data = (unsigned long)psw;
  69. /* Workqueue API brain-damage */
  70. psw->pdev = pdev;
  71. platform_set_drvdata(pdev, psw);
  72. return 0;
  73. err_irq:
  74. free_irq(irq, pdev);
  75. err:
  76. kfree(psw);
  77. return ret;
  78. }
  79. static int switch_drv_remove(struct platform_device *pdev)
  80. {
  81. struct push_switch *psw = platform_get_drvdata(pdev);
  82. struct push_switch_platform_info *psw_info = pdev->dev.platform_data;
  83. int irq = platform_get_irq(pdev, 0);
  84. if (psw_info->name)
  85. device_remove_file(&pdev->dev, &dev_attr_switch);
  86. platform_set_drvdata(pdev, NULL);
  87. flush_scheduled_work();
  88. del_timer_sync(&psw->debounce);
  89. free_irq(irq, pdev);
  90. kfree(psw);
  91. return 0;
  92. }
  93. static struct platform_driver switch_driver = {
  94. .probe = switch_drv_probe,
  95. .remove = switch_drv_remove,
  96. .driver = {
  97. .name = DRV_NAME,
  98. },
  99. };
  100. static int __init switch_init(void)
  101. {
  102. printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
  103. return platform_driver_register(&switch_driver);
  104. }
  105. static void __exit switch_exit(void)
  106. {
  107. platform_driver_unregister(&switch_driver);
  108. }
  109. module_init(switch_init);
  110. module_exit(switch_exit);
  111. MODULE_VERSION(DRV_VERSION);
  112. MODULE_AUTHOR("Paul Mundt");
  113. MODULE_LICENSE("GPL v2");