blob: 887c932229d0e4550c086d498a090ac136de2f9d [file] [log] [blame]
Shunli Wang11c02692019-03-29 16:34:46 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// mt8183-mt6358.c --
4// MT8183-MT6358-TS3A227-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/ts3a227e.h"
17
18static struct snd_soc_jack headset_jack;
19
Shunli Wang11c02692019-03-29 16:34:46 +080020static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
21 struct snd_pcm_hw_params *params)
22{
23 struct snd_soc_pcm_runtime *rtd = substream->private_data;
24 unsigned int rate = params_rate(params);
25 unsigned int mclk_fs_ratio = 128;
26 unsigned int mclk_fs = rate * mclk_fs_ratio;
27
28 return snd_soc_dai_set_sysclk(rtd->cpu_dai,
29 0, mclk_fs, SND_SOC_CLOCK_OUT);
30}
31
32static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
33 .hw_params = mt8183_mt6358_i2s_hw_params,
34};
35
36static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
37 struct snd_pcm_hw_params *params)
38{
39 dev_dbg(rtd->dev, "%s(), fix format to 32bit\n", __func__);
40
41 /* fix BE i2s format to 32bit, clean param mask first */
42 snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
43 0, SNDRV_PCM_FORMAT_LAST);
44
45 params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
46 return 0;
47}
48
49static const struct snd_soc_dapm_widget
50mt8183_mt6358_ts3a227_max98357_dapm_widgets[] = {
51 SND_SOC_DAPM_OUTPUT("IT6505_8CH"),
52};
53
54static const struct snd_soc_dapm_route
55mt8183_mt6358_ts3a227_max98357_dapm_routes[] = {
56 {"IT6505_8CH", NULL, "TDM"},
57};
58
Tzung-Bi Shih1df1e542019-04-08 18:47:28 +080059static int
60mt8183_mt6358_ts3a227_max98357_bt_sco_startup(
61 struct snd_pcm_substream *substream)
62{
63 static const unsigned int rates[] = {
64 8000, 16000
65 };
66 static const struct snd_pcm_hw_constraint_list constraints_rates = {
67 .count = ARRAY_SIZE(rates),
68 .list = rates,
69 .mask = 0,
70 };
71 static const unsigned int channels[] = {
72 1,
73 };
74 static const struct snd_pcm_hw_constraint_list constraints_channels = {
75 .count = ARRAY_SIZE(channels),
76 .list = channels,
77 .mask = 0,
78 };
79
80 struct snd_pcm_runtime *runtime = substream->runtime;
81
82 snd_pcm_hw_constraint_list(runtime, 0,
83 SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
84 runtime->hw.channels_max = 1;
85 snd_pcm_hw_constraint_list(runtime, 0,
86 SNDRV_PCM_HW_PARAM_CHANNELS,
87 &constraints_channels);
88
89 runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
90 snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
91
92 return 0;
93}
94
95static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_bt_sco_ops = {
96 .startup = mt8183_mt6358_ts3a227_max98357_bt_sco_startup,
97};
98
Kuninori Morimotofa284fd2019-06-06 13:08:49 +090099/* FE */
100SND_SOC_DAILINK_DEFS(playback1,
101 DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
102 DAILINK_COMP_ARRAY(COMP_DUMMY()),
103 DAILINK_COMP_ARRAY(COMP_EMPTY()));
104
105SND_SOC_DAILINK_DEFS(playback2,
106 DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
107 DAILINK_COMP_ARRAY(COMP_DUMMY()),
108 DAILINK_COMP_ARRAY(COMP_EMPTY()));
109
110SND_SOC_DAILINK_DEFS(playback3,
111 DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
112 DAILINK_COMP_ARRAY(COMP_DUMMY()),
113 DAILINK_COMP_ARRAY(COMP_EMPTY()));
114
115SND_SOC_DAILINK_DEFS(capture1,
116 DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
117 DAILINK_COMP_ARRAY(COMP_DUMMY()),
118 DAILINK_COMP_ARRAY(COMP_EMPTY()));
119
120SND_SOC_DAILINK_DEFS(capture2,
121 DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
122 DAILINK_COMP_ARRAY(COMP_DUMMY()),
123 DAILINK_COMP_ARRAY(COMP_EMPTY()));
124
125SND_SOC_DAILINK_DEFS(capture3,
126 DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
127 DAILINK_COMP_ARRAY(COMP_DUMMY()),
128 DAILINK_COMP_ARRAY(COMP_EMPTY()));
129
130SND_SOC_DAILINK_DEFS(capture_mono,
131 DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
132 DAILINK_COMP_ARRAY(COMP_DUMMY()),
133 DAILINK_COMP_ARRAY(COMP_EMPTY()));
134
135SND_SOC_DAILINK_DEFS(playback_hdmi,
136 DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
137 DAILINK_COMP_ARRAY(COMP_DUMMY()),
138 DAILINK_COMP_ARRAY(COMP_EMPTY()));
139
140/* BE */
141SND_SOC_DAILINK_DEFS(primary_codec,
142 DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
143 DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
144 DAILINK_COMP_ARRAY(COMP_EMPTY()));
145
146SND_SOC_DAILINK_DEFS(pcm1,
147 DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
148 DAILINK_COMP_ARRAY(COMP_DUMMY()),
149 DAILINK_COMP_ARRAY(COMP_EMPTY()));
150
151SND_SOC_DAILINK_DEFS(pcm2,
152 DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
153 DAILINK_COMP_ARRAY(COMP_DUMMY()),
154 DAILINK_COMP_ARRAY(COMP_EMPTY()));
155
156SND_SOC_DAILINK_DEFS(i2s0,
157 DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
158 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
159 DAILINK_COMP_ARRAY(COMP_EMPTY()));
160
161SND_SOC_DAILINK_DEFS(i2s1,
162 DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
163 DAILINK_COMP_ARRAY(COMP_DUMMY()),
164 DAILINK_COMP_ARRAY(COMP_EMPTY()));
165
166SND_SOC_DAILINK_DEFS(i2s2,
167 DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
168 DAILINK_COMP_ARRAY(COMP_DUMMY()),
169 DAILINK_COMP_ARRAY(COMP_EMPTY()));
170
171SND_SOC_DAILINK_DEFS(i2s3,
172 DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
173 DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi")),
174 DAILINK_COMP_ARRAY(COMP_EMPTY()));
175
176SND_SOC_DAILINK_DEFS(i2s5,
177 DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
178 DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm")),
179 DAILINK_COMP_ARRAY(COMP_EMPTY()));
180
181SND_SOC_DAILINK_DEFS(tdm,
182 DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
183 DAILINK_COMP_ARRAY(COMP_DUMMY()),
184 DAILINK_COMP_ARRAY(COMP_EMPTY()));
185
Shunli Wang11c02692019-03-29 16:34:46 +0800186static struct snd_soc_dai_link
187mt8183_mt6358_ts3a227_max98357_dai_links[] = {
188 /* FE */
189 {
190 .name = "Playback_1",
191 .stream_name = "Playback_1",
Shunli Wang11c02692019-03-29 16:34:46 +0800192 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
193 SND_SOC_DPCM_TRIGGER_PRE},
194 .dynamic = 1,
195 .dpcm_playback = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900196 SND_SOC_DAILINK_REG(playback1),
Shunli Wang11c02692019-03-29 16:34:46 +0800197 },
198 {
199 .name = "Playback_2",
200 .stream_name = "Playback_2",
Shunli Wang11c02692019-03-29 16:34:46 +0800201 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
202 SND_SOC_DPCM_TRIGGER_PRE},
203 .dynamic = 1,
204 .dpcm_playback = 1,
Tzung-Bi Shih1df1e542019-04-08 18:47:28 +0800205 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900206 SND_SOC_DAILINK_REG(playback2),
Shunli Wang11c02692019-03-29 16:34:46 +0800207 },
208 {
209 .name = "Playback_3",
210 .stream_name = "Playback_3",
Shunli Wang11c02692019-03-29 16:34:46 +0800211 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
212 SND_SOC_DPCM_TRIGGER_PRE},
213 .dynamic = 1,
214 .dpcm_playback = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900215 SND_SOC_DAILINK_REG(playback3),
Shunli Wang11c02692019-03-29 16:34:46 +0800216 },
217 {
218 .name = "Capture_1",
219 .stream_name = "Capture_1",
Shunli Wang11c02692019-03-29 16:34:46 +0800220 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
221 SND_SOC_DPCM_TRIGGER_PRE},
222 .dynamic = 1,
223 .dpcm_capture = 1,
Tzung-Bi Shih1df1e542019-04-08 18:47:28 +0800224 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900225 SND_SOC_DAILINK_REG(capture1),
Shunli Wang11c02692019-03-29 16:34:46 +0800226 },
227 {
228 .name = "Capture_2",
229 .stream_name = "Capture_2",
Shunli Wang11c02692019-03-29 16:34:46 +0800230 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
231 SND_SOC_DPCM_TRIGGER_PRE},
232 .dynamic = 1,
233 .dpcm_capture = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900234 SND_SOC_DAILINK_REG(capture2),
Shunli Wang11c02692019-03-29 16:34:46 +0800235 },
236 {
237 .name = "Capture_3",
238 .stream_name = "Capture_3",
Shunli Wang11c02692019-03-29 16:34:46 +0800239 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
240 SND_SOC_DPCM_TRIGGER_PRE},
241 .dynamic = 1,
242 .dpcm_capture = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900243 SND_SOC_DAILINK_REG(capture3),
Shunli Wang11c02692019-03-29 16:34:46 +0800244 },
245 {
246 .name = "Capture_Mono_1",
247 .stream_name = "Capture_Mono_1",
Shunli Wang11c02692019-03-29 16:34:46 +0800248 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
249 SND_SOC_DPCM_TRIGGER_PRE},
250 .dynamic = 1,
251 .dpcm_capture = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900252 SND_SOC_DAILINK_REG(capture_mono),
Shunli Wang11c02692019-03-29 16:34:46 +0800253 },
254 {
255 .name = "Playback_HDMI",
256 .stream_name = "Playback_HDMI",
Shunli Wang11c02692019-03-29 16:34:46 +0800257 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
258 SND_SOC_DPCM_TRIGGER_PRE},
259 .dynamic = 1,
260 .dpcm_playback = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900261 SND_SOC_DAILINK_REG(playback_hdmi),
Shunli Wang11c02692019-03-29 16:34:46 +0800262 },
263 /* BE */
264 {
265 .name = "Primary Codec",
Shunli Wang11c02692019-03-29 16:34:46 +0800266 .no_pcm = 1,
267 .dpcm_playback = 1,
268 .dpcm_capture = 1,
269 .ignore_suspend = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900270 SND_SOC_DAILINK_REG(primary_codec),
Shunli Wang11c02692019-03-29 16:34:46 +0800271 },
272 {
273 .name = "PCM 1",
Shunli Wang11c02692019-03-29 16:34:46 +0800274 .no_pcm = 1,
275 .dpcm_playback = 1,
276 .dpcm_capture = 1,
277 .ignore_suspend = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900278 SND_SOC_DAILINK_REG(pcm1),
Shunli Wang11c02692019-03-29 16:34:46 +0800279 },
280 {
281 .name = "PCM 2",
Shunli Wang11c02692019-03-29 16:34:46 +0800282 .no_pcm = 1,
283 .dpcm_playback = 1,
284 .dpcm_capture = 1,
285 .ignore_suspend = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900286 SND_SOC_DAILINK_REG(pcm2),
Shunli Wang11c02692019-03-29 16:34:46 +0800287 },
288 {
289 .name = "I2S0",
Shunli Wang11c02692019-03-29 16:34:46 +0800290 .no_pcm = 1,
291 .dpcm_capture = 1,
292 .ignore_suspend = 1,
293 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
294 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900295 SND_SOC_DAILINK_REG(i2s0),
Shunli Wang11c02692019-03-29 16:34:46 +0800296 },
297 {
298 .name = "I2S1",
Shunli Wang11c02692019-03-29 16:34:46 +0800299 .no_pcm = 1,
300 .dpcm_playback = 1,
301 .ignore_suspend = 1,
302 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
303 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900304 SND_SOC_DAILINK_REG(i2s1),
Shunli Wang11c02692019-03-29 16:34:46 +0800305 },
306 {
307 .name = "I2S2",
Shunli Wang11c02692019-03-29 16:34:46 +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 Morimotofa284fd2019-06-06 13:08:49 +0900313 SND_SOC_DAILINK_REG(i2s2),
Shunli Wang11c02692019-03-29 16:34:46 +0800314 },
315 {
316 .name = "I2S3",
Shunli Wang11c02692019-03-29 16:34:46 +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 Morimotofa284fd2019-06-06 13:08:49 +0900322 SND_SOC_DAILINK_REG(i2s3),
Shunli Wang11c02692019-03-29 16:34:46 +0800323 },
324 {
325 .name = "I2S5",
Shunli Wang11c02692019-03-29 16:34:46 +0800326 .no_pcm = 1,
327 .dpcm_playback = 1,
328 .ignore_suspend = 1,
329 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
330 .ops = &mt8183_mt6358_i2s_ops,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900331 SND_SOC_DAILINK_REG(i2s5),
Shunli Wang11c02692019-03-29 16:34:46 +0800332 },
333 {
334 .name = "TDM",
Shunli Wang11c02692019-03-29 16:34:46 +0800335 .no_pcm = 1,
336 .dpcm_playback = 1,
337 .ignore_suspend = 1,
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900338 SND_SOC_DAILINK_REG(tdm),
Shunli Wang11c02692019-03-29 16:34:46 +0800339 },
340};
341
342static int
343mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *cpnt);
344
345static struct snd_soc_aux_dev mt8183_mt6358_ts3a227_max98357_headset_dev = {
346 .name = "Headset Chip",
347 .init = mt8183_mt6358_ts3a227_max98357_headset_init,
348};
349
350static struct snd_soc_card mt8183_mt6358_ts3a227_max98357_card = {
351 .name = "mt8183_mt6358_ts3a227_max98357",
352 .owner = THIS_MODULE,
353 .dai_link = mt8183_mt6358_ts3a227_max98357_dai_links,
354 .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_max98357_dai_links),
355 .aux_dev = &mt8183_mt6358_ts3a227_max98357_headset_dev,
356 .num_aux_devs = 1,
357};
358
359static int
360mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *component)
361{
362 int ret;
363
364 /* Enable Headset and 4 Buttons Jack detection */
365 ret = snd_soc_card_jack_new(&mt8183_mt6358_ts3a227_max98357_card,
366 "Headset Jack",
367 SND_JACK_HEADSET |
368 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
369 SND_JACK_BTN_2 | SND_JACK_BTN_3,
370 &headset_jack,
Tzung-Bi Shih108d0d42019-05-22 22:55:05 +0800371 NULL, 0);
Shunli Wang11c02692019-03-29 16:34:46 +0800372 if (ret)
373 return ret;
374
375 ret = ts3a227e_enable_jack_detect(component, &headset_jack);
376
377 return ret;
378}
379
380static int
381mt8183_mt6358_ts3a227_max98357_dev_probe(struct platform_device *pdev)
382{
383 struct snd_soc_card *card = &mt8183_mt6358_ts3a227_max98357_card;
384 struct device_node *platform_node;
385 struct snd_soc_dai_link *dai_link;
386 struct pinctrl *default_pins;
Tzung-Bi Shih63ba8e42019-05-22 22:54:02 +0800387 int i;
Shunli Wang11c02692019-03-29 16:34:46 +0800388
389 card->dev = &pdev->dev;
390
391 platform_node = of_parse_phandle(pdev->dev.of_node,
392 "mediatek,platform", 0);
393 if (!platform_node) {
394 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
395 return -EINVAL;
396 }
397
398 for_each_card_prelinks(card, i, dai_link) {
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900399 if (dai_link->platforms->name)
Shunli Wang11c02692019-03-29 16:34:46 +0800400 continue;
Kuninori Morimotofa284fd2019-06-06 13:08:49 +0900401 dai_link->platforms->of_node = platform_node;
Shunli Wang11c02692019-03-29 16:34:46 +0800402 }
403
404 mt8183_mt6358_ts3a227_max98357_headset_dev.codec_of_node =
405 of_parse_phandle(pdev->dev.of_node,
406 "mediatek,headset-codec", 0);
407 if (!mt8183_mt6358_ts3a227_max98357_headset_dev.codec_of_node) {
408 dev_err(&pdev->dev,
409 "Property 'mediatek,headset-codec' missing/invalid\n");
410 return -EINVAL;
411 }
412
Shunli Wang11c02692019-03-29 16:34:46 +0800413 default_pins =
414 devm_pinctrl_get_select(&pdev->dev, PINCTRL_STATE_DEFAULT);
415 if (IS_ERR(default_pins)) {
416 dev_err(&pdev->dev, "%s set pins failed\n",
417 __func__);
418 return PTR_ERR(default_pins);
419 }
420
Tzung-Bi Shih63ba8e42019-05-22 22:54:02 +0800421 return devm_snd_soc_register_card(&pdev->dev, card);
Shunli Wang11c02692019-03-29 16:34:46 +0800422}
423
424#ifdef CONFIG_OF
425static const struct of_device_id mt8183_mt6358_ts3a227_max98357_dt_match[] = {
426 {.compatible = "mediatek,mt8183_mt6358_ts3a227_max98357",},
427 {}
428};
429#endif
430
431static struct platform_driver mt8183_mt6358_ts3a227_max98357_driver = {
432 .driver = {
433 .name = "mt8183_mt6358_ts3a227_max98357",
Shunli Wang11c02692019-03-29 16:34:46 +0800434#ifdef CONFIG_OF
435 .of_match_table = mt8183_mt6358_ts3a227_max98357_dt_match,
436#endif
437 },
438 .probe = mt8183_mt6358_ts3a227_max98357_dev_probe,
439};
440
441module_platform_driver(mt8183_mt6358_ts3a227_max98357_driver);
442
443/* Module information */
444MODULE_DESCRIPTION("MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver");
445MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
446MODULE_LICENSE("GPL v2");
447MODULE_ALIAS("mt8183_mt6358_ts3a227_max98357 soc card");
448