Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // mt8183-mt6358.c  --
0004 //  MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver
0005 //
0006 // Copyright (c) 2018 MediaTek Inc.
0007 // Author: Shunli Wang <shunli.wang@mediatek.com>
0008 
0009 #include <linux/module.h>
0010 #include <linux/of_device.h>
0011 #include <linux/pinctrl/consumer.h>
0012 #include <sound/jack.h>
0013 #include <sound/pcm_params.h>
0014 #include <sound/soc.h>
0015 
0016 #include "../../codecs/rt1015.h"
0017 #include "../../codecs/ts3a227e.h"
0018 #include "mt8183-afe-common.h"
0019 
0020 #define RT1015_CODEC_DAI "rt1015-aif"
0021 #define RT1015_DEV0_NAME "rt1015.6-0028"
0022 #define RT1015_DEV1_NAME "rt1015.6-0029"
0023 
0024 enum PINCTRL_PIN_STATE {
0025     PIN_STATE_DEFAULT = 0,
0026     PIN_TDM_OUT_ON,
0027     PIN_TDM_OUT_OFF,
0028     PIN_WOV,
0029     PIN_STATE_MAX
0030 };
0031 
0032 static const char * const mt8183_pin_str[PIN_STATE_MAX] = {
0033     "default", "aud_tdm_out_on", "aud_tdm_out_off", "wov",
0034 };
0035 
0036 struct mt8183_mt6358_ts3a227_max98357_priv {
0037     struct pinctrl *pinctrl;
0038     struct pinctrl_state *pin_states[PIN_STATE_MAX];
0039     struct snd_soc_jack headset_jack, hdmi_jack;
0040 };
0041 
0042 static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
0043                        struct snd_pcm_hw_params *params)
0044 {
0045     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0046     unsigned int rate = params_rate(params);
0047     unsigned int mclk_fs_ratio = 128;
0048     unsigned int mclk_fs = rate * mclk_fs_ratio;
0049 
0050     return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
0051                       0, mclk_fs, SND_SOC_CLOCK_OUT);
0052 }
0053 
0054 static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
0055     .hw_params = mt8183_mt6358_i2s_hw_params,
0056 };
0057 
0058 static int
0059 mt8183_mt6358_rt1015_i2s_hw_params(struct snd_pcm_substream *substream,
0060                    struct snd_pcm_hw_params *params)
0061 {
0062     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0063     unsigned int rate = params_rate(params);
0064     unsigned int mclk_fs_ratio = 128;
0065     unsigned int mclk_fs = rate * mclk_fs_ratio;
0066     struct snd_soc_card *card = rtd->card;
0067     struct snd_soc_dai *codec_dai;
0068     int ret, i;
0069 
0070     for_each_rtd_codec_dais(rtd, i, codec_dai) {
0071         ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
0072                 rate * 64, rate * 256);
0073         if (ret < 0) {
0074             dev_err(card->dev, "failed to set pll\n");
0075             return ret;
0076         }
0077 
0078         ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
0079                 rate * 256, SND_SOC_CLOCK_IN);
0080         if (ret < 0) {
0081             dev_err(card->dev, "failed to set sysclk\n");
0082             return ret;
0083         }
0084     }
0085 
0086     return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
0087                       0, mclk_fs, SND_SOC_CLOCK_OUT);
0088 }
0089 
0090 static const struct snd_soc_ops mt8183_mt6358_rt1015_i2s_ops = {
0091     .hw_params = mt8183_mt6358_rt1015_i2s_hw_params,
0092 };
0093 
0094 static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
0095                       struct snd_pcm_hw_params *params)
0096 {
0097     dev_dbg(rtd->dev, "%s(), fix format to S32_LE\n", __func__);
0098 
0099     /* fix BE i2s format to S32_LE, clean param mask first */
0100     snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
0101                  0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
0102 
0103     params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
0104     return 0;
0105 }
0106 
0107 static int mt8183_rt1015_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
0108                          struct snd_pcm_hw_params *params)
0109 {
0110     dev_dbg(rtd->dev, "%s(), fix format to S24_LE\n", __func__);
0111 
0112     /* fix BE i2s format to S24_LE, clean param mask first */
0113     snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
0114                  0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
0115 
0116     params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
0117     return 0;
0118 }
0119 
0120 static int
0121 mt8183_mt6358_startup(struct snd_pcm_substream *substream)
0122 {
0123     static const unsigned int rates[] = {
0124         48000,
0125     };
0126     static const struct snd_pcm_hw_constraint_list constraints_rates = {
0127         .count = ARRAY_SIZE(rates),
0128         .list  = rates,
0129         .mask = 0,
0130     };
0131     static const unsigned int channels[] = {
0132         2,
0133     };
0134     static const struct snd_pcm_hw_constraint_list constraints_channels = {
0135         .count = ARRAY_SIZE(channels),
0136         .list = channels,
0137         .mask = 0,
0138     };
0139 
0140     struct snd_pcm_runtime *runtime = substream->runtime;
0141 
0142     snd_pcm_hw_constraint_list(runtime, 0,
0143                    SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
0144     runtime->hw.channels_max = 2;
0145     snd_pcm_hw_constraint_list(runtime, 0,
0146                    SNDRV_PCM_HW_PARAM_CHANNELS,
0147                    &constraints_channels);
0148 
0149     runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
0150     snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
0151 
0152     return 0;
0153 }
0154 
0155 static const struct snd_soc_ops mt8183_mt6358_ops = {
0156     .startup = mt8183_mt6358_startup,
0157 };
0158 
0159 static int
0160 mt8183_mt6358_ts3a227_max98357_bt_sco_startup(
0161     struct snd_pcm_substream *substream)
0162 {
0163     static const unsigned int rates[] = {
0164         8000, 16000
0165     };
0166     static const struct snd_pcm_hw_constraint_list constraints_rates = {
0167         .count = ARRAY_SIZE(rates),
0168         .list  = rates,
0169         .mask = 0,
0170     };
0171     static const unsigned int channels[] = {
0172         1,
0173     };
0174     static const struct snd_pcm_hw_constraint_list constraints_channels = {
0175         .count = ARRAY_SIZE(channels),
0176         .list = channels,
0177         .mask = 0,
0178     };
0179 
0180     struct snd_pcm_runtime *runtime = substream->runtime;
0181 
0182     snd_pcm_hw_constraint_list(runtime, 0,
0183             SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
0184     runtime->hw.channels_max = 1;
0185     snd_pcm_hw_constraint_list(runtime, 0,
0186             SNDRV_PCM_HW_PARAM_CHANNELS,
0187             &constraints_channels);
0188 
0189     runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
0190     snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
0191 
0192     return 0;
0193 }
0194 
0195 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_bt_sco_ops = {
0196     .startup = mt8183_mt6358_ts3a227_max98357_bt_sco_startup,
0197 };
0198 
0199 /* FE */
0200 SND_SOC_DAILINK_DEFS(playback1,
0201     DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
0202     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0203     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0204 
0205 SND_SOC_DAILINK_DEFS(playback2,
0206     DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
0207     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0208     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0209 
0210 SND_SOC_DAILINK_DEFS(playback3,
0211     DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
0212     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0213     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0214 
0215 SND_SOC_DAILINK_DEFS(capture1,
0216     DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
0217     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0218     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0219 
0220 SND_SOC_DAILINK_DEFS(capture2,
0221     DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
0222     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0223     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0224 
0225 SND_SOC_DAILINK_DEFS(capture3,
0226     DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
0227     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0228     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0229 
0230 SND_SOC_DAILINK_DEFS(capture_mono,
0231     DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
0232     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0233     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0234 
0235 SND_SOC_DAILINK_DEFS(playback_hdmi,
0236     DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
0237     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0238     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0239 
0240 SND_SOC_DAILINK_DEFS(wake_on_voice,
0241     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0242     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0243     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0244 
0245 /* BE */
0246 SND_SOC_DAILINK_DEFS(primary_codec,
0247     DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
0248     DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
0249     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0250 
0251 SND_SOC_DAILINK_DEFS(pcm1,
0252     DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
0253     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0254     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0255 
0256 SND_SOC_DAILINK_DEFS(pcm2,
0257     DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
0258     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0259     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0260 
0261 SND_SOC_DAILINK_DEFS(i2s0,
0262     DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
0263     DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm-wb")),
0264     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0265 
0266 SND_SOC_DAILINK_DEFS(i2s1,
0267     DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
0268     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0269     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0270 
0271 SND_SOC_DAILINK_DEFS(i2s2,
0272     DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
0273     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0274     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0275 
0276 SND_SOC_DAILINK_DEFS(i2s3_max98357a,
0277     DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
0278     DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi")),
0279     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0280 
0281 SND_SOC_DAILINK_DEFS(i2s3_rt1015,
0282     DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
0283     DAILINK_COMP_ARRAY(COMP_CODEC(RT1015_DEV0_NAME, RT1015_CODEC_DAI),
0284                COMP_CODEC(RT1015_DEV1_NAME, RT1015_CODEC_DAI)),
0285     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0286 
0287 SND_SOC_DAILINK_DEFS(i2s3_rt1015p,
0288     DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
0289     DAILINK_COMP_ARRAY(COMP_CODEC("rt1015p", "HiFi")),
0290     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0291 
0292 SND_SOC_DAILINK_DEFS(i2s5,
0293     DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
0294     DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm-wb")),
0295     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0296 
0297 SND_SOC_DAILINK_DEFS(tdm,
0298     DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
0299     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
0300     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0301 
0302 static int mt8183_mt6358_tdm_startup(struct snd_pcm_substream *substream)
0303 {
0304     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0305     struct mt8183_mt6358_ts3a227_max98357_priv *priv =
0306         snd_soc_card_get_drvdata(rtd->card);
0307     int ret;
0308 
0309     if (IS_ERR(priv->pin_states[PIN_TDM_OUT_ON]))
0310         return PTR_ERR(priv->pin_states[PIN_TDM_OUT_ON]);
0311 
0312     ret = pinctrl_select_state(priv->pinctrl,
0313                    priv->pin_states[PIN_TDM_OUT_ON]);
0314     if (ret)
0315         dev_err(rtd->card->dev, "%s failed to select state %d\n",
0316             __func__, ret);
0317 
0318     return ret;
0319 }
0320 
0321 static void mt8183_mt6358_tdm_shutdown(struct snd_pcm_substream *substream)
0322 {
0323     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0324     struct mt8183_mt6358_ts3a227_max98357_priv *priv =
0325         snd_soc_card_get_drvdata(rtd->card);
0326     int ret;
0327 
0328     if (IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF]))
0329         return;
0330 
0331     ret = pinctrl_select_state(priv->pinctrl,
0332                    priv->pin_states[PIN_TDM_OUT_OFF]);
0333     if (ret)
0334         dev_err(rtd->card->dev, "%s failed to select state %d\n",
0335             __func__, ret);
0336 }
0337 
0338 static const struct snd_soc_ops mt8183_mt6358_tdm_ops = {
0339     .startup = mt8183_mt6358_tdm_startup,
0340     .shutdown = mt8183_mt6358_tdm_shutdown,
0341 };
0342 
0343 static int
0344 mt8183_mt6358_ts3a227_max98357_wov_startup(
0345     struct snd_pcm_substream *substream)
0346 {
0347     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0348     struct snd_soc_card *card = rtd->card;
0349     struct mt8183_mt6358_ts3a227_max98357_priv *priv =
0350             snd_soc_card_get_drvdata(card);
0351 
0352     return pinctrl_select_state(priv->pinctrl,
0353                     priv->pin_states[PIN_WOV]);
0354 }
0355 
0356 static void
0357 mt8183_mt6358_ts3a227_max98357_wov_shutdown(
0358     struct snd_pcm_substream *substream)
0359 {
0360     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0361     struct snd_soc_card *card = rtd->card;
0362     struct mt8183_mt6358_ts3a227_max98357_priv *priv =
0363             snd_soc_card_get_drvdata(card);
0364     int ret;
0365 
0366     ret = pinctrl_select_state(priv->pinctrl,
0367                    priv->pin_states[PIN_STATE_DEFAULT]);
0368     if (ret)
0369         dev_err(card->dev, "%s failed to select state %d\n",
0370             __func__, ret);
0371 }
0372 
0373 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_wov_ops = {
0374     .startup = mt8183_mt6358_ts3a227_max98357_wov_startup,
0375     .shutdown = mt8183_mt6358_ts3a227_max98357_wov_shutdown,
0376 };
0377 
0378 static int
0379 mt8183_mt6358_ts3a227_max98357_hdmi_init(struct snd_soc_pcm_runtime *rtd)
0380 {
0381     struct mt8183_mt6358_ts3a227_max98357_priv *priv =
0382         snd_soc_card_get_drvdata(rtd->card);
0383     int ret;
0384 
0385     ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
0386                     &priv->hdmi_jack);
0387     if (ret)
0388         return ret;
0389 
0390     return snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component,
0391                       &priv->hdmi_jack, NULL);
0392 }
0393 
0394 static struct snd_soc_dai_link mt8183_mt6358_ts3a227_dai_links[] = {
0395     /* FE */
0396     {
0397         .name = "Playback_1",
0398         .stream_name = "Playback_1",
0399         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0400                 SND_SOC_DPCM_TRIGGER_PRE},
0401         .dynamic = 1,
0402         .dpcm_playback = 1,
0403         .ops = &mt8183_mt6358_ops,
0404         SND_SOC_DAILINK_REG(playback1),
0405     },
0406     {
0407         .name = "Playback_2",
0408         .stream_name = "Playback_2",
0409         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0410                 SND_SOC_DPCM_TRIGGER_PRE},
0411         .dynamic = 1,
0412         .dpcm_playback = 1,
0413         .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
0414         SND_SOC_DAILINK_REG(playback2),
0415     },
0416     {
0417         .name = "Playback_3",
0418         .stream_name = "Playback_3",
0419         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0420                 SND_SOC_DPCM_TRIGGER_PRE},
0421         .dynamic = 1,
0422         .dpcm_playback = 1,
0423         SND_SOC_DAILINK_REG(playback3),
0424     },
0425     {
0426         .name = "Capture_1",
0427         .stream_name = "Capture_1",
0428         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0429                 SND_SOC_DPCM_TRIGGER_PRE},
0430         .dynamic = 1,
0431         .dpcm_capture = 1,
0432         .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
0433         SND_SOC_DAILINK_REG(capture1),
0434     },
0435     {
0436         .name = "Capture_2",
0437         .stream_name = "Capture_2",
0438         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0439                 SND_SOC_DPCM_TRIGGER_PRE},
0440         .dynamic = 1,
0441         .dpcm_capture = 1,
0442         SND_SOC_DAILINK_REG(capture2),
0443     },
0444     {
0445         .name = "Capture_3",
0446         .stream_name = "Capture_3",
0447         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0448                 SND_SOC_DPCM_TRIGGER_PRE},
0449         .dynamic = 1,
0450         .dpcm_capture = 1,
0451         .ops = &mt8183_mt6358_ops,
0452         SND_SOC_DAILINK_REG(capture3),
0453     },
0454     {
0455         .name = "Capture_Mono_1",
0456         .stream_name = "Capture_Mono_1",
0457         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0458                 SND_SOC_DPCM_TRIGGER_PRE},
0459         .dynamic = 1,
0460         .dpcm_capture = 1,
0461         SND_SOC_DAILINK_REG(capture_mono),
0462     },
0463     {
0464         .name = "Playback_HDMI",
0465         .stream_name = "Playback_HDMI",
0466         .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
0467                 SND_SOC_DPCM_TRIGGER_PRE},
0468         .dynamic = 1,
0469         .dpcm_playback = 1,
0470         SND_SOC_DAILINK_REG(playback_hdmi),
0471     },
0472     {
0473         .name = "Wake on Voice",
0474         .stream_name = "Wake on Voice",
0475         .ignore_suspend = 1,
0476         .ignore = 1,
0477         SND_SOC_DAILINK_REG(wake_on_voice),
0478         .ops = &mt8183_mt6358_ts3a227_max98357_wov_ops,
0479     },
0480 
0481     /* BE */
0482     {
0483         .name = "Primary Codec",
0484         .no_pcm = 1,
0485         .dpcm_playback = 1,
0486         .dpcm_capture = 1,
0487         .ignore_suspend = 1,
0488         SND_SOC_DAILINK_REG(primary_codec),
0489     },
0490     {
0491         .name = "PCM 1",
0492         .no_pcm = 1,
0493         .dpcm_playback = 1,
0494         .dpcm_capture = 1,
0495         .ignore_suspend = 1,
0496         SND_SOC_DAILINK_REG(pcm1),
0497     },
0498     {
0499         .name = "PCM 2",
0500         .no_pcm = 1,
0501         .dpcm_playback = 1,
0502         .dpcm_capture = 1,
0503         .ignore_suspend = 1,
0504         SND_SOC_DAILINK_REG(pcm2),
0505     },
0506     {
0507         .name = "I2S0",
0508         .no_pcm = 1,
0509         .dpcm_capture = 1,
0510         .ignore_suspend = 1,
0511         .ops = &mt8183_mt6358_i2s_ops,
0512         SND_SOC_DAILINK_REG(i2s0),
0513     },
0514     {
0515         .name = "I2S1",
0516         .no_pcm = 1,
0517         .dpcm_playback = 1,
0518         .ignore_suspend = 1,
0519         .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
0520         .ops = &mt8183_mt6358_i2s_ops,
0521         SND_SOC_DAILINK_REG(i2s1),
0522     },
0523     {
0524         .name = "I2S2",
0525         .no_pcm = 1,
0526         .dpcm_capture = 1,
0527         .ignore_suspend = 1,
0528         .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
0529         .ops = &mt8183_mt6358_i2s_ops,
0530         SND_SOC_DAILINK_REG(i2s2),
0531     },
0532     {
0533         .name = "I2S3",
0534         .no_pcm = 1,
0535         .dpcm_playback = 1,
0536         .ignore_suspend = 1,
0537     },
0538     {
0539         .name = "I2S5",
0540         .no_pcm = 1,
0541         .dpcm_playback = 1,
0542         .ignore_suspend = 1,
0543         .ops = &mt8183_mt6358_i2s_ops,
0544         SND_SOC_DAILINK_REG(i2s5),
0545     },
0546     {
0547         .name = "TDM",
0548         .no_pcm = 1,
0549         .dai_fmt = SND_SOC_DAIFMT_I2S |
0550                SND_SOC_DAIFMT_IB_IF |
0551                SND_SOC_DAIFMT_CBM_CFM,
0552         .dpcm_playback = 1,
0553         .ignore_suspend = 1,
0554         .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
0555         .ops = &mt8183_mt6358_tdm_ops,
0556         .ignore = 1,
0557         .init = mt8183_mt6358_ts3a227_max98357_hdmi_init,
0558         SND_SOC_DAILINK_REG(tdm),
0559     },
0560 };
0561 
0562 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357_card = {
0563     .name = "mt8183_mt6358_ts3a227_max98357",
0564     .owner = THIS_MODULE,
0565     .dai_link = mt8183_mt6358_ts3a227_dai_links,
0566     .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
0567 };
0568 
0569 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357b_card = {
0570     .name = "mt8183_mt6358_ts3a227_max98357b",
0571     .owner = THIS_MODULE,
0572     .dai_link = mt8183_mt6358_ts3a227_dai_links,
0573     .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
0574 };
0575 
0576 static struct snd_soc_codec_conf mt8183_mt6358_ts3a227_rt1015_amp_conf[] = {
0577     {
0578         .dlc = COMP_CODEC_CONF(RT1015_DEV0_NAME),
0579         .name_prefix = "Left",
0580     },
0581     {
0582         .dlc = COMP_CODEC_CONF(RT1015_DEV1_NAME),
0583         .name_prefix = "Right",
0584     },
0585 };
0586 
0587 static struct snd_soc_card mt8183_mt6358_ts3a227_rt1015_card = {
0588     .name = "mt8183_mt6358_ts3a227_rt1015",
0589     .owner = THIS_MODULE,
0590     .dai_link = mt8183_mt6358_ts3a227_dai_links,
0591     .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
0592     .codec_conf = mt8183_mt6358_ts3a227_rt1015_amp_conf,
0593     .num_configs = ARRAY_SIZE(mt8183_mt6358_ts3a227_rt1015_amp_conf),
0594 };
0595 
0596 static struct snd_soc_card mt8183_mt6358_ts3a227_rt1015p_card = {
0597     .name = "mt8183_mt6358_ts3a227_rt1015p",
0598     .owner = THIS_MODULE,
0599     .dai_link = mt8183_mt6358_ts3a227_dai_links,
0600     .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
0601 };
0602 
0603 static int
0604 mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *component)
0605 {
0606     int ret;
0607     struct mt8183_mt6358_ts3a227_max98357_priv *priv =
0608             snd_soc_card_get_drvdata(component->card);
0609 
0610     /* Enable Headset and 4 Buttons Jack detection */
0611     ret = snd_soc_card_jack_new(component->card,
0612                     "Headset Jack",
0613                     SND_JACK_HEADSET |
0614                     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0615                     SND_JACK_BTN_2 | SND_JACK_BTN_3,
0616                     &priv->headset_jack);
0617     if (ret)
0618         return ret;
0619 
0620     ret = ts3a227e_enable_jack_detect(component, &priv->headset_jack);
0621 
0622     return ret;
0623 }
0624 
0625 static struct snd_soc_aux_dev mt8183_mt6358_ts3a227_max98357_headset_dev = {
0626     .dlc = COMP_EMPTY(),
0627     .init = mt8183_mt6358_ts3a227_max98357_headset_init,
0628 };
0629 
0630 static int
0631 mt8183_mt6358_ts3a227_max98357_dev_probe(struct platform_device *pdev)
0632 {
0633     struct snd_soc_card *card;
0634     struct device_node *platform_node, *ec_codec, *hdmi_codec;
0635     struct snd_soc_dai_link *dai_link;
0636     struct mt8183_mt6358_ts3a227_max98357_priv *priv;
0637     int ret, i;
0638 
0639     platform_node = of_parse_phandle(pdev->dev.of_node,
0640                      "mediatek,platform", 0);
0641     if (!platform_node) {
0642         dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
0643         return -EINVAL;
0644     }
0645 
0646     card = (struct snd_soc_card *)of_device_get_match_data(&pdev->dev);
0647     if (!card)
0648         return -EINVAL;
0649     card->dev = &pdev->dev;
0650 
0651     ec_codec = of_parse_phandle(pdev->dev.of_node, "mediatek,ec-codec", 0);
0652     hdmi_codec = of_parse_phandle(pdev->dev.of_node,
0653                       "mediatek,hdmi-codec", 0);
0654 
0655     for_each_card_prelinks(card, i, dai_link) {
0656         if (ec_codec && strcmp(dai_link->name, "Wake on Voice") == 0) {
0657             dai_link->cpus[0].name = NULL;
0658             dai_link->cpus[0].of_node = ec_codec;
0659             dai_link->cpus[0].dai_name = NULL;
0660             dai_link->codecs[0].name = NULL;
0661             dai_link->codecs[0].of_node = ec_codec;
0662             dai_link->codecs[0].dai_name = "Wake on Voice";
0663             dai_link->platforms[0].of_node = ec_codec;
0664             dai_link->ignore = 0;
0665         }
0666 
0667         if (strcmp(dai_link->name, "I2S3") == 0) {
0668             if (card == &mt8183_mt6358_ts3a227_max98357_card ||
0669                 card == &mt8183_mt6358_ts3a227_max98357b_card) {
0670                 dai_link->be_hw_params_fixup =
0671                     mt8183_i2s_hw_params_fixup;
0672                 dai_link->ops = &mt8183_mt6358_i2s_ops;
0673                 dai_link->cpus = i2s3_max98357a_cpus;
0674                 dai_link->num_cpus =
0675                     ARRAY_SIZE(i2s3_max98357a_cpus);
0676                 dai_link->codecs = i2s3_max98357a_codecs;
0677                 dai_link->num_codecs =
0678                     ARRAY_SIZE(i2s3_max98357a_codecs);
0679                 dai_link->platforms = i2s3_max98357a_platforms;
0680                 dai_link->num_platforms =
0681                     ARRAY_SIZE(i2s3_max98357a_platforms);
0682             } else if (card == &mt8183_mt6358_ts3a227_rt1015_card) {
0683                 dai_link->be_hw_params_fixup =
0684                     mt8183_rt1015_i2s_hw_params_fixup;
0685                 dai_link->ops = &mt8183_mt6358_rt1015_i2s_ops;
0686                 dai_link->cpus = i2s3_rt1015_cpus;
0687                 dai_link->num_cpus =
0688                     ARRAY_SIZE(i2s3_rt1015_cpus);
0689                 dai_link->codecs = i2s3_rt1015_codecs;
0690                 dai_link->num_codecs =
0691                     ARRAY_SIZE(i2s3_rt1015_codecs);
0692                 dai_link->platforms = i2s3_rt1015_platforms;
0693                 dai_link->num_platforms =
0694                     ARRAY_SIZE(i2s3_rt1015_platforms);
0695             } else if (card == &mt8183_mt6358_ts3a227_rt1015p_card) {
0696                 dai_link->be_hw_params_fixup =
0697                     mt8183_rt1015_i2s_hw_params_fixup;
0698                 dai_link->ops = &mt8183_mt6358_i2s_ops;
0699                 dai_link->cpus = i2s3_rt1015p_cpus;
0700                 dai_link->num_cpus =
0701                     ARRAY_SIZE(i2s3_rt1015p_cpus);
0702                 dai_link->codecs = i2s3_rt1015p_codecs;
0703                 dai_link->num_codecs =
0704                     ARRAY_SIZE(i2s3_rt1015p_codecs);
0705                 dai_link->platforms = i2s3_rt1015p_platforms;
0706                 dai_link->num_platforms =
0707                     ARRAY_SIZE(i2s3_rt1015p_platforms);
0708             }
0709         }
0710 
0711         if (card == &mt8183_mt6358_ts3a227_max98357b_card) {
0712             if (strcmp(dai_link->name, "I2S2") == 0 ||
0713                 strcmp(dai_link->name, "I2S3") == 0)
0714                 dai_link->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
0715                             SND_SOC_DAIFMT_NB_NF |
0716                             SND_SOC_DAIFMT_CBM_CFM;
0717         }
0718 
0719         if (hdmi_codec && strcmp(dai_link->name, "TDM") == 0) {
0720             dai_link->codecs->of_node = hdmi_codec;
0721             dai_link->ignore = 0;
0722         }
0723 
0724         if (!dai_link->platforms->name)
0725             dai_link->platforms->of_node = platform_node;
0726     }
0727 
0728     mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node =
0729         of_parse_phandle(pdev->dev.of_node,
0730                  "mediatek,headset-codec", 0);
0731     if (mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node) {
0732         card->aux_dev = &mt8183_mt6358_ts3a227_max98357_headset_dev;
0733         card->num_aux_devs = 1;
0734     }
0735 
0736     priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0737     if (!priv)
0738         return -ENOMEM;
0739 
0740     snd_soc_card_set_drvdata(card, priv);
0741 
0742     priv->pinctrl = devm_pinctrl_get(&pdev->dev);
0743     if (IS_ERR(priv->pinctrl)) {
0744         dev_err(&pdev->dev, "%s devm_pinctrl_get failed\n",
0745             __func__);
0746         return PTR_ERR(priv->pinctrl);
0747     }
0748 
0749     for (i = 0; i < PIN_STATE_MAX; i++) {
0750         priv->pin_states[i] = pinctrl_lookup_state(priv->pinctrl,
0751                                mt8183_pin_str[i]);
0752         if (IS_ERR(priv->pin_states[i])) {
0753             ret = PTR_ERR(priv->pin_states[i]);
0754             dev_info(&pdev->dev, "%s Can't find pin state %s %d\n",
0755                  __func__, mt8183_pin_str[i], ret);
0756         }
0757     }
0758 
0759     if (!IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF])) {
0760         ret = pinctrl_select_state(priv->pinctrl,
0761                        priv->pin_states[PIN_TDM_OUT_OFF]);
0762         if (ret)
0763             dev_info(&pdev->dev,
0764                  "%s failed to select state %d\n",
0765                  __func__, ret);
0766     }
0767 
0768     if (!IS_ERR(priv->pin_states[PIN_STATE_DEFAULT])) {
0769         ret = pinctrl_select_state(priv->pinctrl,
0770                        priv->pin_states[PIN_STATE_DEFAULT]);
0771         if (ret)
0772             dev_info(&pdev->dev,
0773                  "%s failed to select state %d\n",
0774                  __func__, ret);
0775     }
0776 
0777     ret = devm_snd_soc_register_card(&pdev->dev, card);
0778 
0779     of_node_put(platform_node);
0780     of_node_put(ec_codec);
0781     of_node_put(hdmi_codec);
0782     return ret;
0783 }
0784 
0785 #ifdef CONFIG_OF
0786 static const struct of_device_id mt8183_mt6358_ts3a227_max98357_dt_match[] = {
0787     {
0788         .compatible = "mediatek,mt8183_mt6358_ts3a227_max98357",
0789         .data = &mt8183_mt6358_ts3a227_max98357_card,
0790     },
0791     {
0792         .compatible = "mediatek,mt8183_mt6358_ts3a227_max98357b",
0793         .data = &mt8183_mt6358_ts3a227_max98357b_card,
0794     },
0795     {
0796         .compatible = "mediatek,mt8183_mt6358_ts3a227_rt1015",
0797         .data = &mt8183_mt6358_ts3a227_rt1015_card,
0798     },
0799     {
0800         .compatible = "mediatek,mt8183_mt6358_ts3a227_rt1015p",
0801         .data = &mt8183_mt6358_ts3a227_rt1015p_card,
0802     },
0803     {}
0804 };
0805 #endif
0806 
0807 static struct platform_driver mt8183_mt6358_ts3a227_max98357_driver = {
0808     .driver = {
0809         .name = "mt8183_mt6358_ts3a227",
0810 #ifdef CONFIG_OF
0811         .of_match_table = mt8183_mt6358_ts3a227_max98357_dt_match,
0812 #endif
0813         .pm = &snd_soc_pm_ops,
0814     },
0815     .probe = mt8183_mt6358_ts3a227_max98357_dev_probe,
0816 };
0817 
0818 module_platform_driver(mt8183_mt6358_ts3a227_max98357_driver);
0819 
0820 /* Module information */
0821 MODULE_DESCRIPTION("MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver");
0822 MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
0823 MODULE_LICENSE("GPL v2");
0824 MODULE_ALIAS("mt8183_mt6358_ts3a227_max98357 soc card");