blob: 40ebefd625c1e4370e1f6527e3003b03fac4d34d [file] [log] [blame]
Koro Chen0f83f922016-02-19 10:35:59 +08001/*
2 * mt8173-rt5650.c -- MT8173 machine driver with RT5650 codecs
3 *
4 * Copyright (c) 2016 MediaTek Inc.
5 * Author: Koro Chen <koro.chen@mediatek.com>
6 *
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.
10 *
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.
15 */
16
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>
Garlic Tseng0d1d7a62016-06-17 15:43:52 +080022#include "../../codecs/rt5645.h"
Koro Chen0f83f922016-02-19 10:35:59 +080023
24#define MCLK_FOR_CODECS 12288000
25
PC Liao0c198ed2016-06-01 20:00:12 +080026enum mt8173_rt5650_mclk {
27 MT8173_RT5650_MCLK_EXTERNAL = 0,
28 MT8173_RT5650_MCLK_INTERNAL,
29};
30
31struct mt8173_rt5650_platform_data {
32 enum mt8173_rt5650_mclk pll_from;
33 /* 0 = external oscillator; 1 = internal source from mt8173 */
34};
35
36static struct mt8173_rt5650_platform_data mt8173_rt5650_priv = {
37 .pll_from = MT8173_RT5650_MCLK_EXTERNAL,
38};
39
Koro Chen0f83f922016-02-19 10:35:59 +080040static const struct snd_soc_dapm_widget mt8173_rt5650_widgets[] = {
41 SND_SOC_DAPM_SPK("Speaker", NULL),
42 SND_SOC_DAPM_MIC("Int Mic", NULL),
43 SND_SOC_DAPM_HP("Headphone", NULL),
44 SND_SOC_DAPM_MIC("Headset Mic", NULL),
45};
46
47static const struct snd_soc_dapm_route mt8173_rt5650_routes[] = {
48 {"Speaker", NULL, "SPOL"},
49 {"Speaker", NULL, "SPOR"},
50 {"DMIC L1", NULL, "Int Mic"},
51 {"DMIC R1", NULL, "Int Mic"},
52 {"Headphone", NULL, "HPOL"},
53 {"Headphone", NULL, "HPOR"},
Koro Chen0f83f922016-02-19 10:35:59 +080054 {"IN1P", NULL, "Headset Mic"},
55 {"IN1N", NULL, "Headset Mic"},
56};
57
58static const struct snd_kcontrol_new mt8173_rt5650_controls[] = {
59 SOC_DAPM_PIN_SWITCH("Speaker"),
60 SOC_DAPM_PIN_SWITCH("Int Mic"),
61 SOC_DAPM_PIN_SWITCH("Headphone"),
62 SOC_DAPM_PIN_SWITCH("Headset Mic"),
63};
64
65static int mt8173_rt5650_hw_params(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params *params)
67{
68 struct snd_soc_pcm_runtime *rtd = substream->private_data;
PC Liao0c198ed2016-06-01 20:00:12 +080069 unsigned int mclk_clock;
Koro Chen0f83f922016-02-19 10:35:59 +080070 int i, ret;
71
PC Liao0c198ed2016-06-01 20:00:12 +080072 switch (mt8173_rt5650_priv.pll_from) {
73 case MT8173_RT5650_MCLK_EXTERNAL:
74 /* mclk = 12.288M */
75 mclk_clock = MCLK_FOR_CODECS;
76 break;
77 case MT8173_RT5650_MCLK_INTERNAL:
78 /* mclk = sampling rate*256 */
79 mclk_clock = params_rate(params) * 256;
80 break;
81 default:
82 /* mclk = 12.288M */
83 mclk_clock = MCLK_FOR_CODECS;
84 break;
85 }
86
Koro Chen0f83f922016-02-19 10:35:59 +080087 for (i = 0; i < rtd->num_codecs; i++) {
88 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
89
PC Liao0c198ed2016-06-01 20:00:12 +080090 /* pll from mclk */
91 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, mclk_clock,
Koro Chen0f83f922016-02-19 10:35:59 +080092 params_rate(params) * 512);
93 if (ret)
94 return ret;
95
96 /* sysclk from pll */
97 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
98 params_rate(params) * 512,
99 SND_SOC_CLOCK_IN);
100 if (ret)
101 return ret;
102 }
103 return 0;
104}
105
Bhumika Goyal424dfbf2017-03-18 23:13:14 +0530106static const struct snd_soc_ops mt8173_rt5650_ops = {
Koro Chen0f83f922016-02-19 10:35:59 +0800107 .hw_params = mt8173_rt5650_hw_params,
108};
109
110static struct snd_soc_jack mt8173_rt5650_jack;
111
112static int mt8173_rt5650_init(struct snd_soc_pcm_runtime *runtime)
113{
114 struct snd_soc_card *card = runtime->card;
115 struct snd_soc_codec *codec = runtime->codec_dais[0]->codec;
PC Liaod349cae2016-04-21 19:38:14 +0800116 const char *codec_capture_dai = runtime->codec_dais[1]->name;
Koro Chen0f83f922016-02-19 10:35:59 +0800117 int ret;
118
119 rt5645_sel_asrc_clk_src(codec,
PC Liaod349cae2016-04-21 19:38:14 +0800120 RT5645_DA_STEREO_FILTER,
Koro Chen0f83f922016-02-19 10:35:59 +0800121 RT5645_CLK_SEL_I2S1_ASRC);
PC Liaod349cae2016-04-21 19:38:14 +0800122
123 if (!strcmp(codec_capture_dai, "rt5645-aif1")) {
124 rt5645_sel_asrc_clk_src(codec,
125 RT5645_AD_STEREO_FILTER,
126 RT5645_CLK_SEL_I2S1_ASRC);
127 } else if (!strcmp(codec_capture_dai, "rt5645-aif2")) {
128 rt5645_sel_asrc_clk_src(codec,
129 RT5645_AD_STEREO_FILTER,
130 RT5645_CLK_SEL_I2S2_ASRC);
131 } else {
132 dev_warn(card->dev,
133 "Only one dai codec found in DTS, enabled rt5645 AD filter\n");
134 rt5645_sel_asrc_clk_src(codec,
135 RT5645_AD_STEREO_FILTER,
136 RT5645_CLK_SEL_I2S1_ASRC);
137 }
138
Koro Chen0f83f922016-02-19 10:35:59 +0800139 /* enable jack detection */
140 ret = snd_soc_card_jack_new(card, "Headset Jack",
141 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
142 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
143 SND_JACK_BTN_2 | SND_JACK_BTN_3,
144 &mt8173_rt5650_jack, NULL, 0);
145 if (ret) {
146 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
147 return ret;
148 }
149
150 return rt5645_set_jack_detect(codec,
151 &mt8173_rt5650_jack,
152 &mt8173_rt5650_jack,
153 &mt8173_rt5650_jack);
154}
155
156static struct snd_soc_dai_link_component mt8173_rt5650_codecs[] = {
157 {
PC Liaod349cae2016-04-21 19:38:14 +0800158 /* Playback */
159 .dai_name = "rt5645-aif1",
160 },
161 {
162 /* Capture */
Koro Chen0f83f922016-02-19 10:35:59 +0800163 .dai_name = "rt5645-aif1",
164 },
165};
166
167enum {
168 DAI_LINK_PLAYBACK,
169 DAI_LINK_CAPTURE,
PC Liao97e1145a2016-07-05 11:26:21 +0200170 DAI_LINK_HDMI,
Koro Chen0f83f922016-02-19 10:35:59 +0800171 DAI_LINK_CODEC_I2S,
PC Liao97e1145a2016-07-05 11:26:21 +0200172 DAI_LINK_HDMI_I2S,
Koro Chen0f83f922016-02-19 10:35:59 +0800173};
174
175/* Digital audio interface glue - connects codec <---> CPU */
176static struct snd_soc_dai_link mt8173_rt5650_dais[] = {
177 /* Front End DAI links */
178 [DAI_LINK_PLAYBACK] = {
179 .name = "rt5650 Playback",
180 .stream_name = "rt5650 Playback",
181 .cpu_dai_name = "DL1",
182 .codec_name = "snd-soc-dummy",
183 .codec_dai_name = "snd-soc-dummy-dai",
184 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
185 .dynamic = 1,
186 .dpcm_playback = 1,
187 },
188 [DAI_LINK_CAPTURE] = {
189 .name = "rt5650 Capture",
190 .stream_name = "rt5650 Capture",
191 .cpu_dai_name = "VUL",
192 .codec_name = "snd-soc-dummy",
193 .codec_dai_name = "snd-soc-dummy-dai",
194 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
195 .dynamic = 1,
196 .dpcm_capture = 1,
197 },
PC Liao97e1145a2016-07-05 11:26:21 +0200198 [DAI_LINK_HDMI] = {
199 .name = "HDMI",
200 .stream_name = "HDMI PCM",
201 .cpu_dai_name = "HDMI",
202 .codec_name = "snd-soc-dummy",
203 .codec_dai_name = "snd-soc-dummy-dai",
204 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
205 .dynamic = 1,
206 .dpcm_playback = 1,
207 },
Koro Chen0f83f922016-02-19 10:35:59 +0800208 /* Back End DAI links */
209 [DAI_LINK_CODEC_I2S] = {
210 .name = "Codec",
211 .cpu_dai_name = "I2S",
212 .no_pcm = 1,
213 .codecs = mt8173_rt5650_codecs,
PC Liaod349cae2016-04-21 19:38:14 +0800214 .num_codecs = 2,
Koro Chen0f83f922016-02-19 10:35:59 +0800215 .init = mt8173_rt5650_init,
216 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
217 SND_SOC_DAIFMT_CBS_CFS,
218 .ops = &mt8173_rt5650_ops,
219 .ignore_pmdown_time = 1,
220 .dpcm_playback = 1,
221 .dpcm_capture = 1,
222 },
PC Liao97e1145a2016-07-05 11:26:21 +0200223 [DAI_LINK_HDMI_I2S] = {
224 .name = "HDMI BE",
225 .cpu_dai_name = "HDMIO",
226 .no_pcm = 1,
227 .codec_dai_name = "i2s-hifi",
228 .dpcm_playback = 1,
229 },
Koro Chen0f83f922016-02-19 10:35:59 +0800230};
231
232static struct snd_soc_card mt8173_rt5650_card = {
233 .name = "mtk-rt5650",
234 .owner = THIS_MODULE,
235 .dai_link = mt8173_rt5650_dais,
236 .num_links = ARRAY_SIZE(mt8173_rt5650_dais),
237 .controls = mt8173_rt5650_controls,
238 .num_controls = ARRAY_SIZE(mt8173_rt5650_controls),
239 .dapm_widgets = mt8173_rt5650_widgets,
240 .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_widgets),
241 .dapm_routes = mt8173_rt5650_routes,
242 .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_routes),
243};
244
245static int mt8173_rt5650_dev_probe(struct platform_device *pdev)
246{
247 struct snd_soc_card *card = &mt8173_rt5650_card;
248 struct device_node *platform_node;
PC Liaod349cae2016-04-21 19:38:14 +0800249 struct device_node *np;
250 const char *codec_capture_dai;
Koro Chen0f83f922016-02-19 10:35:59 +0800251 int i, ret;
252
253 platform_node = of_parse_phandle(pdev->dev.of_node,
254 "mediatek,platform", 0);
255 if (!platform_node) {
256 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
257 return -EINVAL;
258 }
259
260 for (i = 0; i < card->num_links; i++) {
261 if (mt8173_rt5650_dais[i].platform_name)
262 continue;
263 mt8173_rt5650_dais[i].platform_of_node = platform_node;
264 }
265
266 mt8173_rt5650_codecs[0].of_node =
267 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
268 if (!mt8173_rt5650_codecs[0].of_node) {
269 dev_err(&pdev->dev,
270 "Property 'audio-codec' missing or invalid\n");
271 return -EINVAL;
272 }
PC Liaod349cae2016-04-21 19:38:14 +0800273 mt8173_rt5650_codecs[1].of_node = mt8173_rt5650_codecs[0].of_node;
274
275 if (of_find_node_by_name(platform_node, "codec-capture")) {
276 np = of_get_child_by_name(pdev->dev.of_node, "codec-capture");
277 if (!np) {
278 dev_err(&pdev->dev,
279 "%s: Can't find codec-capture DT node\n",
280 __func__);
281 return -EINVAL;
282 }
283 ret = snd_soc_of_get_dai_name(np, &codec_capture_dai);
284 if (ret < 0) {
285 dev_err(&pdev->dev,
286 "%s codec_capture_dai name fail %d\n",
287 __func__, ret);
288 return ret;
289 }
290 mt8173_rt5650_codecs[1].dai_name = codec_capture_dai;
291 }
292
PC Liao0c198ed2016-06-01 20:00:12 +0800293 if (device_property_present(&pdev->dev, "mediatek,mclk")) {
294 ret = device_property_read_u32(&pdev->dev,
295 "mediatek,mclk",
296 &mt8173_rt5650_priv.pll_from);
297 if (ret) {
298 dev_err(&pdev->dev,
299 "%s snd_soc_register_card fail %d\n",
300 __func__, ret);
301 }
302 }
303
PC Liao97e1145a2016-07-05 11:26:21 +0200304 mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codec_of_node =
305 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
306 if (!mt8173_rt5650_dais[DAI_LINK_HDMI_I2S].codec_of_node) {
307 dev_err(&pdev->dev,
308 "Property 'audio-codec' missing or invalid\n");
309 return -EINVAL;
310 }
Koro Chen0f83f922016-02-19 10:35:59 +0800311 card->dev = &pdev->dev;
Koro Chen0f83f922016-02-19 10:35:59 +0800312
313 ret = devm_snd_soc_register_card(&pdev->dev, card);
314 if (ret)
315 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
316 __func__, ret);
317 return ret;
318}
319
320static const struct of_device_id mt8173_rt5650_dt_match[] = {
321 { .compatible = "mediatek,mt8173-rt5650", },
322 { }
323};
324MODULE_DEVICE_TABLE(of, mt8173_rt5650_dt_match);
325
326static struct platform_driver mt8173_rt5650_driver = {
327 .driver = {
328 .name = "mtk-rt5650",
329 .of_match_table = mt8173_rt5650_dt_match,
330#ifdef CONFIG_PM
331 .pm = &snd_soc_pm_ops,
332#endif
333 },
334 .probe = mt8173_rt5650_dev_probe,
335};
336
337module_platform_driver(mt8173_rt5650_driver);
338
339/* Module information */
340MODULE_DESCRIPTION("MT8173 RT5650 SoC machine driver");
341MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
342MODULE_LICENSE("GPL v2");
343MODULE_ALIAS("platform:mtk-rt5650");
344