mt8173-rt5650-rt5514.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mt8173-rt5650-rt5514.c -- MT8173 machine driver with RT5650/5514 codecs
  4. *
  5. * Copyright (c) 2016 MediaTek Inc.
  6. * Author: Koro Chen <koro.chen@mediatek.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/gpio.h>
  10. #include <linux/of_gpio.h>
  11. #include <sound/soc.h>
  12. #include <sound/jack.h>
  13. #include "../../codecs/rt5645.h"
  14. #define MCLK_FOR_CODECS 12288000
  15. static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_widgets[] = {
  16. SND_SOC_DAPM_SPK("Speaker", NULL),
  17. SND_SOC_DAPM_MIC("Int Mic", NULL),
  18. SND_SOC_DAPM_HP("Headphone", NULL),
  19. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  20. };
  21. static const struct snd_soc_dapm_route mt8173_rt5650_rt5514_routes[] = {
  22. {"Speaker", NULL, "SPOL"},
  23. {"Speaker", NULL, "SPOR"},
  24. {"Sub DMIC1L", NULL, "Int Mic"},
  25. {"Sub DMIC1R", NULL, "Int Mic"},
  26. {"Headphone", NULL, "HPOL"},
  27. {"Headphone", NULL, "HPOR"},
  28. {"IN1P", NULL, "Headset Mic"},
  29. {"IN1N", NULL, "Headset Mic"},
  30. };
  31. static const struct snd_kcontrol_new mt8173_rt5650_rt5514_controls[] = {
  32. SOC_DAPM_PIN_SWITCH("Speaker"),
  33. SOC_DAPM_PIN_SWITCH("Int Mic"),
  34. SOC_DAPM_PIN_SWITCH("Headphone"),
  35. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  36. };
  37. static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream *substream,
  38. struct snd_pcm_hw_params *params)
  39. {
  40. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  41. int i, ret;
  42. for (i = 0; i < rtd->num_codecs; i++) {
  43. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  44. /* pll from mclk 12.288M */
  45. ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
  46. params_rate(params) * 512);
  47. if (ret)
  48. return ret;
  49. /* sysclk from pll */
  50. ret = snd_soc_dai_set_sysclk(codec_dai, 1,
  51. params_rate(params) * 512,
  52. SND_SOC_CLOCK_IN);
  53. if (ret)
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = {
  59. .hw_params = mt8173_rt5650_rt5514_hw_params,
  60. };
  61. static struct snd_soc_jack mt8173_rt5650_rt5514_jack;
  62. static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime)
  63. {
  64. struct snd_soc_card *card = runtime->card;
  65. struct snd_soc_component *component = runtime->codec_dais[0]->component;
  66. int ret;
  67. rt5645_sel_asrc_clk_src(component,
  68. RT5645_DA_STEREO_FILTER |
  69. RT5645_AD_STEREO_FILTER,
  70. RT5645_CLK_SEL_I2S1_ASRC);
  71. /* enable jack detection */
  72. ret = snd_soc_card_jack_new(card, "Headset Jack",
  73. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  74. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  75. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  76. &mt8173_rt5650_rt5514_jack, NULL, 0);
  77. if (ret) {
  78. dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
  79. return ret;
  80. }
  81. return rt5645_set_jack_detect(component,
  82. &mt8173_rt5650_rt5514_jack,
  83. &mt8173_rt5650_rt5514_jack,
  84. &mt8173_rt5650_rt5514_jack);
  85. }
  86. static struct snd_soc_dai_link_component mt8173_rt5650_rt5514_codecs[] = {
  87. {
  88. .dai_name = "rt5645-aif1",
  89. },
  90. {
  91. .dai_name = "rt5514-aif1",
  92. },
  93. };
  94. enum {
  95. DAI_LINK_PLAYBACK,
  96. DAI_LINK_CAPTURE,
  97. DAI_LINK_CODEC_I2S,
  98. };
  99. /* Digital audio interface glue - connects codec <---> CPU */
  100. static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = {
  101. /* Front End DAI links */
  102. [DAI_LINK_PLAYBACK] = {
  103. .name = "rt5650_rt5514 Playback",
  104. .stream_name = "rt5650_rt5514 Playback",
  105. .cpu_dai_name = "DL1",
  106. .codec_name = "snd-soc-dummy",
  107. .codec_dai_name = "snd-soc-dummy-dai",
  108. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  109. .dynamic = 1,
  110. .dpcm_playback = 1,
  111. },
  112. [DAI_LINK_CAPTURE] = {
  113. .name = "rt5650_rt5514 Capture",
  114. .stream_name = "rt5650_rt5514 Capture",
  115. .cpu_dai_name = "VUL",
  116. .codec_name = "snd-soc-dummy",
  117. .codec_dai_name = "snd-soc-dummy-dai",
  118. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  119. .dynamic = 1,
  120. .dpcm_capture = 1,
  121. },
  122. /* Back End DAI links */
  123. [DAI_LINK_CODEC_I2S] = {
  124. .name = "Codec",
  125. .cpu_dai_name = "I2S",
  126. .no_pcm = 1,
  127. .codecs = mt8173_rt5650_rt5514_codecs,
  128. .num_codecs = 2,
  129. .init = mt8173_rt5650_rt5514_init,
  130. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  131. SND_SOC_DAIFMT_CBS_CFS,
  132. .ops = &mt8173_rt5650_rt5514_ops,
  133. .ignore_pmdown_time = 1,
  134. .dpcm_playback = 1,
  135. .dpcm_capture = 1,
  136. },
  137. };
  138. static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = {
  139. {
  140. .name_prefix = "Sub",
  141. },
  142. };
  143. static struct snd_soc_card mt8173_rt5650_rt5514_card = {
  144. .name = "mtk-rt5650-rt5514",
  145. .owner = THIS_MODULE,
  146. .dai_link = mt8173_rt5650_rt5514_dais,
  147. .num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais),
  148. .codec_conf = mt8173_rt5650_rt5514_codec_conf,
  149. .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf),
  150. .controls = mt8173_rt5650_rt5514_controls,
  151. .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls),
  152. .dapm_widgets = mt8173_rt5650_rt5514_widgets,
  153. .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets),
  154. .dapm_routes = mt8173_rt5650_rt5514_routes,
  155. .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes),
  156. };
  157. static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev)
  158. {
  159. struct snd_soc_card *card = &mt8173_rt5650_rt5514_card;
  160. struct device_node *platform_node;
  161. int i, ret;
  162. platform_node = of_parse_phandle(pdev->dev.of_node,
  163. "mediatek,platform", 0);
  164. if (!platform_node) {
  165. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  166. return -EINVAL;
  167. }
  168. for (i = 0; i < card->num_links; i++) {
  169. if (mt8173_rt5650_rt5514_dais[i].platform_name)
  170. continue;
  171. mt8173_rt5650_rt5514_dais[i].platform_of_node = platform_node;
  172. }
  173. mt8173_rt5650_rt5514_codecs[0].of_node =
  174. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
  175. if (!mt8173_rt5650_rt5514_codecs[0].of_node) {
  176. dev_err(&pdev->dev,
  177. "Property 'audio-codec' missing or invalid\n");
  178. return -EINVAL;
  179. }
  180. mt8173_rt5650_rt5514_codecs[1].of_node =
  181. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
  182. if (!mt8173_rt5650_rt5514_codecs[1].of_node) {
  183. dev_err(&pdev->dev,
  184. "Property 'audio-codec' missing or invalid\n");
  185. return -EINVAL;
  186. }
  187. mt8173_rt5650_rt5514_codec_conf[0].of_node =
  188. mt8173_rt5650_rt5514_codecs[1].of_node;
  189. card->dev = &pdev->dev;
  190. ret = devm_snd_soc_register_card(&pdev->dev, card);
  191. if (ret)
  192. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  193. __func__, ret);
  194. return ret;
  195. }
  196. static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = {
  197. { .compatible = "mediatek,mt8173-rt5650-rt5514", },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match);
  201. static struct platform_driver mt8173_rt5650_rt5514_driver = {
  202. .driver = {
  203. .name = "mtk-rt5650-rt5514",
  204. .of_match_table = mt8173_rt5650_rt5514_dt_match,
  205. #ifdef CONFIG_PM
  206. .pm = &snd_soc_pm_ops,
  207. #endif
  208. },
  209. .probe = mt8173_rt5650_rt5514_dev_probe,
  210. };
  211. module_platform_driver(mt8173_rt5650_rt5514_driver);
  212. /* Module information */
  213. MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
  214. MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
  215. MODULE_LICENSE("GPL v2");
  216. MODULE_ALIAS("platform:mtk-rt5650-rt5514");