1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2021 Intel Corporation.
3 
4 /*
5  * Intel SOF Machine Driver with es8336 Codec
6  */
7 
8 #include <linux/device.h>
9 #include <linux/dmi.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/gpio/machine.h>
12 #include <linux/i2c.h>
13 #include <linux/input.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <sound/jack.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-acpi.h>
22 #include "hda_dsp_common.h"
23 
24 /* jd-inv + terminating entry */
25 #define MAX_NO_PROPS 2
26 
27 #define SOF_ES8336_SSP_CODEC(quirk)		((quirk) & GENMASK(3, 0))
28 #define SOF_ES8336_SSP_CODEC_MASK		(GENMASK(3, 0))
29 
30 #define SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK	BIT(4)
31 
32 /* HDMI capture*/
33 #define SOF_SSP_HDMI_CAPTURE_PRESENT		BIT(14)
34 #define SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT		15
35 #define SOF_NO_OF_HDMI_CAPTURE_SSP_MASK		(GENMASK(16, 15))
36 #define SOF_NO_OF_HDMI_CAPTURE_SSP(quirk)	\
37 	(((quirk) << SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT) & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK)
38 
39 #define SOF_HDMI_CAPTURE_1_SSP_SHIFT		7
40 #define SOF_HDMI_CAPTURE_1_SSP_MASK		(GENMASK(9, 7))
41 #define SOF_HDMI_CAPTURE_1_SSP(quirk)	\
42 	(((quirk) << SOF_HDMI_CAPTURE_1_SSP_SHIFT) & SOF_HDMI_CAPTURE_1_SSP_MASK)
43 
44 #define SOF_HDMI_CAPTURE_2_SSP_SHIFT		10
45 #define SOF_HDMI_CAPTURE_2_SSP_MASK		(GENMASK(12, 10))
46 #define SOF_HDMI_CAPTURE_2_SSP(quirk)	\
47 	(((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK)
48 
49 #define SOF_ES8336_ENABLE_DMIC			BIT(5)
50 #define SOF_ES8336_JD_INVERTED			BIT(6)
51 #define SOF_ES8336_HEADPHONE_GPIO		BIT(7)
52 #define SOC_ES8336_HEADSET_MIC1			BIT(8)
53 
54 static unsigned long quirk;
55 
56 static int quirk_override = -1;
57 module_param_named(quirk, quirk_override, int, 0444);
58 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
59 
60 struct sof_es8336_private {
61 	struct device *codec_dev;
62 	struct gpio_desc *gpio_speakers, *gpio_headphone;
63 	struct snd_soc_jack jack;
64 	struct list_head hdmi_pcm_list;
65 	bool speaker_en;
66 };
67 
68 struct sof_hdmi_pcm {
69 	struct list_head head;
70 	struct snd_soc_dai *codec_dai;
71 	int device;
72 };
73 
74 static const struct acpi_gpio_params enable_gpio0 = { 0, 0, true };
75 static const struct acpi_gpio_params enable_gpio1 = { 1, 0, true };
76 
77 static const struct acpi_gpio_mapping acpi_speakers_enable_gpio0[] = {
78 	{ "speakers-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
79 	{ }
80 };
81 
82 static const struct acpi_gpio_mapping acpi_speakers_enable_gpio1[] = {
83 	{ "speakers-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
84 };
85 
86 static const struct acpi_gpio_mapping acpi_enable_both_gpios[] = {
87 	{ "speakers-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
88 	{ "headphone-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
89 	{ }
90 };
91 
92 static const struct acpi_gpio_mapping acpi_enable_both_gpios_rev_order[] = {
93 	{ "speakers-enable-gpios", &enable_gpio1, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
94 	{ "headphone-enable-gpios", &enable_gpio0, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
95 	{ }
96 };
97 
98 static void log_quirks(struct device *dev)
99 {
100 	dev_info(dev, "quirk mask %#lx\n", quirk);
101 	dev_info(dev, "quirk SSP%ld\n",  SOF_ES8336_SSP_CODEC(quirk));
102 	if (quirk & SOF_ES8336_ENABLE_DMIC)
103 		dev_info(dev, "quirk DMIC enabled\n");
104 	if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
105 		dev_info(dev, "Speakers GPIO1 quirk enabled\n");
106 	if (quirk & SOF_ES8336_HEADPHONE_GPIO)
107 		dev_info(dev, "quirk headphone GPIO enabled\n");
108 	if (quirk & SOF_ES8336_JD_INVERTED)
109 		dev_info(dev, "quirk JD inverted enabled\n");
110 	if (quirk & SOC_ES8336_HEADSET_MIC1)
111 		dev_info(dev, "quirk headset at mic1 port enabled\n");
112 }
113 
114 static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
115 					  struct snd_kcontrol *kcontrol, int event)
116 {
117 	struct snd_soc_card *card = w->dapm->card;
118 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
119 
120 	if (priv->speaker_en == !SND_SOC_DAPM_EVENT_ON(event))
121 		return 0;
122 
123 	priv->speaker_en = !SND_SOC_DAPM_EVENT_ON(event);
124 
125 	if (SND_SOC_DAPM_EVENT_ON(event))
126 		msleep(70);
127 
128 	gpiod_set_value_cansleep(priv->gpio_speakers, priv->speaker_en);
129 
130 	if (!(quirk & SOF_ES8336_HEADPHONE_GPIO))
131 		return 0;
132 
133 	if (SND_SOC_DAPM_EVENT_ON(event))
134 		msleep(70);
135 
136 	gpiod_set_value_cansleep(priv->gpio_headphone, priv->speaker_en);
137 
138 	return 0;
139 }
140 
141 static const struct snd_soc_dapm_widget sof_es8316_widgets[] = {
142 	SND_SOC_DAPM_SPK("Speaker", NULL),
143 	SND_SOC_DAPM_HP("Headphone", NULL),
144 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
145 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
146 
147 	SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
148 			    sof_es8316_speaker_power_event,
149 			    SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
150 };
151 
152 static const struct snd_soc_dapm_widget dmic_widgets[] = {
153 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
154 };
155 
156 static const struct snd_soc_dapm_route sof_es8316_audio_map[] = {
157 	{"Headphone", NULL, "HPOL"},
158 	{"Headphone", NULL, "HPOR"},
159 
160 	/*
161 	 * There is no separate speaker output instead the speakers are muxed to
162 	 * the HP outputs. The mux is controlled Speaker and/or headphone switch.
163 	 */
164 	{"Speaker", NULL, "HPOL"},
165 	{"Speaker", NULL, "HPOR"},
166 	{"Speaker", NULL, "Speaker Power"},
167 };
168 
169 static const struct snd_soc_dapm_route sof_es8316_headset_mic2_map[] = {
170 	{"MIC1", NULL, "Internal Mic"},
171 	{"MIC2", NULL, "Headset Mic"},
172 };
173 
174 static const struct snd_soc_dapm_route sof_es8316_headset_mic1_map[] = {
175 	{"MIC2", NULL, "Internal Mic"},
176 	{"MIC1", NULL, "Headset Mic"},
177 };
178 
179 static const struct snd_soc_dapm_route dmic_map[] = {
180 	/* digital mics */
181 	{"DMic", NULL, "SoC DMIC"},
182 };
183 
184 static const struct snd_kcontrol_new sof_es8316_controls[] = {
185 	SOC_DAPM_PIN_SWITCH("Speaker"),
186 	SOC_DAPM_PIN_SWITCH("Headphone"),
187 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
188 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
189 };
190 
191 static struct snd_soc_jack_pin sof_es8316_jack_pins[] = {
192 	{
193 		.pin	= "Headphone",
194 		.mask	= SND_JACK_HEADPHONE,
195 	},
196 	{
197 		.pin	= "Headset Mic",
198 		.mask	= SND_JACK_MICROPHONE,
199 	},
200 };
201 
202 static int dmic_init(struct snd_soc_pcm_runtime *runtime)
203 {
204 	struct snd_soc_card *card = runtime->card;
205 	int ret;
206 
207 	ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
208 					ARRAY_SIZE(dmic_widgets));
209 	if (ret) {
210 		dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
211 		return ret;
212 	}
213 
214 	ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
215 				      ARRAY_SIZE(dmic_map));
216 	if (ret)
217 		dev_err(card->dev, "DMic map addition failed: %d\n", ret);
218 
219 	return ret;
220 }
221 
222 static int sof_hdmi_init(struct snd_soc_pcm_runtime *runtime)
223 {
224 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(runtime->card);
225 	struct snd_soc_dai *dai = asoc_rtd_to_codec(runtime, 0);
226 	struct sof_hdmi_pcm *pcm;
227 
228 	pcm = devm_kzalloc(runtime->card->dev, sizeof(*pcm), GFP_KERNEL);
229 	if (!pcm)
230 		return -ENOMEM;
231 
232 	/* dai_link id is 1:1 mapped to the PCM device */
233 	pcm->device = runtime->dai_link->id;
234 	pcm->codec_dai = dai;
235 
236 	list_add_tail(&pcm->head, &priv->hdmi_pcm_list);
237 
238 	return 0;
239 }
240 
241 static int sof_es8316_init(struct snd_soc_pcm_runtime *runtime)
242 {
243 	struct snd_soc_component *codec = asoc_rtd_to_codec(runtime, 0)->component;
244 	struct snd_soc_card *card = runtime->card;
245 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
246 	const struct snd_soc_dapm_route *custom_map;
247 	int num_routes;
248 	int ret;
249 
250 	card->dapm.idle_bias_off = true;
251 
252 	if (quirk & SOC_ES8336_HEADSET_MIC1) {
253 		custom_map = sof_es8316_headset_mic1_map;
254 		num_routes = ARRAY_SIZE(sof_es8316_headset_mic1_map);
255 	} else {
256 		custom_map = sof_es8316_headset_mic2_map;
257 		num_routes = ARRAY_SIZE(sof_es8316_headset_mic2_map);
258 	}
259 
260 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
261 	if (ret)
262 		return ret;
263 
264 	ret = snd_soc_card_jack_new_pins(card, "Headset",
265 					 SND_JACK_HEADSET | SND_JACK_BTN_0,
266 					 &priv->jack, sof_es8316_jack_pins,
267 					 ARRAY_SIZE(sof_es8316_jack_pins));
268 	if (ret) {
269 		dev_err(card->dev, "jack creation failed %d\n", ret);
270 		return ret;
271 	}
272 
273 	snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
274 
275 	snd_soc_component_set_jack(codec, &priv->jack, NULL);
276 
277 	return 0;
278 }
279 
280 static void sof_es8316_exit(struct snd_soc_pcm_runtime *rtd)
281 {
282 	struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
283 
284 	snd_soc_component_set_jack(component, NULL, NULL);
285 }
286 
287 static int sof_es8336_quirk_cb(const struct dmi_system_id *id)
288 {
289 	quirk = (unsigned long)id->driver_data;
290 
291 	return 1;
292 }
293 
294 /*
295  * this table should only be used to add GPIO or jack-detection quirks
296  * that cannot be detected from ACPI tables. The SSP and DMIC
297  * information are providing by the platform driver and are aligned
298  * with the topology used.
299  *
300  * If the GPIO support is missing, the quirk parameter can be used to
301  * enable speakers. In that case it's recommended to keep the SSP and DMIC
302  * information consistent, overriding the SSP and DMIC can only be done
303  * if the topology file is modified as well.
304  */
305 static const struct dmi_system_id sof_es8336_quirk_table[] = {
306 	{
307 		.callback = sof_es8336_quirk_cb,
308 		.matches = {
309 			DMI_MATCH(DMI_SYS_VENDOR, "IP3 tech"),
310 			DMI_MATCH(DMI_BOARD_NAME, "WN1"),
311 		},
312 		.driver_data = (void *)(SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
313 	},
314 	{
315 		.callback = sof_es8336_quirk_cb,
316 		.matches = {
317 			DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"),
318 			DMI_MATCH(DMI_BOARD_NAME, "BOHB-WAX9-PCB-B2"),
319 		},
320 		.driver_data = (void *)(SOF_ES8336_HEADPHONE_GPIO |
321 					SOC_ES8336_HEADSET_MIC1)
322 	},
323 	{}
324 };
325 
326 static int sof_es8336_hw_params(struct snd_pcm_substream *substream,
327 				struct snd_pcm_hw_params *params)
328 {
329 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
330 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
331 	const int sysclk = 19200000;
332 	int ret;
333 
334 	ret = snd_soc_dai_set_sysclk(codec_dai, 1, sysclk, SND_SOC_CLOCK_OUT);
335 	if (ret < 0) {
336 		dev_err(rtd->dev, "%s, Failed to set ES8336 SYSCLK: %d\n",
337 			__func__, ret);
338 		return ret;
339 	}
340 
341 	return 0;
342 }
343 
344 /* machine stream operations */
345 static struct snd_soc_ops sof_es8336_ops = {
346 	.hw_params = sof_es8336_hw_params,
347 };
348 
349 static struct snd_soc_dai_link_component platform_component[] = {
350 	{
351 		/* name might be overridden during probe */
352 		.name = "0000:00:1f.3"
353 	}
354 };
355 
356 SND_SOC_DAILINK_DEF(es8336_codec,
357 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8336:00", "ES8316 HiFi")));
358 
359 static struct snd_soc_dai_link_component dmic_component[] = {
360 	{
361 		.name = "dmic-codec",
362 		.dai_name = "dmic-hifi",
363 	}
364 };
365 
366 static struct snd_soc_dai_link_component dummy_component[] = {
367 	{
368 		.name = "snd-soc-dummy",
369 		.dai_name = "snd-soc-dummy-dai",
370 	}
371 };
372 
373 static int sof_es8336_late_probe(struct snd_soc_card *card)
374 {
375 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
376 	struct sof_hdmi_pcm *pcm;
377 
378 	if (list_empty(&priv->hdmi_pcm_list))
379 		return -ENOENT;
380 
381 	pcm = list_first_entry(&priv->hdmi_pcm_list, struct sof_hdmi_pcm, head);
382 
383 	return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
384 }
385 
386 /* SoC card */
387 static struct snd_soc_card sof_es8336_card = {
388 	.name = "essx8336", /* sof- prefix added automatically */
389 	.owner = THIS_MODULE,
390 	.dapm_widgets = sof_es8316_widgets,
391 	.num_dapm_widgets = ARRAY_SIZE(sof_es8316_widgets),
392 	.dapm_routes = sof_es8316_audio_map,
393 	.num_dapm_routes = ARRAY_SIZE(sof_es8316_audio_map),
394 	.controls = sof_es8316_controls,
395 	.num_controls = ARRAY_SIZE(sof_es8316_controls),
396 	.fully_routed = true,
397 	.late_probe = sof_es8336_late_probe,
398 	.num_links = 1,
399 };
400 
401 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
402 							  int ssp_codec,
403 							  int dmic_be_num,
404 							  int hdmi_num)
405 {
406 	struct snd_soc_dai_link_component *cpus;
407 	struct snd_soc_dai_link *links;
408 	struct snd_soc_dai_link_component *idisp_components;
409 	int hdmi_id_offset = 0;
410 	int id = 0;
411 	int i;
412 
413 	links = devm_kcalloc(dev, sof_es8336_card.num_links,
414 			     sizeof(struct snd_soc_dai_link), GFP_KERNEL);
415 	cpus = devm_kcalloc(dev, sof_es8336_card.num_links,
416 			    sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
417 	if (!links || !cpus)
418 		goto devm_err;
419 
420 	/* codec SSP */
421 	links[id].name = devm_kasprintf(dev, GFP_KERNEL,
422 					"SSP%d-Codec", ssp_codec);
423 	if (!links[id].name)
424 		goto devm_err;
425 
426 	links[id].id = id;
427 	links[id].codecs = es8336_codec;
428 	links[id].num_codecs = ARRAY_SIZE(es8336_codec);
429 	links[id].platforms = platform_component;
430 	links[id].num_platforms = ARRAY_SIZE(platform_component);
431 	links[id].init = sof_es8316_init;
432 	links[id].exit = sof_es8316_exit;
433 	links[id].ops = &sof_es8336_ops;
434 	links[id].nonatomic = true;
435 	links[id].dpcm_playback = 1;
436 	links[id].dpcm_capture = 1;
437 	links[id].no_pcm = 1;
438 	links[id].cpus = &cpus[id];
439 	links[id].num_cpus = 1;
440 
441 	links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
442 						  "SSP%d Pin",
443 						  ssp_codec);
444 	if (!links[id].cpus->dai_name)
445 		goto devm_err;
446 
447 	id++;
448 
449 	/* dmic */
450 	if (dmic_be_num > 0) {
451 		/* at least we have dmic01 */
452 		links[id].name = "dmic01";
453 		links[id].cpus = &cpus[id];
454 		links[id].cpus->dai_name = "DMIC01 Pin";
455 		links[id].init = dmic_init;
456 		if (dmic_be_num > 1) {
457 			/* set up 2 BE links at most */
458 			links[id + 1].name = "dmic16k";
459 			links[id + 1].cpus = &cpus[id + 1];
460 			links[id + 1].cpus->dai_name = "DMIC16k Pin";
461 			dmic_be_num = 2;
462 		}
463 	} else {
464 		/* HDMI dai link starts at 3 according to current topology settings */
465 		hdmi_id_offset = 2;
466 	}
467 
468 	for (i = 0; i < dmic_be_num; i++) {
469 		links[id].id = id;
470 		links[id].num_cpus = 1;
471 		links[id].codecs = dmic_component;
472 		links[id].num_codecs = ARRAY_SIZE(dmic_component);
473 		links[id].platforms = platform_component;
474 		links[id].num_platforms = ARRAY_SIZE(platform_component);
475 		links[id].ignore_suspend = 1;
476 		links[id].dpcm_capture = 1;
477 		links[id].no_pcm = 1;
478 
479 		id++;
480 	}
481 
482 	/* HDMI */
483 	if (hdmi_num > 0) {
484 		idisp_components = devm_kcalloc(dev,
485 						hdmi_num,
486 						sizeof(struct snd_soc_dai_link_component),
487 						GFP_KERNEL);
488 		if (!idisp_components)
489 			goto devm_err;
490 	}
491 
492 	for (i = 1; i <= hdmi_num; i++) {
493 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
494 						"iDisp%d", i);
495 		if (!links[id].name)
496 			goto devm_err;
497 
498 		links[id].id = id + hdmi_id_offset;
499 		links[id].cpus = &cpus[id];
500 		links[id].num_cpus = 1;
501 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
502 							  "iDisp%d Pin", i);
503 		if (!links[id].cpus->dai_name)
504 			goto devm_err;
505 
506 		idisp_components[i - 1].name = "ehdaudio0D2";
507 		idisp_components[i - 1].dai_name = devm_kasprintf(dev,
508 								  GFP_KERNEL,
509 								  "intel-hdmi-hifi%d",
510 								  i);
511 		if (!idisp_components[i - 1].dai_name)
512 			goto devm_err;
513 
514 		links[id].codecs = &idisp_components[i - 1];
515 		links[id].num_codecs = 1;
516 		links[id].platforms = platform_component;
517 		links[id].num_platforms = ARRAY_SIZE(platform_component);
518 		links[id].init = sof_hdmi_init;
519 		links[id].dpcm_playback = 1;
520 		links[id].no_pcm = 1;
521 
522 		id++;
523 	}
524 
525 	/* HDMI-In SSP */
526 	if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
527 		int num_of_hdmi_ssp = (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
528 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
529 
530 		for (i = 1; i <= num_of_hdmi_ssp; i++) {
531 			int port = (i == 1 ? (quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
532 						SOF_HDMI_CAPTURE_1_SSP_SHIFT :
533 						(quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
534 						SOF_HDMI_CAPTURE_2_SSP_SHIFT);
535 
536 			links[id].cpus = &cpus[id];
537 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
538 								  "SSP%d Pin", port);
539 			if (!links[id].cpus->dai_name)
540 				return NULL;
541 			links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
542 			if (!links[id].name)
543 				return NULL;
544 			links[id].id = id + hdmi_id_offset;
545 			links[id].codecs = dummy_component;
546 			links[id].num_codecs = ARRAY_SIZE(dummy_component);
547 			links[id].platforms = platform_component;
548 			links[id].num_platforms = ARRAY_SIZE(platform_component);
549 			links[id].dpcm_capture = 1;
550 			links[id].no_pcm = 1;
551 			links[id].num_cpus = 1;
552 			id++;
553 		}
554 	}
555 
556 	return links;
557 
558 devm_err:
559 	return NULL;
560 }
561 
562 static char soc_components[30];
563 
564  /* i2c-<HID>:00 with HID being 8 chars */
565 static char codec_name[SND_ACPI_I2C_ID_LEN];
566 
567 static int sof_es8336_probe(struct platform_device *pdev)
568 {
569 	struct device *dev = &pdev->dev;
570 	struct snd_soc_card *card;
571 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
572 	struct property_entry props[MAX_NO_PROPS] = {};
573 	struct sof_es8336_private *priv;
574 	struct fwnode_handle *fwnode;
575 	struct acpi_device *adev;
576 	struct snd_soc_dai_link *dai_links;
577 	struct device *codec_dev;
578 	const struct acpi_gpio_mapping *gpio_mapping;
579 	unsigned int cnt = 0;
580 	int dmic_be_num = 0;
581 	int hdmi_num = 3;
582 	int ret;
583 
584 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
585 	if (!priv)
586 		return -ENOMEM;
587 
588 	card = &sof_es8336_card;
589 	card->dev = dev;
590 
591 	if (pdev->id_entry && pdev->id_entry->driver_data)
592 		quirk = (unsigned long)pdev->id_entry->driver_data;
593 
594 	/* check GPIO DMI quirks */
595 	dmi_check_system(sof_es8336_quirk_table);
596 
597 	/* Use NHLT configuration only for Non-HDMI capture use case.
598 	 * Because more than one SSP will be enabled for HDMI capture hence wrong codec
599 	 * SSP will be set.
600 	 */
601 	if (mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER) {
602 		if (!mach->mach_params.i2s_link_mask) {
603 			dev_warn(dev, "No I2S link information provided, using SSP0. This may need to be modified with the quirk module parameter\n");
604 		} else {
605 			/*
606 			 * Set configuration based on platform NHLT.
607 			 * In this machine driver, we can only support one SSP for the
608 			 * ES8336 link.
609 			 * In some cases multiple SSPs can be reported by NHLT, starting MSB-first
610 			 * seems to pick the right connection.
611 			 */
612 			unsigned long ssp;
613 
614 			/* fls returns 1-based results, SSPs indices are 0-based */
615 			ssp = fls(mach->mach_params.i2s_link_mask) - 1;
616 
617 			quirk |= ssp;
618 		}
619 	}
620 
621 	if (mach->mach_params.dmic_num)
622 		quirk |= SOF_ES8336_ENABLE_DMIC;
623 
624 	if (quirk_override != -1) {
625 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
626 			 quirk, quirk_override);
627 		quirk = quirk_override;
628 	}
629 	log_quirks(dev);
630 
631 	if (quirk & SOF_ES8336_ENABLE_DMIC)
632 		dmic_be_num = 2;
633 
634 	/* compute number of dai links */
635 	sof_es8336_card.num_links = 1 + dmic_be_num + hdmi_num;
636 
637 	if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
638 		sof_es8336_card.num_links += (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
639 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
640 
641 	dai_links = sof_card_dai_links_create(dev,
642 					      SOF_ES8336_SSP_CODEC(quirk),
643 					      dmic_be_num, hdmi_num);
644 	if (!dai_links)
645 		return -ENOMEM;
646 
647 	sof_es8336_card.dai_link = dai_links;
648 
649 	/* fixup codec name based on HID */
650 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
651 	if (adev) {
652 		snprintf(codec_name, sizeof(codec_name),
653 			 "i2c-%s", acpi_dev_name(adev));
654 		put_device(&adev->dev);
655 		dai_links[0].codecs->name = codec_name;
656 
657 		/* also fixup codec dai name if relevant */
658 		if (!strncmp(mach->id, "ESSX8326", SND_ACPI_I2C_ID_LEN))
659 			dai_links[0].codecs->dai_name = "ES8326 HiFi";
660 	} else {
661 		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
662 		return -ENXIO;
663 	}
664 
665 	ret = snd_soc_fixup_dai_links_platform_name(&sof_es8336_card,
666 						    mach->mach_params.platform);
667 	if (ret)
668 		return ret;
669 
670 	codec_dev = acpi_get_first_physical_node(adev);
671 	if (!codec_dev)
672 		return -EPROBE_DEFER;
673 	priv->codec_dev = get_device(codec_dev);
674 
675 	if (quirk & SOF_ES8336_JD_INVERTED)
676 		props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
677 
678 	if (cnt) {
679 		fwnode = fwnode_create_software_node(props, NULL);
680 		if (IS_ERR(fwnode)) {
681 			put_device(codec_dev);
682 			return PTR_ERR(fwnode);
683 		}
684 
685 		ret = device_add_software_node(codec_dev, to_software_node(fwnode));
686 
687 		fwnode_handle_put(fwnode);
688 
689 		if (ret) {
690 			put_device(codec_dev);
691 			return ret;
692 		}
693 	}
694 
695 	/* get speaker enable GPIO */
696 	if (quirk & SOF_ES8336_HEADPHONE_GPIO) {
697 		if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
698 			gpio_mapping = acpi_enable_both_gpios;
699 		else
700 			gpio_mapping = acpi_enable_both_gpios_rev_order;
701 	} else if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK) {
702 		gpio_mapping = acpi_speakers_enable_gpio1;
703 	} else {
704 		gpio_mapping = acpi_speakers_enable_gpio0;
705 	}
706 
707 	ret = devm_acpi_dev_add_driver_gpios(codec_dev, gpio_mapping);
708 	if (ret)
709 		dev_warn(codec_dev, "unable to add GPIO mapping table\n");
710 
711 	priv->gpio_speakers = gpiod_get_optional(codec_dev, "speakers-enable", GPIOD_OUT_LOW);
712 	if (IS_ERR(priv->gpio_speakers)) {
713 		ret = dev_err_probe(dev, PTR_ERR(priv->gpio_speakers),
714 				    "could not get speakers-enable GPIO\n");
715 		goto err_put_codec;
716 	}
717 
718 	priv->gpio_headphone = gpiod_get_optional(codec_dev, "headphone-enable", GPIOD_OUT_LOW);
719 	if (IS_ERR(priv->gpio_headphone)) {
720 		ret = dev_err_probe(dev, PTR_ERR(priv->gpio_headphone),
721 				    "could not get headphone-enable GPIO\n");
722 		goto err_put_codec;
723 	}
724 
725 	INIT_LIST_HEAD(&priv->hdmi_pcm_list);
726 
727 	snd_soc_card_set_drvdata(card, priv);
728 
729 	if (mach->mach_params.dmic_num > 0) {
730 		snprintf(soc_components, sizeof(soc_components),
731 			 "cfg-dmics:%d", mach->mach_params.dmic_num);
732 		card->components = soc_components;
733 	}
734 
735 	ret = devm_snd_soc_register_card(dev, card);
736 	if (ret) {
737 		gpiod_put(priv->gpio_speakers);
738 		dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
739 		goto err_put_codec;
740 	}
741 	platform_set_drvdata(pdev, &sof_es8336_card);
742 	return 0;
743 
744 err_put_codec:
745 	device_remove_software_node(priv->codec_dev);
746 	put_device(codec_dev);
747 	return ret;
748 }
749 
750 static int sof_es8336_remove(struct platform_device *pdev)
751 {
752 	struct snd_soc_card *card = platform_get_drvdata(pdev);
753 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
754 
755 	gpiod_put(priv->gpio_speakers);
756 	device_remove_software_node(priv->codec_dev);
757 	put_device(priv->codec_dev);
758 
759 	return 0;
760 }
761 
762 static const struct platform_device_id board_ids[] = {
763 	{
764 		.name = "sof-essx8336", /* default quirk == 0 */
765 	},
766 	{
767 		.name = "adl_es83x6_c1_h02",
768 		.driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
769 					SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
770 					SOF_HDMI_CAPTURE_1_SSP(0) |
771 					SOF_HDMI_CAPTURE_2_SSP(2) |
772 					SOF_SSP_HDMI_CAPTURE_PRESENT |
773 					SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
774 					SOF_ES8336_JD_INVERTED),
775 	},
776 	{ }
777 };
778 MODULE_DEVICE_TABLE(platform, board_ids);
779 
780 static struct platform_driver sof_es8336_driver = {
781 	.driver = {
782 		.name = "sof-essx8336",
783 		.pm = &snd_soc_pm_ops,
784 	},
785 	.probe = sof_es8336_probe,
786 	.remove = sof_es8336_remove,
787 	.id_table = board_ids,
788 };
789 module_platform_driver(sof_es8336_driver);
790 
791 MODULE_DESCRIPTION("ASoC Intel(R) SOF + ES8336 Machine driver");
792 MODULE_LICENSE("GPL");
793 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
794