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 #define SOF_ES8336_SSP_CODEC(quirk)		((quirk) & GENMASK(3, 0))
25 #define SOF_ES8336_SSP_CODEC_MASK		(GENMASK(3, 0))
26 
27 #define SOF_ES8336_TGL_GPIO_QUIRK		BIT(4)
28 #define SOF_ES8336_ENABLE_DMIC			BIT(5)
29 
30 static unsigned long quirk;
31 
32 static int quirk_override = -1;
33 module_param_named(quirk, quirk_override, int, 0444);
34 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
35 
36 struct sof_es8336_private {
37 	struct device *codec_dev;
38 	struct gpio_desc *gpio_pa;
39 	struct snd_soc_jack jack;
40 	struct list_head hdmi_pcm_list;
41 	bool speaker_en;
42 };
43 
44 struct sof_hdmi_pcm {
45 	struct list_head head;
46 	struct snd_soc_dai *codec_dai;
47 	int device;
48 };
49 
50 static const struct acpi_gpio_params pa_enable_gpio = { 0, 0, true };
51 static const struct acpi_gpio_mapping acpi_es8336_gpios[] = {
52 	{ "pa-enable-gpios", &pa_enable_gpio, 1 },
53 	{ }
54 };
55 
56 static const struct acpi_gpio_params quirk_pa_enable_gpio = { 1, 0, true };
57 static const struct acpi_gpio_mapping quirk_acpi_es8336_gpios[] = {
58 	{ "pa-enable-gpios", &quirk_pa_enable_gpio, 1 },
59 	{ }
60 };
61 
62 static const struct acpi_gpio_mapping *gpio_mapping = acpi_es8336_gpios;
63 
log_quirks(struct device * dev)64 static void log_quirks(struct device *dev)
65 {
66 	dev_info(dev, "quirk SSP%ld",  SOF_ES8336_SSP_CODEC(quirk));
67 }
68 
sof_es8316_speaker_power_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)69 static int sof_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
70 					  struct snd_kcontrol *kcontrol, int event)
71 {
72 	struct snd_soc_card *card = w->dapm->card;
73 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
74 
75 	if (SND_SOC_DAPM_EVENT_ON(event))
76 		priv->speaker_en = false;
77 	else
78 		priv->speaker_en = true;
79 
80 	gpiod_set_value_cansleep(priv->gpio_pa, priv->speaker_en);
81 
82 	return 0;
83 }
84 
85 static const struct snd_soc_dapm_widget sof_es8316_widgets[] = {
86 	SND_SOC_DAPM_SPK("Speaker", NULL),
87 	SND_SOC_DAPM_HP("Headphone", NULL),
88 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
89 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
90 
91 	SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
92 			    sof_es8316_speaker_power_event,
93 			    SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
94 };
95 
96 static const struct snd_soc_dapm_widget dmic_widgets[] = {
97 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
98 };
99 
100 static const struct snd_soc_dapm_route sof_es8316_audio_map[] = {
101 	{"Headphone", NULL, "HPOL"},
102 	{"Headphone", NULL, "HPOR"},
103 
104 	/*
105 	 * There is no separate speaker output instead the speakers are muxed to
106 	 * the HP outputs. The mux is controlled by the "Speaker Power" supply.
107 	 */
108 	{"Speaker", NULL, "HPOL"},
109 	{"Speaker", NULL, "HPOR"},
110 	{"Speaker", NULL, "Speaker Power"},
111 };
112 
113 static const struct snd_soc_dapm_route sof_es8316_intmic_in1_map[] = {
114 	{"MIC1", NULL, "Internal Mic"},
115 	{"MIC2", NULL, "Headset Mic"},
116 };
117 
118 static const struct snd_soc_dapm_route dmic_map[] = {
119 	/* digital mics */
120 	{"DMic", NULL, "SoC DMIC"},
121 };
122 
123 static const struct snd_kcontrol_new sof_es8316_controls[] = {
124 	SOC_DAPM_PIN_SWITCH("Speaker"),
125 	SOC_DAPM_PIN_SWITCH("Headphone"),
126 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
127 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
128 };
129 
130 static struct snd_soc_jack_pin sof_es8316_jack_pins[] = {
131 	{
132 		.pin	= "Headphone",
133 		.mask	= SND_JACK_HEADPHONE,
134 	},
135 	{
136 		.pin	= "Headset Mic",
137 		.mask	= SND_JACK_MICROPHONE,
138 	},
139 };
140 
dmic_init(struct snd_soc_pcm_runtime * runtime)141 static int dmic_init(struct snd_soc_pcm_runtime *runtime)
142 {
143 	struct snd_soc_card *card = runtime->card;
144 	int ret;
145 
146 	ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
147 					ARRAY_SIZE(dmic_widgets));
148 	if (ret) {
149 		dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
150 		return ret;
151 	}
152 
153 	ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
154 				      ARRAY_SIZE(dmic_map));
155 	if (ret)
156 		dev_err(card->dev, "DMic map addition failed: %d\n", ret);
157 
158 	return ret;
159 }
160 
sof_hdmi_init(struct snd_soc_pcm_runtime * runtime)161 static int sof_hdmi_init(struct snd_soc_pcm_runtime *runtime)
162 {
163 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(runtime->card);
164 	struct snd_soc_dai *dai = asoc_rtd_to_codec(runtime, 0);
165 	struct sof_hdmi_pcm *pcm;
166 
167 	pcm = devm_kzalloc(runtime->card->dev, sizeof(*pcm), GFP_KERNEL);
168 	if (!pcm)
169 		return -ENOMEM;
170 
171 	/* dai_link id is 1:1 mapped to the PCM device */
172 	pcm->device = runtime->dai_link->id;
173 	pcm->codec_dai = dai;
174 
175 	list_add_tail(&pcm->head, &priv->hdmi_pcm_list);
176 
177 	return 0;
178 }
179 
sof_es8316_init(struct snd_soc_pcm_runtime * runtime)180 static int sof_es8316_init(struct snd_soc_pcm_runtime *runtime)
181 {
182 	struct snd_soc_component *codec = asoc_rtd_to_codec(runtime, 0)->component;
183 	struct snd_soc_card *card = runtime->card;
184 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
185 	const struct snd_soc_dapm_route *custom_map;
186 	int num_routes;
187 	int ret;
188 
189 	card->dapm.idle_bias_off = true;
190 
191 	custom_map = sof_es8316_intmic_in1_map;
192 	num_routes = ARRAY_SIZE(sof_es8316_intmic_in1_map);
193 
194 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
195 	if (ret)
196 		return ret;
197 
198 	ret = snd_soc_card_jack_new(card, "Headset",
199 				    SND_JACK_HEADSET | SND_JACK_BTN_0,
200 				    &priv->jack, sof_es8316_jack_pins,
201 				    ARRAY_SIZE(sof_es8316_jack_pins));
202 	if (ret) {
203 		dev_err(card->dev, "jack creation failed %d\n", ret);
204 		return ret;
205 	}
206 
207 	snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
208 
209 	snd_soc_component_set_jack(codec, &priv->jack, NULL);
210 
211 	return 0;
212 }
213 
sof_es8316_exit(struct snd_soc_pcm_runtime * rtd)214 static void sof_es8316_exit(struct snd_soc_pcm_runtime *rtd)
215 {
216 	struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
217 
218 	snd_soc_component_set_jack(component, NULL, NULL);
219 }
220 
sof_es8336_quirk_cb(const struct dmi_system_id * id)221 static int sof_es8336_quirk_cb(const struct dmi_system_id *id)
222 {
223 	quirk = (unsigned long)id->driver_data;
224 
225 	if (quirk & SOF_ES8336_TGL_GPIO_QUIRK)
226 		gpio_mapping = quirk_acpi_es8336_gpios;
227 
228 	return 1;
229 }
230 
231 static const struct dmi_system_id sof_es8336_quirk_table[] = {
232 	{
233 		.callback = sof_es8336_quirk_cb,
234 		.matches = {
235 			DMI_MATCH(DMI_SYS_VENDOR, "CHUWI Innovation And Technology"),
236 			DMI_MATCH(DMI_BOARD_NAME, "Hi10 X"),
237 		},
238 		.driver_data = (void *)SOF_ES8336_SSP_CODEC(2)
239 	},
240 	{
241 		.callback = sof_es8336_quirk_cb,
242 		.matches = {
243 			DMI_MATCH(DMI_SYS_VENDOR, "IP3 tech"),
244 			DMI_MATCH(DMI_BOARD_NAME, "WN1"),
245 		},
246 		.driver_data = (void *)(SOF_ES8336_SSP_CODEC(0) |
247 					SOF_ES8336_TGL_GPIO_QUIRK |
248 					SOF_ES8336_ENABLE_DMIC)
249 	},
250 	{}
251 };
252 
sof_es8336_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)253 static int sof_es8336_hw_params(struct snd_pcm_substream *substream,
254 				struct snd_pcm_hw_params *params)
255 {
256 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
257 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
258 	const int sysclk = 19200000;
259 	int ret;
260 
261 	ret = snd_soc_dai_set_sysclk(codec_dai, 1, sysclk, SND_SOC_CLOCK_OUT);
262 	if (ret < 0) {
263 		dev_err(rtd->dev, "%s, Failed to set ES8336 SYSCLK: %d\n",
264 			__func__, ret);
265 		return ret;
266 	}
267 
268 	return 0;
269 }
270 
271 /* machine stream operations */
272 static struct snd_soc_ops sof_es8336_ops = {
273 	.hw_params = sof_es8336_hw_params,
274 };
275 
276 static struct snd_soc_dai_link_component platform_component[] = {
277 	{
278 		/* name might be overridden during probe */
279 		.name = "0000:00:1f.3"
280 	}
281 };
282 
283 SND_SOC_DAILINK_DEF(ssp1_codec,
284 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8336:00", "ES8316 HiFi")));
285 
286 static struct snd_soc_dai_link_component dmic_component[] = {
287 	{
288 		.name = "dmic-codec",
289 		.dai_name = "dmic-hifi",
290 	}
291 };
292 
sof_es8336_late_probe(struct snd_soc_card * card)293 static int sof_es8336_late_probe(struct snd_soc_card *card)
294 {
295 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
296 	struct sof_hdmi_pcm *pcm;
297 
298 	if (list_empty(&priv->hdmi_pcm_list))
299 		return -ENOENT;
300 
301 	pcm = list_first_entry(&priv->hdmi_pcm_list, struct sof_hdmi_pcm, head);
302 
303 	return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
304 }
305 
306 /* SoC card */
307 static struct snd_soc_card sof_es8336_card = {
308 	.name = "essx8336", /* sof- prefix added automatically */
309 	.owner = THIS_MODULE,
310 	.dapm_widgets = sof_es8316_widgets,
311 	.num_dapm_widgets = ARRAY_SIZE(sof_es8316_widgets),
312 	.dapm_routes = sof_es8316_audio_map,
313 	.num_dapm_routes = ARRAY_SIZE(sof_es8316_audio_map),
314 	.controls = sof_es8316_controls,
315 	.num_controls = ARRAY_SIZE(sof_es8316_controls),
316 	.fully_routed = true,
317 	.late_probe = sof_es8336_late_probe,
318 	.num_links = 1,
319 };
320 
sof_card_dai_links_create(struct device * dev,int ssp_codec,int dmic_be_num,int hdmi_num)321 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
322 							  int ssp_codec,
323 							  int dmic_be_num,
324 							  int hdmi_num)
325 {
326 	struct snd_soc_dai_link_component *cpus;
327 	struct snd_soc_dai_link *links;
328 	struct snd_soc_dai_link_component *idisp_components;
329 	int hdmi_id_offset = 0;
330 	int id = 0;
331 	int i;
332 
333 	links = devm_kcalloc(dev, sof_es8336_card.num_links,
334 			     sizeof(struct snd_soc_dai_link), GFP_KERNEL);
335 	cpus = devm_kcalloc(dev, sof_es8336_card.num_links,
336 			    sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
337 	if (!links || !cpus)
338 		goto devm_err;
339 
340 	/* codec SSP */
341 	links[id].name = devm_kasprintf(dev, GFP_KERNEL,
342 					"SSP%d-Codec", ssp_codec);
343 	if (!links[id].name)
344 		goto devm_err;
345 
346 	links[id].id = id;
347 	links[id].codecs = ssp1_codec;
348 	links[id].num_codecs = ARRAY_SIZE(ssp1_codec);
349 	links[id].platforms = platform_component;
350 	links[id].num_platforms = ARRAY_SIZE(platform_component);
351 	links[id].init = sof_es8316_init;
352 	links[id].exit = sof_es8316_exit;
353 	links[id].ops = &sof_es8336_ops;
354 	links[id].nonatomic = true;
355 	links[id].dpcm_playback = 1;
356 	links[id].dpcm_capture = 1;
357 	links[id].no_pcm = 1;
358 	links[id].cpus = &cpus[id];
359 	links[id].num_cpus = 1;
360 
361 	links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
362 						  "SSP%d Pin",
363 						  ssp_codec);
364 	if (!links[id].cpus->dai_name)
365 		goto devm_err;
366 
367 	id++;
368 
369 	/* dmic */
370 	if (dmic_be_num > 0) {
371 		/* at least we have dmic01 */
372 		links[id].name = "dmic01";
373 		links[id].cpus = &cpus[id];
374 		links[id].cpus->dai_name = "DMIC01 Pin";
375 		links[id].init = dmic_init;
376 		if (dmic_be_num > 1) {
377 			/* set up 2 BE links at most */
378 			links[id + 1].name = "dmic16k";
379 			links[id + 1].cpus = &cpus[id + 1];
380 			links[id + 1].cpus->dai_name = "DMIC16k Pin";
381 			dmic_be_num = 2;
382 		}
383 	} else {
384 		/* HDMI dai link starts at 3 according to current topology settings */
385 		hdmi_id_offset = 2;
386 	}
387 
388 	for (i = 0; i < dmic_be_num; i++) {
389 		links[id].id = id;
390 		links[id].num_cpus = 1;
391 		links[id].codecs = dmic_component;
392 		links[id].num_codecs = ARRAY_SIZE(dmic_component);
393 		links[id].platforms = platform_component;
394 		links[id].num_platforms = ARRAY_SIZE(platform_component);
395 		links[id].ignore_suspend = 1;
396 		links[id].dpcm_capture = 1;
397 		links[id].no_pcm = 1;
398 
399 		id++;
400 	}
401 
402 	/* HDMI */
403 	if (hdmi_num > 0) {
404 		idisp_components = devm_kzalloc(dev,
405 						sizeof(struct snd_soc_dai_link_component) *
406 						hdmi_num, GFP_KERNEL);
407 		if (!idisp_components)
408 			goto devm_err;
409 	}
410 
411 	for (i = 1; i <= hdmi_num; i++) {
412 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
413 						"iDisp%d", i);
414 		if (!links[id].name)
415 			goto devm_err;
416 
417 		links[id].id = id + hdmi_id_offset;
418 		links[id].cpus = &cpus[id];
419 		links[id].num_cpus = 1;
420 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
421 							  "iDisp%d Pin", i);
422 		if (!links[id].cpus->dai_name)
423 			goto devm_err;
424 
425 		idisp_components[i - 1].name = "ehdaudio0D2";
426 		idisp_components[i - 1].dai_name = devm_kasprintf(dev,
427 								  GFP_KERNEL,
428 								  "intel-hdmi-hifi%d",
429 								  i);
430 		if (!idisp_components[i - 1].dai_name)
431 			goto devm_err;
432 
433 		links[id].codecs = &idisp_components[i - 1];
434 		links[id].num_codecs = 1;
435 		links[id].platforms = platform_component;
436 		links[id].num_platforms = ARRAY_SIZE(platform_component);
437 		links[id].init = sof_hdmi_init;
438 		links[id].dpcm_playback = 1;
439 		links[id].no_pcm = 1;
440 
441 		id++;
442 	}
443 
444 	return links;
445 
446 devm_err:
447 	return NULL;
448 }
449 
450  /* i2c-<HID>:00 with HID being 8 chars */
451 static char codec_name[SND_ACPI_I2C_ID_LEN];
452 
sof_es8336_probe(struct platform_device * pdev)453 static int sof_es8336_probe(struct platform_device *pdev)
454 {
455 	struct device *dev = &pdev->dev;
456 	struct snd_soc_card *card;
457 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
458 	struct sof_es8336_private *priv;
459 	struct acpi_device *adev;
460 	struct snd_soc_dai_link *dai_links;
461 	struct device *codec_dev;
462 	int dmic_be_num = 0;
463 	int hdmi_num = 3;
464 	int ret;
465 
466 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
467 	if (!priv)
468 		return -ENOMEM;
469 
470 	card = &sof_es8336_card;
471 	card->dev = dev;
472 
473 	if (!dmi_check_system(sof_es8336_quirk_table))
474 		quirk = SOF_ES8336_SSP_CODEC(2);
475 
476 	if (quirk & SOF_ES8336_ENABLE_DMIC)
477 		dmic_be_num = 2;
478 
479 	if (quirk_override != -1) {
480 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
481 			 quirk, quirk_override);
482 		quirk = quirk_override;
483 	}
484 	log_quirks(dev);
485 
486 	sof_es8336_card.num_links += dmic_be_num + hdmi_num;
487 	dai_links = sof_card_dai_links_create(dev,
488 					      SOF_ES8336_SSP_CODEC(quirk),
489 					      dmic_be_num, hdmi_num);
490 	if (!dai_links)
491 		return -ENOMEM;
492 
493 	sof_es8336_card.dai_link = dai_links;
494 
495 	/* fixup codec name based on HID */
496 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
497 	if (adev) {
498 		snprintf(codec_name, sizeof(codec_name),
499 			 "i2c-%s", acpi_dev_name(adev));
500 		put_device(&adev->dev);
501 		dai_links[0].codecs->name = codec_name;
502 	}
503 
504 	ret = snd_soc_fixup_dai_links_platform_name(&sof_es8336_card,
505 						    mach->mach_params.platform);
506 	if (ret)
507 		return ret;
508 
509 	/* get speaker enable GPIO */
510 	codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL, codec_name);
511 	if (!codec_dev)
512 		return -EPROBE_DEFER;
513 
514 	ret = devm_acpi_dev_add_driver_gpios(codec_dev, gpio_mapping);
515 	if (ret)
516 		dev_warn(codec_dev, "unable to add GPIO mapping table\n");
517 
518 	priv->gpio_pa = gpiod_get(codec_dev, "pa-enable", GPIOD_OUT_LOW);
519 	if (IS_ERR(priv->gpio_pa)) {
520 		ret = PTR_ERR(priv->gpio_pa);
521 		dev_err(codec_dev, "%s, could not get pa-enable: %d\n",
522 			__func__, ret);
523 		goto err;
524 	}
525 
526 	priv->codec_dev = codec_dev;
527 	INIT_LIST_HEAD(&priv->hdmi_pcm_list);
528 
529 	snd_soc_card_set_drvdata(card, priv);
530 
531 	ret = devm_snd_soc_register_card(dev, card);
532 	if (ret) {
533 		gpiod_put(priv->gpio_pa);
534 		dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
535 		goto err;
536 	}
537 	platform_set_drvdata(pdev, &sof_es8336_card);
538 	return 0;
539 
540 err:
541 	put_device(codec_dev);
542 	return ret;
543 }
544 
sof_es8336_remove(struct platform_device * pdev)545 static int sof_es8336_remove(struct platform_device *pdev)
546 {
547 	struct snd_soc_card *card = platform_get_drvdata(pdev);
548 	struct sof_es8336_private *priv = snd_soc_card_get_drvdata(card);
549 
550 	gpiod_put(priv->gpio_pa);
551 	put_device(priv->codec_dev);
552 
553 	return 0;
554 }
555 
556 static struct platform_driver sof_es8336_driver = {
557 	.driver = {
558 		.name = "sof-essx8336",
559 		.pm = &snd_soc_pm_ops,
560 	},
561 	.probe = sof_es8336_probe,
562 	.remove = sof_es8336_remove,
563 };
564 module_platform_driver(sof_es8336_driver);
565 
566 MODULE_DESCRIPTION("ASoC Intel(R) SOF + ES8336 Machine driver");
567 MODULE_LICENSE("GPL");
568 MODULE_ALIAS("platform:sof-essx8336");
569 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
570