acp-es8336.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Machine driver for AMD Stoney platform using ES8336 Codec
  4. *
  5. * Copyright 2022 Advanced Micro Devices, Inc.
  6. */
  7. #include <sound/core.h>
  8. #include <sound/soc.h>
  9. #include <sound/pcm.h>
  10. #include <sound/pcm_params.h>
  11. #include <sound/soc-dapm.h>
  12. #include <sound/jack.h>
  13. #include <linux/gpio.h>
  14. #include <linux/device.h>
  15. #include <linux/dmi.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/gpio/machine.h>
  18. #include <linux/i2c.h>
  19. #include <linux/input.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/acpi.h>
  23. #include "acp.h"
  24. #define DUAL_CHANNEL 2
  25. #define DRV_NAME "acp2x_mach"
  26. #define ST_JADEITE 1
  27. #define ES8336_PLL_FREQ (48000 * 256)
  28. static unsigned long acp2x_machine_id;
  29. static struct snd_soc_jack st_jack;
  30. static struct device *codec_dev;
  31. static struct gpio_desc *gpio_pa;
  32. static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
  33. struct snd_kcontrol *kcontrol, int event)
  34. {
  35. if (SND_SOC_DAPM_EVENT_ON(event))
  36. gpiod_set_value_cansleep(gpio_pa, true);
  37. else
  38. gpiod_set_value_cansleep(gpio_pa, false);
  39. return 0;
  40. }
  41. static struct snd_soc_jack_pin st_es8316_jack_pins[] = {
  42. {
  43. .pin = "Headphone",
  44. .mask = SND_JACK_HEADPHONE,
  45. },
  46. {
  47. .pin = "Headset Mic",
  48. .mask = SND_JACK_MICROPHONE,
  49. },
  50. };
  51. static int st_es8336_init(struct snd_soc_pcm_runtime *rtd)
  52. {
  53. int ret;
  54. struct snd_soc_card *card;
  55. struct snd_soc_component *codec;
  56. codec = asoc_rtd_to_codec(rtd, 0)->component;
  57. card = rtd->card;
  58. ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0,
  59. &st_jack, st_es8316_jack_pins,
  60. ARRAY_SIZE(st_es8316_jack_pins));
  61. if (ret) {
  62. dev_err(card->dev, "HP jack creation failed %d\n", ret);
  63. return ret;
  64. }
  65. snd_jack_set_key(st_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
  66. ret = snd_soc_component_set_jack(codec, &st_jack, NULL);
  67. if (ret) {
  68. dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
  69. return ret;
  70. }
  71. return 0;
  72. }
  73. static const unsigned int st_channels[] = {
  74. DUAL_CHANNEL,
  75. };
  76. static const unsigned int st_rates[] = {
  77. 48000,
  78. };
  79. static const struct snd_pcm_hw_constraint_list st_constraints_rates = {
  80. .count = ARRAY_SIZE(st_rates),
  81. .list = st_rates,
  82. .mask = 0,
  83. };
  84. static const struct snd_pcm_hw_constraint_list st_constraints_channels = {
  85. .count = ARRAY_SIZE(st_channels),
  86. .list = st_channels,
  87. .mask = 0,
  88. };
  89. static int st_es8336_codec_startup(struct snd_pcm_substream *substream)
  90. {
  91. struct snd_pcm_runtime *runtime;
  92. struct snd_soc_pcm_runtime *rtd;
  93. struct snd_soc_card *card;
  94. struct acp_platform_info *machine;
  95. struct snd_soc_dai *codec_dai;
  96. int ret;
  97. runtime = substream->runtime;
  98. rtd = asoc_substream_to_rtd(substream);
  99. card = rtd->card;
  100. machine = snd_soc_card_get_drvdata(card);
  101. codec_dai = asoc_rtd_to_codec(rtd, 0);
  102. ret = snd_soc_dai_set_sysclk(codec_dai, 0, ES8336_PLL_FREQ, SND_SOC_CLOCK_IN);
  103. if (ret < 0) {
  104. dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
  105. return ret;
  106. }
  107. runtime->hw.channels_max = DUAL_CHANNEL;
  108. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  109. &st_constraints_channels);
  110. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  111. &st_constraints_rates);
  112. machine->play_i2s_instance = I2S_MICSP_INSTANCE;
  113. machine->cap_i2s_instance = I2S_MICSP_INSTANCE;
  114. machine->capture_channel = CAP_CHANNEL0;
  115. return 0;
  116. }
  117. static const struct snd_soc_ops st_es8336_ops = {
  118. .startup = st_es8336_codec_startup,
  119. };
  120. SND_SOC_DAILINK_DEF(designware1,
  121. DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.2.auto")));
  122. SND_SOC_DAILINK_DEF(codec,
  123. DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8336:00", "ES8316 HiFi")));
  124. SND_SOC_DAILINK_DEF(platform,
  125. DAILINK_COMP_ARRAY(COMP_PLATFORM("acp_audio_dma.1.auto")));
  126. static struct snd_soc_dai_link st_dai_es8336[] = {
  127. {
  128. .name = "amdes8336",
  129. .stream_name = "ES8336 HiFi Play",
  130. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  131. | SND_SOC_DAIFMT_CBP_CFP,
  132. .stop_dma_first = 1,
  133. .dpcm_capture = 1,
  134. .dpcm_playback = 1,
  135. .init = st_es8336_init,
  136. .ops = &st_es8336_ops,
  137. SND_SOC_DAILINK_REG(designware1, codec, platform),
  138. },
  139. };
  140. static const struct snd_soc_dapm_widget st_widgets[] = {
  141. SND_SOC_DAPM_SPK("Speaker", NULL),
  142. SND_SOC_DAPM_HP("Headphone", NULL),
  143. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  144. SND_SOC_DAPM_MIC("Internal Mic", NULL),
  145. SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
  146. sof_es8316_speaker_power_event,
  147. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  148. };
  149. static const struct snd_soc_dapm_route st_audio_route[] = {
  150. {"Speaker", NULL, "HPOL"},
  151. {"Speaker", NULL, "HPOR"},
  152. {"Headphone", NULL, "HPOL"},
  153. {"Headphone", NULL, "HPOR"},
  154. {"MIC1", NULL, "Headset Mic"},
  155. {"MIC2", NULL, "Internal Mic"},
  156. {"Speaker", NULL, "Speaker Power"},
  157. };
  158. static const struct snd_kcontrol_new st_mc_controls[] = {
  159. SOC_DAPM_PIN_SWITCH("Speaker"),
  160. SOC_DAPM_PIN_SWITCH("Headphone"),
  161. SOC_DAPM_PIN_SWITCH("Headset Mic"),
  162. SOC_DAPM_PIN_SWITCH("Internal Mic"),
  163. };
  164. static const struct acpi_gpio_params pa_enable_gpio = { 0, 0, false };
  165. static const struct acpi_gpio_mapping acpi_es8336_gpios[] = {
  166. { "pa-enable-gpios", &pa_enable_gpio, 1 },
  167. { }
  168. };
  169. static int st_es8336_late_probe(struct snd_soc_card *card)
  170. {
  171. struct acpi_device *adev;
  172. int ret;
  173. adev = acpi_dev_get_first_match_dev("ESSX8336", NULL, -1);
  174. if (!adev)
  175. return -ENODEV;
  176. codec_dev = acpi_get_first_physical_node(adev);
  177. acpi_dev_put(adev);
  178. if (!codec_dev)
  179. dev_err(card->dev, "can not find codec dev\n");
  180. ret = devm_acpi_dev_add_driver_gpios(codec_dev, acpi_es8336_gpios);
  181. if (ret)
  182. dev_warn(card->dev, "Failed to add driver gpios\n");
  183. gpio_pa = gpiod_get_optional(codec_dev, "pa-enable", GPIOD_OUT_LOW);
  184. if (IS_ERR(gpio_pa)) {
  185. ret = dev_err_probe(card->dev, PTR_ERR(gpio_pa),
  186. "could not get pa-enable GPIO\n");
  187. put_device(codec_dev);
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static struct snd_soc_card st_card = {
  193. .name = "acpes8336",
  194. .owner = THIS_MODULE,
  195. .dai_link = st_dai_es8336,
  196. .num_links = ARRAY_SIZE(st_dai_es8336),
  197. .dapm_widgets = st_widgets,
  198. .num_dapm_widgets = ARRAY_SIZE(st_widgets),
  199. .dapm_routes = st_audio_route,
  200. .num_dapm_routes = ARRAY_SIZE(st_audio_route),
  201. .controls = st_mc_controls,
  202. .num_controls = ARRAY_SIZE(st_mc_controls),
  203. .late_probe = st_es8336_late_probe,
  204. };
  205. static int st_es8336_quirk_cb(const struct dmi_system_id *id)
  206. {
  207. acp2x_machine_id = ST_JADEITE;
  208. return 1;
  209. }
  210. static const struct dmi_system_id st_es8336_quirk_table[] = {
  211. {
  212. .callback = st_es8336_quirk_cb,
  213. .matches = {
  214. DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMD"),
  215. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jadeite"),
  216. },
  217. },
  218. {
  219. .callback = st_es8336_quirk_cb,
  220. .matches = {
  221. DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "IP3 Technology CO.,Ltd."),
  222. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ASN1D"),
  223. },
  224. },
  225. {
  226. .callback = st_es8336_quirk_cb,
  227. .matches = {
  228. DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Standard"),
  229. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ASN10"),
  230. },
  231. },
  232. {}
  233. };
  234. static int st_es8336_probe(struct platform_device *pdev)
  235. {
  236. int ret;
  237. struct snd_soc_card *card;
  238. struct acp_platform_info *machine;
  239. machine = devm_kzalloc(&pdev->dev, sizeof(struct acp_platform_info), GFP_KERNEL);
  240. if (!machine)
  241. return -ENOMEM;
  242. dmi_check_system(st_es8336_quirk_table);
  243. switch (acp2x_machine_id) {
  244. case ST_JADEITE:
  245. card = &st_card;
  246. st_card.dev = &pdev->dev;
  247. break;
  248. default:
  249. return -ENODEV;
  250. }
  251. platform_set_drvdata(pdev, card);
  252. snd_soc_card_set_drvdata(card, machine);
  253. ret = devm_snd_soc_register_card(&pdev->dev, &st_card);
  254. if (ret) {
  255. return dev_err_probe(&pdev->dev, ret,
  256. "devm_snd_soc_register_card(%s) failed\n",
  257. card->name);
  258. }
  259. return 0;
  260. }
  261. #ifdef CONFIG_ACPI
  262. static const struct acpi_device_id st_audio_acpi_match[] = {
  263. {"AMDI8336", 0},
  264. {},
  265. };
  266. MODULE_DEVICE_TABLE(acpi, st_audio_acpi_match);
  267. #endif
  268. static struct platform_driver st_mach_driver = {
  269. .driver = {
  270. .name = "st-es8316",
  271. .acpi_match_table = ACPI_PTR(st_audio_acpi_match),
  272. .pm = &snd_soc_pm_ops,
  273. },
  274. .probe = st_es8336_probe,
  275. };
  276. module_platform_driver(st_mach_driver);
  277. MODULE_AUTHOR("[email protected]");
  278. MODULE_DESCRIPTION("st-es8316 audio support");
  279. MODULE_LICENSE("GPL v2");