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 
27 #define NAME_SIZE 32
28 
29 #define SOF_CS42L42_SSP_CODEC(quirk)		((quirk) & GENMASK(2, 0))
30 #define SOF_CS42L42_SSP_CODEC_MASK		(GENMASK(2, 0))
31 #define SOF_SPEAKER_AMP_PRESENT			BIT(3)
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 #define SOF_MAX98357A_SPEAKER_AMP_PRESENT	BIT(29)
50 #define SOF_MAX98360A_SPEAKER_AMP_PRESENT	BIT(30)
51 
52 enum {
53 	LINK_NONE = 0,
54 	LINK_HP = 1,
55 	LINK_SPK = 2,
56 	LINK_DMIC = 3,
57 	LINK_HDMI = 4,
58 	LINK_BT = 5,
59 };
60 
61 static struct snd_soc_jack_pin jack_pins[] = {
62 	{
63 		.pin    = "Headphone Jack",
64 		.mask   = SND_JACK_HEADPHONE,
65 	},
66 	{
67 		.pin    = "Headset Mic",
68 		.mask   = SND_JACK_MICROPHONE,
69 	},
70 };
71 
72 /* Default: SSP2 */
73 static unsigned long sof_cs42l42_quirk = SOF_CS42L42_SSP_CODEC(2);
74 
75 struct sof_hdmi_pcm {
76 	struct list_head head;
77 	struct snd_soc_dai *codec_dai;
78 	struct snd_soc_jack hdmi_jack;
79 	int device;
80 };
81 
82 struct sof_card_private {
83 	struct snd_soc_jack headset_jack;
84 	struct list_head hdmi_pcm_list;
85 	bool common_hdmi_codec_drv;
86 };
87 
sof_hdmi_init(struct snd_soc_pcm_runtime * rtd)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 = asoc_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 
sof_cs42l42_init(struct snd_soc_pcm_runtime * rtd)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 = asoc_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 
sof_cs42l42_exit(struct snd_soc_pcm_runtime * rtd)144 static void sof_cs42l42_exit(struct snd_soc_pcm_runtime *rtd)
145 {
146 	struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
147 
148 	snd_soc_component_set_jack(component, NULL, NULL);
149 }
150 
sof_cs42l42_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)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 = asoc_substream_to_rtd(substream);
155 	struct snd_soc_dai *codec_dai = asoc_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 
sof_card_late_probe(struct snd_soc_card * card)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 
dmic_init(struct snd_soc_pcm_runtime * rtd)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 
create_spk_amp_dai_links(struct device * dev,struct snd_soc_dai_link * links,struct snd_soc_dai_link_component * cpus,int * id,int ssp_amp)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, int ssp_amp)
303 {
304 	int ret = 0;
305 
306 	/* speaker amp */
307 	if (!(sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT))
308 		return 0;
309 
310 	links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
311 					 ssp_amp);
312 	if (!links[*id].name) {
313 		ret = -ENOMEM;
314 		goto devm_err;
315 	}
316 
317 	links[*id].id = *id;
318 
319 	if (sof_cs42l42_quirk & SOF_MAX98357A_SPEAKER_AMP_PRESENT) {
320 		max_98357a_dai_link(&links[*id]);
321 	} else if (sof_cs42l42_quirk & SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
322 		max_98360a_dai_link(&links[*id]);
323 	} else {
324 		dev_err(dev, "no amp defined\n");
325 		ret = -EINVAL;
326 		goto devm_err;
327 	}
328 
329 	links[*id].platforms = platform_component;
330 	links[*id].num_platforms = ARRAY_SIZE(platform_component);
331 	links[*id].dpcm_playback = 1;
332 	/* firmware-generated echo reference */
333 	links[*id].dpcm_capture = 1;
334 
335 	links[*id].no_pcm = 1;
336 	links[*id].cpus = &cpus[*id];
337 	links[*id].num_cpus = 1;
338 
339 	links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
340 						   "SSP%d Pin", ssp_amp);
341 	if (!links[*id].cpus->dai_name) {
342 		ret = -ENOMEM;
343 		goto devm_err;
344 	}
345 
346 	(*id)++;
347 
348 devm_err:
349 	return ret;
350 }
351 
create_hp_codec_dai_links(struct device * dev,struct snd_soc_dai_link * links,struct snd_soc_dai_link_component * cpus,int * id,int ssp_codec)352 static int create_hp_codec_dai_links(struct device *dev,
353 				     struct snd_soc_dai_link *links,
354 				     struct snd_soc_dai_link_component *cpus,
355 				     int *id, int ssp_codec)
356 {
357 	/* codec SSP */
358 	links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec",
359 					 ssp_codec);
360 	if (!links[*id].name)
361 		goto devm_err;
362 
363 	links[*id].id = *id;
364 	links[*id].codecs = cs42l42_component;
365 	links[*id].num_codecs = ARRAY_SIZE(cs42l42_component);
366 	links[*id].platforms = platform_component;
367 	links[*id].num_platforms = ARRAY_SIZE(platform_component);
368 	links[*id].init = sof_cs42l42_init;
369 	links[*id].exit = sof_cs42l42_exit;
370 	links[*id].ops = &sof_cs42l42_ops;
371 	links[*id].dpcm_playback = 1;
372 	links[*id].dpcm_capture = 1;
373 	links[*id].no_pcm = 1;
374 	links[*id].cpus = &cpus[*id];
375 	links[*id].num_cpus = 1;
376 
377 	links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
378 						   "SSP%d Pin",
379 						   ssp_codec);
380 	if (!links[*id].cpus->dai_name)
381 		goto devm_err;
382 
383 	(*id)++;
384 
385 	return 0;
386 
387 devm_err:
388 	return -ENOMEM;
389 }
390 
create_dmic_dai_links(struct device * dev,struct snd_soc_dai_link * links,struct snd_soc_dai_link_component * cpus,int * id,int dmic_be_num)391 static int create_dmic_dai_links(struct device *dev,
392 				 struct snd_soc_dai_link *links,
393 				 struct snd_soc_dai_link_component *cpus,
394 				 int *id, int dmic_be_num)
395 {
396 	int i;
397 
398 	/* dmic */
399 	if (dmic_be_num <= 0)
400 		return 0;
401 
402 	/* at least we have dmic01 */
403 	links[*id].name = "dmic01";
404 	links[*id].cpus = &cpus[*id];
405 	links[*id].cpus->dai_name = "DMIC01 Pin";
406 	links[*id].init = dmic_init;
407 	if (dmic_be_num > 1) {
408 		/* set up 2 BE links at most */
409 		links[*id + 1].name = "dmic16k";
410 		links[*id + 1].cpus = &cpus[*id + 1];
411 		links[*id + 1].cpus->dai_name = "DMIC16k Pin";
412 		dmic_be_num = 2;
413 	}
414 
415 	for (i = 0; i < dmic_be_num; i++) {
416 		links[*id].id = *id;
417 		links[*id].num_cpus = 1;
418 		links[*id].codecs = dmic_component;
419 		links[*id].num_codecs = ARRAY_SIZE(dmic_component);
420 		links[*id].platforms = platform_component;
421 		links[*id].num_platforms = ARRAY_SIZE(platform_component);
422 		links[*id].ignore_suspend = 1;
423 		links[*id].dpcm_capture = 1;
424 		links[*id].no_pcm = 1;
425 
426 		(*id)++;
427 	}
428 
429 	return 0;
430 }
431 
create_hdmi_dai_links(struct device * dev,struct snd_soc_dai_link * links,struct snd_soc_dai_link_component * cpus,int * id,int hdmi_num)432 static int create_hdmi_dai_links(struct device *dev,
433 				 struct snd_soc_dai_link *links,
434 				 struct snd_soc_dai_link_component *cpus,
435 				 int *id, int hdmi_num)
436 {
437 	struct snd_soc_dai_link_component *idisp_components;
438 	int i;
439 
440 	/* HDMI */
441 	if (hdmi_num <= 0)
442 		return 0;
443 
444 	idisp_components = devm_kcalloc(dev,
445 					hdmi_num,
446 					sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
447 	if (!idisp_components)
448 		goto devm_err;
449 
450 	for (i = 1; i <= hdmi_num; i++) {
451 		links[*id].name = devm_kasprintf(dev, GFP_KERNEL,
452 						 "iDisp%d", i);
453 		if (!links[*id].name)
454 			goto devm_err;
455 
456 		links[*id].id = *id;
457 		links[*id].cpus = &cpus[*id];
458 		links[*id].num_cpus = 1;
459 		links[*id].cpus->dai_name = devm_kasprintf(dev,
460 							   GFP_KERNEL,
461 							   "iDisp%d Pin",
462 							   i);
463 		if (!links[*id].cpus->dai_name)
464 			goto devm_err;
465 
466 		idisp_components[i - 1].name = "ehdaudio0D2";
467 		idisp_components[i - 1].dai_name = devm_kasprintf(dev,
468 								  GFP_KERNEL,
469 								  "intel-hdmi-hifi%d",
470 								  i);
471 		if (!idisp_components[i - 1].dai_name)
472 			goto devm_err;
473 
474 		links[*id].codecs = &idisp_components[i - 1];
475 		links[*id].num_codecs = 1;
476 		links[*id].platforms = platform_component;
477 		links[*id].num_platforms = ARRAY_SIZE(platform_component);
478 		links[*id].init = sof_hdmi_init;
479 		links[*id].dpcm_playback = 1;
480 		links[*id].no_pcm = 1;
481 
482 		(*id)++;
483 	}
484 
485 	return 0;
486 
487 devm_err:
488 	return -ENOMEM;
489 }
490 
create_bt_offload_dai_links(struct device * dev,struct snd_soc_dai_link * links,struct snd_soc_dai_link_component * cpus,int * id,int ssp_bt)491 static int create_bt_offload_dai_links(struct device *dev,
492 				       struct snd_soc_dai_link *links,
493 				       struct snd_soc_dai_link_component *cpus,
494 				       int *id, int ssp_bt)
495 {
496 	/* bt offload */
497 	if (!(sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT))
498 		return 0;
499 
500 	links[*id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT",
501 					 ssp_bt);
502 	if (!links[*id].name)
503 		goto devm_err;
504 
505 	links[*id].id = *id;
506 	links[*id].codecs = &asoc_dummy_dlc;
507 	links[*id].num_codecs = 1;
508 	links[*id].platforms = platform_component;
509 	links[*id].num_platforms = ARRAY_SIZE(platform_component);
510 
511 	links[*id].dpcm_playback = 1;
512 	links[*id].dpcm_capture = 1;
513 	links[*id].no_pcm = 1;
514 	links[*id].cpus = &cpus[*id];
515 	links[*id].num_cpus = 1;
516 
517 	links[*id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
518 						   "SSP%d Pin",
519 						   ssp_bt);
520 	if (!links[*id].cpus->dai_name)
521 		goto devm_err;
522 
523 	(*id)++;
524 
525 	return 0;
526 
527 devm_err:
528 	return -ENOMEM;
529 }
530 
sof_card_dai_links_create(struct device * dev,int ssp_codec,int ssp_amp,int ssp_bt,int dmic_be_num,int hdmi_num)531 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
532 							  int ssp_codec,
533 							  int ssp_amp,
534 							  int ssp_bt,
535 							  int dmic_be_num,
536 							  int hdmi_num)
537 {
538 	struct snd_soc_dai_link_component *cpus;
539 	struct snd_soc_dai_link *links;
540 	int ret, id = 0, link_seq;
541 
542 	links = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
543 			    sizeof(struct snd_soc_dai_link), GFP_KERNEL);
544 	cpus = devm_kcalloc(dev, sof_audio_card_cs42l42.num_links,
545 			    sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
546 	if (!links || !cpus)
547 		goto devm_err;
548 
549 	link_seq = (sof_cs42l42_quirk & SOF_CS42L42_DAILINK_MASK) >> SOF_CS42L42_DAILINK_SHIFT;
550 
551 	while (link_seq) {
552 		int link_type = link_seq & 0x07;
553 
554 		switch (link_type) {
555 		case LINK_HP:
556 			ret = create_hp_codec_dai_links(dev, links, cpus, &id, ssp_codec);
557 			if (ret < 0) {
558 				dev_err(dev, "fail to create hp codec dai links, ret %d\n",
559 					ret);
560 				goto devm_err;
561 			}
562 			break;
563 		case LINK_SPK:
564 			ret = create_spk_amp_dai_links(dev, links, cpus, &id, ssp_amp);
565 			if (ret < 0) {
566 				dev_err(dev, "fail to create spk amp dai links, ret %d\n",
567 					ret);
568 				goto devm_err;
569 			}
570 			break;
571 		case LINK_DMIC:
572 			ret = create_dmic_dai_links(dev, links, cpus, &id, dmic_be_num);
573 			if (ret < 0) {
574 				dev_err(dev, "fail to create dmic dai links, ret %d\n",
575 					ret);
576 				goto devm_err;
577 			}
578 			break;
579 		case LINK_HDMI:
580 			ret = create_hdmi_dai_links(dev, links, cpus, &id, hdmi_num);
581 			if (ret < 0) {
582 				dev_err(dev, "fail to create hdmi dai links, ret %d\n",
583 					ret);
584 				goto devm_err;
585 			}
586 			break;
587 		case LINK_BT:
588 			ret = create_bt_offload_dai_links(dev, links, cpus, &id, ssp_bt);
589 			if (ret < 0) {
590 				dev_err(dev, "fail to create bt offload dai links, ret %d\n",
591 					ret);
592 				goto devm_err;
593 			}
594 			break;
595 		case LINK_NONE:
596 			/* caught here if it's not used as terminator in macro */
597 		default:
598 			dev_err(dev, "invalid link type %d\n", link_type);
599 			goto devm_err;
600 		}
601 
602 		link_seq >>= 3;
603 	}
604 
605 	return links;
606 devm_err:
607 	return NULL;
608 }
609 
sof_audio_probe(struct platform_device * pdev)610 static int sof_audio_probe(struct platform_device *pdev)
611 {
612 	struct snd_soc_dai_link *dai_links;
613 	struct snd_soc_acpi_mach *mach;
614 	struct sof_card_private *ctx;
615 	int dmic_be_num, hdmi_num;
616 	int ret, ssp_bt, ssp_amp, ssp_codec;
617 
618 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
619 	if (!ctx)
620 		return -ENOMEM;
621 
622 	if (pdev->id_entry && pdev->id_entry->driver_data)
623 		sof_cs42l42_quirk = (unsigned long)pdev->id_entry->driver_data;
624 
625 	mach = pdev->dev.platform_data;
626 
627 	if (soc_intel_is_glk()) {
628 		dmic_be_num = 1;
629 		hdmi_num = 3;
630 	} else {
631 		dmic_be_num = 2;
632 		hdmi_num = (sof_cs42l42_quirk & SOF_CS42L42_NUM_HDMIDEV_MASK) >>
633 			 SOF_CS42L42_NUM_HDMIDEV_SHIFT;
634 		/* default number of HDMI DAI's */
635 		if (!hdmi_num)
636 			hdmi_num = 3;
637 	}
638 
639 	dev_dbg(&pdev->dev, "sof_cs42l42_quirk = %lx\n", sof_cs42l42_quirk);
640 
641 	ssp_bt = (sof_cs42l42_quirk & SOF_CS42L42_SSP_BT_MASK) >>
642 			SOF_CS42L42_SSP_BT_SHIFT;
643 
644 	ssp_amp = (sof_cs42l42_quirk & SOF_CS42L42_SSP_AMP_MASK) >>
645 			SOF_CS42L42_SSP_AMP_SHIFT;
646 
647 	ssp_codec = sof_cs42l42_quirk & SOF_CS42L42_SSP_CODEC_MASK;
648 
649 	/* compute number of dai links */
650 	sof_audio_card_cs42l42.num_links = 1 + dmic_be_num + hdmi_num;
651 
652 	if (sof_cs42l42_quirk & SOF_SPEAKER_AMP_PRESENT)
653 		sof_audio_card_cs42l42.num_links++;
654 	if (sof_cs42l42_quirk & SOF_BT_OFFLOAD_PRESENT)
655 		sof_audio_card_cs42l42.num_links++;
656 
657 	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
658 					      ssp_bt, dmic_be_num, hdmi_num);
659 	if (!dai_links)
660 		return -ENOMEM;
661 
662 	sof_audio_card_cs42l42.dai_link = dai_links;
663 
664 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
665 
666 	sof_audio_card_cs42l42.dev = &pdev->dev;
667 
668 	/* set platform name for each dailink */
669 	ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_cs42l42,
670 						    mach->mach_params.platform);
671 	if (ret)
672 		return ret;
673 
674 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
675 
676 	snd_soc_card_set_drvdata(&sof_audio_card_cs42l42, ctx);
677 
678 	return devm_snd_soc_register_card(&pdev->dev,
679 					  &sof_audio_card_cs42l42);
680 }
681 
682 static const struct platform_device_id board_ids[] = {
683 	{
684 		.name = "glk_cs4242_mx98357a",
685 		.driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(2) |
686 					SOF_SPEAKER_AMP_PRESENT |
687 					SOF_MAX98357A_SPEAKER_AMP_PRESENT |
688 					SOF_CS42L42_SSP_AMP(1)) |
689 					SOF_CS42L42_DAILINK(LINK_SPK, LINK_HP, LINK_DMIC, LINK_HDMI, LINK_NONE),
690 	},
691 	{
692 		.name = "jsl_cs4242_mx98360a",
693 		.driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
694 					SOF_SPEAKER_AMP_PRESENT |
695 					SOF_MAX98360A_SPEAKER_AMP_PRESENT |
696 					SOF_CS42L42_SSP_AMP(1)) |
697 					SOF_CS42L42_DAILINK(LINK_HP, LINK_DMIC, LINK_HDMI, LINK_SPK, LINK_NONE),
698 	},
699 	{
700 		.name = "adl_mx98360a_cs4242",
701 		.driver_data = (kernel_ulong_t)(SOF_CS42L42_SSP_CODEC(0) |
702 				SOF_SPEAKER_AMP_PRESENT |
703 				SOF_MAX98360A_SPEAKER_AMP_PRESENT |
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