hisi_powerkey.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Hisilicon PMIC powerkey driver
  3. *
  4. * Copyright (C) 2013 Hisilicon Ltd.
  5. * Copyright (C) 2015, 2016 Linaro Ltd.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file "COPYING" in the main directory of this
  9. * archive for more details.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/reboot.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/input.h>
  23. #include <linux/slab.h>
  24. /* the held interrupt will trigger after 4 seconds */
  25. #define MAX_HELD_TIME (4 * MSEC_PER_SEC)
  26. static irqreturn_t hi65xx_power_press_isr(int irq, void *q)
  27. {
  28. struct input_dev *input = q;
  29. pm_wakeup_event(input->dev.parent, MAX_HELD_TIME);
  30. input_report_key(input, KEY_POWER, 1);
  31. input_sync(input);
  32. return IRQ_HANDLED;
  33. }
  34. static irqreturn_t hi65xx_power_release_isr(int irq, void *q)
  35. {
  36. struct input_dev *input = q;
  37. pm_wakeup_event(input->dev.parent, MAX_HELD_TIME);
  38. input_report_key(input, KEY_POWER, 0);
  39. input_sync(input);
  40. return IRQ_HANDLED;
  41. }
  42. static irqreturn_t hi65xx_restart_toggle_isr(int irq, void *q)
  43. {
  44. struct input_dev *input = q;
  45. int value = test_bit(KEY_RESTART, input->key);
  46. pm_wakeup_event(input->dev.parent, MAX_HELD_TIME);
  47. input_report_key(input, KEY_RESTART, !value);
  48. input_sync(input);
  49. return IRQ_HANDLED;
  50. }
  51. static const struct {
  52. const char *name;
  53. irqreturn_t (*handler)(int irq, void *q);
  54. } hi65xx_irq_info[] = {
  55. { "down", hi65xx_power_press_isr },
  56. { "up", hi65xx_power_release_isr },
  57. { "hold 4s", hi65xx_restart_toggle_isr },
  58. };
  59. static int hi65xx_powerkey_probe(struct platform_device *pdev)
  60. {
  61. struct device *dev = &pdev->dev;
  62. struct input_dev *input;
  63. int irq, i, error;
  64. input = devm_input_allocate_device(&pdev->dev);
  65. if (!input) {
  66. dev_err(&pdev->dev, "failed to allocate input device\n");
  67. return -ENOMEM;
  68. }
  69. input->phys = "hisi_on/input0";
  70. input->name = "HISI 65xx PowerOn Key";
  71. input_set_capability(input, EV_KEY, KEY_POWER);
  72. input_set_capability(input, EV_KEY, KEY_RESTART);
  73. for (i = 0; i < ARRAY_SIZE(hi65xx_irq_info); i++) {
  74. irq = platform_get_irq_byname(pdev, hi65xx_irq_info[i].name);
  75. if (irq < 0) {
  76. error = irq;
  77. dev_err(dev, "couldn't get irq %s: %d\n",
  78. hi65xx_irq_info[i].name, error);
  79. return error;
  80. }
  81. error = devm_request_any_context_irq(dev, irq,
  82. hi65xx_irq_info[i].handler,
  83. IRQF_ONESHOT,
  84. hi65xx_irq_info[i].name,
  85. input);
  86. if (error < 0) {
  87. dev_err(dev, "couldn't request irq %s: %d\n",
  88. hi65xx_irq_info[i].name, error);
  89. return error;
  90. }
  91. }
  92. error = input_register_device(input);
  93. if (error) {
  94. dev_err(&pdev->dev, "failed to register input device: %d\n",
  95. error);
  96. return error;
  97. }
  98. device_init_wakeup(&pdev->dev, 1);
  99. return 0;
  100. }
  101. static int hi65xx_powerkey_remove(struct platform_device *pdev)
  102. {
  103. device_init_wakeup(&pdev->dev, 0);
  104. return 0;
  105. }
  106. static struct platform_driver hi65xx_powerkey_driver = {
  107. .driver = {
  108. .name = "hi65xx-powerkey",
  109. },
  110. .probe = hi65xx_powerkey_probe,
  111. .remove = hi65xx_powerkey_remove,
  112. };
  113. module_platform_driver(hi65xx_powerkey_driver);
  114. MODULE_AUTHOR("Zhiliang Xue <xuezhiliang@huawei.com");
  115. MODULE_DESCRIPTION("Hisi PMIC Power key driver");
  116. MODULE_LICENSE("GPL v2");