1 // SPDX-License-Identifier: GPL-2.0+
3 * Machine driver for AMD Vangogh platform using NAU8821 & CS35L41
6 * Copyright 2021 Advanced Micro Devices, Inc.
10 #include <sound/soc-dapm.h>
11 #include <linux/module.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
16 #include <sound/jack.h>
17 #include <linux/clk.h>
18 #include <linux/gpio.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/input.h>
22 #include <linux/acpi.h>
23 #include <linux/dmi.h>
25 #include "../../codecs/nau8821.h"
26 #include "../../codecs/cs35l41.h"
30 #define DRV_NAME "acp5x_mach"
31 #define DUAL_CHANNEL 2
32 #define ACP5X_NUVOTON_CODEC_DAI "nau8821-hifi"
34 #define ACP5X_NUVOTON_BCLK 3072000
35 #define ACP5X_NAU8821_FREQ_OUT 12288000
37 static unsigned long acp5x_machine_id;
38 static struct snd_soc_jack vg_headset;
40 static struct snd_soc_jack_pin acp5x_nau8821_jack_pins[] = {
43 .mask = SND_JACK_HEADPHONE,
47 .mask = SND_JACK_MICROPHONE,
51 static int acp5x_8821_init(struct snd_soc_pcm_runtime *rtd)
54 struct snd_soc_card *card = rtd->card;
55 struct snd_soc_component *component =
56 asoc_rtd_to_codec(rtd, 0)->component;
59 * Headset buttons map to the google Reference headset.
60 * These can be configured by userspace.
62 ret = snd_soc_card_jack_new_pins(card, "Headset Jack",
63 SND_JACK_HEADSET | SND_JACK_BTN_0,
64 &vg_headset, acp5x_nau8821_jack_pins,
65 ARRAY_SIZE(acp5x_nau8821_jack_pins));
67 dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
71 snd_jack_set_key(vg_headset.jack, SND_JACK_BTN_0, KEY_MEDIA);
72 nau8821_enable_jack_detect(component, &vg_headset);
76 static int acp5x_cs35l41_init(struct snd_soc_pcm_runtime *rtd)
81 static const unsigned int rates[] = {
85 static const struct snd_pcm_hw_constraint_list constraints_rates = {
86 .count = ARRAY_SIZE(rates),
91 static const unsigned int channels[] = {
95 static const struct snd_pcm_hw_constraint_list constraints_channels = {
96 .count = ARRAY_SIZE(channels),
101 static const unsigned int acp5x_nau8821_format[] = {32};
103 static struct snd_pcm_hw_constraint_list constraints_sample_bits = {
104 .list = acp5x_nau8821_format,
105 .count = ARRAY_SIZE(acp5x_nau8821_format),
108 static int acp5x_8821_startup(struct snd_pcm_substream *substream)
110 struct snd_pcm_runtime *runtime = substream->runtime;
111 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
112 struct snd_soc_card *card = rtd->card;
113 struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(card);
115 machine->play_i2s_instance = I2S_SP_INSTANCE;
116 machine->cap_i2s_instance = I2S_SP_INSTANCE;
118 runtime->hw.channels_max = DUAL_CHANNEL;
119 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
120 &constraints_channels);
121 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
123 snd_pcm_hw_constraint_list(substream->runtime, 0,
124 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
125 &constraints_sample_bits);
129 static int acp5x_nau8821_hw_params(struct snd_pcm_substream *substream,
130 struct snd_pcm_hw_params *params)
132 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
133 struct snd_soc_card *card = rtd->card;
134 struct snd_soc_dai *codec_dai =
135 snd_soc_card_get_codec_dai(card,
136 ACP5X_NUVOTON_CODEC_DAI);
139 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8821_CLK_FLL_BLK, 0,
142 dev_err(card->dev, "can't set FS clock %d\n", ret);
143 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, snd_soc_params_to_bclk(params),
144 params_rate(params) * 256);
146 dev_err(card->dev, "can't set FLL: %d\n", ret);
151 static int acp5x_cs35l41_startup(struct snd_pcm_substream *substream)
153 struct snd_pcm_runtime *runtime = substream->runtime;
154 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
155 struct snd_soc_card *card = rtd->card;
156 struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(card);
158 machine->play_i2s_instance = I2S_HS_INSTANCE;
160 runtime->hw.channels_max = DUAL_CHANNEL;
161 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
162 &constraints_channels);
163 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
168 static int acp5x_cs35l41_hw_params(struct snd_pcm_substream *substream,
169 struct snd_pcm_hw_params *params)
171 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
172 struct snd_soc_card *card = rtd->card;
173 struct snd_soc_dai *codec_dai;
175 unsigned int num_codecs = rtd->num_codecs;
176 unsigned int bclk_val;
179 for (i = 0; i < num_codecs; i++) {
180 codec_dai = asoc_rtd_to_codec(rtd, i);
181 if (strcmp(codec_dai->name, "cs35l41-pcm") == 0) {
182 switch (params_rate(params)) {
187 dev_err(card->dev, "Invalid Samplerate:0x%x\n",
188 params_rate(params));
191 ret = snd_soc_component_set_sysclk(codec_dai->component,
192 0, 0, bclk_val, SND_SOC_CLOCK_IN);
194 dev_err(card->dev, "failed to set sysclk for CS35l41 dai\n");
203 static const struct snd_soc_ops acp5x_8821_ops = {
204 .startup = acp5x_8821_startup,
205 .hw_params = acp5x_nau8821_hw_params,
208 static const struct snd_soc_ops acp5x_cs35l41_play_ops = {
209 .startup = acp5x_cs35l41_startup,
210 .hw_params = acp5x_cs35l41_hw_params,
213 static struct snd_soc_codec_conf cs35l41_conf[] = {
215 .dlc = COMP_CODEC_CONF("spi-VLV1776:00"),
216 .name_prefix = "Left",
219 .dlc = COMP_CODEC_CONF("spi-VLV1776:01"),
220 .name_prefix = "Right",
224 SND_SOC_DAILINK_DEF(acp5x_i2s,
225 DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.0")));
227 SND_SOC_DAILINK_DEF(acp5x_bt,
228 DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.1")));
230 SND_SOC_DAILINK_DEF(nau8821,
231 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-NVTN2020:00",
234 SND_SOC_DAILINK_DEF(cs35l41,
235 DAILINK_COMP_ARRAY(COMP_CODEC("spi-VLV1776:00", "cs35l41-pcm"),
236 COMP_CODEC("spi-VLV1776:01", "cs35l41-pcm")));
238 SND_SOC_DAILINK_DEF(platform,
239 DAILINK_COMP_ARRAY(COMP_PLATFORM("acp5x_i2s_dma.0")));
241 static struct snd_soc_dai_link acp5x_dai[] = {
243 .name = "acp5x-8821-play",
244 .stream_name = "Playback/Capture",
245 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
246 SND_SOC_DAIFMT_CBC_CFC,
249 .ops = &acp5x_8821_ops,
250 .init = acp5x_8821_init,
251 SND_SOC_DAILINK_REG(acp5x_i2s, nau8821, platform),
254 .name = "acp5x-CS35L41-Stereo",
255 .stream_name = "CS35L41 Stereo Playback",
256 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
257 SND_SOC_DAIFMT_CBC_CFC,
260 .ops = &acp5x_cs35l41_play_ops,
261 .init = acp5x_cs35l41_init,
262 SND_SOC_DAILINK_REG(acp5x_bt, cs35l41, platform),
266 static int platform_clock_control(struct snd_soc_dapm_widget *w,
267 struct snd_kcontrol *k, int event)
269 struct snd_soc_dapm_context *dapm = w->dapm;
270 struct snd_soc_card *card = dapm->card;
271 struct snd_soc_dai *codec_dai;
274 codec_dai = snd_soc_card_get_codec_dai(card, ACP5X_NUVOTON_CODEC_DAI);
276 dev_err(card->dev, "Codec dai not found\n");
280 if (SND_SOC_DAPM_EVENT_OFF(event)) {
281 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8821_CLK_INTERNAL,
282 0, SND_SOC_CLOCK_IN);
284 dev_err(card->dev, "set sysclk err = %d\n", ret);
288 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8821_CLK_FLL_BLK, 0,
291 dev_err(codec_dai->dev, "can't set BLK clock %d\n", ret);
292 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, ACP5X_NUVOTON_BCLK,
293 ACP5X_NAU8821_FREQ_OUT);
295 dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
300 static const struct snd_kcontrol_new acp5x_8821_controls[] = {
301 SOC_DAPM_PIN_SWITCH("Headphone"),
302 SOC_DAPM_PIN_SWITCH("Headset Mic"),
303 SOC_DAPM_PIN_SWITCH("Int Mic"),
306 static const struct snd_soc_dapm_widget acp5x_8821_widgets[] = {
307 SND_SOC_DAPM_HP("Headphone", NULL),
308 SND_SOC_DAPM_MIC("Headset Mic", NULL),
309 SND_SOC_DAPM_MIC("Int Mic", NULL),
310 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
311 platform_clock_control, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
314 static const struct snd_soc_dapm_route acp5x_8821_audio_route[] = {
315 /* HP jack connectors - unknown if we have jack detection */
316 { "Headphone", NULL, "HPOL" },
317 { "Headphone", NULL, "HPOR" },
318 { "MICL", NULL, "Headset Mic" },
319 { "MICR", NULL, "Headset Mic" },
320 { "DMIC", NULL, "Int Mic" },
322 { "Headphone", NULL, "Platform Clock" },
323 { "Headset Mic", NULL, "Platform Clock" },
324 { "Int Mic", NULL, "Platform Clock" },
327 static struct snd_soc_card acp5x_card = {
329 .owner = THIS_MODULE,
330 .dai_link = acp5x_dai,
331 .num_links = ARRAY_SIZE(acp5x_dai),
332 .dapm_widgets = acp5x_8821_widgets,
333 .num_dapm_widgets = ARRAY_SIZE(acp5x_8821_widgets),
334 .dapm_routes = acp5x_8821_audio_route,
335 .num_dapm_routes = ARRAY_SIZE(acp5x_8821_audio_route),
336 .codec_conf = cs35l41_conf,
337 .num_configs = ARRAY_SIZE(cs35l41_conf),
338 .controls = acp5x_8821_controls,
339 .num_controls = ARRAY_SIZE(acp5x_8821_controls),
342 static int acp5x_vg_quirk_cb(const struct dmi_system_id *id)
344 acp5x_machine_id = VG_JUPITER;
348 static const struct dmi_system_id acp5x_vg_quirk_table[] = {
350 .callback = acp5x_vg_quirk_cb,
352 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Valve"),
353 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jupiter"),
359 static int acp5x_probe(struct platform_device *pdev)
362 struct acp5x_platform_info *machine;
363 struct snd_soc_card *card;
365 machine = devm_kzalloc(&pdev->dev, sizeof(struct acp5x_platform_info),
370 dmi_check_system(acp5x_vg_quirk_table);
371 switch (acp5x_machine_id) {
374 acp5x_card.dev = &pdev->dev;
379 platform_set_drvdata(pdev, card);
380 snd_soc_card_set_drvdata(card, machine);
382 ret = devm_snd_soc_register_card(&pdev->dev, card);
384 return dev_err_probe(&pdev->dev, ret,
385 "snd_soc_register_card(%s) failed\n",
391 static struct platform_driver acp5x_mach_driver = {
393 .name = "acp5x_mach",
394 .pm = &snd_soc_pm_ops,
396 .probe = acp5x_probe,
399 module_platform_driver(acp5x_mach_driver);
401 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
402 MODULE_DESCRIPTION("NAU8821 & CS35L41 audio support");
403 MODULE_LICENSE("GPL v2");
404 MODULE_ALIAS("platform:" DRV_NAME);