Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Intel Kabylake I2S Machine Driver with MAXIM98927
0004  * RT5514 and RT5663 Codecs
0005  *
0006  * Copyright (C) 2017, Intel Corporation. All rights reserved.
0007  *
0008  * Modified from:
0009  *   Intel Kabylake I2S Machine driver supporting MAXIM98927 and
0010  *   RT5663 codecs
0011  */
0012 
0013 #include <linux/input.h>
0014 #include <linux/module.h>
0015 #include <linux/platform_device.h>
0016 #include <sound/core.h>
0017 #include <sound/jack.h>
0018 #include <sound/pcm.h>
0019 #include <sound/pcm_params.h>
0020 #include <sound/soc.h>
0021 #include <sound/soc-acpi.h>
0022 #include "../../codecs/rt5514.h"
0023 #include "../../codecs/rt5663.h"
0024 #include "../../codecs/hdac_hdmi.h"
0025 #include <linux/clk.h>
0026 #include <linux/clk-provider.h>
0027 #include <linux/clkdev.h>
0028 
0029 #define KBL_REALTEK_CODEC_DAI "rt5663-aif"
0030 #define KBL_REALTEK_DMIC_CODEC_DAI "rt5514-aif1"
0031 #define KBL_MAXIM_CODEC_DAI "max98927-aif1"
0032 #define MAXIM_DEV0_NAME "i2c-MX98927:00"
0033 #define MAXIM_DEV1_NAME "i2c-MX98927:01"
0034 #define RT5514_DEV_NAME "i2c-10EC5514:00"
0035 #define RT5663_DEV_NAME "i2c-10EC5663:00"
0036 #define RT5514_AIF1_BCLK_FREQ (48000 * 8 * 16)
0037 #define RT5514_AIF1_SYSCLK_FREQ 12288000
0038 #define NAME_SIZE 32
0039 
0040 #define DMIC_CH(p) p->list[p->count-1]
0041 
0042 
0043 static struct snd_soc_card kabylake_audio_card;
0044 static const struct snd_pcm_hw_constraint_list *dmic_constraints;
0045 
0046 struct kbl_hdmi_pcm {
0047     struct list_head head;
0048     struct snd_soc_dai *codec_dai;
0049     int device;
0050 };
0051 
0052 struct kbl_codec_private {
0053     struct snd_soc_jack kabylake_headset;
0054     struct list_head hdmi_pcm_list;
0055     struct snd_soc_jack kabylake_hdmi[2];
0056     struct clk *mclk;
0057     struct clk *sclk;
0058 };
0059 
0060 enum {
0061     KBL_DPCM_AUDIO_PB = 0,
0062     KBL_DPCM_AUDIO_CP,
0063     KBL_DPCM_AUDIO_HS_PB,
0064     KBL_DPCM_AUDIO_ECHO_REF_CP,
0065     KBL_DPCM_AUDIO_DMIC_CP,
0066     KBL_DPCM_AUDIO_RT5514_DSP,
0067     KBL_DPCM_AUDIO_HDMI1_PB,
0068     KBL_DPCM_AUDIO_HDMI2_PB,
0069 };
0070 
0071 static const struct snd_kcontrol_new kabylake_controls[] = {
0072     SOC_DAPM_PIN_SWITCH("Headphone Jack"),
0073     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0074     SOC_DAPM_PIN_SWITCH("Left Spk"),
0075     SOC_DAPM_PIN_SWITCH("Right Spk"),
0076     SOC_DAPM_PIN_SWITCH("DMIC"),
0077 };
0078 
0079 static int platform_clock_control(struct snd_soc_dapm_widget *w,
0080             struct snd_kcontrol *k, int  event)
0081 {
0082     struct snd_soc_dapm_context *dapm = w->dapm;
0083     struct snd_soc_card *card = dapm->card;
0084     struct kbl_codec_private *priv = snd_soc_card_get_drvdata(card);
0085     int ret = 0;
0086 
0087     /*
0088      * MCLK/SCLK need to be ON early for a successful synchronization of
0089      * codec internal clock. And the clocks are turned off during
0090      * POST_PMD after the stream is stopped.
0091      */
0092     switch (event) {
0093     case SND_SOC_DAPM_PRE_PMU:
0094         /* Enable MCLK */
0095         ret = clk_set_rate(priv->mclk, 24000000);
0096         if (ret < 0) {
0097             dev_err(card->dev, "Can't set rate for mclk, err: %d\n",
0098                 ret);
0099             return ret;
0100         }
0101 
0102         ret = clk_prepare_enable(priv->mclk);
0103         if (ret < 0) {
0104             dev_err(card->dev, "Can't enable mclk, err: %d\n", ret);
0105             return ret;
0106         }
0107 
0108         /* Enable SCLK */
0109         ret = clk_set_rate(priv->sclk, 3072000);
0110         if (ret < 0) {
0111             dev_err(card->dev, "Can't set rate for sclk, err: %d\n",
0112                 ret);
0113             clk_disable_unprepare(priv->mclk);
0114             return ret;
0115         }
0116 
0117         ret = clk_prepare_enable(priv->sclk);
0118         if (ret < 0) {
0119             dev_err(card->dev, "Can't enable sclk, err: %d\n", ret);
0120             clk_disable_unprepare(priv->mclk);
0121         }
0122         break;
0123     case SND_SOC_DAPM_POST_PMD:
0124         clk_disable_unprepare(priv->mclk);
0125         clk_disable_unprepare(priv->sclk);
0126         break;
0127     default:
0128         return 0;
0129     }
0130 
0131     return 0;
0132 }
0133 
0134 static const struct snd_soc_dapm_widget kabylake_widgets[] = {
0135     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0136     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0137     SND_SOC_DAPM_SPK("Left Spk", NULL),
0138     SND_SOC_DAPM_SPK("Right Spk", NULL),
0139     SND_SOC_DAPM_MIC("DMIC", NULL),
0140     SND_SOC_DAPM_SPK("HDMI1", NULL),
0141     SND_SOC_DAPM_SPK("HDMI2", NULL),
0142     SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
0143             platform_clock_control, SND_SOC_DAPM_PRE_PMU |
0144             SND_SOC_DAPM_POST_PMD),
0145 
0146 };
0147 
0148 static struct snd_soc_jack_pin jack_pins[] = {
0149     {
0150         .pin    = "Headphone Jack",
0151         .mask   = SND_JACK_HEADPHONE,
0152     },
0153     {
0154         .pin    = "Headset Mic",
0155         .mask   = SND_JACK_MICROPHONE,
0156     },
0157 };
0158 
0159 static const struct snd_soc_dapm_route kabylake_map[] = {
0160     /* Headphones */
0161     { "Headphone Jack", NULL, "Platform Clock" },
0162     { "Headphone Jack", NULL, "HPOL" },
0163     { "Headphone Jack", NULL, "HPOR" },
0164 
0165     /* speaker */
0166     { "Left Spk", NULL, "Left BE_OUT" },
0167     { "Right Spk", NULL, "Right BE_OUT" },
0168 
0169     /* other jacks */
0170     { "Headset Mic", NULL, "Platform Clock" },
0171     { "IN1P", NULL, "Headset Mic" },
0172     { "IN1N", NULL, "Headset Mic" },
0173 
0174     /* CODEC BE connections */
0175     { "Left HiFi Playback", NULL, "ssp0 Tx" },
0176     { "Right HiFi Playback", NULL, "ssp0 Tx" },
0177     { "ssp0 Tx", NULL, "spk_out" },
0178 
0179     { "AIF Playback", NULL, "ssp1 Tx" },
0180     { "ssp1 Tx", NULL, "codec1_out" },
0181 
0182     { "hs_in", NULL, "ssp1 Rx" },
0183     { "ssp1 Rx", NULL, "AIF Capture" },
0184 
0185     { "codec1_in", NULL, "ssp0 Rx" },
0186     { "ssp0 Rx", NULL, "AIF1 Capture" },
0187 
0188     /* IV feedback path */
0189     { "codec0_fb_in", NULL, "ssp0 Rx"},
0190     { "ssp0 Rx", NULL, "Left HiFi Capture" },
0191     { "ssp0 Rx", NULL, "Right HiFi Capture" },
0192 
0193     /* DMIC */
0194     { "DMIC1L", NULL, "DMIC" },
0195     { "DMIC1R", NULL, "DMIC" },
0196     { "DMIC2L", NULL, "DMIC" },
0197     { "DMIC2R", NULL, "DMIC" },
0198 
0199     { "hifi2", NULL, "iDisp2 Tx" },
0200     { "iDisp2 Tx", NULL, "iDisp2_out" },
0201     { "hifi1", NULL, "iDisp1 Tx" },
0202     { "iDisp1 Tx", NULL, "iDisp1_out" },
0203 };
0204 
0205 static struct snd_soc_codec_conf max98927_codec_conf[] = {
0206     {
0207         .dlc = COMP_CODEC_CONF(MAXIM_DEV0_NAME),
0208         .name_prefix = "Right",
0209     },
0210     {
0211         .dlc = COMP_CODEC_CONF(MAXIM_DEV1_NAME),
0212         .name_prefix = "Left",
0213     },
0214 };
0215 
0216 
0217 static int kabylake_rt5663_fe_init(struct snd_soc_pcm_runtime *rtd)
0218 {
0219     struct snd_soc_dapm_context *dapm;
0220     struct snd_soc_component *component = asoc_rtd_to_cpu(rtd, 0)->component;
0221     int ret;
0222 
0223     dapm = snd_soc_component_get_dapm(component);
0224     ret = snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
0225     if (ret)
0226         dev_err(rtd->dev, "Ref Cap -Ignore suspend failed = %d\n", ret);
0227 
0228     return ret;
0229 }
0230 
0231 static int kabylake_rt5663_codec_init(struct snd_soc_pcm_runtime *rtd)
0232 {
0233     int ret;
0234     struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card);
0235     struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
0236     struct snd_soc_jack *jack;
0237 
0238     /*
0239      * Headset buttons map to the google Reference headset.
0240      * These can be configured by userspace.
0241      */
0242     ret = snd_soc_card_jack_new_pins(&kabylake_audio_card, "Headset Jack",
0243                      SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0244                      SND_JACK_BTN_2 | SND_JACK_BTN_3,
0245                      &ctx->kabylake_headset,
0246                      jack_pins,
0247                      ARRAY_SIZE(jack_pins));
0248     if (ret) {
0249         dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
0250         return ret;
0251     }
0252 
0253     jack = &ctx->kabylake_headset;
0254     snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
0255     snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
0256     snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
0257     snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
0258 
0259     snd_soc_component_set_jack(component, &ctx->kabylake_headset, NULL);
0260 
0261     ret = snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "DMIC");
0262     if (ret)
0263         dev_err(rtd->dev, "DMIC - Ignore suspend failed = %d\n", ret);
0264 
0265     return ret;
0266 }
0267 
0268 static int kabylake_hdmi_init(struct snd_soc_pcm_runtime *rtd, int device)
0269 {
0270     struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(rtd->card);
0271     struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
0272     struct kbl_hdmi_pcm *pcm;
0273 
0274     pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
0275     if (!pcm)
0276         return -ENOMEM;
0277 
0278     pcm->device = device;
0279     pcm->codec_dai = dai;
0280 
0281     list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
0282 
0283     return 0;
0284 }
0285 
0286 static int kabylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
0287 {
0288     return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI1_PB);
0289 }
0290 
0291 static int kabylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
0292 {
0293     return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI2_PB);
0294 }
0295 
0296 static const unsigned int rates[] = {
0297     48000,
0298 };
0299 
0300 static const struct snd_pcm_hw_constraint_list constraints_rates = {
0301     .count = ARRAY_SIZE(rates),
0302     .list  = rates,
0303     .mask = 0,
0304 };
0305 
0306 static const unsigned int channels[] = {
0307     2,
0308 };
0309 
0310 static const struct snd_pcm_hw_constraint_list constraints_channels = {
0311     .count = ARRAY_SIZE(channels),
0312     .list = channels,
0313     .mask = 0,
0314 };
0315 
0316 static int kbl_fe_startup(struct snd_pcm_substream *substream)
0317 {
0318     struct snd_pcm_runtime *runtime = substream->runtime;
0319 
0320     /*
0321      * On this platform for PCM device we support,
0322      * 48Khz
0323      * stereo
0324      * 16 bit audio
0325      */
0326 
0327     runtime->hw.channels_max = 2;
0328     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
0329                        &constraints_channels);
0330 
0331     runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
0332     snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
0333 
0334     snd_pcm_hw_constraint_list(runtime, 0,
0335                 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
0336 
0337     return 0;
0338 }
0339 
0340 static const struct snd_soc_ops kabylake_rt5663_fe_ops = {
0341     .startup = kbl_fe_startup,
0342 };
0343 
0344 static int kabylake_ssp_fixup(struct snd_soc_pcm_runtime *rtd,
0345                     struct snd_pcm_hw_params *params)
0346 {
0347     struct snd_interval *rate = hw_param_interval(params,
0348             SNDRV_PCM_HW_PARAM_RATE);
0349     struct snd_interval *chan = hw_param_interval(params,
0350             SNDRV_PCM_HW_PARAM_CHANNELS);
0351     struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0352     struct snd_soc_dpcm *dpcm, *rtd_dpcm = NULL;
0353 
0354     /*
0355      * The following loop will be called only for playback stream
0356      * In this platform, there is only one playback device on every SSP
0357      */
0358     for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
0359         rtd_dpcm = dpcm;
0360         break;
0361     }
0362 
0363     /*
0364      * This following loop will be called only for capture stream
0365      * In this platform, there is only one capture device on every SSP
0366      */
0367     for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_CAPTURE, dpcm) {
0368         rtd_dpcm = dpcm;
0369         break;
0370     }
0371 
0372     if (!rtd_dpcm)
0373         return -EINVAL;
0374 
0375     /*
0376      * The above 2 loops are mutually exclusive based on the stream direction,
0377      * thus rtd_dpcm variable will never be overwritten
0378      */
0379 
0380     /*
0381      * The ADSP will convert the FE rate to 48k, stereo, 24 bit
0382      */
0383     if (!strcmp(rtd_dpcm->fe->dai_link->name, "Kbl Audio Port") ||
0384         !strcmp(rtd_dpcm->fe->dai_link->name, "Kbl Audio Headset Playback") ||
0385         !strcmp(rtd_dpcm->fe->dai_link->name, "Kbl Audio Capture Port")) {
0386         rate->min = rate->max = 48000;
0387         chan->min = chan->max = 2;
0388         snd_mask_none(fmt);
0389         snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
0390     } else if (!strcmp(rtd_dpcm->fe->dai_link->name, "Kbl Audio DMIC cap")) {
0391         if (params_channels(params) == 2 ||
0392                 DMIC_CH(dmic_constraints) == 2)
0393             chan->min = chan->max = 2;
0394         else
0395             chan->min = chan->max = 4;
0396     }
0397     /*
0398      * The speaker on the SSP0 supports S16_LE and not S24_LE.
0399      * thus changing the mask here
0400      */
0401     if (!strcmp(rtd_dpcm->be->dai_link->name, "SSP0-Codec"))
0402         snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
0403 
0404     return 0;
0405 }
0406 
0407 static int kabylake_rt5663_hw_params(struct snd_pcm_substream *substream,
0408     struct snd_pcm_hw_params *params)
0409 {
0410     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0411     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0412     int ret;
0413 
0414     /* use ASRC for internal clocks, as PLL rate isn't multiple of BCLK */
0415     rt5663_sel_asrc_clk_src(codec_dai->component,
0416             RT5663_DA_STEREO_FILTER | RT5663_AD_STEREO_FILTER,
0417             RT5663_CLK_SEL_I2S1_ASRC);
0418 
0419     ret = snd_soc_dai_set_sysclk(codec_dai,
0420             RT5663_SCLK_S_MCLK, 24576000, SND_SOC_CLOCK_IN);
0421     if (ret < 0)
0422         dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
0423 
0424     return ret;
0425 }
0426 
0427 static struct snd_soc_ops kabylake_rt5663_ops = {
0428     .hw_params = kabylake_rt5663_hw_params,
0429 };
0430 
0431 static int kabylake_ssp0_hw_params(struct snd_pcm_substream *substream,
0432     struct snd_pcm_hw_params *params)
0433 {
0434     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0435     struct snd_soc_dai *codec_dai;
0436     int ret = 0, j;
0437 
0438     for_each_rtd_codec_dais(rtd, j, codec_dai) {
0439         if (!strcmp(codec_dai->component->name, RT5514_DEV_NAME)) {
0440             ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0, 8, 16);
0441             if (ret < 0) {
0442                 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
0443                 return ret;
0444             }
0445 
0446             ret = snd_soc_dai_set_sysclk(codec_dai,
0447                 RT5514_SCLK_S_MCLK, 24576000, SND_SOC_CLOCK_IN);
0448             if (ret < 0) {
0449                 dev_err(rtd->dev, "set sysclk err: %d\n", ret);
0450                 return ret;
0451             }
0452         }
0453         if (!strcmp(codec_dai->component->name, MAXIM_DEV0_NAME)) {
0454             ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
0455             if (ret < 0) {
0456                 dev_err(rtd->dev, "DEV0 TDM slot err:%d\n", ret);
0457                 return ret;
0458             }
0459         }
0460 
0461         if (!strcmp(codec_dai->component->name, MAXIM_DEV1_NAME)) {
0462             ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
0463             if (ret < 0) {
0464                 dev_err(rtd->dev, "DEV1 TDM slot err:%d\n", ret);
0465                 return ret;
0466             }
0467         }
0468     }
0469     return ret;
0470 }
0471 
0472 static struct snd_soc_ops kabylake_ssp0_ops = {
0473     .hw_params = kabylake_ssp0_hw_params,
0474 };
0475 
0476 static const unsigned int channels_dmic[] = {
0477     4,
0478 };
0479 
0480 static const struct snd_pcm_hw_constraint_list constraints_dmic_channels = {
0481     .count = ARRAY_SIZE(channels_dmic),
0482     .list = channels_dmic,
0483     .mask = 0,
0484 };
0485 
0486 static const unsigned int dmic_2ch[] = {
0487     2,
0488 };
0489 
0490 static const struct snd_pcm_hw_constraint_list constraints_dmic_2ch = {
0491     .count = ARRAY_SIZE(dmic_2ch),
0492     .list = dmic_2ch,
0493     .mask = 0,
0494 };
0495 
0496 static int kabylake_dmic_startup(struct snd_pcm_substream *substream)
0497 {
0498     struct snd_pcm_runtime *runtime = substream->runtime;
0499 
0500     runtime->hw.channels_max = DMIC_CH(dmic_constraints);
0501     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
0502             dmic_constraints);
0503 
0504     runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
0505     snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
0506 
0507     return snd_pcm_hw_constraint_list(substream->runtime, 0,
0508             SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
0509 }
0510 
0511 static struct snd_soc_ops kabylake_dmic_ops = {
0512     .startup = kabylake_dmic_startup,
0513 };
0514 
0515 SND_SOC_DAILINK_DEF(dummy,
0516     DAILINK_COMP_ARRAY(COMP_DUMMY()));
0517 
0518 SND_SOC_DAILINK_DEF(system,
0519     DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
0520 
0521 SND_SOC_DAILINK_DEF(system2,
0522     DAILINK_COMP_ARRAY(COMP_CPU("System Pin2")));
0523 
0524 SND_SOC_DAILINK_DEF(echoref,
0525     DAILINK_COMP_ARRAY(COMP_CPU("Echoref Pin")));
0526 
0527 SND_SOC_DAILINK_DEF(spi_cpu,
0528     DAILINK_COMP_ARRAY(COMP_CPU("spi-PRP0001:00")));
0529 SND_SOC_DAILINK_DEF(spi_platform,
0530     DAILINK_COMP_ARRAY(COMP_PLATFORM("spi-PRP0001:00")));
0531 
0532 SND_SOC_DAILINK_DEF(dmic,
0533     DAILINK_COMP_ARRAY(COMP_CPU("DMIC Pin")));
0534 
0535 SND_SOC_DAILINK_DEF(hdmi1,
0536     DAILINK_COMP_ARRAY(COMP_CPU("HDMI1 Pin")));
0537 
0538 SND_SOC_DAILINK_DEF(hdmi2,
0539     DAILINK_COMP_ARRAY(COMP_CPU("HDMI2 Pin")));
0540 
0541 SND_SOC_DAILINK_DEF(ssp0_pin,
0542     DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
0543 SND_SOC_DAILINK_DEF(ssp0_codec,
0544     DAILINK_COMP_ARRAY(
0545     /* Left */ COMP_CODEC(MAXIM_DEV0_NAME, KBL_MAXIM_CODEC_DAI),
0546     /* Right */COMP_CODEC(MAXIM_DEV1_NAME, KBL_MAXIM_CODEC_DAI),
0547     /* dmic */ COMP_CODEC(RT5514_DEV_NAME, KBL_REALTEK_DMIC_CODEC_DAI)));
0548 
0549 SND_SOC_DAILINK_DEF(ssp1_pin,
0550     DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
0551 SND_SOC_DAILINK_DEF(ssp1_codec,
0552     DAILINK_COMP_ARRAY(COMP_CODEC(RT5663_DEV_NAME, KBL_REALTEK_CODEC_DAI)));
0553 
0554 SND_SOC_DAILINK_DEF(idisp1_pin,
0555     DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
0556 SND_SOC_DAILINK_DEF(idisp1_codec,
0557     DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
0558 
0559 SND_SOC_DAILINK_DEF(idisp2_pin,
0560     DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
0561 SND_SOC_DAILINK_DEF(idisp2_codec,
0562     DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
0563 
0564 SND_SOC_DAILINK_DEF(platform,
0565     DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
0566 
0567 /* kabylake digital audio interface glue - connects codec <--> CPU */
0568 static struct snd_soc_dai_link kabylake_dais[] = {
0569     /* Front End DAI links */
0570     [KBL_DPCM_AUDIO_PB] = {
0571         .name = "Kbl Audio Port",
0572         .stream_name = "Audio",
0573         .dynamic = 1,
0574         .nonatomic = 1,
0575         .init = kabylake_rt5663_fe_init,
0576         .trigger = {
0577             SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0578         .dpcm_playback = 1,
0579         .ops = &kabylake_rt5663_fe_ops,
0580         SND_SOC_DAILINK_REG(system, dummy, platform),
0581     },
0582     [KBL_DPCM_AUDIO_CP] = {
0583         .name = "Kbl Audio Capture Port",
0584         .stream_name = "Audio Record",
0585         .dynamic = 1,
0586         .nonatomic = 1,
0587         .trigger = {
0588             SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0589         .dpcm_capture = 1,
0590         .ops = &kabylake_rt5663_fe_ops,
0591         SND_SOC_DAILINK_REG(system, dummy, platform),
0592     },
0593     [KBL_DPCM_AUDIO_HS_PB] = {
0594         .name = "Kbl Audio Headset Playback",
0595         .stream_name = "Headset Audio",
0596         .dpcm_playback = 1,
0597         .nonatomic = 1,
0598         .dynamic = 1,
0599         SND_SOC_DAILINK_REG(system2, dummy, platform),
0600     },
0601     [KBL_DPCM_AUDIO_ECHO_REF_CP] = {
0602         .name = "Kbl Audio Echo Reference cap",
0603         .stream_name = "Echoreference Capture",
0604         .init = NULL,
0605         .dpcm_capture = 1,
0606         .nonatomic = 1,
0607         SND_SOC_DAILINK_REG(echoref, dummy, platform),
0608     },
0609     [KBL_DPCM_AUDIO_RT5514_DSP] = {
0610         .name = "rt5514 dsp",
0611         .stream_name = "Wake on Voice",
0612         SND_SOC_DAILINK_REG(spi_cpu, dummy, spi_platform),
0613     },
0614     [KBL_DPCM_AUDIO_DMIC_CP] = {
0615         .name = "Kbl Audio DMIC cap",
0616         .stream_name = "dmiccap",
0617         .init = NULL,
0618         .dpcm_capture = 1,
0619         .nonatomic = 1,
0620         .dynamic = 1,
0621         .ops = &kabylake_dmic_ops,
0622         SND_SOC_DAILINK_REG(dmic, dummy, platform),
0623     },
0624     [KBL_DPCM_AUDIO_HDMI1_PB] = {
0625         .name = "Kbl HDMI Port1",
0626         .stream_name = "Hdmi1",
0627         .dpcm_playback = 1,
0628         .init = NULL,
0629         .trigger = {
0630             SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0631         .nonatomic = 1,
0632         .dynamic = 1,
0633         SND_SOC_DAILINK_REG(hdmi1, dummy, platform),
0634     },
0635     [KBL_DPCM_AUDIO_HDMI2_PB] = {
0636         .name = "Kbl HDMI Port2",
0637         .stream_name = "Hdmi2",
0638         .dpcm_playback = 1,
0639         .init = NULL,
0640         .trigger = {
0641             SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0642         .nonatomic = 1,
0643         .dynamic = 1,
0644         SND_SOC_DAILINK_REG(hdmi2, dummy, platform),
0645     },
0646     /* Back End DAI links */
0647     /* single Back end dai for both max speakers and dmic */
0648     {
0649         /* SSP0 - Codec */
0650         .name = "SSP0-Codec",
0651         .id = 0,
0652         .no_pcm = 1,
0653         .dai_fmt = SND_SOC_DAIFMT_DSP_B |
0654             SND_SOC_DAIFMT_NB_NF |
0655             SND_SOC_DAIFMT_CBC_CFC,
0656         .ignore_pmdown_time = 1,
0657         .be_hw_params_fixup = kabylake_ssp_fixup,
0658         .dpcm_playback = 1,
0659         .dpcm_capture = 1,
0660         .ops = &kabylake_ssp0_ops,
0661         SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
0662     },
0663     {
0664         .name = "SSP1-Codec",
0665         .id = 1,
0666         .no_pcm = 1,
0667         .init = kabylake_rt5663_codec_init,
0668         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0669             SND_SOC_DAIFMT_CBC_CFC,
0670         .ignore_pmdown_time = 1,
0671         .be_hw_params_fixup = kabylake_ssp_fixup,
0672         .ops = &kabylake_rt5663_ops,
0673         .dpcm_playback = 1,
0674         .dpcm_capture = 1,
0675         SND_SOC_DAILINK_REG(ssp1_pin, ssp1_codec, platform),
0676     },
0677     {
0678         .name = "iDisp1",
0679         .id = 3,
0680         .dpcm_playback = 1,
0681         .init = kabylake_hdmi1_init,
0682         .no_pcm = 1,
0683         SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
0684     },
0685     {
0686         .name = "iDisp2",
0687         .id = 4,
0688         .init = kabylake_hdmi2_init,
0689         .dpcm_playback = 1,
0690         .no_pcm = 1,
0691         SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
0692     },
0693 };
0694 
0695 static int kabylake_set_bias_level(struct snd_soc_card *card,
0696     struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
0697 {
0698     struct snd_soc_component *component = dapm->component;
0699     struct kbl_codec_private *priv = snd_soc_card_get_drvdata(card);
0700     int ret = 0;
0701 
0702     if (!component || strcmp(component->name, RT5514_DEV_NAME))
0703         return 0;
0704 
0705     if (IS_ERR(priv->mclk))
0706         return 0;
0707 
0708     /*
0709      * It's required to control mclk directly in the set_bias_level
0710      * function for rt5514 codec or the recording function could
0711      * break.
0712      */
0713     switch (level) {
0714     case SND_SOC_BIAS_PREPARE:
0715         if (dapm->bias_level == SND_SOC_BIAS_ON) {
0716             if (!__clk_is_enabled(priv->mclk))
0717                 return 0;
0718             dev_dbg(card->dev, "Disable mclk");
0719             clk_disable_unprepare(priv->mclk);
0720         } else {
0721             dev_dbg(card->dev, "Enable mclk");
0722             ret = clk_set_rate(priv->mclk, 24000000);
0723             if (ret) {
0724                 dev_err(card->dev, "Can't set rate for mclk, err: %d\n",
0725                     ret);
0726                 return ret;
0727             }
0728 
0729             ret = clk_prepare_enable(priv->mclk);
0730             if (ret) {
0731                 dev_err(card->dev, "Can't enable mclk, err: %d\n",
0732                     ret);
0733 
0734                 /* mclk is already enabled in FW */
0735                 ret = 0;
0736             }
0737         }
0738         break;
0739     default:
0740         break;
0741     }
0742 
0743     return ret;
0744 }
0745 
0746 static int kabylake_card_late_probe(struct snd_soc_card *card)
0747 {
0748     struct kbl_codec_private *ctx = snd_soc_card_get_drvdata(card);
0749     struct kbl_hdmi_pcm *pcm;
0750     struct snd_soc_component *component = NULL;
0751     int err, i = 0;
0752     char jack_name[NAME_SIZE];
0753 
0754     list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
0755         component = pcm->codec_dai->component;
0756         snprintf(jack_name, sizeof(jack_name),
0757             "HDMI/DP,pcm=%d Jack", pcm->device);
0758         err = snd_soc_card_jack_new(card, jack_name,
0759                 SND_JACK_AVOUT, &ctx->kabylake_hdmi[i]);
0760 
0761         if (err)
0762             return err;
0763         err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
0764                         &ctx->kabylake_hdmi[i]);
0765         if (err < 0)
0766             return err;
0767         i++;
0768     }
0769 
0770     if (!component)
0771         return -EINVAL;
0772 
0773     return hdac_hdmi_jack_port_init(component, &card->dapm);
0774 }
0775 
0776 /*
0777  * kabylake audio machine driver for  MAX98927 + RT5514 + RT5663
0778  */
0779 static struct snd_soc_card kabylake_audio_card = {
0780     .name = "kbl-r5514-5663-max",
0781     .owner = THIS_MODULE,
0782     .dai_link = kabylake_dais,
0783     .num_links = ARRAY_SIZE(kabylake_dais),
0784     .set_bias_level = kabylake_set_bias_level,
0785     .controls = kabylake_controls,
0786     .num_controls = ARRAY_SIZE(kabylake_controls),
0787     .dapm_widgets = kabylake_widgets,
0788     .num_dapm_widgets = ARRAY_SIZE(kabylake_widgets),
0789     .dapm_routes = kabylake_map,
0790     .num_dapm_routes = ARRAY_SIZE(kabylake_map),
0791     .codec_conf = max98927_codec_conf,
0792     .num_configs = ARRAY_SIZE(max98927_codec_conf),
0793     .fully_routed = true,
0794     .late_probe = kabylake_card_late_probe,
0795 };
0796 
0797 static int kabylake_audio_probe(struct platform_device *pdev)
0798 {
0799     struct kbl_codec_private *ctx;
0800     struct snd_soc_acpi_mach *mach;
0801     int ret;
0802 
0803     ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
0804     if (!ctx)
0805         return -ENOMEM;
0806 
0807     INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
0808 
0809     kabylake_audio_card.dev = &pdev->dev;
0810     snd_soc_card_set_drvdata(&kabylake_audio_card, ctx);
0811 
0812     mach = pdev->dev.platform_data;
0813     if (mach)
0814         dmic_constraints = mach->mach_params.dmic_num == 2 ?
0815             &constraints_dmic_2ch : &constraints_dmic_channels;
0816 
0817     ctx->mclk = devm_clk_get(&pdev->dev, "ssp1_mclk");
0818     if (IS_ERR(ctx->mclk)) {
0819         ret = PTR_ERR(ctx->mclk);
0820         if (ret == -ENOENT) {
0821             dev_info(&pdev->dev,
0822                 "Failed to get ssp1_mclk, defer probe\n");
0823             return -EPROBE_DEFER;
0824         }
0825 
0826         dev_err(&pdev->dev, "Failed to get ssp1_mclk with err:%d\n",
0827                                 ret);
0828         return ret;
0829     }
0830 
0831     ctx->sclk = devm_clk_get(&pdev->dev, "ssp1_sclk");
0832     if (IS_ERR(ctx->sclk)) {
0833         ret = PTR_ERR(ctx->sclk);
0834         if (ret == -ENOENT) {
0835             dev_info(&pdev->dev,
0836                 "Failed to get ssp1_sclk, defer probe\n");
0837             return -EPROBE_DEFER;
0838         }
0839 
0840         dev_err(&pdev->dev, "Failed to get ssp1_sclk with err:%d\n",
0841                                 ret);
0842         return ret;
0843     }
0844 
0845     return devm_snd_soc_register_card(&pdev->dev, &kabylake_audio_card);
0846 }
0847 
0848 static const struct platform_device_id kbl_board_ids[] = {
0849     { .name = "kbl_r5514_5663_max" },
0850     { }
0851 };
0852 MODULE_DEVICE_TABLE(platform, kbl_board_ids);
0853 
0854 static struct platform_driver kabylake_audio = {
0855     .probe = kabylake_audio_probe,
0856     .driver = {
0857         .name = "kbl_r5514_5663_max",
0858         .pm = &snd_soc_pm_ops,
0859     },
0860     .id_table = kbl_board_ids,
0861 };
0862 
0863 module_platform_driver(kabylake_audio)
0864 
0865 /* Module information */
0866 MODULE_DESCRIPTION("Audio Machine driver-RT5663 RT5514 & MAX98927");
0867 MODULE_AUTHOR("Harsha Priya <harshapriya.n@intel.com>");
0868 MODULE_LICENSE("GPL v2");