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 One 7 B1-750 */
537 		.matches = {
538 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
539 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "VESPA2"),
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 Iconia Tab 8 W1-810 */
549 		.matches = {
550 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
551 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
552 		},
553 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
554 					BYT_RT5640_JD_SRC_JD1_IN4P |
555 					BYT_RT5640_OVCD_TH_1500UA |
556 					BYT_RT5640_OVCD_SF_0P75 |
557 					BYT_RT5640_SSP0_AIF1 |
558 					BYT_RT5640_MCLK_EN),
559 	},
560 	{	/* Acer One 10 S1002 */
561 		.matches = {
562 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
563 			DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
564 		},
565 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
566 					BYT_RT5640_JD_SRC_JD2_IN4N |
567 					BYT_RT5640_OVCD_TH_2000UA |
568 					BYT_RT5640_OVCD_SF_0P75 |
569 					BYT_RT5640_DIFF_MIC |
570 					BYT_RT5640_SSP0_AIF2 |
571 					BYT_RT5640_MCLK_EN),
572 	},
573 	{
574 		.matches = {
575 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
576 			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
577 		},
578 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
579 					BYT_RT5640_JD_SRC_JD2_IN4N |
580 					BYT_RT5640_OVCD_TH_2000UA |
581 					BYT_RT5640_OVCD_SF_0P75 |
582 					BYT_RT5640_SSP0_AIF1 |
583 					BYT_RT5640_MCLK_EN),
584 	},
585 	{
586 		/* Advantech MICA-071 */
587 		.matches = {
588 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Advantech"),
589 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MICA-071"),
590 		},
591 		/* OVCD Th = 1500uA to reliable detect head-phones vs -set */
592 		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
593 					BYT_RT5640_JD_SRC_JD2_IN4N |
594 					BYT_RT5640_OVCD_TH_1500UA |
595 					BYT_RT5640_OVCD_SF_0P75 |
596 					BYT_RT5640_MONO_SPEAKER |
597 					BYT_RT5640_DIFF_MIC |
598 					BYT_RT5640_MCLK_EN),
599 	},
600 	{
601 		.matches = {
602 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
603 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
604 		},
605 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
606 					BYT_RT5640_MONO_SPEAKER |
607 					BYT_RT5640_SSP0_AIF1 |
608 					BYT_RT5640_MCLK_EN),
609 	},
610 	{
611 		.matches = {
612 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
613 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
614 		},
615 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
616 					BYT_RT5640_JD_SRC_JD2_IN4N |
617 					BYT_RT5640_OVCD_TH_2000UA |
618 					BYT_RT5640_OVCD_SF_0P75 |
619 					BYT_RT5640_SSP0_AIF1 |
620 					BYT_RT5640_MCLK_EN),
621 	},
622 	{
623 		.matches = {
624 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
625 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ME176C"),
626 		},
627 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
628 					BYT_RT5640_JD_SRC_JD2_IN4N |
629 					BYT_RT5640_OVCD_TH_2000UA |
630 					BYT_RT5640_OVCD_SF_0P75 |
631 					BYT_RT5640_SSP0_AIF1 |
632 					BYT_RT5640_MCLK_EN |
633 					BYT_RT5640_USE_AMCR0F28),
634 	},
635 	{
636 		.matches = {
637 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
638 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
639 		},
640 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
641 					BYT_RT5640_JD_SRC_JD2_IN4N |
642 					BYT_RT5640_OVCD_TH_2000UA |
643 					BYT_RT5640_OVCD_SF_0P75 |
644 					BYT_RT5640_MCLK_EN),
645 	},
646 	{
647 		.matches = {
648 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
649 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
650 		},
651 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
652 					BYT_RT5640_JD_SRC_JD2_IN4N |
653 					BYT_RT5640_OVCD_TH_2000UA |
654 					BYT_RT5640_OVCD_SF_0P75 |
655 					BYT_RT5640_MONO_SPEAKER |
656 					BYT_RT5640_DIFF_MIC |
657 					BYT_RT5640_SSP0_AIF2 |
658 					BYT_RT5640_MCLK_EN),
659 	},
660 	{
661 		.matches = {
662 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
663 			DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
664 		},
665 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
666 					BYT_RT5640_JD_SRC_EXT_GPIO |
667 					BYT_RT5640_OVCD_TH_2000UA |
668 					BYT_RT5640_OVCD_SF_0P75 |
669 					BYT_RT5640_SSP0_AIF1 |
670 					BYT_RT5640_MCLK_EN |
671 					BYT_RT5640_USE_AMCR0F28),
672 	},
673 	{	/* Chuwi Vi8 (CWI506) */
674 		.matches = {
675 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
676 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
677 			/* The above are too generic, also match BIOS info */
678 			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
679 		},
680 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
681 					BYT_RT5640_MONO_SPEAKER |
682 					BYT_RT5640_SSP0_AIF1 |
683 					BYT_RT5640_MCLK_EN),
684 	},
685 	{
686 		/* Chuwi Vi10 (CWI505) */
687 		.matches = {
688 			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
689 			DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
690 			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
691 			DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
692 		},
693 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
694 					BYT_RT5640_JD_SRC_JD2_IN4N |
695 					BYT_RT5640_OVCD_TH_2000UA |
696 					BYT_RT5640_OVCD_SF_0P75 |
697 					BYT_RT5640_DIFF_MIC |
698 					BYT_RT5640_SSP0_AIF1 |
699 					BYT_RT5640_MCLK_EN),
700 	},
701 	{
702 		/* Chuwi Hi8 (CWI509) */
703 		.matches = {
704 			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
705 			DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
706 			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
707 			DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
708 		},
709 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
710 					BYT_RT5640_JD_SRC_JD2_IN4N |
711 					BYT_RT5640_OVCD_TH_2000UA |
712 					BYT_RT5640_OVCD_SF_0P75 |
713 					BYT_RT5640_MONO_SPEAKER |
714 					BYT_RT5640_DIFF_MIC |
715 					BYT_RT5640_SSP0_AIF1 |
716 					BYT_RT5640_MCLK_EN),
717 	},
718 	{
719 		.matches = {
720 			DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
721 			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
722 		},
723 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
724 	},
725 	{	/* Connect Tablet 9 */
726 		.matches = {
727 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
728 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
729 		},
730 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
731 					BYT_RT5640_MONO_SPEAKER |
732 					BYT_RT5640_SSP0_AIF1 |
733 					BYT_RT5640_MCLK_EN),
734 	},
735 	{
736 		.matches = {
737 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
738 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
739 		},
740 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
741 					BYT_RT5640_JD_SRC_JD2_IN4N |
742 					BYT_RT5640_OVCD_TH_2000UA |
743 					BYT_RT5640_OVCD_SF_0P75 |
744 					BYT_RT5640_MONO_SPEAKER |
745 					BYT_RT5640_MCLK_EN),
746 	},
747 	{	/* Estar Beauty HD MID 7316R */
748 		.matches = {
749 			DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
750 			DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
751 		},
752 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
753 					BYT_RT5640_MONO_SPEAKER |
754 					BYT_RT5640_SSP0_AIF1 |
755 					BYT_RT5640_MCLK_EN),
756 	},
757 	{	/* Glavey TM800A550L */
758 		.matches = {
759 			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
760 			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
761 			/* Above strings are too generic, also match on BIOS version */
762 			DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
763 		},
764 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
765 					BYT_RT5640_SSP0_AIF1 |
766 					BYT_RT5640_MCLK_EN),
767 	},
768 	{
769 		.matches = {
770 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
771 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
772 		},
773 		.driver_data = (void *)(BYT_RT5640_DMIC2_MAP |
774 					BYT_RT5640_MCLK_EN |
775 					BYT_RT5640_LINEOUT |
776 					BYT_RT5640_LINEOUT_AS_HP2 |
777 					BYT_RT5640_HSMIC2_ON_IN1 |
778 					BYT_RT5640_JD_HP_ELITEP_1000G2),
779 	},
780 	{	/* HP Pavilion x2 10-k0XX, 10-n0XX */
781 		.matches = {
782 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
783 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
784 		},
785 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
786 					BYT_RT5640_JD_SRC_JD2_IN4N |
787 					BYT_RT5640_OVCD_TH_1500UA |
788 					BYT_RT5640_OVCD_SF_0P75 |
789 					BYT_RT5640_SSP0_AIF1 |
790 					BYT_RT5640_MCLK_EN),
791 	},
792 	{	/* HP Pavilion x2 10-p0XX */
793 		.matches = {
794 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
795 			DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
796 		},
797 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
798 					BYT_RT5640_JD_SRC_JD1_IN4P |
799 					BYT_RT5640_OVCD_TH_2000UA |
800 					BYT_RT5640_OVCD_SF_0P75 |
801 					BYT_RT5640_MCLK_EN),
802 	},
803 	{	/* HP Pro Tablet 408 */
804 		.matches = {
805 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
806 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pro Tablet 408"),
807 		},
808 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
809 					BYT_RT5640_JD_SRC_JD2_IN4N |
810 					BYT_RT5640_OVCD_TH_1500UA |
811 					BYT_RT5640_OVCD_SF_0P75 |
812 					BYT_RT5640_SSP0_AIF1 |
813 					BYT_RT5640_MCLK_EN),
814 	},
815 	{	/* HP Stream 7 */
816 		.matches = {
817 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
818 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
819 		},
820 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
821 					BYT_RT5640_MONO_SPEAKER |
822 					BYT_RT5640_JD_NOT_INV |
823 					BYT_RT5640_SSP0_AIF1 |
824 					BYT_RT5640_MCLK_EN),
825 	},
826 	{	/* HP Stream 8 */
827 		.matches = {
828 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
829 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 8 Tablet"),
830 		},
831 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
832 					BYT_RT5640_JD_NOT_INV |
833 					BYT_RT5640_SSP0_AIF1 |
834 					BYT_RT5640_MCLK_EN),
835 	},
836 	{	/* I.T.Works TW891 */
837 		.matches = {
838 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
839 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
840 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
841 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
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 	{	/* Lamina I8270 / T701BR.SE */
849 		.matches = {
850 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
851 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
852 		},
853 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
854 					BYT_RT5640_MONO_SPEAKER |
855 					BYT_RT5640_JD_NOT_INV |
856 					BYT_RT5640_SSP0_AIF1 |
857 					BYT_RT5640_MCLK_EN),
858 	},
859 	{	/* Lenovo Miix 2 8 */
860 		.matches = {
861 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
862 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
863 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
864 		},
865 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
866 					BYT_RT5640_JD_SRC_JD2_IN4N |
867 					BYT_RT5640_OVCD_TH_2000UA |
868 					BYT_RT5640_OVCD_SF_0P75 |
869 					BYT_RT5640_MONO_SPEAKER |
870 					BYT_RT5640_MCLK_EN),
871 	},
872 	{	/* Lenovo Miix 3-830 */
873 		.matches = {
874 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
875 			DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
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_MONO_SPEAKER |
882 					BYT_RT5640_DIFF_MIC |
883 					BYT_RT5640_SSP0_AIF1 |
884 					BYT_RT5640_MCLK_EN),
885 	},
886 	{	/* Linx Linx7 tablet */
887 		.matches = {
888 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
889 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
890 		},
891 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
892 					BYT_RT5640_MONO_SPEAKER |
893 					BYT_RT5640_JD_NOT_INV |
894 					BYT_RT5640_SSP0_AIF1 |
895 					BYT_RT5640_MCLK_EN),
896 	},
897 	{	/* Mele PCG03 Mini PC */
898 		.matches = {
899 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
900 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
901 		},
902 		.driver_data = (void *)(BYT_RT5640_NO_INTERNAL_MIC_MAP |
903 					BYT_RT5640_NO_SPEAKERS |
904 					BYT_RT5640_SSP0_AIF1),
905 	},
906 	{	/* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
907 		.matches = {
908 			DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
909 			DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
910 		},
911 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
912 					BYT_RT5640_MONO_SPEAKER |
913 					BYT_RT5640_SSP0_AIF1 |
914 					BYT_RT5640_MCLK_EN),
915 	},
916 	{
917 		/* MPMAN MPWIN895CL */
918 		.matches = {
919 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
920 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
921 		},
922 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
923 					BYT_RT5640_MONO_SPEAKER |
924 					BYT_RT5640_SSP0_AIF1 |
925 					BYT_RT5640_MCLK_EN),
926 	},
927 	{	/* MSI S100 tablet */
928 		.matches = {
929 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
930 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
931 		},
932 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
933 					BYT_RT5640_JD_SRC_JD2_IN4N |
934 					BYT_RT5640_OVCD_TH_2000UA |
935 					BYT_RT5640_OVCD_SF_0P75 |
936 					BYT_RT5640_MONO_SPEAKER |
937 					BYT_RT5640_DIFF_MIC |
938 					BYT_RT5640_MCLK_EN),
939 	},
940 	{	/* Nuvison/TMax TM800W560 */
941 		.matches = {
942 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
943 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
944 		},
945 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
946 					BYT_RT5640_JD_SRC_JD2_IN4N |
947 					BYT_RT5640_OVCD_TH_2000UA |
948 					BYT_RT5640_OVCD_SF_0P75 |
949 					BYT_RT5640_JD_NOT_INV |
950 					BYT_RT5640_DIFF_MIC |
951 					BYT_RT5640_SSP0_AIF1 |
952 					BYT_RT5640_MCLK_EN),
953 	},
954 	{	/* Onda v975w */
955 		.matches = {
956 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
957 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
958 			/* The above are too generic, also match BIOS info */
959 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
960 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
961 		},
962 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
963 					BYT_RT5640_JD_SRC_JD2_IN4N |
964 					BYT_RT5640_OVCD_TH_2000UA |
965 					BYT_RT5640_OVCD_SF_0P75 |
966 					BYT_RT5640_DIFF_MIC |
967 					BYT_RT5640_MCLK_EN),
968 	},
969 	{	/* Pipo W4 */
970 		.matches = {
971 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
972 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
973 			/* The above are too generic, also match BIOS info */
974 			DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
975 		},
976 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
977 					BYT_RT5640_MONO_SPEAKER |
978 					BYT_RT5640_SSP0_AIF1 |
979 					BYT_RT5640_MCLK_EN),
980 	},
981 	{	/* Point of View Mobii TAB-P800W (V2.0) */
982 		.matches = {
983 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
984 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
985 			/* The above are too generic, also match BIOS info */
986 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
987 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
988 		},
989 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
990 					BYT_RT5640_JD_SRC_JD2_IN4N |
991 					BYT_RT5640_OVCD_TH_2000UA |
992 					BYT_RT5640_OVCD_SF_0P75 |
993 					BYT_RT5640_MONO_SPEAKER |
994 					BYT_RT5640_DIFF_MIC |
995 					BYT_RT5640_SSP0_AIF2 |
996 					BYT_RT5640_MCLK_EN),
997 	},
998 	{	/* Point of View Mobii TAB-P800W (V2.1) */
999 		.matches = {
1000 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1001 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1002 			/* The above are too generic, also match BIOS info */
1003 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1004 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1005 		},
1006 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1007 					BYT_RT5640_JD_SRC_JD2_IN4N |
1008 					BYT_RT5640_OVCD_TH_2000UA |
1009 					BYT_RT5640_OVCD_SF_0P75 |
1010 					BYT_RT5640_MONO_SPEAKER |
1011 					BYT_RT5640_DIFF_MIC |
1012 					BYT_RT5640_SSP0_AIF2 |
1013 					BYT_RT5640_MCLK_EN),
1014 	},
1015 	{	/* Point of View Mobii TAB-P1005W-232 (V2.0) */
1016 		.matches = {
1017 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "POV"),
1018 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "I102A"),
1019 		},
1020 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1021 					BYT_RT5640_JD_SRC_JD2_IN4N |
1022 					BYT_RT5640_OVCD_TH_2000UA |
1023 					BYT_RT5640_OVCD_SF_0P75 |
1024 					BYT_RT5640_DIFF_MIC |
1025 					BYT_RT5640_SSP0_AIF1 |
1026 					BYT_RT5640_MCLK_EN),
1027 	},
1028 	{
1029 		/* Prowise PT301 */
1030 		.matches = {
1031 			DMI_MATCH(DMI_SYS_VENDOR, "Prowise"),
1032 			DMI_MATCH(DMI_PRODUCT_NAME, "PT301"),
1033 		},
1034 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1035 					BYT_RT5640_JD_SRC_JD2_IN4N |
1036 					BYT_RT5640_OVCD_TH_2000UA |
1037 					BYT_RT5640_OVCD_SF_0P75 |
1038 					BYT_RT5640_DIFF_MIC |
1039 					BYT_RT5640_SSP0_AIF1 |
1040 					BYT_RT5640_MCLK_EN),
1041 	},
1042 	{
1043 		/* Teclast X89 */
1044 		.matches = {
1045 			DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
1046 			DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
1047 		},
1048 		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
1049 					BYT_RT5640_JD_SRC_JD1_IN4P |
1050 					BYT_RT5640_OVCD_TH_2000UA |
1051 					BYT_RT5640_OVCD_SF_1P0 |
1052 					BYT_RT5640_SSP0_AIF1 |
1053 					BYT_RT5640_MCLK_EN),
1054 	},
1055 	{	/* Toshiba Satellite Click Mini L9W-B */
1056 		.matches = {
1057 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1058 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
1059 		},
1060 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1061 					BYT_RT5640_JD_SRC_JD2_IN4N |
1062 					BYT_RT5640_OVCD_TH_1500UA |
1063 					BYT_RT5640_OVCD_SF_0P75 |
1064 					BYT_RT5640_SSP0_AIF1 |
1065 					BYT_RT5640_MCLK_EN),
1066 	},
1067 	{	/* Toshiba Encore WT8-A */
1068 		.matches = {
1069 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1070 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
1071 		},
1072 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1073 					BYT_RT5640_JD_SRC_JD2_IN4N |
1074 					BYT_RT5640_OVCD_TH_2000UA |
1075 					BYT_RT5640_OVCD_SF_0P75 |
1076 					BYT_RT5640_JD_NOT_INV |
1077 					BYT_RT5640_MCLK_EN),
1078 	},
1079 	{	/* Toshiba Encore WT10-A */
1080 		.matches = {
1081 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1082 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
1083 		},
1084 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1085 					BYT_RT5640_JD_SRC_JD1_IN4P |
1086 					BYT_RT5640_OVCD_TH_2000UA |
1087 					BYT_RT5640_OVCD_SF_0P75 |
1088 					BYT_RT5640_SSP0_AIF2 |
1089 					BYT_RT5640_MCLK_EN),
1090 	},
1091 	{	/* Voyo Winpad A15 */
1092 		.matches = {
1093 			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1094 			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1095 			/* Above strings are too generic, also match on BIOS date */
1096 			DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
1097 		},
1098 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1099 					BYT_RT5640_JD_SRC_JD2_IN4N |
1100 					BYT_RT5640_OVCD_TH_2000UA |
1101 					BYT_RT5640_OVCD_SF_0P75 |
1102 					BYT_RT5640_DIFF_MIC |
1103 					BYT_RT5640_MCLK_EN),
1104 	},
1105 	{	/* Catch-all for generic Insyde tablets, must be last */
1106 		.matches = {
1107 			DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
1108 		},
1109 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1110 					BYT_RT5640_MCLK_EN |
1111 					BYT_RT5640_SSP0_AIF1),
1112 
1113 	},
1114 	{}
1115 };
1116 
1117 /*
1118  * Note this MUST be called before snd_soc_register_card(), so that the props
1119  * are in place before the codec component driver's probe function parses them.
1120  */
1121 static int byt_rt5640_add_codec_device_props(struct device *i2c_dev,
1122 					     struct byt_rt5640_private *priv)
1123 {
1124 	struct property_entry props[MAX_NO_PROPS] = {};
1125 	struct fwnode_handle *fwnode;
1126 	int cnt = 0;
1127 	int ret;
1128 
1129 	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1130 	case BYT_RT5640_DMIC1_MAP:
1131 		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
1132 						  RT5640_DMIC1_DATA_PIN_IN1P);
1133 		break;
1134 	case BYT_RT5640_DMIC2_MAP:
1135 		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
1136 						  RT5640_DMIC2_DATA_PIN_IN1N);
1137 		break;
1138 	case BYT_RT5640_IN1_MAP:
1139 		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1140 			props[cnt++] =
1141 				PROPERTY_ENTRY_BOOL("realtek,in1-differential");
1142 		break;
1143 	case BYT_RT5640_IN3_MAP:
1144 		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1145 			props[cnt++] =
1146 				PROPERTY_ENTRY_BOOL("realtek,in3-differential");
1147 		break;
1148 	}
1149 
1150 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1151 		if (BYT_RT5640_JDSRC(byt_rt5640_quirk) != RT5640_JD_SRC_EXT_GPIO) {
1152 			props[cnt++] = PROPERTY_ENTRY_U32(
1153 					    "realtek,jack-detect-source",
1154 					    BYT_RT5640_JDSRC(byt_rt5640_quirk));
1155 		}
1156 
1157 		props[cnt++] = PROPERTY_ENTRY_U32(
1158 				    "realtek,over-current-threshold-microamp",
1159 				    BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
1160 
1161 		props[cnt++] = PROPERTY_ENTRY_U32(
1162 				    "realtek,over-current-scale-factor",
1163 				    BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
1164 	}
1165 
1166 	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
1167 		props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
1168 
1169 	fwnode = fwnode_create_software_node(props, NULL);
1170 	if (IS_ERR(fwnode)) {
1171 		/* put_device() is handled in caller */
1172 		return PTR_ERR(fwnode);
1173 	}
1174 
1175 	ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
1176 
1177 	fwnode_handle_put(fwnode);
1178 
1179 	return ret;
1180 }
1181 
1182 /* Some Android devs specify IRQs/GPIOS in a special AMCR0F28 ACPI device */
1183 static const struct acpi_gpio_params amcr0f28_jd_gpio = { 1, 0, false };
1184 
1185 static const struct acpi_gpio_mapping amcr0f28_gpios[] = {
1186 	{ "rt5640-jd-gpios", &amcr0f28_jd_gpio, 1 },
1187 	{ }
1188 };
1189 
1190 static int byt_rt5640_get_amcr0f28_settings(struct snd_soc_card *card)
1191 {
1192 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1193 	struct rt5640_set_jack_data *data = &priv->jack_data;
1194 	struct acpi_device *adev;
1195 	int ret = 0;
1196 
1197 	adev = acpi_dev_get_first_match_dev("AMCR0F28", "1", -1);
1198 	if (!adev) {
1199 		dev_err(card->dev, "error cannot find AMCR0F28 adev\n");
1200 		return -ENOENT;
1201 	}
1202 
1203 	data->codec_irq_override = acpi_dev_gpio_irq_get(adev, 0);
1204 	if (data->codec_irq_override < 0) {
1205 		ret = data->codec_irq_override;
1206 		dev_err(card->dev, "error %d getting codec IRQ\n", ret);
1207 		goto put_adev;
1208 	}
1209 
1210 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk) == RT5640_JD_SRC_EXT_GPIO) {
1211 		acpi_dev_add_driver_gpios(adev, amcr0f28_gpios);
1212 		data->jd_gpio = devm_fwnode_gpiod_get(card->dev, acpi_fwnode_handle(adev),
1213 						      "rt5640-jd", GPIOD_IN, "rt5640-jd");
1214 		acpi_dev_remove_driver_gpios(adev);
1215 
1216 		if (IS_ERR(data->jd_gpio)) {
1217 			ret = PTR_ERR(data->jd_gpio);
1218 			dev_err(card->dev, "error %d getting jd GPIO\n", ret);
1219 		}
1220 	}
1221 
1222 put_adev:
1223 	acpi_dev_put(adev);
1224 	return ret;
1225 }
1226 
1227 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
1228 {
1229 	struct snd_soc_card *card = runtime->card;
1230 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1231 	struct rt5640_set_jack_data *jack_data = &priv->jack_data;
1232 	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
1233 	const struct snd_soc_dapm_route *custom_map = NULL;
1234 	int num_routes = 0;
1235 	int ret;
1236 
1237 	card->dapm.idle_bias_off = true;
1238 	jack_data->use_platform_clock = true;
1239 
1240 	/* Start with RC clk for jack-detect (we disable MCLK below) */
1241 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
1242 		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1243 			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
1244 
1245 	rt5640_sel_asrc_clk_src(component,
1246 				RT5640_DA_STEREO_FILTER |
1247 				RT5640_DA_MONO_L_FILTER	|
1248 				RT5640_DA_MONO_R_FILTER	|
1249 				RT5640_AD_STEREO_FILTER	|
1250 				RT5640_AD_MONO_L_FILTER	|
1251 				RT5640_AD_MONO_R_FILTER,
1252 				RT5640_CLK_SEL_ASRC);
1253 
1254 	ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
1255 					ARRAY_SIZE(byt_rt5640_controls));
1256 	if (ret) {
1257 		dev_err(card->dev, "unable to add card controls\n");
1258 		return ret;
1259 	}
1260 
1261 	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1262 	case BYT_RT5640_IN1_MAP:
1263 		custom_map = byt_rt5640_intmic_in1_map;
1264 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
1265 		break;
1266 	case BYT_RT5640_IN3_MAP:
1267 		custom_map = byt_rt5640_intmic_in3_map;
1268 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
1269 		break;
1270 	case BYT_RT5640_DMIC1_MAP:
1271 		custom_map = byt_rt5640_intmic_dmic1_map;
1272 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
1273 		break;
1274 	case BYT_RT5640_DMIC2_MAP:
1275 		custom_map = byt_rt5640_intmic_dmic2_map;
1276 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
1277 		break;
1278 	}
1279 
1280 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
1281 	if (ret)
1282 		return ret;
1283 
1284 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1) {
1285 		ret = snd_soc_dapm_add_routes(&card->dapm,
1286 					byt_rt5640_hsmic2_in1_map,
1287 					ARRAY_SIZE(byt_rt5640_hsmic2_in1_map));
1288 		if (ret)
1289 			return ret;
1290 	}
1291 
1292 	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
1293 		ret = snd_soc_dapm_add_routes(&card->dapm,
1294 					byt_rt5640_ssp2_aif2_map,
1295 					ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
1296 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
1297 		ret = snd_soc_dapm_add_routes(&card->dapm,
1298 					byt_rt5640_ssp0_aif1_map,
1299 					ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
1300 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
1301 		ret = snd_soc_dapm_add_routes(&card->dapm,
1302 					byt_rt5640_ssp0_aif2_map,
1303 					ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
1304 	} else {
1305 		ret = snd_soc_dapm_add_routes(&card->dapm,
1306 					byt_rt5640_ssp2_aif1_map,
1307 					ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
1308 	}
1309 	if (ret)
1310 		return ret;
1311 
1312 	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1313 		ret = snd_soc_dapm_add_routes(&card->dapm,
1314 					byt_rt5640_mono_spk_map,
1315 					ARRAY_SIZE(byt_rt5640_mono_spk_map));
1316 	} else if (!(byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)) {
1317 		ret = snd_soc_dapm_add_routes(&card->dapm,
1318 					byt_rt5640_stereo_spk_map,
1319 					ARRAY_SIZE(byt_rt5640_stereo_spk_map));
1320 	}
1321 	if (ret)
1322 		return ret;
1323 
1324 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1325 		ret = snd_soc_dapm_add_routes(&card->dapm,
1326 					byt_rt5640_lineout_map,
1327 					ARRAY_SIZE(byt_rt5640_lineout_map));
1328 		if (ret)
1329 			return ret;
1330 	}
1331 
1332 	/*
1333 	 * The firmware might enable the clock at boot (this information
1334 	 * may or may not be reflected in the enable clock register).
1335 	 * To change the rate we must disable the clock first to cover
1336 	 * these cases. Due to common clock framework restrictions that
1337 	 * do not allow to disable a clock that has not been enabled,
1338 	 * we need to enable the clock first.
1339 	 */
1340 	ret = clk_prepare_enable(priv->mclk);
1341 	if (!ret)
1342 		clk_disable_unprepare(priv->mclk);
1343 
1344 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1345 		ret = clk_set_rate(priv->mclk, 25000000);
1346 	else
1347 		ret = clk_set_rate(priv->mclk, 19200000);
1348 	if (ret) {
1349 		dev_err(card->dev, "unable to set MCLK rate\n");
1350 		return ret;
1351 	}
1352 
1353 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1354 		ret = snd_soc_card_jack_new_pins(card, "Headset",
1355 						 SND_JACK_HEADSET | SND_JACK_BTN_0,
1356 						 &priv->jack, rt5640_pins,
1357 						 ARRAY_SIZE(rt5640_pins));
1358 		if (ret) {
1359 			dev_err(card->dev, "Jack creation failed %d\n", ret);
1360 			return ret;
1361 		}
1362 		snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1363 				 KEY_PLAYPAUSE);
1364 
1365 		if (byt_rt5640_quirk & BYT_RT5640_USE_AMCR0F28) {
1366 			ret = byt_rt5640_get_amcr0f28_settings(card);
1367 			if (ret)
1368 				return ret;
1369 		}
1370 
1371 		snd_soc_component_set_jack(component, &priv->jack, &priv->jack_data);
1372 	}
1373 
1374 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1375 		ret = snd_soc_card_jack_new_pins(card, "Headset",
1376 						 SND_JACK_HEADSET,
1377 						 &priv->jack, rt5640_pins,
1378 						 ARRAY_SIZE(rt5640_pins));
1379 		if (ret)
1380 			return ret;
1381 
1382 		ret = snd_soc_card_jack_new_pins(card, "Headset 2",
1383 						 SND_JACK_HEADSET,
1384 						 &priv->jack2, rt5640_pins2,
1385 						 ARRAY_SIZE(rt5640_pins2));
1386 		if (ret)
1387 			return ret;
1388 
1389 		rt5640_jack_gpio.data = priv;
1390 		rt5640_jack_gpio.gpiod_dev = priv->codec_dev;
1391 		rt5640_jack_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack1_check;
1392 		ret = snd_soc_jack_add_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1393 		if (ret)
1394 			return ret;
1395 
1396 		rt5640_set_ovcd_params(component);
1397 		rt5640_jack2_gpio.data = component;
1398 		rt5640_jack2_gpio.gpiod_dev = priv->codec_dev;
1399 		rt5640_jack2_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack2_check;
1400 		ret = snd_soc_jack_add_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1401 		if (ret) {
1402 			snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1403 			return ret;
1404 		}
1405 	}
1406 
1407 	return 0;
1408 }
1409 
1410 static void byt_rt5640_exit(struct snd_soc_pcm_runtime *runtime)
1411 {
1412 	struct snd_soc_card *card = runtime->card;
1413 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1414 
1415 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1416 		snd_soc_jack_free_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1417 		snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1418 	}
1419 }
1420 
1421 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1422 			    struct snd_pcm_hw_params *params)
1423 {
1424 	struct snd_interval *rate = hw_param_interval(params,
1425 			SNDRV_PCM_HW_PARAM_RATE);
1426 	struct snd_interval *channels = hw_param_interval(params,
1427 						SNDRV_PCM_HW_PARAM_CHANNELS);
1428 	int ret, bits;
1429 
1430 	/* The DSP will convert the FE rate to 48k, stereo */
1431 	rate->min = rate->max = 48000;
1432 	channels->min = channels->max = 2;
1433 
1434 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1435 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1436 		/* set SSP0 to 16-bit */
1437 		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1438 		bits = 16;
1439 	} else {
1440 		/* set SSP2 to 24-bit */
1441 		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1442 		bits = 24;
1443 	}
1444 
1445 	/*
1446 	 * Default mode for SSP configuration is TDM 4 slot, override config
1447 	 * with explicit setting to I2S 2ch. The word length is set with
1448 	 * dai_set_tdm_slot() since there is no other API exposed
1449 	 */
1450 	ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
1451 				  SND_SOC_DAIFMT_I2S     |
1452 				  SND_SOC_DAIFMT_NB_NF   |
1453 				  SND_SOC_DAIFMT_BP_FP);
1454 	if (ret < 0) {
1455 		dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1456 		return ret;
1457 	}
1458 
1459 	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
1460 	if (ret < 0) {
1461 		dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1462 		return ret;
1463 	}
1464 
1465 	return 0;
1466 }
1467 
1468 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1469 {
1470 	return snd_pcm_hw_constraint_single(substream->runtime,
1471 			SNDRV_PCM_HW_PARAM_RATE, 48000);
1472 }
1473 
1474 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1475 	.startup = byt_rt5640_aif1_startup,
1476 };
1477 
1478 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1479 	.hw_params = byt_rt5640_aif1_hw_params,
1480 };
1481 
1482 SND_SOC_DAILINK_DEF(dummy,
1483 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
1484 
1485 SND_SOC_DAILINK_DEF(media,
1486 	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
1487 
1488 SND_SOC_DAILINK_DEF(deepbuffer,
1489 	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
1490 
1491 SND_SOC_DAILINK_DEF(ssp2_port,
1492 	/* overwritten for ssp0 routing */
1493 	DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
1494 SND_SOC_DAILINK_DEF(ssp2_codec,
1495 	DAILINK_COMP_ARRAY(COMP_CODEC(
1496 	/* overwritten with HID */ "i2c-10EC5640:00",
1497 	/* changed w/ quirk */	"rt5640-aif1")));
1498 
1499 SND_SOC_DAILINK_DEF(platform,
1500 	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
1501 
1502 static struct snd_soc_dai_link byt_rt5640_dais[] = {
1503 	[MERR_DPCM_AUDIO] = {
1504 		.name = "Baytrail Audio Port",
1505 		.stream_name = "Baytrail Audio",
1506 		.nonatomic = true,
1507 		.dynamic = 1,
1508 		.dpcm_playback = 1,
1509 		.dpcm_capture = 1,
1510 		.ops = &byt_rt5640_aif1_ops,
1511 		SND_SOC_DAILINK_REG(media, dummy, platform),
1512 	},
1513 	[MERR_DPCM_DEEP_BUFFER] = {
1514 		.name = "Deep-Buffer Audio Port",
1515 		.stream_name = "Deep-Buffer Audio",
1516 		.nonatomic = true,
1517 		.dynamic = 1,
1518 		.dpcm_playback = 1,
1519 		.ops = &byt_rt5640_aif1_ops,
1520 		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
1521 	},
1522 		/* back ends */
1523 	{
1524 		.name = "SSP2-Codec",
1525 		.id = 0,
1526 		.no_pcm = 1,
1527 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1528 						| SND_SOC_DAIFMT_CBC_CFC,
1529 		.be_hw_params_fixup = byt_rt5640_codec_fixup,
1530 		.dpcm_playback = 1,
1531 		.dpcm_capture = 1,
1532 		.init = byt_rt5640_init,
1533 		.exit = byt_rt5640_exit,
1534 		.ops = &byt_rt5640_be_ssp2_ops,
1535 		SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
1536 	},
1537 };
1538 
1539 /* SoC card */
1540 static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1541 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1542 static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1543 #endif
1544 static char byt_rt5640_components[64]; /* = "cfg-spk:* cfg-mic:* ..." */
1545 
1546 static int byt_rt5640_suspend(struct snd_soc_card *card)
1547 {
1548 	struct snd_soc_component *component;
1549 
1550 	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1551 		return 0;
1552 
1553 	for_each_card_components(card, component) {
1554 		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1555 			dev_dbg(component->dev, "disabling jack detect before suspend\n");
1556 			snd_soc_component_set_jack(component, NULL, NULL);
1557 			break;
1558 		}
1559 	}
1560 
1561 	return 0;
1562 }
1563 
1564 static int byt_rt5640_resume(struct snd_soc_card *card)
1565 {
1566 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1567 	struct snd_soc_component *component;
1568 
1569 	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1570 		return 0;
1571 
1572 	for_each_card_components(card, component) {
1573 		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1574 			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1575 			snd_soc_component_set_jack(component, &priv->jack,
1576 						   &priv->jack_data);
1577 			break;
1578 		}
1579 	}
1580 
1581 	return 0;
1582 }
1583 
1584 /* use space before codec name to simplify card ID, and simplify driver name */
1585 #define SOF_CARD_NAME "bytcht rt5640" /* card name will be 'sof-bytcht rt5640' */
1586 #define SOF_DRIVER_NAME "SOF"
1587 
1588 #define CARD_NAME "bytcr-rt5640"
1589 #define DRIVER_NAME NULL /* card name will be used for driver name */
1590 
1591 static struct snd_soc_card byt_rt5640_card = {
1592 	.owner = THIS_MODULE,
1593 	.dai_link = byt_rt5640_dais,
1594 	.num_links = ARRAY_SIZE(byt_rt5640_dais),
1595 	.dapm_widgets = byt_rt5640_widgets,
1596 	.num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1597 	.dapm_routes = byt_rt5640_audio_map,
1598 	.num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1599 	.fully_routed = true,
1600 	.suspend_pre = byt_rt5640_suspend,
1601 	.resume_post = byt_rt5640_resume,
1602 };
1603 
1604 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
1605 	u64 aif_value;       /* 1: AIF1, 2: AIF2 */
1606 	u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
1607 };
1608 
1609 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1610 {
1611 	struct device *dev = &pdev->dev;
1612 	static const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3", "none" };
1613 	struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
1614 	__maybe_unused const char *spk_type;
1615 	const struct dmi_system_id *dmi_id;
1616 	const char *headset2_string = "";
1617 	const char *lineout_string = "";
1618 	struct byt_rt5640_private *priv;
1619 	const char *platform_name;
1620 	struct acpi_device *adev;
1621 	struct device *codec_dev;
1622 	bool sof_parent;
1623 	int ret_val = 0;
1624 	int dai_index = 0;
1625 	int i, cfg_spk;
1626 	int aif;
1627 
1628 	is_bytcr = false;
1629 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1630 	if (!priv)
1631 		return -ENOMEM;
1632 
1633 	/* register the soc card */
1634 	byt_rt5640_card.dev = dev;
1635 	snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1636 
1637 	/* fix index of codec dai */
1638 	for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1639 		if (!strcmp(byt_rt5640_dais[i].codecs->name,
1640 			    "i2c-10EC5640:00")) {
1641 			dai_index = i;
1642 			break;
1643 		}
1644 	}
1645 
1646 	/* fixup codec name based on HID */
1647 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
1648 	if (adev) {
1649 		snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1650 			 "i2c-%s", acpi_dev_name(adev));
1651 		byt_rt5640_dais[dai_index].codecs->name = byt_rt5640_codec_name;
1652 	} else {
1653 		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
1654 		return -ENXIO;
1655 	}
1656 
1657 	codec_dev = acpi_get_first_physical_node(adev);
1658 	acpi_dev_put(adev);
1659 	if (!codec_dev)
1660 		return -EPROBE_DEFER;
1661 	priv->codec_dev = get_device(codec_dev);
1662 
1663 	/*
1664 	 * swap SSP0 if bytcr is detected
1665 	 * (will be overridden if DMI quirk is detected)
1666 	 */
1667 	if (soc_intel_is_byt()) {
1668 		if (mach->mach_params.acpi_ipc_irq_index == 0)
1669 			is_bytcr = true;
1670 	}
1671 
1672 	if (is_bytcr) {
1673 		/*
1674 		 * Baytrail CR platforms may have CHAN package in BIOS, try
1675 		 * to find relevant routing quirk based as done on Windows
1676 		 * platforms. We have to read the information directly from the
1677 		 * BIOS, at this stage the card is not created and the links
1678 		 * with the codec driver/pdata are non-existent
1679 		 */
1680 
1681 		struct acpi_chan_package chan_package = { 0 };
1682 
1683 		/* format specified: 2 64-bit integers */
1684 		struct acpi_buffer format = {sizeof("NN"), "NN"};
1685 		struct acpi_buffer state = {0, NULL};
1686 		struct snd_soc_acpi_package_context pkg_ctx;
1687 		bool pkg_found = false;
1688 
1689 		state.length = sizeof(chan_package);
1690 		state.pointer = &chan_package;
1691 
1692 		pkg_ctx.name = "CHAN";
1693 		pkg_ctx.length = 2;
1694 		pkg_ctx.format = &format;
1695 		pkg_ctx.state = &state;
1696 		pkg_ctx.data_valid = false;
1697 
1698 		pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1699 							       &pkg_ctx);
1700 		if (pkg_found) {
1701 			if (chan_package.aif_value == 1) {
1702 				dev_info(dev, "BIOS Routing: AIF1 connected\n");
1703 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1704 			} else  if (chan_package.aif_value == 2) {
1705 				dev_info(dev, "BIOS Routing: AIF2 connected\n");
1706 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1707 			} else {
1708 				dev_info(dev, "BIOS Routing isn't valid, ignored\n");
1709 				pkg_found = false;
1710 			}
1711 		}
1712 
1713 		if (!pkg_found) {
1714 			/* no BIOS indications, assume SSP0-AIF2 connection */
1715 			byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1716 		}
1717 
1718 		/* change defaults for Baytrail-CR capture */
1719 		byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1720 	} else {
1721 		byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1722 				    BYT_RT5640_JD_SRC_JD2_IN4N |
1723 				    BYT_RT5640_OVCD_TH_2000UA |
1724 				    BYT_RT5640_OVCD_SF_0P75;
1725 	}
1726 
1727 	/* check quirks before creating card */
1728 	dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1729 	if (dmi_id)
1730 		byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1731 	if (quirk_override != -1) {
1732 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
1733 			 byt_rt5640_quirk, quirk_override);
1734 		byt_rt5640_quirk = quirk_override;
1735 	}
1736 
1737 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1738 		acpi_dev_add_driver_gpios(ACPI_COMPANION(priv->codec_dev),
1739 					  byt_rt5640_hp_elitepad_1000g2_gpios);
1740 
1741 		priv->hsmic_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1742 							   "headset-mic-detect", GPIOD_IN,
1743 							   "headset-mic-detect");
1744 		if (IS_ERR(priv->hsmic_detect)) {
1745 			ret_val = dev_err_probe(dev, PTR_ERR(priv->hsmic_detect),
1746 						"getting hsmic-detect GPIO\n");
1747 			goto err_device;
1748 		}
1749 	}
1750 
1751 	/* Must be called before register_card, also see declaration comment. */
1752 	ret_val = byt_rt5640_add_codec_device_props(codec_dev, priv);
1753 	if (ret_val)
1754 		goto err_remove_gpios;
1755 
1756 	log_quirks(dev);
1757 
1758 	if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1759 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1760 		byt_rt5640_dais[dai_index].codecs->dai_name = "rt5640-aif2";
1761 		aif = 2;
1762 	} else {
1763 		aif = 1;
1764 	}
1765 
1766 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1767 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1768 		byt_rt5640_dais[dai_index].cpus->dai_name = "ssp0-port";
1769 
1770 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1771 		priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
1772 		if (IS_ERR(priv->mclk)) {
1773 			ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
1774 						"Failed to get MCLK from pmc_plt_clk_3\n");
1775 			goto err;
1776 		}
1777 		/*
1778 		 * Fall back to bit clock usage when clock is not
1779 		 * available likely due to missing dependencies.
1780 		 */
1781 		if (!priv->mclk)
1782 			byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1783 	}
1784 
1785 	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS) {
1786 		cfg_spk = 0;
1787 		spk_type = "none";
1788 	} else if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1789 		cfg_spk = 1;
1790 		spk_type = "mono";
1791 	} else {
1792 		cfg_spk = 2;
1793 		spk_type = "stereo";
1794 	}
1795 
1796 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1797 		if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
1798 			lineout_string = " cfg-hp2:lineout";
1799 		else
1800 			lineout_string = " cfg-lineout:2";
1801 	}
1802 
1803 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
1804 		headset2_string = " cfg-hs2:in1";
1805 
1806 	snprintf(byt_rt5640_components, sizeof(byt_rt5640_components),
1807 		 "cfg-spk:%d cfg-mic:%s aif:%d%s%s", cfg_spk,
1808 		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)], aif,
1809 		 lineout_string, headset2_string);
1810 	byt_rt5640_card.components = byt_rt5640_components;
1811 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1812 	snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1813 		 "bytcr-rt5640-%s-spk-%s-mic", spk_type,
1814 		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1815 	byt_rt5640_card.long_name = byt_rt5640_long_name;
1816 #endif
1817 
1818 	/* override platform name, if required */
1819 	platform_name = mach->mach_params.platform;
1820 
1821 	ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5640_card,
1822 							platform_name);
1823 	if (ret_val)
1824 		goto err;
1825 
1826 	sof_parent = snd_soc_acpi_sof_parent(dev);
1827 
1828 	/* set card and driver name */
1829 	if (sof_parent) {
1830 		byt_rt5640_card.name = SOF_CARD_NAME;
1831 		byt_rt5640_card.driver_name = SOF_DRIVER_NAME;
1832 	} else {
1833 		byt_rt5640_card.name = CARD_NAME;
1834 		byt_rt5640_card.driver_name = DRIVER_NAME;
1835 	}
1836 
1837 	/* set pm ops */
1838 	if (sof_parent)
1839 		dev->driver->pm = &snd_soc_pm_ops;
1840 
1841 	ret_val = devm_snd_soc_register_card(dev, &byt_rt5640_card);
1842 	if (ret_val) {
1843 		dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
1844 		goto err;
1845 	}
1846 	platform_set_drvdata(pdev, &byt_rt5640_card);
1847 	return ret_val;
1848 
1849 err:
1850 	device_remove_software_node(priv->codec_dev);
1851 err_remove_gpios:
1852 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1853 		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1854 err_device:
1855 	put_device(priv->codec_dev);
1856 	return ret_val;
1857 }
1858 
1859 static void snd_byt_rt5640_mc_remove(struct platform_device *pdev)
1860 {
1861 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1862 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1863 
1864 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1865 		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1866 
1867 	device_remove_software_node(priv->codec_dev);
1868 	put_device(priv->codec_dev);
1869 }
1870 
1871 static struct platform_driver snd_byt_rt5640_mc_driver = {
1872 	.driver = {
1873 		.name = "bytcr_rt5640",
1874 	},
1875 	.probe = snd_byt_rt5640_mc_probe,
1876 	.remove_new = snd_byt_rt5640_mc_remove,
1877 };
1878 
1879 module_platform_driver(snd_byt_rt5640_mc_driver);
1880 
1881 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1882 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1883 MODULE_LICENSE("GPL v2");
1884 MODULE_ALIAS("platform:bytcr_rt5640");
1885