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