mt8173-rt5650-rt5676.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * mt8173-rt5650-rt5676.c -- MT8173 machine driver with RT5650/5676 codecs
  3. *
  4. * Copyright (c) 2015 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. #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. /* Digital audio interface glue - connects codec <---> CPU */
  118. static struct snd_soc_dai_link mt8173_rt5650_rt5676_dais[] = {
  119. /* Front End DAI links */
  120. {
  121. .name = "rt5650_rt5676 Playback",
  122. .stream_name = "rt5650_rt5676 Playback",
  123. .cpu_dai_name = "DL1",
  124. .codec_name = "snd-soc-dummy",
  125. .codec_dai_name = "snd-soc-dummy-dai",
  126. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  127. .dynamic = 1,
  128. .dpcm_playback = 1,
  129. },
  130. {
  131. .name = "rt5650_rt5676 Capture",
  132. .stream_name = "rt5650_rt5676 Capture",
  133. .cpu_dai_name = "VUL",
  134. .codec_name = "snd-soc-dummy",
  135. .codec_dai_name = "snd-soc-dummy-dai",
  136. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  137. .dynamic = 1,
  138. .dpcm_capture = 1,
  139. },
  140. /* Back End DAI links */
  141. {
  142. .name = "Codec",
  143. .cpu_dai_name = "I2S",
  144. .no_pcm = 1,
  145. .codecs = mt8173_rt5650_rt5676_codecs,
  146. .num_codecs = 2,
  147. .init = mt8173_rt5650_rt5676_init,
  148. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  149. SND_SOC_DAIFMT_CBS_CFS,
  150. .ops = &mt8173_rt5650_rt5676_ops,
  151. .ignore_pmdown_time = 1,
  152. .dpcm_playback = 1,
  153. .dpcm_capture = 1,
  154. },
  155. { /* rt5676 <-> rt5650 intercodec link: Sets rt5676 I2S2 as master */
  156. .name = "rt5650_rt5676 intercodec",
  157. .stream_name = "rt5650_rt5676 intercodec",
  158. .cpu_dai_name = "snd-soc-dummy-dai",
  159. .platform_name = "snd-soc-dummy",
  160. .no_pcm = 1,
  161. .codec_dai_name = "rt5677-aif2",
  162. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  163. SND_SOC_DAIFMT_CBM_CFM,
  164. },
  165. };
  166. static struct snd_soc_codec_conf mt8173_rt5650_rt5676_codec_conf[] = {
  167. {
  168. .name_prefix = "Sub",
  169. },
  170. };
  171. static struct snd_soc_card mt8173_rt5650_rt5676_card = {
  172. .name = "mtk-rt5650-rt5676",
  173. .owner = THIS_MODULE,
  174. .dai_link = mt8173_rt5650_rt5676_dais,
  175. .num_links = ARRAY_SIZE(mt8173_rt5650_rt5676_dais),
  176. .codec_conf = mt8173_rt5650_rt5676_codec_conf,
  177. .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5676_codec_conf),
  178. .controls = mt8173_rt5650_rt5676_controls,
  179. .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5676_controls),
  180. .dapm_widgets = mt8173_rt5650_rt5676_widgets,
  181. .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5676_widgets),
  182. .dapm_routes = mt8173_rt5650_rt5676_routes,
  183. .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5676_routes),
  184. };
  185. static int mt8173_rt5650_rt5676_dev_probe(struct platform_device *pdev)
  186. {
  187. struct snd_soc_card *card = &mt8173_rt5650_rt5676_card;
  188. struct device_node *platform_node;
  189. int i, ret;
  190. platform_node = of_parse_phandle(pdev->dev.of_node,
  191. "mediatek,platform", 0);
  192. if (!platform_node) {
  193. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  194. return -EINVAL;
  195. }
  196. for (i = 0; i < card->num_links; i++) {
  197. if (mt8173_rt5650_rt5676_dais[i].platform_name)
  198. continue;
  199. mt8173_rt5650_rt5676_dais[i].platform_of_node = platform_node;
  200. }
  201. mt8173_rt5650_rt5676_codecs[0].of_node =
  202. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
  203. if (!mt8173_rt5650_rt5676_codecs[0].of_node) {
  204. dev_err(&pdev->dev,
  205. "Property 'audio-codec' missing or invalid\n");
  206. return -EINVAL;
  207. }
  208. mt8173_rt5650_rt5676_codecs[1].of_node =
  209. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
  210. if (!mt8173_rt5650_rt5676_codecs[1].of_node) {
  211. dev_err(&pdev->dev,
  212. "Property 'audio-codec' missing or invalid\n");
  213. return -EINVAL;
  214. }
  215. mt8173_rt5650_rt5676_codec_conf[0].of_node =
  216. mt8173_rt5650_rt5676_codecs[1].of_node;
  217. mt8173_rt5650_rt5676_dais[3].codec_of_node =
  218. mt8173_rt5650_rt5676_codecs[1].of_node;
  219. card->dev = &pdev->dev;
  220. platform_set_drvdata(pdev, card);
  221. ret = devm_snd_soc_register_card(&pdev->dev, card);
  222. if (ret)
  223. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  224. __func__, ret);
  225. return ret;
  226. }
  227. static const struct of_device_id mt8173_rt5650_rt5676_dt_match[] = {
  228. { .compatible = "mediatek,mt8173-rt5650-rt5676", },
  229. { }
  230. };
  231. MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5676_dt_match);
  232. static struct platform_driver mt8173_rt5650_rt5676_driver = {
  233. .driver = {
  234. .name = "mtk-rt5650-rt5676",
  235. .of_match_table = mt8173_rt5650_rt5676_dt_match,
  236. #ifdef CONFIG_PM
  237. .pm = &snd_soc_pm_ops,
  238. #endif
  239. },
  240. .probe = mt8173_rt5650_rt5676_dev_probe,
  241. };
  242. module_platform_driver(mt8173_rt5650_rt5676_driver);
  243. /* Module information */
  244. MODULE_DESCRIPTION("MT8173 RT5650 and RT5676 SoC machine driver");
  245. MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
  246. MODULE_LICENSE("GPL v2");
  247. MODULE_ALIAS("platform:mtk-rt5650-rt5676");