linux/sound/soc/mediatek/mt8173-rt5650.c
<<
>>
Prefs
   1/*
   2 * mt8173-rt5650.c  --  MT8173 machine driver with RT5650 codecs
   3 *
   4 * Copyright (c) 2016 MediaTek Inc.
   5 * Author: Koro Chen <koro.chen@mediatek.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 and
   9 * only version 2 as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful,
  12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 */
  16
  17#include <linux/module.h>
  18#include <linux/gpio.h>
  19#include <linux/of_gpio.h>
  20#include <sound/soc.h>
  21#include <sound/jack.h>
  22#include "../codecs/rt5645.h"
  23
  24#define MCLK_FOR_CODECS         12288000
  25
  26static const struct snd_soc_dapm_widget mt8173_rt5650_widgets[] = {
  27        SND_SOC_DAPM_SPK("Speaker", NULL),
  28        SND_SOC_DAPM_MIC("Int Mic", NULL),
  29        SND_SOC_DAPM_HP("Headphone", NULL),
  30        SND_SOC_DAPM_MIC("Headset Mic", NULL),
  31};
  32
  33static const struct snd_soc_dapm_route mt8173_rt5650_routes[] = {
  34        {"Speaker", NULL, "SPOL"},
  35        {"Speaker", NULL, "SPOR"},
  36        {"DMIC L1", NULL, "Int Mic"},
  37        {"DMIC R1", NULL, "Int Mic"},
  38        {"Headphone", NULL, "HPOL"},
  39        {"Headphone", NULL, "HPOR"},
  40        {"Headset Mic", NULL, "micbias1"},
  41        {"Headset Mic", NULL, "micbias2"},
  42        {"IN1P", NULL, "Headset Mic"},
  43        {"IN1N", NULL, "Headset Mic"},
  44};
  45
  46static const struct snd_kcontrol_new mt8173_rt5650_controls[] = {
  47        SOC_DAPM_PIN_SWITCH("Speaker"),
  48        SOC_DAPM_PIN_SWITCH("Int Mic"),
  49        SOC_DAPM_PIN_SWITCH("Headphone"),
  50        SOC_DAPM_PIN_SWITCH("Headset Mic"),
  51};
  52
  53static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
  54                                   struct snd_pcm_hw_params *params)
  55{
  56        struct snd_soc_pcm_runtime *rtd = substream->private_data;
  57        int i, ret;
  58
  59        for (i = 0; i < rtd->num_codecs; i++) {
  60                struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  61
  62                /* pll from mclk 12.288M */
  63                ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
  64                                          params_rate(params) * 512);
  65                if (ret)
  66                        return ret;
  67
  68                /* sysclk from pll */
  69                ret = snd_soc_dai_set_sysclk(codec_dai, 1,
  70                                             params_rate(params) * 512,
  71                                             SND_SOC_CLOCK_IN);
  72                if (ret)
  73                        return ret;
  74        }
  75        return 0;
  76}
  77
  78static struct snd_soc_ops mt8173_rt5650_ops = {
  79        .hw_params = mt8173_rt5650_hw_params,
  80};
  81
  82static struct snd_soc_jack mt8173_rt5650_jack;
  83
  84static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
  85{
  86        struct snd_soc_card *card = runtime->card;
  87        struct snd_soc_codec *codec = runtime->codec_dais[0]->codec;
  88        const char *codec_capture_dai = runtime->codec_dais[1]->name;
  89        int ret;
  90
  91        rt5645_sel_asrc_clk_src(codec,
  92                                RT5645_DA_STEREO_FILTER,
  93                                RT5645_CLK_SEL_I2S1_ASRC);
  94
  95        if (!strcmp(codec_capture_dai, "rt5645-aif1")) {
  96                rt5645_sel_asrc_clk_src(codec,
  97                                        RT5645_AD_STEREO_FILTER,
  98                                        RT5645_CLK_SEL_I2S1_ASRC);
  99        } else if (!strcmp(codec_capture_dai, "rt5645-aif2")) {
 100                rt5645_sel_asrc_clk_src(codec,
 101                                        RT5645_AD_STEREO_FILTER,
 102                                        RT5645_CLK_SEL_I2S2_ASRC);
 103        } else {
 104                dev_warn(card->dev,
 105                         "Only one dai codec found in DTS, enabled rt5645 AD filter\n");
 106                rt5645_sel_asrc_clk_src(codec,
 107                                        RT5645_AD_STEREO_FILTER,
 108                                        RT5645_CLK_SEL_I2S1_ASRC);
 109        }
 110
 111        /* enable jack detection */
 112        ret = snd_soc_card_jack_new(card, "Headset Jack",
 113                                    SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
 114                                    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
 115                                    SND_JACK_BTN_2 | SND_JACK_BTN_3,
 116                                    &mt8173_rt5650_jack, NULL, 0);
 117        if (ret) {
 118                dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
 119                return ret;
 120        }
 121
 122        return rt5645_set_jack_detect(codec,
 123                                      &mt8173_rt5650_jack,
 124                                      &mt8173_rt5650_jack,
 125                                      &mt8173_rt5650_jack);
 126}
 127
 128static struct snd_soc_dai_link_component mt8173_rt5650_codecs[] = {
 129        {
 130                /* Playback */
 131                .dai_name = "rt5645-aif1",
 132        },
 133        {
 134                /* Capture */
 135                .dai_name = "rt5645-aif1",
 136        },
 137};
 138
 139enum {
 140        DAI_LINK_PLAYBACK,
 141        DAI_LINK_CAPTURE,
 142        DAI_LINK_CODEC_I2S,
 143};
 144
 145/* Digital audio interface glue - connects codec <---> CPU */
 146static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
 147        /* Front End DAI links */
 148        [DAI_LINK_PLAYBACK] = {
 149                .name = "rt5650 Playback",
 150                .stream_name = "rt5650 Playback",
 151                .cpu_dai_name = "DL1",
 152                .codec_name = "snd-soc-dummy",
 153                .codec_dai_name = "snd-soc-dummy-dai",
 154                .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
 155                .dynamic = 1,
 156                .dpcm_playback = 1,
 157        },
 158        [DAI_LINK_CAPTURE] = {
 159                .name = "rt5650 Capture",
 160                .stream_name = "rt5650 Capture",
 161                .cpu_dai_name = "VUL",
 162                .codec_name = "snd-soc-dummy",
 163                .codec_dai_name = "snd-soc-dummy-dai",
 164                .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
 165                .dynamic = 1,
 166                .dpcm_capture = 1,
 167        },
 168        /* Back End DAI links */
 169        [DAI_LINK_CODEC_I2S] = {
 170                .name = "Codec",
 171                .cpu_dai_name = "I2S",
 172                .no_pcm = 1,
 173                .codecs = mt8173_rt5650_codecs,
 174                .num_codecs = 2,
 175                .init = mt8173_rt5650_init,
 176                .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
 177                           SND_SOC_DAIFMT_CBS_CFS,
 178                .ops = &mt8173_rt5650_ops,
 179                .ignore_pmdown_time = 1,
 180                .dpcm_playback = 1,
 181                .dpcm_capture = 1,
 182        },
 183};
 184
 185static struct snd_soc_card mt8173_rt5650_card = {
 186        .name = "mtk-rt5650",
 187        .owner = THIS_MODULE,
 188        .dai_link = mt8173_rt5650_dais,
 189        .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
 190        .controls = mt8173_rt5650_controls,
 191        .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
 192        .dapm_widgets = mt8173_rt5650_widgets,
 193        .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
 194        .dapm_routes = mt8173_rt5650_routes,
 195        .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
 196};
 197
 198static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
 199{
 200        struct snd_soc_card *card = &mt8173_rt5650_card;
 201        struct device_node *platform_node;
 202        struct device_node *np;
 203        const char *codec_capture_dai;
 204        int i, ret;
 205
 206        platform_node = of_parse_phandle(pdev->dev.of_node,
 207                                         "mediatek,platform", 0);
 208        if (!platform_node) {
 209                dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
 210                return -EINVAL;
 211        }
 212
 213        for (i = 0; i < card->num_links; i++) {
 214                if (mt8173_rt5650_dais[i].platform_name)
 215                        continue;
 216                mt8173_rt5650_dais[i].platform_of_node = platform_node;
 217        }
 218
 219        mt8173_rt5650_codecs[0].of_node =
 220                of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
 221        if (!mt8173_rt5650_codecs[0].of_node) {
 222                dev_err(&pdev->dev,
 223                        "Property 'audio-codec' missing or invalid\n");
 224                return -EINVAL;
 225        }
 226        mt8173_rt5650_codecs[1].of_node = mt8173_rt5650_codecs[0].of_node;
 227
 228        if (of_find_node_by_name(platform_node, "codec-capture")) {
 229                np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
 230                if (!np) {
 231                        dev_err(&pdev->dev,
 232                                "%s: Can't find codec-capture DT node\n",
 233                                __func__);
 234                        return -EINVAL;
 235                }
 236                ret = snd_soc_of_get_dai_name(np, &codec_capture_dai);
 237                if (ret < 0) {
 238                        dev_err(&pdev->dev,
 239                                "%s codec_capture_dai name fail %d\n",
 240                                __func__, ret);
 241                        return ret;
 242                }
 243                mt8173_rt5650_codecs[1].dai_name = codec_capture_dai;
 244        }
 245
 246        card->dev = &pdev->dev;
 247        platform_set_drvdata(pdev, card);
 248
 249        ret = devm_snd_soc_register_card(&pdev->dev, card);
 250        if (ret)
 251                dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
 252                        __func__, ret);
 253        return ret;
 254}
 255
 256static const struct of_device_id mt8173_rt5650_dt_match[] = {
 257        { .compatible = "mediatek,mt8173-rt5650", },
 258        { }
 259};
 260MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
 261
 262static struct platform_driver mt8173_rt5650_driver = {
 263        .driver = {
 264                   .name = "mtk-rt5650",
 265                   .of_match_table = mt8173_rt5650_dt_match,
 266#ifdef CONFIG_PM
 267                   .pm = &snd_soc_pm_ops,
 268#endif
 269        },
 270        .probe = mt8173_rt5650_dev_probe,
 271};
 272
 273module_platform_driver(mt8173_rt5650_driver);
 274
 275/* Module information */
 276MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
 277MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
 278MODULE_LICENSE("GPL v2");
 279MODULE_ALIAS("platform:mtk-rt5650");
 280
 281