mt8173-rt5650-rt5676.c 9.1 KB

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