UPSTREAM: ASoC: da7219: Remove support for 32KHz PLL mode
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / rt5625.c
1 /*
2  * rt5625.c  --  RT5625 ALSA SoC audio codec driver
3  *
4  * Copyright 2011 Realtek Semiconductor Corp.
5  * Author: Johnny Hsu <johnnyhsu@realtek.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pm.h>
17 #include <linux/i2c.h>
18 #include <linux/platform_device.h>
19 #include <linux/spi/spi.h>
20 #include <linux/of_gpio.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
28 #include <linux/gpio.h>
29 #include "rt5625.h"
30
31 #define INVALID_GPIO -1
32
33 //#define RT5625_PROC
34 #ifdef RT5625_PROC
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/vmalloc.h>
38 char debug_write_read = 0;
39 #endif
40
41 #if 1
42 #define DBG(x...) printk(KERN_INFO x)
43 #else
44 #define DBG(x...) do { } while (0)
45 #endif
46
47 static struct snd_soc_codec *rt5625_codec;
48
49 #define RT5625_REG_RW 1 /* for debug */
50 //#define RT5625_DEMO 1         /* only for demo; please remove it */
51
52 #define RT5625_F_SMT_PHO
53 #define RT5625_PLY_BIT 0
54 #define RT5625_PLY_MASK (0x1)
55 #define RT5625_REC_BIT 1
56 #define RT5625_REC_MASK (0x1 << RT5625_REC_BIT)
57 #define RT5625_3G_BIT 2
58 #define RT5625_3G_MASK (0x1 << RT5625_3G_BIT)
59 #define RT5625_BT_BIT 3
60 #define RT5625_BT_MASK (0x1 << RT5625_BT_BIT)
61 #define RT5625_VOIP_BIT 4
62 #define RT5625_VOIP_MASK (0x1 << RT5625_VOIP_BIT)
63
64 struct rt5625_priv {
65         unsigned int stereo_sysclk;
66         unsigned int voice_sysclk;
67
68         int vodsp_fun;
69         int spk_ctr_status;
70         int spk_ctr_pin;
71         int spk_ctr_on;
72         int spk_ctr_off;
73 #ifdef RT5625_F_SMT_PHO
74         int app_bmp;/* bit{0, 1, 2, 3, 4} = {play, rec, 3g, bt, voip} */
75         int pll_sel;
76         int pll2_sel;
77         int dac_active;
78         int adc_active;
79         int headset;
80         int vodsp_fun_bak;
81 #endif
82 };
83
84 #ifdef RT5625_F_SMT_PHO
85 static u16 rt5625_voip_back[][2] = {
86         {RT5625_VODSP_PDM_CTL, 0x0000},
87         {RT5625_F_DAC_ADC_VDAC, 0x0000},
88 };
89 #define RT5625_VOIP_BK_NUM \
90         (sizeof(rt5625_voip_back) / sizeof(rt5625_voip_back[0]))
91 #endif
92
93 #ifdef RT5625_DEMO
94 struct rt5625_init_reg {
95         u8 reg;
96         u16 val;
97 };
98
99 static struct rt5625_init_reg init_list[] = {
100         {RT5625_HP_OUT_VOL              , 0x8888},      //default is -12db
101         {RT5625_SPK_OUT_VOL             , 0x8080},      //default is 0db
102         {RT5625_PHONEIN_VOL     , 0xe800},      //phone differential
103         {RT5625_DAC_MIC_CTRL    , 0xee01},      //DAC to hpmixer & spkmixer
104         {RT5625_OUTMIX_CTRL             , 0x2bc8},      //spk from spkmixer; hp from hpmixer; aux from monomixer; classAB
105         {RT5625_ADC_REC_MIXER   , 0x1f1f},      //record source from mic1 & mic2
106         {RT5625_GEN_CTRL1               , 0x0c08},      //speaker vdd ratio is 1; 1.25VDD ratio
107 };
108 #define RT5625_INIT_REG_LEN ARRAY_SIZE(init_list)
109
110 static int rt5625_reg_init(struct snd_soc_codec *codec)
111 {
112         int i;
113         for (i = 0; i < RT5625_INIT_REG_LEN; i++)
114                 snd_soc_write(codec, init_list[i].reg, init_list[i].val);
115         return 0;
116 }
117 #endif
118
119 static const u16 rt5625_reg[0x80] = {
120         [RT5625_RESET] = 0x59b4,
121         [RT5625_SPK_OUT_VOL] = 0x8080,
122         [RT5625_HP_OUT_VOL] = 0x8080,
123         [RT5625_AUX_OUT_VOL] = 0x8080,
124         [RT5625_PHONEIN_VOL] = 0xc800,
125         [RT5625_LINE_IN_VOL] = 0xe808,
126         [RT5625_DAC_VOL] = 0x1010,
127         [RT5625_MIC_VOL] = 0x0808,
128         [RT5625_DAC_MIC_CTRL] = 0xee0f,
129         [RT5625_ADC_REC_GAIN] = 0xcbcb,
130         [RT5625_ADC_REC_MIXER] = 0x7f7f,
131         [RT5625_VDAC_OUT_VOL] = 0xe010,
132         [RT5625_OUTMIX_CTRL] = 0x8008,
133         [RT5625_VODSP_CTL] = 0x2007,
134         [RT5625_DMIC_CTRL] = 0x00c0,
135         [RT5625_PD_CTRL] = 0xef00,
136         [RT5625_GEN_CTRL1] = 0x0c0a,
137         [RT5625_LDO_CTRL] = 0x0029,
138         [RT5625_GPIO_CONFIG] = 0xbe3e,
139         [RT5625_GPIO_POLAR] = 0x3e3e,
140         [RT5625_GPIO_STATUS] = 0x803a,
141         [RT5625_SOFT_VOL_CTRL] = 0x0009,
142         [RT5625_DAC_CLK_CTRL1] = 0x3075,
143         [RT5625_DAC_CLK_CTRL2] = 0x1010,
144         [RT5625_VDAC_CLK_CTRL1] = 0x3110,
145         [RT5625_PS_CTRL] = 0x0553,
146         [RT5625_VENDOR_ID1] = 0x10ec,
147         [RT5625_VENDOR_ID2] = 0x5c02,
148 };
149
150 rt5625_dsp_reg rt5625_dsp_init[] = {
151         {0x232C, 0x0025},
152         {0x230B, 0x0001},
153         {0x2308, 0x007F},
154         {0x23F8, 0x4003},
155         {0x2301, 0x0002},
156         {0x2328, 0x0001},
157         {0x2304, 0x00FA},
158         {0x2305, 0x0500},
159         {0x2306, 0x4000},
160         {0x230D, 0x0300},
161         {0x230E, 0x0280},
162         {0x2312, 0x00B1},
163         {0x2314, 0xC000},
164         {0x2316, 0x0041},
165         {0x2317, 0x2800},
166         {0x2318, 0x0800},
167         {0x231D, 0x0050},
168         {0x231F, 0x4000},
169         {0x2330, 0x0008},
170         {0x2335, 0x000A},
171         {0x2336, 0x0004},
172         {0x2337, 0x5000},
173         {0x233A, 0x0300},
174         {0x233B, 0x0030},
175         {0x2341, 0x0008},
176         {0x2343, 0x0800},       
177         {0x2352, 0x7FFF},
178         {0x237F, 0x0400},
179         {0x23A7, 0x2800},
180         {0x22CE, 0x0400},
181         {0x22D3, 0x1500},
182         {0x22D4, 0x2800},
183         {0x22D5, 0x3000},
184         {0x2399, 0x2800},
185         {0x230C, 0x0000},       
186 };
187 #define RT5625_DSP_INIT_NUM ARRAY_SIZE(rt5625_dsp_init)
188
189 static int rt5625_volatile_register(
190         struct snd_soc_codec *codec, unsigned int reg)
191 {
192         switch (reg) {
193         case RT5625_RESET:
194         case RT5625_PD_CTRL:
195         case RT5625_GPIO_STATUS:
196         case RT5625_OTC_STATUS:
197         case RT5625_PRIV_DATA:
198         case RT5625_EQ_CTRL:
199         case RT5625_DSP_DATA:
200         case RT5625_DSP_CMD:
201         case RT5625_VENDOR_ID1:
202         case RT5625_VENDOR_ID2:
203                 return 1;
204         default:
205                 return 0;
206         }
207 }
208
209 static int rt5625_readable_register(
210         struct snd_soc_codec *codec, unsigned int reg)
211 {
212         switch (reg) {
213         case RT5625_RESET:
214         case RT5625_SPK_OUT_VOL:
215         case RT5625_HP_OUT_VOL:
216         case RT5625_AUX_OUT_VOL:
217         case RT5625_PHONEIN_VOL:
218         case RT5625_LINE_IN_VOL:
219         case RT5625_DAC_VOL:
220         case RT5625_MIC_VOL:
221         case RT5625_DAC_MIC_CTRL:
222         case RT5625_ADC_REC_GAIN:
223         case RT5625_ADC_REC_MIXER:
224         case RT5625_VDAC_OUT_VOL:
225         case RT5625_VODSP_PDM_CTL:
226         case RT5625_OUTMIX_CTRL:
227         case RT5625_VODSP_CTL:
228         case RT5625_MIC_CTRL:
229         case RT5625_DMIC_CTRL:
230         case RT5625_PD_CTRL:
231         case RT5625_F_DAC_ADC_VDAC:
232         case RT5625_SDP_CTRL:
233         case RT5625_EXT_SDP_CTRL:
234         case RT5625_PWR_ADD1:
235         case RT5625_PWR_ADD2:
236         case RT5625_PWR_ADD3:
237         case RT5625_GEN_CTRL1:
238         case RT5625_GEN_CTRL2:
239         case RT5625_PLL_CTRL:
240         case RT5625_PLL2_CTRL:
241         case RT5625_LDO_CTRL:
242         case RT5625_GPIO_CONFIG:
243         case RT5625_GPIO_POLAR:
244         case RT5625_GPIO_STICKY:
245         case RT5625_GPIO_WAKEUP:
246         case RT5625_GPIO_STATUS:
247         case RT5625_GPIO_SHARING:
248         case RT5625_OTC_STATUS:
249         case RT5625_SOFT_VOL_CTRL:
250         case RT5625_GPIO_OUT_CTRL:
251         case RT5625_MISC_CTRL:
252         case RT5625_DAC_CLK_CTRL1:
253         case RT5625_DAC_CLK_CTRL2:
254         case RT5625_VDAC_CLK_CTRL1:
255         case RT5625_PS_CTRL:
256         case RT5625_PRIV_INDEX:
257         case RT5625_PRIV_DATA:
258         case RT5625_EQ_CTRL:
259         case RT5625_DSP_ADDR:
260         case RT5625_DSP_DATA:
261         case RT5625_DSP_CMD:
262         case RT5625_VENDOR_ID1:
263         case RT5625_VENDOR_ID2:
264                 return 1;
265         default:
266                 return 0;
267         }
268 }
269
270 static unsigned int rt5625_read(struct snd_soc_codec *codec,
271                                 unsigned int reg)
272 {
273         unsigned int val;
274
275         val = codec->hw_read(codec, reg);
276         return val;
277 }
278
279 static int rt5625_write(struct snd_soc_codec *codec, unsigned int reg,
280         unsigned int value)
281 {
282         unsigned int val;
283         u8 data[3];
284
285         data[0] = reg;
286         data[1] = (value >> 8) & 0xff;
287         data[2] = value & 0xff;
288         
289         val = codec->hw_write(codec->control_data, data, 3);
290         return val;
291 }
292
293 static int rt5625_reset(struct snd_soc_codec *codec)
294 {
295         return snd_soc_write(codec, RT5625_RESET, 0);
296 }
297
298 /**
299  * rt5625_index_write - Write private register.
300  * @codec: SoC audio codec device.
301  * @reg: Private register index.
302  * @value: Private register Data.
303  *
304  * Modify private register for advanced setting. It can be written through
305  * private index (0x6a) and data (0x6c) register.
306  *
307  * Returns 0 for success or negative error code.
308  */
309 static int rt5625_index_write(struct snd_soc_codec *codec,
310                 unsigned int reg, unsigned int value)
311 {
312         int ret;
313
314         ret = snd_soc_write(codec, RT5625_PRIV_INDEX, reg);
315         if (ret < 0) {
316                 dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
317                 goto err;
318         }
319         ret = snd_soc_write(codec, RT5625_PRIV_DATA, value);
320         if (ret < 0) {
321                 dev_err(codec->dev, "Failed to set private value: %d\n", ret);
322                 goto err;
323         }
324         return 0;
325
326 err:
327         return ret;
328 }
329
330 /**
331  * rt5625_index_read - Read private register.
332  * @codec: SoC audio codec device.
333  * @reg: Private register index.
334  *
335  * Read advanced setting from private register. It can be read through
336  * private index (0x6a) and data (0x6c) register.
337  *
338  * Returns private register value or negative error code.
339  */
340 static unsigned int rt5625_index_read(
341         struct snd_soc_codec *codec, unsigned int reg)
342 {
343         int ret;
344
345         ret = snd_soc_write(codec, RT5625_PRIV_INDEX, reg);
346         if (ret < 0) {
347                 dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
348                 return ret;
349         }
350         return snd_soc_read(codec, RT5625_PRIV_DATA);
351 }
352
353 /**
354  * rt5625_index_update_bits - update private register bits
355  * @codec: Audio codec
356  * @reg: Private register index.
357  * @mask: Register mask
358  * @value: New value
359  *
360  * Writes new register value.
361  *
362  * Returns 1 for change, 0 for no change, or negative error code.
363  */
364 static int rt5625_index_update_bits(struct snd_soc_codec *codec,
365         unsigned int reg, unsigned int mask, unsigned int value)
366 {
367         unsigned int old, new;
368         int change, ret;
369
370         ret = rt5625_index_read(codec, reg);
371         if (ret < 0) {
372                 dev_err(codec->dev, "Failed to read private reg: %d\n", ret);
373                 goto err;
374         }
375
376         old = ret;
377         new = (old & ~mask) | (value & mask);
378         change = old != new;
379         if (change) {
380                 ret = rt5625_index_write(codec, reg, new);
381                 if (ret < 0) {
382                         dev_err(codec->dev,
383                                 "Failed to write private reg: %d\n", ret);
384                         goto err;
385                 }
386         }
387         return change;
388
389 err:
390         return ret;
391 }
392
393 /**
394  * rt5625_dsp_done - Wait until DSP is ready.
395  * @codec: SoC Audio Codec device.
396  *
397  * To check voice DSP status and confirm it's ready for next work.
398  *
399  * Returns 0 for success or negative error code.
400  */
401 static int rt5625_dsp_done(struct snd_soc_codec *codec)
402 {
403         unsigned int count = 0, dsp_val;
404
405         dsp_val = snd_soc_read(codec, RT5625_DSP_CMD);
406         while(dsp_val & RT5625_DSP_BUSY_MASK) {
407                 if(count > 10)
408                         return -EBUSY;
409                 dsp_val = snd_soc_read(codec, RT5625_DSP_CMD);
410                 count ++;               
411         }
412
413         return 0;
414 }
415
416 /**
417  * rt5625_dsp_write - Write DSP register.
418  * @codec: SoC audio codec device.
419  * @reg: DSP register index.
420  * @value: DSP register Data.
421  *
422  * Modify voice DSP register for sound effect. The DSP can be controlled
423  * through DSP addr (0x70), data (0x72) and cmd (0x74) register. It has
424  * to wait until the DSP is ready.
425  *
426  * Returns 0 for success or negative error code.
427  */
428 static int rt5625_dsp_write(struct snd_soc_codec *codec,
429                 unsigned int reg, unsigned int value)
430 {
431         int ret;
432
433         ret = rt5625_dsp_done(codec);
434         if (ret < 0) {
435                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
436                 goto err;
437         }
438         ret = snd_soc_write(codec, RT5625_DSP_ADDR, reg);
439         if (ret < 0) {
440                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
441                 goto err;
442         }
443         ret = snd_soc_write(codec, RT5625_DSP_DATA, value);
444         if (ret < 0) {
445                 dev_err(codec->dev, "Failed to write DSP data reg: %d\n", ret);
446                 goto err;
447         }
448         ret = snd_soc_write(codec, RT5625_DSP_CMD,
449                 RT5625_DSP_W_EN | RT5625_DSP_CMD_MW);
450         if (ret < 0) {
451                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
452                 goto err;
453         }
454         mdelay(10);
455         return 0;
456         
457 err:
458         return ret;
459 }
460
461 /**
462  * rt5625_dsp_read - Read DSP register.
463  * @codec: SoC audio codec device.
464  * @reg: DSP register index.
465  *
466  * Read DSP setting value from voice DSP. The DSP can be controlled
467  * through DSP addr (0x70), data (0x72) and cmd (0x74) register. Each
468  * command has to wait until the DSP is ready.
469  *
470  * Returns DSP register value or negative error code.
471  */
472 static unsigned int rt5625_dsp_read(
473         struct snd_soc_codec *codec, unsigned int reg)
474 {
475         unsigned int val_h, val_l;
476         int ret = 0;
477
478         ret = rt5625_dsp_done(codec);
479         if (ret < 0) {
480                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
481                 goto err;
482         }
483         ret = snd_soc_write(codec, RT5625_DSP_ADDR, reg);
484         if (ret < 0) {
485                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
486                 goto err;
487         }
488         ret = snd_soc_write(codec, RT5625_DSP_CMD,
489                 RT5625_DSP_R_EN | RT5625_DSP_CMD_MR);
490         if (ret < 0) {
491                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
492                 goto err;
493         }
494
495         /* Read DSP high byte data */
496         ret = rt5625_dsp_done(codec);
497         if (ret < 0) {
498                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
499                 goto err;
500         }
501         ret = snd_soc_write(codec, RT5625_DSP_ADDR, 0x26);
502         if (ret < 0) {
503                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
504                 goto err;
505         }
506         ret = snd_soc_write(codec, RT5625_DSP_CMD,
507                 RT5625_DSP_R_EN | RT5625_DSP_CMD_RR);
508         if (ret < 0) {
509                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
510                 goto err;
511         }
512         ret = rt5625_dsp_done(codec);
513         if (ret < 0) {
514                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
515                 goto err;
516         }
517         ret = snd_soc_read(codec, RT5625_DSP_DATA);
518         if (ret < 0) {
519                 dev_err(codec->dev, "Failed to read DSP data reg: %d\n", ret);
520                 goto err;
521         }
522         val_h = ret;
523
524         /* Read DSP low byte data */
525         ret = snd_soc_write(codec, RT5625_DSP_ADDR, 0x25);
526         if (ret < 0) {
527                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
528                 goto err;
529         }
530         ret = snd_soc_write(codec, RT5625_DSP_CMD,
531                 RT5625_DSP_R_EN | RT5625_DSP_CMD_RR);
532         if (ret < 0) {
533                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
534                 goto err;
535         }
536         ret = rt5625_dsp_done(codec);
537         if (ret < 0) {
538                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
539                 goto err;
540         }
541         ret = snd_soc_read(codec, RT5625_DSP_DATA);
542         if (ret < 0) {
543                 dev_err(codec->dev, "Failed to read DSP data reg: %d\n", ret);
544                 goto err;
545         }
546         val_l = ret;
547
548         return ((val_h & 0xff) << 8) |(val_l & 0xff);
549
550 err:
551         return ret;
552 }
553
554 static const char *rt5625_spk_ctr_sel[] = {"Disable", "Enable"};
555
556 /* ADCR function select */
557 static const char *adcr_fun_sel[] = {
558         "Stereo ADC", "Voice ADC", "VoDSP", "PDM Slave"};
559
560 static const struct soc_enum adcr_fun_sel_enum =
561         SOC_ENUM_SINGLE(RT5625_F_DAC_ADC_VDAC, RT5625_ADCR_F_SFT,
562                                 ARRAY_SIZE(adcr_fun_sel), adcr_fun_sel);
563
564 /* ADCL function select */
565 static const char *adcl_fun_sel[] = {"Stereo ADC", "VoDSP"};
566
567 static const struct soc_enum adcl_fun_sel_enum =
568         SOC_ENUM_SINGLE(RT5625_F_DAC_ADC_VDAC, RT5625_ADCL_F_SFT,
569                         ARRAY_SIZE(adcl_fun_sel), adcl_fun_sel);
570
571 /* Voice DSP */
572 static const char *rt5625_aec_fun[] = {"Disable", "Enable"};
573
574 static const SOC_ENUM_SINGLE_DECL(rt5625_aec_fun_enum, 0, 0, rt5625_aec_fun);
575
576 static const char *rt5625_dsp_lrck[] = {"8KHz", "16KHz"};
577
578 static const SOC_ENUM_SINGLE_DECL(rt5625_dsp_lrck_enum,
579         RT5625_VODSP_CTL, RT5625_DSP_LRCK_SFT, rt5625_dsp_lrck);
580
581 static const char *rt5625_bp_ctrl[] = {"Bypass", "Normal"};
582
583 static const SOC_ENUM_SINGLE_DECL(rt5625_bp_ctrl_enum,
584         RT5625_VODSP_CTL, RT5625_DSP_BP_SFT, rt5625_bp_ctrl);
585
586 static const char *rt5625_pd_ctrl[] = {"Power down", "Normal"};
587
588 static const SOC_ENUM_SINGLE_DECL(rt5625_pd_ctrl_enum,
589         RT5625_VODSP_CTL, RT5625_DSP_PD_SFT, rt5625_pd_ctrl);
590
591 static const char *rt5625_rst_ctrl[] = {"Reset", "Normal"};
592
593 static const SOC_ENUM_SINGLE_DECL(rt5625_rst_ctrl_enum,
594         RT5625_VODSP_CTL, RT5625_DSP_RST_SFT, rt5625_rst_ctrl);
595 /* Speaker */
596 static const char *rt5625_spk_out[] = {"Class AB", "Class D"};
597
598 static const SOC_ENUM_SINGLE_DECL(rt5625_spk_out_enum,
599         RT5625_OUTMIX_CTRL, RT5625_SPK_T_SFT, rt5625_spk_out);
600
601 static const char *rt5625_spkl_src[] = {"LPRN", "LPRP", "LPLN", "MM"};
602
603 static const SOC_ENUM_SINGLE_DECL(rt5625_spkl_src_enum,
604         RT5625_OUTMIX_CTRL, RT5625_SPKN_S_SFT, rt5625_spkl_src);
605
606 static const char *rt5625_spkamp_ratio[] = {"2.25 Vdd", "2.00 Vdd",
607                 "1.75 Vdd", "1.50 Vdd", "1.25 Vdd", "1.00 Vdd"};
608
609 static const SOC_ENUM_SINGLE_DECL(rt5625_spkamp_ratio_enum,
610         RT5625_GEN_CTRL1, RT5625_SPK_R_SFT, rt5625_spkamp_ratio);
611
612 static const char *rt5625_Pin_mode[] = {"IRQ Out", "GPIO enable", "Reserved", "VoDSP bypass"};  
613 static const char *rt5625_Pin_configuration[] = {"Output", "Input"};
614 static const char *rt5625_Pin_level[] = {"Low", "High"};
615
616 /* Output/Input Mode */
617 //static const char *rt5625_auxout_mode[] = {"Differential", "Single ended"};
618
619 //static const SOC_ENUM_SINGLE_DECL(rt5625_auxout_mode_enum,
620 //      RT5625_OUTMIX_CTRL, RT5625_AUXOUT_MODE_SFT, rt5625_auxout_mode);
621
622 //static const char *rt5625_input_mode[] = {"Single ended", "Differential"};
623
624 //static const SOC_ENUM_SINGLE_DECL(rt5625_phone_mode_enum,
625 //      RT5625_PHONEIN_VOL, RT5625_PHO_DIFF_SFT, rt5625_input_mode);
626
627 //static const SOC_ENUM_SINGLE_DECL(rt5625_mic1_mode_enum,
628 //      RT5625_MIC_VOL, RT5625_MIC1_DIFF_SFT, rt5625_input_mode);
629
630 //static const SOC_ENUM_SINGLE_DECL(rt5625_mic2_mode_enum,
631 //      RT5625_MIC_VOL, RT5625_MIC2_DIFF_SFT, rt5625_input_mode);
632
633 static const struct soc_enum rt5625_gpio2_enum[] = {
634 SOC_ENUM_SINGLE(RT5625_GPIO_SHARING, 0, 4, rt5625_Pin_mode),         /*0*/
635 SOC_ENUM_SINGLE(RT5625_GPIO_CONFIG, 2, 2, rt5625_Pin_configuration),/*1*/
636 SOC_ENUM_SINGLE(RT5625_GPIO_OUT_CTRL, 2, 2, rt5625_Pin_level),        /*2*/
637 };
638
639 static int rt5625_adcr_fun_sel_put(struct snd_kcontrol *kcontrol,
640         struct snd_ctl_elem_value *ucontrol)
641 {
642         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
643         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
644         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
645         unsigned int val, mask, bitmask;
646
647         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
648                 ;
649         if (ucontrol->value.enumerated.item[0] > e->max - 1)
650                 return -EINVAL;
651         val = ucontrol->value.enumerated.item[0] << e->shift_l;
652         mask = (bitmask - 1) << e->shift_l;
653
654         snd_soc_update_bits(codec, RT5625_PD_CTRL,
655                 RT5625_PWR_PR0, RT5625_PWR_PR0);
656         if ((rt5625->app_bmp & RT5625_3G_MASK) &&
657                 rt5625->vodsp_fun == RT5625_AEC_EN) {
658                 snd_soc_update_bits(codec, e->reg, mask, RT5625_ADCR_F_PDM);
659         } else if (rt5625->app_bmp & RT5625_VOIP_MASK &&
660                 rt5625->vodsp_fun == RT5625_AEC_EN) {
661                 snd_soc_update_bits(codec, e->reg, mask, RT5625_ADCR_F_PDM);
662         } else if (rt5625->app_bmp & RT5625_BT_MASK) {
663                 snd_soc_update_bits(codec, e->reg, mask, RT5625_ADCR_F_VADC);
664         } else {
665                 snd_soc_update_bits(codec, e->reg, mask, val);
666         }
667         snd_soc_update_bits(codec, RT5625_PD_CTRL, RT5625_PWR_PR0, 0);
668
669         return 0;
670 }
671
672 static int rt5625_adcl_fun_sel_put(struct snd_kcontrol *kcontrol,
673         struct snd_ctl_elem_value *ucontrol)
674 {
675         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
676         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
677         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
678         unsigned int val, mask, bitmask;
679
680         for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
681                 ;
682         if (ucontrol->value.enumerated.item[0] > e->max - 1)
683                 return -EINVAL;
684         val = ucontrol->value.enumerated.item[0] << e->shift_l;
685         mask = (bitmask - 1) << e->shift_l;
686
687         snd_soc_update_bits(codec, RT5625_PD_CTRL,
688                 RT5625_PWR_PR0, RT5625_PWR_PR0);
689         if ((rt5625->app_bmp & RT5625_3G_MASK) &&
690                 rt5625->vodsp_fun == RT5625_AEC_EN) {
691                 snd_soc_update_bits(codec, e->reg, mask, RT5625_ADCL_F_DSP);
692         } else {
693                 snd_soc_update_bits(codec, e->reg, mask, val);
694         }
695         snd_soc_update_bits(codec, RT5625_PD_CTRL, RT5625_PWR_PR0, 0);
696
697         return 0;
698 }
699
700 static int rt5625_init_vodsp_aec(struct snd_soc_codec *codec)
701 {
702         int i, ret = 0;
703
704         /*disable LDO power*/
705         snd_soc_update_bits(codec, RT5625_LDO_CTRL,
706                 RT5625_LDO_MASK, RT5625_LDO_DIS);
707         mdelay(20);     
708         snd_soc_update_bits(codec, RT5625_VODSP_CTL,
709                 RT5625_DSP_PD_MASK, RT5625_DSP_PD_NOR);
710         /*enable LDO power and set output voltage to 1.2V*/
711         snd_soc_update_bits(codec, RT5625_LDO_CTRL,
712                 RT5625_LDO_MASK | RT5625_LDO_VC_MASK,
713                 RT5625_LDO_EN | RT5625_LDO_VC_1_20V);
714         mdelay(20);
715         /*enable power of VODSP I2C interface*/ 
716         snd_soc_update_bits(codec, RT5625_PWR_ADD3, RT5625_P_DSP_IF |
717                 RT5625_P_DSP_I2C, RT5625_P_DSP_IF | RT5625_P_DSP_I2C);
718         mdelay(1);
719         /*Reset VODSP*/
720         snd_soc_update_bits(codec, RT5625_VODSP_CTL,
721                 RT5625_DSP_RST_MASK, RT5625_DSP_RST_EN);
722         mdelay(1);
723         /*set VODSP to non-reset status*/
724         snd_soc_update_bits(codec, RT5625_VODSP_CTL,
725                 RT5625_DSP_RST_MASK, RT5625_DSP_RST_NOR);
726         mdelay(20);
727
728         /*initize AEC paramter*/
729         for(i = 0; i < RT5625_DSP_INIT_NUM; i++) {
730                 ret = rt5625_dsp_write(codec, rt5625_dsp_init[i].index,
731                                         rt5625_dsp_init[i].value);
732                 if(ret)
733                         return -EIO;
734         }               
735         mdelay(10);     
736         //printk("[DSP poweron] 0x%04x: 0x%04x\n", 0x230C, rt5625_dsp_read(codec, 0x230C));
737
738         return 0;
739 }
740
741 static int rt5625_aec_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
742 {
743         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
744         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
745
746         ucontrol->value.integer.value[0] = rt5625->vodsp_fun;
747         return 0;
748 }
749
750
751 static int rt5625_aec_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
752
753         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
754         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
755
756         if(ucontrol->value.integer.value[0] == rt5625->vodsp_fun)
757                 return 0;
758         rt5625->vodsp_fun = ucontrol->value.integer.value[0];
759
760         switch(rt5625->vodsp_fun) {
761         case RT5625_AEC_EN:
762                 break;
763         case RT5625_AEC_DIS:
764                 if (!(rt5625->app_bmp & RT5625_3G_MASK) ||
765                         ((rt5625->app_bmp & RT5625_3G_MASK) && rt5625->headset)) {
766                         snd_soc_update_bits(codec, RT5625_VODSP_CTL,
767                                 RT5625_DSP_PD_MASK, RT5625_DSP_PD_EN);
768                         snd_soc_update_bits(codec, RT5625_PWR_ADD3,
769                                 RT5625_P_DSP_IF | RT5625_P_DSP_I2C, 0);
770                         snd_soc_update_bits(codec, RT5625_LDO_CTRL,
771                                 RT5625_LDO_MASK, RT5625_LDO_DIS);
772                 }
773                 break;
774         default:
775                 break;
776         }
777
778         return 0;
779 }
780
781 #ifdef RT5625_F_SMT_PHO
782 static int rt5625_app_get(struct snd_kcontrol *kcontrol, 
783                 struct snd_ctl_elem_value *ucontrol)
784 {
785         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
786         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
787
788         pr_info("App status: %x\n", rt5625->app_bmp);
789
790         return 0;
791 }
792
793 static int rt5625_cap_voip_chk_put(struct snd_kcontrol *kcontrol, 
794                         struct snd_ctl_elem_value *ucontrol)
795 {
796         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
797         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
798         int i, upd;
799
800         /* VoIP start up if record & playback all turn on, or AEC       *
801          * is disabled. otherwise, cheat dapm.                  */
802         if ((rt5625->app_bmp & RT5625_REC_MASK) == 0 ||
803                 (rt5625->app_bmp & RT5625_PLY_MASK) == 0) {
804                 /* backup registers for voip routing */
805                 for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
806                         rt5625_voip_back[i][1] =
807                                 snd_soc_read(codec, rt5625_voip_back[i][0]);
808                 /* cheat dapm */
809                 snd_soc_update_bits(codec, RT5625_VODSP_PDM_CTL,
810                         RT5625_REC_IIS_S_MASK, RT5625_REC_IIS_S_SRC2);
811                 return 0;
812         }
813
814         if (rt5625->headset) {
815                 /* backup registers for voip routing */
816                 for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
817                         rt5625_voip_back[i][1] =
818                                 snd_soc_read(codec, rt5625_voip_back[i][0]);
819                 /* cheat dapm */
820                 snd_soc_update_bits(codec, RT5625_VODSP_PDM_CTL,
821                         RT5625_REC_IIS_S_MASK, RT5625_REC_IIS_S_SRC2);
822         } else
823                 rt5625->vodsp_fun = RT5625_AEC_EN;
824
825         upd = (rt5625->app_bmp & ~RT5625_VOIP_MASK) |
826                 (ucontrol->value.integer.value[0] << RT5625_VOIP_BIT);
827         if (rt5625->app_bmp != upd) {
828                 rt5625->app_bmp = upd;
829                 rt5625->app_bmp &= ~(RT5625_3G_MASK | RT5625_BT_MASK);
830         }
831
832         return 0;
833 }
834
835 static int rt5625_cap_voip_put(struct snd_kcontrol *kcontrol, 
836                 struct snd_ctl_elem_value *ucontrol)
837 {
838         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
839         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
840         int i;
841
842         if (rt5625->app_bmp & RT5625_VOIP_MASK) {
843                 if (rt5625->headset) {
844                         snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
845                                 RT5625_M_RM_L_MIC1 | RT5625_M_RM_R_MIC1 | RT5625_M_RM_L_PHO,
846                                 RT5625_M_RM_L_MIC1 | RT5625_M_RM_R_MIC1 | RT5625_M_RM_L_PHO);
847                         /* recover all changes by voip */
848                         for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
849                                 snd_soc_write(codec, rt5625_voip_back[i][0],
850                                                 rt5625_voip_back[i][1]);
851                         snd_soc_update_bits(codec, RT5625_VODSP_PDM_CTL,
852                                 RT5625_SRC1_PWR | RT5625_SRC2_PWR, 0);
853                 } else {
854                         snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
855                                 RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 | RT5625_M_RM_R_MIC2 | RT5625_M_RM_L_PHO,
856                                 RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 | RT5625_M_RM_R_MIC2 | RT5625_M_RM_L_PHO);
857                         rt5625->vodsp_fun = RT5625_AEC_EN;
858                         /* Mic1 & Mic2 boost 0db */
859                         snd_soc_update_bits(codec, RT5625_MIC_CTRL,
860                                 RT5625_MIC1_BST_MASK | RT5625_MIC2_BST_MASK,
861                                 RT5625_MIC1_BST_BYPASS | RT5625_MIC2_BST_BYPASS);
862                         /* Capture volume gain 9db */
863                         snd_soc_update_bits(codec, RT5625_ADC_REC_GAIN,
864                                 RT5625_G_ADCL_MASK | RT5625_G_ADCR_MASK, (0x11 << 8 ) | 0x11);
865                 }
866         } else {
867                 /* recover all changes by voip */
868                 for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
869                         snd_soc_write(codec, rt5625_voip_back[i][0],
870                                         rt5625_voip_back[i][1]);
871                 snd_soc_update_bits(codec, RT5625_VODSP_PDM_CTL,
872                         RT5625_SRC1_PWR | RT5625_SRC2_PWR, 0);
873                 if (rt5625->app_bmp & RT5625_3G_MASK &&
874                         rt5625->vodsp_fun ==  RT5625_AEC_EN) {
875                         /* Mic1 boost 0db */
876                         snd_soc_update_bits(codec, RT5625_MIC_CTRL,
877                                 RT5625_MIC1_BST_MASK, RT5625_MIC1_BST_BYPASS);
878                         /* Capture volume gain 9db */
879                         snd_soc_update_bits(codec, RT5625_ADC_REC_GAIN,
880                                 RT5625_G_ADCR_MASK, 0x11);
881                 }
882         }
883
884         return 0;
885 }
886
887 static int rt5625_hs_voip_chk_put(struct snd_kcontrol *kcontrol, 
888                         struct snd_ctl_elem_value *ucontrol)
889 {
890         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
891         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
892         int i, upd;
893
894         rt5625->headset = true;
895         if ((rt5625->app_bmp & RT5625_REC_MASK) == 0 ||
896                 (rt5625->app_bmp & RT5625_PLY_MASK) == 0) {
897                 /* backup registers for voip routing */
898                 for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
899                         rt5625_voip_back[i][1] =
900                                 snd_soc_read(codec, rt5625_voip_back[i][0]);
901                 rt5625->vodsp_fun_bak = rt5625->vodsp_fun;
902                 /* cheat dapm */
903                 snd_soc_update_bits(codec, RT5625_VODSP_PDM_CTL,
904                         RT5625_REC_IIS_S_MASK | RT5625_RXDP_PWR, RT5625_REC_IIS_S_ADC);
905                 return 0;
906         }
907
908         upd = (rt5625->app_bmp & ~RT5625_VOIP_MASK) |
909                 (ucontrol->value.integer.value[0] << RT5625_VOIP_BIT);
910         if (rt5625->app_bmp != upd) {
911                 rt5625->app_bmp = upd;
912                 rt5625->app_bmp &= ~(RT5625_3G_MASK | RT5625_BT_MASK);
913         }
914
915         return 0;
916 }
917
918 static int rt5625_hs_voip_put(struct snd_kcontrol *kcontrol, 
919                 struct snd_ctl_elem_value *ucontrol)
920 {
921         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
922         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
923         int i;
924
925         if (rt5625->app_bmp & RT5625_VOIP_MASK) {
926                 snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
927                         RT5625_M_RM_R_MIC1 | RT5625_M_RM_L_PHO,
928                         RT5625_M_RM_R_MIC1 | RT5625_M_RM_L_PHO);
929                 /* Mic1 & Mic2 boost 0db */
930                 snd_soc_update_bits(codec, RT5625_MIC_CTRL,
931                         RT5625_MIC1_BST_MASK | RT5625_MIC2_BST_MASK,
932                         RT5625_MIC1_BST_BYPASS | RT5625_MIC2_BST_BYPASS);
933                 /* Capture volume gain 9db */
934                 snd_soc_update_bits(codec, RT5625_ADC_REC_GAIN,
935                         RT5625_G_ADCL_MASK | RT5625_G_ADCR_MASK, (0x11 << 8 ) | 0x11);
936         } else {
937                 /* recover all changes by voip */
938                 for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
939                         snd_soc_write(codec, rt5625_voip_back[i][0],
940                                         rt5625_voip_back[i][1]);
941                 rt5625->vodsp_fun = rt5625->vodsp_fun_bak;
942                 if ((rt5625->app_bmp & RT5625_3G_MASK) && rt5625->vodsp_fun == RT5625_AEC_EN) {
943                         snd_soc_update_bits(codec, RT5625_F_DAC_ADC_VDAC,
944                                 RT5625_ADCR_F_MASK, RT5625_ADCR_F_PDM);
945                 } else if (rt5625->app_bmp & RT5625_BT_MASK) {
946                         snd_soc_update_bits(codec, RT5625_F_DAC_ADC_VDAC,
947                                 RT5625_ADCR_F_MASK, RT5625_ADCR_F_VADC);
948                 } else {
949                         snd_soc_update_bits(codec, RT5625_F_DAC_ADC_VDAC,
950                                 RT5625_ADCR_F_MASK, RT5625_ADCR_F_ADC);
951                 }
952         }
953
954         return 0;
955 }
956
957 static int rt5625_voip_chk_put(struct snd_kcontrol *kcontrol, 
958                 struct snd_ctl_elem_value *ucontrol)
959 {
960         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
961         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
962         int i, upd;
963
964         rt5625->headset = false;
965         /* voip start-up if record is on-going; otherwise, cheat dapm */
966         if ((rt5625->app_bmp & RT5625_REC_MASK) == 0 ||
967                 (rt5625->app_bmp & RT5625_PLY_MASK) == 0) {
968                 /* backup registers for voip routing */
969                 for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
970                         rt5625_voip_back[i][1] =
971                                 snd_soc_read(codec, rt5625_voip_back[i][0]);
972                 rt5625->vodsp_fun_bak = rt5625->vodsp_fun;
973                 /* cheat dapm */
974                 snd_soc_update_bits(codec, RT5625_VODSP_PDM_CTL,
975                         RT5625_RXDP_S_MASK | RT5625_REC_IIS_S_MASK | RT5625_RXDP_PWR,
976                         RT5625_RXDP_S_SRC1 | RT5625_REC_IIS_S_SRC2);
977                 return 0;
978         }
979
980         upd = (rt5625->app_bmp & ~RT5625_VOIP_MASK) |
981                 (ucontrol->value.integer.value[0] << RT5625_VOIP_BIT);
982         if (rt5625->app_bmp != upd) {
983                 rt5625->app_bmp = upd;
984                 rt5625->app_bmp &= ~(RT5625_3G_MASK | RT5625_BT_MASK);
985         }
986
987         return 0;
988 }
989
990 static int rt5625_voip_put(struct snd_kcontrol *kcontrol, 
991                 struct snd_ctl_elem_value *ucontrol)
992 {
993         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
994         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
995         int i;
996
997         if (rt5625->app_bmp & RT5625_VOIP_MASK) {
998                 snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
999                         RT5625_M_RM_R_MIC2 | RT5625_M_RM_L_PHO,
1000                         RT5625_M_RM_R_MIC2 | RT5625_M_RM_L_PHO);
1001                 /* Mic1 & Mic2 boost 0db */
1002                 snd_soc_update_bits(codec, RT5625_MIC_CTRL,
1003                         RT5625_MIC1_BST_MASK | RT5625_MIC2_BST_MASK,
1004                         RT5625_MIC1_BST_BYPASS | RT5625_MIC2_BST_BYPASS);
1005                 /* Capture volume gain 9db */
1006                 snd_soc_update_bits(codec, RT5625_ADC_REC_GAIN,
1007                         RT5625_G_ADCL_MASK | RT5625_G_ADCR_MASK, (0x11 << 8 ) | 0x11);
1008         } else {
1009                 /* recover all changes by voip */
1010                 for(i = 0; i < RT5625_VOIP_BK_NUM; i++)
1011                         snd_soc_write(codec, rt5625_voip_back[i][0],
1012                                         rt5625_voip_back[i][1]);
1013                 rt5625->vodsp_fun = rt5625->vodsp_fun_bak;
1014                 snd_soc_update_bits(codec, RT5625_VODSP_PDM_CTL,
1015                         RT5625_SRC1_PWR | RT5625_SRC2_PWR, 0);
1016                 if ((rt5625->app_bmp & RT5625_3G_MASK) && rt5625->vodsp_fun == RT5625_AEC_EN) {
1017                         snd_soc_update_bits(codec, RT5625_F_DAC_ADC_VDAC,
1018                                 RT5625_ADCR_F_MASK, RT5625_ADCR_F_PDM);
1019                 } else if (rt5625->app_bmp & RT5625_BT_MASK) {
1020                         snd_soc_update_bits(codec, RT5625_F_DAC_ADC_VDAC,
1021                                 RT5625_ADCR_F_MASK, RT5625_ADCR_F_VADC);
1022                 } else {
1023                         snd_soc_update_bits(codec, RT5625_F_DAC_ADC_VDAC,
1024                                 RT5625_ADCR_F_MASK, RT5625_ADCR_F_ADC);
1025                 }
1026         }
1027
1028         return 0;
1029 }
1030
1031 static int rt5625_voip_get(struct snd_kcontrol *kcontrol, 
1032                 struct snd_ctl_elem_value *ucontrol)
1033 {
1034         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1035         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1036
1037         ucontrol->value.integer.value[0] =
1038                 (rt5625->app_bmp & RT5625_VOIP_MASK) >> RT5625_VOIP_BIT;
1039
1040         return 0;
1041 }
1042
1043 static int rt5625_play_get(struct snd_kcontrol *kcontrol, 
1044                 struct snd_ctl_elem_value *ucontrol)
1045 {
1046         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1047         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1048
1049         ucontrol->value.integer.value[0] =
1050                 (rt5625->app_bmp & RT5625_PLY_MASK) >> RT5625_PLY_BIT;
1051
1052         return 0;
1053 }
1054
1055 static int rt5625_play_put(struct snd_kcontrol *kcontrol, 
1056                 struct snd_ctl_elem_value *ucontrol)
1057 {
1058         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1059         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1060         int upd;
1061
1062         upd = (rt5625->app_bmp & ~RT5625_PLY_MASK) |
1063                 (ucontrol->value.integer.value[0] << RT5625_PLY_BIT);
1064         if (rt5625->app_bmp != upd)
1065                 rt5625->app_bmp = upd;
1066
1067         if (!(rt5625->app_bmp & RT5625_3G_MASK) ||
1068                 ((rt5625->app_bmp & RT5625_3G_MASK) && rt5625->headset)) {
1069                 snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
1070                         RT5625_M_RM_L_PHO, RT5625_M_RM_L_PHO);
1071         }
1072
1073         return 0;
1074 }
1075
1076 static int rt5625_rec_get(struct snd_kcontrol *kcontrol, 
1077                 struct snd_ctl_elem_value *ucontrol)
1078 {
1079         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1080         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1081
1082         ucontrol->value.integer.value[0] =
1083                 (rt5625->app_bmp & RT5625_REC_MASK) >> RT5625_REC_BIT;
1084
1085         return 0;
1086 }
1087
1088 static int rt5625_rec_put(struct snd_kcontrol *kcontrol, 
1089                 struct snd_ctl_elem_value *ucontrol)
1090 {
1091         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1092         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1093         int upd;
1094
1095         upd = (rt5625->app_bmp & ~RT5625_REC_MASK) |
1096                 (ucontrol->value.integer.value[0] << RT5625_REC_BIT);
1097         if (rt5625->app_bmp != upd)
1098                 rt5625->app_bmp = upd;
1099
1100         if (!(rt5625->app_bmp & RT5625_3G_MASK) ||
1101                 ((rt5625->app_bmp & RT5625_3G_MASK) && rt5625->headset)) {
1102                 snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
1103                         RT5625_M_RM_L_PHO, RT5625_M_RM_L_PHO);
1104         }
1105
1106
1107         return 0;
1108 }
1109
1110 static int rt5625_bt_get(struct snd_kcontrol *kcontrol, 
1111                 struct snd_ctl_elem_value *ucontrol)
1112 {
1113         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1114         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1115
1116         ucontrol->value.integer.value[0] =
1117                 (rt5625->app_bmp & RT5625_BT_MASK) >> RT5625_BT_BIT;
1118
1119         return 0;
1120 }
1121
1122 static int rt5625_bt_put(struct snd_kcontrol *kcontrol, 
1123                 struct snd_ctl_elem_value *ucontrol)
1124 {
1125         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1126         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1127         int upd;
1128
1129         if (!(rt5625->app_bmp & RT5625_REC_MASK)) {
1130                 snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
1131                         RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 |
1132                         RT5625_M_RM_R_MIC1 | RT5625_M_RM_R_MIC2,
1133                         RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 |
1134                         RT5625_M_RM_R_MIC1 | RT5625_M_RM_R_MIC2);
1135         }
1136
1137         upd = (rt5625->app_bmp & ~RT5625_BT_MASK) |
1138                 (ucontrol->value.integer.value[0] << RT5625_BT_BIT);
1139         if (rt5625->app_bmp != upd) {
1140                 rt5625->app_bmp = upd;
1141                 rt5625->app_bmp &= ~(RT5625_3G_MASK | RT5625_VOIP_MASK);
1142         }
1143
1144         return 0;
1145 }
1146
1147 static int rt5625_3g_get(struct snd_kcontrol *kcontrol, 
1148                 struct snd_ctl_elem_value *ucontrol)
1149 {
1150         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1151         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1152         
1153         ucontrol->value.integer.value[0] =
1154                 (rt5625->app_bmp & RT5625_3G_MASK) >> RT5625_3G_BIT;
1155
1156         return 0;
1157 }
1158
1159 static int rt5625_3g_put(struct snd_kcontrol *kcontrol, 
1160                 struct snd_ctl_elem_value *ucontrol)
1161 {
1162         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1163         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1164         int upd;
1165
1166         rt5625->headset = false;
1167         if (!(rt5625->app_bmp & RT5625_REC_MASK)) {
1168                 snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
1169                         RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 | RT5625_M_RM_R_MIC2,
1170                         RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 | RT5625_M_RM_R_MIC2);
1171         }
1172
1173         upd = (rt5625->app_bmp & ~RT5625_3G_MASK) |
1174                 (ucontrol->value.integer.value[0] << RT5625_3G_BIT);
1175         if (rt5625->app_bmp != upd) {
1176                 rt5625->app_bmp = upd;
1177                 rt5625->app_bmp &= ~(RT5625_BT_MASK | RT5625_VOIP_MASK);
1178         }
1179
1180         return 0;
1181 }
1182
1183 static int rt5625_hs_3g_put(struct snd_kcontrol *kcontrol, 
1184                 struct snd_ctl_elem_value *ucontrol)
1185 {
1186         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1187         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1188         int upd;
1189
1190         rt5625->headset = true;
1191         if (!(rt5625->app_bmp & RT5625_REC_MASK)) {
1192                 snd_soc_update_bits(codec, RT5625_ADC_REC_MIXER,
1193                         RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 |
1194                         RT5625_M_RM_R_MIC1 | RT5625_M_RM_R_MIC2,
1195                         RT5625_M_RM_L_MIC1 | RT5625_M_RM_L_MIC2 |
1196                         RT5625_M_RM_R_MIC1 | RT5625_M_RM_R_MIC2);
1197         }
1198         upd = (rt5625->app_bmp & ~RT5625_3G_MASK) |
1199                 (ucontrol->value.integer.value[0] << RT5625_3G_BIT);
1200         if (rt5625->app_bmp != upd) {
1201                 rt5625->app_bmp = upd;
1202                 rt5625->app_bmp &= ~(RT5625_BT_MASK | RT5625_VOIP_MASK);
1203         }
1204
1205         return 0;
1206 }
1207
1208 static int rt5625_dump_dsp_get(struct snd_kcontrol *kcontrol, 
1209                 struct snd_ctl_elem_value *ucontrol)
1210 {
1211         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1212         int i;
1213         u16 val;
1214
1215         pr_info("\n[ RT5625 DSP Register ]\n");
1216         for (i = 0; i < RT5625_DSP_INIT_NUM; i++) {
1217                 val = rt5625_dsp_read(codec, rt5625_dsp_init[i].index);
1218                 if (val) pr_info("    0x%x: 0x%x\n",
1219                         rt5625_dsp_init[i].index, val);
1220         }
1221         return 0;
1222 }
1223
1224 static int rt5625_dac_active_get(struct snd_kcontrol *kcontrol, 
1225                 struct snd_ctl_elem_value *ucontrol)
1226 {
1227         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1228         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1229
1230         ucontrol->value.integer.value[0] = rt5625->dac_active;
1231         return 0;
1232 }
1233
1234 static int rt5625_dac_active_put(struct snd_kcontrol *kcontrol, 
1235                 struct snd_ctl_elem_value *ucontrol)
1236 {
1237         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1238         struct snd_soc_dapm_context *dapm = &codec->dapm;
1239         struct snd_soc_dapm_widget *w;
1240         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1241
1242         if(ucontrol->value.integer.value[0] == rt5625->dac_active)
1243                 return 0;
1244         rt5625->dac_active = ucontrol->value.integer.value[0];
1245
1246         /* playback is on-going; do nothing when turn off BT */
1247         if (rt5625->dac_active == 0 && rt5625->app_bmp & RT5625_PLY_MASK)
1248                 return 0;
1249
1250         list_for_each_entry(w, &dapm->card->widgets, list)
1251         {
1252                 if (!w->sname || w->dapm != dapm)
1253                         continue;
1254                 if (strstr(w->sname, "Playback")) {
1255                         pr_info("widget %s %s %s\n", w->name, w->sname,
1256                                 rt5625->dac_active ? "active" : "inactive");
1257                         w->active = rt5625->dac_active;
1258                 }
1259         }
1260
1261         if (!(rt5625->dac_active))
1262                 snd_soc_dapm_sync(dapm);
1263
1264         return 0;
1265 }
1266
1267 static int rt5625_adc_active_get(struct snd_kcontrol *kcontrol, 
1268                 struct snd_ctl_elem_value *ucontrol)
1269 {
1270         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1271         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1272
1273         ucontrol->value.integer.value[0] = rt5625->adc_active;
1274         return 0;
1275 }
1276
1277 static int rt5625_adc_active_put(struct snd_kcontrol *kcontrol, 
1278                 struct snd_ctl_elem_value *ucontrol)
1279 {
1280         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1281         struct snd_soc_dapm_context *dapm = &codec->dapm;
1282         struct snd_soc_dapm_widget *w;
1283         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1284
1285         if(ucontrol->value.integer.value[0] == rt5625->adc_active)
1286                 return 0;
1287         rt5625->adc_active = ucontrol->value.integer.value[0];
1288
1289         /* record is on-going; do nothing when turn off BT */
1290         if (rt5625->adc_active == 0 && rt5625->app_bmp & RT5625_REC_MASK)
1291                 return 0;
1292         
1293         list_for_each_entry(w, &dapm->card->widgets, list)
1294         {
1295                 if (!w->sname || w->dapm != dapm)
1296                         continue;
1297                 if (strstr(w->sname, "Capture")) {
1298                         pr_info("widget %s %s %s\n", w->name, w->sname,
1299                                 rt5625->adc_active ? "active" : "inactive");
1300                         w->active = rt5625->adc_active;
1301                 }
1302         }
1303
1304         if (!(rt5625->adc_active))
1305                 snd_soc_dapm_sync(dapm);
1306
1307         return 0;
1308 }
1309
1310 static int rt5625_pll_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1311 {
1312         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1313         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1314
1315         ucontrol->value.integer.value[0] = rt5625->pll_sel;
1316         return 0;
1317 }
1318
1319
1320 static int rt5625_pll_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1321
1322         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1323         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1324
1325         if(ucontrol->value.integer.value[0] == rt5625->pll_sel)
1326                 return 0;
1327         rt5625->pll_sel = ucontrol->value.integer.value[0];
1328
1329         switch(rt5625->pll_sel) {
1330         case RT5625_PLL_DIS:
1331                 pr_info("%s(): Disable\n", __func__);
1332                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
1333                                         RT5625_SCLK_PLL1, 0);
1334                 snd_soc_write(codec, RT5625_DAC_CLK_CTRL2, 0);
1335                 break;
1336
1337         case RT5625_PLL_112896_225792:
1338                 pr_info("%s(): 11.2896>22.5792\n", __func__);
1339                 snd_soc_write(codec, RT5625_GEN_CTRL2, 0x0000);
1340                 snd_soc_write(codec, RT5625_PLL_CTRL, 0x06a0);
1341                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
1342                         RT5625_SCLK_PLL1, RT5625_SCLK_PLL1);
1343                 snd_soc_write(codec, RT5625_DAC_CLK_CTRL2, 0x0210);
1344                 break;
1345
1346         case RT5625_PLL_112896_24576:
1347                 pr_info("%s(): 11.2896->24.576\n", __func__);
1348                 snd_soc_write(codec, RT5625_GEN_CTRL2, 0x0000);
1349                 snd_soc_write(codec, RT5625_PLL_CTRL, 0x922f);
1350                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
1351                         RT5625_SCLK_PLL1, RT5625_SCLK_PLL1);
1352                 snd_soc_write(codec, RT5625_DAC_CLK_CTRL2, 0x0210);
1353                 break;
1354
1355         default:
1356                 break;
1357         }
1358
1359         return 0;
1360 }
1361
1362 static int rt5625_pll2_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1363 {
1364         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1365         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1366
1367         ucontrol->value.integer.value[0] = rt5625->pll2_sel;
1368         return 0;
1369 }
1370
1371 static int rt5625_pll2_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1372 {
1373         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1374         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1375
1376         if(ucontrol->value.integer.value[0] == rt5625->pll2_sel)
1377                 return 0;
1378         rt5625->pll2_sel = ucontrol->value.integer.value[0];
1379
1380         if(rt5625->pll2_sel != RT5625_PLL_DIS) {
1381                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
1382                         RT5625_VSCLK_MASK, RT5625_VSCLK_PLL2);
1383                 snd_soc_write(codec, RT5625_PLL2_CTRL, RT5625_PLL2_EN);
1384                 snd_soc_write(codec, RT5625_VDAC_CLK_CTRL1,
1385                                 RT5625_VBCLK_DIV1_4);
1386                 snd_soc_update_bits(codec, RT5625_EXT_SDP_CTRL,
1387                         RT5625_PCM_CS_MASK, RT5625_PCM_CS_VSCLK);
1388         }
1389
1390         return 0;
1391 }
1392
1393 static const char *rt5625_pll_sel[] = {"Disable", "11.2896->22.5792", "11.2896->24.576"};
1394
1395 static const SOC_ENUM_SINGLE_DECL(rt5625_pll_sel_enum, 0, 0, rt5625_pll_sel);
1396
1397 static const char *rt5625_pll2_sel[] = {"Disable", "Enable"};
1398
1399 static const SOC_ENUM_SINGLE_DECL(rt5625_pll2_sel_enum, 0, 0, rt5625_pll2_sel);
1400 #endif
1401
1402 static const char *rt5625_AUXOUT_mode[] = {"Differential mode", "Single-ended mode"}; 
1403 static const char *rt5625_Differential_Input_Control[] = {"Disable", "Enable"};  
1404
1405 static const struct soc_enum rt5625_differential_enum[] = {
1406 SOC_ENUM_SINGLE(RT5625_OUTMIX_CTRL, 4, 2, rt5625_AUXOUT_mode),         /*0*/
1407 SOC_ENUM_SINGLE(RT5625_PHONEIN_VOL, 13, 2, rt5625_Differential_Input_Control),/*1*/
1408 SOC_ENUM_SINGLE(RT5625_MIC_VOL, 15, 2, rt5625_Differential_Input_Control),        /*2*/
1409 SOC_ENUM_SINGLE(RT5625_MIC_VOL, 7, 2, rt5625_Differential_Input_Control),         /*3*/
1410 };
1411
1412 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
1413 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -3525, 75, 0);
1414 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
1415 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -1650, 150, 0);
1416 static const DECLARE_TLV_DB_SCALE(dmic_bst_tlv, 0, 600, 0);
1417 /* {0, +20, +30, +40} dB */
1418 static unsigned int mic_bst_tlv[] = {
1419         TLV_DB_RANGE_HEAD(2),
1420         0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
1421         1, 3, TLV_DB_SCALE_ITEM(2000, 1000, 0),
1422 };
1423
1424 #ifdef RT5625_REG_RW
1425 #define REGVAL_MAX 0xffff
1426 static unsigned int regctl_addr = 0x3e;
1427 static int rt5625_regctl_info(struct snd_kcontrol *kcontrol,
1428                 struct snd_ctl_elem_info *uinfo)
1429 {
1430         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1431         uinfo->count = 2;
1432         uinfo->value.integer.min = 0;
1433         uinfo->value.integer.max = REGVAL_MAX;
1434         return 0;
1435 }
1436
1437 static int rt5625_regctl_get(struct snd_kcontrol *kcontrol,
1438                 struct snd_ctl_elem_value *ucontrol)
1439 {
1440         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1441         ucontrol->value.integer.value[0] = regctl_addr;
1442         ucontrol->value.integer.value[1] = snd_soc_read(codec, regctl_addr);
1443         return 0;
1444 }
1445
1446 static int rt5625_regctl_put(struct snd_kcontrol *kcontrol,
1447                 struct snd_ctl_elem_value *ucontrol)
1448 {
1449         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1450         regctl_addr = ucontrol->value.integer.value[0];
1451         if(ucontrol->value.integer.value[1] <= REGVAL_MAX)
1452                 snd_soc_write(codec, regctl_addr, ucontrol->value.integer.value[1]);
1453         return 0;
1454 }
1455 #endif
1456
1457 /*speaker ext control*/
1458 static const struct soc_enum spk_ctr_enum =
1459         SOC_ENUM_SINGLE(0, 0, ARRAY_SIZE(rt5625_spk_ctr_sel), rt5625_spk_ctr_sel);
1460
1461 static int rt5625_spk_ctr_get(struct snd_kcontrol *kcontrol,
1462                 struct snd_ctl_elem_value *ucontrol)
1463 {
1464         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1465         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1466
1467         ucontrol->value.integer.value[0] = rt5625->spk_ctr_status;
1468         return 0;
1469 }
1470
1471 static int rt5625_spk_ctr_put(struct snd_kcontrol *kcontrol,
1472                 struct snd_ctl_elem_value *ucontrol)
1473 {
1474         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1475         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1476
1477         rt5625->spk_ctr_status = ucontrol->value.integer.value[0];
1478
1479         if(rt5625->spk_ctr_pin != INVALID_GPIO)
1480         {
1481                 if(rt5625->spk_ctr_status)
1482                         gpio_set_value(rt5625->spk_ctr_pin,rt5625->spk_ctr_on);
1483                 else
1484                         gpio_set_value(rt5625->spk_ctr_pin,rt5625->spk_ctr_off);
1485         }
1486         return 0;
1487 }
1488
1489 static const struct snd_kcontrol_new rt5625_snd_controls[] = {
1490
1491         #if defined (CONFIG_SND_SOC_RT5625_SPK_FORM_SPKOUT)
1492         SOC_DOUBLE_TLV("SPKOUT Playback Volume", RT5625_SPK_OUT_VOL,
1493                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1494         SOC_DOUBLE("SPKOUT Playback Switch", RT5625_SPK_OUT_VOL,
1495                 RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1496         SOC_DOUBLE_TLV("Earpiece Playback Volume", RT5625_SPK_OUT_VOL,
1497                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1498         SOC_DOUBLE("Earpiece Playback Switch", RT5625_SPK_OUT_VOL,
1499                 RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1500         #endif
1501
1502         #if defined (CONFIG_SND_SOC_RT5625_SPK_FORM_HPOUT)
1503         SOC_DOUBLE_TLV("SPKOUT Playback Volume", RT5625_HP_OUT_VOL,
1504                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1505         SOC_DOUBLE("SPKOUT Playback Switch", RT5625_HP_OUT_VOL,
1506                 RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1507         SOC_DOUBLE_TLV("Earpiece Playback Volume", RT5625_HP_OUT_VOL,
1508                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1509         SOC_DOUBLE("Earpiece Playback Switch", RT5625_HP_OUT_VOL,
1510                 RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1511         SOC_ENUM_EXT("SPK_CTR",spk_ctr_enum, rt5625_spk_ctr_get, rt5625_spk_ctr_put),
1512         #endif
1513
1514         SOC_ENUM("SPK Amp Type", rt5625_spk_out_enum),
1515         SOC_ENUM("Left SPK Source", rt5625_spkl_src_enum),
1516         SOC_ENUM("SPK Amp Ratio", rt5625_spkamp_ratio_enum),
1517         //SOC_DOUBLE_TLV("Headphone Playback Volume", RT5625_HP_OUT_VOL,
1518         //      RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1519         //SOC_DOUBLE("Headphone Playback Switch", RT5625_HP_OUT_VOL,
1520         //              RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1521         //SOC_ENUM("AUXOUT Mode Control", rt5625_auxout_mode_enum),
1522         SOC_DOUBLE_TLV("AUXOUT Playback Volume", RT5625_AUX_OUT_VOL,
1523                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1524         SOC_DOUBLE("AUXOUT Playback Switch", RT5625_AUX_OUT_VOL,
1525                         RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1526         SOC_DOUBLE_TLV("PCM Playback Volume", RT5625_DAC_VOL,
1527                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 63, 1, dmic_bst_tlv),
1528         //SOC_ENUM("Phone Mode Control", rt5625_phone_mode_enum),
1529         SOC_SINGLE_TLV("Phone Playback Volume", RT5625_PHONEIN_VOL,
1530                 RT5625_L_VOL_SFT, 31, 1, in_vol_tlv),
1531         //SOC_ENUM("MIC1 Mode Control", rt5625_mic1_mode_enum),
1532         SOC_SINGLE_TLV("MIC1 Boost", RT5625_MIC_CTRL,
1533                 RT5625_MIC1_BST_SFT, 3, 0, mic_bst_tlv),
1534         SOC_SINGLE_TLV("Mic1 Playback Volume", RT5625_MIC_VOL,
1535                 RT5625_L_VOL_SFT, 31, 1, in_vol_tlv),
1536         //SOC_ENUM("MIC2 Mode Control", rt5625_mic2_mode_enum),
1537         SOC_SINGLE_TLV("MIC2 Boost", RT5625_MIC_CTRL,
1538                 RT5625_MIC2_BST_SFT, 3, 0, mic_bst_tlv),
1539         SOC_SINGLE_TLV("Mic2 Playback Volume", RT5625_MIC_VOL,
1540                 RT5625_R_VOL_SFT, 31, 1, in_vol_tlv),
1541         SOC_SINGLE_TLV("Dmic Boost", RT5625_DMIC_CTRL,
1542                 RT5625_DIG_BST_SFT, 7, 0, dmic_bst_tlv),
1543         SOC_DOUBLE_TLV("LineIn Playback Volume", RT5625_LINE_IN_VOL,
1544                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, in_vol_tlv),
1545         SOC_DOUBLE_TLV("PCM Capture Volume", RT5625_ADC_REC_GAIN,
1546                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 0, adc_vol_tlv),
1547         //SOC_DOUBLE_TLV("ADC Record Gain", RT5625_ADC_REC_GAIN,
1548         //      RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 0, adc_vol_tlv),
1549         /* This item does'nt affect path connected; only for clock choosen */
1550         SOC_ENUM_EXT("ADCR fun select Control", adcr_fun_sel_enum,
1551                 snd_soc_get_enum_double, rt5625_adcr_fun_sel_put),
1552         SOC_ENUM_EXT("ADCL fun select Control", adcl_fun_sel_enum,
1553                 snd_soc_get_enum_double, rt5625_adcl_fun_sel_put),
1554
1555         /* Voice DSP */
1556         SOC_ENUM_EXT("VoDSP AEC", rt5625_aec_fun_enum,
1557                 rt5625_aec_get, rt5625_aec_put),
1558         SOC_ENUM("VoDSP LRCK Control",  rt5625_dsp_lrck_enum),
1559         SOC_ENUM("VoDSP BP Pin Control",  rt5625_bp_ctrl_enum),
1560         SOC_ENUM("VoDSP Power Down Pin Control",  rt5625_pd_ctrl_enum),
1561         SOC_ENUM("VoDSP Reset Pin Control",  rt5625_rst_ctrl_enum),
1562
1563 #ifdef RT5625_F_SMT_PHO
1564         SOC_ENUM("GPIO2 mode", rt5625_gpio2_enum[0]),
1565         SOC_ENUM("GPIO2 configuration", rt5625_gpio2_enum[1]),
1566         SOC_ENUM("GPIO2 level", rt5625_gpio2_enum[2]),
1567
1568         SOC_SINGLE_EXT("VoDSP Dump", 0, 0, 1, 0, rt5625_dump_dsp_get, NULL),
1569         SOC_SINGLE_EXT("DAC Switch", 0, 0, 1, 0, rt5625_dac_active_get, rt5625_dac_active_put),
1570         SOC_SINGLE_EXT("ADC Switch", 0, 0, 1, 0, rt5625_adc_active_get, rt5625_adc_active_put),
1571         SOC_ENUM_EXT("PLL Switch", rt5625_pll_sel_enum, rt5625_pll_get, rt5625_pll_put),
1572         SOC_ENUM_EXT("PLL2 Switch", rt5625_pll2_sel_enum, rt5625_pll2_get, rt5625_pll2_put),
1573         SOC_SINGLE_EXT("VoIP Check", 0, 0, 1, 0, rt5625_voip_get, rt5625_voip_chk_put),
1574         SOC_SINGLE_EXT("VoIP Switch", 0, 0, 1, 0, rt5625_voip_get, rt5625_voip_put),
1575         SOC_SINGLE_EXT("Capture VoIP Check", 0, 0, 1, 0, rt5625_voip_get, rt5625_cap_voip_chk_put),
1576         SOC_SINGLE_EXT("Capture VoIP Switch", 0, 0, 1, 0, rt5625_voip_get, rt5625_cap_voip_put),
1577         SOC_SINGLE_EXT("Headset VoIP Check", 0, 0, 1, 0, rt5625_voip_get, rt5625_hs_voip_chk_put),
1578         SOC_SINGLE_EXT("Headset VoIP Switch", 0, 0, 1, 0, rt5625_voip_get, rt5625_hs_voip_put),
1579         SOC_SINGLE_EXT("Playback Switch", 0, 0, 1, 0, rt5625_play_get, rt5625_play_put),
1580         SOC_SINGLE_EXT("Record Switch", 0, 0, 1, 0, rt5625_rec_get, rt5625_rec_put),
1581         SOC_SINGLE_EXT("BT Switch", 0, 0, 1, 0, rt5625_bt_get, rt5625_bt_put),
1582         SOC_SINGLE_EXT("3G Switch", 0, 0, 1, 0, rt5625_3g_get, rt5625_3g_put),
1583         SOC_SINGLE_EXT("Headset 3G Switch", 0, 0, 1, 0, rt5625_3g_get, rt5625_hs_3g_put),
1584         SOC_SINGLE_EXT("APP disp", 0, 0, 1, 0, rt5625_app_get, NULL),
1585 #endif
1586         //SOC_DOUBLE_TLV("SPKOUT Playback Volume", RT5625_SPK_OUT_VOL,
1587         //      RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1588         //SOC_DOUBLE("SPKOUT Playback Switch", RT5625_SPK_OUT_VOL,
1589         //              RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1590         SOC_DOUBLE_TLV("HPOUT Playback Volume", RT5625_HP_OUT_VOL,
1591                 RT5625_L_VOL_SFT, RT5625_R_VOL_SFT, 31, 1, out_vol_tlv),
1592         SOC_DOUBLE("HPOUT Playback Switch", RT5625_HP_OUT_VOL,
1593                         RT5625_L_MUTE_SFT, RT5625_R_MUTE_SFT, 1, 1),
1594         SOC_ENUM("AUXOUT mode switch", rt5625_differential_enum[0]),
1595         SOC_ENUM("Phone Differential Input Control", rt5625_differential_enum[1]),
1596         SOC_ENUM("MIC1 Differential Input Control", rt5625_differential_enum[2]),
1597         SOC_ENUM("MIC2 Differential Input Control", rt5625_differential_enum[3]),
1598
1599 #ifdef RT5625_REG_RW
1600         {
1601                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1602                 .name = "Register Control",
1603                 .info = rt5625_regctl_info,
1604                 .get = rt5625_regctl_get,
1605                 .put = rt5625_regctl_put,
1606         },
1607 #endif
1608 };
1609
1610
1611  /*Left ADC Rec mixer*/
1612 static const struct snd_kcontrol_new rt5625_adcl_rec_mixer[] = {
1613         SOC_DAPM_SINGLE("Mic1 Capture Switch", RT5625_ADC_REC_MIXER,
1614                                 RT5625_M_RM_L_MIC1_SFT, 1, 1),
1615         SOC_DAPM_SINGLE("Mic2 Capture Switch", RT5625_ADC_REC_MIXER,
1616                                 RT5625_M_RM_L_MIC2_SFT, 1, 1),
1617         SOC_DAPM_SINGLE("LineIn Capture Switch", RT5625_ADC_REC_MIXER,
1618                                 RT5625_M_RM_L_LINE_SFT, 1, 1),
1619         SOC_DAPM_SINGLE("Phone Capture Switch", RT5625_ADC_REC_MIXER,
1620                                 RT5625_M_RM_L_PHO_SFT, 1, 1),
1621         SOC_DAPM_SINGLE("HP Mixer Capture Switch", RT5625_ADC_REC_MIXER,
1622                                 RT5625_M_RM_L_HM_SFT, 1, 1),
1623         SOC_DAPM_SINGLE("SPK Mixer Capture Switch", RT5625_ADC_REC_MIXER,
1624                                 RT5625_M_RM_L_SM_SFT, 1, 1),
1625         SOC_DAPM_SINGLE("MoNo Mixer Capture Switch", RT5625_ADC_REC_MIXER,
1626                                 RT5625_M_RM_L_MM_SFT, 1, 1),
1627 };
1628
1629 /*Right ADC Rec mixer*/
1630 static const struct snd_kcontrol_new rt5625_adcr_rec_mixer[] = {
1631         SOC_DAPM_SINGLE("Mic1 Capture Switch", RT5625_ADC_REC_MIXER,
1632                                 RT5625_M_RM_R_MIC1_SFT, 1, 1),
1633         SOC_DAPM_SINGLE("Mic2 Capture Switch", RT5625_ADC_REC_MIXER,
1634                                 RT5625_M_RM_R_MIC2_SFT, 1, 1),
1635         SOC_DAPM_SINGLE("LineIn Capture Switch", RT5625_ADC_REC_MIXER,
1636                                 RT5625_M_RM_R_LINE_SFT, 1, 1),
1637         SOC_DAPM_SINGLE("Phone Capture Switch", RT5625_ADC_REC_MIXER,
1638                                 RT5625_M_RM_R_PHO_SFT, 1, 1),
1639         SOC_DAPM_SINGLE("HP Mixer Capture Switch", RT5625_ADC_REC_MIXER,
1640                                 RT5625_M_RM_R_HM_SFT, 1, 1),
1641         SOC_DAPM_SINGLE("SPK Mixer Capture Switch", RT5625_ADC_REC_MIXER,
1642                                 RT5625_M_RM_R_SM_SFT, 1, 1),
1643         SOC_DAPM_SINGLE("MoNo Mixer Capture Switch", RT5625_ADC_REC_MIXER,
1644                                 RT5625_M_RM_R_MM_SFT, 1, 1),
1645 };
1646
1647 /* HP Mixer for mono input */
1648 static const struct snd_kcontrol_new rt5625_hp_mixer[] = {
1649         SOC_DAPM_SINGLE("LineIn Playback Switch", RT5625_LINE_IN_VOL,
1650                                 RT5625_M_LI_HM_SFT, 1, 1),
1651         SOC_DAPM_SINGLE("Phone Playback Switch", RT5625_PHONEIN_VOL,
1652                                 RT5625_M_PHO_HM_SFT, 1, 1),
1653         SOC_DAPM_SINGLE("Mic1 Playback Switch", RT5625_DAC_MIC_CTRL,
1654                                 RT5625_M_MIC1_HM_SFT, 1, 1),
1655         SOC_DAPM_SINGLE("Mic2 Playback Switch", RT5625_DAC_MIC_CTRL,
1656                                 RT5625_M_MIC2_HM_SFT, 1, 1),
1657         SOC_DAPM_SINGLE("Voice DAC Playback Switch", RT5625_VDAC_OUT_VOL,
1658                                 RT5625_M_VDAC_HM_SFT, 1, 1),
1659 };
1660
1661 /* Left HP Mixer */
1662 static const struct snd_kcontrol_new rt5625_hpl_mixer[] = {
1663         SOC_DAPM_SINGLE("ADC Playback Switch", RT5625_ADC_REC_GAIN,
1664                                 RT5625_M_ADCL_HM_SFT, 1, 1),
1665         SOC_DAPM_SINGLE("DAC Playback Switch", RT5625_DAC_MIC_CTRL,
1666                                 RT5625_M_DACL_HM_SFT, 1, 1),
1667 };
1668
1669 /* Right HP Mixer */
1670 static const struct snd_kcontrol_new rt5625_hpr_mixer[] = {
1671         SOC_DAPM_SINGLE("ADC Playback Switch", RT5625_ADC_REC_GAIN,
1672                                 RT5625_M_ADCR_HM_SFT, 1, 1),
1673         SOC_DAPM_SINGLE("DAC Playback Switch", RT5625_DAC_MIC_CTRL,
1674                                 RT5625_M_DACR_HM_SFT, 1, 1),
1675 };
1676
1677 /* Mono Mixer */
1678 static const struct snd_kcontrol_new rt5625_mono_mixer[] = {
1679         SOC_DAPM_SINGLE("ADCL Playback Switch", RT5625_ADC_REC_GAIN,
1680                                 RT5625_M_ADCL_MM_SFT, 1, 1),
1681         SOC_DAPM_SINGLE("ADCR Playback Switch", RT5625_ADC_REC_GAIN,
1682                                 RT5625_M_ADCR_MM_SFT, 1, 1),
1683         SOC_DAPM_SINGLE("Line Mixer Playback Switch", RT5625_LINE_IN_VOL,
1684                                 RT5625_M_LI_MM_SFT, 1, 1),
1685         SOC_DAPM_SINGLE("Mic1 Playback Switch", RT5625_DAC_MIC_CTRL,
1686                                 RT5625_M_MIC1_MM_SFT, 1, 1),
1687         SOC_DAPM_SINGLE("Mic2 Playback Switch", RT5625_DAC_MIC_CTRL,
1688                                 RT5625_M_MIC2_MM_SFT, 1, 1),
1689         SOC_DAPM_SINGLE("DAC Mixer Playback Switch", RT5625_DAC_MIC_CTRL,
1690                                 RT5625_M_DAC_MM_SFT, 1, 1),
1691         SOC_DAPM_SINGLE("Voice DAC Playback Switch", RT5625_VDAC_OUT_VOL,
1692                                 RT5625_M_VDAC_MM_SFT, 1, 1),
1693 };
1694
1695 /* Speaker Mixer */
1696 static const struct snd_kcontrol_new rt5625_spk_mixer[] = {
1697         SOC_DAPM_SINGLE("Line Mixer Playback Switch", RT5625_LINE_IN_VOL,
1698                                 RT5625_M_LI_SM_SFT, 1, 1),
1699         SOC_DAPM_SINGLE("Phone Playback Switch", RT5625_PHONEIN_VOL,
1700                                 RT5625_M_PHO_SM_SFT, 1, 1),
1701         SOC_DAPM_SINGLE("Mic1 Playback Switch", RT5625_DAC_MIC_CTRL,
1702                                 RT5625_M_MIC1_SM_SFT, 1, 1),
1703         SOC_DAPM_SINGLE("Mic2 Playback Switch", RT5625_DAC_MIC_CTRL,
1704                                 RT5625_M_MIC2_SM_SFT, 1, 1),
1705         SOC_DAPM_SINGLE("DAC Mixer Playback Switch", RT5625_DAC_MIC_CTRL,
1706                                 RT5625_M_DAC_SM_SFT, 1, 1),
1707         SOC_DAPM_SINGLE("Voice DAC Playback Switch", RT5625_VDAC_OUT_VOL,
1708                                 RT5625_M_VDAC_SM_SFT, 1, 1),
1709 };
1710
1711 static int rt5625_dac_func_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *k, int event)
1712 {
1713         struct snd_soc_codec *codec = w->codec;
1714
1715         switch (event) {
1716         case SND_SOC_DAPM_PRE_REG:
1717                 snd_soc_update_bits(codec, RT5625_PD_CTRL,
1718                         RT5625_PWR_PR1, RT5625_PWR_PR1);
1719                 break;
1720
1721         case SND_SOC_DAPM_POST_REG:
1722                 snd_soc_update_bits(codec, RT5625_PD_CTRL,
1723                         RT5625_PWR_PR1, 0);
1724                 break;
1725
1726         default:
1727                 return 0;
1728         }       
1729
1730         return 0;
1731 }
1732
1733 static int rt5625_hpmix_event(struct snd_soc_dapm_widget *w,
1734                 struct snd_kcontrol *kcontrol, int event)
1735 {
1736         struct snd_soc_codec *codec = w->codec;
1737
1738         switch (event) {
1739         case SND_SOC_DAPM_PRE_PMD:
1740                 snd_soc_update_bits(codec, RT5625_PWR_ADD2,
1741                         RT5625_P_HM_L | RT5625_P_HM_R, 0);
1742                 break;
1743
1744         case SND_SOC_DAPM_POST_PMU:
1745                 snd_soc_update_bits(codec, RT5625_PWR_ADD2,
1746                         RT5625_P_HM_L | RT5625_P_HM_R,
1747                         RT5625_P_HM_L | RT5625_P_HM_R);
1748                 break;
1749
1750         default:
1751                 return 0;
1752         }
1753
1754         return 0;
1755 }
1756
1757 static int rt5625_vodsp_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *k, int event)
1758 {
1759         struct snd_soc_codec *codec = w->codec;
1760         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
1761
1762         switch (event) {
1763         case SND_SOC_DAPM_POST_PMD:
1764                 //pr_info("%s(): PMD\n", __func__);
1765                 snd_soc_update_bits(codec, RT5625_VODSP_CTL,
1766                         RT5625_DSP_PD_MASK, RT5625_DSP_PD_EN);
1767                 snd_soc_update_bits(codec, RT5625_PWR_ADD3,
1768                         RT5625_P_DSP_IF | RT5625_P_DSP_I2C, 0);
1769                 snd_soc_update_bits(codec, RT5625_LDO_CTRL,
1770                         RT5625_LDO_MASK, RT5625_LDO_DIS);
1771                 break;
1772
1773         case SND_SOC_DAPM_POST_PMU:
1774                 //pr_info("%s(): PMU\n", __func__);
1775                 if(rt5625->vodsp_fun == RT5625_AEC_EN)
1776                         rt5625_init_vodsp_aec(codec);
1777                 //pr_info("[DSP poweron] 0x%04x: 0x%04x\n", 0x230C, rt5625_dsp_read(codec, 0x230C));
1778                 break;
1779
1780         default:
1781                 return 0;
1782         }       
1783
1784         return 0;
1785 }
1786
1787
1788 static void hp_depop_mode2(struct snd_soc_codec *codec)
1789 {
1790         snd_soc_update_bits(codec, RT5625_PWR_ADD1,
1791                 RT5625_P_SG_EN, RT5625_P_SG_EN);
1792         snd_soc_update_bits(codec, RT5625_PWR_ADD3,
1793                 RT5625_P_HPL_VOL | RT5625_P_HPR_VOL,
1794                 RT5625_P_HPL_VOL | RT5625_P_HPR_VOL);
1795         snd_soc_write(codec, RT5625_MISC_CTRL, RT5625_HP_DEPOP_M2);
1796         schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1797         snd_soc_update_bits(codec, RT5625_PWR_ADD1,
1798                 RT5625_P_HPO_AMP | RT5625_P_HPO_ENH,
1799                 RT5625_P_HPO_AMP | RT5625_P_HPO_ENH);
1800 }
1801
1802 /* enable depop function for mute/unmute */
1803 static void hp_mute_unmute_depop(struct snd_soc_codec *codec,int mute)
1804 {
1805         if(mute) {
1806                 snd_soc_update_bits(codec, RT5625_PWR_ADD1,
1807                         RT5625_P_SG_EN, RT5625_P_SG_EN);
1808                 snd_soc_write(codec, RT5625_MISC_CTRL, RT5625_MUM_DEPOP |
1809                         RT5625_HPR_MUM_DEPOP | RT5625_HPL_MUM_DEPOP);
1810                 snd_soc_update_bits(codec, RT5625_HP_OUT_VOL,
1811                         RT5625_L_MUTE | RT5625_R_MUTE,
1812                         RT5625_L_MUTE | RT5625_R_MUTE);
1813                 mdelay(50);
1814                 snd_soc_update_bits(codec, RT5625_PWR_ADD1, RT5625_P_SG_EN, 0);
1815         } else {
1816                 snd_soc_update_bits(codec, RT5625_PWR_ADD1,
1817                         RT5625_P_SG_EN, RT5625_P_SG_EN);
1818                 snd_soc_write(codec, RT5625_MISC_CTRL, RT5625_MUM_DEPOP |
1819                         RT5625_HPR_MUM_DEPOP | RT5625_HPL_MUM_DEPOP);
1820                 snd_soc_update_bits(codec,RT5625_HP_OUT_VOL,
1821                         RT5625_L_MUTE | RT5625_R_MUTE, 0);
1822                 mdelay(50);
1823         }
1824 }
1825
1826 static int rt5625_hp_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *k, int event)
1827 {
1828         struct snd_soc_codec *codec = w->codec;
1829
1830         switch (event) {
1831         case SND_SOC_DAPM_PRE_PMD:
1832                 hp_mute_unmute_depop(codec,1);
1833                 snd_soc_update_bits(codec, RT5625_PWR_ADD1,
1834                         RT5625_P_HPO_AMP | RT5625_P_HPO_ENH, 0);
1835                 snd_soc_update_bits(codec, RT5625_PWR_ADD3,
1836                         RT5625_P_HPL_VOL | RT5625_P_HPR_VOL, 0);
1837                 break;
1838
1839         case SND_SOC_DAPM_POST_PMU:
1840                 hp_depop_mode2(codec);
1841                 hp_mute_unmute_depop(codec,0);
1842                 break;
1843
1844         default:
1845                 return 0;
1846         }       
1847
1848         return 0;
1849 }
1850
1851 /* DAC function select MUX */
1852 static const char *dac_fun_sel[] = {
1853         "Stereo DAC", "SRC2 Out", "TxDP", "TxDC"};
1854
1855 static const struct soc_enum dac_fun_sel_enum =
1856         SOC_ENUM_SINGLE(RT5625_F_DAC_ADC_VDAC, RT5625_DAC_F_SFT,
1857                                 ARRAY_SIZE(dac_fun_sel), dac_fun_sel);
1858
1859 static const struct snd_kcontrol_new dac_fun_sel_mux =
1860         SOC_DAPM_ENUM("DAC Function Select Mux", dac_fun_sel_enum);
1861
1862 /* Voice DAC source select MUX */
1863 static const char *vdac_src_sel[] = {
1864         "Voice PCM", "SRC2 Out", "TxDP", "TxDC"};
1865
1866 static const struct soc_enum vdac_src_sel_enum =
1867         SOC_ENUM_SINGLE(RT5625_F_DAC_ADC_VDAC, RT5625_VDAC_S_SFT,
1868                                 ARRAY_SIZE(vdac_src_sel), vdac_src_sel);
1869
1870 static const struct snd_kcontrol_new vdac_src_sel_mux =
1871         SOC_DAPM_ENUM("Voice DAC Source Mux", vdac_src_sel_enum);
1872
1873 /* SRC1 power switch */
1874 static const struct snd_kcontrol_new src1_pwr_sw_control =
1875         SOC_DAPM_SINGLE("Switch", RT5625_VODSP_PDM_CTL,
1876                                 RT5625_SRC1_PWR_SFT, 1, 0);
1877
1878 /* SRC2 power switch */
1879 static const struct snd_kcontrol_new src2_pwr_sw_control =
1880         SOC_DAPM_SINGLE("Switch", RT5625_VODSP_PDM_CTL,
1881                                 RT5625_SRC2_PWR_SFT, 1, 0);
1882
1883 /* SRC2 source select MUX */
1884 static const char *src2_src_sel[] = {"TxDP", "TxDC"};
1885
1886 static const struct soc_enum src2_src_sel_enum =
1887         SOC_ENUM_SINGLE(RT5625_VODSP_PDM_CTL, RT5625_SRC2_S_SFT,
1888                         ARRAY_SIZE(src2_src_sel), src2_src_sel);
1889
1890 static const struct snd_kcontrol_new src2_src_sel_mux =
1891         SOC_DAPM_ENUM("SRC2 Source Mux", src2_src_sel_enum);
1892
1893 /* VoDSP RxDP power switch */
1894 static const struct snd_kcontrol_new rxdp_pwr_sw_control =
1895         SOC_DAPM_SINGLE("Switch", RT5625_VODSP_PDM_CTL,
1896                                 RT5625_RXDP_PWR_SFT, 1, 0);
1897 /* VoDSP RxDC power switch */
1898 static const struct snd_kcontrol_new rxdc_pwr_sw_control =
1899         SOC_DAPM_SINGLE("Switch", RT5625_VODSP_PDM_CTL,
1900                                 RT5625_RXDC_PWR_SFT, 1, 0);
1901
1902 /* VoDSP RxDP source select MUX */
1903 static const char *rxdp_src_sel[] = {"SRC1 Output", "ADCL to VoDSP",
1904                         "Voice to Stereo", "ADCR to VoDSP"};
1905
1906 static const struct soc_enum rxdp_src_sel_enum =
1907         SOC_ENUM_SINGLE(RT5625_VODSP_PDM_CTL, RT5625_RXDP_S_SFT,
1908                         ARRAY_SIZE(rxdp_src_sel), rxdp_src_sel);
1909
1910 static const struct snd_kcontrol_new rxdp_src_sel_mux =
1911         SOC_DAPM_ENUM("RxDP Source Mux", rxdp_src_sel_enum);
1912
1913 /* PCM source select MUX */
1914 static const char *pcm_src_sel[] = {"ADCR", "TxDP"};
1915
1916 static const struct soc_enum pcm_src_sel_enum =
1917         SOC_ENUM_SINGLE(RT5625_VODSP_PDM_CTL, RT5625_PCM_S_SFT,
1918                         ARRAY_SIZE(pcm_src_sel), pcm_src_sel);
1919
1920 static const struct snd_kcontrol_new pcm_src_sel_mux =
1921         SOC_DAPM_ENUM("PCM Source Mux", pcm_src_sel_enum);
1922
1923 /* Main stereo record I2S source select MUX */
1924 static const char *rec_iis_src_sel[] = {"ADC", "Voice to Stereo", "SRC2 Output"};
1925
1926 static const struct soc_enum rec_iis_src_enum =
1927         SOC_ENUM_SINGLE(RT5625_VODSP_PDM_CTL, RT5625_REC_IIS_S_SFT,
1928                         ARRAY_SIZE(rec_iis_src_sel), rec_iis_src_sel);
1929
1930 static const struct snd_kcontrol_new rec_iis_src_mux =
1931         SOC_DAPM_ENUM("REC I2S Source Mux", rec_iis_src_enum);
1932
1933 /* SPK volume input select MUX */
1934 static const char *spkvol_input_sel[] = {"VMID", "HP Mixer", "SPK Mixer", "Mono Mixer"};
1935
1936 static const struct soc_enum spkvol_input_enum =
1937         SOC_ENUM_SINGLE(RT5625_OUTMIX_CTRL, RT5625_SPKVOL_S_SFT,
1938                         ARRAY_SIZE(spkvol_input_sel), spkvol_input_sel);
1939
1940 static const struct snd_kcontrol_new spkvol_input_mux =
1941         SOC_DAPM_ENUM("SPK Vol Input Mux", spkvol_input_enum);
1942
1943 /* HP volume input select MUX */
1944 static const char *hpvol_input_sel[] = {"VMID", "HP Mixer"};
1945
1946 static const struct soc_enum hplvol_input_enum =
1947         SOC_ENUM_SINGLE(RT5625_OUTMIX_CTRL, RT5625_HPVOL_L_S_SFT,
1948                         ARRAY_SIZE(hpvol_input_sel), hpvol_input_sel);
1949
1950 static const struct snd_kcontrol_new hplvol_input_mux =
1951         SOC_DAPM_ENUM("HPL Vol Input Mux", hplvol_input_enum);
1952
1953 static const struct soc_enum hprvol_input_enum =
1954         SOC_ENUM_SINGLE(RT5625_OUTMIX_CTRL, RT5625_HPVOL_R_S_SFT,
1955                         ARRAY_SIZE(hpvol_input_sel), hpvol_input_sel);
1956
1957 static const struct snd_kcontrol_new hprvol_input_mux =
1958         SOC_DAPM_ENUM("HPR Vol Input Mux", hprvol_input_enum);
1959
1960 /* AUX volume input select MUX */
1961 static const struct soc_enum auxvol_input_enum =
1962         SOC_ENUM_SINGLE(RT5625_OUTMIX_CTRL, RT5625_AUXVOL_S_SFT,
1963                         ARRAY_SIZE(spkvol_input_sel), spkvol_input_sel);
1964
1965 static const struct snd_kcontrol_new auxvol_input_mux =
1966         SOC_DAPM_ENUM("AUX Vol Input Mux", auxvol_input_enum);
1967
1968 static const struct snd_soc_dapm_widget rt5625_dapm_widgets[] = {
1969         /* supply */
1970         SND_SOC_DAPM_SUPPLY("IIS Interface", RT5625_PWR_ADD1,
1971                                 RT5625_P_I2S_BIT, 0, NULL, 0),
1972         SND_SOC_DAPM_SUPPLY("PLL1", RT5625_PWR_ADD2,
1973                                 RT5625_P_PLL1_BIT, 0, NULL, 0),
1974         SND_SOC_DAPM_SUPPLY("PLL2", RT5625_PWR_ADD2,
1975                                 RT5625_P_PLL2_BIT, 0, NULL, 0),
1976         SND_SOC_DAPM_VMID("VMID"),
1977         SND_SOC_DAPM_SUPPLY("DAC Ref", RT5625_PWR_ADD1,
1978                                 RT5625_P_DAC_REF_BIT, 0, NULL, 0),
1979         /* microphone bias */
1980         SND_SOC_DAPM_MICBIAS("Mic1 Bias", RT5625_PWR_ADD1,
1981                                         RT5625_P_MB1_BIT, 0),
1982         SND_SOC_DAPM_MICBIAS("Mic2 Bias", RT5625_PWR_ADD1,
1983                                         RT5625_P_MB2_BIT, 0),
1984
1985         /* Input */
1986         SND_SOC_DAPM_INPUT("Left LineIn"),
1987         SND_SOC_DAPM_INPUT("Right LineIn"),
1988         SND_SOC_DAPM_INPUT("Phone"),
1989         SND_SOC_DAPM_INPUT("Mic1"),
1990         SND_SOC_DAPM_INPUT("Mic2"),
1991
1992         SND_SOC_DAPM_PGA("Mic1 Boost", RT5625_PWR_ADD3,
1993                         RT5625_P_MIC1_BST_BIT, 0, NULL, 0),
1994         SND_SOC_DAPM_PGA("Mic2 Boost", RT5625_PWR_ADD3,
1995                         RT5625_P_MIC2_BST_BIT, 0, NULL, 0),
1996         SND_SOC_DAPM_PGA("Phone Rec Mixer", RT5625_PWR_ADD3,
1997                         RT5625_P_PH_ADMIX_BIT, 0, NULL, 0),
1998
1999         SND_SOC_DAPM_MIXER("Left Rec Mixer", RT5625_PWR_ADD2,
2000                 RT5625_P_ADCL_RM_BIT, 0, rt5625_adcl_rec_mixer,
2001                 ARRAY_SIZE(rt5625_adcl_rec_mixer)),
2002         SND_SOC_DAPM_MIXER("Right Rec Mixer", RT5625_PWR_ADD2,
2003                 RT5625_P_ADCR_RM_BIT, 0, rt5625_adcr_rec_mixer,
2004                 ARRAY_SIZE(rt5625_adcr_rec_mixer)),
2005
2006         SND_SOC_DAPM_ADC("Left ADC", NULL, RT5625_PWR_ADD2,
2007                                         RT5625_P_ADCL_BIT, 0),
2008         SND_SOC_DAPM_ADC("Right ADC", NULL, RT5625_PWR_ADD2,
2009                                         RT5625_P_ADCR_BIT, 0),
2010         SND_SOC_DAPM_PGA("ADC", SND_SOC_NOPM, 0, 0, NULL, 0),//johnny-3-9
2011         SND_SOC_DAPM_MUX("PCM src select Mux", SND_SOC_NOPM, 0, 0,
2012                                 &pcm_src_sel_mux),
2013         SND_SOC_DAPM_MUX("IIS src select Mux", SND_SOC_NOPM, 0, 0,
2014                                 &rec_iis_src_mux),
2015
2016         /* Input Stream Audio Interface */
2017         SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
2018         SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Voice Capture", 0, SND_SOC_NOPM, 0, 0),
2019
2020         /* Voice DSP */
2021         SND_SOC_DAPM_MUX("SRC2 src select Mux", SND_SOC_NOPM, 0, 0,
2022                                 &src2_src_sel_mux),
2023         SND_SOC_DAPM_SWITCH("SRC1 Enable", SND_SOC_NOPM, 0, 0,
2024                                 &src1_pwr_sw_control),
2025         SND_SOC_DAPM_SWITCH("SRC2 Enable", SND_SOC_NOPM, 0, 0,
2026                                 &src2_pwr_sw_control),
2027         SND_SOC_DAPM_SWITCH("RxDP Enable", SND_SOC_NOPM, 0, 0,
2028                                 &rxdp_pwr_sw_control),
2029         SND_SOC_DAPM_SWITCH("RxDC Enable", SND_SOC_NOPM, 0, 0,
2030                                 &rxdc_pwr_sw_control),
2031
2032         SND_SOC_DAPM_MUX("RxDP src select Mux", SND_SOC_NOPM, 0, 0,
2033                                 &rxdp_src_sel_mux),
2034
2035         SND_SOC_DAPM_PGA("TxDP", SND_SOC_NOPM, 0, 0, NULL, 0),
2036         SND_SOC_DAPM_PGA("TxDC", SND_SOC_NOPM, 0, 0, NULL, 0),
2037         SND_SOC_DAPM_PGA("PDM", SND_SOC_NOPM, 0, 0, NULL, 0),
2038         SND_SOC_DAPM_PGA("RxDP", SND_SOC_NOPM, 0, 0, NULL, 0),
2039         SND_SOC_DAPM_PGA("RxDC", SND_SOC_NOPM, 0, 0, NULL, 0),
2040         SND_SOC_DAPM_PGA_E("Voice DSP", SND_SOC_NOPM,
2041                 0, 0, NULL, 0, rt5625_vodsp_event,
2042                 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
2043
2044         /* Output */
2045         /* Output Stream Audio Interface */
2046         SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 HiFi Playback",
2047                                 0, SND_SOC_NOPM, 0, 0),
2048         SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Voice Playback",
2049                                 0, SND_SOC_NOPM, 0, 0),
2050
2051         /* DAC function select Mux */
2052         SND_SOC_DAPM_MUX_E("DAC fun Mux", SND_SOC_NOPM, 0, 0,
2053                 &dac_fun_sel_mux, rt5625_dac_func_event,
2054                 SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG),
2055         /* VDAC source select Mux */
2056         SND_SOC_DAPM_MUX_E("VDAC src Mux", SND_SOC_NOPM, 0, 0,
2057                 &vdac_src_sel_mux, rt5625_dac_func_event,
2058                 SND_SOC_DAPM_PRE_REG | SND_SOC_DAPM_POST_REG),
2059
2060         SND_SOC_DAPM_DAC("Left DAC", NULL, RT5625_PWR_ADD2,
2061                                         RT5625_P_DACL_BIT, 0),
2062         SND_SOC_DAPM_DAC("Right DAC", NULL, RT5625_PWR_ADD2,
2063                                         RT5625_P_DACR_BIT, 0),
2064         SND_SOC_DAPM_DAC("Voice DAC", NULL, RT5625_PWR_ADD2,
2065                                         RT5625_P_VDAC_BIT, 0),
2066
2067         SND_SOC_DAPM_PGA("Mic1 Volume", RT5625_PWR_ADD3,
2068                                 RT5625_P_MIC1_VOL_BIT, 0, NULL, 0),
2069         SND_SOC_DAPM_PGA("Mic2 Volume", RT5625_PWR_ADD3,
2070                                 RT5625_P_MIC2_VOL_BIT, 0, NULL, 0),
2071         SND_SOC_DAPM_PGA("Left LineIn Volume", RT5625_PWR_ADD3,
2072                                 RT5625_P_LV_L_BIT, 0, NULL, 0),
2073         SND_SOC_DAPM_PGA("Right LineIn Volume", RT5625_PWR_ADD3,
2074                                 RT5625_P_LV_R_BIT, 0, NULL, 0),
2075         SND_SOC_DAPM_PGA("Phone Volume", RT5625_PWR_ADD3,
2076                                 RT5625_P_PH_VOL_BIT, 0, NULL, 0),
2077
2078         SND_SOC_DAPM_PGA("Left DAC To Mixer", RT5625_PWR_ADD1,
2079                                 RT5625_P_DACL_MIX_BIT, 0, NULL, 0),
2080         SND_SOC_DAPM_PGA("Right DAC To Mixer", RT5625_PWR_ADD1,
2081                                 RT5625_P_DACR_MIX_BIT, 0, NULL, 0),
2082         SND_SOC_DAPM_PGA("Voice DAC To Mixer", RT5625_PWR_ADD1,
2083                                 RT5625_P_VDAC_MIX_BIT, 0, NULL, 0),
2084
2085         SND_SOC_DAPM_MIXER("SPK Mixer", RT5625_PWR_ADD2,
2086                 RT5625_P_SM_BIT, 0, rt5625_spk_mixer,
2087                 ARRAY_SIZE(rt5625_spk_mixer)),  
2088         SND_SOC_DAPM_MIXER("Mono HP Mixer", SND_SOC_NOPM, 0, 0,
2089                 rt5625_hp_mixer, ARRAY_SIZE(rt5625_hp_mixer)),
2090         SND_SOC_DAPM_MIXER("Left HP Mixer", SND_SOC_NOPM, 0, 0,
2091                 rt5625_hpl_mixer, ARRAY_SIZE(rt5625_hpl_mixer)),
2092         SND_SOC_DAPM_MIXER("Right HP Mixer", SND_SOC_NOPM, 0, 0,
2093                 rt5625_hpr_mixer, ARRAY_SIZE(rt5625_hpr_mixer)),
2094         SND_SOC_DAPM_MIXER("Mono Mixer", RT5625_PWR_ADD2,
2095                 RT5625_P_MM_BIT, 0, rt5625_mono_mixer,
2096                 ARRAY_SIZE(rt5625_mono_mixer)),
2097
2098         SND_SOC_DAPM_MIXER_E("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0,
2099                 rt5625_hpmix_event, SND_SOC_DAPM_PRE_PMD |
2100                 SND_SOC_DAPM_POST_PMU),
2101
2102         SND_SOC_DAPM_MIXER("DAC Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
2103         SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
2104
2105         SND_SOC_DAPM_MUX("SPK Vol Input Mux", SND_SOC_NOPM,
2106                         0, 0, &spkvol_input_mux),
2107         SND_SOC_DAPM_SUPPLY("SPKL Vol", RT5625_PWR_ADD3,
2108                         RT5625_P_SPKL_VOL_BIT, 0, NULL, 0),
2109         SND_SOC_DAPM_SUPPLY("SPKR Vol", RT5625_PWR_ADD3,
2110                         RT5625_P_SPKR_VOL_BIT, 0, NULL, 0),
2111         SND_SOC_DAPM_MUX("HPL Vol Input Mux", RT5625_PWR_ADD3,
2112                         RT5625_P_HPL_VOL_BIT, 0, &hplvol_input_mux),
2113         SND_SOC_DAPM_MUX("HPR Vol Input Mux", RT5625_PWR_ADD3,
2114                         RT5625_P_HPR_VOL_BIT, 0, &hprvol_input_mux),
2115         SND_SOC_DAPM_MUX("AUX Vol Input Mux", RT5625_PWR_ADD3,
2116                         RT5625_P_AUX_VOL_BIT, 0, &auxvol_input_mux),
2117
2118         SND_SOC_DAPM_SUPPLY("SPK Amp", RT5625_PWR_ADD1,
2119                                 RT5625_P_SPK_AMP_BIT, 0, NULL, 0),
2120         SND_SOC_DAPM_PGA_E("HP Amp", SND_SOC_NOPM, 0, 0, NULL, 0,
2121                 rt5625_hp_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
2122                                 
2123         SND_SOC_DAPM_OUTPUT("SPKL"),
2124         SND_SOC_DAPM_OUTPUT("SPKR"),
2125         SND_SOC_DAPM_OUTPUT("HPL"),
2126         SND_SOC_DAPM_OUTPUT("HPR"),
2127         SND_SOC_DAPM_OUTPUT("AUX"),
2128 };
2129
2130 static const struct snd_soc_dapm_route rt5625_dapm_routes[] = {
2131         {"DAC Ref", NULL, "IIS Interface"},
2132         {"DAC Ref", NULL, "PLL1"},
2133         {"DAC Ref", NULL, "PLL2"},
2134
2135         /* Input */
2136         {"Phone Rec Mixer", NULL, "Phone"},
2137         {"Mic1 Boost", NULL, "Mic1"},
2138         {"Mic2 Boost", NULL, "Mic2"},
2139
2140         {"Left Rec Mixer", "LineIn Capture Switch", "Left LineIn"},
2141         {"Left Rec Mixer", "Phone Capture Switch", "Phone Rec Mixer"},
2142         {"Left Rec Mixer", "Mic1 Capture Switch", "Mic1 Boost"},
2143         {"Left Rec Mixer", "Mic2 Capture Switch", "Mic2 Boost"},
2144         {"Left Rec Mixer", "HP Mixer Capture Switch", "Left HP Mixer"},
2145         {"Left Rec Mixer", "SPK Mixer Capture Switch", "SPK Mixer"},
2146         {"Left Rec Mixer", "MoNo Mixer Capture Switch", "Mono Mixer"},
2147
2148         {"Right Rec Mixer", "LineIn Capture Switch", "Right LineIn"},
2149         {"Right Rec Mixer", "Phone Capture Switch", "Phone Rec Mixer"},
2150         {"Right Rec Mixer", "Mic1 Capture Switch", "Mic1 Boost"},
2151         {"Right Rec Mixer", "Mic2 Capture Switch", "Mic2 Boost"},
2152         {"Right Rec Mixer", "HP Mixer Capture Switch", "Right HP Mixer"},
2153         {"Right Rec Mixer", "SPK Mixer Capture Switch", "SPK Mixer"},
2154         {"Right Rec Mixer", "MoNo Mixer Capture Switch", "Mono Mixer"},
2155
2156         {"Left ADC", NULL, "DAC Ref"},
2157         {"Left ADC", NULL, "Left Rec Mixer"},
2158         {"Right ADC", NULL, "DAC Ref"},
2159         {"Right ADC", NULL, "Right Rec Mixer"},
2160
2161         {"PCM src select Mux", "TxDP", "TxDP"},
2162         {"PCM src select Mux", "ADCR", "Right ADC"},
2163
2164         //johnny-3-9 [s]
2165         {"ADC", NULL, "Left ADC"},
2166         {"ADC", NULL, "Right ADC"},
2167         {"IIS src select Mux", "ADC", "ADC"},
2168         //{"IIS src select Mux", "ADC", "Left ADC"},
2169         //{"IIS src select Mux", "ADC", "Right ADC"},
2170         //johnny-3-9 [e]
2171         {"IIS src select Mux", "Voice to Stereo", "AIF2RX"},
2172         {"IIS src select Mux", "SRC2 Output", "SRC2 Enable"},
2173
2174         {"AIF2TX", NULL, "IIS Interface"},
2175         {"AIF2TX", NULL, "PCM src select Mux"},
2176         {"AIF1TX", NULL, "IIS Interface"},
2177         {"AIF1TX", NULL, "IIS src select Mux"},
2178
2179         /* Output */
2180         {"AIF1RX", NULL, "IIS Interface"},
2181         {"AIF2RX", NULL, "IIS Interface"},
2182
2183         {"DAC fun Mux", "SRC2 Out", "SRC2 Enable"},
2184         {"DAC fun Mux", "TxDP", "TxDP"},
2185         {"DAC fun Mux", "TxDC", "TxDC"},
2186         {"DAC fun Mux", "Stereo DAC", "AIF1RX"},
2187
2188         {"VDAC src Mux", "SRC2 Out", "SRC2 Enable"},
2189         {"VDAC src Mux", "TxDP", "TxDP"},
2190         {"VDAC src Mux", "TxDC", "TxDC"},
2191         {"VDAC src Mux", "Voice PCM", "AIF2RX"},
2192
2193         {"Left DAC", NULL, "DAC Ref"},
2194         {"Left DAC", NULL, "DAC fun Mux"},
2195         {"Right DAC", NULL, "DAC Ref"},
2196         {"Right DAC", NULL, "DAC fun Mux"},
2197         {"Voice DAC", NULL, "DAC Ref"},
2198         {"Voice DAC", NULL, "VDAC src Mux"},
2199
2200         {"Left LineIn Volume", NULL, "Left LineIn"},
2201         {"Right LineIn Volume", NULL, "Right LineIn"},
2202         {"Phone Volume", NULL, "Phone"},
2203         {"Mic1 Volume", NULL, "Mic1 Boost"},
2204         {"Mic2 Volume", NULL, "Mic2 Boost"},
2205
2206         {"Left DAC To Mixer", NULL, "Left DAC"},
2207         {"Right DAC To Mixer", NULL, "Right DAC"},
2208         {"Voice DAC To Mixer", NULL, "Voice DAC"},
2209
2210         {"DAC Mixer", NULL, "Left DAC To Mixer"},
2211         {"DAC Mixer", NULL, "Right DAC To Mixer"},
2212         {"Line Mixer", NULL, "Left LineIn Volume"},
2213         {"Line Mixer", NULL, "Right LineIn Volume"},
2214
2215         {"Mono HP Mixer", "LineIn Playback Switch", "Line Mixer"},
2216         {"Mono HP Mixer", "Phone Playback Switch", "Phone Volume"},
2217         {"Mono HP Mixer", "Mic1 Playback Switch", "Mic1 Volume"},
2218         {"Mono HP Mixer", "Mic2 Playback Switch", "Mic2 Volume"},
2219         {"Mono HP Mixer", "Voice DAC Playback Switch", "Voice DAC To Mixer"},
2220         {"Left HP Mixer", "ADC Playback Switch", "Left Rec Mixer"},
2221         {"Left HP Mixer", "DAC Playback Switch", "Left DAC To Mixer"},
2222         {"Right HP Mixer", "ADC Playback Switch", "Right Rec Mixer"},
2223         {"Right HP Mixer", "DAC Playback Switch", "Right DAC To Mixer"},
2224
2225         {"SPK Mixer", "Line Mixer Playback Switch", "Line Mixer"},
2226         {"SPK Mixer", "Phone Playback Switch", "Phone Volume"},
2227         {"SPK Mixer", "Mic1 Playback Switch", "Mic1 Volume"},
2228         {"SPK Mixer", "Mic2 Playback Switch", "Mic2 Volume"},
2229         {"SPK Mixer", "DAC Mixer Playback Switch", "DAC Mixer"},
2230         {"SPK Mixer", "Voice DAC Playback Switch", "Voice DAC To Mixer"},
2231
2232         {"Mono Mixer", "Line Mixer Playback Switch", "Line Mixer"},
2233         {"Mono Mixer", "ADCL Playback Switch","Left Rec Mixer"},
2234         {"Mono Mixer", "ADCR Playback Switch","Right Rec Mixer"},
2235         {"Mono Mixer", "Mic1 Playback Switch", "Mic1 Volume"},
2236         {"Mono Mixer", "Mic2 Playback Switch", "Mic2 Volume"},
2237         {"Mono Mixer", "DAC Mixer Playback Switch", "DAC Mixer"},
2238         {"Mono Mixer", "Voice DAC Playback Switch", "Voice DAC To Mixer"},
2239
2240         {"HP Mixer", NULL, "Mono HP Mixer"},
2241         {"HP Mixer", NULL, "Left HP Mixer"},
2242         {"HP Mixer", NULL, "Right HP Mixer"},
2243
2244         {"SPK Vol Input Mux", "VMID", "VMID"},
2245         {"SPK Vol Input Mux", "HP Mixer", "HP Mixer"},
2246         {"SPK Vol Input Mux", "SPK Mixer", "SPK Mixer"},
2247         {"SPK Vol Input Mux", "Mono Mixer", "Mono Mixer"},
2248         {"SPK Vol Input Mux", NULL, "SPKL Vol"},
2249         {"SPK Vol Input Mux", NULL, "SPKR Vol"},
2250         
2251         {"HPL Vol Input Mux", "HP Mixer", "HP Mixer"},
2252         {"HPL Vol Input Mux", "VMID", "VMID"},
2253         {"HP Amp", NULL, "HPL Vol Input Mux"},
2254         {"HPR Vol Input Mux", "HP Mixer", "HP Mixer"},
2255         {"HPR Vol Input Mux", "VMID", "VMID"},
2256         {"HP Amp", NULL, "HPR Vol Input Mux"},
2257
2258         {"AUX Vol Input Mux", "VMID", "VMID"},
2259         {"AUX Vol Input Mux", "HP Mixer", "HP Mixer"},
2260         {"AUX Vol Input Mux", "SPK Mixer", "SPK Mixer"},
2261         {"AUX Vol Input Mux", "Mono Mixer", "Mono Mixer"},
2262
2263         {"SPKL", NULL, "SPK Amp"},
2264         {"SPKL", NULL, "SPK Vol Input Mux"},
2265         {"SPKR", NULL, "SPK Amp"},
2266         {"SPKR", NULL, "SPK Vol Input Mux"},
2267         {"HPL", NULL, "HP Amp"},
2268         {"HPR", NULL, "HP Amp"},
2269         {"AUX", NULL, "AUX Vol Input Mux"},
2270
2271         /* Voice DSP */
2272         {"SRC1 Enable", "Switch", "AIF1RX"},
2273
2274         {"RxDP src select Mux", "Voice to Stereo", "AIF2RX"},
2275         {"RxDP src select Mux", "ADCL to VoDSP", "Left ADC"},
2276         {"RxDP src select Mux", "SRC1 Output", "SRC1 Enable"},
2277         {"RxDP src select Mux", "ADCR to VoDSP", "Right ADC"},
2278
2279         {"RxDP Enable", "Switch", "RxDP src select Mux"},
2280         {"RxDC Enable", "Switch", "Left ADC"},
2281
2282         {"RxDP", NULL, "RxDP Enable"},
2283         {"RxDC", NULL, "RxDC Enable"},
2284         {"PDM", NULL, "Right ADC"},
2285
2286         {"Voice DSP", NULL, "RxDP"},
2287         {"Voice DSP", NULL, "RxDC"},
2288         {"Voice DSP", NULL, "PDM"},
2289
2290         {"TxDP", NULL, "Voice DSP"},
2291         {"TxDC", NULL, "Voice DSP"},
2292
2293         {"SRC2 src select Mux", "TxDP", "TxDP"},
2294         {"SRC2 src select Mux", "TxDC", "TxDC"},
2295         {"SRC2 Enable", "Switch", "SRC2 src select Mux"},
2296 };
2297
2298 struct _pll_div{
2299         u32 pll_in;
2300         u32 pll_out;
2301         u16 regvalue;
2302 };
2303
2304 /**************************************************************
2305   *     watch out!
2306   *     our codec support you to select different source as pll input, but if you 
2307   *     use both of the I2S audio interface and pcm interface instantially. 
2308   *     The two DAI must have the same pll setting params, so you have to offer
2309   *     the same pll input, and set our codec's sysclk the same one, we suggest 
2310   *     24576000.
2311   **************************************************************/
2312 static const struct _pll_div codec_master_pll1_div[] = {
2313         {  2048000,   8192000,  0x0ea0},
2314         {  3686400,   8192000,  0x4e27},
2315         { 12000000,   8192000,  0x456b},
2316         { 13000000,   8192000,  0x495f},
2317         { 13100000,   8192000,  0x0320},
2318         {  2048000,  11289600,  0xf637},
2319         {  3686400,  11289600,  0x2f22},
2320         { 12000000,  11289600,  0x3e2f},
2321         { 13000000,  11289600,  0x4d5b},
2322         { 13100000,  11289600,  0x363b},
2323         {  2048000,  16384000,  0x1ea0},
2324         {  3686400,  16384000,  0x9e27},
2325         { 12000000,  16384000,  0x452b},
2326         { 13000000,  16384000,  0x542f},
2327         { 13100000,  16384000,  0x03a0},
2328         {  2048000,  16934400,  0xe625},
2329         {  3686400,  16934400,  0x9126},
2330         { 12000000,  16934400,  0x4d2c},
2331         { 13000000,  16934400,  0x742f},
2332         { 13100000,  16934400,  0x3c27},
2333         {  2048000,  22579200,  0x2aa0},
2334         {  3686400,  22579200,  0x2f20},
2335         { 12000000,  22579200,  0x7e2f},
2336         { 13000000,  22579200,  0x742f},
2337         { 13100000,  22579200,  0x3c27},
2338         {  2048000,  24576000,  0x2ea0},
2339         {  3686400,  24576000,  0xee27},
2340         { 11289600,  24576000,  0x950F},
2341         { 12000000,  24576000,  0x2915},
2342         { 12288000,  24576000,  0x0600},
2343         { 13000000,  24576000,  0x772e},
2344         { 13100000,  24576000,  0x0d20},
2345         { 26000000,  24576000,  0x2027},
2346         { 26000000,  22579200,  0x392f},
2347         { 24576000,  22579200,  0x0921},
2348         { 24576000,  24576000,  0x02a0},
2349 };
2350
2351 static const struct _pll_div codec_bclk_pll1_div[] = {
2352         {  256000,   4096000,  0x3ea0},
2353         {  352800,   5644800,  0x3ea0},
2354         {  512000,   8192000,  0x3ea0},
2355         {  705600,  11289600,  0x3ea0},
2356         { 1024000,  16384000,  0x3ea0}, 
2357         { 1411200,  22579200,  0x3ea0},
2358         { 1536000,  24576000,  0x3ea0}, 
2359         { 2048000,  16384000,  0x1ea0}, 
2360         { 2822400,  22579200,  0x1ea0},
2361         { 3072000,  24576000,  0x1ea0},
2362         {  705600,  11289600,  0x3ea0},
2363         {  705600,   8467200,  0x3ab0},
2364         { 2822400,  11289600,  0x1ee0},
2365         { 3072000,  12288000,  0x1ee0},                 
2366 };
2367
2368 static const struct _pll_div codec_vbclk_pll1_div[] = {
2369         {  256000,   4096000,  0x3ea0},
2370         {  352800,   5644800,  0x3ea0},
2371         {  512000,   8192000,  0x3ea0},
2372         {  705600,  11289600,  0x3ea0},
2373         { 1024000,  16384000,  0x3ea0}, 
2374         { 1411200,  22579200,  0x3ea0},
2375         { 1536000,  24576000,  0x3ea0}, 
2376         { 2048000,  16384000,  0x1ea0}, 
2377         { 2822400,  22579200,  0x1ea0},
2378         { 3072000,  24576000,  0x1ea0},
2379         {  705600,  11289600,  0x3ea0},
2380         {  705600,   8467200,  0x3ab0},
2381 };
2382
2383 struct _coeff_div_stereo {
2384         unsigned int mclk;
2385         unsigned int rate;
2386         unsigned int reg60;
2387         unsigned int reg62;
2388 };
2389
2390 struct _coeff_div_voice {
2391         unsigned int mclk;
2392         unsigned int rate;
2393         unsigned int reg64;
2394 };
2395
2396 /* bclk is config to 32fs, if codec is choose to be slave mode,
2397 input bclk should be 32*fs */
2398 static const struct _coeff_div_stereo coeff_div_stereo[] = {
2399         {24576000, 48000, 0x3174, 0x1010}, 
2400         {12288000, 48000, 0x1174, 0x0000},
2401         {18432000, 48000, 0x2174, 0x1111},
2402         {36864000, 48000, 0x2274, 0x2020},
2403         {49152000, 48000, 0xf074, 0x3030},
2404         {24576000, 48000, 0x3172, 0x1010},
2405         {24576000,  8000, 0xB274, 0x2424},
2406         {24576000, 16000, 0xB174, 0x2222},
2407         {24576000, 32000, 0xB074, 0x2121},
2408         {22579200, 11025, 0X3374, 0x1414},
2409         {22579200, 22050, 0X3274, 0x1212},
2410         {22579200, 44100, 0X3174, 0x1010},
2411         {12288000,  8000, 0xB174, 0x2222},
2412         {11289600, 44100, 0X3072, 0x0000},
2413 };
2414
2415 /* bclk is config to 32fs, if codec is choose to be slave mode,
2416 input bclk should be 32*fs */
2417 static const struct _coeff_div_voice coeff_div_voice[] = {
2418         {24576000, 16000, 0x2622}, 
2419         {24576000, 8000, 0x2824},
2420         {2048000,8000,0x3000},
2421 };
2422
2423 static int get_coeff(unsigned int mclk, unsigned int rate, int mode)
2424 {
2425         int i;
2426
2427         pr_info("mclk = %d, rate = %d, mode = %d\n", mclk, rate, mode);
2428
2429         if (!mode)
2430                 for (i = 0; i < ARRAY_SIZE(coeff_div_stereo); i++) {
2431                         if ((coeff_div_stereo[i].rate == rate) &&
2432                                 (coeff_div_stereo[i].mclk == mclk))
2433                                 return i;
2434                 }
2435         else
2436                 for (i = 0; i< ARRAY_SIZE(coeff_div_voice); i++) {
2437                         if ((coeff_div_voice[i].rate == rate) &&
2438                                 (coeff_div_voice[i].mclk == mclk))
2439                                 return i;
2440                 }
2441
2442         pr_err("can't find a matched mclk and rate in %s\n",
2443                (mode ? "coeff_div_voice[]" : "coeff_div_audio[]"));
2444
2445         return -EINVAL;
2446 }
2447
2448
2449 static int rt5625_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
2450                         unsigned int freq_in, unsigned int freq_out)
2451 {
2452         struct snd_soc_codec *codec = dai->codec;
2453         int i, ret = -EINVAL;
2454
2455         if (pll_id < RT5625_PLL_MCLK || pll_id > RT5625_PLL_VBCLK)
2456                 return -EINVAL;
2457
2458         if (!freq_in || !freq_out) {
2459                 dev_info(dai->dev, "PLL is closed\n");
2460                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
2461                         RT5625_SCLK_MASK, RT5625_SCLK_MCLK);
2462                 return 0;
2463         }
2464
2465         if (RT5625_PLL_MCLK == pll_id) {
2466                 for (i = 0; i < ARRAY_SIZE(codec_master_pll1_div); i ++)
2467                         if ((freq_in == codec_master_pll1_div[i].pll_in) &&
2468                                 (freq_out == codec_master_pll1_div[i].pll_out)) {
2469                                 snd_soc_write(codec, RT5625_GEN_CTRL2,
2470                                         RT5625_PLL1_S_MCLK);
2471                                 snd_soc_write(codec, RT5625_PLL_CTRL,
2472                                         codec_master_pll1_div[i].regvalue);
2473                                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
2474                                         RT5625_SCLK_MASK, RT5625_SCLK_PLL1);
2475                                 ret = 0;
2476                                 break;
2477                         }
2478         } else if (RT5625_PLL_MCLK_TO_VSYSCLK == pll_id) {
2479                 for (i = 0; i < ARRAY_SIZE(codec_master_pll1_div); i ++)
2480                 {
2481                         if ((freq_in == codec_master_pll1_div[i].pll_in) && (freq_out == codec_master_pll1_div[i].pll_out))
2482                         {
2483                                 snd_soc_write(codec, RT5625_GEN_CTRL2, 0x0000);  /*PLL source from MCLK*/
2484                                 snd_soc_write(codec, RT5625_PLL_CTRL, codec_master_pll1_div[i].regvalue);  /*set pll code*/
2485                                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1, 0x0030, 0x0030);  /* Voice SYSCLK source from FLL1 */
2486                                 ret = 0;
2487                         }
2488                 }
2489         }else if (RT5625_PLL_BCLK == pll_id) {
2490                 for (i = 0; i < ARRAY_SIZE(codec_bclk_pll1_div); i ++)
2491                         if ((freq_in == codec_bclk_pll1_div[i].pll_in) &&
2492                                 (freq_out == codec_bclk_pll1_div[i].pll_out)) {
2493                                 snd_soc_write(codec, RT5625_GEN_CTRL2,
2494                                         RT5625_PLL1_S_BCLK);
2495                                 snd_soc_write(codec, RT5625_PLL_CTRL,
2496                                         codec_bclk_pll1_div[i].regvalue);
2497                                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
2498                                         RT5625_SCLK_MASK, RT5625_SCLK_PLL1);
2499                                 ret = 0;
2500                                 break;
2501                         }
2502         } else if (RT5625_PLL_VBCLK == pll_id) {
2503                 for (i = 0; i < ARRAY_SIZE(codec_vbclk_pll1_div); i ++)
2504                         if ((freq_in == codec_vbclk_pll1_div[i].pll_in) &&
2505                                 (freq_out == codec_vbclk_pll1_div[i].pll_out)) {
2506                                 snd_soc_write(codec, RT5625_GEN_CTRL2,
2507                                         RT5625_PLL1_S_VBCLK);
2508                                 snd_soc_write(codec, RT5625_PLL_CTRL,
2509                                         codec_vbclk_pll1_div[i].regvalue);
2510                                 snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
2511                                         RT5625_SCLK_MASK, RT5625_SCLK_PLL1);
2512                                 ret = 0;
2513                                 break;
2514                         }
2515         }
2516
2517         return ret;
2518 }
2519
2520
2521 static int rt5625_set_dai_sysclk(struct snd_soc_dai *dai, 
2522                 int clk_id, unsigned int freq, int dir)
2523 {
2524         struct snd_soc_codec *codec = dai->codec;
2525         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
2526         unsigned int val = 0;
2527
2528         switch (dai->id) {
2529         case RT5625_AIF1:
2530                 if (freq == rt5625->stereo_sysclk)
2531                         return 0;
2532                 rt5625->stereo_sysclk = freq;
2533                 break;
2534
2535         case RT5625_AIF2:
2536                 if (freq == rt5625->voice_sysclk)
2537                         return 0;
2538                 rt5625->voice_sysclk = freq;
2539                 break;
2540
2541         default:
2542                 return -EINVAL;
2543         }
2544
2545         switch (clk_id) {
2546         case RT5625_SCLK_S_MCLK:
2547                 break;
2548
2549         case RT5625_SCLK_S_PLL:
2550                 val |= RT5625_SCLK_PLL1;
2551                 break;
2552
2553         default:
2554                 dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
2555                 return -EINVAL;
2556         }
2557         snd_soc_update_bits(codec, RT5625_GEN_CTRL1,
2558                         RT5625_SCLK_MASK, val);
2559
2560         dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
2561         
2562         return 0;
2563 }
2564
2565 static int rt5625_hw_params(struct snd_pcm_substream *substream, 
2566                         struct snd_pcm_hw_params *params,
2567                         struct snd_soc_dai *dai)
2568 {
2569         struct snd_soc_pcm_runtime *rtd = substream->private_data;
2570         struct snd_soc_codec *codec = rtd->codec;
2571         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
2572         unsigned int iface = 0, rate = params_rate(params), coeff;
2573
2574         switch (params_format(params)) {
2575         case SNDRV_PCM_FORMAT_S16_LE:
2576                 break;
2577
2578         case SNDRV_PCM_FORMAT_S20_3LE:
2579                 iface |= RT5625_I2S_DL_20;
2580                 break;
2581
2582         case SNDRV_PCM_FORMAT_S24_LE:
2583                 iface |= RT5625_I2S_DL_24;
2584                 break;
2585
2586         case SNDRV_PCM_FORMAT_S8:
2587                 iface |= RT5625_I2S_DL_8;
2588                 break;
2589
2590         default:
2591                 return -EINVAL;
2592         }
2593
2594         switch (dai->id) {
2595         case RT5625_AIF1:
2596                 coeff = get_coeff(rt5625->stereo_sysclk, rate, 0);
2597                 if (coeff < 0) {
2598                         dev_err(codec->dev, "Unsupported clock setting\n");
2599                         return -EINVAL;
2600                 }
2601                 snd_soc_write(codec, RT5625_DAC_CLK_CTRL1,
2602                                 coeff_div_stereo[coeff].reg60);
2603                 snd_soc_write(codec, RT5625_DAC_CLK_CTRL2,
2604                                 coeff_div_stereo[coeff].reg62);
2605                 snd_soc_update_bits(codec, RT5625_SDP_CTRL,
2606                                 RT5625_I2S_DL_MASK, iface);
2607                 break;
2608
2609         case RT5625_AIF2:
2610                 rate = 8000;
2611                 coeff = get_coeff(rt5625->voice_sysclk, rate, 1);
2612                 if (coeff < 0) {
2613                         dev_err(codec->dev, "Unsupported clock setting\n");
2614                         return -EINVAL;
2615                 }
2616                 snd_soc_write(codec, RT5625_VDAC_CLK_CTRL1,
2617                                 coeff_div_voice[coeff].reg64);
2618                 iface |= 0x0100; //Voice SYSCLK source from FLL1
2619                 snd_soc_update_bits(codec, RT5625_EXT_SDP_CTRL,
2620                                 RT5625_I2S_DL_MASK, iface);
2621                 break;
2622
2623         default:
2624                 return -EINVAL;
2625         }
2626
2627 #ifdef RT5625_F_SMT_PHO
2628         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2629                 rt5625->app_bmp |= RT5625_PLY_MASK;
2630         else
2631                 rt5625->app_bmp |= RT5625_REC_MASK;
2632 #endif
2633
2634         return 0;
2635 }
2636
2637 static int rt5625_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2638 {
2639         struct snd_soc_codec *codec = dai->codec;
2640         unsigned int iface = 0;
2641         bool slave;
2642
2643         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2644         case SND_SOC_DAIFMT_CBM_CFM:
2645                 slave = false;
2646                 break;
2647
2648         case SND_SOC_DAIFMT_CBS_CFS:
2649                 slave = true;
2650                 break;
2651
2652         default:
2653                 return -EINVAL;
2654         }
2655
2656         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2657         case SND_SOC_DAIFMT_I2S:
2658                 break;
2659
2660         case SND_SOC_DAIFMT_LEFT_J:
2661                 iface |= RT5625_I2S_DF_LEFT;
2662                 break;
2663
2664         case SND_SOC_DAIFMT_DSP_A:
2665                 iface |= RT5625_I2S_DF_PCM_A;
2666                 break;
2667
2668         case SND_SOC_DAIFMT_DSP_B:
2669                 iface |= RT5625_I2S_DF_PCM_B;
2670                 break;
2671
2672         default:
2673                 return -EINVAL;                 
2674         }
2675
2676         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2677         case SND_SOC_DAIFMT_NB_NF:
2678                 break;
2679
2680         case SND_SOC_DAIFMT_IB_NF:
2681                 iface |= RT5625_I2S_BP_INV;
2682                 break;
2683
2684         default:
2685                 return -EINVAL;
2686         }
2687         iface |= 0x8000;      /*enable vopcm*/
2688         
2689         switch (dai->id) {
2690         case RT5625_AIF1:
2691                 if (slave)
2692                         iface |= RT5625_I2S_M_SLV;
2693                 snd_soc_update_bits(codec, RT5625_SDP_CTRL,
2694                         RT5625_I2S_M_MASK | RT5625_I2S_BP_MASK |
2695                         RT5625_I2S_DF_MASK, iface);
2696                 break;
2697
2698         case RT5625_AIF2:
2699                 if (slave)
2700                         iface |= RT5625_PCM_M_SLV;
2701                 snd_soc_update_bits(codec, RT5625_EXT_SDP_CTRL,
2702                         RT5625_PCM_M_MASK | RT5625_I2S_BP_MASK |
2703                         RT5625_I2S_DF_MASK, iface);
2704                 break;
2705
2706         default:
2707                 return -EINVAL;
2708         }
2709         return 0;
2710 }
2711
2712 #ifdef RT5625_F_SMT_PHO
2713 static int rt5625_trigger(struct snd_pcm_substream *substream,
2714         int cmd, struct snd_soc_dai *dai)
2715 {
2716         struct snd_soc_pcm_runtime *rtd = substream->private_data;
2717         struct snd_soc_codec *codec = rtd->codec;
2718         struct rt5625_priv *rt5625 = snd_soc_codec_get_drvdata(codec);
2719         int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
2720
2721         switch (cmd) {
2722         case SNDRV_PCM_TRIGGER_START:
2723         case SNDRV_PCM_TRIGGER_RESUME:
2724         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2725                 if (capture)
2726                         rt5625->app_bmp |= RT5625_REC_MASK;
2727                 else
2728                         rt5625->app_bmp |= RT5625_PLY_MASK;
2729                 break;
2730
2731         case SNDRV_PCM_TRIGGER_STOP:
2732         case SNDRV_PCM_TRIGGER_SUSPEND:
2733         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2734                 if (capture)
2735                         rt5625->app_bmp &= ~RT5625_REC_MASK;
2736                 else
2737                         rt5625->app_bmp &= ~RT5625_PLY_MASK;
2738                 rt5625->app_bmp &= ~RT5625_VOIP_MASK;
2739                 break;
2740
2741         default:
2742                 break;
2743         }
2744
2745         return 0;
2746 }
2747 #endif
2748
2749 static int rt5625_set_bias_level(struct snd_soc_codec *codec, 
2750                         enum snd_soc_bias_level level)
2751 {
2752         switch(level) {
2753         case SND_SOC_BIAS_ON:
2754 #ifdef RT5625_DEMO
2755                 snd_soc_update_bits(codec, RT5625_HP_OUT_VOL,
2756                                 RT5625_L_MUTE | RT5625_R_MUTE, 0);
2757                 snd_soc_update_bits(codec, RT5625_SPK_OUT_VOL,
2758                                 RT5625_L_MUTE | RT5625_R_MUTE, 0);
2759 #endif
2760                 break;
2761
2762         case SND_SOC_BIAS_PREPARE:
2763                 snd_soc_write(codec, RT5625_PD_CTRL, 0x0000);
2764                 snd_soc_update_bits(codec, RT5625_PWR_ADD1,
2765                                 RT5625_P_MB1 | RT5625_P_MB2,
2766                                 RT5625_P_MB1 | RT5625_P_MB2);
2767                 break;
2768
2769         case SND_SOC_BIAS_STANDBY:
2770 #ifdef RT5625_DEMO
2771                 snd_soc_update_bits(codec, RT5625_HP_OUT_VOL,
2772                         RT5625_L_MUTE | RT5625_R_MUTE,
2773                         RT5625_L_MUTE | RT5625_R_MUTE);
2774                 snd_soc_update_bits(codec, RT5625_SPK_OUT_VOL,
2775                         RT5625_L_MUTE | RT5625_R_MUTE,
2776                         RT5625_L_MUTE | RT5625_R_MUTE);
2777                 snd_soc_update_bits(codec, RT5625_PWR_ADD1,
2778                                 RT5625_P_MB1 | RT5625_P_MB2, 0);
2779 #endif
2780                 if (SND_SOC_BIAS_OFF == codec->dapm.bias_level) {
2781                         snd_soc_write(codec, RT5625_PD_CTRL, 0);
2782                         snd_soc_write(codec, RT5625_PWR_ADD1,
2783                                         RT5625_P_MAIN_BIAS);
2784                         snd_soc_write(codec, RT5625_PWR_ADD2, RT5625_P_VREF);
2785                         codec->cache_only = false;
2786                         snd_soc_cache_sync(codec);
2787                 }
2788                 break;
2789
2790         case SND_SOC_BIAS_OFF:
2791 #ifdef RT5625_DEMO
2792                 snd_soc_update_bits(codec, RT5625_HP_OUT_VOL,
2793                         RT5625_L_MUTE | RT5625_R_MUTE,
2794                         RT5625_L_MUTE | RT5625_R_MUTE);
2795                 snd_soc_update_bits(codec, RT5625_SPK_OUT_VOL,
2796                         RT5625_L_MUTE | RT5625_R_MUTE,
2797                         RT5625_L_MUTE | RT5625_R_MUTE);
2798 #endif
2799                 snd_soc_write(codec, RT5625_PWR_ADD1, 0x0000);
2800                 snd_soc_write(codec, RT5625_PWR_ADD2, 0x0000);
2801                 snd_soc_write(codec, RT5625_PWR_ADD3, 0x0000);
2802                 
2803                 break;
2804         }
2805         codec->dapm.bias_level = level;
2806
2807         return 0;
2808 }
2809
2810 #ifdef RT5625_PROC      
2811 static int rt5625_proc_init(void);
2812 #endif
2813
2814 static int rt5625_probe(struct snd_soc_codec *codec)
2815 {
2816         int ret;
2817
2818         ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
2819         if (ret != 0) {
2820                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
2821                 return ret;
2822         }
2823         
2824         #ifdef RT5625_PROC      
2825         rt5625_proc_init();
2826         #endif
2827         
2828         rt5625_reset(codec);
2829         snd_soc_write(codec, RT5625_PD_CTRL, 0);
2830         snd_soc_write(codec, RT5625_PWR_ADD1, RT5625_P_MAIN_BIAS);
2831         snd_soc_write(codec, RT5625_PWR_ADD2, RT5625_P_VREF);
2832 #ifdef RT5625_DEMO
2833         rt5625_reg_init(codec);
2834 #endif
2835         codec->dapm.bias_level = SND_SOC_BIAS_STANDBY;
2836         rt5625_codec = codec;
2837         return 0;
2838 }
2839
2840 static int rt5625_remove(struct snd_soc_codec *codec)
2841 {
2842         rt5625_set_bias_level(codec, SND_SOC_BIAS_OFF);
2843         return 0;
2844 }
2845
2846 #ifdef CONFIG_PM
2847 static int rt5625_suspend(struct snd_soc_codec *codec)
2848 {
2849         rt5625_set_bias_level(codec, SND_SOC_BIAS_OFF);
2850         return 0;
2851 }
2852
2853 static int rt5625_resume(struct snd_soc_codec *codec)
2854 {
2855         rt5625_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2856         return 0;
2857 }
2858 #else
2859 #define rt5625_suspend NULL
2860 #define rt5625_resume NULL
2861 #endif
2862
2863 #define RT5625_STEREO_RATES SNDRV_PCM_RATE_8000_48000
2864 #define RT5625_VOICE_RATES SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000
2865 #define RT5625_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
2866                         SNDRV_PCM_FMTBIT_S20_3LE |\
2867                         SNDRV_PCM_FMTBIT_S24_LE |\
2868                         SNDRV_PCM_FMTBIT_S8)
2869
2870 struct snd_soc_dai_ops rt5625_aif_dai_ops = {
2871 #ifdef RT5625_F_SMT_PHO
2872         .trigger = rt5625_trigger,
2873 #endif
2874         .hw_params = rt5625_hw_params,
2875         .set_fmt = rt5625_set_dai_fmt,
2876         .set_sysclk = rt5625_set_dai_sysclk,
2877         .set_pll = rt5625_set_dai_pll,
2878 };
2879
2880 struct snd_soc_dai_driver rt5625_dai[] = {
2881         {
2882                 .name = "rt5625-aif1",
2883                 .id = RT5625_AIF1,
2884                 .playback = {
2885                         .stream_name = "HiFi Playback",
2886                         .channels_min = 1,
2887                         .channels_max = 2,
2888                         .rates = RT5625_STEREO_RATES,
2889                         .formats = RT5625_FORMATS,
2890                 },
2891                 .capture = {
2892                         .stream_name = "HiFi Capture",
2893                         .channels_min = 1,
2894                         .channels_max = 2,
2895                         .rates = RT5625_STEREO_RATES,
2896                         .formats = RT5625_FORMATS,
2897                 },
2898                 .ops = &rt5625_aif_dai_ops,
2899         },
2900         {
2901                 .name = "rt5625-aif2",
2902                 .id = RT5625_AIF2,
2903                 .playback = {
2904                         .stream_name = "Voice Playback",
2905                         .channels_min = 1,
2906                         .channels_max = 2,
2907                         .rates = RT5625_VOICE_RATES,
2908                         .formats = RT5625_FORMATS,
2909                 },
2910                 .capture = {
2911                         .stream_name = "Voice Capture",
2912                         .channels_min = 1,
2913                         .channels_max = 2,
2914                         .rates = RT5625_VOICE_RATES,
2915                         .formats = RT5625_FORMATS,
2916                 },
2917                 .ops = &rt5625_aif_dai_ops,
2918         },
2919 };
2920
2921 static struct snd_soc_codec_driver soc_codec_dev_rt5625 = {
2922         .probe = rt5625_probe,
2923         .remove = rt5625_remove,
2924         .suspend = rt5625_suspend,
2925         .resume = rt5625_resume,
2926         .set_bias_level = rt5625_set_bias_level,
2927         .reg_cache_size = 0x80,
2928         .reg_word_size = sizeof(u16),
2929         .reg_cache_default = rt5625_reg,
2930         .volatile_register = rt5625_volatile_register,
2931         .readable_register = rt5625_readable_register,
2932         .reg_cache_step = 1,
2933         .controls = rt5625_snd_controls,
2934         .num_controls = ARRAY_SIZE(rt5625_snd_controls),
2935         .dapm_widgets = rt5625_dapm_widgets,
2936         .num_dapm_widgets = ARRAY_SIZE(rt5625_dapm_widgets),
2937         .dapm_routes = rt5625_dapm_routes,
2938         .num_dapm_routes = ARRAY_SIZE(rt5625_dapm_routes),
2939 };
2940
2941 static const struct i2c_device_id rt5625_i2c_id[] = {
2942         { "rt5625", 0 },
2943         { }
2944 };
2945 MODULE_DEVICE_TABLE(i2c, rt5625_i2c_id);
2946
2947 #ifdef CONFIG_OF
2948 /*
2949 dts:
2950         codec@1e {
2951                 compatible = "rt5625";
2952                 reg = <0x1e>;
2953                 spk-ctr-pin = <&gpio3 GPIO_D7 GPIO_ACTIVE_HIGH>;
2954                 spk-ctr-on = <1>;
2955                 spk-ctr-off = <0>;
2956         };
2957 */
2958
2959 static int rt5625_parse_dt_property(struct device *dev,
2960                                   struct rt5625_priv *rt5625)
2961 {
2962         struct device_node *node = dev->of_node;
2963         int ret;
2964
2965         printk("%s()\n", __FUNCTION__);
2966
2967         if (!node)
2968                 return -ENODEV;
2969
2970         rt5625->spk_ctr_pin= of_get_named_gpio_flags(node, "spk-ctr-pin", 0, NULL);
2971         if (rt5625->spk_ctr_pin < 0) {
2972                 DBG("%s() Can not read property spk-ctr-pin\n", __FUNCTION__);
2973                 rt5625->spk_ctr_pin = INVALID_GPIO;
2974         }
2975
2976         ret = of_property_read_u32(node, "spk-ctr-on", &rt5625->spk_ctr_on);
2977         if (ret < 0) {
2978                 DBG("%s() Can not read property spk-ctr-on\n", __FUNCTION__);
2979                 rt5625->spk_ctr_on = 1;
2980         }
2981
2982         ret = of_property_read_u32(node, "spk-ctr-off", &rt5625->spk_ctr_off);
2983         if (ret < 0) {
2984                 DBG("%s() Can not read property spk-ctr-off\n", __FUNCTION__);
2985                 rt5625->spk_ctr_off = 0;
2986         }
2987
2988         return 0;
2989 }
2990 #else
2991 static int rt5625_parse_dt_property(struct device *dev,
2992                                   struct rt3261_priv *rt3261)
2993 {
2994         return -ENOSYS;
2995 }
2996 #endif //#ifdef CONFIG_OF
2997
2998 static int rt5625_i2c_probe(struct i2c_client *i2c,
2999                     const struct i2c_device_id *id)
3000 {
3001         struct rt5625_priv *rt5625;
3002         int ret;
3003         //struct rt5625_platform_data *pdata = pdata = i2c->dev.platform_data;
3004
3005         rt5625 = kzalloc(sizeof(struct rt5625_priv), GFP_KERNEL);
3006         if (NULL == rt5625)
3007                 return -ENOMEM;
3008
3009         ret = rt5625_parse_dt_property(&i2c->dev, rt5625);
3010         if (ret < 0) {
3011                 printk("%s() parse device tree property error %d\n", __FUNCTION__, ret);
3012                 return ret;
3013         }
3014
3015         if(rt5625->spk_ctr_pin != INVALID_GPIO)
3016         {
3017                 gpio_request(rt5625->spk_ctr_pin,NULL);
3018                 gpio_direction_output(rt5625->spk_ctr_pin,rt5625->spk_ctr_off);
3019                 rt5625->spk_ctr_status = 0;
3020         }
3021
3022         i2c_set_clientdata(i2c, rt5625);
3023
3024         ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5625,
3025                         rt5625_dai, ARRAY_SIZE(rt5625_dai));
3026         if (ret < 0)
3027                 kfree(rt5625);
3028
3029         return ret;
3030 }
3031
3032 static int rt5625_i2c_remove(struct i2c_client *i2c)
3033 {
3034         snd_soc_unregister_codec(&i2c->dev);
3035         kfree(i2c_get_clientdata(i2c));
3036         return 0;
3037 }
3038
3039 struct i2c_driver rt5625_i2c_driver = {
3040         .driver = {
3041                 .name = "rt5625",
3042                 .owner = THIS_MODULE,
3043         },
3044         .probe = rt5625_i2c_probe,
3045         .remove   = rt5625_i2c_remove,
3046         .id_table = rt5625_i2c_id,
3047 };
3048
3049 static int __init rt5625_modinit(void)
3050 {
3051         return i2c_add_driver(&rt5625_i2c_driver);
3052 }
3053 module_init(rt5625_modinit);
3054
3055 static void __exit rt5625_modexit(void)
3056 {
3057         i2c_del_driver(&rt5625_i2c_driver);
3058 }
3059 module_exit(rt5625_modexit);
3060
3061 MODULE_DESCRIPTION("ASoC RT5625 driver");
3062 MODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
3063 MODULE_LICENSE("GPL");
3064
3065
3066 #ifdef RT5625_PROC
3067
3068 static ssize_t rt5625_proc_write(struct file *file, const char __user *buffer,
3069                            unsigned long len, void *data)
3070 {
3071         char *cookie_pot; 
3072         char *p;
3073         int reg;
3074         int value;
3075         
3076         cookie_pot = (char *)vmalloc( len );
3077         if (!cookie_pot) 
3078         {
3079                 return -ENOMEM;
3080         } 
3081         else 
3082         {
3083                 if (copy_from_user( cookie_pot, buffer, len )) 
3084                         return -EFAULT;
3085         }
3086
3087         switch(cookie_pot[0])
3088         {
3089         case 'd':
3090         case 'D':
3091                 debug_write_read ++;
3092                 debug_write_read %= 2;
3093                 if(debug_write_read != 0)
3094                         printk("Debug read and write reg on\n");
3095                 else    
3096                         printk("Debug read and write reg off\n");       
3097                 break;  
3098         case 'r':
3099         case 'R':
3100                 printk("Read reg debug\n");             
3101                 if(cookie_pot[1] ==':')
3102                 {
3103                         debug_write_read = 1;
3104                         strsep(&cookie_pot,":");
3105                         while((p=strsep(&cookie_pot,",")))
3106                         {
3107                                 reg = simple_strtol(p,NULL,16);
3108                                 value = rt5625_read(rt5625_codec,reg);
3109                                 printk("rt5625_read:0x%04x = 0x%04x\n",reg,value);
3110                         }
3111                         debug_write_read = 0;
3112                         printk("\n");
3113                 }
3114                 else
3115                 {
3116                         printk("Error Read reg debug.\n");
3117                         printk("For example: echo r:22,23,24,25>rt5625_ts\n");
3118                 }
3119                 break;
3120         case 'w':
3121         case 'W':
3122                 printk("Write reg debug\n");            
3123                 if(cookie_pot[1] ==':')
3124                 {
3125                         debug_write_read = 1;
3126                         strsep(&cookie_pot,":");
3127                         while((p=strsep(&cookie_pot,"=")))
3128                         {
3129                                 reg = simple_strtol(p,NULL,16);
3130                                 p=strsep(&cookie_pot,",");
3131                                 value = simple_strtol(p,NULL,16);
3132                                 rt5625_write(rt5625_codec,reg,value);
3133                                 printk("rt5625_write:0x%04x = 0x%04x\n",reg,value);
3134                         }
3135                         debug_write_read = 0;
3136                         printk("\n");
3137                 }
3138                 else
3139                 {
3140                         printk("Error Write reg debug.\n");
3141                         printk("For example: w:22=0,23=0,24=0,25=0>rt5625_ts\n");
3142                 }
3143                 break;
3144         case 'a':
3145                 printk("Dump reg \n");          
3146
3147                 for(reg = 0; reg < 0x6e; reg+=2)
3148                 {
3149                         value = rt5625_read(rt5625_codec,reg);
3150                         printk("rt5625_read:0x%04x = 0x%04x\n",reg,value);
3151                 }
3152
3153                 break;          
3154         default:
3155                 printk("Help for rt5625_ts .\n-->The Cmd list: \n");
3156                 printk("-->'d&&D' Open or Off the debug\n");
3157                 printk("-->'r&&R' Read reg debug,Example: echo 'r:22,23,24,25'>rt5625_ts\n");
3158                 printk("-->'w&&W' Write reg debug,Example: echo 'w:22=0,23=0,24=0,25=0'>rt5625_ts\n");
3159                 break;
3160         }
3161
3162         return len;
3163 }
3164
3165 static const struct file_operations rt5625_proc_fops = {
3166         .owner          = THIS_MODULE,
3167 };
3168
3169 static int rt5625_proc_init(void)
3170 {
3171         struct proc_dir_entry *rt5625_proc_entry;
3172         rt5625_proc_entry = create_proc_entry("driver/rt5625_ts", 0777, NULL);
3173         if(rt5625_proc_entry != NULL)
3174         {
3175                 rt5625_proc_entry->write_proc = rt5625_proc_write;
3176                 return 0;
3177         }
3178         else
3179         {
3180                 printk("create proc error !\n");
3181                 return -1;
3182         }
3183 }
3184 #endif
3185