GNU Linux-libre 5.19-rc6-gnu
[releases.git] / sound / soc / mediatek / mt8183 / mt8183-mt6358-ts3a227-max98357.c
1 // 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 <linux/of_device.h>
11 #include <linux/pinctrl/consumer.h>
12 #include <sound/jack.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15
16 #include "../../codecs/rt1015.h"
17 #include "../../codecs/ts3a227e.h"
18 #include "mt8183-afe-common.h"
19
20 #define RT1015_CODEC_DAI "rt1015-aif"
21 #define RT1015_DEV0_NAME "rt1015.6-0028"
22 #define RT1015_DEV1_NAME "rt1015.6-0029"
23
24 enum PINCTRL_PIN_STATE {
25         PIN_STATE_DEFAULT = 0,
26         PIN_TDM_OUT_ON,
27         PIN_TDM_OUT_OFF,
28         PIN_WOV,
29         PIN_STATE_MAX
30 };
31
32 static const char * const mt8183_pin_str[PIN_STATE_MAX] = {
33         "default", "aud_tdm_out_on", "aud_tdm_out_off", "wov",
34 };
35
36 struct mt8183_mt6358_ts3a227_max98357_priv {
37         struct pinctrl *pinctrl;
38         struct pinctrl_state *pin_states[PIN_STATE_MAX];
39         struct snd_soc_jack headset_jack, hdmi_jack;
40 };
41
42 static int mt8183_mt6358_i2s_hw_params(struct snd_pcm_substream *substream,
43                                        struct snd_pcm_hw_params *params)
44 {
45         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
46         unsigned int rate = params_rate(params);
47         unsigned int mclk_fs_ratio = 128;
48         unsigned int mclk_fs = rate * mclk_fs_ratio;
49
50         return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
51                                       0, mclk_fs, SND_SOC_CLOCK_OUT);
52 }
53
54 static const struct snd_soc_ops mt8183_mt6358_i2s_ops = {
55         .hw_params = mt8183_mt6358_i2s_hw_params,
56 };
57
58 static int
59 mt8183_mt6358_rt1015_i2s_hw_params(struct snd_pcm_substream *substream,
60                                    struct snd_pcm_hw_params *params)
61 {
62         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
63         unsigned int rate = params_rate(params);
64         unsigned int mclk_fs_ratio = 128;
65         unsigned int mclk_fs = rate * mclk_fs_ratio;
66         struct snd_soc_card *card = rtd->card;
67         struct snd_soc_dai *codec_dai;
68         int ret, i;
69
70         for_each_rtd_codec_dais(rtd, i, codec_dai) {
71                 ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
72                                 rate * 64, rate * 256);
73                 if (ret < 0) {
74                         dev_err(card->dev, "failed to set pll\n");
75                         return ret;
76                 }
77
78                 ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
79                                 rate * 256, SND_SOC_CLOCK_IN);
80                 if (ret < 0) {
81                         dev_err(card->dev, "failed to set sysclk\n");
82                         return ret;
83                 }
84         }
85
86         return snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0),
87                                       0, mclk_fs, SND_SOC_CLOCK_OUT);
88 }
89
90 static const struct snd_soc_ops mt8183_mt6358_rt1015_i2s_ops = {
91         .hw_params = mt8183_mt6358_rt1015_i2s_hw_params,
92 };
93
94 static int mt8183_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
95                                       struct snd_pcm_hw_params *params)
96 {
97         dev_dbg(rtd->dev, "%s(), fix format to S32_LE\n", __func__);
98
99         /* fix BE i2s format to S32_LE, clean param mask first */
100         snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
101                              0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
102
103         params_set_format(params, SNDRV_PCM_FORMAT_S32_LE);
104         return 0;
105 }
106
107 static int mt8183_rt1015_i2s_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
108                                              struct snd_pcm_hw_params *params)
109 {
110         dev_dbg(rtd->dev, "%s(), fix format to S24_LE\n", __func__);
111
112         /* fix BE i2s format to S24_LE, clean param mask first */
113         snd_mask_reset_range(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT),
114                              0, (__force unsigned int)SNDRV_PCM_FORMAT_LAST);
115
116         params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
117         return 0;
118 }
119
120 static int
121 mt8183_mt6358_startup(struct snd_pcm_substream *substream)
122 {
123         static const unsigned int rates[] = {
124                 48000,
125         };
126         static const struct snd_pcm_hw_constraint_list constraints_rates = {
127                 .count = ARRAY_SIZE(rates),
128                 .list  = rates,
129                 .mask = 0,
130         };
131         static const unsigned int channels[] = {
132                 2,
133         };
134         static const struct snd_pcm_hw_constraint_list constraints_channels = {
135                 .count = ARRAY_SIZE(channels),
136                 .list = channels,
137                 .mask = 0,
138         };
139
140         struct snd_pcm_runtime *runtime = substream->runtime;
141
142         snd_pcm_hw_constraint_list(runtime, 0,
143                                    SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
144         runtime->hw.channels_max = 2;
145         snd_pcm_hw_constraint_list(runtime, 0,
146                                    SNDRV_PCM_HW_PARAM_CHANNELS,
147                                    &constraints_channels);
148
149         runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
150         snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
151
152         return 0;
153 }
154
155 static const struct snd_soc_ops mt8183_mt6358_ops = {
156         .startup = mt8183_mt6358_startup,
157 };
158
159 static int
160 mt8183_mt6358_ts3a227_max98357_bt_sco_startup(
161         struct snd_pcm_substream *substream)
162 {
163         static const unsigned int rates[] = {
164                 8000, 16000
165         };
166         static const struct snd_pcm_hw_constraint_list constraints_rates = {
167                 .count = ARRAY_SIZE(rates),
168                 .list  = rates,
169                 .mask = 0,
170         };
171         static const unsigned int channels[] = {
172                 1,
173         };
174         static const struct snd_pcm_hw_constraint_list constraints_channels = {
175                 .count = ARRAY_SIZE(channels),
176                 .list = channels,
177                 .mask = 0,
178         };
179
180         struct snd_pcm_runtime *runtime = substream->runtime;
181
182         snd_pcm_hw_constraint_list(runtime, 0,
183                         SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
184         runtime->hw.channels_max = 1;
185         snd_pcm_hw_constraint_list(runtime, 0,
186                         SNDRV_PCM_HW_PARAM_CHANNELS,
187                         &constraints_channels);
188
189         runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
190         snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
191
192         return 0;
193 }
194
195 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_bt_sco_ops = {
196         .startup = mt8183_mt6358_ts3a227_max98357_bt_sco_startup,
197 };
198
199 /* FE */
200 SND_SOC_DAILINK_DEFS(playback1,
201         DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
202         DAILINK_COMP_ARRAY(COMP_DUMMY()),
203         DAILINK_COMP_ARRAY(COMP_EMPTY()));
204
205 SND_SOC_DAILINK_DEFS(playback2,
206         DAILINK_COMP_ARRAY(COMP_CPU("DL2")),
207         DAILINK_COMP_ARRAY(COMP_DUMMY()),
208         DAILINK_COMP_ARRAY(COMP_EMPTY()));
209
210 SND_SOC_DAILINK_DEFS(playback3,
211         DAILINK_COMP_ARRAY(COMP_CPU("DL3")),
212         DAILINK_COMP_ARRAY(COMP_DUMMY()),
213         DAILINK_COMP_ARRAY(COMP_EMPTY()));
214
215 SND_SOC_DAILINK_DEFS(capture1,
216         DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
217         DAILINK_COMP_ARRAY(COMP_DUMMY()),
218         DAILINK_COMP_ARRAY(COMP_EMPTY()));
219
220 SND_SOC_DAILINK_DEFS(capture2,
221         DAILINK_COMP_ARRAY(COMP_CPU("UL2")),
222         DAILINK_COMP_ARRAY(COMP_DUMMY()),
223         DAILINK_COMP_ARRAY(COMP_EMPTY()));
224
225 SND_SOC_DAILINK_DEFS(capture3,
226         DAILINK_COMP_ARRAY(COMP_CPU("UL3")),
227         DAILINK_COMP_ARRAY(COMP_DUMMY()),
228         DAILINK_COMP_ARRAY(COMP_EMPTY()));
229
230 SND_SOC_DAILINK_DEFS(capture_mono,
231         DAILINK_COMP_ARRAY(COMP_CPU("UL_MONO_1")),
232         DAILINK_COMP_ARRAY(COMP_DUMMY()),
233         DAILINK_COMP_ARRAY(COMP_EMPTY()));
234
235 SND_SOC_DAILINK_DEFS(playback_hdmi,
236         DAILINK_COMP_ARRAY(COMP_CPU("HDMI")),
237         DAILINK_COMP_ARRAY(COMP_DUMMY()),
238         DAILINK_COMP_ARRAY(COMP_EMPTY()));
239
240 SND_SOC_DAILINK_DEFS(wake_on_voice,
241         DAILINK_COMP_ARRAY(COMP_DUMMY()),
242         DAILINK_COMP_ARRAY(COMP_DUMMY()),
243         DAILINK_COMP_ARRAY(COMP_EMPTY()));
244
245 /* BE */
246 SND_SOC_DAILINK_DEFS(primary_codec,
247         DAILINK_COMP_ARRAY(COMP_CPU("ADDA")),
248         DAILINK_COMP_ARRAY(COMP_CODEC("mt6358-sound", "mt6358-snd-codec-aif1")),
249         DAILINK_COMP_ARRAY(COMP_EMPTY()));
250
251 SND_SOC_DAILINK_DEFS(pcm1,
252         DAILINK_COMP_ARRAY(COMP_CPU("PCM 1")),
253         DAILINK_COMP_ARRAY(COMP_DUMMY()),
254         DAILINK_COMP_ARRAY(COMP_EMPTY()));
255
256 SND_SOC_DAILINK_DEFS(pcm2,
257         DAILINK_COMP_ARRAY(COMP_CPU("PCM 2")),
258         DAILINK_COMP_ARRAY(COMP_DUMMY()),
259         DAILINK_COMP_ARRAY(COMP_EMPTY()));
260
261 SND_SOC_DAILINK_DEFS(i2s0,
262         DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
263         DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm-wb")),
264         DAILINK_COMP_ARRAY(COMP_EMPTY()));
265
266 SND_SOC_DAILINK_DEFS(i2s1,
267         DAILINK_COMP_ARRAY(COMP_CPU("I2S1")),
268         DAILINK_COMP_ARRAY(COMP_DUMMY()),
269         DAILINK_COMP_ARRAY(COMP_EMPTY()));
270
271 SND_SOC_DAILINK_DEFS(i2s2,
272         DAILINK_COMP_ARRAY(COMP_CPU("I2S2")),
273         DAILINK_COMP_ARRAY(COMP_DUMMY()),
274         DAILINK_COMP_ARRAY(COMP_EMPTY()));
275
276 SND_SOC_DAILINK_DEFS(i2s3_max98357a,
277         DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
278         DAILINK_COMP_ARRAY(COMP_CODEC("max98357a", "HiFi")),
279         DAILINK_COMP_ARRAY(COMP_EMPTY()));
280
281 SND_SOC_DAILINK_DEFS(i2s3_rt1015,
282         DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
283         DAILINK_COMP_ARRAY(COMP_CODEC(RT1015_DEV0_NAME, RT1015_CODEC_DAI),
284                            COMP_CODEC(RT1015_DEV1_NAME, RT1015_CODEC_DAI)),
285         DAILINK_COMP_ARRAY(COMP_EMPTY()));
286
287 SND_SOC_DAILINK_DEFS(i2s3_rt1015p,
288         DAILINK_COMP_ARRAY(COMP_CPU("I2S3")),
289         DAILINK_COMP_ARRAY(COMP_CODEC("rt1015p", "HiFi")),
290         DAILINK_COMP_ARRAY(COMP_EMPTY()));
291
292 SND_SOC_DAILINK_DEFS(i2s5,
293         DAILINK_COMP_ARRAY(COMP_CPU("I2S5")),
294         DAILINK_COMP_ARRAY(COMP_CODEC("bt-sco", "bt-sco-pcm-wb")),
295         DAILINK_COMP_ARRAY(COMP_EMPTY()));
296
297 SND_SOC_DAILINK_DEFS(tdm,
298         DAILINK_COMP_ARRAY(COMP_CPU("TDM")),
299         DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
300         DAILINK_COMP_ARRAY(COMP_EMPTY()));
301
302 static int mt8183_mt6358_tdm_startup(struct snd_pcm_substream *substream)
303 {
304         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
305         struct mt8183_mt6358_ts3a227_max98357_priv *priv =
306                 snd_soc_card_get_drvdata(rtd->card);
307         int ret;
308
309         if (IS_ERR(priv->pin_states[PIN_TDM_OUT_ON]))
310                 return PTR_ERR(priv->pin_states[PIN_TDM_OUT_ON]);
311
312         ret = pinctrl_select_state(priv->pinctrl,
313                                    priv->pin_states[PIN_TDM_OUT_ON]);
314         if (ret)
315                 dev_err(rtd->card->dev, "%s failed to select state %d\n",
316                         __func__, ret);
317
318         return ret;
319 }
320
321 static void mt8183_mt6358_tdm_shutdown(struct snd_pcm_substream *substream)
322 {
323         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
324         struct mt8183_mt6358_ts3a227_max98357_priv *priv =
325                 snd_soc_card_get_drvdata(rtd->card);
326         int ret;
327
328         if (IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF]))
329                 return;
330
331         ret = pinctrl_select_state(priv->pinctrl,
332                                    priv->pin_states[PIN_TDM_OUT_OFF]);
333         if (ret)
334                 dev_err(rtd->card->dev, "%s failed to select state %d\n",
335                         __func__, ret);
336 }
337
338 static const struct snd_soc_ops mt8183_mt6358_tdm_ops = {
339         .startup = mt8183_mt6358_tdm_startup,
340         .shutdown = mt8183_mt6358_tdm_shutdown,
341 };
342
343 static int
344 mt8183_mt6358_ts3a227_max98357_wov_startup(
345         struct snd_pcm_substream *substream)
346 {
347         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
348         struct snd_soc_card *card = rtd->card;
349         struct mt8183_mt6358_ts3a227_max98357_priv *priv =
350                         snd_soc_card_get_drvdata(card);
351
352         return pinctrl_select_state(priv->pinctrl,
353                                     priv->pin_states[PIN_WOV]);
354 }
355
356 static void
357 mt8183_mt6358_ts3a227_max98357_wov_shutdown(
358         struct snd_pcm_substream *substream)
359 {
360         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
361         struct snd_soc_card *card = rtd->card;
362         struct mt8183_mt6358_ts3a227_max98357_priv *priv =
363                         snd_soc_card_get_drvdata(card);
364         int ret;
365
366         ret = pinctrl_select_state(priv->pinctrl,
367                                    priv->pin_states[PIN_STATE_DEFAULT]);
368         if (ret)
369                 dev_err(card->dev, "%s failed to select state %d\n",
370                         __func__, ret);
371 }
372
373 static const struct snd_soc_ops mt8183_mt6358_ts3a227_max98357_wov_ops = {
374         .startup = mt8183_mt6358_ts3a227_max98357_wov_startup,
375         .shutdown = mt8183_mt6358_ts3a227_max98357_wov_shutdown,
376 };
377
378 static int
379 mt8183_mt6358_ts3a227_max98357_hdmi_init(struct snd_soc_pcm_runtime *rtd)
380 {
381         struct mt8183_mt6358_ts3a227_max98357_priv *priv =
382                 snd_soc_card_get_drvdata(rtd->card);
383         int ret;
384
385         ret = snd_soc_card_jack_new(rtd->card, "HDMI Jack", SND_JACK_LINEOUT,
386                                     &priv->hdmi_jack);
387         if (ret)
388                 return ret;
389
390         return snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component,
391                                           &priv->hdmi_jack, NULL);
392 }
393
394 static struct snd_soc_dai_link mt8183_mt6358_ts3a227_dai_links[] = {
395         /* FE */
396         {
397                 .name = "Playback_1",
398                 .stream_name = "Playback_1",
399                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
400                             SND_SOC_DPCM_TRIGGER_PRE},
401                 .dynamic = 1,
402                 .dpcm_playback = 1,
403                 .ops = &mt8183_mt6358_ops,
404                 SND_SOC_DAILINK_REG(playback1),
405         },
406         {
407                 .name = "Playback_2",
408                 .stream_name = "Playback_2",
409                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
410                             SND_SOC_DPCM_TRIGGER_PRE},
411                 .dynamic = 1,
412                 .dpcm_playback = 1,
413                 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
414                 SND_SOC_DAILINK_REG(playback2),
415         },
416         {
417                 .name = "Playback_3",
418                 .stream_name = "Playback_3",
419                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
420                             SND_SOC_DPCM_TRIGGER_PRE},
421                 .dynamic = 1,
422                 .dpcm_playback = 1,
423                 SND_SOC_DAILINK_REG(playback3),
424         },
425         {
426                 .name = "Capture_1",
427                 .stream_name = "Capture_1",
428                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
429                             SND_SOC_DPCM_TRIGGER_PRE},
430                 .dynamic = 1,
431                 .dpcm_capture = 1,
432                 .ops = &mt8183_mt6358_ts3a227_max98357_bt_sco_ops,
433                 SND_SOC_DAILINK_REG(capture1),
434         },
435         {
436                 .name = "Capture_2",
437                 .stream_name = "Capture_2",
438                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
439                             SND_SOC_DPCM_TRIGGER_PRE},
440                 .dynamic = 1,
441                 .dpcm_capture = 1,
442                 SND_SOC_DAILINK_REG(capture2),
443         },
444         {
445                 .name = "Capture_3",
446                 .stream_name = "Capture_3",
447                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
448                             SND_SOC_DPCM_TRIGGER_PRE},
449                 .dynamic = 1,
450                 .dpcm_capture = 1,
451                 .ops = &mt8183_mt6358_ops,
452                 SND_SOC_DAILINK_REG(capture3),
453         },
454         {
455                 .name = "Capture_Mono_1",
456                 .stream_name = "Capture_Mono_1",
457                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
458                             SND_SOC_DPCM_TRIGGER_PRE},
459                 .dynamic = 1,
460                 .dpcm_capture = 1,
461                 SND_SOC_DAILINK_REG(capture_mono),
462         },
463         {
464                 .name = "Playback_HDMI",
465                 .stream_name = "Playback_HDMI",
466                 .trigger = {SND_SOC_DPCM_TRIGGER_PRE,
467                             SND_SOC_DPCM_TRIGGER_PRE},
468                 .dynamic = 1,
469                 .dpcm_playback = 1,
470                 SND_SOC_DAILINK_REG(playback_hdmi),
471         },
472         {
473                 .name = "Wake on Voice",
474                 .stream_name = "Wake on Voice",
475                 .ignore_suspend = 1,
476                 .ignore = 1,
477                 SND_SOC_DAILINK_REG(wake_on_voice),
478                 .ops = &mt8183_mt6358_ts3a227_max98357_wov_ops,
479         },
480
481         /* BE */
482         {
483                 .name = "Primary Codec",
484                 .no_pcm = 1,
485                 .dpcm_playback = 1,
486                 .dpcm_capture = 1,
487                 .ignore_suspend = 1,
488                 SND_SOC_DAILINK_REG(primary_codec),
489         },
490         {
491                 .name = "PCM 1",
492                 .no_pcm = 1,
493                 .dpcm_playback = 1,
494                 .dpcm_capture = 1,
495                 .ignore_suspend = 1,
496                 SND_SOC_DAILINK_REG(pcm1),
497         },
498         {
499                 .name = "PCM 2",
500                 .no_pcm = 1,
501                 .dpcm_playback = 1,
502                 .dpcm_capture = 1,
503                 .ignore_suspend = 1,
504                 SND_SOC_DAILINK_REG(pcm2),
505         },
506         {
507                 .name = "I2S0",
508                 .no_pcm = 1,
509                 .dpcm_capture = 1,
510                 .ignore_suspend = 1,
511                 .ops = &mt8183_mt6358_i2s_ops,
512                 SND_SOC_DAILINK_REG(i2s0),
513         },
514         {
515                 .name = "I2S1",
516                 .no_pcm = 1,
517                 .dpcm_playback = 1,
518                 .ignore_suspend = 1,
519                 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
520                 .ops = &mt8183_mt6358_i2s_ops,
521                 SND_SOC_DAILINK_REG(i2s1),
522         },
523         {
524                 .name = "I2S2",
525                 .no_pcm = 1,
526                 .dpcm_capture = 1,
527                 .ignore_suspend = 1,
528                 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
529                 .ops = &mt8183_mt6358_i2s_ops,
530                 SND_SOC_DAILINK_REG(i2s2),
531         },
532         {
533                 .name = "I2S3",
534                 .no_pcm = 1,
535                 .dpcm_playback = 1,
536                 .ignore_suspend = 1,
537         },
538         {
539                 .name = "I2S5",
540                 .no_pcm = 1,
541                 .dpcm_playback = 1,
542                 .ignore_suspend = 1,
543                 .ops = &mt8183_mt6358_i2s_ops,
544                 SND_SOC_DAILINK_REG(i2s5),
545         },
546         {
547                 .name = "TDM",
548                 .no_pcm = 1,
549                 .dai_fmt = SND_SOC_DAIFMT_I2S |
550                            SND_SOC_DAIFMT_IB_IF |
551                            SND_SOC_DAIFMT_CBM_CFM,
552                 .dpcm_playback = 1,
553                 .ignore_suspend = 1,
554                 .be_hw_params_fixup = mt8183_i2s_hw_params_fixup,
555                 .ops = &mt8183_mt6358_tdm_ops,
556                 .ignore = 1,
557                 .init = mt8183_mt6358_ts3a227_max98357_hdmi_init,
558                 SND_SOC_DAILINK_REG(tdm),
559         },
560 };
561
562 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357_card = {
563         .name = "mt8183_mt6358_ts3a227_max98357",
564         .owner = THIS_MODULE,
565         .dai_link = mt8183_mt6358_ts3a227_dai_links,
566         .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
567 };
568
569 static struct snd_soc_card mt8183_mt6358_ts3a227_max98357b_card = {
570         .name = "mt8183_mt6358_ts3a227_max98357b",
571         .owner = THIS_MODULE,
572         .dai_link = mt8183_mt6358_ts3a227_dai_links,
573         .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
574 };
575
576 static struct snd_soc_codec_conf mt8183_mt6358_ts3a227_rt1015_amp_conf[] = {
577         {
578                 .dlc = COMP_CODEC_CONF(RT1015_DEV0_NAME),
579                 .name_prefix = "Left",
580         },
581         {
582                 .dlc = COMP_CODEC_CONF(RT1015_DEV1_NAME),
583                 .name_prefix = "Right",
584         },
585 };
586
587 static struct snd_soc_card mt8183_mt6358_ts3a227_rt1015_card = {
588         .name = "mt8183_mt6358_ts3a227_rt1015",
589         .owner = THIS_MODULE,
590         .dai_link = mt8183_mt6358_ts3a227_dai_links,
591         .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
592         .codec_conf = mt8183_mt6358_ts3a227_rt1015_amp_conf,
593         .num_configs = ARRAY_SIZE(mt8183_mt6358_ts3a227_rt1015_amp_conf),
594 };
595
596 static struct snd_soc_card mt8183_mt6358_ts3a227_rt1015p_card = {
597         .name = "mt8183_mt6358_ts3a227_rt1015p",
598         .owner = THIS_MODULE,
599         .dai_link = mt8183_mt6358_ts3a227_dai_links,
600         .num_links = ARRAY_SIZE(mt8183_mt6358_ts3a227_dai_links),
601 };
602
603 static int
604 mt8183_mt6358_ts3a227_max98357_headset_init(struct snd_soc_component *component)
605 {
606         int ret;
607         struct mt8183_mt6358_ts3a227_max98357_priv *priv =
608                         snd_soc_card_get_drvdata(component->card);
609
610         /* Enable Headset and 4 Buttons Jack detection */
611         ret = snd_soc_card_jack_new(component->card,
612                                     "Headset Jack",
613                                     SND_JACK_HEADSET |
614                                     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
615                                     SND_JACK_BTN_2 | SND_JACK_BTN_3,
616                                     &priv->headset_jack);
617         if (ret)
618                 return ret;
619
620         ret = ts3a227e_enable_jack_detect(component, &priv->headset_jack);
621
622         return ret;
623 }
624
625 static struct snd_soc_aux_dev mt8183_mt6358_ts3a227_max98357_headset_dev = {
626         .dlc = COMP_EMPTY(),
627         .init = mt8183_mt6358_ts3a227_max98357_headset_init,
628 };
629
630 static int
631 mt8183_mt6358_ts3a227_max98357_dev_probe(struct platform_device *pdev)
632 {
633         struct snd_soc_card *card;
634         struct device_node *platform_node, *ec_codec, *hdmi_codec;
635         struct snd_soc_dai_link *dai_link;
636         struct mt8183_mt6358_ts3a227_max98357_priv *priv;
637         int ret, i;
638
639         platform_node = of_parse_phandle(pdev->dev.of_node,
640                                          "mediatek,platform", 0);
641         if (!platform_node) {
642                 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
643                 return -EINVAL;
644         }
645
646         card = (struct snd_soc_card *)of_device_get_match_data(&pdev->dev);
647         if (!card)
648                 return -EINVAL;
649         card->dev = &pdev->dev;
650
651         ec_codec = of_parse_phandle(pdev->dev.of_node, "mediatek,ec-codec", 0);
652         hdmi_codec = of_parse_phandle(pdev->dev.of_node,
653                                       "mediatek,hdmi-codec", 0);
654
655         for_each_card_prelinks(card, i, dai_link) {
656                 if (ec_codec && strcmp(dai_link->name, "Wake on Voice") == 0) {
657                         dai_link->cpus[0].name = NULL;
658                         dai_link->cpus[0].of_node = ec_codec;
659                         dai_link->cpus[0].dai_name = NULL;
660                         dai_link->codecs[0].name = NULL;
661                         dai_link->codecs[0].of_node = ec_codec;
662                         dai_link->codecs[0].dai_name = "Wake on Voice";
663                         dai_link->platforms[0].of_node = ec_codec;
664                         dai_link->ignore = 0;
665                 }
666
667                 if (strcmp(dai_link->name, "I2S3") == 0) {
668                         if (card == &mt8183_mt6358_ts3a227_max98357_card ||
669                             card == &mt8183_mt6358_ts3a227_max98357b_card) {
670                                 dai_link->be_hw_params_fixup =
671                                         mt8183_i2s_hw_params_fixup;
672                                 dai_link->ops = &mt8183_mt6358_i2s_ops;
673                                 dai_link->cpus = i2s3_max98357a_cpus;
674                                 dai_link->num_cpus =
675                                         ARRAY_SIZE(i2s3_max98357a_cpus);
676                                 dai_link->codecs = i2s3_max98357a_codecs;
677                                 dai_link->num_codecs =
678                                         ARRAY_SIZE(i2s3_max98357a_codecs);
679                                 dai_link->platforms = i2s3_max98357a_platforms;
680                                 dai_link->num_platforms =
681                                         ARRAY_SIZE(i2s3_max98357a_platforms);
682                         } else if (card == &mt8183_mt6358_ts3a227_rt1015_card) {
683                                 dai_link->be_hw_params_fixup =
684                                         mt8183_rt1015_i2s_hw_params_fixup;
685                                 dai_link->ops = &mt8183_mt6358_rt1015_i2s_ops;
686                                 dai_link->cpus = i2s3_rt1015_cpus;
687                                 dai_link->num_cpus =
688                                         ARRAY_SIZE(i2s3_rt1015_cpus);
689                                 dai_link->codecs = i2s3_rt1015_codecs;
690                                 dai_link->num_codecs =
691                                         ARRAY_SIZE(i2s3_rt1015_codecs);
692                                 dai_link->platforms = i2s3_rt1015_platforms;
693                                 dai_link->num_platforms =
694                                         ARRAY_SIZE(i2s3_rt1015_platforms);
695                         } else if (card == &mt8183_mt6358_ts3a227_rt1015p_card) {
696                                 dai_link->be_hw_params_fixup =
697                                         mt8183_rt1015_i2s_hw_params_fixup;
698                                 dai_link->ops = &mt8183_mt6358_i2s_ops;
699                                 dai_link->cpus = i2s3_rt1015p_cpus;
700                                 dai_link->num_cpus =
701                                         ARRAY_SIZE(i2s3_rt1015p_cpus);
702                                 dai_link->codecs = i2s3_rt1015p_codecs;
703                                 dai_link->num_codecs =
704                                         ARRAY_SIZE(i2s3_rt1015p_codecs);
705                                 dai_link->platforms = i2s3_rt1015p_platforms;
706                                 dai_link->num_platforms =
707                                         ARRAY_SIZE(i2s3_rt1015p_platforms);
708                         }
709                 }
710
711                 if (card == &mt8183_mt6358_ts3a227_max98357b_card) {
712                         if (strcmp(dai_link->name, "I2S2") == 0 ||
713                             strcmp(dai_link->name, "I2S3") == 0)
714                                 dai_link->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
715                                                     SND_SOC_DAIFMT_NB_NF |
716                                                     SND_SOC_DAIFMT_CBM_CFM;
717                 }
718
719                 if (hdmi_codec && strcmp(dai_link->name, "TDM") == 0) {
720                         dai_link->codecs->of_node = hdmi_codec;
721                         dai_link->ignore = 0;
722                 }
723
724                 if (!dai_link->platforms->name)
725                         dai_link->platforms->of_node = platform_node;
726         }
727
728         mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node =
729                 of_parse_phandle(pdev->dev.of_node,
730                                  "mediatek,headset-codec", 0);
731         if (mt8183_mt6358_ts3a227_max98357_headset_dev.dlc.of_node) {
732                 card->aux_dev = &mt8183_mt6358_ts3a227_max98357_headset_dev;
733                 card->num_aux_devs = 1;
734         }
735
736         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
737         if (!priv)
738                 return -ENOMEM;
739
740         snd_soc_card_set_drvdata(card, priv);
741
742         priv->pinctrl = devm_pinctrl_get(&pdev->dev);
743         if (IS_ERR(priv->pinctrl)) {
744                 dev_err(&pdev->dev, "%s devm_pinctrl_get failed\n",
745                         __func__);
746                 return PTR_ERR(priv->pinctrl);
747         }
748
749         for (i = 0; i < PIN_STATE_MAX; i++) {
750                 priv->pin_states[i] = pinctrl_lookup_state(priv->pinctrl,
751                                                            mt8183_pin_str[i]);
752                 if (IS_ERR(priv->pin_states[i])) {
753                         ret = PTR_ERR(priv->pin_states[i]);
754                         dev_info(&pdev->dev, "%s Can't find pin state %s %d\n",
755                                  __func__, mt8183_pin_str[i], ret);
756                 }
757         }
758
759         if (!IS_ERR(priv->pin_states[PIN_TDM_OUT_OFF])) {
760                 ret = pinctrl_select_state(priv->pinctrl,
761                                            priv->pin_states[PIN_TDM_OUT_OFF]);
762                 if (ret)
763                         dev_info(&pdev->dev,
764                                  "%s failed to select state %d\n",
765                                  __func__, ret);
766         }
767
768         if (!IS_ERR(priv->pin_states[PIN_STATE_DEFAULT])) {
769                 ret = pinctrl_select_state(priv->pinctrl,
770                                            priv->pin_states[PIN_STATE_DEFAULT]);
771                 if (ret)
772                         dev_info(&pdev->dev,
773                                  "%s failed to select state %d\n",
774                                  __func__, ret);
775         }
776
777         ret = devm_snd_soc_register_card(&pdev->dev, card);
778
779         of_node_put(platform_node);
780         of_node_put(ec_codec);
781         of_node_put(hdmi_codec);
782         return ret;
783 }
784
785 #ifdef CONFIG_OF
786 static const struct of_device_id mt8183_mt6358_ts3a227_max98357_dt_match[] = {
787         {
788                 .compatible = "mediatek,mt8183_mt6358_ts3a227_max98357",
789                 .data = &mt8183_mt6358_ts3a227_max98357_card,
790         },
791         {
792                 .compatible = "mediatek,mt8183_mt6358_ts3a227_max98357b",
793                 .data = &mt8183_mt6358_ts3a227_max98357b_card,
794         },
795         {
796                 .compatible = "mediatek,mt8183_mt6358_ts3a227_rt1015",
797                 .data = &mt8183_mt6358_ts3a227_rt1015_card,
798         },
799         {
800                 .compatible = "mediatek,mt8183_mt6358_ts3a227_rt1015p",
801                 .data = &mt8183_mt6358_ts3a227_rt1015p_card,
802         },
803         {}
804 };
805 #endif
806
807 static struct platform_driver mt8183_mt6358_ts3a227_max98357_driver = {
808         .driver = {
809                 .name = "mt8183_mt6358_ts3a227",
810 #ifdef CONFIG_OF
811                 .of_match_table = mt8183_mt6358_ts3a227_max98357_dt_match,
812 #endif
813                 .pm = &snd_soc_pm_ops,
814         },
815         .probe = mt8183_mt6358_ts3a227_max98357_dev_probe,
816 };
817
818 module_platform_driver(mt8183_mt6358_ts3a227_max98357_driver);
819
820 /* Module information */
821 MODULE_DESCRIPTION("MT8183-MT6358-TS3A227-MAX98357 ALSA SoC machine driver");
822 MODULE_AUTHOR("Shunli Wang <shunli.wang@mediatek.com>");
823 MODULE_LICENSE("GPL v2");
824 MODULE_ALIAS("mt8183_mt6358_ts3a227_max98357 soc card");