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