xref: /linux/sound/soc/mediatek/mt8173/mt8173-rt5650-rt5514.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt8173-rt5650-rt5514.c  --  MT8173 machine driver with RT5650/5514 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 static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_widgets[] = {
19 	SND_SOC_DAPM_SPK("Speaker", NULL),
20 	SND_SOC_DAPM_MIC("Int Mic", NULL),
21 	SND_SOC_DAPM_HP("Headphone", NULL),
22 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
23 };
24 
25 static const struct snd_soc_dapm_route mt8173_rt5650_rt5514_routes[] = {
26 	{"Speaker", NULL, "SPOL"},
27 	{"Speaker", NULL, "SPOR"},
28 	{"Sub DMIC1L", NULL, "Int Mic"},
29 	{"Sub DMIC1R", NULL, "Int Mic"},
30 	{"Headphone", NULL, "HPOL"},
31 	{"Headphone", NULL, "HPOR"},
32 	{"IN1P", NULL, "Headset Mic"},
33 	{"IN1N", NULL, "Headset Mic"},
34 };
35 
36 static const struct snd_kcontrol_new mt8173_rt5650_rt5514_controls[] = {
37 	SOC_DAPM_PIN_SWITCH("Speaker"),
38 	SOC_DAPM_PIN_SWITCH("Int Mic"),
39 	SOC_DAPM_PIN_SWITCH("Headphone"),
40 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
41 };
42 
43 static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream *substream,
44 					  struct snd_pcm_hw_params *params)
45 {
46 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
47 	struct snd_soc_dai *codec_dai;
48 	int i, ret;
49 
50 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
51 		/* pll from mclk 12.288M */
52 		ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
53 					  params_rate(params) * 512);
54 		if (ret)
55 			return ret;
56 
57 		/* sysclk from pll */
58 		ret = snd_soc_dai_set_sysclk(codec_dai, 1,
59 					     params_rate(params) * 512,
60 					     SND_SOC_CLOCK_IN);
61 		if (ret)
62 			return ret;
63 	}
64 	return 0;
65 }
66 
67 static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = {
68 	.hw_params = mt8173_rt5650_rt5514_hw_params,
69 };
70 
71 static struct snd_soc_jack mt8173_rt5650_rt5514_jack;
72 
73 static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime)
74 {
75 	struct snd_soc_card *card = runtime->card;
76 	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
77 	int ret;
78 
79 	rt5645_sel_asrc_clk_src(component,
80 				RT5645_DA_STEREO_FILTER |
81 				RT5645_AD_STEREO_FILTER,
82 				RT5645_CLK_SEL_I2S1_ASRC);
83 
84 	/* enable jack detection */
85 	ret = snd_soc_card_jack_new(card, "Headset Jack",
86 				    SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
87 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
88 				    SND_JACK_BTN_2 | SND_JACK_BTN_3,
89 				    &mt8173_rt5650_rt5514_jack);
90 	if (ret) {
91 		dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
92 		return ret;
93 	}
94 
95 	return rt5645_set_jack_detect(component,
96 				      &mt8173_rt5650_rt5514_jack,
97 				      &mt8173_rt5650_rt5514_jack,
98 				      &mt8173_rt5650_rt5514_jack);
99 }
100 
101 enum {
102 	DAI_LINK_PLAYBACK,
103 	DAI_LINK_CAPTURE,
104 	DAI_LINK_CODEC_I2S,
105 };
106 
107 SND_SOC_DAILINK_DEFS(playback,
108 	DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
109 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
110 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
111 
112 SND_SOC_DAILINK_DEFS(capture,
113 	DAILINK_COMP_ARRAY(COMP_CPU("VUL")),
114 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
115 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
116 
117 SND_SOC_DAILINK_DEFS(codec,
118 	DAILINK_COMP_ARRAY(COMP_CPU("I2S")),
119 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1"),
120 			   COMP_CODEC(NULL, "rt5514-aif1")),
121 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
122 
123 /* Digital audio interface glue - connects codec <---> CPU */
124 static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = {
125 	/* Front End DAI links */
126 	[DAI_LINK_PLAYBACK] = {
127 		.name = "rt5650_rt5514 Playback",
128 		.stream_name = "rt5650_rt5514 Playback",
129 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
130 		.dynamic = 1,
131 		.dpcm_playback = 1,
132 		SND_SOC_DAILINK_REG(playback),
133 	},
134 	[DAI_LINK_CAPTURE] = {
135 		.name = "rt5650_rt5514 Capture",
136 		.stream_name = "rt5650_rt5514 Capture",
137 		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
138 		.dynamic = 1,
139 		.dpcm_capture = 1,
140 		SND_SOC_DAILINK_REG(capture),
141 	},
142 	/* Back End DAI links */
143 	[DAI_LINK_CODEC_I2S] = {
144 		.name = "Codec",
145 		.no_pcm = 1,
146 		.init = mt8173_rt5650_rt5514_init,
147 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
148 			   SND_SOC_DAIFMT_CBS_CFS,
149 		.ops = &mt8173_rt5650_rt5514_ops,
150 		.ignore_pmdown_time = 1,
151 		.dpcm_playback = 1,
152 		.dpcm_capture = 1,
153 		SND_SOC_DAILINK_REG(codec),
154 	},
155 };
156 
157 static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = {
158 	{
159 		.name_prefix = "Sub",
160 	},
161 };
162 
163 static struct snd_soc_card mt8173_rt5650_rt5514_card = {
164 	.name = "mtk-rt5650-rt5514",
165 	.owner = THIS_MODULE,
166 	.dai_link = mt8173_rt5650_rt5514_dais,
167 	.num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais),
168 	.codec_conf = mt8173_rt5650_rt5514_codec_conf,
169 	.num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf),
170 	.controls = mt8173_rt5650_rt5514_controls,
171 	.num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls),
172 	.dapm_widgets = mt8173_rt5650_rt5514_widgets,
173 	.num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets),
174 	.dapm_routes = mt8173_rt5650_rt5514_routes,
175 	.num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes),
176 };
177 
178 static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev)
179 {
180 	struct snd_soc_card *card = &mt8173_rt5650_rt5514_card;
181 	struct device_node *platform_node;
182 	struct snd_soc_dai_link *dai_link;
183 	int i, ret;
184 
185 	platform_node = of_parse_phandle(pdev->dev.of_node,
186 					 "mediatek,platform", 0);
187 	if (!platform_node) {
188 		dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
189 		return -EINVAL;
190 	}
191 
192 	for_each_card_prelinks(card, i, dai_link) {
193 		if (dai_link->platforms->name)
194 			continue;
195 		dai_link->platforms->of_node = platform_node;
196 	}
197 
198 	mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node =
199 		of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
200 	if (!mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[0].of_node) {
201 		dev_err(&pdev->dev,
202 			"Property 'audio-codec' missing or invalid\n");
203 		return -EINVAL;
204 	}
205 	mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node =
206 		of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
207 	if (!mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node) {
208 		dev_err(&pdev->dev,
209 			"Property 'audio-codec' missing or invalid\n");
210 		return -EINVAL;
211 	}
212 	mt8173_rt5650_rt5514_codec_conf[0].dlc.of_node =
213 		mt8173_rt5650_rt5514_dais[DAI_LINK_CODEC_I2S].codecs[1].of_node;
214 
215 	card->dev = &pdev->dev;
216 
217 	ret = devm_snd_soc_register_card(&pdev->dev, card);
218 
219 	of_node_put(platform_node);
220 	return ret;
221 }
222 
223 static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = {
224 	{ .compatible = "mediatek,mt8173-rt5650-rt5514", },
225 	{ }
226 };
227 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match);
228 
229 static struct platform_driver mt8173_rt5650_rt5514_driver = {
230 	.driver = {
231 		   .name = "mtk-rt5650-rt5514",
232 		   .of_match_table = mt8173_rt5650_rt5514_dt_match,
233 		   .pm = &snd_soc_pm_ops,
234 	},
235 	.probe = mt8173_rt5650_rt5514_dev_probe,
236 };
237 
238 module_platform_driver(mt8173_rt5650_rt5514_driver);
239 
240 /* Module information */
241 MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
242 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
243 MODULE_LICENSE("GPL v2");
244 MODULE_ALIAS("platform:mtk-rt5650-rt5514");
245 
246