Initial commit
[kernel/linux-3.0.git] / sound / soc / samsung / midas_wm1811.c
1 /*
2  *  midas_wm1811.c
3  *
4  *  Copyright (c) 2011 Samsung Electronics Co. Ltd
5  *
6  *  This program is free software; you can redistribute  it and/or modify it
7  *  under  the terms of  the GNU General  Public License as published by the
8  *  Free Software Foundation;  either version 2 of the  License, or (at your
9  *  option) any later version.
10  */
11 #include <linux/platform_device.h>
12 #include <linux/clk.h>
13 #include <linux/io.h>
14 #include <linux/gpio.h>
15 #include <linux/delay.h>
16 #include <linux/slab.h>
17 #include <linux/workqueue.h>
18 #include <linux/input.h>
19 #include <linux/wakelock.h>
20 #include <linux/suspend.h>
21
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/jack.h>
27
28 #include <mach/regs-clock.h>
29 #include <mach/pmu.h>
30 #include <mach/midas-sound.h>
31 #ifdef CONFIG_MACH_GC1
32 #include <mach/gc1-jack.h>
33 #endif
34
35 #include <linux/mfd/wm8994/core.h>
36 #include <linux/mfd/wm8994/registers.h>
37 #include <linux/mfd/wm8994/pdata.h>
38 #include <linux/mfd/wm8994/gpio.h>
39
40 #if defined(CONFIG_SND_USE_MUIC_SWITCH)
41 #include <linux/mfd/max77693-private.h>
42 #endif
43
44
45 #include "i2s.h"
46 #include "s3c-i2s-v2.h"
47 #include "../codecs/wm8994.h"
48
49
50 #define MIDAS_DEFAULT_MCLK1     24000000
51 #define MIDAS_DEFAULT_MCLK2     32768
52 #define MIDAS_DEFAULT_SYNC_CLK  11289600
53
54 #define WM1811_JACKDET_MODE_NONE  0x0000
55 #define WM1811_JACKDET_MODE_JACK  0x0100
56 #define WM1811_JACKDET_MODE_MIC   0x0080
57 #define WM1811_JACKDET_MODE_AUDIO 0x0180
58
59 #define WM1811_JACKDET_BTN0     0x04
60 #define WM1811_JACKDET_BTN1     0x10
61 #define WM1811_JACKDET_BTN2     0x08
62
63
64 static struct wm8958_micd_rate midas_det_rates[] = {
65         { MIDAS_DEFAULT_MCLK2,     true,  0,  0 },
66         { MIDAS_DEFAULT_MCLK2,    false,  0,  0 },
67         { MIDAS_DEFAULT_SYNC_CLK,  true,  7,  7 },
68         { MIDAS_DEFAULT_SYNC_CLK, false,  7,  7 },
69 };
70
71 static struct wm8958_micd_rate midas_jackdet_rates[] = {
72         { MIDAS_DEFAULT_MCLK2,     true,  0,  0 },
73         { MIDAS_DEFAULT_MCLK2,    false,  0,  0 },
74         { MIDAS_DEFAULT_SYNC_CLK,  true, 12, 12 },
75         { MIDAS_DEFAULT_SYNC_CLK, false,  7,  8 },
76 };
77
78 static int aif2_mode;
79 const char *aif2_mode_text[] = {
80         "Slave", "Master"
81 };
82
83 static int kpcs_mode = 2;
84 const char *kpcs_mode_text[] = {
85         "Off", "On"
86 };
87
88 static int input_clamp;
89 const char *input_clamp_text[] = {
90         "Off", "On"
91 };
92
93 static int lineout_mode;
94 const char *lineout_mode_text[] = {
95         "Off", "On"
96 };
97
98 #ifndef CONFIG_SEC_DEV_JACK
99 /* To support PBA function test */
100 static struct class *jack_class;
101 static struct device *jack_dev;
102 #endif
103
104 #ifdef SND_USE_BIAS_LEVEL
105 static bool midas_fll1_active;
106 struct snd_soc_dai *midas_aif1_dai;
107 #endif
108
109 struct wm1811_machine_priv {
110         struct snd_soc_jack jack;
111         struct snd_soc_codec *codec;
112         struct delayed_work mic_work;
113         struct wake_lock jackdet_wake_lock;
114 };
115
116 #ifdef CONFIG_MACH_GC1
117 static struct snd_soc_codec *wm1811_codec;
118
119 void set_wm1811_micbias2(bool on)
120 {
121         if (wm1811_codec == NULL) {
122                 pr_err(KERN_ERR "WM1811 MICBIAS2 set error!\n");
123                 return;
124         }
125
126         if (on) {
127                 snd_soc_update_bits(wm1811_codec, WM8994_POWER_MANAGEMENT_1,
128                         WM8994_MICB2_ENA, WM8994_MICB2_ENA);
129         } else {
130                 snd_soc_update_bits(wm1811_codec, WM8994_POWER_MANAGEMENT_1,
131                         WM8994_MICB2_ENA, 0);
132
133         }
134 return;
135 }
136 EXPORT_SYMBOL(set_wm1811_micbias2);
137 #endif
138
139 static void midas_gpio_init(void)
140 {
141         int err;
142 #ifdef CONFIG_SND_SOC_USE_EXTERNAL_MIC_BIAS
143         /* Main Microphone BIAS */
144         err = gpio_request(GPIO_MIC_BIAS_EN, "MAIN MIC");
145         if (err) {
146                 pr_err(KERN_ERR "MIC_BIAS_EN GPIO set error!\n");
147                 return;
148         }
149         gpio_direction_output(GPIO_MIC_BIAS_EN, 1);
150         gpio_set_value(GPIO_MIC_BIAS_EN, 0);
151         gpio_free(GPIO_MIC_BIAS_EN);
152 #endif
153
154 #ifdef CONFIG_SND_USE_SUB_MIC
155         /* Sub Microphone BIAS */
156         err = gpio_request(GPIO_SUB_MIC_BIAS_EN, "SUB MIC");
157         if (err) {
158                 pr_err(KERN_ERR "SUB_MIC_BIAS_EN GPIO set error!\n");
159                 return;
160         }
161         gpio_direction_output(GPIO_SUB_MIC_BIAS_EN, 1);
162         gpio_set_value(GPIO_SUB_MIC_BIAS_EN, 0);
163         gpio_free(GPIO_SUB_MIC_BIAS_EN);
164 #endif
165
166 #ifdef CONFIG_SND_USE_THIRD_MIC
167         /* Third Microphone BIAS */
168         err = gpio_request(GPIO_THIRD_MIC_BIAS_EN, "THIRD MIC");
169         if (err) {
170                 pr_err(KERN_ERR "THIRD_MIC_BIAS_EN GPIO set error!\n");
171                 return;
172         }
173         gpio_direction_output(GPIO_THIRD_MIC_BIAS_EN, 1);
174         gpio_set_value(GPIO_THIRD_MIC_BIAS_EN, 0);
175         gpio_free(GPIO_THIRD_MIC_BIAS_EN);
176 #endif
177
178 #ifdef CONFIG_FM_RADIO
179         /* FM/Third Mic GPIO */
180         err = gpio_request(GPIO_FM_MIC_SW, "GPL0");
181         if (err) {
182                 pr_err(KERN_ERR "FM/THIRD_MIC Switch GPIO set error!\n");
183                 return;
184         }
185         gpio_direction_output(GPIO_FM_MIC_SW, 1);
186         gpio_set_value(GPIO_FM_MIC_SW, 0);
187         gpio_free(GPIO_FM_MIC_SW);
188 #endif
189
190 #ifdef CONFIG_SND_USE_LINEOUT_SWITCH
191         err = gpio_request(GPIO_LINEOUT_EN, "LINEOUT_EN");
192         if (err) {
193                 pr_err(KERN_ERR "LINEOUT_EN GPIO set error!\n");
194                 return;
195         }
196         gpio_direction_output(GPIO_LINEOUT_EN, 1);
197         gpio_set_value(GPIO_LINEOUT_EN, 0);
198         gpio_free(GPIO_LINEOUT_EN);
199 #endif
200 }
201
202 static const struct soc_enum lineout_mode_enum[] = {
203         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lineout_mode_text), lineout_mode_text),
204 };
205
206 static int get_lineout_mode(struct snd_kcontrol *kcontrol,
207         struct snd_ctl_elem_value *ucontrol)
208 {
209         ucontrol->value.integer.value[0] = lineout_mode;
210         return 0;
211 }
212
213 static int set_lineout_mode(struct snd_kcontrol *kcontrol,
214         struct snd_ctl_elem_value *ucontrol)
215 {
216         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
217
218         lineout_mode = ucontrol->value.integer.value[0];
219         dev_dbg(codec->dev, "set lineout mode : %s\n",
220                 lineout_mode_text[lineout_mode]);
221         return 0;
222
223 }
224 static const struct soc_enum aif2_mode_enum[] = {
225         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(aif2_mode_text), aif2_mode_text),
226 };
227
228 static const struct soc_enum kpcs_mode_enum[] = {
229         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(kpcs_mode_text), kpcs_mode_text),
230 };
231
232 static const struct soc_enum input_clamp_enum[] = {
233         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_clamp_text), input_clamp_text),
234 };
235
236 static int get_aif2_mode(struct snd_kcontrol *kcontrol,
237         struct snd_ctl_elem_value *ucontrol)
238 {
239         ucontrol->value.integer.value[0] = aif2_mode;
240         return 0;
241 }
242
243 static int set_aif2_mode(struct snd_kcontrol *kcontrol,
244         struct snd_ctl_elem_value *ucontrol)
245 {
246         if (aif2_mode == ucontrol->value.integer.value[0])
247                 return 0;
248
249         aif2_mode = ucontrol->value.integer.value[0];
250
251         pr_info("set aif2 mode : %s\n", aif2_mode_text[aif2_mode]);
252
253         return 0;
254 }
255
256 static int get_kpcs_mode(struct snd_kcontrol *kcontrol,
257         struct snd_ctl_elem_value *ucontrol)
258 {
259         ucontrol->value.integer.value[0] = kpcs_mode;
260         return 0;
261 }
262
263 static int set_kpcs_mode(struct snd_kcontrol *kcontrol,
264         struct snd_ctl_elem_value *ucontrol)
265 {
266
267         kpcs_mode = ucontrol->value.integer.value[0];
268
269         pr_info("set kpcs mode : %d\n", kpcs_mode);
270
271         return 0;
272 }
273
274 static int get_input_clamp(struct snd_kcontrol *kcontrol,
275         struct snd_ctl_elem_value *ucontrol)
276 {
277         ucontrol->value.integer.value[0] = input_clamp;
278         return 0;
279 }
280
281 static int set_input_clamp(struct snd_kcontrol *kcontrol,
282         struct snd_ctl_elem_value *ucontrol)
283 {
284         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
285
286         input_clamp = ucontrol->value.integer.value[0];
287
288         if (input_clamp) {
289                 snd_soc_update_bits(codec, WM8994_INPUT_MIXER_1,
290                                 WM8994_INPUTS_CLAMP, WM8994_INPUTS_CLAMP);
291                 msleep(100);
292         } else {
293                 snd_soc_update_bits(codec, WM8994_INPUT_MIXER_1,
294                                 WM8994_INPUTS_CLAMP, 0);
295         }
296         pr_info("set fm input_clamp : %s\n", input_clamp_text[input_clamp]);
297
298         return 0;
299 }
300
301
302 static int midas_ext_micbias(struct snd_soc_dapm_widget *w,
303                              struct snd_kcontrol *kcontrol, int event)
304 {
305         struct snd_soc_codec *codec = w->codec;
306
307         dev_dbg(codec->dev, "%s event is %02X", w->name, event);
308
309 #ifdef CONFIG_SND_SOC_USE_EXTERNAL_MIC_BIAS
310         switch (event) {
311         case SND_SOC_DAPM_PRE_PMU:
312                 gpio_set_value(GPIO_MIC_BIAS_EN, 1);
313                 msleep(150);
314                 break;
315         case SND_SOC_DAPM_POST_PMD:
316                 gpio_set_value(GPIO_MIC_BIAS_EN, 0);
317                 break;
318         }
319 #endif
320         return 0;
321 }
322
323 static int midas_ext_submicbias(struct snd_soc_dapm_widget *w,
324                                 struct snd_kcontrol *kcontrol, int event)
325 {
326         struct snd_soc_codec *codec = w->codec;
327
328         dev_dbg(codec->dev, "%s event is %02X", w->name, event);
329
330 #ifdef CONFIG_SND_USE_SUB_MIC
331         switch (event) {
332         case SND_SOC_DAPM_PRE_PMU:
333                 gpio_set_value(GPIO_SUB_MIC_BIAS_EN, 1);
334                 msleep(150);
335                 break;
336         case SND_SOC_DAPM_POST_PMD:
337                 gpio_set_value(GPIO_SUB_MIC_BIAS_EN, 0);
338                 break;
339         }
340 #endif
341         return 0;
342 }
343
344 static int midas_ext_thirdmicbias(struct snd_soc_dapm_widget *w,
345                                 struct snd_kcontrol *kcontrol, int event)
346 {
347         struct snd_soc_codec *codec = w->codec;
348
349         dev_dbg(codec->dev, "%s event is %02X", w->name, event);
350
351 #ifdef CONFIG_SND_USE_THIRD_MIC
352         switch (event) {
353         case SND_SOC_DAPM_PRE_PMU:
354                 gpio_set_value(GPIO_THIRD_MIC_BIAS_EN, 1);
355                 break;
356         case SND_SOC_DAPM_POST_PMD:
357                 gpio_set_value(GPIO_THIRD_MIC_BIAS_EN, 0);
358                 break;
359         }
360 #endif
361         return 0;
362 }
363
364 /*
365  * midas_ext_spkmode :
366  * For phone device have 1 external speaker
367  * should mix LR data in a speaker mixer (mono setting)
368  */
369 static int midas_ext_spkmode(struct snd_soc_dapm_widget *w,
370                              struct snd_kcontrol *kcontrol, int event)
371 {
372         int ret = 0;
373 #ifndef CONFIG_SND_USE_STEREO_SPEAKER
374         struct snd_soc_codec *codec = w->codec;
375
376         ret = snd_soc_update_bits(codec, WM8994_SPKOUT_MIXERS,
377                                   WM8994_SPKMIXR_TO_SPKOUTL_MASK,
378                                   WM8994_SPKMIXR_TO_SPKOUTL);
379 #endif
380         return ret;
381 }
382
383 static int midas_lineout_switch(struct snd_soc_dapm_widget *w,
384                              struct snd_kcontrol *kcontrol, int event)
385 {
386         struct snd_soc_codec *codec = w->codec;
387
388         dev_dbg(codec->dev, "%s event is %02X", w->name, event);
389
390 #if defined(CONFIG_SND_USE_MUIC_SWITCH)
391         switch (event) {
392         case SND_SOC_DAPM_POST_PMU:
393                 msleep(150);
394                 max77693_muic_set_audio_switch(1);
395                 break;
396         case SND_SOC_DAPM_PRE_PMD:
397                 max77693_muic_set_audio_switch(0);
398                 break;
399         }
400 #endif
401
402 #ifdef CONFIG_SND_USE_LINEOUT_SWITCH
403         switch (event) {
404         case SND_SOC_DAPM_POST_PMU:
405                 gpio_set_value(GPIO_LINEOUT_EN, 1);
406                 break;
407         case SND_SOC_DAPM_PRE_PMD:
408                 gpio_set_value(GPIO_LINEOUT_EN, 0);
409                 break;
410         }
411 #endif
412         return 0;
413 }
414
415 static void midas_micd_set_rate(struct snd_soc_codec *codec)
416 {
417         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
418         int best, i, sysclk, val;
419         bool idle;
420         const struct wm8958_micd_rate *rates = NULL;
421         int num_rates = 0;
422
423         idle = !wm8994->jack_mic;
424
425         sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
426         if (sysclk & WM8994_SYSCLK_SRC)
427                 sysclk = wm8994->aifclk[1];
428         else
429                 sysclk = wm8994->aifclk[0];
430
431         if (wm8994->jackdet) {
432                 rates = midas_jackdet_rates;
433                 num_rates = ARRAY_SIZE(midas_jackdet_rates);
434                 wm8994->pdata->micd_rates = midas_jackdet_rates;
435                 wm8994->pdata->num_micd_rates = num_rates;
436         } else {
437                 rates = midas_det_rates;
438                 num_rates = ARRAY_SIZE(midas_det_rates);
439                 wm8994->pdata->micd_rates = midas_det_rates;
440                 wm8994->pdata->num_micd_rates = num_rates;
441         }
442
443         best = 0;
444         for (i = 0; i < num_rates; i++) {
445                 if (rates[i].idle != idle)
446                         continue;
447                 if (abs(rates[i].sysclk - sysclk) <
448                     abs(rates[best].sysclk - sysclk))
449                         best = i;
450                 else if (rates[best].idle != idle)
451                         best = i;
452         }
453
454         val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
455                 | rates[best].rate << WM8958_MICD_RATE_SHIFT;
456
457         snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
458                             WM8958_MICD_BIAS_STARTTIME_MASK |
459                             WM8958_MICD_RATE_MASK, val);
460 }
461
462 #ifdef SND_USE_BIAS_LEVEL
463 static void midas_start_fll1(struct snd_soc_dai *aif1_dai)
464 {
465         int ret;
466         if (midas_fll1_active)
467                 return;
468
469         dev_info(aif1_dai->dev, "Moving to audio clocking settings\n");
470
471         /* Switch AIF1 to MCLK2 while we bring stuff up */
472         ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
473                                      MIDAS_DEFAULT_MCLK2, SND_SOC_CLOCK_IN);
474         if (ret < 0)
475                 dev_err(aif1_dai->dev, "Unable to switch to MCLK2: %d\n", ret);
476
477         /* Start the 24MHz clock to provide a high frequency reference to
478          * provide a high frequency reference for the FLL, giving improved
479          * performance.
480          */
481         midas_snd_set_mclk(true, true);
482
483         /* Switch the FLL */
484         ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
485                                   WM8994_FLL_SRC_MCLK1, MIDAS_DEFAULT_MCLK1,
486                                   MIDAS_DEFAULT_SYNC_CLK);
487         if (ret < 0)
488                 dev_err(aif1_dai->dev, "Unable to start FLL1: %d\n", ret);
489
490         /* Then switch AIF1CLK to it */
491         ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_FLL1,
492                                      MIDAS_DEFAULT_SYNC_CLK, SND_SOC_CLOCK_IN);
493         if (ret < 0)
494                 dev_err(aif1_dai->dev, "Unable to switch to FLL1: %d\n", ret);
495
496         midas_fll1_active = true;
497 }
498 #endif
499
500 static void midas_micdet(u16 status, void *data)
501 {
502         struct wm1811_machine_priv *wm1811 = data;
503         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(wm1811->codec);
504         int report;
505
506
507         wake_lock_timeout(&wm1811->jackdet_wake_lock, 5 * HZ);
508
509         /* Either nothing present or just starting detection */
510         if (!(status & WM8958_MICD_STS)) {
511                 if (!wm8994->jackdet) {
512                         /* If nothing present then clear our statuses */
513                         dev_dbg(wm1811->codec->dev, "Detected open circuit\n");
514                         wm8994->jack_mic = false;
515                         wm8994->mic_detecting = true;
516
517                         midas_micd_set_rate(wm1811->codec);
518
519                         snd_soc_jack_report(wm8994->micdet[0].jack, 0,
520                                             wm8994->btn_mask |
521                                              SND_JACK_HEADSET);
522                 }
523                 /*ToDo*/
524                 /*return;*/
525         }
526
527         /* If the measurement is showing a high impedence we've got a
528          * microphone.
529          */
530         if (wm8994->mic_detecting && (status & 0x400)) {
531                 dev_info(wm1811->codec->dev, "Detected microphone\n");
532
533                 wm8994->mic_detecting = false;
534                 wm8994->jack_mic = true;
535
536                 midas_micd_set_rate(wm1811->codec);
537
538                 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
539                                     SND_JACK_HEADSET);
540         }
541
542         if (wm8994->mic_detecting && status & 0x4) {
543                 dev_info(wm1811->codec->dev, "Detected headphone\n");
544                 wm8994->mic_detecting = false;
545
546                 midas_micd_set_rate(wm1811->codec);
547
548                 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
549                                     SND_JACK_HEADSET);
550
551                 /* If we have jackdet that will detect removal */
552                 if (wm8994->jackdet) {
553                         mutex_lock(&wm8994->accdet_lock);
554
555                         snd_soc_update_bits(wm1811->codec, WM8958_MIC_DETECT_1,
556                                             WM8958_MICD_ENA, 0);
557
558                         if (wm8994->active_refcount) {
559                                 snd_soc_update_bits(wm1811->codec,
560                                         WM8994_ANTIPOP_2,
561                                         WM1811_JACKDET_MODE_MASK,
562                                         WM1811_JACKDET_MODE_AUDIO);
563                         }
564
565                         mutex_unlock(&wm8994->accdet_lock);
566
567                         if (wm8994->pdata->jd_ext_cap) {
568                                 mutex_lock(&wm1811->codec->mutex);
569                                 snd_soc_dapm_disable_pin(&wm1811->codec->dapm,
570                                                          "MICBIAS2");
571                                 snd_soc_dapm_sync(&wm1811->codec->dapm);
572                                 mutex_unlock(&wm1811->codec->mutex);
573                         }
574                 }
575         }
576
577         /* Report short circuit as a button */
578         if (wm8994->jack_mic) {
579                 report = 0;
580                 if (status & WM1811_JACKDET_BTN0)
581                         report |= SND_JACK_BTN_0;
582
583                 if (status & WM1811_JACKDET_BTN1)
584                         report |= SND_JACK_BTN_1;
585
586                 if (status & WM1811_JACKDET_BTN2)
587                         report |= SND_JACK_BTN_2;
588
589                 dev_dbg(wm1811->codec->dev, "Detected Button: %08x (%08X)\n",
590                         report, status);
591
592                 snd_soc_jack_report(wm8994->micdet[0].jack, report,
593                                     wm8994->btn_mask);
594         }
595 }
596
597 #ifdef CONFIG_SND_SAMSUNG_I2S_MASTER
598 static int set_epll_rate(unsigned long rate)
599 {
600         struct clk *fout_epll;
601
602         fout_epll = clk_get(NULL, "fout_epll");
603         if (IS_ERR(fout_epll)) {
604                 printk(KERN_ERR "%s: failed to get fout_epll\n", __func__);
605                 return -ENOENT;
606         }
607
608         if (rate == clk_get_rate(fout_epll))
609                 goto out;
610
611         clk_set_rate(fout_epll, rate);
612 out:
613         clk_put(fout_epll);
614
615         return 0;
616 }
617 #endif /* CONFIG_SND_SAMSUNG_I2S_MASTER */
618
619 #ifndef CONFIG_SND_SAMSUNG_I2S_MASTER
620 static int midas_wm1811_aif1_hw_params(struct snd_pcm_substream *substream,
621         struct snd_pcm_hw_params *params)
622 {
623         struct snd_soc_pcm_runtime *rtd = substream->private_data;
624         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
625         struct snd_soc_dai *codec_dai = rtd->codec_dai;
626         unsigned int pll_out;
627         int ret;
628
629         dev_info(codec_dai->dev, "%s ++\n", __func__);
630         /* AIF1CLK should be >=3MHz for optimal performance */
631         if (params_rate(params) == 8000 || params_rate(params) == 11025)
632                 pll_out = params_rate(params) * 512;
633         else
634                 pll_out = params_rate(params) * 256;
635
636         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
637                                         | SND_SOC_DAIFMT_NB_NF
638                                         | SND_SOC_DAIFMT_CBM_CFM);
639         if (ret < 0)
640                 return ret;
641
642         /* Set the cpu DAI configuration */
643         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
644                                         | SND_SOC_DAIFMT_NB_NF
645                                         | SND_SOC_DAIFMT_CBM_CFM);
646         if (ret < 0)
647                 return ret;
648
649 #ifndef SND_USE_BIAS_LEVEL
650         /* Switch the FLL */
651         ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
652                                   WM8994_FLL_SRC_MCLK1, MIDAS_DEFAULT_MCLK1,
653                                   pll_out);
654         if (ret < 0)
655                 dev_err(codec_dai->dev, "Unable to start FLL1: %d\n", ret);
656
657         ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL1,
658                                      pll_out, SND_SOC_CLOCK_IN);
659         if (ret < 0) {
660                 dev_err(codec_dai->dev, "Unable to switch to FLL1: %d\n", ret);
661                 return ret;
662         }
663
664         ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK,
665                                      0, MOD_OPCLK_PCLK);
666         if (ret < 0)
667                 return ret;
668 #else
669         midas_start_fll1(codec_dai);
670 #endif
671
672         if (ret < 0)
673                 return ret;
674
675         dev_info(codec_dai->dev, "%s --\n", __func__);
676
677         return 0;
678 }
679 #else /* CONFIG_SND_SAMSUNG_I2S_MASTER */
680 static int midas_wm1811_aif1_hw_params(struct snd_pcm_substream *substream,
681         struct snd_pcm_hw_params *params)
682 {
683         struct snd_soc_pcm_runtime *rtd = substream->private_data;
684         struct snd_soc_dai *codec_dai = rtd->codec_dai;
685         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
686         int bfs, psr, rfs, ret;
687         unsigned long rclk;
688
689         switch (params_format(params)) {
690         case SNDRV_PCM_FORMAT_U24:
691         case SNDRV_PCM_FORMAT_S24:
692                 bfs = 48;
693                 break;
694         case SNDRV_PCM_FORMAT_U16_LE:
695         case SNDRV_PCM_FORMAT_S16_LE:
696                 bfs = 32;
697                 break;
698         default:
699                 return -EINVAL;
700         }
701
702         switch (params_rate(params)) {
703         case 16000:
704         case 22050:
705         case 24000:
706         case 32000:
707         case 44100:
708         case 48000:
709         case 88200:
710         case 96000:
711                 if (bfs == 48)
712                         rfs = 384;
713                 else
714                         rfs = 256;
715                 break;
716         case 64000:
717                 rfs = 384;
718                 break;
719         case 8000:
720         case 11025:
721         case 12000:
722                 if (bfs == 48)
723                         rfs = 768;
724                 else
725                         rfs = 512;
726                 break;
727         default:
728                 return -EINVAL;
729         }
730
731         rclk = params_rate(params) * rfs;
732
733         switch (rclk) {
734         case 4096000:
735         case 5644800:
736         case 6144000:
737         case 8467200:
738         case 9216000:
739                 psr = 8;
740                 break;
741         case 8192000:
742         case 11289600:
743         case 12288000:
744         case 16934400:
745         case 18432000:
746                 psr = 4;
747                 break;
748         case 22579200:
749         case 24576000:
750         case 33868800:
751         case 36864000:
752                 psr = 2;
753                 break;
754         case 67737600:
755         case 73728000:
756                 psr = 1;
757                 break;
758         default:
759                 printk(KERN_INFO "Not yet supported!\n");
760                 return -EINVAL;
761         }
762
763         set_epll_rate(rclk * psr);
764
765         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
766                                         | SND_SOC_DAIFMT_NB_NF
767                                         | SND_SOC_DAIFMT_CBS_CFS);
768         if (ret < 0)
769                 return ret;
770
771         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
772                                         | SND_SOC_DAIFMT_NB_NF
773                                         | SND_SOC_DAIFMT_CBS_CFS);
774         if (ret < 0)
775                 return ret;
776
777         ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK1,
778                                      rclk, SND_SOC_CLOCK_IN);
779         if (ret < 0)
780                 return ret;
781
782         ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
783                                      0, SND_SOC_CLOCK_OUT);
784         if (ret < 0)
785                 return ret;
786
787         ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_BCLK, bfs);
788         if (ret < 0)
789                 return ret;
790
791         return 0;
792 }
793 #endif /* CONFIG_SND_SAMSUNG_I2S_MASTER */
794
795 /*
796  * Midas WM1811 DAI operations.
797  */
798 static struct snd_soc_ops midas_wm1811_aif1_ops = {
799         .hw_params = midas_wm1811_aif1_hw_params,
800 };
801
802 static int midas_wm1811_aif2_hw_params(struct snd_pcm_substream *substream,
803                                         struct snd_pcm_hw_params *params)
804 {
805         struct snd_soc_pcm_runtime *rtd = substream->private_data;
806         struct snd_soc_dai *codec_dai = rtd->codec_dai;
807         int ret;
808         int prate;
809         int bclk;
810
811         dev_info(codec_dai->dev, "%s ++\n", __func__);
812         prate = params_rate(params);
813         switch (params_rate(params)) {
814         case 8000:
815         case 16000:
816                break;
817         default:
818                 dev_warn(codec_dai->dev, "Unsupported LRCLK %d, falling back to 8000Hz\n",
819                                 (int)params_rate(params));
820                 prate = 8000;
821         }
822
823 #if defined(CONFIG_LTE_MODEM_CMC221) || defined(CONFIG_MACH_M0_CTC)
824 #if defined(CONFIG_MACH_C1_KOR_LGT)
825         /* Set the codec DAI configuration */
826         if (aif2_mode == 0) {
827                 if (kpcs_mode == 1)
828                         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
829                                 | SND_SOC_DAIFMT_NB_NF
830                                 | SND_SOC_DAIFMT_CBS_CFS);
831                 else
832                         ret = snd_soc_dai_set_fmt(codec_dai,
833                                 SND_SOC_DAIFMT_DSP_A
834                                 | SND_SOC_DAIFMT_IB_NF
835                                 | SND_SOC_DAIFMT_CBS_CFS);
836         } else
837                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
838                                 | SND_SOC_DAIFMT_NB_NF
839                                 | SND_SOC_DAIFMT_CBM_CFM);
840 #else
841         if (aif2_mode == 0)
842                 /* Set the codec DAI configuration */
843                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A
844                                 | SND_SOC_DAIFMT_IB_NF
845                                 | SND_SOC_DAIFMT_CBS_CFS);
846         else
847                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A
848                                 | SND_SOC_DAIFMT_IB_NF
849                                 | SND_SOC_DAIFMT_CBM_CFM);
850 #endif
851 #else
852         /* Set the codec DAI configuration, aif2_mode:0 is slave */
853         if (aif2_mode == 0)
854                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
855                                         | SND_SOC_DAIFMT_NB_NF
856                                         | SND_SOC_DAIFMT_CBS_CFS);
857         else
858                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
859                                         | SND_SOC_DAIFMT_NB_NF
860                                         | SND_SOC_DAIFMT_CBM_CFM);
861 #endif
862
863         if (ret < 0)
864                 return ret;
865
866 #if defined(CONFIG_LTE_MODEM_CMC221)
867         if (kpcs_mode == 1) {
868                 switch (prate) {
869                 case 8000:
870                         bclk = 256000;
871                         break;
872                 case 16000:
873                         bclk = 512000;
874                         break;
875                 default:
876                         return -EINVAL;
877                 }
878         } else {
879         bclk = 2048000;
880         }
881 #elif defined(CONFIG_MACH_M0_CTC)
882         bclk = 2048000;
883 #else
884         switch (prate) {
885         case 8000:
886                 bclk = 256000;
887                 break;
888         case 16000:
889                 bclk = 512000;
890                 break;
891         default:
892                 return -EINVAL;
893         }
894 #endif
895
896 #ifdef SND_USE_BIAS_LEVEL
897         if (!midas_fll1_active)
898                 midas_start_fll1(midas_aif1_dai);
899 #endif
900
901         if (aif2_mode == 0) {
902                 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL2,
903                                         WM8994_FLL_SRC_BCLK,
904                                         bclk, prate * 256);
905         } else {
906                 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL2,
907                                           WM8994_FLL_SRC_MCLK1,
908                                           MIDAS_DEFAULT_MCLK1, prate * 256);
909         }
910
911         if (ret < 0)
912                 dev_err(codec_dai->dev, "Unable to configure FLL2: %d\n", ret);
913
914         ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL2,
915                                      prate * 256, SND_SOC_CLOCK_IN);
916         if (ret < 0)
917                 dev_err(codec_dai->dev, "Unable to switch to FLL2: %d\n", ret);
918
919         dev_info(codec_dai->dev, "%s --\n", __func__);
920         return 0;
921 }
922
923 static struct snd_soc_ops midas_wm1811_aif2_ops = {
924         .hw_params = midas_wm1811_aif2_hw_params,
925 };
926
927 static int midas_wm1811_aif3_hw_params(struct snd_pcm_substream *substream,
928                                         struct snd_pcm_hw_params *params)
929 {
930         pr_err("%s: enter\n", __func__);
931         return 0;
932 }
933
934 static struct snd_soc_ops midas_wm1811_aif3_ops = {
935         .hw_params = midas_wm1811_aif3_hw_params,
936 };
937
938 static const struct snd_kcontrol_new midas_controls[] = {
939         SOC_DAPM_PIN_SWITCH("HP"),
940         SOC_DAPM_PIN_SWITCH("SPK"),
941         SOC_DAPM_PIN_SWITCH("RCV"),
942         SOC_DAPM_PIN_SWITCH("FM In"),
943         SOC_DAPM_PIN_SWITCH("LINE"),
944         SOC_DAPM_PIN_SWITCH("HDMI"),
945         SOC_DAPM_PIN_SWITCH("Main Mic"),
946         SOC_DAPM_PIN_SWITCH("Sub Mic"),
947         SOC_DAPM_PIN_SWITCH("Third Mic"),
948         SOC_DAPM_PIN_SWITCH("Headset Mic"),
949
950         SOC_ENUM_EXT("AIF2 Mode", aif2_mode_enum[0],
951                 get_aif2_mode, set_aif2_mode),
952
953         SOC_ENUM_EXT("KPCS Mode", kpcs_mode_enum[0],
954                 get_kpcs_mode, set_kpcs_mode),
955
956         SOC_ENUM_EXT("Input Clamp", input_clamp_enum[0],
957                 get_input_clamp, set_input_clamp),
958
959         SOC_ENUM_EXT("LineoutSwitch Mode", lineout_mode_enum[0],
960                 get_lineout_mode, set_lineout_mode),
961
962 };
963
964 const struct snd_soc_dapm_widget midas_dapm_widgets[] = {
965         SND_SOC_DAPM_HP("HP", NULL),
966         SND_SOC_DAPM_SPK("SPK", midas_ext_spkmode),
967         SND_SOC_DAPM_SPK("RCV", NULL),
968         SND_SOC_DAPM_LINE("LINE", midas_lineout_switch),
969         SND_SOC_DAPM_LINE("HDMI", NULL),
970
971         SND_SOC_DAPM_MIC("Headset Mic", NULL),
972         SND_SOC_DAPM_MIC("Main Mic", midas_ext_micbias),
973         SND_SOC_DAPM_MIC("Sub Mic", midas_ext_submicbias),
974         SND_SOC_DAPM_MIC("Third Mic", midas_ext_thirdmicbias),
975         SND_SOC_DAPM_LINE("FM In", NULL),
976
977         SND_SOC_DAPM_INPUT("S5P RP"),
978 };
979
980 const struct snd_soc_dapm_route midas_dapm_routes[] = {
981         { "HP", NULL, "HPOUT1L" },
982         { "HP", NULL, "HPOUT1R" },
983
984         { "SPK", NULL, "SPKOUTLN" },
985         { "SPK", NULL, "SPKOUTLP" },
986         { "SPK", NULL, "SPKOUTRN" },
987         { "SPK", NULL, "SPKOUTRP" },
988
989         { "RCV", NULL, "HPOUT2N" },
990         { "RCV", NULL, "HPOUT2P" },
991
992         { "LINE", NULL, "LINEOUT2N" },
993         { "LINE", NULL, "LINEOUT2P" },
994
995         { "HDMI", NULL, "LINEOUT1N" },
996         { "HDMI", NULL, "LINEOUT1P" },
997
998         { "IN1LP", NULL, "MICBIAS1" },
999         { "IN1LN", NULL, "MICBIAS1" },
1000         { "MICBIAS1", NULL, "Main Mic" },
1001
1002         { "IN1RP", NULL, "Sub Mic" },
1003         { "IN1RN", NULL, "Sub Mic" },
1004
1005         { "IN2LP:VXRN", NULL, "MICBIAS2" },
1006         { "MICBIAS2", NULL, "Headset Mic" },
1007
1008         { "AIF1DAC1L", NULL, "S5P RP" },
1009         { "AIF1DAC1R", NULL, "S5P RP" },
1010
1011         { "IN2RN", NULL, "FM In" },
1012         { "IN2RP:VXRP", NULL, "FM In" },
1013
1014         { "IN2RN", NULL, "Third Mic" },
1015         { "IN2RP:VXRP", NULL, "Third Mic" },
1016 };
1017
1018 static struct snd_soc_dai_driver midas_ext_dai[] = {
1019         {
1020                 .name = "midas.cp",
1021                 .playback = {
1022                         .channels_min = 1,
1023                         .channels_max = 2,
1024                         .rate_min = 8000,
1025                         .rate_max = 16000,
1026                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1027                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1028                 },
1029                 .capture = {
1030                         .channels_min = 1,
1031                         .channels_max = 2,
1032                         .rate_min = 8000,
1033                         .rate_max = 16000,
1034                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1035                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1036                 },
1037         },
1038         {
1039                 .name = "midas.bt",
1040                 .playback = {
1041                         .channels_min = 1,
1042                         .channels_max = 2,
1043                         .rate_min = 8000,
1044                         .rate_max = 16000,
1045                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1046                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1047                 },
1048                 .capture = {
1049                         .channels_min = 1,
1050                         .channels_max = 2,
1051                         .rate_min = 8000,
1052                         .rate_max = 16000,
1053                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1054                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1055                 },
1056         },
1057 };
1058
1059 #ifndef CONFIG_SEC_DEV_JACK
1060 static ssize_t earjack_state_show(struct device *dev,
1061         struct device_attribute *attr, char *buf)
1062 {
1063         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1064         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1065
1066         int report = 0;
1067
1068         if ((wm8994->micdet[0].jack->status & SND_JACK_HEADPHONE) ||
1069                 (wm8994->micdet[0].jack->status & SND_JACK_HEADSET)) {
1070                 report = 1;
1071         }
1072
1073         return sprintf(buf, "%d\n", report);
1074 }
1075
1076 static ssize_t earjack_state_store(struct device *dev,
1077         struct device_attribute *attr, const char *buf, size_t size)
1078 {
1079         pr_info("%s : operate nothing\n", __func__);
1080
1081         return size;
1082 }
1083
1084 static ssize_t earjack_key_state_show(struct device *dev,
1085         struct device_attribute *attr, char *buf)
1086 {
1087         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1088         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1089
1090         int report = 0;
1091
1092         if (wm8994->micdet[0].jack->status & SND_JACK_BTN_0)
1093                 report = 1;
1094
1095         return sprintf(buf, "%d\n", report);
1096 }
1097
1098 static ssize_t earjack_key_state_store(struct device *dev,
1099         struct device_attribute *attr, const char *buf, size_t size)
1100 {
1101         pr_info("%s : operate nothing\n", __func__);
1102
1103         return size;
1104 }
1105
1106 static ssize_t earjack_select_jack_show(struct device *dev,
1107         struct device_attribute *attr, char *buf)
1108 {
1109         pr_info("%s : operate nothing\n", __func__);
1110
1111         return 0;
1112 }
1113
1114 static ssize_t earjack_select_jack_store(struct device *dev,
1115         struct device_attribute *attr, const char *buf, size_t size)
1116 {
1117         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1118         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1119
1120         wm8994->mic_detecting = false;
1121         wm8994->jack_mic = true;
1122
1123         midas_micd_set_rate(codec);
1124
1125         if ((!size) || (buf[0] != '1')) {
1126                 snd_soc_jack_report(wm8994->micdet[0].jack,
1127                                     0, SND_JACK_HEADSET);
1128                 dev_info(codec->dev, "Forced remove microphone\n");
1129         } else {
1130
1131                 snd_soc_jack_report(wm8994->micdet[0].jack,
1132                                     SND_JACK_HEADSET, SND_JACK_HEADSET);
1133                 dev_info(codec->dev, "Forced detect microphone\n");
1134         }
1135
1136         return size;
1137 }
1138
1139 static ssize_t reselect_jack_show(struct device *dev,
1140         struct device_attribute *attr, char *buf)
1141 {
1142         pr_info("%s : operate nothing\n", __func__);
1143         return 0;
1144 }
1145
1146 static ssize_t reselect_jack_store(struct device *dev,
1147         struct device_attribute *attr, const char *buf, size_t size)
1148 {
1149         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1150         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1151         int reg = 0;
1152
1153         reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
1154         if (reg == 0x402) {
1155                 dev_info(codec->dev, "Detected open circuit\n");
1156
1157                 snd_soc_update_bits(codec, WM8958_MICBIAS2,
1158                                     WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
1159                 /* Enable debounce while removed */
1160                 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
1161                                     WM1811_JACKDET_DB, WM1811_JACKDET_DB);
1162
1163                 wm8994->mic_detecting = false;
1164                 wm8994->jack_mic = false;
1165                 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
1166                                     WM8958_MICD_ENA, 0);
1167
1168                 if (wm8994->active_refcount) {
1169                         snd_soc_update_bits(codec,
1170                                 WM8994_ANTIPOP_2,
1171                                 WM1811_JACKDET_MODE_MASK,
1172                                 WM1811_JACKDET_MODE_AUDIO);
1173                 } else {
1174                         snd_soc_update_bits(codec,
1175                                 WM8994_ANTIPOP_2,
1176                                 WM1811_JACKDET_MODE_MASK,
1177                                 WM1811_JACKDET_MODE_JACK);
1178                 }
1179
1180                 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
1181                                     SND_JACK_MECHANICAL | SND_JACK_HEADSET |
1182                                     wm8994->btn_mask);
1183         }
1184         return size;
1185 }
1186
1187 static DEVICE_ATTR(reselect_jack, S_IRUGO | S_IWUSR | S_IWGRP,
1188                 reselect_jack_show, reselect_jack_store);
1189
1190 static DEVICE_ATTR(select_jack, S_IRUGO | S_IWUSR | S_IWGRP,
1191                    earjack_select_jack_show, earjack_select_jack_store);
1192
1193 static DEVICE_ATTR(key_state, S_IRUGO | S_IWUSR | S_IWGRP,
1194                    earjack_key_state_show, earjack_key_state_store);
1195
1196 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR | S_IWGRP,
1197                    earjack_state_show, earjack_state_store);
1198 #endif
1199
1200 static int midas_wm1811_init_paiftx(struct snd_soc_pcm_runtime *rtd)
1201 {
1202         struct snd_soc_codec *codec = rtd->codec;
1203         struct wm1811_machine_priv *wm1811
1204                 = snd_soc_card_get_drvdata(codec->card);
1205         struct snd_soc_dai *aif1_dai = rtd->codec_dai;
1206         struct wm8994 *control = codec->control_data;
1207         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1208         int ret;
1209
1210 #ifdef SND_USE_BIAS_LEVEL
1211         midas_aif1_dai = aif1_dai;
1212 #endif
1213
1214 #ifdef CONFIG_MACH_GC1
1215         wm1811_codec = codec;
1216 #endif
1217
1218         midas_snd_set_mclk(true, false);
1219
1220         rtd->codec_dai->driver->playback.channels_max =
1221                                 rtd->cpu_dai->driver->playback.channels_max;
1222
1223         ret = snd_soc_add_controls(codec, midas_controls,
1224                                         ARRAY_SIZE(midas_controls));
1225
1226         ret = snd_soc_dapm_new_controls(&codec->dapm, midas_dapm_widgets,
1227                                            ARRAY_SIZE(midas_dapm_widgets));
1228         if (ret != 0)
1229                 dev_err(codec->dev, "Failed to add DAPM widgets: %d\n", ret);
1230
1231         ret = snd_soc_dapm_add_routes(&codec->dapm, midas_dapm_routes,
1232                                            ARRAY_SIZE(midas_dapm_routes));
1233         if (ret != 0)
1234                 dev_err(codec->dev, "Failed to add DAPM routes: %d\n", ret);
1235
1236         ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
1237                                      MIDAS_DEFAULT_MCLK2, SND_SOC_CLOCK_IN);
1238         if (ret < 0)
1239                 dev_err(codec->dev, "Failed to boot clocking\n");
1240
1241         /* Force AIF1CLK on as it will be master for jack detection */
1242         if (wm8994->revision > 1) {
1243                 ret = snd_soc_dapm_force_enable_pin(&codec->dapm, "AIF1CLK");
1244                 if (ret < 0)
1245                         dev_err(codec->dev, "Failed to enable AIF1CLK: %d\n",
1246                                         ret);
1247         }
1248
1249         ret = snd_soc_dapm_disable_pin(&codec->dapm, "S5P RP");
1250         if (ret < 0)
1251                 dev_err(codec->dev, "Failed to disable S5P RP: %d\n", ret);
1252
1253         snd_soc_dapm_ignore_suspend(&codec->dapm, "RCV");
1254         snd_soc_dapm_ignore_suspend(&codec->dapm, "SPK");
1255         snd_soc_dapm_ignore_suspend(&codec->dapm, "HP");
1256         snd_soc_dapm_ignore_suspend(&codec->dapm, "Headset Mic");
1257         snd_soc_dapm_ignore_suspend(&codec->dapm, "Sub Mic");
1258         snd_soc_dapm_ignore_suspend(&codec->dapm, "Main Mic");
1259         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF1DACDAT");
1260         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF2DACDAT");
1261         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF3DACDAT");
1262         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF1ADCDAT");
1263         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF2ADCDAT");
1264         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF3ADCDAT");
1265         snd_soc_dapm_ignore_suspend(&codec->dapm, "FM In");
1266         snd_soc_dapm_ignore_suspend(&codec->dapm, "LINE");
1267         snd_soc_dapm_ignore_suspend(&codec->dapm, "HDMI");
1268         snd_soc_dapm_ignore_suspend(&codec->dapm, "Third Mic");
1269
1270         wm1811->codec = codec;
1271
1272         midas_micd_set_rate(codec);
1273
1274 #ifdef CONFIG_SEC_DEV_JACK
1275         /* By default use idle_bias_off, will override for WM8994 */
1276         codec->dapm.idle_bias_off = 0;
1277 #else /* CONFIG_SEC_DEV_JACK */
1278         wm1811->jack.status = 0;
1279
1280         ret = snd_soc_jack_new(codec, "Midas Jack",
1281                                 SND_JACK_HEADSET | SND_JACK_BTN_0 |
1282                                 SND_JACK_BTN_1 | SND_JACK_BTN_2,
1283                                 &wm1811->jack);
1284
1285         if (ret < 0)
1286                 dev_err(codec->dev, "Failed to create jack: %d\n", ret);
1287
1288         ret = snd_jack_set_key(wm1811->jack.jack, SND_JACK_BTN_0, KEY_MEDIA);
1289
1290         if (ret < 0)
1291                 dev_err(codec->dev, "Failed to set KEY_MEDIA: %d\n", ret);
1292
1293         ret = snd_jack_set_key(wm1811->jack.jack, SND_JACK_BTN_1,
1294                                                         KEY_VOLUMEDOWN);
1295         if (ret < 0)
1296                 dev_err(codec->dev, "Failed to set KEY_VOLUMEUP: %d\n", ret);
1297
1298         ret = snd_jack_set_key(wm1811->jack.jack, SND_JACK_BTN_2,
1299                                                         KEY_VOLUMEUP);
1300
1301         if (ret < 0)
1302                 dev_err(codec->dev, "Failed to set KEY_VOLUMEDOWN: %d\n", ret);
1303
1304         if (wm8994->revision > 1) {
1305                 dev_info(codec->dev, "wm1811: Rev %c support mic detection\n",
1306                         'A' + wm8994->revision);
1307                 ret = wm8958_mic_detect(codec, &wm1811->jack, midas_micdet,
1308                         wm1811);
1309
1310                 if (ret < 0)
1311                         dev_err(codec->dev, "Failed start detection: %d\n",
1312                                 ret);
1313         } else {
1314                 dev_info(codec->dev, "wm1811: Rev %c doesn't support mic detection\n",
1315                         'A' + wm8994->revision);
1316                 codec->dapm.idle_bias_off = 0;
1317         }
1318         /* To wakeup for earjack event in suspend mode */
1319         enable_irq_wake(control->irq);
1320
1321         wake_lock_init(&wm1811->jackdet_wake_lock,
1322                                         WAKE_LOCK_SUSPEND, "midas_jackdet");
1323
1324         /* To support PBA function test */
1325         jack_class = class_create(THIS_MODULE, "audio");
1326
1327         if (IS_ERR(jack_class))
1328                 pr_err("Failed to create class\n");
1329
1330         jack_dev = device_create(jack_class, NULL, 0, codec, "earjack");
1331
1332         if (device_create_file(jack_dev, &dev_attr_select_jack) < 0)
1333                 pr_err("Failed to create device file (%s)!\n",
1334                         dev_attr_select_jack.attr.name);
1335
1336         if (device_create_file(jack_dev, &dev_attr_key_state) < 0)
1337                 pr_err("Failed to create device file (%s)!\n",
1338                         dev_attr_key_state.attr.name);
1339
1340         if (device_create_file(jack_dev, &dev_attr_state) < 0)
1341                 pr_err("Failed to create device file (%s)!\n",
1342                         dev_attr_state.attr.name);
1343
1344         if (device_create_file(jack_dev, &dev_attr_reselect_jack) < 0)
1345                 pr_err("Failed to create device file (%s)!\n",
1346                         dev_attr_reselect_jack.attr.name);
1347
1348 #endif /* CONFIG_SEC_DEV_JACK */
1349         return snd_soc_dapm_sync(&codec->dapm);
1350 }
1351
1352 static struct snd_soc_dai_link midas_dai[] = {
1353         { /* Sec_Fifo DAI i/f */
1354                 .name = "Sec_FIFO TX",
1355                 .stream_name = "Sec_Dai",
1356                 .cpu_dai_name = "samsung-i2s.4",
1357                 .codec_dai_name = "wm8994-aif1",
1358 #ifndef CONFIG_SND_SOC_SAMSUNG_USE_DMA_WRAPPER
1359                 .platform_name = "samsung-audio-idma",
1360 #else
1361                 .platform_name = "samsung-audio",
1362 #endif
1363                 .codec_name = "wm8994-codec",
1364                 .init = midas_wm1811_init_paiftx,
1365                 .ops = &midas_wm1811_aif1_ops,
1366         },
1367         {
1368                 .name = "Midas_WM1811 Voice",
1369                 .stream_name = "Voice Tx/Rx",
1370                 .cpu_dai_name = "midas.cp",
1371                 .codec_dai_name = "wm8994-aif2",
1372                 .platform_name = "snd-soc-dummy",
1373                 .codec_name = "wm8994-codec",
1374                 .ops = &midas_wm1811_aif2_ops,
1375                 .ignore_suspend = 1,
1376         },
1377         {
1378                 .name = "Midas_WM1811 BT",
1379                 .stream_name = "BT Tx/Rx",
1380                 .cpu_dai_name = "midas.bt",
1381                 .codec_dai_name = "wm8994-aif3",
1382                 .platform_name = "snd-soc-dummy",
1383                 .codec_name = "wm8994-codec",
1384                 .ops = &midas_wm1811_aif3_ops,
1385                 .ignore_suspend = 1,
1386         },
1387         { /* Primary DAI i/f */
1388                 .name = "WM8994 AIF1",
1389                 .stream_name = "Pri_Dai",
1390                 .cpu_dai_name = "samsung-i2s.0",
1391                 .codec_dai_name = "wm8994-aif1",
1392                 .platform_name = "samsung-audio",
1393                 .codec_name = "wm8994-codec",
1394                 .ops = &midas_wm1811_aif1_ops,
1395         },
1396 };
1397
1398 static int midas_card_suspend_pre(struct snd_soc_card *card)
1399 {
1400         struct snd_soc_codec *codec = card->rtd->codec;
1401         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1402
1403 #ifdef CONFIG_SEC_DEV_JACK
1404         snd_soc_dapm_disable_pin(&codec->dapm, "AIF1CLK");
1405 #endif
1406
1407         return 0;
1408 }
1409
1410 static int midas_card_suspend_post(struct snd_soc_card *card)
1411 {
1412         struct snd_soc_codec *codec = card->rtd->codec;
1413         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1414         struct snd_soc_dai *aif2_dai = card->rtd[1].codec_dai;
1415         int ret;
1416
1417         if (!codec->active) {
1418 #ifndef SND_USE_BIAS_LEVEL
1419                 ret = snd_soc_dai_set_sysclk(aif2_dai,
1420                                              WM8994_SYSCLK_MCLK2,
1421                                              MIDAS_DEFAULT_MCLK2,
1422                                              SND_SOC_CLOCK_IN);
1423
1424                 if (ret < 0)
1425                         dev_err(codec->dev, "Unable to switch to MCLK2: %d\n",
1426                                 ret);
1427
1428                 ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2, 0, 0, 0);
1429
1430                 if (ret < 0)
1431                         dev_err(codec->dev, "Unable to stop FLL2\n");
1432
1433                 ret = snd_soc_dai_set_sysclk(aif1_dai,
1434                                              WM8994_SYSCLK_MCLK2,
1435                                              MIDAS_DEFAULT_MCLK2,
1436                                              SND_SOC_CLOCK_IN);
1437                 if (ret < 0)
1438                         dev_err(codec->dev, "Unable to switch to MCLK2\n");
1439
1440                 ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, 0, 0, 0);
1441
1442                 if (ret < 0)
1443                         dev_err(codec->dev, "Unable to stop FLL1\n");
1444 #endif
1445
1446                 midas_snd_set_mclk(false, true);
1447         }
1448
1449 #ifdef CONFIG_ARCH_EXYNOS5
1450         exynos5_sys_powerdown_xxti_control(midas_snd_get_mclk() ? 1 : 0);
1451 #else /* for CONFIG_ARCH_EXYNOS5 */
1452         exynos4_sys_powerdown_xusbxti_control(midas_snd_get_mclk() ? 1 : 0);
1453 #endif
1454
1455         return 0;
1456 }
1457
1458 static int midas_card_resume_pre(struct snd_soc_card *card)
1459 {
1460         struct snd_soc_codec *codec = card->rtd->codec;
1461         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1462         int ret;
1463
1464         midas_snd_set_mclk(true, false);
1465
1466 #ifndef SND_USE_BIAS_LEVEL
1467         /* Switch the FLL */
1468         ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
1469                                   WM8994_FLL_SRC_MCLK1,
1470                                   MIDAS_DEFAULT_MCLK1,
1471                                   MIDAS_DEFAULT_SYNC_CLK);
1472
1473         if (ret < 0)
1474                 dev_err(aif1_dai->dev, "Unable to start FLL1: %d\n", ret);
1475
1476         /* Then switch AIF1CLK to it */
1477         ret = snd_soc_dai_set_sysclk(aif1_dai,
1478                                      WM8994_SYSCLK_FLL1,
1479                                      MIDAS_DEFAULT_SYNC_CLK,
1480                                      SND_SOC_CLOCK_IN);
1481
1482         if (ret < 0)
1483                 dev_err(aif1_dai->dev, "Unable to switch to FLL1: %d\n", ret);
1484 #endif
1485
1486         return 0;
1487 }
1488
1489 static int midas_card_resume_post(struct snd_soc_card *card)
1490 {
1491         struct snd_soc_codec *codec = card->rtd->codec;
1492         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1493         int reg = 0;
1494
1495         /* workaround for jack detection
1496          * sometimes WM8994_GPIO_1 type changed wrong function type
1497          * so if type mismatched, update to IRQ type
1498          */
1499         reg = snd_soc_read(codec, WM8994_GPIO_1);
1500
1501         if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_IRQ) {
1502                 dev_err(codec->dev, "%s: GPIO1 type 0x%x\n", __func__, reg);
1503                 snd_soc_write(codec, WM8994_GPIO_1, WM8994_GP_FN_IRQ);
1504         }
1505
1506 #ifdef CONFIG_SEC_DEV_JACK
1507         snd_soc_dapm_force_enable_pin(&codec->dapm, "AIF1CLK");
1508 #endif
1509
1510         return 0;
1511 }
1512
1513 #ifdef SND_USE_BIAS_LEVEL
1514 static int midas_set_bias_level(struct snd_soc_card *card,
1515                                 struct snd_soc_dapm_context *dapm,
1516                                 enum snd_soc_bias_level level)
1517 {
1518         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1519
1520         if (dapm->dev != aif1_dai->dev)
1521                 return 0;
1522
1523         switch (level) {
1524         case SND_SOC_BIAS_PREPARE:
1525                 midas_start_fll1(card->rtd[0].codec_dai);
1526                 break;
1527
1528         default:
1529                 break;
1530         }
1531
1532         return 0;
1533 }
1534
1535 static int midas_set_bias_level_post(struct snd_soc_card *card,
1536                                      struct snd_soc_dapm_context *dapm,
1537                                      enum snd_soc_bias_level level)
1538 {
1539         struct snd_soc_codec *codec = card->rtd->codec;
1540         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1541         struct snd_soc_dai *aif2_dai = card->rtd[1].codec_dai;
1542         int ret;
1543
1544         if (dapm->dev != aif1_dai->dev)
1545                 return 0;
1546
1547         switch (level) {
1548         case SND_SOC_BIAS_STANDBY:
1549
1550                 /* When going idle stop FLL1 and revert to using MCLK2
1551                  * directly for minimum power consumptin for accessory
1552                  * detection.
1553                  */
1554                 if (card->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
1555                         dev_info(aif1_dai->dev, "Moving to STANDBY\n");
1556
1557                         ret = snd_soc_dai_set_sysclk(aif2_dai,
1558                                                      WM8994_SYSCLK_MCLK2,
1559                                                      MIDAS_DEFAULT_MCLK2,
1560                                                      SND_SOC_CLOCK_IN);
1561                         if (ret < 0)
1562                                 dev_err(codec->dev, "Failed to switch to MCLK2\n");
1563
1564                         ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
1565                                                   0, 0, 0);
1566
1567                         if (ret < 0)
1568                                 dev_err(codec->dev,
1569                                         "Failed to change FLL2\n");
1570
1571                         ret = snd_soc_dai_set_sysclk(aif1_dai,
1572                                                      WM8994_SYSCLK_MCLK2,
1573                                                      MIDAS_DEFAULT_MCLK2,
1574                                                      SND_SOC_CLOCK_IN);
1575                         if (ret < 0)
1576                                 dev_err(codec->dev,
1577                                         "Failed to switch to MCLK2\n");
1578
1579                         ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
1580                                                   0, 0, 0);
1581                         if (ret < 0)
1582                                 dev_err(codec->dev,
1583                                         "Failed to stop FLL1\n");
1584
1585
1586                         midas_fll1_active = false;
1587                         midas_snd_set_mclk(false, false);
1588                 }
1589
1590                 break;
1591         default:
1592                 break;
1593         }
1594
1595         card->dapm.bias_level = level;
1596
1597         return 0;
1598 }
1599 #endif
1600
1601 static struct snd_soc_card midas = {
1602         .name = "Midas_WM1811",
1603         .dai_link = midas_dai,
1604
1605         /* If you want to use sec_fifo device,
1606          * changes the num_link = 2 or ARRAY_SIZE(midas_dai). */
1607         .num_links = ARRAY_SIZE(midas_dai),
1608
1609 #ifdef SND_USE_BIAS_LEVEL
1610         .set_bias_level = midas_set_bias_level,
1611         .set_bias_level_post = midas_set_bias_level_post,
1612 #endif
1613
1614         .suspend_post = midas_card_suspend_post,
1615         .resume_pre = midas_card_resume_pre,
1616         .suspend_pre = midas_card_suspend_pre,
1617         .resume_post = midas_card_resume_post
1618 };
1619
1620 static struct platform_device *midas_snd_device;
1621
1622 static int __init midas_audio_init(void)
1623 {
1624         struct wm1811_machine_priv *wm1811;
1625         int ret;
1626
1627         wm1811 = kzalloc(sizeof *wm1811, GFP_KERNEL);
1628         if (!wm1811) {
1629                 pr_err("Failed to allocate memory\n");
1630                 ret = -ENOMEM;
1631                 goto err_kzalloc;
1632         }
1633         snd_soc_card_set_drvdata(&midas, wm1811);
1634
1635         midas_snd_device = platform_device_alloc("soc-audio", -1);
1636         if (!midas_snd_device) {
1637                 ret = -ENOMEM;
1638                 goto err_device_alloc;
1639         }
1640
1641         ret = snd_soc_register_dais(&midas_snd_device->dev, midas_ext_dai,
1642                                                 ARRAY_SIZE(midas_ext_dai));
1643         if (ret != 0)
1644                 pr_err("Failed to register external DAIs: %d\n", ret);
1645
1646         platform_set_drvdata(midas_snd_device, &midas);
1647
1648         ret = platform_device_add(midas_snd_device);
1649         if (ret)
1650                 platform_device_put(midas_snd_device);
1651
1652         midas_gpio_init();
1653
1654         return ret;
1655
1656 err_device_alloc:
1657         kfree(wm1811);
1658 err_kzalloc:
1659         return ret;
1660 }
1661 module_init(midas_audio_init);
1662
1663 static void __exit midas_audio_exit(void)
1664 {
1665         struct snd_soc_card *card = &midas;
1666         struct wm1811_machine_priv *wm1811 = snd_soc_card_get_drvdata(card);
1667         platform_device_unregister(midas_snd_device);
1668         kfree(wm1811);
1669 }
1670 module_exit(midas_audio_exit);
1671
1672 MODULE_AUTHOR("JS. Park <aitdark.park@samsung.com>");
1673 MODULE_DESCRIPTION("ALSA SoC Midas WM1811");
1674 MODULE_LICENSE("GPL");