sdw.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018-2023, Linaro Limited.
  3. // Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. #include <dt-bindings/sound/qcom,lpass.h>
  5. #include <dt-bindings/sound/qcom,q6afe.h>
  6. #include <linux/module.h>
  7. #include <sound/soc.h>
  8. #include "sdw.h"
  9. static bool qcom_snd_is_sdw_dai(int id)
  10. {
  11. switch (id) {
  12. case WSA_CODEC_DMA_RX_0:
  13. case WSA_CODEC_DMA_TX_0:
  14. case WSA_CODEC_DMA_RX_1:
  15. case WSA_CODEC_DMA_TX_1:
  16. case WSA_CODEC_DMA_TX_2:
  17. case RX_CODEC_DMA_RX_0:
  18. case TX_CODEC_DMA_TX_0:
  19. case RX_CODEC_DMA_RX_1:
  20. case TX_CODEC_DMA_TX_1:
  21. case RX_CODEC_DMA_RX_2:
  22. case TX_CODEC_DMA_TX_2:
  23. case RX_CODEC_DMA_RX_3:
  24. case TX_CODEC_DMA_TX_3:
  25. case RX_CODEC_DMA_RX_4:
  26. case TX_CODEC_DMA_TX_4:
  27. case RX_CODEC_DMA_RX_5:
  28. case TX_CODEC_DMA_TX_5:
  29. case RX_CODEC_DMA_RX_6:
  30. case RX_CODEC_DMA_RX_7:
  31. case SLIMBUS_0_RX...SLIMBUS_6_TX:
  32. return true;
  33. default:
  34. break;
  35. }
  36. /* DSP Bypass usecase, cpu dai index overlaps with DSP dai ids,
  37. * DO NOT MERGE into top switch case */
  38. switch (id) {
  39. case LPASS_CDC_DMA_TX3:
  40. case LPASS_CDC_DMA_RX0:
  41. return true;
  42. default:
  43. break;
  44. }
  45. return false;
  46. }
  47. /**
  48. * qcom_snd_sdw_startup() - Helper to start Soundwire stream for SoC audio card
  49. * @substream: The PCM substream from audio, as passed to snd_soc_ops->startup()
  50. *
  51. * Helper for the SoC audio card (snd_soc_ops->startup()) to allocate and set
  52. * Soundwire stream runtime to each codec DAI.
  53. *
  54. * The shutdown() callback should call sdw_release_stream() on the same
  55. * sdw_stream_runtime.
  56. *
  57. * Return: 0 or errno
  58. */
  59. int qcom_snd_sdw_startup(struct snd_pcm_substream *substream)
  60. {
  61. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  62. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  63. u32 rx_ch[SDW_MAX_PORTS], tx_ch[SDW_MAX_PORTS];
  64. struct sdw_stream_runtime *sruntime;
  65. struct snd_soc_dai *codec_dai;
  66. u32 rx_ch_cnt = 0, tx_ch_cnt = 0;
  67. int ret, i, j;
  68. if (!qcom_snd_is_sdw_dai(cpu_dai->id))
  69. return 0;
  70. sruntime = sdw_alloc_stream(cpu_dai->name, SDW_STREAM_PCM);
  71. if (!sruntime)
  72. return -ENOMEM;
  73. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  74. ret = snd_soc_dai_set_stream(codec_dai, sruntime,
  75. substream->stream);
  76. if (ret < 0 && ret != -ENOTSUPP) {
  77. dev_err(rtd->dev, "Failed to set sdw stream on %s\n", codec_dai->name);
  78. goto err_set_stream;
  79. } else if (ret == -ENOTSUPP) {
  80. /* Ignore unsupported */
  81. continue;
  82. }
  83. ret = snd_soc_dai_get_channel_map(codec_dai, &tx_ch_cnt, tx_ch,
  84. &rx_ch_cnt, rx_ch);
  85. if (ret != 0 && ret != -ENOTSUPP) {
  86. dev_err(rtd->dev, "Failed to get codec chan map %s\n", codec_dai->name);
  87. goto err_set_stream;
  88. } else if (ret == -ENOTSUPP) {
  89. /* Ignore unsupported */
  90. continue;
  91. }
  92. }
  93. switch (cpu_dai->id) {
  94. case RX_CODEC_DMA_RX_0:
  95. case TX_CODEC_DMA_TX_3:
  96. if (tx_ch_cnt || rx_ch_cnt) {
  97. for_each_rtd_codec_dais(rtd, j, codec_dai) {
  98. ret = snd_soc_dai_set_channel_map(codec_dai,
  99. tx_ch_cnt, tx_ch,
  100. rx_ch_cnt, rx_ch);
  101. if (ret != 0 && ret != -ENOTSUPP)
  102. goto err_set_stream;
  103. }
  104. }
  105. }
  106. return 0;
  107. err_set_stream:
  108. sdw_release_stream(sruntime);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL_GPL(qcom_snd_sdw_startup);
  112. int qcom_snd_sdw_prepare(struct snd_pcm_substream *substream,
  113. bool *stream_prepared)
  114. {
  115. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  116. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  117. struct sdw_stream_runtime *sruntime;
  118. int ret;
  119. if (!qcom_snd_is_sdw_dai(cpu_dai->id))
  120. return 0;
  121. sruntime = qcom_snd_sdw_get_stream(substream);
  122. if (!sruntime)
  123. return 0;
  124. if (*stream_prepared)
  125. return 0;
  126. ret = sdw_prepare_stream(sruntime);
  127. if (ret)
  128. return ret;
  129. /**
  130. * NOTE: there is a strict hw requirement about the ordering of port
  131. * enables and actual WSA881x PA enable. PA enable should only happen
  132. * after soundwire ports are enabled if not DC on the line is
  133. * accumulated resulting in Click/Pop Noise
  134. * PA enable/mute are handled as part of codec DAPM and digital mute.
  135. */
  136. ret = sdw_enable_stream(sruntime);
  137. if (ret) {
  138. sdw_deprepare_stream(sruntime);
  139. return ret;
  140. }
  141. *stream_prepared = true;
  142. return ret;
  143. }
  144. EXPORT_SYMBOL_GPL(qcom_snd_sdw_prepare);
  145. struct sdw_stream_runtime *qcom_snd_sdw_get_stream(struct snd_pcm_substream *substream)
  146. {
  147. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  148. struct snd_soc_dai *codec_dai;
  149. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  150. struct sdw_stream_runtime *sruntime;
  151. int i;
  152. if (!qcom_snd_is_sdw_dai(cpu_dai->id))
  153. return NULL;
  154. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  155. sruntime = snd_soc_dai_get_stream(codec_dai, substream->stream);
  156. if (sruntime != ERR_PTR(-ENOTSUPP))
  157. return sruntime;
  158. }
  159. return NULL;
  160. }
  161. EXPORT_SYMBOL_GPL(qcom_snd_sdw_get_stream);
  162. void qcom_snd_sdw_shutdown(struct snd_pcm_substream *substream)
  163. {
  164. struct sdw_stream_runtime *sruntime = qcom_snd_sdw_get_stream(substream);
  165. sdw_release_stream(sruntime);
  166. }
  167. EXPORT_SYMBOL_GPL(qcom_snd_sdw_shutdown);
  168. int qcom_snd_sdw_hw_free(struct snd_pcm_substream *substream, bool *stream_prepared)
  169. {
  170. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  171. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  172. struct sdw_stream_runtime *sruntime;
  173. if (!qcom_snd_is_sdw_dai(cpu_dai->id))
  174. return 0;
  175. sruntime = qcom_snd_sdw_get_stream(substream);
  176. if (sruntime && *stream_prepared) {
  177. sdw_disable_stream(sruntime);
  178. sdw_deprepare_stream(sruntime);
  179. *stream_prepared = false;
  180. }
  181. return 0;
  182. }
  183. EXPORT_SYMBOL_GPL(qcom_snd_sdw_hw_free);
  184. MODULE_DESCRIPTION("Qualcomm ASoC SoundWire helper functions");
  185. MODULE_LICENSE("GPL");