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
= asoc_substream_to_rtd(substream
);
61 unsigned int mclk_clock
;
62 struct snd_soc_dai
*codec_dai
;
65 switch (mt8173_rt5650_priv
.pll_from
) {
66 case MT8173_RT5650_MCLK_EXTERNAL
:
68 mclk_clock
= MCLK_FOR_CODECS
;
70 case MT8173_RT5650_MCLK_INTERNAL
:
71 /* mclk = sampling rate*256 */
72 mclk_clock
= params_rate(params
) * 256;
76 mclk_clock
= MCLK_FOR_CODECS
;
80 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
82 ret
= snd_soc_dai_set_pll(codec_dai
, 0, 0, mclk_clock
,
83 params_rate(params
) * 512);
88 ret
= snd_soc_dai_set_sysclk(codec_dai
, 1,
89 params_rate(params
) * 512,
97 static const struct snd_soc_ops mt8173_rt5650_ops
= {
98 .hw_params
= mt8173_rt5650_hw_params
,
101 static struct snd_soc_jack mt8173_rt5650_jack
, mt8173_rt5650_hdmi_jack
;
103 static int mt8173_rt5650_init(struct snd_soc_pcm_runtime
*runtime
)
105 struct snd_soc_card
*card
= runtime
->card
;
106 struct snd_soc_component
*component
= asoc_rtd_to_codec(runtime
, 0)->component
;
107 const char *codec_capture_dai
= asoc_rtd_to_codec(runtime
, 1)->name
;
110 rt5645_sel_asrc_clk_src(component
,
111 RT5645_DA_STEREO_FILTER
,
112 RT5645_CLK_SEL_I2S1_ASRC
);
114 if (!strcmp(codec_capture_dai
, "rt5645-aif1")) {
115 rt5645_sel_asrc_clk_src(component
,
116 RT5645_AD_STEREO_FILTER
,
117 RT5645_CLK_SEL_I2S1_ASRC
);
118 } else if (!strcmp(codec_capture_dai
, "rt5645-aif2")) {
119 rt5645_sel_asrc_clk_src(component
,
120 RT5645_AD_STEREO_FILTER
,
121 RT5645_CLK_SEL_I2S2_ASRC
);
124 "Only one dai codec found in DTS, enabled rt5645 AD filter\n");
125 rt5645_sel_asrc_clk_src(component
,
126 RT5645_AD_STEREO_FILTER
,
127 RT5645_CLK_SEL_I2S1_ASRC
);
130 /* enable jack detection */
131 ret
= snd_soc_card_jack_new(card
, "Headset Jack",
132 SND_JACK_HEADPHONE
| SND_JACK_MICROPHONE
|
133 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
134 SND_JACK_BTN_2
| SND_JACK_BTN_3
,
135 &mt8173_rt5650_jack
, NULL
, 0);
137 dev_err(card
->dev
, "Can't new Headset Jack %d\n", ret
);
141 return rt5645_set_jack_detect(component
,
144 &mt8173_rt5650_jack
);
147 static int mt8173_rt5650_hdmi_init(struct snd_soc_pcm_runtime
*rtd
)
151 ret
= snd_soc_card_jack_new(rtd
->card
, "HDMI Jack", SND_JACK_LINEOUT
,
152 &mt8173_rt5650_hdmi_jack
, NULL
, 0);
156 return snd_soc_component_set_jack(asoc_rtd_to_codec(rtd
, 0)->component
,
157 &mt8173_rt5650_hdmi_jack
, NULL
);
168 SND_SOC_DAILINK_DEFS(playback
,
169 DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
170 DAILINK_COMP_ARRAY(COMP_DUMMY()),
171 DAILINK_COMP_ARRAY(COMP_EMPTY()));
173 SND_SOC_DAILINK_DEFS(capture
,
174 DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
175 DAILINK_COMP_ARRAY(COMP_DUMMY()),
176 DAILINK_COMP_ARRAY(COMP_EMPTY()));
178 SND_SOC_DAILINK_DEFS(hdmi_pcm
,
179 DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
180 DAILINK_COMP_ARRAY(COMP_DUMMY()),
181 DAILINK_COMP_ARRAY(COMP_EMPTY()));
183 SND_SOC_DAILINK_DEFS(codec
,
184 DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
185 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "rt5645-aif1"), /* Playback */
186 COMP_CODEC(NULL
, "rt5645-aif1")),/* Capture */
187 DAILINK_COMP_ARRAY(COMP_EMPTY()));
189 SND_SOC_DAILINK_DEFS(hdmi_be
,
190 DAILINK_COMP_ARRAY(COMP_CPU("HDMIO")),
191 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "i2s-hifi")),
192 DAILINK_COMP_ARRAY(COMP_EMPTY()));
194 /* Digital audio interface glue - connects codec <---> CPU */
195 static struct snd_soc_dai_link mt8173_rt5650_dais
[] = {
196 /* Front End DAI links */
197 [DAI_LINK_PLAYBACK
] = {
198 .name
= "rt5650 Playback",
199 .stream_name
= "rt5650 Playback",
200 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
203 SND_SOC_DAILINK_REG(playback
),
205 [DAI_LINK_CAPTURE
] = {
206 .name
= "rt5650 Capture",
207 .stream_name
= "rt5650 Capture",
208 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
211 SND_SOC_DAILINK_REG(capture
),
215 .stream_name
= "HDMI PCM",
216 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
219 SND_SOC_DAILINK_REG(hdmi_pcm
),
221 /* Back End DAI links */
222 [DAI_LINK_CODEC_I2S
] = {
225 .init
= mt8173_rt5650_init
,
226 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
227 SND_SOC_DAIFMT_CBS_CFS
,
228 .ops
= &mt8173_rt5650_ops
,
229 .ignore_pmdown_time
= 1,
232 SND_SOC_DAILINK_REG(codec
),
234 [DAI_LINK_HDMI_I2S
] = {
238 .init
= mt8173_rt5650_hdmi_init
,
239 SND_SOC_DAILINK_REG(hdmi_be
),
243 static struct snd_soc_card mt8173_rt5650_card
= {
244 .name
= "mtk-rt5650",
245 .owner
= THIS_MODULE
,
246 .dai_link
= mt8173_rt5650_dais
,
247 .num_links
= ARRAY_SIZE(mt8173_rt5650_dais
),
248 .controls
= mt8173_rt5650_controls
,
249 .num_controls
= ARRAY_SIZE(mt8173_rt5650_controls
),
250 .dapm_widgets
= mt8173_rt5650_widgets
,
251 .num_dapm_widgets
= ARRAY_SIZE(mt8173_rt5650_widgets
),
252 .dapm_routes
= mt8173_rt5650_routes
,
253 .num_dapm_routes
= ARRAY_SIZE(mt8173_rt5650_routes
),
256 static int mt8173_rt5650_dev_probe(struct platform_device
*pdev
)
258 struct snd_soc_card
*card
= &mt8173_rt5650_card
;
259 struct device_node
*platform_node
;
260 struct device_node
*np
;
261 const char *codec_capture_dai
;
262 struct snd_soc_dai_link
*dai_link
;
265 platform_node
= of_parse_phandle(pdev
->dev
.of_node
,
266 "mediatek,platform", 0);
267 if (!platform_node
) {
268 dev_err(&pdev
->dev
, "Property 'platform' missing or invalid\n");
272 for_each_card_prelinks(card
, i
, dai_link
) {
273 if (dai_link
->platforms
->name
)
275 dai_link
->platforms
->of_node
= platform_node
;
278 mt8173_rt5650_dais
[DAI_LINK_CODEC_I2S
].codecs
[0].of_node
=
279 of_parse_phandle(pdev
->dev
.of_node
, "mediatek,audio-codec", 0);
280 if (!mt8173_rt5650_dais
[DAI_LINK_CODEC_I2S
].codecs
[0].of_node
) {
282 "Property 'audio-codec' missing or invalid\n");
285 mt8173_rt5650_dais
[DAI_LINK_CODEC_I2S
].codecs
[1].of_node
=
286 mt8173_rt5650_dais
[DAI_LINK_CODEC_I2S
].codecs
[0].of_node
;
288 np
= of_get_child_by_name(pdev
->dev
.of_node
, "codec-capture");
290 ret
= snd_soc_of_get_dai_name(np
, &codec_capture_dai
);
294 "%s codec_capture_dai name fail %d\n",
298 mt8173_rt5650_dais
[DAI_LINK_CODEC_I2S
].codecs
[1].dai_name
=
302 if (device_property_present(&pdev
->dev
, "mediatek,mclk")) {
303 ret
= device_property_read_u32(&pdev
->dev
,
305 &mt8173_rt5650_priv
.pll_from
);
308 "%s snd_soc_register_card fail %d\n",
313 mt8173_rt5650_dais
[DAI_LINK_HDMI_I2S
].codecs
->of_node
=
314 of_parse_phandle(pdev
->dev
.of_node
, "mediatek,audio-codec", 1);
315 if (!mt8173_rt5650_dais
[DAI_LINK_HDMI_I2S
].codecs
->of_node
) {
317 "Property 'audio-codec' missing or invalid\n");
320 card
->dev
= &pdev
->dev
;
322 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
324 dev_err(&pdev
->dev
, "%s snd_soc_register_card fail %d\n",
329 static const struct of_device_id mt8173_rt5650_dt_match
[] = {
330 { .compatible
= "mediatek,mt8173-rt5650", },
333 MODULE_DEVICE_TABLE(of
, mt8173_rt5650_dt_match
);
335 static struct platform_driver mt8173_rt5650_driver
= {
337 .name
= "mtk-rt5650",
338 .of_match_table
= mt8173_rt5650_dt_match
,
340 .pm
= &snd_soc_pm_ops
,
343 .probe
= mt8173_rt5650_dev_probe
,
346 module_platform_driver(mt8173_rt5650_driver
);
348 /* Module information */
349 MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
350 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
351 MODULE_LICENSE("GPL v2");
352 MODULE_ALIAS("platform:mtk-rt5650");