mt8173-rt5650-rt5514.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * mt8173-rt5650-rt5514.c -- MT8173 machine driver with RT5650/5514 codecs
  3. *
  4. * Copyright (c) 2016 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. #define MCLK_FOR_CODECS 12288000
  23. static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_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_rt5514_routes[] = {
  30. {"Speaker", NULL, "SPOL"},
  31. {"Speaker", NULL, "SPOR"},
  32. {"Sub DMIC1L", NULL, "Int Mic"},
  33. {"Sub DMIC1R", 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_rt5514_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_rt5514_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_rt5514_ops = {
  69. .hw_params = mt8173_rt5650_rt5514_hw_params,
  70. };
  71. static struct snd_soc_jack mt8173_rt5650_rt5514_jack;
  72. static int mt8173_rt5650_rt5514_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. int ret;
  77. rt5645_sel_asrc_clk_src(codec,
  78. RT5645_DA_STEREO_FILTER |
  79. RT5645_AD_STEREO_FILTER,
  80. RT5645_CLK_SEL_I2S1_ASRC);
  81. /* enable jack detection */
  82. ret = snd_soc_card_jack_new(card, "Headset Jack",
  83. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  84. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  85. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  86. &mt8173_rt5650_rt5514_jack, NULL, 0);
  87. if (ret) {
  88. dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
  89. return ret;
  90. }
  91. return rt5645_set_jack_detect(codec,
  92. &mt8173_rt5650_rt5514_jack,
  93. &mt8173_rt5650_rt5514_jack,
  94. &mt8173_rt5650_rt5514_jack);
  95. }
  96. static struct snd_soc_dai_link_component mt8173_rt5650_rt5514_codecs[] = {
  97. {
  98. .dai_name = "rt5645-aif1",
  99. },
  100. {
  101. .dai_name = "rt5514-aif1",
  102. },
  103. };
  104. enum {
  105. DAI_LINK_PLAYBACK,
  106. DAI_LINK_CAPTURE,
  107. DAI_LINK_CODEC_I2S,
  108. };
  109. /* Digital audio interface glue - connects codec <---> CPU */
  110. static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = {
  111. /* Front End DAI links */
  112. [DAI_LINK_PLAYBACK] = {
  113. .name = "rt5650_rt5514 Playback",
  114. .stream_name = "rt5650_rt5514 Playback",
  115. .cpu_dai_name = "DL1",
  116. .codec_name = "snd-soc-dummy",
  117. .codec_dai_name = "snd-soc-dummy-dai",
  118. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  119. .dynamic = 1,
  120. .dpcm_playback = 1,
  121. },
  122. [DAI_LINK_CAPTURE] = {
  123. .name = "rt5650_rt5514 Capture",
  124. .stream_name = "rt5650_rt5514 Capture",
  125. .cpu_dai_name = "VUL",
  126. .codec_name = "snd-soc-dummy",
  127. .codec_dai_name = "snd-soc-dummy-dai",
  128. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  129. .dynamic = 1,
  130. .dpcm_capture = 1,
  131. },
  132. /* Back End DAI links */
  133. [DAI_LINK_CODEC_I2S] = {
  134. .name = "Codec",
  135. .cpu_dai_name = "I2S",
  136. .no_pcm = 1,
  137. .codecs = mt8173_rt5650_rt5514_codecs,
  138. .num_codecs = 2,
  139. .init = mt8173_rt5650_rt5514_init,
  140. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  141. SND_SOC_DAIFMT_CBS_CFS,
  142. .ops = &mt8173_rt5650_rt5514_ops,
  143. .ignore_pmdown_time = 1,
  144. .dpcm_playback = 1,
  145. .dpcm_capture = 1,
  146. },
  147. };
  148. static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = {
  149. {
  150. .name_prefix = "Sub",
  151. },
  152. };
  153. static struct snd_soc_card mt8173_rt5650_rt5514_card = {
  154. .name = "mtk-rt5650-rt5514",
  155. .owner = THIS_MODULE,
  156. .dai_link = mt8173_rt5650_rt5514_dais,
  157. .num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais),
  158. .codec_conf = mt8173_rt5650_rt5514_codec_conf,
  159. .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf),
  160. .controls = mt8173_rt5650_rt5514_controls,
  161. .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls),
  162. .dapm_widgets = mt8173_rt5650_rt5514_widgets,
  163. .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets),
  164. .dapm_routes = mt8173_rt5650_rt5514_routes,
  165. .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes),
  166. };
  167. static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev)
  168. {
  169. struct snd_soc_card *card = &mt8173_rt5650_rt5514_card;
  170. struct device_node *platform_node;
  171. int i, ret;
  172. platform_node = of_parse_phandle(pdev->dev.of_node,
  173. "mediatek,platform", 0);
  174. if (!platform_node) {
  175. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  176. return -EINVAL;
  177. }
  178. for (i = 0; i < card->num_links; i++) {
  179. if (mt8173_rt5650_rt5514_dais[i].platform_name)
  180. continue;
  181. mt8173_rt5650_rt5514_dais[i].platform_of_node = platform_node;
  182. }
  183. mt8173_rt5650_rt5514_codecs[0].of_node =
  184. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
  185. if (!mt8173_rt5650_rt5514_codecs[0].of_node) {
  186. dev_err(&pdev->dev,
  187. "Property 'audio-codec' missing or invalid\n");
  188. return -EINVAL;
  189. }
  190. mt8173_rt5650_rt5514_codecs[1].of_node =
  191. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
  192. if (!mt8173_rt5650_rt5514_codecs[1].of_node) {
  193. dev_err(&pdev->dev,
  194. "Property 'audio-codec' missing or invalid\n");
  195. return -EINVAL;
  196. }
  197. mt8173_rt5650_rt5514_codec_conf[0].of_node =
  198. mt8173_rt5650_rt5514_codecs[1].of_node;
  199. card->dev = &pdev->dev;
  200. platform_set_drvdata(pdev, card);
  201. ret = devm_snd_soc_register_card(&pdev->dev, card);
  202. if (ret)
  203. dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
  204. __func__, ret);
  205. return ret;
  206. }
  207. static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = {
  208. { .compatible = "mediatek,mt8173-rt5650-rt5514", },
  209. { }
  210. };
  211. MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match);
  212. static struct platform_driver mt8173_rt5650_rt5514_driver = {
  213. .driver = {
  214. .name = "mtk-rt5650-rt5514",
  215. .of_match_table = mt8173_rt5650_rt5514_dt_match,
  216. #ifdef CONFIG_PM
  217. .pm = &snd_soc_pm_ops,
  218. #endif
  219. },
  220. .probe = mt8173_rt5650_rt5514_dev_probe,
  221. };
  222. module_platform_driver(mt8173_rt5650_rt5514_driver);
  223. /* Module information */
  224. MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
  225. MODULE_AUTHOR("Koro Chen <[email protected]>");
  226. MODULE_LICENSE("GPL v2");
  227. MODULE_ALIAS("platform:mtk-rt5650-rt5514");