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