1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel Kabylake I2S Machine Driver with MAXIM98927
4  * and RT5663 Codecs
5  *
6  * Copyright (C) 2017, Intel Corporation. All rights reserved.
7  *
8  * Modified from:
9  *   Intel Skylake I2S Machine driver
10  */
11 
12 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <sound/core.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/soc-acpi.h>
21 #include "../../codecs/rt5663.h"
22 #include "../../codecs/hdac_hdmi.h"
23 #include <linux/clk.h>
24 #include <linux/clk-provider.h>
25 #include <linux/clkdev.h>
26 
27 #define KBL_REALTEK_CODEC_DAI "rt5663-aif"
28 #define KBL_MAXIM_CODEC_DAI "max98927-aif1"
29 #define DMIC_CH(p) p->list[p->count-1]
30 #define MAXIM_DEV0_NAME "i2c-MX98927:00"
31 #define MAXIM_DEV1_NAME "i2c-MX98927:01"
32 
33 static struct snd_soc_card *kabylake_audio_card;
34 static const struct snd_pcm_hw_constraint_list *dmic_constraints;
35 static struct snd_soc_jack skylake_hdmi[3];
36 
37 struct kbl_hdmi_pcm {
38 	struct list_head head;
39 	struct snd_soc_dai *codec_dai;
40 	int device;
41 };
42 
43 struct kbl_rt5663_private {
44 	struct snd_soc_jack kabylake_headset;
45 	struct list_head hdmi_pcm_list;
46 	struct clk *mclk;
47 	struct clk *sclk;
48 };
49 
50 enum {
51 	KBL_DPCM_AUDIO_PB = 0,
52 	KBL_DPCM_AUDIO_CP,
53 	KBL_DPCM_AUDIO_HS_PB,
54 	KBL_DPCM_AUDIO_ECHO_REF_CP,
55 	KBL_DPCM_AUDIO_REF_CP,
56 	KBL_DPCM_AUDIO_DMIC_CP,
57 	KBL_DPCM_AUDIO_HDMI1_PB,
58 	KBL_DPCM_AUDIO_HDMI2_PB,
59 	KBL_DPCM_AUDIO_HDMI3_PB,
60 };
61 
62 static const struct snd_kcontrol_new kabylake_controls[] = {
63 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
64 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
65 	SOC_DAPM_PIN_SWITCH("Left Spk"),
66 	SOC_DAPM_PIN_SWITCH("Right Spk"),
67 };
68 
69 static int platform_clock_control(struct snd_soc_dapm_widget *w,
70 			struct snd_kcontrol *k, int  event)
71 {
72 	struct snd_soc_dapm_context *dapm = w->dapm;
73 	struct snd_soc_card *card = dapm->card;
74 	struct kbl_rt5663_private *priv = snd_soc_card_get_drvdata(card);
75 	int ret = 0;
76 
77 	/*
78 	 * MCLK/SCLK need to be ON early for a successful synchronization of
79 	 * codec internal clock. And the clocks are turned off during
80 	 * POST_PMD after the stream is stopped.
81 	 */
82 	switch (event) {
83 	case SND_SOC_DAPM_PRE_PMU:
84 		/* Enable MCLK */
85 		ret = clk_set_rate(priv->mclk, 24000000);
86 		if (ret < 0) {
87 			dev_err(card->dev, "Can't set rate for mclk, err: %d\n",
88 				ret);
89 			return ret;
90 		}
91 
92 		ret = clk_prepare_enable(priv->mclk);
93 		if (ret < 0) {
94 			dev_err(card->dev, "Can't enable mclk, err: %d\n", ret);
95 			return ret;
96 		}
97 
98 		/* Enable SCLK */
99 		ret = clk_set_rate(priv->sclk, 3072000);
100 		if (ret < 0) {
101 			dev_err(card->dev, "Can't set rate for sclk, err: %d\n",
102 				ret);
103 			clk_disable_unprepare(priv->mclk);
104 			return ret;
105 		}
106 
107 		ret = clk_prepare_enable(priv->sclk);
108 		if (ret < 0) {
109 			dev_err(card->dev, "Can't enable sclk, err: %d\n", ret);
110 			clk_disable_unprepare(priv->mclk);
111 		}
112 		break;
113 	case SND_SOC_DAPM_POST_PMD:
114 		clk_disable_unprepare(priv->mclk);
115 		clk_disable_unprepare(priv->sclk);
116 		break;
117 	default:
118 		return 0;
119 	}
120 
121 	return 0;
122 }
123 
124 static const struct snd_soc_dapm_widget kabylake_widgets[] = {
125 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
126 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
127 	SND_SOC_DAPM_SPK("Left Spk", NULL),
128 	SND_SOC_DAPM_SPK("Right Spk", NULL),
129 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
130 	SND_SOC_DAPM_SPK("HDMI1", NULL),
131 	SND_SOC_DAPM_SPK("HDMI2", NULL),
132 	SND_SOC_DAPM_SPK("HDMI3", NULL),
133 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
134 			platform_clock_control, SND_SOC_DAPM_PRE_PMU |
135 			SND_SOC_DAPM_POST_PMD),
136 };
137 
138 static const struct snd_soc_dapm_route kabylake_map[] = {
139 	/* HP jack connectors - unknown if we have jack detection */
140 	{ "Headphone Jack", NULL, "Platform Clock" },
141 	{ "Headphone Jack", NULL, "HPOL" },
142 	{ "Headphone Jack", NULL, "HPOR" },
143 
144 	/* speaker */
145 	{ "Left Spk", NULL, "Left BE_OUT" },
146 	{ "Right Spk", NULL, "Right BE_OUT" },
147 
148 	/* other jacks */
149 	{ "Headset Mic", NULL, "Platform Clock" },
150 	{ "IN1P", NULL, "Headset Mic" },
151 	{ "IN1N", NULL, "Headset Mic" },
152 	{ "DMic", NULL, "SoC DMIC" },
153 
154 	/* CODEC BE connections */
155 	{ "Left HiFi Playback", NULL, "ssp0 Tx" },
156 	{ "Right HiFi Playback", NULL, "ssp0 Tx" },
157 	{ "ssp0 Tx", NULL, "spk_out" },
158 
159 	{ "AIF Playback", NULL, "ssp1 Tx" },
160 	{ "ssp1 Tx", NULL, "codec1_out" },
161 
162 	{ "hs_in", NULL, "ssp1 Rx" },
163 	{ "ssp1 Rx", NULL, "AIF Capture" },
164 
165 	/* IV feedback path */
166 	{ "codec0_fb_in", NULL, "ssp0 Rx"},
167 	{ "ssp0 Rx", NULL, "Left HiFi Capture" },
168 	{ "ssp0 Rx", NULL, "Right HiFi Capture" },
169 
170 	/* DMIC */
171 	{ "dmic01_hifi", NULL, "DMIC01 Rx" },
172 	{ "DMIC01 Rx", NULL, "DMIC AIF" },
173 
174 	{ "hifi3", NULL, "iDisp3 Tx"},
175 	{ "iDisp3 Tx", NULL, "iDisp3_out"},
176 	{ "hifi2", NULL, "iDisp2 Tx"},
177 	{ "iDisp2 Tx", NULL, "iDisp2_out"},
178 	{ "hifi1", NULL, "iDisp1 Tx"},
179 	{ "iDisp1 Tx", NULL, "iDisp1_out"},
180 };
181 
182 enum {
183 	KBL_DPCM_AUDIO_5663_PB = 0,
184 	KBL_DPCM_AUDIO_5663_CP,
185 	KBL_DPCM_AUDIO_5663_HDMI1_PB,
186 	KBL_DPCM_AUDIO_5663_HDMI2_PB,
187 };
188 
189 static const struct snd_kcontrol_new kabylake_5663_controls[] = {
190 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
191 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
192 };
193 
194 static const struct snd_soc_dapm_widget kabylake_5663_widgets[] = {
195 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
196 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
197 	SND_SOC_DAPM_SPK("DP", NULL),
198 	SND_SOC_DAPM_SPK("HDMI", NULL),
199 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
200 			platform_clock_control, SND_SOC_DAPM_PRE_PMU |
201 			SND_SOC_DAPM_POST_PMD),
202 };
203 
204 static const struct snd_soc_dapm_route kabylake_5663_map[] = {
205 	{ "Headphone Jack", NULL, "Platform Clock" },
206 	{ "Headphone Jack", NULL, "HPOL" },
207 	{ "Headphone Jack", NULL, "HPOR" },
208 
209 	/* other jacks */
210 	{ "Headset Mic", NULL, "Platform Clock" },
211 	{ "IN1P", NULL, "Headset Mic" },
212 	{ "IN1N", NULL, "Headset Mic" },
213 
214 	{ "HDMI", NULL, "hif5 Output" },
215 	{ "DP", NULL, "hif6 Output" },
216 
217 	/* CODEC BE connections */
218 	{ "AIF Playback", NULL, "ssp1 Tx" },
219 	{ "ssp1 Tx", NULL, "codec1_out" },
220 
221 	{ "codec0_in", NULL, "ssp1 Rx" },
222 	{ "ssp1 Rx", NULL, "AIF Capture" },
223 
224 	{ "hifi2", NULL, "iDisp2 Tx"},
225 	{ "iDisp2 Tx", NULL, "iDisp2_out"},
226 	{ "hifi1", NULL, "iDisp1 Tx"},
227 	{ "iDisp1 Tx", NULL, "iDisp1_out"},
228 };
229 
230 static struct snd_soc_codec_conf max98927_codec_conf[] = {
231 	{
232 		.dlc = COMP_CODEC_CONF(MAXIM_DEV0_NAME),
233 		.name_prefix = "Right",
234 	},
235 	{
236 		.dlc = COMP_CODEC_CONF(MAXIM_DEV1_NAME),
237 		.name_prefix = "Left",
238 	},
239 };
240 
241 static int kabylake_rt5663_fe_init(struct snd_soc_pcm_runtime *rtd)
242 {
243 	int ret;
244 	struct snd_soc_dapm_context *dapm;
245 	struct snd_soc_component *component = rtd->cpu_dai->component;
246 
247 	dapm = snd_soc_component_get_dapm(component);
248 	ret = snd_soc_dapm_ignore_suspend(dapm, "Reference Capture");
249 	if (ret) {
250 		dev_err(rtd->dev, "Ref Cap ignore suspend failed %d\n", ret);
251 		return ret;
252 	}
253 
254 	return ret;
255 }
256 
257 static int kabylake_rt5663_codec_init(struct snd_soc_pcm_runtime *rtd)
258 {
259 	int ret;
260 	struct kbl_rt5663_private *ctx = snd_soc_card_get_drvdata(rtd->card);
261 	struct snd_soc_component *component = rtd->codec_dai->component;
262 	struct snd_soc_jack *jack;
263 
264 	/*
265 	 * Headset buttons map to the google Reference headset.
266 	 * These can be configured by userspace.
267 	 */
268 	ret = snd_soc_card_jack_new(kabylake_audio_card, "Headset Jack",
269 			SND_JACK_HEADSET | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
270 			SND_JACK_BTN_2 | SND_JACK_BTN_3, &ctx->kabylake_headset,
271 			NULL, 0);
272 	if (ret) {
273 		dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
274 		return ret;
275 	}
276 
277 	jack = &ctx->kabylake_headset;
278 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
279 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
280 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
281 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
282 
283 	snd_soc_component_set_jack(component, &ctx->kabylake_headset, NULL);
284 
285 	return ret;
286 }
287 
288 static int kabylake_rt5663_max98927_codec_init(struct snd_soc_pcm_runtime *rtd)
289 {
290 	int ret;
291 
292 	ret = kabylake_rt5663_codec_init(rtd);
293 	if (ret)
294 		return ret;
295 
296 	ret = snd_soc_dapm_ignore_suspend(&rtd->card->dapm, "SoC DMIC");
297 	if (ret) {
298 		dev_err(rtd->dev, "SoC DMIC ignore suspend failed %d\n", ret);
299 		return ret;
300 	}
301 
302 	return ret;
303 }
304 
305 static int kabylake_hdmi_init(struct snd_soc_pcm_runtime *rtd, int device)
306 {
307 	struct kbl_rt5663_private *ctx = snd_soc_card_get_drvdata(rtd->card);
308 	struct snd_soc_dai *dai = rtd->codec_dai;
309 	struct kbl_hdmi_pcm *pcm;
310 
311 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
312 	if (!pcm)
313 		return -ENOMEM;
314 
315 	pcm->device = device;
316 	pcm->codec_dai = dai;
317 
318 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
319 
320 	return 0;
321 }
322 
323 static int kabylake_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
324 {
325 	return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI1_PB);
326 }
327 
328 static int kabylake_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
329 {
330 	return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI2_PB);
331 }
332 
333 static int kabylake_hdmi3_init(struct snd_soc_pcm_runtime *rtd)
334 {
335 	return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_HDMI3_PB);
336 }
337 
338 static int kabylake_5663_hdmi1_init(struct snd_soc_pcm_runtime *rtd)
339 {
340 	return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_5663_HDMI1_PB);
341 }
342 
343 static int kabylake_5663_hdmi2_init(struct snd_soc_pcm_runtime *rtd)
344 {
345 	return kabylake_hdmi_init(rtd, KBL_DPCM_AUDIO_5663_HDMI2_PB);
346 }
347 
348 static unsigned int rates[] = {
349 	48000,
350 };
351 
352 static const struct snd_pcm_hw_constraint_list constraints_rates = {
353 	.count = ARRAY_SIZE(rates),
354 	.list  = rates,
355 	.mask = 0,
356 };
357 
358 static unsigned int channels[] = {
359 	2,
360 };
361 
362 static const struct snd_pcm_hw_constraint_list constraints_channels = {
363 	.count = ARRAY_SIZE(channels),
364 	.list = channels,
365 	.mask = 0,
366 };
367 
368 static int kbl_fe_startup(struct snd_pcm_substream *substream)
369 {
370 	struct snd_pcm_runtime *runtime = substream->runtime;
371 
372 	/*
373 	 * On this platform for PCM device we support,
374 	 * 48Khz
375 	 * stereo
376 	 * 16 bit audio
377 	 */
378 
379 	runtime->hw.channels_max = 2;
380 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
381 					   &constraints_channels);
382 
383 	runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE;
384 	snd_pcm_hw_constraint_msbits(runtime, 0, 16, 16);
385 
386 	snd_pcm_hw_constraint_list(runtime, 0,
387 				SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
388 
389 	return 0;
390 }
391 
392 static const struct snd_soc_ops kabylake_rt5663_fe_ops = {
393 	.startup = kbl_fe_startup,
394 };
395 
396 static int kabylake_ssp_fixup(struct snd_soc_pcm_runtime *rtd,
397 	struct snd_pcm_hw_params *params)
398 {
399 	struct snd_interval *rate = hw_param_interval(params,
400 			SNDRV_PCM_HW_PARAM_RATE);
401 	struct snd_interval *chan = hw_param_interval(params,
402 			SNDRV_PCM_HW_PARAM_CHANNELS);
403 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
404 	struct snd_soc_dpcm *dpcm = container_of(
405 			params, struct snd_soc_dpcm, hw_params);
406 	struct snd_soc_dai_link *fe_dai_link = dpcm->fe->dai_link;
407 	struct snd_soc_dai_link *be_dai_link = dpcm->be->dai_link;
408 
409 	/*
410 	 * The ADSP will convert the FE rate to 48k, stereo, 24 bit
411 	 */
412 	if (!strcmp(fe_dai_link->name, "Kbl Audio Port") ||
413 	    !strcmp(fe_dai_link->name, "Kbl Audio Headset Playback") ||
414 	    !strcmp(fe_dai_link->name, "Kbl Audio Capture Port")) {
415 		rate->min = rate->max = 48000;
416 		chan->min = chan->max = 2;
417 		snd_mask_none(fmt);
418 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
419 	}
420 	/*
421 	 * The speaker on the SSP0 supports S16_LE and not S24_LE.
422 	 * thus changing the mask here
423 	 */
424 	if (!strcmp(be_dai_link->name, "SSP0-Codec"))
425 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
426 
427 	return 0;
428 }
429 
430 static int kabylake_rt5663_hw_params(struct snd_pcm_substream *substream,
431 	struct snd_pcm_hw_params *params)
432 {
433 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
434 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
435 	int ret;
436 
437 	/* use ASRC for internal clocks, as PLL rate isn't multiple of BCLK */
438 	rt5663_sel_asrc_clk_src(codec_dai->component,
439 			RT5663_DA_STEREO_FILTER | RT5663_AD_STEREO_FILTER,
440 			RT5663_CLK_SEL_I2S1_ASRC);
441 
442 	ret = snd_soc_dai_set_sysclk(codec_dai,
443 			RT5663_SCLK_S_MCLK, 24576000, SND_SOC_CLOCK_IN);
444 	if (ret < 0)
445 		dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
446 
447 	return ret;
448 }
449 
450 static struct snd_soc_ops kabylake_rt5663_ops = {
451 	.hw_params = kabylake_rt5663_hw_params,
452 };
453 
454 static int kabylake_dmic_fixup(struct snd_soc_pcm_runtime *rtd,
455 		struct snd_pcm_hw_params *params)
456 {
457 	struct snd_interval *chan = hw_param_interval(params,
458 				SNDRV_PCM_HW_PARAM_CHANNELS);
459 
460 	if (params_channels(params) == 2 || DMIC_CH(dmic_constraints) == 2)
461 		chan->min = chan->max = 2;
462 	else
463 		chan->min = chan->max = 4;
464 
465 	return 0;
466 }
467 
468 static int kabylake_ssp0_hw_params(struct snd_pcm_substream *substream,
469 					struct snd_pcm_hw_params *params)
470 {
471 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
472 	struct snd_soc_dai *codec_dai;
473 	int ret = 0, j;
474 
475 	for_each_rtd_codec_dai(rtd, j, codec_dai) {
476 		if (!strcmp(codec_dai->component->name, MAXIM_DEV0_NAME)) {
477 			/*
478 			 * Use channel 4 and 5 for the first amp
479 			 */
480 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x30, 3, 8, 16);
481 			if (ret < 0) {
482 				dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
483 				return ret;
484 			}
485 		}
486 		if (!strcmp(codec_dai->component->name, MAXIM_DEV1_NAME)) {
487 			/*
488 			 * Use channel 6 and 7 for the second amp
489 			 */
490 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC0, 3, 8, 16);
491 			if (ret < 0) {
492 				dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
493 				return ret;
494 			}
495 		}
496 	}
497 	return ret;
498 }
499 
500 static struct snd_soc_ops kabylake_ssp0_ops = {
501 	.hw_params = kabylake_ssp0_hw_params,
502 };
503 
504 static unsigned int channels_dmic[] = {
505 	2, 4,
506 };
507 
508 static struct snd_pcm_hw_constraint_list constraints_dmic_channels = {
509 	.count = ARRAY_SIZE(channels_dmic),
510 	.list = channels_dmic,
511 	.mask = 0,
512 };
513 
514 static const unsigned int dmic_2ch[] = {
515 	2,
516 };
517 
518 static const struct snd_pcm_hw_constraint_list constraints_dmic_2ch = {
519 	.count = ARRAY_SIZE(dmic_2ch),
520 	.list = dmic_2ch,
521 	.mask = 0,
522 };
523 
524 static int kabylake_dmic_startup(struct snd_pcm_substream *substream)
525 {
526 	struct snd_pcm_runtime *runtime = substream->runtime;
527 
528 	runtime->hw.channels_max = DMIC_CH(dmic_constraints);
529 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
530 			dmic_constraints);
531 
532 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
533 			SNDRV_PCM_HW_PARAM_RATE, &constraints_rates);
534 }
535 
536 static struct snd_soc_ops kabylake_dmic_ops = {
537 	.startup = kabylake_dmic_startup,
538 };
539 
540 static unsigned int rates_16000[] = {
541 	16000,
542 };
543 
544 static const struct snd_pcm_hw_constraint_list constraints_16000 = {
545 	.count = ARRAY_SIZE(rates_16000),
546 	.list  = rates_16000,
547 };
548 
549 static const unsigned int ch_mono[] = {
550 	1,
551 };
552 
553 static const struct snd_pcm_hw_constraint_list constraints_refcap = {
554 	.count = ARRAY_SIZE(ch_mono),
555 	.list  = ch_mono,
556 };
557 
558 static int kabylake_refcap_startup(struct snd_pcm_substream *substream)
559 {
560 	substream->runtime->hw.channels_max = 1;
561 	snd_pcm_hw_constraint_list(substream->runtime, 0,
562 					SNDRV_PCM_HW_PARAM_CHANNELS,
563 					&constraints_refcap);
564 
565 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
566 				SNDRV_PCM_HW_PARAM_RATE,
567 				&constraints_16000);
568 }
569 
570 static struct snd_soc_ops skylake_refcap_ops = {
571 	.startup = kabylake_refcap_startup,
572 };
573 
574 SND_SOC_DAILINK_DEF(dummy,
575 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
576 
577 SND_SOC_DAILINK_DEF(system,
578 	DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
579 
580 SND_SOC_DAILINK_DEF(system2,
581 	DAILINK_COMP_ARRAY(COMP_CPU("System Pin2")));
582 
583 SND_SOC_DAILINK_DEF(echoref,
584 	DAILINK_COMP_ARRAY(COMP_CPU("Echoref Pin")));
585 
586 SND_SOC_DAILINK_DEF(reference,
587 	DAILINK_COMP_ARRAY(COMP_CPU("Reference Pin")));
588 
589 SND_SOC_DAILINK_DEF(dmic,
590 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC Pin")));
591 
592 SND_SOC_DAILINK_DEF(hdmi1,
593 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI1 Pin")));
594 
595 SND_SOC_DAILINK_DEF(hdmi2,
596 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI2 Pin")));
597 
598 SND_SOC_DAILINK_DEF(hdmi3,
599 	DAILINK_COMP_ARRAY(COMP_CPU("HDMI3 Pin")));
600 
601 SND_SOC_DAILINK_DEF(ssp0_pin,
602 	DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
603 SND_SOC_DAILINK_DEF(ssp0_codec,
604 	DAILINK_COMP_ARRAY(
605 	/* Left */	COMP_CODEC(MAXIM_DEV0_NAME, KBL_MAXIM_CODEC_DAI),
606 	/* Right */	COMP_CODEC(MAXIM_DEV1_NAME, KBL_MAXIM_CODEC_DAI)));
607 
608 SND_SOC_DAILINK_DEF(ssp1_pin,
609 	DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
610 SND_SOC_DAILINK_DEF(ssp1_codec,
611 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5663:00",
612 				      KBL_REALTEK_CODEC_DAI)));
613 
614 SND_SOC_DAILINK_DEF(dmic01_pin,
615 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
616 SND_SOC_DAILINK_DEF(dmic_codec,
617 	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
618 
619 SND_SOC_DAILINK_DEF(idisp1_pin,
620 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
621 SND_SOC_DAILINK_DEF(idisp1_codec,
622 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
623 
624 SND_SOC_DAILINK_DEF(idisp2_pin,
625 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
626 SND_SOC_DAILINK_DEF(idisp2_codec,
627 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
628 
629 SND_SOC_DAILINK_DEF(idisp3_pin,
630 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
631 SND_SOC_DAILINK_DEF(idisp3_codec,
632 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
633 
634 SND_SOC_DAILINK_DEF(platform,
635 	DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
636 
637 /* kabylake digital audio interface glue - connects codec <--> CPU */
638 static struct snd_soc_dai_link kabylake_dais[] = {
639 	/* Front End DAI links */
640 	[KBL_DPCM_AUDIO_PB] = {
641 		.name = "Kbl Audio Port",
642 		.stream_name = "Audio",
643 		.dynamic = 1,
644 		.nonatomic = 1,
645 		.init = kabylake_rt5663_fe_init,
646 		.trigger = {
647 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
648 		.dpcm_playback = 1,
649 		.ops = &kabylake_rt5663_fe_ops,
650 		SND_SOC_DAILINK_REG(system, dummy, platform),
651 	},
652 	[KBL_DPCM_AUDIO_CP] = {
653 		.name = "Kbl Audio Capture Port",
654 		.stream_name = "Audio Record",
655 		.dynamic = 1,
656 		.nonatomic = 1,
657 		.trigger = {
658 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
659 		.dpcm_capture = 1,
660 		.ops = &kabylake_rt5663_fe_ops,
661 		SND_SOC_DAILINK_REG(system, dummy, platform),
662 	},
663 	[KBL_DPCM_AUDIO_HS_PB] = {
664 		.name = "Kbl Audio Headset Playback",
665 		.stream_name = "Headset Audio",
666 		.dpcm_playback = 1,
667 		.nonatomic = 1,
668 		.dynamic = 1,
669 		SND_SOC_DAILINK_REG(system2, dummy, platform),
670 	},
671 	[KBL_DPCM_AUDIO_ECHO_REF_CP] = {
672 		.name = "Kbl Audio Echo Reference cap",
673 		.stream_name = "Echoreference Capture",
674 		.init = NULL,
675 		.capture_only = 1,
676 		.nonatomic = 1,
677 		SND_SOC_DAILINK_REG(echoref, dummy, platform),
678 	},
679 	[KBL_DPCM_AUDIO_REF_CP] = {
680 		.name = "Kbl Audio Reference cap",
681 		.stream_name = "Wake on Voice",
682 		.init = NULL,
683 		.dpcm_capture = 1,
684 		.nonatomic = 1,
685 		.dynamic = 1,
686 		.ops = &skylake_refcap_ops,
687 		SND_SOC_DAILINK_REG(reference, dummy, platform),
688 	},
689 	[KBL_DPCM_AUDIO_DMIC_CP] = {
690 		.name = "Kbl Audio DMIC cap",
691 		.stream_name = "dmiccap",
692 		.init = NULL,
693 		.dpcm_capture = 1,
694 		.nonatomic = 1,
695 		.dynamic = 1,
696 		.ops = &kabylake_dmic_ops,
697 		SND_SOC_DAILINK_REG(dmic, dummy, platform),
698 	},
699 	[KBL_DPCM_AUDIO_HDMI1_PB] = {
700 		.name = "Kbl HDMI Port1",
701 		.stream_name = "Hdmi1",
702 		.dpcm_playback = 1,
703 		.init = NULL,
704 		.trigger = {
705 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
706 		.nonatomic = 1,
707 		.dynamic = 1,
708 		SND_SOC_DAILINK_REG(hdmi1, dummy, platform),
709 	},
710 	[KBL_DPCM_AUDIO_HDMI2_PB] = {
711 		.name = "Kbl HDMI Port2",
712 		.stream_name = "Hdmi2",
713 		.dpcm_playback = 1,
714 		.init = NULL,
715 		.trigger = {
716 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
717 		.nonatomic = 1,
718 		.dynamic = 1,
719 		SND_SOC_DAILINK_REG(hdmi2, dummy, platform),
720 	},
721 	[KBL_DPCM_AUDIO_HDMI3_PB] = {
722 		.name = "Kbl HDMI Port3",
723 		.stream_name = "Hdmi3",
724 		.trigger = {
725 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
726 		.dpcm_playback = 1,
727 		.init = NULL,
728 		.nonatomic = 1,
729 		.dynamic = 1,
730 		SND_SOC_DAILINK_REG(hdmi3, dummy, platform),
731 	},
732 
733 	/* Back End DAI links */
734 	{
735 		/* SSP0 - Codec */
736 		.name = "SSP0-Codec",
737 		.id = 0,
738 		.no_pcm = 1,
739 		.dai_fmt = SND_SOC_DAIFMT_DSP_B |
740 			SND_SOC_DAIFMT_NB_NF |
741 			SND_SOC_DAIFMT_CBS_CFS,
742 		.ignore_pmdown_time = 1,
743 		.be_hw_params_fixup = kabylake_ssp_fixup,
744 		.dpcm_playback = 1,
745 		.ops = &kabylake_ssp0_ops,
746 		SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
747 	},
748 	{
749 		/* SSP1 - Codec */
750 		.name = "SSP1-Codec",
751 		.id = 1,
752 		.no_pcm = 1,
753 		.init = kabylake_rt5663_max98927_codec_init,
754 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
755 			SND_SOC_DAIFMT_CBS_CFS,
756 		.ignore_pmdown_time = 1,
757 		.be_hw_params_fixup = kabylake_ssp_fixup,
758 		.ops = &kabylake_rt5663_ops,
759 		.dpcm_playback = 1,
760 		.dpcm_capture = 1,
761 		SND_SOC_DAILINK_REG(ssp1_pin, ssp1_codec, platform),
762 	},
763 	{
764 		.name = "dmic01",
765 		.id = 2,
766 		.be_hw_params_fixup = kabylake_dmic_fixup,
767 		.ignore_suspend = 1,
768 		.dpcm_capture = 1,
769 		.no_pcm = 1,
770 		SND_SOC_DAILINK_REG(dmic01_pin, dmic_codec, platform),
771 	},
772 	{
773 		.name = "iDisp1",
774 		.id = 3,
775 		.dpcm_playback = 1,
776 		.init = kabylake_hdmi1_init,
777 		.no_pcm = 1,
778 		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
779 	},
780 	{
781 		.name = "iDisp2",
782 		.id = 4,
783 		.init = kabylake_hdmi2_init,
784 		.dpcm_playback = 1,
785 		.no_pcm = 1,
786 		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
787 	},
788 	{
789 		.name = "iDisp3",
790 		.id = 5,
791 		.init = kabylake_hdmi3_init,
792 		.dpcm_playback = 1,
793 		.no_pcm = 1,
794 		SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
795 	},
796 };
797 
798 static struct snd_soc_dai_link kabylake_5663_dais[] = {
799 	/* Front End DAI links */
800 	[KBL_DPCM_AUDIO_5663_PB] = {
801 		.name = "Kbl Audio Port",
802 		.stream_name = "Audio",
803 		.dynamic = 1,
804 		.nonatomic = 1,
805 		.trigger = {
806 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
807 		.dpcm_playback = 1,
808 		.ops = &kabylake_rt5663_fe_ops,
809 		SND_SOC_DAILINK_REG(system, dummy, platform),
810 	},
811 	[KBL_DPCM_AUDIO_5663_CP] = {
812 		.name = "Kbl Audio Capture Port",
813 		.stream_name = "Audio Record",
814 		.dynamic = 1,
815 		.nonatomic = 1,
816 		.trigger = {
817 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
818 		.dpcm_capture = 1,
819 		.ops = &kabylake_rt5663_fe_ops,
820 		SND_SOC_DAILINK_REG(system, dummy, platform),
821 	},
822 	[KBL_DPCM_AUDIO_5663_HDMI1_PB] = {
823 		.name = "Kbl HDMI Port1",
824 		.stream_name = "Hdmi1",
825 		.dpcm_playback = 1,
826 		.init = NULL,
827 		.trigger = {
828 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
829 		.nonatomic = 1,
830 		.dynamic = 1,
831 		SND_SOC_DAILINK_REG(hdmi1, dummy, platform),
832 	},
833 	[KBL_DPCM_AUDIO_5663_HDMI2_PB] = {
834 		.name = "Kbl HDMI Port2",
835 		.stream_name = "Hdmi2",
836 		.dpcm_playback = 1,
837 		.init = NULL,
838 		.trigger = {
839 			SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
840 		.nonatomic = 1,
841 		.dynamic = 1,
842 		SND_SOC_DAILINK_REG(hdmi2, dummy, platform),
843 	},
844 
845 	/* Back End DAI links */
846 	{
847 		/* SSP1 - Codec */
848 		.name = "SSP1-Codec",
849 		.id = 0,
850 		.no_pcm = 1,
851 		.init = kabylake_rt5663_codec_init,
852 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
853 			SND_SOC_DAIFMT_CBS_CFS,
854 		.ignore_pmdown_time = 1,
855 		.be_hw_params_fixup = kabylake_ssp_fixup,
856 		.ops = &kabylake_rt5663_ops,
857 		.dpcm_playback = 1,
858 		.dpcm_capture = 1,
859 		SND_SOC_DAILINK_REG(ssp1_pin, ssp1_codec, platform),
860 	},
861 	{
862 		.name = "iDisp1",
863 		.id = 1,
864 		.dpcm_playback = 1,
865 		.init = kabylake_5663_hdmi1_init,
866 		.no_pcm = 1,
867 		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
868 	},
869 	{
870 		.name = "iDisp2",
871 		.id = 2,
872 		.init = kabylake_5663_hdmi2_init,
873 		.dpcm_playback = 1,
874 		.no_pcm = 1,
875 		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
876 	},
877 };
878 
879 #define NAME_SIZE	32
880 static int kabylake_card_late_probe(struct snd_soc_card *card)
881 {
882 	struct kbl_rt5663_private *ctx = snd_soc_card_get_drvdata(card);
883 	struct kbl_hdmi_pcm *pcm;
884 	struct snd_soc_component *component = NULL;
885 	int err, i = 0;
886 	char jack_name[NAME_SIZE];
887 
888 	list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
889 		component = pcm->codec_dai->component;
890 		snprintf(jack_name, sizeof(jack_name),
891 			"HDMI/DP, pcm=%d Jack", pcm->device);
892 		err = snd_soc_card_jack_new(card, jack_name,
893 					SND_JACK_AVOUT, &skylake_hdmi[i],
894 					NULL, 0);
895 
896 		if (err)
897 			return err;
898 
899 		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
900 						&skylake_hdmi[i]);
901 		if (err < 0)
902 			return err;
903 
904 		i++;
905 	}
906 
907 	if (!component)
908 		return -EINVAL;
909 
910 	return hdac_hdmi_jack_port_init(component, &card->dapm);
911 }
912 
913 /* kabylake audio machine driver for SPT + RT5663 */
914 static struct snd_soc_card kabylake_audio_card_rt5663_m98927 = {
915 	.name = "kblrt5663max",
916 	.owner = THIS_MODULE,
917 	.dai_link = kabylake_dais,
918 	.num_links = ARRAY_SIZE(kabylake_dais),
919 	.controls = kabylake_controls,
920 	.num_controls = ARRAY_SIZE(kabylake_controls),
921 	.dapm_widgets = kabylake_widgets,
922 	.num_dapm_widgets = ARRAY_SIZE(kabylake_widgets),
923 	.dapm_routes = kabylake_map,
924 	.num_dapm_routes = ARRAY_SIZE(kabylake_map),
925 	.codec_conf = max98927_codec_conf,
926 	.num_configs = ARRAY_SIZE(max98927_codec_conf),
927 	.fully_routed = true,
928 	.late_probe = kabylake_card_late_probe,
929 };
930 
931 /* kabylake audio machine driver for RT5663 */
932 static struct snd_soc_card kabylake_audio_card_rt5663 = {
933 	.name = "kblrt5663",
934 	.owner = THIS_MODULE,
935 	.dai_link = kabylake_5663_dais,
936 	.num_links = ARRAY_SIZE(kabylake_5663_dais),
937 	.controls = kabylake_5663_controls,
938 	.num_controls = ARRAY_SIZE(kabylake_5663_controls),
939 	.dapm_widgets = kabylake_5663_widgets,
940 	.num_dapm_widgets = ARRAY_SIZE(kabylake_5663_widgets),
941 	.dapm_routes = kabylake_5663_map,
942 	.num_dapm_routes = ARRAY_SIZE(kabylake_5663_map),
943 	.fully_routed = true,
944 	.late_probe = kabylake_card_late_probe,
945 };
946 
947 static int kabylake_audio_probe(struct platform_device *pdev)
948 {
949 	struct kbl_rt5663_private *ctx;
950 	struct snd_soc_acpi_mach *mach;
951 	int ret;
952 
953 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
954 	if (!ctx)
955 		return -ENOMEM;
956 
957 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
958 
959 	kabylake_audio_card =
960 		(struct snd_soc_card *)pdev->id_entry->driver_data;
961 
962 	kabylake_audio_card->dev = &pdev->dev;
963 	snd_soc_card_set_drvdata(kabylake_audio_card, ctx);
964 
965 	mach = (&pdev->dev)->platform_data;
966 	if (mach)
967 		dmic_constraints = mach->mach_params.dmic_num == 2 ?
968 			&constraints_dmic_2ch : &constraints_dmic_channels;
969 
970 	ctx->mclk = devm_clk_get(&pdev->dev, "ssp1_mclk");
971 	if (IS_ERR(ctx->mclk)) {
972 		ret = PTR_ERR(ctx->mclk);
973 		if (ret == -ENOENT) {
974 			dev_info(&pdev->dev,
975 				"Failed to get ssp1_sclk, defer probe\n");
976 			return -EPROBE_DEFER;
977 		}
978 
979 		dev_err(&pdev->dev, "Failed to get ssp1_mclk with err:%d\n",
980 								ret);
981 		return ret;
982 	}
983 
984 	ctx->sclk = devm_clk_get(&pdev->dev, "ssp1_sclk");
985 	if (IS_ERR(ctx->sclk)) {
986 		ret = PTR_ERR(ctx->sclk);
987 		if (ret == -ENOENT) {
988 			dev_info(&pdev->dev,
989 				"Failed to get ssp1_sclk, defer probe\n");
990 			return -EPROBE_DEFER;
991 		}
992 
993 		dev_err(&pdev->dev, "Failed to get ssp1_sclk with err:%d\n",
994 								ret);
995 		return ret;
996 	}
997 
998 	return devm_snd_soc_register_card(&pdev->dev, kabylake_audio_card);
999 }
1000 
1001 static const struct platform_device_id kbl_board_ids[] = {
1002 	{
1003 		.name = "kbl_rt5663",
1004 		.driver_data = (kernel_ulong_t)&kabylake_audio_card_rt5663,
1005 	},
1006 	{
1007 		.name = "kbl_rt5663_m98927",
1008 		.driver_data =
1009 			(kernel_ulong_t)&kabylake_audio_card_rt5663_m98927,
1010 	},
1011 	{ }
1012 };
1013 
1014 static struct platform_driver kabylake_audio = {
1015 	.probe = kabylake_audio_probe,
1016 	.driver = {
1017 		.name = "kbl_rt5663_m98927",
1018 		.pm = &snd_soc_pm_ops,
1019 	},
1020 	.id_table = kbl_board_ids,
1021 };
1022 
1023 module_platform_driver(kabylake_audio)
1024 
1025 /* Module information */
1026 MODULE_DESCRIPTION("Audio Machine driver-RT5663 & MAX98927 in I2S mode");
1027 MODULE_AUTHOR("Naveen M <naveen.m@intel.com>");
1028 MODULE_AUTHOR("Harsha Priya <harshapriya.n@intel.com>");
1029 MODULE_LICENSE("GPL v2");
1030 MODULE_ALIAS("platform:kbl_rt5663");
1031 MODULE_ALIAS("platform:kbl_rt5663_m98927");
1032