xref: /linux/sound/soc/intel/boards/sof_cs42l42.c (revision 52fee5c9158000db607d734383fd862969782de5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2021 Intel Corporation.
3 
4 /*
5  * Intel SOF Machine Driver with Cirrus Logic CS42L42 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/regulator/consumer.h>
13 #include <linux/dmi.h>
14 #include <sound/core.h>
15 #include <sound/jack.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/sof.h>
20 #include <sound/soc-acpi.h>
21 #include <dt-bindings/sound/cs42l42.h>
22 #include "../../codecs/hdac_hdmi.h"
23 #include "../common/soc-intel-quirks.h"
24 #include "hda_dsp_common.h"
25 #include "sof_maxim_common.h"
26 #include "sof_ssp_common.h"
27 
28 #define NAME_SIZE 32
29 
30 #define SOF_CS42L42_SSP_CODEC(quirk)		((quirk) & GENMASK(2, 0))
31 #define SOF_CS42L42_SSP_CODEC_MASK		(GENMASK(2, 0))
32 #define SOF_CS42L42_SSP_AMP_SHIFT		4
33 #define SOF_CS42L42_SSP_AMP_MASK		(GENMASK(6, 4))
34 #define SOF_CS42L42_SSP_AMP(quirk)	\
35 	(((quirk) << SOF_CS42L42_SSP_AMP_SHIFT) & SOF_CS42L42_SSP_AMP_MASK)
36 #define SOF_CS42L42_NUM_HDMIDEV_SHIFT		7
37 #define SOF_CS42L42_NUM_HDMIDEV_MASK		(GENMASK(9, 7))
38 #define SOF_CS42L42_NUM_HDMIDEV(quirk)	\
39 	(((quirk) << SOF_CS42L42_NUM_HDMIDEV_SHIFT) & SOF_CS42L42_NUM_HDMIDEV_MASK)
40 #define SOF_CS42L42_DAILINK_SHIFT		10
41 #define SOF_CS42L42_DAILINK_MASK		(GENMASK(24, 10))
42 #define SOF_CS42L42_DAILINK(link1, link2, link3, link4, link5) \
43 	((((link1) | ((link2) << 3) | ((link3) << 6) | ((link4) << 9) | ((link5) << 12)) << SOF_CS42L42_DAILINK_SHIFT) & SOF_CS42L42_DAILINK_MASK)
44 #define SOF_BT_OFFLOAD_PRESENT			BIT(25)
45 #define SOF_CS42L42_SSP_BT_SHIFT		26
46 #define SOF_CS42L42_SSP_BT_MASK			(GENMASK(28, 26))
47 #define SOF_CS42L42_SSP_BT(quirk)	\
48 	(((quirk) << SOF_CS42L42_SSP_BT_SHIFT) & SOF_CS42L42_SSP_BT_MASK)
49 
50 enum {
51 	LINK_NONE = 0,
52 	LINK_HP = 1,
53 	LINK_SPK = 2,
54 	LINK_DMIC = 3,
55 	LINK_HDMI = 4,
56 	LINK_BT = 5,
57 };
58 
59 static struct snd_soc_jack_pin jack_pins[] = {
60 	{
61 		.pin    = "Headphone Jack",
62 		.mask   = SND_JACK_HEADPHONE,
63 	},
64 	{
65 		.pin    = "Headset Mic",
66 		.mask   = SND_JACK_MICROPHONE,
67 	},
68 };
69 
70 /* Default: SSP2 */
71 static unsigned long sof_cs42l42_quirk = SOF_CS42L42_SSP_CODEC(2);
72 
73 struct sof_hdmi_pcm {
74 	struct list_head head;
75 	struct snd_soc_dai *codec_dai;
76 	struct snd_soc_jack hdmi_jack;
77 	int device;
78 };
79 
80 struct sof_card_private {
81 	struct snd_soc_jack headset_jack;
82 	struct list_head hdmi_pcm_list;
83 	bool common_hdmi_codec_drv;
84 	enum sof_ssp_codec codec_type;
85 	enum sof_ssp_codec amp_type;
86 };
87 
88 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
89 {
90 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
91 	struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);
92 	struct sof_hdmi_pcm *pcm;
93 
94 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
95 	if (!pcm)
96 		return -ENOMEM;
97 
98 	/* dai_link id is 1:1 mapped to the PCM device */
99 	pcm->device = rtd->dai_link->id;
100 	pcm->codec_dai = dai;
101 
102 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
103 
104 	return 0;
105 }
106 
107 static int sof_cs42l42_init(struct snd_soc_pcm_runtime *rtd)
108 {
109 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
110 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
111 	struct snd_soc_jack *jack = &ctx->headset_jack;
112 	int ret;
113 
114 	/*
115 	 * Headset buttons map to the google Reference headset.
116 	 * These can be configured by userspace.
117 	 */
118 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
119 					 SND_JACK_HEADSET | SND_JACK_BTN_0 |
120 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
121 					 SND_JACK_BTN_3,
122 					 jack,
123 					 jack_pins,
124 					 ARRAY_SIZE(jack_pins));
125 	if (ret) {
126 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
127 		return ret;
128 	}
129 
130 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
131 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
132 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
133 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
134 
135 	ret = snd_soc_component_set_jack(component, jack, NULL);
136 	if (ret) {
137 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
138 		return ret;
139 	}
140 
141 	return ret;
142 };
143 
144 static void sof_cs42l42_exit(struct snd_soc_pcm_runtime *rtd)
145 {
146 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
147 
148 	snd_soc_component_set_jack(component, NULL, NULL);
149 }
150 
151 static int sof_cs42l42_hw_params(struct snd_pcm_substream *substream,
152 				 struct snd_pcm_hw_params *params)
153 {
154 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
155 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
156 	int clk_freq, ret;
157 
158 	clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
159 
160 	if (clk_freq <= 0) {
161 		dev_err(rtd->dev, "get bclk freq failed: %d\n", clk_freq);
162 		return -EINVAL;
163 	}
164 
165 	/* Configure sysclk for codec */
166 	ret = snd_soc_dai_set_sysclk(codec_dai, 0,
167 				     clk_freq, SND_SOC_CLOCK_IN);
168 	if (ret < 0)
169 		dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
170 
171 	return ret;
172 }
173 
174 static const struct snd_soc_ops sof_cs42l42_ops = {
175 	.hw_params = sof_cs42l42_hw_params,
176 };
177 
178 static struct snd_soc_dai_link_component platform_component[] = {
179 	{
180 		/* name might be overridden during probe */
181 		.name = "0000:00:1f.3"
182 	}
183 };
184 
185 static int sof_card_late_probe(struct snd_soc_card *card)
186 {
187 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
188 	struct snd_soc_component *component = NULL;
189 	char jack_name[NAME_SIZE];
190 	struct sof_hdmi_pcm *pcm;
191 	int err;
192 
193 	if (list_empty(&ctx->hdmi_pcm_list))
194 		return -EINVAL;
195 
196 	if (ctx->common_hdmi_codec_drv) {
197 		pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
198 				       head);
199 		component = pcm->codec_dai->component;
200 		return hda_dsp_hdmi_build_controls(card, component);
201 	}
202 
203 	list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
204 		component = pcm->codec_dai->component;
205 		snprintf(jack_name, sizeof(jack_name),
206 			 "HDMI/DP, pcm=%d Jack", pcm->device);
207 		err = snd_soc_card_jack_new(card, jack_name,
208 					    SND_JACK_AVOUT, &pcm->hdmi_jack);
209 
210 		if (err)
211 			return err;
212 
213 		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
214 					  &pcm->hdmi_jack);
215 		if (err < 0)
216 			return err;
217 	}
218 
219 	return hdac_hdmi_jack_port_init(component, &card->dapm);
220 }
221 
222 static const struct snd_kcontrol_new sof_controls[] = {
223 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
224 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
225 };
226 
227 static const struct snd_soc_dapm_widget sof_widgets[] = {
228 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
229 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
230 };
231 
232 static const struct snd_soc_dapm_widget dmic_widgets[] = {
233 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
234 };
235 
236 static const struct snd_soc_dapm_route sof_map[] = {
237 	/* HP jack connectors - unknown if we have jack detection */
238 	{"Headphone Jack", NULL, "HP"},
239 
240 	/* other jacks */
241 	{"HS", NULL, "Headset Mic"},
242 };
243 
244 static const struct snd_soc_dapm_route dmic_map[] = {
245 	/* digital mics */
246 	{"DMic", NULL, "SoC DMIC"},
247 };
248 
249 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
250 {
251 	struct snd_soc_card *card = rtd->card;
252 	int ret;
253 
254 	ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
255 					ARRAY_SIZE(dmic_widgets));
256 	if (ret) {
257 		dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
258 		/* Don't need to add routes if widget addition failed */
259 		return ret;
260 	}
261 
262 	ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
263 				      ARRAY_SIZE(dmic_map));
264 
265 	if (ret)
266 		dev_err(card->dev, "DMic map addition failed: %d\n", ret);
267 
268 	return ret;
269 }
270 
271 /* sof audio machine driver for cs42l42 codec */
272 static struct snd_soc_card sof_audio_card_cs42l42 = {
273 	.name = "cs42l42", /* the sof- prefix is added by the core */
274 	.owner = THIS_MODULE,
275 	.controls = sof_controls,
276 	.num_controls = ARRAY_SIZE(sof_controls),
277 	.dapm_widgets = sof_widgets,
278 	.num_dapm_widgets = ARRAY_SIZE(sof_widgets),
279 	.dapm_routes = sof_map,
280 	.num_dapm_routes = ARRAY_SIZE(sof_map),
281 	.fully_routed = true,
282 	.late_probe = sof_card_late_probe,
283 };
284 
285 static struct snd_soc_dai_link_component cs42l42_component[] = {
286 	{
287 		.name = "i2c-10134242:00",
288 		.dai_name = "cs42l42",
289 	}
290 };
291 
292 static struct snd_soc_dai_link_component dmic_component[] = {
293 	{
294 		.name = "dmic-codec",
295 		.dai_name = "dmic-hifi",
296 	}
297 };
298 
299 static int create_spk_amp_dai_links(struct device *dev,
300 				    struct snd_soc_dai_link *links,
301 				    struct snd_soc_dai_link_component *cpus,
302 				    int *id, enum sof_ssp_codec amp_type,
303 				    int ssp_amp)
304 {
305 	int ret = 0;
306 
307 	/* speaker amp */
308 	if (amp_type == CODEC_NONE)
309 		return 0;
310 
311 	links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
312 					 ssp_amp);
313 	if (!links[*id].name) {
314 		ret = -ENOMEM;
315 		goto devm_err;
316 	}
317 
318 	links[*id].id = *id;
319 
320 	switch (amp_type) {
321 	case CODEC_MAX98357A:
322 		max_98357a_dai_link(&links[*id]);
323 		break;
324 	case CODEC_MAX98360A:
325 		max_98360a_dai_link(&links[*id]);
326 		break;
327 	default:
328 		dev_err(dev, "invalid amp type %d\n", amp_type);
329 		return -EINVAL;
330 	}
331 
332 	links[*id].platforms = platform_component;
333 	links[*id].num_platforms = ARRAY_SIZE(platform_component);
334 	links[*id].dpcm_playback = 1;
335 	/* firmware-generated echo reference */
336 	links[*id].dpcm_capture = 1;
337 
338 	links[*id].no_pcm = 1;
339 	links[*id].cpus = &cpus[*id];
340 	links[*id].num_cpus = 1;
341 
342 	links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
343 						   "SSP%d Pin", ssp_amp);
344 	if (!links[*id].cpus->dai_name) {
345 		ret = -ENOMEM;
346 		goto devm_err;
347 	}
348 
349 	(*id)++;
350 
351 devm_err:
352 	return ret;
353 }
354 
355 static int create_hp_codec_dai_links(struct device *dev,
356 				     struct snd_soc_dai_link *links,
357 				     struct snd_soc_dai_link_component *cpus,
358 				     int *id, int ssp_codec)
359 {
360 	/* codec SSP */
361 	links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
362 					 ssp_codec);
363 	if (!links[*id].name)
364 		goto devm_err;
365 
366 	links[*id].id = *id;
367 	links[*id].codecs = cs42l42_component;
368 	links[*id].num_codecs = ARRAY_SIZE(cs42l42_component);
369 	links[*id].platforms = platform_component;
370 	links[*id].num_platforms = ARRAY_SIZE(platform_component);
371 	links[*id].init = sof_cs42l42_init;
372 	links[*id].exit = sof_cs42l42_exit;
373 	links[*id].ops = &sof_cs42l42_ops;
374 	links[*id].dpcm_playback = 1;
375 	links[*id].dpcm_capture = 1;
376 	links[*id].no_pcm = 1;
377 	links[*id].cpus = &cpus[*id];
378 	links[*id].num_cpus = 1;
379 
380 	links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
381 						   "SSP%d Pin",
382 						   ssp_codec);
383 	if (!links[*id].cpus->dai_name)
384 		goto devm_err;
385 
386 	(*id)++;
387 
388 	return 0;
389 
390 devm_err:
391 	return -ENOMEM;
392 }
393 
394 static int create_dmic_dai_links(struct device *dev,
395 				 struct snd_soc_dai_link *links,
396 				 struct snd_soc_dai_link_component *cpus,
397 				 int *id, int dmic_be_num)
398 {
399 	int i;
400 
401 	/* dmic */
402 	if (dmic_be_num <= 0)
403 		return 0;
404 
405 	/* at least we have dmic01 */
406 	links[*id].name = "dmic01";
407 	links[*id].cpus = &cpus[*id];
408 	links[*id].cpus->dai_name = "DMIC01 Pin";
409 	links[*id].init = dmic_init;
410 	if (dmic_be_num > 1) {
411 		/* set up 2 BE links at most */
412 		links[*id + 1].name = "dmic16k";
413 		links[*id + 1].cpus = &cpus[*id + 1];
414 		links[*id + 1].cpus->dai_name = "DMIC16k Pin";
415 		dmic_be_num = 2;
416 	}
417 
418 	for (i = 0; i < dmic_be_num; i++) {
419 		links[*id].id = *id;
420 		links[*id].num_cpus = 1;
421 		links[*id].codecs = dmic_component;
422 		links[*id].num_codecs = ARRAY_SIZE(dmic_component);
423 		links[*id].platforms = platform_component;
424 		links[*id].num_platforms = ARRAY_SIZE(platform_component);
425 		links[*id].ignore_suspend = 1;
426 		links[*id].dpcm_capture = 1;
427 		links[*id].no_pcm = 1;
428 
429 		(*id)++;
430 	}
431 
432 	return 0;
433 }
434 
435 static int create_hdmi_dai_links(struct device *dev,
436 				 struct snd_soc_dai_link *links,
437 				 struct snd_soc_dai_link_component *cpus,
438 				 int *id, int hdmi_num)
439 {
440 	struct snd_soc_dai_link_component *idisp_components;
441 	int i;
442 
443 	/* HDMI */
444 	if (hdmi_num <= 0)
445 		return 0;
446 
447 	idisp_components = devm_kcalloc(dev,
448 					hdmi_num,
449 					sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
450 	if (!idisp_components)
451 		goto devm_err;
452 
453 	for (i = 1; i <= hdmi_num; i++) {
454 		links[*id].name = devm_kasprintf(dev, GFP_KERNEL,
455 						 "iDisp%d", i);
456 		if (!links[*id].name)
457 			goto devm_err;
458 
459 		links[*id].id = *id;
460 		links[*id].cpus = &cpus[*id];
461 		links[*id].num_cpus = 1;
462 		links[*id].cpus->dai_name = devm_kasprintf(dev,
463 							   GFP_KERNEL,
464 							   "iDisp%d Pin",
465 							   i);
466 		if (!links[*id].cpus->dai_name)
467 			goto devm_err;
468 
469 		idisp_components[i - 1].name = "ehdaudio0D2";
470 		idisp_components[i - 1].dai_name = devm_kasprintf(dev,
471 								  GFP_KERNEL,
472 								  "intel-hdmi-hifi%d",
473 								  i);
474 		if (!idisp_components[i - 1].dai_name)
475 			goto devm_err;
476 
477 		links[*id].codecs = &idisp_components[i - 1];
478 		links[*id].num_codecs = 1;
479 		links[*id].platforms = platform_component;
480 		links[*id].num_platforms = ARRAY_SIZE(platform_component);
481 		links[*id].init = sof_hdmi_init;
482 		links[*id].dpcm_playback = 1;
483 		links[*id].no_pcm = 1;
484 
485 		(*id)++;
486 	}
487 
488 	return 0;
489 
490 devm_err:
491 	return -ENOMEM;
492 }
493 
494 static int create_bt_offload_dai_links(struct device *dev,
495 				       struct snd_soc_dai_link *links,
496 				       struct snd_soc_dai_link_component *cpus,
497 				       int *id, int ssp_bt)
498 {
499 	/* bt offload */
500 	if (!(sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT))
501 		return 0;
502 
503 	links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT",
504 					 ssp_bt);
505 	if (!links[*id].name)
506 		goto devm_err;
507 
508 	links[*id].id = *id;
509 	links[*id].codecs = &snd_soc_dummy_dlc;
510 	links[*id].num_codecs = 1;
511 	links[*id].platforms = platform_component;
512 	links[*id].num_platforms = ARRAY_SIZE(platform_component);
513 
514 	links[*id].dpcm_playback = 1;
515 	links[*id].dpcm_capture = 1;
516 	links[*id].no_pcm = 1;
517 	links[*id].cpus = &cpus[*id];
518 	links[*id].num_cpus = 1;
519 
520 	links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
521 						   "SSP%d Pin",
522 						   ssp_bt);
523 	if (!links[*id].cpus->dai_name)
524 		goto devm_err;
525 
526 	(*id)++;
527 
528 	return 0;
529 
530 devm_err:
531 	return -ENOMEM;
532 }
533 
534 static struct snd_soc_dai_link *
535 sof_card_dai_links_create(struct device *dev, enum sof_ssp_codec amp_type,
536 			  int ssp_codec, int ssp_amp, int ssp_bt,
537 			  int dmic_be_num, int hdmi_num)
538 {
539 	struct snd_soc_dai_link_component *cpus;
540 	struct snd_soc_dai_link *links;
541 	int ret, id = 0, link_seq;
542 
543 	links = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
544 			    sizeof(struct snd_soc_dai_link), GFP_KERNEL);
545 	cpus = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
546 			    sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
547 	if (!links || !cpus)
548 		goto devm_err;
549 
550 	link_seq = (sof_cs42l42_quirk & SOF_CS42L42_DAILINK_MASK) >> SOF_CS42L42_DAILINK_SHIFT;
551 
552 	while (link_seq) {
553 		int link_type = link_seq & 0x07;
554 
555 		switch (link_type) {
556 		case LINK_HP:
557 			ret = create_hp_codec_dai_links(dev, links, cpus, &id, ssp_codec);
558 			if (ret < 0) {
559 				dev_err(dev, "fail to create hp codec dai links, ret %d\n",
560 					ret);
561 				goto devm_err;
562 			}
563 			break;
564 		case LINK_SPK:
565 			ret = create_spk_amp_dai_links(dev, links, cpus, &id,
566 						       amp_type, ssp_amp);
567 			if (ret < 0) {
568 				dev_err(dev, "fail to create spk amp dai links, ret %d\n",
569 					ret);
570 				goto devm_err;
571 			}
572 			break;
573 		case LINK_DMIC:
574 			ret = create_dmic_dai_links(dev, links, cpus, &id, dmic_be_num);
575 			if (ret < 0) {
576 				dev_err(dev, "fail to create dmic dai links, ret %d\n",
577 					ret);
578 				goto devm_err;
579 			}
580 			break;
581 		case LINK_HDMI:
582 			ret = create_hdmi_dai_links(dev, links, cpus, &id, hdmi_num);
583 			if (ret < 0) {
584 				dev_err(dev, "fail to create hdmi dai links, ret %d\n",
585 					ret);
586 				goto devm_err;
587 			}
588 			break;
589 		case LINK_BT:
590 			ret = create_bt_offload_dai_links(dev, links, cpus, &id, ssp_bt);
591 			if (ret < 0) {
592 				dev_err(dev, "fail to create bt offload dai links, ret %d\n",
593 					ret);
594 				goto devm_err;
595 			}
596 			break;
597 		case LINK_NONE:
598 			/* caught here if it's not used as terminator in macro */
599 		default:
600 			dev_err(dev, "invalid link type %d\n", link_type);
601 			goto devm_err;
602 		}
603 
604 		link_seq >>= 3;
605 	}
606 
607 	return links;
608 devm_err:
609 	return NULL;
610 }
611 
612 static int sof_audio_probe(struct platform_device *pdev)
613 {
614 	struct snd_soc_dai_link *dai_links;
615 	struct snd_soc_acpi_mach *mach;
616 	struct sof_card_private *ctx;
617 	int dmic_be_num, hdmi_num;
618 	int ret, ssp_bt, ssp_amp, ssp_codec;
619 
620 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
621 	if (!ctx)
622 		return -ENOMEM;
623 
624 	if (pdev->id_entry && pdev->id_entry->driver_data)
625 		sof_cs42l42_quirk = (unsigned long)pdev->id_entry->driver_data;
626 
627 	mach = pdev->dev.platform_data;
628 
629 	ctx->codec_type = sof_ssp_detect_codec_type(&pdev->dev);
630 	ctx->amp_type = sof_ssp_detect_amp_type(&pdev->dev);
631 
632 	if (soc_intel_is_glk()) {
633 		dmic_be_num = 1;
634 		hdmi_num = 3;
635 	} else {
636 		dmic_be_num = 2;
637 		hdmi_num = (sof_cs42l42_quirk & SOF_CS42L42_NUM_HDMIDEV_MASK) >>
638 			 SOF_CS42L42_NUM_HDMIDEV_SHIFT;
639 		/* default number of HDMI DAI's */
640 		if (!hdmi_num)
641 			hdmi_num = 3;
642 	}
643 
644 	dev_dbg(&pdev->dev, "sof_cs42l42_quirk = %lx\n", sof_cs42l42_quirk);
645 
646 	ssp_bt = (sof_cs42l42_quirk & SOF_CS42L42_SSP_BT_MASK) >>
647 			SOF_CS42L42_SSP_BT_SHIFT;
648 
649 	ssp_amp = (sof_cs42l42_quirk & SOF_CS42L42_SSP_AMP_MASK) >>
650 			SOF_CS42L42_SSP_AMP_SHIFT;
651 
652 	ssp_codec = sof_cs42l42_quirk & SOF_CS42L42_SSP_CODEC_MASK;
653 
654 	/* compute number of dai links */
655 	sof_audio_card_cs42l42.num_links = 1 + dmic_be_num + hdmi_num;
656 
657 	if (ctx->amp_type != CODEC_NONE)
658 		sof_audio_card_cs42l42.num_links++;
659 	if (sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT)
660 		sof_audio_card_cs42l42.num_links++;
661 
662 	dai_links = sof_card_dai_links_create(&pdev->dev, ctx->amp_type,
663 					      ssp_codec, ssp_amp, ssp_bt,
664 					      dmic_be_num, hdmi_num);
665 	if (!dai_links)
666 		return -ENOMEM;
667 
668 	sof_audio_card_cs42l42.dai_link = dai_links;
669 
670 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
671 
672 	sof_audio_card_cs42l42.dev = &pdev->dev;
673 
674 	/* set platform name for each dailink */
675 	ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_cs42l42,
676 						    mach->mach_params.platform);
677 	if (ret)
678 		return ret;
679 
680 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
681 
682 	snd_soc_card_set_drvdata(&sof_audio_card_cs42l42, ctx);
683 
684 	return devm_snd_soc_register_card(&pdev->dev,
685 					  &sof_audio_card_cs42l42);
686 }
687 
688 static const struct platform_device_id board_ids[] = {
689 	{
690 		.name = "glk_cs4242_mx98357a",
691 		.driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(2) |
692 					SOF_CS42L42_SSP_AMP(1)) |
693 					SOF_CS42L42_DAILINK(LINK_SPK, LINK_HP, LINK_DMIC, LINK_HDMI, LINK_NONE),
694 	},
695 	{
696 		.name = "jsl_cs4242_mx98360a",
697 		.driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
698 					SOF_CS42L42_SSP_AMP(1)) |
699 					SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_NONE),
700 	},
701 	{
702 		.name = "adl_mx98360a_cs4242",
703 		.driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
704 				SOF_CS42L42_SSP_AMP(1) |
705 				SOF_CS42L42_NUM_HDMIDEV(4) |
706 				SOF_BT_OFFLOAD_PRESENT |
707 				SOF_CS42L42_SSP_BT(2) |
708 				SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_BT)),
709 	},
710 	{ }
711 };
712 MODULE_DEVICE_TABLE(platform, board_ids);
713 
714 static struct platform_driver sof_audio = {
715 	.probe = sof_audio_probe,
716 	.driver = {
717 		.name = "sof_cs42l42",
718 		.pm = &snd_soc_pm_ops,
719 	},
720 	.id_table = board_ids,
721 };
722 module_platform_driver(sof_audio)
723 
724 /* Module information */
725 MODULE_DESCRIPTION("SOF Audio Machine driver for CS42L42");
726 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
727 MODULE_LICENSE("GPL");
728 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
729 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
730 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_SSP_COMMON);
731