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_kzalloc(dev,
485 						sizeof(struct snd_soc_dai_link_component) *
486 						hdmi_num, GFP_KERNEL);
487 		if (!idisp_components)
488 			goto devm_err;
489 	}
490 
491 	for (i = 1; i <= hdmi_num; i++) {
492 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
493 						"iDisp%d", i);
494 		if (!links[id].name)
495 			goto devm_err;
496 
497 		links[id].id = id + hdmi_id_offset;
498 		links[id].cpus = &cpus[id];
499 		links[id].num_cpus = 1;
500 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
501 							  "iDisp%d Pin", i);
502 		if (!links[id].cpus->dai_name)
503 			goto devm_err;
504 
505 		idisp_components[i - 1].name = "ehdaudio0D2";
506 		idisp_components[i - 1].dai_name = devm_kasprintf(dev,
507 								  GFP_KERNEL,
508 								  "intel-hdmi-hifi%d",
509 								  i);
510 		if (!idisp_components[i - 1].dai_name)
511 			goto devm_err;
512 
513 		links[id].codecs = &idisp_components[i - 1];
514 		links[id].num_codecs = 1;
515 		links[id].platforms = platform_component;
516 		links[id].num_platforms = ARRAY_SIZE(platform_component);
517 		links[id].init = sof_hdmi_init;
518 		links[id].dpcm_playback = 1;
519 		links[id].no_pcm = 1;
520 
521 		id++;
522 	}
523 
524 	/* HDMI-In SSP */
525 	if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
526 		int num_of_hdmi_ssp = (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
527 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
528 
529 		for (i = 1; i <= num_of_hdmi_ssp; i++) {
530 			int port = (i == 1 ? (quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
531 						SOF_HDMI_CAPTURE_1_SSP_SHIFT :
532 						(quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
533 						SOF_HDMI_CAPTURE_2_SSP_SHIFT);
534 
535 			links[id].cpus = &cpus[id];
536 			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
537 								  "SSP%d Pin", port);
538 			if (!links[id].cpus->dai_name)
539 				return NULL;
540 			links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
541 			if (!links[id].name)
542 				return NULL;
543 			links[id].id = id + hdmi_id_offset;
544 			links[id].codecs = dummy_component;
545 			links[id].num_codecs = ARRAY_SIZE(dummy_component);
546 			links[id].platforms = platform_component;
547 			links[id].num_platforms = ARRAY_SIZE(platform_component);
548 			links[id].dpcm_capture = 1;
549 			links[id].no_pcm = 1;
550 			links[id].num_cpus = 1;
551 			id++;
552 		}
553 	}
554 
555 	return links;
556 
557 devm_err:
558 	return NULL;
559 }
560 
561 static char soc_components[30];
562 
563  /* i2c-<HID>:00 with HID being 8 chars */
564 static char codec_name[SND_ACPI_I2C_ID_LEN];
565 
566 static int sof_es8336_probe(struct platform_device *pdev)
567 {
568 	struct device *dev = &pdev->dev;
569 	struct snd_soc_card *card;
570 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
571 	struct property_entry props[MAX_NO_PROPS] = {};
572 	struct sof_es8336_private *priv;
573 	struct fwnode_handle *fwnode;
574 	struct acpi_device *adev;
575 	struct snd_soc_dai_link *dai_links;
576 	struct device *codec_dev;
577 	const struct acpi_gpio_mapping *gpio_mapping;
578 	unsigned int cnt = 0;
579 	int dmic_be_num = 0;
580 	int hdmi_num = 3;
581 	int ret;
582 
583 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
584 	if (!priv)
585 		return -ENOMEM;
586 
587 	card = &sof_es8336_card;
588 	card->dev = dev;
589 
590 	if (pdev->id_entry && pdev->id_entry->driver_data)
591 		quirk = (unsigned long)pdev->id_entry->driver_data;
592 
593 	/* check GPIO DMI quirks */
594 	dmi_check_system(sof_es8336_quirk_table);
595 
596 	/* Use NHLT configuration only for Non-HDMI capture use case.
597 	 * Because more than one SSP will be enabled for HDMI capture hence wrong codec
598 	 * SSP will be set.
599 	 */
600 	if (mach->tplg_quirk_mask & SND_SOC_ACPI_TPLG_INTEL_SSP_NUMBER) {
601 		if (!mach->mach_params.i2s_link_mask) {
602 			dev_warn(dev, "No I2S link information provided, using SSP0. This may need to be modified with the quirk module parameter\n");
603 		} else {
604 			/*
605 			 * Set configuration based on platform NHLT.
606 			 * In this machine driver, we can only support one SSP for the
607 			 * ES8336 link.
608 			 * In some cases multiple SSPs can be reported by NHLT, starting MSB-first
609 			 * seems to pick the right connection.
610 			 */
611 			unsigned long ssp;
612 
613 			/* fls returns 1-based results, SSPs indices are 0-based */
614 			ssp = fls(mach->mach_params.i2s_link_mask) - 1;
615 
616 			quirk |= ssp;
617 		}
618 	}
619 
620 	if (mach->mach_params.dmic_num)
621 		quirk |= SOF_ES8336_ENABLE_DMIC;
622 
623 	if (quirk_override != -1) {
624 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
625 			 quirk, quirk_override);
626 		quirk = quirk_override;
627 	}
628 	log_quirks(dev);
629 
630 	if (quirk & SOF_ES8336_ENABLE_DMIC)
631 		dmic_be_num = 2;
632 
633 	/* compute number of dai links */
634 	sof_es8336_card.num_links = 1 + dmic_be_num + hdmi_num;
635 
636 	if (quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
637 		sof_es8336_card.num_links += (quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
638 				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
639 
640 	dai_links = sof_card_dai_links_create(dev,
641 					      SOF_ES8336_SSP_CODEC(quirk),
642 					      dmic_be_num, hdmi_num);
643 	if (!dai_links)
644 		return -ENOMEM;
645 
646 	sof_es8336_card.dai_link = dai_links;
647 
648 	/* fixup codec name based on HID */
649 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
650 	if (adev) {
651 		snprintf(codec_name, sizeof(codec_name),
652 			 "i2c-%s", acpi_dev_name(adev));
653 		put_device(&adev->dev);
654 		dai_links[0].codecs->name = codec_name;
655 
656 		/* also fixup codec dai name if relevant */
657 		if (!strncmp(mach->id, "ESSX8326", SND_ACPI_I2C_ID_LEN))
658 			dai_links[0].codecs->dai_name = "ES8326 HiFi";
659 	} else {
660 		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
661 		return -ENXIO;
662 	}
663 
664 	ret = snd_soc_fixup_dai_links_platform_name(&sof_es8336_card,
665 						    mach->mach_params.platform);
666 	if (ret)
667 		return ret;
668 
669 	codec_dev = acpi_get_first_physical_node(adev);
670 	if (!codec_dev)
671 		return -EPROBE_DEFER;
672 	priv->codec_dev = get_device(codec_dev);
673 
674 	if (quirk & SOF_ES8336_JD_INVERTED)
675 		props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
676 
677 	if (cnt) {
678 		fwnode = fwnode_create_software_node(props, NULL);
679 		if (IS_ERR(fwnode)) {
680 			put_device(codec_dev);
681 			return PTR_ERR(fwnode);
682 		}
683 
684 		ret = device_add_software_node(codec_dev, to_software_node(fwnode));
685 
686 		fwnode_handle_put(fwnode);
687 
688 		if (ret) {
689 			put_device(codec_dev);
690 			return ret;
691 		}
692 	}
693 
694 	/* get speaker enable GPIO */
695 	if (quirk & SOF_ES8336_HEADPHONE_GPIO) {
696 		if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK)
697 			gpio_mapping = acpi_enable_both_gpios;
698 		else
699 			gpio_mapping = acpi_enable_both_gpios_rev_order;
700 	} else if (quirk & SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK) {
701 		gpio_mapping = acpi_speakers_enable_gpio1;
702 	} else {
703 		gpio_mapping = acpi_speakers_enable_gpio0;
704 	}
705 
706 	ret = devm_acpi_dev_add_driver_gpios(codec_dev, gpio_mapping);
707 	if (ret)
708 		dev_warn(codec_dev, "unable to add GPIO mapping table\n");
709 
710 	priv->gpio_speakers = gpiod_get_optional(codec_dev, "speakers-enable", GPIOD_OUT_LOW);
711 	if (IS_ERR(priv->gpio_speakers)) {
712 		ret = dev_err_probe(dev, PTR_ERR(priv->gpio_speakers),
713 				    "could not get speakers-enable GPIO\n");
714 		goto err_put_codec;
715 	}
716 
717 	priv->gpio_headphone = gpiod_get_optional(codec_dev, "headphone-enable", GPIOD_OUT_LOW);
718 	if (IS_ERR(priv->gpio_headphone)) {
719 		ret = dev_err_probe(dev, PTR_ERR(priv->gpio_headphone),
720 				    "could not get headphone-enable GPIO\n");
721 		goto err_put_codec;
722 	}
723 
724 	INIT_LIST_HEAD(&priv->hdmi_pcm_list);
725 
726 	snd_soc_card_set_drvdata(card, priv);
727 
728 	if (mach->mach_params.dmic_num > 0) {
729 		snprintf(soc_components, sizeof(soc_components),
730 			 "cfg-dmics:%d", mach->mach_params.dmic_num);
731 		card->components = soc_components;
732 	}
733 
734 	ret = devm_snd_soc_register_card(dev, card);
735 	if (ret) {
736 		gpiod_put(priv->gpio_speakers);
737 		dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
738 		goto err_put_codec;
739 	}
740 	platform_set_drvdata(pdev, &sof_es8336_card);
741 	return 0;
742 
743 err_put_codec:
744 	device_remove_software_node(priv->codec_dev);
745 	put_device(codec_dev);
746 	return ret;
747 }
748 
749 static int sof_es8336_remove(struct platform_device *pdev)
750 {
751 	struct snd_soc_card *card = platform_get_drvdata(pdev);
752 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
753 
754 	gpiod_put(priv->gpio_speakers);
755 	device_remove_software_node(priv->codec_dev);
756 	put_device(priv->codec_dev);
757 
758 	return 0;
759 }
760 
761 static const struct platform_device_id board_ids[] = {
762 	{
763 		.name = "adl_es83x6_c1_h02",
764 		.driver_data = (kernel_ulong_t)(SOF_ES8336_SSP_CODEC(1) |
765 					SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
766 					SOF_HDMI_CAPTURE_1_SSP(0) |
767 					SOF_HDMI_CAPTURE_2_SSP(2) |
768 					SOF_SSP_HDMI_CAPTURE_PRESENT |
769 					SOF_ES8336_SPEAKERS_EN_GPIO1_QUIRK |
770 					SOF_ES8336_JD_INVERTED),
771 	},
772 	{ }
773 };
774 MODULE_DEVICE_TABLE(platform, board_ids);
775 
776 static struct platform_driver sof_es8336_driver = {
777 	.driver = {
778 		.name = "sof-essx8336",
779 		.pm = &snd_soc_pm_ops,
780 	},
781 	.probe = sof_es8336_probe,
782 	.remove = sof_es8336_remove,
783 	.id_table = board_ids,
784 };
785 module_platform_driver(sof_es8336_driver);
786 
787 MODULE_DESCRIPTION("ASoC Intel(R) SOF + ES8336 Machine driver");
788 MODULE_LICENSE("GPL");
789 MODULE_ALIAS("platform:sof-essx8336");
790 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
791