1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
4  *
5  *  Copyright (C) 2014 Intel Corp
6  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  */
11 
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/platform_device.h>
17 #include <linux/acpi.h>
18 #include <linux/clk.h>
19 #include <linux/device.h>
20 #include <linux/dmi.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/gpio/machine.h>
23 #include <linux/input.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 <dt-bindings/sound/rt5640.h>
31 #include "../../codecs/rt5640.h"
32 #include "../atom/sst-atom-controls.h"
33 #include "../common/soc-intel-quirks.h"
34 
35 enum {
36 	BYT_RT5640_DMIC1_MAP,
37 	BYT_RT5640_DMIC2_MAP,
38 	BYT_RT5640_IN1_MAP,
39 	BYT_RT5640_IN3_MAP,
40 	BYT_RT5640_NO_INTERNAL_MIC_MAP,
41 };
42 
43 #define RT5640_JD_SRC_EXT_GPIO			0x0f
44 
45 enum {
46 	BYT_RT5640_JD_SRC_GPIO1		= (RT5640_JD_SRC_GPIO1 << 4),
47 	BYT_RT5640_JD_SRC_JD1_IN4P	= (RT5640_JD_SRC_JD1_IN4P << 4),
48 	BYT_RT5640_JD_SRC_JD2_IN4N	= (RT5640_JD_SRC_JD2_IN4N << 4),
49 	BYT_RT5640_JD_SRC_GPIO2		= (RT5640_JD_SRC_GPIO2 << 4),
50 	BYT_RT5640_JD_SRC_GPIO3		= (RT5640_JD_SRC_GPIO3 << 4),
51 	BYT_RT5640_JD_SRC_GPIO4		= (RT5640_JD_SRC_GPIO4 << 4),
52 	BYT_RT5640_JD_SRC_EXT_GPIO	= (RT5640_JD_SRC_EXT_GPIO << 4)
53 };
54 
55 enum {
56 	BYT_RT5640_OVCD_TH_600UA	= (6 << 8),
57 	BYT_RT5640_OVCD_TH_1500UA	= (15 << 8),
58 	BYT_RT5640_OVCD_TH_2000UA	= (20 << 8),
59 };
60 
61 enum {
62 	BYT_RT5640_OVCD_SF_0P5		= (RT5640_OVCD_SF_0P5 << 13),
63 	BYT_RT5640_OVCD_SF_0P75		= (RT5640_OVCD_SF_0P75 << 13),
64 	BYT_RT5640_OVCD_SF_1P0		= (RT5640_OVCD_SF_1P0 << 13),
65 	BYT_RT5640_OVCD_SF_1P5		= (RT5640_OVCD_SF_1P5 << 13),
66 };
67 
68 #define BYT_RT5640_MAP(quirk)		((quirk) &  GENMASK(3, 0))
69 #define BYT_RT5640_JDSRC(quirk)		(((quirk) & GENMASK(7, 4)) >> 4)
70 #define BYT_RT5640_OVCD_TH(quirk)	(((quirk) & GENMASK(12, 8)) >> 8)
71 #define BYT_RT5640_OVCD_SF(quirk)	(((quirk) & GENMASK(14, 13)) >> 13)
72 #define BYT_RT5640_JD_NOT_INV		BIT(16)
73 #define BYT_RT5640_MONO_SPEAKER		BIT(17)
74 #define BYT_RT5640_DIFF_MIC		BIT(18) /* default is single-ended */
75 #define BYT_RT5640_SSP2_AIF2		BIT(19) /* default is using AIF1  */
76 #define BYT_RT5640_SSP0_AIF1		BIT(20)
77 #define BYT_RT5640_SSP0_AIF2		BIT(21)
78 #define BYT_RT5640_MCLK_EN		BIT(22)
79 #define BYT_RT5640_MCLK_25MHZ		BIT(23)
80 #define BYT_RT5640_NO_SPEAKERS		BIT(24)
81 #define BYT_RT5640_LINEOUT		BIT(25)
82 #define BYT_RT5640_LINEOUT_AS_HP2	BIT(26)
83 #define BYT_RT5640_HSMIC2_ON_IN1	BIT(27)
84 #define BYT_RT5640_JD_HP_ELITEP_1000G2	BIT(28)
85 #define BYT_RT5640_USE_AMCR0F28		BIT(29)
86 #define BYT_RT5640_SWAPPED_SPEAKERS	BIT(30)
87 
88 #define BYTCR_INPUT_DEFAULTS				\
89 	(BYT_RT5640_IN3_MAP |				\
90 	 BYT_RT5640_JD_SRC_JD1_IN4P |			\
91 	 BYT_RT5640_OVCD_TH_2000UA |			\
92 	 BYT_RT5640_OVCD_SF_0P75 |			\
93 	 BYT_RT5640_DIFF_MIC)
94 
95 /* in-diff or dmic-pin + jdsrc + ovcd-th + -sf + jd-inv + terminating entry */
96 #define MAX_NO_PROPS 6
97 
98 struct byt_rt5640_private {
99 	struct snd_soc_jack jack;
100 	struct snd_soc_jack jack2;
101 	struct rt5640_set_jack_data jack_data;
102 	struct gpio_desc *hsmic_detect;
103 	struct clk *mclk;
104 	struct device *codec_dev;
105 };
106 static bool is_bytcr;
107 
108 static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
109 static int quirk_override = -1;
110 module_param_named(quirk, quirk_override, int, 0444);
111 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
112 
log_quirks(struct device * dev)113 static void log_quirks(struct device *dev)
114 {
115 	int map;
116 	bool has_mclk = false;
117 	bool has_ssp0 = false;
118 	bool has_ssp0_aif1 = false;
119 	bool has_ssp0_aif2 = false;
120 	bool has_ssp2_aif2 = false;
121 
122 	map = BYT_RT5640_MAP(byt_rt5640_quirk);
123 	switch (map) {
124 	case BYT_RT5640_DMIC1_MAP:
125 		dev_info(dev, "quirk DMIC1_MAP enabled\n");
126 		break;
127 	case BYT_RT5640_DMIC2_MAP:
128 		dev_info(dev, "quirk DMIC2_MAP enabled\n");
129 		break;
130 	case BYT_RT5640_IN1_MAP:
131 		dev_info(dev, "quirk IN1_MAP enabled\n");
132 		break;
133 	case BYT_RT5640_IN3_MAP:
134 		dev_info(dev, "quirk IN3_MAP enabled\n");
135 		break;
136 	case BYT_RT5640_NO_INTERNAL_MIC_MAP:
137 		dev_info(dev, "quirk NO_INTERNAL_MIC_MAP enabled\n");
138 		break;
139 	default:
140 		dev_err(dev, "quirk map 0x%x is not supported, microphone input will not work\n", map);
141 		break;
142 	}
143 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
144 		dev_info(dev, "quirk HSMIC2_ON_IN1 enabled\n");
145 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
146 		dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
147 			 BYT_RT5640_JDSRC(byt_rt5640_quirk));
148 		dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
149 			 BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
150 		dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
151 			 BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
152 	}
153 	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
154 		dev_info(dev, "quirk JD_NOT_INV enabled\n");
155 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
156 		dev_info(dev, "quirk JD_HP_ELITEPAD_1000G2 enabled\n");
157 	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
158 		dev_info(dev, "quirk MONO_SPEAKER enabled\n");
159 	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)
160 		dev_info(dev, "quirk NO_SPEAKERS enabled\n");
161 	if (byt_rt5640_quirk & BYT_RT5640_SWAPPED_SPEAKERS)
162 		dev_info(dev, "quirk SWAPPED_SPEAKERS enabled\n");
163 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT)
164 		dev_info(dev, "quirk LINEOUT enabled\n");
165 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
166 		dev_info(dev, "quirk LINEOUT_AS_HP2 enabled\n");
167 	if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
168 		dev_info(dev, "quirk DIFF_MIC enabled\n");
169 	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
170 		dev_info(dev, "quirk SSP0_AIF1 enabled\n");
171 		has_ssp0 = true;
172 		has_ssp0_aif1 = true;
173 	}
174 	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
175 		dev_info(dev, "quirk SSP0_AIF2 enabled\n");
176 		has_ssp0 = true;
177 		has_ssp0_aif2 = true;
178 	}
179 	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
180 		dev_info(dev, "quirk SSP2_AIF2 enabled\n");
181 		has_ssp2_aif2 = true;
182 	}
183 	if (is_bytcr && !has_ssp0)
184 		dev_err(dev, "Invalid routing, bytcr detected but no SSP0-based quirk, audio cannot work with SSP2 on bytcr\n");
185 	if (has_ssp0_aif1 && has_ssp0_aif2)
186 		dev_err(dev, "Invalid routing, SSP0 cannot be connected to both AIF1 and AIF2\n");
187 	if (has_ssp0 && has_ssp2_aif2)
188 		dev_err(dev, "Invalid routing, cannot have both SSP0 and SSP2 connected to codec\n");
189 
190 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
191 		dev_info(dev, "quirk MCLK_EN enabled\n");
192 		has_mclk = true;
193 	}
194 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
195 		if (has_mclk)
196 			dev_info(dev, "quirk MCLK_25MHZ enabled\n");
197 		else
198 			dev_err(dev, "quirk MCLK_25MHZ enabled but quirk MCLK not selected, will be ignored\n");
199 	}
200 }
201 
byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai * codec_dai,int rate)202 static int byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
203 					      int rate)
204 {
205 	int ret;
206 
207 	/* Configure the PLL before selecting it */
208 	if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
209 		/* use bitclock as PLL input */
210 		if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
211 		    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
212 			/* 2x16 bit slots on SSP0 */
213 			ret = snd_soc_dai_set_pll(codec_dai, 0,
214 						  RT5640_PLL1_S_BCLK1,
215 						  rate * 32, rate * 512);
216 		} else {
217 			/* 2x15 bit slots on SSP2 */
218 			ret = snd_soc_dai_set_pll(codec_dai, 0,
219 						  RT5640_PLL1_S_BCLK1,
220 						  rate * 50, rate * 512);
221 		}
222 	} else {
223 		if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
224 			ret = snd_soc_dai_set_pll(codec_dai, 0,
225 						  RT5640_PLL1_S_MCLK,
226 						  25000000, rate * 512);
227 		} else {
228 			ret = snd_soc_dai_set_pll(codec_dai, 0,
229 						  RT5640_PLL1_S_MCLK,
230 						  19200000, rate * 512);
231 		}
232 	}
233 
234 	if (ret < 0) {
235 		dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
236 		return ret;
237 	}
238 
239 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
240 				     rate * 512, SND_SOC_CLOCK_IN);
241 	if (ret < 0) {
242 		dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
243 		return ret;
244 	}
245 
246 	return 0;
247 }
248 
249 #define BYT_CODEC_DAI1	"rt5640-aif1"
250 #define BYT_CODEC_DAI2	"rt5640-aif2"
251 
byt_rt5640_get_codec_dai(struct snd_soc_dapm_context * dapm)252 static struct snd_soc_dai *byt_rt5640_get_codec_dai(struct snd_soc_dapm_context *dapm)
253 {
254 	struct snd_soc_card *card = dapm->card;
255 	struct snd_soc_dai *codec_dai;
256 
257 	codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
258 	if (!codec_dai)
259 		codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
260 	if (!codec_dai)
261 		dev_err(card->dev, "Error codec dai not found\n");
262 
263 	return codec_dai;
264 }
265 
platform_clock_control(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)266 static int platform_clock_control(struct snd_soc_dapm_widget *w,
267 				  struct snd_kcontrol *k, int  event)
268 {
269 	struct snd_soc_dapm_context *dapm = w->dapm;
270 	struct snd_soc_card *card = dapm->card;
271 	struct snd_soc_dai *codec_dai;
272 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
273 	int ret;
274 
275 	codec_dai = byt_rt5640_get_codec_dai(dapm);
276 	if (!codec_dai)
277 		return -EIO;
278 
279 	if (SND_SOC_DAPM_EVENT_ON(event)) {
280 		ret = clk_prepare_enable(priv->mclk);
281 		if (ret < 0) {
282 			dev_err(card->dev, "could not configure MCLK state\n");
283 			return ret;
284 		}
285 		ret = byt_rt5640_prepare_and_enable_pll1(codec_dai, 48000);
286 	} else {
287 		/*
288 		 * Set codec clock source to internal clock before
289 		 * turning off the platform clock. Codec needs clock
290 		 * for Jack detection and button press
291 		 */
292 		ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
293 					     48000 * 512,
294 					     SND_SOC_CLOCK_IN);
295 		if (!ret)
296 			clk_disable_unprepare(priv->mclk);
297 	}
298 
299 	if (ret < 0) {
300 		dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
301 		return ret;
302 	}
303 
304 	return 0;
305 }
306 
byt_rt5640_event_lineout(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)307 static int byt_rt5640_event_lineout(struct snd_soc_dapm_widget *w,
308 			struct snd_kcontrol *k, int event)
309 {
310 	unsigned int gpio_ctrl3_val = RT5640_GP1_PF_OUT;
311 	struct snd_soc_dai *codec_dai;
312 
313 	if (!(byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2))
314 		return 0;
315 
316 	/*
317 	 * On devices which use line-out as a second headphones output,
318 	 * the codec's GPIO1 pin is used to enable an external HP-amp.
319 	 */
320 
321 	codec_dai = byt_rt5640_get_codec_dai(w->dapm);
322 	if (!codec_dai)
323 		return -EIO;
324 
325 	if (SND_SOC_DAPM_EVENT_ON(event))
326 		gpio_ctrl3_val |= RT5640_GP1_OUT_HI;
327 
328 	snd_soc_component_update_bits(codec_dai->component, RT5640_GPIO_CTRL3,
329 		RT5640_GP1_PF_MASK | RT5640_GP1_OUT_MASK, gpio_ctrl3_val);
330 
331 	return 0;
332 }
333 
334 static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
335 	SND_SOC_DAPM_HP("Headphone", NULL),
336 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
337 	SND_SOC_DAPM_MIC("Headset Mic 2", NULL),
338 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
339 	SND_SOC_DAPM_SPK("Speaker", NULL),
340 	SND_SOC_DAPM_LINE("Line Out", byt_rt5640_event_lineout),
341 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
342 			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
343 			    SND_SOC_DAPM_POST_PMD),
344 };
345 
346 static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
347 	{"Headphone", NULL, "Platform Clock"},
348 	{"Headset Mic", NULL, "Platform Clock"},
349 	{"Headset Mic", NULL, "MICBIAS1"},
350 	{"IN2P", NULL, "Headset Mic"},
351 	{"Headphone", NULL, "HPOL"},
352 	{"Headphone", NULL, "HPOR"},
353 };
354 
355 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
356 	{"Internal Mic", NULL, "Platform Clock"},
357 	{"DMIC1", NULL, "Internal Mic"},
358 };
359 
360 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
361 	{"Internal Mic", NULL, "Platform Clock"},
362 	{"DMIC2", NULL, "Internal Mic"},
363 };
364 
365 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
366 	{"Internal Mic", NULL, "Platform Clock"},
367 	{"Internal Mic", NULL, "MICBIAS1"},
368 	{"IN1P", NULL, "Internal Mic"},
369 };
370 
371 static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
372 	{"Internal Mic", NULL, "Platform Clock"},
373 	{"Internal Mic", NULL, "MICBIAS1"},
374 	{"IN3P", NULL, "Internal Mic"},
375 };
376 
377 static const struct snd_soc_dapm_route byt_rt5640_hsmic2_in1_map[] = {
378 	{"Headset Mic 2", NULL, "Platform Clock"},
379 	{"Headset Mic 2", NULL, "MICBIAS1"},
380 	{"IN1P", NULL, "Headset Mic 2"},
381 };
382 
383 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
384 	{"ssp2 Tx", NULL, "codec_out0"},
385 	{"ssp2 Tx", NULL, "codec_out1"},
386 	{"codec_in0", NULL, "ssp2 Rx"},
387 	{"codec_in1", NULL, "ssp2 Rx"},
388 
389 	{"AIF1 Playback", NULL, "ssp2 Tx"},
390 	{"ssp2 Rx", NULL, "AIF1 Capture"},
391 };
392 
393 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
394 	{"ssp2 Tx", NULL, "codec_out0"},
395 	{"ssp2 Tx", NULL, "codec_out1"},
396 	{"codec_in0", NULL, "ssp2 Rx"},
397 	{"codec_in1", NULL, "ssp2 Rx"},
398 
399 	{"AIF2 Playback", NULL, "ssp2 Tx"},
400 	{"ssp2 Rx", NULL, "AIF2 Capture"},
401 };
402 
403 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
404 	{"ssp0 Tx", NULL, "modem_out"},
405 	{"modem_in", NULL, "ssp0 Rx"},
406 
407 	{"AIF1 Playback", NULL, "ssp0 Tx"},
408 	{"ssp0 Rx", NULL, "AIF1 Capture"},
409 };
410 
411 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
412 	{"ssp0 Tx", NULL, "modem_out"},
413 	{"modem_in", NULL, "ssp0 Rx"},
414 
415 	{"AIF2 Playback", NULL, "ssp0 Tx"},
416 	{"ssp0 Rx", NULL, "AIF2 Capture"},
417 };
418 
419 static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
420 	{"Speaker", NULL, "Platform Clock"},
421 	{"Speaker", NULL, "SPOLP"},
422 	{"Speaker", NULL, "SPOLN"},
423 	{"Speaker", NULL, "SPORP"},
424 	{"Speaker", NULL, "SPORN"},
425 };
426 
427 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
428 	{"Speaker", NULL, "Platform Clock"},
429 	{"Speaker", NULL, "SPOLP"},
430 	{"Speaker", NULL, "SPOLN"},
431 };
432 
433 static const struct snd_soc_dapm_route byt_rt5640_lineout_map[] = {
434 	{"Line Out", NULL, "Platform Clock"},
435 	{"Line Out", NULL, "LOUTR"},
436 	{"Line Out", NULL, "LOUTL"},
437 };
438 
439 static const struct snd_kcontrol_new byt_rt5640_controls[] = {
440 	SOC_DAPM_PIN_SWITCH("Headphone"),
441 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
442 	SOC_DAPM_PIN_SWITCH("Headset Mic 2"),
443 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
444 	SOC_DAPM_PIN_SWITCH("Speaker"),
445 	SOC_DAPM_PIN_SWITCH("Line Out"),
446 };
447 
448 static struct snd_soc_jack_pin rt5640_pins[] = {
449 	{
450 		.pin	= "Headphone",
451 		.mask	= SND_JACK_HEADPHONE,
452 	},
453 	{
454 		.pin	= "Headset Mic",
455 		.mask	= SND_JACK_MICROPHONE,
456 	},
457 };
458 
459 static struct snd_soc_jack_pin rt5640_pins2[] = {
460 	{
461 		/* The 2nd headset jack uses lineout with an external HP-amp */
462 		.pin	= "Line Out",
463 		.mask	= SND_JACK_HEADPHONE,
464 	},
465 	{
466 		.pin	= "Headset Mic 2",
467 		.mask	= SND_JACK_MICROPHONE,
468 	},
469 };
470 
471 static struct snd_soc_jack_gpio rt5640_jack_gpio = {
472 	.name = "hp-detect",
473 	.report = SND_JACK_HEADSET,
474 	.invert = true,
475 	.debounce_time = 200,
476 };
477 
478 static struct snd_soc_jack_gpio rt5640_jack2_gpio = {
479 	.name = "hp2-detect",
480 	.report = SND_JACK_HEADSET,
481 	.invert = true,
482 	.debounce_time = 200,
483 };
484 
485 static const struct acpi_gpio_params acpi_gpio0 = { 0, 0, false };
486 static const struct acpi_gpio_params acpi_gpio1 = { 1, 0, false };
487 static const struct acpi_gpio_params acpi_gpio2 = { 2, 0, false };
488 
489 static const struct acpi_gpio_mapping byt_rt5640_hp_elitepad_1000g2_gpios[] = {
490 	{ "hp-detect-gpios", &acpi_gpio0, 1, },
491 	{ "headset-mic-detect-gpios", &acpi_gpio1, 1, },
492 	{ "hp2-detect-gpios", &acpi_gpio2, 1, },
493 	{ },
494 };
495 
byt_rt5640_hp_elitepad_1000g2_jack1_check(void * data)496 static int byt_rt5640_hp_elitepad_1000g2_jack1_check(void *data)
497 {
498 	struct byt_rt5640_private *priv = data;
499 	int jack_status, mic_status;
500 
501 	jack_status = gpiod_get_value_cansleep(rt5640_jack_gpio.desc);
502 	if (jack_status)
503 		return 0;
504 
505 	mic_status = gpiod_get_value_cansleep(priv->hsmic_detect);
506 	if (mic_status)
507 		return SND_JACK_HEADPHONE;
508 	else
509 		return SND_JACK_HEADSET;
510 }
511 
byt_rt5640_hp_elitepad_1000g2_jack2_check(void * data)512 static int byt_rt5640_hp_elitepad_1000g2_jack2_check(void *data)
513 {
514 	struct snd_soc_component *component = data;
515 	int jack_status, report;
516 
517 	jack_status = gpiod_get_value_cansleep(rt5640_jack2_gpio.desc);
518 	if (jack_status)
519 		return 0;
520 
521 	rt5640_enable_micbias1_for_ovcd(component);
522 	report = rt5640_detect_headset(component, rt5640_jack2_gpio.desc);
523 	rt5640_disable_micbias1_for_ovcd(component);
524 
525 	return report;
526 }
527 
byt_rt5640_aif1_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)528 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
529 					struct snd_pcm_hw_params *params)
530 {
531 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
532 	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
533 
534 	return byt_rt5640_prepare_and_enable_pll1(dai, params_rate(params));
535 }
536 
537 /* Please keep this list alphabetically sorted */
538 static const struct dmi_system_id byt_rt5640_quirk_table[] = {
539 	{	/* Acer Iconia One 7 B1-750 */
540 		.matches = {
541 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
542 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "VESPA2"),
543 		},
544 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
545 					BYT_RT5640_JD_SRC_JD1_IN4P |
546 					BYT_RT5640_OVCD_TH_1500UA |
547 					BYT_RT5640_OVCD_SF_0P75 |
548 					BYT_RT5640_SSP0_AIF1 |
549 					BYT_RT5640_MCLK_EN),
550 	},
551 	{	/* Acer Iconia Tab 8 W1-810 */
552 		.matches = {
553 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
554 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
555 		},
556 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
557 					BYT_RT5640_JD_SRC_JD1_IN4P |
558 					BYT_RT5640_OVCD_TH_1500UA |
559 					BYT_RT5640_OVCD_SF_0P75 |
560 					BYT_RT5640_SSP0_AIF1 |
561 					BYT_RT5640_MCLK_EN),
562 	},
563 	{	/* Acer One 10 S1002 */
564 		.matches = {
565 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
566 			DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
567 		},
568 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
569 					BYT_RT5640_JD_SRC_JD2_IN4N |
570 					BYT_RT5640_OVCD_TH_2000UA |
571 					BYT_RT5640_OVCD_SF_0P75 |
572 					BYT_RT5640_DIFF_MIC |
573 					BYT_RT5640_SSP0_AIF2 |
574 					BYT_RT5640_MCLK_EN),
575 	},
576 	{
577 		.matches = {
578 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
579 			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
580 		},
581 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
582 					BYT_RT5640_JD_SRC_JD2_IN4N |
583 					BYT_RT5640_OVCD_TH_2000UA |
584 					BYT_RT5640_OVCD_SF_0P75 |
585 					BYT_RT5640_SSP0_AIF1 |
586 					BYT_RT5640_MCLK_EN),
587 	},
588 	{
589 		/* Advantech MICA-071 */
590 		.matches = {
591 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Advantech"),
592 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MICA-071"),
593 		},
594 		/* OVCD Th = 1500uA to reliable detect head-phones vs -set */
595 		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
596 					BYT_RT5640_JD_SRC_JD2_IN4N |
597 					BYT_RT5640_OVCD_TH_1500UA |
598 					BYT_RT5640_OVCD_SF_0P75 |
599 					BYT_RT5640_MONO_SPEAKER |
600 					BYT_RT5640_DIFF_MIC |
601 					BYT_RT5640_MCLK_EN),
602 	},
603 	{
604 		.matches = {
605 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
606 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
607 		},
608 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
609 					BYT_RT5640_MONO_SPEAKER |
610 					BYT_RT5640_SSP0_AIF1 |
611 					BYT_RT5640_MCLK_EN),
612 	},
613 	{
614 		.matches = {
615 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
616 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 101 CESIUM"),
617 		},
618 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
619 					BYT_RT5640_JD_NOT_INV |
620 					BYT_RT5640_DIFF_MIC |
621 					BYT_RT5640_SSP0_AIF1 |
622 					BYT_RT5640_MCLK_EN),
623 	},
624 	{
625 		.matches = {
626 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
627 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
628 		},
629 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
630 					BYT_RT5640_JD_SRC_JD2_IN4N |
631 					BYT_RT5640_OVCD_TH_2000UA |
632 					BYT_RT5640_OVCD_SF_0P75 |
633 					BYT_RT5640_SSP0_AIF1 |
634 					BYT_RT5640_MCLK_EN),
635 	},
636 	{
637 		.matches = {
638 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
639 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ME176C"),
640 		},
641 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
642 					BYT_RT5640_JD_SRC_JD2_IN4N |
643 					BYT_RT5640_OVCD_TH_2000UA |
644 					BYT_RT5640_OVCD_SF_0P75 |
645 					BYT_RT5640_SSP0_AIF1 |
646 					BYT_RT5640_MCLK_EN |
647 					BYT_RT5640_USE_AMCR0F28),
648 	},
649 	{
650 		/* Asus T100TAF, unlike other T100TA* models this one has a mono speaker */
651 		.matches = {
652 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
653 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
654 		},
655 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
656 					BYT_RT5640_JD_SRC_JD2_IN4N |
657 					BYT_RT5640_OVCD_TH_2000UA |
658 					BYT_RT5640_OVCD_SF_0P75 |
659 					BYT_RT5640_MONO_SPEAKER |
660 					BYT_RT5640_DIFF_MIC |
661 					BYT_RT5640_SSP0_AIF2 |
662 					BYT_RT5640_MCLK_EN),
663 	},
664 	{
665 		/* Asus T100TA and T100TAM, must come after T100TAF (mono spk) match */
666 		.matches = {
667 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
668 			DMI_MATCH(DMI_PRODUCT_NAME, "T100TA"),
669 		},
670 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
671 					BYT_RT5640_JD_SRC_JD2_IN4N |
672 					BYT_RT5640_OVCD_TH_2000UA |
673 					BYT_RT5640_OVCD_SF_0P75 |
674 					BYT_RT5640_MCLK_EN),
675 	},
676 	{
677 		.matches = {
678 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
679 			DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
680 		},
681 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
682 					BYT_RT5640_JD_SRC_EXT_GPIO |
683 					BYT_RT5640_OVCD_TH_2000UA |
684 					BYT_RT5640_OVCD_SF_0P75 |
685 					BYT_RT5640_SSP0_AIF1 |
686 					BYT_RT5640_MCLK_EN |
687 					BYT_RT5640_USE_AMCR0F28),
688 	},
689 	{	/* Chuwi Vi8 (CWI506) */
690 		.matches = {
691 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
692 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
693 			/* The above are too generic, also match BIOS info */
694 			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
695 		},
696 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
697 					BYT_RT5640_MONO_SPEAKER |
698 					BYT_RT5640_SSP0_AIF1 |
699 					BYT_RT5640_MCLK_EN),
700 	},
701 	{	/* Chuwi Vi8 dual-boot (CWI506) */
702 		.matches = {
703 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
704 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
705 			/* The above are too generic, also match BIOS info */
706 			DMI_MATCH(DMI_BIOS_VERSION, "CHUWI2.D86JHBNR02"),
707 		},
708 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
709 					BYT_RT5640_MONO_SPEAKER |
710 					BYT_RT5640_SSP0_AIF1 |
711 					BYT_RT5640_MCLK_EN),
712 	},
713 	{
714 		/* Chuwi Vi10 (CWI505) */
715 		.matches = {
716 			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
717 			DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
718 			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
719 			DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
720 		},
721 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
722 					BYT_RT5640_JD_SRC_JD2_IN4N |
723 					BYT_RT5640_OVCD_TH_2000UA |
724 					BYT_RT5640_OVCD_SF_0P75 |
725 					BYT_RT5640_DIFF_MIC |
726 					BYT_RT5640_SSP0_AIF1 |
727 					BYT_RT5640_MCLK_EN),
728 	},
729 	{
730 		/* Chuwi Hi8 (CWI509) */
731 		.matches = {
732 			DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
733 			DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
734 			DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
735 			DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
736 		},
737 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
738 					BYT_RT5640_JD_SRC_JD2_IN4N |
739 					BYT_RT5640_OVCD_TH_2000UA |
740 					BYT_RT5640_OVCD_SF_0P75 |
741 					BYT_RT5640_MONO_SPEAKER |
742 					BYT_RT5640_DIFF_MIC |
743 					BYT_RT5640_SSP0_AIF1 |
744 					BYT_RT5640_MCLK_EN),
745 	},
746 	{
747 		.matches = {
748 			DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
749 			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
750 		},
751 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
752 	},
753 	{	/* Connect Tablet 9 */
754 		.matches = {
755 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
756 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
757 		},
758 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
759 					BYT_RT5640_MONO_SPEAKER |
760 					BYT_RT5640_SSP0_AIF1 |
761 					BYT_RT5640_MCLK_EN),
762 	},
763 	{
764 		.matches = {
765 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
766 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
767 		},
768 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
769 					BYT_RT5640_JD_SRC_JD2_IN4N |
770 					BYT_RT5640_OVCD_TH_2000UA |
771 					BYT_RT5640_OVCD_SF_0P75 |
772 					BYT_RT5640_MONO_SPEAKER |
773 					BYT_RT5640_MCLK_EN),
774 	},
775 	{	/* Estar Beauty HD MID 7316R */
776 		.matches = {
777 			DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
778 			DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
779 		},
780 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
781 					BYT_RT5640_MONO_SPEAKER |
782 					BYT_RT5640_SSP0_AIF1 |
783 					BYT_RT5640_MCLK_EN),
784 	},
785 	{	/* Glavey TM800A550L */
786 		.matches = {
787 			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
788 			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
789 			/* Above strings are too generic, also match on BIOS version */
790 			DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
791 		},
792 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
793 					BYT_RT5640_SSP0_AIF1 |
794 					BYT_RT5640_MCLK_EN),
795 	},
796 	{
797 		.matches = {
798 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
799 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
800 		},
801 		.driver_data = (void *)(BYT_RT5640_DMIC2_MAP |
802 					BYT_RT5640_MCLK_EN |
803 					BYT_RT5640_LINEOUT |
804 					BYT_RT5640_LINEOUT_AS_HP2 |
805 					BYT_RT5640_HSMIC2_ON_IN1 |
806 					BYT_RT5640_JD_HP_ELITEP_1000G2),
807 	},
808 	{	/* HP Pavilion x2 10-k0XX, 10-n0XX */
809 		.matches = {
810 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
811 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
812 		},
813 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
814 					BYT_RT5640_JD_SRC_JD2_IN4N |
815 					BYT_RT5640_OVCD_TH_1500UA |
816 					BYT_RT5640_OVCD_SF_0P75 |
817 					BYT_RT5640_SSP0_AIF1 |
818 					BYT_RT5640_MCLK_EN),
819 	},
820 	{	/* HP Pavilion x2 10-p0XX */
821 		.matches = {
822 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
823 			DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
824 		},
825 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
826 					BYT_RT5640_JD_SRC_JD1_IN4P |
827 					BYT_RT5640_OVCD_TH_2000UA |
828 					BYT_RT5640_OVCD_SF_0P75 |
829 					BYT_RT5640_MCLK_EN),
830 	},
831 	{	/* HP Pro Tablet 408 */
832 		.matches = {
833 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
834 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Pro Tablet 408"),
835 		},
836 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
837 					BYT_RT5640_JD_SRC_JD2_IN4N |
838 					BYT_RT5640_OVCD_TH_1500UA |
839 					BYT_RT5640_OVCD_SF_0P75 |
840 					BYT_RT5640_SSP0_AIF1 |
841 					BYT_RT5640_MCLK_EN),
842 	},
843 	{	/* HP Stream 7 */
844 		.matches = {
845 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
846 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
847 		},
848 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
849 					BYT_RT5640_MONO_SPEAKER |
850 					BYT_RT5640_JD_NOT_INV |
851 					BYT_RT5640_SSP0_AIF1 |
852 					BYT_RT5640_MCLK_EN),
853 	},
854 	{	/* HP Stream 8 */
855 		.matches = {
856 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
857 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 8 Tablet"),
858 		},
859 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
860 					BYT_RT5640_JD_NOT_INV |
861 					BYT_RT5640_SSP0_AIF1 |
862 					BYT_RT5640_MCLK_EN),
863 	},
864 	{	/* I.T.Works TW891 */
865 		.matches = {
866 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
867 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
868 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
869 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
870 		},
871 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
872 					BYT_RT5640_MONO_SPEAKER |
873 					BYT_RT5640_SSP0_AIF1 |
874 					BYT_RT5640_MCLK_EN),
875 	},
876 	{	/* Lamina I8270 / T701BR.SE */
877 		.matches = {
878 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
879 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
880 		},
881 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
882 					BYT_RT5640_MONO_SPEAKER |
883 					BYT_RT5640_JD_NOT_INV |
884 					BYT_RT5640_SSP0_AIF1 |
885 					BYT_RT5640_MCLK_EN),
886 	},
887 	{	/* Lenovo Miix 2 8 */
888 		.matches = {
889 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
890 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
891 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
892 		},
893 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
894 					BYT_RT5640_JD_SRC_JD2_IN4N |
895 					BYT_RT5640_OVCD_TH_2000UA |
896 					BYT_RT5640_OVCD_SF_0P75 |
897 					BYT_RT5640_MONO_SPEAKER |
898 					BYT_RT5640_MCLK_EN),
899 	},
900 	{	/* Lenovo Miix 3-830 */
901 		.matches = {
902 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
903 			DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
904 		},
905 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
906 					BYT_RT5640_JD_SRC_JD2_IN4N |
907 					BYT_RT5640_OVCD_TH_2000UA |
908 					BYT_RT5640_OVCD_SF_0P75 |
909 					BYT_RT5640_MONO_SPEAKER |
910 					BYT_RT5640_DIFF_MIC |
911 					BYT_RT5640_SSP0_AIF1 |
912 					BYT_RT5640_MCLK_EN),
913 	},
914 	{	/* Linx Linx7 tablet */
915 		.matches = {
916 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
917 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
918 		},
919 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
920 					BYT_RT5640_MONO_SPEAKER |
921 					BYT_RT5640_JD_NOT_INV |
922 					BYT_RT5640_SSP0_AIF1 |
923 					BYT_RT5640_MCLK_EN),
924 	},
925 	{
926 		/* Medion Lifetab S10346 */
927 		.matches = {
928 			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
929 			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
930 			/* Above strings are much too generic, also match on BIOS date */
931 			DMI_MATCH(DMI_BIOS_DATE, "10/22/2015"),
932 		},
933 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
934 					BYT_RT5640_SWAPPED_SPEAKERS |
935 					BYT_RT5640_SSP0_AIF1 |
936 					BYT_RT5640_MCLK_EN),
937 	},
938 	{	/* Mele PCG03 Mini PC */
939 		.matches = {
940 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
941 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
942 		},
943 		.driver_data = (void *)(BYT_RT5640_NO_INTERNAL_MIC_MAP |
944 					BYT_RT5640_NO_SPEAKERS |
945 					BYT_RT5640_SSP0_AIF1),
946 	},
947 	{	/* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
948 		.matches = {
949 			DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
950 			DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
951 		},
952 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
953 					BYT_RT5640_MONO_SPEAKER |
954 					BYT_RT5640_SSP0_AIF1 |
955 					BYT_RT5640_MCLK_EN),
956 	},
957 	{
958 		/* MPMAN MPWIN895CL */
959 		.matches = {
960 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
961 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
962 		},
963 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
964 					BYT_RT5640_MONO_SPEAKER |
965 					BYT_RT5640_SSP0_AIF1 |
966 					BYT_RT5640_MCLK_EN),
967 	},
968 	{	/* MSI S100 tablet */
969 		.matches = {
970 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
971 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
972 		},
973 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
974 					BYT_RT5640_JD_SRC_JD2_IN4N |
975 					BYT_RT5640_OVCD_TH_2000UA |
976 					BYT_RT5640_OVCD_SF_0P75 |
977 					BYT_RT5640_MONO_SPEAKER |
978 					BYT_RT5640_DIFF_MIC |
979 					BYT_RT5640_MCLK_EN),
980 	},
981 	{	/* Nuvison/TMax TM800W560 */
982 		.matches = {
983 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
984 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
985 		},
986 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
987 					BYT_RT5640_JD_SRC_JD2_IN4N |
988 					BYT_RT5640_OVCD_TH_2000UA |
989 					BYT_RT5640_OVCD_SF_0P75 |
990 					BYT_RT5640_JD_NOT_INV |
991 					BYT_RT5640_DIFF_MIC |
992 					BYT_RT5640_SSP0_AIF1 |
993 					BYT_RT5640_MCLK_EN),
994 	},
995 	{	/* Onda v975w */
996 		.matches = {
997 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
998 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
999 			/* The above are too generic, also match BIOS info */
1000 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
1001 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
1002 		},
1003 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1004 					BYT_RT5640_JD_SRC_JD2_IN4N |
1005 					BYT_RT5640_OVCD_TH_2000UA |
1006 					BYT_RT5640_OVCD_SF_0P75 |
1007 					BYT_RT5640_DIFF_MIC |
1008 					BYT_RT5640_MCLK_EN),
1009 	},
1010 	{	/* Pipo W4 */
1011 		.matches = {
1012 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1013 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1014 			/* The above are too generic, also match BIOS info */
1015 			DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
1016 		},
1017 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1018 					BYT_RT5640_MONO_SPEAKER |
1019 					BYT_RT5640_SSP0_AIF1 |
1020 					BYT_RT5640_MCLK_EN),
1021 	},
1022 	{	/* Point of View Mobii TAB-P800W (V2.0) */
1023 		.matches = {
1024 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1025 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1026 			/* The above are too generic, also match BIOS info */
1027 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
1028 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
1029 		},
1030 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1031 					BYT_RT5640_JD_SRC_JD2_IN4N |
1032 					BYT_RT5640_OVCD_TH_2000UA |
1033 					BYT_RT5640_OVCD_SF_0P75 |
1034 					BYT_RT5640_MONO_SPEAKER |
1035 					BYT_RT5640_DIFF_MIC |
1036 					BYT_RT5640_SSP0_AIF2 |
1037 					BYT_RT5640_MCLK_EN),
1038 	},
1039 	{	/* Point of View Mobii TAB-P800W (V2.1) */
1040 		.matches = {
1041 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1042 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1043 			/* The above are too generic, also match BIOS info */
1044 			DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1045 			DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1046 		},
1047 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1048 					BYT_RT5640_JD_SRC_JD2_IN4N |
1049 					BYT_RT5640_OVCD_TH_2000UA |
1050 					BYT_RT5640_OVCD_SF_0P75 |
1051 					BYT_RT5640_MONO_SPEAKER |
1052 					BYT_RT5640_DIFF_MIC |
1053 					BYT_RT5640_SSP0_AIF2 |
1054 					BYT_RT5640_MCLK_EN),
1055 	},
1056 	{	/* Point of View Mobii TAB-P1005W-232 (V2.0) */
1057 		.matches = {
1058 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "POV"),
1059 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "I102A"),
1060 		},
1061 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1062 					BYT_RT5640_JD_SRC_JD2_IN4N |
1063 					BYT_RT5640_OVCD_TH_2000UA |
1064 					BYT_RT5640_OVCD_SF_0P75 |
1065 					BYT_RT5640_DIFF_MIC |
1066 					BYT_RT5640_SSP0_AIF1 |
1067 					BYT_RT5640_MCLK_EN),
1068 	},
1069 	{
1070 		/* Prowise PT301 */
1071 		.matches = {
1072 			DMI_MATCH(DMI_SYS_VENDOR, "Prowise"),
1073 			DMI_MATCH(DMI_PRODUCT_NAME, "PT301"),
1074 		},
1075 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1076 					BYT_RT5640_JD_SRC_JD2_IN4N |
1077 					BYT_RT5640_OVCD_TH_2000UA |
1078 					BYT_RT5640_OVCD_SF_0P75 |
1079 					BYT_RT5640_DIFF_MIC |
1080 					BYT_RT5640_SSP0_AIF1 |
1081 					BYT_RT5640_MCLK_EN),
1082 	},
1083 	{
1084 		/* Teclast X89 */
1085 		.matches = {
1086 			DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
1087 			DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
1088 		},
1089 		.driver_data = (void *)(BYT_RT5640_IN3_MAP |
1090 					BYT_RT5640_JD_SRC_JD1_IN4P |
1091 					BYT_RT5640_OVCD_TH_2000UA |
1092 					BYT_RT5640_OVCD_SF_1P0 |
1093 					BYT_RT5640_SSP0_AIF1 |
1094 					BYT_RT5640_MCLK_EN),
1095 	},
1096 	{	/* Toshiba Satellite Click Mini L9W-B */
1097 		.matches = {
1098 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1099 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
1100 		},
1101 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1102 					BYT_RT5640_JD_SRC_JD2_IN4N |
1103 					BYT_RT5640_OVCD_TH_1500UA |
1104 					BYT_RT5640_OVCD_SF_0P75 |
1105 					BYT_RT5640_SSP0_AIF1 |
1106 					BYT_RT5640_MCLK_EN),
1107 	},
1108 	{	/* Toshiba Encore WT8-A */
1109 		.matches = {
1110 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1111 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
1112 		},
1113 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1114 					BYT_RT5640_JD_SRC_JD2_IN4N |
1115 					BYT_RT5640_OVCD_TH_2000UA |
1116 					BYT_RT5640_OVCD_SF_0P75 |
1117 					BYT_RT5640_JD_NOT_INV |
1118 					BYT_RT5640_MCLK_EN),
1119 	},
1120 	{	/* Toshiba Encore WT10-A */
1121 		.matches = {
1122 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1123 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
1124 		},
1125 		.driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1126 					BYT_RT5640_JD_SRC_JD1_IN4P |
1127 					BYT_RT5640_OVCD_TH_2000UA |
1128 					BYT_RT5640_OVCD_SF_0P75 |
1129 					BYT_RT5640_SSP0_AIF2 |
1130 					BYT_RT5640_MCLK_EN),
1131 	},
1132 	{	/* Voyo Winpad A15 */
1133 		.matches = {
1134 			DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1135 			DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1136 			/* Above strings are too generic, also match on BIOS date */
1137 			DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
1138 		},
1139 		.driver_data = (void *)(BYT_RT5640_IN1_MAP |
1140 					BYT_RT5640_JD_SRC_JD2_IN4N |
1141 					BYT_RT5640_OVCD_TH_2000UA |
1142 					BYT_RT5640_OVCD_SF_0P75 |
1143 					BYT_RT5640_DIFF_MIC |
1144 					BYT_RT5640_MCLK_EN),
1145 	},
1146 	{	/* Catch-all for generic Insyde tablets, must be last */
1147 		.matches = {
1148 			DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
1149 		},
1150 		.driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1151 					BYT_RT5640_MCLK_EN |
1152 					BYT_RT5640_SSP0_AIF1),
1153 
1154 	},
1155 	{}
1156 };
1157 
1158 /*
1159  * Note this MUST be called before snd_soc_register_card(), so that the props
1160  * are in place before the codec component driver's probe function parses them.
1161  */
byt_rt5640_add_codec_device_props(struct device * i2c_dev,struct byt_rt5640_private * priv)1162 static int byt_rt5640_add_codec_device_props(struct device *i2c_dev,
1163 					     struct byt_rt5640_private *priv)
1164 {
1165 	struct property_entry props[MAX_NO_PROPS] = {};
1166 	struct fwnode_handle *fwnode;
1167 	int cnt = 0;
1168 	int ret;
1169 
1170 	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1171 	case BYT_RT5640_DMIC1_MAP:
1172 		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
1173 						  RT5640_DMIC1_DATA_PIN_IN1P);
1174 		break;
1175 	case BYT_RT5640_DMIC2_MAP:
1176 		props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
1177 						  RT5640_DMIC2_DATA_PIN_IN1N);
1178 		break;
1179 	case BYT_RT5640_IN1_MAP:
1180 		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1181 			props[cnt++] =
1182 				PROPERTY_ENTRY_BOOL("realtek,in1-differential");
1183 		break;
1184 	case BYT_RT5640_IN3_MAP:
1185 		if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1186 			props[cnt++] =
1187 				PROPERTY_ENTRY_BOOL("realtek,in3-differential");
1188 		break;
1189 	}
1190 
1191 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1192 		if (BYT_RT5640_JDSRC(byt_rt5640_quirk) != RT5640_JD_SRC_EXT_GPIO) {
1193 			props[cnt++] = PROPERTY_ENTRY_U32(
1194 					    "realtek,jack-detect-source",
1195 					    BYT_RT5640_JDSRC(byt_rt5640_quirk));
1196 		}
1197 
1198 		props[cnt++] = PROPERTY_ENTRY_U32(
1199 				    "realtek,over-current-threshold-microamp",
1200 				    BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
1201 
1202 		props[cnt++] = PROPERTY_ENTRY_U32(
1203 				    "realtek,over-current-scale-factor",
1204 				    BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
1205 	}
1206 
1207 	if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
1208 		props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
1209 
1210 	fwnode = fwnode_create_software_node(props, NULL);
1211 	if (IS_ERR(fwnode)) {
1212 		/* put_device() is handled in caller */
1213 		return PTR_ERR(fwnode);
1214 	}
1215 
1216 	ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
1217 
1218 	fwnode_handle_put(fwnode);
1219 
1220 	return ret;
1221 }
1222 
1223 /* Some Android devs specify IRQs/GPIOS in a special AMCR0F28 ACPI device */
1224 static const struct acpi_gpio_params amcr0f28_jd_gpio = { 1, 0, false };
1225 
1226 static const struct acpi_gpio_mapping amcr0f28_gpios[] = {
1227 	{ "rt5640-jd-gpios", &amcr0f28_jd_gpio, 1 },
1228 	{ }
1229 };
1230 
byt_rt5640_get_amcr0f28_settings(struct snd_soc_card * card)1231 static int byt_rt5640_get_amcr0f28_settings(struct snd_soc_card *card)
1232 {
1233 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1234 	struct rt5640_set_jack_data *data = &priv->jack_data;
1235 	struct acpi_device *adev;
1236 	int ret = 0;
1237 
1238 	adev = acpi_dev_get_first_match_dev("AMCR0F28", "1", -1);
1239 	if (!adev) {
1240 		dev_err(card->dev, "error cannot find AMCR0F28 adev\n");
1241 		return -ENOENT;
1242 	}
1243 
1244 	data->codec_irq_override = acpi_dev_gpio_irq_get(adev, 0);
1245 	if (data->codec_irq_override < 0) {
1246 		ret = data->codec_irq_override;
1247 		dev_err(card->dev, "error %d getting codec IRQ\n", ret);
1248 		goto put_adev;
1249 	}
1250 
1251 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk) == RT5640_JD_SRC_EXT_GPIO) {
1252 		acpi_dev_add_driver_gpios(adev, amcr0f28_gpios);
1253 		data->jd_gpio = devm_fwnode_gpiod_get(card->dev, acpi_fwnode_handle(adev),
1254 						      "rt5640-jd", GPIOD_IN, "rt5640-jd");
1255 		acpi_dev_remove_driver_gpios(adev);
1256 
1257 		if (IS_ERR(data->jd_gpio)) {
1258 			ret = PTR_ERR(data->jd_gpio);
1259 			dev_err(card->dev, "error %d getting jd GPIO\n", ret);
1260 		}
1261 	}
1262 
1263 put_adev:
1264 	acpi_dev_put(adev);
1265 	return ret;
1266 }
1267 
byt_rt5640_init(struct snd_soc_pcm_runtime * runtime)1268 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
1269 {
1270 	struct snd_soc_card *card = runtime->card;
1271 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1272 	struct rt5640_set_jack_data *jack_data = &priv->jack_data;
1273 	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
1274 	const struct snd_soc_dapm_route *custom_map = NULL;
1275 	int num_routes = 0;
1276 	int ret;
1277 
1278 	card->dapm.idle_bias_off = true;
1279 	jack_data->use_platform_clock = true;
1280 
1281 	/* Start with RC clk for jack-detect (we disable MCLK below) */
1282 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
1283 		snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1284 			RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
1285 
1286 	rt5640_sel_asrc_clk_src(component,
1287 				RT5640_DA_STEREO_FILTER |
1288 				RT5640_DA_MONO_L_FILTER	|
1289 				RT5640_DA_MONO_R_FILTER	|
1290 				RT5640_AD_STEREO_FILTER	|
1291 				RT5640_AD_MONO_L_FILTER	|
1292 				RT5640_AD_MONO_R_FILTER,
1293 				RT5640_CLK_SEL_ASRC);
1294 
1295 	ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
1296 					ARRAY_SIZE(byt_rt5640_controls));
1297 	if (ret) {
1298 		dev_err(card->dev, "unable to add card controls\n");
1299 		return ret;
1300 	}
1301 
1302 	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1303 	case BYT_RT5640_IN1_MAP:
1304 		custom_map = byt_rt5640_intmic_in1_map;
1305 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
1306 		break;
1307 	case BYT_RT5640_IN3_MAP:
1308 		custom_map = byt_rt5640_intmic_in3_map;
1309 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
1310 		break;
1311 	case BYT_RT5640_DMIC1_MAP:
1312 		custom_map = byt_rt5640_intmic_dmic1_map;
1313 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
1314 		break;
1315 	case BYT_RT5640_DMIC2_MAP:
1316 		custom_map = byt_rt5640_intmic_dmic2_map;
1317 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
1318 		break;
1319 	}
1320 
1321 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
1322 	if (ret)
1323 		return ret;
1324 
1325 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1) {
1326 		ret = snd_soc_dapm_add_routes(&card->dapm,
1327 					byt_rt5640_hsmic2_in1_map,
1328 					ARRAY_SIZE(byt_rt5640_hsmic2_in1_map));
1329 		if (ret)
1330 			return ret;
1331 	}
1332 
1333 	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
1334 		ret = snd_soc_dapm_add_routes(&card->dapm,
1335 					byt_rt5640_ssp2_aif2_map,
1336 					ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
1337 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
1338 		ret = snd_soc_dapm_add_routes(&card->dapm,
1339 					byt_rt5640_ssp0_aif1_map,
1340 					ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
1341 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
1342 		ret = snd_soc_dapm_add_routes(&card->dapm,
1343 					byt_rt5640_ssp0_aif2_map,
1344 					ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
1345 	} else {
1346 		ret = snd_soc_dapm_add_routes(&card->dapm,
1347 					byt_rt5640_ssp2_aif1_map,
1348 					ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
1349 	}
1350 	if (ret)
1351 		return ret;
1352 
1353 	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1354 		ret = snd_soc_dapm_add_routes(&card->dapm,
1355 					byt_rt5640_mono_spk_map,
1356 					ARRAY_SIZE(byt_rt5640_mono_spk_map));
1357 	} else if (!(byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)) {
1358 		ret = snd_soc_dapm_add_routes(&card->dapm,
1359 					byt_rt5640_stereo_spk_map,
1360 					ARRAY_SIZE(byt_rt5640_stereo_spk_map));
1361 	}
1362 	if (ret)
1363 		return ret;
1364 
1365 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1366 		ret = snd_soc_dapm_add_routes(&card->dapm,
1367 					byt_rt5640_lineout_map,
1368 					ARRAY_SIZE(byt_rt5640_lineout_map));
1369 		if (ret)
1370 			return ret;
1371 	}
1372 
1373 	/*
1374 	 * The firmware might enable the clock at boot (this information
1375 	 * may or may not be reflected in the enable clock register).
1376 	 * To change the rate we must disable the clock first to cover
1377 	 * these cases. Due to common clock framework restrictions that
1378 	 * do not allow to disable a clock that has not been enabled,
1379 	 * we need to enable the clock first.
1380 	 */
1381 	ret = clk_prepare_enable(priv->mclk);
1382 	if (!ret)
1383 		clk_disable_unprepare(priv->mclk);
1384 
1385 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1386 		ret = clk_set_rate(priv->mclk, 25000000);
1387 	else
1388 		ret = clk_set_rate(priv->mclk, 19200000);
1389 	if (ret) {
1390 		dev_err(card->dev, "unable to set MCLK rate\n");
1391 		return ret;
1392 	}
1393 
1394 	if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1395 		ret = snd_soc_card_jack_new_pins(card, "Headset",
1396 						 SND_JACK_HEADSET | SND_JACK_BTN_0,
1397 						 &priv->jack, rt5640_pins,
1398 						 ARRAY_SIZE(rt5640_pins));
1399 		if (ret) {
1400 			dev_err(card->dev, "Jack creation failed %d\n", ret);
1401 			return ret;
1402 		}
1403 		snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1404 				 KEY_PLAYPAUSE);
1405 
1406 		if (byt_rt5640_quirk & BYT_RT5640_USE_AMCR0F28) {
1407 			ret = byt_rt5640_get_amcr0f28_settings(card);
1408 			if (ret)
1409 				return ret;
1410 		}
1411 
1412 		snd_soc_component_set_jack(component, &priv->jack, &priv->jack_data);
1413 	}
1414 
1415 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1416 		ret = snd_soc_card_jack_new_pins(card, "Headset",
1417 						 SND_JACK_HEADSET,
1418 						 &priv->jack, rt5640_pins,
1419 						 ARRAY_SIZE(rt5640_pins));
1420 		if (ret)
1421 			return ret;
1422 
1423 		ret = snd_soc_card_jack_new_pins(card, "Headset 2",
1424 						 SND_JACK_HEADSET,
1425 						 &priv->jack2, rt5640_pins2,
1426 						 ARRAY_SIZE(rt5640_pins2));
1427 		if (ret)
1428 			return ret;
1429 
1430 		rt5640_jack_gpio.data = priv;
1431 		rt5640_jack_gpio.gpiod_dev = priv->codec_dev;
1432 		rt5640_jack_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack1_check;
1433 		ret = snd_soc_jack_add_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1434 		if (ret)
1435 			return ret;
1436 
1437 		rt5640_set_ovcd_params(component);
1438 		rt5640_jack2_gpio.data = component;
1439 		rt5640_jack2_gpio.gpiod_dev = priv->codec_dev;
1440 		rt5640_jack2_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack2_check;
1441 		ret = snd_soc_jack_add_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1442 		if (ret) {
1443 			snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1444 			return ret;
1445 		}
1446 	}
1447 
1448 	return 0;
1449 }
1450 
byt_rt5640_exit(struct snd_soc_pcm_runtime * runtime)1451 static void byt_rt5640_exit(struct snd_soc_pcm_runtime *runtime)
1452 {
1453 	struct snd_soc_card *card = runtime->card;
1454 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1455 
1456 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1457 		snd_soc_jack_free_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1458 		snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1459 	}
1460 }
1461 
byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)1462 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1463 			    struct snd_pcm_hw_params *params)
1464 {
1465 	struct snd_interval *rate = hw_param_interval(params,
1466 			SNDRV_PCM_HW_PARAM_RATE);
1467 	struct snd_interval *channels = hw_param_interval(params,
1468 						SNDRV_PCM_HW_PARAM_CHANNELS);
1469 	int ret, bits;
1470 
1471 	/* The DSP will convert the FE rate to 48k, stereo */
1472 	rate->min = rate->max = 48000;
1473 	channels->min = channels->max = 2;
1474 
1475 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1476 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1477 		/* set SSP0 to 16-bit */
1478 		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1479 		bits = 16;
1480 	} else {
1481 		/* set SSP2 to 24-bit */
1482 		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1483 		bits = 24;
1484 	}
1485 
1486 	/*
1487 	 * Default mode for SSP configuration is TDM 4 slot, override config
1488 	 * with explicit setting to I2S 2ch. The word length is set with
1489 	 * dai_set_tdm_slot() since there is no other API exposed
1490 	 */
1491 	ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
1492 				  SND_SOC_DAIFMT_I2S     |
1493 				  SND_SOC_DAIFMT_NB_NF   |
1494 				  SND_SOC_DAIFMT_BP_FP);
1495 	if (ret < 0) {
1496 		dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1497 		return ret;
1498 	}
1499 
1500 	ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
1501 	if (ret < 0) {
1502 		dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1503 		return ret;
1504 	}
1505 
1506 	return 0;
1507 }
1508 
byt_rt5640_aif1_startup(struct snd_pcm_substream * substream)1509 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1510 {
1511 	return snd_pcm_hw_constraint_single(substream->runtime,
1512 			SNDRV_PCM_HW_PARAM_RATE, 48000);
1513 }
1514 
1515 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1516 	.startup = byt_rt5640_aif1_startup,
1517 };
1518 
1519 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1520 	.hw_params = byt_rt5640_aif1_hw_params,
1521 };
1522 
1523 SND_SOC_DAILINK_DEF(dummy,
1524 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
1525 
1526 SND_SOC_DAILINK_DEF(media,
1527 	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
1528 
1529 SND_SOC_DAILINK_DEF(deepbuffer,
1530 	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
1531 
1532 SND_SOC_DAILINK_DEF(ssp2_port,
1533 	/* overwritten for ssp0 routing */
1534 	DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
1535 SND_SOC_DAILINK_DEF(ssp2_codec,
1536 	DAILINK_COMP_ARRAY(COMP_CODEC(
1537 	/* overwritten with HID */ "i2c-10EC5640:00",
1538 	/* changed w/ quirk */	"rt5640-aif1")));
1539 
1540 SND_SOC_DAILINK_DEF(platform,
1541 	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
1542 
1543 static struct snd_soc_dai_link byt_rt5640_dais[] = {
1544 	[MERR_DPCM_AUDIO] = {
1545 		.name = "Baytrail Audio Port",
1546 		.stream_name = "Baytrail Audio",
1547 		.nonatomic = true,
1548 		.dynamic = 1,
1549 		.dpcm_playback = 1,
1550 		.dpcm_capture = 1,
1551 		.ops = &byt_rt5640_aif1_ops,
1552 		SND_SOC_DAILINK_REG(media, dummy, platform),
1553 	},
1554 	[MERR_DPCM_DEEP_BUFFER] = {
1555 		.name = "Deep-Buffer Audio Port",
1556 		.stream_name = "Deep-Buffer Audio",
1557 		.nonatomic = true,
1558 		.dynamic = 1,
1559 		.dpcm_playback = 1,
1560 		.ops = &byt_rt5640_aif1_ops,
1561 		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
1562 	},
1563 		/* back ends */
1564 	{
1565 		.name = "SSP2-Codec",
1566 		.id = 0,
1567 		.no_pcm = 1,
1568 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1569 						| SND_SOC_DAIFMT_CBC_CFC,
1570 		.be_hw_params_fixup = byt_rt5640_codec_fixup,
1571 		.dpcm_playback = 1,
1572 		.dpcm_capture = 1,
1573 		.init = byt_rt5640_init,
1574 		.exit = byt_rt5640_exit,
1575 		.ops = &byt_rt5640_be_ssp2_ops,
1576 		SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
1577 	},
1578 };
1579 
1580 /* SoC card */
1581 static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1582 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1583 static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1584 #endif
1585 static char byt_rt5640_components[64]; /* = "cfg-spk:* cfg-mic:* ..." */
1586 
byt_rt5640_suspend(struct snd_soc_card * card)1587 static int byt_rt5640_suspend(struct snd_soc_card *card)
1588 {
1589 	struct snd_soc_component *component;
1590 
1591 	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1592 		return 0;
1593 
1594 	for_each_card_components(card, component) {
1595 		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1596 			dev_dbg(component->dev, "disabling jack detect before suspend\n");
1597 			snd_soc_component_set_jack(component, NULL, NULL);
1598 			break;
1599 		}
1600 	}
1601 
1602 	return 0;
1603 }
1604 
byt_rt5640_resume(struct snd_soc_card * card)1605 static int byt_rt5640_resume(struct snd_soc_card *card)
1606 {
1607 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1608 	struct snd_soc_component *component;
1609 
1610 	if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1611 		return 0;
1612 
1613 	for_each_card_components(card, component) {
1614 		if (!strcmp(component->name, byt_rt5640_codec_name)) {
1615 			dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1616 			snd_soc_component_set_jack(component, &priv->jack,
1617 						   &priv->jack_data);
1618 			break;
1619 		}
1620 	}
1621 
1622 	return 0;
1623 }
1624 
1625 /* use space before codec name to simplify card ID, and simplify driver name */
1626 #define SOF_CARD_NAME "bytcht rt5640" /* card name will be 'sof-bytcht rt5640' */
1627 #define SOF_DRIVER_NAME "SOF"
1628 
1629 #define CARD_NAME "bytcr-rt5640"
1630 #define DRIVER_NAME NULL /* card name will be used for driver name */
1631 
1632 static struct snd_soc_card byt_rt5640_card = {
1633 	.owner = THIS_MODULE,
1634 	.dai_link = byt_rt5640_dais,
1635 	.num_links = ARRAY_SIZE(byt_rt5640_dais),
1636 	.dapm_widgets = byt_rt5640_widgets,
1637 	.num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1638 	.dapm_routes = byt_rt5640_audio_map,
1639 	.num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1640 	.fully_routed = true,
1641 	.suspend_pre = byt_rt5640_suspend,
1642 	.resume_post = byt_rt5640_resume,
1643 };
1644 
1645 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
1646 	u64 aif_value;       /* 1: AIF1, 2: AIF2 */
1647 	u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
1648 };
1649 
snd_byt_rt5640_mc_probe(struct platform_device * pdev)1650 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1651 {
1652 	struct device *dev = &pdev->dev;
1653 	static const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3", "none" };
1654 	struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
1655 	__maybe_unused const char *spk_type;
1656 	const struct dmi_system_id *dmi_id;
1657 	const char *headset2_string = "";
1658 	const char *lineout_string = "";
1659 	struct byt_rt5640_private *priv;
1660 	const char *platform_name;
1661 	struct acpi_device *adev;
1662 	struct device *codec_dev;
1663 	const char *cfg_spk;
1664 	bool sof_parent;
1665 	int ret_val = 0;
1666 	int dai_index = 0;
1667 	int i, aif;
1668 
1669 	is_bytcr = false;
1670 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1671 	if (!priv)
1672 		return -ENOMEM;
1673 
1674 	/* register the soc card */
1675 	byt_rt5640_card.dev = dev;
1676 	snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1677 
1678 	/* fix index of codec dai */
1679 	for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1680 		if (!strcmp(byt_rt5640_dais[i].codecs->name,
1681 			    "i2c-10EC5640:00")) {
1682 			dai_index = i;
1683 			break;
1684 		}
1685 	}
1686 
1687 	/* fixup codec name based on HID */
1688 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
1689 	if (adev) {
1690 		snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1691 			 "i2c-%s", acpi_dev_name(adev));
1692 		byt_rt5640_dais[dai_index].codecs->name = byt_rt5640_codec_name;
1693 	} else {
1694 		dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
1695 		return -ENXIO;
1696 	}
1697 
1698 	codec_dev = acpi_get_first_physical_node(adev);
1699 	acpi_dev_put(adev);
1700 	if (!codec_dev)
1701 		return -EPROBE_DEFER;
1702 	priv->codec_dev = get_device(codec_dev);
1703 
1704 	/*
1705 	 * swap SSP0 if bytcr is detected
1706 	 * (will be overridden if DMI quirk is detected)
1707 	 */
1708 	if (soc_intel_is_byt()) {
1709 		if (mach->mach_params.acpi_ipc_irq_index == 0)
1710 			is_bytcr = true;
1711 	}
1712 
1713 	if (is_bytcr) {
1714 		/*
1715 		 * Baytrail CR platforms may have CHAN package in BIOS, try
1716 		 * to find relevant routing quirk based as done on Windows
1717 		 * platforms. We have to read the information directly from the
1718 		 * BIOS, at this stage the card is not created and the links
1719 		 * with the codec driver/pdata are non-existent
1720 		 */
1721 
1722 		struct acpi_chan_package chan_package = { 0 };
1723 
1724 		/* format specified: 2 64-bit integers */
1725 		struct acpi_buffer format = {sizeof("NN"), "NN"};
1726 		struct acpi_buffer state = {0, NULL};
1727 		struct snd_soc_acpi_package_context pkg_ctx;
1728 		bool pkg_found = false;
1729 
1730 		state.length = sizeof(chan_package);
1731 		state.pointer = &chan_package;
1732 
1733 		pkg_ctx.name = "CHAN";
1734 		pkg_ctx.length = 2;
1735 		pkg_ctx.format = &format;
1736 		pkg_ctx.state = &state;
1737 		pkg_ctx.data_valid = false;
1738 
1739 		pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1740 							       &pkg_ctx);
1741 		if (pkg_found) {
1742 			if (chan_package.aif_value == 1) {
1743 				dev_info(dev, "BIOS Routing: AIF1 connected\n");
1744 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1745 			} else  if (chan_package.aif_value == 2) {
1746 				dev_info(dev, "BIOS Routing: AIF2 connected\n");
1747 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1748 			} else {
1749 				dev_info(dev, "BIOS Routing isn't valid, ignored\n");
1750 				pkg_found = false;
1751 			}
1752 		}
1753 
1754 		if (!pkg_found) {
1755 			/* no BIOS indications, assume SSP0-AIF2 connection */
1756 			byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1757 		}
1758 
1759 		/* change defaults for Baytrail-CR capture */
1760 		byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1761 	} else {
1762 		byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1763 				    BYT_RT5640_JD_SRC_JD2_IN4N |
1764 				    BYT_RT5640_OVCD_TH_2000UA |
1765 				    BYT_RT5640_OVCD_SF_0P75;
1766 	}
1767 
1768 	/* check quirks before creating card */
1769 	dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1770 	if (dmi_id)
1771 		byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1772 	if (quirk_override != -1) {
1773 		dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
1774 			 byt_rt5640_quirk, quirk_override);
1775 		byt_rt5640_quirk = quirk_override;
1776 	}
1777 
1778 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1779 		acpi_dev_add_driver_gpios(ACPI_COMPANION(priv->codec_dev),
1780 					  byt_rt5640_hp_elitepad_1000g2_gpios);
1781 
1782 		priv->hsmic_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1783 							   "headset-mic-detect", GPIOD_IN,
1784 							   "headset-mic-detect");
1785 		if (IS_ERR(priv->hsmic_detect)) {
1786 			ret_val = dev_err_probe(dev, PTR_ERR(priv->hsmic_detect),
1787 						"getting hsmic-detect GPIO\n");
1788 			goto err_device;
1789 		}
1790 	}
1791 
1792 	/* Must be called before register_card, also see declaration comment. */
1793 	ret_val = byt_rt5640_add_codec_device_props(codec_dev, priv);
1794 	if (ret_val)
1795 		goto err_remove_gpios;
1796 
1797 	log_quirks(dev);
1798 
1799 	if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1800 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1801 		byt_rt5640_dais[dai_index].codecs->dai_name = "rt5640-aif2";
1802 		aif = 2;
1803 	} else {
1804 		aif = 1;
1805 	}
1806 
1807 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1808 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1809 		byt_rt5640_dais[dai_index].cpus->dai_name = "ssp0-port";
1810 
1811 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1812 		priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
1813 		if (IS_ERR(priv->mclk)) {
1814 			ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
1815 						"Failed to get MCLK from pmc_plt_clk_3\n");
1816 			goto err;
1817 		}
1818 		/*
1819 		 * Fall back to bit clock usage when clock is not
1820 		 * available likely due to missing dependencies.
1821 		 */
1822 		if (!priv->mclk)
1823 			byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1824 	}
1825 
1826 	if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS) {
1827 		cfg_spk = "0";
1828 		spk_type = "none";
1829 	} else if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1830 		cfg_spk = "1";
1831 		spk_type = "mono";
1832 	} else if (byt_rt5640_quirk & BYT_RT5640_SWAPPED_SPEAKERS) {
1833 		cfg_spk = "swapped";
1834 		spk_type = "swapped";
1835 	} else {
1836 		cfg_spk = "2";
1837 		spk_type = "stereo";
1838 	}
1839 
1840 	if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1841 		if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
1842 			lineout_string = " cfg-hp2:lineout";
1843 		else
1844 			lineout_string = " cfg-lineout:2";
1845 	}
1846 
1847 	if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
1848 		headset2_string = " cfg-hs2:in1";
1849 
1850 	snprintf(byt_rt5640_components, sizeof(byt_rt5640_components),
1851 		 "cfg-spk:%s cfg-mic:%s aif:%d%s%s", cfg_spk,
1852 		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)], aif,
1853 		 lineout_string, headset2_string);
1854 	byt_rt5640_card.components = byt_rt5640_components;
1855 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1856 	snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1857 		 "bytcr-rt5640-%s-spk-%s-mic", spk_type,
1858 		 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1859 	byt_rt5640_card.long_name = byt_rt5640_long_name;
1860 #endif
1861 
1862 	/* override platform name, if required */
1863 	platform_name = mach->mach_params.platform;
1864 
1865 	ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5640_card,
1866 							platform_name);
1867 	if (ret_val)
1868 		goto err;
1869 
1870 	sof_parent = snd_soc_acpi_sof_parent(dev);
1871 
1872 	/* set card and driver name */
1873 	if (sof_parent) {
1874 		byt_rt5640_card.name = SOF_CARD_NAME;
1875 		byt_rt5640_card.driver_name = SOF_DRIVER_NAME;
1876 	} else {
1877 		byt_rt5640_card.name = CARD_NAME;
1878 		byt_rt5640_card.driver_name = DRIVER_NAME;
1879 	}
1880 
1881 	/* set pm ops */
1882 	if (sof_parent)
1883 		dev->driver->pm = &snd_soc_pm_ops;
1884 
1885 	ret_val = devm_snd_soc_register_card(dev, &byt_rt5640_card);
1886 	if (ret_val) {
1887 		dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
1888 		goto err;
1889 	}
1890 	platform_set_drvdata(pdev, &byt_rt5640_card);
1891 	return ret_val;
1892 
1893 err:
1894 	device_remove_software_node(priv->codec_dev);
1895 err_remove_gpios:
1896 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1897 		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1898 err_device:
1899 	put_device(priv->codec_dev);
1900 	return ret_val;
1901 }
1902 
snd_byt_rt5640_mc_remove(struct platform_device * pdev)1903 static void snd_byt_rt5640_mc_remove(struct platform_device *pdev)
1904 {
1905 	struct snd_soc_card *card = platform_get_drvdata(pdev);
1906 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1907 
1908 	if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1909 		acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1910 
1911 	device_remove_software_node(priv->codec_dev);
1912 	put_device(priv->codec_dev);
1913 }
1914 
1915 static struct platform_driver snd_byt_rt5640_mc_driver = {
1916 	.driver = {
1917 		.name = "bytcr_rt5640",
1918 	},
1919 	.probe = snd_byt_rt5640_mc_probe,
1920 	.remove_new = snd_byt_rt5640_mc_remove,
1921 };
1922 
1923 module_platform_driver(snd_byt_rt5640_mc_driver);
1924 
1925 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1926 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1927 MODULE_LICENSE("GPL v2");
1928 MODULE_ALIAS("platform:bytcr_rt5640");
1929