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