1 // SPDX-License-Identifier: GPL-2.0+
3 // Machine driver for AMD ACP Audio engine using DA7219 & MAX98357 codec.
5 //Copyright 2016 Advanced Micro Devices, Inc.
7 #include <sound/core.h>
10 #include <sound/pcm_params.h>
11 #include <sound/soc-dapm.h>
12 #include <sound/jack.h>
13 #include <linux/clk.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/input.h>
20 #include <linux/acpi.h>
22 #include "raven/acp3x.h"
23 #include "../codecs/rt5682.h"
24 #include "../codecs/rt1015.h"
26 #define PCO_PLAT_CLK 48000000
27 #define RT5682_PLL_FREQ (48000 * 512)
28 #define DUAL_CHANNEL 2
30 static struct snd_soc_jack pco_jack;
31 static struct clk *rt5682_dai_wclk;
32 static struct clk *rt5682_dai_bclk;
33 static struct gpio_desc *dmic_sel;
34 void *soc_is_rltk_max(struct device *dev);
42 static int acp3x_5682_init(struct snd_soc_pcm_runtime *rtd)
45 struct snd_soc_card *card = rtd->card;
46 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
47 struct snd_soc_component *component = codec_dai->component;
49 dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
51 /* set rt5682 dai fmt */
52 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
53 | SND_SOC_DAIFMT_NB_NF
54 | SND_SOC_DAIFMT_CBM_CFM);
56 dev_err(rtd->card->dev,
57 "Failed to set rt5682 dai fmt: %d\n", ret);
62 ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL2, RT5682_PLL2_S_MCLK,
63 PCO_PLAT_CLK, RT5682_PLL_FREQ);
65 dev_err(rtd->dev, "can't set rt5682 PLL: %d\n", ret);
69 /* Set codec sysclk */
70 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL2,
71 RT5682_PLL_FREQ, SND_SOC_CLOCK_IN);
74 "Failed to set rt5682 SYSCLK: %d\n", ret);
78 /* Set tdm/i2s1 master bclk ratio */
79 ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
82 "Failed to set rt5682 tdm bclk ratio: %d\n", ret);
86 rt5682_dai_wclk = clk_get(component->dev, "rt5682-dai-wclk");
87 rt5682_dai_bclk = clk_get(component->dev, "rt5682-dai-bclk");
89 ret = snd_soc_card_jack_new(card, "Headset Jack",
90 SND_JACK_HEADSET | SND_JACK_LINEOUT |
91 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
92 SND_JACK_BTN_2 | SND_JACK_BTN_3,
95 dev_err(card->dev, "HP jack creation failed %d\n", ret);
99 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
100 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
101 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
102 snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
104 ret = snd_soc_component_set_jack(component, &pco_jack, NULL);
106 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
113 static int rt5682_clk_enable(struct snd_pcm_substream *substream)
116 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
118 /* RT5682 will support only 48K output with 48M mclk */
119 clk_set_rate(rt5682_dai_wclk, 48000);
120 clk_set_rate(rt5682_dai_bclk, 48000 * 64);
121 ret = clk_prepare_enable(rt5682_dai_wclk);
123 dev_err(rtd->dev, "can't enable wclk %d\n", ret);
130 static void rt5682_clk_disable(void)
132 clk_disable_unprepare(rt5682_dai_wclk);
135 static const unsigned int channels[] = {
139 static const unsigned int rates[] = {
143 static const struct snd_pcm_hw_constraint_list constraints_rates = {
144 .count = ARRAY_SIZE(rates),
149 static const struct snd_pcm_hw_constraint_list constraints_channels = {
150 .count = ARRAY_SIZE(channels),
155 static int acp3x_5682_startup(struct snd_pcm_substream *substream)
157 struct snd_pcm_runtime *runtime = substream->runtime;
158 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
159 struct snd_soc_card *card = rtd->card;
160 struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
162 machine->play_i2s_instance = I2S_SP_INSTANCE;
163 machine->cap_i2s_instance = I2S_SP_INSTANCE;
165 runtime->hw.channels_max = DUAL_CHANNEL;
166 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
167 &constraints_channels);
168 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
170 return rt5682_clk_enable(substream);
173 static int acp3x_max_startup(struct snd_pcm_substream *substream)
175 struct snd_pcm_runtime *runtime = substream->runtime;
176 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
177 struct snd_soc_card *card = rtd->card;
178 struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
180 machine->play_i2s_instance = I2S_BT_INSTANCE;
182 runtime->hw.channels_max = DUAL_CHANNEL;
183 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
184 &constraints_channels);
185 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
187 return rt5682_clk_enable(substream);
190 static int acp3x_ec_dmic0_startup(struct snd_pcm_substream *substream)
192 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
193 struct snd_soc_card *card = rtd->card;
194 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
195 struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
197 machine->cap_i2s_instance = I2S_BT_INSTANCE;
198 snd_soc_dai_set_bclk_ratio(codec_dai, 64);
200 return rt5682_clk_enable(substream);
203 static int dmic_switch;
205 static int dmic_get(struct snd_kcontrol *kcontrol,
206 struct snd_ctl_elem_value *ucontrol)
208 ucontrol->value.integer.value[0] = dmic_switch;
212 static int dmic_set(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol)
216 dmic_switch = ucontrol->value.integer.value[0];
217 gpiod_set_value(dmic_sel, dmic_switch);
222 static void rt5682_shutdown(struct snd_pcm_substream *substream)
224 rt5682_clk_disable();
227 static const struct snd_soc_ops acp3x_5682_ops = {
228 .startup = acp3x_5682_startup,
229 .shutdown = rt5682_shutdown,
232 static const struct snd_soc_ops acp3x_max_play_ops = {
233 .startup = acp3x_max_startup,
234 .shutdown = rt5682_shutdown,
237 static const struct snd_soc_ops acp3x_ec_cap0_ops = {
238 .startup = acp3x_ec_dmic0_startup,
239 .shutdown = rt5682_shutdown,
242 SND_SOC_DAILINK_DEF(acp3x_i2s,
243 DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.0")));
244 SND_SOC_DAILINK_DEF(acp3x_bt,
245 DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.2")));
247 SND_SOC_DAILINK_DEF(rt5682,
248 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", "rt5682-aif1")));
249 SND_SOC_DAILINK_DEF(max,
250 DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", "HiFi")));
251 SND_SOC_DAILINK_DEF(rt1015,
252 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC1015:00", "rt1015-aif"),
253 COMP_CODEC("i2c-10EC1015:01", "rt1015-aif")));
254 SND_SOC_DAILINK_DEF(cros_ec,
255 DAILINK_COMP_ARRAY(COMP_CODEC("GOOG0013:00", "EC Codec I2S RX")));
257 SND_SOC_DAILINK_DEF(platform,
258 DAILINK_COMP_ARRAY(COMP_PLATFORM("acp3x_rv_i2s_dma.0")));
260 static struct snd_soc_codec_conf rt1015_conf[] = {
262 .dlc = COMP_CODEC_CONF("i2c-10EC1015:00"),
263 .name_prefix = "Left",
266 .dlc = COMP_CODEC_CONF("i2c-10EC1015:01"),
267 .name_prefix = "Right",
271 static struct snd_soc_dai_link acp3x_dai[] = {
273 .name = "acp3x-5682-play",
274 .stream_name = "Playback",
275 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
276 | SND_SOC_DAIFMT_CBM_CFM,
277 .init = acp3x_5682_init,
280 .ops = &acp3x_5682_ops,
281 SND_SOC_DAILINK_REG(acp3x_i2s, rt5682, platform),
284 .name = "acp3x-max98357-play",
285 .stream_name = "HiFi Playback",
286 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
287 | SND_SOC_DAIFMT_CBS_CFS,
289 .ops = &acp3x_max_play_ops,
291 .num_cpus = ARRAY_SIZE(acp3x_bt),
292 .platforms = platform,
293 .num_platforms = ARRAY_SIZE(platform),
296 .name = "acp3x-ec-dmic0-capture",
297 .stream_name = "Capture DMIC0",
298 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
299 | SND_SOC_DAIFMT_CBS_CFS,
301 .ops = &acp3x_ec_cap0_ops,
302 SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform),
306 static const char * const dmic_mux_text[] = {
311 static SOC_ENUM_SINGLE_DECL(
312 acp3x_dmic_enum, SND_SOC_NOPM, 0, dmic_mux_text);
314 static const struct snd_kcontrol_new acp3x_dmic_mux_control =
315 SOC_DAPM_ENUM_EXT("DMIC Select Mux", acp3x_dmic_enum,
318 static const struct snd_soc_dapm_widget acp3x_5682_widgets[] = {
319 SND_SOC_DAPM_HP("Headphone Jack", NULL),
320 SND_SOC_DAPM_SPK("Spk", NULL),
321 SND_SOC_DAPM_MIC("Headset Mic", NULL),
322 SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
323 &acp3x_dmic_mux_control),
326 static const struct snd_soc_dapm_route acp3x_5682_audio_route[] = {
327 {"Headphone Jack", NULL, "HPOL"},
328 {"Headphone Jack", NULL, "HPOR"},
329 {"IN1P", NULL, "Headset Mic"},
330 {"Spk", NULL, "Speaker"},
331 {"Dmic Mux", "Front Mic", "DMIC"},
332 {"Dmic Mux", "Rear Mic", "DMIC"},
335 static const struct snd_kcontrol_new acp3x_5682_mc_controls[] = {
336 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
337 SOC_DAPM_PIN_SWITCH("Spk"),
338 SOC_DAPM_PIN_SWITCH("Headset Mic"),
341 static struct snd_soc_card acp3x_5682 = {
342 .name = "acp3xalc5682m98357",
343 .owner = THIS_MODULE,
344 .dai_link = acp3x_dai,
345 .num_links = ARRAY_SIZE(acp3x_dai),
346 .dapm_widgets = acp3x_5682_widgets,
347 .num_dapm_widgets = ARRAY_SIZE(acp3x_5682_widgets),
348 .dapm_routes = acp3x_5682_audio_route,
349 .num_dapm_routes = ARRAY_SIZE(acp3x_5682_audio_route),
350 .controls = acp3x_5682_mc_controls,
351 .num_controls = ARRAY_SIZE(acp3x_5682_mc_controls),
354 static const struct snd_soc_dapm_widget acp3x_1015_widgets[] = {
355 SND_SOC_DAPM_HP("Headphone Jack", NULL),
356 SND_SOC_DAPM_MIC("Headset Mic", NULL),
357 SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
358 &acp3x_dmic_mux_control),
359 SND_SOC_DAPM_SPK("Left Spk", NULL),
360 SND_SOC_DAPM_SPK("Right Spk", NULL),
363 static const struct snd_soc_dapm_route acp3x_1015_route[] = {
364 {"Headphone Jack", NULL, "HPOL"},
365 {"Headphone Jack", NULL, "HPOR"},
366 {"IN1P", NULL, "Headset Mic"},
367 {"Dmic Mux", "Front Mic", "DMIC"},
368 {"Dmic Mux", "Rear Mic", "DMIC"},
369 {"Left Spk", NULL, "Left SPO"},
370 {"Right Spk", NULL, "Right SPO"},
373 static const struct snd_kcontrol_new acp3x_mc_1015_controls[] = {
374 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
375 SOC_DAPM_PIN_SWITCH("Headset Mic"),
376 SOC_DAPM_PIN_SWITCH("Left Spk"),
377 SOC_DAPM_PIN_SWITCH("Right Spk"),
380 static struct snd_soc_card acp3x_1015 = {
381 .name = "acp3xalc56821015",
382 .owner = THIS_MODULE,
383 .dai_link = acp3x_dai,
384 .num_links = ARRAY_SIZE(acp3x_dai),
385 .dapm_widgets = acp3x_1015_widgets,
386 .num_dapm_widgets = ARRAY_SIZE(acp3x_1015_widgets),
387 .dapm_routes = acp3x_1015_route,
388 .num_dapm_routes = ARRAY_SIZE(acp3x_1015_route),
389 .codec_conf = rt1015_conf,
390 .num_configs = ARRAY_SIZE(rt1015_conf),
391 .controls = acp3x_mc_1015_controls,
392 .num_controls = ARRAY_SIZE(acp3x_mc_1015_controls),
395 void *soc_is_rltk_max(struct device *dev)
397 const struct acpi_device_id *match;
399 match = acpi_match_device(dev->driver->acpi_match_table, dev);
402 return (void *)match->driver_data;
405 static void card_spk_dai_link_present(struct snd_soc_dai_link *links,
406 const char *card_name)
408 if (!strcmp(card_name, "acp3xalc56821015")) {
409 links[1].codecs = rt1015;
410 links[1].num_codecs = ARRAY_SIZE(rt1015);
412 links[1].codecs = max;
413 links[1].num_codecs = ARRAY_SIZE(max);
417 static int acp3x_probe(struct platform_device *pdev)
420 struct snd_soc_card *card;
421 struct acp3x_platform_info *machine;
422 struct device *dev = &pdev->dev;
424 card = (struct snd_soc_card *)soc_is_rltk_max(dev);
428 machine = devm_kzalloc(&pdev->dev, sizeof(*machine), GFP_KERNEL);
432 card_spk_dai_link_present(card->dai_link, card->name);
433 card->dev = &pdev->dev;
434 platform_set_drvdata(pdev, card);
435 snd_soc_card_set_drvdata(card, machine);
437 dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
438 if (IS_ERR(dmic_sel)) {
439 dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n",
441 return PTR_ERR(dmic_sel);
444 ret = devm_snd_soc_register_card(&pdev->dev, card);
447 "devm_snd_soc_register_card(%s) failed: %d\n",
454 static const struct acpi_device_id acp3x_audio_acpi_match[] = {
455 { "AMDI5682", (unsigned long)&acp3x_5682},
456 { "AMDI1015", (unsigned long)&acp3x_1015},
459 MODULE_DEVICE_TABLE(acpi, acp3x_audio_acpi_match);
461 static struct platform_driver acp3x_audio = {
463 .name = "acp3x-alc5682-max98357",
464 .acpi_match_table = ACPI_PTR(acp3x_audio_acpi_match),
465 .pm = &snd_soc_pm_ops,
467 .probe = acp3x_probe,
470 module_platform_driver(acp3x_audio);
472 MODULE_AUTHOR("akshu.agrawal@amd.com");
473 MODULE_AUTHOR("Vishnuvardhanrao.Ravulapati@amd.com");
474 MODULE_DESCRIPTION("ALC5682 ALC1015 & MAX98357 audio support");
475 MODULE_LICENSE("GPL v2");