mt2701-wm8960.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mt2701-wm8960.c -- MT2701 WM8960 ALSA SoC machine driver
  4. *
  5. * Copyright (c) 2017 MediaTek Inc.
  6. * Author: Ryder Lee <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <sound/soc.h>
  10. #include "mt2701-afe-common.h"
  11. static const struct snd_soc_dapm_widget mt2701_wm8960_widgets[] = {
  12. SND_SOC_DAPM_HP("Headphone", NULL),
  13. SND_SOC_DAPM_MIC("AMIC", NULL),
  14. };
  15. static const struct snd_kcontrol_new mt2701_wm8960_controls[] = {
  16. SOC_DAPM_PIN_SWITCH("Headphone"),
  17. SOC_DAPM_PIN_SWITCH("AMIC"),
  18. };
  19. static int mt2701_wm8960_be_ops_hw_params(struct snd_pcm_substream *substream,
  20. struct snd_pcm_hw_params *params)
  21. {
  22. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  23. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  24. struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  25. unsigned int mclk_rate;
  26. unsigned int rate = params_rate(params);
  27. unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;
  28. unsigned int div_bck_over_lrck = 64;
  29. mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
  30. snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
  31. snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
  32. return 0;
  33. }
  34. static const struct snd_soc_ops mt2701_wm8960_be_ops = {
  35. .hw_params = mt2701_wm8960_be_ops_hw_params
  36. };
  37. SND_SOC_DAILINK_DEFS(playback,
  38. DAILINK_COMP_ARRAY(COMP_CPU("PCMO0")),
  39. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  40. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  41. SND_SOC_DAILINK_DEFS(capture,
  42. DAILINK_COMP_ARRAY(COMP_CPU("PCM0")),
  43. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  44. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  45. SND_SOC_DAILINK_DEFS(codec,
  46. DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
  47. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8960-hifi")),
  48. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  49. static struct snd_soc_dai_link mt2701_wm8960_dai_links[] = {
  50. /* FE */
  51. {
  52. .name = "wm8960-playback",
  53. .stream_name = "wm8960-playback",
  54. .trigger = {SND_SOC_DPCM_TRIGGER_POST,
  55. SND_SOC_DPCM_TRIGGER_POST},
  56. .dynamic = 1,
  57. .dpcm_playback = 1,
  58. SND_SOC_DAILINK_REG(playback),
  59. },
  60. {
  61. .name = "wm8960-capture",
  62. .stream_name = "wm8960-capture",
  63. .trigger = {SND_SOC_DPCM_TRIGGER_POST,
  64. SND_SOC_DPCM_TRIGGER_POST},
  65. .dynamic = 1,
  66. .dpcm_capture = 1,
  67. SND_SOC_DAILINK_REG(capture),
  68. },
  69. /* BE */
  70. {
  71. .name = "wm8960-codec",
  72. .no_pcm = 1,
  73. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
  74. | SND_SOC_DAIFMT_GATED,
  75. .ops = &mt2701_wm8960_be_ops,
  76. .dpcm_playback = 1,
  77. .dpcm_capture = 1,
  78. SND_SOC_DAILINK_REG(codec),
  79. },
  80. };
  81. static struct snd_soc_card mt2701_wm8960_card = {
  82. .name = "mt2701-wm8960",
  83. .owner = THIS_MODULE,
  84. .dai_link = mt2701_wm8960_dai_links,
  85. .num_links = ARRAY_SIZE(mt2701_wm8960_dai_links),
  86. .controls = mt2701_wm8960_controls,
  87. .num_controls = ARRAY_SIZE(mt2701_wm8960_controls),
  88. .dapm_widgets = mt2701_wm8960_widgets,
  89. .num_dapm_widgets = ARRAY_SIZE(mt2701_wm8960_widgets),
  90. };
  91. static int mt2701_wm8960_machine_probe(struct platform_device *pdev)
  92. {
  93. struct snd_soc_card *card = &mt2701_wm8960_card;
  94. struct device_node *platform_node, *codec_node;
  95. struct snd_soc_dai_link *dai_link;
  96. int ret, i;
  97. platform_node = of_parse_phandle(pdev->dev.of_node,
  98. "mediatek,platform", 0);
  99. if (!platform_node) {
  100. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  101. return -EINVAL;
  102. }
  103. for_each_card_prelinks(card, i, dai_link) {
  104. if (dai_link->platforms->name)
  105. continue;
  106. dai_link->platforms->of_node = platform_node;
  107. }
  108. card->dev = &pdev->dev;
  109. codec_node = of_parse_phandle(pdev->dev.of_node,
  110. "mediatek,audio-codec", 0);
  111. if (!codec_node) {
  112. dev_err(&pdev->dev,
  113. "Property 'audio-codec' missing or invalid\n");
  114. ret = -EINVAL;
  115. goto put_platform_node;
  116. }
  117. for_each_card_prelinks(card, i, dai_link) {
  118. if (dai_link->codecs->name)
  119. continue;
  120. dai_link->codecs->of_node = codec_node;
  121. }
  122. ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
  123. if (ret) {
  124. dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
  125. goto put_codec_node;
  126. }
  127. ret = devm_snd_soc_register_card(&pdev->dev, card);
  128. if (ret)
  129. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  130. __func__, ret);
  131. put_codec_node:
  132. of_node_put(codec_node);
  133. put_platform_node:
  134. of_node_put(platform_node);
  135. return ret;
  136. }
  137. #ifdef CONFIG_OF
  138. static const struct of_device_id mt2701_wm8960_machine_dt_match[] = {
  139. {.compatible = "mediatek,mt2701-wm8960-machine",},
  140. {}
  141. };
  142. #endif
  143. static struct platform_driver mt2701_wm8960_machine = {
  144. .driver = {
  145. .name = "mt2701-wm8960",
  146. #ifdef CONFIG_OF
  147. .of_match_table = mt2701_wm8960_machine_dt_match,
  148. #endif
  149. },
  150. .probe = mt2701_wm8960_machine_probe,
  151. };
  152. module_platform_driver(mt2701_wm8960_machine);
  153. /* Module information */
  154. MODULE_DESCRIPTION("MT2701 WM8960 ALSA SoC machine driver");
  155. MODULE_AUTHOR("Ryder Lee <[email protected]>");
  156. MODULE_LICENSE("GPL v2");
  157. MODULE_ALIAS("mt2701 wm8960 soc card");