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 *channels = 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 channels->min = channels->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 = rtd->codec_dai->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,
171 dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
175 nau8825_enable_jack_detect(component, &skylake_headset);
177 snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
182 static int skylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
184 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
185 struct snd_soc_dai *dai = rtd->codec_dai;
186 struct skl_hdmi_pcm *pcm;
188 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
192 pcm->device = SKL_DPCM_AUDIO_HDMI1_PB;
193 pcm->codec_dai = dai;
195 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
200 static int skylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
202 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
203 struct snd_soc_dai *dai = rtd->codec_dai;
204 struct skl_hdmi_pcm *pcm;
206 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
210 pcm->device = SKL_DPCM_AUDIO_HDMI2_PB;
211 pcm->codec_dai = dai;
213 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
218 static int skylake_hdmi3_init(struct snd_soc_pcm_runtime *rtd)
220 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
221 struct snd_soc_dai *dai = rtd->codec_dai;
222 struct skl_hdmi_pcm *pcm;
224 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
228 pcm->device = SKL_DPCM_AUDIO_HDMI3_PB;
229 pcm->codec_dai = dai;
231 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
236 static int skylake_nau8825_fe_init(struct snd_soc_pcm_runtime *rtd)
238 struct snd_soc_dapm_context *dapm;
239 struct snd_soc_component *component = rtd->cpu_dai->component;
241 dapm = snd_soc_component_get_dapm(component);
242 snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
247 static const unsigned int rates[] = {
251 static const struct snd_pcm_hw_constraint_list constraints_rates = {
252 .count = ARRAY_SIZE(rates),
257 static const unsigned int channels[] = {
261 static const struct snd_pcm_hw_constraint_list constraints_channels = {
262 .count = ARRAY_SIZE(channels),
267 static int skl_fe_startup(struct snd_pcm_substream *substream)
269 struct snd_pcm_runtime *runtime = substream->runtime;
272 * On this platform for PCM device we support,
278 runtime->hw.channels_max = 2;
279 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
280 &constraints_channels);
282 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
283 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
285 snd_pcm_hw_constraint_list(runtime, 0,
286 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
291 static const struct snd_soc_ops skylake_nau8825_fe_ops = {
292 .startup = skl_fe_startup,
295 static int skylake_nau8825_hw_params(struct snd_pcm_substream *substream,
296 struct snd_pcm_hw_params *params)
298 struct snd_soc_pcm_runtime *rtd = substream->private_data;
299 struct snd_soc_dai *codec_dai = rtd->codec_dai;
302 ret = snd_soc_dai_set_sysclk(codec_dai,
303 NAU8825_CLK_MCLK, 24000000, SND_SOC_CLOCK_IN);
306 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
311 static const struct snd_soc_ops skylake_nau8825_ops = {
312 .hw_params = skylake_nau8825_hw_params,
315 static int skylake_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
316 struct snd_pcm_hw_params *params)
318 struct snd_interval *channels = hw_param_interval(params,
319 SNDRV_PCM_HW_PARAM_CHANNELS);
321 if (params_channels(params) == 2 || DMIC_CH(dmic_constraints) == 2)
322 channels->min = channels->max = 2;
324 channels->min = channels->max = 4;
329 static const unsigned int channels_dmic[] = {
333 static const struct snd_pcm_hw_constraint_list constraints_dmic_channels = {
334 .count = ARRAY_SIZE(channels_dmic),
335 .list = channels_dmic,
339 static const unsigned int dmic_2ch[] = {
343 static const struct snd_pcm_hw_constraint_list constraints_dmic_2ch = {
344 .count = ARRAY_SIZE(dmic_2ch),
349 static int skylake_dmic_startup(struct snd_pcm_substream *substream)
351 struct snd_pcm_runtime *runtime = substream->runtime;
353 runtime->hw.channels_max = DMIC_CH(dmic_constraints);
354 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
357 return snd_pcm_hw_constraint_list(substream->runtime, 0,
358 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
361 static const struct snd_soc_ops skylake_dmic_ops = {
362 .startup = skylake_dmic_startup,
365 static const unsigned int rates_16000[] = {
369 static const struct snd_pcm_hw_constraint_list constraints_16000 = {
370 .count = ARRAY_SIZE(rates_16000),
374 static const unsigned int ch_mono[] = {
378 static const struct snd_pcm_hw_constraint_list constraints_refcap = {
379 .count = ARRAY_SIZE(ch_mono),
383 static int skylake_refcap_startup(struct snd_pcm_substream *substream)
385 substream->runtime->hw.channels_max = 1;
386 snd_pcm_hw_constraint_list(substream->runtime, 0,
387 SNDRV_PCM_HW_PARAM_CHANNELS,
388 &constraints_refcap);
390 return snd_pcm_hw_constraint_list(substream->runtime, 0,
391 SNDRV_PCM_HW_PARAM_RATE,
395 static const struct snd_soc_ops skylake_refcap_ops = {
396 .startup = skylake_refcap_startup,
399 /* skylake digital audio interface glue - connects codec <--> CPU */
400 static struct snd_soc_dai_link skylake_dais[] = {
401 /* Front End DAI links */
402 [SKL_DPCM_AUDIO_PB] = {
403 .name = "Skl Audio Port",
404 .stream_name = "Audio",
405 .cpu_dai_name = "System Pin",
406 .platform_name = "0000:00:1f.3",
408 .codec_name = "snd-soc-dummy",
409 .codec_dai_name = "snd-soc-dummy-dai",
411 .init = skylake_nau8825_fe_init,
413 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
415 .ops = &skylake_nau8825_fe_ops,
417 [SKL_DPCM_AUDIO_CP] = {
418 .name = "Skl Audio Capture Port",
419 .stream_name = "Audio Record",
420 .cpu_dai_name = "System Pin",
421 .platform_name = "0000:00:1f.3",
423 .codec_name = "snd-soc-dummy",
424 .codec_dai_name = "snd-soc-dummy-dai",
427 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
429 .ops = &skylake_nau8825_fe_ops,
431 [SKL_DPCM_AUDIO_REF_CP] = {
432 .name = "Skl Audio Reference cap",
433 .stream_name = "Wake on Voice",
434 .cpu_dai_name = "Reference Pin",
435 .codec_name = "snd-soc-dummy",
436 .codec_dai_name = "snd-soc-dummy-dai",
437 .platform_name = "0000:00:1f.3",
442 .ops = &skylake_refcap_ops,
444 [SKL_DPCM_AUDIO_DMIC_CP] = {
445 .name = "Skl Audio DMIC cap",
446 .stream_name = "dmiccap",
447 .cpu_dai_name = "DMIC Pin",
448 .codec_name = "snd-soc-dummy",
449 .codec_dai_name = "snd-soc-dummy-dai",
450 .platform_name = "0000:00:1f.3",
455 .ops = &skylake_dmic_ops,
457 [SKL_DPCM_AUDIO_HDMI1_PB] = {
458 .name = "Skl HDMI Port1",
459 .stream_name = "Hdmi1",
460 .cpu_dai_name = "HDMI1 Pin",
461 .codec_name = "snd-soc-dummy",
462 .codec_dai_name = "snd-soc-dummy-dai",
463 .platform_name = "0000:00:1f.3",
467 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
471 [SKL_DPCM_AUDIO_HDMI2_PB] = {
472 .name = "Skl HDMI Port2",
473 .stream_name = "Hdmi2",
474 .cpu_dai_name = "HDMI2 Pin",
475 .codec_name = "snd-soc-dummy",
476 .codec_dai_name = "snd-soc-dummy-dai",
477 .platform_name = "0000:00:1f.3",
481 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
485 [SKL_DPCM_AUDIO_HDMI3_PB] = {
486 .name = "Skl HDMI Port3",
487 .stream_name = "Hdmi3",
488 .cpu_dai_name = "HDMI3 Pin",
489 .codec_name = "snd-soc-dummy",
490 .codec_dai_name = "snd-soc-dummy-dai",
491 .platform_name = "0000:00:1f.3",
493 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
500 /* Back End DAI links */
503 .name = "SSP0-Codec",
505 .cpu_dai_name = "SSP0 Pin",
506 .platform_name = "0000:00:1f.3",
508 .codec_name = "MX98357A:00",
509 .codec_dai_name = SKL_MAXIM_CODEC_DAI,
510 .dai_fmt = SND_SOC_DAIFMT_I2S |
511 SND_SOC_DAIFMT_NB_NF |
512 SND_SOC_DAIFMT_CBS_CFS,
513 .ignore_pmdown_time = 1,
514 .be_hw_params_fixup = skylake_ssp_fixup,
519 .name = "SSP1-Codec",
521 .cpu_dai_name = "SSP1 Pin",
522 .platform_name = "0000:00:1f.3",
524 .codec_name = "i2c-10508825:00",
525 .codec_dai_name = SKL_NUVOTON_CODEC_DAI,
526 .init = skylake_nau8825_codec_init,
527 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
528 SND_SOC_DAIFMT_CBS_CFS,
529 .ignore_pmdown_time = 1,
530 .be_hw_params_fixup = skylake_ssp_fixup,
531 .ops = &skylake_nau8825_ops,
538 .cpu_dai_name = "DMIC01 Pin",
539 .codec_name = "dmic-codec",
540 .codec_dai_name = "dmic-hifi",
541 .platform_name = "0000:00:1f.3",
542 .be_hw_params_fixup = skylake_dmic_fixup,
550 .cpu_dai_name = "iDisp1 Pin",
551 .codec_name = "ehdaudio0D2",
552 .codec_dai_name = "intel-hdmi-hifi1",
553 .platform_name = "0000:00:1f.3",
555 .init = skylake_hdmi1_init,
561 .cpu_dai_name = "iDisp2 Pin",
562 .codec_name = "ehdaudio0D2",
563 .codec_dai_name = "intel-hdmi-hifi2",
564 .platform_name = "0000:00:1f.3",
565 .init = skylake_hdmi2_init,
572 .cpu_dai_name = "iDisp3 Pin",
573 .codec_name = "ehdaudio0D2",
574 .codec_dai_name = "intel-hdmi-hifi3",
575 .platform_name = "0000:00:1f.3",
576 .init = skylake_hdmi3_init,
583 static int skylake_card_late_probe(struct snd_soc_card *card)
585 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(card);
586 struct skl_hdmi_pcm *pcm;
587 struct snd_soc_component *component = NULL;
589 char jack_name[NAME_SIZE];
591 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
592 component = pcm->codec_dai->component;
593 snprintf(jack_name, sizeof(jack_name),
594 "HDMI/DP, pcm=%d Jack", pcm->device);
595 err = snd_soc_card_jack_new(card, jack_name,
603 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
614 return hdac_hdmi_jack_port_init(component, &card->dapm);
617 /* skylake audio machine driver for SPT + NAU88L25 */
618 static struct snd_soc_card skylake_audio_card = {
619 .name = "sklnau8825max",
620 .owner = THIS_MODULE,
621 .dai_link = skylake_dais,
622 .num_links = ARRAY_SIZE(skylake_dais),
623 .controls = skylake_controls,
624 .num_controls = ARRAY_SIZE(skylake_controls),
625 .dapm_widgets = skylake_widgets,
626 .num_dapm_widgets = ARRAY_SIZE(skylake_widgets),
627 .dapm_routes = skylake_map,
628 .num_dapm_routes = ARRAY_SIZE(skylake_map),
629 .fully_routed = true,
630 .late_probe = skylake_card_late_probe,
633 static int skylake_audio_probe(struct platform_device *pdev)
635 struct skl_nau8825_private *ctx;
636 struct snd_soc_acpi_mach *mach;
638 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
642 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
644 skylake_audio_card.dev = &pdev->dev;
645 snd_soc_card_set_drvdata(&skylake_audio_card, ctx);
647 mach = (&pdev->dev)->platform_data;
649 dmic_constraints = mach->mach_params.dmic_num == 2 ?
650 &constraints_dmic_2ch : &constraints_dmic_channels;
652 return devm_snd_soc_register_card(&pdev->dev, &skylake_audio_card);
655 static const struct platform_device_id skl_board_ids[] = {
656 { .name = "skl_n88l25_m98357a" },
657 { .name = "kbl_n88l25_m98357a" },
661 static struct platform_driver skylake_audio = {
662 .probe = skylake_audio_probe,
664 .name = "skl_n88l25_m98357a",
665 .pm = &snd_soc_pm_ops,
667 .id_table = skl_board_ids,
670 module_platform_driver(skylake_audio)
672 /* Module information */
673 MODULE_DESCRIPTION("Audio Machine driver-NAU88L25 & MAX98357A in I2S mode");
674 MODULE_AUTHOR("Rohit Ainapure <rohit.m.ainapure@intel.com");
675 MODULE_LICENSE("GPL v2");
676 MODULE_ALIAS("platform:skl_n88l25_m98357a");
677 MODULE_ALIAS("platform:kbl_n88l25_m98357a");