1 // SPDX-License-Identifier: GPL-2.0+
3 * Machine driver for AMD Stoney platform using ES8336 Codec
5 * Copyright 2022 Advanced Micro Devices, Inc.
8 #include <sound/core.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc-dapm.h>
13 #include <sound/jack.h>
14 #include <linux/gpio.h>
15 #include <linux/device.h>
16 #include <linux/dmi.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/gpio/machine.h>
19 #include <linux/i2c.h>
20 #include <linux/input.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/acpi.h>
27 #define DUAL_CHANNEL 2
28 #define DRV_NAME "acp2x_mach"
30 #define ES8336_PLL_FREQ (48000 * 256)
32 static unsigned long acp2x_machine_id;
33 static struct snd_soc_jack st_jack;
34 static struct device *codec_dev;
35 static struct gpio_desc *gpio_pa;
37 static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
38 struct snd_kcontrol *kcontrol, int event)
40 if (SND_SOC_DAPM_EVENT_ON(event))
41 gpiod_set_value_cansleep(gpio_pa, true);
43 gpiod_set_value_cansleep(gpio_pa, false);
48 static struct snd_soc_jack_pin st_es8316_jack_pins[] = {
51 .mask = SND_JACK_HEADPHONE,
55 .mask = SND_JACK_MICROPHONE,
59 static int st_es8336_init(struct snd_soc_pcm_runtime *rtd)
62 struct snd_soc_card *card;
63 struct snd_soc_component *codec;
65 codec = snd_soc_rtd_to_codec(rtd, 0)->component;
68 ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0,
69 &st_jack, st_es8316_jack_pins,
70 ARRAY_SIZE(st_es8316_jack_pins));
72 dev_err(card->dev, "HP jack creation failed %d\n", ret);
75 snd_jack_set_key(st_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
76 ret = snd_soc_component_set_jack(codec, &st_jack, NULL);
78 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
84 static const unsigned int st_channels[] = {
88 static const unsigned int st_rates[] = {
92 static const struct snd_pcm_hw_constraint_list st_constraints_rates = {
93 .count = ARRAY_SIZE(st_rates),
98 static const struct snd_pcm_hw_constraint_list st_constraints_channels = {
99 .count = ARRAY_SIZE(st_channels),
104 static int st_es8336_codec_startup(struct snd_pcm_substream *substream)
106 struct snd_pcm_runtime *runtime;
107 struct snd_soc_pcm_runtime *rtd;
108 struct snd_soc_card *card;
109 struct acp_platform_info *machine;
110 struct snd_soc_dai *codec_dai;
113 runtime = substream->runtime;
114 rtd = snd_soc_substream_to_rtd(substream);
116 machine = snd_soc_card_get_drvdata(card);
117 codec_dai = snd_soc_rtd_to_codec(rtd, 0);
118 ret = snd_soc_dai_set_sysclk(codec_dai, 0, ES8336_PLL_FREQ, SND_SOC_CLOCK_IN);
120 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
123 runtime->hw.channels_max = DUAL_CHANNEL;
124 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
125 &st_constraints_channels);
126 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
127 &st_constraints_rates);
129 machine->play_i2s_instance = I2S_MICSP_INSTANCE;
130 machine->cap_i2s_instance = I2S_MICSP_INSTANCE;
131 machine->capture_channel = CAP_CHANNEL0;
135 static const struct snd_soc_ops st_es8336_ops = {
136 .startup = st_es8336_codec_startup,
139 SND_SOC_DAILINK_DEF(designware1,
140 DAILINK_COMP_ARRAY(COMP_CPU("designware-i2s.2.auto")));
141 SND_SOC_DAILINK_DEF(codec,
142 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8336:00", "ES8316 HiFi")));
143 SND_SOC_DAILINK_DEF(platform,
144 DAILINK_COMP_ARRAY(COMP_PLATFORM("acp_audio_dma.1.auto")));
146 static struct snd_soc_dai_link st_dai_es8336[] = {
149 .stream_name = "ES8336 HiFi Play",
150 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
151 | SND_SOC_DAIFMT_CBP_CFP,
152 .trigger_stop = SND_SOC_TRIGGER_ORDER_LDC,
155 .init = st_es8336_init,
156 .ops = &st_es8336_ops,
157 SND_SOC_DAILINK_REG(designware1, codec, platform),
161 static const struct snd_soc_dapm_widget st_widgets[] = {
162 SND_SOC_DAPM_SPK("Speaker", NULL),
163 SND_SOC_DAPM_HP("Headphone", NULL),
164 SND_SOC_DAPM_MIC("Headset Mic", NULL),
165 SND_SOC_DAPM_MIC("Internal Mic", NULL),
167 SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
168 sof_es8316_speaker_power_event,
169 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
172 static const struct snd_soc_dapm_route st_audio_route[] = {
173 {"Speaker", NULL, "HPOL"},
174 {"Speaker", NULL, "HPOR"},
175 {"Headphone", NULL, "HPOL"},
176 {"Headphone", NULL, "HPOR"},
177 {"MIC1", NULL, "Headset Mic"},
178 {"MIC2", NULL, "Internal Mic"},
179 {"Speaker", NULL, "Speaker Power"},
182 static const struct snd_kcontrol_new st_mc_controls[] = {
183 SOC_DAPM_PIN_SWITCH("Speaker"),
184 SOC_DAPM_PIN_SWITCH("Headphone"),
185 SOC_DAPM_PIN_SWITCH("Headset Mic"),
186 SOC_DAPM_PIN_SWITCH("Internal Mic"),
189 static const struct acpi_gpio_params pa_enable_gpio = { 0, 0, false };
190 static const struct acpi_gpio_mapping acpi_es8336_gpios[] = {
191 { "pa-enable-gpios", &pa_enable_gpio, 1 },
195 static int st_es8336_late_probe(struct snd_soc_card *card)
197 struct acpi_device *adev;
200 adev = acpi_dev_get_first_match_dev("ESSX8336", NULL, -1);
204 codec_dev = acpi_get_first_physical_node(adev);
207 dev_err(card->dev, "can not find codec dev\n");
211 ret = devm_acpi_dev_add_driver_gpios(codec_dev, acpi_es8336_gpios);
213 dev_warn(card->dev, "Failed to add driver gpios\n");
215 gpio_pa = gpiod_get_optional(codec_dev, "pa-enable", GPIOD_OUT_LOW);
216 if (IS_ERR(gpio_pa)) {
217 ret = dev_err_probe(card->dev, PTR_ERR(gpio_pa),
218 "could not get pa-enable GPIO\n");
219 put_device(codec_dev);
225 static struct snd_soc_card st_card = {
227 .owner = THIS_MODULE,
228 .dai_link = st_dai_es8336,
229 .num_links = ARRAY_SIZE(st_dai_es8336),
230 .dapm_widgets = st_widgets,
231 .num_dapm_widgets = ARRAY_SIZE(st_widgets),
232 .dapm_routes = st_audio_route,
233 .num_dapm_routes = ARRAY_SIZE(st_audio_route),
234 .controls = st_mc_controls,
235 .num_controls = ARRAY_SIZE(st_mc_controls),
236 .late_probe = st_es8336_late_probe,
239 static int st_es8336_quirk_cb(const struct dmi_system_id *id)
241 acp2x_machine_id = ST_JADEITE;
245 static const struct dmi_system_id st_es8336_quirk_table[] = {
247 .callback = st_es8336_quirk_cb,
249 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMD"),
250 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jadeite"),
254 .callback = st_es8336_quirk_cb,
256 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "IP3 Technology CO.,Ltd."),
257 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ASN1D"),
261 .callback = st_es8336_quirk_cb,
263 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Standard"),
264 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ASN10"),
270 static int st_es8336_probe(struct platform_device *pdev)
273 struct snd_soc_card *card;
274 struct acp_platform_info *machine;
276 machine = devm_kzalloc(&pdev->dev, sizeof(struct acp_platform_info), GFP_KERNEL);
280 dmi_check_system(st_es8336_quirk_table);
281 switch (acp2x_machine_id) {
284 st_card.dev = &pdev->dev;
290 platform_set_drvdata(pdev, card);
291 snd_soc_card_set_drvdata(card, machine);
292 ret = devm_snd_soc_register_card(&pdev->dev, &st_card);
294 return dev_err_probe(&pdev->dev, ret,
295 "devm_snd_soc_register_card(%s) failed\n",
302 static const struct acpi_device_id st_audio_acpi_match[] = {
306 MODULE_DEVICE_TABLE(acpi, st_audio_acpi_match);
309 static struct platform_driver st_mach_driver = {
312 .acpi_match_table = ACPI_PTR(st_audio_acpi_match),
313 .pm = &snd_soc_pm_ops,
315 .probe = st_es8336_probe,
318 module_platform_driver(st_mach_driver);
320 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
321 MODULE_DESCRIPTION("st-es8316 audio support");
322 MODULE_LICENSE("GPL v2");