1 // SPDX-License-Identifier: GPL-2.0
3 * mt8173-rt5650-rt5514.c -- MT8173 machine driver with RT5650/5514 codecs
5 * Copyright (c) 2016 MediaTek Inc.
6 * Author: Koro Chen <koro.chen@mediatek.com>
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"
16 #define MCLK_FOR_CODECS 12288000
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
),
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"},
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"),
43 static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream
*substream
,
44 struct snd_pcm_hw_params
*params
)
46 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
47 struct snd_soc_dai
*codec_dai
;
50 for_each_rtd_codec_dai(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);
58 ret
= snd_soc_dai_set_sysclk(codec_dai
, 1,
59 params_rate(params
) * 512,
67 static const struct snd_soc_ops mt8173_rt5650_rt5514_ops
= {
68 .hw_params
= mt8173_rt5650_rt5514_hw_params
,
71 static struct snd_soc_jack mt8173_rt5650_rt5514_jack
;
73 static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime
*runtime
)
75 struct snd_soc_card
*card
= runtime
->card
;
76 struct snd_soc_component
*component
= runtime
->codec_dais
[0]->component
;
79 rt5645_sel_asrc_clk_src(component
,
80 RT5645_DA_STEREO_FILTER
|
81 RT5645_AD_STEREO_FILTER
,
82 RT5645_CLK_SEL_I2S1_ASRC
);
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
, NULL
, 0);
91 dev_err(card
->dev
, "Can't new Headset Jack %d\n", ret
);
95 return rt5645_set_jack_detect(component
,
96 &mt8173_rt5650_rt5514_jack
,
97 &mt8173_rt5650_rt5514_jack
,
98 &mt8173_rt5650_rt5514_jack
);
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()));
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()));
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()));
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
},
132 SND_SOC_DAILINK_REG(playback
),
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
},
140 SND_SOC_DAILINK_REG(capture
),
142 /* Back End DAI links */
143 [DAI_LINK_CODEC_I2S
] = {
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,
153 SND_SOC_DAILINK_REG(codec
),
157 static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf
[] = {
159 .name_prefix
= "Sub",
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
),
178 static int mt8173_rt5650_rt5514_dev_probe(struct platform_device
*pdev
)
180 struct snd_soc_card
*card
= &mt8173_rt5650_rt5514_card
;
181 struct device_node
*platform_node
;
182 struct snd_soc_dai_link
*dai_link
;
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");
192 for_each_card_prelinks(card
, i
, dai_link
) {
193 if (dai_link
->platforms
->name
)
195 dai_link
->platforms
->of_node
= platform_node
;
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
) {
202 "Property 'audio-codec' missing or invalid\n");
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
) {
209 "Property 'audio-codec' missing or invalid\n");
212 mt8173_rt5650_rt5514_codec_conf
[0].dlc
.of_node
=
213 mt8173_rt5650_rt5514_dais
[DAI_LINK_CODEC_I2S
].codecs
[1].of_node
;
215 card
->dev
= &pdev
->dev
;
217 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
219 dev_err(&pdev
->dev
, "%s snd_soc_register_card fail %d\n",
224 static const struct of_device_id mt8173_rt5650_rt5514_dt_match
[] = {
225 { .compatible
= "mediatek,mt8173-rt5650-rt5514", },
228 MODULE_DEVICE_TABLE(of
, mt8173_rt5650_rt5514_dt_match
);
230 static struct platform_driver mt8173_rt5650_rt5514_driver
= {
232 .name
= "mtk-rt5650-rt5514",
233 .of_match_table
= mt8173_rt5650_rt5514_dt_match
,
235 .pm
= &snd_soc_pm_ops
,
238 .probe
= mt8173_rt5650_rt5514_dev_probe
,
241 module_platform_driver(mt8173_rt5650_rt5514_driver
);
243 /* Module information */
244 MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
245 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
246 MODULE_LICENSE("GPL v2");
247 MODULE_ALIAS("platform:mtk-rt5650-rt5514");