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