Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / sound / soc / mediatek / mt8173-rt5650-rt5676.c
blob5c4c58c69c516aa32b76ae1e141f01685a6f0f62
1 /*
2 * mt8173-rt5650-rt5676.c -- MT8173 machine driver with RT5650/5676 codecs
4 * Copyright (c) 2015 MediaTek Inc.
5 * Author: Koro Chen <koro.chen@mediatek.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
20 #include <sound/soc.h>
21 #include <sound/jack.h>
22 #include "../codecs/rt5645.h"
23 #include "../codecs/rt5677.h"
25 #define MCLK_FOR_CODECS 12288000
27 static const struct snd_soc_dapm_widget mt8173_rt5650_rt5676_widgets[] = {
28 SND_SOC_DAPM_SPK("Speaker", NULL),
29 SND_SOC_DAPM_MIC("Int Mic", NULL),
30 SND_SOC_DAPM_HP("Headphone", NULL),
31 SND_SOC_DAPM_MIC("Headset Mic", NULL),
34 static const struct snd_soc_dapm_route mt8173_rt5650_rt5676_routes[] = {
35 {"Speaker", NULL, "SPOL"},
36 {"Speaker", NULL, "SPOR"},
37 {"Speaker", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
38 {"Sub DMIC L1", NULL, "Int Mic"}, /* DMIC from 5676 */
39 {"Sub DMIC R1", NULL, "Int Mic"},
40 {"Headphone", NULL, "HPOL"},
41 {"Headphone", NULL, "HPOR"},
42 {"Headphone", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
43 {"Headset Mic", NULL, "micbias1"},
44 {"Headset Mic", NULL, "micbias2"},
45 {"IN1P", NULL, "Headset Mic"},
46 {"IN1N", NULL, "Headset Mic"},
47 {"Sub AIF2RX", NULL, "Headset Mic"}, /* IF2 DAC from 5650 */
50 static const struct snd_kcontrol_new mt8173_rt5650_rt5676_controls[] = {
51 SOC_DAPM_PIN_SWITCH("Speaker"),
52 SOC_DAPM_PIN_SWITCH("Int Mic"),
53 SOC_DAPM_PIN_SWITCH("Headphone"),
54 SOC_DAPM_PIN_SWITCH("Headset Mic"),
57 static int mt8173_rt5650_rt5676_hw_params(struct snd_pcm_substream *substream,
58 struct snd_pcm_hw_params *params)
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
61 int i, ret;
63 for (i = 0; i < rtd->num_codecs; i++) {
64 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
66 /* pll from mclk 12.288M */
67 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
68 params_rate(params) * 512);
69 if (ret)
70 return ret;
72 /* sysclk from pll */
73 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
74 params_rate(params) * 512,
75 SND_SOC_CLOCK_IN);
76 if (ret)
77 return ret;
79 return 0;
82 static struct snd_soc_ops mt8173_rt5650_rt5676_ops = {
83 .hw_params = mt8173_rt5650_rt5676_hw_params,
86 static struct snd_soc_jack mt8173_rt5650_rt5676_jack;
88 static int mt8173_rt5650_rt5676_init(struct snd_soc_pcm_runtime *runtime)
90 struct snd_soc_card *card = runtime->card;
91 struct snd_soc_codec *codec = runtime->codec_dais[0]->codec;
92 struct snd_soc_codec *codec_sub = runtime->codec_dais[1]->codec;
93 int ret;
95 rt5645_sel_asrc_clk_src(codec,
96 RT5645_DA_STEREO_FILTER |
97 RT5645_AD_STEREO_FILTER,
98 RT5645_CLK_SEL_I2S1_ASRC);
99 rt5677_sel_asrc_clk_src(codec_sub,
100 RT5677_DA_STEREO_FILTER |
101 RT5677_AD_STEREO1_FILTER,
102 RT5677_CLK_SEL_I2S1_ASRC);
103 rt5677_sel_asrc_clk_src(codec_sub,
104 RT5677_AD_STEREO2_FILTER |
105 RT5677_I2S2_SOURCE,
106 RT5677_CLK_SEL_I2S2_ASRC);
108 /* enable jack detection */
109 ret = snd_soc_card_jack_new(card, "Headset Jack",
110 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
111 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
112 SND_JACK_BTN_2 | SND_JACK_BTN_3,
113 &mt8173_rt5650_rt5676_jack, NULL, 0);
114 if (ret) {
115 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
116 return ret;
119 return rt5645_set_jack_detect(codec,
120 &mt8173_rt5650_rt5676_jack,
121 &mt8173_rt5650_rt5676_jack,
122 &mt8173_rt5650_rt5676_jack);
125 static struct snd_soc_dai_link_component mt8173_rt5650_rt5676_codecs[] = {
127 .dai_name = "rt5645-aif1",
130 .dai_name = "rt5677-aif1",
134 enum {
135 DAI_LINK_PLAYBACK,
136 DAI_LINK_CAPTURE,
137 DAI_LINK_CODEC_I2S,
138 DAI_LINK_INTERCODEC
141 /* Digital audio interface glue - connects codec <---> CPU */
142 static struct snd_soc_dai_link mt8173_rt5650_rt5676_dais[] = {
143 /* Front End DAI links */
144 [DAI_LINK_PLAYBACK] = {
145 .name = "rt5650_rt5676 Playback",
146 .stream_name = "rt5650_rt5676 Playback",
147 .cpu_dai_name = "DL1",
148 .codec_name = "snd-soc-dummy",
149 .codec_dai_name = "snd-soc-dummy-dai",
150 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
151 .dynamic = 1,
152 .dpcm_playback = 1,
154 [DAI_LINK_CAPTURE] = {
155 .name = "rt5650_rt5676 Capture",
156 .stream_name = "rt5650_rt5676 Capture",
157 .cpu_dai_name = "VUL",
158 .codec_name = "snd-soc-dummy",
159 .codec_dai_name = "snd-soc-dummy-dai",
160 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
161 .dynamic = 1,
162 .dpcm_capture = 1,
165 /* Back End DAI links */
166 [DAI_LINK_CODEC_I2S] = {
167 .name = "Codec",
168 .cpu_dai_name = "I2S",
169 .no_pcm = 1,
170 .codecs = mt8173_rt5650_rt5676_codecs,
171 .num_codecs = 2,
172 .init = mt8173_rt5650_rt5676_init,
173 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
174 SND_SOC_DAIFMT_CBS_CFS,
175 .ops = &mt8173_rt5650_rt5676_ops,
176 .ignore_pmdown_time = 1,
177 .dpcm_playback = 1,
178 .dpcm_capture = 1,
180 /* rt5676 <-> rt5650 intercodec link: Sets rt5676 I2S2 as master */
181 [DAI_LINK_INTERCODEC] = {
182 .name = "rt5650_rt5676 intercodec",
183 .stream_name = "rt5650_rt5676 intercodec",
184 .cpu_dai_name = "snd-soc-dummy-dai",
185 .platform_name = "snd-soc-dummy",
186 .no_pcm = 1,
187 .codec_dai_name = "rt5677-aif2",
188 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
189 SND_SOC_DAIFMT_CBM_CFM,
194 static struct snd_soc_codec_conf mt8173_rt5650_rt5676_codec_conf[] = {
196 .name_prefix = "Sub",
200 static struct snd_soc_card mt8173_rt5650_rt5676_card = {
201 .name = "mtk-rt5650-rt5676",
202 .owner = THIS_MODULE,
203 .dai_link = mt8173_rt5650_rt5676_dais,
204 .num_links = ARRAY_SIZE(mt8173_rt5650_rt5676_dais),
205 .codec_conf = mt8173_rt5650_rt5676_codec_conf,
206 .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5676_codec_conf),
207 .controls = mt8173_rt5650_rt5676_controls,
208 .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5676_controls),
209 .dapm_widgets = mt8173_rt5650_rt5676_widgets,
210 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5676_widgets),
211 .dapm_routes = mt8173_rt5650_rt5676_routes,
212 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5676_routes),
215 static int mt8173_rt5650_rt5676_dev_probe(struct platform_device *pdev)
217 struct snd_soc_card *card = &mt8173_rt5650_rt5676_card;
218 struct device_node *platform_node;
219 int i, ret;
221 platform_node = of_parse_phandle(pdev->dev.of_node,
222 "mediatek,platform", 0);
223 if (!platform_node) {
224 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
225 return -EINVAL;
228 for (i = 0; i < card->num_links; i++) {
229 if (mt8173_rt5650_rt5676_dais[i].platform_name)
230 continue;
231 mt8173_rt5650_rt5676_dais[i].platform_of_node = platform_node;
234 mt8173_rt5650_rt5676_codecs[0].of_node =
235 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
236 if (!mt8173_rt5650_rt5676_codecs[0].of_node) {
237 dev_err(&pdev->dev,
238 "Property 'audio-codec' missing or invalid\n");
239 return -EINVAL;
241 mt8173_rt5650_rt5676_codecs[1].of_node =
242 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
243 if (!mt8173_rt5650_rt5676_codecs[1].of_node) {
244 dev_err(&pdev->dev,
245 "Property 'audio-codec' missing or invalid\n");
246 return -EINVAL;
248 mt8173_rt5650_rt5676_codec_conf[0].of_node =
249 mt8173_rt5650_rt5676_codecs[1].of_node;
251 mt8173_rt5650_rt5676_dais[DAI_LINK_INTERCODEC].codec_of_node =
252 mt8173_rt5650_rt5676_codecs[1].of_node;
254 card->dev = &pdev->dev;
255 platform_set_drvdata(pdev, card);
257 ret = devm_snd_soc_register_card(&pdev->dev, card);
258 if (ret)
259 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
260 __func__, ret);
261 return ret;
264 static const struct of_device_id mt8173_rt5650_rt5676_dt_match[] = {
265 { .compatible = "mediatek,mt8173-rt5650-rt5676", },
268 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5676_dt_match);
270 static struct platform_driver mt8173_rt5650_rt5676_driver = {
271 .driver = {
272 .name = "mtk-rt5650-rt5676",
273 .of_match_table = mt8173_rt5650_rt5676_dt_match,
274 #ifdef CONFIG_PM
275 .pm = &snd_soc_pm_ops,
276 #endif
278 .probe = mt8173_rt5650_rt5676_dev_probe,
281 module_platform_driver(mt8173_rt5650_rt5676_driver);
283 /* Module information */
284 MODULE_DESCRIPTION("MT8173 RT5650 and RT5676 SoC machine driver");
285 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
286 MODULE_LICENSE("GPL v2");
287 MODULE_ALIAS("platform:mtk-rt5650-rt5676");