stpmic1_onkey.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) STMicroelectronics 2018
  3. // Author: Pascal Paillet <[email protected]> for STMicroelectronics.
  4. #include <linux/input.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/mfd/stpmic1.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/property.h>
  11. #include <linux/regmap.h>
  12. /**
  13. * struct stpmic1_onkey - OnKey data
  14. * @input_dev: pointer to input device
  15. * @irq_falling: irq that we are hooked on to
  16. * @irq_rising: irq that we are hooked on to
  17. */
  18. struct stpmic1_onkey {
  19. struct input_dev *input_dev;
  20. int irq_falling;
  21. int irq_rising;
  22. };
  23. static irqreturn_t onkey_falling_irq(int irq, void *ponkey)
  24. {
  25. struct stpmic1_onkey *onkey = ponkey;
  26. struct input_dev *input_dev = onkey->input_dev;
  27. input_report_key(input_dev, KEY_POWER, 1);
  28. pm_wakeup_event(input_dev->dev.parent, 0);
  29. input_sync(input_dev);
  30. return IRQ_HANDLED;
  31. }
  32. static irqreturn_t onkey_rising_irq(int irq, void *ponkey)
  33. {
  34. struct stpmic1_onkey *onkey = ponkey;
  35. struct input_dev *input_dev = onkey->input_dev;
  36. input_report_key(input_dev, KEY_POWER, 0);
  37. pm_wakeup_event(input_dev->dev.parent, 0);
  38. input_sync(input_dev);
  39. return IRQ_HANDLED;
  40. }
  41. static int stpmic1_onkey_probe(struct platform_device *pdev)
  42. {
  43. struct stpmic1 *pmic = dev_get_drvdata(pdev->dev.parent);
  44. struct device *dev = &pdev->dev;
  45. struct input_dev *input_dev;
  46. struct stpmic1_onkey *onkey;
  47. unsigned int val, reg = 0;
  48. int error;
  49. onkey = devm_kzalloc(dev, sizeof(*onkey), GFP_KERNEL);
  50. if (!onkey)
  51. return -ENOMEM;
  52. onkey->irq_falling = platform_get_irq_byname(pdev, "onkey-falling");
  53. if (onkey->irq_falling < 0)
  54. return onkey->irq_falling;
  55. onkey->irq_rising = platform_get_irq_byname(pdev, "onkey-rising");
  56. if (onkey->irq_rising < 0)
  57. return onkey->irq_rising;
  58. if (!device_property_read_u32(dev, "power-off-time-sec", &val)) {
  59. if (val > 0 && val <= 16) {
  60. dev_dbg(dev, "power-off-time=%d seconds\n", val);
  61. reg |= PONKEY_PWR_OFF;
  62. reg |= ((16 - val) & PONKEY_TURNOFF_TIMER_MASK);
  63. } else {
  64. dev_err(dev, "power-off-time-sec out of range\n");
  65. return -EINVAL;
  66. }
  67. }
  68. if (device_property_present(dev, "st,onkey-clear-cc-flag"))
  69. reg |= PONKEY_CC_FLAG_CLEAR;
  70. error = regmap_update_bits(pmic->regmap, PKEY_TURNOFF_CR,
  71. PONKEY_TURNOFF_MASK, reg);
  72. if (error) {
  73. dev_err(dev, "PKEY_TURNOFF_CR write failed: %d\n", error);
  74. return error;
  75. }
  76. if (device_property_present(dev, "st,onkey-pu-inactive")) {
  77. error = regmap_update_bits(pmic->regmap, PADS_PULL_CR,
  78. PONKEY_PU_INACTIVE,
  79. PONKEY_PU_INACTIVE);
  80. if (error) {
  81. dev_err(dev, "ONKEY Pads configuration failed: %d\n",
  82. error);
  83. return error;
  84. }
  85. }
  86. input_dev = devm_input_allocate_device(dev);
  87. if (!input_dev) {
  88. dev_err(dev, "Can't allocate Pwr Onkey Input Device\n");
  89. return -ENOMEM;
  90. }
  91. input_dev->name = "pmic_onkey";
  92. input_dev->phys = "pmic_onkey/input0";
  93. input_set_capability(input_dev, EV_KEY, KEY_POWER);
  94. onkey->input_dev = input_dev;
  95. /* interrupt is nested in a thread */
  96. error = devm_request_threaded_irq(dev, onkey->irq_falling, NULL,
  97. onkey_falling_irq, IRQF_ONESHOT,
  98. dev_name(dev), onkey);
  99. if (error) {
  100. dev_err(dev, "Can't get IRQ Onkey Falling: %d\n", error);
  101. return error;
  102. }
  103. error = devm_request_threaded_irq(dev, onkey->irq_rising, NULL,
  104. onkey_rising_irq, IRQF_ONESHOT,
  105. dev_name(dev), onkey);
  106. if (error) {
  107. dev_err(dev, "Can't get IRQ Onkey Rising: %d\n", error);
  108. return error;
  109. }
  110. error = input_register_device(input_dev);
  111. if (error) {
  112. dev_err(dev, "Can't register power button: %d\n", error);
  113. return error;
  114. }
  115. platform_set_drvdata(pdev, onkey);
  116. device_init_wakeup(dev, true);
  117. return 0;
  118. }
  119. static int __maybe_unused stpmic1_onkey_suspend(struct device *dev)
  120. {
  121. struct platform_device *pdev = to_platform_device(dev);
  122. struct stpmic1_onkey *onkey = platform_get_drvdata(pdev);
  123. if (device_may_wakeup(dev)) {
  124. enable_irq_wake(onkey->irq_falling);
  125. enable_irq_wake(onkey->irq_rising);
  126. }
  127. return 0;
  128. }
  129. static int __maybe_unused stpmic1_onkey_resume(struct device *dev)
  130. {
  131. struct platform_device *pdev = to_platform_device(dev);
  132. struct stpmic1_onkey *onkey = platform_get_drvdata(pdev);
  133. if (device_may_wakeup(dev)) {
  134. disable_irq_wake(onkey->irq_falling);
  135. disable_irq_wake(onkey->irq_rising);
  136. }
  137. return 0;
  138. }
  139. static SIMPLE_DEV_PM_OPS(stpmic1_onkey_pm,
  140. stpmic1_onkey_suspend,
  141. stpmic1_onkey_resume);
  142. static const struct of_device_id of_stpmic1_onkey_match[] = {
  143. { .compatible = "st,stpmic1-onkey" },
  144. { },
  145. };
  146. MODULE_DEVICE_TABLE(of, of_stpmic1_onkey_match);
  147. static struct platform_driver stpmic1_onkey_driver = {
  148. .probe = stpmic1_onkey_probe,
  149. .driver = {
  150. .name = "stpmic1_onkey",
  151. .of_match_table = of_match_ptr(of_stpmic1_onkey_match),
  152. .pm = &stpmic1_onkey_pm,
  153. },
  154. };
  155. module_platform_driver(stpmic1_onkey_driver);
  156. MODULE_DESCRIPTION("Onkey driver for STPMIC1");
  157. MODULE_AUTHOR("Pascal Paillet <[email protected]>");
  158. MODULE_LICENSE("GPL v2");