acp-da7219-max98357a.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Machine driver for AMD ACP Audio engine using DA7219 & MAX98357 codec
  3. *
  4. * Copyright 2017 Advanced Micro Devices, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include <sound/core.h>
  26. #include <sound/soc.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc-dapm.h>
  30. #include <sound/jack.h>
  31. #include <linux/clk.h>
  32. #include <linux/gpio.h>
  33. #include <linux/module.h>
  34. #include <linux/i2c.h>
  35. #include <linux/acpi.h>
  36. #include "../codecs/da7219.h"
  37. #include "../codecs/da7219-aad.h"
  38. #define CZ_PLAT_CLK 24000000
  39. #define MCLK_RATE 24576000
  40. #define DUAL_CHANNEL 2
  41. static struct snd_soc_jack cz_jack;
  42. struct clk *da7219_dai_clk;
  43. static int cz_da7219_init(struct snd_soc_pcm_runtime *rtd)
  44. {
  45. int ret;
  46. struct snd_soc_card *card = rtd->card;
  47. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  48. struct snd_soc_component *component = codec_dai->component;
  49. dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
  50. ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK,
  51. CZ_PLAT_CLK, SND_SOC_CLOCK_IN);
  52. if (ret < 0) {
  53. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  54. return ret;
  55. }
  56. ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL,
  57. CZ_PLAT_CLK, MCLK_RATE);
  58. if (ret < 0) {
  59. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  60. return ret;
  61. }
  62. da7219_dai_clk = clk_get(component->dev, "da7219-dai-clks");
  63. ret = snd_soc_card_jack_new(card, "Headset Jack",
  64. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  65. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  66. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  67. &cz_jack, NULL, 0);
  68. if (ret) {
  69. dev_err(card->dev, "HP jack creation failed %d\n", ret);
  70. return ret;
  71. }
  72. da7219_aad_jack_det(component, &cz_jack);
  73. return 0;
  74. }
  75. static int cz_da7219_hw_params(struct snd_pcm_substream *substream,
  76. struct snd_pcm_hw_params *params)
  77. {
  78. int ret = 0;
  79. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  80. ret = clk_prepare_enable(da7219_dai_clk);
  81. if (ret < 0) {
  82. dev_err(rtd->dev, "can't enable master clock %d\n", ret);
  83. return ret;
  84. }
  85. return ret;
  86. }
  87. static int cz_da7219_hw_free(struct snd_pcm_substream *substream)
  88. {
  89. clk_disable_unprepare(da7219_dai_clk);
  90. return 0;
  91. }
  92. static const unsigned int channels[] = {
  93. DUAL_CHANNEL,
  94. };
  95. static const unsigned int rates[] = {
  96. 48000,
  97. };
  98. static const struct snd_pcm_hw_constraint_list constraints_rates = {
  99. .count = ARRAY_SIZE(rates),
  100. .list = rates,
  101. .mask = 0,
  102. };
  103. static const struct snd_pcm_hw_constraint_list constraints_channels = {
  104. .count = ARRAY_SIZE(channels),
  105. .list = channels,
  106. .mask = 0,
  107. };
  108. static int cz_fe_startup(struct snd_pcm_substream *substream)
  109. {
  110. struct snd_pcm_runtime *runtime = substream->runtime;
  111. /*
  112. * On this platform for PCM device we support stereo
  113. */
  114. runtime->hw.channels_max = DUAL_CHANNEL;
  115. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  116. &constraints_channels);
  117. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  118. &constraints_rates);
  119. return 0;
  120. }
  121. static struct snd_soc_ops cz_da7219_cap_ops = {
  122. .hw_params = cz_da7219_hw_params,
  123. .hw_free = cz_da7219_hw_free,
  124. .startup = cz_fe_startup,
  125. };
  126. static struct snd_soc_ops cz_max_play_ops = {
  127. .hw_params = cz_da7219_hw_params,
  128. .hw_free = cz_da7219_hw_free,
  129. };
  130. static struct snd_soc_ops cz_dmic_cap_ops = {
  131. .hw_params = cz_da7219_hw_params,
  132. .hw_free = cz_da7219_hw_free,
  133. };
  134. static struct snd_soc_dai_link cz_dai_7219_98357[] = {
  135. {
  136. .name = "amd-da7219-play-cap",
  137. .stream_name = "Playback and Capture",
  138. .platform_name = "acp_audio_dma.0.auto",
  139. .cpu_dai_name = "designware-i2s.3.auto",
  140. .codec_dai_name = "da7219-hifi",
  141. .codec_name = "i2c-DLGS7219:00",
  142. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  143. | SND_SOC_DAIFMT_CBM_CFM,
  144. .init = cz_da7219_init,
  145. .dpcm_playback = 1,
  146. .dpcm_capture = 1,
  147. .ops = &cz_da7219_cap_ops,
  148. },
  149. {
  150. .name = "amd-max98357-play",
  151. .stream_name = "HiFi Playback",
  152. .platform_name = "acp_audio_dma.0.auto",
  153. .cpu_dai_name = "designware-i2s.1.auto",
  154. .codec_dai_name = "HiFi",
  155. .codec_name = "MX98357A:00",
  156. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  157. | SND_SOC_DAIFMT_CBM_CFM,
  158. .dpcm_playback = 1,
  159. .ops = &cz_max_play_ops,
  160. },
  161. {
  162. .name = "dmic",
  163. .stream_name = "DMIC Capture",
  164. .platform_name = "acp_audio_dma.0.auto",
  165. .cpu_dai_name = "designware-i2s.2.auto",
  166. .codec_dai_name = "adau7002-hifi",
  167. .codec_name = "ADAU7002:00",
  168. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  169. | SND_SOC_DAIFMT_CBM_CFM,
  170. .dpcm_capture = 1,
  171. .ops = &cz_dmic_cap_ops,
  172. },
  173. };
  174. static const struct snd_soc_dapm_widget cz_widgets[] = {
  175. SND_SOC_DAPM_HP("Headphones", NULL),
  176. SND_SOC_DAPM_SPK("Speakers", NULL),
  177. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  178. SND_SOC_DAPM_MIC("Int Mic", NULL),
  179. };
  180. static const struct snd_soc_dapm_route cz_audio_route[] = {
  181. {"Headphones", NULL, "HPL"},
  182. {"Headphones", NULL, "HPR"},
  183. {"MIC", NULL, "Headset Mic"},
  184. {"Speakers", NULL, "Speaker"},
  185. {"PDM_DAT", NULL, "Int Mic"},
  186. };
  187. static const struct snd_kcontrol_new cz_mc_controls[] = {
  188. SOC_DAPM_PIN_SWITCH("Headphones"),
  189. SOC_DAPM_PIN_SWITCH("Speakers"),
  190. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  191. SOC_DAPM_PIN_SWITCH("Int Mic"),
  192. };
  193. static struct snd_soc_card cz_card = {
  194. .name = "acpd7219m98357",
  195. .owner = THIS_MODULE,
  196. .dai_link = cz_dai_7219_98357,
  197. .num_links = ARRAY_SIZE(cz_dai_7219_98357),
  198. .dapm_widgets = cz_widgets,
  199. .num_dapm_widgets = ARRAY_SIZE(cz_widgets),
  200. .dapm_routes = cz_audio_route,
  201. .num_dapm_routes = ARRAY_SIZE(cz_audio_route),
  202. .controls = cz_mc_controls,
  203. .num_controls = ARRAY_SIZE(cz_mc_controls),
  204. };
  205. static int cz_probe(struct platform_device *pdev)
  206. {
  207. int ret;
  208. struct snd_soc_card *card;
  209. card = &cz_card;
  210. cz_card.dev = &pdev->dev;
  211. platform_set_drvdata(pdev, card);
  212. ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
  213. if (ret) {
  214. dev_err(&pdev->dev,
  215. "devm_snd_soc_register_card(%s) failed: %d\n",
  216. cz_card.name, ret);
  217. return ret;
  218. }
  219. return 0;
  220. }
  221. static const struct acpi_device_id cz_audio_acpi_match[] = {
  222. { "AMD7219", 0 },
  223. {},
  224. };
  225. MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
  226. static struct platform_driver cz_pcm_driver = {
  227. .driver = {
  228. .name = "cz-da7219-max98357a",
  229. .acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
  230. .pm = &snd_soc_pm_ops,
  231. },
  232. .probe = cz_probe,
  233. };
  234. module_platform_driver(cz_pcm_driver);
  235. MODULE_AUTHOR("akshu.agrawal@amd.com");
  236. MODULE_DESCRIPTION("DA7219 & MAX98357A audio support");
  237. MODULE_LICENSE("GPL v2");