Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * mt8173-rt5650.c  --  MT8173 machine driver with RT5650 codecs
0004  *
0005  * Copyright (c) 2016 MediaTek Inc.
0006  * Author: Koro Chen <koro.chen@mediatek.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/gpio.h>
0011 #include <linux/of_gpio.h>
0012 #include <sound/soc.h>
0013 #include <sound/jack.h>
0014 #include "../../codecs/rt5645.h"
0015 
0016 #define MCLK_FOR_CODECS     12288000
0017 
0018 enum mt8173_rt5650_mclk {
0019     MT8173_RT5650_MCLK_EXTERNAL = 0,
0020     MT8173_RT5650_MCLK_INTERNAL,
0021 };
0022 
0023 struct mt8173_rt5650_platform_data {
0024     enum mt8173_rt5650_mclk pll_from;
0025     /* 0 = external oscillator; 1 = internal source from mt8173 */
0026 };
0027 
0028 static struct mt8173_rt5650_platform_data mt8173_rt5650_priv = {
0029     .pll_from = MT8173_RT5650_MCLK_EXTERNAL,
0030 };
0031 
0032 static const struct snd_soc_dapm_widget mt8173_rt5650_widgets[] = {
0033     SND_SOC_DAPM_SPK("Ext Spk", NULL),
0034     SND_SOC_DAPM_MIC("Int Mic", NULL),
0035     SND_SOC_DAPM_HP("Headphone", NULL),
0036     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0037 };
0038 
0039 static const struct snd_soc_dapm_route mt8173_rt5650_routes[] = {
0040     {"Ext Spk", NULL, "SPOL"},
0041     {"Ext Spk", NULL, "SPOR"},
0042     {"DMIC L1", NULL, "Int Mic"},
0043     {"DMIC R1", NULL, "Int Mic"},
0044     {"Headphone", NULL, "HPOL"},
0045     {"Headphone", NULL, "HPOR"},
0046     {"IN1P", NULL, "Headset Mic"},
0047     {"IN1N", NULL, "Headset Mic"},
0048 };
0049 
0050 static const struct snd_kcontrol_new mt8173_rt5650_controls[] = {
0051     SOC_DAPM_PIN_SWITCH("Ext Spk"),
0052     SOC_DAPM_PIN_SWITCH("Int Mic"),
0053     SOC_DAPM_PIN_SWITCH("Headphone"),
0054     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0055 };
0056 
0057 static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
0058                    struct snd_pcm_hw_params *params)
0059 {
0060     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0061     unsigned int mclk_clock;
0062     struct snd_soc_dai *codec_dai;
0063     int i, ret;
0064 
0065     switch (mt8173_rt5650_priv.pll_from) {
0066     case MT8173_RT5650_MCLK_EXTERNAL:
0067         /* mclk = 12.288M */
0068         mclk_clock = MCLK_FOR_CODECS;
0069         break;
0070     case MT8173_RT5650_MCLK_INTERNAL:
0071         /* mclk = sampling rate*256 */
0072         mclk_clock = params_rate(params) * 256;
0073         break;
0074     default:
0075         /* mclk = 12.288M */
0076         mclk_clock = MCLK_FOR_CODECS;
0077         break;
0078     }
0079 
0080     for_each_rtd_codec_dais(rtd, i, codec_dai) {
0081         /* pll from mclk */
0082         ret = snd_soc_dai_set_pll(codec_dai, 0, 0, mclk_clock,
0083                       params_rate(params) * 512);
0084         if (ret)
0085             return ret;
0086 
0087         /* sysclk from pll */
0088         ret = snd_soc_dai_set_sysclk(codec_dai, 1,
0089                          params_rate(params) * 512,
0090                          SND_SOC_CLOCK_IN);
0091         if (ret)
0092             return ret;
0093     }
0094     return 0;
0095 }
0096 
0097 static const struct snd_soc_ops mt8173_rt5650_ops = {
0098     .hw_params = mt8173_rt5650_hw_params,
0099 };
0100 
0101 static struct snd_soc_jack mt8173_rt5650_jack, mt8173_rt5650_hdmi_jack;
0102 
0103 static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
0104 {
0105     struct snd_soc_card *card = runtime->card;
0106     struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
0107     const char *codec_capture_dai = asoc_rtd_to_codec(runtime, 1)->name;
0108     int ret;
0109 
0110     rt5645_sel_asrc_clk_src(component,
0111                 RT5645_DA_STEREO_FILTER,
0112                 RT5645_CLK_SEL_I2S1_ASRC);
0113 
0114     if (!strcmp(codec_capture_dai, "rt5645-aif1")) {
0115         rt5645_sel_asrc_clk_src(component,
0116                     RT5645_AD_STEREO_FILTER,
0117                     RT5645_CLK_SEL_I2S1_ASRC);
0118     } else if (!strcmp(codec_capture_dai, "rt5645-aif2")) {
0119         rt5645_sel_asrc_clk_src(component,
0120                     RT5645_AD_STEREO_FILTER,
0121                     RT5645_CLK_SEL_I2S2_ASRC);
0122     } else {
0123         dev_warn(card->dev,
0124              "Only one dai codec found in DTS, enabled rt5645 AD filter\n");
0125         rt5645_sel_asrc_clk_src(component,
0126                     RT5645_AD_STEREO_FILTER,
0127                     RT5645_CLK_SEL_I2S1_ASRC);
0128     }
0129 
0130     /* enable jack detection */
0131     ret = snd_soc_card_jack_new(card, "Headset Jack",
0132                     SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
0133                     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0134                     SND_JACK_BTN_2 | SND_JACK_BTN_3,
0135                     &mt8173_rt5650_jack);
0136     if (ret) {
0137         dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
0138         return ret;
0139     }
0140 
0141     return rt5645_set_jack_detect(component,
0142                       &mt8173_rt5650_jack,
0143                       &mt8173_rt5650_jack,
0144                       &mt8173_rt5650_jack);
0145 }
0146 
0147 static int mt8173_rt5650_hdmi_init(struct snd_soc_pcm_runtime *rtd)
0148 {
0149     int ret;
0150 
0151     ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
0152                     &mt8173_rt5650_hdmi_jack);
0153     if (ret)
0154         return ret;
0155 
0156     return snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component,
0157                       &mt8173_rt5650_hdmi_jack, NULL);
0158 }
0159 
0160 enum {
0161     DAI_LINK_PLAYBACK,
0162     DAI_LINK_CAPTURE,
0163     DAI_LINK_HDMI,
0164     DAI_LINK_CODEC_I2S,
0165     DAI_LINK_HDMI_I2S,
0166 };
0167 
0168 SND_SOC_DAILINK_DEFS(playback,
0169     DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
0170     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0171     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0172 
0173 SND_SOC_DAILINK_DEFS(capture,
0174     DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
0175     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0176     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0177 
0178 SND_SOC_DAILINK_DEFS(hdmi_pcm,
0179     DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
0180     DAILINK_COMP_ARRAY(COMP_DUMMY()),
0181     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0182 
0183 SND_SOC_DAILINK_DEFS(codec,
0184     DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
0185     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"), /* Playback */
0186                COMP_CODEC(NULL, "rt5645-aif1")),/* Capture */
0187     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0188 
0189 SND_SOC_DAILINK_DEFS(hdmi_be,
0190     DAILINK_COMP_ARRAY(COMP_CPU("HDMIO")),
0191     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
0192     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0193 
0194 /* Digital audio interface glue - connects codec <---> CPU */
0195 static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
0196     /* Front End DAI links */
0197     [DAI_LINK_PLAYBACK] = {
0198         .name = "rt5650 Playback",
0199         .stream_name = "rt5650 Playback",
0200         .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0201         .dynamic = 1,
0202         .dpcm_playback = 1,
0203         SND_SOC_DAILINK_REG(playback),
0204     },
0205     [DAI_LINK_CAPTURE] = {
0206         .name = "rt5650 Capture",
0207         .stream_name = "rt5650 Capture",
0208         .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0209         .dynamic = 1,
0210         .dpcm_capture = 1,
0211         SND_SOC_DAILINK_REG(capture),
0212     },
0213     [DAI_LINK_HDMI] = {
0214         .name = "HDMI",
0215         .stream_name = "HDMI PCM",
0216         .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
0217         .dynamic = 1,
0218         .dpcm_playback = 1,
0219         SND_SOC_DAILINK_REG(hdmi_pcm),
0220     },
0221     /* Back End DAI links */
0222     [DAI_LINK_CODEC_I2S] = {
0223         .name = "Codec",
0224         .no_pcm = 1,
0225         .init = mt8173_rt5650_init,
0226         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0227                SND_SOC_DAIFMT_CBS_CFS,
0228         .ops = &mt8173_rt5650_ops,
0229         .ignore_pmdown_time = 1,
0230         .dpcm_playback = 1,
0231         .dpcm_capture = 1,
0232         SND_SOC_DAILINK_REG(codec),
0233     },
0234     [DAI_LINK_HDMI_I2S] = {
0235         .name = "HDMI BE",
0236         .no_pcm = 1,
0237         .dpcm_playback = 1,
0238         .init = mt8173_rt5650_hdmi_init,
0239         SND_SOC_DAILINK_REG(hdmi_be),
0240     },
0241 };
0242 
0243 static struct snd_soc_card mt8173_rt5650_card = {
0244     .name = "mtk-rt5650",
0245     .owner = THIS_MODULE,
0246     .dai_link = mt8173_rt5650_dais,
0247     .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
0248     .controls = mt8173_rt5650_controls,
0249     .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
0250     .dapm_widgets = mt8173_rt5650_widgets,
0251     .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
0252     .dapm_routes = mt8173_rt5650_routes,
0253     .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
0254 };
0255 
0256 static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
0257 {
0258     struct snd_soc_card *card = &mt8173_rt5650_card;
0259     struct device_node *platform_node;
0260     struct device_node *np;
0261     const char *codec_capture_dai;
0262     struct snd_soc_dai_link *dai_link;
0263     int i, ret;
0264 
0265     platform_node = of_parse_phandle(pdev->dev.of_node,
0266                      "mediatek,platform", 0);
0267     if (!platform_node) {
0268         dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
0269         return -EINVAL;
0270     }
0271 
0272     for_each_card_prelinks(card, i, dai_link) {
0273         if (dai_link->platforms->name)
0274             continue;
0275         dai_link->platforms->of_node = platform_node;
0276     }
0277 
0278     mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
0279         of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
0280     if (!mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
0281         dev_err(&pdev->dev,
0282             "Property 'audio-codec' missing or invalid\n");
0283         ret = -EINVAL;
0284         goto put_platform_node;
0285     }
0286     mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
0287         mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node;
0288 
0289     np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
0290     if (np) {
0291         ret = snd_soc_of_get_dai_name(np, &codec_capture_dai);
0292         of_node_put(np);
0293         if (ret < 0) {
0294             dev_err(&pdev->dev,
0295                 "%s codec_capture_dai name fail %d\n",
0296                 __func__, ret);
0297             goto put_platform_node;
0298         }
0299         mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].dai_name =
0300             codec_capture_dai;
0301     }
0302 
0303     if (device_property_present(&pdev->dev, "mediatek,mclk")) {
0304         ret = device_property_read_u32(&pdev->dev,
0305                            "mediatek,mclk",
0306                            &mt8173_rt5650_priv.pll_from);
0307         if (ret) {
0308             dev_err(&pdev->dev,
0309                 "%s snd_soc_register_card fail %d\n",
0310                 __func__, ret);
0311         }
0312     }
0313 
0314     mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node =
0315         of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
0316     if (!mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node) {
0317         dev_err(&pdev->dev,
0318             "Property 'audio-codec' missing or invalid\n");
0319         ret = -EINVAL;
0320         goto put_platform_node;
0321     }
0322     card->dev = &pdev->dev;
0323 
0324     ret = devm_snd_soc_register_card(&pdev->dev, card);
0325 
0326 put_platform_node:
0327     of_node_put(platform_node);
0328     return ret;
0329 }
0330 
0331 static const struct of_device_id mt8173_rt5650_dt_match[] = {
0332     { .compatible = "mediatek,mt8173-rt5650", },
0333     { }
0334 };
0335 MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
0336 
0337 static struct platform_driver mt8173_rt5650_driver = {
0338     .driver = {
0339            .name = "mtk-rt5650",
0340            .of_match_table = mt8173_rt5650_dt_match,
0341            .pm = &snd_soc_pm_ops,
0342     },
0343     .probe = mt8173_rt5650_dev_probe,
0344 };
0345 
0346 module_platform_driver(mt8173_rt5650_driver);
0347 
0348 /* Module information */
0349 MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
0350 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
0351 MODULE_LICENSE("GPL v2");
0352 MODULE_ALIAS("platform:mtk-rt5650");
0353