push-switch.c 2.9 KB

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