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