1 // SPDX-License-Identifier: GPL-2.0
3 * mt8173-rt5650.c -- MT8173 machine driver with RT5650 codecs
5 * Copyright (c) 2016 MediaTek Inc.
6 * Author: Koro Chen <koro.chen@mediatek.com>
9 #include <linux/module.h>
10 #include <linux/gpio.h>
11 #include <linux/of_gpio.h>
12 #include <sound/soc.h>
13 #include <sound/jack.h>
14 #include "../../codecs/rt5645.h"
16 #define MCLK_FOR_CODECS 12288000
18 enum mt8173_rt5650_mclk {
19 MT8173_RT5650_MCLK_EXTERNAL = 0,
20 MT8173_RT5650_MCLK_INTERNAL,
23 struct mt8173_rt5650_platform_data {
24 enum mt8173_rt5650_mclk pll_from;
25 /* 0 = external oscillator; 1 = internal source from mt8173 */
28 static struct mt8173_rt5650_platform_data mt8173_rt5650_priv = {
29 .pll_from = MT8173_RT5650_MCLK_EXTERNAL,
32 static const struct snd_soc_dapm_widget mt8173_rt5650_widgets[] = {
33 SND_SOC_DAPM_SPK("Speaker", NULL),
34 SND_SOC_DAPM_MIC("Int Mic", NULL),
35 SND_SOC_DAPM_HP("Headphone", NULL),
36 SND_SOC_DAPM_MIC("Headset Mic", NULL),
39 static const struct snd_soc_dapm_route mt8173_rt5650_routes[] = {
40 {"Speaker", NULL, "SPOL"},
41 {"Speaker", NULL, "SPOR"},
42 {"DMIC L1", NULL, "Int Mic"},
43 {"DMIC R1", NULL, "Int Mic"},
44 {"Headphone", NULL, "HPOL"},
45 {"Headphone", NULL, "HPOR"},
46 {"IN1P", NULL, "Headset Mic"},
47 {"IN1N", NULL, "Headset Mic"},
50 static const struct snd_kcontrol_new mt8173_rt5650_controls[] = {
51 SOC_DAPM_PIN_SWITCH("Speaker"),
52 SOC_DAPM_PIN_SWITCH("Int Mic"),
53 SOC_DAPM_PIN_SWITCH("Headphone"),
54 SOC_DAPM_PIN_SWITCH("Headset Mic"),
57 static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
58 struct snd_pcm_hw_params *params)
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
61 unsigned int mclk_clock;
64 switch (mt8173_rt5650_priv.pll_from) {
65 case MT8173_RT5650_MCLK_EXTERNAL:
67 mclk_clock = MCLK_FOR_CODECS;
69 case MT8173_RT5650_MCLK_INTERNAL:
70 /* mclk = sampling rate*256 */
71 mclk_clock = params_rate(params) * 256;
75 mclk_clock = MCLK_FOR_CODECS;
79 for (i = 0; i < rtd->num_codecs; i++) {
80 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
83 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, mclk_clock,
84 params_rate(params) * 512);
89 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
90 params_rate(params) * 512,
98 static const struct snd_soc_ops mt8173_rt5650_ops = {
99 .hw_params = mt8173_rt5650_hw_params,
102 static struct snd_soc_jack mt8173_rt5650_jack;
104 static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
106 struct snd_soc_card *card = runtime->card;
107 struct snd_soc_component *component = runtime->codec_dais[0]->component;
108 const char *codec_capture_dai = runtime->codec_dais[1]->name;
111 rt5645_sel_asrc_clk_src(component,
112 RT5645_DA_STEREO_FILTER,
113 RT5645_CLK_SEL_I2S1_ASRC);
115 if (!strcmp(codec_capture_dai, "rt5645-aif1")) {
116 rt5645_sel_asrc_clk_src(component,
117 RT5645_AD_STEREO_FILTER,
118 RT5645_CLK_SEL_I2S1_ASRC);
119 } else if (!strcmp(codec_capture_dai, "rt5645-aif2")) {
120 rt5645_sel_asrc_clk_src(component,
121 RT5645_AD_STEREO_FILTER,
122 RT5645_CLK_SEL_I2S2_ASRC);
125 "Only one dai codec found in DTS, enabled rt5645 AD filter\n");
126 rt5645_sel_asrc_clk_src(component,
127 RT5645_AD_STEREO_FILTER,
128 RT5645_CLK_SEL_I2S1_ASRC);
131 /* enable jack detection */
132 ret = snd_soc_card_jack_new(card, "Headset Jack",
133 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
134 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
135 SND_JACK_BTN_2 | SND_JACK_BTN_3,
136 &mt8173_rt5650_jack, NULL, 0);
138 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
142 return rt5645_set_jack_detect(component,
145 &mt8173_rt5650_jack);
148 static struct snd_soc_dai_link_component mt8173_rt5650_codecs[] = {
151 .dai_name = "rt5645-aif1",
155 .dai_name = "rt5645-aif1",
167 /* Digital audio interface glue - connects codec <---> CPU */
168 static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
169 /* Front End DAI links */
170 [DAI_LINK_PLAYBACK] = {
171 .name = "rt5650 Playback",
172 .stream_name = "rt5650 Playback",
173 .cpu_dai_name = "DL1",
174 .codec_name = "snd-soc-dummy",
175 .codec_dai_name = "snd-soc-dummy-dai",
176 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
180 [DAI_LINK_CAPTURE] = {
181 .name = "rt5650 Capture",
182 .stream_name = "rt5650 Capture",
183 .cpu_dai_name = "VUL",
184 .codec_name = "snd-soc-dummy",
185 .codec_dai_name = "snd-soc-dummy-dai",
186 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
192 .stream_name = "HDMI PCM",
193 .cpu_dai_name = "HDMI",
194 .codec_name = "snd-soc-dummy",
195 .codec_dai_name = "snd-soc-dummy-dai",
196 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
200 /* Back End DAI links */
201 [DAI_LINK_CODEC_I2S] = {
203 .cpu_dai_name = "I2S",
205 .codecs = mt8173_rt5650_codecs,
207 .init = mt8173_rt5650_init,
208 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
209 SND_SOC_DAIFMT_CBS_CFS,
210 .ops = &mt8173_rt5650_ops,
211 .ignore_pmdown_time = 1,
215 [DAI_LINK_HDMI_I2S] = {
217 .cpu_dai_name = "HDMIO",
219 .codec_dai_name = "i2s-hifi",
224 static struct snd_soc_card mt8173_rt5650_card = {
225 .name = "mtk-rt5650",
226 .owner = THIS_MODULE,
227 .dai_link = mt8173_rt5650_dais,
228 .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
229 .controls = mt8173_rt5650_controls,
230 .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
231 .dapm_widgets = mt8173_rt5650_widgets,
232 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
233 .dapm_routes = mt8173_rt5650_routes,
234 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
237 static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
239 struct snd_soc_card *card = &mt8173_rt5650_card;
240 struct device_node *platform_node;
241 struct device_node *np;
242 const char *codec_capture_dai;
245 platform_node = of_parse_phandle(pdev->dev.of_node,
246 "mediatek,platform", 0);
247 if (!platform_node) {
248 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
252 for (i = 0; i < card->num_links; i++) {
253 if (mt8173_rt5650_dais[i].platform_name)
255 mt8173_rt5650_dais[i].platform_of_node = platform_node;
258 mt8173_rt5650_codecs[0].of_node =
259 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
260 if (!mt8173_rt5650_codecs[0].of_node) {
262 "Property 'audio-codec' missing or invalid\n");
265 mt8173_rt5650_codecs[1].of_node = mt8173_rt5650_codecs[0].of_node;
267 np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
269 ret = snd_soc_of_get_dai_name(np, &codec_capture_dai);
273 "%s codec_capture_dai name fail %d\n",
277 mt8173_rt5650_codecs[1].dai_name = codec_capture_dai;
280 if (device_property_present(&pdev->dev, "mediatek,mclk")) {
281 ret = device_property_read_u32(&pdev->dev,
283 &mt8173_rt5650_priv.pll_from);
286 "%s snd_soc_register_card fail %d\n",
291 mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codec_of_node =
292 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
293 if (!mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codec_of_node) {
295 "Property 'audio-codec' missing or invalid\n");
298 card->dev = &pdev->dev;
300 ret = devm_snd_soc_register_card(&pdev->dev, card);
302 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
305 of_node_put(platform_node);
309 static const struct of_device_id mt8173_rt5650_dt_match[] = {
310 { .compatible = "mediatek,mt8173-rt5650", },
313 MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
315 static struct platform_driver mt8173_rt5650_driver = {
317 .name = "mtk-rt5650",
318 .of_match_table = mt8173_rt5650_dt_match,
320 .pm = &snd_soc_pm_ops,
323 .probe = mt8173_rt5650_dev_probe,
326 module_platform_driver(mt8173_rt5650_driver);
328 /* Module information */
329 MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
330 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
331 MODULE_LICENSE("GPL v2");
332 MODULE_ALIAS("platform:mtk-rt5650");