Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * mt8195-mt6359.c  --
0004  *  MT8195-MT6359 ALSA SoC machine driver code
0005  *
0006  * Copyright (c) 2022 MediaTek Inc.
0007  * Author: Trevor Wu <trevor.wu@mediatek.com>
0008  *     YC Hung <yc.hung@mediatek.com>
0009  */
0010 
0011 #include <linux/input.h>
0012 #include <linux/module.h>
0013 #include <linux/of_device.h>
0014 #include <linux/pm_runtime.h>
0015 #include <sound/jack.h>
0016 #include <sound/pcm_params.h>
0017 #include <sound/rt5682.h>
0018 #include <sound/soc.h>
0019 #include "../../codecs/mt6359.h"
0020 #include "../../codecs/rt1011.h"
0021 #include "../../codecs/rt5682.h"
0022 #include "../common/mtk-afe-platform-driver.h"
0023 #include "../common/mtk-dsp-sof-common.h"
0024 #include "../common/mtk-soc-card.h"
0025 #include "mt8195-afe-clk.h"
0026 #include "mt8195-afe-common.h"
0027 
0028 #define RT1011_SPEAKER_AMP_PRESENT      BIT(0)
0029 #define RT1019_SPEAKER_AMP_PRESENT      BIT(1)
0030 #define MAX98390_SPEAKER_AMP_PRESENT        BIT(2)
0031 
0032 #define RT1011_CODEC_DAI    "rt1011-aif"
0033 #define RT1011_DEV0_NAME    "rt1011.2-0038"
0034 #define RT1011_DEV1_NAME    "rt1011.2-0039"
0035 
0036 #define RT1019_CODEC_DAI    "HiFi"
0037 #define RT1019_DEV0_NAME    "rt1019p"
0038 
0039 #define MAX98390_CODEC_DAI  "max98390-aif1"
0040 #define MAX98390_DEV0_NAME  "max98390.2-0038" /* right */
0041 #define MAX98390_DEV1_NAME  "max98390.2-0039" /* left */
0042 
0043 #define RT5682_CODEC_DAI    "rt5682-aif1"
0044 #define RT5682_DEV0_NAME    "rt5682.2-001a"
0045 
0046 #define RT5682S_CODEC_DAI   "rt5682s-aif1"
0047 #define RT5682S_DEV0_NAME   "rt5682s.2-001a"
0048 
0049 #define SOF_DMA_DL2 "SOF_DMA_DL2"
0050 #define SOF_DMA_DL3 "SOF_DMA_DL3"
0051 #define SOF_DMA_UL4 "SOF_DMA_UL4"
0052 #define SOF_DMA_UL5 "SOF_DMA_UL5"
0053 
0054 struct mt8195_card_data {
0055     const char *name;
0056     unsigned long quirk;
0057 };
0058 
0059 struct mt8195_mt6359_priv {
0060     struct snd_soc_jack headset_jack;
0061     struct snd_soc_jack dp_jack;
0062     struct snd_soc_jack hdmi_jack;
0063     struct clk *i2so1_mclk;
0064 };
0065 
0066 static const struct snd_soc_dapm_widget mt8195_mt6359_widgets[] = {
0067     SND_SOC_DAPM_HP("Headphone", NULL),
0068     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0069     SND_SOC_DAPM_MIXER(SOF_DMA_DL2, SND_SOC_NOPM, 0, 0, NULL, 0),
0070     SND_SOC_DAPM_MIXER(SOF_DMA_DL3, SND_SOC_NOPM, 0, 0, NULL, 0),
0071     SND_SOC_DAPM_MIXER(SOF_DMA_UL4, SND_SOC_NOPM, 0, 0, NULL, 0),
0072     SND_SOC_DAPM_MIXER(SOF_DMA_UL5, SND_SOC_NOPM, 0, 0, NULL, 0),
0073 };
0074 
0075 static const struct snd_soc_dapm_route mt8195_mt6359_routes[] = {
0076     /* headset */
0077     { "Headphone", NULL, "HPOL" },
0078     { "Headphone", NULL, "HPOR" },
0079     { "IN1P", NULL, "Headset Mic" },
0080     /* SOF Uplink */
0081     {SOF_DMA_UL4, NULL, "O034"},
0082     {SOF_DMA_UL4, NULL, "O035"},
0083     {SOF_DMA_UL5, NULL, "O036"},
0084     {SOF_DMA_UL5, NULL, "O037"},
0085     /* SOF Downlink */
0086     {"I070", NULL, SOF_DMA_DL2},
0087     {"I071", NULL, SOF_DMA_DL2},
0088     {"I020", NULL, SOF_DMA_DL3},
0089     {"I021", NULL, SOF_DMA_DL3},
0090 };
0091 
0092 static const struct snd_kcontrol_new mt8195_mt6359_controls[] = {
0093     SOC_DAPM_PIN_SWITCH("Headphone"),
0094     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0095 };
0096 
0097 static const struct snd_soc_dapm_widget mt8195_dual_speaker_widgets[] = {
0098     SND_SOC_DAPM_SPK("Left Spk", NULL),
0099     SND_SOC_DAPM_SPK("Right Spk", NULL),
0100 };
0101 
0102 static const struct snd_kcontrol_new mt8195_dual_speaker_controls[] = {
0103     SOC_DAPM_PIN_SWITCH("Left Spk"),
0104     SOC_DAPM_PIN_SWITCH("Right Spk"),
0105 };
0106 
0107 static const struct snd_soc_dapm_widget mt8195_speaker_widgets[] = {
0108     SND_SOC_DAPM_SPK("Ext Spk", NULL),
0109 };
0110 
0111 static const struct snd_kcontrol_new mt8195_speaker_controls[] = {
0112     SOC_DAPM_PIN_SWITCH("Ext Spk"),
0113 };
0114 
0115 static const struct snd_soc_dapm_route mt8195_rt1011_routes[] = {
0116     { "Left Spk", NULL, "Left SPO" },
0117     { "Right Spk", NULL, "Right SPO" },
0118 };
0119 
0120 static const struct snd_soc_dapm_route mt8195_rt1019_routes[] = {
0121     { "Ext Spk", NULL, "Speaker" },
0122 };
0123 
0124 static const struct snd_soc_dapm_route mt8195_max98390_routes[] = {
0125     { "Left Spk", NULL, "Left BE_OUT" },
0126     { "Right Spk", NULL, "Right BE_OUT" },
0127 };
0128 
0129 #define CKSYS_AUD_TOP_CFG 0x032c
0130 #define CKSYS_AUD_TOP_MON 0x0330
0131 
0132 static int mt8195_mt6359_mtkaif_calibration(struct snd_soc_pcm_runtime *rtd)
0133 {
0134     struct snd_soc_component *cmpnt_afe =
0135         snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
0136     struct snd_soc_component *cmpnt_codec =
0137         asoc_rtd_to_codec(rtd, 0)->component;
0138     struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt_afe);
0139     struct mt8195_afe_private *afe_priv = afe->platform_priv;
0140     struct mtkaif_param *param = &afe_priv->mtkaif_params;
0141     int chosen_phase_1, chosen_phase_2, chosen_phase_3;
0142     int prev_cycle_1, prev_cycle_2, prev_cycle_3;
0143     int test_done_1, test_done_2, test_done_3;
0144     int cycle_1, cycle_2, cycle_3;
0145     int mtkaif_chosen_phase[MT8195_MTKAIF_MISO_NUM];
0146     int mtkaif_phase_cycle[MT8195_MTKAIF_MISO_NUM];
0147     int mtkaif_calibration_num_phase;
0148     bool mtkaif_calibration_ok;
0149     unsigned int monitor;
0150     int counter;
0151     int phase;
0152     int i;
0153 
0154     dev_dbg(afe->dev, "%s(), start\n", __func__);
0155 
0156     param->mtkaif_calibration_ok = false;
0157     for (i = 0; i < MT8195_MTKAIF_MISO_NUM; i++) {
0158         param->mtkaif_chosen_phase[i] = -1;
0159         param->mtkaif_phase_cycle[i] = 0;
0160         mtkaif_chosen_phase[i] = -1;
0161         mtkaif_phase_cycle[i] = 0;
0162     }
0163 
0164     if (IS_ERR(afe_priv->topckgen)) {
0165         dev_info(afe->dev, "%s() Cannot find topckgen controller\n",
0166              __func__);
0167         return 0;
0168     }
0169 
0170     pm_runtime_get_sync(afe->dev);
0171     mt6359_mtkaif_calibration_enable(cmpnt_codec);
0172 
0173     /* set test type to synchronizer pulse */
0174     regmap_update_bits(afe_priv->topckgen,
0175                CKSYS_AUD_TOP_CFG, 0xffff, 0x4);
0176     mtkaif_calibration_num_phase = 42;  /* mt6359: 0 ~ 42 */
0177     mtkaif_calibration_ok = true;
0178 
0179     for (phase = 0;
0180          phase <= mtkaif_calibration_num_phase && mtkaif_calibration_ok;
0181          phase++) {
0182         mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
0183                             phase, phase, phase);
0184 
0185         regmap_update_bits(afe_priv->topckgen,
0186                    CKSYS_AUD_TOP_CFG, 0x1, 0x1);
0187 
0188         test_done_1 = 0;
0189         test_done_2 = 0;
0190         test_done_3 = 0;
0191         cycle_1 = -1;
0192         cycle_2 = -1;
0193         cycle_3 = -1;
0194         counter = 0;
0195         while (!(test_done_1 & test_done_2 & test_done_3)) {
0196             regmap_read(afe_priv->topckgen,
0197                     CKSYS_AUD_TOP_MON, &monitor);
0198             test_done_1 = (monitor >> 28) & 0x1;
0199             test_done_2 = (monitor >> 29) & 0x1;
0200             test_done_3 = (monitor >> 30) & 0x1;
0201             if (test_done_1 == 1)
0202                 cycle_1 = monitor & 0xf;
0203 
0204             if (test_done_2 == 1)
0205                 cycle_2 = (monitor >> 4) & 0xf;
0206 
0207             if (test_done_3 == 1)
0208                 cycle_3 = (monitor >> 8) & 0xf;
0209 
0210             /* handle if never test done */
0211             if (++counter > 10000) {
0212                 dev_info(afe->dev, "%s(), test fail, cycle_1 %d, cycle_2 %d, cycle_3 %d, monitor 0x%x\n",
0213                      __func__,
0214                      cycle_1, cycle_2, cycle_3, monitor);
0215                 mtkaif_calibration_ok = false;
0216                 break;
0217             }
0218         }
0219 
0220         if (phase == 0) {
0221             prev_cycle_1 = cycle_1;
0222             prev_cycle_2 = cycle_2;
0223             prev_cycle_3 = cycle_3;
0224         }
0225 
0226         if (cycle_1 != prev_cycle_1 &&
0227             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] < 0) {
0228             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] = phase - 1;
0229             mtkaif_phase_cycle[MT8195_MTKAIF_MISO_0] = prev_cycle_1;
0230         }
0231 
0232         if (cycle_2 != prev_cycle_2 &&
0233             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] < 0) {
0234             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] = phase - 1;
0235             mtkaif_phase_cycle[MT8195_MTKAIF_MISO_1] = prev_cycle_2;
0236         }
0237 
0238         if (cycle_3 != prev_cycle_3 &&
0239             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] < 0) {
0240             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] = phase - 1;
0241             mtkaif_phase_cycle[MT8195_MTKAIF_MISO_2] = prev_cycle_3;
0242         }
0243 
0244         regmap_update_bits(afe_priv->topckgen,
0245                    CKSYS_AUD_TOP_CFG, 0x1, 0x0);
0246 
0247         if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] >= 0 &&
0248             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] >= 0 &&
0249             mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] >= 0)
0250             break;
0251     }
0252 
0253     if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] < 0) {
0254         mtkaif_calibration_ok = false;
0255         chosen_phase_1 = 0;
0256     } else {
0257         chosen_phase_1 = mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0];
0258     }
0259 
0260     if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] < 0) {
0261         mtkaif_calibration_ok = false;
0262         chosen_phase_2 = 0;
0263     } else {
0264         chosen_phase_2 = mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1];
0265     }
0266 
0267     if (mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] < 0) {
0268         mtkaif_calibration_ok = false;
0269         chosen_phase_3 = 0;
0270     } else {
0271         chosen_phase_3 = mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2];
0272     }
0273 
0274     mt6359_set_mtkaif_calibration_phase(cmpnt_codec,
0275                         chosen_phase_1,
0276                         chosen_phase_2,
0277                         chosen_phase_3);
0278 
0279     mt6359_mtkaif_calibration_disable(cmpnt_codec);
0280     pm_runtime_put(afe->dev);
0281 
0282     param->mtkaif_calibration_ok = mtkaif_calibration_ok;
0283     param->mtkaif_chosen_phase[MT8195_MTKAIF_MISO_0] = chosen_phase_1;
0284     param->mtkaif_chosen_phase[MT8195_MTKAIF_MISO_1] = chosen_phase_2;
0285     param->mtkaif_chosen_phase[MT8195_MTKAIF_MISO_2] = chosen_phase_3;
0286     for (i = 0; i < MT8195_MTKAIF_MISO_NUM; i++)
0287         param->mtkaif_phase_cycle[i] = mtkaif_phase_cycle[i];
0288 
0289     dev_info(afe->dev, "%s(), end, calibration ok %d\n",
0290          __func__, param->mtkaif_calibration_ok);
0291 
0292     return 0;
0293 }
0294 
0295 static int mt8195_mt6359_init(struct snd_soc_pcm_runtime *rtd)
0296 {
0297     struct snd_soc_component *cmpnt_codec =
0298         asoc_rtd_to_codec(rtd, 0)->component;
0299 
0300     /* set mtkaif protocol */
0301     mt6359_set_mtkaif_protocol(cmpnt_codec,
0302                    MT6359_MTKAIF_PROTOCOL_2_CLK_P2);
0303 
0304     /* mtkaif calibration */
0305     mt8195_mt6359_mtkaif_calibration(rtd);
0306 
0307     return 0;
0308 }
0309 
0310 static int mt8195_hdmitx_dptx_startup(struct snd_pcm_substream *substream)
0311 {
0312     static const unsigned int rates[] = {
0313         48000
0314     };
0315     static const unsigned int channels[] = {
0316         2, 4, 6, 8
0317     };
0318     static const struct snd_pcm_hw_constraint_list constraints_rates = {
0319         .count = ARRAY_SIZE(rates),
0320         .list  = rates,
0321         .mask = 0,
0322     };
0323     static const struct snd_pcm_hw_constraint_list constraints_channels = {
0324         .count = ARRAY_SIZE(channels),
0325         .list  = channels,
0326         .mask = 0,
0327     };
0328 
0329     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0330     struct snd_pcm_runtime *runtime = substream->runtime;
0331     int ret;
0332 
0333     ret = snd_pcm_hw_constraint_list(runtime, 0,
0334                      SNDRV_PCM_HW_PARAM_RATE,
0335                      &constraints_rates);
0336     if (ret < 0) {
0337         dev_err(rtd->dev, "hw_constraint_list rate failed\n");
0338         return ret;
0339     }
0340 
0341     ret = snd_pcm_hw_constraint_list(runtime, 0,
0342                      SNDRV_PCM_HW_PARAM_CHANNELS,
0343                      &constraints_channels);
0344     if (ret < 0) {
0345         dev_err(rtd->dev, "hw_constraint_list channel failed\n");
0346         return ret;
0347     }
0348 
0349     return 0;
0350 }
0351 
0352 static const struct snd_soc_ops mt8195_hdmitx_dptx_playback_ops = {
0353     .startup = mt8195_hdmitx_dptx_startup,
0354 };
0355 
0356 static int mt8195_dptx_hw_params(struct snd_pcm_substream *substream,
0357                  struct snd_pcm_hw_params *params)
0358 {
0359     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0360     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0361 
0362     return snd_soc_dai_set_sysclk(cpu_dai, 0, params_rate(params) * 256,
0363                       SND_SOC_CLOCK_OUT);
0364 }
0365 
0366 static const struct snd_soc_ops mt8195_dptx_ops = {
0367     .hw_params = mt8195_dptx_hw_params,
0368 };
0369 
0370 static int mt8195_dptx_codec_init(struct snd_soc_pcm_runtime *rtd)
0371 {
0372     struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
0373     struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
0374     struct snd_soc_component *cmpnt_codec =
0375         asoc_rtd_to_codec(rtd, 0)->component;
0376     int ret;
0377 
0378     ret = snd_soc_card_jack_new(rtd->card, "DP Jack", SND_JACK_LINEOUT,
0379                     &priv->dp_jack);
0380     if (ret)
0381         return ret;
0382 
0383     return snd_soc_component_set_jack(cmpnt_codec, &priv->dp_jack, NULL);
0384 }
0385 
0386 static int mt8195_hdmi_codec_init(struct snd_soc_pcm_runtime *rtd)
0387 {
0388     struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
0389     struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
0390     struct snd_soc_component *cmpnt_codec =
0391         asoc_rtd_to_codec(rtd, 0)->component;
0392     int ret;
0393 
0394     ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
0395                     &priv->hdmi_jack);
0396     if (ret)
0397         return ret;
0398 
0399     return snd_soc_component_set_jack(cmpnt_codec, &priv->hdmi_jack, NULL);
0400 }
0401 
0402 static int mt8195_dptx_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
0403                        struct snd_pcm_hw_params *params)
0404 {
0405     /* fix BE i2s format to S24_LE, clean param mask first */
0406     snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
0407                  0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
0408 
0409     params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
0410 
0411     return 0;
0412 }
0413 
0414 static int mt8195_playback_startup(struct snd_pcm_substream *substream)
0415 {
0416     static const unsigned int rates[] = {
0417         48000
0418     };
0419     static const unsigned int channels[] = {
0420         2
0421     };
0422     static const struct snd_pcm_hw_constraint_list constraints_rates = {
0423         .count = ARRAY_SIZE(rates),
0424         .list  = rates,
0425         .mask = 0,
0426     };
0427     static const struct snd_pcm_hw_constraint_list constraints_channels = {
0428         .count = ARRAY_SIZE(channels),
0429         .list  = channels,
0430         .mask = 0,
0431     };
0432 
0433     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0434     struct snd_pcm_runtime *runtime = substream->runtime;
0435     int ret;
0436 
0437     ret = snd_pcm_hw_constraint_list(runtime, 0,
0438                      SNDRV_PCM_HW_PARAM_RATE,
0439                      &constraints_rates);
0440     if (ret < 0) {
0441         dev_err(rtd->dev, "hw_constraint_list rate failed\n");
0442         return ret;
0443     }
0444 
0445     ret = snd_pcm_hw_constraint_list(runtime, 0,
0446                      SNDRV_PCM_HW_PARAM_CHANNELS,
0447                      &constraints_channels);
0448     if (ret < 0) {
0449         dev_err(rtd->dev, "hw_constraint_list channel failed\n");
0450         return ret;
0451     }
0452 
0453     return 0;
0454 }
0455 
0456 static const struct snd_soc_ops mt8195_playback_ops = {
0457     .startup = mt8195_playback_startup,
0458 };
0459 
0460 static int mt8195_capture_startup(struct snd_pcm_substream *substream)
0461 {
0462     static const unsigned int rates[] = {
0463         48000
0464     };
0465     static const unsigned int channels[] = {
0466         1, 2
0467     };
0468     static const struct snd_pcm_hw_constraint_list constraints_rates = {
0469         .count = ARRAY_SIZE(rates),
0470         .list  = rates,
0471         .mask = 0,
0472     };
0473     static const struct snd_pcm_hw_constraint_list constraints_channels = {
0474         .count = ARRAY_SIZE(channels),
0475         .list  = channels,
0476         .mask = 0,
0477     };
0478 
0479     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0480     struct snd_pcm_runtime *runtime = substream->runtime;
0481     int ret;
0482 
0483     ret = snd_pcm_hw_constraint_list(runtime, 0,
0484                      SNDRV_PCM_HW_PARAM_RATE,
0485                      &constraints_rates);
0486     if (ret < 0) {
0487         dev_err(rtd->dev, "hw_constraint_list rate failed\n");
0488         return ret;
0489     }
0490 
0491     ret = snd_pcm_hw_constraint_list(runtime, 0,
0492                      SNDRV_PCM_HW_PARAM_CHANNELS,
0493                      &constraints_channels);
0494     if (ret < 0) {
0495         dev_err(rtd->dev, "hw_constraint_list channel failed\n");
0496         return ret;
0497     }
0498 
0499     return 0;
0500 }
0501 
0502 static const struct snd_soc_ops mt8195_capture_ops = {
0503     .startup = mt8195_capture_startup,
0504 };
0505 
0506 static int mt8195_rt5682_etdm_hw_params(struct snd_pcm_substream *substream,
0507                     struct snd_pcm_hw_params *params)
0508 {
0509     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0510     struct snd_soc_card *card = rtd->card;
0511     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0512     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0513     unsigned int rate = params_rate(params);
0514     int bitwidth;
0515     int ret;
0516 
0517     bitwidth = snd_pcm_format_width(params_format(params));
0518     if (bitwidth < 0) {
0519         dev_err(card->dev, "invalid bit width: %d\n", bitwidth);
0520         return bitwidth;
0521     }
0522 
0523     ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x00, 0x0, 0x2, bitwidth);
0524     if (ret) {
0525         dev_err(card->dev, "failed to set tdm slot\n");
0526         return ret;
0527     }
0528 
0529     ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL1, RT5682_PLL1_S_MCLK,
0530                   rate * 256, rate * 512);
0531     if (ret) {
0532         dev_err(card->dev, "failed to set pll\n");
0533         return ret;
0534     }
0535 
0536     ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
0537                      rate * 512, SND_SOC_CLOCK_IN);
0538     if (ret) {
0539         dev_err(card->dev, "failed to set sysclk\n");
0540         return ret;
0541     }
0542 
0543     return snd_soc_dai_set_sysclk(cpu_dai, 0, rate * 256,
0544                       SND_SOC_CLOCK_OUT);
0545 }
0546 
0547 static const struct snd_soc_ops mt8195_rt5682_etdm_ops = {
0548     .hw_params = mt8195_rt5682_etdm_hw_params,
0549 };
0550 
0551 static int mt8195_rt5682_init(struct snd_soc_pcm_runtime *rtd)
0552 {
0553     struct snd_soc_component *cmpnt_codec =
0554         asoc_rtd_to_codec(rtd, 0)->component;
0555     struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(rtd->card);
0556     struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
0557     struct snd_soc_jack *jack = &priv->headset_jack;
0558     struct snd_soc_component *cmpnt_afe =
0559         snd_soc_rtdcom_lookup(rtd, AFE_PCM_NAME);
0560     struct mtk_base_afe *afe = snd_soc_component_get_drvdata(cmpnt_afe);
0561     struct mt8195_afe_private *afe_priv = afe->platform_priv;
0562     int ret;
0563 
0564     priv->i2so1_mclk = afe_priv->clk[MT8195_CLK_TOP_APLL12_DIV2];
0565 
0566     ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
0567                     SND_JACK_HEADSET | SND_JACK_BTN_0 |
0568                     SND_JACK_BTN_1 | SND_JACK_BTN_2 |
0569                     SND_JACK_BTN_3,
0570                     jack);
0571     if (ret) {
0572         dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
0573         return ret;
0574     }
0575 
0576     snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
0577     snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
0578     snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
0579     snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
0580 
0581     ret = snd_soc_component_set_jack(cmpnt_codec, jack, NULL);
0582     if (ret) {
0583         dev_err(rtd->dev, "Headset Jack set failed: %d\n", ret);
0584         return ret;
0585     }
0586 
0587     return 0;
0588 };
0589 
0590 static int mt8195_rt1011_etdm_hw_params(struct snd_pcm_substream *substream,
0591                     struct snd_pcm_hw_params *params)
0592 {
0593     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0594     struct snd_soc_dai *codec_dai;
0595     struct snd_soc_card *card = rtd->card;
0596     int srate, i, ret;
0597 
0598     srate = params_rate(params);
0599 
0600     for_each_rtd_codec_dais(rtd, i, codec_dai) {
0601         ret = snd_soc_dai_set_pll(codec_dai, 0, RT1011_PLL1_S_BCLK,
0602                       64 * srate, 256 * srate);
0603         if (ret < 0) {
0604             dev_err(card->dev, "codec_dai clock not set\n");
0605             return ret;
0606         }
0607 
0608         ret = snd_soc_dai_set_sysclk(codec_dai,
0609                          RT1011_FS_SYS_PRE_S_PLL1,
0610                          256 * srate, SND_SOC_CLOCK_IN);
0611         if (ret < 0) {
0612             dev_err(card->dev, "codec_dai clock not set\n");
0613             return ret;
0614         }
0615     }
0616     return 0;
0617 }
0618 
0619 static const struct snd_soc_ops mt8195_rt1011_etdm_ops = {
0620     .hw_params = mt8195_rt1011_etdm_hw_params,
0621 };
0622 
0623 static int mt8195_rt1011_init(struct snd_soc_pcm_runtime *rtd)
0624 {
0625     struct snd_soc_card *card = rtd->card;
0626     int ret;
0627 
0628     ret = snd_soc_dapm_new_controls(&card->dapm, mt8195_dual_speaker_widgets,
0629                     ARRAY_SIZE(mt8195_dual_speaker_widgets));
0630     if (ret) {
0631         dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
0632         /* Don't need to add routes if widget addition failed */
0633         return ret;
0634     }
0635 
0636     ret = snd_soc_add_card_controls(card, mt8195_dual_speaker_controls,
0637                     ARRAY_SIZE(mt8195_dual_speaker_controls));
0638     if (ret) {
0639         dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
0640         return ret;
0641     }
0642 
0643     ret = snd_soc_dapm_add_routes(&card->dapm, mt8195_rt1011_routes,
0644                       ARRAY_SIZE(mt8195_rt1011_routes));
0645     if (ret)
0646         dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
0647 
0648     return ret;
0649 }
0650 
0651 static int mt8195_rt1019_init(struct snd_soc_pcm_runtime *rtd)
0652 {
0653     struct snd_soc_card *card = rtd->card;
0654     int ret;
0655 
0656     ret = snd_soc_dapm_new_controls(&card->dapm, mt8195_speaker_widgets,
0657                     ARRAY_SIZE(mt8195_speaker_widgets));
0658     if (ret) {
0659         dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
0660         /* Don't need to add routes if widget addition failed */
0661         return ret;
0662     }
0663 
0664     ret = snd_soc_add_card_controls(card, mt8195_speaker_controls,
0665                     ARRAY_SIZE(mt8195_speaker_controls));
0666     if (ret) {
0667         dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
0668         return ret;
0669     }
0670 
0671     ret = snd_soc_dapm_add_routes(&card->dapm, mt8195_rt1019_routes,
0672                       ARRAY_SIZE(mt8195_rt1019_routes));
0673     if (ret)
0674         dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
0675 
0676     return ret;
0677 }
0678 
0679 static int mt8195_max98390_init(struct snd_soc_pcm_runtime *rtd)
0680 {
0681     struct snd_soc_card *card = rtd->card;
0682     int ret;
0683 
0684     ret = snd_soc_dapm_new_controls(&card->dapm, mt8195_dual_speaker_widgets,
0685                     ARRAY_SIZE(mt8195_dual_speaker_widgets));
0686     if (ret) {
0687         dev_err(rtd->dev, "unable to add dapm controls, ret %d\n", ret);
0688         /* Don't need to add routes if widget addition failed */
0689         return ret;
0690     }
0691 
0692     ret = snd_soc_add_card_controls(card, mt8195_dual_speaker_controls,
0693                     ARRAY_SIZE(mt8195_dual_speaker_controls));
0694     if (ret) {
0695         dev_err(rtd->dev, "unable to add card controls, ret %d\n", ret);
0696         return ret;
0697     }
0698 
0699     ret = snd_soc_dapm_add_routes(&card->dapm, mt8195_max98390_routes,
0700                       ARRAY_SIZE(mt8195_max98390_routes));
0701     if (ret)
0702         dev_err(rtd->dev, "unable to add dapm routes, ret %d\n", ret);
0703 
0704     return ret;
0705 }
0706 
0707 static int mt8195_etdm_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
0708                        struct snd_pcm_hw_params *params)
0709 {
0710     /* fix BE i2s format to S24_LE, clean param mask first */
0711     snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
0712                  0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
0713 
0714     params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
0715 
0716     return 0;
0717 }
0718 
0719 static int mt8195_set_bias_level_post(struct snd_soc_card *card,
0720     struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
0721 {
0722     struct snd_soc_component *component = dapm->component;
0723     struct mtk_soc_card_data *soc_card_data = snd_soc_card_get_drvdata(card);
0724     struct mt8195_mt6359_priv *priv = soc_card_data->mach_priv;
0725     int ret;
0726 
0727     /*
0728      * It's required to control mclk directly in the set_bias_level_post
0729      * function for rt5682 and rt5682s codec, or the unexpected pop happens
0730      * at the end of playback.
0731      */
0732     if (!component ||
0733         (strcmp(component->name, RT5682_DEV0_NAME) &&
0734         strcmp(component->name, RT5682S_DEV0_NAME)))
0735         return 0;
0736 
0737     switch (level) {
0738     case SND_SOC_BIAS_OFF:
0739         if (!__clk_is_enabled(priv->i2so1_mclk))
0740             return 0;
0741 
0742         clk_disable_unprepare(priv->i2so1_mclk);
0743         dev_dbg(card->dev, "Disable i2so1 mclk\n");
0744         break;
0745     case SND_SOC_BIAS_ON:
0746         ret = clk_prepare_enable(priv->i2so1_mclk);
0747         if (ret) {
0748             dev_err(card->dev, "Can't enable i2so1 mclk: %d\n", ret);
0749             return ret;
0750         }
0751         dev_dbg(card->dev, "Enable i2so1 mclk\n");
0752         break;
0753     default:
0754         break;
0755     }
0756 
0757     return 0;
0758 }
0759 
0760 enum {
0761     DAI_LINK_DL2_FE,
0762     DAI_LINK_DL3_FE,
0763     DAI_LINK_DL6_FE,
0764     DAI_LINK_DL7_FE,
0765     DAI_LINK_DL8_FE,
0766     DAI_LINK_DL10_FE,
0767     DAI_LINK_DL11_FE,
0768     DAI_LINK_UL1_FE,
0769     DAI_LINK_UL2_FE,
0770     DAI_LINK_UL3_FE,
0771     DAI_LINK_UL4_FE,
0772     DAI_LINK_UL5_FE,
0773     DAI_LINK_UL6_FE,
0774     DAI_LINK_UL8_FE,
0775     DAI_LINK_UL9_FE,
0776     DAI_LINK_UL10_FE,
0777     DAI_LINK_DL_SRC_BE,
0778     DAI_LINK_DPTX_BE,
0779     DAI_LINK_ETDM1_IN_BE,
0780     DAI_LINK_ETDM2_IN_BE,
0781     DAI_LINK_ETDM1_OUT_BE,
0782     DAI_LINK_ETDM2_OUT_BE,
0783     DAI_LINK_ETDM3_OUT_BE,
0784     DAI_LINK_PCM1_BE,
0785     DAI_LINK_UL_SRC1_BE,
0786     DAI_LINK_UL_SRC2_BE,
0787     DAI_LINK_REGULAR_LAST = DAI_LINK_UL_SRC2_BE,
0788     DAI_LINK_SOF_START,
0789     DAI_LINK_SOF_DL2_BE = DAI_LINK_SOF_START,
0790     DAI_LINK_SOF_DL3_BE,
0791     DAI_LINK_SOF_UL4_BE,
0792     DAI_LINK_SOF_UL5_BE,
0793     DAI_LINK_SOF_END = DAI_LINK_SOF_UL5_BE,
0794 };
0795 
0796 #define DAI_LINK_REGULAR_NUM    (DAI_LINK_REGULAR_LAST + 1)
0797 
0798 /* FE */
0799 SND_SOC_DAILINK_DEFS(DL2_FE,
0800              DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
0801              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0802              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0803 
0804 SND_SOC_DAILINK_DEFS(DL3_FE,
0805              DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
0806              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0807              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0808 
0809 SND_SOC_DAILINK_DEFS(DL6_FE,
0810              DAILINK_COMP_ARRAY(COMP_CPU("DL6")),
0811              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0812              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0813 
0814 SND_SOC_DAILINK_DEFS(DL7_FE,
0815              DAILINK_COMP_ARRAY(COMP_CPU("DL7")),
0816              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0817              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0818 
0819 SND_SOC_DAILINK_DEFS(DL8_FE,
0820              DAILINK_COMP_ARRAY(COMP_CPU("DL8")),
0821              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0822              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0823 
0824 SND_SOC_DAILINK_DEFS(DL10_FE,
0825              DAILINK_COMP_ARRAY(COMP_CPU("DL10")),
0826              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0827              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0828 
0829 SND_SOC_DAILINK_DEFS(DL11_FE,
0830              DAILINK_COMP_ARRAY(COMP_CPU("DL11")),
0831              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0832              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0833 
0834 SND_SOC_DAILINK_DEFS(UL1_FE,
0835              DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
0836              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0837              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0838 
0839 SND_SOC_DAILINK_DEFS(UL2_FE,
0840              DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
0841              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0842              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0843 
0844 SND_SOC_DAILINK_DEFS(UL3_FE,
0845              DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
0846              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0847              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0848 
0849 SND_SOC_DAILINK_DEFS(UL4_FE,
0850              DAILINK_COMP_ARRAY(COMP_CPU("UL4")),
0851              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0852              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0853 
0854 SND_SOC_DAILINK_DEFS(UL5_FE,
0855              DAILINK_COMP_ARRAY(COMP_CPU("UL5")),
0856              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0857              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0858 
0859 SND_SOC_DAILINK_DEFS(UL6_FE,
0860              DAILINK_COMP_ARRAY(COMP_CPU("UL6")),
0861              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0862              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0863 
0864 SND_SOC_DAILINK_DEFS(UL8_FE,
0865              DAILINK_COMP_ARRAY(COMP_CPU("UL8")),
0866              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0867              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0868 
0869 SND_SOC_DAILINK_DEFS(UL9_FE,
0870              DAILINK_COMP_ARRAY(COMP_CPU("UL9")),
0871              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0872              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0873 
0874 SND_SOC_DAILINK_DEFS(UL10_FE,
0875              DAILINK_COMP_ARRAY(COMP_CPU("UL10")),
0876              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0877              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0878 
0879 /* BE */
0880 SND_SOC_DAILINK_DEFS(DL_SRC_BE,
0881              DAILINK_COMP_ARRAY(COMP_CPU("DL_SRC")),
0882              DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
0883                            "mt6359-snd-codec-aif1")),
0884              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0885 
0886 SND_SOC_DAILINK_DEFS(DPTX_BE,
0887              DAILINK_COMP_ARRAY(COMP_CPU("DPTX")),
0888              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0889              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0890 
0891 SND_SOC_DAILINK_DEFS(ETDM1_IN_BE,
0892              DAILINK_COMP_ARRAY(COMP_CPU("ETDM1_IN")),
0893              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0894              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0895 
0896 SND_SOC_DAILINK_DEFS(ETDM2_IN_BE,
0897              DAILINK_COMP_ARRAY(COMP_CPU("ETDM2_IN")),
0898              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0899              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0900 
0901 SND_SOC_DAILINK_DEFS(ETDM1_OUT_BE,
0902              DAILINK_COMP_ARRAY(COMP_CPU("ETDM1_OUT")),
0903              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0904              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0905 
0906 SND_SOC_DAILINK_DEFS(ETDM2_OUT_BE,
0907              DAILINK_COMP_ARRAY(COMP_CPU("ETDM2_OUT")),
0908              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0909              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0910 
0911 SND_SOC_DAILINK_DEFS(ETDM3_OUT_BE,
0912              DAILINK_COMP_ARRAY(COMP_CPU("ETDM3_OUT")),
0913              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0914              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0915 
0916 SND_SOC_DAILINK_DEFS(PCM1_BE,
0917              DAILINK_COMP_ARRAY(COMP_CPU("PCM1")),
0918              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0919              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0920 
0921 SND_SOC_DAILINK_DEFS(UL_SRC1_BE,
0922              DAILINK_COMP_ARRAY(COMP_CPU("UL_SRC1")),
0923              DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
0924                            "mt6359-snd-codec-aif1"),
0925                     COMP_CODEC("dmic-codec",
0926                            "dmic-hifi")),
0927              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0928 
0929 SND_SOC_DAILINK_DEFS(UL_SRC2_BE,
0930              DAILINK_COMP_ARRAY(COMP_CPU("UL_SRC2")),
0931              DAILINK_COMP_ARRAY(COMP_CODEC("mt6359-sound",
0932                            "mt6359-snd-codec-aif2")),
0933              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0934 
0935 SND_SOC_DAILINK_DEFS(AFE_SOF_DL2,
0936              DAILINK_COMP_ARRAY(COMP_CPU("SOF_DL2")),
0937              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0938              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0939 
0940 SND_SOC_DAILINK_DEFS(AFE_SOF_DL3,
0941              DAILINK_COMP_ARRAY(COMP_CPU("SOF_DL3")),
0942              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0943              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0944 
0945 SND_SOC_DAILINK_DEFS(AFE_SOF_UL4,
0946              DAILINK_COMP_ARRAY(COMP_CPU("SOF_UL4")),
0947              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0948              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0949 
0950 SND_SOC_DAILINK_DEFS(AFE_SOF_UL5,
0951              DAILINK_COMP_ARRAY(COMP_CPU("SOF_UL5")),
0952              DAILINK_COMP_ARRAY(COMP_DUMMY()),
0953              DAILINK_COMP_ARRAY(COMP_EMPTY()));
0954 
0955 /* codec */
0956 SND_SOC_DAILINK_DEF(rt1019_comps,
0957             DAILINK_COMP_ARRAY(COMP_CODEC(RT1019_DEV0_NAME,
0958                           RT1019_CODEC_DAI)));
0959 
0960 SND_SOC_DAILINK_DEF(rt1011_comps,
0961             DAILINK_COMP_ARRAY(COMP_CODEC(RT1011_DEV0_NAME,
0962                           RT1011_CODEC_DAI),
0963                        COMP_CODEC(RT1011_DEV1_NAME,
0964                           RT1011_CODEC_DAI)));
0965 
0966 SND_SOC_DAILINK_DEF(max98390_comps,
0967             DAILINK_COMP_ARRAY(COMP_CODEC(MAX98390_DEV0_NAME,
0968                           MAX98390_CODEC_DAI),
0969                        COMP_CODEC(MAX98390_DEV1_NAME,
0970                           MAX98390_CODEC_DAI)));
0971 
0972 static const struct sof_conn_stream g_sof_conn_streams[] = {
0973     { "ETDM2_OUT_BE", "AFE_SOF_DL2", SOF_DMA_DL2, SNDRV_PCM_STREAM_PLAYBACK},
0974     { "ETDM1_OUT_BE", "AFE_SOF_DL3", SOF_DMA_DL3, SNDRV_PCM_STREAM_PLAYBACK},
0975     { "UL_SRC1_BE", "AFE_SOF_UL4", SOF_DMA_UL4, SNDRV_PCM_STREAM_CAPTURE},
0976     { "ETDM2_IN_BE", "AFE_SOF_UL5", SOF_DMA_UL5, SNDRV_PCM_STREAM_CAPTURE},
0977 };
0978 
0979 static struct snd_soc_dai_link mt8195_mt6359_dai_links[] = {
0980     /* FE */
0981     [DAI_LINK_DL2_FE] = {
0982         .name = "DL2_FE",
0983         .stream_name = "DL2 Playback",
0984         .trigger = {
0985             SND_SOC_DPCM_TRIGGER_POST,
0986             SND_SOC_DPCM_TRIGGER_POST,
0987         },
0988         .dynamic = 1,
0989         .dpcm_playback = 1,
0990         .ops = &mt8195_playback_ops,
0991         SND_SOC_DAILINK_REG(DL2_FE),
0992     },
0993     [DAI_LINK_DL3_FE] = {
0994         .name = "DL3_FE",
0995         .stream_name = "DL3 Playback",
0996         .trigger = {
0997             SND_SOC_DPCM_TRIGGER_POST,
0998             SND_SOC_DPCM_TRIGGER_POST,
0999         },
1000         .dynamic = 1,
1001         .dpcm_playback = 1,
1002         .ops = &mt8195_playback_ops,
1003         SND_SOC_DAILINK_REG(DL3_FE),
1004     },
1005     [DAI_LINK_DL6_FE] = {
1006         .name = "DL6_FE",
1007         .stream_name = "DL6 Playback",
1008         .trigger = {
1009             SND_SOC_DPCM_TRIGGER_POST,
1010             SND_SOC_DPCM_TRIGGER_POST,
1011         },
1012         .dynamic = 1,
1013         .dpcm_playback = 1,
1014         .ops = &mt8195_playback_ops,
1015         SND_SOC_DAILINK_REG(DL6_FE),
1016     },
1017     [DAI_LINK_DL7_FE] = {
1018         .name = "DL7_FE",
1019         .stream_name = "DL7 Playback",
1020         .trigger = {
1021             SND_SOC_DPCM_TRIGGER_PRE,
1022             SND_SOC_DPCM_TRIGGER_PRE,
1023         },
1024         .dynamic = 1,
1025         .dpcm_playback = 1,
1026         SND_SOC_DAILINK_REG(DL7_FE),
1027     },
1028     [DAI_LINK_DL8_FE] = {
1029         .name = "DL8_FE",
1030         .stream_name = "DL8 Playback",
1031         .trigger = {
1032             SND_SOC_DPCM_TRIGGER_POST,
1033             SND_SOC_DPCM_TRIGGER_POST,
1034         },
1035         .dynamic = 1,
1036         .dpcm_playback = 1,
1037         .ops = &mt8195_playback_ops,
1038         SND_SOC_DAILINK_REG(DL8_FE),
1039     },
1040     [DAI_LINK_DL10_FE] = {
1041         .name = "DL10_FE",
1042         .stream_name = "DL10 Playback",
1043         .trigger = {
1044             SND_SOC_DPCM_TRIGGER_POST,
1045             SND_SOC_DPCM_TRIGGER_POST,
1046         },
1047         .dynamic = 1,
1048         .dpcm_playback = 1,
1049         .ops = &mt8195_hdmitx_dptx_playback_ops,
1050         SND_SOC_DAILINK_REG(DL10_FE),
1051     },
1052     [DAI_LINK_DL11_FE] = {
1053         .name = "DL11_FE",
1054         .stream_name = "DL11 Playback",
1055         .trigger = {
1056             SND_SOC_DPCM_TRIGGER_POST,
1057             SND_SOC_DPCM_TRIGGER_POST,
1058         },
1059         .dynamic = 1,
1060         .dpcm_playback = 1,
1061         .ops = &mt8195_playback_ops,
1062         SND_SOC_DAILINK_REG(DL11_FE),
1063     },
1064     [DAI_LINK_UL1_FE] = {
1065         .name = "UL1_FE",
1066         .stream_name = "UL1 Capture",
1067         .trigger = {
1068             SND_SOC_DPCM_TRIGGER_PRE,
1069             SND_SOC_DPCM_TRIGGER_PRE,
1070         },
1071         .dynamic = 1,
1072         .dpcm_capture = 1,
1073         SND_SOC_DAILINK_REG(UL1_FE),
1074     },
1075     [DAI_LINK_UL2_FE] = {
1076         .name = "UL2_FE",
1077         .stream_name = "UL2 Capture",
1078         .trigger = {
1079             SND_SOC_DPCM_TRIGGER_POST,
1080             SND_SOC_DPCM_TRIGGER_POST,
1081         },
1082         .dynamic = 1,
1083         .dpcm_capture = 1,
1084         .ops = &mt8195_capture_ops,
1085         SND_SOC_DAILINK_REG(UL2_FE),
1086     },
1087     [DAI_LINK_UL3_FE] = {
1088         .name = "UL3_FE",
1089         .stream_name = "UL3 Capture",
1090         .trigger = {
1091             SND_SOC_DPCM_TRIGGER_POST,
1092             SND_SOC_DPCM_TRIGGER_POST,
1093         },
1094         .dynamic = 1,
1095         .dpcm_capture = 1,
1096         .ops = &mt8195_capture_ops,
1097         SND_SOC_DAILINK_REG(UL3_FE),
1098     },
1099     [DAI_LINK_UL4_FE] = {
1100         .name = "UL4_FE",
1101         .stream_name = "UL4 Capture",
1102         .trigger = {
1103             SND_SOC_DPCM_TRIGGER_POST,
1104             SND_SOC_DPCM_TRIGGER_POST,
1105         },
1106         .dynamic = 1,
1107         .dpcm_capture = 1,
1108         .ops = &mt8195_capture_ops,
1109         SND_SOC_DAILINK_REG(UL4_FE),
1110     },
1111     [DAI_LINK_UL5_FE] = {
1112         .name = "UL5_FE",
1113         .stream_name = "UL5 Capture",
1114         .trigger = {
1115             SND_SOC_DPCM_TRIGGER_POST,
1116             SND_SOC_DPCM_TRIGGER_POST,
1117         },
1118         .dynamic = 1,
1119         .dpcm_capture = 1,
1120         .ops = &mt8195_capture_ops,
1121         SND_SOC_DAILINK_REG(UL5_FE),
1122     },
1123     [DAI_LINK_UL6_FE] = {
1124         .name = "UL6_FE",
1125         .stream_name = "UL6 Capture",
1126         .trigger = {
1127             SND_SOC_DPCM_TRIGGER_PRE,
1128             SND_SOC_DPCM_TRIGGER_PRE,
1129         },
1130         .dynamic = 1,
1131         .dpcm_capture = 1,
1132         SND_SOC_DAILINK_REG(UL6_FE),
1133     },
1134     [DAI_LINK_UL8_FE] = {
1135         .name = "UL8_FE",
1136         .stream_name = "UL8 Capture",
1137         .trigger = {
1138             SND_SOC_DPCM_TRIGGER_POST,
1139             SND_SOC_DPCM_TRIGGER_POST,
1140         },
1141         .dynamic = 1,
1142         .dpcm_capture = 1,
1143         .ops = &mt8195_capture_ops,
1144         SND_SOC_DAILINK_REG(UL8_FE),
1145     },
1146     [DAI_LINK_UL9_FE] = {
1147         .name = "UL9_FE",
1148         .stream_name = "UL9 Capture",
1149         .trigger = {
1150             SND_SOC_DPCM_TRIGGER_POST,
1151             SND_SOC_DPCM_TRIGGER_POST,
1152         },
1153         .dynamic = 1,
1154         .dpcm_capture = 1,
1155         .ops = &mt8195_capture_ops,
1156         SND_SOC_DAILINK_REG(UL9_FE),
1157     },
1158     [DAI_LINK_UL10_FE] = {
1159         .name = "UL10_FE",
1160         .stream_name = "UL10 Capture",
1161         .trigger = {
1162             SND_SOC_DPCM_TRIGGER_POST,
1163             SND_SOC_DPCM_TRIGGER_POST,
1164         },
1165         .dynamic = 1,
1166         .dpcm_capture = 1,
1167         .ops = &mt8195_capture_ops,
1168         SND_SOC_DAILINK_REG(UL10_FE),
1169     },
1170     /* BE */
1171     [DAI_LINK_DL_SRC_BE] = {
1172         .name = "DL_SRC_BE",
1173         .no_pcm = 1,
1174         .dpcm_playback = 1,
1175         SND_SOC_DAILINK_REG(DL_SRC_BE),
1176     },
1177     [DAI_LINK_DPTX_BE] = {
1178         .name = "DPTX_BE",
1179         .no_pcm = 1,
1180         .dpcm_playback = 1,
1181         .ops = &mt8195_dptx_ops,
1182         .be_hw_params_fixup = mt8195_dptx_hw_params_fixup,
1183         SND_SOC_DAILINK_REG(DPTX_BE),
1184     },
1185     [DAI_LINK_ETDM1_IN_BE] = {
1186         .name = "ETDM1_IN_BE",
1187         .no_pcm = 1,
1188         .dai_fmt = SND_SOC_DAIFMT_I2S |
1189             SND_SOC_DAIFMT_NB_NF |
1190             SND_SOC_DAIFMT_CBS_CFS,
1191         .dpcm_capture = 1,
1192         SND_SOC_DAILINK_REG(ETDM1_IN_BE),
1193     },
1194     [DAI_LINK_ETDM2_IN_BE] = {
1195         .name = "ETDM2_IN_BE",
1196         .no_pcm = 1,
1197         .dai_fmt = SND_SOC_DAIFMT_I2S |
1198             SND_SOC_DAIFMT_NB_NF |
1199             SND_SOC_DAIFMT_CBS_CFS,
1200         .dpcm_capture = 1,
1201         .init = mt8195_rt5682_init,
1202         .ops = &mt8195_rt5682_etdm_ops,
1203         .be_hw_params_fixup = mt8195_etdm_hw_params_fixup,
1204         SND_SOC_DAILINK_REG(ETDM2_IN_BE),
1205     },
1206     [DAI_LINK_ETDM1_OUT_BE] = {
1207         .name = "ETDM1_OUT_BE",
1208         .no_pcm = 1,
1209         .dai_fmt = SND_SOC_DAIFMT_I2S |
1210             SND_SOC_DAIFMT_NB_NF |
1211             SND_SOC_DAIFMT_CBS_CFS,
1212         .dpcm_playback = 1,
1213         .ops = &mt8195_rt5682_etdm_ops,
1214         .be_hw_params_fixup = mt8195_etdm_hw_params_fixup,
1215         SND_SOC_DAILINK_REG(ETDM1_OUT_BE),
1216     },
1217     [DAI_LINK_ETDM2_OUT_BE] = {
1218         .name = "ETDM2_OUT_BE",
1219         .no_pcm = 1,
1220         .dai_fmt = SND_SOC_DAIFMT_I2S |
1221             SND_SOC_DAIFMT_NB_NF |
1222             SND_SOC_DAIFMT_CBS_CFS,
1223         .dpcm_playback = 1,
1224         SND_SOC_DAILINK_REG(ETDM2_OUT_BE),
1225     },
1226     [DAI_LINK_ETDM3_OUT_BE] = {
1227         .name = "ETDM3_OUT_BE",
1228         .no_pcm = 1,
1229         .dai_fmt = SND_SOC_DAIFMT_I2S |
1230             SND_SOC_DAIFMT_NB_NF |
1231             SND_SOC_DAIFMT_CBS_CFS,
1232         .dpcm_playback = 1,
1233         SND_SOC_DAILINK_REG(ETDM3_OUT_BE),
1234     },
1235     [DAI_LINK_PCM1_BE] = {
1236         .name = "PCM1_BE",
1237         .no_pcm = 1,
1238         .dai_fmt = SND_SOC_DAIFMT_I2S |
1239             SND_SOC_DAIFMT_NB_NF |
1240             SND_SOC_DAIFMT_CBS_CFS,
1241         .dpcm_playback = 1,
1242         .dpcm_capture = 1,
1243         SND_SOC_DAILINK_REG(PCM1_BE),
1244     },
1245     [DAI_LINK_UL_SRC1_BE] = {
1246         .name = "UL_SRC1_BE",
1247         .no_pcm = 1,
1248         .dpcm_capture = 1,
1249         SND_SOC_DAILINK_REG(UL_SRC1_BE),
1250     },
1251     [DAI_LINK_UL_SRC2_BE] = {
1252         .name = "UL_SRC2_BE",
1253         .no_pcm = 1,
1254         .dpcm_capture = 1,
1255         SND_SOC_DAILINK_REG(UL_SRC2_BE),
1256     },
1257     /* SOF BE */
1258     [DAI_LINK_SOF_DL2_BE] = {
1259         .name = "AFE_SOF_DL2",
1260         .no_pcm = 1,
1261         .dpcm_playback = 1,
1262         SND_SOC_DAILINK_REG(AFE_SOF_DL2),
1263     },
1264     [DAI_LINK_SOF_DL3_BE] = {
1265         .name = "AFE_SOF_DL3",
1266         .no_pcm = 1,
1267         .dpcm_playback = 1,
1268         SND_SOC_DAILINK_REG(AFE_SOF_DL3),
1269     },
1270     [DAI_LINK_SOF_UL4_BE] = {
1271         .name = "AFE_SOF_UL4",
1272         .no_pcm = 1,
1273         .dpcm_capture = 1,
1274         SND_SOC_DAILINK_REG(AFE_SOF_UL4),
1275     },
1276     [DAI_LINK_SOF_UL5_BE] = {
1277         .name = "AFE_SOF_UL5",
1278         .no_pcm = 1,
1279         .dpcm_capture = 1,
1280         SND_SOC_DAILINK_REG(AFE_SOF_UL5),
1281     },
1282 };
1283 
1284 static struct snd_soc_codec_conf rt1011_codec_conf[] = {
1285     {
1286         .dlc = COMP_CODEC_CONF(RT1011_DEV0_NAME),
1287         .name_prefix = "Left",
1288     },
1289     {
1290         .dlc = COMP_CODEC_CONF(RT1011_DEV1_NAME),
1291         .name_prefix = "Right",
1292     },
1293 };
1294 
1295 static struct snd_soc_codec_conf max98390_codec_conf[] = {
1296     {
1297         .dlc = COMP_CODEC_CONF(MAX98390_DEV0_NAME),
1298         .name_prefix = "Right",
1299     },
1300     {
1301         .dlc = COMP_CODEC_CONF(MAX98390_DEV1_NAME),
1302         .name_prefix = "Left",
1303     },
1304 };
1305 
1306 static struct snd_soc_card mt8195_mt6359_soc_card = {
1307     .owner = THIS_MODULE,
1308     .dai_link = mt8195_mt6359_dai_links,
1309     .num_links = ARRAY_SIZE(mt8195_mt6359_dai_links),
1310     .controls = mt8195_mt6359_controls,
1311     .num_controls = ARRAY_SIZE(mt8195_mt6359_controls),
1312     .dapm_widgets = mt8195_mt6359_widgets,
1313     .num_dapm_widgets = ARRAY_SIZE(mt8195_mt6359_widgets),
1314     .dapm_routes = mt8195_mt6359_routes,
1315     .num_dapm_routes = ARRAY_SIZE(mt8195_mt6359_routes),
1316     .set_bias_level_post = mt8195_set_bias_level_post,
1317 };
1318 
1319 /* fixup the BE DAI link to match any values from topology */
1320 static int mt8195_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
1321                  struct snd_pcm_hw_params *params)
1322 {
1323     int ret;
1324 
1325     ret = mtk_sof_dai_link_fixup(rtd, params);
1326 
1327     if (!strcmp(rtd->dai_link->name, "ETDM2_IN_BE") ||
1328         !strcmp(rtd->dai_link->name, "ETDM1_OUT_BE")) {
1329         mt8195_etdm_hw_params_fixup(rtd, params);
1330     }
1331 
1332     return ret;
1333 }
1334 
1335 static int mt8195_mt6359_dev_probe(struct platform_device *pdev)
1336 {
1337     struct snd_soc_card *card = &mt8195_mt6359_soc_card;
1338     struct snd_soc_dai_link *dai_link;
1339     struct mtk_soc_card_data *soc_card_data;
1340     struct mt8195_mt6359_priv *mach_priv;
1341     struct device_node *platform_node, *adsp_node, *dp_node, *hdmi_node;
1342     struct mt8195_card_data *card_data;
1343     int is5682s = 0;
1344     int init6359 = 0;
1345     int sof_on = 0;
1346     int ret, i;
1347 
1348     card_data = (struct mt8195_card_data *)of_device_get_match_data(&pdev->dev);
1349     card->dev = &pdev->dev;
1350 
1351     ret = snd_soc_of_parse_card_name(card, "model");
1352     if (ret) {
1353         dev_err(&pdev->dev, "%s new card name parsing error %d\n",
1354             __func__, ret);
1355         return ret;
1356     }
1357 
1358     if (!card->name)
1359         card->name = card_data->name;
1360 
1361     if (strstr(card->name, "_5682s"))
1362         is5682s = 1;
1363     soc_card_data = devm_kzalloc(&pdev->dev, sizeof(*card_data), GFP_KERNEL);
1364     if (!soc_card_data)
1365         return -ENOMEM;
1366 
1367     mach_priv = devm_kzalloc(&pdev->dev, sizeof(*mach_priv), GFP_KERNEL);
1368     if (!mach_priv)
1369         return -ENOMEM;
1370 
1371     soc_card_data->mach_priv = mach_priv;
1372 
1373     adsp_node = of_parse_phandle(pdev->dev.of_node, "mediatek,adsp", 0);
1374     if (adsp_node) {
1375         struct mtk_sof_priv *sof_priv;
1376 
1377         sof_priv = devm_kzalloc(&pdev->dev, sizeof(*sof_priv), GFP_KERNEL);
1378         if (!sof_priv) {
1379             ret = -ENOMEM;
1380             goto err_kzalloc;
1381         }
1382         sof_priv->conn_streams = g_sof_conn_streams;
1383         sof_priv->num_streams = ARRAY_SIZE(g_sof_conn_streams);
1384         sof_priv->sof_dai_link_fixup = mt8195_dai_link_fixup;
1385         soc_card_data->sof_priv = sof_priv;
1386         card->late_probe = mtk_sof_card_late_probe;
1387         sof_on = 1;
1388     }
1389 
1390     if (of_property_read_bool(pdev->dev.of_node, "mediatek,dai-link")) {
1391         ret = mtk_sof_dailink_parse_of(card, pdev->dev.of_node,
1392                            "mediatek,dai-link",
1393                            mt8195_mt6359_dai_links,
1394                            ARRAY_SIZE(mt8195_mt6359_dai_links));
1395         if (ret) {
1396             dev_dbg(&pdev->dev, "Parse dai-link fail\n");
1397             goto err_parse_of;
1398         }
1399     } else {
1400         if (!sof_on)
1401             card->num_links = DAI_LINK_REGULAR_NUM;
1402     }
1403 
1404     platform_node = of_parse_phandle(pdev->dev.of_node,
1405                      "mediatek,platform", 0);
1406     if (!platform_node) {
1407         dev_dbg(&pdev->dev, "Property 'platform' missing or invalid\n");
1408         ret = -EINVAL;
1409         goto err_platform_node;
1410     }
1411 
1412     dp_node = of_parse_phandle(pdev->dev.of_node, "mediatek,dptx-codec", 0);
1413     hdmi_node = of_parse_phandle(pdev->dev.of_node,
1414                      "mediatek,hdmi-codec", 0);
1415 
1416     for_each_card_prelinks(card, i, dai_link) {
1417         if (!dai_link->platforms->name) {
1418             if (!strncmp(dai_link->name, "AFE_SOF", strlen("AFE_SOF")) && sof_on)
1419                 dai_link->platforms->of_node = adsp_node;
1420             else
1421                 dai_link->platforms->of_node = platform_node;
1422         }
1423 
1424         if (strcmp(dai_link->name, "DPTX_BE") == 0) {
1425             if (!dp_node) {
1426                 dev_dbg(&pdev->dev, "No property 'dptx-codec'\n");
1427             } else {
1428                 dai_link->codecs->of_node = dp_node;
1429                 dai_link->codecs->name = NULL;
1430                 dai_link->codecs->dai_name = "i2s-hifi";
1431                 dai_link->init = mt8195_dptx_codec_init;
1432             }
1433         } else if (strcmp(dai_link->name, "ETDM3_OUT_BE") == 0) {
1434             if (!hdmi_node) {
1435                 dev_dbg(&pdev->dev, "No property 'hdmi-codec'\n");
1436             } else {
1437                 dai_link->codecs->of_node = hdmi_node;
1438                 dai_link->codecs->name = NULL;
1439                 dai_link->codecs->dai_name = "i2s-hifi";
1440                 dai_link->init = mt8195_hdmi_codec_init;
1441             }
1442         } else if (strcmp(dai_link->name, "ETDM1_OUT_BE") == 0 ||
1443                strcmp(dai_link->name, "ETDM2_IN_BE") == 0) {
1444             dai_link->codecs->name =
1445                 is5682s ? RT5682S_DEV0_NAME : RT5682_DEV0_NAME;
1446             dai_link->codecs->dai_name =
1447                 is5682s ? RT5682S_CODEC_DAI : RT5682_CODEC_DAI;
1448         } else if (strcmp(dai_link->name, "DL_SRC_BE") == 0 ||
1449                strcmp(dai_link->name, "UL_SRC1_BE") == 0 ||
1450                strcmp(dai_link->name, "UL_SRC2_BE") == 0) {
1451             if (!init6359) {
1452                 dai_link->init = mt8195_mt6359_init;
1453                 init6359 = 1;
1454             }
1455         } else if (strcmp(dai_link->name, "ETDM2_OUT_BE") == 0) {
1456             switch (card_data->quirk) {
1457             case RT1011_SPEAKER_AMP_PRESENT:
1458                 dai_link->codecs = rt1011_comps;
1459                 dai_link->num_codecs = ARRAY_SIZE(rt1011_comps);
1460                 dai_link->init = mt8195_rt1011_init;
1461                 dai_link->ops = &mt8195_rt1011_etdm_ops;
1462                 dai_link->be_hw_params_fixup = mt8195_etdm_hw_params_fixup;
1463                 card->codec_conf = rt1011_codec_conf;
1464                 card->num_configs = ARRAY_SIZE(rt1011_codec_conf);
1465                 break;
1466             case RT1019_SPEAKER_AMP_PRESENT:
1467                 dai_link->codecs = rt1019_comps;
1468                 dai_link->num_codecs = ARRAY_SIZE(rt1019_comps);
1469                 dai_link->init = mt8195_rt1019_init;
1470                 break;
1471             case MAX98390_SPEAKER_AMP_PRESENT:
1472                 dai_link->codecs = max98390_comps;
1473                 dai_link->num_codecs = ARRAY_SIZE(max98390_comps);
1474                 dai_link->init = mt8195_max98390_init;
1475                 card->codec_conf = max98390_codec_conf;
1476                 card->num_configs = ARRAY_SIZE(max98390_codec_conf);
1477                 break;
1478             default:
1479                 break;
1480             }
1481         }
1482     }
1483 
1484     snd_soc_card_set_drvdata(card, soc_card_data);
1485 
1486     ret = devm_snd_soc_register_card(&pdev->dev, card);
1487 
1488     of_node_put(platform_node);
1489     of_node_put(dp_node);
1490     of_node_put(hdmi_node);
1491 err_kzalloc:
1492 err_parse_of:
1493 err_platform_node:
1494     of_node_put(adsp_node);
1495     return ret;
1496 }
1497 
1498 static struct mt8195_card_data mt8195_mt6359_rt1019_rt5682_card = {
1499     .name = "mt8195_r1019_5682",
1500     .quirk = RT1019_SPEAKER_AMP_PRESENT,
1501 };
1502 
1503 static struct mt8195_card_data mt8195_mt6359_rt1011_rt5682_card = {
1504     .name = "mt8195_r1011_5682",
1505     .quirk = RT1011_SPEAKER_AMP_PRESENT,
1506 };
1507 
1508 static struct mt8195_card_data mt8195_mt6359_max98390_rt5682_card = {
1509     .name = "mt8195_m98390_r5682",
1510     .quirk = MAX98390_SPEAKER_AMP_PRESENT,
1511 };
1512 
1513 static const struct of_device_id mt8195_mt6359_dt_match[] = {
1514     {
1515         .compatible = "mediatek,mt8195_mt6359_rt1019_rt5682",
1516         .data = &mt8195_mt6359_rt1019_rt5682_card,
1517     },
1518     {
1519         .compatible = "mediatek,mt8195_mt6359_rt1011_rt5682",
1520         .data = &mt8195_mt6359_rt1011_rt5682_card,
1521     },
1522     {
1523         .compatible = "mediatek,mt8195_mt6359_max98390_rt5682",
1524         .data = &mt8195_mt6359_max98390_rt5682_card,
1525     },
1526     {},
1527 };
1528 
1529 static const struct dev_pm_ops mt8195_mt6359_pm_ops = {
1530     .poweroff = snd_soc_poweroff,
1531     .restore = snd_soc_resume,
1532 };
1533 
1534 static struct platform_driver mt8195_mt6359_driver = {
1535     .driver = {
1536         .name = "mt8195_mt6359",
1537         .of_match_table = mt8195_mt6359_dt_match,
1538         .pm = &mt8195_mt6359_pm_ops,
1539     },
1540     .probe = mt8195_mt6359_dev_probe,
1541 };
1542 
1543 module_platform_driver(mt8195_mt6359_driver);
1544 
1545 /* Module information */
1546 MODULE_DESCRIPTION("MT8195-MT6359 ALSA SoC machine driver");
1547 MODULE_AUTHOR("Trevor Wu <trevor.wu@mediatek.com>");
1548 MODULE_AUTHOR("YC Hung <yc.hung@mediatek.com>");
1549 MODULE_LICENSE("GPL");
1550 MODULE_ALIAS("mt8195_mt6359 soc card");