1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mt8173-rt5650-rt5676.c -- MT8173 machine driver with RT5650/5676 codecs
4 *
5 * Copyright (c) 2015 MediaTek Inc.
6 * Author: Koro Chen <koro.chen@mediatek.com>
7 */
8
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"
15 #include "../../codecs/rt5677.h"
16
17 #define MCLK_FOR_CODECS 12288000
18
19 static const struct snd_soc_dapm_widget mt8173_rt5650_rt5676_widgets[] = {
20 SND_SOC_DAPM_SPK("Speaker", NULL),
21 SND_SOC_DAPM_MIC("Int Mic", NULL),
22 SND_SOC_DAPM_HP("Headphone", NULL),
23 SND_SOC_DAPM_MIC("Headset Mic", NULL),
24 };
25
26 static const struct snd_soc_dapm_route mt8173_rt5650_rt5676_routes[] = {
27 {"Speaker", NULL, "SPOL"},
28 {"Speaker", NULL, "SPOR"},
29 {"Speaker", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
30 {"Sub DMIC L1", NULL, "Int Mic"}, /* DMIC from 5676 */
31 {"Sub DMIC R1", NULL, "Int Mic"},
32 {"Headphone", NULL, "HPOL"},
33 {"Headphone", NULL, "HPOR"},
34 {"Headphone", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
35 {"IN1P", NULL, "Headset Mic"},
36 {"IN1N", NULL, "Headset Mic"},
37 {"Sub AIF2RX", NULL, "Headset Mic"}, /* IF2 DAC from 5650 */
38 };
39
40 static const struct snd_kcontrol_new mt8173_rt5650_rt5676_controls[] = {
41 SOC_DAPM_PIN_SWITCH("Speaker"),
42 SOC_DAPM_PIN_SWITCH("Int Mic"),
43 SOC_DAPM_PIN_SWITCH("Headphone"),
44 SOC_DAPM_PIN_SWITCH("Headset Mic"),
45 };
46
mt8173_rt5650_rt5676_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)47 static int mt8173_rt5650_rt5676_hw_params(struct snd_pcm_substream *substream,
48 struct snd_pcm_hw_params *params)
49 {
50 struct snd_soc_pcm_runtime *rtd = substream->private_data;
51 int i, ret;
52
53 for (i = 0; i < rtd->num_codecs; i++) {
54 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
55
56 /* pll from mclk 12.288M */
57 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
58 params_rate(params) * 512);
59 if (ret)
60 return ret;
61
62 /* sysclk from pll */
63 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
64 params_rate(params) * 512,
65 SND_SOC_CLOCK_IN);
66 if (ret)
67 return ret;
68 }
69 return 0;
70 }
71
72 static const struct snd_soc_ops mt8173_rt5650_rt5676_ops = {
73 .hw_params = mt8173_rt5650_rt5676_hw_params,
74 };
75
76 static struct snd_soc_jack mt8173_rt5650_rt5676_jack;
77
mt8173_rt5650_rt5676_init(struct snd_soc_pcm_runtime * runtime)78 static int mt8173_rt5650_rt5676_init(struct snd_soc_pcm_runtime *runtime)
79 {
80 struct snd_soc_card *card = runtime->card;
81 struct snd_soc_component *component = runtime->codec_dais[0]->component;
82 struct snd_soc_component *component_sub = runtime->codec_dais[1]->component;
83 int ret;
84
85 rt5645_sel_asrc_clk_src(component,
86 RT5645_DA_STEREO_FILTER |
87 RT5645_AD_STEREO_FILTER,
88 RT5645_CLK_SEL_I2S1_ASRC);
89 rt5677_sel_asrc_clk_src(component_sub,
90 RT5677_DA_STEREO_FILTER |
91 RT5677_AD_STEREO1_FILTER,
92 RT5677_CLK_SEL_I2S1_ASRC);
93 rt5677_sel_asrc_clk_src(component_sub,
94 RT5677_AD_STEREO2_FILTER |
95 RT5677_I2S2_SOURCE,
96 RT5677_CLK_SEL_I2S2_ASRC);
97
98 /* enable jack detection */
99 ret = snd_soc_card_jack_new(card, "Headset Jack",
100 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
101 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
102 SND_JACK_BTN_2 | SND_JACK_BTN_3,
103 &mt8173_rt5650_rt5676_jack, NULL, 0);
104 if (ret) {
105 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
106 return ret;
107 }
108
109 return rt5645_set_jack_detect(component,
110 &mt8173_rt5650_rt5676_jack,
111 &mt8173_rt5650_rt5676_jack,
112 &mt8173_rt5650_rt5676_jack);
113 }
114
115 static struct snd_soc_dai_link_component mt8173_rt5650_rt5676_codecs[] = {
116 {
117 .dai_name = "rt5645-aif1",
118 },
119 {
120 .dai_name = "rt5677-aif1",
121 },
122 };
123
124 enum {
125 DAI_LINK_PLAYBACK,
126 DAI_LINK_CAPTURE,
127 DAI_LINK_HDMI,
128 DAI_LINK_CODEC_I2S,
129 DAI_LINK_HDMI_I2S,
130 DAI_LINK_INTERCODEC
131 };
132
133 /* Digital audio interface glue - connects codec <---> CPU */
134 static struct snd_soc_dai_link mt8173_rt5650_rt5676_dais[] = {
135 /* Front End DAI links */
136 [DAI_LINK_PLAYBACK] = {
137 .name = "rt5650_rt5676 Playback",
138 .stream_name = "rt5650_rt5676 Playback",
139 .cpu_dai_name = "DL1",
140 .codec_name = "snd-soc-dummy",
141 .codec_dai_name = "snd-soc-dummy-dai",
142 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
143 .dynamic = 1,
144 .dpcm_playback = 1,
145 },
146 [DAI_LINK_CAPTURE] = {
147 .name = "rt5650_rt5676 Capture",
148 .stream_name = "rt5650_rt5676 Capture",
149 .cpu_dai_name = "VUL",
150 .codec_name = "snd-soc-dummy",
151 .codec_dai_name = "snd-soc-dummy-dai",
152 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
153 .dynamic = 1,
154 .dpcm_capture = 1,
155 },
156 [DAI_LINK_HDMI] = {
157 .name = "HDMI",
158 .stream_name = "HDMI PCM",
159 .cpu_dai_name = "HDMI",
160 .codec_name = "snd-soc-dummy",
161 .codec_dai_name = "snd-soc-dummy-dai",
162 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
163 .dynamic = 1,
164 .dpcm_playback = 1,
165 },
166
167 /* Back End DAI links */
168 [DAI_LINK_CODEC_I2S] = {
169 .name = "Codec",
170 .cpu_dai_name = "I2S",
171 .no_pcm = 1,
172 .codecs = mt8173_rt5650_rt5676_codecs,
173 .num_codecs = 2,
174 .init = mt8173_rt5650_rt5676_init,
175 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
176 SND_SOC_DAIFMT_CBS_CFS,
177 .ops = &mt8173_rt5650_rt5676_ops,
178 .ignore_pmdown_time = 1,
179 .dpcm_playback = 1,
180 .dpcm_capture = 1,
181 },
182 [DAI_LINK_HDMI_I2S] = {
183 .name = "HDMI BE",
184 .cpu_dai_name = "HDMIO",
185 .no_pcm = 1,
186 .codec_dai_name = "i2s-hifi",
187 .dpcm_playback = 1,
188 },
189 /* rt5676 <-> rt5650 intercodec link: Sets rt5676 I2S2 as master */
190 [DAI_LINK_INTERCODEC] = {
191 .name = "rt5650_rt5676 intercodec",
192 .stream_name = "rt5650_rt5676 intercodec",
193 .cpu_dai_name = "snd-soc-dummy-dai",
194 .platform_name = "snd-soc-dummy",
195 .no_pcm = 1,
196 .codec_dai_name = "rt5677-aif2",
197 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
198 SND_SOC_DAIFMT_CBM_CFM,
199 },
200
201 };
202
203 static struct snd_soc_codec_conf mt8173_rt5650_rt5676_codec_conf[] = {
204 {
205 .name_prefix = "Sub",
206 },
207 };
208
209 static struct snd_soc_card mt8173_rt5650_rt5676_card = {
210 .name = "mtk-rt5650-rt5676",
211 .owner = THIS_MODULE,
212 .dai_link = mt8173_rt5650_rt5676_dais,
213 .num_links = ARRAY_SIZE(mt8173_rt5650_rt5676_dais),
214 .codec_conf = mt8173_rt5650_rt5676_codec_conf,
215 .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5676_codec_conf),
216 .controls = mt8173_rt5650_rt5676_controls,
217 .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5676_controls),
218 .dapm_widgets = mt8173_rt5650_rt5676_widgets,
219 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5676_widgets),
220 .dapm_routes = mt8173_rt5650_rt5676_routes,
221 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5676_routes),
222 };
223
mt8173_rt5650_rt5676_dev_probe(struct platform_device * pdev)224 static int mt8173_rt5650_rt5676_dev_probe(struct platform_device *pdev)
225 {
226 struct snd_soc_card *card = &mt8173_rt5650_rt5676_card;
227 struct device_node *platform_node;
228 int i, ret;
229
230 platform_node = of_parse_phandle(pdev->dev.of_node,
231 "mediatek,platform", 0);
232 if (!platform_node) {
233 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
234 return -EINVAL;
235 }
236
237 for (i = 0; i < card->num_links; i++) {
238 if (mt8173_rt5650_rt5676_dais[i].platform_name)
239 continue;
240 mt8173_rt5650_rt5676_dais[i].platform_of_node = platform_node;
241 }
242
243 mt8173_rt5650_rt5676_codecs[0].of_node =
244 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
245 if (!mt8173_rt5650_rt5676_codecs[0].of_node) {
246 dev_err(&pdev->dev,
247 "Property 'audio-codec' missing or invalid\n");
248 return -EINVAL;
249 }
250 mt8173_rt5650_rt5676_codecs[1].of_node =
251 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
252 if (!mt8173_rt5650_rt5676_codecs[1].of_node) {
253 dev_err(&pdev->dev,
254 "Property 'audio-codec' missing or invalid\n");
255 return -EINVAL;
256 }
257 mt8173_rt5650_rt5676_codec_conf[0].of_node =
258 mt8173_rt5650_rt5676_codecs[1].of_node;
259
260 mt8173_rt5650_rt5676_dais[DAI_LINK_INTERCODEC].codec_of_node =
261 mt8173_rt5650_rt5676_codecs[1].of_node;
262
263 mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codec_of_node =
264 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 2);
265 if (!mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codec_of_node) {
266 dev_err(&pdev->dev,
267 "Property 'audio-codec' missing or invalid\n");
268 return -EINVAL;
269 }
270
271 card->dev = &pdev->dev;
272
273 ret = devm_snd_soc_register_card(&pdev->dev, card);
274 if (ret)
275 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
276 __func__, ret);
277 return ret;
278 }
279
280 static const struct of_device_id mt8173_rt5650_rt5676_dt_match[] = {
281 { .compatible = "mediatek,mt8173-rt5650-rt5676", },
282 { }
283 };
284 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5676_dt_match);
285
286 static struct platform_driver mt8173_rt5650_rt5676_driver = {
287 .driver = {
288 .name = "mtk-rt5650-rt5676",
289 .of_match_table = mt8173_rt5650_rt5676_dt_match,
290 #ifdef CONFIG_PM
291 .pm = &snd_soc_pm_ops,
292 #endif
293 },
294 .probe = mt8173_rt5650_rt5676_dev_probe,
295 };
296
297 module_platform_driver(mt8173_rt5650_rt5676_driver);
298
299 /* Module information */
300 MODULE_DESCRIPTION("MT8173 RT5650 and RT5676 SoC machine driver");
301 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
302 MODULE_LICENSE("GPL v2");
303 MODULE_ALIAS("platform:mtk-rt5650-rt5676");
304
305