acp-rt5645.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Machine driver for AMD ACP Audio engine using Realtek RT5645 codec
  3. *
  4. * Copyright 2017 Advanced Micro Devices, Inc.
  5. *
  6. * This file is modified from rt288 machine driver
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. *
  27. */
  28. #include <sound/core.h>
  29. #include <sound/soc.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc-dapm.h>
  33. #include <sound/jack.h>
  34. #include <linux/gpio.h>
  35. #include <linux/module.h>
  36. #include <linux/i2c.h>
  37. #include <linux/acpi.h>
  38. #include "../codecs/rt5645.h"
  39. #define CZ_PLAT_CLK 24000000
  40. static struct snd_soc_jack cz_jack;
  41. static int cz_aif1_hw_params(struct snd_pcm_substream *substream,
  42. struct snd_pcm_hw_params *params)
  43. {
  44. int ret = 0;
  45. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  46. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  47. ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
  48. CZ_PLAT_CLK, params_rate(params) * 512);
  49. if (ret < 0) {
  50. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  51. return ret;
  52. }
  53. ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
  54. params_rate(params) * 512, SND_SOC_CLOCK_OUT);
  55. if (ret < 0) {
  56. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  57. return ret;
  58. }
  59. return ret;
  60. }
  61. static int cz_init(struct snd_soc_pcm_runtime *rtd)
  62. {
  63. int ret;
  64. struct snd_soc_card *card;
  65. struct snd_soc_component *codec;
  66. codec = asoc_rtd_to_codec(rtd, 0)->component;
  67. card = rtd->card;
  68. ret = snd_soc_card_jack_new(card, "Headset Jack",
  69. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  70. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  71. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  72. &cz_jack);
  73. if (ret) {
  74. dev_err(card->dev, "HP jack creation failed %d\n", ret);
  75. return ret;
  76. }
  77. rt5645_set_jack_detect(codec, &cz_jack, &cz_jack, &cz_jack);
  78. return 0;
  79. }
  80. static const struct snd_soc_ops cz_aif1_ops = {
  81. .hw_params = cz_aif1_hw_params,
  82. };
  83. SND_SOC_DAILINK_DEF(designware1,
  84. DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.1.auto")));
  85. SND_SOC_DAILINK_DEF(designware2,
  86. DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.2.auto")));
  87. SND_SOC_DAILINK_DEF(codec,
  88. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5650:00", "rt5645-aif1")));
  89. SND_SOC_DAILINK_DEF(platform,
  90. DAILINK_COMP_ARRAY(COMP_PLATFORM("acp_audio_dma.0.auto")));
  91. static struct snd_soc_dai_link cz_dai_rt5650[] = {
  92. {
  93. .name = "amd-rt5645-play",
  94. .stream_name = "RT5645_AIF1",
  95. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  96. | SND_SOC_DAIFMT_CBP_CFP,
  97. .init = cz_init,
  98. .ops = &cz_aif1_ops,
  99. SND_SOC_DAILINK_REG(designware1, codec, platform),
  100. },
  101. {
  102. .name = "amd-rt5645-cap",
  103. .stream_name = "RT5645_AIF1",
  104. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  105. | SND_SOC_DAIFMT_CBP_CFP,
  106. .ops = &cz_aif1_ops,
  107. SND_SOC_DAILINK_REG(designware2, codec, platform),
  108. },
  109. };
  110. static const struct snd_soc_dapm_widget cz_widgets[] = {
  111. SND_SOC_DAPM_HP("Headphones", NULL),
  112. SND_SOC_DAPM_SPK("Speakers", NULL),
  113. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  114. SND_SOC_DAPM_MIC("Int Mic", NULL),
  115. };
  116. static const struct snd_soc_dapm_route cz_audio_route[] = {
  117. {"Headphones", NULL, "HPOL"},
  118. {"Headphones", NULL, "HPOR"},
  119. {"RECMIXL", NULL, "Headset Mic"},
  120. {"RECMIXR", NULL, "Headset Mic"},
  121. {"Speakers", NULL, "SPOL"},
  122. {"Speakers", NULL, "SPOR"},
  123. {"DMIC L2", NULL, "Int Mic"},
  124. {"DMIC R2", NULL, "Int Mic"},
  125. };
  126. static const struct snd_kcontrol_new cz_mc_controls[] = {
  127. SOC_DAPM_PIN_SWITCH("Headphones"),
  128. SOC_DAPM_PIN_SWITCH("Speakers"),
  129. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  130. SOC_DAPM_PIN_SWITCH("Int Mic"),
  131. };
  132. static struct snd_soc_card cz_card = {
  133. .name = "acprt5650",
  134. .owner = THIS_MODULE,
  135. .dai_link = cz_dai_rt5650,
  136. .num_links = ARRAY_SIZE(cz_dai_rt5650),
  137. .dapm_widgets = cz_widgets,
  138. .num_dapm_widgets = ARRAY_SIZE(cz_widgets),
  139. .dapm_routes = cz_audio_route,
  140. .num_dapm_routes = ARRAY_SIZE(cz_audio_route),
  141. .controls = cz_mc_controls,
  142. .num_controls = ARRAY_SIZE(cz_mc_controls),
  143. };
  144. static int cz_probe(struct platform_device *pdev)
  145. {
  146. int ret;
  147. struct snd_soc_card *card;
  148. card = &cz_card;
  149. cz_card.dev = &pdev->dev;
  150. platform_set_drvdata(pdev, card);
  151. ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
  152. if (ret) {
  153. dev_err(&pdev->dev,
  154. "devm_snd_soc_register_card(%s) failed: %d\n",
  155. cz_card.name, ret);
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. #ifdef CONFIG_ACPI
  161. static const struct acpi_device_id cz_audio_acpi_match[] = {
  162. { "AMDI1002", 0 },
  163. {},
  164. };
  165. MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
  166. #endif
  167. static struct platform_driver cz_pcm_driver = {
  168. .driver = {
  169. .name = "cz-rt5645",
  170. .acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
  171. .pm = &snd_soc_pm_ops,
  172. },
  173. .probe = cz_probe,
  174. };
  175. module_platform_driver(cz_pcm_driver);
  176. MODULE_AUTHOR("[email protected]");
  177. MODULE_DESCRIPTION("cz-rt5645 audio support");
  178. MODULE_LICENSE("GPL v2");