e3x0-button.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2014, National Instruments Corp. All rights reserved.
  3. *
  4. * Driver for NI Ettus Research USRP E3x0 Button Driver
  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 as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/input.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/of.h>
  22. #include <linux/slab.h>
  23. static irqreturn_t e3x0_button_release_handler(int irq, void *data)
  24. {
  25. struct input_dev *idev = data;
  26. input_report_key(idev, KEY_POWER, 0);
  27. input_sync(idev);
  28. return IRQ_HANDLED;
  29. }
  30. static irqreturn_t e3x0_button_press_handler(int irq, void *data)
  31. {
  32. struct input_dev *idev = data;
  33. input_report_key(idev, KEY_POWER, 1);
  34. pm_wakeup_event(idev->dev.parent, 0);
  35. input_sync(idev);
  36. return IRQ_HANDLED;
  37. }
  38. static int __maybe_unused e3x0_button_suspend(struct device *dev)
  39. {
  40. struct platform_device *pdev = to_platform_device(dev);
  41. if (device_may_wakeup(dev))
  42. enable_irq_wake(platform_get_irq_byname(pdev, "press"));
  43. return 0;
  44. }
  45. static int __maybe_unused e3x0_button_resume(struct device *dev)
  46. {
  47. struct platform_device *pdev = to_platform_device(dev);
  48. if (device_may_wakeup(dev))
  49. disable_irq_wake(platform_get_irq_byname(pdev, "press"));
  50. return 0;
  51. }
  52. static SIMPLE_DEV_PM_OPS(e3x0_button_pm_ops,
  53. e3x0_button_suspend, e3x0_button_resume);
  54. static int e3x0_button_probe(struct platform_device *pdev)
  55. {
  56. struct input_dev *input;
  57. int irq_press, irq_release;
  58. int error;
  59. irq_press = platform_get_irq_byname(pdev, "press");
  60. if (irq_press < 0) {
  61. dev_err(&pdev->dev, "No IRQ for 'press', error=%d\n",
  62. irq_press);
  63. return irq_press;
  64. }
  65. irq_release = platform_get_irq_byname(pdev, "release");
  66. if (irq_release < 0) {
  67. dev_err(&pdev->dev, "No IRQ for 'release', error=%d\n",
  68. irq_release);
  69. return irq_release;
  70. }
  71. input = devm_input_allocate_device(&pdev->dev);
  72. if (!input)
  73. return -ENOMEM;
  74. input->name = "NI Ettus Research USRP E3x0 Button Driver";
  75. input->phys = "e3x0_button/input0";
  76. input->dev.parent = &pdev->dev;
  77. input_set_capability(input, EV_KEY, KEY_POWER);
  78. error = devm_request_irq(&pdev->dev, irq_press,
  79. e3x0_button_press_handler, 0,
  80. "e3x0-button", input);
  81. if (error) {
  82. dev_err(&pdev->dev, "Failed to request 'press' IRQ#%d: %d\n",
  83. irq_press, error);
  84. return error;
  85. }
  86. error = devm_request_irq(&pdev->dev, irq_release,
  87. e3x0_button_release_handler, 0,
  88. "e3x0-button", input);
  89. if (error) {
  90. dev_err(&pdev->dev, "Failed to request 'release' IRQ#%d: %d\n",
  91. irq_release, error);
  92. return error;
  93. }
  94. error = input_register_device(input);
  95. if (error) {
  96. dev_err(&pdev->dev, "Can't register input device: %d\n", error);
  97. return error;
  98. }
  99. platform_set_drvdata(pdev, input);
  100. device_init_wakeup(&pdev->dev, 1);
  101. return 0;
  102. }
  103. static int e3x0_button_remove(struct platform_device *pdev)
  104. {
  105. device_init_wakeup(&pdev->dev, 0);
  106. return 0;
  107. }
  108. #ifdef CONFIG_OF
  109. static const struct of_device_id e3x0_button_match[] = {
  110. { .compatible = "ettus,e3x0-button", },
  111. { }
  112. };
  113. MODULE_DEVICE_TABLE(of, e3x0_button_match);
  114. #endif
  115. static struct platform_driver e3x0_button_driver = {
  116. .driver = {
  117. .name = "e3x0-button",
  118. .of_match_table = of_match_ptr(e3x0_button_match),
  119. .pm = &e3x0_button_pm_ops,
  120. },
  121. .probe = e3x0_button_probe,
  122. .remove = e3x0_button_remove,
  123. };
  124. module_platform_driver(e3x0_button_driver);
  125. MODULE_LICENSE("GPL v2");
  126. MODULE_AUTHOR("Moritz Fischer <moritz.fischer@ettus.com>");
  127. MODULE_DESCRIPTION("NI Ettus Research USRP E3x0 Button driver");
  128. MODULE_ALIAS("platform:e3x0-button");