audio_slimslave.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2013-2014, 2017-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/fs.h>
  8. #include <linux/device.h>
  9. #include <linux/mutex.h>
  10. #include <linux/miscdevice.h>
  11. #include <sound/audio_slimslave.h>
  12. #include <linux/slimbus/slimbus.h>
  13. #include <linux/pm_runtime.h>
  14. static struct slim_device *slim;
  15. static int vote_count;
  16. struct mutex suspend_lock;
  17. bool suspend;
  18. static int audio_slim_open(struct inode *inode, struct file *file)
  19. {
  20. pr_debug("%s:\n", __func__);
  21. if (vote_count) {
  22. pr_debug("%s: unvote: vote_count=%d\n", __func__, vote_count);
  23. pm_runtime_mark_last_busy(slim->dev.parent);
  24. pm_runtime_put(slim->dev.parent);
  25. vote_count--;
  26. }
  27. return 0;
  28. };
  29. static int audio_slim_release(struct inode *inode, struct file *file)
  30. {
  31. pr_debug("%s:\n", __func__);
  32. if (vote_count) {
  33. pr_debug("%s: unvote: vote_count=%d\n", __func__, vote_count);
  34. pm_runtime_mark_last_busy(slim->dev.parent);
  35. pm_runtime_put(slim->dev.parent);
  36. vote_count--;
  37. } else {
  38. pr_debug("%s: vote: vote_count=%d\n", __func__, vote_count);
  39. pm_runtime_get_sync(slim->dev.parent);
  40. vote_count++;
  41. }
  42. return 0;
  43. };
  44. static long audio_slim_ioctl(struct file *file, unsigned int cmd,
  45. unsigned long u_arg)
  46. {
  47. switch (cmd) {
  48. case AUDIO_SLIMSLAVE_VOTE:
  49. mutex_lock(&suspend_lock);
  50. if (!vote_count && !suspend) {
  51. pr_debug("%s:AUDIO_SLIMSLAVE_VOTE\n", __func__);
  52. pm_runtime_get_sync(slim->dev.parent);
  53. vote_count++;
  54. } else {
  55. pr_err("%s:Invalid vote: vote_count=%d suspend=%d\n",
  56. __func__, vote_count, suspend);
  57. }
  58. mutex_unlock(&suspend_lock);
  59. break;
  60. case AUDIO_SLIMSLAVE_UNVOTE:
  61. mutex_lock(&suspend_lock);
  62. if (vote_count && !suspend) {
  63. pr_debug("%s:AUDIO_SLIMSLAVE_UNVOTE\n", __func__);
  64. pm_runtime_mark_last_busy(slim->dev.parent);
  65. pm_runtime_put(slim->dev.parent);
  66. vote_count--;
  67. } else {
  68. pr_err("%s:Invalid unvote: vote_count=%d suspend=%d\n",
  69. __func__, vote_count, suspend);
  70. }
  71. mutex_unlock(&suspend_lock);
  72. break;
  73. default:
  74. pr_debug("%s: Invalid ioctl cmd: %d\n", __func__, cmd);
  75. break;
  76. }
  77. return 0;
  78. }
  79. static const struct file_operations audio_slimslave_fops = {
  80. .open = audio_slim_open,
  81. .unlocked_ioctl = audio_slim_ioctl,
  82. .release = audio_slim_release,
  83. };
  84. struct miscdevice audio_slimslave_misc = {
  85. .minor = MISC_DYNAMIC_MINOR,
  86. .name = AUDIO_SLIMSLAVE_IOCTL_NAME,
  87. .fops = &audio_slimslave_fops,
  88. };
  89. static int audio_slimslave_probe(struct slim_device *audio_slim)
  90. {
  91. pr_debug("%s:\n", __func__);
  92. mutex_init(&suspend_lock);
  93. suspend = false;
  94. slim = audio_slim;
  95. misc_register(&audio_slimslave_misc);
  96. return 0;
  97. }
  98. static int audio_slimslave_remove(struct slim_device *audio_slim)
  99. {
  100. pr_debug("%s:\n", __func__);
  101. misc_deregister(&audio_slimslave_misc);
  102. return 0;
  103. }
  104. static int audio_slimslave_resume(struct slim_device *audio_slim)
  105. {
  106. pr_debug("%s:\n", __func__);
  107. mutex_lock(&suspend_lock);
  108. suspend = false;
  109. mutex_unlock(&suspend_lock);
  110. return 0;
  111. }
  112. static int audio_slimslave_suspend(struct slim_device *audio_slim,
  113. pm_message_t pmesg)
  114. {
  115. pr_debug("%s:\n", __func__);
  116. mutex_lock(&suspend_lock);
  117. suspend = true;
  118. mutex_unlock(&suspend_lock);
  119. return 0;
  120. }
  121. static const struct slim_device_id audio_slimslave_dt_match[] = {
  122. {"audio-slimslave", 0},
  123. {}
  124. };
  125. static struct slim_driver audio_slimslave_driver = {
  126. .driver = {
  127. .name = "audio-slimslave",
  128. .owner = THIS_MODULE,
  129. },
  130. .probe = audio_slimslave_probe,
  131. .remove = audio_slimslave_remove,
  132. .id_table = audio_slimslave_dt_match,
  133. .resume = audio_slimslave_resume,
  134. .suspend = audio_slimslave_suspend,
  135. };
  136. int __init audio_slimslave_init(void)
  137. {
  138. return slim_driver_register(&audio_slimslave_driver);
  139. }
  140. void audio_slimslave_exit(void)
  141. {
  142. slim_driver_unregister(&audio_slimslave_driver);
  143. }
  144. /* Module information */
  145. MODULE_DESCRIPTION("Audio side Slimbus slave driver");
  146. MODULE_LICENSE("GPL v2");