1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Skylake I2S Machine Driver with MAXIM98357A
6 * Copyright (C) 2015, Intel Corporation. All rights reserved.
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <sound/core.h>
12 #include <sound/jack.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
16 #include <sound/soc-acpi.h>
17 #include "../../codecs/nau8825.h"
18 #include "../../codecs/hdac_hdmi.h"
20 #define SKL_NUVOTON_CODEC_DAI "nau8825-hifi"
21 #define SKL_MAXIM_CODEC_DAI "HiFi"
22 #define DMIC_CH(p) p->list[p->count-1]
24 static struct snd_soc_jack skylake_headset;
25 static struct snd_soc_card skylake_audio_card;
26 static const struct snd_pcm_hw_constraint_list *dmic_constraints;
27 static struct snd_soc_jack skylake_hdmi[3];
30 struct list_head head;
31 struct snd_soc_dai *codec_dai;
35 struct skl_nau8825_private {
36 struct list_head hdmi_pcm_list;
40 SKL_DPCM_AUDIO_PB = 0,
42 SKL_DPCM_AUDIO_REF_CP,
43 SKL_DPCM_AUDIO_DMIC_CP,
44 SKL_DPCM_AUDIO_HDMI1_PB,
45 SKL_DPCM_AUDIO_HDMI2_PB,
46 SKL_DPCM_AUDIO_HDMI3_PB,
49 static int platform_clock_control(struct snd_soc_dapm_widget *w,
50 struct snd_kcontrol *k, int event)
52 struct snd_soc_dapm_context *dapm = w->dapm;
53 struct snd_soc_card *card = dapm->card;
54 struct snd_soc_dai *codec_dai;
57 codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
59 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
63 if (SND_SOC_DAPM_EVENT_ON(event)) {
64 ret = snd_soc_dai_set_sysclk(codec_dai,
65 NAU8825_CLK_MCLK, 24000000, SND_SOC_CLOCK_IN);
67 dev_err(card->dev, "set sysclk err = %d\n", ret);
71 ret = snd_soc_dai_set_sysclk(codec_dai,
72 NAU8825_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
74 dev_err(card->dev, "set sysclk err = %d\n", ret);
82 static const struct snd_kcontrol_new skylake_controls[] = {
83 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
84 SOC_DAPM_PIN_SWITCH("Headset Mic"),
85 SOC_DAPM_PIN_SWITCH("Spk"),
88 static const struct snd_soc_dapm_widget skylake_widgets[] = {
89 SND_SOC_DAPM_HP("Headphone Jack", NULL),
90 SND_SOC_DAPM_MIC("Headset Mic", NULL),
91 SND_SOC_DAPM_SPK("Spk", NULL),
92 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
93 SND_SOC_DAPM_SPK("DP1", NULL),
94 SND_SOC_DAPM_SPK("DP2", NULL),
95 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
96 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
97 SND_SOC_DAPM_POST_PMD),
100 static const struct snd_soc_dapm_route skylake_map[] = {
101 /* HP jack connectors - unknown if we have jack detection */
102 { "Headphone Jack", NULL, "HPOL" },
103 { "Headphone Jack", NULL, "HPOR" },
106 { "Spk", NULL, "Speaker" },
109 { "MIC", NULL, "Headset Mic" },
110 { "DMic", NULL, "SoC DMIC" },
112 /* CODEC BE connections */
113 { "HiFi Playback", NULL, "ssp0 Tx" },
114 { "ssp0 Tx", NULL, "codec0_out" },
116 { "Playback", NULL, "ssp1 Tx" },
117 { "ssp1 Tx", NULL, "codec1_out" },
119 { "codec0_in", NULL, "ssp1 Rx" },
120 { "ssp1 Rx", NULL, "Capture" },
123 { "dmic01_hifi", NULL, "DMIC01 Rx" },
124 { "DMIC01 Rx", NULL, "DMIC AIF" },
126 { "hifi3", NULL, "iDisp3 Tx"},
127 { "iDisp3 Tx", NULL, "iDisp3_out"},
128 { "hifi2", NULL, "iDisp2 Tx"},
129 { "iDisp2 Tx", NULL, "iDisp2_out"},
130 { "hifi1", NULL, "iDisp1 Tx"},
131 { "iDisp1 Tx", NULL, "iDisp1_out"},
133 { "Headphone Jack", NULL, "Platform Clock" },
134 { "Headset Mic", NULL, "Platform Clock" },
137 static int skylake_ssp_fixup(struct snd_soc_pcm_runtime *rtd,
138 struct snd_pcm_hw_params *params)
140 struct snd_interval *rate = hw_param_interval(params,
141 SNDRV_PCM_HW_PARAM_RATE);
142 struct snd_interval *chan = hw_param_interval(params,
143 SNDRV_PCM_HW_PARAM_CHANNELS);
144 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
146 /* The ADSP will covert the FE rate to 48k, stereo */
147 rate->min = rate->max = 48000;
148 chan->min = chan->max = 2;
150 /* set SSP0 to 24 bit */
152 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
157 static int skylake_nau8825_codec_init(struct snd_soc_pcm_runtime *rtd)
160 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
163 * Headset buttons map to the google Reference headset.
164 * These can be configured by userspace.
166 ret = snd_soc_card_jack_new(&skylake_audio_card, "Headset Jack",
167 SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
168 SND_JACK_BTN_2 | SND_JACK_BTN_3, &skylake_headset);
170 dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
174 nau8825_enable_jack_detect(component, &skylake_headset);
176 snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
181 static int skylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
183 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
184 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
185 struct skl_hdmi_pcm *pcm;
187 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
191 pcm->device = SKL_DPCM_AUDIO_HDMI1_PB;
192 pcm->codec_dai = dai;
194 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
199 static int skylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
201 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
202 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
203 struct skl_hdmi_pcm *pcm;
205 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
209 pcm->device = SKL_DPCM_AUDIO_HDMI2_PB;
210 pcm->codec_dai = dai;
212 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
217 static int skylake_hdmi3_init(struct snd_soc_pcm_runtime *rtd)
219 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
220 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
221 struct skl_hdmi_pcm *pcm;
223 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
227 pcm->device = SKL_DPCM_AUDIO_HDMI3_PB;
228 pcm->codec_dai = dai;
230 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
235 static int skylake_nau8825_fe_init(struct snd_soc_pcm_runtime *rtd)
237 struct snd_soc_dapm_context *dapm;
238 struct snd_soc_component *component = asoc_rtd_to_cpu(rtd, 0)->component;
240 dapm = snd_soc_component_get_dapm(component);
241 snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
246 static const unsigned int rates[] = {
250 static const struct snd_pcm_hw_constraint_list constraints_rates = {
251 .count = ARRAY_SIZE(rates),
256 static const unsigned int channels[] = {
260 static const struct snd_pcm_hw_constraint_list constraints_channels = {
261 .count = ARRAY_SIZE(channels),
266 static int skl_fe_startup(struct snd_pcm_substream *substream)
268 struct snd_pcm_runtime *runtime = substream->runtime;
271 * On this platform for PCM device we support,
277 runtime->hw.channels_max = 2;
278 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
279 &constraints_channels);
281 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
282 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
284 snd_pcm_hw_constraint_list(runtime, 0,
285 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
290 static const struct snd_soc_ops skylake_nau8825_fe_ops = {
291 .startup = skl_fe_startup,
294 static int skylake_nau8825_hw_params(struct snd_pcm_substream *substream,
295 struct snd_pcm_hw_params *params)
297 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
298 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
301 ret = snd_soc_dai_set_sysclk(codec_dai,
302 NAU8825_CLK_MCLK, 24000000, SND_SOC_CLOCK_IN);
305 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
310 static const struct snd_soc_ops skylake_nau8825_ops = {
311 .hw_params = skylake_nau8825_hw_params,
314 static int skylake_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
315 struct snd_pcm_hw_params *params)
317 struct snd_interval *chan = hw_param_interval(params,
318 SNDRV_PCM_HW_PARAM_CHANNELS);
320 if (params_channels(params) == 2 || DMIC_CH(dmic_constraints) == 2)
321 chan->min = chan->max = 2;
323 chan->min = chan->max = 4;
328 static const unsigned int channels_dmic[] = {
332 static const struct snd_pcm_hw_constraint_list constraints_dmic_channels = {
333 .count = ARRAY_SIZE(channels_dmic),
334 .list = channels_dmic,
338 static const unsigned int dmic_2ch[] = {
342 static const struct snd_pcm_hw_constraint_list constraints_dmic_2ch = {
343 .count = ARRAY_SIZE(dmic_2ch),
348 static int skylake_dmic_startup(struct snd_pcm_substream *substream)
350 struct snd_pcm_runtime *runtime = substream->runtime;
352 runtime->hw.channels_max = DMIC_CH(dmic_constraints);
353 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
356 return snd_pcm_hw_constraint_list(substream->runtime, 0,
357 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
360 static const struct snd_soc_ops skylake_dmic_ops = {
361 .startup = skylake_dmic_startup,
364 static const unsigned int rates_16000[] = {
368 static const struct snd_pcm_hw_constraint_list constraints_16000 = {
369 .count = ARRAY_SIZE(rates_16000),
373 static const unsigned int ch_mono[] = {
377 static const struct snd_pcm_hw_constraint_list constraints_refcap = {
378 .count = ARRAY_SIZE(ch_mono),
382 static int skylake_refcap_startup(struct snd_pcm_substream *substream)
384 substream->runtime->hw.channels_max = 1;
385 snd_pcm_hw_constraint_list(substream->runtime, 0,
386 SNDRV_PCM_HW_PARAM_CHANNELS,
387 &constraints_refcap);
389 return snd_pcm_hw_constraint_list(substream->runtime, 0,
390 SNDRV_PCM_HW_PARAM_RATE,
394 static const struct snd_soc_ops skylake_refcap_ops = {
395 .startup = skylake_refcap_startup,
398 SND_SOC_DAILINK_DEF(dummy,
399 DAILINK_COMP_ARRAY(COMP_DUMMY()));
401 SND_SOC_DAILINK_DEF(system,
402 DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
404 SND_SOC_DAILINK_DEF(reference,
405 DAILINK_COMP_ARRAY(COMP_CPU("Reference Pin")));
407 SND_SOC_DAILINK_DEF(dmic,
408 DAILINK_COMP_ARRAY(COMP_CPU("DMIC Pin")));
410 SND_SOC_DAILINK_DEF(hdmi1,
411 DAILINK_COMP_ARRAY(COMP_CPU("HDMI1 Pin")));
413 SND_SOC_DAILINK_DEF(hdmi2,
414 DAILINK_COMP_ARRAY(COMP_CPU("HDMI2 Pin")));
416 SND_SOC_DAILINK_DEF(hdmi3,
417 DAILINK_COMP_ARRAY(COMP_CPU("HDMI3 Pin")));
419 SND_SOC_DAILINK_DEF(ssp0_pin,
420 DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
421 SND_SOC_DAILINK_DEF(ssp0_codec,
422 DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", SKL_MAXIM_CODEC_DAI)));
424 SND_SOC_DAILINK_DEF(ssp1_pin,
425 DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
426 SND_SOC_DAILINK_DEF(ssp1_codec,
427 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10508825:00",
428 SKL_NUVOTON_CODEC_DAI)));
430 SND_SOC_DAILINK_DEF(dmic_pin,
431 DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
432 SND_SOC_DAILINK_DEF(dmic_codec,
433 DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
435 SND_SOC_DAILINK_DEF(idisp1_pin,
436 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
437 SND_SOC_DAILINK_DEF(idisp1_codec,
438 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
440 SND_SOC_DAILINK_DEF(idisp2_pin,
441 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
442 SND_SOC_DAILINK_DEF(idisp2_codec,
443 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
445 SND_SOC_DAILINK_DEF(idisp3_pin,
446 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
447 SND_SOC_DAILINK_DEF(idisp3_codec,
448 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
450 SND_SOC_DAILINK_DEF(platform,
451 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
453 /* skylake digital audio interface glue - connects codec <--> CPU */
454 static struct snd_soc_dai_link skylake_dais[] = {
455 /* Front End DAI links */
456 [SKL_DPCM_AUDIO_PB] = {
457 .name = "Skl Audio Port",
458 .stream_name = "Audio",
461 .init = skylake_nau8825_fe_init,
463 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
465 .ops = &skylake_nau8825_fe_ops,
466 SND_SOC_DAILINK_REG(system, dummy, platform),
468 [SKL_DPCM_AUDIO_CP] = {
469 .name = "Skl Audio Capture Port",
470 .stream_name = "Audio Record",
474 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
476 .ops = &skylake_nau8825_fe_ops,
477 SND_SOC_DAILINK_REG(system, dummy, platform),
479 [SKL_DPCM_AUDIO_REF_CP] = {
480 .name = "Skl Audio Reference cap",
481 .stream_name = "Wake on Voice",
486 .ops = &skylake_refcap_ops,
487 SND_SOC_DAILINK_REG(reference, dummy, platform),
489 [SKL_DPCM_AUDIO_DMIC_CP] = {
490 .name = "Skl Audio DMIC cap",
491 .stream_name = "dmiccap",
496 .ops = &skylake_dmic_ops,
497 SND_SOC_DAILINK_REG(dmic, dummy, platform),
499 [SKL_DPCM_AUDIO_HDMI1_PB] = {
500 .name = "Skl HDMI Port1",
501 .stream_name = "Hdmi1",
505 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
508 SND_SOC_DAILINK_REG(hdmi1, dummy, platform),
510 [SKL_DPCM_AUDIO_HDMI2_PB] = {
511 .name = "Skl HDMI Port2",
512 .stream_name = "Hdmi2",
516 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
519 SND_SOC_DAILINK_REG(hdmi2, dummy, platform),
521 [SKL_DPCM_AUDIO_HDMI3_PB] = {
522 .name = "Skl HDMI Port3",
523 .stream_name = "Hdmi3",
525 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
530 SND_SOC_DAILINK_REG(hdmi3, dummy, platform),
533 /* Back End DAI links */
536 .name = "SSP0-Codec",
539 .dai_fmt = SND_SOC_DAIFMT_I2S |
540 SND_SOC_DAIFMT_NB_NF |
541 SND_SOC_DAIFMT_CBC_CFC,
542 .ignore_pmdown_time = 1,
543 .be_hw_params_fixup = skylake_ssp_fixup,
545 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
549 .name = "SSP1-Codec",
552 .init = skylake_nau8825_codec_init,
553 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
554 SND_SOC_DAIFMT_CBC_CFC,
555 .ignore_pmdown_time = 1,
556 .be_hw_params_fixup = skylake_ssp_fixup,
557 .ops = &skylake_nau8825_ops,
560 SND_SOC_DAILINK_REG(ssp1_pin, ssp1_codec, platform),
565 .be_hw_params_fixup = skylake_dmic_fixup,
569 SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
575 .init = skylake_hdmi1_init,
577 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
582 .init = skylake_hdmi2_init,
585 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
590 .init = skylake_hdmi3_init,
593 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
598 static int skylake_card_late_probe(struct snd_soc_card *card)
600 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(card);
601 struct skl_hdmi_pcm *pcm;
602 struct snd_soc_component *component = NULL;
604 char jack_name[NAME_SIZE];
606 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
607 component = pcm->codec_dai->component;
608 snprintf(jack_name, sizeof(jack_name),
609 "HDMI/DP, pcm=%d Jack", pcm->device);
610 err = snd_soc_card_jack_new(card, jack_name,
617 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
628 return hdac_hdmi_jack_port_init(component, &card->dapm);
631 /* skylake audio machine driver for SPT + NAU88L25 */
632 static struct snd_soc_card skylake_audio_card = {
633 .name = "sklnau8825max",
634 .owner = THIS_MODULE,
635 .dai_link = skylake_dais,
636 .num_links = ARRAY_SIZE(skylake_dais),
637 .controls = skylake_controls,
638 .num_controls = ARRAY_SIZE(skylake_controls),
639 .dapm_widgets = skylake_widgets,
640 .num_dapm_widgets = ARRAY_SIZE(skylake_widgets),
641 .dapm_routes = skylake_map,
642 .num_dapm_routes = ARRAY_SIZE(skylake_map),
643 .fully_routed = true,
644 .late_probe = skylake_card_late_probe,
647 static int skylake_audio_probe(struct platform_device *pdev)
649 struct skl_nau8825_private *ctx;
650 struct snd_soc_acpi_mach *mach;
652 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
656 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
658 skylake_audio_card.dev = &pdev->dev;
659 snd_soc_card_set_drvdata(&skylake_audio_card, ctx);
661 mach = pdev->dev.platform_data;
663 dmic_constraints = mach->mach_params.dmic_num == 2 ?
664 &constraints_dmic_2ch : &constraints_dmic_channels;
666 return devm_snd_soc_register_card(&pdev->dev, &skylake_audio_card);
669 static const struct platform_device_id skl_board_ids[] = {
670 { .name = "skl_n88l25_m98357a" },
671 { .name = "kbl_n88l25_m98357a" },
674 MODULE_DEVICE_TABLE(platform, skl_board_ids);
676 static struct platform_driver skylake_audio = {
677 .probe = skylake_audio_probe,
679 .name = "skl_n88l25_m98357a",
680 .pm = &snd_soc_pm_ops,
682 .id_table = skl_board_ids,
685 module_platform_driver(skylake_audio)
687 /* Module information */
688 MODULE_DESCRIPTION("Audio Machine driver-NAU88L25 & MAX98357A in I2S mode");
689 MODULE_AUTHOR("Rohit Ainapure <rohit.m.ainapure@intel.com");
690 MODULE_LICENSE("GPL v2");