smp2p_sleepstate.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/suspend.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/soc/qcom/smem_state.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irqreturn.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of.h>
  13. #include <linux/pm_wakeup.h>
  14. #define PROC_AWAKE_ID 12 /* 12th bit */
  15. #define AWAKE_BIT BIT(PROC_AWAKE_ID)
  16. static struct qcom_smem_state *state;
  17. static struct wakeup_source *notify_ws;
  18. /**
  19. * sleepstate_pm_notifier() - PM notifier callback function.
  20. * @nb: Pointer to the notifier block.
  21. * @event: Suspend state event from PM module.
  22. * @unused: Null pointer from PM module.
  23. *
  24. * This function is register as callback function to get notifications
  25. * from the PM module on the system suspend state.
  26. */
  27. static int sleepstate_pm_notifier(struct notifier_block *nb,
  28. unsigned long event, void *unused)
  29. {
  30. switch (event) {
  31. case PM_SUSPEND_PREPARE:
  32. qcom_smem_state_update_bits(state, AWAKE_BIT, 0);
  33. break;
  34. case PM_POST_SUSPEND:
  35. qcom_smem_state_update_bits(state, AWAKE_BIT, AWAKE_BIT);
  36. break;
  37. }
  38. return NOTIFY_DONE;
  39. }
  40. static struct notifier_block sleepstate_pm_nb = {
  41. .notifier_call = sleepstate_pm_notifier,
  42. .priority = INT_MAX,
  43. };
  44. static irqreturn_t smp2p_sleepstate_handler(int irq, void *ctxt)
  45. {
  46. __pm_wakeup_event(notify_ws, 200);
  47. return IRQ_HANDLED;
  48. }
  49. static int smp2p_sleepstate_probe(struct platform_device *pdev)
  50. {
  51. int ret;
  52. int irq;
  53. struct device *dev = &pdev->dev;
  54. struct device_node *node = dev->of_node;
  55. state = qcom_smem_state_get(&pdev->dev, 0, &ret);
  56. if (IS_ERR(state))
  57. return PTR_ERR(state);
  58. qcom_smem_state_update_bits(state, AWAKE_BIT, AWAKE_BIT);
  59. ret = register_pm_notifier(&sleepstate_pm_nb);
  60. if (ret) {
  61. dev_err(dev, "%s: power state notif error %d\n", __func__, ret);
  62. return ret;
  63. }
  64. notify_ws = wakeup_source_register(&pdev->dev, "smp2p-sleepstate");
  65. if (!notify_ws) {
  66. ret = -ENOMEM;
  67. goto err_ws;
  68. }
  69. irq = of_irq_get_byname(node, "smp2p-sleepstate-in");
  70. if (irq <= 0) {
  71. dev_err(dev, "failed to get irq for smp2p_sleep_state\n");
  72. ret = -EPROBE_DEFER;
  73. goto err;
  74. }
  75. dev_dbg(dev, "got smp2p-sleepstate-in irq %d\n", irq);
  76. ret = devm_request_threaded_irq(dev, irq, NULL,
  77. smp2p_sleepstate_handler,
  78. IRQF_ONESHOT | IRQF_TRIGGER_RISING,
  79. "smp2p_sleepstate", dev);
  80. if (ret) {
  81. dev_err(dev, "fail to register smp2p threaded_irq=%d\n", irq);
  82. goto err;
  83. }
  84. return 0;
  85. err:
  86. wakeup_source_unregister(notify_ws);
  87. err_ws:
  88. unregister_pm_notifier(&sleepstate_pm_nb);
  89. return ret;
  90. }
  91. static const struct of_device_id smp2p_slst_match_table[] = {
  92. {.compatible = "qcom,smp2p-sleepstate"},
  93. {},
  94. };
  95. static struct platform_driver smp2p_sleepstate_driver = {
  96. .probe = smp2p_sleepstate_probe,
  97. .driver = {
  98. .name = "smp2p_sleepstate",
  99. .of_match_table = smp2p_slst_match_table,
  100. },
  101. };
  102. static int __init smp2p_sleepstate_init(void)
  103. {
  104. int ret;
  105. ret = platform_driver_register(&smp2p_sleepstate_driver);
  106. if (ret) {
  107. pr_err("%s: register failed %d\n", __func__, ret);
  108. return ret;
  109. }
  110. return 0;
  111. }
  112. module_init(smp2p_sleepstate_init);
  113. MODULE_DESCRIPTION("SMP2P SLEEP STATE");
  114. MODULE_LICENSE("GPL");