xref: /linux/sound/soc/intel/boards/bytcht_es8316.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  bytcht_es8316.c - ASoc Machine driver for Intel Baytrail/Cherrytrail
4  *                    platforms with Everest ES8316 SoC
5  *
6  *  Copyright (C) 2017 Endless Mobile, Inc.
7  *  Authors: David Yang <yangxiaohua@everest-semi.com>,
8  *           Daniel Drake <drake@endlessm.com>
9  *
10  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13  */
14 #include <linux/acpi.h>
15 #include <linux/clk.h>
16 #include <linux/device.h>
17 #include <linux/dmi.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <sound/jack.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-acpi.h>
30 #include "../../codecs/es83xx-dsm-common.h"
31 #include "../atom/sst-atom-controls.h"
32 #include "../common/soc-intel-quirks.h"
33 
34 /* jd-inv + terminating entry */
35 #define MAX_NO_PROPS 2
36 
37 struct byt_cht_es8316_private {
38 	struct clk *mclk;
39 	struct snd_soc_jack jack;
40 	struct gpio_desc *speaker_en_gpio;
41 	struct device *codec_dev;
42 	bool speaker_en;
43 };
44 
45 enum {
46 	BYT_CHT_ES8316_INTMIC_IN1_MAP,
47 	BYT_CHT_ES8316_INTMIC_IN2_MAP,
48 };
49 
50 #define BYT_CHT_ES8316_MAP(quirk)		((quirk) & GENMASK(3, 0))
51 #define BYT_CHT_ES8316_SSP0			BIT(16)
52 #define BYT_CHT_ES8316_MONO_SPEAKER		BIT(17)
53 #define BYT_CHT_ES8316_JD_INVERTED		BIT(18)
54 
55 static unsigned long quirk;
56 
57 static int quirk_override = -1;
58 module_param_named(quirk, quirk_override, int, 0444);
59 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
60 
61 static void log_quirks(struct device *dev)
62 {
63 	if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN1_MAP)
64 		dev_info(dev, "quirk IN1_MAP enabled");
65 	if (BYT_CHT_ES8316_MAP(quirk) == BYT_CHT_ES8316_INTMIC_IN2_MAP)
66 		dev_info(dev, "quirk IN2_MAP enabled");
67 	if (quirk & BYT_CHT_ES8316_SSP0)
68 		dev_info(dev, "quirk SSP0 enabled");
69 	if (quirk & BYT_CHT_ES8316_MONO_SPEAKER)
70 		dev_info(dev, "quirk MONO_SPEAKER enabled\n");
71 	if (quirk & BYT_CHT_ES8316_JD_INVERTED)
72 		dev_info(dev, "quirk JD_INVERTED enabled\n");
73 }
74 
75 static int byt_cht_es8316_speaker_power_event(struct snd_soc_dapm_widget *w,
76 	struct snd_kcontrol *kcontrol, int event)
77 {
78 	struct snd_soc_card *card = w->dapm->card;
79 	struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
80 
81 	if (SND_SOC_DAPM_EVENT_ON(event))
82 		priv->speaker_en = true;
83 	else
84 		priv->speaker_en = false;
85 
86 	gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
87 
88 	return 0;
89 }
90 
91 static const struct snd_soc_dapm_widget byt_cht_es8316_widgets[] = {
92 	SND_SOC_DAPM_SPK("Speaker", NULL),
93 	SND_SOC_DAPM_HP("Headphone", NULL),
94 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
95 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
96 
97 	SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
98 			    byt_cht_es8316_speaker_power_event,
99 			    SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
100 };
101 
102 static const struct snd_soc_dapm_route byt_cht_es8316_audio_map[] = {
103 	{"Headphone", NULL, "HPOL"},
104 	{"Headphone", NULL, "HPOR"},
105 
106 	/*
107 	 * There is no separate speaker output instead the speakers are muxed to
108 	 * the HP outputs. The mux is controlled by the "Speaker Power" supply.
109 	 */
110 	{"Speaker", NULL, "HPOL"},
111 	{"Speaker", NULL, "HPOR"},
112 	{"Speaker", NULL, "Speaker Power"},
113 };
114 
115 static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in1_map[] = {
116 	{"MIC1", NULL, "Internal Mic"},
117 	{"MIC2", NULL, "Headset Mic"},
118 };
119 
120 static const struct snd_soc_dapm_route byt_cht_es8316_intmic_in2_map[] = {
121 	{"MIC2", NULL, "Internal Mic"},
122 	{"MIC1", NULL, "Headset Mic"},
123 };
124 
125 static const struct snd_soc_dapm_route byt_cht_es8316_ssp0_map[] = {
126 	{"Playback", NULL, "ssp0 Tx"},
127 	{"ssp0 Tx", NULL, "modem_out"},
128 	{"modem_in", NULL, "ssp0 Rx"},
129 	{"ssp0 Rx", NULL, "Capture"},
130 };
131 
132 static const struct snd_soc_dapm_route byt_cht_es8316_ssp2_map[] = {
133 	{"Playback", NULL, "ssp2 Tx"},
134 	{"ssp2 Tx", NULL, "codec_out0"},
135 	{"ssp2 Tx", NULL, "codec_out1"},
136 	{"codec_in0", NULL, "ssp2 Rx" },
137 	{"codec_in1", NULL, "ssp2 Rx" },
138 	{"ssp2 Rx", NULL, "Capture"},
139 };
140 
141 static const struct snd_kcontrol_new byt_cht_es8316_controls[] = {
142 	SOC_DAPM_PIN_SWITCH("Speaker"),
143 	SOC_DAPM_PIN_SWITCH("Headphone"),
144 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
145 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
146 };
147 
148 static struct snd_soc_jack_pin byt_cht_es8316_jack_pins[] = {
149 	{
150 		.pin	= "Headphone",
151 		.mask	= SND_JACK_HEADPHONE,
152 	},
153 	{
154 		.pin	= "Headset Mic",
155 		.mask	= SND_JACK_MICROPHONE,
156 	},
157 };
158 
159 static int byt_cht_es8316_init(struct snd_soc_pcm_runtime *runtime)
160 {
161 	struct snd_soc_component *codec = snd_soc_rtd_to_codec(runtime, 0)->component;
162 	struct snd_soc_card *card = runtime->card;
163 	struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
164 	const struct snd_soc_dapm_route *custom_map;
165 	int num_routes;
166 	int ret;
167 
168 	card->dapm.idle_bias_off = true;
169 
170 	switch (BYT_CHT_ES8316_MAP(quirk)) {
171 	case BYT_CHT_ES8316_INTMIC_IN1_MAP:
172 	default:
173 		custom_map = byt_cht_es8316_intmic_in1_map;
174 		num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in1_map);
175 		break;
176 	case BYT_CHT_ES8316_INTMIC_IN2_MAP:
177 		custom_map = byt_cht_es8316_intmic_in2_map;
178 		num_routes = ARRAY_SIZE(byt_cht_es8316_intmic_in2_map);
179 		break;
180 	}
181 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
182 	if (ret)
183 		return ret;
184 
185 	if (quirk & BYT_CHT_ES8316_SSP0) {
186 		custom_map = byt_cht_es8316_ssp0_map;
187 		num_routes = ARRAY_SIZE(byt_cht_es8316_ssp0_map);
188 	} else {
189 		custom_map = byt_cht_es8316_ssp2_map;
190 		num_routes = ARRAY_SIZE(byt_cht_es8316_ssp2_map);
191 	}
192 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
193 	if (ret)
194 		return ret;
195 
196 	/*
197 	 * The firmware might enable the clock at boot (this information
198 	 * may or may not be reflected in the enable clock register).
199 	 * To change the rate we must disable the clock first to cover these
200 	 * cases. Due to common clock framework restrictions that do not allow
201 	 * to disable a clock that has not been enabled, we need to enable
202 	 * the clock first.
203 	 */
204 	ret = clk_prepare_enable(priv->mclk);
205 	if (!ret)
206 		clk_disable_unprepare(priv->mclk);
207 
208 	ret = clk_set_rate(priv->mclk, 19200000);
209 	if (ret)
210 		dev_err(card->dev, "unable to set MCLK rate\n");
211 
212 	ret = clk_prepare_enable(priv->mclk);
213 	if (ret)
214 		dev_err(card->dev, "unable to enable MCLK\n");
215 
216 	ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_codec(runtime, 0), 0, 19200000,
217 				     SND_SOC_CLOCK_IN);
218 	if (ret < 0) {
219 		dev_err(card->dev, "can't set codec clock %d\n", ret);
220 		return ret;
221 	}
222 
223 	ret = snd_soc_card_jack_new_pins(card, "Headset",
224 					 SND_JACK_HEADSET | SND_JACK_BTN_0,
225 					 &priv->jack, byt_cht_es8316_jack_pins,
226 					 ARRAY_SIZE(byt_cht_es8316_jack_pins));
227 	if (ret) {
228 		dev_err(card->dev, "jack creation failed %d\n", ret);
229 		return ret;
230 	}
231 
232 	snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
233 	snd_soc_component_set_jack(codec, &priv->jack, NULL);
234 
235 	return 0;
236 }
237 
238 static int byt_cht_es8316_codec_fixup(struct snd_soc_pcm_runtime *rtd,
239 			    struct snd_pcm_hw_params *params)
240 {
241 	struct snd_interval *rate = hw_param_interval(params,
242 			SNDRV_PCM_HW_PARAM_RATE);
243 	struct snd_interval *channels = hw_param_interval(params,
244 						SNDRV_PCM_HW_PARAM_CHANNELS);
245 	int ret, bits;
246 
247 	/* The DSP will convert the FE rate to 48k, stereo */
248 	rate->min = rate->max = 48000;
249 	channels->min = channels->max = 2;
250 
251 	if (quirk & BYT_CHT_ES8316_SSP0) {
252 		/* set SSP0 to 16-bit */
253 		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
254 		bits = 16;
255 	} else {
256 		/* set SSP2 to 24-bit */
257 		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
258 		bits = 24;
259 	}
260 
261 	/*
262 	 * Default mode for SSP configuration is TDM 4 slot, override config
263 	 * with explicit setting to I2S 2ch 24-bit. The word length is set with
264 	 * dai_set_tdm_slot() since there is no other API exposed
265 	 */
266 	ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
267 				SND_SOC_DAIFMT_I2S     |
268 				SND_SOC_DAIFMT_NB_NF   |
269 				SND_SOC_DAIFMT_BP_FP
270 		);
271 	if (ret < 0) {
272 		dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
273 		return ret;
274 	}
275 
276 	ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
277 	if (ret < 0) {
278 		dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
279 		return ret;
280 	}
281 
282 	return 0;
283 }
284 
285 static int byt_cht_es8316_aif1_startup(struct snd_pcm_substream *substream)
286 {
287 	return snd_pcm_hw_constraint_single(substream->runtime,
288 			SNDRV_PCM_HW_PARAM_RATE, 48000);
289 }
290 
291 static const struct snd_soc_ops byt_cht_es8316_aif1_ops = {
292 	.startup = byt_cht_es8316_aif1_startup,
293 };
294 
295 SND_SOC_DAILINK_DEF(dummy,
296 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
297 
298 SND_SOC_DAILINK_DEF(media,
299 	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
300 
301 SND_SOC_DAILINK_DEF(deepbuffer,
302 	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
303 
304 SND_SOC_DAILINK_DEF(ssp2_port,
305 	DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
306 SND_SOC_DAILINK_DEF(ssp2_codec,
307 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8316:00", "ES8316 HiFi")));
308 
309 SND_SOC_DAILINK_DEF(platform,
310 	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
311 
312 static struct snd_soc_dai_link byt_cht_es8316_dais[] = {
313 	[MERR_DPCM_AUDIO] = {
314 		.name = "Audio Port",
315 		.stream_name = "Audio",
316 		.nonatomic = true,
317 		.dynamic = 1,
318 		.ops = &byt_cht_es8316_aif1_ops,
319 		SND_SOC_DAILINK_REG(media, dummy, platform),
320 	},
321 
322 	[MERR_DPCM_DEEP_BUFFER] = {
323 		.name = "Deep-Buffer Audio Port",
324 		.stream_name = "Deep-Buffer Audio",
325 		.nonatomic = true,
326 		.dynamic = 1,
327 		.playback_only = 1,
328 		.ops = &byt_cht_es8316_aif1_ops,
329 		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
330 	},
331 
332 		/* back ends */
333 	{
334 		.name = "SSP2-Codec",
335 		.id = 0,
336 		.no_pcm = 1,
337 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
338 						| SND_SOC_DAIFMT_CBC_CFC,
339 		.be_hw_params_fixup = byt_cht_es8316_codec_fixup,
340 		.init = byt_cht_es8316_init,
341 		SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
342 	},
343 };
344 
345 
346 /* SoC card */
347 static char codec_name[SND_ACPI_I2C_ID_LEN];
348 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
349 static char long_name[50]; /* = "bytcht-es8316-*-spk-*-mic" */
350 #endif
351 static char components_string[32]; /* = "cfg-spk:* cfg-mic:* */
352 
353 static int byt_cht_es8316_suspend(struct snd_soc_card *card)
354 {
355 	struct snd_soc_component *component;
356 
357 	for_each_card_components(card, component) {
358 		if (!strcmp(component->name, codec_name)) {
359 			dev_dbg(component->dev, "disabling jack detect before suspend\n");
360 			snd_soc_component_set_jack(component, NULL, NULL);
361 			break;
362 		}
363 	}
364 
365 	return 0;
366 }
367 
368 static int byt_cht_es8316_resume(struct snd_soc_card *card)
369 {
370 	struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
371 	struct snd_soc_component *component;
372 
373 	for_each_card_components(card, component) {
374 		if (!strcmp(component->name, codec_name)) {
375 			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
376 			snd_soc_component_set_jack(component, &priv->jack, NULL);
377 			break;
378 		}
379 	}
380 
381 	/*
382 	 * Some Cherry Trail boards with an ES8316 codec have a bug in their
383 	 * ACPI tables where the MSSL1680 touchscreen's _PS0 and _PS3 methods
384 	 * wrongly also set the speaker-enable GPIO to 1/0. Testing has shown
385 	 * that this really is a bug and the GPIO has no influence on the
386 	 * touchscreen at all.
387 	 *
388 	 * The silead.c touchscreen driver does not support runtime suspend, so
389 	 * the GPIO can only be changed underneath us during a system suspend.
390 	 * This resume() function runs from a pm complete() callback, and thus
391 	 * is guaranteed to run after the touchscreen driver/ACPI-subsys has
392 	 * brought the touchscreen back up again (and thus changed the GPIO).
393 	 *
394 	 * So to work around this we pass GPIOD_FLAGS_BIT_NONEXCLUSIVE when
395 	 * requesting the GPIO and we set its value here to undo any changes
396 	 * done by the touchscreen's broken _PS0 ACPI method.
397 	 */
398 	gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
399 
400 	return 0;
401 }
402 
403 /* use space before codec name to simplify card ID, and simplify driver name */
404 #define SOF_CARD_NAME "bytcht es8316" /* card name will be 'sof-bytcht es8316' */
405 #define SOF_DRIVER_NAME "SOF"
406 
407 #define CARD_NAME "bytcht-es8316"
408 #define DRIVER_NAME NULL /* card name will be used for driver name */
409 
410 static struct snd_soc_card byt_cht_es8316_card = {
411 	.owner = THIS_MODULE,
412 	.dai_link = byt_cht_es8316_dais,
413 	.num_links = ARRAY_SIZE(byt_cht_es8316_dais),
414 	.dapm_widgets = byt_cht_es8316_widgets,
415 	.num_dapm_widgets = ARRAY_SIZE(byt_cht_es8316_widgets),
416 	.dapm_routes = byt_cht_es8316_audio_map,
417 	.num_dapm_routes = ARRAY_SIZE(byt_cht_es8316_audio_map),
418 	.controls = byt_cht_es8316_controls,
419 	.num_controls = ARRAY_SIZE(byt_cht_es8316_controls),
420 	.fully_routed = true,
421 	.suspend_pre = byt_cht_es8316_suspend,
422 	.resume_post = byt_cht_es8316_resume,
423 };
424 
425 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
426 
427 static const struct acpi_gpio_mapping byt_cht_es8316_gpios[] = {
428 	{ "speaker-enable-gpios", &first_gpio, 1 },
429 	{ },
430 };
431 
432 /* Please keep this list alphabetically sorted */
433 static const struct dmi_system_id byt_cht_es8316_quirk_table[] = {
434 	{	/* Irbis NB41 */
435 		.matches = {
436 			DMI_MATCH(DMI_SYS_VENDOR, "IRBIS"),
437 			DMI_MATCH(DMI_PRODUCT_NAME, "NB41"),
438 		},
439 		.driver_data = (void *)(BYT_CHT_ES8316_SSP0
440 					| BYT_CHT_ES8316_INTMIC_IN2_MAP
441 					| BYT_CHT_ES8316_JD_INVERTED),
442 	},
443 	{	/* Nanote UMPC-01 */
444 		.matches = {
445 			DMI_MATCH(DMI_SYS_VENDOR, "RWC CO.,LTD"),
446 			DMI_MATCH(DMI_PRODUCT_NAME, "UMPC-01"),
447 		},
448 		.driver_data = (void *)BYT_CHT_ES8316_INTMIC_IN1_MAP,
449 	},
450 	{	/* Teclast X98 Plus II */
451 		.matches = {
452 			DMI_MATCH(DMI_SYS_VENDOR, "TECLAST"),
453 			DMI_MATCH(DMI_PRODUCT_NAME, "X98 Plus II"),
454 		},
455 		.driver_data = (void *)(BYT_CHT_ES8316_INTMIC_IN1_MAP
456 					| BYT_CHT_ES8316_JD_INVERTED),
457 	},
458 	{}
459 };
460 
461 static int byt_cht_es8316_get_quirks_from_dsm(struct byt_cht_es8316_private *priv,
462 					      bool is_bytcr)
463 {
464 	int ret, val1, val2, dsm_quirk = 0;
465 
466 	if (is_bytcr)
467 		dsm_quirk |= BYT_CHT_ES8316_SSP0;
468 
469 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_MAINMIC_TYPE_ARG, &val1);
470 	if (ret < 0)
471 		return ret;
472 
473 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPMIC_TYPE_ARG, &val2);
474 	if (ret < 0)
475 		return ret;
476 
477 	if (val1 == PLATFORM_MIC_AMIC_LIN1RIN1 && val2 == PLATFORM_MIC_AMIC_LIN2RIN2) {
478 		dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN1_MAP;
479 	} else if (val1 == PLATFORM_MIC_AMIC_LIN2RIN2 && val2 == PLATFORM_MIC_AMIC_LIN1RIN1) {
480 		dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN2_MAP;
481 	} else {
482 		dev_warn(priv->codec_dev, "Unknown mic settings mainmic 0x%02x hpmic 0x%02x\n",
483 			 val1, val2);
484 		return -EINVAL;
485 	}
486 
487 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_SPK_TYPE_ARG, &val1);
488 	if (ret < 0)
489 		return ret;
490 
491 	switch (val1) {
492 	case PLATFORM_SPK_MONO:
493 		dsm_quirk |= BYT_CHT_ES8316_MONO_SPEAKER;
494 		break;
495 	case PLATFORM_SPK_STEREO:
496 		break;
497 	default:
498 		dev_warn(priv->codec_dev, "Unknown speaker setting 0x%02x\n", val1);
499 		return -EINVAL;
500 	}
501 
502 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPDET_INV_ARG, &val1);
503 	if (ret < 0)
504 		return ret;
505 
506 	switch (val1) {
507 	case PLATFORM_HPDET_NORMAL:
508 		break;
509 	case PLATFORM_HPDET_INVERTED:
510 		dsm_quirk |= BYT_CHT_ES8316_JD_INVERTED;
511 		break;
512 	default:
513 		dev_warn(priv->codec_dev, "Unknown hpdet-inv setting 0x%02x\n", val1);
514 		return -EINVAL;
515 	}
516 
517 	quirk = dsm_quirk;
518 	return 0;
519 }
520 
521 static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
522 {
523 	struct device *dev = &pdev->dev;
524 	static const char * const mic_name[] = { "in1", "in2" };
525 	struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
526 	struct property_entry props[MAX_NO_PROPS] = {};
527 	struct byt_cht_es8316_private *priv;
528 	const struct dmi_system_id *dmi_id;
529 	struct fwnode_handle *fwnode;
530 	bool sof_parent, is_bytcr;
531 	const char *platform_name;
532 	struct acpi_device *adev;
533 	struct device *codec_dev;
534 	unsigned int cnt = 0;
535 	int dai_index = 0;
536 	int i;
537 	int ret = 0;
538 
539 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
540 	if (!priv)
541 		return -ENOMEM;
542 
543 	/* fix index of codec dai */
544 	for (i = 0; i < ARRAY_SIZE(byt_cht_es8316_dais); i++) {
545 		if (byt_cht_es8316_dais[i].num_codecs &&
546 		    !strcmp(byt_cht_es8316_dais[i].codecs->name,
547 			    "i2c-ESSX8316:00")) {
548 			dai_index = i;
549 			break;
550 		}
551 	}
552 
553 	/* fixup codec name based on HID */
554 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
555 	if (adev) {
556 		snprintf(codec_name, sizeof(codec_name),
557 			 "i2c-%s", acpi_dev_name(adev));
558 		byt_cht_es8316_dais[dai_index].codecs->name = codec_name;
559 	} else {
560 		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
561 		return -ENOENT;
562 	}
563 
564 	codec_dev = acpi_get_first_physical_node(adev);
565 	acpi_dev_put(adev);
566 	if (!codec_dev)
567 		return -EPROBE_DEFER;
568 	priv->codec_dev = get_device(codec_dev);
569 
570 	/* override platform name, if required */
571 	byt_cht_es8316_card.dev = dev;
572 	platform_name = mach->mach_params.platform;
573 
574 	ret = snd_soc_fixup_dai_links_platform_name(&byt_cht_es8316_card,
575 						    platform_name);
576 	if (ret) {
577 		put_device(codec_dev);
578 		return ret;
579 	}
580 
581 	es83xx_dsm_dump(priv->codec_dev);
582 
583 	/* Check for BYTCR or other platform and setup quirks */
584 	is_bytcr = soc_intel_is_byt() && mach->mach_params.acpi_ipc_irq_index == 0;
585 	dmi_id = dmi_first_match(byt_cht_es8316_quirk_table);
586 	if (dmi_id) {
587 		quirk = (unsigned long)dmi_id->driver_data;
588 	} else if (!byt_cht_es8316_get_quirks_from_dsm(priv, is_bytcr)) {
589 		dev_info(dev, "Using ACPI DSM info for quirks\n");
590 	} else if (is_bytcr) {
591 		/* On BYTCR default to SSP0, internal-mic-in2-map, mono-spk */
592 		quirk = BYT_CHT_ES8316_SSP0 | BYT_CHT_ES8316_INTMIC_IN2_MAP |
593 			BYT_CHT_ES8316_MONO_SPEAKER;
594 	} else {
595 		/* Others default to internal-mic-in1-map, mono-speaker */
596 		quirk = BYT_CHT_ES8316_INTMIC_IN1_MAP |
597 			BYT_CHT_ES8316_MONO_SPEAKER;
598 	}
599 	if (quirk_override != -1) {
600 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
601 			 quirk, quirk_override);
602 		quirk = quirk_override;
603 	}
604 	log_quirks(dev);
605 
606 	if (quirk & BYT_CHT_ES8316_SSP0)
607 		byt_cht_es8316_dais[dai_index].cpus->dai_name = "ssp0-port";
608 
609 	/* get the clock */
610 	priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
611 	if (IS_ERR(priv->mclk)) {
612 		put_device(codec_dev);
613 		return dev_err_probe(dev, PTR_ERR(priv->mclk), "clk_get pmc_plt_clk_3 failed\n");
614 	}
615 
616 	if (quirk & BYT_CHT_ES8316_JD_INVERTED)
617 		props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
618 
619 	if (cnt) {
620 		fwnode = fwnode_create_software_node(props, NULL);
621 		if (IS_ERR(fwnode)) {
622 			put_device(codec_dev);
623 			return PTR_ERR(fwnode);
624 		}
625 
626 		ret = device_add_software_node(codec_dev, to_software_node(fwnode));
627 
628 		fwnode_handle_put(fwnode);
629 
630 		if (ret) {
631 			put_device(codec_dev);
632 			return ret;
633 		}
634 	}
635 
636 	/* get speaker enable GPIO */
637 	devm_acpi_dev_add_driver_gpios(codec_dev, byt_cht_es8316_gpios);
638 	priv->speaker_en_gpio =
639 		gpiod_get_optional(codec_dev, "speaker-enable",
640 				   /* see comment in byt_cht_es8316_resume() */
641 				   GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
642 	if (IS_ERR(priv->speaker_en_gpio)) {
643 		ret = dev_err_probe(dev, PTR_ERR(priv->speaker_en_gpio),
644 				    "get speaker GPIO failed\n");
645 		goto err_put_codec;
646 	}
647 
648 	snprintf(components_string, sizeof(components_string),
649 		 "cfg-spk:%s cfg-mic:%s",
650 		 (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "1" : "2",
651 		 mic_name[BYT_CHT_ES8316_MAP(quirk)]);
652 	byt_cht_es8316_card.components = components_string;
653 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
654 	snprintf(long_name, sizeof(long_name), "bytcht-es8316-%s-spk-%s-mic",
655 		 (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "mono" : "stereo",
656 		 mic_name[BYT_CHT_ES8316_MAP(quirk)]);
657 	byt_cht_es8316_card.long_name = long_name;
658 #endif
659 
660 	sof_parent = snd_soc_acpi_sof_parent(dev);
661 
662 	/* set card and driver name */
663 	if (sof_parent) {
664 		byt_cht_es8316_card.name = SOF_CARD_NAME;
665 		byt_cht_es8316_card.driver_name = SOF_DRIVER_NAME;
666 	} else {
667 		byt_cht_es8316_card.name = CARD_NAME;
668 		byt_cht_es8316_card.driver_name = DRIVER_NAME;
669 	}
670 
671 	/* set pm ops */
672 	if (sof_parent)
673 		dev->driver->pm = &snd_soc_pm_ops;
674 
675 	/* register the soc card */
676 	snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
677 
678 	ret = devm_snd_soc_register_card(dev, &byt_cht_es8316_card);
679 	if (ret) {
680 		gpiod_put(priv->speaker_en_gpio);
681 		dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
682 		goto err_put_codec;
683 	}
684 	platform_set_drvdata(pdev, &byt_cht_es8316_card);
685 	return 0;
686 
687 err_put_codec:
688 	device_remove_software_node(priv->codec_dev);
689 	put_device(priv->codec_dev);
690 	return ret;
691 }
692 
693 static void snd_byt_cht_es8316_mc_remove(struct platform_device *pdev)
694 {
695 	struct snd_soc_card *card = platform_get_drvdata(pdev);
696 	struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
697 
698 	gpiod_put(priv->speaker_en_gpio);
699 	device_remove_software_node(priv->codec_dev);
700 	put_device(priv->codec_dev);
701 }
702 
703 static struct platform_driver snd_byt_cht_es8316_mc_driver = {
704 	.driver = {
705 		.name = "bytcht_es8316",
706 	},
707 	.probe = snd_byt_cht_es8316_mc_probe,
708 	.remove = snd_byt_cht_es8316_mc_remove,
709 };
710 
711 module_platform_driver(snd_byt_cht_es8316_mc_driver);
712 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");
713 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
714 MODULE_LICENSE("GPL v2");
715 MODULE_ALIAS("platform:bytcht_es8316");
716