mt2701-wm8960.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <ryder.lee@mediatek.com>
  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 = substream->private_data;
  23. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  24. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  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 struct snd_soc_ops mt2701_wm8960_be_ops = {
  35. .hw_params = mt2701_wm8960_be_ops_hw_params
  36. };
  37. static struct snd_soc_dai_link mt2701_wm8960_dai_links[] = {
  38. /* FE */
  39. {
  40. .name = "wm8960-playback",
  41. .stream_name = "wm8960-playback",
  42. .cpu_dai_name = "PCMO0",
  43. .codec_name = "snd-soc-dummy",
  44. .codec_dai_name = "snd-soc-dummy-dai",
  45. .trigger = {SND_SOC_DPCM_TRIGGER_POST,
  46. SND_SOC_DPCM_TRIGGER_POST},
  47. .dynamic = 1,
  48. .dpcm_playback = 1,
  49. },
  50. {
  51. .name = "wm8960-capture",
  52. .stream_name = "wm8960-capture",
  53. .cpu_dai_name = "PCM0",
  54. .codec_name = "snd-soc-dummy",
  55. .codec_dai_name = "snd-soc-dummy-dai",
  56. .trigger = {SND_SOC_DPCM_TRIGGER_POST,
  57. SND_SOC_DPCM_TRIGGER_POST},
  58. .dynamic = 1,
  59. .dpcm_capture = 1,
  60. },
  61. /* BE */
  62. {
  63. .name = "wm8960-codec",
  64. .cpu_dai_name = "I2S0",
  65. .no_pcm = 1,
  66. .codec_dai_name = "wm8960-hifi",
  67. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
  68. | SND_SOC_DAIFMT_GATED,
  69. .ops = &mt2701_wm8960_be_ops,
  70. .dpcm_playback = 1,
  71. .dpcm_capture = 1,
  72. },
  73. };
  74. static struct snd_soc_card mt2701_wm8960_card = {
  75. .name = "mt2701-wm8960",
  76. .owner = THIS_MODULE,
  77. .dai_link = mt2701_wm8960_dai_links,
  78. .num_links = ARRAY_SIZE(mt2701_wm8960_dai_links),
  79. .controls = mt2701_wm8960_controls,
  80. .num_controls = ARRAY_SIZE(mt2701_wm8960_controls),
  81. .dapm_widgets = mt2701_wm8960_widgets,
  82. .num_dapm_widgets = ARRAY_SIZE(mt2701_wm8960_widgets),
  83. };
  84. static int mt2701_wm8960_machine_probe(struct platform_device *pdev)
  85. {
  86. struct snd_soc_card *card = &mt2701_wm8960_card;
  87. struct device_node *platform_node, *codec_node;
  88. int ret, i;
  89. platform_node = of_parse_phandle(pdev->dev.of_node,
  90. "mediatek,platform", 0);
  91. if (!platform_node) {
  92. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  93. return -EINVAL;
  94. }
  95. for (i = 0; i < card->num_links; i++) {
  96. if (mt2701_wm8960_dai_links[i].platform_name)
  97. continue;
  98. mt2701_wm8960_dai_links[i].platform_of_node = platform_node;
  99. }
  100. card->dev = &pdev->dev;
  101. codec_node = of_parse_phandle(pdev->dev.of_node,
  102. "mediatek,audio-codec", 0);
  103. if (!codec_node) {
  104. dev_err(&pdev->dev,
  105. "Property 'audio-codec' missing or invalid\n");
  106. return -EINVAL;
  107. }
  108. for (i = 0; i < card->num_links; i++) {
  109. if (mt2701_wm8960_dai_links[i].codec_name)
  110. continue;
  111. mt2701_wm8960_dai_links[i].codec_of_node = codec_node;
  112. }
  113. ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
  114. if (ret) {
  115. dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
  116. return ret;
  117. }
  118. ret = devm_snd_soc_register_card(&pdev->dev, card);
  119. if (ret)
  120. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  121. __func__, ret);
  122. return ret;
  123. }
  124. #ifdef CONFIG_OF
  125. static const struct of_device_id mt2701_wm8960_machine_dt_match[] = {
  126. {.compatible = "mediatek,mt2701-wm8960-machine",},
  127. {}
  128. };
  129. #endif
  130. static struct platform_driver mt2701_wm8960_machine = {
  131. .driver = {
  132. .name = "mt2701-wm8960",
  133. .owner = THIS_MODULE,
  134. #ifdef CONFIG_OF
  135. .of_match_table = mt2701_wm8960_machine_dt_match,
  136. #endif
  137. },
  138. .probe = mt2701_wm8960_machine_probe,
  139. };
  140. module_platform_driver(mt2701_wm8960_machine);
  141. /* Module information */
  142. MODULE_DESCRIPTION("MT2701 WM8960 ALSA SoC machine driver");
  143. MODULE_AUTHOR("Ryder Lee <ryder.lee@mediatek.com>");
  144. MODULE_LICENSE("GPL v2");
  145. MODULE_ALIAS("mt2701 wm8960 soc card");