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