GNU Linux-libre 4.14.266-gnu1
[releases.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/init.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/platform_device.h>
24 #include <linux/acpi.h>
25 #include <linux/device.h>
26 #include <linux/dmi.h>
27 #include <linux/slab.h>
28 #include <asm/cpu_device_id.h>
29 #include <asm/platform_sst_audio.h>
30 #include <linux/clk.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/jack.h>
35 #include "../../codecs/rt5640.h"
36 #include "../atom/sst-atom-controls.h"
37 #include "../common/sst-acpi.h"
38 #include "../common/sst-dsp.h"
39
40 enum {
41         BYT_RT5640_DMIC1_MAP,
42         BYT_RT5640_DMIC2_MAP,
43         BYT_RT5640_IN1_MAP,
44         BYT_RT5640_IN3_MAP,
45 };
46
47 #define BYT_RT5640_MAP(quirk)   ((quirk) & 0xff)
48 #define BYT_RT5640_DMIC_EN      BIT(16)
49 #define BYT_RT5640_MONO_SPEAKER BIT(17)
50 #define BYT_RT5640_DIFF_MIC     BIT(18) /* defaut is single-ended */
51 #define BYT_RT5640_SSP2_AIF2     BIT(19) /* default is using AIF1  */
52 #define BYT_RT5640_SSP0_AIF1     BIT(20)
53 #define BYT_RT5640_SSP0_AIF2     BIT(21)
54 #define BYT_RT5640_MCLK_EN      BIT(22)
55 #define BYT_RT5640_MCLK_25MHZ   BIT(23)
56
57 struct byt_rt5640_private {
58         struct clk *mclk;
59 };
60 static bool is_bytcr;
61
62 static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
63 static unsigned int quirk_override;
64 module_param_named(quirk, quirk_override, uint, 0444);
65 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
66
67 static void log_quirks(struct device *dev)
68 {
69         int map;
70         bool has_dmic = false;
71         bool has_mclk = false;
72         bool has_ssp0 = false;
73         bool has_ssp0_aif1 = false;
74         bool has_ssp0_aif2 = false;
75         bool has_ssp2_aif2 = false;
76
77         map = BYT_RT5640_MAP(byt_rt5640_quirk);
78         switch (map) {
79         case BYT_RT5640_DMIC1_MAP:
80                 dev_info(dev, "quirk DMIC1_MAP enabled\n");
81                 has_dmic = true;
82                 break;
83         case BYT_RT5640_DMIC2_MAP:
84                 dev_info(dev, "quirk DMIC2_MAP enabled\n");
85                 has_dmic = true;
86                 break;
87         case BYT_RT5640_IN1_MAP:
88                 dev_info(dev, "quirk IN1_MAP enabled\n");
89                 break;
90         case BYT_RT5640_IN3_MAP:
91                 dev_info(dev, "quirk IN3_MAP enabled\n");
92                 break;
93         default:
94                 dev_err(dev, "quirk map 0x%x is not supported, microphone input will not work\n", map);
95                 break;
96         }
97         if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) {
98                 if (has_dmic)
99                         dev_info(dev, "quirk DMIC enabled\n");
100                 else
101                         dev_err(dev, "quirk DMIC enabled but no DMIC input set, will be ignored\n");
102         }
103         if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
104                 dev_info(dev, "quirk MONO_SPEAKER enabled\n");
105         if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC) {
106                 if (!has_dmic)
107                         dev_info(dev, "quirk DIFF_MIC enabled\n");
108                 else
109                         dev_info(dev, "quirk DIFF_MIC enabled but DMIC input selected, will be ignored\n");
110         }
111         if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
112                 dev_info(dev, "quirk SSP0_AIF1 enabled\n");
113                 has_ssp0 = true;
114                 has_ssp0_aif1 = true;
115         }
116         if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
117                 dev_info(dev, "quirk SSP0_AIF2 enabled\n");
118                 has_ssp0 = true;
119                 has_ssp0_aif2 = true;
120         }
121         if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
122                 dev_info(dev, "quirk SSP2_AIF2 enabled\n");
123                 has_ssp2_aif2 = true;
124         }
125         if (is_bytcr && !has_ssp0)
126                 dev_err(dev, "Invalid routing, bytcr detected but no SSP0-based quirk, audio cannot work with SSP2 on bytcr\n");
127         if (has_ssp0_aif1 && has_ssp0_aif2)
128                 dev_err(dev, "Invalid routing, SSP0 cannot be connected to both AIF1 and AIF2\n");
129         if (has_ssp0 && has_ssp2_aif2)
130                 dev_err(dev, "Invalid routing, cannot have both SSP0 and SSP2 connected to codec\n");
131
132         if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
133                 dev_info(dev, "quirk MCLK_EN enabled\n");
134                 has_mclk = true;
135         }
136         if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
137                 if (has_mclk)
138                         dev_info(dev, "quirk MCLK_25MHZ enabled\n");
139                 else
140                         dev_err(dev, "quirk MCLK_25MHZ enabled but quirk MCLK not selected, will be ignored\n");
141         }
142 }
143
144
145 #define BYT_CODEC_DAI1  "rt5640-aif1"
146 #define BYT_CODEC_DAI2  "rt5640-aif2"
147
148 static inline struct snd_soc_dai *byt_get_codec_dai(struct snd_soc_card *card)
149 {
150         struct snd_soc_pcm_runtime *rtd;
151
152         list_for_each_entry(rtd, &card->rtd_list, list) {
153                 if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI1,
154                              strlen(BYT_CODEC_DAI1)))
155                         return rtd->codec_dai;
156                 if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI2,
157                                 strlen(BYT_CODEC_DAI2)))
158                         return rtd->codec_dai;
159
160         }
161         return NULL;
162 }
163
164 static int platform_clock_control(struct snd_soc_dapm_widget *w,
165                                   struct snd_kcontrol *k, int  event)
166 {
167         struct snd_soc_dapm_context *dapm = w->dapm;
168         struct snd_soc_card *card = dapm->card;
169         struct snd_soc_dai *codec_dai;
170         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
171         int ret;
172
173         codec_dai = byt_get_codec_dai(card);
174         if (!codec_dai) {
175                 dev_err(card->dev,
176                         "Codec dai not found; Unable to set platform clock\n");
177                 return -EIO;
178         }
179
180         if (SND_SOC_DAPM_EVENT_ON(event)) {
181                 if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
182                         ret = clk_prepare_enable(priv->mclk);
183                         if (ret < 0) {
184                                 dev_err(card->dev,
185                                         "could not configure MCLK state\n");
186                                 return ret;
187                         }
188                 }
189                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
190                                              48000 * 512,
191                                              SND_SOC_CLOCK_IN);
192         } else {
193                 /*
194                  * Set codec clock source to internal clock before
195                  * turning off the platform clock. Codec needs clock
196                  * for Jack detection and button press
197                  */
198                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
199                                              48000 * 512,
200                                              SND_SOC_CLOCK_IN);
201                 if (!ret) {
202                         if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk)
203                                 clk_disable_unprepare(priv->mclk);
204                 }
205         }
206
207         if (ret < 0) {
208                 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
209                 return ret;
210         }
211
212         return 0;
213 }
214
215 static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
216         SND_SOC_DAPM_HP("Headphone", NULL),
217         SND_SOC_DAPM_MIC("Headset Mic", NULL),
218         SND_SOC_DAPM_MIC("Internal Mic", NULL),
219         SND_SOC_DAPM_SPK("Speaker", NULL),
220         SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
221                             platform_clock_control, SND_SOC_DAPM_PRE_PMU |
222                             SND_SOC_DAPM_POST_PMD),
223
224 };
225
226 static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
227         {"Headphone", NULL, "Platform Clock"},
228         {"Headset Mic", NULL, "Platform Clock"},
229         {"Headset Mic", NULL, "MICBIAS1"},
230         {"IN2P", NULL, "Headset Mic"},
231         {"Headphone", NULL, "HPOL"},
232         {"Headphone", NULL, "HPOR"},
233 };
234
235 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
236         {"Internal Mic", NULL, "Platform Clock"},
237         {"DMIC1", NULL, "Internal Mic"},
238 };
239
240 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
241         {"Internal Mic", NULL, "Platform Clock"},
242         {"DMIC2", NULL, "Internal Mic"},
243 };
244
245 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
246         {"Internal Mic", NULL, "Platform Clock"},
247         {"Internal Mic", NULL, "MICBIAS1"},
248         {"IN1P", NULL, "Internal Mic"},
249 };
250
251 static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
252         {"Internal Mic", NULL, "Platform Clock"},
253         {"Internal Mic", NULL, "MICBIAS1"},
254         {"IN3P", NULL, "Internal Mic"},
255 };
256
257 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
258         {"ssp2 Tx", NULL, "codec_out0"},
259         {"ssp2 Tx", NULL, "codec_out1"},
260         {"codec_in0", NULL, "ssp2 Rx"},
261         {"codec_in1", NULL, "ssp2 Rx"},
262
263         {"AIF1 Playback", NULL, "ssp2 Tx"},
264         {"ssp2 Rx", NULL, "AIF1 Capture"},
265 };
266
267 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
268         {"ssp2 Tx", NULL, "codec_out0"},
269         {"ssp2 Tx", NULL, "codec_out1"},
270         {"codec_in0", NULL, "ssp2 Rx"},
271         {"codec_in1", NULL, "ssp2 Rx"},
272
273         {"AIF2 Playback", NULL, "ssp2 Tx"},
274         {"ssp2 Rx", NULL, "AIF2 Capture"},
275 };
276
277 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
278         {"ssp0 Tx", NULL, "modem_out"},
279         {"modem_in", NULL, "ssp0 Rx"},
280
281         {"AIF1 Playback", NULL, "ssp0 Tx"},
282         {"ssp0 Rx", NULL, "AIF1 Capture"},
283 };
284
285 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
286         {"ssp0 Tx", NULL, "modem_out"},
287         {"modem_in", NULL, "ssp0 Rx"},
288
289         {"AIF2 Playback", NULL, "ssp0 Tx"},
290         {"ssp0 Rx", NULL, "AIF2 Capture"},
291 };
292
293 static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
294         {"Speaker", NULL, "Platform Clock"},
295         {"Speaker", NULL, "SPOLP"},
296         {"Speaker", NULL, "SPOLN"},
297         {"Speaker", NULL, "SPORP"},
298         {"Speaker", NULL, "SPORN"},
299 };
300
301 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
302         {"Speaker", NULL, "Platform Clock"},
303         {"Speaker", NULL, "SPOLP"},
304         {"Speaker", NULL, "SPOLN"},
305 };
306
307 static const struct snd_kcontrol_new byt_rt5640_controls[] = {
308         SOC_DAPM_PIN_SWITCH("Headphone"),
309         SOC_DAPM_PIN_SWITCH("Headset Mic"),
310         SOC_DAPM_PIN_SWITCH("Internal Mic"),
311         SOC_DAPM_PIN_SWITCH("Speaker"),
312 };
313
314 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
315                                         struct snd_pcm_hw_params *params)
316 {
317         struct snd_soc_pcm_runtime *rtd = substream->private_data;
318         struct snd_soc_dai *codec_dai = rtd->codec_dai;
319         int ret;
320
321         ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
322                                      params_rate(params) * 512,
323                                      SND_SOC_CLOCK_IN);
324
325         if (ret < 0) {
326                 dev_err(rtd->dev, "can't set codec clock %d\n", ret);
327                 return ret;
328         }
329
330         if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
331                 /* use bitclock as PLL input */
332                 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
333                         (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
334
335                         /* 2x16 bit slots on SSP0 */
336                         ret = snd_soc_dai_set_pll(codec_dai, 0,
337                                                 RT5640_PLL1_S_BCLK1,
338                                                 params_rate(params) * 32,
339                                                 params_rate(params) * 512);
340                 } else {
341                         /* 2x15 bit slots on SSP2 */
342                         ret = snd_soc_dai_set_pll(codec_dai, 0,
343                                                 RT5640_PLL1_S_BCLK1,
344                                                 params_rate(params) * 50,
345                                                 params_rate(params) * 512);
346                 }
347         } else {
348                 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
349                         ret = snd_soc_dai_set_pll(codec_dai, 0,
350                                                 RT5640_PLL1_S_MCLK,
351                                                 25000000,
352                                                 params_rate(params) * 512);
353                 } else {
354                         ret = snd_soc_dai_set_pll(codec_dai, 0,
355                                                 RT5640_PLL1_S_MCLK,
356                                                 19200000,
357                                                 params_rate(params) * 512);
358                 }
359         }
360
361         if (ret < 0) {
362                 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
363                 return ret;
364         }
365
366         return 0;
367 }
368
369 static int byt_rt5640_quirk_cb(const struct dmi_system_id *id)
370 {
371         byt_rt5640_quirk = (unsigned long)id->driver_data;
372         return 1;
373 }
374
375 static const struct dmi_system_id byt_rt5640_quirk_table[] = {
376         {
377                 .callback = byt_rt5640_quirk_cb,
378                 .matches = {
379                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
380                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
381                 },
382                 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
383                                                  BYT_RT5640_MCLK_EN),
384         },
385         {
386                 .callback = byt_rt5640_quirk_cb,
387                 .matches = {
388                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
389                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
390                 },
391                 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
392                                                  BYT_RT5640_MONO_SPEAKER |
393                                                  BYT_RT5640_DIFF_MIC |
394                                                  BYT_RT5640_SSP0_AIF2 |
395                                                  BYT_RT5640_MCLK_EN
396                                                  ),
397         },
398         {
399                 .callback = byt_rt5640_quirk_cb,
400                 .matches = {
401                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "DellInc."),
402                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
403                 },
404                 .driver_data = (unsigned long *)(BYT_RT5640_DMIC2_MAP |
405                                                  BYT_RT5640_DMIC_EN |
406                                                  BYT_RT5640_MCLK_EN),
407         },
408         {
409                 .callback = byt_rt5640_quirk_cb,
410                 .matches = {
411                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
412                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
413                 },
414                 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
415                                                  BYT_RT5640_MCLK_EN),
416         },
417         {
418                 .callback = byt_rt5640_quirk_cb,
419                 .matches = {
420                         DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
421                         DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
422                 },
423                 .driver_data = (unsigned long *)(BYT_RT5640_DMIC1_MAP |
424                                                  BYT_RT5640_DMIC_EN),
425         },
426         {
427                 .callback = byt_rt5640_quirk_cb,
428                 .matches = {
429                         DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
430                         DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
431                 },
432                 .driver_data = (unsigned long *)(BYT_RT5640_IN3_MAP |
433                                                 BYT_RT5640_MCLK_EN |
434                                                 BYT_RT5640_SSP0_AIF1),
435         },
436         {
437                 .callback = byt_rt5640_quirk_cb,
438                 .matches = {
439                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
440                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
441                 },
442                 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
443                                                  BYT_RT5640_MCLK_EN |
444                                                  BYT_RT5640_SSP0_AIF1),
445
446         },
447         {
448                 .callback = byt_rt5640_quirk_cb,
449                 .matches = {
450                         DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
451                 },
452                 .driver_data = (unsigned long *)(BYT_RT5640_IN3_MAP |
453                                                  BYT_RT5640_MCLK_EN |
454                                                  BYT_RT5640_SSP0_AIF1),
455
456         },
457         {}
458 };
459
460 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
461 {
462         int ret;
463         struct snd_soc_codec *codec = runtime->codec;
464         struct snd_soc_card *card = runtime->card;
465         const struct snd_soc_dapm_route *custom_map;
466         struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
467         int num_routes;
468
469         card->dapm.idle_bias_off = true;
470
471         rt5640_sel_asrc_clk_src(codec,
472                                 RT5640_DA_STEREO_FILTER |
473                                 RT5640_DA_MONO_L_FILTER |
474                                 RT5640_DA_MONO_R_FILTER |
475                                 RT5640_AD_STEREO_FILTER |
476                                 RT5640_AD_MONO_L_FILTER |
477                                 RT5640_AD_MONO_R_FILTER,
478                                 RT5640_CLK_SEL_ASRC);
479
480         ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
481                                         ARRAY_SIZE(byt_rt5640_controls));
482         if (ret) {
483                 dev_err(card->dev, "unable to add card controls\n");
484                 return ret;
485         }
486
487         switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
488         case BYT_RT5640_IN1_MAP:
489                 custom_map = byt_rt5640_intmic_in1_map;
490                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
491                 break;
492         case BYT_RT5640_IN3_MAP:
493                 custom_map = byt_rt5640_intmic_in3_map;
494                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
495                 break;
496         case BYT_RT5640_DMIC2_MAP:
497                 custom_map = byt_rt5640_intmic_dmic2_map;
498                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
499                 break;
500         default:
501                 custom_map = byt_rt5640_intmic_dmic1_map;
502                 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
503         }
504
505         ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
506         if (ret)
507                 return ret;
508
509         if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
510                 ret = snd_soc_dapm_add_routes(&card->dapm,
511                                         byt_rt5640_ssp2_aif2_map,
512                                         ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
513         } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
514                 ret = snd_soc_dapm_add_routes(&card->dapm,
515                                         byt_rt5640_ssp0_aif1_map,
516                                         ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
517         } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
518                 ret = snd_soc_dapm_add_routes(&card->dapm,
519                                         byt_rt5640_ssp0_aif2_map,
520                                         ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
521         } else {
522                 ret = snd_soc_dapm_add_routes(&card->dapm,
523                                         byt_rt5640_ssp2_aif1_map,
524                                         ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
525         }
526         if (ret)
527                 return ret;
528
529         if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
530                 ret = snd_soc_dapm_add_routes(&card->dapm,
531                                         byt_rt5640_mono_spk_map,
532                                         ARRAY_SIZE(byt_rt5640_mono_spk_map));
533         } else {
534                 ret = snd_soc_dapm_add_routes(&card->dapm,
535                                         byt_rt5640_stereo_spk_map,
536                                         ARRAY_SIZE(byt_rt5640_stereo_spk_map));
537         }
538         if (ret)
539                 return ret;
540
541         if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC) {
542                 snd_soc_update_bits(codec,  RT5640_IN1_IN2, RT5640_IN_DF1,
543                                     RT5640_IN_DF1);
544         }
545
546         if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) {
547                 ret = rt5640_dmic_enable(codec, 0, 0);
548                 if (ret)
549                         return ret;
550         }
551
552         snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
553         snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
554
555         if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
556                 /*
557                  * The firmware might enable the clock at
558                  * boot (this information may or may not
559                  * be reflected in the enable clock register).
560                  * To change the rate we must disable the clock
561                  * first to cover these cases. Due to common
562                  * clock framework restrictions that do not allow
563                  * to disable a clock that has not been enabled,
564                  * we need to enable the clock first.
565                  */
566                 ret = clk_prepare_enable(priv->mclk);
567                 if (!ret)
568                         clk_disable_unprepare(priv->mclk);
569
570                 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
571                         ret = clk_set_rate(priv->mclk, 25000000);
572                 else
573                         ret = clk_set_rate(priv->mclk, 19200000);
574
575                 if (ret)
576                         dev_err(card->dev, "unable to set MCLK rate\n");
577         }
578
579         return ret;
580 }
581
582 static const struct snd_soc_pcm_stream byt_rt5640_dai_params = {
583         .formats = SNDRV_PCM_FMTBIT_S24_LE,
584         .rate_min = 48000,
585         .rate_max = 48000,
586         .channels_min = 2,
587         .channels_max = 2,
588 };
589
590 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
591                             struct snd_pcm_hw_params *params)
592 {
593         struct snd_interval *rate = hw_param_interval(params,
594                         SNDRV_PCM_HW_PARAM_RATE);
595         struct snd_interval *channels = hw_param_interval(params,
596                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
597         int ret;
598
599         /* The DSP will covert the FE rate to 48k, stereo */
600         rate->min = rate->max = 48000;
601         channels->min = channels->max = 2;
602
603         if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
604                 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
605
606                 /* set SSP0 to 16-bit */
607                 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
608
609                 /*
610                  * Default mode for SSP configuration is TDM 4 slot, override config
611                  * with explicit setting to I2S 2ch 16-bit. The word length is set with
612                  * dai_set_tdm_slot() since there is no other API exposed
613                  */
614                 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
615                                         SND_SOC_DAIFMT_I2S     |
616                                         SND_SOC_DAIFMT_NB_NF   |
617                                         SND_SOC_DAIFMT_CBS_CFS
618                         );
619                 if (ret < 0) {
620                         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
621                         return ret;
622                 }
623
624                 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
625                 if (ret < 0) {
626                         dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
627                         return ret;
628                 }
629
630         } else {
631
632                 /* set SSP2 to 24-bit */
633                 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
634
635                 /*
636                  * Default mode for SSP configuration is TDM 4 slot, override config
637                  * with explicit setting to I2S 2ch 24-bit. The word length is set with
638                  * dai_set_tdm_slot() since there is no other API exposed
639                  */
640                 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
641                                         SND_SOC_DAIFMT_I2S     |
642                                         SND_SOC_DAIFMT_NB_NF   |
643                                         SND_SOC_DAIFMT_CBS_CFS
644                         );
645                 if (ret < 0) {
646                         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
647                         return ret;
648                 }
649
650                 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
651                 if (ret < 0) {
652                         dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
653                         return ret;
654                 }
655         }
656         return 0;
657 }
658
659 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
660 {
661         return snd_pcm_hw_constraint_single(substream->runtime,
662                         SNDRV_PCM_HW_PARAM_RATE, 48000);
663 }
664
665 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
666         .startup = byt_rt5640_aif1_startup,
667 };
668
669 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
670         .hw_params = byt_rt5640_aif1_hw_params,
671 };
672
673 static struct snd_soc_dai_link byt_rt5640_dais[] = {
674         [MERR_DPCM_AUDIO] = {
675                 .name = "Baytrail Audio Port",
676                 .stream_name = "Baytrail Audio",
677                 .cpu_dai_name = "media-cpu-dai",
678                 .codec_dai_name = "snd-soc-dummy-dai",
679                 .codec_name = "snd-soc-dummy",
680                 .platform_name = "sst-mfld-platform",
681                 .nonatomic = true,
682                 .dynamic = 1,
683                 .dpcm_playback = 1,
684                 .dpcm_capture = 1,
685                 .ops = &byt_rt5640_aif1_ops,
686         },
687         [MERR_DPCM_DEEP_BUFFER] = {
688                 .name = "Deep-Buffer Audio Port",
689                 .stream_name = "Deep-Buffer Audio",
690                 .cpu_dai_name = "deepbuffer-cpu-dai",
691                 .codec_dai_name = "snd-soc-dummy-dai",
692                 .codec_name = "snd-soc-dummy",
693                 .platform_name = "sst-mfld-platform",
694                 .nonatomic = true,
695                 .dynamic = 1,
696                 .dpcm_playback = 1,
697                 .ops = &byt_rt5640_aif1_ops,
698         },
699         [MERR_DPCM_COMPR] = {
700                 .name = "Baytrail Compressed Port",
701                 .stream_name = "Baytrail Compress",
702                 .cpu_dai_name = "compress-cpu-dai",
703                 .codec_dai_name = "snd-soc-dummy-dai",
704                 .codec_name = "snd-soc-dummy",
705                 .platform_name = "sst-mfld-platform",
706         },
707                 /* back ends */
708         {
709                 .name = "SSP2-Codec",
710                 .id = 1,
711                 .cpu_dai_name = "ssp2-port", /* overwritten for ssp0 routing */
712                 .platform_name = "sst-mfld-platform",
713                 .no_pcm = 1,
714                 .codec_dai_name = "rt5640-aif1", /* changed w/ quirk */
715                 .codec_name = "i2c-10EC5640:00", /* overwritten with HID */
716                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
717                                                 | SND_SOC_DAIFMT_CBS_CFS,
718                 .be_hw_params_fixup = byt_rt5640_codec_fixup,
719                 .ignore_suspend = 1,
720                 .nonatomic = true,
721                 .dpcm_playback = 1,
722                 .dpcm_capture = 1,
723                 .init = byt_rt5640_init,
724                 .ops = &byt_rt5640_be_ssp2_ops,
725         },
726 };
727
728 /* SoC card */
729 static struct snd_soc_card byt_rt5640_card = {
730         .name = "bytcr-rt5640",
731         .owner = THIS_MODULE,
732         .dai_link = byt_rt5640_dais,
733         .num_links = ARRAY_SIZE(byt_rt5640_dais),
734         .dapm_widgets = byt_rt5640_widgets,
735         .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
736         .dapm_routes = byt_rt5640_audio_map,
737         .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
738         .fully_routed = true,
739 };
740
741 static char byt_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
742 static char byt_rt5640_codec_aif_name[12]; /*  = "rt5640-aif[1|2]" */
743 static char byt_rt5640_cpu_dai_name[10]; /*  = "ssp[0|2]-port" */
744
745 static bool is_valleyview(void)
746 {
747         static const struct x86_cpu_id cpu_ids[] = {
748                 { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
749                 {}
750         };
751
752         if (!x86_match_cpu(cpu_ids))
753                 return false;
754         return true;
755 }
756
757 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
758         u64 aif_value;       /* 1: AIF1, 2: AIF2 */
759         u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
760 };
761
762 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
763 {
764         int ret_val = 0;
765         struct sst_acpi_mach *mach;
766         const char *i2c_name = NULL;
767         int i;
768         int dai_index;
769         struct byt_rt5640_private *priv;
770
771         is_bytcr = false;
772         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_ATOMIC);
773         if (!priv)
774                 return -ENOMEM;
775
776         /* register the soc card */
777         byt_rt5640_card.dev = &pdev->dev;
778         mach = byt_rt5640_card.dev->platform_data;
779         snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
780
781         /* fix index of codec dai */
782         dai_index = MERR_DPCM_COMPR + 1;
783         for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
784                 if (!strcmp(byt_rt5640_dais[i].codec_name, "i2c-10EC5640:00")) {
785                         dai_index = i;
786                         break;
787                 }
788         }
789
790         /* fixup codec name based on HID */
791         i2c_name = sst_acpi_find_name_from_hid(mach->id);
792         if (i2c_name != NULL) {
793                 snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
794                         "%s%s", "i2c-", i2c_name);
795
796                 byt_rt5640_dais[dai_index].codec_name = byt_rt5640_codec_name;
797         }
798
799         /*
800          * swap SSP0 if bytcr is detected
801          * (will be overridden if DMI quirk is detected)
802          */
803         if (is_valleyview()) {
804                 struct sst_platform_info *p_info = mach->pdata;
805                 const struct sst_res_info *res_info = p_info->res_info;
806
807                 if (res_info->acpi_ipc_irq_index == 0)
808                         is_bytcr = true;
809         }
810
811         if (is_bytcr) {
812                 /*
813                  * Baytrail CR platforms may have CHAN package in BIOS, try
814                  * to find relevant routing quirk based as done on Windows
815                  * platforms. We have to read the information directly from the
816                  * BIOS, at this stage the card is not created and the links
817                  * with the codec driver/pdata are non-existent
818                  */
819
820                 struct acpi_chan_package chan_package;
821
822                 /* format specified: 2 64-bit integers */
823                 struct acpi_buffer format = {sizeof("NN"), "NN"};
824                 struct acpi_buffer state = {0, NULL};
825                 struct sst_acpi_package_context pkg_ctx;
826                 bool pkg_found = false;
827
828                 state.length = sizeof(chan_package);
829                 state.pointer = &chan_package;
830
831                 pkg_ctx.name = "CHAN";
832                 pkg_ctx.length = 2;
833                 pkg_ctx.format = &format;
834                 pkg_ctx.state = &state;
835                 pkg_ctx.data_valid = false;
836
837                 pkg_found = sst_acpi_find_package_from_hid(mach->id, &pkg_ctx);
838                 if (pkg_found) {
839                         if (chan_package.aif_value == 1) {
840                                 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
841                                 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
842                         } else  if (chan_package.aif_value == 2) {
843                                 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
844                                 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
845                         } else {
846                                 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
847                                 pkg_found = false;
848                         }
849                 }
850
851                 if (!pkg_found) {
852                         /* no BIOS indications, assume SSP0-AIF2 connection */
853                         byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
854                 }
855
856                 /* change defaults for Baytrail-CR capture */
857                 byt_rt5640_quirk |= BYT_RT5640_IN1_MAP;
858                 byt_rt5640_quirk |= BYT_RT5640_DIFF_MIC;
859         } else {
860                 byt_rt5640_quirk |= (BYT_RT5640_DMIC1_MAP |
861                                 BYT_RT5640_DMIC_EN);
862         }
863
864         /* check quirks before creating card */
865         dmi_check_system(byt_rt5640_quirk_table);
866         if (quirk_override) {
867                 dev_info(&pdev->dev, "Overriding quirk 0x%x => 0x%x\n",
868                          (unsigned int)byt_rt5640_quirk, quirk_override);
869                 byt_rt5640_quirk = quirk_override;
870         }
871         log_quirks(&pdev->dev);
872
873         if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
874             (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
875
876                 /* fixup codec aif name */
877                 snprintf(byt_rt5640_codec_aif_name,
878                         sizeof(byt_rt5640_codec_aif_name),
879                         "%s", "rt5640-aif2");
880
881                 byt_rt5640_dais[dai_index].codec_dai_name =
882                         byt_rt5640_codec_aif_name;
883         }
884
885         if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
886             (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
887
888                 /* fixup cpu dai name name */
889                 snprintf(byt_rt5640_cpu_dai_name,
890                         sizeof(byt_rt5640_cpu_dai_name),
891                         "%s", "ssp0-port");
892
893                 byt_rt5640_dais[dai_index].cpu_dai_name =
894                         byt_rt5640_cpu_dai_name;
895         }
896
897         if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && (is_valleyview())) {
898                 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
899                 if (IS_ERR(priv->mclk)) {
900                         ret_val = PTR_ERR(priv->mclk);
901
902                         dev_err(&pdev->dev,
903                                 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
904                                 ret_val);
905
906                         /*
907                          * Fall back to bit clock usage for -ENOENT (clock not
908                          * available likely due to missing dependencies), bail
909                          * for all other errors, including -EPROBE_DEFER
910                          */
911                         if (ret_val != -ENOENT)
912                                 return ret_val;
913                         byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
914                 }
915         }
916
917         ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
918
919         if (ret_val) {
920                 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
921                         ret_val);
922                 return ret_val;
923         }
924         platform_set_drvdata(pdev, &byt_rt5640_card);
925         return ret_val;
926 }
927
928 static struct platform_driver snd_byt_rt5640_mc_driver = {
929         .driver = {
930                 .name = "bytcr_rt5640",
931         },
932         .probe = snd_byt_rt5640_mc_probe,
933 };
934
935 module_platform_driver(snd_byt_rt5640_mc_driver);
936
937 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
938 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
939 MODULE_LICENSE("GPL v2");
940 MODULE_ALIAS("platform:bytcr_rt5640");