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