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