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