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
= asoc_substream_to_rtd(substream
);
131 struct snd_soc_dai
*cpu_dai
= asoc_rtd_to_cpu(rtd
, 0);
132 struct snd_soc_dai
*codec_dai
= asoc_rtd_to_codec(rtd
, 0);
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 SND_SOC_DAILINK_DEFS(fe_multi_ch_out
,
167 DAILINK_COMP_ARRAY(COMP_CPU("PCM_multi")),
168 DAILINK_COMP_ARRAY(COMP_DUMMY()),
169 DAILINK_COMP_ARRAY(COMP_EMPTY()));
171 SND_SOC_DAILINK_DEFS(fe_pcm0_in
,
172 DAILINK_COMP_ARRAY(COMP_CPU("PCM0")),
173 DAILINK_COMP_ARRAY(COMP_DUMMY()),
174 DAILINK_COMP_ARRAY(COMP_EMPTY()));
176 SND_SOC_DAILINK_DEFS(fe_pcm1_in
,
177 DAILINK_COMP_ARRAY(COMP_CPU("PCM1")),
178 DAILINK_COMP_ARRAY(COMP_DUMMY()),
179 DAILINK_COMP_ARRAY(COMP_EMPTY()));
181 SND_SOC_DAILINK_DEFS(fe_bt_out
,
182 DAILINK_COMP_ARRAY(COMP_CPU("PCM_BT_DL")),
183 DAILINK_COMP_ARRAY(COMP_DUMMY()),
184 DAILINK_COMP_ARRAY(COMP_EMPTY()));
186 SND_SOC_DAILINK_DEFS(fe_bt_in
,
187 DAILINK_COMP_ARRAY(COMP_CPU("PCM_BT_UL")),
188 DAILINK_COMP_ARRAY(COMP_DUMMY()),
189 DAILINK_COMP_ARRAY(COMP_EMPTY()));
191 SND_SOC_DAILINK_DEFS(be_i2s0
,
192 DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
193 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "cs42448")),
194 DAILINK_COMP_ARRAY(COMP_EMPTY()));
196 SND_SOC_DAILINK_DEFS(be_i2s1
,
197 DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
198 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "cs42448")),
199 DAILINK_COMP_ARRAY(COMP_EMPTY()));
201 SND_SOC_DAILINK_DEFS(be_i2s2
,
202 DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
203 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "cs42448")),
204 DAILINK_COMP_ARRAY(COMP_EMPTY()));
206 SND_SOC_DAILINK_DEFS(be_i2s3
,
207 DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
208 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "cs42448")),
209 DAILINK_COMP_ARRAY(COMP_EMPTY()));
211 SND_SOC_DAILINK_DEFS(be_mrg_bt
,
212 DAILINK_COMP_ARRAY(COMP_CPU("MRG BT")),
213 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "bt-sco-pcm-wb")),
214 DAILINK_COMP_ARRAY(COMP_EMPTY()));
216 static struct snd_soc_dai_link mt2701_cs42448_dai_links
[] = {
218 [DAI_LINK_FE_MULTI_CH_OUT
] = {
219 .name
= "mt2701-cs42448-multi-ch-out",
220 .stream_name
= "mt2701-cs42448-multi-ch-out",
221 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
,
222 SND_SOC_DPCM_TRIGGER_POST
},
223 .ops
= &mt2701_cs42448_48k_fe_ops
,
226 SND_SOC_DAILINK_REG(fe_multi_ch_out
),
228 [DAI_LINK_FE_PCM0_IN
] = {
229 .name
= "mt2701-cs42448-pcm0",
230 .stream_name
= "mt2701-cs42448-pcm0-data-UL",
231 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
,
232 SND_SOC_DPCM_TRIGGER_POST
},
233 .ops
= &mt2701_cs42448_48k_fe_ops
,
236 SND_SOC_DAILINK_REG(fe_pcm0_in
),
238 [DAI_LINK_FE_PCM1_IN
] = {
239 .name
= "mt2701-cs42448-pcm1-data-UL",
240 .stream_name
= "mt2701-cs42448-pcm1-data-UL",
241 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
,
242 SND_SOC_DPCM_TRIGGER_POST
},
243 .ops
= &mt2701_cs42448_48k_fe_ops
,
246 SND_SOC_DAILINK_REG(fe_pcm1_in
),
248 [DAI_LINK_FE_BT_OUT
] = {
249 .name
= "mt2701-cs42448-pcm-BT-out",
250 .stream_name
= "mt2701-cs42448-pcm-BT",
251 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
,
252 SND_SOC_DPCM_TRIGGER_POST
},
255 SND_SOC_DAILINK_REG(fe_bt_out
),
257 [DAI_LINK_FE_BT_IN
] = {
258 .name
= "mt2701-cs42448-pcm-BT-in",
259 .stream_name
= "mt2701-cs42448-pcm-BT",
260 .trigger
= {SND_SOC_DPCM_TRIGGER_POST
,
261 SND_SOC_DPCM_TRIGGER_POST
},
264 SND_SOC_DAILINK_REG(fe_bt_in
),
267 [DAI_LINK_BE_I2S0
] = {
268 .name
= "mt2701-cs42448-I2S0",
270 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBS_CFS
271 | SND_SOC_DAIFMT_GATED
,
272 .ops
= &mt2701_cs42448_be_ops
,
275 SND_SOC_DAILINK_REG(be_i2s0
),
277 [DAI_LINK_BE_I2S1
] = {
278 .name
= "mt2701-cs42448-I2S1",
280 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBS_CFS
281 | SND_SOC_DAIFMT_GATED
,
282 .ops
= &mt2701_cs42448_be_ops
,
285 SND_SOC_DAILINK_REG(be_i2s1
),
287 [DAI_LINK_BE_I2S2
] = {
288 .name
= "mt2701-cs42448-I2S2",
290 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBS_CFS
291 | SND_SOC_DAIFMT_GATED
,
292 .ops
= &mt2701_cs42448_be_ops
,
295 SND_SOC_DAILINK_REG(be_i2s2
),
297 [DAI_LINK_BE_I2S3
] = {
298 .name
= "mt2701-cs42448-I2S3",
300 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_CBS_CFS
301 | SND_SOC_DAIFMT_GATED
,
302 .ops
= &mt2701_cs42448_be_ops
,
305 SND_SOC_DAILINK_REG(be_i2s3
),
307 [DAI_LINK_BE_MRG_BT
] = {
308 .name
= "mt2701-cs42448-MRG-BT",
312 SND_SOC_DAILINK_REG(be_mrg_bt
),
316 static struct snd_soc_card mt2701_cs42448_soc_card
= {
317 .name
= "mt2701-cs42448",
318 .owner
= THIS_MODULE
,
319 .dai_link
= mt2701_cs42448_dai_links
,
320 .num_links
= ARRAY_SIZE(mt2701_cs42448_dai_links
),
321 .controls
= mt2701_cs42448_controls
,
322 .num_controls
= ARRAY_SIZE(mt2701_cs42448_controls
),
323 .dapm_widgets
= mt2701_cs42448_asoc_card_dapm_widgets
,
324 .num_dapm_widgets
= ARRAY_SIZE(mt2701_cs42448_asoc_card_dapm_widgets
),
327 static int mt2701_cs42448_machine_probe(struct platform_device
*pdev
)
329 struct snd_soc_card
*card
= &mt2701_cs42448_soc_card
;
332 struct device_node
*platform_node
, *codec_node
, *codec_node_bt_mrg
;
333 struct mt2701_cs42448_private
*priv
=
334 devm_kzalloc(&pdev
->dev
, sizeof(struct mt2701_cs42448_private
),
336 struct device
*dev
= &pdev
->dev
;
337 struct snd_soc_dai_link
*dai_link
;
342 platform_node
= of_parse_phandle(pdev
->dev
.of_node
,
343 "mediatek,platform", 0);
344 if (!platform_node
) {
345 dev_err(&pdev
->dev
, "Property 'platform' missing or invalid\n");
348 for_each_card_prelinks(card
, i
, dai_link
) {
349 if (dai_link
->platforms
->name
)
351 dai_link
->platforms
->of_node
= platform_node
;
356 codec_node
= of_parse_phandle(pdev
->dev
.of_node
,
357 "mediatek,audio-codec", 0);
360 "Property 'audio-codec' missing or invalid\n");
363 for_each_card_prelinks(card
, i
, dai_link
) {
364 if (dai_link
->codecs
->name
)
366 dai_link
->codecs
->of_node
= codec_node
;
369 codec_node_bt_mrg
= of_parse_phandle(pdev
->dev
.of_node
,
370 "mediatek,audio-codec-bt-mrg", 0);
371 if (!codec_node_bt_mrg
) {
373 "Property 'audio-codec-bt-mrg' missing or invalid\n");
376 mt2701_cs42448_dai_links
[DAI_LINK_BE_MRG_BT
].codecs
->of_node
379 ret
= snd_soc_of_parse_audio_routing(card
, "audio-routing");
381 dev_err(&pdev
->dev
, "failed to parse audio-routing: %d\n", ret
);
385 priv
->i2s1_in_mux_gpio_sel_1
=
386 of_get_named_gpio(dev
->of_node
, "i2s1-in-sel-gpio1", 0);
387 if (gpio_is_valid(priv
->i2s1_in_mux_gpio_sel_1
)) {
388 ret
= devm_gpio_request(dev
, priv
->i2s1_in_mux_gpio_sel_1
,
389 "i2s1_in_mux_gpio_sel_1");
391 dev_warn(&pdev
->dev
, "%s devm_gpio_request fail %d\n",
393 gpio_direction_output(priv
->i2s1_in_mux_gpio_sel_1
, 0);
396 priv
->i2s1_in_mux_gpio_sel_2
=
397 of_get_named_gpio(dev
->of_node
, "i2s1-in-sel-gpio2", 0);
398 if (gpio_is_valid(priv
->i2s1_in_mux_gpio_sel_2
)) {
399 ret
= devm_gpio_request(dev
, priv
->i2s1_in_mux_gpio_sel_2
,
400 "i2s1_in_mux_gpio_sel_2");
402 dev_warn(&pdev
->dev
, "%s devm_gpio_request fail2 %d\n",
404 gpio_direction_output(priv
->i2s1_in_mux_gpio_sel_2
, 0);
406 snd_soc_card_set_drvdata(card
, priv
);
408 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
411 dev_err(&pdev
->dev
, "%s snd_soc_register_card fail %d\n",
417 static const struct of_device_id mt2701_cs42448_machine_dt_match
[] = {
418 {.compatible
= "mediatek,mt2701-cs42448-machine",},
423 static struct platform_driver mt2701_cs42448_machine
= {
425 .name
= "mt2701-cs42448",
427 .of_match_table
= mt2701_cs42448_machine_dt_match
,
430 .probe
= mt2701_cs42448_machine_probe
,
433 module_platform_driver(mt2701_cs42448_machine
);
435 /* Module information */
436 MODULE_DESCRIPTION("MT2701 CS42448 ALSA SoC machine driver");
437 MODULE_AUTHOR("Ir Lian <ir.lian@mediatek.com>");
438 MODULE_LICENSE("GPL v2");
439 MODULE_ALIAS("mt2701 cs42448 soc card");