cpcap-pwrbutton.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * CPCAP Power Button Input Driver
  3. *
  4. * Copyright (C) 2017 Sebastian Reichel <sre@kernel.org>
  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 = platform_get_irq(pdev, 0);
  50. int err;
  51. button = devm_kmalloc(&pdev->dev, sizeof(*button), GFP_KERNEL);
  52. if (!button)
  53. return -ENOMEM;
  54. button->idev = devm_input_allocate_device(&pdev->dev);
  55. if (!button->idev)
  56. return -ENOMEM;
  57. button->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  58. if (!button->regmap)
  59. return -ENODEV;
  60. button->dev = &pdev->dev;
  61. button->idev->name = "cpcap-pwrbutton";
  62. button->idev->phys = "cpcap-pwrbutton/input0";
  63. button->idev->dev.parent = button->dev;
  64. input_set_capability(button->idev, EV_KEY, KEY_POWER);
  65. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  66. powerbutton_irq, IRQF_ONESHOT, "cpcap_pwrbutton", button);
  67. if (err < 0) {
  68. dev_err(&pdev->dev, "IRQ request failed: %d\n", err);
  69. return err;
  70. }
  71. err = input_register_device(button->idev);
  72. if (err) {
  73. dev_err(&pdev->dev, "Input register failed: %d\n", err);
  74. return err;
  75. }
  76. device_init_wakeup(&pdev->dev, true);
  77. return 0;
  78. }
  79. #ifdef CONFIG_OF
  80. static const struct of_device_id cpcap_pwrbutton_dt_match_table[] = {
  81. { .compatible = "motorola,cpcap-pwrbutton" },
  82. {},
  83. };
  84. MODULE_DEVICE_TABLE(of, cpcap_pwrbutton_dt_match_table);
  85. #endif
  86. static struct platform_driver cpcap_power_button_driver = {
  87. .probe = cpcap_power_button_probe,
  88. .driver = {
  89. .name = "cpcap-pwrbutton",
  90. .of_match_table = of_match_ptr(cpcap_pwrbutton_dt_match_table),
  91. },
  92. };
  93. module_platform_driver(cpcap_power_button_driver);
  94. MODULE_ALIAS("platform:cpcap-pwrbutton");
  95. MODULE_DESCRIPTION("CPCAP Power Button");
  96. MODULE_LICENSE("GPL");
  97. MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");