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