1 // SPDX-License-Identifier: GPL-2.0
3 * mt2701-cs42448.c -- MT2701 CS42448 ALSA SoC machine driver
5 * Copyright (c) 2016 MediaTek Inc.
6 * Author: Ir Lian <ir.lian@mediatek.com>
7 * Garlic Tseng <garlic.tseng@mediatek.com>
10 #include <linux/module.h>
11 #include <sound/soc.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/pinctrl/consumer.h>
16 #include "mt2701-afe-common.h"
18 struct mt2701_cs42448_private {
20 struct gpio_desc *i2s1_in_mux_sel_1;
21 struct gpio_desc *i2s1_in_mux_sel_2;
24 static const char * const i2sin_mux_switch_text[] = {
31 static const struct soc_enum i2sin_mux_enum =
32 SOC_ENUM_SINGLE_EXT(4, i2sin_mux_switch_text);
34 static int mt2701_cs42448_i2sin1_mux_get(struct snd_kcontrol *kcontrol,
35 struct snd_ctl_elem_value *ucontrol)
37 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
38 struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
40 ucontrol->value.integer.value[0] = priv->i2s1_in_mux;
44 static int mt2701_cs42448_i2sin1_mux_set(struct snd_kcontrol *kcontrol,
45 struct snd_ctl_elem_value *ucontrol)
47 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
48 struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
50 if (ucontrol->value.integer.value[0] == priv->i2s1_in_mux)
53 switch (ucontrol->value.integer.value[0]) {
55 gpiod_set_value(priv->i2s1_in_mux_sel_1, 0);
56 gpiod_set_value(priv->i2s1_in_mux_sel_2, 0);
59 gpiod_set_value(priv->i2s1_in_mux_sel_1, 1);
60 gpiod_set_value(priv->i2s1_in_mux_sel_2, 0);
63 gpiod_set_value(priv->i2s1_in_mux_sel_1, 0);
64 gpiod_set_value(priv->i2s1_in_mux_sel_2, 1);
67 gpiod_set_value(priv->i2s1_in_mux_sel_1, 1);
68 gpiod_set_value(priv->i2s1_in_mux_sel_2, 1);
71 dev_warn(card->dev, "%s invalid setting\n", __func__);
74 priv->i2s1_in_mux = ucontrol->value.integer.value[0];
78 static const struct snd_soc_dapm_widget
79 mt2701_cs42448_asoc_card_dapm_widgets[] = {
80 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
81 SND_SOC_DAPM_MIC("AMIC", NULL),
82 SND_SOC_DAPM_LINE("Tuner In", NULL),
83 SND_SOC_DAPM_LINE("Satellite Tuner In", NULL),
84 SND_SOC_DAPM_LINE("AUX In", NULL),
87 static const struct snd_kcontrol_new mt2701_cs42448_controls[] = {
88 SOC_DAPM_PIN_SWITCH("Line Out Jack"),
89 SOC_DAPM_PIN_SWITCH("AMIC"),
90 SOC_DAPM_PIN_SWITCH("Tuner In"),
91 SOC_DAPM_PIN_SWITCH("Satellite Tuner In"),
92 SOC_DAPM_PIN_SWITCH("AUX In"),
93 SOC_ENUM_EXT("I2SIN1_MUX_Switch", i2sin_mux_enum,
94 mt2701_cs42448_i2sin1_mux_get,
95 mt2701_cs42448_i2sin1_mux_set),
98 static const unsigned int mt2701_cs42448_sampling_rates[] = {48000};
100 static const struct snd_pcm_hw_constraint_list mt2701_cs42448_constraints_rates = {
101 .count = ARRAY_SIZE(mt2701_cs42448_sampling_rates),
102 .list = mt2701_cs42448_sampling_rates,
106 static int mt2701_cs42448_fe_ops_startup(struct snd_pcm_substream *substream)
110 err = snd_pcm_hw_constraint_list(substream->runtime, 0,
111 SNDRV_PCM_HW_PARAM_RATE,
112 &mt2701_cs42448_constraints_rates);
114 dev_err(substream->pcm->card->dev,
115 "%s snd_pcm_hw_constraint_list failed: 0x%x\n",
122 static const struct snd_soc_ops mt2701_cs42448_48k_fe_ops = {
123 .startup = mt2701_cs42448_fe_ops_startup,
126 static int mt2701_cs42448_be_ops_hw_params(struct snd_pcm_substream *substream,
127 struct snd_pcm_hw_params *params)
129 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
130 struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
131 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
132 unsigned int mclk_rate;
133 unsigned int rate = params_rate(params);
134 unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;
135 unsigned int div_bck_over_lrck = 64;
137 mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
140 snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
143 snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
148 static const struct snd_soc_ops mt2701_cs42448_be_ops = {
149 .hw_params = mt2701_cs42448_be_ops_hw_params
153 DAI_LINK_FE_MULTI_CH_OUT,
165 SND_SOC_DAILINK_DEFS(fe_multi_ch_out,
166 DAILINK_COMP_ARRAY(COMP_CPU("PCM_multi")),
167 DAILINK_COMP_ARRAY(COMP_DUMMY()),
168 DAILINK_COMP_ARRAY(COMP_EMPTY()));
170 SND_SOC_DAILINK_DEFS(fe_pcm0_in,
171 DAILINK_COMP_ARRAY(COMP_CPU("PCM0")),
172 DAILINK_COMP_ARRAY(COMP_DUMMY()),
173 DAILINK_COMP_ARRAY(COMP_EMPTY()));
175 SND_SOC_DAILINK_DEFS(fe_pcm1_in,
176 DAILINK_COMP_ARRAY(COMP_CPU("PCM1")),
177 DAILINK_COMP_ARRAY(COMP_DUMMY()),
178 DAILINK_COMP_ARRAY(COMP_EMPTY()));
180 SND_SOC_DAILINK_DEFS(fe_bt_out,
181 DAILINK_COMP_ARRAY(COMP_CPU("PCM_BT_DL")),
182 DAILINK_COMP_ARRAY(COMP_DUMMY()),
183 DAILINK_COMP_ARRAY(COMP_EMPTY()));
185 SND_SOC_DAILINK_DEFS(fe_bt_in,
186 DAILINK_COMP_ARRAY(COMP_CPU("PCM_BT_UL")),
187 DAILINK_COMP_ARRAY(COMP_DUMMY()),
188 DAILINK_COMP_ARRAY(COMP_EMPTY()));
190 SND_SOC_DAILINK_DEFS(be_i2s0,
191 DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
192 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "cs42448")),
193 DAILINK_COMP_ARRAY(COMP_EMPTY()));
195 SND_SOC_DAILINK_DEFS(be_i2s1,
196 DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
197 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "cs42448")),
198 DAILINK_COMP_ARRAY(COMP_EMPTY()));
200 SND_SOC_DAILINK_DEFS(be_i2s2,
201 DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
202 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "cs42448")),
203 DAILINK_COMP_ARRAY(COMP_EMPTY()));
205 SND_SOC_DAILINK_DEFS(be_i2s3,
206 DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
207 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "cs42448")),
208 DAILINK_COMP_ARRAY(COMP_EMPTY()));
210 SND_SOC_DAILINK_DEFS(be_mrg_bt,
211 DAILINK_COMP_ARRAY(COMP_CPU("MRG BT")),
212 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "bt-sco-pcm-wb")),
213 DAILINK_COMP_ARRAY(COMP_EMPTY()));
215 static struct snd_soc_dai_link mt2701_cs42448_dai_links[] = {
217 [DAI_LINK_FE_MULTI_CH_OUT] = {
218 .name = "mt2701-cs42448-multi-ch-out",
219 .stream_name = "mt2701-cs42448-multi-ch-out",
220 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
221 SND_SOC_DPCM_TRIGGER_POST},
222 .ops = &mt2701_cs42448_48k_fe_ops,
225 SND_SOC_DAILINK_REG(fe_multi_ch_out),
227 [DAI_LINK_FE_PCM0_IN] = {
228 .name = "mt2701-cs42448-pcm0",
229 .stream_name = "mt2701-cs42448-pcm0-data-UL",
230 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
231 SND_SOC_DPCM_TRIGGER_POST},
232 .ops = &mt2701_cs42448_48k_fe_ops,
235 SND_SOC_DAILINK_REG(fe_pcm0_in),
237 [DAI_LINK_FE_PCM1_IN] = {
238 .name = "mt2701-cs42448-pcm1-data-UL",
239 .stream_name = "mt2701-cs42448-pcm1-data-UL",
240 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
241 SND_SOC_DPCM_TRIGGER_POST},
242 .ops = &mt2701_cs42448_48k_fe_ops,
245 SND_SOC_DAILINK_REG(fe_pcm1_in),
247 [DAI_LINK_FE_BT_OUT] = {
248 .name = "mt2701-cs42448-pcm-BT-out",
249 .stream_name = "mt2701-cs42448-pcm-BT",
250 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
251 SND_SOC_DPCM_TRIGGER_POST},
254 SND_SOC_DAILINK_REG(fe_bt_out),
256 [DAI_LINK_FE_BT_IN] = {
257 .name = "mt2701-cs42448-pcm-BT-in",
258 .stream_name = "mt2701-cs42448-pcm-BT",
259 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
260 SND_SOC_DPCM_TRIGGER_POST},
263 SND_SOC_DAILINK_REG(fe_bt_in),
266 [DAI_LINK_BE_I2S0] = {
267 .name = "mt2701-cs42448-I2S0",
269 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
270 | SND_SOC_DAIFMT_GATED,
271 .ops = &mt2701_cs42448_be_ops,
274 SND_SOC_DAILINK_REG(be_i2s0),
276 [DAI_LINK_BE_I2S1] = {
277 .name = "mt2701-cs42448-I2S1",
279 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
280 | SND_SOC_DAIFMT_GATED,
281 .ops = &mt2701_cs42448_be_ops,
284 SND_SOC_DAILINK_REG(be_i2s1),
286 [DAI_LINK_BE_I2S2] = {
287 .name = "mt2701-cs42448-I2S2",
289 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
290 | SND_SOC_DAIFMT_GATED,
291 .ops = &mt2701_cs42448_be_ops,
294 SND_SOC_DAILINK_REG(be_i2s2),
296 [DAI_LINK_BE_I2S3] = {
297 .name = "mt2701-cs42448-I2S3",
299 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
300 | SND_SOC_DAIFMT_GATED,
301 .ops = &mt2701_cs42448_be_ops,
304 SND_SOC_DAILINK_REG(be_i2s3),
306 [DAI_LINK_BE_MRG_BT] = {
307 .name = "mt2701-cs42448-MRG-BT",
311 SND_SOC_DAILINK_REG(be_mrg_bt),
315 static struct snd_soc_card mt2701_cs42448_soc_card = {
316 .name = "mt2701-cs42448",
317 .owner = THIS_MODULE,
318 .dai_link = mt2701_cs42448_dai_links,
319 .num_links = ARRAY_SIZE(mt2701_cs42448_dai_links),
320 .controls = mt2701_cs42448_controls,
321 .num_controls = ARRAY_SIZE(mt2701_cs42448_controls),
322 .dapm_widgets = mt2701_cs42448_asoc_card_dapm_widgets,
323 .num_dapm_widgets = ARRAY_SIZE(mt2701_cs42448_asoc_card_dapm_widgets),
326 static int mt2701_cs42448_machine_probe(struct platform_device *pdev)
328 struct snd_soc_card *card = &mt2701_cs42448_soc_card;
331 struct device_node *platform_node, *codec_node, *codec_node_bt_mrg;
332 struct mt2701_cs42448_private *priv =
333 devm_kzalloc(&pdev->dev, sizeof(struct mt2701_cs42448_private),
335 struct device *dev = &pdev->dev;
336 struct snd_soc_dai_link *dai_link;
341 platform_node = of_parse_phandle(pdev->dev.of_node,
342 "mediatek,platform", 0);
343 if (!platform_node) {
344 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
347 for_each_card_prelinks(card, i, dai_link) {
348 if (dai_link->platforms->name)
350 dai_link->platforms->of_node = platform_node;
355 codec_node = of_parse_phandle(pdev->dev.of_node,
356 "mediatek,audio-codec", 0);
359 "Property 'audio-codec' missing or invalid\n");
362 for_each_card_prelinks(card, i, dai_link) {
363 if (dai_link->codecs->name)
365 dai_link->codecs->of_node = codec_node;
368 codec_node_bt_mrg = of_parse_phandle(pdev->dev.of_node,
369 "mediatek,audio-codec-bt-mrg", 0);
370 if (!codec_node_bt_mrg) {
372 "Property 'audio-codec-bt-mrg' missing or invalid\n");
375 mt2701_cs42448_dai_links[DAI_LINK_BE_MRG_BT].codecs->of_node
378 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
380 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
384 priv->i2s1_in_mux_sel_1 = devm_gpiod_get_optional(dev, "i2s1-in-sel-gpio1",
386 if (IS_ERR(priv->i2s1_in_mux_sel_1))
387 return dev_err_probe(dev, PTR_ERR(priv->i2s1_in_mux_sel_1),
388 "error getting mux 1 selector\n");
390 priv->i2s1_in_mux_sel_2 = devm_gpiod_get_optional(dev, "i2s1-in-sel-gpio2",
392 if (IS_ERR(priv->i2s1_in_mux_sel_2))
393 return dev_err_probe(dev, PTR_ERR(priv->i2s1_in_mux_sel_2),
394 "error getting mux 2 selector\n");
396 snd_soc_card_set_drvdata(card, priv);
398 ret = devm_snd_soc_register_card(&pdev->dev, card);
401 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
407 static const struct of_device_id mt2701_cs42448_machine_dt_match[] = {
408 {.compatible = "mediatek,mt2701-cs42448-machine",},
411 MODULE_DEVICE_TABLE(of, mt2701_cs42448_machine_dt_match);
414 static struct platform_driver mt2701_cs42448_machine = {
416 .name = "mt2701-cs42448",
418 .of_match_table = mt2701_cs42448_machine_dt_match,
421 .probe = mt2701_cs42448_machine_probe,
424 module_platform_driver(mt2701_cs42448_machine);
426 /* Module information */
427 MODULE_DESCRIPTION("MT2701 CS42448 ALSA SoC machine driver");
428 MODULE_AUTHOR("Ir Lian <ir.lian@mediatek.com>");
429 MODULE_LICENSE("GPL v2");
430 MODULE_ALIAS("mt2701 cs42448 soc card");