1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2017-18 Intel Corporation.
5 * Intel Kabylake I2S Machine Driver with MAX98357A & DA7219 Codecs
8 * Intel Kabylake I2S Machine driver supporting MAXIM98927 and
12 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <sound/core.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include "../../codecs/da7219.h"
21 #include "../../codecs/hdac_hdmi.h"
22 #include "../../codecs/da7219-aad.h"
24 #define KBL_DIALOG_CODEC_DAI "da7219-hifi"
25 #define KBL_MAXIM_CODEC_DAI "HiFi"
26 #define MAXIM_DEV0_NAME "MX98357A:00"
27 #define DUAL_CHANNEL 2
28 #define QUAD_CHANNEL 4
30 static struct snd_soc_card *kabylake_audio_card;
31 static struct snd_soc_jack skylake_hdmi[3];
34 struct list_head head;
35 struct snd_soc_dai *codec_dai;
39 struct kbl_codec_private {
40 struct snd_soc_jack kabylake_headset;
41 struct list_head hdmi_pcm_list;
45 KBL_DPCM_AUDIO_PB = 0,
47 KBL_DPCM_AUDIO_REF_CP,
48 KBL_DPCM_AUDIO_DMIC_CP,
49 KBL_DPCM_AUDIO_HDMI1_PB,
50 KBL_DPCM_AUDIO_HDMI2_PB,
51 KBL_DPCM_AUDIO_HDMI3_PB,
54 static int platform_clock_control(struct snd_soc_dapm_widget *w,
55 struct snd_kcontrol *k, int event)
57 struct snd_soc_dapm_context *dapm = w->dapm;
58 struct snd_soc_card *card = dapm->card;
59 struct snd_soc_dai *codec_dai;
62 codec_dai = snd_soc_card_get_codec_dai(card, KBL_DIALOG_CODEC_DAI);
64 dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n");
68 if (SND_SOC_DAPM_EVENT_OFF(event)) {
69 ret = snd_soc_dai_set_pll(codec_dai, 0,
70 DA7219_SYSCLK_MCLK, 0, 0);
72 dev_err(card->dev, "failed to stop PLL: %d\n", ret);
73 } else if (SND_SOC_DAPM_EVENT_ON(event)) {
74 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
75 0, DA7219_PLL_FREQ_OUT_98304);
77 dev_err(card->dev, "failed to start PLL: %d\n", ret);
83 static const struct snd_kcontrol_new kabylake_controls[] = {
84 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
85 SOC_DAPM_PIN_SWITCH("Headset Mic"),
86 SOC_DAPM_PIN_SWITCH("Spk"),
89 static const struct snd_soc_dapm_widget kabylake_widgets[] = {
90 SND_SOC_DAPM_HP("Headphone Jack", NULL),
91 SND_SOC_DAPM_MIC("Headset Mic", NULL),
92 SND_SOC_DAPM_SPK("Spk", NULL),
93 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
94 SND_SOC_DAPM_SPK("HDMI1", NULL),
95 SND_SOC_DAPM_SPK("HDMI2", NULL),
96 SND_SOC_DAPM_SPK("HDMI3", NULL),
97 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
98 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
99 SND_SOC_DAPM_POST_PMD),
102 static const struct snd_soc_dapm_route kabylake_map[] = {
103 { "Headphone Jack", NULL, "HPL" },
104 { "Headphone Jack", NULL, "HPR" },
107 { "Spk", NULL, "Speaker" },
110 { "MIC", NULL, "Headset Mic" },
111 { "DMic", NULL, "SoC DMIC" },
113 {"HDMI1", NULL, "hif5-0 Output"},
114 {"HDMI2", NULL, "hif6-0 Output"},
115 {"HDMI3", NULL, "hif7-0 Output"},
117 /* CODEC BE connections */
118 { "HiFi Playback", NULL, "ssp0 Tx" },
119 { "ssp0 Tx", NULL, "codec0_out" },
121 { "Playback", NULL, "ssp1 Tx" },
122 { "ssp1 Tx", NULL, "codec1_out" },
124 { "codec0_in", NULL, "ssp1 Rx" },
125 { "ssp1 Rx", NULL, "Capture" },
128 { "dmic01_hifi", NULL, "DMIC01 Rx" },
129 { "DMIC01 Rx", NULL, "DMIC AIF" },
131 { "hifi1", NULL, "iDisp1 Tx" },
132 { "iDisp1 Tx", NULL, "iDisp1_out" },
133 { "hifi2", NULL, "iDisp2 Tx" },
134 { "iDisp2 Tx", NULL, "iDisp2_out" },
135 { "hifi3", NULL, "iDisp3 Tx"},
136 { "iDisp3 Tx", NULL, "iDisp3_out"},
138 { "Headphone Jack", NULL, "Platform Clock" },
139 { "Headset Mic", NULL, "Platform Clock" },
142 static int kabylake_ssp_fixup(struct snd_soc_pcm_runtime *rtd,
143 struct snd_pcm_hw_params *params)
145 struct snd_interval *rate = hw_param_interval(params,
146 SNDRV_PCM_HW_PARAM_RATE);
147 struct snd_interval *chan = hw_param_interval(params,
148 SNDRV_PCM_HW_PARAM_CHANNELS);
149 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
151 /* The ADSP will convert the FE rate to 48k, stereo */
152 rate->min = rate->max = 48000;
153 chan->min = chan->max = DUAL_CHANNEL;
155 /* set SSP to 24 bit */
157 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
162 static int kabylake_da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
164 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card);
165 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
166 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
167 struct snd_soc_jack *jack;
170 /* Configure sysclk for codec */
171 ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, 24576000,
174 dev_err(rtd->dev, "can't set codec sysclk configuration\n");
179 * Headset buttons map to the google Reference headset.
180 * These can be configured by userspace.
182 ret = snd_soc_card_jack_new(kabylake_audio_card, "Headset Jack",
183 SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
184 SND_JACK_BTN_2 | SND_JACK_BTN_3 | SND_JACK_LINEOUT,
185 &ctx->kabylake_headset, NULL, 0);
187 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
191 jack = &ctx->kabylake_headset;
193 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
194 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
195 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
196 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
197 da7219_aad_jack_det(component, &ctx->kabylake_headset);
199 ret = snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
201 dev_err(rtd->dev, "SoC DMIC - Ignore suspend failed %d\n", ret);
206 static int kabylake_hdmi_init(struct snd_soc_pcm_runtime *rtd, int device)
208 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card);
209 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
210 struct kbl_hdmi_pcm *pcm;
212 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
216 pcm->device = device;
217 pcm->codec_dai = dai;
219 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
224 static int kabylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
226 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI1_PB);
229 static int kabylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
231 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI2_PB);
234 static int kabylake_hdmi3_init(struct snd_soc_pcm_runtime *rtd)
236 return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI3_PB);
239 static int kabylake_da7219_fe_init(struct snd_soc_pcm_runtime *rtd)
241 struct snd_soc_dapm_context *dapm;
242 struct snd_soc_component *component = asoc_rtd_to_cpu(rtd, 0)->component;
244 dapm = snd_soc_component_get_dapm(component);
245 snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
250 static const unsigned int rates[] = {
254 static const struct snd_pcm_hw_constraint_list constraints_rates = {
255 .count = ARRAY_SIZE(rates),
260 static const unsigned int channels[] = {
264 static const struct snd_pcm_hw_constraint_list constraints_channels = {
265 .count = ARRAY_SIZE(channels),
270 static unsigned int channels_quad[] = {
274 static struct snd_pcm_hw_constraint_list constraints_channels_quad = {
275 .count = ARRAY_SIZE(channels_quad),
276 .list = channels_quad,
280 static int kbl_fe_startup(struct snd_pcm_substream *substream)
282 struct snd_pcm_runtime *runtime = substream->runtime;
285 * On this platform for PCM device we support,
291 runtime->hw.channels_max = DUAL_CHANNEL;
292 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
293 &constraints_channels);
295 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
296 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
298 snd_pcm_hw_constraint_list(runtime, 0,
299 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
304 static const struct snd_soc_ops kabylake_da7219_fe_ops = {
305 .startup = kbl_fe_startup,
308 static int kabylake_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
309 struct snd_pcm_hw_params *params)
311 struct snd_interval *chan = hw_param_interval(params,
312 SNDRV_PCM_HW_PARAM_CHANNELS);
315 * set BE channel constraint as user FE channels
318 if (params_channels(params) == 2)
319 chan->min = chan->max = 2;
321 chan->min = chan->max = 4;
326 static int kabylake_dmic_startup(struct snd_pcm_substream *substream)
328 struct snd_pcm_runtime *runtime = substream->runtime;
330 runtime->hw.channels_min = runtime->hw.channels_max = QUAD_CHANNEL;
331 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
332 &constraints_channels_quad);
334 return snd_pcm_hw_constraint_list(substream->runtime, 0,
335 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
338 static struct snd_soc_ops kabylake_dmic_ops = {
339 .startup = kabylake_dmic_startup,
342 static unsigned int rates_16000[] = {
346 static const struct snd_pcm_hw_constraint_list constraints_16000 = {
347 .count = ARRAY_SIZE(rates_16000),
351 static const unsigned int ch_mono[] = {
355 static const struct snd_pcm_hw_constraint_list constraints_refcap = {
356 .count = ARRAY_SIZE(ch_mono),
360 static int kabylake_refcap_startup(struct snd_pcm_substream *substream)
362 substream->runtime->hw.channels_max = 1;
363 snd_pcm_hw_constraint_list(substream->runtime, 0,
364 SNDRV_PCM_HW_PARAM_CHANNELS,
365 &constraints_refcap);
367 return snd_pcm_hw_constraint_list(substream->runtime, 0,
368 SNDRV_PCM_HW_PARAM_RATE,
372 static struct snd_soc_ops skylake_refcap_ops = {
373 .startup = kabylake_refcap_startup,
376 SND_SOC_DAILINK_DEF(dummy,
377 DAILINK_COMP_ARRAY(COMP_DUMMY()));
379 SND_SOC_DAILINK_DEF(system,
380 DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
382 SND_SOC_DAILINK_DEF(reference,
383 DAILINK_COMP_ARRAY(COMP_CPU("Reference Pin")));
385 SND_SOC_DAILINK_DEF(dmic,
386 DAILINK_COMP_ARRAY(COMP_CPU("DMIC Pin")));
388 SND_SOC_DAILINK_DEF(hdmi1,
389 DAILINK_COMP_ARRAY(COMP_CPU("HDMI1 Pin")));
391 SND_SOC_DAILINK_DEF(hdmi2,
392 DAILINK_COMP_ARRAY(COMP_CPU("HDMI2 Pin")));
394 SND_SOC_DAILINK_DEF(hdmi3,
395 DAILINK_COMP_ARRAY(COMP_CPU("HDMI3 Pin")));
397 SND_SOC_DAILINK_DEF(ssp0_pin,
398 DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
399 SND_SOC_DAILINK_DEF(ssp0_codec,
400 DAILINK_COMP_ARRAY(COMP_CODEC(MAXIM_DEV0_NAME,
401 KBL_MAXIM_CODEC_DAI)));
403 SND_SOC_DAILINK_DEF(ssp1_pin,
404 DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
405 SND_SOC_DAILINK_DEF(ssp1_codec,
406 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7219:00",
407 KBL_DIALOG_CODEC_DAI)));
409 SND_SOC_DAILINK_DEF(dmic_pin,
410 DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
411 SND_SOC_DAILINK_DEF(dmic_codec,
412 DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
414 SND_SOC_DAILINK_DEF(idisp1_pin,
415 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
416 SND_SOC_DAILINK_DEF(idisp1_codec,
417 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2",
418 "intel-hdmi-hifi1")));
420 SND_SOC_DAILINK_DEF(idisp2_pin,
421 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
422 SND_SOC_DAILINK_DEF(idisp2_codec,
423 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
425 SND_SOC_DAILINK_DEF(idisp3_pin,
426 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
427 SND_SOC_DAILINK_DEF(idisp3_codec,
428 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
430 SND_SOC_DAILINK_DEF(platform,
431 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
433 /* kabylake digital audio interface glue - connects codec <--> CPU */
434 static struct snd_soc_dai_link kabylake_dais[] = {
435 /* Front End DAI links */
436 [KBL_DPCM_AUDIO_PB] = {
437 .name = "Kbl Audio Port",
438 .stream_name = "Audio",
441 .init = kabylake_da7219_fe_init,
443 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
445 .ops = &kabylake_da7219_fe_ops,
446 SND_SOC_DAILINK_REG(system, dummy, platform),
448 [KBL_DPCM_AUDIO_CP] = {
449 .name = "Kbl Audio Capture Port",
450 .stream_name = "Audio Record",
454 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
456 .ops = &kabylake_da7219_fe_ops,
457 SND_SOC_DAILINK_REG(system, dummy, platform),
459 [KBL_DPCM_AUDIO_REF_CP] = {
460 .name = "Kbl Audio Reference cap",
461 .stream_name = "Wake on Voice",
466 .ops = &skylake_refcap_ops,
467 SND_SOC_DAILINK_REG(reference, dummy, platform),
469 [KBL_DPCM_AUDIO_DMIC_CP] = {
470 .name = "Kbl Audio DMIC cap",
471 .stream_name = "dmiccap",
476 .ops = &kabylake_dmic_ops,
477 SND_SOC_DAILINK_REG(dmic, dummy, platform),
479 [KBL_DPCM_AUDIO_HDMI1_PB] = {
480 .name = "Kbl HDMI Port1",
481 .stream_name = "Hdmi1",
485 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
488 SND_SOC_DAILINK_REG(hdmi1, dummy, platform),
490 [KBL_DPCM_AUDIO_HDMI2_PB] = {
491 .name = "Kbl HDMI Port2",
492 .stream_name = "Hdmi2",
496 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
499 SND_SOC_DAILINK_REG(hdmi2, dummy, platform),
501 [KBL_DPCM_AUDIO_HDMI3_PB] = {
502 .name = "Kbl HDMI Port3",
503 .stream_name = "Hdmi3",
505 SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
510 SND_SOC_DAILINK_REG(hdmi3, dummy, platform),
513 /* Back End DAI links */
516 .name = "SSP0-Codec",
519 .dai_fmt = SND_SOC_DAIFMT_I2S |
520 SND_SOC_DAIFMT_NB_NF |
521 SND_SOC_DAIFMT_CBS_CFS,
522 .ignore_pmdown_time = 1,
523 .be_hw_params_fixup = kabylake_ssp_fixup,
525 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
529 .name = "SSP1-Codec",
532 .init = kabylake_da7219_codec_init,
533 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
534 SND_SOC_DAIFMT_CBS_CFS,
535 .ignore_pmdown_time = 1,
536 .be_hw_params_fixup = kabylake_ssp_fixup,
539 SND_SOC_DAILINK_REG(ssp1_pin, ssp1_codec, platform),
544 .be_hw_params_fixup = kabylake_dmic_fixup,
548 SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
554 .init = kabylake_hdmi1_init,
556 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
561 .init = kabylake_hdmi2_init,
564 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
569 .init = kabylake_hdmi3_init,
572 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
577 static int kabylake_card_late_probe(struct snd_soc_card *card)
579 struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(card);
580 struct kbl_hdmi_pcm *pcm;
581 struct snd_soc_component *component = NULL;
583 char jack_name[NAME_SIZE];
585 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
586 component = pcm->codec_dai->component;
587 snprintf(jack_name, sizeof(jack_name),
588 "HDMI/DP, pcm=%d Jack", pcm->device);
589 err = snd_soc_card_jack_new(card, jack_name,
590 SND_JACK_AVOUT, &skylake_hdmi[i],
596 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
608 return hdac_hdmi_jack_port_init(component, &card->dapm);
611 /* kabylake audio machine driver for SPT + DA7219 */
612 static struct snd_soc_card kabylake_audio_card_da7219_m98357a = {
613 .name = "kblda7219max",
614 .owner = THIS_MODULE,
615 .dai_link = kabylake_dais,
616 .num_links = ARRAY_SIZE(kabylake_dais),
617 .controls = kabylake_controls,
618 .num_controls = ARRAY_SIZE(kabylake_controls),
619 .dapm_widgets = kabylake_widgets,
620 .num_dapm_widgets = ARRAY_SIZE(kabylake_widgets),
621 .dapm_routes = kabylake_map,
622 .num_dapm_routes = ARRAY_SIZE(kabylake_map),
623 .fully_routed = true,
624 .late_probe = kabylake_card_late_probe,
627 static int kabylake_audio_probe(struct platform_device *pdev)
629 struct kbl_codec_private *ctx;
631 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
635 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
637 kabylake_audio_card =
638 (struct snd_soc_card *)pdev->id_entry->driver_data;
640 kabylake_audio_card->dev = &pdev->dev;
641 snd_soc_card_set_drvdata(kabylake_audio_card, ctx);
642 return devm_snd_soc_register_card(&pdev->dev, kabylake_audio_card);
645 static const struct platform_device_id kbl_board_ids[] = {
647 .name = "kbl_da7219_mx98357a",
649 (kernel_ulong_t)&kabylake_audio_card_da7219_m98357a,
653 MODULE_DEVICE_TABLE(platform, kbl_board_ids);
655 static struct platform_driver kabylake_audio = {
656 .probe = kabylake_audio_probe,
658 .name = "kbl_da7219_max98357a",
659 .pm = &snd_soc_pm_ops,
661 .id_table = kbl_board_ids,
664 module_platform_driver(kabylake_audio)
666 /* Module information */
667 MODULE_DESCRIPTION("Audio Machine driver-DA7219 & MAX98357A in I2S mode");
668 MODULE_AUTHOR("Naveen Manohar <naveen.m@intel.com>");
669 MODULE_LICENSE("GPL v2");