push-switch.c 3.1 KB

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