sc8280xp.c 5.6 KB

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