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