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 
47 static struct snd_soc_jack_pin mt8173_rt5650_rt5676_jack_pins[] = {
48 	{
49 		.pin	= "Headphone",
50 		.mask	= SND_JACK_HEADPHONE,
51 	},
52 	{
53 		.pin	= "Headset Mic",
54 		.mask	= SND_JACK_MICROPHONE,
55 	},
56 };
57 
mt8173_rt5650_rt5676_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)58 static int mt8173_rt5650_rt5676_hw_params(struct snd_pcm_substream *substream,
59 					  struct snd_pcm_hw_params *params)
60 {
61 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
62 	struct snd_soc_dai *codec_dai;
63 	int i, ret;
64 
65 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
66 		/* pll from mclk 12.288M */
67 		ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
68 					  params_rate(params) * 512);
69 		if (ret)
70 			return ret;
71 
72 		/* sysclk from pll */
73 		ret = snd_soc_dai_set_sysclk(codec_dai, 1,
74 					     params_rate(params) * 512,
75 					     SND_SOC_CLOCK_IN);
76 		if (ret)
77 			return ret;
78 	}
79 	return 0;
80 }
81 
82 static const struct snd_soc_ops mt8173_rt5650_rt5676_ops = {
83 	.hw_params = mt8173_rt5650_rt5676_hw_params,
84 };
85 
86 static struct snd_soc_jack mt8173_rt5650_rt5676_jack;
87 
mt8173_rt5650_rt5676_init(struct snd_soc_pcm_runtime * runtime)88 static int mt8173_rt5650_rt5676_init(struct snd_soc_pcm_runtime *runtime)
89 {
90 	struct snd_soc_card *card = runtime->card;
91 	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
92 	struct snd_soc_component *component_sub = asoc_rtd_to_codec(runtime, 1)->component;
93 	int ret;
94 
95 	rt5645_sel_asrc_clk_src(component,
96 				RT5645_DA_STEREO_FILTER |
97 				RT5645_AD_STEREO_FILTER,
98 				RT5645_CLK_SEL_I2S1_ASRC);
99 	rt5677_sel_asrc_clk_src(component_sub,
100 				RT5677_DA_STEREO_FILTER |
101 				RT5677_AD_STEREO1_FILTER,
102 				RT5677_CLK_SEL_I2S1_ASRC);
103 	rt5677_sel_asrc_clk_src(component_sub,
104 				RT5677_AD_STEREO2_FILTER |
105 				RT5677_I2S2_SOURCE,
106 				RT5677_CLK_SEL_I2S2_ASRC);
107 
108 	/* enable jack detection */
109 	ret = snd_soc_card_jack_new_pins(card, "Headset Jack",
110 					 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
111 					 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
112 					 SND_JACK_BTN_2 | SND_JACK_BTN_3,
113 					 &mt8173_rt5650_rt5676_jack,
114 					 mt8173_rt5650_rt5676_jack_pins,
115 					 ARRAY_SIZE(mt8173_rt5650_rt5676_jack_pins));
116 	if (ret) {
117 		dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
118 		return ret;
119 	}
120 
121 	return rt5645_set_jack_detect(component,
122 				      &mt8173_rt5650_rt5676_jack,
123 				      &mt8173_rt5650_rt5676_jack,
124 				      &mt8173_rt5650_rt5676_jack);
125 }
126 
127 
128 enum {
129 	DAI_LINK_PLAYBACK,
130 	DAI_LINK_CAPTURE,
131 	DAI_LINK_HDMI,
132 	DAI_LINK_CODEC_I2S,
133 	DAI_LINK_HDMI_I2S,
134 	DAI_LINK_INTERCODEC
135 };
136 
137 SND_SOC_DAILINK_DEFS(playback,
138 	DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
139 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
140 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
141 
142 SND_SOC_DAILINK_DEFS(capture,
143 	DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
144 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
145 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
146 
147 SND_SOC_DAILINK_DEFS(hdmi_pcm,
148 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
149 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
150 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
151 
152 SND_SOC_DAILINK_DEFS(codec,
153 	DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
154 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"),
155 			   COMP_CODEC(NULL, "rt5677-aif1")),
156 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
157 
158 SND_SOC_DAILINK_DEFS(hdmi_be,
159 	DAILINK_COMP_ARRAY(COMP_CPU("HDMIO")),
160 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
161 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
162 
163 SND_SOC_DAILINK_DEFS(intercodec,
164 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
165 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5677-aif2")),
166 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
167 
168 /* Digital audio interface glue - connects codec <---> CPU */
169 static struct snd_soc_dai_link mt8173_rt5650_rt5676_dais[] = {
170 	/* Front End DAI links */
171 	[DAI_LINK_PLAYBACK] = {
172 		.name = "rt5650_rt5676 Playback",
173 		.stream_name = "rt5650_rt5676 Playback",
174 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
175 		.dynamic = 1,
176 		.dpcm_playback = 1,
177 		SND_SOC_DAILINK_REG(playback),
178 	},
179 	[DAI_LINK_CAPTURE] = {
180 		.name = "rt5650_rt5676 Capture",
181 		.stream_name = "rt5650_rt5676 Capture",
182 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
183 		.dynamic = 1,
184 		.dpcm_capture = 1,
185 		SND_SOC_DAILINK_REG(capture),
186 	},
187 	[DAI_LINK_HDMI] = {
188 		.name = "HDMI",
189 		.stream_name = "HDMI PCM",
190 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
191 		.dynamic = 1,
192 		.dpcm_playback = 1,
193 		SND_SOC_DAILINK_REG(hdmi_pcm),
194 	},
195 
196 	/* Back End DAI links */
197 	[DAI_LINK_CODEC_I2S] = {
198 		.name = "Codec",
199 		.no_pcm = 1,
200 		.init = mt8173_rt5650_rt5676_init,
201 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
202 			   SND_SOC_DAIFMT_CBS_CFS,
203 		.ops = &mt8173_rt5650_rt5676_ops,
204 		.ignore_pmdown_time = 1,
205 		.dpcm_playback = 1,
206 		.dpcm_capture = 1,
207 		SND_SOC_DAILINK_REG(codec),
208 	},
209 	[DAI_LINK_HDMI_I2S] = {
210 		.name = "HDMI BE",
211 		.no_pcm = 1,
212 		.dpcm_playback = 1,
213 		SND_SOC_DAILINK_REG(hdmi_be),
214 	},
215 	/* rt5676 <-> rt5650 intercodec link: Sets rt5676 I2S2 as master */
216 	[DAI_LINK_INTERCODEC] = {
217 		.name = "rt5650_rt5676 intercodec",
218 		.stream_name = "rt5650_rt5676 intercodec",
219 		.no_pcm = 1,
220 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
221 			   SND_SOC_DAIFMT_CBM_CFM,
222 		SND_SOC_DAILINK_REG(intercodec),
223 	},
224 };
225 
226 static struct snd_soc_codec_conf mt8173_rt5650_rt5676_codec_conf[] = {
227 	{
228 		.name_prefix = "Sub",
229 	},
230 };
231 
232 static struct snd_soc_card mt8173_rt5650_rt5676_card = {
233 	.name = "mtk-rt5650-rt5676",
234 	.owner = THIS_MODULE,
235 	.dai_link = mt8173_rt5650_rt5676_dais,
236 	.num_links = ARRAY_SIZE(mt8173_rt5650_rt5676_dais),
237 	.codec_conf = mt8173_rt5650_rt5676_codec_conf,
238 	.num_configs = ARRAY_SIZE(mt8173_rt5650_rt5676_codec_conf),
239 	.controls = mt8173_rt5650_rt5676_controls,
240 	.num_controls = ARRAY_SIZE(mt8173_rt5650_rt5676_controls),
241 	.dapm_widgets = mt8173_rt5650_rt5676_widgets,
242 	.num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5676_widgets),
243 	.dapm_routes = mt8173_rt5650_rt5676_routes,
244 	.num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5676_routes),
245 };
246 
mt8173_rt5650_rt5676_dev_probe(struct platform_device * pdev)247 static int mt8173_rt5650_rt5676_dev_probe(struct platform_device *pdev)
248 {
249 	struct snd_soc_card *card = &mt8173_rt5650_rt5676_card;
250 	struct device_node *platform_node;
251 	struct snd_soc_dai_link *dai_link;
252 	int i, ret;
253 
254 	platform_node = of_parse_phandle(pdev->dev.of_node,
255 					 "mediatek,platform", 0);
256 	if (!platform_node) {
257 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
258 		return -EINVAL;
259 	}
260 
261 	for_each_card_prelinks(card, i, dai_link) {
262 		if (dai_link->platforms->name)
263 			continue;
264 		dai_link->platforms->of_node = platform_node;
265 	}
266 
267 	mt8173_rt5650_rt5676_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
268 		of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
269 	if (!mt8173_rt5650_rt5676_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
270 		dev_err(&pdev->dev,
271 			"Property 'audio-codec' missing or invalid\n");
272 		ret = -EINVAL;
273 		goto put_node;
274 	}
275 	mt8173_rt5650_rt5676_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
276 		of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
277 	if (!mt8173_rt5650_rt5676_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node) {
278 		dev_err(&pdev->dev,
279 			"Property 'audio-codec' missing or invalid\n");
280 		ret = -EINVAL;
281 		goto put_node;
282 	}
283 	mt8173_rt5650_rt5676_codec_conf[0].dlc.of_node =
284 		mt8173_rt5650_rt5676_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node;
285 
286 	mt8173_rt5650_rt5676_dais[DAI_LINK_INTERCODEC].codecs->of_node =
287 		mt8173_rt5650_rt5676_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node;
288 
289 	mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codecs->of_node =
290 		of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 2);
291 	if (!mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codecs->of_node) {
292 		dev_err(&pdev->dev,
293 			"Property 'audio-codec' missing or invalid\n");
294 		ret = -EINVAL;
295 		goto put_node;
296 	}
297 
298 	card->dev = &pdev->dev;
299 
300 	ret = devm_snd_soc_register_card(&pdev->dev, card);
301 
302 put_node:
303 	of_node_put(platform_node);
304 	return ret;
305 }
306 
307 static const struct of_device_id mt8173_rt5650_rt5676_dt_match[] = {
308 	{ .compatible = "mediatek,mt8173-rt5650-rt5676", },
309 	{ }
310 };
311 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5676_dt_match);
312 
313 static struct platform_driver mt8173_rt5650_rt5676_driver = {
314 	.driver = {
315 		   .name = "mtk-rt5650-rt5676",
316 		   .of_match_table = mt8173_rt5650_rt5676_dt_match,
317 		   .pm = &snd_soc_pm_ops,
318 	},
319 	.probe = mt8173_rt5650_rt5676_dev_probe,
320 };
321 
322 module_platform_driver(mt8173_rt5650_rt5676_driver);
323 
324 /* Module information */
325 MODULE_DESCRIPTION("MT8173 RT5650 and RT5676 SoC machine driver");
326 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
327 MODULE_LICENSE("GPL v2");
328 MODULE_ALIAS("platform:mtk-rt5650-rt5676");
329 
330