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