mt8173-rt5650.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * mt8173-rt5650.c -- MT8173 machine driver with RT5650 codecs
  3. *
  4. * Copyright (c) 2016 MediaTek Inc.
  5. * Author: Koro Chen <koro.chen@mediatek.com>
  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. #define MCLK_FOR_CODECS 12288000
  23. static const struct snd_soc_dapm_widget mt8173_rt5650_widgets[] = {
  24. SND_SOC_DAPM_SPK("Speaker", NULL),
  25. SND_SOC_DAPM_MIC("Int Mic", NULL),
  26. SND_SOC_DAPM_HP("Headphone", NULL),
  27. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  28. };
  29. static const struct snd_soc_dapm_route mt8173_rt5650_routes[] = {
  30. {"Speaker", NULL, "SPOL"},
  31. {"Speaker", NULL, "SPOR"},
  32. {"DMIC L1", NULL, "Int Mic"},
  33. {"DMIC R1", NULL, "Int Mic"},
  34. {"Headphone", NULL, "HPOL"},
  35. {"Headphone", NULL, "HPOR"},
  36. {"Headset Mic", NULL, "micbias1"},
  37. {"Headset Mic", NULL, "micbias2"},
  38. {"IN1P", NULL, "Headset Mic"},
  39. {"IN1N", NULL, "Headset Mic"},
  40. };
  41. static const struct snd_kcontrol_new mt8173_rt5650_controls[] = {
  42. SOC_DAPM_PIN_SWITCH("Speaker"),
  43. SOC_DAPM_PIN_SWITCH("Int Mic"),
  44. SOC_DAPM_PIN_SWITCH("Headphone"),
  45. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  46. };
  47. static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
  48. struct snd_pcm_hw_params *params)
  49. {
  50. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  51. int i, ret;
  52. for (i = 0; i < rtd->num_codecs; i++) {
  53. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  54. /* pll from mclk 12.288M */
  55. ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
  56. params_rate(params) * 512);
  57. if (ret)
  58. return ret;
  59. /* sysclk from pll */
  60. ret = snd_soc_dai_set_sysclk(codec_dai, 1,
  61. params_rate(params) * 512,
  62. SND_SOC_CLOCK_IN);
  63. if (ret)
  64. return ret;
  65. }
  66. return 0;
  67. }
  68. static struct snd_soc_ops mt8173_rt5650_ops = {
  69. .hw_params = mt8173_rt5650_hw_params,
  70. };
  71. static struct snd_soc_jack mt8173_rt5650_jack;
  72. static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
  73. {
  74. struct snd_soc_card *card = runtime->card;
  75. struct snd_soc_codec *codec = runtime->codec_dais[0]->codec;
  76. const char *codec_capture_dai = runtime->codec_dais[1]->name;
  77. int ret;
  78. rt5645_sel_asrc_clk_src(codec,
  79. RT5645_DA_STEREO_FILTER,
  80. RT5645_CLK_SEL_I2S1_ASRC);
  81. if (!strcmp(codec_capture_dai, "rt5645-aif1")) {
  82. rt5645_sel_asrc_clk_src(codec,
  83. RT5645_AD_STEREO_FILTER,
  84. RT5645_CLK_SEL_I2S1_ASRC);
  85. } else if (!strcmp(codec_capture_dai, "rt5645-aif2")) {
  86. rt5645_sel_asrc_clk_src(codec,
  87. RT5645_AD_STEREO_FILTER,
  88. RT5645_CLK_SEL_I2S2_ASRC);
  89. } else {
  90. dev_warn(card->dev,
  91. "Only one dai codec found in DTS, enabled rt5645 AD filter\n");
  92. rt5645_sel_asrc_clk_src(codec,
  93. RT5645_AD_STEREO_FILTER,
  94. RT5645_CLK_SEL_I2S1_ASRC);
  95. }
  96. /* enable jack detection */
  97. ret = snd_soc_card_jack_new(card, "Headset Jack",
  98. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  99. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  100. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  101. &mt8173_rt5650_jack, NULL, 0);
  102. if (ret) {
  103. dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
  104. return ret;
  105. }
  106. return rt5645_set_jack_detect(codec,
  107. &mt8173_rt5650_jack,
  108. &mt8173_rt5650_jack,
  109. &mt8173_rt5650_jack);
  110. }
  111. static struct snd_soc_dai_link_component mt8173_rt5650_codecs[] = {
  112. {
  113. /* Playback */
  114. .dai_name = "rt5645-aif1",
  115. },
  116. {
  117. /* Capture */
  118. .dai_name = "rt5645-aif1",
  119. },
  120. };
  121. enum {
  122. DAI_LINK_PLAYBACK,
  123. DAI_LINK_CAPTURE,
  124. DAI_LINK_CODEC_I2S,
  125. };
  126. /* Digital audio interface glue - connects codec <---> CPU */
  127. static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
  128. /* Front End DAI links */
  129. [DAI_LINK_PLAYBACK] = {
  130. .name = "rt5650 Playback",
  131. .stream_name = "rt5650 Playback",
  132. .cpu_dai_name = "DL1",
  133. .codec_name = "snd-soc-dummy",
  134. .codec_dai_name = "snd-soc-dummy-dai",
  135. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  136. .dynamic = 1,
  137. .dpcm_playback = 1,
  138. },
  139. [DAI_LINK_CAPTURE] = {
  140. .name = "rt5650 Capture",
  141. .stream_name = "rt5650 Capture",
  142. .cpu_dai_name = "VUL",
  143. .codec_name = "snd-soc-dummy",
  144. .codec_dai_name = "snd-soc-dummy-dai",
  145. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  146. .dynamic = 1,
  147. .dpcm_capture = 1,
  148. },
  149. /* Back End DAI links */
  150. [DAI_LINK_CODEC_I2S] = {
  151. .name = "Codec",
  152. .cpu_dai_name = "I2S",
  153. .no_pcm = 1,
  154. .codecs = mt8173_rt5650_codecs,
  155. .num_codecs = 2,
  156. .init = mt8173_rt5650_init,
  157. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  158. SND_SOC_DAIFMT_CBS_CFS,
  159. .ops = &mt8173_rt5650_ops,
  160. .ignore_pmdown_time = 1,
  161. .dpcm_playback = 1,
  162. .dpcm_capture = 1,
  163. },
  164. };
  165. static struct snd_soc_card mt8173_rt5650_card = {
  166. .name = "mtk-rt5650",
  167. .owner = THIS_MODULE,
  168. .dai_link = mt8173_rt5650_dais,
  169. .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
  170. .controls = mt8173_rt5650_controls,
  171. .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
  172. .dapm_widgets = mt8173_rt5650_widgets,
  173. .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
  174. .dapm_routes = mt8173_rt5650_routes,
  175. .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
  176. };
  177. static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
  178. {
  179. struct snd_soc_card *card = &mt8173_rt5650_card;
  180. struct device_node *platform_node;
  181. struct device_node *np;
  182. const char *codec_capture_dai;
  183. int i, ret;
  184. platform_node = of_parse_phandle(pdev->dev.of_node,
  185. "mediatek,platform", 0);
  186. if (!platform_node) {
  187. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  188. return -EINVAL;
  189. }
  190. for (i = 0; i < card->num_links; i++) {
  191. if (mt8173_rt5650_dais[i].platform_name)
  192. continue;
  193. mt8173_rt5650_dais[i].platform_of_node = platform_node;
  194. }
  195. mt8173_rt5650_codecs[0].of_node =
  196. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
  197. if (!mt8173_rt5650_codecs[0].of_node) {
  198. dev_err(&pdev->dev,
  199. "Property 'audio-codec' missing or invalid\n");
  200. return -EINVAL;
  201. }
  202. mt8173_rt5650_codecs[1].of_node = mt8173_rt5650_codecs[0].of_node;
  203. if (of_find_node_by_name(platform_node, "codec-capture")) {
  204. np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
  205. if (!np) {
  206. dev_err(&pdev->dev,
  207. "%s: Can't find codec-capture DT node\n",
  208. __func__);
  209. return -EINVAL;
  210. }
  211. ret = snd_soc_of_get_dai_name(np, &codec_capture_dai);
  212. if (ret < 0) {
  213. dev_err(&pdev->dev,
  214. "%s codec_capture_dai name fail %d\n",
  215. __func__, ret);
  216. return ret;
  217. }
  218. mt8173_rt5650_codecs[1].dai_name = codec_capture_dai;
  219. }
  220. card->dev = &pdev->dev;
  221. platform_set_drvdata(pdev, card);
  222. ret = devm_snd_soc_register_card(&pdev->dev, card);
  223. if (ret)
  224. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  225. __func__, ret);
  226. return ret;
  227. }
  228. static const struct of_device_id mt8173_rt5650_dt_match[] = {
  229. { .compatible = "mediatek,mt8173-rt5650", },
  230. { }
  231. };
  232. MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
  233. static struct platform_driver mt8173_rt5650_driver = {
  234. .driver = {
  235. .name = "mtk-rt5650",
  236. .of_match_table = mt8173_rt5650_dt_match,
  237. #ifdef CONFIG_PM
  238. .pm = &snd_soc_pm_ops,
  239. #endif
  240. },
  241. .probe = mt8173_rt5650_dev_probe,
  242. };
  243. module_platform_driver(mt8173_rt5650_driver);
  244. /* Module information */
  245. MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
  246. MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
  247. MODULE_LICENSE("GPL v2");
  248. MODULE_ALIAS("platform:mtk-rt5650");