1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2021 Intel Corporation.
3 // Copyright(c) 2021 Nuvoton Corporation.
6 * Intel SOF Machine Driver with Nuvoton headphone codec NAU8825
7 * and speaker codec RT1019P MAX98360a or MAX98373
10 #include <linux/input.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/dmi.h>
14 #include <sound/core.h>
15 #include <sound/jack.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/sof.h>
20 #include <sound/soc-acpi.h>
21 #include "../../codecs/nau8825.h"
22 #include "../common/soc-intel-quirks.h"
23 #include "hda_dsp_common.h"
24 #include "sof_realtek_common.h"
25 #include "sof_maxim_common.h"
29 #define SOF_NAU8825_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
30 #define SOF_NAU8825_SSP_CODEC_MASK (GENMASK(2, 0))
31 #define SOF_SPEAKER_AMP_PRESENT BIT(3)
32 #define SOF_NAU8825_SSP_AMP_SHIFT 4
33 #define SOF_NAU8825_SSP_AMP_MASK (GENMASK(6, 4))
34 #define SOF_NAU8825_SSP_AMP(quirk) \
35 (((quirk) << SOF_NAU8825_SSP_AMP_SHIFT) & SOF_NAU8825_SSP_AMP_MASK)
36 #define SOF_NAU8825_NUM_HDMIDEV_SHIFT 7
37 #define SOF_NAU8825_NUM_HDMIDEV_MASK (GENMASK(9, 7))
38 #define SOF_NAU8825_NUM_HDMIDEV(quirk) \
39 (((quirk) << SOF_NAU8825_NUM_HDMIDEV_SHIFT) & SOF_NAU8825_NUM_HDMIDEV_MASK)
41 /* BT audio offload: reserve 3 bits for future */
42 #define SOF_BT_OFFLOAD_SSP_SHIFT 10
43 #define SOF_BT_OFFLOAD_SSP_MASK (GENMASK(12, 10))
44 #define SOF_BT_OFFLOAD_SSP(quirk) \
45 (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
46 #define SOF_SSP_BT_OFFLOAD_PRESENT BIT(13)
47 #define SOF_RT1019P_SPEAKER_AMP_PRESENT BIT(14)
48 #define SOF_MAX98373_SPEAKER_AMP_PRESENT BIT(15)
49 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(16)
51 static unsigned long sof_nau8825_quirk = SOF_NAU8825_SSP_CODEC(0);
54 struct list_head head;
55 struct snd_soc_dai *codec_dai;
59 struct sof_card_private {
61 struct snd_soc_jack sof_headset;
62 struct list_head hdmi_pcm_list;
65 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
67 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
68 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
69 struct sof_hdmi_pcm *pcm;
71 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
75 /* dai_link id is 1:1 mapped to the PCM device */
76 pcm->device = rtd->dai_link->id;
79 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
84 static int sof_nau8825_codec_init(struct snd_soc_pcm_runtime *rtd)
86 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
87 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
89 struct snd_soc_jack *jack;
93 * Headset buttons map to the google Reference headset.
94 * These can be configured by userspace.
96 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
97 SND_JACK_HEADSET | SND_JACK_BTN_0 |
98 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
102 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
106 jack = &ctx->sof_headset;
108 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
109 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
110 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
111 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
112 ret = snd_soc_component_set_jack(component, jack, NULL);
115 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
122 static void sof_nau8825_codec_exit(struct snd_soc_pcm_runtime *rtd)
124 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
126 snd_soc_component_set_jack(component, NULL, NULL);
129 static int sof_nau8825_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_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
136 clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
139 dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
143 /* Configure clock for codec */
144 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_BLK, 0,
147 dev_err(codec_dai->dev, "can't set BCLK clock %d\n", ret);
151 /* Configure pll for codec */
152 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, clk_freq,
153 params_rate(params) * 256);
155 dev_err(codec_dai->dev, "can't set BCLK: %d\n", ret);
162 static struct snd_soc_ops sof_nau8825_ops = {
163 .hw_params = sof_nau8825_hw_params,
166 static struct snd_soc_dai_link_component platform_component[] = {
168 /* name might be overridden during probe */
169 .name = "0000:00:1f.3"
173 static int sof_card_late_probe(struct snd_soc_card *card)
175 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
176 struct snd_soc_dapm_context *dapm = &card->dapm;
177 struct sof_hdmi_pcm *pcm;
180 if (list_empty(&ctx->hdmi_pcm_list))
183 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);
185 if (sof_nau8825_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
186 /* Disable Left and Right Spk pin after boot */
187 snd_soc_dapm_disable_pin(dapm, "Left Spk");
188 snd_soc_dapm_disable_pin(dapm, "Right Spk");
189 err = snd_soc_dapm_sync(dapm);
194 return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
197 static const struct snd_kcontrol_new sof_controls[] = {
198 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
199 SOC_DAPM_PIN_SWITCH("Headset Mic"),
200 SOC_DAPM_PIN_SWITCH("Left Spk"),
201 SOC_DAPM_PIN_SWITCH("Right Spk"),
204 static const struct snd_kcontrol_new speaker_controls[] = {
205 SOC_DAPM_PIN_SWITCH("Spk"),
208 static const struct snd_soc_dapm_widget sof_widgets[] = {
209 SND_SOC_DAPM_HP("Headphone Jack", NULL),
210 SND_SOC_DAPM_MIC("Headset Mic", NULL),
211 SND_SOC_DAPM_SPK("Left Spk", NULL),
212 SND_SOC_DAPM_SPK("Right Spk", NULL),
215 static const struct snd_soc_dapm_widget speaker_widgets[] = {
216 SND_SOC_DAPM_SPK("Spk", NULL),
219 static const struct snd_soc_dapm_widget dmic_widgets[] = {
220 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
223 static const struct snd_soc_dapm_route sof_map[] = {
224 /* HP jack connectors - unknown if we have jack detection */
225 { "Headphone Jack", NULL, "HPOL" },
226 { "Headphone Jack", NULL, "HPOR" },
229 { "MIC", NULL, "Headset Mic" },
232 static const struct snd_soc_dapm_route speaker_map[] = {
234 { "Spk", NULL, "Speaker" },
237 static const struct snd_soc_dapm_route dmic_map[] = {
239 {"DMic", NULL, "SoC DMIC"},
242 static int speaker_codec_init(struct snd_soc_pcm_runtime *rtd)
244 struct snd_soc_card *card = rtd->card;
247 ret = snd_soc_dapm_new_controls(&card->dapm, speaker_widgets,
248 ARRAY_SIZE(speaker_widgets));
250 dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
251 /* Don't need to add routes if widget addition failed */
255 ret = snd_soc_add_card_controls(card, speaker_controls,
256 ARRAY_SIZE(speaker_controls));
258 dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
262 ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map,
263 ARRAY_SIZE(speaker_map));
266 dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
270 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
272 struct snd_soc_card *card = rtd->card;
275 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
276 ARRAY_SIZE(dmic_widgets));
278 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
279 /* Don't need to add routes if widget addition failed */
283 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
284 ARRAY_SIZE(dmic_map));
287 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
292 /* sof audio machine driver for nau8825 codec */
293 static struct snd_soc_card sof_audio_card_nau8825 = {
294 .name = "nau8825", /* the sof- prefix is added by the core */
295 .owner = THIS_MODULE,
296 .controls = sof_controls,
297 .num_controls = ARRAY_SIZE(sof_controls),
298 .dapm_widgets = sof_widgets,
299 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
300 .dapm_routes = sof_map,
301 .num_dapm_routes = ARRAY_SIZE(sof_map),
302 .fully_routed = true,
303 .late_probe = sof_card_late_probe,
306 static struct snd_soc_dai_link_component nau8825_component[] = {
308 .name = "i2c-10508825:00",
309 .dai_name = "nau8825-hifi",
313 static struct snd_soc_dai_link_component dmic_component[] = {
315 .name = "dmic-codec",
316 .dai_name = "dmic-hifi",
320 static struct snd_soc_dai_link_component rt1019p_component[] = {
322 .name = "RTL1019:00",
327 static struct snd_soc_dai_link_component dummy_component[] = {
329 .name = "snd-soc-dummy",
330 .dai_name = "snd-soc-dummy-dai",
334 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
340 struct snd_soc_dai_link_component *idisp_components;
341 struct snd_soc_dai_link_component *cpus;
342 struct snd_soc_dai_link *links;
345 links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
346 sof_audio_card_nau8825.num_links, GFP_KERNEL);
347 cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
348 sof_audio_card_nau8825.num_links, GFP_KERNEL);
353 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
354 "SSP%d-Codec", ssp_codec);
359 links[id].codecs = nau8825_component;
360 links[id].num_codecs = ARRAY_SIZE(nau8825_component);
361 links[id].platforms = platform_component;
362 links[id].num_platforms = ARRAY_SIZE(platform_component);
363 links[id].init = sof_nau8825_codec_init;
364 links[id].exit = sof_nau8825_codec_exit;
365 links[id].ops = &sof_nau8825_ops;
366 links[id].dpcm_playback = 1;
367 links[id].dpcm_capture = 1;
368 links[id].no_pcm = 1;
369 links[id].cpus = &cpus[id];
370 links[id].num_cpus = 1;
372 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
375 if (!links[id].cpus->dai_name)
381 if (dmic_be_num > 0) {
382 /* at least we have dmic01 */
383 links[id].name = "dmic01";
384 links[id].cpus = &cpus[id];
385 links[id].cpus->dai_name = "DMIC01 Pin";
386 links[id].init = dmic_init;
387 if (dmic_be_num > 1) {
388 /* set up 2 BE links at most */
389 links[id + 1].name = "dmic16k";
390 links[id + 1].cpus = &cpus[id + 1];
391 links[id + 1].cpus->dai_name = "DMIC16k Pin";
396 for (i = 0; i < dmic_be_num; i++) {
398 links[id].num_cpus = 1;
399 links[id].codecs = dmic_component;
400 links[id].num_codecs = ARRAY_SIZE(dmic_component);
401 links[id].platforms = platform_component;
402 links[id].num_platforms = ARRAY_SIZE(platform_component);
403 links[id].ignore_suspend = 1;
404 links[id].dpcm_capture = 1;
405 links[id].no_pcm = 1;
411 idisp_components = devm_kzalloc(dev,
412 sizeof(struct snd_soc_dai_link_component) *
413 hdmi_num, GFP_KERNEL);
414 if (!idisp_components)
417 for (i = 1; i <= hdmi_num; i++) {
418 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
424 links[id].cpus = &cpus[id];
425 links[id].num_cpus = 1;
426 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
428 if (!links[id].cpus->dai_name)
431 idisp_components[i - 1].name = "ehdaudio0D2";
432 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
436 if (!idisp_components[i - 1].dai_name)
439 links[id].codecs = &idisp_components[i - 1];
440 links[id].num_codecs = 1;
441 links[id].platforms = platform_component;
442 links[id].num_platforms = ARRAY_SIZE(platform_component);
443 links[id].init = sof_hdmi_init;
444 links[id].dpcm_playback = 1;
445 links[id].no_pcm = 1;
450 if (sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT) {
451 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
452 "SSP%d-Codec", ssp_amp);
457 if (sof_nau8825_quirk & SOF_RT1019P_SPEAKER_AMP_PRESENT) {
458 links[id].codecs = rt1019p_component;
459 links[id].num_codecs = ARRAY_SIZE(rt1019p_component);
460 links[id].init = speaker_codec_init;
461 } else if (sof_nau8825_quirk &
462 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
463 links[id].codecs = max_98373_components;
464 links[id].num_codecs = ARRAY_SIZE(max_98373_components);
465 links[id].init = max_98373_spk_codec_init;
466 links[id].ops = &max_98373_ops;
467 /* feedback stream */
468 links[id].dpcm_capture = 1;
469 } else if (sof_nau8825_quirk &
470 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
471 max_98360a_dai_link(&links[id]);
476 links[id].platforms = platform_component;
477 links[id].num_platforms = ARRAY_SIZE(platform_component);
478 links[id].dpcm_playback = 1;
479 links[id].no_pcm = 1;
480 links[id].cpus = &cpus[id];
481 links[id].num_cpus = 1;
482 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
485 if (!links[id].cpus->dai_name)
490 /* BT audio offload */
491 if (sof_nau8825_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
492 int port = (sof_nau8825_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
493 SOF_BT_OFFLOAD_SSP_SHIFT;
496 links[id].cpus = &cpus[id];
497 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
499 if (!links[id].cpus->dai_name)
501 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
504 links[id].codecs = dummy_component;
505 links[id].num_codecs = ARRAY_SIZE(dummy_component);
506 links[id].platforms = platform_component;
507 links[id].num_platforms = ARRAY_SIZE(platform_component);
508 links[id].dpcm_playback = 1;
509 links[id].dpcm_capture = 1;
510 links[id].no_pcm = 1;
511 links[id].num_cpus = 1;
519 static int sof_audio_probe(struct platform_device *pdev)
521 struct snd_soc_dai_link *dai_links;
522 struct snd_soc_acpi_mach *mach;
523 struct sof_card_private *ctx;
524 int dmic_be_num, hdmi_num;
525 int ret, ssp_amp, ssp_codec;
527 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
531 if (pdev->id_entry && pdev->id_entry->driver_data)
532 sof_nau8825_quirk = (unsigned long)pdev->id_entry->driver_data;
534 mach = pdev->dev.platform_data;
536 /* A speaker amp might not be present when the quirk claims one is.
537 * Detect this via whether the machine driver match includes quirk_data.
539 if ((sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
540 sof_nau8825_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
542 dev_dbg(&pdev->dev, "sof_nau8825_quirk = %lx\n", sof_nau8825_quirk);
544 /* default number of DMIC DAI's */
546 hdmi_num = (sof_nau8825_quirk & SOF_NAU8825_NUM_HDMIDEV_MASK) >>
547 SOF_NAU8825_NUM_HDMIDEV_SHIFT;
548 /* default number of HDMI DAI's */
552 ssp_amp = (sof_nau8825_quirk & SOF_NAU8825_SSP_AMP_MASK) >>
553 SOF_NAU8825_SSP_AMP_SHIFT;
555 ssp_codec = sof_nau8825_quirk & SOF_NAU8825_SSP_CODEC_MASK;
557 /* compute number of dai links */
558 sof_audio_card_nau8825.num_links = 1 + dmic_be_num + hdmi_num;
560 if (sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT)
561 sof_audio_card_nau8825.num_links++;
563 if (sof_nau8825_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
564 max_98373_set_codec_conf(&sof_audio_card_nau8825);
566 if (sof_nau8825_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
567 sof_audio_card_nau8825.num_links++;
569 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
570 dmic_be_num, hdmi_num);
574 sof_audio_card_nau8825.dai_link = dai_links;
576 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
578 sof_audio_card_nau8825.dev = &pdev->dev;
580 /* set platform name for each dailink */
581 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_nau8825,
582 mach->mach_params.platform);
586 snd_soc_card_set_drvdata(&sof_audio_card_nau8825, ctx);
588 return devm_snd_soc_register_card(&pdev->dev,
589 &sof_audio_card_nau8825);
592 static const struct platform_device_id board_ids[] = {
594 .name = "sof_nau8825",
595 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
596 SOF_NAU8825_NUM_HDMIDEV(4) |
597 SOF_BT_OFFLOAD_SSP(2) |
598 SOF_SSP_BT_OFFLOAD_PRESENT),
602 .name = "adl_rt1019p_nau8825",
603 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
604 SOF_SPEAKER_AMP_PRESENT |
605 SOF_RT1019P_SPEAKER_AMP_PRESENT |
606 SOF_NAU8825_SSP_AMP(2) |
607 SOF_NAU8825_NUM_HDMIDEV(4)),
610 .name = "adl_max98373_nau8825",
611 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
612 SOF_SPEAKER_AMP_PRESENT |
613 SOF_MAX98373_SPEAKER_AMP_PRESENT |
614 SOF_NAU8825_SSP_AMP(1) |
615 SOF_NAU8825_NUM_HDMIDEV(4) |
616 SOF_BT_OFFLOAD_SSP(2) |
617 SOF_SSP_BT_OFFLOAD_PRESENT),
620 /* The limitation of length of char array, shorten the name */
621 .name = "adl_mx98360a_nau8825",
622 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
623 SOF_SPEAKER_AMP_PRESENT |
624 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
625 SOF_NAU8825_SSP_AMP(1) |
626 SOF_NAU8825_NUM_HDMIDEV(4) |
627 SOF_BT_OFFLOAD_SSP(2) |
628 SOF_SSP_BT_OFFLOAD_PRESENT),
633 MODULE_DEVICE_TABLE(platform, board_ids);
635 static struct platform_driver sof_audio = {
636 .probe = sof_audio_probe,
638 .name = "sof_nau8825",
639 .pm = &snd_soc_pm_ops,
641 .id_table = board_ids,
643 module_platform_driver(sof_audio)
645 /* Module information */
646 MODULE_DESCRIPTION("SOF Audio Machine driver for NAU8825");
647 MODULE_AUTHOR("David Lin <ctlin0@nuvoton.com>");
648 MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
649 MODULE_LICENSE("GPL");
650 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
651 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);