6be78a206242fdef050fa77708987923098edcdc
[linux-2.6-microblaze.git] / sound / soc / intel / boards / sof_rt5682.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2019-2020 Intel Corporation.
3
4 /*
5  * Intel SOF Machine Driver with Realtek rt5682 Codec
6  * and speaker codec MAX98357A or RT1015.
7  */
8 #include <linux/i2c.h>
9 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/clk.h>
13 #include <linux/dmi.h>
14 #include <sound/core.h>
15 #include <sound/jack.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/sof.h>
20 #include <sound/rt5682.h>
21 #include <sound/soc-acpi.h>
22 #include "../../codecs/rt1015.h"
23 #include "../../codecs/rt5682.h"
24 #include "../../codecs/hdac_hdmi.h"
25 #include "../common/soc-intel-quirks.h"
26 #include "hda_dsp_common.h"
27 #include "sof_maxim_common.h"
28 #include "sof_realtek_common.h"
29
30 #define NAME_SIZE 32
31
32 #define SOF_RT5682_SSP_CODEC(quirk)             ((quirk) & GENMASK(2, 0))
33 #define SOF_RT5682_SSP_CODEC_MASK                       (GENMASK(2, 0))
34 #define SOF_RT5682_MCLK_EN                      BIT(3)
35 #define SOF_RT5682_MCLK_24MHZ                   BIT(4)
36 #define SOF_SPEAKER_AMP_PRESENT         BIT(5)
37 #define SOF_RT5682_SSP_AMP_SHIFT                6
38 #define SOF_RT5682_SSP_AMP_MASK                 (GENMASK(8, 6))
39 #define SOF_RT5682_SSP_AMP(quirk)       \
40         (((quirk) << SOF_RT5682_SSP_AMP_SHIFT) & SOF_RT5682_SSP_AMP_MASK)
41 #define SOF_RT5682_MCLK_BYTCHT_EN               BIT(9)
42 #define SOF_RT5682_NUM_HDMIDEV_SHIFT            10
43 #define SOF_RT5682_NUM_HDMIDEV_MASK             (GENMASK(12, 10))
44 #define SOF_RT5682_NUM_HDMIDEV(quirk)   \
45         ((quirk << SOF_RT5682_NUM_HDMIDEV_SHIFT) & SOF_RT5682_NUM_HDMIDEV_MASK)
46 #define SOF_RT1011_SPEAKER_AMP_PRESENT          BIT(13)
47 #define SOF_RT1015_SPEAKER_AMP_PRESENT          BIT(14)
48 #define SOF_RT1015_SPEAKER_AMP_100FS            BIT(15)
49 #define SOF_RT1015P_SPEAKER_AMP_PRESENT         BIT(16)
50 #define SOF_MAX98373_SPEAKER_AMP_PRESENT        BIT(17)
51 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT       BIT(18)
52
53 /* BT audio offload: reserve 3 bits for future */
54 #define SOF_BT_OFFLOAD_SSP_SHIFT                19
55 #define SOF_BT_OFFLOAD_SSP_MASK         (GENMASK(21, 19))
56 #define SOF_BT_OFFLOAD_SSP(quirk)       \
57         (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
58 #define SOF_SSP_BT_OFFLOAD_PRESENT              BIT(22)
59
60 /* Default: MCLK on, MCLK 19.2M, SSP0  */
61 static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
62                                         SOF_RT5682_SSP_CODEC(0);
63
64 static int is_legacy_cpu;
65
66 static struct snd_soc_jack sof_hdmi[3];
67
68 struct sof_hdmi_pcm {
69         struct list_head head;
70         struct snd_soc_dai *codec_dai;
71         int device;
72 };
73
74 struct sof_card_private {
75         struct clk *mclk;
76         struct snd_soc_jack sof_headset;
77         struct list_head hdmi_pcm_list;
78         bool common_hdmi_codec_drv;
79 };
80
81 static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
82 {
83         sof_rt5682_quirk = (unsigned long)id->driver_data;
84         return 1;
85 }
86
87 static const struct dmi_system_id sof_rt5682_quirk_table[] = {
88         {
89                 .callback = sof_rt5682_quirk_cb,
90                 .matches = {
91                         DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
92                         DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"),
93                 },
94                 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
95         },
96         {
97                 .callback = sof_rt5682_quirk_cb,
98                 .matches = {
99                         DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
100                         DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
101                 },
102                 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
103         },
104         {
105                 .callback = sof_rt5682_quirk_cb,
106                 .matches = {
107                         DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
108                         DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
109                 },
110                 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
111                                         SOF_RT5682_MCLK_24MHZ |
112                                         SOF_RT5682_SSP_CODEC(1)),
113         },
114         {
115                 /*
116                  * Dooly is hatch family but using rt1015 amp so it
117                  * requires a quirk before "Google_Hatch".
118                  */
119                 .callback = sof_rt5682_quirk_cb,
120                 .matches = {
121                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
122                         DMI_MATCH(DMI_PRODUCT_NAME, "Dooly"),
123                 },
124                 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
125                                         SOF_RT5682_MCLK_24MHZ |
126                                         SOF_RT5682_SSP_CODEC(0) |
127                                         SOF_SPEAKER_AMP_PRESENT |
128                                         SOF_RT1015_SPEAKER_AMP_PRESENT |
129                                         SOF_RT1015_SPEAKER_AMP_100FS |
130                                         SOF_RT5682_SSP_AMP(1)),
131         },
132         {
133                 .callback = sof_rt5682_quirk_cb,
134                 .matches = {
135                         DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Hatch"),
136                 },
137                 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
138                                         SOF_RT5682_MCLK_24MHZ |
139                                         SOF_RT5682_SSP_CODEC(0) |
140                                         SOF_SPEAKER_AMP_PRESENT |
141                                         SOF_RT5682_SSP_AMP(1)),
142         },
143         {
144                 .callback = sof_rt5682_quirk_cb,
145                 .matches = {
146                         DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
147                         DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
148                 },
149                 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
150                                         SOF_RT5682_SSP_CODEC(0)),
151         },
152         {
153                 .callback = sof_rt5682_quirk_cb,
154                 .matches = {
155                         DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
156                         DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
157                 },
158                 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
159                                         SOF_RT5682_SSP_CODEC(0) |
160                                         SOF_SPEAKER_AMP_PRESENT |
161                                         SOF_MAX98373_SPEAKER_AMP_PRESENT |
162                                         SOF_RT5682_SSP_AMP(2) |
163                                         SOF_RT5682_NUM_HDMIDEV(4)),
164         },
165         {}
166 };
167
168 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
169 {
170         struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
171         struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
172         struct sof_hdmi_pcm *pcm;
173
174         pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
175         if (!pcm)
176                 return -ENOMEM;
177
178         /* dai_link id is 1:1 mapped to the PCM device */
179         pcm->device = rtd->dai_link->id;
180         pcm->codec_dai = dai;
181
182         list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
183
184         return 0;
185 }
186
187 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
188 {
189         struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
190         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
191         struct snd_soc_jack *jack;
192         int ret;
193
194         /* need to enable ASRC function for 24MHz mclk rate */
195         if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
196             (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
197                 rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER |
198                                         RT5682_AD_STEREO1_FILTER,
199                                         RT5682_CLK_SEL_I2S1_ASRC);
200         }
201
202         if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
203                 /*
204                  * The firmware might enable the clock at
205                  * boot (this information may or may not
206                  * be reflected in the enable clock register).
207                  * To change the rate we must disable the clock
208                  * first to cover these cases. Due to common
209                  * clock framework restrictions that do not allow
210                  * to disable a clock that has not been enabled,
211                  * we need to enable the clock first.
212                  */
213                 ret = clk_prepare_enable(ctx->mclk);
214                 if (!ret)
215                         clk_disable_unprepare(ctx->mclk);
216
217                 ret = clk_set_rate(ctx->mclk, 19200000);
218
219                 if (ret)
220                         dev_err(rtd->dev, "unable to set MCLK rate\n");
221         }
222
223         /*
224          * Headset buttons map to the google Reference headset.
225          * These can be configured by userspace.
226          */
227         ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
228                                     SND_JACK_HEADSET | SND_JACK_BTN_0 |
229                                     SND_JACK_BTN_1 | SND_JACK_BTN_2 |
230                                     SND_JACK_BTN_3,
231                                     &ctx->sof_headset, NULL, 0);
232         if (ret) {
233                 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
234                 return ret;
235         }
236
237         jack = &ctx->sof_headset;
238
239         snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
240         snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
241         snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
242         snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
243         ret = snd_soc_component_set_jack(component, jack, NULL);
244
245         if (ret) {
246                 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
247                 return ret;
248         }
249
250         return ret;
251 };
252
253 static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
254 {
255         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
256
257         snd_soc_component_set_jack(component, NULL, NULL);
258 }
259
260 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
261                                 struct snd_pcm_hw_params *params)
262 {
263         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
264         struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
265         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
266         int clk_id, clk_freq, pll_out, ret;
267
268         if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
269                 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
270                         ret = clk_prepare_enable(ctx->mclk);
271                         if (ret < 0) {
272                                 dev_err(rtd->dev,
273                                         "could not configure MCLK state");
274                                 return ret;
275                         }
276                 }
277
278                 clk_id = RT5682_PLL1_S_MCLK;
279
280                 /* get the tplg configured mclk. */
281                 clk_freq = sof_dai_get_mclk(rtd);
282
283                 /* mclk from the quirk is the first choice */
284                 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ) {
285                         if (clk_freq != 24000000)
286                                 dev_warn(rtd->dev, "configure wrong mclk in tplg, please use 24MHz.\n");
287                         clk_freq = 24000000;
288                 } else if (clk_freq == 0) {
289                         /* use default mclk if not specified correct in topology */
290                         clk_freq = 19200000;
291                 } else if (clk_freq < 0) {
292                         return clk_freq;
293                 }
294         } else {
295                 clk_id = RT5682_PLL1_S_BCLK1;
296                 clk_freq = params_rate(params) * 50;
297         }
298
299         pll_out = params_rate(params) * 512;
300
301         ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
302         if (ret < 0)
303                 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
304
305         /* Configure sysclk for codec */
306         ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
307                                      pll_out, SND_SOC_CLOCK_IN);
308         if (ret < 0)
309                 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
310
311         /*
312          * slot_width should equal or large than data length, set them
313          * be the same
314          */
315         ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
316                                        params_width(params));
317         if (ret < 0) {
318                 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
319                 return ret;
320         }
321
322         return ret;
323 }
324
325 static struct snd_soc_ops sof_rt5682_ops = {
326         .hw_params = sof_rt5682_hw_params,
327 };
328
329 static int sof_rt1015_hw_params(struct snd_pcm_substream *substream,
330                                 struct snd_pcm_hw_params *params)
331 {
332         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
333         struct snd_soc_card *card = rtd->card;
334         struct snd_soc_dai *codec_dai;
335         int i, fs, ret;
336
337         if (!snd_soc_card_get_codec_dai(card, "rt1015-aif"))
338                 return 0;
339
340         if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_100FS)
341                 fs = 100;
342         else
343                 fs = 64;
344
345         for_each_rtd_codec_dais(rtd, i, codec_dai) {
346                 ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
347                                           params_rate(params) * fs,
348                                           params_rate(params) * 256);
349                 if (ret < 0) {
350                         dev_err(card->dev, "failed to set pll\n");
351                         return ret;
352                 }
353                 /* Configure sysclk for codec */
354                 ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
355                                              params_rate(params) * 256,
356                                              SND_SOC_CLOCK_IN);
357                 if (ret < 0) {
358                         dev_err(card->dev, "failed to set sysclk\n");
359                         return ret;
360                 }
361
362                 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_100FS) {
363                         if (!strcmp(codec_dai->component->name, "i2c-10EC1015:00")) {
364                                 ret = snd_soc_dai_set_tdm_slot(codec_dai,
365                                                                0x0, 0x1, 4, 24);
366                                 if (ret < 0) {
367                                         dev_err(card->dev, "failed to set tdm slot\n");
368                                         return ret;
369                                 }
370                         }
371
372                         if (!strcmp(codec_dai->component->name, "i2c-10EC1015:01")) {
373                                 ret = snd_soc_dai_set_tdm_slot(codec_dai,
374                                                                0x0, 0x2, 4, 24);
375                                 if (ret < 0) {
376                                         dev_err(card->dev, "failed to set tdm slot\n");
377                                         return ret;
378                                 }
379                         }
380                 }
381         }
382
383         return 0;
384 }
385
386 static struct snd_soc_ops sof_rt1015_ops = {
387         .hw_params = sof_rt1015_hw_params,
388 };
389
390 static struct snd_soc_dai_link_component platform_component[] = {
391         {
392                 /* name might be overridden during probe */
393                 .name = "0000:00:1f.3"
394         }
395 };
396
397 static int sof_card_late_probe(struct snd_soc_card *card)
398 {
399         struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
400         struct snd_soc_component *component = NULL;
401         struct snd_soc_dapm_context *dapm = &card->dapm;
402         char jack_name[NAME_SIZE];
403         struct sof_hdmi_pcm *pcm;
404         int err;
405         int i = 0;
406
407         /* HDMI is not supported by SOF on Baytrail/CherryTrail */
408         if (is_legacy_cpu)
409                 return 0;
410
411         if (list_empty(&ctx->hdmi_pcm_list))
412                 return -EINVAL;
413
414         if (ctx->common_hdmi_codec_drv) {
415                 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
416                                        head);
417                 component = pcm->codec_dai->component;
418                 return hda_dsp_hdmi_build_controls(card, component);
419         }
420
421         list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
422                 component = pcm->codec_dai->component;
423                 snprintf(jack_name, sizeof(jack_name),
424                          "HDMI/DP, pcm=%d Jack", pcm->device);
425                 err = snd_soc_card_jack_new(card, jack_name,
426                                             SND_JACK_AVOUT, &sof_hdmi[i],
427                                             NULL, 0);
428
429                 if (err)
430                         return err;
431
432                 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
433                                           &sof_hdmi[i]);
434                 if (err < 0)
435                         return err;
436
437                 i++;
438         }
439
440         if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
441                 /* Disable Left and Right Spk pin after boot */
442                 snd_soc_dapm_disable_pin(dapm, "Left Spk");
443                 snd_soc_dapm_disable_pin(dapm, "Right Spk");
444                 err = snd_soc_dapm_sync(dapm);
445                 if (err < 0)
446                         return err;
447         }
448         return hdac_hdmi_jack_port_init(component, &card->dapm);
449 }
450
451 static const struct snd_kcontrol_new sof_controls[] = {
452         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
453         SOC_DAPM_PIN_SWITCH("Headset Mic"),
454         SOC_DAPM_PIN_SWITCH("Spk"),
455         SOC_DAPM_PIN_SWITCH("Left Spk"),
456         SOC_DAPM_PIN_SWITCH("Right Spk"),
457
458 };
459
460 static const struct snd_soc_dapm_widget sof_widgets[] = {
461         SND_SOC_DAPM_HP("Headphone Jack", NULL),
462         SND_SOC_DAPM_MIC("Headset Mic", NULL),
463         SND_SOC_DAPM_SPK("Spk", NULL),
464         SND_SOC_DAPM_SPK("Left Spk", NULL),
465         SND_SOC_DAPM_SPK("Right Spk", NULL),
466 };
467
468 static const struct snd_soc_dapm_widget dmic_widgets[] = {
469         SND_SOC_DAPM_MIC("SoC DMIC", NULL),
470 };
471
472 static const struct snd_soc_dapm_route sof_map[] = {
473         /* HP jack connectors - unknown if we have jack detection */
474         { "Headphone Jack", NULL, "HPOL" },
475         { "Headphone Jack", NULL, "HPOR" },
476
477         /* other jacks */
478         { "IN1P", NULL, "Headset Mic" },
479 };
480
481 static const struct snd_soc_dapm_route speaker_map[] = {
482         /* speaker */
483         { "Spk", NULL, "Speaker" },
484 };
485
486 static const struct snd_soc_dapm_route speaker_map_lr[] = {
487         { "Left Spk", NULL, "Left SPO" },
488         { "Right Spk", NULL, "Right SPO" },
489 };
490
491 static const struct snd_soc_dapm_route dmic_map[] = {
492         /* digital mics */
493         {"DMic", NULL, "SoC DMIC"},
494 };
495
496 static int speaker_codec_init_lr(struct snd_soc_pcm_runtime *rtd)
497 {
498         return snd_soc_dapm_add_routes(&rtd->card->dapm, speaker_map_lr,
499                                        ARRAY_SIZE(speaker_map_lr));
500 }
501
502 static int speaker_codec_init(struct snd_soc_pcm_runtime *rtd)
503 {
504         struct snd_soc_card *card = rtd->card;
505         int ret;
506
507         ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map,
508                                       ARRAY_SIZE(speaker_map));
509
510         if (ret)
511                 dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
512         return ret;
513 }
514
515 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
516 {
517         struct snd_soc_card *card = rtd->card;
518         int ret;
519
520         ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
521                                         ARRAY_SIZE(dmic_widgets));
522         if (ret) {
523                 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
524                 /* Don't need to add routes if widget addition failed */
525                 return ret;
526         }
527
528         ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
529                                       ARRAY_SIZE(dmic_map));
530
531         if (ret)
532                 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
533
534         return ret;
535 }
536
537 static struct snd_soc_codec_conf rt1015_amp_conf[] = {
538         {
539                 .dlc = COMP_CODEC_CONF("i2c-10EC1015:00"),
540                 .name_prefix = "Left",
541         },
542         {
543                 .dlc = COMP_CODEC_CONF("i2c-10EC1015:01"),
544                 .name_prefix = "Right",
545         },
546 };
547
548 /* sof audio machine driver for rt5682 codec */
549 static struct snd_soc_card sof_audio_card_rt5682 = {
550         .name = "rt5682", /* the sof- prefix is added by the core */
551         .owner = THIS_MODULE,
552         .controls = sof_controls,
553         .num_controls = ARRAY_SIZE(sof_controls),
554         .dapm_widgets = sof_widgets,
555         .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
556         .dapm_routes = sof_map,
557         .num_dapm_routes = ARRAY_SIZE(sof_map),
558         .fully_routed = true,
559         .late_probe = sof_card_late_probe,
560 };
561
562 static struct snd_soc_dai_link_component rt5682_component[] = {
563         {
564                 .name = "i2c-10EC5682:00",
565                 .dai_name = "rt5682-aif1",
566         }
567 };
568
569 static struct snd_soc_dai_link_component dmic_component[] = {
570         {
571                 .name = "dmic-codec",
572                 .dai_name = "dmic-hifi",
573         }
574 };
575
576 static struct snd_soc_dai_link_component max98357a_component[] = {
577         {
578                 .name = "MX98357A:00",
579                 .dai_name = "HiFi",
580         }
581 };
582
583 static struct snd_soc_dai_link_component max98360a_component[] = {
584         {
585                 .name = "MX98360A:00",
586                 .dai_name = "HiFi",
587         }
588 };
589
590 static struct snd_soc_dai_link_component rt1015_components[] = {
591         {
592                 .name = "i2c-10EC1015:00",
593                 .dai_name = "rt1015-aif",
594         },
595         {
596                 .name = "i2c-10EC1015:01",
597                 .dai_name = "rt1015-aif",
598         },
599 };
600
601 static struct snd_soc_dai_link_component dummy_component[] = {
602         {
603                 .name = "snd-soc-dummy",
604                 .dai_name = "snd-soc-dummy-dai",
605         }
606 };
607
608 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
609                                                           int ssp_codec,
610                                                           int ssp_amp,
611                                                           int dmic_be_num,
612                                                           int hdmi_num)
613 {
614         struct snd_soc_dai_link_component *idisp_components;
615         struct snd_soc_dai_link_component *cpus;
616         struct snd_soc_dai_link *links;
617         int i, id = 0;
618
619         links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
620                              sof_audio_card_rt5682.num_links, GFP_KERNEL);
621         cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component) *
622                              sof_audio_card_rt5682.num_links, GFP_KERNEL);
623         if (!links || !cpus)
624                 goto devm_err;
625
626         /* codec SSP */
627         links[id].name = devm_kasprintf(dev, GFP_KERNEL,
628                                         "SSP%d-Codec", ssp_codec);
629         if (!links[id].name)
630                 goto devm_err;
631
632         links[id].id = id;
633         links[id].codecs = rt5682_component;
634         links[id].num_codecs = ARRAY_SIZE(rt5682_component);
635         links[id].platforms = platform_component;
636         links[id].num_platforms = ARRAY_SIZE(platform_component);
637         links[id].init = sof_rt5682_codec_init;
638         links[id].exit = sof_rt5682_codec_exit;
639         links[id].ops = &sof_rt5682_ops;
640         links[id].dpcm_playback = 1;
641         links[id].dpcm_capture = 1;
642         links[id].no_pcm = 1;
643         links[id].cpus = &cpus[id];
644         links[id].num_cpus = 1;
645         if (is_legacy_cpu) {
646                 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
647                                                           "ssp%d-port",
648                                                           ssp_codec);
649                 if (!links[id].cpus->dai_name)
650                         goto devm_err;
651         } else {
652                 /*
653                  * Currently, On SKL+ platforms MCLK will be turned off in sof
654                  * runtime suspended, and it will go into runtime suspended
655                  * right after playback is stop. However, rt5682 will output
656                  * static noise if sysclk turns off during playback. Set
657                  * ignore_pmdown_time to power down rt5682 immediately and
658                  * avoid the noise.
659                  * It can be removed once we can control MCLK by driver.
660                  */
661                 links[id].ignore_pmdown_time = 1;
662                 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
663                                                           "SSP%d Pin",
664                                                           ssp_codec);
665                 if (!links[id].cpus->dai_name)
666                         goto devm_err;
667         }
668         id++;
669
670         /* dmic */
671         if (dmic_be_num > 0) {
672                 /* at least we have dmic01 */
673                 links[id].name = "dmic01";
674                 links[id].cpus = &cpus[id];
675                 links[id].cpus->dai_name = "DMIC01 Pin";
676                 links[id].init = dmic_init;
677                 if (dmic_be_num > 1) {
678                         /* set up 2 BE links at most */
679                         links[id + 1].name = "dmic16k";
680                         links[id + 1].cpus = &cpus[id + 1];
681                         links[id + 1].cpus->dai_name = "DMIC16k Pin";
682                         dmic_be_num = 2;
683                 }
684         }
685
686         for (i = 0; i < dmic_be_num; i++) {
687                 links[id].id = id;
688                 links[id].num_cpus = 1;
689                 links[id].codecs = dmic_component;
690                 links[id].num_codecs = ARRAY_SIZE(dmic_component);
691                 links[id].platforms = platform_component;
692                 links[id].num_platforms = ARRAY_SIZE(platform_component);
693                 links[id].ignore_suspend = 1;
694                 links[id].dpcm_capture = 1;
695                 links[id].no_pcm = 1;
696                 id++;
697         }
698
699         /* HDMI */
700         if (hdmi_num > 0) {
701                 idisp_components = devm_kzalloc(dev,
702                                    sizeof(struct snd_soc_dai_link_component) *
703                                    hdmi_num, GFP_KERNEL);
704                 if (!idisp_components)
705                         goto devm_err;
706         }
707         for (i = 1; i <= hdmi_num; i++) {
708                 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
709                                                 "iDisp%d", i);
710                 if (!links[id].name)
711                         goto devm_err;
712
713                 links[id].id = id;
714                 links[id].cpus = &cpus[id];
715                 links[id].num_cpus = 1;
716                 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
717                                                           "iDisp%d Pin", i);
718                 if (!links[id].cpus->dai_name)
719                         goto devm_err;
720
721                 idisp_components[i - 1].name = "ehdaudio0D2";
722                 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
723                                                                   GFP_KERNEL,
724                                                                   "intel-hdmi-hifi%d",
725                                                                   i);
726                 if (!idisp_components[i - 1].dai_name)
727                         goto devm_err;
728
729                 links[id].codecs = &idisp_components[i - 1];
730                 links[id].num_codecs = 1;
731                 links[id].platforms = platform_component;
732                 links[id].num_platforms = ARRAY_SIZE(platform_component);
733                 links[id].init = sof_hdmi_init;
734                 links[id].dpcm_playback = 1;
735                 links[id].no_pcm = 1;
736                 id++;
737         }
738
739         /* speaker amp */
740         if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
741                 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
742                                                 "SSP%d-Codec", ssp_amp);
743                 if (!links[id].name)
744                         goto devm_err;
745
746                 links[id].id = id;
747                 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) {
748                         links[id].codecs = rt1015_components;
749                         links[id].num_codecs = ARRAY_SIZE(rt1015_components);
750                         links[id].init = speaker_codec_init_lr;
751                         links[id].ops = &sof_rt1015_ops;
752                 } else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
753                         sof_rt1015p_dai_link(&links[id]);
754                 } else if (sof_rt5682_quirk &
755                                 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
756                         links[id].codecs = max_98373_components;
757                         links[id].num_codecs = ARRAY_SIZE(max_98373_components);
758                         links[id].init = max_98373_spk_codec_init;
759                         links[id].ops = &max_98373_ops;
760                         /* feedback stream */
761                         links[id].dpcm_capture = 1;
762                 } else if (sof_rt5682_quirk &
763                                 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
764                         links[id].codecs = max98360a_component;
765                         links[id].num_codecs = ARRAY_SIZE(max98360a_component);
766                         links[id].init = speaker_codec_init;
767                 } else if (sof_rt5682_quirk &
768                                 SOF_RT1011_SPEAKER_AMP_PRESENT) {
769                         sof_rt1011_dai_link(&links[id]);
770                 } else {
771                         links[id].codecs = max98357a_component;
772                         links[id].num_codecs = ARRAY_SIZE(max98357a_component);
773                         links[id].init = speaker_codec_init;
774                 }
775                 links[id].platforms = platform_component;
776                 links[id].num_platforms = ARRAY_SIZE(platform_component);
777                 links[id].dpcm_playback = 1;
778                 links[id].no_pcm = 1;
779                 links[id].cpus = &cpus[id];
780                 links[id].num_cpus = 1;
781                 if (is_legacy_cpu) {
782                         links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
783                                                                   "ssp%d-port",
784                                                                   ssp_amp);
785                         if (!links[id].cpus->dai_name)
786                                 goto devm_err;
787
788                 } else {
789                         links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
790                                                                   "SSP%d Pin",
791                                                                   ssp_amp);
792                         if (!links[id].cpus->dai_name)
793                                 goto devm_err;
794                 }
795                 id++;
796         }
797
798         /* BT audio offload */
799         if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
800                 int port = (sof_rt5682_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
801                                 SOF_BT_OFFLOAD_SSP_SHIFT;
802
803                 links[id].id = id;
804                 links[id].cpus = &cpus[id];
805                 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
806                                                           "SSP%d Pin", port);
807                 if (!links[id].cpus->dai_name)
808                         goto devm_err;
809                 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
810                 if (!links[id].name)
811                         goto devm_err;
812                 links[id].codecs = dummy_component;
813                 links[id].num_codecs = ARRAY_SIZE(dummy_component);
814                 links[id].platforms = platform_component;
815                 links[id].num_platforms = ARRAY_SIZE(platform_component);
816                 links[id].dpcm_playback = 1;
817                 links[id].dpcm_capture = 1;
818                 links[id].no_pcm = 1;
819                 links[id].num_cpus = 1;
820         }
821
822         return links;
823 devm_err:
824         return NULL;
825 }
826
827 static int sof_audio_probe(struct platform_device *pdev)
828 {
829         struct snd_soc_dai_link *dai_links;
830         struct snd_soc_acpi_mach *mach;
831         struct sof_card_private *ctx;
832         int dmic_be_num, hdmi_num;
833         int ret, ssp_amp, ssp_codec;
834
835         ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
836         if (!ctx)
837                 return -ENOMEM;
838
839         if (pdev->id_entry && pdev->id_entry->driver_data)
840                 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
841
842         dmi_check_system(sof_rt5682_quirk_table);
843
844         mach = pdev->dev.platform_data;
845
846         /* A speaker amp might not be present when the quirk claims one is.
847          * Detect this via whether the machine driver match includes quirk_data.
848          */
849         if ((sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
850                 sof_rt5682_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
851
852         if (soc_intel_is_byt() || soc_intel_is_cht()) {
853                 is_legacy_cpu = 1;
854                 dmic_be_num = 0;
855                 hdmi_num = 0;
856                 /* default quirk for legacy cpu */
857                 sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
858                                                 SOF_RT5682_MCLK_BYTCHT_EN |
859                                                 SOF_RT5682_SSP_CODEC(2);
860         } else {
861                 dmic_be_num = 2;
862                 hdmi_num = (sof_rt5682_quirk & SOF_RT5682_NUM_HDMIDEV_MASK) >>
863                          SOF_RT5682_NUM_HDMIDEV_SHIFT;
864                 /* default number of HDMI DAI's */
865                 if (!hdmi_num)
866                         hdmi_num = 3;
867         }
868
869         /* need to get main clock from pmc */
870         if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
871                 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
872                 if (IS_ERR(ctx->mclk)) {
873                         ret = PTR_ERR(ctx->mclk);
874
875                         dev_err(&pdev->dev,
876                                 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
877                                 ret);
878                         return ret;
879                 }
880
881                 ret = clk_prepare_enable(ctx->mclk);
882                 if (ret < 0) {
883                         dev_err(&pdev->dev,
884                                 "could not configure MCLK state");
885                         return ret;
886                 }
887         }
888
889         dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
890
891         ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
892                         SOF_RT5682_SSP_AMP_SHIFT;
893
894         ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
895
896         /* compute number of dai links */
897         sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num;
898
899         if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
900                 sof_audio_card_rt5682.num_links++;
901
902         if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
903                 max_98373_set_codec_conf(&sof_audio_card_rt5682);
904         else if (sof_rt5682_quirk & SOF_RT1011_SPEAKER_AMP_PRESENT)
905                 sof_rt1011_codec_conf(&sof_audio_card_rt5682);
906         else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
907                 sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
908
909         if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
910                 sof_audio_card_rt5682.num_links++;
911
912         dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
913                                               dmic_be_num, hdmi_num);
914         if (!dai_links)
915                 return -ENOMEM;
916
917         sof_audio_card_rt5682.dai_link = dai_links;
918
919         if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) {
920                 sof_audio_card_rt5682.codec_conf = rt1015_amp_conf;
921                 sof_audio_card_rt5682.num_configs = ARRAY_SIZE(rt1015_amp_conf);
922         }
923
924         INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
925
926         sof_audio_card_rt5682.dev = &pdev->dev;
927
928         /* set platform name for each dailink */
929         ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
930                                                     mach->mach_params.platform);
931         if (ret)
932                 return ret;
933
934         ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
935
936         snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
937
938         return devm_snd_soc_register_card(&pdev->dev,
939                                           &sof_audio_card_rt5682);
940 }
941
942 static const struct platform_device_id board_ids[] = {
943         {
944                 .name = "sof_rt5682",
945         },
946         {
947                 .name = "tgl_max98357a_rt5682",
948                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
949                                         SOF_RT5682_SSP_CODEC(0) |
950                                         SOF_SPEAKER_AMP_PRESENT |
951                                         SOF_RT5682_SSP_AMP(1) |
952                                         SOF_RT5682_NUM_HDMIDEV(4) |
953                                         SOF_BT_OFFLOAD_SSP(2) |
954                                         SOF_SSP_BT_OFFLOAD_PRESENT),
955         },
956         {
957                 .name = "jsl_rt5682_rt1015",
958                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
959                                         SOF_RT5682_MCLK_24MHZ |
960                                         SOF_RT5682_SSP_CODEC(0) |
961                                         SOF_SPEAKER_AMP_PRESENT |
962                                         SOF_RT1015_SPEAKER_AMP_PRESENT |
963                                         SOF_RT5682_SSP_AMP(1)),
964         },
965         {
966                 .name = "tgl_max98373_rt5682",
967                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
968                                         SOF_RT5682_SSP_CODEC(0) |
969                                         SOF_SPEAKER_AMP_PRESENT |
970                                         SOF_MAX98373_SPEAKER_AMP_PRESENT |
971                                         SOF_RT5682_SSP_AMP(1) |
972                                         SOF_RT5682_NUM_HDMIDEV(4) |
973                                         SOF_BT_OFFLOAD_SSP(2) |
974                                         SOF_SSP_BT_OFFLOAD_PRESENT),
975         },
976         {
977                 .name = "jsl_rt5682_max98360a",
978                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
979                                         SOF_RT5682_MCLK_24MHZ |
980                                         SOF_RT5682_SSP_CODEC(0) |
981                                         SOF_SPEAKER_AMP_PRESENT |
982                                         SOF_MAX98360A_SPEAKER_AMP_PRESENT |
983                                         SOF_RT5682_SSP_AMP(1)),
984         },
985         {
986                 .name = "cml_rt1015_rt5682",
987                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
988                                         SOF_RT5682_MCLK_24MHZ |
989                                         SOF_RT5682_SSP_CODEC(0) |
990                                         SOF_SPEAKER_AMP_PRESENT |
991                                         SOF_RT1015_SPEAKER_AMP_PRESENT |
992                                         SOF_RT1015_SPEAKER_AMP_100FS |
993                                         SOF_RT5682_SSP_AMP(1)),
994         },
995         {
996                 .name = "tgl_rt1011_rt5682",
997                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
998                                         SOF_RT5682_SSP_CODEC(0) |
999                                         SOF_SPEAKER_AMP_PRESENT |
1000                                         SOF_RT1011_SPEAKER_AMP_PRESENT |
1001                                         SOF_RT5682_SSP_AMP(1) |
1002                                         SOF_RT5682_NUM_HDMIDEV(4) |
1003                                         SOF_BT_OFFLOAD_SSP(2) |
1004                                         SOF_SSP_BT_OFFLOAD_PRESENT),
1005         },
1006         {
1007                 .name = "jsl_rt5682_rt1015p",
1008                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1009                                         SOF_RT5682_MCLK_24MHZ |
1010                                         SOF_RT5682_SSP_CODEC(0) |
1011                                         SOF_SPEAKER_AMP_PRESENT |
1012                                         SOF_RT1015P_SPEAKER_AMP_PRESENT |
1013                                         SOF_RT5682_SSP_AMP(1)),
1014         },
1015         {
1016                 .name = "adl_max98373_rt5682",
1017                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1018                                         SOF_RT5682_SSP_CODEC(0) |
1019                                         SOF_SPEAKER_AMP_PRESENT |
1020                                         SOF_MAX98373_SPEAKER_AMP_PRESENT |
1021                                         SOF_RT5682_SSP_AMP(1) |
1022                                         SOF_RT5682_NUM_HDMIDEV(4) |
1023                                         SOF_BT_OFFLOAD_SSP(2) |
1024                                         SOF_SSP_BT_OFFLOAD_PRESENT),
1025         },
1026         {
1027                 .name = "adl_max98357a_rt5682",
1028                 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1029                                         SOF_RT5682_SSP_CODEC(0) |
1030                                         SOF_SPEAKER_AMP_PRESENT |
1031                                         SOF_RT5682_SSP_AMP(2) |
1032                                         SOF_RT5682_NUM_HDMIDEV(4)),
1033         },
1034         { }
1035 };
1036
1037 static struct platform_driver sof_audio = {
1038         .probe = sof_audio_probe,
1039         .driver = {
1040                 .name = "sof_rt5682",
1041                 .pm = &snd_soc_pm_ops,
1042         },
1043         .id_table = board_ids,
1044 };
1045 module_platform_driver(sof_audio)
1046
1047 /* Module information */
1048 MODULE_DESCRIPTION("SOF Audio Machine driver");
1049 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
1050 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
1051 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
1052 MODULE_LICENSE("GPL v2");
1053 MODULE_ALIAS("platform:sof_rt5682");
1054 MODULE_ALIAS("platform:tgl_max98357a_rt5682");
1055 MODULE_ALIAS("platform:jsl_rt5682_rt1015");
1056 MODULE_ALIAS("platform:tgl_max98373_rt5682");
1057 MODULE_ALIAS("platform:jsl_rt5682_max98360a");
1058 MODULE_ALIAS("platform:cml_rt1015_rt5682");
1059 MODULE_ALIAS("platform:tgl_rt1011_rt5682");
1060 MODULE_ALIAS("platform:jsl_rt5682_rt1015p");
1061 MODULE_ALIAS("platform:adl_max98373_rt5682");
1062 MODULE_ALIAS("platform:adl_max98357a_rt5682");
1063 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1064 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);