1
2
3
4
5
6
7
8
9
10#include <linux/module.h>
11#include <sound/soc.h>
12#include <linux/delay.h>
13#include <linux/gpio.h>
14#include <linux/pinctrl/consumer.h>
15#include <linux/of_gpio.h>
16
17#include "mt2701-afe-common.h"
18
19struct mt2701_cs42448_private {
20 int i2s1_in_mux;
21 int i2s1_in_mux_gpio_sel_1;
22 int i2s1_in_mux_gpio_sel_2;
23};
24
25static const char * const i2sin_mux_switch_text[] = {
26 "ADC_SDOUT2",
27 "ADC_SDOUT3",
28 "I2S_IN_1",
29 "I2S_IN_2",
30};
31
32static const struct soc_enum i2sin_mux_enum =
33 SOC_ENUM_SINGLE_EXT(4, i2sin_mux_switch_text);
34
35static int mt2701_cs42448_i2sin1_mux_get(struct snd_kcontrol *kcontrol,
36 struct snd_ctl_elem_value *ucontrol)
37{
38 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
39 struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
40
41 ucontrol->value.integer.value[0] = priv->i2s1_in_mux;
42 return 0;
43}
44
45static int mt2701_cs42448_i2sin1_mux_set(struct snd_kcontrol *kcontrol,
46 struct snd_ctl_elem_value *ucontrol)
47{
48 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
49 struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
50
51 if (ucontrol->value.integer.value[0] == priv->i2s1_in_mux)
52 return 0;
53
54 switch (ucontrol->value.integer.value[0]) {
55 case 0:
56 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 0);
57 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 0);
58 break;
59 case 1:
60 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 1);
61 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 0);
62 break;
63 case 2:
64 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 0);
65 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 1);
66 break;
67 case 3:
68 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 1);
69 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 1);
70 break;
71 default:
72 dev_warn(card->dev, "%s invalid setting\n", __func__);
73 }
74
75 priv->i2s1_in_mux = ucontrol->value.integer.value[0];
76 return 0;
77}
78
79static const struct snd_soc_dapm_widget
80 mt2701_cs42448_asoc_card_dapm_widgets[] = {
81 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
82 SND_SOC_DAPM_MIC("AMIC", NULL),
83 SND_SOC_DAPM_LINE("Tuner In", NULL),
84 SND_SOC_DAPM_LINE("Satellite Tuner In", NULL),
85 SND_SOC_DAPM_LINE("AUX In", NULL),
86};
87
88static const struct snd_kcontrol_new mt2701_cs42448_controls[] = {
89 SOC_DAPM_PIN_SWITCH("Line Out Jack"),
90 SOC_DAPM_PIN_SWITCH("AMIC"),
91 SOC_DAPM_PIN_SWITCH("Tuner In"),
92 SOC_DAPM_PIN_SWITCH("Satellite Tuner In"),
93 SOC_DAPM_PIN_SWITCH("AUX In"),
94 SOC_ENUM_EXT("I2SIN1_MUX_Switch", i2sin_mux_enum,
95 mt2701_cs42448_i2sin1_mux_get,
96 mt2701_cs42448_i2sin1_mux_set),
97};
98
99static const unsigned int mt2701_cs42448_sampling_rates[] = {48000};
100
101static const struct snd_pcm_hw_constraint_list mt2701_cs42448_constraints_rates = {
102 .count = ARRAY_SIZE(mt2701_cs42448_sampling_rates),
103 .list = mt2701_cs42448_sampling_rates,
104 .mask = 0,
105};
106
107static int mt2701_cs42448_fe_ops_startup(struct snd_pcm_substream *substream)
108{
109 int err;
110
111 err = snd_pcm_hw_constraint_list(substream->runtime, 0,
112 SNDRV_PCM_HW_PARAM_RATE,
113 &mt2701_cs42448_constraints_rates);
114 if (err < 0) {
115 dev_err(substream->pcm->card->dev,
116 "%s snd_pcm_hw_constraint_list failed: 0x%x\n",
117 __func__, err);
118 return err;
119 }
120 return 0;
121}
122
123static const struct snd_soc_ops mt2701_cs42448_48k_fe_ops = {
124 .startup = mt2701_cs42448_fe_ops_startup,
125};
126
127static int mt2701_cs42448_be_ops_hw_params(struct snd_pcm_substream *substream,
128 struct snd_pcm_hw_params *params)
129{
130 struct snd_soc_pcm_runtime *rtd = substream->private_data;
131 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
132 struct snd_soc_dai *codec_dai = rtd->codec_dai;
133 unsigned int mclk_rate;
134 unsigned int rate = params_rate(params);
135 unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;
136 unsigned int div_bck_over_lrck = 64;
137
138 mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
139
140
141 snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
142
143
144 snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
145
146 return 0;
147}
148
149static struct snd_soc_ops mt2701_cs42448_be_ops = {
150 .hw_params = mt2701_cs42448_be_ops_hw_params
151};
152
153enum {
154 DAI_LINK_FE_MULTI_CH_OUT,
155 DAI_LINK_FE_PCM0_IN,
156 DAI_LINK_FE_PCM1_IN,
157 DAI_LINK_FE_BT_OUT,
158 DAI_LINK_FE_BT_IN,
159 DAI_LINK_BE_I2S0,
160 DAI_LINK_BE_I2S1,
161 DAI_LINK_BE_I2S2,
162 DAI_LINK_BE_I2S3,
163 DAI_LINK_BE_MRG_BT,
164};
165
166static struct snd_soc_dai_link mt2701_cs42448_dai_links[] = {
167
168 [DAI_LINK_FE_MULTI_CH_OUT] = {
169 .name = "mt2701-cs42448-multi-ch-out",
170 .stream_name = "mt2701-cs42448-multi-ch-out",
171 .cpu_dai_name = "PCM_multi",
172 .codec_name = "snd-soc-dummy",
173 .codec_dai_name = "snd-soc-dummy-dai",
174 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
175 SND_SOC_DPCM_TRIGGER_POST},
176 .ops = &mt2701_cs42448_48k_fe_ops,
177 .dynamic = 1,
178 .dpcm_playback = 1,
179 },
180 [DAI_LINK_FE_PCM0_IN] = {
181 .name = "mt2701-cs42448-pcm0",
182 .stream_name = "mt2701-cs42448-pcm0-data-UL",
183 .cpu_dai_name = "PCM0",
184 .codec_name = "snd-soc-dummy",
185 .codec_dai_name = "snd-soc-dummy-dai",
186 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
187 SND_SOC_DPCM_TRIGGER_POST},
188 .ops = &mt2701_cs42448_48k_fe_ops,
189 .dynamic = 1,
190 .dpcm_capture = 1,
191 },
192 [DAI_LINK_FE_PCM1_IN] = {
193 .name = "mt2701-cs42448-pcm1-data-UL",
194 .stream_name = "mt2701-cs42448-pcm1-data-UL",
195 .cpu_dai_name = "PCM1",
196 .codec_name = "snd-soc-dummy",
197 .codec_dai_name = "snd-soc-dummy-dai",
198 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
199 SND_SOC_DPCM_TRIGGER_POST},
200 .ops = &mt2701_cs42448_48k_fe_ops,
201 .dynamic = 1,
202 .dpcm_capture = 1,
203 },
204 [DAI_LINK_FE_BT_OUT] = {
205 .name = "mt2701-cs42448-pcm-BT-out",
206 .stream_name = "mt2701-cs42448-pcm-BT",
207 .cpu_dai_name = "PCM_BT_DL",
208 .codec_name = "snd-soc-dummy",
209 .codec_dai_name = "snd-soc-dummy-dai",
210 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
211 SND_SOC_DPCM_TRIGGER_POST},
212 .dynamic = 1,
213 .dpcm_playback = 1,
214 },
215 [DAI_LINK_FE_BT_IN] = {
216 .name = "mt2701-cs42448-pcm-BT-in",
217 .stream_name = "mt2701-cs42448-pcm-BT",
218 .cpu_dai_name = "PCM_BT_UL",
219 .codec_name = "snd-soc-dummy",
220 .codec_dai_name = "snd-soc-dummy-dai",
221 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
222 SND_SOC_DPCM_TRIGGER_POST},
223 .dynamic = 1,
224 .dpcm_capture = 1,
225 },
226
227 [DAI_LINK_BE_I2S0] = {
228 .name = "mt2701-cs42448-I2S0",
229 .cpu_dai_name = "I2S0",
230 .no_pcm = 1,
231 .codec_dai_name = "cs42448",
232 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
233 | SND_SOC_DAIFMT_GATED,
234 .ops = &mt2701_cs42448_be_ops,
235 .dpcm_playback = 1,
236 .dpcm_capture = 1,
237 },
238 [DAI_LINK_BE_I2S1] = {
239 .name = "mt2701-cs42448-I2S1",
240 .cpu_dai_name = "I2S1",
241 .no_pcm = 1,
242 .codec_dai_name = "cs42448",
243 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
244 | SND_SOC_DAIFMT_GATED,
245 .ops = &mt2701_cs42448_be_ops,
246 .dpcm_playback = 1,
247 .dpcm_capture = 1,
248 },
249 [DAI_LINK_BE_I2S2] = {
250 .name = "mt2701-cs42448-I2S2",
251 .cpu_dai_name = "I2S2",
252 .no_pcm = 1,
253 .codec_dai_name = "cs42448",
254 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
255 | SND_SOC_DAIFMT_GATED,
256 .ops = &mt2701_cs42448_be_ops,
257 .dpcm_playback = 1,
258 .dpcm_capture = 1,
259 },
260 [DAI_LINK_BE_I2S3] = {
261 .name = "mt2701-cs42448-I2S3",
262 .cpu_dai_name = "I2S3",
263 .no_pcm = 1,
264 .codec_dai_name = "cs42448",
265 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
266 | SND_SOC_DAIFMT_GATED,
267 .ops = &mt2701_cs42448_be_ops,
268 .dpcm_playback = 1,
269 .dpcm_capture = 1,
270 },
271 [DAI_LINK_BE_MRG_BT] = {
272 .name = "mt2701-cs42448-MRG-BT",
273 .cpu_dai_name = "MRG BT",
274 .no_pcm = 1,
275 .codec_dai_name = "bt-sco-pcm-wb",
276 .dpcm_playback = 1,
277 .dpcm_capture = 1,
278 },
279};
280
281static struct snd_soc_card mt2701_cs42448_soc_card = {
282 .name = "mt2701-cs42448",
283 .owner = THIS_MODULE,
284 .dai_link = mt2701_cs42448_dai_links,
285 .num_links = ARRAY_SIZE(mt2701_cs42448_dai_links),
286 .controls = mt2701_cs42448_controls,
287 .num_controls = ARRAY_SIZE(mt2701_cs42448_controls),
288 .dapm_widgets = mt2701_cs42448_asoc_card_dapm_widgets,
289 .num_dapm_widgets = ARRAY_SIZE(mt2701_cs42448_asoc_card_dapm_widgets),
290};
291
292static int mt2701_cs42448_machine_probe(struct platform_device *pdev)
293{
294 struct snd_soc_card *card = &mt2701_cs42448_soc_card;
295 int ret;
296 int i;
297 struct device_node *platform_node, *codec_node, *codec_node_bt_mrg;
298 struct mt2701_cs42448_private *priv =
299 devm_kzalloc(&pdev->dev, sizeof(struct mt2701_cs42448_private),
300 GFP_KERNEL);
301 struct device *dev = &pdev->dev;
302 struct snd_soc_dai_link *dai_link;
303
304 if (!priv)
305 return -ENOMEM;
306
307 platform_node = of_parse_phandle(pdev->dev.of_node,
308 "mediatek,platform", 0);
309 if (!platform_node) {
310 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
311 return -EINVAL;
312 }
313 for_each_card_prelinks(card, i, dai_link) {
314 if (dai_link->platform_name)
315 continue;
316 dai_link->platform_of_node = platform_node;
317 }
318
319 card->dev = dev;
320
321 codec_node = of_parse_phandle(pdev->dev.of_node,
322 "mediatek,audio-codec", 0);
323 if (!codec_node) {
324 dev_err(&pdev->dev,
325 "Property 'audio-codec' missing or invalid\n");
326 return -EINVAL;
327 }
328 for_each_card_prelinks(card, i, dai_link) {
329 if (dai_link->codec_name)
330 continue;
331 dai_link->codec_of_node = codec_node;
332 }
333
334 codec_node_bt_mrg = of_parse_phandle(pdev->dev.of_node,
335 "mediatek,audio-codec-bt-mrg", 0);
336 if (!codec_node_bt_mrg) {
337 dev_err(&pdev->dev,
338 "Property 'audio-codec-bt-mrg' missing or invalid\n");
339 return -EINVAL;
340 }
341 mt2701_cs42448_dai_links[DAI_LINK_BE_MRG_BT].codec_of_node
342 = codec_node_bt_mrg;
343
344 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
345 if (ret) {
346 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
347 return ret;
348 }
349
350 priv->i2s1_in_mux_gpio_sel_1 =
351 of_get_named_gpio(dev->of_node, "i2s1-in-sel-gpio1", 0);
352 if (gpio_is_valid(priv->i2s1_in_mux_gpio_sel_1)) {
353 ret = devm_gpio_request(dev, priv->i2s1_in_mux_gpio_sel_1,
354 "i2s1_in_mux_gpio_sel_1");
355 if (ret)
356 dev_warn(&pdev->dev, "%s devm_gpio_request fail %d\n",
357 __func__, ret);
358 gpio_direction_output(priv->i2s1_in_mux_gpio_sel_1, 0);
359 }
360
361 priv->i2s1_in_mux_gpio_sel_2 =
362 of_get_named_gpio(dev->of_node, "i2s1-in-sel-gpio2", 0);
363 if (gpio_is_valid(priv->i2s1_in_mux_gpio_sel_2)) {
364 ret = devm_gpio_request(dev, priv->i2s1_in_mux_gpio_sel_2,
365 "i2s1_in_mux_gpio_sel_2");
366 if (ret)
367 dev_warn(&pdev->dev, "%s devm_gpio_request fail2 %d\n",
368 __func__, ret);
369 gpio_direction_output(priv->i2s1_in_mux_gpio_sel_2, 0);
370 }
371 snd_soc_card_set_drvdata(card, priv);
372
373 ret = devm_snd_soc_register_card(&pdev->dev, card);
374
375 if (ret)
376 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
377 __func__, ret);
378 return ret;
379}
380
381#ifdef CONFIG_OF
382static const struct of_device_id mt2701_cs42448_machine_dt_match[] = {
383 {.compatible = "mediatek,mt2701-cs42448-machine",},
384 {}
385};
386#endif
387
388static struct platform_driver mt2701_cs42448_machine = {
389 .driver = {
390 .name = "mt2701-cs42448",
391 #ifdef CONFIG_OF
392 .of_match_table = mt2701_cs42448_machine_dt_match,
393 #endif
394 },
395 .probe = mt2701_cs42448_machine_probe,
396};
397
398module_platform_driver(mt2701_cs42448_machine);
399
400
401MODULE_DESCRIPTION("MT2701 CS42448 ALSA SoC machine driver");
402MODULE_AUTHOR("Ir Lian <ir.lian@mediatek.com>");
403MODULE_LICENSE("GPL v2");
404MODULE_ALIAS("mt2701 cs42448 soc card");
405