cpcap-pwrbutton.c 2.6 KB

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