blob: 82675ed057dee9eb5df78351c120af92041fd506 [file] [log] [blame]
Ryder Lee3a280ed2018-04-25 12:19:59 +08001// SPDX-License-Identifier: GPL-2.0
Koro Chen662e8d92015-06-15 22:38:04 +08002/*
3 * mt8173-rt5650-rt5676.c -- MT8173 machine driver with RT5650/5676 codecs
4 *
5 * Copyright (c) 2015 MediaTek Inc.
6 * Author: Koro Chen <koro.chen@mediatek.com>
Koro Chen662e8d92015-06-15 22:38:04 +08007 */
8
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>
Garlic Tseng0d1d7a62016-06-17 15:43:52 +080014#include "../../codecs/rt5645.h"
15#include "../../codecs/rt5677.h"
Koro Chen662e8d92015-06-15 22:38:04 +080016
17#define MCLK_FOR_CODECS 12288000
18
19static const struct snd_soc_dapm_widget mt8173_rt5650_rt5676_widgets[] = {
20 SND_SOC_DAPM_SPK("Speaker", NULL),
21 SND_SOC_DAPM_MIC("Int Mic", NULL),
22 SND_SOC_DAPM_HP("Headphone", NULL),
23 SND_SOC_DAPM_MIC("Headset Mic", NULL),
24};
25
26static const struct snd_soc_dapm_route mt8173_rt5650_rt5676_routes[] = {
27 {"Speaker", NULL, "SPOL"},
28 {"Speaker", NULL, "SPOR"},
29 {"Speaker", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
30 {"Sub DMIC L1", NULL, "Int Mic"}, /* DMIC from 5676 */
31 {"Sub DMIC R1", NULL, "Int Mic"},
32 {"Headphone", NULL, "HPOL"},
33 {"Headphone", NULL, "HPOR"},
34 {"Headphone", NULL, "Sub AIF2TX"}, /* IF2 ADC to 5650 */
Koro Chen662e8d92015-06-15 22:38:04 +080035 {"IN1P", NULL, "Headset Mic"},
36 {"IN1N", NULL, "Headset Mic"},
37 {"Sub AIF2RX", NULL, "Headset Mic"}, /* IF2 DAC from 5650 */
38};
39
40static const struct snd_kcontrol_new mt8173_rt5650_rt5676_controls[] = {
41 SOC_DAPM_PIN_SWITCH("Speaker"),
42 SOC_DAPM_PIN_SWITCH("Int Mic"),
43 SOC_DAPM_PIN_SWITCH("Headphone"),
44 SOC_DAPM_PIN_SWITCH("Headset Mic"),
45};
46
47static int mt8173_rt5650_rt5676_hw_params(struct snd_pcm_substream *substream,
48 struct snd_pcm_hw_params *params)
49{
50 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000051 struct snd_soc_dai *codec_dai;
Koro Chen662e8d92015-06-15 22:38:04 +080052 int i, ret;
53
Kuninori Morimoto0b7990e2018-09-03 02:12:56 +000054 for_each_rtd_codec_dai(rtd, i, codec_dai) {
Koro Chen662e8d92015-06-15 22:38:04 +080055 /* pll from mclk 12.288M */
56 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
57 params_rate(params) * 512);
58 if (ret)
59 return ret;
60
61 /* sysclk from pll */
62 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
63 params_rate(params) * 512,
64 SND_SOC_CLOCK_IN);
65 if (ret)
66 return ret;
67 }
68 return 0;
69}
70
Bhumika Goyal424dfbf2017-03-18 23:13:14 +053071static const struct snd_soc_ops mt8173_rt5650_rt5676_ops = {
Koro Chen662e8d92015-06-15 22:38:04 +080072 .hw_params = mt8173_rt5650_rt5676_hw_params,
73};
74
75static struct snd_soc_jack mt8173_rt5650_rt5676_jack;
76
77static int mt8173_rt5650_rt5676_init(struct snd_soc_pcm_runtime *runtime)
78{
79 struct snd_soc_card *card = runtime->card;
Kuninori Morimoto79223bf2018-01-29 03:49:31 +000080 struct snd_soc_component *component = runtime->codec_dais[0]->component;
81 struct snd_soc_component *component_sub = runtime->codec_dais[1]->component;
Koro Chen662e8d92015-06-15 22:38:04 +080082 int ret;
83
Kuninori Morimoto79223bf2018-01-29 03:49:31 +000084 rt5645_sel_asrc_clk_src(component,
Koro Chen662e8d92015-06-15 22:38:04 +080085 RT5645_DA_STEREO_FILTER |
86 RT5645_AD_STEREO_FILTER,
87 RT5645_CLK_SEL_I2S1_ASRC);
Kuninori Morimoto79223bf2018-01-29 03:49:31 +000088 rt5677_sel_asrc_clk_src(component_sub,
Koro Chen662e8d92015-06-15 22:38:04 +080089 RT5677_DA_STEREO_FILTER |
90 RT5677_AD_STEREO1_FILTER,
91 RT5677_CLK_SEL_I2S1_ASRC);
Kuninori Morimoto79223bf2018-01-29 03:49:31 +000092 rt5677_sel_asrc_clk_src(component_sub,
Koro Chen662e8d92015-06-15 22:38:04 +080093 RT5677_AD_STEREO2_FILTER |
94 RT5677_I2S2_SOURCE,
95 RT5677_CLK_SEL_I2S2_ASRC);
96
97 /* enable jack detection */
98 ret = snd_soc_card_jack_new(card, "Headset Jack",
99 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
100 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
101 SND_JACK_BTN_2 | SND_JACK_BTN_3,
102 &mt8173_rt5650_rt5676_jack, NULL, 0);
103 if (ret) {
104 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
105 return ret;
106 }
107
Kuninori Morimoto79223bf2018-01-29 03:49:31 +0000108 return rt5645_set_jack_detect(component,
Koro Chen662e8d92015-06-15 22:38:04 +0800109 &mt8173_rt5650_rt5676_jack,
110 &mt8173_rt5650_rt5676_jack,
111 &mt8173_rt5650_rt5676_jack);
112}
113
114static struct snd_soc_dai_link_component mt8173_rt5650_rt5676_codecs[] = {
115 {
116 .dai_name = "rt5645-aif1",
117 },
118 {
119 .dai_name = "rt5677-aif1",
120 },
121};
122
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100123enum {
124 DAI_LINK_PLAYBACK,
125 DAI_LINK_CAPTURE,
Koro Chenc0133e32016-04-20 10:59:56 +0200126 DAI_LINK_HDMI,
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100127 DAI_LINK_CODEC_I2S,
Koro Chenc0133e32016-04-20 10:59:56 +0200128 DAI_LINK_HDMI_I2S,
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100129 DAI_LINK_INTERCODEC
130};
131
Koro Chen662e8d92015-06-15 22:38:04 +0800132/* Digital audio interface glue - connects codec <---> CPU */
133static struct snd_soc_dai_link mt8173_rt5650_rt5676_dais[] = {
134 /* Front End DAI links */
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100135 [DAI_LINK_PLAYBACK] = {
Koro Chen662e8d92015-06-15 22:38:04 +0800136 .name = "rt5650_rt5676 Playback",
137 .stream_name = "rt5650_rt5676 Playback",
138 .cpu_dai_name = "DL1",
Koro Chen662e8d92015-06-15 22:38:04 +0800139 .codec_name = "snd-soc-dummy",
140 .codec_dai_name = "snd-soc-dummy-dai",
141 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
142 .dynamic = 1,
143 .dpcm_playback = 1,
144 },
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100145 [DAI_LINK_CAPTURE] = {
Koro Chen662e8d92015-06-15 22:38:04 +0800146 .name = "rt5650_rt5676 Capture",
147 .stream_name = "rt5650_rt5676 Capture",
148 .cpu_dai_name = "VUL",
Koro Chen662e8d92015-06-15 22:38:04 +0800149 .codec_name = "snd-soc-dummy",
150 .codec_dai_name = "snd-soc-dummy-dai",
151 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
152 .dynamic = 1,
153 .dpcm_capture = 1,
154 },
Koro Chenc0133e32016-04-20 10:59:56 +0200155 [DAI_LINK_HDMI] = {
156 .name = "HDMI",
157 .stream_name = "HDMI PCM",
158 .cpu_dai_name = "HDMI",
159 .codec_name = "snd-soc-dummy",
160 .codec_dai_name = "snd-soc-dummy-dai",
161 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
162 .dynamic = 1,
163 .dpcm_playback = 1,
164 },
Koro Chen662e8d92015-06-15 22:38:04 +0800165
166 /* Back End DAI links */
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100167 [DAI_LINK_CODEC_I2S] = {
Koro Chen662e8d92015-06-15 22:38:04 +0800168 .name = "Codec",
169 .cpu_dai_name = "I2S",
Koro Chen662e8d92015-06-15 22:38:04 +0800170 .no_pcm = 1,
171 .codecs = mt8173_rt5650_rt5676_codecs,
172 .num_codecs = 2,
173 .init = mt8173_rt5650_rt5676_init,
174 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
175 SND_SOC_DAIFMT_CBS_CFS,
176 .ops = &mt8173_rt5650_rt5676_ops,
177 .ignore_pmdown_time = 1,
178 .dpcm_playback = 1,
179 .dpcm_capture = 1,
180 },
Koro Chenc0133e32016-04-20 10:59:56 +0200181 [DAI_LINK_HDMI_I2S] = {
182 .name = "HDMI BE",
183 .cpu_dai_name = "HDMIO",
184 .no_pcm = 1,
185 .codec_dai_name = "i2s-hifi",
186 .dpcm_playback = 1,
187 },
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100188 /* rt5676 <-> rt5650 intercodec link: Sets rt5676 I2S2 as master */
189 [DAI_LINK_INTERCODEC] = {
Koro Chen662e8d92015-06-15 22:38:04 +0800190 .name = "rt5650_rt5676 intercodec",
191 .stream_name = "rt5650_rt5676 intercodec",
192 .cpu_dai_name = "snd-soc-dummy-dai",
193 .platform_name = "snd-soc-dummy",
194 .no_pcm = 1,
195 .codec_dai_name = "rt5677-aif2",
196 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
197 SND_SOC_DAIFMT_CBM_CFM,
198 },
199
200};
201
202static struct snd_soc_codec_conf mt8173_rt5650_rt5676_codec_conf[] = {
203 {
204 .name_prefix = "Sub",
205 },
206};
207
208static struct snd_soc_card mt8173_rt5650_rt5676_card = {
209 .name = "mtk-rt5650-rt5676",
Axel Lin54d86972015-08-21 20:59:21 +0800210 .owner = THIS_MODULE,
Koro Chen662e8d92015-06-15 22:38:04 +0800211 .dai_link = mt8173_rt5650_rt5676_dais,
212 .num_links = ARRAY_SIZE(mt8173_rt5650_rt5676_dais),
213 .codec_conf = mt8173_rt5650_rt5676_codec_conf,
214 .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5676_codec_conf),
215 .controls = mt8173_rt5650_rt5676_controls,
216 .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5676_controls),
217 .dapm_widgets = mt8173_rt5650_rt5676_widgets,
218 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5676_widgets),
219 .dapm_routes = mt8173_rt5650_rt5676_routes,
220 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5676_routes),
221};
222
223static int mt8173_rt5650_rt5676_dev_probe(struct platform_device *pdev)
224{
225 struct snd_soc_card *card = &mt8173_rt5650_rt5676_card;
Koro Chen94319ba2015-07-09 10:51:27 +0800226 struct device_node *platform_node;
227 int i, ret;
228
229 platform_node = of_parse_phandle(pdev->dev.of_node,
230 "mediatek,platform", 0);
231 if (!platform_node) {
232 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
233 return -EINVAL;
234 }
235
236 for (i = 0; i < card->num_links; i++) {
237 if (mt8173_rt5650_rt5676_dais[i].platform_name)
238 continue;
239 mt8173_rt5650_rt5676_dais[i].platform_of_node = platform_node;
240 }
Koro Chen662e8d92015-06-15 22:38:04 +0800241
242 mt8173_rt5650_rt5676_codecs[0].of_node =
243 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
244 if (!mt8173_rt5650_rt5676_codecs[0].of_node) {
245 dev_err(&pdev->dev,
246 "Property 'audio-codec' missing or invalid\n");
247 return -EINVAL;
248 }
249 mt8173_rt5650_rt5676_codecs[1].of_node =
250 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
251 if (!mt8173_rt5650_rt5676_codecs[1].of_node) {
252 dev_err(&pdev->dev,
253 "Property 'audio-codec' missing or invalid\n");
254 return -EINVAL;
255 }
256 mt8173_rt5650_rt5676_codec_conf[0].of_node =
257 mt8173_rt5650_rt5676_codecs[1].of_node;
258
Philipp Zabel05f5afd2016-03-02 15:49:06 +0100259 mt8173_rt5650_rt5676_dais[DAI_LINK_INTERCODEC].codec_of_node =
Koro Chen662e8d92015-06-15 22:38:04 +0800260 mt8173_rt5650_rt5676_codecs[1].of_node;
261
Koro Chenc0133e32016-04-20 10:59:56 +0200262 mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codec_of_node =
263 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 2);
264 if (!mt8173_rt5650_rt5676_dais[DAI_LINK_HDMI_I2S].codec_of_node) {
265 dev_err(&pdev->dev,
266 "Property 'audio-codec' missing or invalid\n");
267 return -EINVAL;
268 }
269
Koro Chen662e8d92015-06-15 22:38:04 +0800270 card->dev = &pdev->dev;
Koro Chen662e8d92015-06-15 22:38:04 +0800271
Axel Lin887e83e2015-08-30 17:18:50 +0800272 ret = devm_snd_soc_register_card(&pdev->dev, card);
Koro Chen662e8d92015-06-15 22:38:04 +0800273 if (ret)
274 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
275 __func__, ret);
276 return ret;
277}
278
Koro Chen662e8d92015-06-15 22:38:04 +0800279static const struct of_device_id mt8173_rt5650_rt5676_dt_match[] = {
280 { .compatible = "mediatek,mt8173-rt5650-rt5676", },
281 { }
282};
283MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5676_dt_match);
284
285static struct platform_driver mt8173_rt5650_rt5676_driver = {
286 .driver = {
287 .name = "mtk-rt5650-rt5676",
Koro Chen662e8d92015-06-15 22:38:04 +0800288 .of_match_table = mt8173_rt5650_rt5676_dt_match,
289#ifdef CONFIG_PM
290 .pm = &snd_soc_pm_ops,
291#endif
292 },
293 .probe = mt8173_rt5650_rt5676_dev_probe,
Koro Chen662e8d92015-06-15 22:38:04 +0800294};
295
296module_platform_driver(mt8173_rt5650_rt5676_driver);
297
298/* Module information */
299MODULE_DESCRIPTION("MT8173 RT5650 and RT5676 SoC machine driver");
300MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
301MODULE_LICENSE("GPL v2");
302MODULE_ALIAS("platform:mtk-rt5650-rt5676");
303