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