ASoC: Intel: bytcr_rt5640: Add quirk for the Advantech MICA-071 tablet
[platform/kernel/linux-rpi.git] / sound / soc / intel / boards / bytcr_rt5640.c
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
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
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
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
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
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
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
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
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 Tab 8 W1-810 */
537                 .matches = {
538                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
539                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
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 One 10 S1002 */
549                 .matches = {
550                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
551                         DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
552                 },
553                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
554                                         BYT_RT5640_JD_SRC_JD2_IN4N |
555                                         BYT_RT5640_OVCD_TH_2000UA |
556                                         BYT_RT5640_OVCD_SF_0P75 |
557                                         BYT_RT5640_DIFF_MIC |
558                                         BYT_RT5640_SSP0_AIF2 |
559                                         BYT_RT5640_MCLK_EN),
560         },
561         {
562                 .matches = {
563                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
564                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
565                 },
566                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
567                                         BYT_RT5640_JD_SRC_JD2_IN4N |
568                                         BYT_RT5640_OVCD_TH_2000UA |
569                                         BYT_RT5640_OVCD_SF_0P75 |
570                                         BYT_RT5640_SSP0_AIF1 |
571                                         BYT_RT5640_MCLK_EN),
572         },
573         {
574                 /* Advantech MICA-071 */
575                 .matches = {
576                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Advantech"),
577                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MICA-071"),
578                 },
579                 /* OVCD Th = 1500uA to reliable detect head-phones vs -set */
580                 .driver_data = (void *)(BYT_RT5640_IN3_MAP |
581                                         BYT_RT5640_JD_SRC_JD2_IN4N |
582                                         BYT_RT5640_OVCD_TH_1500UA |
583                                         BYT_RT5640_OVCD_SF_0P75 |
584                                         BYT_RT5640_MONO_SPEAKER |
585                                         BYT_RT5640_DIFF_MIC |
586                                         BYT_RT5640_MCLK_EN),
587         },
588         {
589                 .matches = {
590                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
591                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
592                 },
593                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
594                                         BYT_RT5640_MONO_SPEAKER |
595                                         BYT_RT5640_SSP0_AIF1 |
596                                         BYT_RT5640_MCLK_EN),
597         },
598         {
599                 .matches = {
600                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
601                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
602                 },
603                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
604                                         BYT_RT5640_JD_SRC_JD2_IN4N |
605                                         BYT_RT5640_OVCD_TH_2000UA |
606                                         BYT_RT5640_OVCD_SF_0P75 |
607                                         BYT_RT5640_SSP0_AIF1 |
608                                         BYT_RT5640_MCLK_EN),
609         },
610         {
611                 .matches = {
612                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
613                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ME176C"),
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, "T100TA"),
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_MCLK_EN),
632         },
633         {
634                 .matches = {
635                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
636                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
637                 },
638                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
639                                         BYT_RT5640_JD_SRC_JD2_IN4N |
640                                         BYT_RT5640_OVCD_TH_2000UA |
641                                         BYT_RT5640_OVCD_SF_0P75 |
642                                         BYT_RT5640_MONO_SPEAKER |
643                                         BYT_RT5640_DIFF_MIC |
644                                         BYT_RT5640_SSP0_AIF2 |
645                                         BYT_RT5640_MCLK_EN),
646         },
647         {       /* Chuwi Vi8 (CWI506) */
648                 .matches = {
649                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
650                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
651                         /* The above are too generic, also match BIOS info */
652                         DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
653                 },
654                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
655                                         BYT_RT5640_MONO_SPEAKER |
656                                         BYT_RT5640_SSP0_AIF1 |
657                                         BYT_RT5640_MCLK_EN),
658         },
659         {
660                 /* Chuwi Vi10 (CWI505) */
661                 .matches = {
662                         DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
663                         DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
664                         DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
665                         DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
666                 },
667                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
668                                         BYT_RT5640_JD_SRC_JD2_IN4N |
669                                         BYT_RT5640_OVCD_TH_2000UA |
670                                         BYT_RT5640_OVCD_SF_0P75 |
671                                         BYT_RT5640_DIFF_MIC |
672                                         BYT_RT5640_SSP0_AIF1 |
673                                         BYT_RT5640_MCLK_EN),
674         },
675         {
676                 /* Chuwi Hi8 (CWI509) */
677                 .matches = {
678                         DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
679                         DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
680                         DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
681                         DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
682                 },
683                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
684                                         BYT_RT5640_JD_SRC_JD2_IN4N |
685                                         BYT_RT5640_OVCD_TH_2000UA |
686                                         BYT_RT5640_OVCD_SF_0P75 |
687                                         BYT_RT5640_MONO_SPEAKER |
688                                         BYT_RT5640_DIFF_MIC |
689                                         BYT_RT5640_SSP0_AIF1 |
690                                         BYT_RT5640_MCLK_EN),
691         },
692         {
693                 .matches = {
694                         DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
695                         DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
696                 },
697                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
698         },
699         {       /* Connect Tablet 9 */
700                 .matches = {
701                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
702                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
703                 },
704                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
705                                         BYT_RT5640_MONO_SPEAKER |
706                                         BYT_RT5640_SSP0_AIF1 |
707                                         BYT_RT5640_MCLK_EN),
708         },
709         {
710                 .matches = {
711                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
712                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
713                 },
714                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
715                                         BYT_RT5640_JD_SRC_JD2_IN4N |
716                                         BYT_RT5640_OVCD_TH_2000UA |
717                                         BYT_RT5640_OVCD_SF_0P75 |
718                                         BYT_RT5640_MONO_SPEAKER |
719                                         BYT_RT5640_MCLK_EN),
720         },
721         {       /* Estar Beauty HD MID 7316R */
722                 .matches = {
723                         DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
724                         DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
725                 },
726                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
727                                         BYT_RT5640_MONO_SPEAKER |
728                                         BYT_RT5640_SSP0_AIF1 |
729                                         BYT_RT5640_MCLK_EN),
730         },
731         {       /* Glavey TM800A550L */
732                 .matches = {
733                         DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
734                         DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
735                         /* Above strings are too generic, also match on BIOS version */
736                         DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
737                 },
738                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
739                                         BYT_RT5640_SSP0_AIF1 |
740                                         BYT_RT5640_MCLK_EN),
741         },
742         {
743                 .matches = {
744                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
745                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
746                 },
747                 .driver_data = (void *)(BYT_RT5640_DMIC2_MAP |
748                                         BYT_RT5640_MCLK_EN |
749                                         BYT_RT5640_LINEOUT |
750                                         BYT_RT5640_LINEOUT_AS_HP2 |
751                                         BYT_RT5640_HSMIC2_ON_IN1 |
752                                         BYT_RT5640_JD_HP_ELITEP_1000G2),
753         },
754         {       /* HP Pavilion x2 10-k0XX, 10-n0XX */
755                 .matches = {
756                         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
757                         DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
758                 },
759                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
760                                         BYT_RT5640_JD_SRC_JD2_IN4N |
761                                         BYT_RT5640_OVCD_TH_1500UA |
762                                         BYT_RT5640_OVCD_SF_0P75 |
763                                         BYT_RT5640_SSP0_AIF1 |
764                                         BYT_RT5640_MCLK_EN),
765         },
766         {       /* HP Pavilion x2 10-p0XX */
767                 .matches = {
768                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
769                         DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
770                 },
771                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
772                                         BYT_RT5640_JD_SRC_JD1_IN4P |
773                                         BYT_RT5640_OVCD_TH_2000UA |
774                                         BYT_RT5640_OVCD_SF_0P75 |
775                                         BYT_RT5640_MCLK_EN),
776         },
777         {       /* HP Pro Tablet 408 */
778                 .matches = {
779                         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
780                         DMI_MATCH(DMI_PRODUCT_NAME, "HP Pro Tablet 408"),
781                 },
782                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
783                                         BYT_RT5640_JD_SRC_JD2_IN4N |
784                                         BYT_RT5640_OVCD_TH_1500UA |
785                                         BYT_RT5640_OVCD_SF_0P75 |
786                                         BYT_RT5640_SSP0_AIF1 |
787                                         BYT_RT5640_MCLK_EN),
788         },
789         {       /* HP Stream 7 */
790                 .matches = {
791                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
792                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
793                 },
794                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
795                                         BYT_RT5640_MONO_SPEAKER |
796                                         BYT_RT5640_JD_NOT_INV |
797                                         BYT_RT5640_SSP0_AIF1 |
798                                         BYT_RT5640_MCLK_EN),
799         },
800         {       /* I.T.Works TW891 */
801                 .matches = {
802                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
803                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
804                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
805                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
806                 },
807                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
808                                         BYT_RT5640_MONO_SPEAKER |
809                                         BYT_RT5640_SSP0_AIF1 |
810                                         BYT_RT5640_MCLK_EN),
811         },
812         {       /* Lamina I8270 / T701BR.SE */
813                 .matches = {
814                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
815                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
816                 },
817                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
818                                         BYT_RT5640_MONO_SPEAKER |
819                                         BYT_RT5640_JD_NOT_INV |
820                                         BYT_RT5640_SSP0_AIF1 |
821                                         BYT_RT5640_MCLK_EN),
822         },
823         {       /* Lenovo Miix 2 8 */
824                 .matches = {
825                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
826                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
827                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
828                 },
829                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
830                                         BYT_RT5640_JD_SRC_JD2_IN4N |
831                                         BYT_RT5640_OVCD_TH_2000UA |
832                                         BYT_RT5640_OVCD_SF_0P75 |
833                                         BYT_RT5640_MONO_SPEAKER |
834                                         BYT_RT5640_MCLK_EN),
835         },
836         {       /* Lenovo Miix 3-830 */
837                 .matches = {
838                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
839                         DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
840                 },
841                 .driver_data = (void *)(BYT_RT5640_IN1_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_DIFF_MIC |
847                                         BYT_RT5640_SSP0_AIF1 |
848                                         BYT_RT5640_MCLK_EN),
849         },
850         {       /* Linx Linx7 tablet */
851                 .matches = {
852                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
853                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
854                 },
855                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
856                                         BYT_RT5640_MONO_SPEAKER |
857                                         BYT_RT5640_JD_NOT_INV |
858                                         BYT_RT5640_SSP0_AIF1 |
859                                         BYT_RT5640_MCLK_EN),
860         },
861         {       /* Mele PCG03 Mini PC */
862                 .matches = {
863                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Mini PC"),
864                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Mini PC"),
865                 },
866                 .driver_data = (void *)(BYT_RT5640_NO_INTERNAL_MIC_MAP |
867                                         BYT_RT5640_NO_SPEAKERS |
868                                         BYT_RT5640_SSP0_AIF1),
869         },
870         {       /* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
871                 .matches = {
872                         DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
873                         DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
874                 },
875                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
876                                         BYT_RT5640_MONO_SPEAKER |
877                                         BYT_RT5640_SSP0_AIF1 |
878                                         BYT_RT5640_MCLK_EN),
879         },
880         {
881                 /* MPMAN MPWIN895CL */
882                 .matches = {
883                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
884                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
885                 },
886                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
887                                         BYT_RT5640_MONO_SPEAKER |
888                                         BYT_RT5640_SSP0_AIF1 |
889                                         BYT_RT5640_MCLK_EN),
890         },
891         {       /* MSI S100 tablet */
892                 .matches = {
893                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
894                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
895                 },
896                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
897                                         BYT_RT5640_JD_SRC_JD2_IN4N |
898                                         BYT_RT5640_OVCD_TH_2000UA |
899                                         BYT_RT5640_OVCD_SF_0P75 |
900                                         BYT_RT5640_MONO_SPEAKER |
901                                         BYT_RT5640_DIFF_MIC |
902                                         BYT_RT5640_MCLK_EN),
903         },
904         {       /* Nuvison/TMax TM800W560 */
905                 .matches = {
906                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
907                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
908                 },
909                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
910                                         BYT_RT5640_JD_SRC_JD2_IN4N |
911                                         BYT_RT5640_OVCD_TH_2000UA |
912                                         BYT_RT5640_OVCD_SF_0P75 |
913                                         BYT_RT5640_JD_NOT_INV |
914                                         BYT_RT5640_DIFF_MIC |
915                                         BYT_RT5640_SSP0_AIF1 |
916                                         BYT_RT5640_MCLK_EN),
917         },
918         {       /* Onda v975w */
919                 .matches = {
920                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
921                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
922                         /* The above are too generic, also match BIOS info */
923                         DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
924                         DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
925                 },
926                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
927                                         BYT_RT5640_JD_SRC_JD2_IN4N |
928                                         BYT_RT5640_OVCD_TH_2000UA |
929                                         BYT_RT5640_OVCD_SF_0P75 |
930                                         BYT_RT5640_DIFF_MIC |
931                                         BYT_RT5640_MCLK_EN),
932         },
933         {       /* Pipo W4 */
934                 .matches = {
935                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
936                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
937                         /* The above are too generic, also match BIOS info */
938                         DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
939                 },
940                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
941                                         BYT_RT5640_MONO_SPEAKER |
942                                         BYT_RT5640_SSP0_AIF1 |
943                                         BYT_RT5640_MCLK_EN),
944         },
945         {       /* Point of View Mobii TAB-P800W (V2.0) */
946                 .matches = {
947                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
948                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
949                         /* The above are too generic, also match BIOS info */
950                         DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
951                         DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
952                 },
953                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
954                                         BYT_RT5640_JD_SRC_JD2_IN4N |
955                                         BYT_RT5640_OVCD_TH_2000UA |
956                                         BYT_RT5640_OVCD_SF_0P75 |
957                                         BYT_RT5640_MONO_SPEAKER |
958                                         BYT_RT5640_DIFF_MIC |
959                                         BYT_RT5640_SSP0_AIF2 |
960                                         BYT_RT5640_MCLK_EN),
961         },
962         {       /* Point of View Mobii TAB-P800W (V2.1) */
963                 .matches = {
964                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
965                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
966                         /* The above are too generic, also match BIOS info */
967                         DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
968                         DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
969                 },
970                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
971                                         BYT_RT5640_JD_SRC_JD2_IN4N |
972                                         BYT_RT5640_OVCD_TH_2000UA |
973                                         BYT_RT5640_OVCD_SF_0P75 |
974                                         BYT_RT5640_MONO_SPEAKER |
975                                         BYT_RT5640_DIFF_MIC |
976                                         BYT_RT5640_SSP0_AIF2 |
977                                         BYT_RT5640_MCLK_EN),
978         },
979         {       /* Point of View Mobii TAB-P1005W-232 (V2.0) */
980                 .matches = {
981                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "POV"),
982                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "I102A"),
983                 },
984                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
985                                         BYT_RT5640_JD_SRC_JD2_IN4N |
986                                         BYT_RT5640_OVCD_TH_2000UA |
987                                         BYT_RT5640_OVCD_SF_0P75 |
988                                         BYT_RT5640_DIFF_MIC |
989                                         BYT_RT5640_SSP0_AIF1 |
990                                         BYT_RT5640_MCLK_EN),
991         },
992         {
993                 /* Prowise PT301 */
994                 .matches = {
995                         DMI_MATCH(DMI_SYS_VENDOR, "Prowise"),
996                         DMI_MATCH(DMI_PRODUCT_NAME, "PT301"),
997                 },
998                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
999                                         BYT_RT5640_JD_SRC_JD2_IN4N |
1000                                         BYT_RT5640_OVCD_TH_2000UA |
1001                                         BYT_RT5640_OVCD_SF_0P75 |
1002                                         BYT_RT5640_DIFF_MIC |
1003                                         BYT_RT5640_SSP0_AIF1 |
1004                                         BYT_RT5640_MCLK_EN),
1005         },
1006         {
1007                 /* Teclast X89 */
1008                 .matches = {
1009                         DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
1010                         DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
1011                 },
1012                 .driver_data = (void *)(BYT_RT5640_IN3_MAP |
1013                                         BYT_RT5640_JD_SRC_JD1_IN4P |
1014                                         BYT_RT5640_OVCD_TH_2000UA |
1015                                         BYT_RT5640_OVCD_SF_1P0 |
1016                                         BYT_RT5640_SSP0_AIF1 |
1017                                         BYT_RT5640_MCLK_EN),
1018         },
1019         {       /* Toshiba Satellite Click Mini L9W-B */
1020                 .matches = {
1021                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1022                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
1023                 },
1024                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1025                                         BYT_RT5640_JD_SRC_JD2_IN4N |
1026                                         BYT_RT5640_OVCD_TH_1500UA |
1027                                         BYT_RT5640_OVCD_SF_0P75 |
1028                                         BYT_RT5640_SSP0_AIF1 |
1029                                         BYT_RT5640_MCLK_EN),
1030         },
1031         {       /* Toshiba Encore WT8-A */
1032                 .matches = {
1033                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1034                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
1035                 },
1036                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1037                                         BYT_RT5640_JD_SRC_JD2_IN4N |
1038                                         BYT_RT5640_OVCD_TH_2000UA |
1039                                         BYT_RT5640_OVCD_SF_0P75 |
1040                                         BYT_RT5640_JD_NOT_INV |
1041                                         BYT_RT5640_MCLK_EN),
1042         },
1043         {       /* Toshiba Encore WT10-A */
1044                 .matches = {
1045                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1046                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
1047                 },
1048                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
1049                                         BYT_RT5640_JD_SRC_JD1_IN4P |
1050                                         BYT_RT5640_OVCD_TH_2000UA |
1051                                         BYT_RT5640_OVCD_SF_0P75 |
1052                                         BYT_RT5640_SSP0_AIF2 |
1053                                         BYT_RT5640_MCLK_EN),
1054         },
1055         {       /* Voyo Winpad A15 */
1056                 .matches = {
1057                         DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1058                         DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1059                         /* Above strings are too generic, also match on BIOS date */
1060                         DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
1061                 },
1062                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
1063                                         BYT_RT5640_JD_SRC_JD2_IN4N |
1064                                         BYT_RT5640_OVCD_TH_2000UA |
1065                                         BYT_RT5640_OVCD_SF_0P75 |
1066                                         BYT_RT5640_DIFF_MIC |
1067                                         BYT_RT5640_MCLK_EN),
1068         },
1069         {       /* Catch-all for generic Insyde tablets, must be last */
1070                 .matches = {
1071                         DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
1072                 },
1073                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
1074                                         BYT_RT5640_MCLK_EN |
1075                                         BYT_RT5640_SSP0_AIF1),
1076
1077         },
1078         {}
1079 };
1080
1081 /*
1082  * Note this MUST be called before snd_soc_register_card(), so that the props
1083  * are in place before the codec component driver's probe function parses them.
1084  */
1085 static int byt_rt5640_add_codec_device_props(struct device *i2c_dev,
1086                                              struct byt_rt5640_private *priv)
1087 {
1088         struct property_entry props[MAX_NO_PROPS] = {};
1089         struct fwnode_handle *fwnode;
1090         int cnt = 0;
1091         int ret;
1092
1093         switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1094         case BYT_RT5640_DMIC1_MAP:
1095                 props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
1096                                                   RT5640_DMIC1_DATA_PIN_IN1P);
1097                 break;
1098         case BYT_RT5640_DMIC2_MAP:
1099                 props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
1100                                                   RT5640_DMIC2_DATA_PIN_IN1N);
1101                 break;
1102         case BYT_RT5640_IN1_MAP:
1103                 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1104                         props[cnt++] =
1105                                 PROPERTY_ENTRY_BOOL("realtek,in1-differential");
1106                 break;
1107         case BYT_RT5640_IN3_MAP:
1108                 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
1109                         props[cnt++] =
1110                                 PROPERTY_ENTRY_BOOL("realtek,in3-differential");
1111                 break;
1112         }
1113
1114         if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1115                 props[cnt++] = PROPERTY_ENTRY_U32(
1116                                     "realtek,jack-detect-source",
1117                                     BYT_RT5640_JDSRC(byt_rt5640_quirk));
1118
1119                 props[cnt++] = PROPERTY_ENTRY_U32(
1120                                     "realtek,over-current-threshold-microamp",
1121                                     BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
1122
1123                 props[cnt++] = PROPERTY_ENTRY_U32(
1124                                     "realtek,over-current-scale-factor",
1125                                     BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
1126         }
1127
1128         if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
1129                 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
1130
1131         fwnode = fwnode_create_software_node(props, NULL);
1132         if (IS_ERR(fwnode)) {
1133                 /* put_device() is handled in caller */
1134                 return PTR_ERR(fwnode);
1135         }
1136
1137         ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
1138
1139         fwnode_handle_put(fwnode);
1140
1141         return ret;
1142 }
1143
1144 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
1145 {
1146         struct snd_soc_card *card = runtime->card;
1147         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1148         struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
1149         const struct snd_soc_dapm_route *custom_map = NULL;
1150         int num_routes = 0;
1151         int ret;
1152
1153         card->dapm.idle_bias_off = true;
1154
1155         /* Start with RC clk for jack-detect (we disable MCLK below) */
1156         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
1157                 snd_soc_component_update_bits(component, RT5640_GLB_CLK,
1158                         RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
1159
1160         rt5640_sel_asrc_clk_src(component,
1161                                 RT5640_DA_STEREO_FILTER |
1162                                 RT5640_DA_MONO_L_FILTER |
1163                                 RT5640_DA_MONO_R_FILTER |
1164                                 RT5640_AD_STEREO_FILTER |
1165                                 RT5640_AD_MONO_L_FILTER |
1166                                 RT5640_AD_MONO_R_FILTER,
1167                                 RT5640_CLK_SEL_ASRC);
1168
1169         ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
1170                                         ARRAY_SIZE(byt_rt5640_controls));
1171         if (ret) {
1172                 dev_err(card->dev, "unable to add card controls\n");
1173                 return ret;
1174         }
1175
1176         switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
1177         case BYT_RT5640_IN1_MAP:
1178                 custom_map = byt_rt5640_intmic_in1_map;
1179                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
1180                 break;
1181         case BYT_RT5640_IN3_MAP:
1182                 custom_map = byt_rt5640_intmic_in3_map;
1183                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
1184                 break;
1185         case BYT_RT5640_DMIC1_MAP:
1186                 custom_map = byt_rt5640_intmic_dmic1_map;
1187                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
1188                 break;
1189         case BYT_RT5640_DMIC2_MAP:
1190                 custom_map = byt_rt5640_intmic_dmic2_map;
1191                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
1192                 break;
1193         }
1194
1195         ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
1196         if (ret)
1197                 return ret;
1198
1199         if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1) {
1200                 ret = snd_soc_dapm_add_routes(&card->dapm,
1201                                         byt_rt5640_hsmic2_in1_map,
1202                                         ARRAY_SIZE(byt_rt5640_hsmic2_in1_map));
1203                 if (ret)
1204                         return ret;
1205         }
1206
1207         if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
1208                 ret = snd_soc_dapm_add_routes(&card->dapm,
1209                                         byt_rt5640_ssp2_aif2_map,
1210                                         ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
1211         } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
1212                 ret = snd_soc_dapm_add_routes(&card->dapm,
1213                                         byt_rt5640_ssp0_aif1_map,
1214                                         ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
1215         } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
1216                 ret = snd_soc_dapm_add_routes(&card->dapm,
1217                                         byt_rt5640_ssp0_aif2_map,
1218                                         ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
1219         } else {
1220                 ret = snd_soc_dapm_add_routes(&card->dapm,
1221                                         byt_rt5640_ssp2_aif1_map,
1222                                         ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
1223         }
1224         if (ret)
1225                 return ret;
1226
1227         if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1228                 ret = snd_soc_dapm_add_routes(&card->dapm,
1229                                         byt_rt5640_mono_spk_map,
1230                                         ARRAY_SIZE(byt_rt5640_mono_spk_map));
1231         } else if (!(byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)) {
1232                 ret = snd_soc_dapm_add_routes(&card->dapm,
1233                                         byt_rt5640_stereo_spk_map,
1234                                         ARRAY_SIZE(byt_rt5640_stereo_spk_map));
1235         }
1236         if (ret)
1237                 return ret;
1238
1239         if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1240                 ret = snd_soc_dapm_add_routes(&card->dapm,
1241                                         byt_rt5640_lineout_map,
1242                                         ARRAY_SIZE(byt_rt5640_lineout_map));
1243                 if (ret)
1244                         return ret;
1245         }
1246
1247         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1248                 /*
1249                  * The firmware might enable the clock at
1250                  * boot (this information may or may not
1251                  * be reflected in the enable clock register).
1252                  * To change the rate we must disable the clock
1253                  * first to cover these cases. Due to common
1254                  * clock framework restrictions that do not allow
1255                  * to disable a clock that has not been enabled,
1256                  * we need to enable the clock first.
1257                  */
1258                 ret = clk_prepare_enable(priv->mclk);
1259                 if (!ret)
1260                         clk_disable_unprepare(priv->mclk);
1261
1262                 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1263                         ret = clk_set_rate(priv->mclk, 25000000);
1264                 else
1265                         ret = clk_set_rate(priv->mclk, 19200000);
1266
1267                 if (ret) {
1268                         dev_err(card->dev, "unable to set MCLK rate\n");
1269                         return ret;
1270                 }
1271         }
1272
1273         if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1274                 ret = snd_soc_card_jack_new(card, "Headset",
1275                                             SND_JACK_HEADSET | SND_JACK_BTN_0,
1276                                             &priv->jack, rt5640_pins,
1277                                             ARRAY_SIZE(rt5640_pins));
1278                 if (ret) {
1279                         dev_err(card->dev, "Jack creation failed %d\n", ret);
1280                         return ret;
1281                 }
1282                 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1283                                  KEY_PLAYPAUSE);
1284                 snd_soc_component_set_jack(component, &priv->jack, NULL);
1285         }
1286
1287         if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1288                 ret = snd_soc_card_jack_new(card, "Headset",
1289                                             SND_JACK_HEADSET,
1290                                             &priv->jack, rt5640_pins,
1291                                             ARRAY_SIZE(rt5640_pins));
1292                 if (ret)
1293                         return ret;
1294
1295                 ret = snd_soc_card_jack_new(card, "Headset 2",
1296                                             SND_JACK_HEADSET,
1297                                             &priv->jack2, rt5640_pins2,
1298                                             ARRAY_SIZE(rt5640_pins2));
1299                 if (ret)
1300                         return ret;
1301
1302                 rt5640_jack_gpio.data = priv;
1303                 rt5640_jack_gpio.gpiod_dev = priv->codec_dev;
1304                 rt5640_jack_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack1_check;
1305                 ret = snd_soc_jack_add_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1306                 if (ret)
1307                         return ret;
1308
1309                 rt5640_set_ovcd_params(component);
1310                 rt5640_jack2_gpio.data = component;
1311                 rt5640_jack2_gpio.gpiod_dev = priv->codec_dev;
1312                 rt5640_jack2_gpio.jack_status_check = byt_rt5640_hp_elitepad_1000g2_jack2_check;
1313                 ret = snd_soc_jack_add_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1314                 if (ret) {
1315                         snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1316                         return ret;
1317                 }
1318         }
1319
1320         return 0;
1321 }
1322
1323 static void byt_rt5640_exit(struct snd_soc_pcm_runtime *runtime)
1324 {
1325         struct snd_soc_card *card = runtime->card;
1326         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1327
1328         if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1329                 snd_soc_jack_free_gpios(&priv->jack2, 1, &rt5640_jack2_gpio);
1330                 snd_soc_jack_free_gpios(&priv->jack, 1, &rt5640_jack_gpio);
1331         }
1332 }
1333
1334 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1335                             struct snd_pcm_hw_params *params)
1336 {
1337         struct snd_interval *rate = hw_param_interval(params,
1338                         SNDRV_PCM_HW_PARAM_RATE);
1339         struct snd_interval *channels = hw_param_interval(params,
1340                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
1341         int ret, bits;
1342
1343         /* The DSP will covert the FE rate to 48k, stereo */
1344         rate->min = rate->max = 48000;
1345         channels->min = channels->max = 2;
1346
1347         if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1348             (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1349                 /* set SSP0 to 16-bit */
1350                 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1351                 bits = 16;
1352         } else {
1353                 /* set SSP2 to 24-bit */
1354                 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1355                 bits = 24;
1356         }
1357
1358         /*
1359          * Default mode for SSP configuration is TDM 4 slot, override config
1360          * with explicit setting to I2S 2ch. The word length is set with
1361          * dai_set_tdm_slot() since there is no other API exposed
1362          */
1363         ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
1364                                   SND_SOC_DAIFMT_I2S     |
1365                                   SND_SOC_DAIFMT_NB_NF   |
1366                                   SND_SOC_DAIFMT_CBS_CFS);
1367         if (ret < 0) {
1368                 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1369                 return ret;
1370         }
1371
1372         ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
1373         if (ret < 0) {
1374                 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1375                 return ret;
1376         }
1377
1378         return 0;
1379 }
1380
1381 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1382 {
1383         return snd_pcm_hw_constraint_single(substream->runtime,
1384                         SNDRV_PCM_HW_PARAM_RATE, 48000);
1385 }
1386
1387 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1388         .startup = byt_rt5640_aif1_startup,
1389 };
1390
1391 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1392         .hw_params = byt_rt5640_aif1_hw_params,
1393 };
1394
1395 SND_SOC_DAILINK_DEF(dummy,
1396         DAILINK_COMP_ARRAY(COMP_DUMMY()));
1397
1398 SND_SOC_DAILINK_DEF(media,
1399         DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
1400
1401 SND_SOC_DAILINK_DEF(deepbuffer,
1402         DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
1403
1404 SND_SOC_DAILINK_DEF(ssp2_port,
1405         /* overwritten for ssp0 routing */
1406         DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
1407 SND_SOC_DAILINK_DEF(ssp2_codec,
1408         DAILINK_COMP_ARRAY(COMP_CODEC(
1409         /* overwritten with HID */ "i2c-10EC5640:00",
1410         /* changed w/ quirk */  "rt5640-aif1")));
1411
1412 SND_SOC_DAILINK_DEF(platform,
1413         DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
1414
1415 static struct snd_soc_dai_link byt_rt5640_dais[] = {
1416         [MERR_DPCM_AUDIO] = {
1417                 .name = "Baytrail Audio Port",
1418                 .stream_name = "Baytrail Audio",
1419                 .nonatomic = true,
1420                 .dynamic = 1,
1421                 .dpcm_playback = 1,
1422                 .dpcm_capture = 1,
1423                 .ops = &byt_rt5640_aif1_ops,
1424                 SND_SOC_DAILINK_REG(media, dummy, platform),
1425         },
1426         [MERR_DPCM_DEEP_BUFFER] = {
1427                 .name = "Deep-Buffer Audio Port",
1428                 .stream_name = "Deep-Buffer Audio",
1429                 .nonatomic = true,
1430                 .dynamic = 1,
1431                 .dpcm_playback = 1,
1432                 .ops = &byt_rt5640_aif1_ops,
1433                 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
1434         },
1435                 /* back ends */
1436         {
1437                 .name = "SSP2-Codec",
1438                 .id = 0,
1439                 .no_pcm = 1,
1440                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1441                                                 | SND_SOC_DAIFMT_CBS_CFS,
1442                 .be_hw_params_fixup = byt_rt5640_codec_fixup,
1443                 .dpcm_playback = 1,
1444                 .dpcm_capture = 1,
1445                 .init = byt_rt5640_init,
1446                 .exit = byt_rt5640_exit,
1447                 .ops = &byt_rt5640_be_ssp2_ops,
1448                 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
1449         },
1450 };
1451
1452 /* SoC card */
1453 static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1454 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1455 static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1456 #endif
1457 static char byt_rt5640_components[64]; /* = "cfg-spk:* cfg-mic:* ..." */
1458
1459 static int byt_rt5640_suspend(struct snd_soc_card *card)
1460 {
1461         struct snd_soc_component *component;
1462
1463         if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1464                 return 0;
1465
1466         for_each_card_components(card, component) {
1467                 if (!strcmp(component->name, byt_rt5640_codec_name)) {
1468                         dev_dbg(component->dev, "disabling jack detect before suspend\n");
1469                         snd_soc_component_set_jack(component, NULL, NULL);
1470                         break;
1471                 }
1472         }
1473
1474         return 0;
1475 }
1476
1477 static int byt_rt5640_resume(struct snd_soc_card *card)
1478 {
1479         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1480         struct snd_soc_component *component;
1481
1482         if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1483                 return 0;
1484
1485         for_each_card_components(card, component) {
1486                 if (!strcmp(component->name, byt_rt5640_codec_name)) {
1487                         dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1488                         snd_soc_component_set_jack(component, &priv->jack, NULL);
1489                         break;
1490                 }
1491         }
1492
1493         return 0;
1494 }
1495
1496 /* use space before codec name to simplify card ID, and simplify driver name */
1497 #define SOF_CARD_NAME "bytcht rt5640" /* card name will be 'sof-bytcht rt5640' */
1498 #define SOF_DRIVER_NAME "SOF"
1499
1500 #define CARD_NAME "bytcr-rt5640"
1501 #define DRIVER_NAME NULL /* card name will be used for driver name */
1502
1503 static struct snd_soc_card byt_rt5640_card = {
1504         .owner = THIS_MODULE,
1505         .dai_link = byt_rt5640_dais,
1506         .num_links = ARRAY_SIZE(byt_rt5640_dais),
1507         .dapm_widgets = byt_rt5640_widgets,
1508         .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1509         .dapm_routes = byt_rt5640_audio_map,
1510         .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1511         .fully_routed = true,
1512         .suspend_pre = byt_rt5640_suspend,
1513         .resume_post = byt_rt5640_resume,
1514 };
1515
1516 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
1517         u64 aif_value;       /* 1: AIF1, 2: AIF2 */
1518         u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
1519 };
1520
1521 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1522 {
1523         struct device *dev = &pdev->dev;
1524         static const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3", "none" };
1525         __maybe_unused const char *spk_type;
1526         const struct dmi_system_id *dmi_id;
1527         const char *headset2_string = "";
1528         const char *lineout_string = "";
1529         struct byt_rt5640_private *priv;
1530         struct snd_soc_acpi_mach *mach;
1531         const char *platform_name;
1532         struct acpi_device *adev;
1533         struct device *codec_dev;
1534         bool sof_parent;
1535         int ret_val = 0;
1536         int dai_index = 0;
1537         int i, cfg_spk;
1538         int aif;
1539
1540         is_bytcr = false;
1541         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1542         if (!priv)
1543                 return -ENOMEM;
1544
1545         /* register the soc card */
1546         byt_rt5640_card.dev = &pdev->dev;
1547         mach = byt_rt5640_card.dev->platform_data;
1548         snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1549
1550         /* fix index of codec dai */
1551         for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1552                 if (!strcmp(byt_rt5640_dais[i].codecs->name,
1553                             "i2c-10EC5640:00")) {
1554                         dai_index = i;
1555                         break;
1556                 }
1557         }
1558
1559         /* fixup codec name based on HID */
1560         adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
1561         if (adev) {
1562                 snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1563                          "i2c-%s", acpi_dev_name(adev));
1564                 put_device(&adev->dev);
1565                 byt_rt5640_dais[dai_index].codecs->name = byt_rt5640_codec_name;
1566         } else {
1567                 dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id);
1568                 return -ENXIO;
1569         }
1570
1571         /*
1572          * swap SSP0 if bytcr is detected
1573          * (will be overridden if DMI quirk is detected)
1574          */
1575         if (soc_intel_is_byt()) {
1576                 if (mach->mach_params.acpi_ipc_irq_index == 0)
1577                         is_bytcr = true;
1578         }
1579
1580         if (is_bytcr) {
1581                 /*
1582                  * Baytrail CR platforms may have CHAN package in BIOS, try
1583                  * to find relevant routing quirk based as done on Windows
1584                  * platforms. We have to read the information directly from the
1585                  * BIOS, at this stage the card is not created and the links
1586                  * with the codec driver/pdata are non-existent
1587                  */
1588
1589                 struct acpi_chan_package chan_package;
1590
1591                 /* format specified: 2 64-bit integers */
1592                 struct acpi_buffer format = {sizeof("NN"), "NN"};
1593                 struct acpi_buffer state = {0, NULL};
1594                 struct snd_soc_acpi_package_context pkg_ctx;
1595                 bool pkg_found = false;
1596
1597                 state.length = sizeof(chan_package);
1598                 state.pointer = &chan_package;
1599
1600                 pkg_ctx.name = "CHAN";
1601                 pkg_ctx.length = 2;
1602                 pkg_ctx.format = &format;
1603                 pkg_ctx.state = &state;
1604                 pkg_ctx.data_valid = false;
1605
1606                 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1607                                                                &pkg_ctx);
1608                 if (pkg_found) {
1609                         if (chan_package.aif_value == 1) {
1610                                 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
1611                                 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1612                         } else  if (chan_package.aif_value == 2) {
1613                                 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
1614                                 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1615                         } else {
1616                                 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
1617                                 pkg_found = false;
1618                         }
1619                 }
1620
1621                 if (!pkg_found) {
1622                         /* no BIOS indications, assume SSP0-AIF2 connection */
1623                         byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1624                 }
1625
1626                 /* change defaults for Baytrail-CR capture */
1627                 byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1628         } else {
1629                 byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1630                                     BYT_RT5640_JD_SRC_JD2_IN4N |
1631                                     BYT_RT5640_OVCD_TH_2000UA |
1632                                     BYT_RT5640_OVCD_SF_0P75;
1633         }
1634
1635         /* check quirks before creating card */
1636         dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1637         if (dmi_id)
1638                 byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1639         if (quirk_override != -1) {
1640                 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1641                          byt_rt5640_quirk, quirk_override);
1642                 byt_rt5640_quirk = quirk_override;
1643         }
1644
1645         codec_dev = acpi_get_first_physical_node(adev);
1646         if (!codec_dev)
1647                 return -EPROBE_DEFER;
1648         priv->codec_dev = get_device(codec_dev);
1649
1650         if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2) {
1651                 acpi_dev_add_driver_gpios(ACPI_COMPANION(priv->codec_dev),
1652                                           byt_rt5640_hp_elitepad_1000g2_gpios);
1653
1654                 priv->hsmic_detect = devm_fwnode_gpiod_get(&pdev->dev, codec_dev->fwnode,
1655                                                            "headset-mic-detect", GPIOD_IN,
1656                                                            "headset-mic-detect");
1657                 if (IS_ERR(priv->hsmic_detect)) {
1658                         ret_val = PTR_ERR(priv->hsmic_detect);
1659                         dev_err_probe(&pdev->dev, ret_val, "getting hsmic-detect GPIO\n");
1660                         goto err_device;
1661                 }
1662         }
1663
1664         /* Must be called before register_card, also see declaration comment. */
1665         ret_val = byt_rt5640_add_codec_device_props(codec_dev, priv);
1666         if (ret_val)
1667                 goto err_remove_gpios;
1668
1669         log_quirks(&pdev->dev);
1670
1671         if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1672             (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1673                 byt_rt5640_dais[dai_index].codecs->dai_name = "rt5640-aif2";
1674                 aif = 2;
1675         } else {
1676                 aif = 1;
1677         }
1678
1679         if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1680             (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1681                 byt_rt5640_dais[dai_index].cpus->dai_name = "ssp0-port";
1682
1683         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1684                 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
1685                 if (IS_ERR(priv->mclk)) {
1686                         ret_val = PTR_ERR(priv->mclk);
1687
1688                         dev_err(&pdev->dev,
1689                                 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
1690                                 ret_val);
1691
1692                         /*
1693                          * Fall back to bit clock usage for -ENOENT (clock not
1694                          * available likely due to missing dependencies), bail
1695                          * for all other errors, including -EPROBE_DEFER
1696                          */
1697                         if (ret_val != -ENOENT)
1698                                 goto err;
1699                         byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1700                 }
1701         }
1702
1703         if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS) {
1704                 cfg_spk = 0;
1705                 spk_type = "none";
1706         } else if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1707                 cfg_spk = 1;
1708                 spk_type = "mono";
1709         } else {
1710                 cfg_spk = 2;
1711                 spk_type = "stereo";
1712         }
1713
1714         if (byt_rt5640_quirk & BYT_RT5640_LINEOUT) {
1715                 if (byt_rt5640_quirk & BYT_RT5640_LINEOUT_AS_HP2)
1716                         lineout_string = " cfg-hp2:lineout";
1717                 else
1718                         lineout_string = " cfg-lineout:2";
1719         }
1720
1721         if (byt_rt5640_quirk & BYT_RT5640_HSMIC2_ON_IN1)
1722                 headset2_string = " cfg-hs2:in1";
1723
1724         snprintf(byt_rt5640_components, sizeof(byt_rt5640_components),
1725                  "cfg-spk:%d cfg-mic:%s aif:%d%s%s", cfg_spk,
1726                  map_name[BYT_RT5640_MAP(byt_rt5640_quirk)], aif,
1727                  lineout_string, headset2_string);
1728         byt_rt5640_card.components = byt_rt5640_components;
1729 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1730         snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1731                  "bytcr-rt5640-%s-spk-%s-mic", spk_type,
1732                  map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1733         byt_rt5640_card.long_name = byt_rt5640_long_name;
1734 #endif
1735
1736         /* override plaform name, if required */
1737         platform_name = mach->mach_params.platform;
1738
1739         ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5640_card,
1740                                                         platform_name);
1741         if (ret_val)
1742                 goto err;
1743
1744         sof_parent = snd_soc_acpi_sof_parent(&pdev->dev);
1745
1746         /* set card and driver name */
1747         if (sof_parent) {
1748                 byt_rt5640_card.name = SOF_CARD_NAME;
1749                 byt_rt5640_card.driver_name = SOF_DRIVER_NAME;
1750         } else {
1751                 byt_rt5640_card.name = CARD_NAME;
1752                 byt_rt5640_card.driver_name = DRIVER_NAME;
1753         }
1754
1755         /* set pm ops */
1756         if (sof_parent)
1757                 dev->driver->pm = &snd_soc_pm_ops;
1758
1759         ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
1760
1761         if (ret_val) {
1762                 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
1763                         ret_val);
1764                 goto err;
1765         }
1766         platform_set_drvdata(pdev, &byt_rt5640_card);
1767         return ret_val;
1768
1769 err:
1770         device_remove_software_node(priv->codec_dev);
1771 err_remove_gpios:
1772         if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1773                 acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1774 err_device:
1775         put_device(priv->codec_dev);
1776         return ret_val;
1777 }
1778
1779 static int snd_byt_rt5640_mc_remove(struct platform_device *pdev)
1780 {
1781         struct snd_soc_card *card = platform_get_drvdata(pdev);
1782         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1783
1784         if (byt_rt5640_quirk & BYT_RT5640_JD_HP_ELITEP_1000G2)
1785                 acpi_dev_remove_driver_gpios(ACPI_COMPANION(priv->codec_dev));
1786
1787         device_remove_software_node(priv->codec_dev);
1788         put_device(priv->codec_dev);
1789         return 0;
1790 }
1791
1792 static struct platform_driver snd_byt_rt5640_mc_driver = {
1793         .driver = {
1794                 .name = "bytcr_rt5640",
1795         },
1796         .probe = snd_byt_rt5640_mc_probe,
1797         .remove = snd_byt_rt5640_mc_remove,
1798 };
1799
1800 module_platform_driver(snd_byt_rt5640_mc_driver);
1801
1802 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1803 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1804 MODULE_LICENSE("GPL v2");
1805 MODULE_ALIAS("platform:bytcr_rt5640");