sm8250.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2020, Linaro Limited
  3. #include <linux/module.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/of_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. #define DRIVER_NAME "sm8250"
  16. #define MI2S_BCLK_RATE 1536000
  17. struct sm8250_snd_data {
  18. bool stream_prepared[AFE_PORT_MAX];
  19. struct snd_soc_card *card;
  20. struct sdw_stream_runtime *sruntime[AFE_PORT_MAX];
  21. struct snd_soc_jack jack;
  22. bool jack_setup;
  23. };
  24. static int sm8250_snd_init(struct snd_soc_pcm_runtime *rtd)
  25. {
  26. struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
  27. return qcom_snd_wcd_jack_setup(rtd, &data->jack, &data->jack_setup);
  28. }
  29. static int sm8250_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
  30. struct snd_pcm_hw_params *params)
  31. {
  32. struct snd_interval *rate = hw_param_interval(params,
  33. SNDRV_PCM_HW_PARAM_RATE);
  34. struct snd_interval *channels = hw_param_interval(params,
  35. SNDRV_PCM_HW_PARAM_CHANNELS);
  36. rate->min = rate->max = 48000;
  37. channels->min = channels->max = 2;
  38. return 0;
  39. }
  40. static int sm8250_snd_startup(struct snd_pcm_substream *substream)
  41. {
  42. unsigned int fmt = SND_SOC_DAIFMT_BP_FP;
  43. unsigned int codec_dai_fmt = SND_SOC_DAIFMT_BC_FC;
  44. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  45. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  46. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  47. switch (cpu_dai->id) {
  48. case TERTIARY_MI2S_RX:
  49. codec_dai_fmt |= SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_I2S;
  50. snd_soc_dai_set_sysclk(cpu_dai,
  51. Q6AFE_LPASS_CLK_ID_TER_MI2S_IBIT,
  52. MI2S_BCLK_RATE, SNDRV_PCM_STREAM_PLAYBACK);
  53. snd_soc_dai_set_fmt(cpu_dai, fmt);
  54. snd_soc_dai_set_fmt(codec_dai, codec_dai_fmt);
  55. break;
  56. default:
  57. break;
  58. }
  59. return 0;
  60. }
  61. static int sm8250_snd_hw_params(struct snd_pcm_substream *substream,
  62. struct snd_pcm_hw_params *params)
  63. {
  64. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  65. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  66. struct sm8250_snd_data *pdata = snd_soc_card_get_drvdata(rtd->card);
  67. return qcom_snd_sdw_hw_params(substream, params, &pdata->sruntime[cpu_dai->id]);
  68. }
  69. static int sm8250_snd_prepare(struct snd_pcm_substream *substream)
  70. {
  71. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  72. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  73. struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
  74. struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
  75. return qcom_snd_sdw_prepare(substream, sruntime,
  76. &data->stream_prepared[cpu_dai->id]);
  77. }
  78. static int sm8250_snd_hw_free(struct snd_pcm_substream *substream)
  79. {
  80. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  81. struct sm8250_snd_data *data = snd_soc_card_get_drvdata(rtd->card);
  82. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  83. struct sdw_stream_runtime *sruntime = data->sruntime[cpu_dai->id];
  84. return qcom_snd_sdw_hw_free(substream, sruntime,
  85. &data->stream_prepared[cpu_dai->id]);
  86. }
  87. static const struct snd_soc_ops sm8250_be_ops = {
  88. .startup = sm8250_snd_startup,
  89. .hw_params = sm8250_snd_hw_params,
  90. .hw_free = sm8250_snd_hw_free,
  91. .prepare = sm8250_snd_prepare,
  92. };
  93. static void sm8250_add_be_ops(struct snd_soc_card *card)
  94. {
  95. struct snd_soc_dai_link *link;
  96. int i;
  97. for_each_card_prelinks(card, i, link) {
  98. if (link->no_pcm == 1) {
  99. link->init = sm8250_snd_init;
  100. link->be_hw_params_fixup = sm8250_be_hw_params_fixup;
  101. link->ops = &sm8250_be_ops;
  102. }
  103. }
  104. }
  105. static int sm8250_platform_probe(struct platform_device *pdev)
  106. {
  107. struct snd_soc_card *card;
  108. struct sm8250_snd_data *data;
  109. struct device *dev = &pdev->dev;
  110. int ret;
  111. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  112. if (!card)
  113. return -ENOMEM;
  114. card->owner = THIS_MODULE;
  115. /* Allocate the private data */
  116. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  117. if (!data)
  118. return -ENOMEM;
  119. card->dev = dev;
  120. dev_set_drvdata(dev, card);
  121. snd_soc_card_set_drvdata(card, data);
  122. ret = qcom_snd_parse_of(card);
  123. if (ret)
  124. return ret;
  125. card->driver_name = DRIVER_NAME;
  126. sm8250_add_be_ops(card);
  127. return devm_snd_soc_register_card(dev, card);
  128. }
  129. static const struct of_device_id snd_sm8250_dt_match[] = {
  130. {.compatible = "qcom,sm8250-sndcard"},
  131. {.compatible = "qcom,qrb5165-rb5-sndcard"},
  132. {}
  133. };
  134. MODULE_DEVICE_TABLE(of, snd_sm8250_dt_match);
  135. static struct platform_driver snd_sm8250_driver = {
  136. .probe = sm8250_platform_probe,
  137. .driver = {
  138. .name = "snd-sm8250",
  139. .of_match_table = snd_sm8250_dt_match,
  140. },
  141. };
  142. module_platform_driver(snd_sm8250_driver);
  143. MODULE_AUTHOR("Srinivas Kandagatla <[email protected]");
  144. MODULE_DESCRIPTION("SM8250 ASoC Machine Driver");
  145. MODULE_LICENSE("GPL v2");