1 // SPDX-License-Identifier: GPL-2.0
3 * mt8173-rt5650-rt5514.c -- MT8173 machine driver with RT5650/5514 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 static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_widgets[] = {
17 SND_SOC_DAPM_SPK("Speaker", NULL),
18 SND_SOC_DAPM_MIC("Int Mic", NULL),
19 SND_SOC_DAPM_HP("Headphone", NULL),
20 SND_SOC_DAPM_MIC("Headset Mic", NULL),
23 static const struct snd_soc_dapm_route mt8173_rt5650_rt5514_routes[] = {
24 {"Speaker", NULL, "SPOL"},
25 {"Speaker", NULL, "SPOR"},
26 {"Sub DMIC1L", NULL, "Int Mic"},
27 {"Sub DMIC1R", NULL, "Int Mic"},
28 {"Headphone", NULL, "HPOL"},
29 {"Headphone", NULL, "HPOR"},
30 {"IN1P", NULL, "Headset Mic"},
31 {"IN1N", NULL, "Headset Mic"},
34 static const struct snd_kcontrol_new mt8173_rt5650_rt5514_controls[] = {
35 SOC_DAPM_PIN_SWITCH("Speaker"),
36 SOC_DAPM_PIN_SWITCH("Int Mic"),
37 SOC_DAPM_PIN_SWITCH("Headphone"),
38 SOC_DAPM_PIN_SWITCH("Headset Mic"),
41 static struct snd_soc_jack_pin mt8173_rt5650_rt5514_jack_pins[] = {
44 .mask = SND_JACK_HEADPHONE,
48 .mask = SND_JACK_MICROPHONE,
52 static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream *substream,
53 struct snd_pcm_hw_params *params)
55 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
56 struct snd_soc_dai *codec_dai;
59 for_each_rtd_codec_dais(rtd, i, codec_dai) {
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);
67 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
68 params_rate(params) * 512,
76 static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = {
77 .hw_params = mt8173_rt5650_rt5514_hw_params,
80 static struct snd_soc_jack mt8173_rt5650_rt5514_jack;
82 static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime)
84 struct snd_soc_card *card = runtime->card;
85 struct snd_soc_component *component = snd_soc_rtd_to_codec(runtime, 0)->component;
88 rt5645_sel_asrc_clk_src(component,
89 RT5645_DA_STEREO_FILTER |
90 RT5645_AD_STEREO_FILTER,
91 RT5645_CLK_SEL_I2S1_ASRC);
93 /* enable jack detection */
94 ret = snd_soc_card_jack_new_pins(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,
99 mt8173_rt5650_rt5514_jack_pins,
100 ARRAY_SIZE(mt8173_rt5650_rt5514_jack_pins));
102 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
106 return rt5645_set_jack_detect(component,
107 &mt8173_rt5650_rt5514_jack,
108 &mt8173_rt5650_rt5514_jack,
109 &mt8173_rt5650_rt5514_jack);
118 SND_SOC_DAILINK_DEFS(playback,
119 DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
120 DAILINK_COMP_ARRAY(COMP_DUMMY()),
121 DAILINK_COMP_ARRAY(COMP_EMPTY()));
123 SND_SOC_DAILINK_DEFS(capture,
124 DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
125 DAILINK_COMP_ARRAY(COMP_DUMMY()),
126 DAILINK_COMP_ARRAY(COMP_EMPTY()));
128 SND_SOC_DAILINK_DEFS(codec,
129 DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
130 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"),
131 COMP_CODEC(NULL, "rt5514-aif1")),
132 DAILINK_COMP_ARRAY(COMP_EMPTY()));
134 /* Digital audio interface glue - connects codec <---> CPU */
135 static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = {
136 /* Front End DAI links */
137 [DAI_LINK_PLAYBACK] = {
138 .name = "rt5650_rt5514 Playback",
139 .stream_name = "rt5650_rt5514 Playback",
140 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
143 SND_SOC_DAILINK_REG(playback),
145 [DAI_LINK_CAPTURE] = {
146 .name = "rt5650_rt5514 Capture",
147 .stream_name = "rt5650_rt5514 Capture",
148 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
151 SND_SOC_DAILINK_REG(capture),
153 /* Back End DAI links */
154 [DAI_LINK_CODEC_I2S] = {
157 .init = mt8173_rt5650_rt5514_init,
158 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
159 SND_SOC_DAIFMT_CBS_CFS,
160 .ops = &mt8173_rt5650_rt5514_ops,
161 .ignore_pmdown_time = 1,
164 SND_SOC_DAILINK_REG(codec),
168 static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = {
170 .name_prefix = "Sub",
174 static struct snd_soc_card mt8173_rt5650_rt5514_card = {
175 .name = "mtk-rt5650-rt5514",
176 .owner = THIS_MODULE,
177 .dai_link = mt8173_rt5650_rt5514_dais,
178 .num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais),
179 .codec_conf = mt8173_rt5650_rt5514_codec_conf,
180 .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf),
181 .controls = mt8173_rt5650_rt5514_controls,
182 .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls),
183 .dapm_widgets = mt8173_rt5650_rt5514_widgets,
184 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets),
185 .dapm_routes = mt8173_rt5650_rt5514_routes,
186 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes),
189 static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev)
191 struct snd_soc_card *card = &mt8173_rt5650_rt5514_card;
192 struct device_node *platform_node;
193 struct snd_soc_dai_link *dai_link;
196 platform_node = of_parse_phandle(pdev->dev.of_node,
197 "mediatek,platform", 0);
198 if (!platform_node) {
199 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
203 for_each_card_prelinks(card, i, dai_link) {
204 if (dai_link->platforms->name)
206 dai_link->platforms->of_node = platform_node;
209 mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
210 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
211 if (!mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
213 "Property 'audio-codec' missing or invalid\n");
217 mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
218 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
219 if (!mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node) {
221 "Property 'audio-codec' missing or invalid\n");
225 mt8173_rt5650_rt5514_codec_conf[0].dlc.of_node =
226 mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node;
228 card->dev = &pdev->dev;
230 ret = devm_snd_soc_register_card(&pdev->dev, card);
233 of_node_put(platform_node);
237 static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = {
238 { .compatible = "mediatek,mt8173-rt5650-rt5514", },
241 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match);
243 static struct platform_driver mt8173_rt5650_rt5514_driver = {
245 .name = "mtk-rt5650-rt5514",
246 .of_match_table = mt8173_rt5650_rt5514_dt_match,
247 .pm = &snd_soc_pm_ops,
249 .probe = mt8173_rt5650_rt5514_dev_probe,
252 module_platform_driver(mt8173_rt5650_rt5514_driver);
254 /* Module information */
255 MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
256 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
257 MODULE_LICENSE("GPL v2");
258 MODULE_ALIAS("platform:mtk-rt5650-rt5514");