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