sc8280xp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2022, Linaro Limited
  3. #include <dt-bindings/sound/qcom,q6afe.h>
  4. #include <linux/module.h>
  5. #include <linux/platform_device.h>
  6. #include <sound/soc.h>
  7. #include <sound/soc-dapm.h>
  8. #include <sound/pcm.h>
  9. #include <linux/soundwire/sdw.h>
  10. #include <sound/jack.h>
  11. #include <linux/input-event-codes.h>
  12. #include "qdsp6/q6afe.h"
  13. #include "common.h"
  14. #include "sdw.h"
  15. struct sc8280xp_snd_data {
  16. bool stream_prepared[AFE_PORT_MAX];
  17. struct snd_soc_card *card;
  18. struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
  19. struct snd_soc_jack jack;
  20. struct snd_soc_jack dp_jack[8];
  21. bool jack_setup;
  22. };
  23. static int sc8280xp_snd_init(struct snd_soc_pcm_runtime *rtd)
  24. {
  25. struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
  26. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  27. struct snd_soc_card *card = rtd->card;
  28. struct snd_soc_jack *dp_jack = NULL;
  29. int dp_pcm_id = 0;
  30. switch (cpu_dai->id) {
  31. case WSA_CODEC_DMA_RX_0:
  32. case WSA_CODEC_DMA_RX_1:
  33. /*
  34. * Set limit of -3 dB on Digital Volume and 0 dB on PA Volume
  35. * to reduce the risk of speaker damage until we have active
  36. * speaker protection in place.
  37. */
  38. snd_soc_limit_volume(card, "WSA_RX0 Digital Volume", 81);
  39. snd_soc_limit_volume(card, "WSA_RX1 Digital Volume", 81);
  40. snd_soc_limit_volume(card, "SpkrLeft PA Volume", 17);
  41. snd_soc_limit_volume(card, "SpkrRight PA Volume", 17);
  42. break;
  43. case DISPLAY_PORT_RX_0:
  44. /* DISPLAY_PORT dai ids are not contiguous */
  45. dp_pcm_id = 0;
  46. dp_jack = &data->dp_jack[dp_pcm_id];
  47. break;
  48. case DISPLAY_PORT_RX_1 ... DISPLAY_PORT_RX_7:
  49. dp_pcm_id = cpu_dai->id - DISPLAY_PORT_RX_1 + 1;
  50. dp_jack = &data->dp_jack[dp_pcm_id];
  51. break;
  52. default:
  53. break;
  54. }
  55. if (dp_jack)
  56. return qcom_snd_dp_jack_setup(rtd, dp_jack, dp_pcm_id);
  57. return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
  58. }
  59. static void sc8280xp_snd_shutdown(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. struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
  64. struct sdw_stream_runtime *sruntime = pdata->sruntime[cpu_dai->id];
  65. pdata->sruntime[cpu_dai->id] = NULL;
  66. sdw_release_stream(sruntime);
  67. }
  68. static int sc8280xp_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  69. struct snd_pcm_hw_params *params)
  70. {
  71. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  72. struct snd_interval *rate = hw_param_interval(params,
  73. SNDRV_PCM_HW_PARAM_RATE);
  74. struct snd_interval *channels = hw_param_interval(params,
  75. SNDRV_PCM_HW_PARAM_CHANNELS);
  76. rate->min = rate->max = 48000;
  77. channels->min = 2;
  78. channels->max = 2;
  79. switch (cpu_dai->id) {
  80. case TX_CODEC_DMA_TX_0:
  81. case TX_CODEC_DMA_TX_1:
  82. case TX_CODEC_DMA_TX_2:
  83. case TX_CODEC_DMA_TX_3:
  84. channels->min = 1;
  85. break;
  86. default:
  87. break;
  88. }
  89. return 0;
  90. }
  91. static int sc8280xp_snd_hw_params(struct snd_pcm_substream *substream,
  92. struct snd_pcm_hw_params *params)
  93. {
  94. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  95. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  96. struct sc8280xp_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
  97. return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
  98. }
  99. static int sc8280xp_snd_prepare(struct snd_pcm_substream *substream)
  100. {
  101. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  102. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  103. struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
  104. struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
  105. return qcom_snd_sdw_prepare(substream, sruntime,
  106. &data->stream_prepared[cpu_dai->id]);
  107. }
  108. static int sc8280xp_snd_hw_free(struct snd_pcm_substream *substream)
  109. {
  110. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  111. struct sc8280xp_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
  112. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  113. struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
  114. return qcom_snd_sdw_hw_free(substream, sruntime,
  115. &data->stream_prepared[cpu_dai->id]);
  116. }
  117. static const struct snd_soc_ops sc8280xp_be_ops = {
  118. .startup = qcom_snd_sdw_startup,
  119. .shutdown = sc8280xp_snd_shutdown,
  120. .hw_params = sc8280xp_snd_hw_params,
  121. .hw_free = sc8280xp_snd_hw_free,
  122. .prepare = sc8280xp_snd_prepare,
  123. };
  124. static void sc8280xp_add_be_ops(struct snd_soc_card *card)
  125. {
  126. struct snd_soc_dai_link *link;
  127. int i;
  128. for_each_card_prelinks(card, i, link) {
  129. if (link->no_pcm == 1) {
  130. link->init = sc8280xp_snd_init;
  131. link->be_hw_params_fixup = sc8280xp_be_hw_params_fixup;
  132. link->ops = &sc8280xp_be_ops;
  133. }
  134. }
  135. }
  136. static int sc8280xp_platform_probe(struct platform_device *pdev)
  137. {
  138. struct snd_soc_card *card;
  139. struct sc8280xp_snd_data *data;
  140. struct device *dev = &pdev->dev;
  141. int ret;
  142. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  143. if (!card)
  144. return -ENOMEM;
  145. card->owner = THIS_MODULE;
  146. /* Allocate the private data */
  147. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  148. if (!data)
  149. return -ENOMEM;
  150. card->dev = dev;
  151. dev_set_drvdata(dev, card);
  152. snd_soc_card_set_drvdata(card, data);
  153. ret = qcom_snd_parse_of(card);
  154. if (ret)
  155. return ret;
  156. card->driver_name = of_device_get_match_data(dev);
  157. sc8280xp_add_be_ops(card);
  158. return devm_snd_soc_register_card(dev, card);
  159. }
  160. static const struct of_device_id snd_sc8280xp_dt_match[] = {
  161. {.compatible = "qcom,qcm6490-idp-sndcard", "qcm6490"},
  162. {.compatible = "qcom,qcs6490-rb3gen2-sndcard", "qcs6490"},
  163. {.compatible = "qcom,sc8280xp-sndcard", "sc8280xp"},
  164. {.compatible = "qcom,sm8450-sndcard", "sm8450"},
  165. {.compatible = "qcom,sm8550-sndcard", "sm8550"},
  166. {.compatible = "qcom,sm8650-sndcard", "sm8650"},
  167. {}
  168. };
  169. MODULE_DEVICE_TABLE(of, snd_sc8280xp_dt_match);
  170. static struct platform_driver snd_sc8280xp_driver = {
  171. .probe = sc8280xp_platform_probe,
  172. .driver = {
  173. .name = "snd-sc8280xp",
  174. .of_match_table = snd_sc8280xp_dt_match,
  175. },
  176. };
  177. module_platform_driver(snd_sc8280xp_driver);
  178. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  179. MODULE_DESCRIPTION("SC8280XP ASoC Machine Driver");
  180. MODULE_LICENSE("GPL");