GNU Linux-libre 4.14.332-gnu1
[releases.git] / sound / soc / intel / boards / cht_bsw_rt5645.c
1 /*
2  *  cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms
3  *                     Cherrytrail and Braswell, with RT5645 codec.
4  *
5  *  Copyright (C) 2015 Intel Corp
6  *  Author: Fang, Yang A <yang.a.fang@intel.com>
7  *              N,Harshapriya <harshapriya.n@intel.com>
8  *  This file is modified from cht_bsw_rt5672.c
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; version 2 of the License.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21  */
22
23 #include <linux/module.h>
24 #include <linux/acpi.h>
25 #include <linux/platform_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/rt5645.h"
36 #include "../atom/sst-atom-controls.h"
37 #include "../common/sst-acpi.h"
38
39 #define CHT_PLAT_CLK_3_HZ       19200000
40 #define CHT_CODEC_DAI1  "rt5645-aif1"
41 #define CHT_CODEC_DAI2  "rt5645-aif2"
42
43 struct cht_acpi_card {
44         char *codec_id;
45         int codec_type;
46         struct snd_soc_card *soc_card;
47 };
48
49 struct cht_mc_private {
50         struct snd_soc_jack jack;
51         struct cht_acpi_card *acpi_card;
52         char codec_name[16];
53         struct clk *mclk;
54 };
55
56 #define CHT_RT5645_MAP(quirk)   ((quirk) & 0xff)
57 #define CHT_RT5645_SSP2_AIF2     BIT(16) /* default is using AIF1  */
58 #define CHT_RT5645_SSP0_AIF1     BIT(17)
59 #define CHT_RT5645_SSP0_AIF2     BIT(18)
60
61 static unsigned long cht_rt5645_quirk = 0;
62
63 static void log_quirks(struct device *dev)
64 {
65         if (cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2)
66                 dev_info(dev, "quirk SSP2_AIF2 enabled");
67         if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1)
68                 dev_info(dev, "quirk SSP0_AIF1 enabled");
69         if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)
70                 dev_info(dev, "quirk SSP0_AIF2 enabled");
71 }
72
73 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
74 {
75         struct snd_soc_pcm_runtime *rtd;
76
77         list_for_each_entry(rtd, &card->rtd_list, list) {
78                 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI1,
79                              strlen(CHT_CODEC_DAI1)))
80                         return rtd->codec_dai;
81                 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI2,
82                              strlen(CHT_CODEC_DAI2)))
83                         return rtd->codec_dai;
84         }
85         return NULL;
86 }
87
88 static int platform_clock_control(struct snd_soc_dapm_widget *w,
89                 struct snd_kcontrol *k, int  event)
90 {
91         struct snd_soc_dapm_context *dapm = w->dapm;
92         struct snd_soc_card *card = dapm->card;
93         struct snd_soc_dai *codec_dai;
94         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
95         int ret;
96
97         codec_dai = cht_get_codec_dai(card);
98         if (!codec_dai) {
99                 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
100                 return -EIO;
101         }
102
103         if (SND_SOC_DAPM_EVENT_ON(event)) {
104                 if (ctx->mclk) {
105                         ret = clk_prepare_enable(ctx->mclk);
106                         if (ret < 0) {
107                                 dev_err(card->dev,
108                                         "could not configure MCLK state");
109                                 return ret;
110                         }
111                 }
112         } else {
113                 /* Set codec sysclk source to its internal clock because codec PLL will
114                  * be off when idle and MCLK will also be off when codec is
115                  * runtime suspended. Codec needs clock for jack detection and button
116                  * press. MCLK is turned off with clock framework or ACPI.
117                  */
118                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
119                                         48000 * 512, SND_SOC_CLOCK_IN);
120                 if (ret < 0) {
121                         dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
122                         return ret;
123                 }
124
125                 if (ctx->mclk)
126                         clk_disable_unprepare(ctx->mclk);
127         }
128
129         return 0;
130 }
131
132 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
133         SND_SOC_DAPM_HP("Headphone", NULL),
134         SND_SOC_DAPM_MIC("Headset Mic", NULL),
135         SND_SOC_DAPM_MIC("Int Mic", NULL),
136         SND_SOC_DAPM_MIC("Int Analog Mic", NULL),
137         SND_SOC_DAPM_SPK("Ext Spk", NULL),
138         SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
139                         platform_clock_control, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
140 };
141
142 static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
143         {"IN1P", NULL, "Headset Mic"},
144         {"IN1N", NULL, "Headset Mic"},
145         {"DMIC L1", NULL, "Int Mic"},
146         {"DMIC R1", NULL, "Int Mic"},
147         {"IN2P", NULL, "Int Analog Mic"},
148         {"IN2N", NULL, "Int Analog Mic"},
149         {"Headphone", NULL, "HPOL"},
150         {"Headphone", NULL, "HPOR"},
151         {"Ext Spk", NULL, "SPOL"},
152         {"Ext Spk", NULL, "SPOR"},
153         {"Headphone", NULL, "Platform Clock"},
154         {"Headset Mic", NULL, "Platform Clock"},
155         {"Int Mic", NULL, "Platform Clock"},
156         {"Int Analog Mic", NULL, "Platform Clock"},
157         {"Int Analog Mic", NULL, "micbias1"},
158         {"Int Analog Mic", NULL, "micbias2"},
159         {"Ext Spk", NULL, "Platform Clock"},
160 };
161
162 static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
163         {"IN1P", NULL, "Headset Mic"},
164         {"IN1N", NULL, "Headset Mic"},
165         {"DMIC L2", NULL, "Int Mic"},
166         {"DMIC R2", NULL, "Int Mic"},
167         {"Headphone", NULL, "HPOL"},
168         {"Headphone", NULL, "HPOR"},
169         {"Ext Spk", NULL, "SPOL"},
170         {"Ext Spk", NULL, "SPOR"},
171         {"Headphone", NULL, "Platform Clock"},
172         {"Headset Mic", NULL, "Platform Clock"},
173         {"Int Mic", NULL, "Platform Clock"},
174         {"Ext Spk", NULL, "Platform Clock"},
175 };
176
177 static const struct snd_soc_dapm_route cht_rt5645_ssp2_aif1_map[] = {
178         {"AIF1 Playback", NULL, "ssp2 Tx"},
179         {"ssp2 Tx", NULL, "codec_out0"},
180         {"ssp2 Tx", NULL, "codec_out1"},
181         {"codec_in0", NULL, "ssp2 Rx" },
182         {"codec_in1", NULL, "ssp2 Rx" },
183         {"ssp2 Rx", NULL, "AIF1 Capture"},
184 };
185
186 static const struct snd_soc_dapm_route cht_rt5645_ssp2_aif2_map[] = {
187         {"AIF2 Playback", NULL, "ssp2 Tx"},
188         {"ssp2 Tx", NULL, "codec_out0"},
189         {"ssp2 Tx", NULL, "codec_out1"},
190         {"codec_in0", NULL, "ssp2 Rx" },
191         {"codec_in1", NULL, "ssp2 Rx" },
192         {"ssp2 Rx", NULL, "AIF2 Capture"},
193 };
194
195 static const struct snd_soc_dapm_route cht_rt5645_ssp0_aif1_map[] = {
196         {"AIF1 Playback", NULL, "ssp0 Tx"},
197         {"ssp0 Tx", NULL, "modem_out"},
198         {"modem_in", NULL, "ssp0 Rx" },
199         {"ssp0 Rx", NULL, "AIF1 Capture"},
200 };
201
202 static const struct snd_soc_dapm_route cht_rt5645_ssp0_aif2_map[] = {
203         {"AIF2 Playback", NULL, "ssp0 Tx"},
204         {"ssp0 Tx", NULL, "modem_out"},
205         {"modem_in", NULL, "ssp0 Rx" },
206         {"ssp0 Rx", NULL, "AIF2 Capture"},
207 };
208
209 static const struct snd_kcontrol_new cht_mc_controls[] = {
210         SOC_DAPM_PIN_SWITCH("Headphone"),
211         SOC_DAPM_PIN_SWITCH("Headset Mic"),
212         SOC_DAPM_PIN_SWITCH("Int Mic"),
213         SOC_DAPM_PIN_SWITCH("Int Analog Mic"),
214         SOC_DAPM_PIN_SWITCH("Ext Spk"),
215 };
216
217 static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
218         {
219                 .pin    = "Headphone",
220                 .mask   = SND_JACK_HEADPHONE,
221         },
222         {
223                 .pin    = "Headset Mic",
224                 .mask   = SND_JACK_MICROPHONE,
225         },
226 };
227
228 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
229                              struct snd_pcm_hw_params *params)
230 {
231         struct snd_soc_pcm_runtime *rtd = substream->private_data;
232         struct snd_soc_dai *codec_dai = rtd->codec_dai;
233         int ret;
234
235         /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
236         ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
237                                   CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
238         if (ret < 0) {
239                 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
240                 return ret;
241         }
242
243         ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
244                                 params_rate(params) * 512, SND_SOC_CLOCK_IN);
245         if (ret < 0) {
246                 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
247                 return ret;
248         }
249
250         return 0;
251 }
252
253 /* uncomment when we have a real quirk
254 static int cht_rt5645_quirk_cb(const struct dmi_system_id *id)
255 {
256         cht_rt5645_quirk = (unsigned long)id->driver_data;
257         return 1;
258 }
259 */
260
261 static const struct dmi_system_id cht_rt5645_quirk_table[] = {
262         {
263         },
264 };
265
266 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
267 {
268         int ret;
269         int jack_type;
270         struct snd_soc_codec *codec = runtime->codec;
271         struct snd_soc_card *card = runtime->card;
272         struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
273
274         if ((cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2) ||
275             (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
276                 /* Select clk_i2s2_asrc as ASRC clock source */
277                 rt5645_sel_asrc_clk_src(codec,
278                                         RT5645_DA_STEREO_FILTER |
279                                         RT5645_DA_MONO_L_FILTER |
280                                         RT5645_DA_MONO_R_FILTER |
281                                         RT5645_AD_STEREO_FILTER,
282                                         RT5645_CLK_SEL_I2S2_ASRC);
283         } else {
284                 /* Select clk_i2s1_asrc as ASRC clock source */
285                 rt5645_sel_asrc_clk_src(codec,
286                                         RT5645_DA_STEREO_FILTER |
287                                         RT5645_DA_MONO_L_FILTER |
288                                         RT5645_DA_MONO_R_FILTER |
289                                         RT5645_AD_STEREO_FILTER,
290                                         RT5645_CLK_SEL_I2S1_ASRC);
291         }
292
293         if (cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2) {
294                 ret = snd_soc_dapm_add_routes(&card->dapm,
295                                         cht_rt5645_ssp2_aif2_map,
296                                         ARRAY_SIZE(cht_rt5645_ssp2_aif2_map));
297         } else if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1) {
298                 ret = snd_soc_dapm_add_routes(&card->dapm,
299                                         cht_rt5645_ssp0_aif1_map,
300                                         ARRAY_SIZE(cht_rt5645_ssp0_aif1_map));
301         } else if (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2) {
302                 ret = snd_soc_dapm_add_routes(&card->dapm,
303                                         cht_rt5645_ssp0_aif2_map,
304                                         ARRAY_SIZE(cht_rt5645_ssp0_aif2_map));
305         } else {
306                 ret = snd_soc_dapm_add_routes(&card->dapm,
307                                         cht_rt5645_ssp2_aif1_map,
308                                         ARRAY_SIZE(cht_rt5645_ssp2_aif1_map));
309         }
310         if (ret)
311                 return ret;
312
313         if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
314                 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
315                                         SND_JACK_BTN_0 | SND_JACK_BTN_1 |
316                                         SND_JACK_BTN_2 | SND_JACK_BTN_3;
317         else
318                 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
319
320         ret = snd_soc_card_jack_new(runtime->card, "Headset",
321                                     jack_type, &ctx->jack,
322                                     cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
323         if (ret) {
324                 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
325                 return ret;
326         }
327
328         rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
329
330         if (ctx->mclk) {
331                 /*
332                  * The firmware might enable the clock at
333                  * boot (this information may or may not
334                  * be reflected in the enable clock register).
335                  * To change the rate we must disable the clock
336                  * first to cover these cases. Due to common
337                  * clock framework restrictions that do not allow
338                  * to disable a clock that has not been enabled,
339                  * we need to enable the clock first.
340                  */
341                 ret = clk_prepare_enable(ctx->mclk);
342                 if (!ret)
343                         clk_disable_unprepare(ctx->mclk);
344
345                 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
346
347                 if (ret)
348                         dev_err(runtime->dev, "unable to set MCLK rate\n");
349         }
350         return ret;
351 }
352
353 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
354                             struct snd_pcm_hw_params *params)
355 {
356         int ret;
357         struct snd_interval *rate = hw_param_interval(params,
358                         SNDRV_PCM_HW_PARAM_RATE);
359         struct snd_interval *channels = hw_param_interval(params,
360                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
361
362         /* The DSP will covert the FE rate to 48k, stereo, 24bits */
363         rate->min = rate->max = 48000;
364         channels->min = channels->max = 2;
365
366         if ((cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1) ||
367                 (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
368
369                 /* set SSP0 to 16-bit */
370                 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
371
372                 /*
373                  * Default mode for SSP configuration is TDM 4 slot, override config
374                  * with explicit setting to I2S 2ch 16-bit. The word length is set with
375                  * dai_set_tdm_slot() since there is no other API exposed
376                  */
377                 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
378                                         SND_SOC_DAIFMT_I2S     |
379                                         SND_SOC_DAIFMT_NB_NF   |
380                                         SND_SOC_DAIFMT_CBS_CFS
381                         );
382                 if (ret < 0) {
383                         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
384                         return ret;
385                 }
386
387                 ret = snd_soc_dai_set_fmt(rtd->codec_dai,
388                                         SND_SOC_DAIFMT_I2S     |
389                                         SND_SOC_DAIFMT_NB_NF   |
390                                         SND_SOC_DAIFMT_CBS_CFS
391                         );
392                 if (ret < 0) {
393                         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
394                         return ret;
395                 }
396
397                 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
398                 if (ret < 0) {
399                         dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
400                         return ret;
401                 }
402
403         } else {
404
405                 /* set SSP2 to 24-bit */
406                 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
407
408                 /*
409                  * Default mode for SSP configuration is TDM 4 slot
410                  */
411                 ret = snd_soc_dai_set_fmt(rtd->codec_dai,
412                                         SND_SOC_DAIFMT_DSP_B |
413                                         SND_SOC_DAIFMT_IB_NF |
414                                         SND_SOC_DAIFMT_CBS_CFS);
415                 if (ret < 0) {
416                         dev_err(rtd->dev, "can't set format to TDM %d\n", ret);
417                         return ret;
418                 }
419
420                 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
421                 ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0xF, 0xF, 4, 24);
422                 if (ret < 0) {
423                         dev_err(rtd->dev, "can't set codec TDM slot %d\n", ret);
424                         return ret;
425                 }
426         }
427         return 0;
428 }
429
430 static int cht_aif1_startup(struct snd_pcm_substream *substream)
431 {
432         return snd_pcm_hw_constraint_single(substream->runtime,
433                         SNDRV_PCM_HW_PARAM_RATE, 48000);
434 }
435
436 static const struct snd_soc_ops cht_aif1_ops = {
437         .startup = cht_aif1_startup,
438 };
439
440 static const struct snd_soc_ops cht_be_ssp2_ops = {
441         .hw_params = cht_aif1_hw_params,
442 };
443
444 static struct snd_soc_dai_link cht_dailink[] = {
445         [MERR_DPCM_AUDIO] = {
446                 .name = "Audio Port",
447                 .stream_name = "Audio",
448                 .cpu_dai_name = "media-cpu-dai",
449                 .codec_dai_name = "snd-soc-dummy-dai",
450                 .codec_name = "snd-soc-dummy",
451                 .platform_name = "sst-mfld-platform",
452                 .nonatomic = true,
453                 .dynamic = 1,
454                 .dpcm_playback = 1,
455                 .dpcm_capture = 1,
456                 .ops = &cht_aif1_ops,
457         },
458         [MERR_DPCM_DEEP_BUFFER] = {
459                 .name = "Deep-Buffer Audio Port",
460                 .stream_name = "Deep-Buffer Audio",
461                 .cpu_dai_name = "deepbuffer-cpu-dai",
462                 .codec_dai_name = "snd-soc-dummy-dai",
463                 .codec_name = "snd-soc-dummy",
464                 .platform_name = "sst-mfld-platform",
465                 .nonatomic = true,
466                 .dynamic = 1,
467                 .dpcm_playback = 1,
468                 .ops = &cht_aif1_ops,
469         },
470         [MERR_DPCM_COMPR] = {
471                 .name = "Compressed Port",
472                 .stream_name = "Compress",
473                 .cpu_dai_name = "compress-cpu-dai",
474                 .codec_dai_name = "snd-soc-dummy-dai",
475                 .codec_name = "snd-soc-dummy",
476                 .platform_name = "sst-mfld-platform",
477         },
478         /* CODEC<->CODEC link */
479         /* back ends */
480         {
481                 .name = "SSP2-Codec",
482                 .id = 1,
483                 .cpu_dai_name = "ssp2-port",
484                 .platform_name = "sst-mfld-platform",
485                 .no_pcm = 1,
486                 .codec_dai_name = "rt5645-aif1",
487                 .codec_name = "i2c-10EC5645:00",
488                 .init = cht_codec_init,
489                 .be_hw_params_fixup = cht_codec_fixup,
490                 .nonatomic = true,
491                 .dpcm_playback = 1,
492                 .dpcm_capture = 1,
493                 .ops = &cht_be_ssp2_ops,
494         },
495 };
496
497 /* SoC card */
498 static struct snd_soc_card snd_soc_card_chtrt5645 = {
499         .name = "chtrt5645",
500         .owner = THIS_MODULE,
501         .dai_link = cht_dailink,
502         .num_links = ARRAY_SIZE(cht_dailink),
503         .dapm_widgets = cht_dapm_widgets,
504         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
505         .dapm_routes = cht_rt5645_audio_map,
506         .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
507         .controls = cht_mc_controls,
508         .num_controls = ARRAY_SIZE(cht_mc_controls),
509 };
510
511 static struct snd_soc_card snd_soc_card_chtrt5650 = {
512         .name = "chtrt5650",
513         .owner = THIS_MODULE,
514         .dai_link = cht_dailink,
515         .num_links = ARRAY_SIZE(cht_dailink),
516         .dapm_widgets = cht_dapm_widgets,
517         .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
518         .dapm_routes = cht_rt5650_audio_map,
519         .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
520         .controls = cht_mc_controls,
521         .num_controls = ARRAY_SIZE(cht_mc_controls),
522 };
523
524 static struct cht_acpi_card snd_soc_cards[] = {
525         {"10EC5640", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
526         {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
527         {"10EC5648", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
528         {"10EC3270", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
529         {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
530 };
531
532 static char cht_rt5645_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
533 static char cht_rt5645_codec_aif_name[12]; /*  = "rt5645-aif[1|2]" */
534 static char cht_rt5645_cpu_dai_name[10]; /*  = "ssp[0|2]-port" */
535
536 static bool is_valleyview(void)
537 {
538         static const struct x86_cpu_id cpu_ids[] = {
539                 { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
540                 {}
541         };
542
543         if (!x86_match_cpu(cpu_ids))
544                 return false;
545         return true;
546 }
547
548 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
549         u64 aif_value;       /* 1: AIF1, 2: AIF2 */
550         u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
551 };
552
553 static int snd_cht_mc_probe(struct platform_device *pdev)
554 {
555         int ret_val = 0;
556         int i;
557         struct cht_mc_private *drv;
558         struct snd_soc_card *card = snd_soc_cards[0].soc_card;
559         struct sst_acpi_mach *mach;
560         const char *i2c_name = NULL;
561         int dai_index = 0;
562         bool found = false;
563         bool is_bytcr = false;
564
565         drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
566         if (!drv)
567                 return -ENOMEM;
568
569         mach = (&pdev->dev)->platform_data;
570
571         for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
572                 if (acpi_dev_found(snd_soc_cards[i].codec_id) &&
573                         (!strncmp(snd_soc_cards[i].codec_id, mach->id, 8))) {
574                         dev_dbg(&pdev->dev,
575                                 "found codec %s\n", snd_soc_cards[i].codec_id);
576                         card = snd_soc_cards[i].soc_card;
577                         drv->acpi_card = &snd_soc_cards[i];
578                         found = true;
579                         break;
580                 }
581         }
582
583         if (!found) {
584                 dev_err(&pdev->dev, "No matching HID found in supported list\n");
585                 return -ENODEV;
586         }
587
588         card->dev = &pdev->dev;
589         sprintf(drv->codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
590
591         /* set correct codec name */
592         for (i = 0; i < ARRAY_SIZE(cht_dailink); i++)
593                 if (!strcmp(card->dai_link[i].codec_name, "i2c-10EC5645:00")) {
594                         card->dai_link[i].codec_name = drv->codec_name;
595                         dai_index = i;
596                 }
597
598         /* fixup codec name based on HID */
599         i2c_name = sst_acpi_find_name_from_hid(mach->id);
600         if (i2c_name != NULL) {
601                 snprintf(cht_rt5645_codec_name, sizeof(cht_rt5645_codec_name),
602                         "%s%s", "i2c-", i2c_name);
603                 cht_dailink[dai_index].codec_name = cht_rt5645_codec_name;
604         }
605
606         /*
607          * swap SSP0 if bytcr is detected
608          * (will be overridden if DMI quirk is detected)
609          */
610         if (is_valleyview()) {
611                 struct sst_platform_info *p_info = mach->pdata;
612                 const struct sst_res_info *res_info = p_info->res_info;
613
614                 if (res_info->acpi_ipc_irq_index == 0)
615                         is_bytcr = true;
616         }
617
618         if (is_bytcr) {
619                 /*
620                  * Baytrail CR platforms may have CHAN package in BIOS, try
621                  * to find relevant routing quirk based as done on Windows
622                  * platforms. We have to read the information directly from the
623                  * BIOS, at this stage the card is not created and the links
624                  * with the codec driver/pdata are non-existent
625                  */
626
627                 struct acpi_chan_package chan_package;
628
629                 /* format specified: 2 64-bit integers */
630                 struct acpi_buffer format = {sizeof("NN"), "NN"};
631                 struct acpi_buffer state = {0, NULL};
632                 struct sst_acpi_package_context pkg_ctx;
633                 bool pkg_found = false;
634
635                 state.length = sizeof(chan_package);
636                 state.pointer = &chan_package;
637
638                 pkg_ctx.name = "CHAN";
639                 pkg_ctx.length = 2;
640                 pkg_ctx.format = &format;
641                 pkg_ctx.state = &state;
642                 pkg_ctx.data_valid = false;
643
644                 pkg_found = sst_acpi_find_package_from_hid(mach->id, &pkg_ctx);
645                 if (pkg_found) {
646                         if (chan_package.aif_value == 1) {
647                                 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
648                                 cht_rt5645_quirk |= CHT_RT5645_SSP0_AIF1;
649                         } else  if (chan_package.aif_value == 2) {
650                                 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
651                                 cht_rt5645_quirk |= CHT_RT5645_SSP0_AIF2;
652                         } else {
653                                 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
654                                 pkg_found = false;
655                         }
656                 }
657
658                 if (!pkg_found) {
659                         /* no BIOS indications, assume SSP0-AIF2 connection */
660                         cht_rt5645_quirk |= CHT_RT5645_SSP0_AIF2;
661                 }
662         }
663
664         /* check quirks before creating card */
665         dmi_check_system(cht_rt5645_quirk_table);
666         log_quirks(&pdev->dev);
667
668         if ((cht_rt5645_quirk & CHT_RT5645_SSP2_AIF2) ||
669                 (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
670
671                 /* fixup codec aif name */
672                 snprintf(cht_rt5645_codec_aif_name,
673                         sizeof(cht_rt5645_codec_aif_name),
674                         "%s", "rt5645-aif2");
675
676                 cht_dailink[dai_index].codec_dai_name =
677                         cht_rt5645_codec_aif_name;
678         }
679
680         if ((cht_rt5645_quirk & CHT_RT5645_SSP0_AIF1) ||
681                 (cht_rt5645_quirk & CHT_RT5645_SSP0_AIF2)) {
682
683                 /* fixup cpu dai name name */
684                 snprintf(cht_rt5645_cpu_dai_name,
685                         sizeof(cht_rt5645_cpu_dai_name),
686                         "%s", "ssp0-port");
687
688                 cht_dailink[dai_index].cpu_dai_name =
689                         cht_rt5645_cpu_dai_name;
690         }
691
692         if (is_valleyview()) {
693                 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
694                 if (IS_ERR(drv->mclk)) {
695                         dev_err(&pdev->dev,
696                                 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
697                                 PTR_ERR(drv->mclk));
698                         return PTR_ERR(drv->mclk);
699                 }
700         }
701
702         snd_soc_card_set_drvdata(card, drv);
703         ret_val = devm_snd_soc_register_card(&pdev->dev, card);
704         if (ret_val) {
705                 dev_err(&pdev->dev,
706                         "snd_soc_register_card failed %d\n", ret_val);
707                 return ret_val;
708         }
709         platform_set_drvdata(pdev, card);
710         return ret_val;
711 }
712
713 static struct platform_driver snd_cht_mc_driver = {
714         .driver = {
715                 .name = "cht-bsw-rt5645",
716         },
717         .probe = snd_cht_mc_probe,
718 };
719
720 module_platform_driver(snd_cht_mc_driver)
721
722 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
723 MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
724 MODULE_LICENSE("GPL v2");
725 MODULE_ALIAS("platform:cht-bsw-rt5645");