1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
4  *
5  *  Copyright (C) 2014 Intel Corp
6  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  */
11 
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/platform_device.h>
17 #include <linux/acpi.h>
18 #include <linux/clk.h>
19 #include <linux/device.h>
20 #include <linux/dmi.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio/machine.h>
23 #include <linux/input.h>
24 #include <linux/slab.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/jack.h>
29 #include <sound/soc-acpi.h>
30 #include <dt-bindings/sound/rt5640.h>
31 #include "../../codecs/rt5640.h"
32 #include "../atom/sst-atom-controls.h"
33 #include "../common/soc-intel-quirks.h"
34 
35 enum {
36 	BYT_RT5640_DMIC1_MAP,
37 	BYT_RT5640_DMIC2_MAP,
38 	BYT_RT5640_IN1_MAP,
39 	BYT_RT5640_IN3_MAP,
40 	BYT_RT5640_NO_INTERNAL_MIC_MAP,
41 };
42 
43 enum {
44 	BYT_RT5640_JD_SRC_GPIO1		= (RT5640_JD_SRC_GPIO1 << 4),
45 	BYT_RT5640_JD_SRC_JD1_IN4P	= (RT5640_JD_SRC_JD1_IN4P << 4),
46 	BYT_RT5640_JD_SRC_JD2_IN4N	= (RT5640_JD_SRC_JD2_IN4N << 4),
47 	BYT_RT5640_JD_SRC_GPIO2		= (RT5640_JD_SRC_GPIO2 << 4),
48 	BYT_RT5640_JD_SRC_GPIO3		= (RT5640_JD_SRC_GPIO3 << 4),
49 	BYT_RT5640_JD_SRC_GPIO4		= (RT5640_JD_SRC_GPIO4 << 4),
50 };
51 
52 enum {
53 	BYT_RT5640_OVCD_TH_600UA	= (6 << 8),
54 	BYT_RT5640_OVCD_TH_1500UA	= (15 << 8),
55 	BYT_RT5640_OVCD_TH_2000UA	= (20 << 8),
56 };
57 
58 enum {
59 	BYT_RT5640_OVCD_SF_0P5		= (RT5640_OVCD_SF_0P5 << 13),
60 	BYT_RT5640_OVCD_SF_0P75		= (RT5640_OVCD_SF_0P75 << 13),
61 	BYT_RT5640_OVCD_SF_1P0		= (RT5640_OVCD_SF_1P0 << 13),
62 	BYT_RT5640_OVCD_SF_1P5		= (RT5640_OVCD_SF_1P5 << 13),
63 };
64 
65 #define BYT_RT5640_MAP(quirk)		((quirk) &  GENMASK(3, 0))
66 #define BYT_RT5640_JDSRC(quirk)		(((quirk) & GENMASK(7, 4)) >> 4)
67 #define BYT_RT5640_OVCD_TH(quirk)	(((quirk) & GENMASK(12, 8)) >> 8)
68 #define BYT_RT5640_OVCD_SF(quirk)	(((quirk) & GENMASK(14, 13)) >> 13)
69 #define BYT_RT5640_JD_NOT_INV		BIT(16)
70 #define BYT_RT5640_MONO_SPEAKER		BIT(17)
71 #define BYT_RT5640_DIFF_MIC		BIT(18) /* default is single-ended */
72 #define BYT_RT5640_SSP2_AIF2		BIT(19) /* default is using AIF1  */
73 #define BYT_RT5640_SSP0_AIF1		BIT(20)
74 #define BYT_RT5640_SSP0_AIF2		BIT(21)
75 #define BYT_RT5640_MCLK_EN		BIT(22)
76 #define BYT_RT5640_MCLK_25MHZ		BIT(23)
77 #define BYT_RT5640_NO_SPEAKERS		BIT(24)
78 #define BYT_RT5640_LINEOUT		BIT(25)
79 #define BYT_RT5640_LINEOUT_AS_HP2	BIT(26)
80 #define BYT_RT5640_HSMIC2_ON_IN1	BIT(27)
81 #define BYT_RT5640_JD_HP_ELITEP_1000G2	BIT(28)
82 
83 #define BYTCR_INPUT_DEFAULTS				\
84 	(BYT_RT5640_IN3_MAP |				\
85 	 BYT_RT5640_JD_SRC_JD1_IN4P |			\
86 	 BYT_RT5640_OVCD_TH_2000UA |			\
87 	 BYT_RT5640_OVCD_SF_0P75 |			\
88 	 BYT_RT5640_DIFF_MIC)
89 
90 /* in-diff or dmic-pin + jdsrc + ovcd-th + -sf + jd-inv + terminating entry */
91 #define MAX_NO_PROPS 6
92 
93 struct byt_rt5640_private {
94 	struct snd_soc_jack jack;
95 	struct snd_soc_jack jack2;
96 	struct gpio_desc *hsmic_detect;
97 	struct clk *mclk;
98 	struct device *codec_dev;
99 };
100 static bool is_bytcr;
101 
102 static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
103 static int quirk_override = -1;
104 module_param_named(quirk, quirk_override, int, 0444);
105 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
106 
107 static void log_quirks(struct device *dev)
108 {
109 	int map;
110 	bool has_mclk = false;
111 	bool has_ssp0 = false;
112 	bool has_ssp0_aif1 = false;
113 	bool has_ssp0_aif2 = false;
114 	bool has_ssp2_aif2 = false;
115 
116 	map = BYT_RT5640_MAP(byt_rt5640_quirk);
117 	switch (map) {
118 	case BYT_RT5640_DMIC1_MAP:
119 		dev_info(dev, "quirk DMIC1_MAP enabled\n");
120 		break;
121 	case BYT_RT5640_DMIC2_MAP:
122 		dev_info(dev, "quirk DMIC2_MAP enabled\n");
123 		break;
124 	case BYT_RT5640_IN1_MAP:
125 		dev_info(dev, "quirk IN1_MAP enabled\n");
126 		break;
127 	case BYT_RT5640_IN3_MAP:
128 		dev_info(dev, "quirk IN3_MAP enabled\n");
129 		break;
130 	case BYT_RT5640_NO_INTERNAL_MIC_MAP:
131 		dev_info(dev, "quirk NO_INTERNAL_MIC_MAP enabled\n");
132 		break;
133 	default:
134 		dev_err(dev, "quirk map 0x%x is not supported, microphone input will not work\n", map);
135 		break;
136 	}
137 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
138 		dev_info(dev, "quirk HSMIC2_ON_IN1 enabled\n");
139 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
140 		dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
141 			 BYT_RT5640_JDSRC(byt_rt5640_quirk));
142 		dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
143 			 BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
144 		dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
145 			 BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
146 	}
147 	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
148 		dev_info(dev, "quirk JD_NOT_INV enabled\n");
149 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
150 		dev_info(dev, "quirk JD_HP_ELITEPAD_1000G2 enabled\n");
151 	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
152 		dev_info(dev, "quirk MONO_SPEAKER enabled\n");
153 	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)
154 		dev_info(dev, "quirk NO_SPEAKERS enabled\n");
155 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT)
156 		dev_info(dev, "quirk LINEOUT enabled\n");
157 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
158 		dev_info(dev, "quirk LINEOUT_AS_HP2 enabled\n");
159 	if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
160 		dev_info(dev, "quirk DIFF_MIC enabled\n");
161 	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
162 		dev_info(dev, "quirk SSP0_AIF1 enabled\n");
163 		has_ssp0 = true;
164 		has_ssp0_aif1 = true;
165 	}
166 	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
167 		dev_info(dev, "quirk SSP0_AIF2 enabled\n");
168 		has_ssp0 = true;
169 		has_ssp0_aif2 = true;
170 	}
171 	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
172 		dev_info(dev, "quirk SSP2_AIF2 enabled\n");
173 		has_ssp2_aif2 = true;
174 	}
175 	if (is_bytcr && !has_ssp0)
176 		dev_err(dev, "Invalid routing, bytcr detected but no SSP0-based quirk, audio cannot work with SSP2 on bytcr\n");
177 	if (has_ssp0_aif1 && has_ssp0_aif2)
178 		dev_err(dev, "Invalid routing, SSP0 cannot be connected to both AIF1 and AIF2\n");
179 	if (has_ssp0 && has_ssp2_aif2)
180 		dev_err(dev, "Invalid routing, cannot have both SSP0 and SSP2 connected to codec\n");
181 
182 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
183 		dev_info(dev, "quirk MCLK_EN enabled\n");
184 		has_mclk = true;
185 	}
186 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
187 		if (has_mclk)
188 			dev_info(dev, "quirk MCLK_25MHZ enabled\n");
189 		else
190 			dev_err(dev, "quirk MCLK_25MHZ enabled but quirk MCLK not selected, will be ignored\n");
191 	}
192 }
193 
194 static int byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
195 					      int rate)
196 {
197 	int ret;
198 
199 	/* Configure the PLL before selecting it */
200 	if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
201 		/* use bitclock as PLL input */
202 		if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
203 		    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
204 			/* 2x16 bit slots on SSP0 */
205 			ret = snd_soc_dai_set_pll(codec_dai, 0,
206 						  RT5640_PLL1_S_BCLK1,
207 						  rate * 32, rate * 512);
208 		} else {
209 			/* 2x15 bit slots on SSP2 */
210 			ret = snd_soc_dai_set_pll(codec_dai, 0,
211 						  RT5640_PLL1_S_BCLK1,
212 						  rate * 50, rate * 512);
213 		}
214 	} else {
215 		if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
216 			ret = snd_soc_dai_set_pll(codec_dai, 0,
217 						  RT5640_PLL1_S_MCLK,
218 						  25000000, rate * 512);
219 		} else {
220 			ret = snd_soc_dai_set_pll(codec_dai, 0,
221 						  RT5640_PLL1_S_MCLK,
222 						  19200000, rate * 512);
223 		}
224 	}
225 
226 	if (ret < 0) {
227 		dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
228 		return ret;
229 	}
230 
231 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
232 				     rate * 512, SND_SOC_CLOCK_IN);
233 	if (ret < 0) {
234 		dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
235 		return ret;
236 	}
237 
238 	return 0;
239 }
240 
241 #define BYT_CODEC_DAI1	"rt5640-aif1"
242 #define BYT_CODEC_DAI2	"rt5640-aif2"
243 
244 static struct snd_soc_dai *byt_rt5640_get_codec_dai(struct snd_soc_dapm_context *dapm)
245 {
246 	struct snd_soc_card *card = dapm->card;
247 	struct snd_soc_dai *codec_dai;
248 
249 	codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
250 	if (!codec_dai)
251 		codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
252 	if (!codec_dai)
253 		dev_err(card->dev, "Error codec dai not found\n");
254 
255 	return codec_dai;
256 }
257 
258 static int platform_clock_control(struct snd_soc_dapm_widget *w,
259 				  struct snd_kcontrol *k, int  event)
260 {
261 	struct snd_soc_dapm_context *dapm = w->dapm;
262 	struct snd_soc_card *card = dapm->card;
263 	struct snd_soc_dai *codec_dai;
264 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
265 	int ret;
266 
267 	codec_dai = byt_rt5640_get_codec_dai(dapm);
268 	if (!codec_dai)
269 		return -EIO;
270 
271 	if (SND_SOC_DAPM_EVENT_ON(event)) {
272 		ret = clk_prepare_enable(priv->mclk);
273 		if (ret < 0) {
274 			dev_err(card->dev, "could not configure MCLK state\n");
275 			return ret;
276 		}
277 		ret = byt_rt5640_prepare_and_enable_pll1(codec_dai, 48000);
278 	} else {
279 		/*
280 		 * Set codec clock source to internal clock before
281 		 * turning off the platform clock. Codec needs clock
282 		 * for Jack detection and button press
283 		 */
284 		ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
285 					     48000 * 512,
286 					     SND_SOC_CLOCK_IN);
287 		if (!ret)
288 			clk_disable_unprepare(priv->mclk);
289 	}
290 
291 	if (ret < 0) {
292 		dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
293 		return ret;
294 	}
295 
296 	return 0;
297 }
298 
299 static int byt_rt5640_event_lineout(struct snd_soc_dapm_widget *w,
300 			struct snd_kcontrol *k, int event)
301 {
302 	unsigned int gpio_ctrl3_val = RT5640_GP1_PF_OUT;
303 	struct snd_soc_dai *codec_dai;
304 
305 	if (!(byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2))
306 		return 0;
307 
308 	/*
309 	 * On devices which use line-out as a second headphones output,
310 	 * the codec's GPIO1 pin is used to enable an external HP-amp.
311 	 */
312 
313 	codec_dai = byt_rt5640_get_codec_dai(w->dapm);
314 	if (!codec_dai)
315 		return -EIO;
316 
317 	if (SND_SOC_DAPM_EVENT_ON(event))
318 		gpio_ctrl3_val |= RT5640_GP1_OUT_HI;
319 
320 	snd_soc_component_update_bits(codec_dai->component, RT5640_GPIO_CTRL3,
321 		RT5640_GP1_PF_MASK | RT5640_GP1_OUT_MASK, gpio_ctrl3_val);
322 
323 	return 0;
324 }
325 
326 static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
327 	SND_SOC_DAPM_HP("Headphone", NULL),
328 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
329 	SND_SOC_DAPM_MIC("Headset Mic 2", NULL),
330 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
331 	SND_SOC_DAPM_SPK("Speaker", NULL),
332 	SND_SOC_DAPM_LINE("Line Out", byt_rt5640_event_lineout),
333 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
334 			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
335 			    SND_SOC_DAPM_POST_PMD),
336 };
337 
338 static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
339 	{"Headphone", NULL, "Platform Clock"},
340 	{"Headset Mic", NULL, "Platform Clock"},
341 	{"Headset Mic", NULL, "MICBIAS1"},
342 	{"IN2P", NULL, "Headset Mic"},
343 	{"Headphone", NULL, "HPOL"},
344 	{"Headphone", NULL, "HPOR"},
345 };
346 
347 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
348 	{"Internal Mic", NULL, "Platform Clock"},
349 	{"DMIC1", NULL, "Internal Mic"},
350 };
351 
352 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
353 	{"Internal Mic", NULL, "Platform Clock"},
354 	{"DMIC2", NULL, "Internal Mic"},
355 };
356 
357 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
358 	{"Internal Mic", NULL, "Platform Clock"},
359 	{"Internal Mic", NULL, "MICBIAS1"},
360 	{"IN1P", NULL, "Internal Mic"},
361 };
362 
363 static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
364 	{"Internal Mic", NULL, "Platform Clock"},
365 	{"Internal Mic", NULL, "MICBIAS1"},
366 	{"IN3P", NULL, "Internal Mic"},
367 };
368 
369 static const struct snd_soc_dapm_route byt_rt5640_hsmic2_in1_map[] = {
370 	{"Headset Mic 2", NULL, "Platform Clock"},
371 	{"Headset Mic 2", NULL, "MICBIAS1"},
372 	{"IN1P", NULL, "Headset Mic 2"},
373 };
374 
375 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
376 	{"ssp2 Tx", NULL, "codec_out0"},
377 	{"ssp2 Tx", NULL, "codec_out1"},
378 	{"codec_in0", NULL, "ssp2 Rx"},
379 	{"codec_in1", NULL, "ssp2 Rx"},
380 
381 	{"AIF1 Playback", NULL, "ssp2 Tx"},
382 	{"ssp2 Rx", NULL, "AIF1 Capture"},
383 };
384 
385 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
386 	{"ssp2 Tx", NULL, "codec_out0"},
387 	{"ssp2 Tx", NULL, "codec_out1"},
388 	{"codec_in0", NULL, "ssp2 Rx"},
389 	{"codec_in1", NULL, "ssp2 Rx"},
390 
391 	{"AIF2 Playback", NULL, "ssp2 Tx"},
392 	{"ssp2 Rx", NULL, "AIF2 Capture"},
393 };
394 
395 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
396 	{"ssp0 Tx", NULL, "modem_out"},
397 	{"modem_in", NULL, "ssp0 Rx"},
398 
399 	{"AIF1 Playback", NULL, "ssp0 Tx"},
400 	{"ssp0 Rx", NULL, "AIF1 Capture"},
401 };
402 
403 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
404 	{"ssp0 Tx", NULL, "modem_out"},
405 	{"modem_in", NULL, "ssp0 Rx"},
406 
407 	{"AIF2 Playback", NULL, "ssp0 Tx"},
408 	{"ssp0 Rx", NULL, "AIF2 Capture"},
409 };
410 
411 static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
412 	{"Speaker", NULL, "Platform Clock"},
413 	{"Speaker", NULL, "SPOLP"},
414 	{"Speaker", NULL, "SPOLN"},
415 	{"Speaker", NULL, "SPORP"},
416 	{"Speaker", NULL, "SPORN"},
417 };
418 
419 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
420 	{"Speaker", NULL, "Platform Clock"},
421 	{"Speaker", NULL, "SPOLP"},
422 	{"Speaker", NULL, "SPOLN"},
423 };
424 
425 static const struct snd_soc_dapm_route byt_rt5640_lineout_map[] = {
426 	{"Line Out", NULL, "Platform Clock"},
427 	{"Line Out", NULL, "LOUTR"},
428 	{"Line Out", NULL, "LOUTL"},
429 };
430 
431 static const struct snd_kcontrol_new byt_rt5640_controls[] = {
432 	SOC_DAPM_PIN_SWITCH("Headphone"),
433 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
434 	SOC_DAPM_PIN_SWITCH("Headset Mic 2"),
435 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
436 	SOC_DAPM_PIN_SWITCH("Speaker"),
437 	SOC_DAPM_PIN_SWITCH("Line Out"),
438 };
439 
440 static struct snd_soc_jack_pin rt5640_pins[] = {
441 	{
442 		.pin	= "Headphone",
443 		.mask	= SND_JACK_HEADPHONE,
444 	},
445 	{
446 		.pin	= "Headset Mic",
447 		.mask	= SND_JACK_MICROPHONE,
448 	},
449 };
450 
451 static struct snd_soc_jack_pin rt5640_pins2[] = {
452 	{
453 		/* The 2nd headset jack uses lineout with an external HP-amp */
454 		.pin	= "Line Out",
455 		.mask	= SND_JACK_HEADPHONE,
456 	},
457 	{
458 		.pin	= "Headset Mic 2",
459 		.mask	= SND_JACK_MICROPHONE,
460 	},
461 };
462 
463 static struct snd_soc_jack_gpio rt5640_jack_gpio = {
464 	.name = "hp-detect",
465 	.report = SND_JACK_HEADSET,
466 	.invert = true,
467 	.debounce_time = 200,
468 };
469 
470 static struct snd_soc_jack_gpio rt5640_jack2_gpio = {
471 	.name = "hp2-detect",
472 	.report = SND_JACK_HEADSET,
473 	.invert = true,
474 	.debounce_time = 200,
475 };
476 
477 static const struct acpi_gpio_params acpi_gpio0 = { 0, 0, false };
478 static const struct acpi_gpio_params acpi_gpio1 = { 1, 0, false };
479 static const struct acpi_gpio_params acpi_gpio2 = { 2, 0, false };
480 
481 static const struct acpi_gpio_mapping byt_rt5640_hp_elitepad_1000g2_gpios[] = {
482 	{ "hp-detect-gpios", &acpi_gpio0, 1, },
483 	{ "headset-mic-detect-gpios", &acpi_gpio1, 1, },
484 	{ "hp2-detect-gpios", &acpi_gpio2, 1, },
485 	{ },
486 };
487 
488 static int byt_rt5640_hp_elitepad_1000g2_jack1_check(void *data)
489 {
490 	struct byt_rt5640_private *priv = data;
491 	int jack_status, mic_status;
492 
493 	jack_status = gpiod_get_value_cansleep(rt5640_jack_gpio.desc);
494 	if (jack_status)
495 		return 0;
496 
497 	mic_status = gpiod_get_value_cansleep(priv->hsmic_detect);
498 	if (mic_status)
499 		return SND_JACK_HEADPHONE;
500 	else
501 		return SND_JACK_HEADSET;
502 }
503 
504 static int byt_rt5640_hp_elitepad_1000g2_jack2_check(void *data)
505 {
506 	struct snd_soc_component *component = data;
507 	int jack_status, report;
508 
509 	jack_status = gpiod_get_value_cansleep(rt5640_jack2_gpio.desc);
510 	if (jack_status)
511 		return 0;
512 
513 	rt5640_enable_micbias1_for_ovcd(component);
514 	report = rt5640_detect_headset(component, rt5640_jack2_gpio.desc);
515 	rt5640_disable_micbias1_for_ovcd(component);
516 
517 	return report;
518 }
519 
520 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
521 					struct snd_pcm_hw_params *params)
522 {
523 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
524 	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
525 
526 	return byt_rt5640_prepare_and_enable_pll1(dai, params_rate(params));
527 }
528 
529 /* Please keep this list alphabetically sorted */
530 static const struct dmi_system_id byt_rt5640_quirk_table[] = {
531 	{	/* Acer Iconia Tab 8 W1-810 */
532 		.matches = {
533 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
534 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
535 		},
536 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
537 					BYT_RT5640_JD_SRC_JD1_IN4P |
538 					BYT_RT5640_OVCD_TH_1500UA |
539 					BYT_RT5640_OVCD_SF_0P75 |
540 					BYT_RT5640_SSP0_AIF1 |
541 					BYT_RT5640_MCLK_EN),
542 	},
543 	{	/* Acer One 10 S1002 */
544 		.matches = {
545 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
546 			DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
547 		},
548 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
549 					BYT_RT5640_JD_SRC_JD2_IN4N |
550 					BYT_RT5640_OVCD_TH_2000UA |
551 					BYT_RT5640_OVCD_SF_0P75 |
552 					BYT_RT5640_DIFF_MIC |
553 					BYT_RT5640_SSP0_AIF2 |
554 					BYT_RT5640_MCLK_EN),
555 	},
556 	{
557 		.matches = {
558 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
559 			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
560 		},
561 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
562 					BYT_RT5640_JD_SRC_JD2_IN4N |
563 					BYT_RT5640_OVCD_TH_2000UA |
564 					BYT_RT5640_OVCD_SF_0P75 |
565 					BYT_RT5640_SSP0_AIF1 |
566 					BYT_RT5640_MCLK_EN),
567 	},
568 	{
569 		.matches = {
570 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
571 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
572 		},
573 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
574 					BYT_RT5640_MONO_SPEAKER |
575 					BYT_RT5640_SSP0_AIF1 |
576 					BYT_RT5640_MCLK_EN),
577 	},
578 	{
579 		.matches = {
580 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
581 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
582 		},
583 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
584 					BYT_RT5640_JD_SRC_JD2_IN4N |
585 					BYT_RT5640_OVCD_TH_2000UA |
586 					BYT_RT5640_OVCD_SF_0P75 |
587 					BYT_RT5640_SSP0_AIF1 |
588 					BYT_RT5640_MCLK_EN),
589 	},
590 	{
591 		.matches = {
592 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
593 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ME176C"),
594 		},
595 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
596 					BYT_RT5640_JD_SRC_JD2_IN4N |
597 					BYT_RT5640_OVCD_TH_2000UA |
598 					BYT_RT5640_OVCD_SF_0P75 |
599 					BYT_RT5640_SSP0_AIF1 |
600 					BYT_RT5640_MCLK_EN),
601 	},
602 	{
603 		.matches = {
604 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
605 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
606 		},
607 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
608 					BYT_RT5640_JD_SRC_JD2_IN4N |
609 					BYT_RT5640_OVCD_TH_2000UA |
610 					BYT_RT5640_OVCD_SF_0P75 |
611 					BYT_RT5640_MCLK_EN),
612 	},
613 	{
614 		.matches = {
615 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
616 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
617 		},
618 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
619 					BYT_RT5640_JD_SRC_JD2_IN4N |
620 					BYT_RT5640_OVCD_TH_2000UA |
621 					BYT_RT5640_OVCD_SF_0P75 |
622 					BYT_RT5640_MONO_SPEAKER |
623 					BYT_RT5640_DIFF_MIC |
624 					BYT_RT5640_SSP0_AIF2 |
625 					BYT_RT5640_MCLK_EN),
626 	},
627 	{	/* Chuwi Vi8 (CWI506) */
628 		.matches = {
629 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
630 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
631 			/* The above are too generic, also match BIOS info */
632 			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
633 		},
634 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
635 					BYT_RT5640_MONO_SPEAKER |
636 					BYT_RT5640_SSP0_AIF1 |
637 					BYT_RT5640_MCLK_EN),
638 	},
639 	{
640 		/* Chuwi Vi10 (CWI505) */
641 		.matches = {
642 			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
643 			DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
644 			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
645 			DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
646 		},
647 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
648 					BYT_RT5640_JD_SRC_JD2_IN4N |
649 					BYT_RT5640_OVCD_TH_2000UA |
650 					BYT_RT5640_OVCD_SF_0P75 |
651 					BYT_RT5640_DIFF_MIC |
652 					BYT_RT5640_SSP0_AIF1 |
653 					BYT_RT5640_MCLK_EN),
654 	},
655 	{
656 		/* Chuwi Hi8 (CWI509) */
657 		.matches = {
658 			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
659 			DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
660 			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
661 			DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
662 		},
663 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
664 					BYT_RT5640_JD_SRC_JD2_IN4N |
665 					BYT_RT5640_OVCD_TH_2000UA |
666 					BYT_RT5640_OVCD_SF_0P75 |
667 					BYT_RT5640_MONO_SPEAKER |
668 					BYT_RT5640_DIFF_MIC |
669 					BYT_RT5640_SSP0_AIF1 |
670 					BYT_RT5640_MCLK_EN),
671 	},
672 	{
673 		.matches = {
674 			DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
675 			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
676 		},
677 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
678 	},
679 	{	/* Connect Tablet 9 */
680 		.matches = {
681 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
682 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
683 		},
684 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
685 					BYT_RT5640_MONO_SPEAKER |
686 					BYT_RT5640_SSP0_AIF1 |
687 					BYT_RT5640_MCLK_EN),
688 	},
689 	{
690 		.matches = {
691 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
692 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
693 		},
694 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
695 					BYT_RT5640_JD_SRC_JD2_IN4N |
696 					BYT_RT5640_OVCD_TH_2000UA |
697 					BYT_RT5640_OVCD_SF_0P75 |
698 					BYT_RT5640_MONO_SPEAKER |
699 					BYT_RT5640_MCLK_EN),
700 	},
701 	{	/* Estar Beauty HD MID 7316R */
702 		.matches = {
703 			DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
704 			DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
705 		},
706 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
707 					BYT_RT5640_MONO_SPEAKER |
708 					BYT_RT5640_SSP0_AIF1 |
709 					BYT_RT5640_MCLK_EN),
710 	},
711 	{	/* Glavey TM800A550L */
712 		.matches = {
713 			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
714 			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
715 			/* Above strings are too generic, also match on BIOS version */
716 			DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
717 		},
718 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
719 					BYT_RT5640_SSP0_AIF1 |
720 					BYT_RT5640_MCLK_EN),
721 	},
722 	{
723 		.matches = {
724 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
725 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
726 		},
727 		.driver_data = (void *)(BYT_RT5640_DMIC2_MAP |
728 					BYT_RT5640_MCLK_EN |
729 					BYT_RT5640_LINEOUT |
730 					BYT_RT5640_LINEOUT_AS_HP2 |
731 					BYT_RT5640_HSMIC2_ON_IN1 |
732 					BYT_RT5640_JD_HP_ELITEP_1000G2),
733 	},
734 	{	/* HP Pavilion x2 10-k0XX, 10-n0XX */
735 		.matches = {
736 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
737 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
738 		},
739 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
740 					BYT_RT5640_JD_SRC_JD2_IN4N |
741 					BYT_RT5640_OVCD_TH_1500UA |
742 					BYT_RT5640_OVCD_SF_0P75 |
743 					BYT_RT5640_SSP0_AIF1 |
744 					BYT_RT5640_MCLK_EN),
745 	},
746 	{	/* HP Pavilion x2 10-p0XX */
747 		.matches = {
748 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
749 			DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
750 		},
751 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
752 					BYT_RT5640_JD_SRC_JD1_IN4P |
753 					BYT_RT5640_OVCD_TH_2000UA |
754 					BYT_RT5640_OVCD_SF_0P75 |
755 					BYT_RT5640_MCLK_EN),
756 	},
757 	{	/* HP Stream 7 */
758 		.matches = {
759 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
760 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
761 		},
762 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
763 					BYT_RT5640_MONO_SPEAKER |
764 					BYT_RT5640_JD_NOT_INV |
765 					BYT_RT5640_SSP0_AIF1 |
766 					BYT_RT5640_MCLK_EN),
767 	},
768 	{	/* I.T.Works TW891 */
769 		.matches = {
770 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
771 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
772 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
773 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
774 		},
775 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
776 					BYT_RT5640_MONO_SPEAKER |
777 					BYT_RT5640_SSP0_AIF1 |
778 					BYT_RT5640_MCLK_EN),
779 	},
780 	{	/* Lamina I8270 / T701BR.SE */
781 		.matches = {
782 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
783 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
784 		},
785 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
786 					BYT_RT5640_MONO_SPEAKER |
787 					BYT_RT5640_JD_NOT_INV |
788 					BYT_RT5640_SSP0_AIF1 |
789 					BYT_RT5640_MCLK_EN),
790 	},
791 	{	/* Lenovo Miix 2 8 */
792 		.matches = {
793 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
794 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
795 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
796 		},
797 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
798 					BYT_RT5640_JD_SRC_JD2_IN4N |
799 					BYT_RT5640_OVCD_TH_2000UA |
800 					BYT_RT5640_OVCD_SF_0P75 |
801 					BYT_RT5640_MONO_SPEAKER |
802 					BYT_RT5640_MCLK_EN),
803 	},
804 	{	/* Lenovo Miix 3-830 */
805 		.matches = {
806 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
807 			DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
808 		},
809 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
810 					BYT_RT5640_JD_SRC_JD2_IN4N |
811 					BYT_RT5640_OVCD_TH_2000UA |
812 					BYT_RT5640_OVCD_SF_0P75 |
813 					BYT_RT5640_MONO_SPEAKER |
814 					BYT_RT5640_DIFF_MIC |
815 					BYT_RT5640_SSP0_AIF1 |
816 					BYT_RT5640_MCLK_EN),
817 	},
818 	{	/* Linx Linx7 tablet */
819 		.matches = {
820 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
821 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
822 		},
823 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
824 					BYT_RT5640_MONO_SPEAKER |
825 					BYT_RT5640_JD_NOT_INV |
826 					BYT_RT5640_SSP0_AIF1 |
827 					BYT_RT5640_MCLK_EN),
828 	},
829 	{	/* Mele PCG03 Mini PC */
830 		.matches = {
831 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
832 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
833 		},
834 		.driver_data = (void *)(BYT_RT5640_NO_INTERNAL_MIC_MAP |
835 					BYT_RT5640_NO_SPEAKERS |
836 					BYT_RT5640_SSP0_AIF1),
837 	},
838 	{	/* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
839 		.matches = {
840 			DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
841 			DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
842 		},
843 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
844 					BYT_RT5640_MONO_SPEAKER |
845 					BYT_RT5640_SSP0_AIF1 |
846 					BYT_RT5640_MCLK_EN),
847 	},
848 	{
849 		/* MPMAN MPWIN895CL */
850 		.matches = {
851 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
852 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
853 		},
854 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
855 					BYT_RT5640_MONO_SPEAKER |
856 					BYT_RT5640_SSP0_AIF1 |
857 					BYT_RT5640_MCLK_EN),
858 	},
859 	{	/* MSI S100 tablet */
860 		.matches = {
861 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
862 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
863 		},
864 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
865 					BYT_RT5640_JD_SRC_JD2_IN4N |
866 					BYT_RT5640_OVCD_TH_2000UA |
867 					BYT_RT5640_OVCD_SF_0P75 |
868 					BYT_RT5640_MONO_SPEAKER |
869 					BYT_RT5640_DIFF_MIC |
870 					BYT_RT5640_MCLK_EN),
871 	},
872 	{	/* Nuvison/TMax TM800W560 */
873 		.matches = {
874 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
875 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
876 		},
877 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
878 					BYT_RT5640_JD_SRC_JD2_IN4N |
879 					BYT_RT5640_OVCD_TH_2000UA |
880 					BYT_RT5640_OVCD_SF_0P75 |
881 					BYT_RT5640_JD_NOT_INV |
882 					BYT_RT5640_DIFF_MIC |
883 					BYT_RT5640_SSP0_AIF1 |
884 					BYT_RT5640_MCLK_EN),
885 	},
886 	{	/* Onda v975w */
887 		.matches = {
888 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
889 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
890 			/* The above are too generic, also match BIOS info */
891 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
892 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
893 		},
894 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
895 					BYT_RT5640_JD_SRC_JD2_IN4N |
896 					BYT_RT5640_OVCD_TH_2000UA |
897 					BYT_RT5640_OVCD_SF_0P75 |
898 					BYT_RT5640_DIFF_MIC |
899 					BYT_RT5640_MCLK_EN),
900 	},
901 	{	/* Pipo W4 */
902 		.matches = {
903 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
904 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
905 			/* The above are too generic, also match BIOS info */
906 			DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
907 		},
908 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
909 					BYT_RT5640_MONO_SPEAKER |
910 					BYT_RT5640_SSP0_AIF1 |
911 					BYT_RT5640_MCLK_EN),
912 	},
913 	{	/* Point of View Mobii TAB-P800W (V2.0) */
914 		.matches = {
915 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
916 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
917 			/* The above are too generic, also match BIOS info */
918 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
919 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
920 		},
921 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
922 					BYT_RT5640_JD_SRC_JD2_IN4N |
923 					BYT_RT5640_OVCD_TH_2000UA |
924 					BYT_RT5640_OVCD_SF_0P75 |
925 					BYT_RT5640_MONO_SPEAKER |
926 					BYT_RT5640_DIFF_MIC |
927 					BYT_RT5640_SSP0_AIF2 |
928 					BYT_RT5640_MCLK_EN),
929 	},
930 	{	/* Point of View Mobii TAB-P800W (V2.1) */
931 		.matches = {
932 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
933 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
934 			/* The above are too generic, also match BIOS info */
935 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
936 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
937 		},
938 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
939 					BYT_RT5640_JD_SRC_JD2_IN4N |
940 					BYT_RT5640_OVCD_TH_2000UA |
941 					BYT_RT5640_OVCD_SF_0P75 |
942 					BYT_RT5640_MONO_SPEAKER |
943 					BYT_RT5640_DIFF_MIC |
944 					BYT_RT5640_SSP0_AIF2 |
945 					BYT_RT5640_MCLK_EN),
946 	},
947 	{	/* Point of View Mobii TAB-P1005W-232 (V2.0) */
948 		.matches = {
949 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "POV"),
950 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "I102A"),
951 		},
952 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
953 					BYT_RT5640_JD_SRC_JD2_IN4N |
954 					BYT_RT5640_OVCD_TH_2000UA |
955 					BYT_RT5640_OVCD_SF_0P75 |
956 					BYT_RT5640_DIFF_MIC |
957 					BYT_RT5640_SSP0_AIF1 |
958 					BYT_RT5640_MCLK_EN),
959 	},
960 	{
961 		/* Prowise PT301 */
962 		.matches = {
963 			DMI_MATCH(DMI_SYS_VENDOR, "Prowise"),
964 			DMI_MATCH(DMI_PRODUCT_NAME, "PT301"),
965 		},
966 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
967 					BYT_RT5640_JD_SRC_JD2_IN4N |
968 					BYT_RT5640_OVCD_TH_2000UA |
969 					BYT_RT5640_OVCD_SF_0P75 |
970 					BYT_RT5640_DIFF_MIC |
971 					BYT_RT5640_SSP0_AIF1 |
972 					BYT_RT5640_MCLK_EN),
973 	},
974 	{
975 		/* Teclast X89 */
976 		.matches = {
977 			DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
978 			DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
979 		},
980 		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
981 					BYT_RT5640_JD_SRC_JD1_IN4P |
982 					BYT_RT5640_OVCD_TH_2000UA |
983 					BYT_RT5640_OVCD_SF_1P0 |
984 					BYT_RT5640_SSP0_AIF1 |
985 					BYT_RT5640_MCLK_EN),
986 	},
987 	{	/* Toshiba Satellite Click Mini L9W-B */
988 		.matches = {
989 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
990 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
991 		},
992 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
993 					BYT_RT5640_JD_SRC_JD2_IN4N |
994 					BYT_RT5640_OVCD_TH_1500UA |
995 					BYT_RT5640_OVCD_SF_0P75 |
996 					BYT_RT5640_SSP0_AIF1 |
997 					BYT_RT5640_MCLK_EN),
998 	},
999 	{	/* Toshiba Encore WT8-A */
1000 		.matches = {
1001 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1002 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
1003 		},
1004 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1005 					BYT_RT5640_JD_SRC_JD2_IN4N |
1006 					BYT_RT5640_OVCD_TH_2000UA |
1007 					BYT_RT5640_OVCD_SF_0P75 |
1008 					BYT_RT5640_JD_NOT_INV |
1009 					BYT_RT5640_MCLK_EN),
1010 	},
1011 	{	/* Toshiba Encore WT10-A */
1012 		.matches = {
1013 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1014 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
1015 		},
1016 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1017 					BYT_RT5640_JD_SRC_JD1_IN4P |
1018 					BYT_RT5640_OVCD_TH_2000UA |
1019 					BYT_RT5640_OVCD_SF_0P75 |
1020 					BYT_RT5640_SSP0_AIF2 |
1021 					BYT_RT5640_MCLK_EN),
1022 	},
1023 	{	/* Voyo Winpad A15 */
1024 		.matches = {
1025 			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1026 			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1027 			/* Above strings are too generic, also match on BIOS date */
1028 			DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
1029 		},
1030 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1031 					BYT_RT5640_JD_SRC_JD2_IN4N |
1032 					BYT_RT5640_OVCD_TH_2000UA |
1033 					BYT_RT5640_OVCD_SF_0P75 |
1034 					BYT_RT5640_DIFF_MIC |
1035 					BYT_RT5640_MCLK_EN),
1036 	},
1037 	{	/* Catch-all for generic Insyde tablets, must be last */
1038 		.matches = {
1039 			DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
1040 		},
1041 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1042 					BYT_RT5640_MCLK_EN |
1043 					BYT_RT5640_SSP0_AIF1),
1044 
1045 	},
1046 	{}
1047 };
1048 
1049 /*
1050  * Note this MUST be called before snd_soc_register_card(), so that the props
1051  * are in place before the codec component driver's probe function parses them.
1052  */
1053 static int byt_rt5640_add_codec_device_props(struct device *i2c_dev,
1054 					     struct byt_rt5640_private *priv)
1055 {
1056 	struct property_entry props[MAX_NO_PROPS] = {};
1057 	struct fwnode_handle *fwnode;
1058 	int cnt = 0;
1059 	int ret;
1060 
1061 	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1062 	case BYT_RT5640_DMIC1_MAP:
1063 		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
1064 						  RT5640_DMIC1_DATA_PIN_IN1P);
1065 		break;
1066 	case BYT_RT5640_DMIC2_MAP:
1067 		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
1068 						  RT5640_DMIC2_DATA_PIN_IN1N);
1069 		break;
1070 	case BYT_RT5640_IN1_MAP:
1071 		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1072 			props[cnt++] =
1073 				PROPERTY_ENTRY_BOOL("realtek,in1-differential");
1074 		break;
1075 	case BYT_RT5640_IN3_MAP:
1076 		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1077 			props[cnt++] =
1078 				PROPERTY_ENTRY_BOOL("realtek,in3-differential");
1079 		break;
1080 	}
1081 
1082 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1083 		props[cnt++] = PROPERTY_ENTRY_U32(
1084 				    "realtek,jack-detect-source",
1085 				    BYT_RT5640_JDSRC(byt_rt5640_quirk));
1086 
1087 		props[cnt++] = PROPERTY_ENTRY_U32(
1088 				    "realtek,over-current-threshold-microamp",
1089 				    BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
1090 
1091 		props[cnt++] = PROPERTY_ENTRY_U32(
1092 				    "realtek,over-current-scale-factor",
1093 				    BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
1094 	}
1095 
1096 	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
1097 		props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
1098 
1099 	fwnode = fwnode_create_software_node(props, NULL);
1100 	if (IS_ERR(fwnode)) {
1101 		/* put_device() is handled in caller */
1102 		return PTR_ERR(fwnode);
1103 	}
1104 
1105 	ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
1106 
1107 	fwnode_handle_put(fwnode);
1108 
1109 	return ret;
1110 }
1111 
1112 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
1113 {
1114 	struct snd_soc_card *card = runtime->card;
1115 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1116 	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
1117 	const struct snd_soc_dapm_route *custom_map = NULL;
1118 	int num_routes = 0;
1119 	int ret;
1120 
1121 	card->dapm.idle_bias_off = true;
1122 
1123 	/* Start with RC clk for jack-detect (we disable MCLK below) */
1124 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
1125 		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1126 			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
1127 
1128 	rt5640_sel_asrc_clk_src(component,
1129 				RT5640_DA_STEREO_FILTER |
1130 				RT5640_DA_MONO_L_FILTER	|
1131 				RT5640_DA_MONO_R_FILTER	|
1132 				RT5640_AD_STEREO_FILTER	|
1133 				RT5640_AD_MONO_L_FILTER	|
1134 				RT5640_AD_MONO_R_FILTER,
1135 				RT5640_CLK_SEL_ASRC);
1136 
1137 	ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
1138 					ARRAY_SIZE(byt_rt5640_controls));
1139 	if (ret) {
1140 		dev_err(card->dev, "unable to add card controls\n");
1141 		return ret;
1142 	}
1143 
1144 	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1145 	case BYT_RT5640_IN1_MAP:
1146 		custom_map = byt_rt5640_intmic_in1_map;
1147 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
1148 		break;
1149 	case BYT_RT5640_IN3_MAP:
1150 		custom_map = byt_rt5640_intmic_in3_map;
1151 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
1152 		break;
1153 	case BYT_RT5640_DMIC1_MAP:
1154 		custom_map = byt_rt5640_intmic_dmic1_map;
1155 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
1156 		break;
1157 	case BYT_RT5640_DMIC2_MAP:
1158 		custom_map = byt_rt5640_intmic_dmic2_map;
1159 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
1160 		break;
1161 	}
1162 
1163 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
1164 	if (ret)
1165 		return ret;
1166 
1167 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1) {
1168 		ret = snd_soc_dapm_add_routes(&card->dapm,
1169 					byt_rt5640_hsmic2_in1_map,
1170 					ARRAY_SIZE(byt_rt5640_hsmic2_in1_map));
1171 		if (ret)
1172 			return ret;
1173 	}
1174 
1175 	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
1176 		ret = snd_soc_dapm_add_routes(&card->dapm,
1177 					byt_rt5640_ssp2_aif2_map,
1178 					ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
1179 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
1180 		ret = snd_soc_dapm_add_routes(&card->dapm,
1181 					byt_rt5640_ssp0_aif1_map,
1182 					ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
1183 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
1184 		ret = snd_soc_dapm_add_routes(&card->dapm,
1185 					byt_rt5640_ssp0_aif2_map,
1186 					ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
1187 	} else {
1188 		ret = snd_soc_dapm_add_routes(&card->dapm,
1189 					byt_rt5640_ssp2_aif1_map,
1190 					ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
1191 	}
1192 	if (ret)
1193 		return ret;
1194 
1195 	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1196 		ret = snd_soc_dapm_add_routes(&card->dapm,
1197 					byt_rt5640_mono_spk_map,
1198 					ARRAY_SIZE(byt_rt5640_mono_spk_map));
1199 	} else if (!(byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)) {
1200 		ret = snd_soc_dapm_add_routes(&card->dapm,
1201 					byt_rt5640_stereo_spk_map,
1202 					ARRAY_SIZE(byt_rt5640_stereo_spk_map));
1203 	}
1204 	if (ret)
1205 		return ret;
1206 
1207 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1208 		ret = snd_soc_dapm_add_routes(&card->dapm,
1209 					byt_rt5640_lineout_map,
1210 					ARRAY_SIZE(byt_rt5640_lineout_map));
1211 		if (ret)
1212 			return ret;
1213 	}
1214 
1215 	/*
1216 	 * The firmware might enable the clock at boot (this information
1217 	 * may or may not be reflected in the enable clock register).
1218 	 * To change the rate we must disable the clock first to cover
1219 	 * these cases. Due to common clock framework restrictions that
1220 	 * do not allow to disable a clock that has not been enabled,
1221 	 * we need to enable the clock first.
1222 	 */
1223 	ret = clk_prepare_enable(priv->mclk);
1224 	if (!ret)
1225 		clk_disable_unprepare(priv->mclk);
1226 
1227 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1228 		ret = clk_set_rate(priv->mclk, 25000000);
1229 	else
1230 		ret = clk_set_rate(priv->mclk, 19200000);
1231 	if (ret) {
1232 		dev_err(card->dev, "unable to set MCLK rate\n");
1233 		return ret;
1234 	}
1235 
1236 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1237 		ret = snd_soc_card_jack_new(card, "Headset",
1238 					    SND_JACK_HEADSET | SND_JACK_BTN_0,
1239 					    &priv->jack, rt5640_pins,
1240 					    ARRAY_SIZE(rt5640_pins));
1241 		if (ret) {
1242 			dev_err(card->dev, "Jack creation failed %d\n", ret);
1243 			return ret;
1244 		}
1245 		snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1246 				 KEY_PLAYPAUSE);
1247 		snd_soc_component_set_jack(component, &priv->jack, NULL);
1248 	}
1249 
1250 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1251 		ret = snd_soc_card_jack_new(card, "Headset",
1252 					    SND_JACK_HEADSET,
1253 					    &priv->jack, rt5640_pins,
1254 					    ARRAY_SIZE(rt5640_pins));
1255 		if (ret)
1256 			return ret;
1257 
1258 		ret = snd_soc_card_jack_new(card, "Headset 2",
1259 					    SND_JACK_HEADSET,
1260 					    &priv->jack2, rt5640_pins2,
1261 					    ARRAY_SIZE(rt5640_pins2));
1262 		if (ret)
1263 			return ret;
1264 
1265 		rt5640_jack_gpio.data = priv;
1266 		rt5640_jack_gpio.gpiod_dev = priv->codec_dev;
1267 		rt5640_jack_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack1_check;
1268 		ret = snd_soc_jack_add_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1269 		if (ret)
1270 			return ret;
1271 
1272 		rt5640_set_ovcd_params(component);
1273 		rt5640_jack2_gpio.data = component;
1274 		rt5640_jack2_gpio.gpiod_dev = priv->codec_dev;
1275 		rt5640_jack2_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack2_check;
1276 		ret = snd_soc_jack_add_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1277 		if (ret) {
1278 			snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1279 			return ret;
1280 		}
1281 	}
1282 
1283 	return 0;
1284 }
1285 
1286 static void byt_rt5640_exit(struct snd_soc_pcm_runtime *runtime)
1287 {
1288 	struct snd_soc_card *card = runtime->card;
1289 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1290 
1291 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1292 		snd_soc_jack_free_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1293 		snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1294 	}
1295 }
1296 
1297 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1298 			    struct snd_pcm_hw_params *params)
1299 {
1300 	struct snd_interval *rate = hw_param_interval(params,
1301 			SNDRV_PCM_HW_PARAM_RATE);
1302 	struct snd_interval *channels = hw_param_interval(params,
1303 						SNDRV_PCM_HW_PARAM_CHANNELS);
1304 	int ret, bits;
1305 
1306 	/* The DSP will covert the FE rate to 48k, stereo */
1307 	rate->min = rate->max = 48000;
1308 	channels->min = channels->max = 2;
1309 
1310 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1311 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1312 		/* set SSP0 to 16-bit */
1313 		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1314 		bits = 16;
1315 	} else {
1316 		/* set SSP2 to 24-bit */
1317 		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1318 		bits = 24;
1319 	}
1320 
1321 	/*
1322 	 * Default mode for SSP configuration is TDM 4 slot, override config
1323 	 * with explicit setting to I2S 2ch. The word length is set with
1324 	 * dai_set_tdm_slot() since there is no other API exposed
1325 	 */
1326 	ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
1327 				  SND_SOC_DAIFMT_I2S     |
1328 				  SND_SOC_DAIFMT_NB_NF   |
1329 				  SND_SOC_DAIFMT_CBC_CFC);
1330 	if (ret < 0) {
1331 		dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1332 		return ret;
1333 	}
1334 
1335 	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
1336 	if (ret < 0) {
1337 		dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1338 		return ret;
1339 	}
1340 
1341 	return 0;
1342 }
1343 
1344 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1345 {
1346 	return snd_pcm_hw_constraint_single(substream->runtime,
1347 			SNDRV_PCM_HW_PARAM_RATE, 48000);
1348 }
1349 
1350 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1351 	.startup = byt_rt5640_aif1_startup,
1352 };
1353 
1354 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1355 	.hw_params = byt_rt5640_aif1_hw_params,
1356 };
1357 
1358 SND_SOC_DAILINK_DEF(dummy,
1359 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
1360 
1361 SND_SOC_DAILINK_DEF(media,
1362 	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
1363 
1364 SND_SOC_DAILINK_DEF(deepbuffer,
1365 	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
1366 
1367 SND_SOC_DAILINK_DEF(ssp2_port,
1368 	/* overwritten for ssp0 routing */
1369 	DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
1370 SND_SOC_DAILINK_DEF(ssp2_codec,
1371 	DAILINK_COMP_ARRAY(COMP_CODEC(
1372 	/* overwritten with HID */ "i2c-10EC5640:00",
1373 	/* changed w/ quirk */	"rt5640-aif1")));
1374 
1375 SND_SOC_DAILINK_DEF(platform,
1376 	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
1377 
1378 static struct snd_soc_dai_link byt_rt5640_dais[] = {
1379 	[MERR_DPCM_AUDIO] = {
1380 		.name = "Baytrail Audio Port",
1381 		.stream_name = "Baytrail Audio",
1382 		.nonatomic = true,
1383 		.dynamic = 1,
1384 		.dpcm_playback = 1,
1385 		.dpcm_capture = 1,
1386 		.ops = &byt_rt5640_aif1_ops,
1387 		SND_SOC_DAILINK_REG(media, dummy, platform),
1388 	},
1389 	[MERR_DPCM_DEEP_BUFFER] = {
1390 		.name = "Deep-Buffer Audio Port",
1391 		.stream_name = "Deep-Buffer Audio",
1392 		.nonatomic = true,
1393 		.dynamic = 1,
1394 		.dpcm_playback = 1,
1395 		.ops = &byt_rt5640_aif1_ops,
1396 		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
1397 	},
1398 		/* back ends */
1399 	{
1400 		.name = "SSP2-Codec",
1401 		.id = 0,
1402 		.no_pcm = 1,
1403 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1404 						| SND_SOC_DAIFMT_CBC_CFC,
1405 		.be_hw_params_fixup = byt_rt5640_codec_fixup,
1406 		.dpcm_playback = 1,
1407 		.dpcm_capture = 1,
1408 		.init = byt_rt5640_init,
1409 		.exit = byt_rt5640_exit,
1410 		.ops = &byt_rt5640_be_ssp2_ops,
1411 		SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
1412 	},
1413 };
1414 
1415 /* SoC card */
1416 static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1417 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1418 static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1419 #endif
1420 static char byt_rt5640_components[64]; /* = "cfg-spk:* cfg-mic:* ..." */
1421 
1422 static int byt_rt5640_suspend(struct snd_soc_card *card)
1423 {
1424 	struct snd_soc_component *component;
1425 
1426 	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1427 		return 0;
1428 
1429 	for_each_card_components(card, component) {
1430 		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1431 			dev_dbg(component->dev, "disabling jack detect before suspend\n");
1432 			snd_soc_component_set_jack(component, NULL, NULL);
1433 			break;
1434 		}
1435 	}
1436 
1437 	return 0;
1438 }
1439 
1440 static int byt_rt5640_resume(struct snd_soc_card *card)
1441 {
1442 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1443 	struct snd_soc_component *component;
1444 
1445 	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1446 		return 0;
1447 
1448 	for_each_card_components(card, component) {
1449 		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1450 			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1451 			snd_soc_component_set_jack(component, &priv->jack, NULL);
1452 			break;
1453 		}
1454 	}
1455 
1456 	return 0;
1457 }
1458 
1459 /* use space before codec name to simplify card ID, and simplify driver name */
1460 #define SOF_CARD_NAME "bytcht rt5640" /* card name will be 'sof-bytcht rt5640' */
1461 #define SOF_DRIVER_NAME "SOF"
1462 
1463 #define CARD_NAME "bytcr-rt5640"
1464 #define DRIVER_NAME NULL /* card name will be used for driver name */
1465 
1466 static struct snd_soc_card byt_rt5640_card = {
1467 	.owner = THIS_MODULE,
1468 	.dai_link = byt_rt5640_dais,
1469 	.num_links = ARRAY_SIZE(byt_rt5640_dais),
1470 	.dapm_widgets = byt_rt5640_widgets,
1471 	.num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1472 	.dapm_routes = byt_rt5640_audio_map,
1473 	.num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1474 	.fully_routed = true,
1475 	.suspend_pre = byt_rt5640_suspend,
1476 	.resume_post = byt_rt5640_resume,
1477 };
1478 
1479 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
1480 	u64 aif_value;       /* 1: AIF1, 2: AIF2 */
1481 	u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
1482 };
1483 
1484 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1485 {
1486 	struct device *dev = &pdev->dev;
1487 	static const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3", "none" };
1488 	struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
1489 	__maybe_unused const char *spk_type;
1490 	const struct dmi_system_id *dmi_id;
1491 	const char *headset2_string = "";
1492 	const char *lineout_string = "";
1493 	struct byt_rt5640_private *priv;
1494 	const char *platform_name;
1495 	struct acpi_device *adev;
1496 	struct device *codec_dev;
1497 	bool sof_parent;
1498 	int ret_val = 0;
1499 	int dai_index = 0;
1500 	int i, cfg_spk;
1501 	int aif;
1502 
1503 	is_bytcr = false;
1504 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1505 	if (!priv)
1506 		return -ENOMEM;
1507 
1508 	/* register the soc card */
1509 	byt_rt5640_card.dev = dev;
1510 	snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1511 
1512 	/* fix index of codec dai */
1513 	for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1514 		if (!strcmp(byt_rt5640_dais[i].codecs->name,
1515 			    "i2c-10EC5640:00")) {
1516 			dai_index = i;
1517 			break;
1518 		}
1519 	}
1520 
1521 	/* fixup codec name based on HID */
1522 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
1523 	if (adev) {
1524 		snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1525 			 "i2c-%s", acpi_dev_name(adev));
1526 		put_device(&adev->dev);
1527 		byt_rt5640_dais[dai_index].codecs->name = byt_rt5640_codec_name;
1528 	} else {
1529 		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
1530 		return -ENXIO;
1531 	}
1532 
1533 	/*
1534 	 * swap SSP0 if bytcr is detected
1535 	 * (will be overridden if DMI quirk is detected)
1536 	 */
1537 	if (soc_intel_is_byt()) {
1538 		if (mach->mach_params.acpi_ipc_irq_index == 0)
1539 			is_bytcr = true;
1540 	}
1541 
1542 	if (is_bytcr) {
1543 		/*
1544 		 * Baytrail CR platforms may have CHAN package in BIOS, try
1545 		 * to find relevant routing quirk based as done on Windows
1546 		 * platforms. We have to read the information directly from the
1547 		 * BIOS, at this stage the card is not created and the links
1548 		 * with the codec driver/pdata are non-existent
1549 		 */
1550 
1551 		struct acpi_chan_package chan_package;
1552 
1553 		/* format specified: 2 64-bit integers */
1554 		struct acpi_buffer format = {sizeof("NN"), "NN"};
1555 		struct acpi_buffer state = {0, NULL};
1556 		struct snd_soc_acpi_package_context pkg_ctx;
1557 		bool pkg_found = false;
1558 
1559 		state.length = sizeof(chan_package);
1560 		state.pointer = &chan_package;
1561 
1562 		pkg_ctx.name = "CHAN";
1563 		pkg_ctx.length = 2;
1564 		pkg_ctx.format = &format;
1565 		pkg_ctx.state = &state;
1566 		pkg_ctx.data_valid = false;
1567 
1568 		pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1569 							       &pkg_ctx);
1570 		if (pkg_found) {
1571 			if (chan_package.aif_value == 1) {
1572 				dev_info(dev, "BIOS Routing: AIF1 connected\n");
1573 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1574 			} else  if (chan_package.aif_value == 2) {
1575 				dev_info(dev, "BIOS Routing: AIF2 connected\n");
1576 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1577 			} else {
1578 				dev_info(dev, "BIOS Routing isn't valid, ignored\n");
1579 				pkg_found = false;
1580 			}
1581 		}
1582 
1583 		if (!pkg_found) {
1584 			/* no BIOS indications, assume SSP0-AIF2 connection */
1585 			byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1586 		}
1587 
1588 		/* change defaults for Baytrail-CR capture */
1589 		byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1590 	} else {
1591 		byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1592 				    BYT_RT5640_JD_SRC_JD2_IN4N |
1593 				    BYT_RT5640_OVCD_TH_2000UA |
1594 				    BYT_RT5640_OVCD_SF_0P75;
1595 	}
1596 
1597 	/* check quirks before creating card */
1598 	dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1599 	if (dmi_id)
1600 		byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1601 	if (quirk_override != -1) {
1602 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
1603 			 byt_rt5640_quirk, quirk_override);
1604 		byt_rt5640_quirk = quirk_override;
1605 	}
1606 
1607 	codec_dev = acpi_get_first_physical_node(adev);
1608 	if (!codec_dev)
1609 		return -EPROBE_DEFER;
1610 	priv->codec_dev = get_device(codec_dev);
1611 
1612 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1613 		acpi_dev_add_driver_gpios(ACPI_COMPANION(priv->codec_dev),
1614 					  byt_rt5640_hp_elitepad_1000g2_gpios);
1615 
1616 		priv->hsmic_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1617 							   "headset-mic-detect", GPIOD_IN,
1618 							   "headset-mic-detect");
1619 		if (IS_ERR(priv->hsmic_detect)) {
1620 			ret_val = dev_err_probe(dev, PTR_ERR(priv->hsmic_detect),
1621 						"getting hsmic-detect GPIO\n");
1622 			goto err_device;
1623 		}
1624 	}
1625 
1626 	/* Must be called before register_card, also see declaration comment. */
1627 	ret_val = byt_rt5640_add_codec_device_props(codec_dev, priv);
1628 	if (ret_val)
1629 		goto err_remove_gpios;
1630 
1631 	log_quirks(dev);
1632 
1633 	if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1634 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1635 		byt_rt5640_dais[dai_index].codecs->dai_name = "rt5640-aif2";
1636 		aif = 2;
1637 	} else {
1638 		aif = 1;
1639 	}
1640 
1641 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1642 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1643 		byt_rt5640_dais[dai_index].cpus->dai_name = "ssp0-port";
1644 
1645 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1646 		priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
1647 		if (IS_ERR(priv->mclk)) {
1648 			ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
1649 						"Failed to get MCLK from pmc_plt_clk_3\n");
1650 			goto err;
1651 		}
1652 		/*
1653 		 * Fall back to bit clock usage when clock is not
1654 		 * available likely due to missing dependencies.
1655 		 */
1656 		if (!priv->mclk)
1657 			byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1658 	}
1659 
1660 	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS) {
1661 		cfg_spk = 0;
1662 		spk_type = "none";
1663 	} else if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1664 		cfg_spk = 1;
1665 		spk_type = "mono";
1666 	} else {
1667 		cfg_spk = 2;
1668 		spk_type = "stereo";
1669 	}
1670 
1671 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1672 		if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
1673 			lineout_string = " cfg-hp2:lineout";
1674 		else
1675 			lineout_string = " cfg-lineout:2";
1676 	}
1677 
1678 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
1679 		headset2_string = " cfg-hs2:in1";
1680 
1681 	snprintf(byt_rt5640_components, sizeof(byt_rt5640_components),
1682 		 "cfg-spk:%d cfg-mic:%s aif:%d%s%s", cfg_spk,
1683 		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)], aif,
1684 		 lineout_string, headset2_string);
1685 	byt_rt5640_card.components = byt_rt5640_components;
1686 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1687 	snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1688 		 "bytcr-rt5640-%s-spk-%s-mic", spk_type,
1689 		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1690 	byt_rt5640_card.long_name = byt_rt5640_long_name;
1691 #endif
1692 
1693 	/* override plaform name, if required */
1694 	platform_name = mach->mach_params.platform;
1695 
1696 	ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5640_card,
1697 							platform_name);
1698 	if (ret_val)
1699 		goto err;
1700 
1701 	sof_parent = snd_soc_acpi_sof_parent(dev);
1702 
1703 	/* set card and driver name */
1704 	if (sof_parent) {
1705 		byt_rt5640_card.name = SOF_CARD_NAME;
1706 		byt_rt5640_card.driver_name = SOF_DRIVER_NAME;
1707 	} else {
1708 		byt_rt5640_card.name = CARD_NAME;
1709 		byt_rt5640_card.driver_name = DRIVER_NAME;
1710 	}
1711 
1712 	/* set pm ops */
1713 	if (sof_parent)
1714 		dev->driver->pm = &snd_soc_pm_ops;
1715 
1716 	ret_val = devm_snd_soc_register_card(dev, &byt_rt5640_card);
1717 	if (ret_val) {
1718 		dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
1719 		goto err;
1720 	}
1721 	platform_set_drvdata(pdev, &byt_rt5640_card);
1722 	return ret_val;
1723 
1724 err:
1725 	device_remove_software_node(priv->codec_dev);
1726 err_remove_gpios:
1727 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1728 		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1729 err_device:
1730 	put_device(priv->codec_dev);
1731 	return ret_val;
1732 }
1733 
1734 static int snd_byt_rt5640_mc_remove(struct platform_device *pdev)
1735 {
1736 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1737 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1738 
1739 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1740 		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1741 
1742 	device_remove_software_node(priv->codec_dev);
1743 	put_device(priv->codec_dev);
1744 	return 0;
1745 }
1746 
1747 static struct platform_driver snd_byt_rt5640_mc_driver = {
1748 	.driver = {
1749 		.name = "bytcr_rt5640",
1750 	},
1751 	.probe = snd_byt_rt5640_mc_probe,
1752 	.remove = snd_byt_rt5640_mc_remove,
1753 };
1754 
1755 module_platform_driver(snd_byt_rt5640_mc_driver);
1756 
1757 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1758 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1759 MODULE_LICENSE("GPL v2");
1760 MODULE_ALIAS("platform:bytcr_rt5640");
1761