1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2019 Intel Corporation.
3 
4 /*
5  * Intel Cometlake I2S Machine driver for RT1011 + RT5682 codec
6  */
7 
8 #include <linux/input.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/clk.h>
12 #include <linux/dmi.h>
13 #include <linux/slab.h>
14 #include <linux/acpi.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/rt5682.h>
21 #include <sound/soc-acpi.h>
22 #include "../../codecs/rt1011.h"
23 #include "../../codecs/rt5682.h"
24 #include "../../codecs/hdac_hdmi.h"
25 #include "hda_dsp_common.h"
26 
27 /* The platform clock outputs 24Mhz clock to codec as I2S MCLK */
28 #define CML_PLAT_CLK	24000000
29 #define CML_RT1011_CODEC_DAI "rt1011-aif"
30 #define CML_RT5682_CODEC_DAI "rt5682-aif1"
31 #define NAME_SIZE 32
32 
33 static struct snd_soc_jack hdmi_jack[3];
34 
35 struct hdmi_pcm {
36 	struct list_head head;
37 	struct snd_soc_dai *codec_dai;
38 	int device;
39 };
40 
41 struct card_private {
42 	char codec_name[SND_ACPI_I2C_ID_LEN];
43 	struct snd_soc_jack headset;
44 	struct list_head hdmi_pcm_list;
45 	bool common_hdmi_codec_drv;
46 };
47 
48 static const struct snd_kcontrol_new cml_controls[] = {
49 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
50 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
51 	SOC_DAPM_PIN_SWITCH("TL Ext Spk"),
52 	SOC_DAPM_PIN_SWITCH("TR Ext Spk"),
53 	SOC_DAPM_PIN_SWITCH("WL Ext Spk"),
54 	SOC_DAPM_PIN_SWITCH("WR Ext Spk"),
55 };
56 
57 static const struct snd_soc_dapm_widget cml_rt1011_rt5682_widgets[] = {
58 	SND_SOC_DAPM_SPK("TL Ext Spk", NULL),
59 	SND_SOC_DAPM_SPK("TR Ext Spk", NULL),
60 	SND_SOC_DAPM_SPK("WL Ext Spk", NULL),
61 	SND_SOC_DAPM_SPK("WR Ext Spk", NULL),
62 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
63 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
64 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
65 };
66 
67 static const struct snd_soc_dapm_route cml_rt1011_rt5682_map[] = {
68 	/*speaker*/
69 	{"TL Ext Spk", NULL, "TL SPO"},
70 	{"TR Ext Spk", NULL, "TR SPO"},
71 	{"WL Ext Spk", NULL, "WL SPO"},
72 	{"WR Ext Spk", NULL, "WR SPO"},
73 
74 	/* HP jack connectors - unknown if we have jack detection */
75 	{ "Headphone Jack", NULL, "HPOL" },
76 	{ "Headphone Jack", NULL, "HPOR" },
77 
78 	/* other jacks */
79 	{ "IN1P", NULL, "Headset Mic" },
80 
81 	/* DMIC */
82 	{"DMic", NULL, "SoC DMIC"},
83 };
84 
85 static int cml_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
86 {
87 	struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
88 	struct snd_soc_component *component = rtd->codec_dai->component;
89 	struct snd_soc_jack *jack;
90 	int ret;
91 
92 	/* need to enable ASRC function for 24MHz mclk rate */
93 	rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER |
94 					RT5682_AD_STEREO1_FILTER,
95 					RT5682_CLK_SEL_I2S1_ASRC);
96 
97 	/*
98 	 * Headset buttons map to the google Reference headset.
99 	 * These can be configured by userspace.
100 	 */
101 	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
102 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
103 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
104 				    SND_JACK_BTN_3,
105 				    &ctx->headset, NULL, 0);
106 	if (ret) {
107 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
108 		return ret;
109 	}
110 
111 	jack = &ctx->headset;
112 
113 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
114 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
115 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
116 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
117 	ret = snd_soc_component_set_jack(component, jack, NULL);
118 	if (ret)
119 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
120 
121 	return ret;
122 };
123 
124 static int cml_rt5682_hw_params(struct snd_pcm_substream *substream,
125 				struct snd_pcm_hw_params *params)
126 {
127 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
128 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
129 	int clk_id, clk_freq, pll_out, ret;
130 
131 	clk_id = RT5682_PLL1_S_MCLK;
132 	clk_freq = CML_PLAT_CLK;
133 
134 	pll_out = params_rate(params) * 512;
135 
136 	ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
137 	if (ret < 0)
138 		dev_warn(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
139 
140 	/* Configure sysclk for codec */
141 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
142 				     pll_out, SND_SOC_CLOCK_IN);
143 	if (ret < 0)
144 		dev_warn(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
145 
146 	/*
147 	 * slot_width should be equal or large than data length, set them
148 	 * be the same
149 	 */
150 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
151 				       params_width(params));
152 	if (ret < 0)
153 		dev_warn(rtd->dev, "set TDM slot err:%d\n", ret);
154 	return ret;
155 }
156 
157 static int cml_rt1011_hw_params(struct snd_pcm_substream *substream,
158 				struct snd_pcm_hw_params *params)
159 {
160 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
161 	struct snd_soc_dai *codec_dai;
162 	struct snd_soc_card *card = rtd->card;
163 	int srate, i, ret = 0;
164 
165 	srate = params_rate(params);
166 
167 	for (i = 0; i < rtd->num_codecs; i++) {
168 		codec_dai = rtd->codec_dais[i];
169 
170 		/* 100 Fs to drive 24 bit data */
171 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1011_PLL1_S_BCLK,
172 					  100 * srate, 256 * srate);
173 		if (ret < 0) {
174 			dev_err(card->dev, "codec_dai clock not set\n");
175 			return ret;
176 		}
177 
178 		ret = snd_soc_dai_set_sysclk(codec_dai,
179 					     RT1011_FS_SYS_PRE_S_PLL1,
180 					     256 * srate, SND_SOC_CLOCK_IN);
181 		if (ret < 0) {
182 			dev_err(card->dev, "codec_dai clock not set\n");
183 			return ret;
184 		}
185 
186 		/*
187 		 * Codec TDM is configured as 24 bit capture/ playback.
188 		 * 2 CH PB is done over 4 codecs - 2 Woofers and 2 Tweeters.
189 		 * The Left woofer and tweeter plays the Left playback data
190 		 * and  similar by the Right.
191 		 * Hence 2 codecs (1 T and 1 W pair) share same Rx slot.
192 		 * The feedback is captured for each codec individually.
193 		 * Hence all 4 codecs use 1 Tx slot each for feedback.
194 		 */
195 		if (!strcmp(codec_dai->component->name, "i2c-10EC1011:00")) {
196 			ret = snd_soc_dai_set_tdm_slot(codec_dai,
197 						       0x4, 0x1, 4, 24);
198 			if (ret < 0)
199 				break;
200 		}
201 		if (!strcmp(codec_dai->component->name, "i2c-10EC1011:02")) {
202 			ret = snd_soc_dai_set_tdm_slot(codec_dai,
203 						       0x1, 0x1, 4, 24);
204 			if (ret < 0)
205 				break;
206 		}
207 		/* TDM Rx slot 2 is used for Right Woofer & Tweeters pair */
208 		if (!strcmp(codec_dai->component->name, "i2c-10EC1011:01")) {
209 			ret = snd_soc_dai_set_tdm_slot(codec_dai,
210 						       0x8, 0x2, 4, 24);
211 			if (ret < 0)
212 				break;
213 		}
214 		if (!strcmp(codec_dai->component->name, "i2c-10EC1011:03")) {
215 			ret = snd_soc_dai_set_tdm_slot(codec_dai,
216 						       0x2, 0x2, 4, 24);
217 			if (ret < 0)
218 				break;
219 		}
220 	}
221 	if (ret < 0)
222 		dev_err(rtd->dev,
223 			"set codec TDM slot for %s failed with error %d\n",
224 			codec_dai->component->name, ret);
225 	return ret;
226 }
227 
228 static struct snd_soc_ops cml_rt5682_ops = {
229 	.hw_params = cml_rt5682_hw_params,
230 };
231 
232 static const struct snd_soc_ops cml_rt1011_ops = {
233 	.hw_params = cml_rt1011_hw_params,
234 };
235 
236 static int sof_card_late_probe(struct snd_soc_card *card)
237 {
238 	struct card_private *ctx = snd_soc_card_get_drvdata(card);
239 	struct snd_soc_component *component = NULL;
240 	char jack_name[NAME_SIZE];
241 	struct hdmi_pcm *pcm;
242 	int ret, i = 0;
243 
244 	if (list_empty(&ctx->hdmi_pcm_list))
245 		return -EINVAL;
246 
247 	if (ctx->common_hdmi_codec_drv) {
248 		pcm = list_first_entry(&ctx->hdmi_pcm_list, struct hdmi_pcm,
249 				       head);
250 		component = pcm->codec_dai->component;
251 		return hda_dsp_hdmi_build_controls(card, component);
252 	}
253 
254 	list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
255 		component = pcm->codec_dai->component;
256 		snprintf(jack_name, sizeof(jack_name),
257 			 "HDMI/DP, pcm=%d Jack", pcm->device);
258 		ret = snd_soc_card_jack_new(card, jack_name,
259 					    SND_JACK_AVOUT, &hdmi_jack[i],
260 					    NULL, 0);
261 		if (ret)
262 			return ret;
263 
264 		ret = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
265 					  &hdmi_jack[i]);
266 		if (ret < 0)
267 			return ret;
268 
269 		i++;
270 	}
271 
272 	return hdac_hdmi_jack_port_init(component, &card->dapm);
273 }
274 
275 static int hdmi_init(struct snd_soc_pcm_runtime *rtd)
276 {
277 	struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
278 	struct snd_soc_dai *dai = rtd->codec_dai;
279 	struct hdmi_pcm *pcm;
280 
281 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
282 	if (!pcm)
283 		return -ENOMEM;
284 
285 	pcm->device = dai->id;
286 	pcm->codec_dai = dai;
287 
288 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
289 
290 	return 0;
291 }
292 
293 /* Cometlake digital audio interface glue - connects codec <--> CPU */
294 
295 SND_SOC_DAILINK_DEF(ssp0_pin,
296 	DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
297 SND_SOC_DAILINK_DEF(ssp0_codec,
298 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00",
299 				CML_RT5682_CODEC_DAI)));
300 
301 SND_SOC_DAILINK_DEF(ssp1_pin,
302 	DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
303 SND_SOC_DAILINK_DEF(ssp1_codec,
304 	DAILINK_COMP_ARRAY(
305 	/* WL */ COMP_CODEC("i2c-10EC1011:00", CML_RT1011_CODEC_DAI),
306 	/* WR */ COMP_CODEC("i2c-10EC1011:01", CML_RT1011_CODEC_DAI),
307 	/* TL */ COMP_CODEC("i2c-10EC1011:02", CML_RT1011_CODEC_DAI),
308 	/* TR */ COMP_CODEC("i2c-10EC1011:03", CML_RT1011_CODEC_DAI)));
309 
310 SND_SOC_DAILINK_DEF(dmic_pin,
311 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
312 
313 SND_SOC_DAILINK_DEF(dmic16k_pin,
314 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin")));
315 
316 SND_SOC_DAILINK_DEF(dmic_codec,
317 	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
318 
319 SND_SOC_DAILINK_DEF(idisp1_pin,
320 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
321 SND_SOC_DAILINK_DEF(idisp1_codec,
322 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
323 
324 SND_SOC_DAILINK_DEF(idisp2_pin,
325 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
326 SND_SOC_DAILINK_DEF(idisp2_codec,
327 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
328 
329 SND_SOC_DAILINK_DEF(idisp3_pin,
330 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
331 SND_SOC_DAILINK_DEF(idisp3_codec,
332 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
333 
334 SND_SOC_DAILINK_DEF(platform,
335 	DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
336 
337 static struct snd_soc_dai_link cml_rt1011_rt5682_dailink[] = {
338 	/* Back End DAI links */
339 	{
340 		/* SSP0 - Codec */
341 		.name = "SSP0-Codec",
342 		.id = 0,
343 		.init = cml_rt5682_codec_init,
344 		.ignore_pmdown_time = 1,
345 		.ops = &cml_rt5682_ops,
346 		.dpcm_playback = 1,
347 		.dpcm_capture = 1,
348 		.no_pcm = 1,
349 		SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
350 	},
351 	{
352 		.name = "dmic01",
353 		.id = 1,
354 		.ignore_suspend = 1,
355 		.dpcm_capture = 1,
356 		.no_pcm = 1,
357 		SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
358 	},
359 	{
360 		.name = "dmic16k",
361 		.id = 2,
362 		.ignore_suspend = 1,
363 		.dpcm_capture = 1,
364 		.no_pcm = 1,
365 		SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform),
366 	},
367 	{
368 		.name = "iDisp1",
369 		.id = 3,
370 		.init = hdmi_init,
371 		.dpcm_playback = 1,
372 		.no_pcm = 1,
373 		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
374 	},
375 	{
376 		.name = "iDisp2",
377 		.id = 4,
378 		.init = hdmi_init,
379 		.dpcm_playback = 1,
380 		.no_pcm = 1,
381 		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
382 	},
383 	{
384 		.name = "iDisp3",
385 		.id = 5,
386 		.init = hdmi_init,
387 		.dpcm_playback = 1,
388 		.no_pcm = 1,
389 		SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
390 	},
391 	{
392 		/*
393 		 * SSP1 - Codec : added to end of list ensuring
394 		 * reuse of common topologies for other end points
395 		 * and changing only SSP1's codec
396 		 */
397 		.name = "SSP1-Codec",
398 		.id = 6,
399 		.dpcm_playback = 1,
400 		.dpcm_capture = 1, /* Capture stream provides Feedback */
401 		.no_pcm = 1,
402 		.ops = &cml_rt1011_ops,
403 		SND_SOC_DAILINK_REG(ssp1_pin, ssp1_codec, platform),
404 	},
405 };
406 
407 static struct snd_soc_codec_conf rt1011_conf[] = {
408 	{
409 		.dlc = COMP_CODEC_CONF("i2c-10EC1011:00"),
410 		.name_prefix = "WL",
411 	},
412 	{
413 		.dlc = COMP_CODEC_CONF("i2c-10EC1011:01"),
414 		.name_prefix = "WR",
415 	},
416 	{
417 		.dlc = COMP_CODEC_CONF("i2c-10EC1011:02"),
418 		.name_prefix = "TL",
419 	},
420 	{
421 		.dlc = COMP_CODEC_CONF("i2c-10EC1011:03"),
422 		.name_prefix = "TR",
423 	},
424 };
425 
426 /* Cometlake audio machine driver for RT1011 and RT5682 */
427 static struct snd_soc_card snd_soc_card_cml = {
428 	.name = "cml_rt1011_rt5682",
429 	.dai_link = cml_rt1011_rt5682_dailink,
430 	.num_links = ARRAY_SIZE(cml_rt1011_rt5682_dailink),
431 	.codec_conf = rt1011_conf,
432 	.num_configs = ARRAY_SIZE(rt1011_conf),
433 	.dapm_widgets = cml_rt1011_rt5682_widgets,
434 	.num_dapm_widgets = ARRAY_SIZE(cml_rt1011_rt5682_widgets),
435 	.dapm_routes = cml_rt1011_rt5682_map,
436 	.num_dapm_routes = ARRAY_SIZE(cml_rt1011_rt5682_map),
437 	.controls = cml_controls,
438 	.num_controls = ARRAY_SIZE(cml_controls),
439 	.fully_routed = true,
440 	.late_probe = sof_card_late_probe,
441 };
442 
443 static int snd_cml_rt1011_probe(struct platform_device *pdev)
444 {
445 	struct card_private *ctx;
446 	struct snd_soc_acpi_mach *mach;
447 	const char *platform_name;
448 	int ret;
449 
450 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_ATOMIC);
451 	if (!ctx)
452 		return -ENOMEM;
453 
454 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
455 	mach = (&pdev->dev)->platform_data;
456 	snd_soc_card_cml.dev = &pdev->dev;
457 	platform_name = mach->mach_params.platform;
458 
459 	/* set platform name for each dailink */
460 	ret = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cml,
461 						    platform_name);
462 	if (ret)
463 		return ret;
464 
465 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
466 
467 	snd_soc_card_set_drvdata(&snd_soc_card_cml, ctx);
468 
469 	return devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cml);
470 }
471 
472 static struct platform_driver snd_cml_rt1011_rt5682_driver = {
473 	.probe = snd_cml_rt1011_probe,
474 	.driver = {
475 		.name = "cml_rt1011_rt5682",
476 		.pm = &snd_soc_pm_ops,
477 	},
478 };
479 module_platform_driver(snd_cml_rt1011_rt5682_driver);
480 
481 /* Module information */
482 MODULE_DESCRIPTION("Cometlake Audio Machine driver - RT1011 and RT5682 in I2S mode");
483 MODULE_AUTHOR("Naveen Manohar <naveen.m@intel.com>");
484 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
485 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
486 MODULE_LICENSE("GPL v2");
487 MODULE_ALIAS("platform:cml_rt1011_rt5682");
488