tizen 2.3.1 release
[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 #ifdef CONFIG_TARGET_LOCALE_KOR
543         if (wm8994->mic_detecting && status & 0x4
544                 && wm8994->jack_mic == false) {
545 #else
546         if (wm8994->mic_detecting && status & 0x4) {
547 #endif
548                 dev_info(wm1811->codec->dev, "Detected headphone\n");
549                 wm8994->mic_detecting = false;
550 #ifdef CONFIG_TARGET_LOCALE_KOR
551                 wm8994->jack_mic = false;
552 #endif
553                 midas_micd_set_rate(wm1811->codec);
554
555                 snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
556                                     SND_JACK_HEADSET);
557
558                 /* If we have jackdet that will detect removal */
559                 if (wm8994->jackdet) {
560                         mutex_lock(&wm8994->accdet_lock);
561
562                         snd_soc_update_bits(wm1811->codec, WM8958_MIC_DETECT_1,
563                                             WM8958_MICD_ENA, 0);
564
565                         if (wm8994->active_refcount) {
566                                 snd_soc_update_bits(wm1811->codec,
567                                         WM8994_ANTIPOP_2,
568                                         WM1811_JACKDET_MODE_MASK,
569                                         WM1811_JACKDET_MODE_AUDIO);
570                         }
571
572                         mutex_unlock(&wm8994->accdet_lock);
573
574                         if (wm8994->pdata->jd_ext_cap) {
575                                 mutex_lock(&wm1811->codec->mutex);
576                                 snd_soc_dapm_disable_pin(&wm1811->codec->dapm,
577                                                          "MICBIAS2");
578                                 snd_soc_dapm_sync(&wm1811->codec->dapm);
579                                 mutex_unlock(&wm1811->codec->mutex);
580                         }
581                 }
582         }
583
584         /* Report short circuit as a button */
585         if (wm8994->jack_mic) {
586                 report = 0;
587                 if (status & WM1811_JACKDET_BTN0)
588                         report |= SND_JACK_BTN_0;
589
590                 if (status & WM1811_JACKDET_BTN1)
591                         report |= SND_JACK_BTN_1;
592
593                 if (status & WM1811_JACKDET_BTN2)
594                         report |= SND_JACK_BTN_2;
595
596                 dev_dbg(wm1811->codec->dev, "Detected Button: %08x (%08X)\n",
597                         report, status);
598
599                 snd_soc_jack_report(wm8994->micdet[0].jack, report,
600                                     wm8994->btn_mask);
601         }
602 }
603
604 #ifdef CONFIG_SND_SAMSUNG_I2S_MASTER
605 static int set_epll_rate(unsigned long rate)
606 {
607         struct clk *fout_epll;
608
609         fout_epll = clk_get(NULL, "fout_epll");
610         if (IS_ERR(fout_epll)) {
611                 printk(KERN_ERR "%s: failed to get fout_epll\n", __func__);
612                 return -ENOENT;
613         }
614
615         if (rate == clk_get_rate(fout_epll))
616                 goto out;
617
618         clk_set_rate(fout_epll, rate);
619 out:
620         clk_put(fout_epll);
621
622         return 0;
623 }
624 #endif /* CONFIG_SND_SAMSUNG_I2S_MASTER */
625
626 #ifndef CONFIG_SND_SAMSUNG_I2S_MASTER
627 static int midas_wm1811_aif1_hw_params(struct snd_pcm_substream *substream,
628         struct snd_pcm_hw_params *params)
629 {
630         struct snd_soc_pcm_runtime *rtd = substream->private_data;
631         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
632         struct snd_soc_dai *codec_dai = rtd->codec_dai;
633         unsigned int pll_out;
634         int ret;
635
636         dev_info(codec_dai->dev, "%s ++\n", __func__);
637         /* AIF1CLK should be >=3MHz for optimal performance */
638         if (params_rate(params) == 8000 || params_rate(params) == 11025)
639                 pll_out = params_rate(params) * 512;
640         else
641                 pll_out = params_rate(params) * 256;
642
643         ret = snd_soc_dai_set_fmt(codec_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         /* Set the cpu DAI configuration */
650         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
651                                         | SND_SOC_DAIFMT_NB_NF
652                                         | SND_SOC_DAIFMT_CBM_CFM);
653         if (ret < 0)
654                 return ret;
655
656 #ifndef SND_USE_BIAS_LEVEL
657         /* Switch the FLL */
658         ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
659                                   WM8994_FLL_SRC_MCLK1, MIDAS_DEFAULT_MCLK1,
660                                   pll_out);
661         if (ret < 0)
662                 dev_err(codec_dai->dev, "Unable to start FLL1: %d\n", ret);
663
664         ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL1,
665                                      pll_out, SND_SOC_CLOCK_IN);
666         if (ret < 0) {
667                 dev_err(codec_dai->dev, "Unable to switch to FLL1: %d\n", ret);
668                 return ret;
669         }
670
671         ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK,
672                                      0, MOD_OPCLK_PCLK);
673         if (ret < 0)
674                 return ret;
675 #else
676         midas_start_fll1(codec_dai);
677 #endif
678
679         if (ret < 0)
680                 return ret;
681
682         dev_info(codec_dai->dev, "%s --\n", __func__);
683
684         return 0;
685 }
686 #else /* CONFIG_SND_SAMSUNG_I2S_MASTER */
687 static int midas_wm1811_aif1_hw_params(struct snd_pcm_substream *substream,
688         struct snd_pcm_hw_params *params)
689 {
690         struct snd_soc_pcm_runtime *rtd = substream->private_data;
691         struct snd_soc_dai *codec_dai = rtd->codec_dai;
692         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
693         int bfs, psr, rfs, ret;
694         unsigned long rclk;
695
696         switch (params_format(params)) {
697         case SNDRV_PCM_FORMAT_U24:
698         case SNDRV_PCM_FORMAT_S24:
699                 bfs = 48;
700                 break;
701         case SNDRV_PCM_FORMAT_U16_LE:
702         case SNDRV_PCM_FORMAT_S16_LE:
703                 bfs = 32;
704                 break;
705         default:
706                 return -EINVAL;
707         }
708
709         switch (params_rate(params)) {
710         case 16000:
711         case 22050:
712         case 24000:
713         case 32000:
714         case 44100:
715         case 48000:
716         case 88200:
717         case 96000:
718                 if (bfs == 48)
719                         rfs = 384;
720                 else
721                         rfs = 256;
722                 break;
723         case 64000:
724                 rfs = 384;
725                 break;
726         case 8000:
727         case 11025:
728         case 12000:
729                 if (bfs == 48)
730                         rfs = 768;
731                 else
732                         rfs = 512;
733                 break;
734         default:
735                 return -EINVAL;
736         }
737
738         rclk = params_rate(params) * rfs;
739
740         switch (rclk) {
741         case 4096000:
742         case 5644800:
743         case 6144000:
744         case 8467200:
745         case 9216000:
746                 psr = 8;
747                 break;
748         case 8192000:
749         case 11289600:
750         case 12288000:
751         case 16934400:
752         case 18432000:
753                 psr = 4;
754                 break;
755         case 22579200:
756         case 24576000:
757         case 33868800:
758         case 36864000:
759                 psr = 2;
760                 break;
761         case 67737600:
762         case 73728000:
763                 psr = 1;
764                 break;
765         default:
766                 printk(KERN_INFO "Not yet supported!\n");
767                 return -EINVAL;
768         }
769
770         set_epll_rate(rclk * psr);
771
772         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
773                                         | SND_SOC_DAIFMT_NB_NF
774                                         | SND_SOC_DAIFMT_CBS_CFS);
775         if (ret < 0)
776                 return ret;
777
778         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
779                                         | SND_SOC_DAIFMT_NB_NF
780                                         | SND_SOC_DAIFMT_CBS_CFS);
781         if (ret < 0)
782                 return ret;
783
784         ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK1,
785                                      rclk, SND_SOC_CLOCK_IN);
786         if (ret < 0)
787                 return ret;
788
789         ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
790                                      0, SND_SOC_CLOCK_OUT);
791         if (ret < 0)
792                 return ret;
793
794         ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_BCLK, bfs);
795         if (ret < 0)
796                 return ret;
797
798         return 0;
799 }
800 #endif /* CONFIG_SND_SAMSUNG_I2S_MASTER */
801
802 /*
803  * Midas WM1811 DAI operations.
804  */
805 static struct snd_soc_ops midas_wm1811_aif1_ops = {
806         .hw_params = midas_wm1811_aif1_hw_params,
807 };
808
809 static int midas_wm1811_aif2_hw_params(struct snd_pcm_substream *substream,
810                                         struct snd_pcm_hw_params *params)
811 {
812         struct snd_soc_pcm_runtime *rtd = substream->private_data;
813         struct snd_soc_dai *codec_dai = rtd->codec_dai;
814         int ret;
815         int prate;
816         int bclk;
817
818         dev_info(codec_dai->dev, "%s ++\n", __func__);
819         prate = params_rate(params);
820         switch (params_rate(params)) {
821         case 8000:
822         case 16000:
823                break;
824         default:
825                 dev_warn(codec_dai->dev, "Unsupported LRCLK %d, falling back to 8000Hz\n",
826                                 (int)params_rate(params));
827                 prate = 8000;
828         }
829
830 #if defined(CONFIG_LTE_MODEM_CMC221) || defined(CONFIG_MACH_M0_CTC)
831 #if defined(CONFIG_MACH_C1_KOR_LGT)
832         /* Set the codec DAI configuration */
833         if (aif2_mode == 0) {
834                 if (kpcs_mode == 1)
835                         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
836                                 | SND_SOC_DAIFMT_NB_NF
837                                 | SND_SOC_DAIFMT_CBS_CFS);
838                 else
839                         ret = snd_soc_dai_set_fmt(codec_dai,
840                                 SND_SOC_DAIFMT_DSP_A
841                                 | SND_SOC_DAIFMT_IB_NF
842                                 | SND_SOC_DAIFMT_CBS_CFS);
843         } else
844                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
845                                 | SND_SOC_DAIFMT_NB_NF
846                                 | SND_SOC_DAIFMT_CBM_CFM);
847 #else
848         if (aif2_mode == 0)
849                 /* Set the codec DAI configuration */
850                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A
851                                 | SND_SOC_DAIFMT_IB_NF
852                                 | SND_SOC_DAIFMT_CBS_CFS);
853         else
854                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A
855                                 | SND_SOC_DAIFMT_IB_NF
856                                 | SND_SOC_DAIFMT_CBM_CFM);
857 #endif
858 #else
859         /* Set the codec DAI configuration, aif2_mode:0 is slave */
860         if (aif2_mode == 0)
861                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
862                                         | SND_SOC_DAIFMT_NB_NF
863                                         | SND_SOC_DAIFMT_CBS_CFS);
864         else
865                 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
866                                         | SND_SOC_DAIFMT_NB_NF
867                                         | SND_SOC_DAIFMT_CBM_CFM);
868 #endif
869
870         if (ret < 0)
871                 return ret;
872
873 #if defined(CONFIG_LTE_MODEM_CMC221)
874         if (kpcs_mode == 1) {
875                 switch (prate) {
876                 case 8000:
877                         bclk = 256000;
878                         break;
879                 case 16000:
880                         bclk = 512000;
881                         break;
882                 default:
883                         return -EINVAL;
884                 }
885         } else {
886         bclk = 2048000;
887         }
888 #elif defined(CONFIG_MACH_M0_CTC)
889         bclk = 2048000;
890 #else
891         switch (prate) {
892         case 8000:
893                 bclk = 256000;
894                 break;
895         case 16000:
896                 bclk = 512000;
897                 break;
898         default:
899                 return -EINVAL;
900         }
901 #endif
902
903 #ifdef SND_USE_BIAS_LEVEL
904         if (!midas_fll1_active)
905                 midas_start_fll1(midas_aif1_dai);
906 #endif
907
908         if (aif2_mode == 0) {
909                 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL2,
910                                         WM8994_FLL_SRC_BCLK,
911                                         bclk, prate * 256);
912         } else {
913                 ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL2,
914                                           WM8994_FLL_SRC_MCLK1,
915                                           MIDAS_DEFAULT_MCLK1, prate * 256);
916         }
917
918         if (ret < 0)
919                 dev_err(codec_dai->dev, "Unable to configure FLL2: %d\n", ret);
920
921         ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL2,
922                                      prate * 256, SND_SOC_CLOCK_IN);
923         if (ret < 0)
924                 dev_err(codec_dai->dev, "Unable to switch to FLL2: %d\n", ret);
925
926         dev_info(codec_dai->dev, "%s --\n", __func__);
927         return 0;
928 }
929
930 static struct snd_soc_ops midas_wm1811_aif2_ops = {
931         .hw_params = midas_wm1811_aif2_hw_params,
932 };
933
934 static int midas_wm1811_aif3_hw_params(struct snd_pcm_substream *substream,
935                                         struct snd_pcm_hw_params *params)
936 {
937         pr_err("%s: enter\n", __func__);
938         return 0;
939 }
940
941 static struct snd_soc_ops midas_wm1811_aif3_ops = {
942         .hw_params = midas_wm1811_aif3_hw_params,
943 };
944
945 static const struct snd_kcontrol_new midas_controls[] = {
946         SOC_DAPM_PIN_SWITCH("HP"),
947         SOC_DAPM_PIN_SWITCH("SPK"),
948         SOC_DAPM_PIN_SWITCH("RCV"),
949         SOC_DAPM_PIN_SWITCH("FM In"),
950         SOC_DAPM_PIN_SWITCH("LINE"),
951         SOC_DAPM_PIN_SWITCH("HDMI"),
952         SOC_DAPM_PIN_SWITCH("Main Mic"),
953         SOC_DAPM_PIN_SWITCH("Sub Mic"),
954         SOC_DAPM_PIN_SWITCH("Third Mic"),
955         SOC_DAPM_PIN_SWITCH("Headset Mic"),
956
957         SOC_ENUM_EXT("AIF2 Mode", aif2_mode_enum[0],
958                 get_aif2_mode, set_aif2_mode),
959
960         SOC_ENUM_EXT("KPCS Mode", kpcs_mode_enum[0],
961                 get_kpcs_mode, set_kpcs_mode),
962
963         SOC_ENUM_EXT("Input Clamp", input_clamp_enum[0],
964                 get_input_clamp, set_input_clamp),
965
966         SOC_ENUM_EXT("LineoutSwitch Mode", lineout_mode_enum[0],
967                 get_lineout_mode, set_lineout_mode),
968
969 };
970
971 const struct snd_soc_dapm_widget midas_dapm_widgets[] = {
972         SND_SOC_DAPM_HP("HP", NULL),
973         SND_SOC_DAPM_SPK("SPK", midas_ext_spkmode),
974         SND_SOC_DAPM_SPK("RCV", NULL),
975         SND_SOC_DAPM_LINE("LINE", midas_lineout_switch),
976         SND_SOC_DAPM_LINE("HDMI", NULL),
977
978         SND_SOC_DAPM_MIC("Headset Mic", NULL),
979         SND_SOC_DAPM_MIC("Main Mic", midas_ext_micbias),
980         SND_SOC_DAPM_MIC("Sub Mic", midas_ext_submicbias),
981         SND_SOC_DAPM_MIC("Third Mic", midas_ext_thirdmicbias),
982         SND_SOC_DAPM_LINE("FM In", NULL),
983
984         SND_SOC_DAPM_INPUT("S5P RP"),
985 };
986
987 const struct snd_soc_dapm_route midas_dapm_routes[] = {
988         { "HP", NULL, "HPOUT1L" },
989         { "HP", NULL, "HPOUT1R" },
990
991         { "SPK", NULL, "SPKOUTLN" },
992         { "SPK", NULL, "SPKOUTLP" },
993         { "SPK", NULL, "SPKOUTRN" },
994         { "SPK", NULL, "SPKOUTRP" },
995
996         { "RCV", NULL, "HPOUT2N" },
997         { "RCV", NULL, "HPOUT2P" },
998
999         { "LINE", NULL, "LINEOUT2N" },
1000         { "LINE", NULL, "LINEOUT2P" },
1001
1002         { "HDMI", NULL, "LINEOUT1N" },
1003         { "HDMI", NULL, "LINEOUT1P" },
1004
1005         { "IN1LP", NULL, "MICBIAS1" },
1006         { "IN1LN", NULL, "MICBIAS1" },
1007         { "MICBIAS1", NULL, "Main Mic" },
1008
1009         { "IN1RP", NULL, "Sub Mic" },
1010         { "IN1RN", NULL, "Sub Mic" },
1011
1012         { "IN2LP:VXRN", NULL, "MICBIAS2" },
1013         { "MICBIAS2", NULL, "Headset Mic" },
1014
1015         { "AIF1DAC1L", NULL, "S5P RP" },
1016         { "AIF1DAC1R", NULL, "S5P RP" },
1017
1018         { "IN2RN", NULL, "FM In" },
1019         { "IN2RP:VXRP", NULL, "FM In" },
1020
1021         { "IN2RN", NULL, "Third Mic" },
1022         { "IN2RP:VXRP", NULL, "Third Mic" },
1023 };
1024
1025 static struct snd_soc_dai_driver midas_ext_dai[] = {
1026         {
1027                 .name = "midas.cp",
1028                 .playback = {
1029                         .channels_min = 1,
1030                         .channels_max = 2,
1031                         .rate_min = 8000,
1032                         .rate_max = 16000,
1033                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1034                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1035                 },
1036                 .capture = {
1037                         .channels_min = 1,
1038                         .channels_max = 2,
1039                         .rate_min = 8000,
1040                         .rate_max = 16000,
1041                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1042                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1043                 },
1044         },
1045         {
1046                 .name = "midas.bt",
1047                 .playback = {
1048                         .channels_min = 1,
1049                         .channels_max = 2,
1050                         .rate_min = 8000,
1051                         .rate_max = 16000,
1052                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1053                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1054                 },
1055                 .capture = {
1056                         .channels_min = 1,
1057                         .channels_max = 2,
1058                         .rate_min = 8000,
1059                         .rate_max = 16000,
1060                         .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1061                         .formats = SNDRV_PCM_FMTBIT_S16_LE,
1062                 },
1063         },
1064 };
1065
1066 #ifndef CONFIG_SEC_DEV_JACK
1067 static ssize_t earjack_state_show(struct device *dev,
1068         struct device_attribute *attr, char *buf)
1069 {
1070         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1071         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1072
1073         int report = 0;
1074
1075         if ((wm8994->micdet[0].jack->status & SND_JACK_HEADPHONE) ||
1076                 (wm8994->micdet[0].jack->status & SND_JACK_HEADSET)) {
1077                 report = 1;
1078         }
1079
1080         return sprintf(buf, "%d\n", report);
1081 }
1082
1083 static ssize_t earjack_state_store(struct device *dev,
1084         struct device_attribute *attr, const char *buf, size_t size)
1085 {
1086         pr_info("%s : operate nothing\n", __func__);
1087
1088         return size;
1089 }
1090
1091 static ssize_t earjack_key_state_show(struct device *dev,
1092         struct device_attribute *attr, char *buf)
1093 {
1094         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1095         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1096
1097         int report = 0;
1098
1099         if (wm8994->micdet[0].jack->status & SND_JACK_BTN_0)
1100                 report = 1;
1101
1102         return sprintf(buf, "%d\n", report);
1103 }
1104
1105 static ssize_t earjack_key_state_store(struct device *dev,
1106         struct device_attribute *attr, const char *buf, size_t size)
1107 {
1108         pr_info("%s : operate nothing\n", __func__);
1109
1110         return size;
1111 }
1112
1113 static ssize_t earjack_select_jack_show(struct device *dev,
1114         struct device_attribute *attr, char *buf)
1115 {
1116         pr_info("%s : operate nothing\n", __func__);
1117
1118         return 0;
1119 }
1120
1121 static ssize_t earjack_select_jack_store(struct device *dev,
1122         struct device_attribute *attr, const char *buf, size_t size)
1123 {
1124         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1125         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1126
1127         wm8994->mic_detecting = false;
1128         wm8994->jack_mic = true;
1129
1130         midas_micd_set_rate(codec);
1131
1132         if ((!size) || (buf[0] != '1')) {
1133                 snd_soc_jack_report(wm8994->micdet[0].jack,
1134                                     0, SND_JACK_HEADSET);
1135                 dev_info(codec->dev, "Forced remove microphone\n");
1136         } else {
1137
1138                 snd_soc_jack_report(wm8994->micdet[0].jack,
1139                                     SND_JACK_HEADSET, SND_JACK_HEADSET);
1140                 dev_info(codec->dev, "Forced detect microphone\n");
1141         }
1142
1143         return size;
1144 }
1145
1146 static ssize_t reselect_jack_show(struct device *dev,
1147         struct device_attribute *attr, char *buf)
1148 {
1149         pr_info("%s : operate nothing\n", __func__);
1150         return 0;
1151 }
1152
1153 static ssize_t reselect_jack_store(struct device *dev,
1154         struct device_attribute *attr, const char *buf, size_t size)
1155 {
1156         struct snd_soc_codec *codec = dev_get_drvdata(dev);
1157         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1158         int reg = 0;
1159
1160         reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
1161         if (reg == 0x402) {
1162                 dev_info(codec->dev, "Detected open circuit\n");
1163
1164                 snd_soc_update_bits(codec, WM8958_MICBIAS2,
1165                                     WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
1166                 /* Enable debounce while removed */
1167                 snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
1168                                     WM1811_JACKDET_DB, WM1811_JACKDET_DB);
1169
1170                 wm8994->mic_detecting = false;
1171                 wm8994->jack_mic = false;
1172                 snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
1173                                     WM8958_MICD_ENA, 0);
1174
1175                 if (wm8994->active_refcount) {
1176                         snd_soc_update_bits(codec,
1177                                 WM8994_ANTIPOP_2,
1178                                 WM1811_JACKDET_MODE_MASK,
1179                                 WM1811_JACKDET_MODE_AUDIO);
1180                 } else {
1181                         snd_soc_update_bits(codec,
1182                                 WM8994_ANTIPOP_2,
1183                                 WM1811_JACKDET_MODE_MASK,
1184                                 WM1811_JACKDET_MODE_JACK);
1185                 }
1186
1187                 snd_soc_jack_report(wm8994->micdet[0].jack, 0,
1188                                     SND_JACK_MECHANICAL | SND_JACK_HEADSET |
1189                                     wm8994->btn_mask);
1190         }
1191         return size;
1192 }
1193
1194 static DEVICE_ATTR(reselect_jack, S_IRUGO | S_IWUSR | S_IWGRP,
1195                 reselect_jack_show, reselect_jack_store);
1196
1197 static DEVICE_ATTR(select_jack, S_IRUGO | S_IWUSR | S_IWGRP,
1198                    earjack_select_jack_show, earjack_select_jack_store);
1199
1200 static DEVICE_ATTR(key_state, S_IRUGO | S_IWUSR | S_IWGRP,
1201                    earjack_key_state_show, earjack_key_state_store);
1202
1203 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR | S_IWGRP,
1204                    earjack_state_show, earjack_state_store);
1205 #endif
1206
1207 static int midas_wm1811_init_paiftx(struct snd_soc_pcm_runtime *rtd)
1208 {
1209         struct snd_soc_codec *codec = rtd->codec;
1210         struct wm1811_machine_priv *wm1811
1211                 = snd_soc_card_get_drvdata(codec->card);
1212         struct snd_soc_dai *aif1_dai = rtd->codec_dai;
1213         struct wm8994 *control = codec->control_data;
1214         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1215         int ret;
1216
1217 #ifdef SND_USE_BIAS_LEVEL
1218         midas_aif1_dai = aif1_dai;
1219 #endif
1220
1221 #ifdef CONFIG_MACH_GC1
1222         wm1811_codec = codec;
1223 #endif
1224
1225         midas_snd_set_mclk(true, false);
1226
1227         rtd->codec_dai->driver->playback.channels_max =
1228                                 rtd->cpu_dai->driver->playback.channels_max;
1229
1230         ret = snd_soc_add_controls(codec, midas_controls,
1231                                         ARRAY_SIZE(midas_controls));
1232
1233         ret = snd_soc_dapm_new_controls(&codec->dapm, midas_dapm_widgets,
1234                                            ARRAY_SIZE(midas_dapm_widgets));
1235         if (ret != 0)
1236                 dev_err(codec->dev, "Failed to add DAPM widgets: %d\n", ret);
1237
1238         ret = snd_soc_dapm_add_routes(&codec->dapm, midas_dapm_routes,
1239                                            ARRAY_SIZE(midas_dapm_routes));
1240         if (ret != 0)
1241                 dev_err(codec->dev, "Failed to add DAPM routes: %d\n", ret);
1242
1243         ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
1244                                      MIDAS_DEFAULT_MCLK2, SND_SOC_CLOCK_IN);
1245         if (ret < 0)
1246                 dev_err(codec->dev, "Failed to boot clocking\n");
1247
1248         /* Force AIF1CLK on as it will be master for jack detection */
1249         if (wm8994->revision > 1) {
1250                 ret = snd_soc_dapm_force_enable_pin(&codec->dapm, "AIF1CLK");
1251                 if (ret < 0)
1252                         dev_err(codec->dev, "Failed to enable AIF1CLK: %d\n",
1253                                         ret);
1254         }
1255
1256         ret = snd_soc_dapm_disable_pin(&codec->dapm, "S5P RP");
1257         if (ret < 0)
1258                 dev_err(codec->dev, "Failed to disable S5P RP: %d\n", ret);
1259
1260         snd_soc_dapm_ignore_suspend(&codec->dapm, "RCV");
1261         snd_soc_dapm_ignore_suspend(&codec->dapm, "SPK");
1262         snd_soc_dapm_ignore_suspend(&codec->dapm, "HP");
1263         snd_soc_dapm_ignore_suspend(&codec->dapm, "Headset Mic");
1264         snd_soc_dapm_ignore_suspend(&codec->dapm, "Sub Mic");
1265         snd_soc_dapm_ignore_suspend(&codec->dapm, "Main Mic");
1266         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF1DACDAT");
1267         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF2DACDAT");
1268         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF3DACDAT");
1269         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF1ADCDAT");
1270         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF2ADCDAT");
1271         snd_soc_dapm_ignore_suspend(&codec->dapm, "AIF3ADCDAT");
1272         snd_soc_dapm_ignore_suspend(&codec->dapm, "FM In");
1273         snd_soc_dapm_ignore_suspend(&codec->dapm, "LINE");
1274         snd_soc_dapm_ignore_suspend(&codec->dapm, "HDMI");
1275         snd_soc_dapm_ignore_suspend(&codec->dapm, "Third Mic");
1276
1277         wm1811->codec = codec;
1278
1279         midas_micd_set_rate(codec);
1280
1281 #ifdef CONFIG_SEC_DEV_JACK
1282         /* By default use idle_bias_off, will override for WM8994 */
1283         codec->dapm.idle_bias_off = 0;
1284 #else /* CONFIG_SEC_DEV_JACK */
1285         wm1811->jack.status = 0;
1286
1287         ret = snd_soc_jack_new(codec, "Midas Jack",
1288                                 SND_JACK_HEADSET | SND_JACK_BTN_0 |
1289                                 SND_JACK_BTN_1 | SND_JACK_BTN_2,
1290                                 &wm1811->jack);
1291
1292         if (ret < 0)
1293                 dev_err(codec->dev, "Failed to create jack: %d\n", ret);
1294
1295         ret = snd_jack_set_key(wm1811->jack.jack, SND_JACK_BTN_0, KEY_MEDIA);
1296
1297         if (ret < 0)
1298                 dev_err(codec->dev, "Failed to set KEY_MEDIA: %d\n", ret);
1299
1300         ret = snd_jack_set_key(wm1811->jack.jack, SND_JACK_BTN_1,
1301                                                         KEY_VOLUMEDOWN);
1302         if (ret < 0)
1303                 dev_err(codec->dev, "Failed to set KEY_VOLUMEUP: %d\n", ret);
1304
1305         ret = snd_jack_set_key(wm1811->jack.jack, SND_JACK_BTN_2,
1306                                                         KEY_VOLUMEUP);
1307
1308         if (ret < 0)
1309                 dev_err(codec->dev, "Failed to set KEY_VOLUMEDOWN: %d\n", ret);
1310
1311         if (wm8994->revision > 1) {
1312                 dev_info(codec->dev, "wm1811: Rev %c support mic detection\n",
1313                         'A' + wm8994->revision);
1314                 ret = wm8958_mic_detect(codec, &wm1811->jack, midas_micdet,
1315                         wm1811);
1316
1317                 if (ret < 0)
1318                         dev_err(codec->dev, "Failed start detection: %d\n",
1319                                 ret);
1320         } else {
1321                 dev_info(codec->dev, "wm1811: Rev %c doesn't support mic detection\n",
1322                         'A' + wm8994->revision);
1323                 codec->dapm.idle_bias_off = 0;
1324         }
1325         /* To wakeup for earjack event in suspend mode */
1326         enable_irq_wake(control->irq);
1327
1328         wake_lock_init(&wm1811->jackdet_wake_lock,
1329                                         WAKE_LOCK_SUSPEND, "midas_jackdet");
1330
1331         /* To support PBA function test */
1332         jack_class = class_create(THIS_MODULE, "audio");
1333
1334         if (IS_ERR(jack_class))
1335                 pr_err("Failed to create class\n");
1336
1337         jack_dev = device_create(jack_class, NULL, 0, codec, "earjack");
1338
1339         if (device_create_file(jack_dev, &dev_attr_select_jack) < 0)
1340                 pr_err("Failed to create device file (%s)!\n",
1341                         dev_attr_select_jack.attr.name);
1342
1343         if (device_create_file(jack_dev, &dev_attr_key_state) < 0)
1344                 pr_err("Failed to create device file (%s)!\n",
1345                         dev_attr_key_state.attr.name);
1346
1347         if (device_create_file(jack_dev, &dev_attr_state) < 0)
1348                 pr_err("Failed to create device file (%s)!\n",
1349                         dev_attr_state.attr.name);
1350
1351         if (device_create_file(jack_dev, &dev_attr_reselect_jack) < 0)
1352                 pr_err("Failed to create device file (%s)!\n",
1353                         dev_attr_reselect_jack.attr.name);
1354
1355 #endif /* CONFIG_SEC_DEV_JACK */
1356         return snd_soc_dapm_sync(&codec->dapm);
1357 }
1358
1359 static struct snd_soc_dai_link midas_dai[] = {
1360         { /* Sec_Fifo DAI i/f */
1361                 .name = "Sec_FIFO TX",
1362                 .stream_name = "Sec_Dai",
1363                 .cpu_dai_name = "samsung-i2s.4",
1364                 .codec_dai_name = "wm8994-aif1",
1365 #ifndef CONFIG_SND_SOC_SAMSUNG_USE_DMA_WRAPPER
1366                 .platform_name = "samsung-audio-idma",
1367 #else
1368                 .platform_name = "samsung-audio",
1369 #endif
1370                 .codec_name = "wm8994-codec",
1371                 .init = midas_wm1811_init_paiftx,
1372                 .ops = &midas_wm1811_aif1_ops,
1373         },
1374         {
1375                 .name = "Midas_WM1811 Voice",
1376                 .stream_name = "Voice Tx/Rx",
1377                 .cpu_dai_name = "midas.cp",
1378                 .codec_dai_name = "wm8994-aif2",
1379                 .platform_name = "snd-soc-dummy",
1380                 .codec_name = "wm8994-codec",
1381                 .ops = &midas_wm1811_aif2_ops,
1382                 .ignore_suspend = 1,
1383         },
1384         {
1385                 .name = "Midas_WM1811 BT",
1386                 .stream_name = "BT Tx/Rx",
1387                 .cpu_dai_name = "midas.bt",
1388                 .codec_dai_name = "wm8994-aif3",
1389                 .platform_name = "snd-soc-dummy",
1390                 .codec_name = "wm8994-codec",
1391                 .ops = &midas_wm1811_aif3_ops,
1392                 .ignore_suspend = 1,
1393         },
1394         { /* Primary DAI i/f */
1395                 .name = "WM8994 AIF1",
1396                 .stream_name = "Pri_Dai",
1397                 .cpu_dai_name = "samsung-i2s.0",
1398                 .codec_dai_name = "wm8994-aif1",
1399                 .platform_name = "samsung-audio",
1400                 .codec_name = "wm8994-codec",
1401                 .ops = &midas_wm1811_aif1_ops,
1402         },
1403 };
1404
1405 static int midas_card_suspend_pre(struct snd_soc_card *card)
1406 {
1407         struct snd_soc_codec *codec = card->rtd->codec;
1408         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1409
1410 #ifdef CONFIG_SEC_DEV_JACK
1411         snd_soc_dapm_disable_pin(&codec->dapm, "AIF1CLK");
1412 #endif
1413
1414         return 0;
1415 }
1416
1417 static int midas_card_suspend_post(struct snd_soc_card *card)
1418 {
1419         struct snd_soc_codec *codec = card->rtd->codec;
1420         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1421         struct snd_soc_dai *aif2_dai = card->rtd[1].codec_dai;
1422         int ret;
1423
1424         if (!codec->active) {
1425 #ifndef SND_USE_BIAS_LEVEL
1426                 ret = snd_soc_dai_set_sysclk(aif2_dai,
1427                                              WM8994_SYSCLK_MCLK2,
1428                                              MIDAS_DEFAULT_MCLK2,
1429                                              SND_SOC_CLOCK_IN);
1430
1431                 if (ret < 0)
1432                         dev_err(codec->dev, "Unable to switch to MCLK2: %d\n",
1433                                 ret);
1434
1435                 ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2, 0, 0, 0);
1436
1437                 if (ret < 0)
1438                         dev_err(codec->dev, "Unable to stop FLL2\n");
1439
1440                 ret = snd_soc_dai_set_sysclk(aif1_dai,
1441                                              WM8994_SYSCLK_MCLK2,
1442                                              MIDAS_DEFAULT_MCLK2,
1443                                              SND_SOC_CLOCK_IN);
1444                 if (ret < 0)
1445                         dev_err(codec->dev, "Unable to switch to MCLK2\n");
1446
1447                 ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, 0, 0, 0);
1448
1449                 if (ret < 0)
1450                         dev_err(codec->dev, "Unable to stop FLL1\n");
1451 #endif
1452
1453                 midas_snd_set_mclk(false, true);
1454         }
1455
1456 #ifdef CONFIG_ARCH_EXYNOS5
1457         exynos5_sys_powerdown_xxti_control(midas_snd_get_mclk() ? 1 : 0);
1458 #else /* for CONFIG_ARCH_EXYNOS5 */
1459         exynos4_sys_powerdown_xusbxti_control(midas_snd_get_mclk() ? 1 : 0);
1460 #endif
1461
1462         return 0;
1463 }
1464
1465 static int midas_card_resume_pre(struct snd_soc_card *card)
1466 {
1467         struct snd_soc_codec *codec = card->rtd->codec;
1468         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1469         int ret;
1470
1471         midas_snd_set_mclk(true, false);
1472
1473 #ifndef SND_USE_BIAS_LEVEL
1474         /* Switch the FLL */
1475         ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
1476                                   WM8994_FLL_SRC_MCLK1,
1477                                   MIDAS_DEFAULT_MCLK1,
1478                                   MIDAS_DEFAULT_SYNC_CLK);
1479
1480         if (ret < 0)
1481                 dev_err(aif1_dai->dev, "Unable to start FLL1: %d\n", ret);
1482
1483         /* Then switch AIF1CLK to it */
1484         ret = snd_soc_dai_set_sysclk(aif1_dai,
1485                                      WM8994_SYSCLK_FLL1,
1486                                      MIDAS_DEFAULT_SYNC_CLK,
1487                                      SND_SOC_CLOCK_IN);
1488
1489         if (ret < 0)
1490                 dev_err(aif1_dai->dev, "Unable to switch to FLL1: %d\n", ret);
1491 #endif
1492
1493         return 0;
1494 }
1495
1496 static int midas_card_resume_post(struct snd_soc_card *card)
1497 {
1498         struct snd_soc_codec *codec = card->rtd->codec;
1499         struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
1500         int reg = 0;
1501
1502         /* workaround for jack detection
1503          * sometimes WM8994_GPIO_1 type changed wrong function type
1504          * so if type mismatched, update to IRQ type
1505          */
1506         reg = snd_soc_read(codec, WM8994_GPIO_1);
1507
1508         if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_IRQ) {
1509                 dev_err(codec->dev, "%s: GPIO1 type 0x%x\n", __func__, reg);
1510                 snd_soc_write(codec, WM8994_GPIO_1, WM8994_GP_FN_IRQ);
1511         }
1512
1513 #ifdef CONFIG_SEC_DEV_JACK
1514         snd_soc_dapm_force_enable_pin(&codec->dapm, "AIF1CLK");
1515 #endif
1516
1517         return 0;
1518 }
1519
1520 #ifdef SND_USE_BIAS_LEVEL
1521 static int midas_set_bias_level(struct snd_soc_card *card,
1522                                 struct snd_soc_dapm_context *dapm,
1523                                 enum snd_soc_bias_level level)
1524 {
1525         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1526
1527         if (dapm->dev != aif1_dai->dev)
1528                 return 0;
1529
1530         switch (level) {
1531         case SND_SOC_BIAS_PREPARE:
1532                 midas_start_fll1(card->rtd[0].codec_dai);
1533                 break;
1534
1535         default:
1536                 break;
1537         }
1538
1539         return 0;
1540 }
1541
1542 static int midas_set_bias_level_post(struct snd_soc_card *card,
1543                                      struct snd_soc_dapm_context *dapm,
1544                                      enum snd_soc_bias_level level)
1545 {
1546         struct snd_soc_codec *codec = card->rtd->codec;
1547         struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
1548         struct snd_soc_dai *aif2_dai = card->rtd[1].codec_dai;
1549         int ret;
1550
1551         if (dapm->dev != aif1_dai->dev)
1552                 return 0;
1553
1554         switch (level) {
1555         case SND_SOC_BIAS_STANDBY:
1556
1557                 /* When going idle stop FLL1 and revert to using MCLK2
1558                  * directly for minimum power consumptin for accessory
1559                  * detection.
1560                  */
1561                 if (card->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
1562                         dev_info(aif1_dai->dev, "Moving to STANDBY\n");
1563
1564                         ret = snd_soc_dai_set_sysclk(aif2_dai,
1565                                                      WM8994_SYSCLK_MCLK2,
1566                                                      MIDAS_DEFAULT_MCLK2,
1567                                                      SND_SOC_CLOCK_IN);
1568                         if (ret < 0)
1569                                 dev_err(codec->dev, "Failed to switch to MCLK2\n");
1570
1571                         ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
1572                                                   0, 0, 0);
1573
1574                         if (ret < 0)
1575                                 dev_err(codec->dev,
1576                                         "Failed to change FLL2\n");
1577
1578                         ret = snd_soc_dai_set_sysclk(aif1_dai,
1579                                                      WM8994_SYSCLK_MCLK2,
1580                                                      MIDAS_DEFAULT_MCLK2,
1581                                                      SND_SOC_CLOCK_IN);
1582                         if (ret < 0)
1583                                 dev_err(codec->dev,
1584                                         "Failed to switch to MCLK2\n");
1585
1586                         ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
1587                                                   0, 0, 0);
1588                         if (ret < 0)
1589                                 dev_err(codec->dev,
1590                                         "Failed to stop FLL1\n");
1591
1592
1593                         midas_fll1_active = false;
1594                         midas_snd_set_mclk(false, false);
1595                 }
1596
1597                 break;
1598         default:
1599                 break;
1600         }
1601
1602         card->dapm.bias_level = level;
1603
1604         return 0;
1605 }
1606 #endif
1607
1608 static struct snd_soc_card midas = {
1609         .name = "Midas_WM1811",
1610         .dai_link = midas_dai,
1611
1612         /* If you want to use sec_fifo device,
1613          * changes the num_link = 2 or ARRAY_SIZE(midas_dai). */
1614         .num_links = ARRAY_SIZE(midas_dai),
1615
1616 #ifdef SND_USE_BIAS_LEVEL
1617         .set_bias_level = midas_set_bias_level,
1618         .set_bias_level_post = midas_set_bias_level_post,
1619 #endif
1620
1621         .suspend_post = midas_card_suspend_post,
1622         .resume_pre = midas_card_resume_pre,
1623         .suspend_pre = midas_card_suspend_pre,
1624         .resume_post = midas_card_resume_post
1625 };
1626
1627 static struct platform_device *midas_snd_device;
1628
1629 static int __init midas_audio_init(void)
1630 {
1631         struct wm1811_machine_priv *wm1811;
1632         int ret;
1633
1634         wm1811 = kzalloc(sizeof *wm1811, GFP_KERNEL);
1635         if (!wm1811) {
1636                 pr_err("Failed to allocate memory\n");
1637                 ret = -ENOMEM;
1638                 goto err_kzalloc;
1639         }
1640         snd_soc_card_set_drvdata(&midas, wm1811);
1641
1642         midas_snd_device = platform_device_alloc("soc-audio", -1);
1643         if (!midas_snd_device) {
1644                 ret = -ENOMEM;
1645                 goto err_device_alloc;
1646         }
1647
1648         ret = snd_soc_register_dais(&midas_snd_device->dev, midas_ext_dai,
1649                                                 ARRAY_SIZE(midas_ext_dai));
1650         if (ret != 0)
1651                 pr_err("Failed to register external DAIs: %d\n", ret);
1652
1653         platform_set_drvdata(midas_snd_device, &midas);
1654
1655         ret = platform_device_add(midas_snd_device);
1656         if (ret)
1657                 platform_device_put(midas_snd_device);
1658
1659         midas_gpio_init();
1660
1661         return ret;
1662
1663 err_device_alloc:
1664         kfree(wm1811);
1665 err_kzalloc:
1666         return ret;
1667 }
1668 module_init(midas_audio_init);
1669
1670 static void __exit midas_audio_exit(void)
1671 {
1672         struct snd_soc_card *card = &midas;
1673         struct wm1811_machine_priv *wm1811 = snd_soc_card_get_drvdata(card);
1674         platform_device_unregister(midas_snd_device);
1675         kfree(wm1811);
1676 }
1677 module_exit(midas_audio_exit);
1678
1679 MODULE_AUTHOR("JS. Park <aitdark.park@samsung.com>");
1680 MODULE_DESCRIPTION("ALSA SoC Midas WM1811");
1681 MODULE_LICENSE("GPL");