xref: /linux/sound/soc/intel/boards/bytcht_es8316.c (revision 55d0969c451159cff86949b38c39171cab962069)
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 		.dpcm_playback = 1,
319 		.dpcm_capture = 1,
320 		.ops = &byt_cht_es8316_aif1_ops,
321 		SND_SOC_DAILINK_REG(media, dummy, platform),
322 	},
323 
324 	[MERR_DPCM_DEEP_BUFFER] = {
325 		.name = "Deep-Buffer Audio Port",
326 		.stream_name = "Deep-Buffer Audio",
327 		.nonatomic = true,
328 		.dynamic = 1,
329 		.dpcm_playback = 1,
330 		.ops = &byt_cht_es8316_aif1_ops,
331 		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
332 	},
333 
334 		/* back ends */
335 	{
336 		.name = "SSP2-Codec",
337 		.id = 0,
338 		.no_pcm = 1,
339 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
340 						| SND_SOC_DAIFMT_CBC_CFC,
341 		.be_hw_params_fixup = byt_cht_es8316_codec_fixup,
342 		.dpcm_playback = 1,
343 		.dpcm_capture = 1,
344 		.init = byt_cht_es8316_init,
345 		SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
346 	},
347 };
348 
349 
350 /* SoC card */
351 static char codec_name[SND_ACPI_I2C_ID_LEN];
352 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
353 static char long_name[50]; /* = "bytcht-es8316-*-spk-*-mic" */
354 #endif
355 static char components_string[32]; /* = "cfg-spk:* cfg-mic:* */
356 
357 static int byt_cht_es8316_suspend(struct snd_soc_card *card)
358 {
359 	struct snd_soc_component *component;
360 
361 	for_each_card_components(card, component) {
362 		if (!strcmp(component->name, codec_name)) {
363 			dev_dbg(component->dev, "disabling jack detect before suspend\n");
364 			snd_soc_component_set_jack(component, NULL, NULL);
365 			break;
366 		}
367 	}
368 
369 	return 0;
370 }
371 
372 static int byt_cht_es8316_resume(struct snd_soc_card *card)
373 {
374 	struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
375 	struct snd_soc_component *component;
376 
377 	for_each_card_components(card, component) {
378 		if (!strcmp(component->name, codec_name)) {
379 			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
380 			snd_soc_component_set_jack(component, &priv->jack, NULL);
381 			break;
382 		}
383 	}
384 
385 	/*
386 	 * Some Cherry Trail boards with an ES8316 codec have a bug in their
387 	 * ACPI tables where the MSSL1680 touchscreen's _PS0 and _PS3 methods
388 	 * wrongly also set the speaker-enable GPIO to 1/0. Testing has shown
389 	 * that this really is a bug and the GPIO has no influence on the
390 	 * touchscreen at all.
391 	 *
392 	 * The silead.c touchscreen driver does not support runtime suspend, so
393 	 * the GPIO can only be changed underneath us during a system suspend.
394 	 * This resume() function runs from a pm complete() callback, and thus
395 	 * is guaranteed to run after the touchscreen driver/ACPI-subsys has
396 	 * brought the touchscreen back up again (and thus changed the GPIO).
397 	 *
398 	 * So to work around this we pass GPIOD_FLAGS_BIT_NONEXCLUSIVE when
399 	 * requesting the GPIO and we set its value here to undo any changes
400 	 * done by the touchscreen's broken _PS0 ACPI method.
401 	 */
402 	gpiod_set_value_cansleep(priv->speaker_en_gpio, priv->speaker_en);
403 
404 	return 0;
405 }
406 
407 /* use space before codec name to simplify card ID, and simplify driver name */
408 #define SOF_CARD_NAME "bytcht es8316" /* card name will be 'sof-bytcht es8316' */
409 #define SOF_DRIVER_NAME "SOF"
410 
411 #define CARD_NAME "bytcht-es8316"
412 #define DRIVER_NAME NULL /* card name will be used for driver name */
413 
414 static struct snd_soc_card byt_cht_es8316_card = {
415 	.owner = THIS_MODULE,
416 	.dai_link = byt_cht_es8316_dais,
417 	.num_links = ARRAY_SIZE(byt_cht_es8316_dais),
418 	.dapm_widgets = byt_cht_es8316_widgets,
419 	.num_dapm_widgets = ARRAY_SIZE(byt_cht_es8316_widgets),
420 	.dapm_routes = byt_cht_es8316_audio_map,
421 	.num_dapm_routes = ARRAY_SIZE(byt_cht_es8316_audio_map),
422 	.controls = byt_cht_es8316_controls,
423 	.num_controls = ARRAY_SIZE(byt_cht_es8316_controls),
424 	.fully_routed = true,
425 	.suspend_pre = byt_cht_es8316_suspend,
426 	.resume_post = byt_cht_es8316_resume,
427 };
428 
429 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
430 
431 static const struct acpi_gpio_mapping byt_cht_es8316_gpios[] = {
432 	{ "speaker-enable-gpios", &first_gpio, 1 },
433 	{ },
434 };
435 
436 /* Please keep this list alphabetically sorted */
437 static const struct dmi_system_id byt_cht_es8316_quirk_table[] = {
438 	{	/* Irbis NB41 */
439 		.matches = {
440 			DMI_MATCH(DMI_SYS_VENDOR, "IRBIS"),
441 			DMI_MATCH(DMI_PRODUCT_NAME, "NB41"),
442 		},
443 		.driver_data = (void *)(BYT_CHT_ES8316_SSP0
444 					| BYT_CHT_ES8316_INTMIC_IN2_MAP
445 					| BYT_CHT_ES8316_JD_INVERTED),
446 	},
447 	{	/* Nanote UMPC-01 */
448 		.matches = {
449 			DMI_MATCH(DMI_SYS_VENDOR, "RWC CO.,LTD"),
450 			DMI_MATCH(DMI_PRODUCT_NAME, "UMPC-01"),
451 		},
452 		.driver_data = (void *)BYT_CHT_ES8316_INTMIC_IN1_MAP,
453 	},
454 	{	/* Teclast X98 Plus II */
455 		.matches = {
456 			DMI_MATCH(DMI_SYS_VENDOR, "TECLAST"),
457 			DMI_MATCH(DMI_PRODUCT_NAME, "X98 Plus II"),
458 		},
459 		.driver_data = (void *)(BYT_CHT_ES8316_INTMIC_IN1_MAP
460 					| BYT_CHT_ES8316_JD_INVERTED),
461 	},
462 	{}
463 };
464 
465 static int byt_cht_es8316_get_quirks_from_dsm(struct byt_cht_es8316_private *priv,
466 					      bool is_bytcr)
467 {
468 	int ret, val1, val2, dsm_quirk = 0;
469 
470 	if (is_bytcr)
471 		dsm_quirk |= BYT_CHT_ES8316_SSP0;
472 
473 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_MAINMIC_TYPE_ARG, &val1);
474 	if (ret < 0)
475 		return ret;
476 
477 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPMIC_TYPE_ARG, &val2);
478 	if (ret < 0)
479 		return ret;
480 
481 	if (val1 == PLATFORM_MIC_AMIC_LIN1RIN1 && val2 == PLATFORM_MIC_AMIC_LIN2RIN2) {
482 		dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN1_MAP;
483 	} else if (val1 == PLATFORM_MIC_AMIC_LIN2RIN2 && val2 == PLATFORM_MIC_AMIC_LIN1RIN1) {
484 		dsm_quirk |= BYT_CHT_ES8316_INTMIC_IN2_MAP;
485 	} else {
486 		dev_warn(priv->codec_dev, "Unknown mic settings mainmic 0x%02x hpmic 0x%02x\n",
487 			 val1, val2);
488 		return -EINVAL;
489 	}
490 
491 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_SPK_TYPE_ARG, &val1);
492 	if (ret < 0)
493 		return ret;
494 
495 	switch (val1) {
496 	case PLATFORM_SPK_MONO:
497 		dsm_quirk |= BYT_CHT_ES8316_MONO_SPEAKER;
498 		break;
499 	case PLATFORM_SPK_STEREO:
500 		break;
501 	default:
502 		dev_warn(priv->codec_dev, "Unknown speaker setting 0x%02x\n", val1);
503 		return -EINVAL;
504 	}
505 
506 	ret = es83xx_dsm(priv->codec_dev, PLATFORM_HPDET_INV_ARG, &val1);
507 	if (ret < 0)
508 		return ret;
509 
510 	switch (val1) {
511 	case PLATFORM_HPDET_NORMAL:
512 		break;
513 	case PLATFORM_HPDET_INVERTED:
514 		dsm_quirk |= BYT_CHT_ES8316_JD_INVERTED;
515 		break;
516 	default:
517 		dev_warn(priv->codec_dev, "Unknown hpdet-inv setting 0x%02x\n", val1);
518 		return -EINVAL;
519 	}
520 
521 	quirk = dsm_quirk;
522 	return 0;
523 }
524 
525 static int snd_byt_cht_es8316_mc_probe(struct platform_device *pdev)
526 {
527 	struct device *dev = &pdev->dev;
528 	static const char * const mic_name[] = { "in1", "in2" };
529 	struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
530 	struct property_entry props[MAX_NO_PROPS] = {};
531 	struct byt_cht_es8316_private *priv;
532 	const struct dmi_system_id *dmi_id;
533 	struct fwnode_handle *fwnode;
534 	bool sof_parent, is_bytcr;
535 	const char *platform_name;
536 	struct acpi_device *adev;
537 	struct device *codec_dev;
538 	unsigned int cnt = 0;
539 	int dai_index = 0;
540 	int i;
541 	int ret = 0;
542 
543 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
544 	if (!priv)
545 		return -ENOMEM;
546 
547 	/* fix index of codec dai */
548 	for (i = 0; i < ARRAY_SIZE(byt_cht_es8316_dais); i++) {
549 		if (byt_cht_es8316_dais[i].num_codecs &&
550 		    !strcmp(byt_cht_es8316_dais[i].codecs->name,
551 			    "i2c-ESSX8316:00")) {
552 			dai_index = i;
553 			break;
554 		}
555 	}
556 
557 	/* fixup codec name based on HID */
558 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
559 	if (adev) {
560 		snprintf(codec_name, sizeof(codec_name),
561 			 "i2c-%s", acpi_dev_name(adev));
562 		byt_cht_es8316_dais[dai_index].codecs->name = codec_name;
563 	} else {
564 		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
565 		return -ENOENT;
566 	}
567 
568 	codec_dev = acpi_get_first_physical_node(adev);
569 	acpi_dev_put(adev);
570 	if (!codec_dev)
571 		return -EPROBE_DEFER;
572 	priv->codec_dev = get_device(codec_dev);
573 
574 	/* override platform name, if required */
575 	byt_cht_es8316_card.dev = dev;
576 	platform_name = mach->mach_params.platform;
577 
578 	ret = snd_soc_fixup_dai_links_platform_name(&byt_cht_es8316_card,
579 						    platform_name);
580 	if (ret) {
581 		put_device(codec_dev);
582 		return ret;
583 	}
584 
585 	es83xx_dsm_dump(priv->codec_dev);
586 
587 	/* Check for BYTCR or other platform and setup quirks */
588 	is_bytcr = soc_intel_is_byt() && mach->mach_params.acpi_ipc_irq_index == 0;
589 	dmi_id = dmi_first_match(byt_cht_es8316_quirk_table);
590 	if (dmi_id) {
591 		quirk = (unsigned long)dmi_id->driver_data;
592 	} else if (!byt_cht_es8316_get_quirks_from_dsm(priv, is_bytcr)) {
593 		dev_info(dev, "Using ACPI DSM info for quirks\n");
594 	} else if (is_bytcr) {
595 		/* On BYTCR default to SSP0, internal-mic-in2-map, mono-spk */
596 		quirk = BYT_CHT_ES8316_SSP0 | BYT_CHT_ES8316_INTMIC_IN2_MAP |
597 			BYT_CHT_ES8316_MONO_SPEAKER;
598 	} else {
599 		/* Others default to internal-mic-in1-map, mono-speaker */
600 		quirk = BYT_CHT_ES8316_INTMIC_IN1_MAP |
601 			BYT_CHT_ES8316_MONO_SPEAKER;
602 	}
603 	if (quirk_override != -1) {
604 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
605 			 quirk, quirk_override);
606 		quirk = quirk_override;
607 	}
608 	log_quirks(dev);
609 
610 	if (quirk & BYT_CHT_ES8316_SSP0)
611 		byt_cht_es8316_dais[dai_index].cpus->dai_name = "ssp0-port";
612 
613 	/* get the clock */
614 	priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
615 	if (IS_ERR(priv->mclk)) {
616 		put_device(codec_dev);
617 		return dev_err_probe(dev, PTR_ERR(priv->mclk), "clk_get pmc_plt_clk_3 failed\n");
618 	}
619 
620 	if (quirk & BYT_CHT_ES8316_JD_INVERTED)
621 		props[cnt++] = PROPERTY_ENTRY_BOOL("everest,jack-detect-inverted");
622 
623 	if (cnt) {
624 		fwnode = fwnode_create_software_node(props, NULL);
625 		if (IS_ERR(fwnode)) {
626 			put_device(codec_dev);
627 			return PTR_ERR(fwnode);
628 		}
629 
630 		ret = device_add_software_node(codec_dev, to_software_node(fwnode));
631 
632 		fwnode_handle_put(fwnode);
633 
634 		if (ret) {
635 			put_device(codec_dev);
636 			return ret;
637 		}
638 	}
639 
640 	/* get speaker enable GPIO */
641 	devm_acpi_dev_add_driver_gpios(codec_dev, byt_cht_es8316_gpios);
642 	priv->speaker_en_gpio =
643 		gpiod_get_optional(codec_dev, "speaker-enable",
644 				   /* see comment in byt_cht_es8316_resume() */
645 				   GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
646 	if (IS_ERR(priv->speaker_en_gpio)) {
647 		ret = dev_err_probe(dev, PTR_ERR(priv->speaker_en_gpio),
648 				    "get speaker GPIO failed\n");
649 		goto err_put_codec;
650 	}
651 
652 	snprintf(components_string, sizeof(components_string),
653 		 "cfg-spk:%s cfg-mic:%s",
654 		 (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "1" : "2",
655 		 mic_name[BYT_CHT_ES8316_MAP(quirk)]);
656 	byt_cht_es8316_card.components = components_string;
657 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
658 	snprintf(long_name, sizeof(long_name), "bytcht-es8316-%s-spk-%s-mic",
659 		 (quirk & BYT_CHT_ES8316_MONO_SPEAKER) ? "mono" : "stereo",
660 		 mic_name[BYT_CHT_ES8316_MAP(quirk)]);
661 	byt_cht_es8316_card.long_name = long_name;
662 #endif
663 
664 	sof_parent = snd_soc_acpi_sof_parent(dev);
665 
666 	/* set card and driver name */
667 	if (sof_parent) {
668 		byt_cht_es8316_card.name = SOF_CARD_NAME;
669 		byt_cht_es8316_card.driver_name = SOF_DRIVER_NAME;
670 	} else {
671 		byt_cht_es8316_card.name = CARD_NAME;
672 		byt_cht_es8316_card.driver_name = DRIVER_NAME;
673 	}
674 
675 	/* set pm ops */
676 	if (sof_parent)
677 		dev->driver->pm = &snd_soc_pm_ops;
678 
679 	/* register the soc card */
680 	snd_soc_card_set_drvdata(&byt_cht_es8316_card, priv);
681 
682 	ret = devm_snd_soc_register_card(dev, &byt_cht_es8316_card);
683 	if (ret) {
684 		gpiod_put(priv->speaker_en_gpio);
685 		dev_err(dev, "snd_soc_register_card failed: %d\n", ret);
686 		goto err_put_codec;
687 	}
688 	platform_set_drvdata(pdev, &byt_cht_es8316_card);
689 	return 0;
690 
691 err_put_codec:
692 	device_remove_software_node(priv->codec_dev);
693 	put_device(priv->codec_dev);
694 	return ret;
695 }
696 
697 static void snd_byt_cht_es8316_mc_remove(struct platform_device *pdev)
698 {
699 	struct snd_soc_card *card = platform_get_drvdata(pdev);
700 	struct byt_cht_es8316_private *priv = snd_soc_card_get_drvdata(card);
701 
702 	gpiod_put(priv->speaker_en_gpio);
703 	device_remove_software_node(priv->codec_dev);
704 	put_device(priv->codec_dev);
705 }
706 
707 static struct platform_driver snd_byt_cht_es8316_mc_driver = {
708 	.driver = {
709 		.name = "bytcht_es8316",
710 	},
711 	.probe = snd_byt_cht_es8316_mc_probe,
712 	.remove = snd_byt_cht_es8316_mc_remove,
713 };
714 
715 module_platform_driver(snd_byt_cht_es8316_mc_driver);
716 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail Machine driver");
717 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
718 MODULE_LICENSE("GPL v2");
719 MODULE_ALIAS("platform:bytcht_es8316");
720