Back to home page

OSCL-LXR

 
 

    


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