1 /*
2  *  bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
3  *  (derived from bytcr_rt5640.c)
4  *
5  *  Copyright (C) 2015 Intel Corp
6  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  General Public License for more details.
16  *
17  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18  */
19 
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/acpi.h>
26 #include <linux/clk.h>
27 #include <linux/device.h>
28 #include <linux/dmi.h>
29 #include <linux/input.h>
30 #include <linux/gpio/consumer.h>
31 #include <linux/gpio/machine.h>
32 #include <linux/slab.h>
33 #include <asm/cpu_device_id.h>
34 #include <asm/intel-family.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/jack.h>
39 #include <sound/soc-acpi.h>
40 #include "../../codecs/rt5651.h"
41 #include "../atom/sst-atom-controls.h"
42 
43 enum {
44 	BYT_RT5651_DMIC_MAP,
45 	BYT_RT5651_IN1_MAP,
46 	BYT_RT5651_IN2_MAP,
47 	BYT_RT5651_IN1_IN2_MAP,
48 };
49 
50 enum {
51 	BYT_RT5651_JD_NULL	= (RT5651_JD_NULL << 4),
52 	BYT_RT5651_JD1_1	= (RT5651_JD1_1 << 4),
53 	BYT_RT5651_JD1_2	= (RT5651_JD1_2 << 4),
54 	BYT_RT5651_JD2		= (RT5651_JD2 << 4),
55 };
56 
57 enum {
58 	BYT_RT5651_OVCD_TH_600UA  = (6 << 8),
59 	BYT_RT5651_OVCD_TH_1500UA = (15 << 8),
60 	BYT_RT5651_OVCD_TH_2000UA = (20 << 8),
61 };
62 
63 enum {
64 	BYT_RT5651_OVCD_SF_0P5	= (RT5651_OVCD_SF_0P5 << 13),
65 	BYT_RT5651_OVCD_SF_0P75	= (RT5651_OVCD_SF_0P75 << 13),
66 	BYT_RT5651_OVCD_SF_1P0	= (RT5651_OVCD_SF_1P0 << 13),
67 	BYT_RT5651_OVCD_SF_1P5	= (RT5651_OVCD_SF_1P5 << 13),
68 };
69 
70 #define BYT_RT5651_MAP(quirk)		((quirk) & GENMASK(3, 0))
71 #define BYT_RT5651_JDSRC(quirk)		(((quirk) & GENMASK(7, 4)) >> 4)
72 #define BYT_RT5651_OVCD_TH(quirk)	(((quirk) & GENMASK(12, 8)) >> 8)
73 #define BYT_RT5651_OVCD_SF(quirk)	(((quirk) & GENMASK(14, 13)) >> 13)
74 #define BYT_RT5651_DMIC_EN		BIT(16)
75 #define BYT_RT5651_MCLK_EN		BIT(17)
76 #define BYT_RT5651_MCLK_25MHZ		BIT(18)
77 #define BYT_RT5651_SSP2_AIF2		BIT(19) /* default is using AIF1  */
78 #define BYT_RT5651_SSP0_AIF1		BIT(20)
79 #define BYT_RT5651_SSP0_AIF2		BIT(21)
80 #define BYT_RT5651_HP_LR_SWAPPED	BIT(22)
81 #define BYT_RT5651_MONO_SPEAKER		BIT(23)
82 #define BYT_RT5651_JD_NOT_INV		BIT(24)
83 
84 #define BYT_RT5651_DEFAULT_QUIRKS	(BYT_RT5651_MCLK_EN | \
85 					 BYT_RT5651_JD1_1   | \
86 					 BYT_RT5651_OVCD_TH_2000UA | \
87 					 BYT_RT5651_OVCD_SF_0P75)
88 
89 /* jack-detect-source + inv + dmic-en + ovcd-th + -sf + terminating entry */
90 #define MAX_NO_PROPS 6
91 
92 struct byt_rt5651_private {
93 	struct clk *mclk;
94 	struct gpio_desc *ext_amp_gpio;
95 	struct gpio_desc *hp_detect;
96 	struct snd_soc_jack jack;
97 };
98 
99 static const struct acpi_gpio_mapping *byt_rt5651_gpios;
100 
101 /* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */
102 static unsigned long byt_rt5651_quirk = BYT_RT5651_DEFAULT_QUIRKS |
103 					BYT_RT5651_IN2_MAP;
104 
105 static int quirk_override = -1;
106 module_param_named(quirk, quirk_override, int, 0444);
107 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
108 
109 static void log_quirks(struct device *dev)
110 {
111 	if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP)
112 		dev_info(dev, "quirk DMIC_MAP enabled");
113 	if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP)
114 		dev_info(dev, "quirk IN1_MAP enabled");
115 	if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP)
116 		dev_info(dev, "quirk IN2_MAP enabled");
117 	if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_IN2_MAP)
118 		dev_info(dev, "quirk IN1_IN2_MAP enabled");
119 	if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
120 		dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
121 			 BYT_RT5651_JDSRC(byt_rt5651_quirk));
122 		dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
123 			 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
124 		dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
125 			 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
126 	}
127 	if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
128 		dev_info(dev, "quirk DMIC enabled");
129 	if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
130 		dev_info(dev, "quirk MCLK_EN enabled");
131 	if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
132 		dev_info(dev, "quirk MCLK_25MHZ enabled");
133 	if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2)
134 		dev_info(dev, "quirk SSP2_AIF2 enabled\n");
135 	if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1)
136 		dev_info(dev, "quirk SSP0_AIF1 enabled\n");
137 	if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)
138 		dev_info(dev, "quirk SSP0_AIF2 enabled\n");
139 	if (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER)
140 		dev_info(dev, "quirk MONO_SPEAKER enabled\n");
141 	if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
142 		dev_info(dev, "quirk JD_NOT_INV enabled\n");
143 }
144 
145 #define BYT_CODEC_DAI1	"rt5651-aif1"
146 #define BYT_CODEC_DAI2	"rt5651-aif2"
147 
148 static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
149 					      int rate, int bclk_ratio)
150 {
151 	int clk_id, clk_freq, ret;
152 
153 	/* Configure the PLL before selecting it */
154 	if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
155 		clk_id = RT5651_PLL1_S_BCLK1,
156 		clk_freq = rate * bclk_ratio;
157 	} else {
158 		clk_id = RT5651_PLL1_S_MCLK;
159 		if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
160 			clk_freq = 25000000;
161 		else
162 			clk_freq = 19200000;
163 	}
164 	ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512);
165 	if (ret < 0) {
166 		dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
167 		return ret;
168 	}
169 
170 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
171 				     rate * 512, SND_SOC_CLOCK_IN);
172 	if (ret < 0) {
173 		dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
174 		return ret;
175 	}
176 
177 	return 0;
178 }
179 
180 static int platform_clock_control(struct snd_soc_dapm_widget *w,
181 				  struct snd_kcontrol *k, int  event)
182 {
183 	struct snd_soc_dapm_context *dapm = w->dapm;
184 	struct snd_soc_card *card = dapm->card;
185 	struct snd_soc_dai *codec_dai;
186 	struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
187 	int ret;
188 
189 	codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
190 	if (!codec_dai)
191 		codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
192 	if (!codec_dai) {
193 		dev_err(card->dev,
194 			"Codec dai not found; Unable to set platform clock\n");
195 		return -EIO;
196 	}
197 
198 	if (SND_SOC_DAPM_EVENT_ON(event)) {
199 		if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
200 			ret = clk_prepare_enable(priv->mclk);
201 			if (ret < 0) {
202 				dev_err(card->dev,
203 					"could not configure MCLK state");
204 				return ret;
205 			}
206 		}
207 		ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50);
208 	} else {
209 		/*
210 		 * Set codec clock source to internal clock before
211 		 * turning off the platform clock. Codec needs clock
212 		 * for Jack detection and button press
213 		 */
214 		ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
215 					     48000 * 512,
216 					     SND_SOC_CLOCK_IN);
217 		if (!ret)
218 			if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
219 				clk_disable_unprepare(priv->mclk);
220 	}
221 
222 	if (ret < 0) {
223 		dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
224 		return ret;
225 	}
226 
227 	return 0;
228 }
229 
230 static int rt5651_ext_amp_power_event(struct snd_soc_dapm_widget *w,
231 	struct snd_kcontrol *kcontrol, int event)
232 {
233 	struct snd_soc_card *card = w->dapm->card;
234 	struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
235 
236 	if (SND_SOC_DAPM_EVENT_ON(event))
237 		gpiod_set_value_cansleep(priv->ext_amp_gpio, 1);
238 	else
239 		gpiod_set_value_cansleep(priv->ext_amp_gpio, 0);
240 
241 	return 0;
242 }
243 
244 static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
245 	SND_SOC_DAPM_HP("Headphone", NULL),
246 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
247 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
248 	SND_SOC_DAPM_SPK("Speaker", NULL),
249 	SND_SOC_DAPM_LINE("Line In", NULL),
250 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
251 			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
252 			    SND_SOC_DAPM_POST_PMD),
253 	SND_SOC_DAPM_SUPPLY("Ext Amp Power", SND_SOC_NOPM, 0, 0,
254 			    rt5651_ext_amp_power_event,
255 			    SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
256 };
257 
258 static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
259 	{"Headphone", NULL, "Platform Clock"},
260 	{"Headset Mic", NULL, "Platform Clock"},
261 	{"Internal Mic", NULL, "Platform Clock"},
262 	{"Speaker", NULL, "Platform Clock"},
263 	{"Speaker", NULL, "Ext Amp Power"},
264 	{"Line In", NULL, "Platform Clock"},
265 
266 	{"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
267 	{"Headphone", NULL, "HPOL"},
268 	{"Headphone", NULL, "HPOR"},
269 	{"Speaker", NULL, "LOUTL"},
270 	{"Speaker", NULL, "LOUTR"},
271 	{"IN2P", NULL, "Line In"},
272 	{"IN2N", NULL, "Line In"},
273 
274 };
275 
276 static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
277 	{"DMIC L1", NULL, "Internal Mic"},
278 	{"DMIC R1", NULL, "Internal Mic"},
279 	{"IN2P", NULL, "Headset Mic"},
280 };
281 
282 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
283 	{"Internal Mic", NULL, "micbias1"},
284 	{"IN1P", NULL, "Internal Mic"},
285 	{"IN3P", NULL, "Headset Mic"},
286 };
287 
288 static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
289 	{"Internal Mic", NULL, "micbias1"},
290 	{"IN2P", NULL, "Internal Mic"},
291 	{"IN3P", NULL, "Headset Mic"},
292 };
293 
294 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
295 	{"Internal Mic", NULL, "micbias1"},
296 	{"IN1P", NULL, "Internal Mic"},
297 	{"IN2P", NULL, "Internal Mic"},
298 	{"IN3P", NULL, "Headset Mic"},
299 };
300 
301 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = {
302 	{"ssp0 Tx", NULL, "modem_out"},
303 	{"modem_in", NULL, "ssp0 Rx"},
304 
305 	{"AIF1 Playback", NULL, "ssp0 Tx"},
306 	{"ssp0 Rx", NULL, "AIF1 Capture"},
307 };
308 
309 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = {
310 	{"ssp0 Tx", NULL, "modem_out"},
311 	{"modem_in", NULL, "ssp0 Rx"},
312 
313 	{"AIF2 Playback", NULL, "ssp0 Tx"},
314 	{"ssp0 Rx", NULL, "AIF2 Capture"},
315 };
316 
317 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = {
318 	{"ssp2 Tx", NULL, "codec_out0"},
319 	{"ssp2 Tx", NULL, "codec_out1"},
320 	{"codec_in0", NULL, "ssp2 Rx"},
321 	{"codec_in1", NULL, "ssp2 Rx"},
322 
323 	{"AIF1 Playback", NULL, "ssp2 Tx"},
324 	{"ssp2 Rx", NULL, "AIF1 Capture"},
325 };
326 
327 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_map[] = {
328 	{"ssp2 Tx", NULL, "codec_out0"},
329 	{"ssp2 Tx", NULL, "codec_out1"},
330 	{"codec_in0", NULL, "ssp2 Rx"},
331 	{"codec_in1", NULL, "ssp2 Rx"},
332 
333 	{"AIF2 Playback", NULL, "ssp2 Tx"},
334 	{"ssp2 Rx", NULL, "AIF2 Capture"},
335 };
336 
337 static const struct snd_kcontrol_new byt_rt5651_controls[] = {
338 	SOC_DAPM_PIN_SWITCH("Headphone"),
339 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
340 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
341 	SOC_DAPM_PIN_SWITCH("Speaker"),
342 	SOC_DAPM_PIN_SWITCH("Line In"),
343 };
344 
345 static struct snd_soc_jack_pin bytcr_jack_pins[] = {
346 	{
347 		.pin	= "Headphone",
348 		.mask	= SND_JACK_HEADPHONE,
349 	},
350 	{
351 		.pin	= "Headset Mic",
352 		.mask	= SND_JACK_MICROPHONE,
353 	},
354 };
355 
356 static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
357 					struct snd_pcm_hw_params *params)
358 {
359 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
360 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
361 	snd_pcm_format_t format = params_format(params);
362 	int rate = params_rate(params);
363 	int bclk_ratio;
364 
365 	if (format == SNDRV_PCM_FORMAT_S16_LE)
366 		bclk_ratio = 32;
367 	else
368 		bclk_ratio = 50;
369 
370 	return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio);
371 }
372 
373 static const struct acpi_gpio_params pov_p1006w_hp_detect = { 1, 0, false };
374 static const struct acpi_gpio_params pov_p1006w_ext_amp_en = { 2, 0, true };
375 
376 static const struct acpi_gpio_mapping byt_rt5651_pov_p1006w_gpios[] = {
377 	{ "hp-detect-gpios", &pov_p1006w_hp_detect, 1, },
378 	{ "ext-amp-enable-gpios", &pov_p1006w_ext_amp_en, 1, },
379 	{ },
380 };
381 
382 static int byt_rt5651_pov_p1006w_quirk_cb(const struct dmi_system_id *id)
383 {
384 	byt_rt5651_quirk = (unsigned long)id->driver_data;
385 	byt_rt5651_gpios = byt_rt5651_pov_p1006w_gpios;
386 	return 1;
387 }
388 
389 static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
390 {
391 	byt_rt5651_quirk = (unsigned long)id->driver_data;
392 	return 1;
393 }
394 
395 static const struct dmi_system_id byt_rt5651_quirk_table[] = {
396 	{
397 		/* Chuwi Hi8 Pro (CWI513) */
398 		.callback = byt_rt5651_quirk_cb,
399 		.matches = {
400 			DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
401 			DMI_MATCH(DMI_PRODUCT_NAME, "X1D3_C806N"),
402 		},
403 		.driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
404 					BYT_RT5651_IN2_MAP |
405 					BYT_RT5651_HP_LR_SWAPPED |
406 					BYT_RT5651_MONO_SPEAKER),
407 	},
408 	{
409 		/* Chuwi Vi8 Plus (CWI519) */
410 		.callback = byt_rt5651_quirk_cb,
411 		.matches = {
412 			DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
413 			DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"),
414 		},
415 		.driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
416 					BYT_RT5651_IN2_MAP |
417 					BYT_RT5651_HP_LR_SWAPPED |
418 					BYT_RT5651_MONO_SPEAKER),
419 	},
420 	{
421 		/* Complet Electro Serv MY8307 */
422 		.callback = byt_rt5651_quirk_cb,
423 		.matches = {
424 			DMI_MATCH(DMI_SYS_VENDOR, "Complet Electro Serv"),
425 			DMI_MATCH(DMI_PRODUCT_NAME, "MY8307"),
426 		},
427 		.driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
428 					BYT_RT5651_IN2_MAP |
429 					BYT_RT5651_MONO_SPEAKER |
430 					BYT_RT5651_JD_NOT_INV),
431 	},
432 	{
433 		/* I.T.Works TW701, Ployer Momo7w and Trekstor ST70416-6
434 		 * (these all use the same mainboard) */
435 		.callback = byt_rt5651_quirk_cb,
436 		.matches = {
437 			DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
438 			/* Partial match for all of itWORKS.G.WI71C.JGBMRBA,
439 			 * TREK.G.WI71C.JGBMRBA0x and MOMO.G.WI71C.MABMRBA02 */
440 			DMI_MATCH(DMI_BIOS_VERSION, ".G.WI71C."),
441 		},
442 		.driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
443 					BYT_RT5651_IN2_MAP |
444 					BYT_RT5651_SSP0_AIF1 |
445 					BYT_RT5651_MONO_SPEAKER),
446 	},
447 	{
448 		/* KIANO SlimNote 14.2 */
449 		.callback = byt_rt5651_quirk_cb,
450 		.matches = {
451 			DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
452 			DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
453 		},
454 		.driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
455 					BYT_RT5651_IN1_IN2_MAP),
456 	},
457 	{
458 		/* Minnowboard Max B3 */
459 		.callback = byt_rt5651_quirk_cb,
460 		.matches = {
461 			DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
462 			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
463 		},
464 		.driver_data = (void *)(BYT_RT5651_IN1_MAP),
465 	},
466 	{
467 		/* Minnowboard Turbot */
468 		.callback = byt_rt5651_quirk_cb,
469 		.matches = {
470 			DMI_MATCH(DMI_SYS_VENDOR, "ADI"),
471 			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"),
472 		},
473 		.driver_data = (void *)(BYT_RT5651_MCLK_EN |
474 					BYT_RT5651_IN1_MAP),
475 	},
476 	{
477 		/* Point of View mobii wintab p1006w (v1.0) */
478 		.callback = byt_rt5651_pov_p1006w_quirk_cb,
479 		.matches = {
480 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
481 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "BayTrail"),
482 			/* Note 105b is Foxcon's USB/PCI vendor id */
483 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "105B"),
484 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "0E57"),
485 		},
486 		.driver_data = (void *)(BYT_RT5651_DMIC_MAP |
487 					BYT_RT5651_OVCD_TH_2000UA |
488 					BYT_RT5651_OVCD_SF_0P75 |
489 					BYT_RT5651_DMIC_EN |
490 					BYT_RT5651_MCLK_EN |
491 					BYT_RT5651_SSP0_AIF1),
492 	},
493 	{
494 		/* VIOS LTH17 */
495 		.callback = byt_rt5651_quirk_cb,
496 		.matches = {
497 			DMI_MATCH(DMI_SYS_VENDOR, "VIOS"),
498 			DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"),
499 		},
500 		.driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP |
501 					BYT_RT5651_JD1_1 |
502 					BYT_RT5651_OVCD_TH_2000UA |
503 					BYT_RT5651_OVCD_SF_1P0 |
504 					BYT_RT5651_MCLK_EN),
505 	},
506 	{
507 		/* Yours Y8W81 (and others using the same mainboard) */
508 		.callback = byt_rt5651_quirk_cb,
509 		.matches = {
510 			DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
511 			/* Partial match for all devs with a W86C mainboard */
512 			DMI_MATCH(DMI_BIOS_VERSION, ".F.W86C."),
513 		},
514 		.driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
515 					BYT_RT5651_IN2_MAP |
516 					BYT_RT5651_SSP0_AIF1 |
517 					BYT_RT5651_MONO_SPEAKER),
518 	},
519 	{}
520 };
521 
522 /*
523  * Note this MUST be called before snd_soc_register_card(), so that the props
524  * are in place before the codec component driver's probe function parses them.
525  */
526 static int byt_rt5651_add_codec_device_props(struct device *i2c_dev)
527 {
528 	struct property_entry props[MAX_NO_PROPS] = {};
529 	int cnt = 0;
530 
531 	props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source",
532 				BYT_RT5651_JDSRC(byt_rt5651_quirk));
533 
534 	props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp",
535 				BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
536 
537 	props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor",
538 				BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
539 
540 	if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
541 		props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en");
542 
543 	if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
544 		props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
545 
546 	return device_add_properties(i2c_dev, props);
547 }
548 
549 static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
550 {
551 	struct snd_soc_card *card = runtime->card;
552 	struct snd_soc_component *codec = runtime->codec_dai->component;
553 	struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
554 	const struct snd_soc_dapm_route *custom_map;
555 	int num_routes;
556 	int report;
557 	int ret;
558 
559 	card->dapm.idle_bias_off = true;
560 
561 	/* Start with RC clk for jack-detect (we disable MCLK below) */
562 	if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
563 		snd_soc_component_update_bits(codec, RT5651_GLB_CLK,
564 			RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK);
565 
566 	switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
567 	case BYT_RT5651_IN1_MAP:
568 		custom_map = byt_rt5651_intmic_in1_map;
569 		num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
570 		break;
571 	case BYT_RT5651_IN2_MAP:
572 		custom_map = byt_rt5651_intmic_in2_map;
573 		num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
574 		break;
575 	case BYT_RT5651_IN1_IN2_MAP:
576 		custom_map = byt_rt5651_intmic_in1_in2_map;
577 		num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
578 		break;
579 	default:
580 		custom_map = byt_rt5651_intmic_dmic_map;
581 		num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
582 	}
583 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
584 	if (ret)
585 		return ret;
586 
587 	if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) {
588 		ret = snd_soc_dapm_add_routes(&card->dapm,
589 					byt_rt5651_ssp2_aif2_map,
590 					ARRAY_SIZE(byt_rt5651_ssp2_aif2_map));
591 	} else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) {
592 		ret = snd_soc_dapm_add_routes(&card->dapm,
593 					byt_rt5651_ssp0_aif1_map,
594 					ARRAY_SIZE(byt_rt5651_ssp0_aif1_map));
595 	} else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) {
596 		ret = snd_soc_dapm_add_routes(&card->dapm,
597 					byt_rt5651_ssp0_aif2_map,
598 					ARRAY_SIZE(byt_rt5651_ssp0_aif2_map));
599 	} else {
600 		ret = snd_soc_dapm_add_routes(&card->dapm,
601 					byt_rt5651_ssp2_aif1_map,
602 					ARRAY_SIZE(byt_rt5651_ssp2_aif1_map));
603 	}
604 	if (ret)
605 		return ret;
606 
607 	ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
608 					ARRAY_SIZE(byt_rt5651_controls));
609 	if (ret) {
610 		dev_err(card->dev, "unable to add card controls\n");
611 		return ret;
612 	}
613 	snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
614 	snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
615 
616 	if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
617 		/*
618 		 * The firmware might enable the clock at
619 		 * boot (this information may or may not
620 		 * be reflected in the enable clock register).
621 		 * To change the rate we must disable the clock
622 		 * first to cover these cases. Due to common
623 		 * clock framework restrictions that do not allow
624 		 * to disable a clock that has not been enabled,
625 		 * we need to enable the clock first.
626 		 */
627 		ret = clk_prepare_enable(priv->mclk);
628 		if (!ret)
629 			clk_disable_unprepare(priv->mclk);
630 
631 		if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
632 			ret = clk_set_rate(priv->mclk, 25000000);
633 		else
634 			ret = clk_set_rate(priv->mclk, 19200000);
635 
636 		if (ret)
637 			dev_err(card->dev, "unable to set MCLK rate\n");
638 	}
639 
640 	report = 0;
641 	if (BYT_RT5651_JDSRC(byt_rt5651_quirk))
642 		report = SND_JACK_HEADSET | SND_JACK_BTN_0;
643 	else if (priv->hp_detect)
644 		report = SND_JACK_HEADSET;
645 
646 	if (report) {
647 		ret = snd_soc_card_jack_new(runtime->card, "Headset",
648 				    report, &priv->jack, bytcr_jack_pins,
649 				    ARRAY_SIZE(bytcr_jack_pins));
650 		if (ret) {
651 			dev_err(runtime->dev, "jack creation failed %d\n", ret);
652 			return ret;
653 		}
654 
655 		if (report & SND_JACK_BTN_0)
656 			snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
657 					 KEY_PLAYPAUSE);
658 
659 		ret = snd_soc_component_set_jack(codec, &priv->jack,
660 						 priv->hp_detect);
661 		if (ret)
662 			return ret;
663 	}
664 
665 	return 0;
666 }
667 
668 static const struct snd_soc_pcm_stream byt_rt5651_dai_params = {
669 	.formats = SNDRV_PCM_FMTBIT_S24_LE,
670 	.rate_min = 48000,
671 	.rate_max = 48000,
672 	.channels_min = 2,
673 	.channels_max = 2,
674 };
675 
676 static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
677 			    struct snd_pcm_hw_params *params)
678 {
679 	struct snd_interval *rate = hw_param_interval(params,
680 			SNDRV_PCM_HW_PARAM_RATE);
681 	struct snd_interval *channels = hw_param_interval(params,
682 						SNDRV_PCM_HW_PARAM_CHANNELS);
683 	int ret, bits;
684 
685 	/* The DSP will covert the FE rate to 48k, stereo */
686 	rate->min = rate->max = 48000;
687 	channels->min = channels->max = 2;
688 
689 	if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
690 	    (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
691 		/* set SSP0 to 16-bit */
692 		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
693 		bits = 16;
694 	} else {
695 		/* set SSP2 to 24-bit */
696 		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
697 		bits = 24;
698 	}
699 
700 	/*
701 	 * Default mode for SSP configuration is TDM 4 slot, override config
702 	 * with explicit setting to I2S 2ch. The word length is set with
703 	 * dai_set_tdm_slot() since there is no other API exposed
704 	 */
705 	ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
706 				  SND_SOC_DAIFMT_I2S     |
707 				  SND_SOC_DAIFMT_NB_NF   |
708 				  SND_SOC_DAIFMT_CBS_CFS
709 				  );
710 
711 	if (ret < 0) {
712 		dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
713 		return ret;
714 	}
715 
716 	ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, bits);
717 	if (ret < 0) {
718 		dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
719 		return ret;
720 	}
721 
722 	return 0;
723 }
724 
725 static const unsigned int rates_48000[] = {
726 	48000,
727 };
728 
729 static const struct snd_pcm_hw_constraint_list constraints_48000 = {
730 	.count = ARRAY_SIZE(rates_48000),
731 	.list  = rates_48000,
732 };
733 
734 static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
735 {
736 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
737 			SNDRV_PCM_HW_PARAM_RATE,
738 			&constraints_48000);
739 }
740 
741 static const struct snd_soc_ops byt_rt5651_aif1_ops = {
742 	.startup = byt_rt5651_aif1_startup,
743 };
744 
745 static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
746 	.hw_params = byt_rt5651_aif1_hw_params,
747 };
748 
749 static struct snd_soc_dai_link byt_rt5651_dais[] = {
750 	[MERR_DPCM_AUDIO] = {
751 		.name = "Audio Port",
752 		.stream_name = "Audio",
753 		.cpu_dai_name = "media-cpu-dai",
754 		.codec_dai_name = "snd-soc-dummy-dai",
755 		.codec_name = "snd-soc-dummy",
756 		.platform_name = "sst-mfld-platform",
757 		.nonatomic = true,
758 		.dynamic = 1,
759 		.dpcm_playback = 1,
760 		.dpcm_capture = 1,
761 		.ops = &byt_rt5651_aif1_ops,
762 	},
763 	[MERR_DPCM_DEEP_BUFFER] = {
764 		.name = "Deep-Buffer Audio Port",
765 		.stream_name = "Deep-Buffer Audio",
766 		.cpu_dai_name = "deepbuffer-cpu-dai",
767 		.codec_dai_name = "snd-soc-dummy-dai",
768 		.codec_name = "snd-soc-dummy",
769 		.platform_name = "sst-mfld-platform",
770 		.nonatomic = true,
771 		.dynamic = 1,
772 		.dpcm_playback = 1,
773 		.ops = &byt_rt5651_aif1_ops,
774 	},
775 	/* CODEC<->CODEC link */
776 	/* back ends */
777 	{
778 		.name = "SSP2-Codec",
779 		.id = 0,
780 		.cpu_dai_name = "ssp2-port",
781 		.platform_name = "sst-mfld-platform",
782 		.no_pcm = 1,
783 		.codec_dai_name = "rt5651-aif1",
784 		.codec_name = "i2c-10EC5651:00",
785 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
786 						| SND_SOC_DAIFMT_CBS_CFS,
787 		.be_hw_params_fixup = byt_rt5651_codec_fixup,
788 		.ignore_suspend = 1,
789 		.nonatomic = true,
790 		.dpcm_playback = 1,
791 		.dpcm_capture = 1,
792 		.init = byt_rt5651_init,
793 		.ops = &byt_rt5651_be_ssp2_ops,
794 	},
795 };
796 
797 /* SoC card */
798 static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN];
799 static char byt_rt5651_codec_aif_name[12]; /*  = "rt5651-aif[1|2]" */
800 static char byt_rt5651_cpu_dai_name[10]; /*  = "ssp[0|2]-port" */
801 static char byt_rt5651_long_name[50]; /* = "bytcr-rt5651-*-spk-*-mic[-swapped-hp]" */
802 
803 static int byt_rt5651_suspend(struct snd_soc_card *card)
804 {
805 	struct snd_soc_component *component;
806 
807 	if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
808 		return 0;
809 
810 	for_each_card_components(card, component) {
811 		if (!strcmp(component->name, byt_rt5651_codec_name)) {
812 			dev_dbg(component->dev, "disabling jack detect before suspend\n");
813 			snd_soc_component_set_jack(component, NULL, NULL);
814 			break;
815 		}
816 	}
817 
818 	return 0;
819 }
820 
821 static int byt_rt5651_resume(struct snd_soc_card *card)
822 {
823 	struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
824 	struct snd_soc_component *component;
825 
826 	if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
827 		return 0;
828 
829 	for_each_card_components(card, component) {
830 		if (!strcmp(component->name, byt_rt5651_codec_name)) {
831 			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
832 			snd_soc_component_set_jack(component, &priv->jack,
833 						   priv->hp_detect);
834 			break;
835 		}
836 	}
837 
838 	return 0;
839 }
840 
841 static struct snd_soc_card byt_rt5651_card = {
842 	.name = "bytcr-rt5651",
843 	.owner = THIS_MODULE,
844 	.dai_link = byt_rt5651_dais,
845 	.num_links = ARRAY_SIZE(byt_rt5651_dais),
846 	.dapm_widgets = byt_rt5651_widgets,
847 	.num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
848 	.dapm_routes = byt_rt5651_audio_map,
849 	.num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
850 	.fully_routed = true,
851 	.suspend_pre = byt_rt5651_suspend,
852 	.resume_post = byt_rt5651_resume,
853 };
854 
855 static const struct x86_cpu_id baytrail_cpu_ids[] = {
856 	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT }, /* Valleyview */
857 	{}
858 };
859 
860 static const struct x86_cpu_id cherrytrail_cpu_ids[] = {
861 	{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },     /* Braswell */
862 	{}
863 };
864 
865 static const struct acpi_gpio_params ext_amp_enable_gpios = { 0, 0, false };
866 
867 static const struct acpi_gpio_mapping cht_rt5651_gpios[] = {
868 	/*
869 	 * Some boards have I2cSerialBusV2, GpioIo, GpioInt as ACPI resources,
870 	 * other boards may  have I2cSerialBusV2, GpioInt, GpioIo instead.
871 	 * We want the GpioIo one for the ext-amp-enable-gpio.
872 	 */
873 	{ "ext-amp-enable-gpios", &ext_amp_enable_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
874 	{ },
875 };
876 
877 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
878 	u64 aif_value;       /* 1: AIF1, 2: AIF2 */
879 	u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
880 };
881 
882 static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
883 {
884 	static const char * const mic_name[] = { "dmic", "in1", "in2", "in12" };
885 	struct byt_rt5651_private *priv;
886 	struct snd_soc_acpi_mach *mach;
887 	const char *platform_name;
888 	struct acpi_device *adev;
889 	struct device *codec_dev;
890 	const char *hp_swapped;
891 	bool is_bytcr = false;
892 	int ret_val = 0;
893 	int dai_index = 0;
894 	int i;
895 
896 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
897 	if (!priv)
898 		return -ENOMEM;
899 
900 	/* register the soc card */
901 	byt_rt5651_card.dev = &pdev->dev;
902 
903 	mach = byt_rt5651_card.dev->platform_data;
904 	snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
905 
906 	/* fix index of codec dai */
907 	for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
908 		if (!strcmp(byt_rt5651_dais[i].codec_name, "i2c-10EC5651:00")) {
909 			dai_index = i;
910 			break;
911 		}
912 	}
913 
914 	/* fixup codec name based on HID */
915 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
916 	if (adev) {
917 		snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
918 			 "i2c-%s", acpi_dev_name(adev));
919 		put_device(&adev->dev);
920 		byt_rt5651_dais[dai_index].codec_name = byt_rt5651_codec_name;
921 	} else {
922 		dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id);
923 		return -ENODEV;
924 	}
925 
926 	codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL,
927 					    byt_rt5651_codec_name);
928 	if (!codec_dev)
929 		return -EPROBE_DEFER;
930 
931 	/*
932 	 * swap SSP0 if bytcr is detected
933 	 * (will be overridden if DMI quirk is detected)
934 	 */
935 	if (x86_match_cpu(baytrail_cpu_ids)) {
936 		if (mach->mach_params.acpi_ipc_irq_index == 0)
937 			is_bytcr = true;
938 	}
939 
940 	if (is_bytcr) {
941 		/*
942 		 * Baytrail CR platforms may have CHAN package in BIOS, try
943 		 * to find relevant routing quirk based as done on Windows
944 		 * platforms. We have to read the information directly from the
945 		 * BIOS, at this stage the card is not created and the links
946 		 * with the codec driver/pdata are non-existent
947 		 */
948 
949 		struct acpi_chan_package chan_package;
950 
951 		/* format specified: 2 64-bit integers */
952 		struct acpi_buffer format = {sizeof("NN"), "NN"};
953 		struct acpi_buffer state = {0, NULL};
954 		struct snd_soc_acpi_package_context pkg_ctx;
955 		bool pkg_found = false;
956 
957 		state.length = sizeof(chan_package);
958 		state.pointer = &chan_package;
959 
960 		pkg_ctx.name = "CHAN";
961 		pkg_ctx.length = 2;
962 		pkg_ctx.format = &format;
963 		pkg_ctx.state = &state;
964 		pkg_ctx.data_valid = false;
965 
966 		pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
967 							       &pkg_ctx);
968 		if (pkg_found) {
969 			if (chan_package.aif_value == 1) {
970 				dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
971 				byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1;
972 			} else  if (chan_package.aif_value == 2) {
973 				dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
974 				byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
975 			} else {
976 				dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
977 				pkg_found = false;
978 			}
979 		}
980 
981 		if (!pkg_found) {
982 			/* no BIOS indications, assume SSP0-AIF2 connection */
983 			byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
984 		}
985 	}
986 
987 	/* check quirks before creating card */
988 	dmi_check_system(byt_rt5651_quirk_table);
989 
990 	if (quirk_override != -1) {
991 		dev_info(&pdev->dev, "Overriding quirk 0x%x => 0x%x\n",
992 			 (unsigned int)byt_rt5651_quirk, quirk_override);
993 		byt_rt5651_quirk = quirk_override;
994 	}
995 
996 	/* Must be called before register_card, also see declaration comment. */
997 	ret_val = byt_rt5651_add_codec_device_props(codec_dev);
998 	if (ret_val) {
999 		put_device(codec_dev);
1000 		return ret_val;
1001 	}
1002 
1003 	/* Cherry Trail devices use an external amplifier enable gpio */
1004 	if (x86_match_cpu(cherrytrail_cpu_ids) && !byt_rt5651_gpios)
1005 		byt_rt5651_gpios = cht_rt5651_gpios;
1006 
1007 	if (byt_rt5651_gpios) {
1008 		devm_acpi_dev_add_driver_gpios(codec_dev, byt_rt5651_gpios);
1009 		priv->ext_amp_gpio = devm_fwnode_get_index_gpiod_from_child(
1010 						&pdev->dev, "ext-amp-enable", 0,
1011 						codec_dev->fwnode,
1012 						GPIOD_OUT_LOW, "speaker-amp");
1013 		if (IS_ERR(priv->ext_amp_gpio)) {
1014 			ret_val = PTR_ERR(priv->ext_amp_gpio);
1015 			switch (ret_val) {
1016 			case -ENOENT:
1017 				priv->ext_amp_gpio = NULL;
1018 				break;
1019 			default:
1020 				dev_err(&pdev->dev, "Failed to get ext-amp-enable GPIO: %d\n",
1021 					ret_val);
1022 				/* fall through */
1023 			case -EPROBE_DEFER:
1024 				put_device(codec_dev);
1025 				return ret_val;
1026 			}
1027 		}
1028 		priv->hp_detect = devm_fwnode_get_index_gpiod_from_child(
1029 						&pdev->dev, "hp-detect", 0,
1030 						codec_dev->fwnode,
1031 						GPIOD_IN, "hp-detect");
1032 		if (IS_ERR(priv->hp_detect)) {
1033 			ret_val = PTR_ERR(priv->hp_detect);
1034 			switch (ret_val) {
1035 			case -ENOENT:
1036 				priv->hp_detect = NULL;
1037 				break;
1038 			default:
1039 				dev_err(&pdev->dev, "Failed to get hp-detect GPIO: %d\n",
1040 					ret_val);
1041 				/* fall through */
1042 			case -EPROBE_DEFER:
1043 				put_device(codec_dev);
1044 				return ret_val;
1045 			}
1046 		}
1047 	}
1048 
1049 	put_device(codec_dev);
1050 
1051 	log_quirks(&pdev->dev);
1052 
1053 	if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) ||
1054 	    (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
1055 		/* fixup codec aif name */
1056 		snprintf(byt_rt5651_codec_aif_name,
1057 			sizeof(byt_rt5651_codec_aif_name),
1058 			"%s", "rt5651-aif2");
1059 
1060 		byt_rt5651_dais[dai_index].codec_dai_name =
1061 			byt_rt5651_codec_aif_name;
1062 	}
1063 
1064 	if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
1065 	    (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
1066 		/* fixup cpu dai name name */
1067 		snprintf(byt_rt5651_cpu_dai_name,
1068 			sizeof(byt_rt5651_cpu_dai_name),
1069 			"%s", "ssp0-port");
1070 
1071 		byt_rt5651_dais[dai_index].cpu_dai_name =
1072 			byt_rt5651_cpu_dai_name;
1073 	}
1074 
1075 	if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
1076 		priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
1077 		if (IS_ERR(priv->mclk)) {
1078 			ret_val = PTR_ERR(priv->mclk);
1079 			dev_err(&pdev->dev,
1080 				"Failed to get MCLK from pmc_plt_clk_3: %d\n",
1081 				ret_val);
1082 			/*
1083 			 * Fall back to bit clock usage for -ENOENT (clock not
1084 			 * available likely due to missing dependencies), bail
1085 			 * for all other errors, including -EPROBE_DEFER
1086 			 */
1087 			if (ret_val != -ENOENT)
1088 				return ret_val;
1089 			byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
1090 		}
1091 	}
1092 
1093 	if (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED)
1094 		hp_swapped = "-hp-swapped";
1095 	else
1096 		hp_swapped = "";
1097 
1098 	snprintf(byt_rt5651_long_name, sizeof(byt_rt5651_long_name),
1099 		 "bytcr-rt5651-%s-spk-%s-mic%s",
1100 		 (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ?
1101 			"mono" : "stereo",
1102 		 mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)], hp_swapped);
1103 	byt_rt5651_card.long_name = byt_rt5651_long_name;
1104 
1105 	/* override plaform name, if required */
1106 	platform_name = mach->mach_params.platform;
1107 
1108 	ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5651_card,
1109 							platform_name);
1110 	if (ret_val)
1111 		return ret_val;
1112 
1113 	ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5651_card);
1114 
1115 	if (ret_val) {
1116 		dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
1117 			ret_val);
1118 		return ret_val;
1119 	}
1120 	platform_set_drvdata(pdev, &byt_rt5651_card);
1121 	return ret_val;
1122 }
1123 
1124 static struct platform_driver snd_byt_rt5651_mc_driver = {
1125 	.driver = {
1126 		.name = "bytcr_rt5651",
1127 	},
1128 	.probe = snd_byt_rt5651_mc_probe,
1129 };
1130 
1131 module_platform_driver(snd_byt_rt5651_mc_driver);
1132 
1133 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
1134 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1135 MODULE_LICENSE("GPL v2");
1136 MODULE_ALIAS("platform:bytcr_rt5651");
1137