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