mt8173-rt5650.c 8.6 KB

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