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