mt8173-rt5650-rt5676.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mt8173-rt5650-rt5676.c -- MT8173 machine driver with RT5650/5676 codecs
  4. *
  5. * Copyright (c) 2015 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. #include "../../codecs/rt5677.h"
  15. #define MCLK_FOR_CODECS 12288000
  16. static const struct snd_soc_dapm_widget mt8173_rt5650_rt5676_widgets[] = {
  17. SND_SOC_DAPM_SPK("Speaker", NULL),
  18. SND_SOC_DAPM_MIC("Int Mic", NULL),
  19. SND_SOC_DAPM_HP("Headphone", NULL),
  20. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  21. };
  22. static const struct snd_soc_dapm_route mt8173_rt5650_rt5676_routes[] = {
  23. {"Speaker", NULL, "SPOL"},
  24. {"Speaker", NULL, "SPOR"},
  25. {"Speaker", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
  26. {"Sub DMIC L1", NULL, "Int Mic"}, /* DMIC from 5676 */
  27. {"Sub DMIC R1", NULL, "Int Mic"},
  28. {"Headphone", NULL, "HPOL"},
  29. {"Headphone", NULL, "HPOR"},
  30. {"Headphone", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
  31. {"IN1P", NULL, "Headset Mic"},
  32. {"IN1N", NULL, "Headset Mic"},
  33. {"Sub AIF2RX", NULL, "Headset Mic"}, /* IF2 DAC from 5650 */
  34. };
  35. static const struct snd_kcontrol_new mt8173_rt5650_rt5676_controls[] = {
  36. SOC_DAPM_PIN_SWITCH("Speaker"),
  37. SOC_DAPM_PIN_SWITCH("Int Mic"),
  38. SOC_DAPM_PIN_SWITCH("Headphone"),
  39. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  40. };
  41. static int mt8173_rt5650_rt5676_hw_params(struct snd_pcm_substream *substream,
  42. struct snd_pcm_hw_params *params)
  43. {
  44. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  45. int i, ret;
  46. for (i = 0; i < rtd->num_codecs; i++) {
  47. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  48. /* pll from mclk 12.288M */
  49. ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
  50. params_rate(params) * 512);
  51. if (ret)
  52. return ret;
  53. /* sysclk from pll */
  54. ret = snd_soc_dai_set_sysclk(codec_dai, 1,
  55. params_rate(params) * 512,
  56. SND_SOC_CLOCK_IN);
  57. if (ret)
  58. return ret;
  59. }
  60. return 0;
  61. }
  62. static const struct snd_soc_ops mt8173_rt5650_rt5676_ops = {
  63. .hw_params = mt8173_rt5650_rt5676_hw_params,
  64. };
  65. static struct snd_soc_jack mt8173_rt5650_rt5676_jack;
  66. static int mt8173_rt5650_rt5676_init(struct snd_soc_pcm_runtime *runtime)
  67. {
  68. struct snd_soc_card *card = runtime->card;
  69. struct snd_soc_component *component = runtime->codec_dais[0]->component;
  70. struct snd_soc_component *component_sub = runtime->codec_dais[1]->component;
  71. int ret;
  72. rt5645_sel_asrc_clk_src(component,
  73. RT5645_DA_STEREO_FILTER |
  74. RT5645_AD_STEREO_FILTER,
  75. RT5645_CLK_SEL_I2S1_ASRC);
  76. rt5677_sel_asrc_clk_src(component_sub,
  77. RT5677_DA_STEREO_FILTER |
  78. RT5677_AD_STEREO1_FILTER,
  79. RT5677_CLK_SEL_I2S1_ASRC);
  80. rt5677_sel_asrc_clk_src(component_sub,
  81. RT5677_AD_STEREO2_FILTER |
  82. RT5677_I2S2_SOURCE,
  83. RT5677_CLK_SEL_I2S2_ASRC);
  84. /* enable jack detection */
  85. ret = snd_soc_card_jack_new(card, "Headset Jack",
  86. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  87. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  88. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  89. &mt8173_rt5650_rt5676_jack, NULL, 0);
  90. if (ret) {
  91. dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
  92. return ret;
  93. }
  94. return rt5645_set_jack_detect(component,
  95. &mt8173_rt5650_rt5676_jack,
  96. &mt8173_rt5650_rt5676_jack,
  97. &mt8173_rt5650_rt5676_jack);
  98. }
  99. static struct snd_soc_dai_link_component mt8173_rt5650_rt5676_codecs[] = {
  100. {
  101. .dai_name = "rt5645-aif1",
  102. },
  103. {
  104. .dai_name = "rt5677-aif1",
  105. },
  106. };
  107. enum {
  108. DAI_LINK_PLAYBACK,
  109. DAI_LINK_CAPTURE,
  110. DAI_LINK_HDMI,
  111. DAI_LINK_CODEC_I2S,
  112. DAI_LINK_HDMI_I2S,
  113. DAI_LINK_INTERCODEC
  114. };
  115. /* Digital audio interface glue - connects codec <---> CPU */
  116. static struct snd_soc_dai_link mt8173_rt5650_rt5676_dais[] = {
  117. /* Front End DAI links */
  118. [DAI_LINK_PLAYBACK] = {
  119. .name = "rt5650_rt5676 Playback",
  120. .stream_name = "rt5650_rt5676 Playback",
  121. .cpu_dai_name = "DL1",
  122. .codec_name = "snd-soc-dummy",
  123. .codec_dai_name = "snd-soc-dummy-dai",
  124. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  125. .dynamic = 1,
  126. .dpcm_playback = 1,
  127. },
  128. [DAI_LINK_CAPTURE] = {
  129. .name = "rt5650_rt5676 Capture",
  130. .stream_name = "rt5650_rt5676 Capture",
  131. .cpu_dai_name = "VUL",
  132. .codec_name = "snd-soc-dummy",
  133. .codec_dai_name = "snd-soc-dummy-dai",
  134. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  135. .dynamic = 1,
  136. .dpcm_capture = 1,
  137. },
  138. [DAI_LINK_HDMI] = {
  139. .name = "HDMI",
  140. .stream_name = "HDMI PCM",
  141. .cpu_dai_name = "HDMI",
  142. .codec_name = "snd-soc-dummy",
  143. .codec_dai_name = "snd-soc-dummy-dai",
  144. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  145. .dynamic = 1,
  146. .dpcm_playback = 1,
  147. },
  148. /* Back End DAI links */
  149. [DAI_LINK_CODEC_I2S] = {
  150. .name = "Codec",
  151. .cpu_dai_name = "I2S",
  152. .no_pcm = 1,
  153. .codecs = mt8173_rt5650_rt5676_codecs,
  154. .num_codecs = 2,
  155. .init = mt8173_rt5650_rt5676_init,
  156. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  157. SND_SOC_DAIFMT_CBS_CFS,
  158. .ops = &mt8173_rt5650_rt5676_ops,
  159. .ignore_pmdown_time = 1,
  160. .dpcm_playback = 1,
  161. .dpcm_capture = 1,
  162. },
  163. [DAI_LINK_HDMI_I2S] = {
  164. .name = "HDMI BE",
  165. .cpu_dai_name = "HDMIO",
  166. .no_pcm = 1,
  167. .codec_dai_name = "i2s-hifi",
  168. .dpcm_playback = 1,
  169. },
  170. /* rt5676 <-> rt5650 intercodec link: Sets rt5676 I2S2 as master */
  171. [DAI_LINK_INTERCODEC] = {
  172. .name = "rt5650_rt5676 intercodec",
  173. .stream_name = "rt5650_rt5676 intercodec",
  174. .cpu_dai_name = "snd-soc-dummy-dai",
  175. .platform_name = "snd-soc-dummy",
  176. .no_pcm = 1,
  177. .codec_dai_name = "rt5677-aif2",
  178. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  179. SND_SOC_DAIFMT_CBM_CFM,
  180. },
  181. };
  182. static struct snd_soc_codec_conf mt8173_rt5650_rt5676_codec_conf[] = {
  183. {
  184. .name_prefix = "Sub",
  185. },
  186. };
  187. static struct snd_soc_card mt8173_rt5650_rt5676_card = {
  188. .name = "mtk-rt5650-rt5676",
  189. .owner = THIS_MODULE,
  190. .dai_link = mt8173_rt5650_rt5676_dais,
  191. .num_links = ARRAY_SIZE(mt8173_rt5650_rt5676_dais),
  192. .codec_conf = mt8173_rt5650_rt5676_codec_conf,
  193. .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5676_codec_conf),
  194. .controls = mt8173_rt5650_rt5676_controls,
  195. .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5676_controls),
  196. .dapm_widgets = mt8173_rt5650_rt5676_widgets,
  197. .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5676_widgets),
  198. .dapm_routes = mt8173_rt5650_rt5676_routes,
  199. .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5676_routes),
  200. };
  201. static int mt8173_rt5650_rt5676_dev_probe(struct platform_device *pdev)
  202. {
  203. struct snd_soc_card *card = &mt8173_rt5650_rt5676_card;
  204. struct device_node *platform_node;
  205. int i, ret;
  206. platform_node = of_parse_phandle(pdev->dev.of_node,
  207. "mediatek,platform", 0);
  208. if (!platform_node) {
  209. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  210. return -EINVAL;
  211. }
  212. for (i = 0; i < card->num_links; i++) {
  213. if (mt8173_rt5650_rt5676_dais[i].platform_name)
  214. continue;
  215. mt8173_rt5650_rt5676_dais[i].platform_of_node = platform_node;
  216. }
  217. mt8173_rt5650_rt5676_codecs[0].of_node =
  218. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
  219. if (!mt8173_rt5650_rt5676_codecs[0].of_node) {
  220. dev_err(&pdev->dev,
  221. "Property 'audio-codec' missing or invalid\n");
  222. return -EINVAL;
  223. }
  224. mt8173_rt5650_rt5676_codecs[1].of_node =
  225. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
  226. if (!mt8173_rt5650_rt5676_codecs[1].of_node) {
  227. dev_err(&pdev->dev,
  228. "Property 'audio-codec' missing or invalid\n");
  229. return -EINVAL;
  230. }
  231. mt8173_rt5650_rt5676_codec_conf[0].of_node =
  232. mt8173_rt5650_rt5676_codecs[1].of_node;
  233. mt8173_rt5650_rt5676_dais[DAI_LINK_INTERCODEC].codec_of_node =
  234. mt8173_rt5650_rt5676_codecs[1].of_node;
  235. mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codec_of_node =
  236. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 2);
  237. if (!mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codec_of_node) {
  238. dev_err(&pdev->dev,
  239. "Property 'audio-codec' missing or invalid\n");
  240. return -EINVAL;
  241. }
  242. card->dev = &pdev->dev;
  243. ret = devm_snd_soc_register_card(&pdev->dev, card);
  244. if (ret)
  245. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  246. __func__, ret);
  247. return ret;
  248. }
  249. static const struct of_device_id mt8173_rt5650_rt5676_dt_match[] = {
  250. { .compatible = "mediatek,mt8173-rt5650-rt5676", },
  251. { }
  252. };
  253. MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5676_dt_match);
  254. static struct platform_driver mt8173_rt5650_rt5676_driver = {
  255. .driver = {
  256. .name = "mtk-rt5650-rt5676",
  257. .of_match_table = mt8173_rt5650_rt5676_dt_match,
  258. #ifdef CONFIG_PM
  259. .pm = &snd_soc_pm_ops,
  260. #endif
  261. },
  262. .probe = mt8173_rt5650_rt5676_dev_probe,
  263. };
  264. module_platform_driver(mt8173_rt5650_rt5676_driver);
  265. /* Module information */
  266. MODULE_DESCRIPTION("MT8173 RT5650 and RT5676 SoC machine driver");
  267. MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
  268. MODULE_LICENSE("GPL v2");
  269. MODULE_ALIAS("platform:mtk-rt5650-rt5676");