xref: /linux/sound/soc/intel/boards/sof_da7219.c (revision ebc733e54a1a79ea2dde2ba5121ae73a188e20d4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2019 Intel Corporation.
3 
4 /*
5  * Intel SOF Machine driver for Dialog headphone codec
6  */
7 
8 #include <linux/input.h>
9 #include <linux/module.h>
10 #include <sound/jack.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <linux/platform_device.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include <sound/sof.h>
17 #include "../../codecs/da7219.h"
18 #include "hda_dsp_common.h"
19 #include "sof_hdmi_common.h"
20 #include "sof_maxim_common.h"
21 #include "sof_ssp_common.h"
22 
23 /* Board Quirks */
24 #define SOF_DA7219_JSL_BOARD			BIT(2)
25 
26 #define DIALOG_CODEC_DAI	"da7219-hifi"
27 
28 struct card_private {
29 	struct snd_soc_jack headset_jack;
30 	struct sof_hdmi_private hdmi;
31 	enum sof_ssp_codec codec_type;
32 	enum sof_ssp_codec amp_type;
33 
34 	unsigned int pll_bypass:1;
35 };
36 
37 static int platform_clock_control(struct snd_soc_dapm_widget *w,
38 				  struct snd_kcontrol *k, int  event)
39 {
40 	struct snd_soc_dapm_context *dapm = w->dapm;
41 	struct snd_soc_card *card = dapm->card;
42 	struct card_private *ctx = snd_soc_card_get_drvdata(card);
43 	struct snd_soc_dai *codec_dai;
44 	int ret = 0;
45 
46 	if (ctx->pll_bypass)
47 		return ret;
48 
49 	/* PLL SRM mode */
50 	codec_dai = snd_soc_card_get_codec_dai(card, DIALOG_CODEC_DAI);
51 	if (!codec_dai) {
52 		dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n");
53 		return -EIO;
54 	}
55 
56 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
57 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK,
58 					  0, 0);
59 		if (ret)
60 			dev_err(card->dev, "failed to stop PLL: %d\n", ret);
61 	} else if (SND_SOC_DAPM_EVENT_ON(event)) {
62 		dev_dbg(card->dev, "pll srm mode\n");
63 
64 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
65 					  0, DA7219_PLL_FREQ_OUT_98304);
66 		if (ret)
67 			dev_err(card->dev, "failed to start PLL: %d\n", ret);
68 	}
69 
70 	return ret;
71 }
72 
73 static const struct snd_kcontrol_new controls[] = {
74 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
75 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
76 	SOC_DAPM_PIN_SWITCH("Line Out"),
77 	SOC_DAPM_PIN_SWITCH("Left Spk"),
78 	SOC_DAPM_PIN_SWITCH("Right Spk"),
79 };
80 
81 static const struct snd_soc_dapm_widget widgets[] = {
82 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
83 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
84 	SND_SOC_DAPM_LINE("Line Out", NULL),
85 
86 	SND_SOC_DAPM_SPK("Left Spk", NULL),
87 	SND_SOC_DAPM_SPK("Right Spk", NULL),
88 
89 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
90 			    platform_clock_control, SND_SOC_DAPM_POST_PMD |
91 			    SND_SOC_DAPM_PRE_PMU),
92 
93 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
94 };
95 
96 static const struct snd_soc_dapm_route audio_map[] = {
97 	{ "Headphone Jack", NULL, "HPL" },
98 	{ "Headphone Jack", NULL, "HPR" },
99 
100 	{ "MIC", NULL, "Headset Mic" },
101 
102 	{ "Headphone Jack", NULL, "Platform Clock" },
103 	{ "Headset Mic", NULL, "Platform Clock" },
104 	{ "Line Out", NULL, "Platform Clock" },
105 
106 	/* digital mics */
107 	{"DMic", NULL, "SoC DMIC"},
108 };
109 
110 static struct snd_soc_jack_pin jack_pins[] = {
111 	{
112 		.pin    = "Headphone Jack",
113 		.mask   = SND_JACK_HEADPHONE,
114 	},
115 	{
116 		.pin    = "Headset Mic",
117 		.mask   = SND_JACK_MICROPHONE,
118 	},
119 	{
120 		.pin    = "Line Out",
121 		.mask   = SND_JACK_LINEOUT,
122 	},
123 };
124 
125 static int da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
126 {
127 	struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
128 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
129 	struct snd_soc_component *component = codec_dai->component;
130 	struct snd_soc_jack *jack = &ctx->headset_jack;
131 	int mclk_rate, ret;
132 
133 	mclk_rate = sof_dai_get_mclk(rtd);
134 	if (mclk_rate <= 0) {
135 		dev_err(rtd->dev, "invalid mclk freq %d\n", mclk_rate);
136 		return -EINVAL;
137 	}
138 
139 	ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, mclk_rate,
140 				     SND_SOC_CLOCK_IN);
141 	if (ret) {
142 		dev_err(rtd->dev, "fail to set sysclk, ret %d\n", ret);
143 		return ret;
144 	}
145 
146 	/*
147 	 * Use PLL bypass mode if MCLK is available, be sure to set the
148 	 * frequency of MCLK to 12.288 or 24.576MHz on topology side.
149 	 */
150 	if (mclk_rate == 12288000 || mclk_rate == 24576000) {
151 		/* PLL bypass mode */
152 		dev_dbg(rtd->dev, "pll bypass mode, mclk rate %d\n", mclk_rate);
153 
154 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
155 		if (ret) {
156 			dev_err(rtd->dev, "fail to set pll, ret %d\n", ret);
157 			return ret;
158 		}
159 
160 		ctx->pll_bypass = 1;
161 	}
162 
163 	/*
164 	 * Headset buttons map to the google Reference headset.
165 	 * These can be configured by userspace.
166 	 */
167 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
168 					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
169 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
170 					 SND_JACK_BTN_3 | SND_JACK_LINEOUT,
171 					 jack, jack_pins, ARRAY_SIZE(jack_pins));
172 	if (ret) {
173 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
174 		return ret;
175 	}
176 
177 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
178 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
179 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
180 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
181 
182 	ret = snd_soc_component_set_jack(component, jack, NULL);
183 	if (ret) {
184 		dev_err(rtd->dev, "fail to set component jack, ret %d\n", ret);
185 		return ret;
186 	}
187 
188 	return ret;
189 }
190 
191 static int max98373_hw_params(struct snd_pcm_substream *substream,
192 			      struct snd_pcm_hw_params *params)
193 {
194 	struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
195 	int ret, j;
196 
197 	for (j = 0; j < runtime->dai_link->num_codecs; j++) {
198 		struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, j);
199 
200 		if (!strcmp(codec_dai->component->name, MAX_98373_DEV0_NAME)) {
201 			/* vmon_slot_no = 0 imon_slot_no = 1 for TX slots */
202 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 3, 4, 16);
203 			if (ret < 0) {
204 				dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
205 				return ret;
206 			}
207 		}
208 		if (!strcmp(codec_dai->component->name, MAX_98373_DEV1_NAME)) {
209 			/* vmon_slot_no = 2 imon_slot_no = 3 for TX slots */
210 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC, 3, 4, 16);
211 			if (ret < 0) {
212 				dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
213 				return ret;
214 			}
215 		}
216 	}
217 
218 	return 0;
219 }
220 
221 static const struct snd_soc_ops max98373_ops = {
222 	.hw_params = max98373_hw_params,
223 };
224 
225 static int hdmi_init(struct snd_soc_pcm_runtime *rtd)
226 {
227 	struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
228 	struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);
229 
230 	ctx->hdmi.hdmi_comp = dai->component;
231 
232 	return 0;
233 }
234 
235 static int card_late_probe(struct snd_soc_card *card)
236 {
237 	struct card_private *ctx = snd_soc_card_get_drvdata(card);
238 
239 	if (!ctx->hdmi.idisp_codec)
240 		return 0;
241 
242 	if (!ctx->hdmi.hdmi_comp)
243 		return -EINVAL;
244 
245 	return hda_dsp_hdmi_build_controls(card, ctx->hdmi.hdmi_comp);
246 }
247 
248 SND_SOC_DAILINK_DEF(ssp0_pin,
249 	DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
250 SND_SOC_DAILINK_DEF(ssp0_codec,
251 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7219:00", DIALOG_CODEC_DAI)));
252 
253 SND_SOC_DAILINK_DEF(ssp1_pin,
254 	DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
255 
256 SND_SOC_DAILINK_DEF(ssp2_pin,
257 	DAILINK_COMP_ARRAY(COMP_CPU("SSP2 Pin")));
258 SND_SOC_DAILINK_DEF(dummy_codec,
259 	DAILINK_COMP_ARRAY(COMP_CODEC("snd-soc-dummy", "snd-soc-dummy-dai")));
260 
261 SND_SOC_DAILINK_DEF(dmic_pin,
262 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
263 SND_SOC_DAILINK_DEF(dmic_codec,
264 	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
265 
266 SND_SOC_DAILINK_DEF(dmic16k_pin,
267 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin")));
268 
269 SND_SOC_DAILINK_DEF(idisp1_pin,
270 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
271 SND_SOC_DAILINK_DEF(idisp1_codec,
272 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
273 
274 SND_SOC_DAILINK_DEF(idisp2_pin,
275 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
276 SND_SOC_DAILINK_DEF(idisp2_codec,
277 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
278 
279 SND_SOC_DAILINK_DEF(idisp3_pin,
280 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
281 SND_SOC_DAILINK_DEF(idisp3_codec,
282 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
283 
284 SND_SOC_DAILINK_DEF(idisp4_pin,
285 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp4 Pin")));
286 SND_SOC_DAILINK_DEF(idisp4_codec,
287 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi4")));
288 
289 SND_SOC_DAILINK_DEF(platform, /* subject to be overridden during probe */
290 	DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
291 
292 static struct snd_soc_dai_link jsl_dais[] = {
293 	/* Back End DAI links */
294 	{
295 		.name = "SSP1-Codec",
296 		.id = 0,
297 		.ignore_pmdown_time = 1,
298 		.no_pcm = 1,
299 		.dpcm_playback = 1,
300 		.dpcm_capture = 1, /* IV feedback */
301 		SND_SOC_DAILINK_REG(ssp1_pin, max_98373_components, platform),
302 	},
303 	{
304 		.name = "SSP0-Codec",
305 		.id = 1,
306 		.no_pcm = 1,
307 		.init = da7219_codec_init,
308 		.ignore_pmdown_time = 1,
309 		.dpcm_playback = 1,
310 		.dpcm_capture = 1,
311 		SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
312 	},
313 	{
314 		.name = "dmic01",
315 		.id = 2,
316 		.ignore_suspend = 1,
317 		.dpcm_capture = 1,
318 		.no_pcm = 1,
319 		SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
320 	},
321 	{
322 		.name = "iDisp1",
323 		.id = 3,
324 		.init = hdmi_init,
325 		.dpcm_playback = 1,
326 		.no_pcm = 1,
327 		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
328 	},
329 	{
330 		.name = "iDisp2",
331 		.id = 4,
332 		.init = hdmi_init,
333 		.dpcm_playback = 1,
334 		.no_pcm = 1,
335 		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
336 	},
337 	{
338 		.name = "iDisp3",
339 		.id = 5,
340 		.init = hdmi_init,
341 		.dpcm_playback = 1,
342 		.no_pcm = 1,
343 		SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
344 	},
345 	{
346 		.name = "dmic16k",
347 		.id = 6,
348 		.ignore_suspend = 1,
349 		.dpcm_capture = 1,
350 		.no_pcm = 1,
351 		SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform),
352 	}
353 };
354 
355 static struct snd_soc_dai_link adl_dais[] = {
356 	/* Back End DAI links */
357 	{
358 		.name = "SSP0-Codec",
359 		.id = 0,
360 		.no_pcm = 1,
361 		.init = da7219_codec_init,
362 		.ignore_pmdown_time = 1,
363 		.dpcm_playback = 1,
364 		.dpcm_capture = 1,
365 		SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
366 	},
367 	{
368 		.name = "dmic01",
369 		.id = 1,
370 		.ignore_suspend = 1,
371 		.dpcm_capture = 1,
372 		.no_pcm = 1,
373 		SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
374 	},
375 	{
376 		.name = "dmic16k",
377 		.id = 2,
378 		.ignore_suspend = 1,
379 		.dpcm_capture = 1,
380 		.no_pcm = 1,
381 		SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform),
382 	},
383 	{
384 		.name = "iDisp1",
385 		.id = 3,
386 		.init = hdmi_init,
387 		.dpcm_playback = 1,
388 		.no_pcm = 1,
389 		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
390 	},
391 	{
392 		.name = "iDisp2",
393 		.id = 4,
394 		.init = hdmi_init,
395 		.dpcm_playback = 1,
396 		.no_pcm = 1,
397 		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
398 	},
399 	{
400 		.name = "iDisp3",
401 		.id = 5,
402 		.init = hdmi_init,
403 		.dpcm_playback = 1,
404 		.no_pcm = 1,
405 		SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
406 	},
407 	{
408 		.name = "iDisp4",
409 		.id = 6,
410 		.init = hdmi_init,
411 		.dpcm_playback = 1,
412 		.no_pcm = 1,
413 		SND_SOC_DAILINK_REG(idisp4_pin, idisp4_codec, platform),
414 	},
415 	{
416 		.name = "SSP1-Codec",
417 		.id = 7,
418 		.no_pcm = 1,
419 		.dpcm_playback = 1,
420 		/* feedback stream or firmware-generated echo reference */
421 		.dpcm_capture = 1,
422 		SND_SOC_DAILINK_REG(ssp1_pin, max_98373_components, platform),
423 	},
424 	{
425 		.name = "SSP2-BT",
426 		.id = 8,
427 		.no_pcm = 1,
428 		.dpcm_playback = 1,
429 		.dpcm_capture = 1,
430 		SND_SOC_DAILINK_REG(ssp2_pin, dummy_codec, platform),
431 	},
432 };
433 
434 static struct snd_soc_card card_da7219 = {
435 	.name = "da7219", /* the sof- prefix is added by the core */
436 	.owner = THIS_MODULE,
437 	.controls = controls,
438 	.num_controls = ARRAY_SIZE(controls),
439 	.dapm_widgets = widgets,
440 	.num_dapm_widgets = ARRAY_SIZE(widgets),
441 	.dapm_routes = audio_map,
442 	.num_dapm_routes = ARRAY_SIZE(audio_map),
443 	.fully_routed = true,
444 	.late_probe = card_late_probe,
445 };
446 
447 static int audio_probe(struct platform_device *pdev)
448 {
449 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
450 	struct snd_soc_dai_link *dai_links;
451 	struct card_private *ctx;
452 	unsigned long board_quirk = 0;
453 	int ret, amp_idx;
454 
455 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
456 	if (!ctx)
457 		return -ENOMEM;
458 
459 	if (pdev->id_entry && pdev->id_entry->driver_data)
460 		board_quirk = (unsigned long)pdev->id_entry->driver_data;
461 
462 	ctx->codec_type = sof_ssp_detect_codec_type(&pdev->dev);
463 	ctx->amp_type = sof_ssp_detect_amp_type(&pdev->dev);
464 
465 	if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
466 		ctx->hdmi.idisp_codec = true;
467 
468 	if (board_quirk & SOF_DA7219_JSL_BOARD) {
469 		/* backward-compatible with existing devices */
470 		switch (ctx->amp_type) {
471 		case CODEC_MAX98360A:
472 			card_da7219.name = devm_kstrdup(&pdev->dev,
473 							"da7219max98360a",
474 							GFP_KERNEL);
475 			break;
476 		case CODEC_MAX98373:
477 			card_da7219.name = devm_kstrdup(&pdev->dev, "da7219max",
478 							GFP_KERNEL);
479 			break;
480 		default:
481 			break;
482 		}
483 
484 		dai_links = jsl_dais;
485 		amp_idx = 0;
486 
487 		card_da7219.num_links = ARRAY_SIZE(jsl_dais);
488 	} else {
489 		dai_links = adl_dais;
490 		amp_idx = 7;
491 
492 		card_da7219.num_links = ARRAY_SIZE(adl_dais);
493 	}
494 
495 	dev_dbg(&pdev->dev, "board_quirk = %lx\n", board_quirk);
496 
497 	/* speaker amp */
498 	switch (ctx->amp_type) {
499 	case CODEC_MAX98360A:
500 		max_98360a_dai_link(&dai_links[amp_idx]);
501 		break;
502 	case CODEC_MAX98373:
503 		dai_links[amp_idx].codecs = max_98373_components;
504 		dai_links[amp_idx].num_codecs = ARRAY_SIZE(max_98373_components);
505 		dai_links[amp_idx].init = max_98373_spk_codec_init;
506 		if (board_quirk & SOF_DA7219_JSL_BOARD) {
507 			dai_links[amp_idx].ops = &max98373_ops; /* use local ops */
508 		} else {
509 			/* TBD: implement the amp for later platform */
510 			dev_err(&pdev->dev, "max98373 not support yet\n");
511 			return -EINVAL;
512 		}
513 
514 		max_98373_set_codec_conf(&card_da7219);
515 		break;
516 	default:
517 		dev_err(&pdev->dev, "invalid amp type %d\n", ctx->amp_type);
518 		return -EINVAL;
519 	}
520 
521 	card_da7219.dai_link = dai_links;
522 
523 	card_da7219.dev = &pdev->dev;
524 
525 	ret = snd_soc_fixup_dai_links_platform_name(&card_da7219,
526 						    mach->mach_params.platform);
527 	if (ret)
528 		return ret;
529 
530 	snd_soc_card_set_drvdata(&card_da7219, ctx);
531 
532 	return devm_snd_soc_register_card(&pdev->dev, &card_da7219);
533 }
534 
535 static const struct platform_device_id board_ids[] = {
536 	{
537 		.name = "jsl_mx98373_da7219",
538 		.driver_data = (kernel_ulong_t)(SOF_DA7219_JSL_BOARD),
539 	},
540 	{
541 		.name = "jsl_mx98360_da7219",
542 		.driver_data = (kernel_ulong_t)(SOF_DA7219_JSL_BOARD),
543 	},
544 	{
545 		.name = "adl_mx98360_da7219",
546 		/* no quirk needed for this board */
547 	},
548 	{ }
549 };
550 MODULE_DEVICE_TABLE(platform, board_ids);
551 
552 static struct platform_driver audio = {
553 	.probe = audio_probe,
554 	.driver = {
555 		.name = "sof_da7219",
556 		.pm = &snd_soc_pm_ops,
557 	},
558 	.id_table = board_ids,
559 };
560 module_platform_driver(audio)
561 
562 /* Module information */
563 MODULE_DESCRIPTION("ASoC Intel(R) SOF Machine driver for Dialog codec");
564 MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>");
565 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
566 MODULE_LICENSE("GPL v2");
567 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
568 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
569 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_SSP_COMMON);
570