mt8173-rt5650-rt5514.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mt8173-rt5650-rt5514.c -- MT8173 machine driver with RT5650/5514 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. static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_widgets[] = {
  16. SND_SOC_DAPM_SPK("Speaker", NULL),
  17. SND_SOC_DAPM_MIC("Int Mic", NULL),
  18. SND_SOC_DAPM_HP("Headphone", NULL),
  19. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  20. };
  21. static const struct snd_soc_dapm_route mt8173_rt5650_rt5514_routes[] = {
  22. {"Speaker", NULL, "SPOL"},
  23. {"Speaker", NULL, "SPOR"},
  24. {"Sub DMIC1L", NULL, "Int Mic"},
  25. {"Sub DMIC1R", NULL, "Int Mic"},
  26. {"Headphone", NULL, "HPOL"},
  27. {"Headphone", NULL, "HPOR"},
  28. {"IN1P", NULL, "Headset Mic"},
  29. {"IN1N", NULL, "Headset Mic"},
  30. };
  31. static const struct snd_kcontrol_new mt8173_rt5650_rt5514_controls[] = {
  32. SOC_DAPM_PIN_SWITCH("Speaker"),
  33. SOC_DAPM_PIN_SWITCH("Int Mic"),
  34. SOC_DAPM_PIN_SWITCH("Headphone"),
  35. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  36. };
  37. static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream *substream,
  38. struct snd_pcm_hw_params *params)
  39. {
  40. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  41. struct snd_soc_dai *codec_dai;
  42. int i, ret;
  43. for_each_rtd_codec_dais(rtd, i, codec_dai) {
  44. /* pll from mclk 12.288M */
  45. ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
  46. params_rate(params) * 512);
  47. if (ret)
  48. return ret;
  49. /* sysclk from pll */
  50. ret = snd_soc_dai_set_sysclk(codec_dai, 1,
  51. params_rate(params) * 512,
  52. SND_SOC_CLOCK_IN);
  53. if (ret)
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = {
  59. .hw_params = mt8173_rt5650_rt5514_hw_params,
  60. };
  61. static struct snd_soc_jack mt8173_rt5650_rt5514_jack;
  62. static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime)
  63. {
  64. struct snd_soc_card *card = runtime->card;
  65. struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
  66. int ret;
  67. rt5645_sel_asrc_clk_src(component,
  68. RT5645_DA_STEREO_FILTER |
  69. RT5645_AD_STEREO_FILTER,
  70. RT5645_CLK_SEL_I2S1_ASRC);
  71. /* enable jack detection */
  72. ret = snd_soc_card_jack_new(card, "Headset Jack",
  73. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  74. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  75. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  76. &mt8173_rt5650_rt5514_jack);
  77. if (ret) {
  78. dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
  79. return ret;
  80. }
  81. return rt5645_set_jack_detect(component,
  82. &mt8173_rt5650_rt5514_jack,
  83. &mt8173_rt5650_rt5514_jack,
  84. &mt8173_rt5650_rt5514_jack);
  85. }
  86. enum {
  87. DAI_LINK_PLAYBACK,
  88. DAI_LINK_CAPTURE,
  89. DAI_LINK_CODEC_I2S,
  90. };
  91. SND_SOC_DAILINK_DEFS(playback,
  92. DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
  93. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  94. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  95. SND_SOC_DAILINK_DEFS(capture,
  96. DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
  97. DAILINK_COMP_ARRAY(COMP_DUMMY()),
  98. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  99. SND_SOC_DAILINK_DEFS(codec,
  100. DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
  101. DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"),
  102. COMP_CODEC(NULL, "rt5514-aif1")),
  103. DAILINK_COMP_ARRAY(COMP_EMPTY()));
  104. /* Digital audio interface glue - connects codec <---> CPU */
  105. static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = {
  106. /* Front End DAI links */
  107. [DAI_LINK_PLAYBACK] = {
  108. .name = "rt5650_rt5514 Playback",
  109. .stream_name = "rt5650_rt5514 Playback",
  110. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  111. .dynamic = 1,
  112. .dpcm_playback = 1,
  113. SND_SOC_DAILINK_REG(playback),
  114. },
  115. [DAI_LINK_CAPTURE] = {
  116. .name = "rt5650_rt5514 Capture",
  117. .stream_name = "rt5650_rt5514 Capture",
  118. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  119. .dynamic = 1,
  120. .dpcm_capture = 1,
  121. SND_SOC_DAILINK_REG(capture),
  122. },
  123. /* Back End DAI links */
  124. [DAI_LINK_CODEC_I2S] = {
  125. .name = "Codec",
  126. .no_pcm = 1,
  127. .init = mt8173_rt5650_rt5514_init,
  128. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  129. SND_SOC_DAIFMT_CBS_CFS,
  130. .ops = &mt8173_rt5650_rt5514_ops,
  131. .ignore_pmdown_time = 1,
  132. .dpcm_playback = 1,
  133. .dpcm_capture = 1,
  134. SND_SOC_DAILINK_REG(codec),
  135. },
  136. };
  137. static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = {
  138. {
  139. .name_prefix = "Sub",
  140. },
  141. };
  142. static struct snd_soc_card mt8173_rt5650_rt5514_card = {
  143. .name = "mtk-rt5650-rt5514",
  144. .owner = THIS_MODULE,
  145. .dai_link = mt8173_rt5650_rt5514_dais,
  146. .num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais),
  147. .codec_conf = mt8173_rt5650_rt5514_codec_conf,
  148. .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf),
  149. .controls = mt8173_rt5650_rt5514_controls,
  150. .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls),
  151. .dapm_widgets = mt8173_rt5650_rt5514_widgets,
  152. .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets),
  153. .dapm_routes = mt8173_rt5650_rt5514_routes,
  154. .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes),
  155. };
  156. static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev)
  157. {
  158. struct snd_soc_card *card = &mt8173_rt5650_rt5514_card;
  159. struct device_node *platform_node;
  160. struct snd_soc_dai_link *dai_link;
  161. int i, ret;
  162. platform_node = of_parse_phandle(pdev->dev.of_node,
  163. "mediatek,platform", 0);
  164. if (!platform_node) {
  165. dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
  166. return -EINVAL;
  167. }
  168. for_each_card_prelinks(card, i, dai_link) {
  169. if (dai_link->platforms->name)
  170. continue;
  171. dai_link->platforms->of_node = platform_node;
  172. }
  173. mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
  174. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
  175. if (!mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
  176. dev_err(&pdev->dev,
  177. "Property 'audio-codec' missing or invalid\n");
  178. ret = -EINVAL;
  179. goto out;
  180. }
  181. mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
  182. of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
  183. if (!mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node) {
  184. dev_err(&pdev->dev,
  185. "Property 'audio-codec' missing or invalid\n");
  186. ret = -EINVAL;
  187. goto out;
  188. }
  189. mt8173_rt5650_rt5514_codec_conf[0].dlc.of_node =
  190. mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node;
  191. card->dev = &pdev->dev;
  192. ret = devm_snd_soc_register_card(&pdev->dev, card);
  193. out:
  194. of_node_put(platform_node);
  195. return ret;
  196. }
  197. static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = {
  198. { .compatible = "mediatek,mt8173-rt5650-rt5514", },
  199. { }
  200. };
  201. MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match);
  202. static struct platform_driver mt8173_rt5650_rt5514_driver = {
  203. .driver = {
  204. .name = "mtk-rt5650-rt5514",
  205. .of_match_table = mt8173_rt5650_rt5514_dt_match,
  206. .pm = &snd_soc_pm_ops,
  207. },
  208. .probe = mt8173_rt5650_rt5514_dev_probe,
  209. };
  210. module_platform_driver(mt8173_rt5650_rt5514_driver);
  211. /* Module information */
  212. MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
  213. MODULE_AUTHOR("Koro Chen <[email protected]>");
  214. MODULE_LICENSE("GPL v2");
  215. MODULE_ALIAS("platform:mtk-rt5650-rt5514");