1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2019 Intel Corporation.
3 
4 /*
5  * Intel SOF Machine Driver with Realtek rt5682 Codec
6  * and speaker codec MAX98357A
7  */
8 #include <linux/i2c.h>
9 #include <linux/input.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/dmi.h>
13 #include <asm/cpu_device_id.h>
14 #include <asm/intel-family.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/rt5682.h"
23 #include "../../codecs/hdac_hdmi.h"
24 
25 #define NAME_SIZE 32
26 
27 #define SOF_RT5682_SSP_CODEC(quirk)		((quirk) & GENMASK(2, 0))
28 #define SOF_RT5682_SSP_CODEC_MASK			(GENMASK(2, 0))
29 #define SOF_RT5682_MCLK_EN			BIT(3)
30 #define SOF_RT5682_MCLK_24MHZ			BIT(4)
31 #define SOF_SPEAKER_AMP_PRESENT		BIT(5)
32 #define SOF_RT5682_SSP_AMP_SHIFT		6
33 #define SOF_RT5682_SSP_AMP_MASK                 (GENMASK(8, 6))
34 #define SOF_RT5682_SSP_AMP(quirk)	\
35 	(((quirk) << SOF_RT5682_SSP_AMP_SHIFT) & SOF_RT5682_SSP_AMP_MASK)
36 
37 /* Default: MCLK on, MCLK 19.2M, SSP0  */
38 static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
39 					SOF_RT5682_SSP_CODEC(0);
40 
41 static int is_legacy_cpu;
42 
43 static struct snd_soc_jack sof_hdmi[3];
44 
45 struct sof_hdmi_pcm {
46 	struct list_head head;
47 	struct snd_soc_dai *codec_dai;
48 	int device;
49 };
50 
51 struct sof_card_private {
52 	struct snd_soc_jack sof_headset;
53 	struct list_head hdmi_pcm_list;
54 };
55 
56 static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
57 {
58 	sof_rt5682_quirk = (unsigned long)id->driver_data;
59 	return 1;
60 }
61 
62 static const struct dmi_system_id sof_rt5682_quirk_table[] = {
63 	{
64 		.callback = sof_rt5682_quirk_cb,
65 		.matches = {
66 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
67 			DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
68 		},
69 		.driver_data = (void *)(SOF_RT5682_MCLK_EN |
70 					SOF_RT5682_MCLK_24MHZ |
71 					SOF_RT5682_SSP_CODEC(1)),
72 	},
73 	{
74 		.callback = sof_rt5682_quirk_cb,
75 		.matches = {
76 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
77 			DMI_MATCH(DMI_PRODUCT_NAME, "Hatch"),
78 		},
79 		.driver_data = (void *)(SOF_RT5682_MCLK_EN |
80 					SOF_RT5682_MCLK_24MHZ |
81 					SOF_RT5682_SSP_CODEC(0) |
82 					SOF_SPEAKER_AMP_PRESENT |
83 					SOF_RT5682_SSP_AMP(1)),
84 	},
85 	{
86 		.callback = sof_rt5682_quirk_cb,
87 		.matches = {
88 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
89 			DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
90 		},
91 		.driver_data = (void *)(SOF_RT5682_MCLK_EN |
92 					SOF_RT5682_SSP_CODEC(0)),
93 	},
94 	{}
95 };
96 
97 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
98 {
99 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
100 	struct snd_soc_dai *dai = rtd->codec_dai;
101 	struct sof_hdmi_pcm *pcm;
102 
103 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
104 	if (!pcm)
105 		return -ENOMEM;
106 
107 	/* dai_link id is 1:1 mapped to the PCM device */
108 	pcm->device = rtd->dai_link->id;
109 	pcm->codec_dai = dai;
110 
111 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
112 
113 	return 0;
114 }
115 
116 static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
117 {
118 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
119 	struct snd_soc_component *component = rtd->codec_dai->component;
120 	struct snd_soc_jack *jack;
121 	int ret;
122 
123 	/* need to enable ASRC function for 24MHz mclk rate */
124 	if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
125 	    (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
126 		rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER |
127 					RT5682_AD_STEREO1_FILTER,
128 					RT5682_CLK_SEL_I2S1_ASRC);
129 	}
130 
131 	/*
132 	 * Headset buttons map to the google Reference headset.
133 	 * These can be configured by userspace.
134 	 */
135 	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
136 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
137 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
138 				    SND_JACK_BTN_3,
139 				    &ctx->sof_headset, NULL, 0);
140 	if (ret) {
141 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
142 		return ret;
143 	}
144 
145 	jack = &ctx->sof_headset;
146 
147 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
148 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
149 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
150 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
151 	ret = snd_soc_component_set_jack(component, jack, NULL);
152 
153 	if (ret) {
154 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
155 		return ret;
156 	}
157 
158 	return ret;
159 };
160 
161 static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
162 				struct snd_pcm_hw_params *params)
163 {
164 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
165 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
166 	int clk_id, clk_freq, pll_out, ret;
167 
168 	if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
169 		clk_id = RT5682_PLL1_S_MCLK;
170 		if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)
171 			clk_freq = 24000000;
172 		else
173 			clk_freq = 19200000;
174 	} else {
175 		clk_id = RT5682_PLL1_S_BCLK1;
176 		clk_freq = params_rate(params) * 50;
177 	}
178 
179 	pll_out = params_rate(params) * 512;
180 
181 	ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
182 	if (ret < 0)
183 		dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
184 
185 	/* Configure sysclk for codec */
186 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
187 				     pll_out, SND_SOC_CLOCK_IN);
188 	if (ret < 0)
189 		dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
190 
191 	/*
192 	 * slot_width should equal or large than data length, set them
193 	 * be the same
194 	 */
195 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
196 				       params_width(params));
197 	if (ret < 0) {
198 		dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
199 		return ret;
200 	}
201 
202 	return ret;
203 }
204 
205 static struct snd_soc_ops sof_rt5682_ops = {
206 	.hw_params = sof_rt5682_hw_params,
207 };
208 
209 static struct snd_soc_dai_link_component platform_component[] = {
210 	{
211 		/* name might be overridden during probe */
212 		.name = "0000:00:1f.3"
213 	}
214 };
215 
216 static int sof_card_late_probe(struct snd_soc_card *card)
217 {
218 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
219 	struct snd_soc_component *component = NULL;
220 	char jack_name[NAME_SIZE];
221 	struct sof_hdmi_pcm *pcm;
222 	int err = 0;
223 	int i = 0;
224 
225 	/* HDMI is not supported by SOF on Baytrail/CherryTrail */
226 	if (is_legacy_cpu)
227 		return 0;
228 
229 	list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
230 		component = pcm->codec_dai->component;
231 		snprintf(jack_name, sizeof(jack_name),
232 			 "HDMI/DP, pcm=%d Jack", pcm->device);
233 		err = snd_soc_card_jack_new(card, jack_name,
234 					    SND_JACK_AVOUT, &sof_hdmi[i],
235 					    NULL, 0);
236 
237 		if (err)
238 			return err;
239 
240 		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
241 					  &sof_hdmi[i]);
242 		if (err < 0)
243 			return err;
244 
245 		i++;
246 	}
247 	if (!component)
248 		return -EINVAL;
249 
250 	return hdac_hdmi_jack_port_init(component, &card->dapm);
251 }
252 
253 static const struct snd_kcontrol_new sof_controls[] = {
254 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
255 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
256 	SOC_DAPM_PIN_SWITCH("Spk"),
257 };
258 
259 static const struct snd_soc_dapm_widget sof_widgets[] = {
260 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
261 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
262 	SND_SOC_DAPM_SPK("Spk", NULL),
263 };
264 
265 static const struct snd_soc_dapm_route sof_map[] = {
266 	/* HP jack connectors - unknown if we have jack detection */
267 	{ "Headphone Jack", NULL, "HPOL" },
268 	{ "Headphone Jack", NULL, "HPOR" },
269 
270 	/* other jacks */
271 	{ "IN1P", NULL, "Headset Mic" },
272 
273 };
274 
275 static const struct snd_soc_dapm_route speaker_map[] = {
276 	/* speaker */
277 	{ "Spk", NULL, "Speaker" },
278 };
279 
280 static int speaker_codec_init(struct snd_soc_pcm_runtime *rtd)
281 {
282 	struct snd_soc_card *card = rtd->card;
283 	int ret;
284 
285 	ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map,
286 				      ARRAY_SIZE(speaker_map));
287 
288 	if (ret)
289 		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
290 	return ret;
291 }
292 
293 /* sof audio machine driver for rt5682 codec */
294 static struct snd_soc_card sof_audio_card_rt5682 = {
295 	.name = "sof_rt5682",
296 	.owner = THIS_MODULE,
297 	.controls = sof_controls,
298 	.num_controls = ARRAY_SIZE(sof_controls),
299 	.dapm_widgets = sof_widgets,
300 	.num_dapm_widgets = ARRAY_SIZE(sof_widgets),
301 	.dapm_routes = sof_map,
302 	.num_dapm_routes = ARRAY_SIZE(sof_map),
303 	.fully_routed = true,
304 	.late_probe = sof_card_late_probe,
305 };
306 
307 static const struct x86_cpu_id legacy_cpi_ids[] = {
308 	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT }, /* Baytrail */
309 	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, /* Cherrytrail */
310 	{}
311 };
312 
313 static struct snd_soc_dai_link_component rt5682_component[] = {
314 	{
315 		.name = "i2c-10EC5682:00",
316 		.dai_name = "rt5682-aif1",
317 	}
318 };
319 
320 static struct snd_soc_dai_link_component dmic_component[] = {
321 	{
322 		.name = "dmic-codec",
323 		.dai_name = "dmic-hifi",
324 	}
325 };
326 
327 static struct snd_soc_dai_link_component max98357a_component[] = {
328 	{
329 		.name = "MX98357A:00",
330 		.dai_name = "HiFi",
331 	}
332 };
333 
334 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
335 							  int ssp_codec,
336 							  int ssp_amp,
337 							  int dmic_num,
338 							  int hdmi_num)
339 {
340 	struct snd_soc_dai_link_component *idisp_components;
341 	struct snd_soc_dai_link *links;
342 	int i, id = 0;
343 
344 	links = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link) *
345 			     sof_audio_card_rt5682.num_links, GFP_KERNEL);
346 	if (!links)
347 		goto devm_err;
348 
349 	/* codec SSP */
350 	links[id].name = devm_kasprintf(dev, GFP_KERNEL,
351 					"SSP%d-Codec", ssp_codec);
352 	if (!links[id].name)
353 		goto devm_err;
354 
355 	links[id].id = id;
356 	links[id].codecs = rt5682_component;
357 	links[id].num_codecs = ARRAY_SIZE(rt5682_component);
358 	links[id].platforms = platform_component;
359 	links[id].num_platforms = ARRAY_SIZE(platform_component);
360 	links[id].init = sof_rt5682_codec_init;
361 	links[id].ops = &sof_rt5682_ops;
362 	links[id].nonatomic = true;
363 	links[id].dpcm_playback = 1;
364 	links[id].dpcm_capture = 1;
365 	links[id].no_pcm = 1;
366 	if (is_legacy_cpu) {
367 		links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
368 							"ssp%d-port",
369 							ssp_codec);
370 		if (!links[id].cpu_dai_name)
371 			goto devm_err;
372 	} else {
373 		/*
374 		 * Currently, On SKL+ platforms MCLK will be turned off in sof
375 		 * runtime suspended, and it will go into runtime suspended
376 		 * right after playback is stop. However, rt5682 will output
377 		 * static noise if sysclk turns off during playback. Set
378 		 * ignore_pmdown_time to power down rt5682 immediately and
379 		 * avoid the noise.
380 		 * It can be removed once we can control MCLK by driver.
381 		 */
382 		links[id].ignore_pmdown_time = 1;
383 		links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
384 							"SSP%d Pin",
385 							ssp_codec);
386 		if (!links[id].cpu_dai_name)
387 			goto devm_err;
388 	}
389 	id++;
390 
391 	/* dmic */
392 	for (i = 1; i <= dmic_num; i++) {
393 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
394 						"dmic%02d", i);
395 		if (!links[id].name)
396 			goto devm_err;
397 
398 		links[id].id = id;
399 		links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
400 							"DMIC%02d Pin", i);
401 		if (!links[id].cpu_dai_name)
402 			goto devm_err;
403 
404 		links[id].codecs = dmic_component;
405 		links[id].num_codecs = ARRAY_SIZE(dmic_component);
406 		links[id].platforms = platform_component;
407 		links[id].num_platforms = ARRAY_SIZE(platform_component);
408 		links[id].ignore_suspend = 1;
409 		links[id].dpcm_capture = 1;
410 		links[id].no_pcm = 1;
411 		id++;
412 	}
413 
414 	/* HDMI */
415 	if (hdmi_num > 0) {
416 		idisp_components = devm_kzalloc(dev,
417 				   sizeof(struct snd_soc_dai_link_component) *
418 				   hdmi_num, GFP_KERNEL);
419 		if (!idisp_components)
420 			goto devm_err;
421 	}
422 	for (i = 1; i <= hdmi_num; i++) {
423 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
424 						"iDisp%d", i);
425 		if (!links[id].name)
426 			goto devm_err;
427 
428 		links[id].id = id;
429 		links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
430 							"iDisp%d Pin", i);
431 		if (!links[id].cpu_dai_name)
432 			goto devm_err;
433 
434 		idisp_components[i - 1].name = "ehdaudio0D2";
435 		idisp_components[i - 1].dai_name = devm_kasprintf(dev,
436 								  GFP_KERNEL,
437 								  "intel-hdmi-hifi%d",
438 								  i);
439 		if (!idisp_components[i - 1].dai_name)
440 			goto devm_err;
441 
442 		links[id].codecs = &idisp_components[i - 1];
443 		links[id].num_codecs = 1;
444 		links[id].platforms = platform_component;
445 		links[id].num_platforms = ARRAY_SIZE(platform_component);
446 		links[id].init = sof_hdmi_init;
447 		links[id].dpcm_playback = 1;
448 		links[id].no_pcm = 1;
449 		id++;
450 	}
451 
452 	/* speaker amp */
453 	if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
454 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
455 						"SSP%d-Codec", ssp_amp);
456 		if (!links[id].name)
457 			goto devm_err;
458 
459 		links[id].id = id;
460 		links[id].codecs = max98357a_component;
461 		links[id].num_codecs = ARRAY_SIZE(max98357a_component);
462 		links[id].platforms = platform_component;
463 		links[id].num_platforms = ARRAY_SIZE(platform_component);
464 		links[id].init = speaker_codec_init,
465 		links[id].nonatomic = true;
466 		links[id].dpcm_playback = 1;
467 		links[id].no_pcm = 1;
468 		if (is_legacy_cpu) {
469 			links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
470 								"ssp%d-port",
471 								ssp_amp);
472 			if (!links[id].cpu_dai_name)
473 				goto devm_err;
474 
475 		} else {
476 			links[id].cpu_dai_name = devm_kasprintf(dev, GFP_KERNEL,
477 								"SSP%d Pin",
478 								ssp_amp);
479 			if (!links[id].cpu_dai_name)
480 				goto devm_err;
481 		}
482 	}
483 
484 	return links;
485 devm_err:
486 	return NULL;
487 }
488 
489 static int sof_audio_probe(struct platform_device *pdev)
490 {
491 	struct snd_soc_dai_link *dai_links;
492 	struct snd_soc_acpi_mach *mach;
493 	struct sof_card_private *ctx;
494 	int dmic_num, hdmi_num;
495 	int ret, ssp_amp, ssp_codec;
496 
497 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_ATOMIC);
498 	if (!ctx)
499 		return -ENOMEM;
500 
501 	if (x86_match_cpu(legacy_cpi_ids)) {
502 		is_legacy_cpu = 1;
503 		dmic_num = 0;
504 		hdmi_num = 0;
505 		/* default quirk for legacy cpu */
506 		sof_rt5682_quirk = SOF_RT5682_SSP_CODEC(2);
507 	} else {
508 		dmic_num = 1;
509 		hdmi_num = 3;
510 	}
511 
512 	dmi_check_system(sof_rt5682_quirk_table);
513 
514 	dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
515 
516 	ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
517 			SOF_RT5682_SSP_AMP_SHIFT;
518 
519 	ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
520 
521 	/* compute number of dai links */
522 	sof_audio_card_rt5682.num_links = 1 + dmic_num + hdmi_num;
523 	if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
524 		sof_audio_card_rt5682.num_links++;
525 
526 	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
527 					      dmic_num, hdmi_num);
528 	if (!dai_links)
529 		return -ENOMEM;
530 
531 	sof_audio_card_rt5682.dai_link = dai_links;
532 
533 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
534 
535 	sof_audio_card_rt5682.dev = &pdev->dev;
536 	mach = (&pdev->dev)->platform_data;
537 
538 	/* set platform name for each dailink */
539 	ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
540 						    mach->mach_params.platform);
541 	if (ret)
542 		return ret;
543 
544 	snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
545 
546 	return devm_snd_soc_register_card(&pdev->dev,
547 					  &sof_audio_card_rt5682);
548 }
549 
550 static struct platform_driver sof_audio = {
551 	.probe = sof_audio_probe,
552 	.driver = {
553 		.name = "sof_rt5682",
554 		.pm = &snd_soc_pm_ops,
555 	},
556 };
557 module_platform_driver(sof_audio)
558 
559 /* Module information */
560 MODULE_DESCRIPTION("SOF Audio Machine driver");
561 MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
562 MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
563 MODULE_LICENSE("GPL v2");
564 MODULE_ALIAS("platform:sof_rt5682");
565