OSDN Git Service

ASoC: Intel: bytcr_rt5640: Add quirk for the Lenovo Miix 3-830 tablet
[android-x86/kernel.git] / sound / soc / intel / boards / bytcr_rt5640.c
1 /*
2  *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
3  *
4  *  Copyright (C) 2014 Intel Corp
5  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
6  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  General Public License for more details.
16  *
17  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18  */
19
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/platform_device.h>
25 #include <linux/acpi.h>
26 #include <linux/clk.h>
27 #include <linux/device.h>
28 #include <linux/dmi.h>
29 #include <linux/input.h>
30 #include <linux/slab.h>
31 #include <asm/cpu_device_id.h>
32 #include <asm/platform_sst_audio.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/jack.h>
37 #include <sound/soc-acpi.h>
38 #include <dt-bindings/sound/rt5640.h>
39 #include "../../codecs/rt5640.h"
40 #include "../atom/sst-atom-controls.h"
41 #include "../common/sst-dsp.h"
42
43 enum {
44         BYT_RT5640_DMIC1_MAP,
45         BYT_RT5640_DMIC2_MAP,
46         BYT_RT5640_IN1_MAP,
47         BYT_RT5640_IN3_MAP,
48 };
49
50 enum {
51         BYT_RT5640_JD_SRC_GPIO1         = (RT5640_JD_SRC_GPIO1 << 4),
52         BYT_RT5640_JD_SRC_JD1_IN4P      = (RT5640_JD_SRC_JD1_IN4P << 4),
53         BYT_RT5640_JD_SRC_JD2_IN4N      = (RT5640_JD_SRC_JD2_IN4N << 4),
54         BYT_RT5640_JD_SRC_GPIO2         = (RT5640_JD_SRC_GPIO2 << 4),
55         BYT_RT5640_JD_SRC_GPIO3         = (RT5640_JD_SRC_GPIO3 << 4),
56         BYT_RT5640_JD_SRC_GPIO4         = (RT5640_JD_SRC_GPIO4 << 4),
57 };
58
59 enum {
60         BYT_RT5640_OVCD_TH_600UA        = (6 << 8),
61         BYT_RT5640_OVCD_TH_1500UA       = (15 << 8),
62         BYT_RT5640_OVCD_TH_2000UA       = (20 << 8),
63 };
64
65 enum {
66         BYT_RT5640_OVCD_SF_0P5          = (RT5640_OVCD_SF_0P5 << 13),
67         BYT_RT5640_OVCD_SF_0P75         = (RT5640_OVCD_SF_0P75 << 13),
68         BYT_RT5640_OVCD_SF_1P0          = (RT5640_OVCD_SF_1P0 << 13),
69         BYT_RT5640_OVCD_SF_1P5          = (RT5640_OVCD_SF_1P5 << 13),
70 };
71
72 #define BYT_RT5640_MAP(quirk)           ((quirk) &  GENMASK(3, 0))
73 #define BYT_RT5640_JDSRC(quirk)         (((quirk) & GENMASK(7, 4)) >> 4)
74 #define BYT_RT5640_OVCD_TH(quirk)       (((quirk) & GENMASK(12, 8)) >> 8)
75 #define BYT_RT5640_OVCD_SF(quirk)       (((quirk) & GENMASK(14, 13)) >> 13)
76 #define BYT_RT5640_JD_NOT_INV           BIT(16)
77 #define BYT_RT5640_MONO_SPEAKER         BIT(17)
78 #define BYT_RT5640_DIFF_MIC             BIT(18) /* default is single-ended */
79 #define BYT_RT5640_SSP2_AIF2            BIT(19) /* default is using AIF1  */
80 #define BYT_RT5640_SSP0_AIF1            BIT(20)
81 #define BYT_RT5640_SSP0_AIF2            BIT(21)
82 #define BYT_RT5640_MCLK_EN              BIT(22)
83 #define BYT_RT5640_MCLK_25MHZ           BIT(23)
84
85 #define BYTCR_INPUT_DEFAULTS                            \
86         (BYT_RT5640_IN3_MAP |                           \
87          BYT_RT5640_JD_SRC_JD1_IN4P |                   \
88          BYT_RT5640_OVCD_TH_2000UA |                    \
89          BYT_RT5640_OVCD_SF_0P75 |                      \
90          BYT_RT5640_DIFF_MIC)
91
92 /* in-diff or dmic-pin + jdsrc + ovcd-th + -sf + jd-inv + terminating entry */
93 #define MAX_NO_PROPS 6
94
95 struct byt_rt5640_private {
96         struct snd_soc_jack jack;
97         struct clk *mclk;
98 };
99 static bool is_bytcr;
100
101 static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
102 static unsigned int quirk_override;
103 module_param_named(quirk, quirk_override, uint, 0444);
104 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
105
106 static void log_quirks(struct device *dev)
107 {
108         int map;
109         bool has_mclk = false;
110         bool has_ssp0 = false;
111         bool has_ssp0_aif1 = false;
112         bool has_ssp0_aif2 = false;
113         bool has_ssp2_aif2 = false;
114
115         map = BYT_RT5640_MAP(byt_rt5640_quirk);
116         switch (map) {
117         case BYT_RT5640_DMIC1_MAP:
118                 dev_info(dev, "quirk DMIC1_MAP enabled\n");
119                 break;
120         case BYT_RT5640_DMIC2_MAP:
121                 dev_info(dev, "quirk DMIC2_MAP enabled\n");
122                 break;
123         case BYT_RT5640_IN1_MAP:
124                 dev_info(dev, "quirk IN1_MAP enabled\n");
125                 break;
126         case BYT_RT5640_IN3_MAP:
127                 dev_info(dev, "quirk IN3_MAP enabled\n");
128                 break;
129         default:
130                 dev_err(dev, "quirk map 0x%x is not supported, microphone input will not work\n", map);
131                 break;
132         }
133         if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
134                 dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
135                          BYT_RT5640_JDSRC(byt_rt5640_quirk));
136                 dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
137                          BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
138                 dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
139                          BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
140         }
141         if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
142                 dev_info(dev, "quirk JD_NOT_INV enabled\n");
143         if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
144                 dev_info(dev, "quirk MONO_SPEAKER enabled\n");
145         if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
146                 dev_info(dev, "quirk DIFF_MIC enabled\n");
147         if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
148                 dev_info(dev, "quirk SSP0_AIF1 enabled\n");
149                 has_ssp0 = true;
150                 has_ssp0_aif1 = true;
151         }
152         if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
153                 dev_info(dev, "quirk SSP0_AIF2 enabled\n");
154                 has_ssp0 = true;
155                 has_ssp0_aif2 = true;
156         }
157         if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
158                 dev_info(dev, "quirk SSP2_AIF2 enabled\n");
159                 has_ssp2_aif2 = true;
160         }
161         if (is_bytcr && !has_ssp0)
162                 dev_err(dev, "Invalid routing, bytcr detected but no SSP0-based quirk, audio cannot work with SSP2 on bytcr\n");
163         if (has_ssp0_aif1 && has_ssp0_aif2)
164                 dev_err(dev, "Invalid routing, SSP0 cannot be connected to both AIF1 and AIF2\n");
165         if (has_ssp0 && has_ssp2_aif2)
166                 dev_err(dev, "Invalid routing, cannot have both SSP0 and SSP2 connected to codec\n");
167
168         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
169                 dev_info(dev, "quirk MCLK_EN enabled\n");
170                 has_mclk = true;
171         }
172         if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
173                 if (has_mclk)
174                         dev_info(dev, "quirk MCLK_25MHZ enabled\n");
175                 else
176                         dev_err(dev, "quirk MCLK_25MHZ enabled but quirk MCLK not selected, will be ignored\n");
177         }
178 }
179
180 static int byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
181                                               int rate)
182 {
183         int ret;
184
185         /* Configure the PLL before selecting it */
186         if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
187                 /* use bitclock as PLL input */
188                 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
189                     (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
190                         /* 2x16 bit slots on SSP0 */
191                         ret = snd_soc_dai_set_pll(codec_dai, 0,
192                                                   RT5640_PLL1_S_BCLK1,
193                                                   rate * 32, rate * 512);
194                 } else {
195                         /* 2x15 bit slots on SSP2 */
196                         ret = snd_soc_dai_set_pll(codec_dai, 0,
197                                                   RT5640_PLL1_S_BCLK1,
198                                                   rate * 50, rate * 512);
199                 }
200         } else {
201                 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
202                         ret = snd_soc_dai_set_pll(codec_dai, 0,
203                                                   RT5640_PLL1_S_MCLK,
204                                                   25000000, rate * 512);
205                 } else {
206                         ret = snd_soc_dai_set_pll(codec_dai, 0,
207                                                   RT5640_PLL1_S_MCLK,
208                                                   19200000, rate * 512);
209                 }
210         }
211
212         if (ret < 0) {
213                 dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
214                 return ret;
215         }
216
217         ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
218                                      rate * 512, SND_SOC_CLOCK_IN);
219         if (ret < 0) {
220                 dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
221                 return ret;
222         }
223
224         return 0;
225 }
226
227 #define BYT_CODEC_DAI1  "rt5640-aif1"
228 #define BYT_CODEC_DAI2  "rt5640-aif2"
229
230 static int platform_clock_control(struct snd_soc_dapm_widget *w,
231                                   struct snd_kcontrol *k, int  event)
232 {
233         struct snd_soc_dapm_context *dapm = w->dapm;
234         struct snd_soc_card *card = dapm->card;
235         struct snd_soc_dai *codec_dai;
236         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
237         int ret;
238
239         codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
240         if (!codec_dai)
241                 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
242
243         if (!codec_dai) {
244                 dev_err(card->dev,
245                         "Codec dai not found; Unable to set platform clock\n");
246                 return -EIO;
247         }
248
249         if (SND_SOC_DAPM_EVENT_ON(event)) {
250                 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
251                         ret = clk_prepare_enable(priv->mclk);
252                         if (ret < 0) {
253                                 dev_err(card->dev,
254                                         "could not configure MCLK state\n");
255                                 return ret;
256                         }
257                 }
258                 ret = byt_rt5640_prepare_and_enable_pll1(codec_dai, 48000);
259         } else {
260                 /*
261                  * Set codec clock source to internal clock before
262                  * turning off the platform clock. Codec needs clock
263                  * for Jack detection and button press
264                  */
265                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
266                                              48000 * 512,
267                                              SND_SOC_CLOCK_IN);
268                 if (!ret) {
269                         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
270                                 clk_disable_unprepare(priv->mclk);
271                 }
272         }
273
274         if (ret < 0) {
275                 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
276                 return ret;
277         }
278
279         return 0;
280 }
281
282 static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
283         SND_SOC_DAPM_HP("Headphone", NULL),
284         SND_SOC_DAPM_MIC("Headset Mic", NULL),
285         SND_SOC_DAPM_MIC("Internal Mic", NULL),
286         SND_SOC_DAPM_SPK("Speaker", NULL),
287         SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
288                             platform_clock_control, SND_SOC_DAPM_PRE_PMU |
289                             SND_SOC_DAPM_POST_PMD),
290
291 };
292
293 static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
294         {"Headphone", NULL, "Platform Clock"},
295         {"Headset Mic", NULL, "Platform Clock"},
296         {"Internal Mic", NULL, "Platform Clock"},
297         {"Speaker", NULL, "Platform Clock"},
298
299         {"Headset Mic", NULL, "MICBIAS1"},
300         {"IN2P", NULL, "Headset Mic"},
301         {"Headphone", NULL, "HPOL"},
302         {"Headphone", NULL, "HPOR"},
303 };
304
305 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
306         {"DMIC1", NULL, "Internal Mic"},
307 };
308
309 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
310         {"DMIC2", NULL, "Internal Mic"},
311 };
312
313 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
314         {"Internal Mic", NULL, "MICBIAS1"},
315         {"IN1P", NULL, "Internal Mic"},
316 };
317
318 static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
319         {"Internal Mic", NULL, "MICBIAS1"},
320         {"IN3P", NULL, "Internal Mic"},
321 };
322
323 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
324         {"ssp2 Tx", NULL, "codec_out0"},
325         {"ssp2 Tx", NULL, "codec_out1"},
326         {"codec_in0", NULL, "ssp2 Rx"},
327         {"codec_in1", NULL, "ssp2 Rx"},
328
329         {"AIF1 Playback", NULL, "ssp2 Tx"},
330         {"ssp2 Rx", NULL, "AIF1 Capture"},
331 };
332
333 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
334         {"ssp2 Tx", NULL, "codec_out0"},
335         {"ssp2 Tx", NULL, "codec_out1"},
336         {"codec_in0", NULL, "ssp2 Rx"},
337         {"codec_in1", NULL, "ssp2 Rx"},
338
339         {"AIF2 Playback", NULL, "ssp2 Tx"},
340         {"ssp2 Rx", NULL, "AIF2 Capture"},
341 };
342
343 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
344         {"ssp0 Tx", NULL, "modem_out"},
345         {"modem_in", NULL, "ssp0 Rx"},
346
347         {"AIF1 Playback", NULL, "ssp0 Tx"},
348         {"ssp0 Rx", NULL, "AIF1 Capture"},
349 };
350
351 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
352         {"ssp0 Tx", NULL, "modem_out"},
353         {"modem_in", NULL, "ssp0 Rx"},
354
355         {"AIF2 Playback", NULL, "ssp0 Tx"},
356         {"ssp0 Rx", NULL, "AIF2 Capture"},
357 };
358
359 static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
360         {"Speaker", NULL, "SPOLP"},
361         {"Speaker", NULL, "SPOLN"},
362         {"Speaker", NULL, "SPORP"},
363         {"Speaker", NULL, "SPORN"},
364 };
365
366 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
367         {"Speaker", NULL, "SPOLP"},
368         {"Speaker", NULL, "SPOLN"},
369 };
370
371 static const struct snd_kcontrol_new byt_rt5640_controls[] = {
372         SOC_DAPM_PIN_SWITCH("Headphone"),
373         SOC_DAPM_PIN_SWITCH("Headset Mic"),
374         SOC_DAPM_PIN_SWITCH("Internal Mic"),
375         SOC_DAPM_PIN_SWITCH("Speaker"),
376 };
377
378 static struct snd_soc_jack_pin rt5640_pins[] = {
379         {
380                 .pin    = "Headphone",
381                 .mask   = SND_JACK_HEADPHONE,
382         },
383         {
384                 .pin    = "Headset Mic",
385                 .mask   = SND_JACK_MICROPHONE,
386         },
387 };
388
389 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
390                                         struct snd_pcm_hw_params *params)
391 {
392         struct snd_soc_pcm_runtime *rtd = substream->private_data;
393         struct snd_soc_dai *dai = rtd->codec_dai;
394
395         return byt_rt5640_prepare_and_enable_pll1(dai, params_rate(params));
396 }
397
398 /* Please keep this list alphabetically sorted */
399 static const struct dmi_system_id byt_rt5640_quirk_table[] = {
400         {       /* Acer Iconia Tab 8 W1-810 */
401                 .matches = {
402                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
403                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
404                 },
405                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
406                                         BYT_RT5640_JD_SRC_JD1_IN4P |
407                                         BYT_RT5640_OVCD_TH_1500UA |
408                                         BYT_RT5640_OVCD_SF_0P75 |
409                                         BYT_RT5640_SSP0_AIF1 |
410                                         BYT_RT5640_MCLK_EN),
411         },
412         {       /* Acer One 10 S1002 */
413                 .matches = {
414                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
415                         DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
416                 },
417                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
418                                         BYT_RT5640_JD_SRC_JD2_IN4N |
419                                         BYT_RT5640_OVCD_TH_2000UA |
420                                         BYT_RT5640_OVCD_SF_0P75 |
421                                         BYT_RT5640_DIFF_MIC |
422                                         BYT_RT5640_SSP0_AIF2 |
423                                         BYT_RT5640_MCLK_EN),
424         },
425         {
426                 .matches = {
427                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
428                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
429                 },
430                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
431                                         BYT_RT5640_JD_SRC_JD2_IN4N |
432                                         BYT_RT5640_OVCD_TH_2000UA |
433                                         BYT_RT5640_OVCD_SF_0P75 |
434                                         BYT_RT5640_SSP0_AIF1 |
435                                         BYT_RT5640_MCLK_EN),
436         },
437         {
438                 .matches = {
439                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
440                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
441                 },
442                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
443                                         BYT_RT5640_MONO_SPEAKER |
444                                         BYT_RT5640_SSP0_AIF1 |
445                                         BYT_RT5640_MCLK_EN),
446         },
447         {
448                 .matches = {
449                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
450                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
451                 },
452                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
453                                         BYT_RT5640_JD_SRC_JD2_IN4N |
454                                         BYT_RT5640_OVCD_TH_2000UA |
455                                         BYT_RT5640_OVCD_SF_0P75 |
456                                         BYT_RT5640_SSP0_AIF1 |
457                                         BYT_RT5640_MCLK_EN),
458         },
459         {
460                 .matches = {
461                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
462                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
463                 },
464                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
465                                         BYT_RT5640_JD_SRC_JD2_IN4N |
466                                         BYT_RT5640_OVCD_TH_2000UA |
467                                         BYT_RT5640_OVCD_SF_0P75 |
468                                         BYT_RT5640_MCLK_EN),
469         },
470         {
471                 .matches = {
472                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
473                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
474                 },
475                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
476                                         BYT_RT5640_JD_SRC_JD2_IN4N |
477                                         BYT_RT5640_OVCD_TH_2000UA |
478                                         BYT_RT5640_OVCD_SF_0P75 |
479                                         BYT_RT5640_MONO_SPEAKER |
480                                         BYT_RT5640_DIFF_MIC |
481                                         BYT_RT5640_SSP0_AIF2 |
482                                         BYT_RT5640_MCLK_EN),
483         },
484         {       /* Chuwi Vi8 (CWI506) */
485                 .matches = {
486                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
487                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
488                         /* The above are too generic, also match BIOS info */
489                         DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
490                 },
491                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
492                                         BYT_RT5640_MONO_SPEAKER |
493                                         BYT_RT5640_SSP0_AIF1 |
494                                         BYT_RT5640_MCLK_EN),
495         },
496         {
497                 /* Chuwi Vi10 (CWI505) */
498                 .matches = {
499                         DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
500                         DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
501                         DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
502                         DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
503                 },
504                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
505                                         BYT_RT5640_JD_SRC_JD2_IN4N |
506                                         BYT_RT5640_OVCD_TH_2000UA |
507                                         BYT_RT5640_OVCD_SF_0P75 |
508                                         BYT_RT5640_DIFF_MIC |
509                                         BYT_RT5640_SSP0_AIF1 |
510                                         BYT_RT5640_MCLK_EN),
511         },
512         {
513                 /* Chuwi Hi8 (CWI509) */
514                 .matches = {
515                         DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
516                         DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
517                         DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
518                         DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
519                 },
520                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
521                                         BYT_RT5640_JD_SRC_JD2_IN4N |
522                                         BYT_RT5640_OVCD_TH_2000UA |
523                                         BYT_RT5640_OVCD_SF_0P75 |
524                                         BYT_RT5640_MONO_SPEAKER |
525                                         BYT_RT5640_DIFF_MIC |
526                                         BYT_RT5640_SSP0_AIF1 |
527                                         BYT_RT5640_MCLK_EN),
528         },
529         {
530                 .matches = {
531                         DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
532                         DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
533                 },
534                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
535         },
536         {       /* Connect Tablet 9 */
537                 .matches = {
538                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
539                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
540                 },
541                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
542                                         BYT_RT5640_MONO_SPEAKER |
543                                         BYT_RT5640_SSP0_AIF1 |
544                                         BYT_RT5640_MCLK_EN),
545         },
546         {
547                 .matches = {
548                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
549                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
550                 },
551                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
552                                         BYT_RT5640_JD_SRC_JD2_IN4N |
553                                         BYT_RT5640_OVCD_TH_2000UA |
554                                         BYT_RT5640_OVCD_SF_0P75 |
555                                         BYT_RT5640_MONO_SPEAKER |
556                                         BYT_RT5640_MCLK_EN),
557         },
558         {       /* Estar Beauty HD MID 7316R */
559                 .matches = {
560                         DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
561                         DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
562                 },
563                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
564                                         BYT_RT5640_MONO_SPEAKER |
565                                         BYT_RT5640_SSP0_AIF1 |
566                                         BYT_RT5640_MCLK_EN),
567         },
568         {       /* Glavey TM800A550L */
569                 .matches = {
570                         DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
571                         DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
572                         /* Above strings are too generic, also match on BIOS version */
573                         DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
574                 },
575                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
576                                         BYT_RT5640_SSP0_AIF1 |
577                                         BYT_RT5640_MCLK_EN),
578         },
579         {
580                 .matches = {
581                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
582                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
583                 },
584                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
585                                         BYT_RT5640_MCLK_EN),
586         },
587         {       /* HP Pavilion x2 10-n000nd */
588                 .matches = {
589                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
590                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
591                 },
592                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
593                                         BYT_RT5640_JD_SRC_JD2_IN4N |
594                                         BYT_RT5640_OVCD_TH_1500UA |
595                                         BYT_RT5640_OVCD_SF_0P75 |
596                                         BYT_RT5640_SSP0_AIF1 |
597                                         BYT_RT5640_MCLK_EN),
598         },
599         {       /* HP Stream 7 */
600                 .matches = {
601                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
602                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
603                 },
604                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
605                                         BYT_RT5640_MONO_SPEAKER |
606                                         BYT_RT5640_JD_NOT_INV |
607                                         BYT_RT5640_SSP0_AIF1 |
608                                         BYT_RT5640_MCLK_EN),
609         },
610         {       /* I.T.Works TW891 */
611                 .matches = {
612                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
613                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
614                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
615                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
616                 },
617                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
618                                         BYT_RT5640_MONO_SPEAKER |
619                                         BYT_RT5640_SSP0_AIF1 |
620                                         BYT_RT5640_MCLK_EN),
621         },
622         {       /* Lamina I8270 / T701BR.SE */
623                 .matches = {
624                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
625                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
626                 },
627                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
628                                         BYT_RT5640_MONO_SPEAKER |
629                                         BYT_RT5640_JD_NOT_INV |
630                                         BYT_RT5640_SSP0_AIF1 |
631                                         BYT_RT5640_MCLK_EN),
632         },
633         {       /* Lenovo Miix 2 8 */
634                 .matches = {
635                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
636                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
637                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
638                 },
639                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
640                                         BYT_RT5640_JD_SRC_JD2_IN4N |
641                                         BYT_RT5640_OVCD_TH_2000UA |
642                                         BYT_RT5640_OVCD_SF_0P75 |
643                                         BYT_RT5640_MONO_SPEAKER |
644                                         BYT_RT5640_MCLK_EN),
645         },
646         {       /* Lenovo Miix 3-830 */
647                 .matches = {
648                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
649                         DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
650                 },
651                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
652                                         BYT_RT5640_JD_SRC_JD2_IN4N |
653                                         BYT_RT5640_OVCD_TH_2000UA |
654                                         BYT_RT5640_OVCD_SF_0P75 |
655                                         BYT_RT5640_MONO_SPEAKER |
656                                         BYT_RT5640_DIFF_MIC |
657                                         BYT_RT5640_SSP0_AIF1 |
658                                         BYT_RT5640_MCLK_EN),
659         },
660         {       /* Linx Linx7 tablet */
661                 .matches = {
662                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
663                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
664                 },
665                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
666                                         BYT_RT5640_MONO_SPEAKER |
667                                         BYT_RT5640_JD_NOT_INV |
668                                         BYT_RT5640_SSP0_AIF1 |
669                                         BYT_RT5640_MCLK_EN),
670         },
671         {       /* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
672                 .matches = {
673                         DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
674                         DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
675                 },
676                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
677                                         BYT_RT5640_MONO_SPEAKER |
678                                         BYT_RT5640_SSP0_AIF1 |
679                                         BYT_RT5640_MCLK_EN),
680         },
681         {
682                 /* MPMAN MPWIN895CL */
683                 .matches = {
684                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
685                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
686                 },
687                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
688                                         BYT_RT5640_MONO_SPEAKER |
689                                         BYT_RT5640_SSP0_AIF1 |
690                                         BYT_RT5640_MCLK_EN),
691         },
692         {       /* MSI S100 tablet */
693                 .matches = {
694                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
695                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
696                 },
697                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
698                                         BYT_RT5640_JD_SRC_JD2_IN4N |
699                                         BYT_RT5640_OVCD_TH_2000UA |
700                                         BYT_RT5640_OVCD_SF_0P75 |
701                                         BYT_RT5640_MONO_SPEAKER |
702                                         BYT_RT5640_DIFF_MIC |
703                                         BYT_RT5640_MCLK_EN),
704         },
705         {       /* Nuvison/TMax TM800W560 */
706                 .matches = {
707                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
708                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
709                 },
710                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
711                                         BYT_RT5640_JD_SRC_JD2_IN4N |
712                                         BYT_RT5640_OVCD_TH_2000UA |
713                                         BYT_RT5640_OVCD_SF_0P75 |
714                                         BYT_RT5640_JD_NOT_INV |
715                                         BYT_RT5640_DIFF_MIC |
716                                         BYT_RT5640_SSP0_AIF1 |
717                                         BYT_RT5640_MCLK_EN),
718         },
719         {       /* Onda v975w */
720                 .matches = {
721                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
722                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
723                         /* The above are too generic, also match BIOS info */
724                         DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
725                         DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
726                 },
727                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
728                                         BYT_RT5640_JD_SRC_JD2_IN4N |
729                                         BYT_RT5640_OVCD_TH_2000UA |
730                                         BYT_RT5640_OVCD_SF_0P75 |
731                                         BYT_RT5640_DIFF_MIC |
732                                         BYT_RT5640_MCLK_EN),
733         },
734         {       /* Pipo W4 */
735                 .matches = {
736                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
737                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
738                         /* The above are too generic, also match BIOS info */
739                         DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
740                 },
741                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
742                                         BYT_RT5640_MONO_SPEAKER |
743                                         BYT_RT5640_SSP0_AIF1 |
744                                         BYT_RT5640_MCLK_EN),
745         },
746         {       /* Point of View Mobii TAB-P800W (V2.0) */
747                 .matches = {
748                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
749                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
750                         /* The above are too generic, also match BIOS info */
751                         DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
752                         DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
753                 },
754                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
755                                         BYT_RT5640_JD_SRC_JD2_IN4N |
756                                         BYT_RT5640_OVCD_TH_2000UA |
757                                         BYT_RT5640_OVCD_SF_0P75 |
758                                         BYT_RT5640_MONO_SPEAKER |
759                                         BYT_RT5640_DIFF_MIC |
760                                         BYT_RT5640_SSP0_AIF2 |
761                                         BYT_RT5640_MCLK_EN),
762         },
763         {       /* Point of View Mobii TAB-P800W (V2.1) */
764                 .matches = {
765                         DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
766                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
767                         /* The above are too generic, also match BIOS info */
768                         DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
769                         DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
770                 },
771                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
772                                         BYT_RT5640_JD_SRC_JD2_IN4N |
773                                         BYT_RT5640_OVCD_TH_2000UA |
774                                         BYT_RT5640_OVCD_SF_0P75 |
775                                         BYT_RT5640_MONO_SPEAKER |
776                                         BYT_RT5640_DIFF_MIC |
777                                         BYT_RT5640_SSP0_AIF2 |
778                                         BYT_RT5640_MCLK_EN),
779         },
780         {
781                 /* Teclast X89 */
782                 .matches = {
783                         DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
784                         DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
785                 },
786                 .driver_data = (void *)(BYT_RT5640_IN3_MAP |
787                                         BYT_RT5640_JD_SRC_JD1_IN4P |
788                                         BYT_RT5640_OVCD_TH_2000UA |
789                                         BYT_RT5640_OVCD_SF_1P0 |
790                                         BYT_RT5640_SSP0_AIF1 |
791                                         BYT_RT5640_MCLK_EN),
792         },
793         {       /* Toshiba Satellite Click Mini L9W-B */
794                 .matches = {
795                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
796                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
797                 },
798                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
799                                         BYT_RT5640_JD_SRC_JD2_IN4N |
800                                         BYT_RT5640_OVCD_TH_1500UA |
801                                         BYT_RT5640_OVCD_SF_0P75 |
802                                         BYT_RT5640_SSP0_AIF1 |
803                                         BYT_RT5640_MCLK_EN),
804         },
805         {       /* Toshiba Encore WT8-A */
806                 .matches = {
807                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
808                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
809                 },
810                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
811                                         BYT_RT5640_JD_SRC_JD2_IN4N |
812                                         BYT_RT5640_OVCD_TH_2000UA |
813                                         BYT_RT5640_OVCD_SF_0P75 |
814                                         BYT_RT5640_JD_NOT_INV |
815                                         BYT_RT5640_MCLK_EN),
816         },
817         {       /* Toshiba Encore WT10-A */
818                 .matches = {
819                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
820                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
821                 },
822                 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
823                                         BYT_RT5640_JD_SRC_JD1_IN4P |
824                                         BYT_RT5640_OVCD_TH_2000UA |
825                                         BYT_RT5640_OVCD_SF_0P75 |
826                                         BYT_RT5640_SSP0_AIF2 |
827                                         BYT_RT5640_MCLK_EN),
828         },
829         {       /* Voyo Winpad A15 */
830                 .matches = {
831                         DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
832                         DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
833                         /* Above strings are too generic, also match on BIOS date */
834                         DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
835                 },
836                 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
837                                         BYT_RT5640_JD_SRC_JD2_IN4N |
838                                         BYT_RT5640_OVCD_TH_2000UA |
839                                         BYT_RT5640_OVCD_SF_0P75 |
840                                         BYT_RT5640_DIFF_MIC |
841                                         BYT_RT5640_MCLK_EN),
842         },
843         {       /* Catch-all for generic Insyde tablets, must be last */
844                 .matches = {
845                         DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
846                 },
847                 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
848                                         BYT_RT5640_MCLK_EN |
849                                         BYT_RT5640_SSP0_AIF1),
850
851         },
852         {}
853 };
854
855 /*
856  * Note this MUST be called before snd_soc_register_card(), so that the props
857  * are in place before the codec component driver's probe function parses them.
858  */
859 static int byt_rt5640_add_codec_device_props(const char *i2c_dev_name)
860 {
861         struct property_entry props[MAX_NO_PROPS] = {};
862         struct device *i2c_dev;
863         int ret, cnt = 0;
864
865         i2c_dev = bus_find_device_by_name(&i2c_bus_type, NULL, i2c_dev_name);
866         if (!i2c_dev)
867                 return -EPROBE_DEFER;
868
869         switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
870         case BYT_RT5640_DMIC1_MAP:
871                 props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
872                                                   RT5640_DMIC1_DATA_PIN_IN1P);
873                 break;
874         case BYT_RT5640_DMIC2_MAP:
875                 props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
876                                                   RT5640_DMIC2_DATA_PIN_IN1N);
877                 break;
878         case BYT_RT5640_IN1_MAP:
879                 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
880                         props[cnt++] =
881                                 PROPERTY_ENTRY_BOOL("realtek,in1-differential");
882                 break;
883         case BYT_RT5640_IN3_MAP:
884                 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
885                         props[cnt++] =
886                                 PROPERTY_ENTRY_BOOL("realtek,in3-differential");
887                 break;
888         }
889
890         if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
891                 props[cnt++] = PROPERTY_ENTRY_U32(
892                                     "realtek,jack-detect-source",
893                                     BYT_RT5640_JDSRC(byt_rt5640_quirk));
894
895                 props[cnt++] = PROPERTY_ENTRY_U32(
896                                     "realtek,over-current-threshold-microamp",
897                                     BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
898
899                 props[cnt++] = PROPERTY_ENTRY_U32(
900                                     "realtek,over-current-scale-factor",
901                                     BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
902         }
903
904         if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
905                 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
906
907         ret = device_add_properties(i2c_dev, props);
908         put_device(i2c_dev);
909
910         return ret;
911 }
912
913 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
914 {
915         struct snd_soc_card *card = runtime->card;
916         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
917         struct snd_soc_component *component = runtime->codec_dai->component;
918         const struct snd_soc_dapm_route *custom_map;
919         int num_routes;
920         int ret;
921
922         card->dapm.idle_bias_off = true;
923
924         /* Start with RC clk for jack-detect (we disable MCLK below) */
925         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
926                 snd_soc_component_update_bits(component, RT5640_GLB_CLK,
927                         RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
928
929         rt5640_sel_asrc_clk_src(component,
930                                 RT5640_DA_STEREO_FILTER |
931                                 RT5640_DA_MONO_L_FILTER |
932                                 RT5640_DA_MONO_R_FILTER |
933                                 RT5640_AD_STEREO_FILTER |
934                                 RT5640_AD_MONO_L_FILTER |
935                                 RT5640_AD_MONO_R_FILTER,
936                                 RT5640_CLK_SEL_ASRC);
937
938         ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
939                                         ARRAY_SIZE(byt_rt5640_controls));
940         if (ret) {
941                 dev_err(card->dev, "unable to add card controls\n");
942                 return ret;
943         }
944
945         switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
946         case BYT_RT5640_IN1_MAP:
947                 custom_map = byt_rt5640_intmic_in1_map;
948                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
949                 break;
950         case BYT_RT5640_IN3_MAP:
951                 custom_map = byt_rt5640_intmic_in3_map;
952                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
953                 break;
954         case BYT_RT5640_DMIC2_MAP:
955                 custom_map = byt_rt5640_intmic_dmic2_map;
956                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
957                 break;
958         default:
959                 custom_map = byt_rt5640_intmic_dmic1_map;
960                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
961         }
962
963         ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
964         if (ret)
965                 return ret;
966
967         if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
968                 ret = snd_soc_dapm_add_routes(&card->dapm,
969                                         byt_rt5640_ssp2_aif2_map,
970                                         ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
971         } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
972                 ret = snd_soc_dapm_add_routes(&card->dapm,
973                                         byt_rt5640_ssp0_aif1_map,
974                                         ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
975         } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
976                 ret = snd_soc_dapm_add_routes(&card->dapm,
977                                         byt_rt5640_ssp0_aif2_map,
978                                         ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
979         } else {
980                 ret = snd_soc_dapm_add_routes(&card->dapm,
981                                         byt_rt5640_ssp2_aif1_map,
982                                         ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
983         }
984         if (ret)
985                 return ret;
986
987         if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
988                 ret = snd_soc_dapm_add_routes(&card->dapm,
989                                         byt_rt5640_mono_spk_map,
990                                         ARRAY_SIZE(byt_rt5640_mono_spk_map));
991         } else {
992                 ret = snd_soc_dapm_add_routes(&card->dapm,
993                                         byt_rt5640_stereo_spk_map,
994                                         ARRAY_SIZE(byt_rt5640_stereo_spk_map));
995         }
996         if (ret)
997                 return ret;
998
999         snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
1000         snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
1001
1002         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1003                 /*
1004                  * The firmware might enable the clock at
1005                  * boot (this information may or may not
1006                  * be reflected in the enable clock register).
1007                  * To change the rate we must disable the clock
1008                  * first to cover these cases. Due to common
1009                  * clock framework restrictions that do not allow
1010                  * to disable a clock that has not been enabled,
1011                  * we need to enable the clock first.
1012                  */
1013                 ret = clk_prepare_enable(priv->mclk);
1014                 if (!ret)
1015                         clk_disable_unprepare(priv->mclk);
1016
1017                 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1018                         ret = clk_set_rate(priv->mclk, 25000000);
1019                 else
1020                         ret = clk_set_rate(priv->mclk, 19200000);
1021
1022                 if (ret) {
1023                         dev_err(card->dev, "unable to set MCLK rate\n");
1024                         return ret;
1025                 }
1026         }
1027
1028         if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1029                 ret = snd_soc_card_jack_new(card, "Headset",
1030                                             SND_JACK_HEADSET | SND_JACK_BTN_0,
1031                                             &priv->jack, rt5640_pins,
1032                                             ARRAY_SIZE(rt5640_pins));
1033                 if (ret) {
1034                         dev_err(card->dev, "Jack creation failed %d\n", ret);
1035                         return ret;
1036                 }
1037                 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1038                                  KEY_PLAYPAUSE);
1039                 snd_soc_component_set_jack(component, &priv->jack, NULL);
1040         }
1041
1042         return 0;
1043 }
1044
1045 static const struct snd_soc_pcm_stream byt_rt5640_dai_params = {
1046         .formats = SNDRV_PCM_FMTBIT_S24_LE,
1047         .rate_min = 48000,
1048         .rate_max = 48000,
1049         .channels_min = 2,
1050         .channels_max = 2,
1051 };
1052
1053 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1054                             struct snd_pcm_hw_params *params)
1055 {
1056         struct snd_interval *rate = hw_param_interval(params,
1057                         SNDRV_PCM_HW_PARAM_RATE);
1058         struct snd_interval *channels = hw_param_interval(params,
1059                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
1060         int ret;
1061
1062         /* The DSP will covert the FE rate to 48k, stereo */
1063         rate->min = rate->max = 48000;
1064         channels->min = channels->max = 2;
1065
1066         if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1067                 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1068
1069                 /* set SSP0 to 16-bit */
1070                 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1071
1072                 /*
1073                  * Default mode for SSP configuration is TDM 4 slot, override config
1074                  * with explicit setting to I2S 2ch 16-bit. The word length is set with
1075                  * dai_set_tdm_slot() since there is no other API exposed
1076                  */
1077                 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
1078                                         SND_SOC_DAIFMT_I2S     |
1079                                         SND_SOC_DAIFMT_NB_NF   |
1080                                         SND_SOC_DAIFMT_CBS_CFS
1081                         );
1082                 if (ret < 0) {
1083                         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1084                         return ret;
1085                 }
1086
1087                 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
1088                 if (ret < 0) {
1089                         dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1090                         return ret;
1091                 }
1092
1093         } else {
1094
1095                 /* set SSP2 to 24-bit */
1096                 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1097
1098                 /*
1099                  * Default mode for SSP configuration is TDM 4 slot, override config
1100                  * with explicit setting to I2S 2ch 24-bit. The word length is set with
1101                  * dai_set_tdm_slot() since there is no other API exposed
1102                  */
1103                 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
1104                                         SND_SOC_DAIFMT_I2S     |
1105                                         SND_SOC_DAIFMT_NB_NF   |
1106                                         SND_SOC_DAIFMT_CBS_CFS
1107                         );
1108                 if (ret < 0) {
1109                         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1110                         return ret;
1111                 }
1112
1113                 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
1114                 if (ret < 0) {
1115                         dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1116                         return ret;
1117                 }
1118         }
1119         return 0;
1120 }
1121
1122 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1123 {
1124         return snd_pcm_hw_constraint_single(substream->runtime,
1125                         SNDRV_PCM_HW_PARAM_RATE, 48000);
1126 }
1127
1128 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1129         .startup = byt_rt5640_aif1_startup,
1130 };
1131
1132 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1133         .hw_params = byt_rt5640_aif1_hw_params,
1134 };
1135
1136 static struct snd_soc_dai_link byt_rt5640_dais[] = {
1137         [MERR_DPCM_AUDIO] = {
1138                 .name = "Baytrail Audio Port",
1139                 .stream_name = "Baytrail Audio",
1140                 .cpu_dai_name = "media-cpu-dai",
1141                 .codec_dai_name = "snd-soc-dummy-dai",
1142                 .codec_name = "snd-soc-dummy",
1143                 .platform_name = "sst-mfld-platform",
1144                 .nonatomic = true,
1145                 .dynamic = 1,
1146                 .dpcm_playback = 1,
1147                 .dpcm_capture = 1,
1148                 .ops = &byt_rt5640_aif1_ops,
1149         },
1150         [MERR_DPCM_DEEP_BUFFER] = {
1151                 .name = "Deep-Buffer Audio Port",
1152                 .stream_name = "Deep-Buffer Audio",
1153                 .cpu_dai_name = "deepbuffer-cpu-dai",
1154                 .codec_dai_name = "snd-soc-dummy-dai",
1155                 .codec_name = "snd-soc-dummy",
1156                 .platform_name = "sst-mfld-platform",
1157                 .nonatomic = true,
1158                 .dynamic = 1,
1159                 .dpcm_playback = 1,
1160                 .ops = &byt_rt5640_aif1_ops,
1161         },
1162                 /* back ends */
1163         {
1164                 .name = "SSP2-Codec",
1165                 .id = 0,
1166                 .cpu_dai_name = "ssp2-port", /* overwritten for ssp0 routing */
1167                 .platform_name = "sst-mfld-platform",
1168                 .no_pcm = 1,
1169                 .codec_dai_name = "rt5640-aif1", /* changed w/ quirk */
1170                 .codec_name = "i2c-10EC5640:00", /* overwritten with HID */
1171                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1172                                                 | SND_SOC_DAIFMT_CBS_CFS,
1173                 .be_hw_params_fixup = byt_rt5640_codec_fixup,
1174                 .ignore_suspend = 1,
1175                 .nonatomic = true,
1176                 .dpcm_playback = 1,
1177                 .dpcm_capture = 1,
1178                 .init = byt_rt5640_init,
1179                 .ops = &byt_rt5640_be_ssp2_ops,
1180         },
1181 };
1182
1183 /* SoC card */
1184 static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1185 static char byt_rt5640_codec_aif_name[12]; /*  = "rt5640-aif[1|2]" */
1186 static char byt_rt5640_cpu_dai_name[10]; /*  = "ssp[0|2]-port" */
1187 static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1188
1189 static int byt_rt5640_suspend(struct snd_soc_card *card)
1190 {
1191         struct snd_soc_component *component;
1192
1193         if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1194                 return 0;
1195
1196         list_for_each_entry(component, &card->component_dev_list, card_list) {
1197                 if (!strcmp(component->name, byt_rt5640_codec_name)) {
1198                         dev_dbg(component->dev, "disabling jack detect before suspend\n");
1199                         snd_soc_component_set_jack(component, NULL, NULL);
1200                         break;
1201                 }
1202         }
1203
1204         return 0;
1205 }
1206
1207 static int byt_rt5640_resume(struct snd_soc_card *card)
1208 {
1209         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1210         struct snd_soc_component *component;
1211
1212         if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1213                 return 0;
1214
1215         list_for_each_entry(component, &card->component_dev_list, card_list) {
1216                 if (!strcmp(component->name, byt_rt5640_codec_name)) {
1217                         dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1218                         snd_soc_component_set_jack(component, &priv->jack, NULL);
1219                         break;
1220                 }
1221         }
1222
1223         return 0;
1224 }
1225
1226 static struct snd_soc_card byt_rt5640_card = {
1227         .name = "bytcr-rt5640",
1228         .owner = THIS_MODULE,
1229         .dai_link = byt_rt5640_dais,
1230         .num_links = ARRAY_SIZE(byt_rt5640_dais),
1231         .dapm_widgets = byt_rt5640_widgets,
1232         .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1233         .dapm_routes = byt_rt5640_audio_map,
1234         .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1235         .fully_routed = true,
1236         .suspend_pre = byt_rt5640_suspend,
1237         .resume_post = byt_rt5640_resume,
1238 };
1239
1240 static bool is_valleyview(void)
1241 {
1242         static const struct x86_cpu_id cpu_ids[] = {
1243                 { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
1244                 {}
1245         };
1246
1247         if (!x86_match_cpu(cpu_ids))
1248                 return false;
1249         return true;
1250 }
1251
1252 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
1253         u64 aif_value;       /* 1: AIF1, 2: AIF2 */
1254         u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
1255 };
1256
1257 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1258 {
1259         const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3" };
1260         const struct dmi_system_id *dmi_id;
1261         struct byt_rt5640_private *priv;
1262         struct snd_soc_acpi_mach *mach;
1263         const char *i2c_name = NULL;
1264         int ret_val = 0;
1265         int dai_index = 0;
1266         int i;
1267
1268         is_bytcr = false;
1269         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1270         if (!priv)
1271                 return -ENOMEM;
1272
1273         /* register the soc card */
1274         byt_rt5640_card.dev = &pdev->dev;
1275         mach = byt_rt5640_card.dev->platform_data;
1276         snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1277
1278         /* fix index of codec dai */
1279         for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1280                 if (!strcmp(byt_rt5640_dais[i].codec_name, "i2c-10EC5640:00")) {
1281                         dai_index = i;
1282                         break;
1283                 }
1284         }
1285
1286         /* fixup codec name based on HID */
1287         i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
1288         if (i2c_name) {
1289                 snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1290                         "%s%s", "i2c-", i2c_name);
1291
1292                 byt_rt5640_dais[dai_index].codec_name = byt_rt5640_codec_name;
1293         }
1294
1295         /*
1296          * swap SSP0 if bytcr is detected
1297          * (will be overridden if DMI quirk is detected)
1298          */
1299         if (is_valleyview()) {
1300                 struct sst_platform_info *p_info = mach->pdata;
1301                 const struct sst_res_info *res_info = p_info->res_info;
1302
1303                 if (res_info->acpi_ipc_irq_index == 0)
1304                         is_bytcr = true;
1305         }
1306
1307         if (is_bytcr) {
1308                 /*
1309                  * Baytrail CR platforms may have CHAN package in BIOS, try
1310                  * to find relevant routing quirk based as done on Windows
1311                  * platforms. We have to read the information directly from the
1312                  * BIOS, at this stage the card is not created and the links
1313                  * with the codec driver/pdata are non-existent
1314                  */
1315
1316                 struct acpi_chan_package chan_package;
1317
1318                 /* format specified: 2 64-bit integers */
1319                 struct acpi_buffer format = {sizeof("NN"), "NN"};
1320                 struct acpi_buffer state = {0, NULL};
1321                 struct snd_soc_acpi_package_context pkg_ctx;
1322                 bool pkg_found = false;
1323
1324                 state.length = sizeof(chan_package);
1325                 state.pointer = &chan_package;
1326
1327                 pkg_ctx.name = "CHAN";
1328                 pkg_ctx.length = 2;
1329                 pkg_ctx.format = &format;
1330                 pkg_ctx.state = &state;
1331                 pkg_ctx.data_valid = false;
1332
1333                 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1334                                                                &pkg_ctx);
1335                 if (pkg_found) {
1336                         if (chan_package.aif_value == 1) {
1337                                 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
1338                                 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1339                         } else  if (chan_package.aif_value == 2) {
1340                                 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
1341                                 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1342                         } else {
1343                                 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
1344                                 pkg_found = false;
1345                         }
1346                 }
1347
1348                 if (!pkg_found) {
1349                         /* no BIOS indications, assume SSP0-AIF2 connection */
1350                         byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1351                 }
1352
1353                 /* change defaults for Baytrail-CR capture */
1354                 byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1355         } else {
1356                 byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1357                                     BYT_RT5640_JD_SRC_JD2_IN4N |
1358                                     BYT_RT5640_OVCD_TH_2000UA |
1359                                     BYT_RT5640_OVCD_SF_0P75;
1360         }
1361
1362         /* check quirks before creating card */
1363         dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1364         if (dmi_id)
1365                 byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1366         if (quirk_override) {
1367                 dev_info(&pdev->dev, "Overriding quirk 0x%x => 0x%x\n",
1368                          (unsigned int)byt_rt5640_quirk, quirk_override);
1369                 byt_rt5640_quirk = quirk_override;
1370         }
1371
1372         /* Must be called before register_card, also see declaration comment. */
1373         ret_val = byt_rt5640_add_codec_device_props(byt_rt5640_codec_name);
1374         if (ret_val)
1375                 return ret_val;
1376
1377         log_quirks(&pdev->dev);
1378
1379         if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1380             (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1381
1382                 /* fixup codec aif name */
1383                 snprintf(byt_rt5640_codec_aif_name,
1384                         sizeof(byt_rt5640_codec_aif_name),
1385                         "%s", "rt5640-aif2");
1386
1387                 byt_rt5640_dais[dai_index].codec_dai_name =
1388                         byt_rt5640_codec_aif_name;
1389         }
1390
1391         if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1392             (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1393
1394                 /* fixup cpu dai name name */
1395                 snprintf(byt_rt5640_cpu_dai_name,
1396                         sizeof(byt_rt5640_cpu_dai_name),
1397                         "%s", "ssp0-port");
1398
1399                 byt_rt5640_dais[dai_index].cpu_dai_name =
1400                         byt_rt5640_cpu_dai_name;
1401         }
1402
1403         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1404                 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
1405                 if (IS_ERR(priv->mclk)) {
1406                         ret_val = PTR_ERR(priv->mclk);
1407
1408                         dev_err(&pdev->dev,
1409                                 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
1410                                 ret_val);
1411
1412                         /*
1413                          * Fall back to bit clock usage for -ENOENT (clock not
1414                          * available likely due to missing dependencies), bail
1415                          * for all other errors, including -EPROBE_DEFER
1416                          */
1417                         if (ret_val != -ENOENT)
1418                                 return ret_val;
1419                         byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1420                 }
1421         }
1422
1423         snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1424                  "bytcr-rt5640-%s-spk-%s-mic",
1425                  (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) ?
1426                         "mono" : "stereo",
1427                  map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1428         byt_rt5640_card.long_name = byt_rt5640_long_name;
1429
1430         ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
1431
1432         if (ret_val) {
1433                 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
1434                         ret_val);
1435                 return ret_val;
1436         }
1437         platform_set_drvdata(pdev, &byt_rt5640_card);
1438         return ret_val;
1439 }
1440
1441 static struct platform_driver snd_byt_rt5640_mc_driver = {
1442         .driver = {
1443                 .name = "bytcr_rt5640",
1444         },
1445         .probe = snd_byt_rt5640_mc_probe,
1446 };
1447
1448 module_platform_driver(snd_byt_rt5640_mc_driver);
1449
1450 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1451 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1452 MODULE_LICENSE("GPL v2");
1453 MODULE_ALIAS("platform:bytcr_rt5640");