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