2 * Intel Skylake I2S Machine Driver with MAXIM98357A
5 * Copyright (C) 2015, Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <sound/core.h>
20 #include <sound/jack.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include "../../codecs/nau8825.h"
25 #include "../../codecs/hdac_hdmi.h"
26 #include "../skylake/skl.h"
28 #define SKL_NUVOTON_CODEC_DAI "nau8825-hifi"
29 #define SKL_MAXIM_CODEC_DAI "HiFi"
30 #define DMIC_CH(p) p->list[p->count-1]
32 static struct snd_soc_jack skylake_headset;
33 static struct snd_soc_card skylake_audio_card;
34 static const struct snd_pcm_hw_constraint_list *dmic_constraints;
35 static struct snd_soc_jack skylake_hdmi[3];
38 struct list_head head;
39 struct snd_soc_dai *codec_dai;
43 struct skl_nau8825_private {
44 struct list_head hdmi_pcm_list;
48 SKL_DPCM_AUDIO_PB = 0,
50 SKL_DPCM_AUDIO_REF_CP,
51 SKL_DPCM_AUDIO_DMIC_CP,
52 SKL_DPCM_AUDIO_HDMI1_PB,
53 SKL_DPCM_AUDIO_HDMI2_PB,
54 SKL_DPCM_AUDIO_HDMI3_PB,
57 static inline struct snd_soc_dai *skl_get_codec_dai(struct snd_soc_card *card)
59 struct snd_soc_pcm_runtime *rtd;
61 list_for_each_entry(rtd, &card->rtd_list, list) {
63 if (!strncmp(rtd->codec_dai->name, SKL_NUVOTON_CODEC_DAI,
64 strlen(SKL_NUVOTON_CODEC_DAI)))
65 return rtd->codec_dai;
71 static int platform_clock_control(struct snd_soc_dapm_widget *w,
72 struct snd_kcontrol *k, int event)
74 struct snd_soc_dapm_context *dapm = w->dapm;
75 struct snd_soc_card *card = dapm->card;
76 struct snd_soc_dai *codec_dai;
79 codec_dai = skl_get_codec_dai(card);
81 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
85 if (SND_SOC_DAPM_EVENT_ON(event)) {
86 ret = snd_soc_dai_set_sysclk(codec_dai,
87 NAU8825_CLK_MCLK, 24000000, SND_SOC_CLOCK_IN);
89 dev_err(card->dev, "set sysclk err = %d\n", ret);
93 ret = snd_soc_dai_set_sysclk(codec_dai,
94 NAU8825_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
96 dev_err(card->dev, "set sysclk err = %d\n", ret);
104 static const struct snd_kcontrol_new skylake_controls[] = {
105 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
106 SOC_DAPM_PIN_SWITCH("Headset Mic"),
107 SOC_DAPM_PIN_SWITCH("Spk"),
110 static const struct snd_soc_dapm_widget skylake_widgets[] = {
111 SND_SOC_DAPM_HP("Headphone Jack", NULL),
112 SND_SOC_DAPM_MIC("Headset Mic", NULL),
113 SND_SOC_DAPM_SPK("Spk", NULL),
114 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
115 SND_SOC_DAPM_SPK("DP1", NULL),
116 SND_SOC_DAPM_SPK("DP2", NULL),
117 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
118 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
119 SND_SOC_DAPM_POST_PMD),
122 static const struct snd_soc_dapm_route skylake_map[] = {
123 /* HP jack connectors - unknown if we have jack detection */
124 { "Headphone Jack", NULL, "HPOL" },
125 { "Headphone Jack", NULL, "HPOR" },
128 { "Spk", NULL, "Speaker" },
131 { "MIC", NULL, "Headset Mic" },
132 { "DMic", NULL, "SoC DMIC" },
134 /* CODEC BE connections */
135 { "HiFi Playback", NULL, "ssp0 Tx" },
136 { "ssp0 Tx", NULL, "codec0_out" },
138 { "Playback", NULL, "ssp1 Tx" },
139 { "ssp1 Tx", NULL, "codec1_out" },
141 { "codec0_in", NULL, "ssp1 Rx" },
142 { "ssp1 Rx", NULL, "Capture" },
145 { "dmic01_hifi", NULL, "DMIC01 Rx" },
146 { "DMIC01 Rx", NULL, "DMIC AIF" },
148 { "hifi3", NULL, "iDisp3 Tx"},
149 { "iDisp3 Tx", NULL, "iDisp3_out"},
150 { "hifi2", NULL, "iDisp2 Tx"},
151 { "iDisp2 Tx", NULL, "iDisp2_out"},
152 { "hifi1", NULL, "iDisp1 Tx"},
153 { "iDisp1 Tx", NULL, "iDisp1_out"},
155 { "Headphone Jack", NULL, "Platform Clock" },
156 { "Headset Mic", NULL, "Platform Clock" },
159 static int skylake_ssp_fixup(struct snd_soc_pcm_runtime *rtd,
160 struct snd_pcm_hw_params *params)
162 struct snd_interval *rate = hw_param_interval(params,
163 SNDRV_PCM_HW_PARAM_RATE);
164 struct snd_interval *channels = hw_param_interval(params,
165 SNDRV_PCM_HW_PARAM_CHANNELS);
166 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
168 /* The ADSP will covert the FE rate to 48k, stereo */
169 rate->min = rate->max = 48000;
170 channels->min = channels->max = 2;
172 /* set SSP0 to 24 bit */
174 snd_mask_set(fmt, SNDRV_PCM_FORMAT_S24_LE);
179 static int skylake_nau8825_codec_init(struct snd_soc_pcm_runtime *rtd)
182 struct snd_soc_codec *codec = rtd->codec;
185 * Headset buttons map to the google Reference headset.
186 * These can be configured by userspace.
188 ret = snd_soc_card_jack_new(&skylake_audio_card, "Headset Jack",
189 SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
190 SND_JACK_BTN_2 | SND_JACK_BTN_3, &skylake_headset,
193 dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
197 nau8825_enable_jack_detect(codec, &skylake_headset);
199 snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
204 static int skylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
206 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
207 struct snd_soc_dai *dai = rtd->codec_dai;
208 struct skl_hdmi_pcm *pcm;
210 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
214 pcm->device = SKL_DPCM_AUDIO_HDMI1_PB;
215 pcm->codec_dai = dai;
217 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
222 static int skylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
224 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
225 struct snd_soc_dai *dai = rtd->codec_dai;
226 struct skl_hdmi_pcm *pcm;
228 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
232 pcm->device = SKL_DPCM_AUDIO_HDMI2_PB;
233 pcm->codec_dai = dai;
235 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
240 static int skylake_hdmi3_init(struct snd_soc_pcm_runtime *rtd)
242 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(rtd->card);
243 struct snd_soc_dai *dai = rtd->codec_dai;
244 struct skl_hdmi_pcm *pcm;
246 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
250 pcm->device = SKL_DPCM_AUDIO_HDMI3_PB;
251 pcm->codec_dai = dai;
253 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
258 static int skylake_nau8825_fe_init(struct snd_soc_pcm_runtime *rtd)
260 struct snd_soc_dapm_context *dapm;
261 struct snd_soc_component *component = rtd->cpu_dai->component;
263 dapm = snd_soc_component_get_dapm(component);
264 snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
269 static unsigned int rates[] = {
273 static struct snd_pcm_hw_constraint_list constraints_rates = {
274 .count = ARRAY_SIZE(rates),
279 static unsigned int channels[] = {
283 static struct snd_pcm_hw_constraint_list constraints_channels = {
284 .count = ARRAY_SIZE(channels),
289 static int skl_fe_startup(struct snd_pcm_substream *substream)
291 struct snd_pcm_runtime *runtime = substream->runtime;
294 * On this platform for PCM device we support,
300 runtime->hw.channels_max = 2;
301 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
302 &constraints_channels);
304 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
305 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
307 snd_pcm_hw_constraint_list(runtime, 0,
308 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
313 static const struct snd_soc_ops skylake_nau8825_fe_ops = {
314 .startup = skl_fe_startup,
317 static int skylake_nau8825_hw_params(struct snd_pcm_substream *substream,
318 struct snd_pcm_hw_params *params)
320 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 struct snd_soc_dai *codec_dai = rtd->codec_dai;
324 ret = snd_soc_dai_set_sysclk(codec_dai,
325 NAU8825_CLK_MCLK, 24000000, SND_SOC_CLOCK_IN);
328 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
333 static const struct snd_soc_ops skylake_nau8825_ops = {
334 .hw_params = skylake_nau8825_hw_params,
337 static int skylake_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
338 struct snd_pcm_hw_params *params)
340 struct snd_interval *channels = hw_param_interval(params,
341 SNDRV_PCM_HW_PARAM_CHANNELS);
343 if (params_channels(params) == 2 || DMIC_CH(dmic_constraints) == 2)
344 channels->min = channels->max = 2;
346 channels->min = channels->max = 4;
351 static unsigned int channels_dmic[] = {
355 static struct snd_pcm_hw_constraint_list constraints_dmic_channels = {
356 .count = ARRAY_SIZE(channels_dmic),
357 .list = channels_dmic,
361 static const unsigned int dmic_2ch[] = {
365 static const struct snd_pcm_hw_constraint_list constraints_dmic_2ch = {
366 .count = ARRAY_SIZE(dmic_2ch),
371 static int skylake_dmic_startup(struct snd_pcm_substream *substream)
373 struct snd_pcm_runtime *runtime = substream->runtime;
375 runtime->hw.channels_max = DMIC_CH(dmic_constraints);
376 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
379 return snd_pcm_hw_constraint_list(substream->runtime, 0,
380 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
383 static const struct snd_soc_ops skylake_dmic_ops = {
384 .startup = skylake_dmic_startup,
387 static unsigned int rates_16000[] = {
391 static struct snd_pcm_hw_constraint_list constraints_16000 = {
392 .count = ARRAY_SIZE(rates_16000),
396 static const unsigned int ch_mono[] = {
400 static const struct snd_pcm_hw_constraint_list constraints_refcap = {
401 .count = ARRAY_SIZE(ch_mono),
405 static int skylake_refcap_startup(struct snd_pcm_substream *substream)
407 substream->runtime->hw.channels_max = 1;
408 snd_pcm_hw_constraint_list(substream->runtime, 0,
409 SNDRV_PCM_HW_PARAM_CHANNELS,
410 &constraints_refcap);
412 return snd_pcm_hw_constraint_list(substream->runtime, 0,
413 SNDRV_PCM_HW_PARAM_RATE,
417 static const struct snd_soc_ops skylaye_refcap_ops = {
418 .startup = skylake_refcap_startup,
421 /* skylake digital audio interface glue - connects codec <--> CPU */
422 static struct snd_soc_dai_link skylake_dais[] = {
423 /* Front End DAI links */
424 [SKL_DPCM_AUDIO_PB] = {
425 .name = "Skl Audio Port",
426 .stream_name = "Audio",
427 .cpu_dai_name = "System Pin",
428 .platform_name = "0000:00:1f.3",
430 .codec_name = "snd-soc-dummy",
431 .codec_dai_name = "snd-soc-dummy-dai",
433 .init = skylake_nau8825_fe_init,
435 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
437 .ops = &skylake_nau8825_fe_ops,
439 [SKL_DPCM_AUDIO_CP] = {
440 .name = "Skl Audio Capture Port",
441 .stream_name = "Audio Record",
442 .cpu_dai_name = "System Pin",
443 .platform_name = "0000:00:1f.3",
445 .codec_name = "snd-soc-dummy",
446 .codec_dai_name = "snd-soc-dummy-dai",
449 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
451 .ops = &skylake_nau8825_fe_ops,
453 [SKL_DPCM_AUDIO_REF_CP] = {
454 .name = "Skl Audio Reference cap",
455 .stream_name = "Wake on Voice",
456 .cpu_dai_name = "Reference Pin",
457 .codec_name = "snd-soc-dummy",
458 .codec_dai_name = "snd-soc-dummy-dai",
459 .platform_name = "0000:00:1f.3",
464 .ops = &skylaye_refcap_ops,
466 [SKL_DPCM_AUDIO_DMIC_CP] = {
467 .name = "Skl Audio DMIC cap",
468 .stream_name = "dmiccap",
469 .cpu_dai_name = "DMIC Pin",
470 .codec_name = "snd-soc-dummy",
471 .codec_dai_name = "snd-soc-dummy-dai",
472 .platform_name = "0000:00:1f.3",
477 .ops = &skylake_dmic_ops,
479 [SKL_DPCM_AUDIO_HDMI1_PB] = {
480 .name = "Skl HDMI Port1",
481 .stream_name = "Hdmi1",
482 .cpu_dai_name = "HDMI1 Pin",
483 .codec_name = "snd-soc-dummy",
484 .codec_dai_name = "snd-soc-dummy-dai",
485 .platform_name = "0000:00:1f.3",
489 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
493 [SKL_DPCM_AUDIO_HDMI2_PB] = {
494 .name = "Skl HDMI Port2",
495 .stream_name = "Hdmi2",
496 .cpu_dai_name = "HDMI2 Pin",
497 .codec_name = "snd-soc-dummy",
498 .codec_dai_name = "snd-soc-dummy-dai",
499 .platform_name = "0000:00:1f.3",
503 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
507 [SKL_DPCM_AUDIO_HDMI3_PB] = {
508 .name = "Skl HDMI Port3",
509 .stream_name = "Hdmi3",
510 .cpu_dai_name = "HDMI3 Pin",
511 .codec_name = "snd-soc-dummy",
512 .codec_dai_name = "snd-soc-dummy-dai",
513 .platform_name = "0000:00:1f.3",
515 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
522 /* Back End DAI links */
525 .name = "SSP0-Codec",
527 .cpu_dai_name = "SSP0 Pin",
528 .platform_name = "0000:00:1f.3",
530 .codec_name = "MX98357A:00",
531 .codec_dai_name = SKL_MAXIM_CODEC_DAI,
532 .dai_fmt = SND_SOC_DAIFMT_I2S |
533 SND_SOC_DAIFMT_NB_NF |
534 SND_SOC_DAIFMT_CBS_CFS,
535 .ignore_pmdown_time = 1,
536 .be_hw_params_fixup = skylake_ssp_fixup,
541 .name = "SSP1-Codec",
543 .cpu_dai_name = "SSP1 Pin",
544 .platform_name = "0000:00:1f.3",
546 .codec_name = "i2c-10508825:00",
547 .codec_dai_name = SKL_NUVOTON_CODEC_DAI,
548 .init = skylake_nau8825_codec_init,
549 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
550 SND_SOC_DAIFMT_CBS_CFS,
551 .ignore_pmdown_time = 1,
552 .be_hw_params_fixup = skylake_ssp_fixup,
553 .ops = &skylake_nau8825_ops,
560 .cpu_dai_name = "DMIC01 Pin",
561 .codec_name = "dmic-codec",
562 .codec_dai_name = "dmic-hifi",
563 .platform_name = "0000:00:1f.3",
564 .be_hw_params_fixup = skylake_dmic_fixup,
572 .cpu_dai_name = "iDisp1 Pin",
573 .codec_name = "ehdaudio0D2",
574 .codec_dai_name = "intel-hdmi-hifi1",
575 .platform_name = "0000:00:1f.3",
577 .init = skylake_hdmi1_init,
583 .cpu_dai_name = "iDisp2 Pin",
584 .codec_name = "ehdaudio0D2",
585 .codec_dai_name = "intel-hdmi-hifi2",
586 .platform_name = "0000:00:1f.3",
587 .init = skylake_hdmi2_init,
594 .cpu_dai_name = "iDisp3 Pin",
595 .codec_name = "ehdaudio0D2",
596 .codec_dai_name = "intel-hdmi-hifi3",
597 .platform_name = "0000:00:1f.3",
598 .init = skylake_hdmi3_init,
605 static int skylake_card_late_probe(struct snd_soc_card *card)
607 struct skl_nau8825_private *ctx = snd_soc_card_get_drvdata(card);
608 struct skl_hdmi_pcm *pcm;
609 struct snd_soc_codec *codec = NULL;
611 char jack_name[NAME_SIZE];
613 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
614 codec = pcm->codec_dai->codec;
615 snprintf(jack_name, sizeof(jack_name),
616 "HDMI/DP, pcm=%d Jack", pcm->device);
617 err = snd_soc_card_jack_new(card, jack_name,
625 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
636 return hdac_hdmi_jack_port_init(codec, &card->dapm);
639 /* skylake audio machine driver for SPT + NAU88L25 */
640 static struct snd_soc_card skylake_audio_card = {
641 .name = "sklnau8825max",
642 .owner = THIS_MODULE,
643 .dai_link = skylake_dais,
644 .num_links = ARRAY_SIZE(skylake_dais),
645 .controls = skylake_controls,
646 .num_controls = ARRAY_SIZE(skylake_controls),
647 .dapm_widgets = skylake_widgets,
648 .num_dapm_widgets = ARRAY_SIZE(skylake_widgets),
649 .dapm_routes = skylake_map,
650 .num_dapm_routes = ARRAY_SIZE(skylake_map),
651 .fully_routed = true,
652 .late_probe = skylake_card_late_probe,
655 static int skylake_audio_probe(struct platform_device *pdev)
657 struct skl_nau8825_private *ctx;
658 struct skl_machine_pdata *pdata;
660 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_ATOMIC);
664 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
666 skylake_audio_card.dev = &pdev->dev;
667 snd_soc_card_set_drvdata(&skylake_audio_card, ctx);
669 pdata = dev_get_drvdata(&pdev->dev);
671 dmic_constraints = pdata->dmic_num == 2 ?
672 &constraints_dmic_2ch : &constraints_dmic_channels;
674 return devm_snd_soc_register_card(&pdev->dev, &skylake_audio_card);
677 static const struct platform_device_id skl_board_ids[] = {
678 { .name = "skl_n88l25_m98357a" },
679 { .name = "kbl_n88l25_m98357a" },
683 static struct platform_driver skylake_audio = {
684 .probe = skylake_audio_probe,
686 .name = "skl_n88l25_m98357a",
687 .pm = &snd_soc_pm_ops,
689 .id_table = skl_board_ids,
692 module_platform_driver(skylake_audio)
694 /* Module information */
695 MODULE_DESCRIPTION("Audio Machine driver-NAU88L25 & MAX98357A in I2S mode");
696 MODULE_AUTHOR("Rohit Ainapure <rohit.m.ainapure@intel.com");
697 MODULE_LICENSE("GPL v2");
698 MODULE_ALIAS("platform:skl_n88l25_m98357a");
699 MODULE_ALIAS("platform:kbl_n88l25_m98357a");