mt8173-rt5650.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <[email protected]>
  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("Ext Spk", 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. {"Ext Spk", NULL, "SPOL"},
  34. {"Ext Spk", 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("Ext Spk"),
  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 = asoc_substream_to_rtd(substream);
  52. unsigned int mclk_clock;
  53. struct snd_soc_dai *codec_dai;
  54. int i, ret;
  55. switch (mt8173_rt5650_priv.pll_from) {
  56. case MT8173_RT5650_MCLK_EXTERNAL:
  57. /* mclk = 12.288M */
  58. mclk_clock = MCLK_FOR_CODECS;
  59. break;
  60. case MT8173_RT5650_MCLK_INTERNAL:
  61. /* mclk = sampling rate*256 */
  62. mclk_clock = params_rate(params) * 256;
  63. break;
  64. default:
  65. /* mclk = 12.288M */
  66. mclk_clock = MCLK_FOR_CODECS;
  67. break;
  68. }
  69. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  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, mt8173_rt5650_hdmi_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 = asoc_rtd_to_codec(runtime, 0)->component;
  92. const char *codec_capture_dai = asoc_rtd_to_codec(runtime, 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);
  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 int mt8173_rt5650_hdmi_init(struct snd_soc_pcm_runtime *rtd)
  128. {
  129. int ret;
  130. ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
  131. &mt8173_rt5650_hdmi_jack);
  132. if (ret)
  133. return ret;
  134. return snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component,
  135. &mt8173_rt5650_hdmi_jack, NULL);
  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. SND_SOC_DAILINK_DEFS(playback,
  145. DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
  146. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  147. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  148. SND_SOC_DAILINK_DEFS(capture,
  149. DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
  150. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  151. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  152. SND_SOC_DAILINK_DEFS(hdmi_pcm,
  153. DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
  154. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  155. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  156. SND_SOC_DAILINK_DEFS(codec,
  157. DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
  158. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"), /* Playback */
  159. COMP_CODEC(NULL, "rt5645-aif1")),/* Capture */
  160. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  161. SND_SOC_DAILINK_DEFS(hdmi_be,
  162. DAILINK_COMP_ARRAY(COMP_CPU("HDMIO")),
  163. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
  164. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  165. /* Digital audio interface glue - connects codec <---> CPU */
  166. static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
  167. /* Front End DAI links */
  168. [DAI_LINK_PLAYBACK] = {
  169. .name = "rt5650 Playback",
  170. .stream_name = "rt5650 Playback",
  171. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  172. .dynamic = 1,
  173. .dpcm_playback = 1,
  174. SND_SOC_DAILINK_REG(playback),
  175. },
  176. [DAI_LINK_CAPTURE] = {
  177. .name = "rt5650 Capture",
  178. .stream_name = "rt5650 Capture",
  179. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  180. .dynamic = 1,
  181. .dpcm_capture = 1,
  182. SND_SOC_DAILINK_REG(capture),
  183. },
  184. [DAI_LINK_HDMI] = {
  185. .name = "HDMI",
  186. .stream_name = "HDMI PCM",
  187. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  188. .dynamic = 1,
  189. .dpcm_playback = 1,
  190. SND_SOC_DAILINK_REG(hdmi_pcm),
  191. },
  192. /* Back End DAI links */
  193. [DAI_LINK_CODEC_I2S] = {
  194. .name = "Codec",
  195. .no_pcm = 1,
  196. .init = mt8173_rt5650_init,
  197. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  198. SND_SOC_DAIFMT_CBS_CFS,
  199. .ops = &mt8173_rt5650_ops,
  200. .ignore_pmdown_time = 1,
  201. .dpcm_playback = 1,
  202. .dpcm_capture = 1,
  203. SND_SOC_DAILINK_REG(codec),
  204. },
  205. [DAI_LINK_HDMI_I2S] = {
  206. .name = "HDMI BE",
  207. .no_pcm = 1,
  208. .dpcm_playback = 1,
  209. .init = mt8173_rt5650_hdmi_init,
  210. SND_SOC_DAILINK_REG(hdmi_be),
  211. },
  212. };
  213. static struct snd_soc_card mt8173_rt5650_card = {
  214. .name = "mtk-rt5650",
  215. .owner = THIS_MODULE,
  216. .dai_link = mt8173_rt5650_dais,
  217. .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
  218. .controls = mt8173_rt5650_controls,
  219. .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
  220. .dapm_widgets = mt8173_rt5650_widgets,
  221. .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
  222. .dapm_routes = mt8173_rt5650_routes,
  223. .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
  224. };
  225. static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
  226. {
  227. struct snd_soc_card *card = &mt8173_rt5650_card;
  228. struct device_node *platform_node;
  229. struct device_node *np;
  230. const char *codec_capture_dai;
  231. struct snd_soc_dai_link *dai_link;
  232. int i, ret;
  233. platform_node = of_parse_phandle(pdev->dev.of_node,
  234. "mediatek,platform", 0);
  235. if (!platform_node) {
  236. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  237. return -EINVAL;
  238. }
  239. for_each_card_prelinks(card, i, dai_link) {
  240. if (dai_link->platforms->name)
  241. continue;
  242. dai_link->platforms->of_node = platform_node;
  243. }
  244. mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
  245. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
  246. if (!mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
  247. dev_err(&pdev->dev,
  248. "Property 'audio-codec' missing or invalid\n");
  249. ret = -EINVAL;
  250. goto put_platform_node;
  251. }
  252. mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
  253. mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node;
  254. np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
  255. if (np) {
  256. ret = snd_soc_of_get_dai_name(np, &codec_capture_dai);
  257. of_node_put(np);
  258. if (ret < 0) {
  259. dev_err(&pdev->dev,
  260. "%s codec_capture_dai name fail %d\n",
  261. __func__, ret);
  262. goto put_platform_node;
  263. }
  264. mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].dai_name =
  265. codec_capture_dai;
  266. }
  267. if (device_property_present(&pdev->dev, "mediatek,mclk")) {
  268. ret = device_property_read_u32(&pdev->dev,
  269. "mediatek,mclk",
  270. &mt8173_rt5650_priv.pll_from);
  271. if (ret) {
  272. dev_err(&pdev->dev,
  273. "%s snd_soc_register_card fail %d\n",
  274. __func__, ret);
  275. }
  276. }
  277. mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node =
  278. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
  279. if (!mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node) {
  280. dev_err(&pdev->dev,
  281. "Property 'audio-codec' missing or invalid\n");
  282. ret = -EINVAL;
  283. goto put_platform_node;
  284. }
  285. card->dev = &pdev->dev;
  286. ret = devm_snd_soc_register_card(&pdev->dev, card);
  287. put_platform_node:
  288. of_node_put(platform_node);
  289. return ret;
  290. }
  291. static const struct of_device_id mt8173_rt5650_dt_match[] = {
  292. { .compatible = "mediatek,mt8173-rt5650", },
  293. { }
  294. };
  295. MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
  296. static struct platform_driver mt8173_rt5650_driver = {
  297. .driver = {
  298. .name = "mtk-rt5650",
  299. .of_match_table = mt8173_rt5650_dt_match,
  300. .pm = &snd_soc_pm_ops,
  301. },
  302. .probe = mt8173_rt5650_dev_probe,
  303. };
  304. module_platform_driver(mt8173_rt5650_driver);
  305. /* Module information */
  306. MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
  307. MODULE_AUTHOR("Koro Chen <[email protected]>");
  308. MODULE_LICENSE("GPL v2");
  309. MODULE_ALIAS("platform:mtk-rt5650");