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