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