acp-da7219-max98357a.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/regulator/machine.h>
  35. #include <linux/regulator/driver.h>
  36. #include <linux/i2c.h>
  37. #include <linux/input.h>
  38. #include <linux/acpi.h>
  39. #include "acp.h"
  40. #include "../codecs/da7219.h"
  41. #include "../codecs/da7219-aad.h"
  42. #define CZ_PLAT_CLK 48000000
  43. #define DUAL_CHANNEL 2
  44. static struct snd_soc_jack cz_jack;
  45. static struct clk *da7219_dai_clk;
  46. extern int bt_uart_enable;
  47. static int cz_da7219_init(struct snd_soc_pcm_runtime *rtd)
  48. {
  49. int ret;
  50. struct snd_soc_card *card = rtd->card;
  51. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  52. struct snd_soc_component *component = codec_dai->component;
  53. dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
  54. ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK,
  55. CZ_PLAT_CLK, SND_SOC_CLOCK_IN);
  56. if (ret < 0) {
  57. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  58. return ret;
  59. }
  60. ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL,
  61. CZ_PLAT_CLK, DA7219_PLL_FREQ_OUT_98304);
  62. if (ret < 0) {
  63. dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
  64. return ret;
  65. }
  66. da7219_dai_clk = clk_get(component->dev, "da7219-dai-clks");
  67. ret = snd_soc_card_jack_new(card, "Headset Jack",
  68. SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
  69. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  70. SND_JACK_BTN_2 | SND_JACK_BTN_3,
  71. &cz_jack, NULL, 0);
  72. if (ret) {
  73. dev_err(card->dev, "HP jack creation failed %d\n", ret);
  74. return ret;
  75. }
  76. snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  77. snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
  78. snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
  79. snd_jack_set_key(cz_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
  80. da7219_aad_jack_det(component, &cz_jack);
  81. return 0;
  82. }
  83. static int da7219_clk_enable(struct snd_pcm_substream *substream)
  84. {
  85. int ret = 0;
  86. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  87. ret = clk_prepare_enable(da7219_dai_clk);
  88. if (ret < 0) {
  89. dev_err(rtd->dev, "can't enable master clock %d\n", ret);
  90. return ret;
  91. }
  92. return ret;
  93. }
  94. static void da7219_clk_disable(void)
  95. {
  96. clk_disable_unprepare(da7219_dai_clk);
  97. }
  98. static const unsigned int channels[] = {
  99. DUAL_CHANNEL,
  100. };
  101. static const unsigned int rates[] = {
  102. 48000,
  103. };
  104. static const struct snd_pcm_hw_constraint_list constraints_rates = {
  105. .count = ARRAY_SIZE(rates),
  106. .list = rates,
  107. .mask = 0,
  108. };
  109. static const struct snd_pcm_hw_constraint_list constraints_channels = {
  110. .count = ARRAY_SIZE(channels),
  111. .list = channels,
  112. .mask = 0,
  113. };
  114. static int cz_da7219_startup(struct snd_pcm_substream *substream)
  115. {
  116. struct snd_pcm_runtime *runtime = substream->runtime;
  117. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  118. struct snd_soc_card *card = rtd->card;
  119. struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
  120. /*
  121. * On this platform for PCM device we support stereo
  122. */
  123. runtime->hw.channels_max = DUAL_CHANNEL;
  124. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  125. &constraints_channels);
  126. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  127. &constraints_rates);
  128. machine->i2s_instance = I2S_SP_INSTANCE;
  129. machine->capture_channel = CAP_CHANNEL1;
  130. return da7219_clk_enable(substream);
  131. }
  132. static void cz_da7219_shutdown(struct snd_pcm_substream *substream)
  133. {
  134. da7219_clk_disable();
  135. }
  136. static int cz_max_startup(struct snd_pcm_substream *substream)
  137. {
  138. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  139. struct snd_soc_card *card = rtd->card;
  140. struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
  141. machine->i2s_instance = I2S_BT_INSTANCE;
  142. return da7219_clk_enable(substream);
  143. }
  144. static void cz_max_shutdown(struct snd_pcm_substream *substream)
  145. {
  146. da7219_clk_disable();
  147. }
  148. static int cz_dmic0_startup(struct snd_pcm_substream *substream)
  149. {
  150. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  151. struct snd_soc_card *card = rtd->card;
  152. struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
  153. machine->i2s_instance = I2S_BT_INSTANCE;
  154. return da7219_clk_enable(substream);
  155. }
  156. static int cz_dmic1_startup(struct snd_pcm_substream *substream)
  157. {
  158. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  159. struct snd_soc_card *card = rtd->card;
  160. struct acp_platform_info *machine = snd_soc_card_get_drvdata(card);
  161. machine->i2s_instance = I2S_SP_INSTANCE;
  162. machine->capture_channel = CAP_CHANNEL0;
  163. return da7219_clk_enable(substream);
  164. }
  165. static void cz_dmic_shutdown(struct snd_pcm_substream *substream)
  166. {
  167. da7219_clk_disable();
  168. }
  169. static const struct snd_soc_ops cz_da7219_cap_ops = {
  170. .startup = cz_da7219_startup,
  171. .shutdown = cz_da7219_shutdown,
  172. };
  173. static const struct snd_soc_ops cz_max_play_ops = {
  174. .startup = cz_max_startup,
  175. .shutdown = cz_max_shutdown,
  176. };
  177. static const struct snd_soc_ops cz_dmic0_cap_ops = {
  178. .startup = cz_dmic0_startup,
  179. .shutdown = cz_dmic_shutdown,
  180. };
  181. static const struct snd_soc_ops cz_dmic1_cap_ops = {
  182. .startup = cz_dmic1_startup,
  183. .shutdown = cz_dmic_shutdown,
  184. };
  185. static struct snd_soc_dai_link cz_dai_7219_98357[] = {
  186. {
  187. .name = "amd-da7219-play",
  188. .stream_name = "Playback",
  189. .platform_name = "acp_audio_dma.0.auto",
  190. .cpu_dai_name = "designware-i2s.1.auto",
  191. .codec_dai_name = "da7219-hifi",
  192. .codec_name = "i2c-DLGS7219:00",
  193. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  194. | SND_SOC_DAIFMT_CBM_CFM,
  195. .init = cz_da7219_init,
  196. .dpcm_playback = 1,
  197. .ops = &cz_da7219_cap_ops,
  198. },
  199. {
  200. .name = "amd-da7219-cap",
  201. .stream_name = "Capture",
  202. .platform_name = "acp_audio_dma.0.auto",
  203. .cpu_dai_name = "designware-i2s.2.auto",
  204. .codec_dai_name = "da7219-hifi",
  205. .codec_name = "i2c-DLGS7219:00",
  206. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  207. | SND_SOC_DAIFMT_CBM_CFM,
  208. .dpcm_capture = 1,
  209. .ops = &cz_da7219_cap_ops,
  210. },
  211. {
  212. .name = "amd-max98357-play",
  213. .stream_name = "HiFi Playback",
  214. .platform_name = "acp_audio_dma.0.auto",
  215. .cpu_dai_name = "designware-i2s.3.auto",
  216. .codec_dai_name = "HiFi",
  217. .codec_name = "MX98357A:00",
  218. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  219. | SND_SOC_DAIFMT_CBM_CFM,
  220. .dpcm_playback = 1,
  221. .ops = &cz_max_play_ops,
  222. },
  223. {
  224. /* C panel DMIC */
  225. .name = "dmic0",
  226. .stream_name = "DMIC0 Capture",
  227. .platform_name = "acp_audio_dma.0.auto",
  228. .cpu_dai_name = "designware-i2s.3.auto",
  229. .codec_dai_name = "adau7002-hifi",
  230. .codec_name = "ADAU7002:00",
  231. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  232. | SND_SOC_DAIFMT_CBM_CFM,
  233. .dpcm_capture = 1,
  234. .ops = &cz_dmic0_cap_ops,
  235. },
  236. {
  237. /* A/B panel DMIC */
  238. .name = "dmic1",
  239. .stream_name = "DMIC1 Capture",
  240. .platform_name = "acp_audio_dma.0.auto",
  241. .cpu_dai_name = "designware-i2s.2.auto",
  242. .codec_dai_name = "adau7002-hifi",
  243. .codec_name = "ADAU7002:00",
  244. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  245. | SND_SOC_DAIFMT_CBM_CFM,
  246. .dpcm_capture = 1,
  247. .ops = &cz_dmic1_cap_ops,
  248. },
  249. };
  250. static const struct snd_soc_dapm_widget cz_widgets[] = {
  251. SND_SOC_DAPM_HP("Headphones", NULL),
  252. SND_SOC_DAPM_SPK("Speakers", NULL),
  253. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  254. SND_SOC_DAPM_MIC("Int Mic", NULL),
  255. };
  256. static const struct snd_soc_dapm_route cz_audio_route[] = {
  257. {"Headphones", NULL, "HPL"},
  258. {"Headphones", NULL, "HPR"},
  259. {"MIC", NULL, "Headset Mic"},
  260. {"Speakers", NULL, "Speaker"},
  261. {"PDM_DAT", NULL, "Int Mic"},
  262. };
  263. static const struct snd_kcontrol_new cz_mc_controls[] = {
  264. SOC_DAPM_PIN_SWITCH("Headphones"),
  265. SOC_DAPM_PIN_SWITCH("Speakers"),
  266. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  267. SOC_DAPM_PIN_SWITCH("Int Mic"),
  268. };
  269. static struct snd_soc_card cz_card = {
  270. .name = "acpd7219m98357",
  271. .owner = THIS_MODULE,
  272. .dai_link = cz_dai_7219_98357,
  273. .num_links = ARRAY_SIZE(cz_dai_7219_98357),
  274. .dapm_widgets = cz_widgets,
  275. .num_dapm_widgets = ARRAY_SIZE(cz_widgets),
  276. .dapm_routes = cz_audio_route,
  277. .num_dapm_routes = ARRAY_SIZE(cz_audio_route),
  278. .controls = cz_mc_controls,
  279. .num_controls = ARRAY_SIZE(cz_mc_controls),
  280. };
  281. static struct regulator_consumer_supply acp_da7219_supplies[] = {
  282. REGULATOR_SUPPLY("VDD", "i2c-DLGS7219:00"),
  283. REGULATOR_SUPPLY("VDDMIC", "i2c-DLGS7219:00"),
  284. REGULATOR_SUPPLY("VDDIO", "i2c-DLGS7219:00"),
  285. REGULATOR_SUPPLY("IOVDD", "ADAU7002:00"),
  286. };
  287. static struct regulator_init_data acp_da7219_data = {
  288. .constraints = {
  289. .always_on = 1,
  290. },
  291. .num_consumer_supplies = ARRAY_SIZE(acp_da7219_supplies),
  292. .consumer_supplies = acp_da7219_supplies,
  293. };
  294. static struct regulator_config acp_da7219_cfg = {
  295. .init_data = &acp_da7219_data,
  296. };
  297. static struct regulator_ops acp_da7219_ops = {
  298. };
  299. static struct regulator_desc acp_da7219_desc = {
  300. .name = "reg-fixed-1.8V",
  301. .type = REGULATOR_VOLTAGE,
  302. .owner = THIS_MODULE,
  303. .ops = &acp_da7219_ops,
  304. .fixed_uV = 1800000, /* 1.8V */
  305. .n_voltages = 1,
  306. };
  307. static int cz_probe(struct platform_device *pdev)
  308. {
  309. int ret;
  310. struct snd_soc_card *card;
  311. struct acp_platform_info *machine;
  312. struct regulator_dev *rdev;
  313. acp_da7219_cfg.dev = &pdev->dev;
  314. rdev = devm_regulator_register(&pdev->dev, &acp_da7219_desc,
  315. &acp_da7219_cfg);
  316. if (IS_ERR(rdev)) {
  317. dev_err(&pdev->dev, "Failed to register regulator: %d\n",
  318. (int)PTR_ERR(rdev));
  319. return -EINVAL;
  320. }
  321. machine = devm_kzalloc(&pdev->dev, sizeof(struct acp_platform_info),
  322. GFP_KERNEL);
  323. if (!machine)
  324. return -ENOMEM;
  325. card = &cz_card;
  326. cz_card.dev = &pdev->dev;
  327. platform_set_drvdata(pdev, card);
  328. snd_soc_card_set_drvdata(card, machine);
  329. ret = devm_snd_soc_register_card(&pdev->dev, &cz_card);
  330. if (ret) {
  331. dev_err(&pdev->dev,
  332. "devm_snd_soc_register_card(%s) failed: %d\n",
  333. cz_card.name, ret);
  334. return ret;
  335. }
  336. bt_uart_enable = !device_property_read_bool(&pdev->dev,
  337. "bt-pad-enable");
  338. return 0;
  339. }
  340. static const struct acpi_device_id cz_audio_acpi_match[] = {
  341. { "AMD7219", 0 },
  342. {},
  343. };
  344. MODULE_DEVICE_TABLE(acpi, cz_audio_acpi_match);
  345. static struct platform_driver cz_pcm_driver = {
  346. .driver = {
  347. .name = "cz-da7219-max98357a",
  348. .acpi_match_table = ACPI_PTR(cz_audio_acpi_match),
  349. .pm = &snd_soc_pm_ops,
  350. },
  351. .probe = cz_probe,
  352. };
  353. module_platform_driver(cz_pcm_driver);
  354. MODULE_AUTHOR("akshu.agrawal@amd.com");
  355. MODULE_DESCRIPTION("DA7219 & MAX98357A audio support");
  356. MODULE_LICENSE("GPL v2");