xref: /linux/sound/soc/mediatek/mt8173/mt8173-rt5650.c (revision f474808acb3c4b30552d9c59b181244e0300d218)
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 <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 
16 #define MCLK_FOR_CODECS		12288000
17 
18 enum mt8173_rt5650_mclk {
19 	MT8173_RT5650_MCLK_EXTERNAL = 0,
20 	MT8173_RT5650_MCLK_INTERNAL,
21 };
22 
23 struct mt8173_rt5650_platform_data {
24 	enum mt8173_rt5650_mclk pll_from;
25 	/* 0 = external oscillator; 1 = internal source from mt8173 */
26 };
27 
28 static struct mt8173_rt5650_platform_data mt8173_rt5650_priv = {
29 	.pll_from = MT8173_RT5650_MCLK_EXTERNAL,
30 };
31 
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),
37 };
38 
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"},
48 };
49 
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"),
55 };
56 
57 static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
58 				   struct snd_pcm_hw_params *params)
59 {
60 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
61 	unsigned int mclk_clock;
62 	struct snd_soc_dai *codec_dai;
63 	int i, ret;
64 
65 	switch (mt8173_rt5650_priv.pll_from) {
66 	case MT8173_RT5650_MCLK_EXTERNAL:
67 		/* mclk = 12.288M */
68 		mclk_clock = MCLK_FOR_CODECS;
69 		break;
70 	case MT8173_RT5650_MCLK_INTERNAL:
71 		/* mclk = sampling rate*256 */
72 		mclk_clock = params_rate(params) * 256;
73 		break;
74 	default:
75 		/* mclk = 12.288M */
76 		mclk_clock = MCLK_FOR_CODECS;
77 		break;
78 	}
79 
80 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
81 		/* pll from mclk */
82 		ret = snd_soc_dai_set_pll(codec_dai, 0, 0, mclk_clock,
83 					  params_rate(params) * 512);
84 		if (ret)
85 			return ret;
86 
87 		/* sysclk from pll */
88 		ret = snd_soc_dai_set_sysclk(codec_dai, 1,
89 					     params_rate(params) * 512,
90 					     SND_SOC_CLOCK_IN);
91 		if (ret)
92 			return ret;
93 	}
94 	return 0;
95 }
96 
97 static const struct snd_soc_ops mt8173_rt5650_ops = {
98 	.hw_params = mt8173_rt5650_hw_params,
99 };
100 
101 static struct snd_soc_jack mt8173_rt5650_jack;
102 
103 static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
104 {
105 	struct snd_soc_card *card = runtime->card;
106 	struct snd_soc_component *component = runtime->codec_dais[0]->component;
107 	const char *codec_capture_dai = runtime->codec_dais[1]->name;
108 	int ret;
109 
110 	rt5645_sel_asrc_clk_src(component,
111 				RT5645_DA_STEREO_FILTER,
112 				RT5645_CLK_SEL_I2S1_ASRC);
113 
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);
122 	} else {
123 		dev_warn(card->dev,
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);
128 	}
129 
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);
136 	if (ret) {
137 		dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
138 		return ret;
139 	}
140 
141 	return rt5645_set_jack_detect(component,
142 				      &mt8173_rt5650_jack,
143 				      &mt8173_rt5650_jack,
144 				      &mt8173_rt5650_jack);
145 }
146 
147 enum {
148 	DAI_LINK_PLAYBACK,
149 	DAI_LINK_CAPTURE,
150 	DAI_LINK_HDMI,
151 	DAI_LINK_CODEC_I2S,
152 	DAI_LINK_HDMI_I2S,
153 };
154 
155 SND_SOC_DAILINK_DEFS(playback,
156 	DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
157 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
158 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
159 
160 SND_SOC_DAILINK_DEFS(capture,
161 	DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
162 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
163 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
164 
165 SND_SOC_DAILINK_DEFS(hdmi_pcm,
166 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
167 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
168 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
169 
170 SND_SOC_DAILINK_DEFS(codec,
171 	DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
172 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"), /* Playback */
173 			   COMP_CODEC(NULL, "rt5645-aif1")),/* Capture */
174 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
175 
176 SND_SOC_DAILINK_DEFS(hdmi_be,
177 	DAILINK_COMP_ARRAY(COMP_CPU("HDMIO")),
178 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
179 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
180 
181 /* Digital audio interface glue - connects codec <---> CPU */
182 static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
183 	/* Front End DAI links */
184 	[DAI_LINK_PLAYBACK] = {
185 		.name = "rt5650 Playback",
186 		.stream_name = "rt5650 Playback",
187 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
188 		.dynamic = 1,
189 		.dpcm_playback = 1,
190 		SND_SOC_DAILINK_REG(playback),
191 	},
192 	[DAI_LINK_CAPTURE] = {
193 		.name = "rt5650 Capture",
194 		.stream_name = "rt5650 Capture",
195 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
196 		.dynamic = 1,
197 		.dpcm_capture = 1,
198 		SND_SOC_DAILINK_REG(capture),
199 	},
200 	[DAI_LINK_HDMI] = {
201 		.name = "HDMI",
202 		.stream_name = "HDMI PCM",
203 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
204 		.dynamic = 1,
205 		.dpcm_playback = 1,
206 		SND_SOC_DAILINK_REG(hdmi_pcm),
207 	},
208 	/* Back End DAI links */
209 	[DAI_LINK_CODEC_I2S] = {
210 		.name = "Codec",
211 		.no_pcm = 1,
212 		.init = mt8173_rt5650_init,
213 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
214 			   SND_SOC_DAIFMT_CBS_CFS,
215 		.ops = &mt8173_rt5650_ops,
216 		.ignore_pmdown_time = 1,
217 		.dpcm_playback = 1,
218 		.dpcm_capture = 1,
219 		SND_SOC_DAILINK_REG(codec),
220 	},
221 	[DAI_LINK_HDMI_I2S] = {
222 		.name = "HDMI BE",
223 		.no_pcm = 1,
224 		.dpcm_playback = 1,
225 		SND_SOC_DAILINK_REG(hdmi_be),
226 	},
227 };
228 
229 static struct snd_soc_card mt8173_rt5650_card = {
230 	.name = "mtk-rt5650",
231 	.owner = THIS_MODULE,
232 	.dai_link = mt8173_rt5650_dais,
233 	.num_links = ARRAY_SIZE(mt8173_rt5650_dais),
234 	.controls = mt8173_rt5650_controls,
235 	.num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
236 	.dapm_widgets = mt8173_rt5650_widgets,
237 	.num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
238 	.dapm_routes = mt8173_rt5650_routes,
239 	.num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
240 };
241 
242 static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
243 {
244 	struct snd_soc_card *card = &mt8173_rt5650_card;
245 	struct device_node *platform_node;
246 	struct device_node *np;
247 	const char *codec_capture_dai;
248 	struct snd_soc_dai_link *dai_link;
249 	int i, ret;
250 
251 	platform_node = of_parse_phandle(pdev->dev.of_node,
252 					 "mediatek,platform", 0);
253 	if (!platform_node) {
254 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
255 		return -EINVAL;
256 	}
257 
258 	for_each_card_prelinks(card, i, dai_link) {
259 		if (dai_link->platforms->name)
260 			continue;
261 		dai_link->platforms->of_node = platform_node;
262 	}
263 
264 	mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
265 		of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
266 	if (!mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
267 		dev_err(&pdev->dev,
268 			"Property 'audio-codec' missing or invalid\n");
269 		return -EINVAL;
270 	}
271 	mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
272 		mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node;
273 
274 	np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
275 	if (np) {
276 		ret = snd_soc_of_get_dai_name(np, &codec_capture_dai);
277 		of_node_put(np);
278 		if (ret < 0) {
279 			dev_err(&pdev->dev,
280 				"%s codec_capture_dai name fail %d\n",
281 				__func__, ret);
282 			return ret;
283 		}
284 		mt8173_rt5650_dais[DAI_LINK_CODEC_I2S].codecs[1].dai_name =
285 			codec_capture_dai;
286 	}
287 
288 	if (device_property_present(&pdev->dev, "mediatek,mclk")) {
289 		ret = device_property_read_u32(&pdev->dev,
290 					       "mediatek,mclk",
291 					       &mt8173_rt5650_priv.pll_from);
292 		if (ret) {
293 			dev_err(&pdev->dev,
294 				"%s snd_soc_register_card fail %d\n",
295 				__func__, ret);
296 		}
297 	}
298 
299 	mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node =
300 		of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
301 	if (!mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codecs->of_node) {
302 		dev_err(&pdev->dev,
303 			"Property 'audio-codec' missing or invalid\n");
304 		return -EINVAL;
305 	}
306 	card->dev = &pdev->dev;
307 
308 	ret = devm_snd_soc_register_card(&pdev->dev, card);
309 	if (ret)
310 		dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
311 			__func__, ret);
312 	return ret;
313 }
314 
315 static const struct of_device_id mt8173_rt5650_dt_match[] = {
316 	{ .compatible = "mediatek,mt8173-rt5650", },
317 	{ }
318 };
319 MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
320 
321 static struct platform_driver mt8173_rt5650_driver = {
322 	.driver = {
323 		   .name = "mtk-rt5650",
324 		   .of_match_table = mt8173_rt5650_dt_match,
325 #ifdef CONFIG_PM
326 		   .pm = &snd_soc_pm_ops,
327 #endif
328 	},
329 	.probe = mt8173_rt5650_dev_probe,
330 };
331 
332 module_platform_driver(mt8173_rt5650_driver);
333 
334 /* Module information */
335 MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
336 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
337 MODULE_LICENSE("GPL v2");
338 MODULE_ALIAS("platform:mtk-rt5650");
339 
340