1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mt8173-rt5650.c -- MT8173 machine driver with RT5650 codecs
4 *
5 * Copyright (c) 2016 MediaTek Inc.
6 * Author: Koro Chen <[email protected]>
7 */
8
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/jack.h>
12 #include "../../codecs/rt5645.h"
13
14 #define MCLK_FOR_CODECS 12288000
15
16 enum mt8173_rt5650_mclk {
17 MT8173_RT5650_MCLK_EXTERNAL = 0,
18 MT8173_RT5650_MCLK_INTERNAL,
19 };
20
21 struct mt8173_rt5650_platform_data {
22 enum mt8173_rt5650_mclk pll_from;
23 /* 0 = external oscillator; 1 = internal source from mt8173 */
24 };
25
26 static struct mt8173_rt5650_platform_data mt8173_rt5650_priv = {
27 .pll_from = MT8173_RT5650_MCLK_EXTERNAL,
28 };
29
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),
35 };
36
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"},
46 };
47
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"),
53 };
54
55 static struct snd_soc_jack_pin mt8173_rt5650_jack_pins[] = {
56 {
57 .pin = "Headphone",
58 .mask = SND_JACK_HEADPHONE,
59 },
60 {
61 .pin = "Headset Mic",
62 .mask = SND_JACK_MICROPHONE,
63 },
64 };
65
mt8173_rt5650_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)66 static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params *params)
68 {
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;
72 int i, ret;
73
74 switch (mt8173_rt5650_priv.pll_from) {
75 case MT8173_RT5650_MCLK_EXTERNAL:
76 /* mclk = 12.288M */
77 mclk_clock = MCLK_FOR_CODECS;
78 break;
79 case MT8173_RT5650_MCLK_INTERNAL:
80 /* mclk = sampling rate*256 */
81 mclk_clock = params_rate(params) * 256;
82 break;
83 default:
84 /* mclk = 12.288M */
85 mclk_clock = MCLK_FOR_CODECS;
86 break;
87 }
88
89 for_each_rtd_codec_dais(rtd, i, codec_dai) {
90 /* pll from mclk */
91 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, mclk_clock,
92 params_rate(params) * 512);
93 if (ret)
94 return ret;
95
96 /* sysclk from pll */
97 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
98 params_rate(params) * 512,
99 SND_SOC_CLOCK_IN);
100 if (ret)
101 return ret;
102 }
103 return 0;
104 }
105
106 static const struct snd_soc_ops mt8173_rt5650_ops = {
107 .hw_params = mt8173_rt5650_hw_params,
108 };
109
110 static struct snd_soc_jack mt8173_rt5650_jack, mt8173_rt5650_hdmi_jack;
111
mt8173_rt5650_init(struct snd_soc_pcm_runtime * runtime)112 static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
113 {
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;
117 int ret;
118
119 rt5645_sel_asrc_clk_src(component,
120 RT5645_DA_STEREO_FILTER,
121 RT5645_CLK_SEL_I2S1_ASRC);
122
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);
131 } else {
132 dev_warn(card->dev,
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);
137 }
138
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,
144 &mt8173_rt5650_jack,
145 mt8173_rt5650_jack_pins,
146 ARRAY_SIZE(mt8173_rt5650_jack_pins));
147 if (ret) {
148 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
149 return ret;
150 }
151
152 return rt5645_set_jack_detect(component,
153 &mt8173_rt5650_jack,
154 &mt8173_rt5650_jack,
155 &mt8173_rt5650_jack);
156 }
157
mt8173_rt5650_hdmi_init(struct snd_soc_pcm_runtime * rtd)158 static int mt8173_rt5650_hdmi_init(struct snd_soc_pcm_runtime *rtd)
159 {
160 int ret;
161
162 ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
163 &mt8173_rt5650_hdmi_jack);
164 if (ret)
165 return ret;
166
167 return snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component,
168 &mt8173_rt5650_hdmi_jack, NULL);
169 }
170
171 enum {
172 DAI_LINK_PLAYBACK,
173 DAI_LINK_CAPTURE,
174 DAI_LINK_HDMI,
175 DAI_LINK_CODEC_I2S,
176 DAI_LINK_HDMI_I2S,
177 };
178
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()));
183
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()));
188
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()));
193
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()));
199
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()));
204
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},
212 .dynamic = 1,
213 .playback_only = 1,
214 SND_SOC_DAILINK_REG(playback),
215 },
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},
220 .dynamic = 1,
221 .capture_only = 1,
222 SND_SOC_DAILINK_REG(capture),
223 },
224 [DAI_LINK_HDMI] = {
225 .name = "HDMI",
226 .stream_name = "HDMI PCM",
227 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
228 .dynamic = 1,
229 .playback_only = 1,
230 SND_SOC_DAILINK_REG(hdmi_pcm),
231 },
232 /* Back End DAI links */
233 [DAI_LINK_CODEC_I2S] = {
234 .name = "Codec",
235 .no_pcm = 1,
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,
241 SND_SOC_DAILINK_REG(codec),
242 },
243 [DAI_LINK_HDMI_I2S] = {
244 .name = "HDMI BE",
245 .no_pcm = 1,
246 .playback_only = 1,
247 .init = mt8173_rt5650_hdmi_init,
248 SND_SOC_DAILINK_REG(hdmi_be),
249 },
250 };
251
252 static struct snd_soc_card mt8173_rt5650_card = {
253 .name = "mtk-rt5650",
254 .owner = THIS_MODULE,
255 .dai_link = mt8173_rt5650_dais,
256 .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
257 .controls = mt8173_rt5650_controls,
258 .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
259 .dapm_widgets = mt8173_rt5650_widgets,
260 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
261 .dapm_routes = mt8173_rt5650_routes,
262 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
263 };
264
mt8173_rt5650_dev_probe(struct platform_device * pdev)265 static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
266 {
267 struct snd_soc_card *card = &mt8173_rt5650_card;
268 struct device_node *platform_node;
269 struct device_node *np;
270 const char *codec_capture_dai;
271 struct snd_soc_dai_link *dai_link;
272 int i, ret;
273
274 platform_node = of_parse_phandle(pdev->dev.of_node,
275 "mediatek,platform", 0);
276 if (!platform_node) {
277 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
278 return -EINVAL;
279 }
280
281 for_each_card_prelinks(card, i, dai_link) {
282 if (dai_link->platforms->name)
283 continue;
284 dai_link->platforms->of_node = platform_node;
285 }
286
287 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
288 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
289 if (!mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
290 dev_err(&pdev->dev,
291 "Property 'audio-codec' missing or invalid\n");
292 ret = -EINVAL;
293 goto put_platform_node;
294 }
295 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
296 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node;
297
298 np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
299 if (np) {
300 ret = snd_soc_of_get_dai_name(np, &codec_capture_dai, 0);
301 of_node_put(np);
302 if (ret < 0) {
303 dev_err(&pdev->dev,
304 "%s codec_capture_dai name fail %d\n",
305 __func__, ret);
306 goto put_platform_node;
307 }
308 mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].dai_name =
309 codec_capture_dai;
310 }
311
312 if (device_property_present(&pdev->dev, "mediatek,mclk")) {
313 ret = device_property_read_u32(&pdev->dev,
314 "mediatek,mclk",
315 &mt8173_rt5650_priv.pll_from);
316 if (ret) {
317 dev_err(&pdev->dev,
318 "%s snd_soc_register_card fail %d\n",
319 __func__, ret);
320 }
321 }
322
323 mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node =
324 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
325 if (!mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node) {
326 dev_err(&pdev->dev,
327 "Property 'audio-codec' missing or invalid\n");
328 ret = -EINVAL;
329 goto put_platform_node;
330 }
331 card->dev = &pdev->dev;
332
333 ret = devm_snd_soc_register_card(&pdev->dev, card);
334
335 put_platform_node:
336 of_node_put(platform_node);
337 return ret;
338 }
339
340 static const struct of_device_id mt8173_rt5650_dt_match[] = {
341 { .compatible = "mediatek,mt8173-rt5650", },
342 { }
343 };
344 MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
345
346 static struct platform_driver mt8173_rt5650_driver = {
347 .driver = {
348 .name = "mtk-rt5650",
349 .of_match_table = mt8173_rt5650_dt_match,
350 .pm = &snd_soc_pm_ops,
351 },
352 .probe = mt8173_rt5650_dev_probe,
353 };
354
355 module_platform_driver(mt8173_rt5650_driver);
356
357 /* Module information */
358 MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
359 MODULE_AUTHOR("Koro Chen <[email protected]>");
360 MODULE_LICENSE("GPL v2");
361 MODULE_ALIAS("platform:mtk-rt5650");
362
363