1 /* 2 * mt8173-rt5650-rt5514.c -- MT8173 machine driver with RT5650/5514 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 26 static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_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 33 static const struct snd_soc_dapm_route mt8173_rt5650_rt5514_routes[] = { 34 {"Speaker", NULL, "SPOL"}, 35 {"Speaker", NULL, "SPOR"}, 36 {"Sub DMIC1L", NULL, "Int Mic"}, 37 {"Sub DMIC1R", NULL, "Int Mic"}, 38 {"Headphone", NULL, "HPOL"}, 39 {"Headphone", NULL, "HPOR"}, 40 {"IN1P", NULL, "Headset Mic"}, 41 {"IN1N", NULL, "Headset Mic"}, 42 }; 43 44 static const struct snd_kcontrol_new mt8173_rt5650_rt5514_controls[] = { 45 SOC_DAPM_PIN_SWITCH("Speaker"), 46 SOC_DAPM_PIN_SWITCH("Int Mic"), 47 SOC_DAPM_PIN_SWITCH("Headphone"), 48 SOC_DAPM_PIN_SWITCH("Headset Mic"), 49 }; 50 51 static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream *substream, 52 struct snd_pcm_hw_params *params) 53 { 54 struct snd_soc_pcm_runtime *rtd = substream->private_data; 55 int i, ret; 56 57 for (i = 0; i < rtd->num_codecs; i++) { 58 struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; 59 60 /* pll from mclk 12.288M */ 61 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS, 62 params_rate(params) * 512); 63 if (ret) 64 return ret; 65 66 /* sysclk from pll */ 67 ret = snd_soc_dai_set_sysclk(codec_dai, 1, 68 params_rate(params) * 512, 69 SND_SOC_CLOCK_IN); 70 if (ret) 71 return ret; 72 } 73 return 0; 74 } 75 76 static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = { 77 .hw_params = mt8173_rt5650_rt5514_hw_params, 78 }; 79 80 static struct snd_soc_jack mt8173_rt5650_rt5514_jack; 81 82 static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime) 83 { 84 struct snd_soc_card *card = runtime->card; 85 struct snd_soc_codec *codec = runtime->codec_dais[0]->codec; 86 int ret; 87 88 rt5645_sel_asrc_clk_src(codec, 89 RT5645_DA_STEREO_FILTER | 90 RT5645_AD_STEREO_FILTER, 91 RT5645_CLK_SEL_I2S1_ASRC); 92 93 /* enable jack detection */ 94 ret = snd_soc_card_jack_new(card, "Headset Jack", 95 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE | 96 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 97 SND_JACK_BTN_2 | SND_JACK_BTN_3, 98 &mt8173_rt5650_rt5514_jack, NULL, 0); 99 if (ret) { 100 dev_err(card->dev, "Can't new Headset Jack %d\n", ret); 101 return ret; 102 } 103 104 return rt5645_set_jack_detect(codec, 105 &mt8173_rt5650_rt5514_jack, 106 &mt8173_rt5650_rt5514_jack, 107 &mt8173_rt5650_rt5514_jack); 108 } 109 110 static struct snd_soc_dai_link_component mt8173_rt5650_rt5514_codecs[] = { 111 { 112 .dai_name = "rt5645-aif1", 113 }, 114 { 115 .dai_name = "rt5514-aif1", 116 }, 117 }; 118 119 enum { 120 DAI_LINK_PLAYBACK, 121 DAI_LINK_CAPTURE, 122 DAI_LINK_CODEC_I2S, 123 }; 124 125 /* Digital audio interface glue - connects codec <---> CPU */ 126 static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = { 127 /* Front End DAI links */ 128 [DAI_LINK_PLAYBACK] = { 129 .name = "rt5650_rt5514 Playback", 130 .stream_name = "rt5650_rt5514 Playback", 131 .cpu_dai_name = "DL1", 132 .codec_name = "snd-soc-dummy", 133 .codec_dai_name = "snd-soc-dummy-dai", 134 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 135 .dynamic = 1, 136 .dpcm_playback = 1, 137 }, 138 [DAI_LINK_CAPTURE] = { 139 .name = "rt5650_rt5514 Capture", 140 .stream_name = "rt5650_rt5514 Capture", 141 .cpu_dai_name = "VUL", 142 .codec_name = "snd-soc-dummy", 143 .codec_dai_name = "snd-soc-dummy-dai", 144 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST}, 145 .dynamic = 1, 146 .dpcm_capture = 1, 147 }, 148 /* Back End DAI links */ 149 [DAI_LINK_CODEC_I2S] = { 150 .name = "Codec", 151 .cpu_dai_name = "I2S", 152 .no_pcm = 1, 153 .codecs = mt8173_rt5650_rt5514_codecs, 154 .num_codecs = 2, 155 .init = mt8173_rt5650_rt5514_init, 156 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 157 SND_SOC_DAIFMT_CBS_CFS, 158 .ops = &mt8173_rt5650_rt5514_ops, 159 .ignore_pmdown_time = 1, 160 .dpcm_playback = 1, 161 .dpcm_capture = 1, 162 }, 163 }; 164 165 static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = { 166 { 167 .name_prefix = "Sub", 168 }, 169 }; 170 171 static struct snd_soc_card mt8173_rt5650_rt5514_card = { 172 .name = "mtk-rt5650-rt5514", 173 .owner = THIS_MODULE, 174 .dai_link = mt8173_rt5650_rt5514_dais, 175 .num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais), 176 .codec_conf = mt8173_rt5650_rt5514_codec_conf, 177 .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf), 178 .controls = mt8173_rt5650_rt5514_controls, 179 .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls), 180 .dapm_widgets = mt8173_rt5650_rt5514_widgets, 181 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets), 182 .dapm_routes = mt8173_rt5650_rt5514_routes, 183 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes), 184 }; 185 186 static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev) 187 { 188 struct snd_soc_card *card = &mt8173_rt5650_rt5514_card; 189 struct device_node *platform_node; 190 int i, ret; 191 192 platform_node = of_parse_phandle(pdev->dev.of_node, 193 "mediatek,platform", 0); 194 if (!platform_node) { 195 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n"); 196 return -EINVAL; 197 } 198 199 for (i = 0; i < card->num_links; i++) { 200 if (mt8173_rt5650_rt5514_dais[i].platform_name) 201 continue; 202 mt8173_rt5650_rt5514_dais[i].platform_of_node = platform_node; 203 } 204 205 mt8173_rt5650_rt5514_codecs[0].of_node = 206 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0); 207 if (!mt8173_rt5650_rt5514_codecs[0].of_node) { 208 dev_err(&pdev->dev, 209 "Property 'audio-codec' missing or invalid\n"); 210 return -EINVAL; 211 } 212 mt8173_rt5650_rt5514_codecs[1].of_node = 213 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1); 214 if (!mt8173_rt5650_rt5514_codecs[1].of_node) { 215 dev_err(&pdev->dev, 216 "Property 'audio-codec' missing or invalid\n"); 217 return -EINVAL; 218 } 219 mt8173_rt5650_rt5514_codec_conf[0].of_node = 220 mt8173_rt5650_rt5514_codecs[1].of_node; 221 222 card->dev = &pdev->dev; 223 224 ret = devm_snd_soc_register_card(&pdev->dev, card); 225 if (ret) 226 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n", 227 __func__, ret); 228 return ret; 229 } 230 231 static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = { 232 { .compatible = "mediatek,mt8173-rt5650-rt5514", }, 233 { } 234 }; 235 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match); 236 237 static struct platform_driver mt8173_rt5650_rt5514_driver = { 238 .driver = { 239 .name = "mtk-rt5650-rt5514", 240 .of_match_table = mt8173_rt5650_rt5514_dt_match, 241 #ifdef CONFIG_PM 242 .pm = &snd_soc_pm_ops, 243 #endif 244 }, 245 .probe = mt8173_rt5650_rt5514_dev_probe, 246 }; 247 248 module_platform_driver(mt8173_rt5650_rt5514_driver); 249 250 /* Module information */ 251 MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver"); 252 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>"); 253 MODULE_LICENSE("GPL v2"); 254 MODULE_ALIAS("platform:mtk-rt5650-rt5514"); 255 256