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 <sound/soc.h>
11 #include <sound/jack.h>
12 #include "../../codecs/rt5645.h"
14 #define MCLK_FOR_CODECS 12288000
16 enum mt8173_rt5650_mclk {
17 MT8173_RT5650_MCLK_EXTERNAL = 0,
18 MT8173_RT5650_MCLK_INTERNAL,
21 struct mt8173_rt5650_platform_data {
22 enum mt8173_rt5650_mclk pll_from;
23 /* 0 = external oscillator; 1 = internal source from mt8173 */
26 static struct mt8173_rt5650_platform_data mt8173_rt5650_priv = {
27 .pll_from = MT8173_RT5650_MCLK_EXTERNAL,
30 static const struct snd_soc_dapm_widget mt8173_rt5650_widgets[] = {
31 SND_SOC_DAPM_SPK("Ext Spk", NULL),
32 SND_SOC_DAPM_MIC("Int Mic", NULL),
33 SND_SOC_DAPM_HP("Headphone", NULL),
34 SND_SOC_DAPM_MIC("Headset Mic", NULL),
37 static const struct snd_soc_dapm_route mt8173_rt5650_routes[] = {
38 {"Ext Spk", NULL, "SPOL"},
39 {"Ext Spk", NULL, "SPOR"},
40 {"DMIC L1", NULL, "Int Mic"},
41 {"DMIC R1", NULL, "Int Mic"},
42 {"Headphone", NULL, "HPOL"},
43 {"Headphone", NULL, "HPOR"},
44 {"IN1P", NULL, "Headset Mic"},
45 {"IN1N", NULL, "Headset Mic"},
48 static const struct snd_kcontrol_new mt8173_rt5650_controls[] = {
49 SOC_DAPM_PIN_SWITCH("Ext Spk"),
50 SOC_DAPM_PIN_SWITCH("Int Mic"),
51 SOC_DAPM_PIN_SWITCH("Headphone"),
52 SOC_DAPM_PIN_SWITCH("Headset Mic"),
55 static struct snd_soc_jack_pin mt8173_rt5650_jack_pins[] = {
58 .mask = SND_JACK_HEADPHONE,
62 .mask = SND_JACK_MICROPHONE,
66 static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
69 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
70 unsigned int mclk_clock;
71 struct snd_soc_dai *codec_dai;
74 switch (mt8173_rt5650_priv.pll_from) {
75 case MT8173_RT5650_MCLK_EXTERNAL:
77 mclk_clock = MCLK_FOR_CODECS;
79 case MT8173_RT5650_MCLK_INTERNAL:
80 /* mclk = sampling rate*256 */
81 mclk_clock = params_rate(params) * 256;
85 mclk_clock = MCLK_FOR_CODECS;
89 for_each_rtd_codec_dais(rtd, i, codec_dai) {
91 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, mclk_clock,
92 params_rate(params) * 512);
97 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
98 params_rate(params) * 512,
106 static const struct snd_soc_ops mt8173_rt5650_ops = {
107 .hw_params = mt8173_rt5650_hw_params,
110 static struct snd_soc_jack mt8173_rt5650_jack, mt8173_rt5650_hdmi_jack;
112 static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
114 struct snd_soc_card *card = runtime->card;
115 struct snd_soc_component *component = snd_soc_rtd_to_codec(runtime, 0)->component;
116 const char *codec_capture_dai = snd_soc_rtd_to_codec(runtime, 1)->name;
119 rt5645_sel_asrc_clk_src(component,
120 RT5645_DA_STEREO_FILTER,
121 RT5645_CLK_SEL_I2S1_ASRC);
123 if (!strcmp(codec_capture_dai, "rt5645-aif1")) {
124 rt5645_sel_asrc_clk_src(component,
125 RT5645_AD_STEREO_FILTER,
126 RT5645_CLK_SEL_I2S1_ASRC);
127 } else if (!strcmp(codec_capture_dai, "rt5645-aif2")) {
128 rt5645_sel_asrc_clk_src(component,
129 RT5645_AD_STEREO_FILTER,
130 RT5645_CLK_SEL_I2S2_ASRC);
133 "Only one dai codec found in DTS, enabled rt5645 AD filter\n");
134 rt5645_sel_asrc_clk_src(component,
135 RT5645_AD_STEREO_FILTER,
136 RT5645_CLK_SEL_I2S1_ASRC);
139 /* enable jack detection */
140 ret = snd_soc_card_jack_new_pins(card, "Headset Jack",
141 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
142 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
143 SND_JACK_BTN_2 | SND_JACK_BTN_3,
145 mt8173_rt5650_jack_pins,
146 ARRAY_SIZE(mt8173_rt5650_jack_pins));
148 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
152 return rt5645_set_jack_detect(component,
155 &mt8173_rt5650_jack);
158 static int mt8173_rt5650_hdmi_init(struct snd_soc_pcm_runtime *rtd)
162 ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
163 &mt8173_rt5650_hdmi_jack);
167 return snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component,
168 &mt8173_rt5650_hdmi_jack, NULL);
179 SND_SOC_DAILINK_DEFS(playback,
180 DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
181 DAILINK_COMP_ARRAY(COMP_DUMMY()),
182 DAILINK_COMP_ARRAY(COMP_EMPTY()));
184 SND_SOC_DAILINK_DEFS(capture,
185 DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
186 DAILINK_COMP_ARRAY(COMP_DUMMY()),
187 DAILINK_COMP_ARRAY(COMP_EMPTY()));
189 SND_SOC_DAILINK_DEFS(hdmi_pcm,
190 DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
191 DAILINK_COMP_ARRAY(COMP_DUMMY()),
192 DAILINK_COMP_ARRAY(COMP_EMPTY()));
194 SND_SOC_DAILINK_DEFS(codec,
195 DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
196 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"), /* Playback */
197 COMP_CODEC(NULL, "rt5645-aif1")),/* Capture */
198 DAILINK_COMP_ARRAY(COMP_EMPTY()));
200 SND_SOC_DAILINK_DEFS(hdmi_be,
201 DAILINK_COMP_ARRAY(COMP_CPU("HDMIO")),
202 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
203 DAILINK_COMP_ARRAY(COMP_EMPTY()));
205 /* Digital audio interface glue - connects codec <---> CPU */
206 static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
207 /* Front End DAI links */
208 [DAI_LINK_PLAYBACK] = {
209 .name = "rt5650 Playback",
210 .stream_name = "rt5650 Playback",
211 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
214 SND_SOC_DAILINK_REG(playback),
216 [DAI_LINK_CAPTURE] = {
217 .name = "rt5650 Capture",
218 .stream_name = "rt5650 Capture",
219 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
222 SND_SOC_DAILINK_REG(capture),
226 .stream_name = "HDMI PCM",
227 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
230 SND_SOC_DAILINK_REG(hdmi_pcm),
232 /* Back End DAI links */
233 [DAI_LINK_CODEC_I2S] = {
236 .init = mt8173_rt5650_init,
237 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
238 SND_SOC_DAIFMT_CBS_CFS,
239 .ops = &mt8173_rt5650_ops,
240 .ignore_pmdown_time = 1,
243 SND_SOC_DAILINK_REG(codec),
245 [DAI_LINK_HDMI_I2S] = {
249 .init = mt8173_rt5650_hdmi_init,
250 SND_SOC_DAILINK_REG(hdmi_be),
254 static struct snd_soc_card mt8173_rt5650_card = {
255 .name = "mtk-rt5650",
256 .owner = THIS_MODULE,
257 .dai_link = mt8173_rt5650_dais,
258 .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
259 .controls = mt8173_rt5650_controls,
260 .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
261 .dapm_widgets = mt8173_rt5650_widgets,
262 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
263 .dapm_routes = mt8173_rt5650_routes,
264 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
267 static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
269 struct snd_soc_card *card = &mt8173_rt5650_card;
270 struct device_node *platform_node;
271 struct device_node *np;
272 const char *codec_capture_dai;
273 struct snd_soc_dai_link *dai_link;
276 platform_node = of_parse_phandle(pdev->dev.of_node,
277 "mediatek,platform", 0);
278 if (!platform_node) {
279 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
283 for_each_card_prelinks(card, i, dai_link) {
284 if (dai_link->platforms->name)
286 dai_link->platforms->of_node = platform_node;
289 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
290 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
291 if (!mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
293 "Property 'audio-codec' missing or invalid\n");
295 goto put_platform_node;
297 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
298 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node;
300 np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
302 ret = snd_soc_of_get_dai_name(np, &codec_capture_dai, 0);
306 "%s codec_capture_dai name fail %d\n",
308 goto put_platform_node;
310 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].dai_name =
314 if (device_property_present(&pdev->dev, "mediatek,mclk")) {
315 ret = device_property_read_u32(&pdev->dev,
317 &mt8173_rt5650_priv.pll_from);
320 "%s snd_soc_register_card fail %d\n",
325 mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node =
326 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
327 if (!mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node) {
329 "Property 'audio-codec' missing or invalid\n");
331 goto put_platform_node;
333 card->dev = &pdev->dev;
335 ret = devm_snd_soc_register_card(&pdev->dev, card);
338 of_node_put(platform_node);
342 static const struct of_device_id mt8173_rt5650_dt_match[] = {
343 { .compatible = "mediatek,mt8173-rt5650", },
346 MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
348 static struct platform_driver mt8173_rt5650_driver = {
350 .name = "mtk-rt5650",
351 .of_match_table = mt8173_rt5650_dt_match,
352 .pm = &snd_soc_pm_ops,
354 .probe = mt8173_rt5650_dev_probe,
357 module_platform_driver(mt8173_rt5650_driver);
359 /* Module information */
360 MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
361 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
362 MODULE_LICENSE("GPL v2");
363 MODULE_ALIAS("platform:mtk-rt5650");