acp-rt5645.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 = substream->private_data;
  46. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  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 = rtd->codec_dai->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, NULL, 0);
  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 struct snd_soc_ops cz_aif1_ops = {
  81. .hw_params = cz_aif1_hw_params,
  82. };
  83. static struct snd_soc_dai_link cz_dai_rt5650[] = {
  84. {
  85. .name = "amd-rt5645-play",
  86. .stream_name = "RT5645_AIF1",
  87. .platform_name = "acp_audio_dma.0.auto",
  88. .cpu_dai_name = "designware-i2s.1.auto",
  89. .codec_dai_name = "rt5645-aif1",
  90. .codec_name = "i2c-10EC5650:00",
  91. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  92. | SND_SOC_DAIFMT_CBM_CFM,
  93. .init = cz_init,
  94. .ops = &cz_aif1_ops,
  95. },
  96. {
  97. .name = "amd-rt5645-cap",
  98. .stream_name = "RT5645_AIF1",
  99. .platform_name = "acp_audio_dma.0.auto",
  100. .cpu_dai_name = "designware-i2s.2.auto",
  101. .codec_dai_name = "rt5645-aif1",
  102. .codec_name = "i2c-10EC5650:00",
  103. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  104. | SND_SOC_DAIFMT_CBM_CFM,
  105. .ops = &cz_aif1_ops,
  106. },
  107. };
  108. static const struct snd_soc_dapm_widget cz_widgets[] = {
  109. SND_SOC_DAPM_HP("Headphones", NULL),
  110. SND_SOC_DAPM_SPK("Speakers", NULL),
  111. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  112. SND_SOC_DAPM_MIC("Int Mic", NULL),
  113. };
  114. static const struct snd_soc_dapm_route cz_audio_route[] = {
  115. {"Headphones", NULL, "HPOL"},
  116. {"Headphones", NULL, "HPOR"},
  117. {"RECMIXL", NULL, "Headset Mic"},
  118. {"RECMIXR", NULL, "Headset Mic"},
  119. {"Speakers", NULL, "SPOL"},
  120. {"Speakers", NULL, "SPOR"},
  121. {"DMIC L2", NULL, "Int Mic"},
  122. {"DMIC R2", NULL, "Int Mic"},
  123. };
  124. static const struct snd_kcontrol_new cz_mc_controls[] = {
  125. SOC_DAPM_PIN_SWITCH("Headphones"),
  126. SOC_DAPM_PIN_SWITCH("Speakers"),
  127. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  128. SOC_DAPM_PIN_SWITCH("Int Mic"),
  129. };
  130. static struct snd_soc_card cz_card = {
  131. .name = "acprt5650",
  132. .owner = THIS_MODULE,
  133. .dai_link = cz_dai_rt5650,
  134. .num_links = ARRAY_SIZE(cz_dai_rt5650),
  135. .dapm_widgets = cz_widgets,
  136. .num_dapm_widgets = ARRAY_SIZE(cz_widgets),
  137. .dapm_routes = cz_audio_route,
  138. .num_dapm_routes = ARRAY_SIZE(cz_audio_route),
  139. .controls = cz_mc_controls,
  140. .num_controls = ARRAY_SIZE(cz_mc_controls),
  141. };
  142. static int cz_probe(struct platform_device *pdev)
  143. {
  144. int ret;
  145. struct snd_soc_card *card;
  146. card = &cz_card;
  147. cz_card.dev = &pdev->dev;
  148. platform_set_drvdata(pdev, card);
  149. ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
  150. if (ret) {
  151. dev_err(&pdev->dev,
  152. "devm_snd_soc_register_card(%s) failed: %d\n",
  153. cz_card.name, ret);
  154. return ret;
  155. }
  156. return 0;
  157. }
  158. static const struct acpi_device_id cz_audio_acpi_match[] = {
  159. { "AMDI1002", 0 },
  160. {},
  161. };
  162. MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
  163. static struct platform_driver cz_pcm_driver = {
  164. .driver = {
  165. .name = "cz-rt5645",
  166. .acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
  167. .pm = &snd_soc_pm_ops,
  168. },
  169. .probe = cz_probe,
  170. };
  171. module_platform_driver(cz_pcm_driver);
  172. MODULE_AUTHOR("akshu.agrawal@amd.com");
  173. MODULE_DESCRIPTION("cz-rt5645 audio support");
  174. MODULE_LICENSE("GPL v2");