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.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/of_gpio.h>
17 #include "mt2701-afe-common.h"
19 struct mt2701_cs42448_private {
21 int i2s1_in_mux_gpio_sel_1;
22 int i2s1_in_mux_gpio_sel_2;
25 static const char * const i2sin_mux_switch_text[] = {
32 static const struct soc_enum i2sin_mux_enum =
33 SOC_ENUM_SINGLE_EXT(4, i2sin_mux_switch_text);
35 static int mt2701_cs42448_i2sin1_mux_get(struct snd_kcontrol *kcontrol,
36 struct snd_ctl_elem_value *ucontrol)
38 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
39 struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
41 ucontrol->value.integer.value[0] = priv->i2s1_in_mux;
45 static int mt2701_cs42448_i2sin1_mux_set(struct snd_kcontrol *kcontrol,
46 struct snd_ctl_elem_value *ucontrol)
48 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
49 struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
51 if (ucontrol->value.integer.value[0] == priv->i2s1_in_mux)
54 switch (ucontrol->value.integer.value[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);
60 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 1);
61 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 0);
64 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 0);
65 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 1);
68 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 1);
69 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 1);
72 dev_warn(card->dev, "%s invalid setting\n", __func__);
75 priv->i2s1_in_mux = ucontrol->value.integer.value[0];
79 static 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),
88 static 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),
99 static const unsigned int mt2701_cs42448_sampling_rates[] = {48000};
101 static 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,
107 static int mt2701_cs42448_fe_ops_startup(struct snd_pcm_substream *substream)
111 err = snd_pcm_hw_constraint_list(substream->runtime, 0,
112 SNDRV_PCM_HW_PARAM_RATE,
113 &mt2701_cs42448_constraints_rates);
115 dev_err(substream->pcm->card->dev,
116 "%s snd_pcm_hw_constraint_list failed: 0x%x\n",
123 static const struct snd_soc_ops mt2701_cs42448_48k_fe_ops = {
124 .startup = mt2701_cs42448_fe_ops_startup,
127 static int mt2701_cs42448_be_ops_hw_params(struct snd_pcm_substream *substream,
128 struct snd_pcm_hw_params *params)
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;
138 mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
141 snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
144 snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
149 static struct snd_soc_ops mt2701_cs42448_be_ops = {
150 .hw_params = mt2701_cs42448_be_ops_hw_params
154 DAI_LINK_FE_MULTI_CH_OUT,
166 static struct snd_soc_dai_link mt2701_cs42448_dai_links[] = {
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,
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,
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,
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},
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},
227 [DAI_LINK_BE_I2S0] = {
228 .name = "mt2701-cs42448-I2S0",
229 .cpu_dai_name = "I2S0",
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,
238 [DAI_LINK_BE_I2S1] = {
239 .name = "mt2701-cs42448-I2S1",
240 .cpu_dai_name = "I2S1",
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,
249 [DAI_LINK_BE_I2S2] = {
250 .name = "mt2701-cs42448-I2S2",
251 .cpu_dai_name = "I2S2",
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,
260 [DAI_LINK_BE_I2S3] = {
261 .name = "mt2701-cs42448-I2S3",
262 .cpu_dai_name = "I2S3",
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,
271 [DAI_LINK_BE_MRG_BT] = {
272 .name = "mt2701-cs42448-MRG-BT",
273 .cpu_dai_name = "MRG BT",
275 .codec_dai_name = "bt-sco-pcm-wb",
281 static 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),
292 static int mt2701_cs42448_machine_probe(struct platform_device *pdev)
294 struct snd_soc_card *card = &mt2701_cs42448_soc_card;
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),
301 struct device *dev = &pdev->dev;
306 platform_node = of_parse_phandle(pdev->dev.of_node,
307 "mediatek,platform", 0);
308 if (!platform_node) {
309 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
312 for (i = 0; i < card->num_links; i++) {
313 if (mt2701_cs42448_dai_links[i].platform_name)
315 mt2701_cs42448_dai_links[i].platform_of_node = platform_node;
320 codec_node = of_parse_phandle(pdev->dev.of_node,
321 "mediatek,audio-codec", 0);
324 "Property 'audio-codec' missing or invalid\n");
327 for (i = 0; i < card->num_links; i++) {
328 if (mt2701_cs42448_dai_links[i].codec_name)
330 mt2701_cs42448_dai_links[i].codec_of_node = codec_node;
333 codec_node_bt_mrg = of_parse_phandle(pdev->dev.of_node,
334 "mediatek,audio-codec-bt-mrg", 0);
335 if (!codec_node_bt_mrg) {
337 "Property 'audio-codec-bt-mrg' missing or invalid\n");
340 mt2701_cs42448_dai_links[DAI_LINK_BE_MRG_BT].codec_of_node
343 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
345 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
349 priv->i2s1_in_mux_gpio_sel_1 =
350 of_get_named_gpio(dev->of_node, "i2s1-in-sel-gpio1", 0);
351 if (gpio_is_valid(priv->i2s1_in_mux_gpio_sel_1)) {
352 ret = devm_gpio_request(dev, priv->i2s1_in_mux_gpio_sel_1,
353 "i2s1_in_mux_gpio_sel_1");
355 dev_warn(&pdev->dev, "%s devm_gpio_request fail %d\n",
357 gpio_direction_output(priv->i2s1_in_mux_gpio_sel_1, 0);
360 priv->i2s1_in_mux_gpio_sel_2 =
361 of_get_named_gpio(dev->of_node, "i2s1-in-sel-gpio2", 0);
362 if (gpio_is_valid(priv->i2s1_in_mux_gpio_sel_2)) {
363 ret = devm_gpio_request(dev, priv->i2s1_in_mux_gpio_sel_2,
364 "i2s1_in_mux_gpio_sel_2");
366 dev_warn(&pdev->dev, "%s devm_gpio_request fail2 %d\n",
368 gpio_direction_output(priv->i2s1_in_mux_gpio_sel_2, 0);
370 snd_soc_card_set_drvdata(card, priv);
372 ret = devm_snd_soc_register_card(&pdev->dev, card);
375 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
381 static const struct of_device_id mt2701_cs42448_machine_dt_match[] = {
382 {.compatible = "mediatek,mt2701-cs42448-machine",},
387 static struct platform_driver mt2701_cs42448_machine = {
389 .name = "mt2701-cs42448",
391 .of_match_table = mt2701_cs42448_machine_dt_match,
394 .probe = mt2701_cs42448_machine_probe,
397 module_platform_driver(mt2701_cs42448_machine);
399 /* Module information */
400 MODULE_DESCRIPTION("MT2701 CS42448 ALSA SoC machine driver");
401 MODULE_AUTHOR("Ir Lian <ir.lian@mediatek.com>");
402 MODULE_LICENSE("GPL v2");
403 MODULE_ALIAS("mt2701 cs42448 soc card");