cpcap-pwrbutton.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * CPCAP Power Button Input Driver
  3. *
  4. * Copyright (C) 2017 Sebastian Reichel <[email protected]>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General
  7. * Public License. See the file "COPYING" in the main directory of this
  8. * archive for more details.
  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/module.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/input.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/regmap.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mfd/motorola-cpcap.h>
  25. #define CPCAP_IRQ_ON 23
  26. #define CPCAP_IRQ_ON_BITMASK (1 << (CPCAP_IRQ_ON % 16))
  27. struct cpcap_power_button {
  28. struct regmap *regmap;
  29. struct input_dev *idev;
  30. struct device *dev;
  31. };
  32. static irqreturn_t powerbutton_irq(int irq, void *_button)
  33. {
  34. struct cpcap_power_button *button = _button;
  35. int val;
  36. val = cpcap_sense_virq(button->regmap, irq);
  37. if (val < 0) {
  38. dev_err(button->dev, "irq read failed: %d", val);
  39. return IRQ_HANDLED;
  40. }
  41. pm_wakeup_event(button->dev, 0);
  42. input_report_key(button->idev, KEY_POWER, val);
  43. input_sync(button->idev);
  44. return IRQ_HANDLED;
  45. }
  46. static int cpcap_power_button_probe(struct platform_device *pdev)
  47. {
  48. struct cpcap_power_button *button;
  49. int irq;
  50. int err;
  51. irq = platform_get_irq(pdev, 0);
  52. if (irq < 0)
  53. return irq;
  54. button = devm_kmalloc(&pdev->dev, sizeof(*button), GFP_KERNEL);
  55. if (!button)
  56. return -ENOMEM;
  57. button->idev = devm_input_allocate_device(&pdev->dev);
  58. if (!button->idev)
  59. return -ENOMEM;
  60. button->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  61. if (!button->regmap)
  62. return -ENODEV;
  63. button->dev = &pdev->dev;
  64. button->idev->name = "cpcap-pwrbutton";
  65. button->idev->phys = "cpcap-pwrbutton/input0";
  66. input_set_capability(button->idev, EV_KEY, KEY_POWER);
  67. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  68. powerbutton_irq, IRQF_ONESHOT, "cpcap_pwrbutton", button);
  69. if (err < 0) {
  70. dev_err(&pdev->dev, "IRQ request failed: %d\n", err);
  71. return err;
  72. }
  73. err = input_register_device(button->idev);
  74. if (err) {
  75. dev_err(&pdev->dev, "Input register failed: %d\n", err);
  76. return err;
  77. }
  78. device_init_wakeup(&pdev->dev, true);
  79. return 0;
  80. }
  81. #ifdef CONFIG_OF
  82. static const struct of_device_id cpcap_pwrbutton_dt_match_table[] = {
  83. { .compatible = "motorola,cpcap-pwrbutton" },
  84. {},
  85. };
  86. MODULE_DEVICE_TABLE(of, cpcap_pwrbutton_dt_match_table);
  87. #endif
  88. static struct platform_driver cpcap_power_button_driver = {
  89. .probe = cpcap_power_button_probe,
  90. .driver = {
  91. .name = "cpcap-pwrbutton",
  92. .of_match_table = of_match_ptr(cpcap_pwrbutton_dt_match_table),
  93. },
  94. };
  95. module_platform_driver(cpcap_power_button_driver);
  96. MODULE_ALIAS("platform:cpcap-pwrbutton");
  97. MODULE_DESCRIPTION("CPCAP Power Button");
  98. MODULE_LICENSE("GPL");
  99. MODULE_AUTHOR("Sebastian Reichel <[email protected]>");