blob: 43f99e59a07866f83a7453df601f9009fdf5880d [file] [log] [blame]
Shunli Wangebbddc72019-03-29 16:34:48 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// mt8183-da7219-max98357.c
4// -- MT8183-DA7219-MAX98357 ALSA SoC machine driver
5//
6// Copyright (c) 2018 MediaTek Inc.
7// Author: Shunli Wang <shunli.wang@mediatek.com>
8
9#include <linux/module.h>
10#include <sound/pcm_params.h>
11#include <sound/soc.h>
12#include <sound/jack.h>
13#include <linux/pinctrl/consumer.h>
14
15#include "mt8183-afe-common.h"
16#include "../../codecs/da7219-aad.h"
17#include "../../codecs/da7219.h"
18
19static struct snd_soc_jack headset_jack;
20
Shunli Wangebbddc72019-03-29 16:34:48 +080021static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
22 struct snd_pcm_hw_params *params)
23{
24 struct snd_soc_pcm_runtime *rtd = substream->private_data;
25 unsigned int rate = params_rate(params);
26 unsigned int mclk_fs_ratio = 128;
27 unsigned int mclk_fs = rate * mclk_fs_ratio;
28
29 return snd_soc_dai_set_sysclk(rtd->cpu_dai,
30 0, mclk_fs, SND_SOC_CLOCK_OUT);
31}
32
33static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
34 .hw_params = mt8183_mt6358_i2s_hw_params,
35};
36
37static int mt8183_da7219_i2s_hw_params(struct snd_pcm_substream *substream,
38 struct snd_pcm_hw_params *params)
39{
40 struct snd_soc_pcm_runtime *rtd = substream->private_data;
41 unsigned int rate = params_rate(params);
42 unsigned int mclk_fs_ratio = 256;
43 unsigned int mclk_fs = rate * mclk_fs_ratio;
44 unsigned int freq;
45 int ret = 0, j;
46
47 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, 0,
48 mclk_fs, SND_SOC_CLOCK_OUT);
49 if (ret < 0)
50 dev_err(rtd->dev, "failed to set cpu dai sysclk\n");
51
52 for (j = 0; j < rtd->num_codecs; j++) {
53 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
54
55 if (!strcmp(codec_dai->component->name, "da7219.5-001a")) {
56 ret = snd_soc_dai_set_sysclk(codec_dai,
57 DA7219_CLKSRC_MCLK,
58 mclk_fs,
59 SND_SOC_CLOCK_IN);
60 if (ret < 0)
61 dev_err(rtd->dev, "failed to set sysclk\n");
62
63 if ((rate % 8000) == 0)
64 freq = DA7219_PLL_FREQ_OUT_98304;
65 else
66 freq = DA7219_PLL_FREQ_OUT_90316;
67
68 ret = snd_soc_dai_set_pll(codec_dai, 0,
69 DA7219_SYSCLK_PLL_SRM,
70 0, freq);
71 if (ret)
72 dev_err(rtd->dev, "failed to start PLL: %d\n",
73 ret);
74 }
75 }
76
77 return ret;
78}
79
80static int mt8183_da7219_hw_free(struct snd_pcm_substream *substream)
81{
82 struct snd_soc_pcm_runtime *rtd = substream->private_data;
83 int ret = 0, j;
84
85 for (j = 0; j < rtd->num_codecs; j++) {
86 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
87
88 if (!strcmp(codec_dai->component->name, "da7219.5-001a")) {
89 ret = snd_soc_dai_set_pll(codec_dai,
90 0, DA7219_SYSCLK_MCLK, 0, 0);
91 if (ret < 0) {
92 dev_err(rtd->dev, "failed to stop PLL: %d\n",
93 ret);
94 break;
95 }
96 }
97 }
98
99 return ret;
100}
101
102static const struct snd_soc_ops mt8183_da7219_i2s_ops = {
103 .hw_params = mt8183_da7219_i2s_hw_params,
104 .hw_free = mt8183_da7219_hw_free,
105};
106
107static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
108 struct snd_pcm_hw_params *params)
109{
110 /* fix BE i2s format to 32bit, clean param mask first */
111 snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
112 0, SNDRV_PCM_FORMAT_LAST);
113
114 params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
115
116 return 0;
117}
118
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900119/* FE */
120SND_SOC_DAILINK_DEFS(playback1,
121 DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
122 DAILINK_COMP_ARRAY(COMP_DUMMY()),
123 DAILINK_COMP_ARRAY(COMP_EMPTY()));
124
125SND_SOC_DAILINK_DEFS(playback2,
126 DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
127 DAILINK_COMP_ARRAY(COMP_DUMMY()),
128 DAILINK_COMP_ARRAY(COMP_EMPTY()));
129
130SND_SOC_DAILINK_DEFS(playback3,
131 DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
132 DAILINK_COMP_ARRAY(COMP_DUMMY()),
133 DAILINK_COMP_ARRAY(COMP_EMPTY()));
134
135SND_SOC_DAILINK_DEFS(capture1,
136 DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
137 DAILINK_COMP_ARRAY(COMP_DUMMY()),
138 DAILINK_COMP_ARRAY(COMP_EMPTY()));
139
140SND_SOC_DAILINK_DEFS(capture2,
141 DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
142 DAILINK_COMP_ARRAY(COMP_DUMMY()),
143 DAILINK_COMP_ARRAY(COMP_EMPTY()));
144
145SND_SOC_DAILINK_DEFS(capture3,
146 DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
147 DAILINK_COMP_ARRAY(COMP_DUMMY()),
148 DAILINK_COMP_ARRAY(COMP_EMPTY()));
149
150SND_SOC_DAILINK_DEFS(capture_mono,
151 DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
152 DAILINK_COMP_ARRAY(COMP_DUMMY()),
153 DAILINK_COMP_ARRAY(COMP_EMPTY()));
154
155SND_SOC_DAILINK_DEFS(playback_hdmi,
156 DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
157 DAILINK_COMP_ARRAY(COMP_DUMMY()),
158 DAILINK_COMP_ARRAY(COMP_EMPTY()));
159
160/* BE */
161SND_SOC_DAILINK_DEFS(primary_codec,
162 DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
163 DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
164 DAILINK_COMP_ARRAY(COMP_EMPTY()));
165
166SND_SOC_DAILINK_DEFS(pcm1,
167 DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
168 DAILINK_COMP_ARRAY(COMP_DUMMY()),
169 DAILINK_COMP_ARRAY(COMP_EMPTY()));
170
171SND_SOC_DAILINK_DEFS(pcm2,
172 DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
173 DAILINK_COMP_ARRAY(COMP_DUMMY()),
174 DAILINK_COMP_ARRAY(COMP_EMPTY()));
175
176SND_SOC_DAILINK_DEFS(i2s0,
177 DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
178 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
179 DAILINK_COMP_ARRAY(COMP_EMPTY()));
180
181SND_SOC_DAILINK_DEFS(i2s1,
182 DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
183 DAILINK_COMP_ARRAY(COMP_DUMMY()),
184 DAILINK_COMP_ARRAY(COMP_EMPTY()));
185
186SND_SOC_DAILINK_DEFS(i2s2,
187 DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
188 DAILINK_COMP_ARRAY(COMP_CODEC("da7219.5-001a", "da7219-hifi")),
189 DAILINK_COMP_ARRAY(COMP_EMPTY()));
190
191SND_SOC_DAILINK_DEFS(i2s3,
192 DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
193 DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi"),
194 COMP_CODEC("da7219.5-001a", "da7219-hifi")),
195 DAILINK_COMP_ARRAY(COMP_EMPTY()));
196
197SND_SOC_DAILINK_DEFS(i2s5,
198 DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
199 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
200 DAILINK_COMP_ARRAY(COMP_EMPTY()));
201
202SND_SOC_DAILINK_DEFS(tdm,
203 DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
204 DAILINK_COMP_ARRAY(COMP_DUMMY()),
205 DAILINK_COMP_ARRAY(COMP_EMPTY()));
206
Shunli Wangebbddc72019-03-29 16:34:48 +0800207static struct snd_soc_dai_link mt8183_da7219_max98357_dai_links[] = {
208 /* FE */
209 {
210 .name = "Playback_1",
211 .stream_name = "Playback_1",
Shunli Wangebbddc72019-03-29 16:34:48 +0800212 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
213 SND_SOC_DPCM_TRIGGER_PRE},
214 .dynamic = 1,
215 .dpcm_playback = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900216 SND_SOC_DAILINK_REG(playback1),
Shunli Wangebbddc72019-03-29 16:34:48 +0800217 },
218 {
219 .name = "Playback_2",
220 .stream_name = "Playback_2",
Shunli Wangebbddc72019-03-29 16:34:48 +0800221 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
222 SND_SOC_DPCM_TRIGGER_PRE},
223 .dynamic = 1,
224 .dpcm_playback = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900225 SND_SOC_DAILINK_REG(playback2),
Shunli Wangebbddc72019-03-29 16:34:48 +0800226 },
227 {
228 .name = "Playback_3",
229 .stream_name = "Playback_3",
Shunli Wangebbddc72019-03-29 16:34:48 +0800230 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
231 SND_SOC_DPCM_TRIGGER_PRE},
232 .dynamic = 1,
233 .dpcm_playback = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900234 SND_SOC_DAILINK_REG(playback3),
Shunli Wangebbddc72019-03-29 16:34:48 +0800235 },
236 {
237 .name = "Capture_1",
238 .stream_name = "Capture_1",
Shunli Wangebbddc72019-03-29 16:34:48 +0800239 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
240 SND_SOC_DPCM_TRIGGER_PRE},
241 .dynamic = 1,
242 .dpcm_capture = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900243 SND_SOC_DAILINK_REG(capture1),
Shunli Wangebbddc72019-03-29 16:34:48 +0800244 },
245 {
246 .name = "Capture_2",
247 .stream_name = "Capture_2",
Shunli Wangebbddc72019-03-29 16:34:48 +0800248 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
249 SND_SOC_DPCM_TRIGGER_PRE},
250 .dynamic = 1,
251 .dpcm_capture = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900252 SND_SOC_DAILINK_REG(capture2),
Shunli Wangebbddc72019-03-29 16:34:48 +0800253 },
254 {
255 .name = "Capture_3",
256 .stream_name = "Capture_3",
Shunli Wangebbddc72019-03-29 16:34:48 +0800257 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
258 SND_SOC_DPCM_TRIGGER_PRE},
259 .dynamic = 1,
260 .dpcm_capture = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900261 SND_SOC_DAILINK_REG(capture3),
Shunli Wangebbddc72019-03-29 16:34:48 +0800262 },
263 {
264 .name = "Capture_Mono_1",
265 .stream_name = "Capture_Mono_1",
Shunli Wangebbddc72019-03-29 16:34:48 +0800266 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
267 SND_SOC_DPCM_TRIGGER_PRE},
268 .dynamic = 1,
269 .dpcm_capture = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900270 SND_SOC_DAILINK_REG(capture_mono),
Shunli Wangebbddc72019-03-29 16:34:48 +0800271 },
272 {
273 .name = "Playback_HDMI",
274 .stream_name = "Playback_HDMI",
Shunli Wangebbddc72019-03-29 16:34:48 +0800275 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
276 SND_SOC_DPCM_TRIGGER_PRE},
277 .dynamic = 1,
278 .dpcm_playback = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900279 SND_SOC_DAILINK_REG(playback_hdmi),
Shunli Wangebbddc72019-03-29 16:34:48 +0800280 },
281 /* BE */
282 {
283 .name = "Primary Codec",
Shunli Wangebbddc72019-03-29 16:34:48 +0800284 .no_pcm = 1,
285 .dpcm_playback = 1,
286 .dpcm_capture = 1,
287 .ignore_suspend = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900288 SND_SOC_DAILINK_REG(primary_codec),
Shunli Wangebbddc72019-03-29 16:34:48 +0800289 },
290 {
291 .name = "PCM 1",
Shunli Wangebbddc72019-03-29 16:34:48 +0800292 .no_pcm = 1,
293 .dpcm_playback = 1,
294 .dpcm_capture = 1,
295 .ignore_suspend = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900296 SND_SOC_DAILINK_REG(pcm1),
Shunli Wangebbddc72019-03-29 16:34:48 +0800297 },
298 {
299 .name = "PCM 2",
Shunli Wangebbddc72019-03-29 16:34:48 +0800300 .no_pcm = 1,
301 .dpcm_playback = 1,
302 .dpcm_capture = 1,
303 .ignore_suspend = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900304 SND_SOC_DAILINK_REG(pcm2),
Shunli Wangebbddc72019-03-29 16:34:48 +0800305 },
306 {
307 .name = "I2S0",
Shunli Wangebbddc72019-03-29 16:34:48 +0800308 .no_pcm = 1,
309 .dpcm_capture = 1,
310 .ignore_suspend = 1,
311 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
312 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900313 SND_SOC_DAILINK_REG(i2s0),
Shunli Wangebbddc72019-03-29 16:34:48 +0800314 },
315 {
316 .name = "I2S1",
Shunli Wangebbddc72019-03-29 16:34:48 +0800317 .no_pcm = 1,
318 .dpcm_playback = 1,
319 .ignore_suspend = 1,
320 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
321 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900322 SND_SOC_DAILINK_REG(i2s1),
Shunli Wangebbddc72019-03-29 16:34:48 +0800323 },
324 {
325 .name = "I2S2",
Shunli Wangebbddc72019-03-29 16:34:48 +0800326 .no_pcm = 1,
327 .dpcm_capture = 1,
328 .ignore_suspend = 1,
329 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
330 .ops = &mt8183_da7219_i2s_ops,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900331 SND_SOC_DAILINK_REG(i2s2),
Shunli Wangebbddc72019-03-29 16:34:48 +0800332 },
333 {
334 .name = "I2S3",
Shunli Wangebbddc72019-03-29 16:34:48 +0800335 .no_pcm = 1,
336 .dpcm_playback = 1,
337 .ignore_suspend = 1,
338 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
339 .ops = &mt8183_da7219_i2s_ops,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900340 SND_SOC_DAILINK_REG(i2s3),
Shunli Wangebbddc72019-03-29 16:34:48 +0800341 },
342 {
343 .name = "I2S5",
Shunli Wangebbddc72019-03-29 16:34:48 +0800344 .no_pcm = 1,
345 .dpcm_playback = 1,
346 .ignore_suspend = 1,
347 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
348 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900349 SND_SOC_DAILINK_REG(i2s5),
Shunli Wangebbddc72019-03-29 16:34:48 +0800350 },
351 {
352 .name = "TDM",
Shunli Wangebbddc72019-03-29 16:34:48 +0800353 .no_pcm = 1,
354 .dpcm_playback = 1,
355 .ignore_suspend = 1,
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900356 SND_SOC_DAILINK_REG(tdm),
Shunli Wangebbddc72019-03-29 16:34:48 +0800357 },
358};
359
360static int
361mt8183_da7219_max98357_headset_init(struct snd_soc_component *component);
362
363static struct snd_soc_aux_dev mt8183_da7219_max98357_headset_dev = {
Kuninori Morimoto5c9e38c2019-08-08 14:53:45 +0900364 .dlc = COMP_EMPTY(),
Shunli Wangebbddc72019-03-29 16:34:48 +0800365 .init = mt8183_da7219_max98357_headset_init,
366};
367
368static struct snd_soc_codec_conf mt6358_codec_conf[] = {
369 {
370 .dev_name = "mt6358-sound",
371 .name_prefix = "Mt6358",
372 },
373};
374
375static struct snd_soc_card mt8183_da7219_max98357_card = {
376 .name = "mt8183_da7219_max98357",
377 .owner = THIS_MODULE,
378 .dai_link = mt8183_da7219_max98357_dai_links,
379 .num_links = ARRAY_SIZE(mt8183_da7219_max98357_dai_links),
380 .aux_dev = &mt8183_da7219_max98357_headset_dev,
381 .num_aux_devs = 1,
382 .codec_conf = mt6358_codec_conf,
383 .num_configs = ARRAY_SIZE(mt6358_codec_conf),
384};
385
386static int
387mt8183_da7219_max98357_headset_init(struct snd_soc_component *component)
388{
389 int ret;
390
391 /* Enable Headset and 4 Buttons Jack detection */
392 ret = snd_soc_card_jack_new(&mt8183_da7219_max98357_card,
393 "Headset Jack",
394 SND_JACK_HEADSET |
395 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
396 SND_JACK_BTN_2 | SND_JACK_BTN_3,
397 &headset_jack,
Tzung-Bi Shih108d0d42019-05-22 22:55:05 +0800398 NULL, 0);
Shunli Wangebbddc72019-03-29 16:34:48 +0800399 if (ret)
400 return ret;
401
402 da7219_aad_jack_det(component, &headset_jack);
403
404 return ret;
405}
406
407static int mt8183_da7219_max98357_dev_probe(struct platform_device *pdev)
408{
409 struct snd_soc_card *card = &mt8183_da7219_max98357_card;
410 struct device_node *platform_node;
411 struct snd_soc_dai_link *dai_link;
412 struct pinctrl *default_pins;
413 int ret, i;
414
415 card->dev = &pdev->dev;
416
417 platform_node = of_parse_phandle(pdev->dev.of_node,
418 "mediatek,platform", 0);
419 if (!platform_node) {
420 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
421 return -EINVAL;
422 }
423
424 for_each_card_prelinks(card, i, dai_link) {
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900425 if (dai_link->platforms->name)
Shunli Wangebbddc72019-03-29 16:34:48 +0800426 continue;
Kuninori Morimoto4b990642019-06-06 13:08:43 +0900427 dai_link->platforms->of_node = platform_node;
Shunli Wangebbddc72019-03-29 16:34:48 +0800428 }
429
Kuninori Morimoto5c9e38c2019-08-08 14:53:45 +0900430 mt8183_da7219_max98357_headset_dev.dlc.of_node =
Shunli Wangebbddc72019-03-29 16:34:48 +0800431 of_parse_phandle(pdev->dev.of_node,
432 "mediatek,headset-codec", 0);
Kuninori Morimoto5c9e38c2019-08-08 14:53:45 +0900433 if (!mt8183_da7219_max98357_headset_dev.dlc.of_node) {
Shunli Wangebbddc72019-03-29 16:34:48 +0800434 dev_err(&pdev->dev,
435 "Property 'mediatek,headset-codec' missing/invalid\n");
436 return -EINVAL;
437 }
438
439 ret = devm_snd_soc_register_card(&pdev->dev, card);
440 if (ret) {
441 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
442 __func__, ret);
443 return ret;
444 }
445
446 default_pins =
447 devm_pinctrl_get_select(&pdev->dev, PINCTRL_STATE_DEFAULT);
448 if (IS_ERR(default_pins)) {
449 dev_err(&pdev->dev, "%s set pins failed\n",
450 __func__);
451 return PTR_ERR(default_pins);
452 }
453
454 return ret;
455}
456
457#ifdef CONFIG_OF
458static const struct of_device_id mt8183_da7219_max98357_dt_match[] = {
459 {.compatible = "mediatek,mt8183_da7219_max98357",},
460 {}
461};
462#endif
463
464static struct platform_driver mt8183_da7219_max98357_driver = {
465 .driver = {
466 .name = "mt8183_da7219_max98357",
Shunli Wangebbddc72019-03-29 16:34:48 +0800467#ifdef CONFIG_OF
468 .of_match_table = mt8183_da7219_max98357_dt_match,
469#endif
470 },
471 .probe = mt8183_da7219_max98357_dev_probe,
472};
473
474module_platform_driver(mt8183_da7219_max98357_driver);
475
476/* Module information */
477MODULE_DESCRIPTION("MT8183-DA7219-MAX98357 ALSA SoC machine driver");
478MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
479MODULE_LICENSE("GPL v2");
480MODULE_ALIAS("mt8183_da7219_max98357 soc card");
481