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)
50 #define SOF_RT1015P_SPEAKER_AMP_PRESENT BIT(17)
52 static unsigned long sof_nau8825_quirk = SOF_NAU8825_SSP_CODEC(0);
55 struct list_head head;
56 struct snd_soc_dai *codec_dai;
60 struct sof_card_private {
62 struct snd_soc_jack sof_headset;
63 struct list_head hdmi_pcm_list;
66 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
68 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
69 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
70 struct sof_hdmi_pcm *pcm;
72 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
76 /* dai_link id is 1:1 mapped to the PCM device */
77 pcm->device = rtd->dai_link->id;
80 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
85 static struct snd_soc_jack_pin jack_pins[] = {
87 .pin = "Headphone Jack",
88 .mask = SND_JACK_HEADPHONE,
92 .mask = SND_JACK_MICROPHONE,
96 static int sof_nau8825_codec_init(struct snd_soc_pcm_runtime *rtd)
98 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
99 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
101 struct snd_soc_jack *jack;
105 * Headset buttons map to the google Reference headset.
106 * These can be configured by userspace.
108 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
109 SND_JACK_HEADSET | SND_JACK_BTN_0 |
110 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
114 ARRAY_SIZE(jack_pins));
116 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
120 jack = &ctx->sof_headset;
122 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
123 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
124 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
125 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
126 ret = snd_soc_component_set_jack(component, jack, NULL);
129 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
136 static void sof_nau8825_codec_exit(struct snd_soc_pcm_runtime *rtd)
138 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
140 snd_soc_component_set_jack(component, NULL, NULL);
143 static int sof_nau8825_hw_params(struct snd_pcm_substream *substream,
144 struct snd_pcm_hw_params *params)
146 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
147 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
150 clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
153 dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
157 /* Configure clock for codec */
158 ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_FLL_BLK, 0,
161 dev_err(codec_dai->dev, "can't set BCLK clock %d\n", ret);
165 /* Configure pll for codec */
166 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, clk_freq,
167 params_rate(params) * 256);
169 dev_err(codec_dai->dev, "can't set BCLK: %d\n", ret);
176 static struct snd_soc_ops sof_nau8825_ops = {
177 .hw_params = sof_nau8825_hw_params,
180 static struct snd_soc_dai_link_component platform_component[] = {
182 /* name might be overridden during probe */
183 .name = "0000:00:1f.3"
187 static int sof_card_late_probe(struct snd_soc_card *card)
189 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
190 struct snd_soc_dapm_context *dapm = &card->dapm;
191 struct sof_hdmi_pcm *pcm;
194 if (sof_nau8825_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
195 /* Disable Left and Right Spk pin after boot */
196 snd_soc_dapm_disable_pin(dapm, "Left Spk");
197 snd_soc_dapm_disable_pin(dapm, "Right Spk");
198 err = snd_soc_dapm_sync(dapm);
203 if (list_empty(&ctx->hdmi_pcm_list))
206 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);
208 return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
211 static const struct snd_kcontrol_new sof_controls[] = {
212 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
213 SOC_DAPM_PIN_SWITCH("Headset Mic"),
214 SOC_DAPM_PIN_SWITCH("Left Spk"),
215 SOC_DAPM_PIN_SWITCH("Right Spk"),
218 static const struct snd_kcontrol_new speaker_controls[] = {
219 SOC_DAPM_PIN_SWITCH("Spk"),
222 static const struct snd_soc_dapm_widget sof_widgets[] = {
223 SND_SOC_DAPM_HP("Headphone Jack", NULL),
224 SND_SOC_DAPM_MIC("Headset Mic", NULL),
225 SND_SOC_DAPM_SPK("Left Spk", NULL),
226 SND_SOC_DAPM_SPK("Right Spk", NULL),
229 static const struct snd_soc_dapm_widget speaker_widgets[] = {
230 SND_SOC_DAPM_SPK("Spk", NULL),
233 static const struct snd_soc_dapm_widget dmic_widgets[] = {
234 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
237 static const struct snd_soc_dapm_route sof_map[] = {
238 /* HP jack connectors - unknown if we have jack detection */
239 { "Headphone Jack", NULL, "HPOL" },
240 { "Headphone Jack", NULL, "HPOR" },
243 { "MIC", NULL, "Headset Mic" },
246 static const struct snd_soc_dapm_route speaker_map[] = {
248 { "Spk", NULL, "Speaker" },
251 static const struct snd_soc_dapm_route dmic_map[] = {
253 {"DMic", NULL, "SoC DMIC"},
256 static int speaker_codec_init(struct snd_soc_pcm_runtime *rtd)
258 struct snd_soc_card *card = rtd->card;
261 ret = snd_soc_dapm_new_controls(&card->dapm, speaker_widgets,
262 ARRAY_SIZE(speaker_widgets));
264 dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
265 /* Don't need to add routes if widget addition failed */
269 ret = snd_soc_add_card_controls(card, speaker_controls,
270 ARRAY_SIZE(speaker_controls));
272 dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
276 ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map,
277 ARRAY_SIZE(speaker_map));
280 dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
284 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
286 struct snd_soc_card *card = rtd->card;
289 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
290 ARRAY_SIZE(dmic_widgets));
292 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
293 /* Don't need to add routes if widget addition failed */
297 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
298 ARRAY_SIZE(dmic_map));
301 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
306 /* sof audio machine driver for nau8825 codec */
307 static struct snd_soc_card sof_audio_card_nau8825 = {
308 .name = "nau8825", /* the sof- prefix is added by the core */
309 .owner = THIS_MODULE,
310 .controls = sof_controls,
311 .num_controls = ARRAY_SIZE(sof_controls),
312 .dapm_widgets = sof_widgets,
313 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
314 .dapm_routes = sof_map,
315 .num_dapm_routes = ARRAY_SIZE(sof_map),
316 .fully_routed = true,
317 .late_probe = sof_card_late_probe,
320 static struct snd_soc_dai_link_component nau8825_component[] = {
322 .name = "i2c-10508825:00",
323 .dai_name = "nau8825-hifi",
327 static struct snd_soc_dai_link_component dmic_component[] = {
329 .name = "dmic-codec",
330 .dai_name = "dmic-hifi",
334 static struct snd_soc_dai_link_component rt1019p_component[] = {
336 .name = "RTL1019:00",
341 static struct snd_soc_dai_link_component dummy_component[] = {
343 .name = "snd-soc-dummy",
344 .dai_name = "snd-soc-dummy-dai",
348 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
354 struct snd_soc_dai_link_component *idisp_components;
355 struct snd_soc_dai_link_component *cpus;
356 struct snd_soc_dai_link *links;
359 links = devm_kcalloc(dev, sof_audio_card_nau8825.num_links,
360 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
361 cpus = devm_kcalloc(dev, sof_audio_card_nau8825.num_links,
362 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
367 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
368 "SSP%d-Codec", ssp_codec);
373 links[id].codecs = nau8825_component;
374 links[id].num_codecs = ARRAY_SIZE(nau8825_component);
375 links[id].platforms = platform_component;
376 links[id].num_platforms = ARRAY_SIZE(platform_component);
377 links[id].init = sof_nau8825_codec_init;
378 links[id].exit = sof_nau8825_codec_exit;
379 links[id].ops = &sof_nau8825_ops;
380 links[id].dpcm_playback = 1;
381 links[id].dpcm_capture = 1;
382 links[id].no_pcm = 1;
383 links[id].cpus = &cpus[id];
384 links[id].num_cpus = 1;
386 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
389 if (!links[id].cpus->dai_name)
395 if (dmic_be_num > 0) {
396 /* at least we have dmic01 */
397 links[id].name = "dmic01";
398 links[id].cpus = &cpus[id];
399 links[id].cpus->dai_name = "DMIC01 Pin";
400 links[id].init = dmic_init;
401 if (dmic_be_num > 1) {
402 /* set up 2 BE links at most */
403 links[id + 1].name = "dmic16k";
404 links[id + 1].cpus = &cpus[id + 1];
405 links[id + 1].cpus->dai_name = "DMIC16k Pin";
410 for (i = 0; i < dmic_be_num; i++) {
412 links[id].num_cpus = 1;
413 links[id].codecs = dmic_component;
414 links[id].num_codecs = ARRAY_SIZE(dmic_component);
415 links[id].platforms = platform_component;
416 links[id].num_platforms = ARRAY_SIZE(platform_component);
417 links[id].ignore_suspend = 1;
418 links[id].dpcm_capture = 1;
419 links[id].no_pcm = 1;
425 idisp_components = devm_kcalloc(dev,
427 sizeof(struct snd_soc_dai_link_component),
429 if (!idisp_components)
432 for (i = 1; i <= hdmi_num; i++) {
433 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
439 links[id].cpus = &cpus[id];
440 links[id].num_cpus = 1;
441 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
443 if (!links[id].cpus->dai_name)
446 idisp_components[i - 1].name = "ehdaudio0D2";
447 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
451 if (!idisp_components[i - 1].dai_name)
454 links[id].codecs = &idisp_components[i - 1];
455 links[id].num_codecs = 1;
456 links[id].platforms = platform_component;
457 links[id].num_platforms = ARRAY_SIZE(platform_component);
458 links[id].init = sof_hdmi_init;
459 links[id].dpcm_playback = 1;
460 links[id].no_pcm = 1;
465 if (sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT) {
466 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
467 "SSP%d-Codec", ssp_amp);
472 if (sof_nau8825_quirk & SOF_RT1019P_SPEAKER_AMP_PRESENT) {
473 links[id].codecs = rt1019p_component;
474 links[id].num_codecs = ARRAY_SIZE(rt1019p_component);
475 links[id].init = speaker_codec_init;
476 } else if (sof_nau8825_quirk &
477 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
478 links[id].codecs = max_98373_components;
479 links[id].num_codecs = ARRAY_SIZE(max_98373_components);
480 links[id].init = max_98373_spk_codec_init;
481 links[id].ops = &max_98373_ops;
482 /* feedback stream */
483 links[id].dpcm_capture = 1;
484 } else if (sof_nau8825_quirk &
485 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
486 max_98360a_dai_link(&links[id]);
487 } else if (sof_nau8825_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
488 sof_rt1015p_dai_link(&links[id]);
493 links[id].platforms = platform_component;
494 links[id].num_platforms = ARRAY_SIZE(platform_component);
495 links[id].dpcm_playback = 1;
496 links[id].no_pcm = 1;
497 links[id].cpus = &cpus[id];
498 links[id].num_cpus = 1;
499 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
502 if (!links[id].cpus->dai_name)
507 /* BT audio offload */
508 if (sof_nau8825_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
509 int port = (sof_nau8825_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
510 SOF_BT_OFFLOAD_SSP_SHIFT;
513 links[id].cpus = &cpus[id];
514 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
516 if (!links[id].cpus->dai_name)
518 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
521 links[id].codecs = dummy_component;
522 links[id].num_codecs = ARRAY_SIZE(dummy_component);
523 links[id].platforms = platform_component;
524 links[id].num_platforms = ARRAY_SIZE(platform_component);
525 links[id].dpcm_playback = 1;
526 links[id].dpcm_capture = 1;
527 links[id].no_pcm = 1;
528 links[id].num_cpus = 1;
536 static int sof_audio_probe(struct platform_device *pdev)
538 struct snd_soc_dai_link *dai_links;
539 struct snd_soc_acpi_mach *mach;
540 struct sof_card_private *ctx;
541 int dmic_be_num, hdmi_num;
542 int ret, ssp_amp, ssp_codec;
544 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
548 if (pdev->id_entry && pdev->id_entry->driver_data)
549 sof_nau8825_quirk = (unsigned long)pdev->id_entry->driver_data;
551 mach = pdev->dev.platform_data;
553 /* A speaker amp might not be present when the quirk claims one is.
554 * Detect this via whether the machine driver match includes quirk_data.
556 if ((sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
557 sof_nau8825_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
559 dev_dbg(&pdev->dev, "sof_nau8825_quirk = %lx\n", sof_nau8825_quirk);
561 /* default number of DMIC DAI's */
563 hdmi_num = (sof_nau8825_quirk & SOF_NAU8825_NUM_HDMIDEV_MASK) >>
564 SOF_NAU8825_NUM_HDMIDEV_SHIFT;
565 /* default number of HDMI DAI's */
569 ssp_amp = (sof_nau8825_quirk & SOF_NAU8825_SSP_AMP_MASK) >>
570 SOF_NAU8825_SSP_AMP_SHIFT;
572 ssp_codec = sof_nau8825_quirk & SOF_NAU8825_SSP_CODEC_MASK;
574 /* compute number of dai links */
575 sof_audio_card_nau8825.num_links = 1 + dmic_be_num + hdmi_num;
577 if (sof_nau8825_quirk & SOF_SPEAKER_AMP_PRESENT)
578 sof_audio_card_nau8825.num_links++;
580 if (sof_nau8825_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
581 max_98373_set_codec_conf(&sof_audio_card_nau8825);
582 else if (sof_nau8825_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
583 sof_rt1015p_codec_conf(&sof_audio_card_nau8825);
585 if (sof_nau8825_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
586 sof_audio_card_nau8825.num_links++;
588 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
589 dmic_be_num, hdmi_num);
593 sof_audio_card_nau8825.dai_link = dai_links;
595 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
597 sof_audio_card_nau8825.dev = &pdev->dev;
599 /* set platform name for each dailink */
600 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_nau8825,
601 mach->mach_params.platform);
605 snd_soc_card_set_drvdata(&sof_audio_card_nau8825, ctx);
607 return devm_snd_soc_register_card(&pdev->dev,
608 &sof_audio_card_nau8825);
611 static const struct platform_device_id board_ids[] = {
613 .name = "sof_nau8825",
614 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
615 SOF_NAU8825_NUM_HDMIDEV(4) |
616 SOF_BT_OFFLOAD_SSP(2) |
617 SOF_SSP_BT_OFFLOAD_PRESENT),
621 .name = "adl_rt1019p_8825",
622 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
623 SOF_SPEAKER_AMP_PRESENT |
624 SOF_RT1019P_SPEAKER_AMP_PRESENT |
625 SOF_NAU8825_SSP_AMP(2) |
626 SOF_NAU8825_NUM_HDMIDEV(4)),
629 .name = "adl_max98373_8825",
630 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
631 SOF_SPEAKER_AMP_PRESENT |
632 SOF_MAX98373_SPEAKER_AMP_PRESENT |
633 SOF_NAU8825_SSP_AMP(1) |
634 SOF_NAU8825_NUM_HDMIDEV(4) |
635 SOF_BT_OFFLOAD_SSP(2) |
636 SOF_SSP_BT_OFFLOAD_PRESENT),
639 /* The limitation of length of char array, shorten the name */
640 .name = "adl_mx98360a_8825",
641 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
642 SOF_SPEAKER_AMP_PRESENT |
643 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
644 SOF_NAU8825_SSP_AMP(1) |
645 SOF_NAU8825_NUM_HDMIDEV(4) |
646 SOF_BT_OFFLOAD_SSP(2) |
647 SOF_SSP_BT_OFFLOAD_PRESENT),
651 .name = "adl_rt1015p_8825",
652 .driver_data = (kernel_ulong_t)(SOF_NAU8825_SSP_CODEC(0) |
653 SOF_SPEAKER_AMP_PRESENT |
654 SOF_RT1015P_SPEAKER_AMP_PRESENT |
655 SOF_NAU8825_SSP_AMP(1) |
656 SOF_NAU8825_NUM_HDMIDEV(4) |
657 SOF_BT_OFFLOAD_SSP(2) |
658 SOF_SSP_BT_OFFLOAD_PRESENT),
662 MODULE_DEVICE_TABLE(platform, board_ids);
664 static struct platform_driver sof_audio = {
665 .probe = sof_audio_probe,
667 .name = "sof_nau8825",
668 .pm = &snd_soc_pm_ops,
670 .id_table = board_ids,
672 module_platform_driver(sof_audio)
674 /* Module information */
675 MODULE_DESCRIPTION("SOF Audio Machine driver for NAU8825");
676 MODULE_AUTHOR("David Lin <ctlin0@nuvoton.com>");
677 MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
678 MODULE_LICENSE("GPL");
679 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
680 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
681 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);