push-switch.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_RO(switch);
  26. static void switch_timer(struct timer_list *t)
  27. {
  28. struct push_switch *psw = from_timer(psw, t, debounce);
  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. timer_setup(&psw->debounce, switch_timer, 0);
  68. /* Workqueue API brain-damage */
  69. psw->pdev = pdev;
  70. platform_set_drvdata(pdev, psw);
  71. return 0;
  72. err_irq:
  73. free_irq(irq, pdev);
  74. err:
  75. kfree(psw);
  76. return ret;
  77. }
  78. static int switch_drv_remove(struct platform_device *pdev)
  79. {
  80. struct push_switch *psw = platform_get_drvdata(pdev);
  81. struct push_switch_platform_info *psw_info = pdev->dev.platform_data;
  82. int irq = platform_get_irq(pdev, 0);
  83. if (psw_info->name)
  84. device_remove_file(&pdev->dev, &dev_attr_switch);
  85. platform_set_drvdata(pdev, NULL);
  86. flush_work(&psw->work);
  87. del_timer_sync(&psw->debounce);
  88. free_irq(irq, pdev);
  89. kfree(psw);
  90. return 0;
  91. }
  92. static struct platform_driver switch_driver = {
  93. .probe = switch_drv_probe,
  94. .remove = switch_drv_remove,
  95. .driver = {
  96. .name = DRV_NAME,
  97. },
  98. };
  99. static int __init switch_init(void)
  100. {
  101. printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
  102. return platform_driver_register(&switch_driver);
  103. }
  104. static void __exit switch_exit(void)
  105. {
  106. platform_driver_unregister(&switch_driver);
  107. }
  108. module_init(switch_init);
  109. module_exit(switch_exit);
  110. MODULE_VERSION(DRV_VERSION);
  111. MODULE_AUTHOR("Paul Mundt");
  112. MODULE_LICENSE("GPL v2");