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;
49 for (i = 0; i < rtd->num_codecs; i++) {
50 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
52 /* pll from mclk 12.288M */
53 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
54 params_rate(params) * 512);
59 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
60 params_rate(params) * 512,
68 static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = {
69 .hw_params = mt8173_rt5650_rt5514_hw_params,
72 static struct snd_soc_jack mt8173_rt5650_rt5514_jack;
74 static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime)
76 struct snd_soc_card *card = runtime->card;
77 struct snd_soc_component *component = runtime->codec_dais[0]->component;
80 rt5645_sel_asrc_clk_src(component,
81 RT5645_DA_STEREO_FILTER |
82 RT5645_AD_STEREO_FILTER,
83 RT5645_CLK_SEL_I2S1_ASRC);
85 /* enable jack detection */
86 ret = snd_soc_card_jack_new(card, "Headset Jack",
87 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
88 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
89 SND_JACK_BTN_2 | SND_JACK_BTN_3,
90 &mt8173_rt5650_rt5514_jack, NULL, 0);
92 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
96 return rt5645_set_jack_detect(component,
97 &mt8173_rt5650_rt5514_jack,
98 &mt8173_rt5650_rt5514_jack,
99 &mt8173_rt5650_rt5514_jack);
102 static struct snd_soc_dai_link_component mt8173_rt5650_rt5514_codecs[] = {
104 .dai_name = "rt5645-aif1",
107 .dai_name = "rt5514-aif1",
117 /* Digital audio interface glue - connects codec <---> CPU */
118 static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = {
119 /* Front End DAI links */
120 [DAI_LINK_PLAYBACK] = {
121 .name = "rt5650_rt5514 Playback",
122 .stream_name = "rt5650_rt5514 Playback",
123 .cpu_dai_name = "DL1",
124 .codec_name = "snd-soc-dummy",
125 .codec_dai_name = "snd-soc-dummy-dai",
126 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
130 [DAI_LINK_CAPTURE] = {
131 .name = "rt5650_rt5514 Capture",
132 .stream_name = "rt5650_rt5514 Capture",
133 .cpu_dai_name = "VUL",
134 .codec_name = "snd-soc-dummy",
135 .codec_dai_name = "snd-soc-dummy-dai",
136 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
140 /* Back End DAI links */
141 [DAI_LINK_CODEC_I2S] = {
143 .cpu_dai_name = "I2S",
145 .codecs = mt8173_rt5650_rt5514_codecs,
147 .init = mt8173_rt5650_rt5514_init,
148 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
149 SND_SOC_DAIFMT_CBS_CFS,
150 .ops = &mt8173_rt5650_rt5514_ops,
151 .ignore_pmdown_time = 1,
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;
184 platform_node = of_parse_phandle(pdev->dev.of_node,
185 "mediatek,platform", 0);
186 if (!platform_node) {
187 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
191 for (i = 0; i < card->num_links; i++) {
192 if (mt8173_rt5650_rt5514_dais[i].platform_name)
194 mt8173_rt5650_rt5514_dais[i].platform_of_node = platform_node;
197 mt8173_rt5650_rt5514_codecs[0].of_node =
198 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
199 if (!mt8173_rt5650_rt5514_codecs[0].of_node) {
201 "Property 'audio-codec' missing or invalid\n");
204 mt8173_rt5650_rt5514_codecs[1].of_node =
205 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
206 if (!mt8173_rt5650_rt5514_codecs[1].of_node) {
208 "Property 'audio-codec' missing or invalid\n");
211 mt8173_rt5650_rt5514_codec_conf[0].of_node =
212 mt8173_rt5650_rt5514_codecs[1].of_node;
214 card->dev = &pdev->dev;
216 ret = devm_snd_soc_register_card(&pdev->dev, card);
218 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
221 of_node_put(platform_node);
225 static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = {
226 { .compatible = "mediatek,mt8173-rt5650-rt5514", },
229 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match);
231 static struct platform_driver mt8173_rt5650_rt5514_driver = {
233 .name = "mtk-rt5650-rt5514",
234 .of_match_table = mt8173_rt5650_rt5514_dt_match,
236 .pm = &snd_soc_pm_ops,
239 .probe = mt8173_rt5650_rt5514_dev_probe,
242 module_platform_driver(mt8173_rt5650_rt5514_driver);
244 /* Module information */
245 MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
246 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
247 MODULE_LICENSE("GPL v2");
248 MODULE_ALIAS("platform:mtk-rt5650-rt5514");