asoc: codecs: Add Headset detect support for ALC5639/40
[linux-2.6.git] / sound / soc / codecs / rt5640.c
1 /*
2  * rt5640.c  --  RT5640 ALSA SoC audio codec driver
3  *
4  * Copyright 2011 Realtek Semiconductor Corp.
5  * Author: Johnny Hsu <johnnyhsu@realtek.com>
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/pm.h>
16 #include <linux/i2c.h>
17 #include <linux/platform_device.h>
18 #include <linux/spi/spi.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/soc-dapm.h>
24 #include <sound/initval.h>
25 #include <sound/tlv.h>
26
27 #include "rt5640.h"
28 #if (CONFIG_SND_SOC_RT5642_MODULE | CONFIG_SND_SOC_RT5642)
29 #include "rt5640-dsp.h"
30 #endif
31
32 #define RT5640_DEMO 1
33 #define RT5640_REG_RW 1
34 #define RT5640_DET_EXT_MIC 0
35
36 #ifdef RT5640_DEMO
37 struct rt5640_init_reg {
38         u8 reg;
39         u16 val;
40 };
41
42 static struct rt5640_init_reg init_list[] = {
43         {RT5640_DUMMY1          , 0x3701},/*fa[12:13] = 1'b;fa[8~10]=1;fa[0]=1*/
44         {RT5640_DEPOP_M1        , 0x0019},/* 8e[4:3] = 11'b; 8e[0] = 1'b */
45         {RT5640_DEPOP_M2        , 0x3100},/* 8f[13] = 1'b */
46         {RT5640_ADDA_CLK1       , 0x1114},/* 73[2] = 1'b  */
47         {RT5640_MICBIAS         , 0x3030},/* 93[5:4] = 11'b */
48         {RT5640_PRIV_INDEX      , 0x003d},/* PR3d[12] = 1'b */
49         {RT5640_PRIV_DATA       , 0x3600},
50         {RT5640_CLS_D_OUT       , 0xa000},/* 8d[11] = 0'b */
51         {RT5640_PRIV_INDEX      , 0x001c},/* PR1c = 0D21'h */
52         {RT5640_PRIV_DATA       , 0x0D21},
53         {RT5640_PRIV_INDEX      , 0x001b},/* PR1B = 0D21'h */
54         {RT5640_PRIV_DATA       , 0x0000},
55         {RT5640_PRIV_INDEX      , 0x0012},/* PR12 = 0aa8'h */
56         {RT5640_PRIV_DATA       , 0x0aa8},
57         {RT5640_PRIV_INDEX      , 0x0014},/* PR14 = 0aaa'h */
58         {RT5640_PRIV_DATA       , 0x0aaa},
59         {RT5640_PRIV_INDEX      , 0x0020},/* PR20 = 6110'h */
60         {RT5640_PRIV_DATA       , 0x6110},
61         {RT5640_PRIV_INDEX      , 0x0021},/* PR21 = e0e0'h */
62         {RT5640_PRIV_DATA       , 0xe0e0},
63         {RT5640_PRIV_INDEX      , 0x0023},/* PR23 = 1804'h */
64         {RT5640_PRIV_DATA       , 0x1804},
65         /*playback*/
66         {RT5640_STO_DAC_MIXER   , 0x1414},/*Dig inf 1 -> Sto DAC mixer -> DACL*/
67         {RT5640_OUT_L3_MIXER    , 0x01fe},/*DACL1 -> OUTMIXL*/
68         {RT5640_OUT_R3_MIXER    , 0x01fe},/*DACR1 -> OUTMIXR */
69         {RT5640_HP_VOL          , 0x8888},/* OUTMIX -> HPVOL */
70         {RT5640_HPO_MIXER       , 0xc000},/* HPVOL -> HPOLMIX */
71 /*      {RT5640_HPO_MIXER       , 0xa000},// DAC1 -> HPOLMIX   */
72         {RT5640_SPK_L_MIXER     , 0x0036},/* DACL1 -> SPKMIXL */
73         {RT5640_SPK_R_MIXER     , 0x0036},/* DACR1 -> SPKMIXR */
74         {RT5640_SPK_VOL         , 0x8888},/* SPKMIX -> SPKVOL */
75         {RT5640_SPO_L_MIXER     , 0xe800},/* SPKVOLL -> SPOLMIX */
76         {RT5640_SPO_R_MIXER     , 0x2800},/* SPKVOLR -> SPORMIX */
77 /*      {RT5640_SPO_L_MIXER     , 0xb800},//DAC -> SPOLMIX */
78 /*      {RT5640_SPO_R_MIXER     , 0x1800},//DAC -> SPORMIX */
79 /*      {RT5640_I2S1_SDP        , 0xD000},//change IIS1 and IIS2 */
80         /*record*/
81         {RT5640_IN1_IN2         , 0x5080},/*IN1 boost 40db & differential mode*/
82         {RT5640_IN3_IN4         , 0x0500},/*IN2 boost 40db & signal ended mode*/
83         {RT5640_REC_L2_MIXER    , 0x005f},/* enable Mic1 -> RECMIXL */
84         {RT5640_REC_R2_MIXER    , 0x005f},/* enable Mic1 -> RECMIXR */
85 /*      {RT5640_REC_L2_MIXER    , 0x006f},//Mic2 -> RECMIXL */
86 /*      {RT5640_REC_R2_MIXER    , 0x006f},//Mic2 -> RECMIXR */
87         {RT5640_STO_ADC_MIXER   , 0x3020},/* ADC -> Sto ADC mixer */
88
89 #if RT5640_DET_EXT_MIC
90         {RT5640_MICBIAS , 0x3800},/* enable MICBIAS short current */
91         {RT5640_GPIO_CTRL1      , 0x8400},/* set GPIO1 to IRQ */
92         {RT5640_GPIO_CTRL3      , 0x0004},/* set GPIO1 output */
93         {RT5640_IRQ_CTRL2       , 0x8000},/*set MICBIAS short current to IRQ */
94                                         /*( if sticky set regBE : 8800 ) */
95 #endif
96
97 };
98 #define RT5640_INIT_REG_LEN ARRAY_SIZE(init_list)
99
100 static int rt5640_reg_init(struct snd_soc_codec *codec)
101 {
102         int i;
103         for (i = 0; i < RT5640_INIT_REG_LEN; i++)
104                 snd_soc_write(codec, init_list[i].reg, init_list[i].val);
105         return 0;
106 }
107 #endif
108
109 static const u16 rt5640_reg[RT5640_VENDOR_ID2 + 1] = {
110         [RT5640_RESET] = 0x000c,
111         [RT5640_SPK_VOL] = 0xc8c8,
112         [RT5640_HP_VOL] = 0xc8c8,
113         [RT5640_OUTPUT] = 0xc8c8,
114         [RT5640_MONO_OUT] = 0x8000,
115         [RT5640_INL_INR_VOL] = 0x0808,
116         [RT5640_DAC1_DIG_VOL] = 0xafaf,
117         [RT5640_DAC2_DIG_VOL] = 0xafaf,
118         [RT5640_ADC_DIG_VOL] = 0x2f2f,
119         [RT5640_ADC_DATA] = 0x2f2f,
120         [RT5640_STO_ADC_MIXER] = 0x7060,
121         [RT5640_MONO_ADC_MIXER] = 0x7070,
122         [RT5640_AD_DA_MIXER] = 0x8080,
123         [RT5640_STO_DAC_MIXER] = 0x5454,
124         [RT5640_MONO_DAC_MIXER] = 0x5454,
125         [RT5640_DIG_MIXER] = 0xaa00,
126         [RT5640_DSP_PATH2] = 0xa000,
127         [RT5640_REC_L2_MIXER] = 0x007f,
128         [RT5640_REC_R2_MIXER] = 0x007f,
129         [RT5640_HPO_MIXER] = 0xe000,
130         [RT5640_SPK_L_MIXER] = 0x003e,
131         [RT5640_SPK_R_MIXER] = 0x003e,
132         [RT5640_SPO_L_MIXER] = 0xf800,
133         [RT5640_SPO_R_MIXER] = 0x3800,
134         [RT5640_SPO_CLSD_RATIO] = 0x0004,
135         [RT5640_MONO_MIXER] = 0xfc00,
136         [RT5640_OUT_L3_MIXER] = 0x01ff,
137         [RT5640_OUT_R3_MIXER] = 0x01ff,
138         [RT5640_LOUT_MIXER] = 0xf000,
139         [RT5640_PWR_ANLG1] = 0x00c0,
140         [RT5640_I2S1_SDP] = 0x8000,
141         [RT5640_I2S2_SDP] = 0x8000,
142         [RT5640_I2S3_SDP] = 0x8000,
143         [RT5640_ADDA_CLK1] = 0x1110,
144         [RT5640_ADDA_CLK2] = 0x0c00,
145         [RT5640_DMIC] = 0x1d00,
146         [RT5640_ASRC_3] = 0x0008,
147         [RT5640_HP_OVCD] = 0x0600,
148         [RT5640_CLS_D_OVCD] = 0x0228,
149         [RT5640_CLS_D_OUT] = 0xa800,
150         [RT5640_DEPOP_M1] = 0x0004,
151         [RT5640_DEPOP_M2] = 0x1100,
152         [RT5640_DEPOP_M3] = 0x0646,
153         [RT5640_CHARGE_PUMP] = 0x0c00,
154         [RT5640_MICBIAS] = 0x3000,
155         [RT5640_EQ_CTRL1] = 0x2080,
156         [RT5640_DRC_AGC_1] = 0x2206,
157         [RT5640_DRC_AGC_2] = 0x1f00,
158         [RT5640_ANC_CTRL1] = 0x034b,
159         [RT5640_ANC_CTRL2] = 0x0066,
160         [RT5640_ANC_CTRL3] = 0x000b,
161         [RT5640_GPIO_CTRL1] = 0x0400,
162         [RT5640_DSP_CTRL3] = 0x2000,
163         [RT5640_BASE_BACK] = 0x0013,
164         [RT5640_MP3_PLUS1] = 0x0680,
165         [RT5640_MP3_PLUS2] = 0x1c17,
166         [RT5640_3D_HP] = 0x8c00,
167         [RT5640_ADJ_HPF] = 0x2a20,
168         [RT5640_HP_CALIB_AMP_DET] = 0x0400,
169         [RT5640_SV_ZCD1] = 0x0809,
170         [RT5640_VENDOR_ID1] = 0x10ec,
171         [RT5640_VENDOR_ID2] = 0x6231,
172 };
173
174 static int rt5640_reset(struct snd_soc_codec *codec)
175 {
176         return snd_soc_write(codec, RT5640_RESET, 0);
177 }
178
179 /**
180  * rt5640_index_write - Write private register.
181  * @codec: SoC audio codec device.
182  * @reg: Private register index.
183  * @value: Private register Data.
184  *
185  * Modify private register for advanced setting. It can be written through
186  * private index (0x6a) and data (0x6c) register.
187  *
188  * Returns 0 for success or negative error code.
189  */
190 static int rt5640_index_write(struct snd_soc_codec *codec,
191                 unsigned int reg, unsigned int value)
192 {
193         int ret;
194
195         ret = snd_soc_write(codec, RT5640_PRIV_INDEX, reg);
196         if (ret < 0) {
197                 dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
198                 goto err;
199         }
200         ret = snd_soc_write(codec, RT5640_PRIV_DATA, value);
201         if (ret < 0) {
202                 dev_err(codec->dev, "Failed to set private value: %d\n", ret);
203                 goto err;
204         }
205         return 0;
206
207 err:
208         return ret;
209 }
210
211 /**
212  * rt5640_index_read - Read private register.
213  * @codec: SoC audio codec device.
214  * @reg: Private register index.
215  *
216  * Read advanced setting from private register. It can be read through
217  * private index (0x6a) and data (0x6c) register.
218  *
219  * Returns private register value or negative error code.
220  */
221 static unsigned int rt5640_index_read(
222         struct snd_soc_codec *codec, unsigned int reg)
223 {
224         int ret;
225
226         ret = snd_soc_write(codec, RT5640_PRIV_INDEX, reg);
227         if (ret < 0) {
228                 dev_err(codec->dev, "Failed to set private addr: %d\n", ret);
229                 return ret;
230         }
231         return snd_soc_read(codec, RT5640_PRIV_DATA);
232 }
233
234 /**
235  * rt5640_index_update_bits - update private register bits
236  * @codec: audio codec
237  * @reg: Private register index.
238  * @mask: register mask
239  * @value: new value
240  *
241  * Writes new register value.
242  *
243  * Returns 1 for change, 0 for no change, or negative error code.
244  */
245 static int rt5640_index_update_bits(struct snd_soc_codec *codec,
246         unsigned int reg, unsigned int mask, unsigned int value)
247 {
248         unsigned int old, new;
249         int change, ret;
250
251         ret = rt5640_index_read(codec, reg);
252         if (ret < 0) {
253                 dev_err(codec->dev, "Failed to read private reg: %d\n", ret);
254                 goto err;
255         }
256
257         old = ret;
258         new = (old & ~mask) | (value & mask);
259         change = old != new;
260         if (change) {
261                 ret = rt5640_index_write(codec, reg, new);
262                 if (ret < 0) {
263                         dev_err(codec->dev,
264                                 "Failed to write private reg: %d\n", ret);
265                         goto err;
266                 }
267         }
268         return change;
269
270 err:
271         return ret;
272 }
273
274 static int rt5640_volatile_register(
275         struct snd_soc_codec *codec, unsigned int reg)
276 {
277         switch (reg) {
278         case RT5640_RESET:
279         case RT5640_PRIV_DATA:
280         case RT5640_ASRC_5:
281         case RT5640_EQ_CTRL1:
282         case RT5640_DRC_AGC_1:
283         case RT5640_ANC_CTRL1:
284         case RT5640_IRQ_CTRL2:
285         case RT5640_INT_IRQ_ST:
286         case RT5640_DSP_CTRL2:
287         case RT5640_DSP_CTRL3:
288         case RT5640_PGM_REG_ARR1:
289         case RT5640_PGM_REG_ARR3:
290                 return 1;
291         default:
292                 return 0;
293         }
294 }
295
296 static int rt5640_readable_register(
297         struct snd_soc_codec *codec, unsigned int reg)
298 {
299         switch (reg) {
300         case RT5640_RESET:
301         case RT5640_SPK_VOL:
302         case RT5640_HP_VOL:
303         case RT5640_OUTPUT:
304         case RT5640_MONO_OUT:
305         case RT5640_IN1_IN2:
306         case RT5640_IN3_IN4:
307         case RT5640_INL_INR_VOL:
308         case RT5640_DAC1_DIG_VOL:
309         case RT5640_DAC2_DIG_VOL:
310         case RT5640_DAC2_CTRL:
311         case RT5640_ADC_DIG_VOL:
312         case RT5640_ADC_DATA:
313         case RT5640_ADC_BST_VOL:
314         case RT5640_STO_ADC_MIXER:
315         case RT5640_MONO_ADC_MIXER:
316         case RT5640_AD_DA_MIXER:
317         case RT5640_STO_DAC_MIXER:
318         case RT5640_MONO_DAC_MIXER:
319         case RT5640_DIG_MIXER:
320         case RT5640_DSP_PATH1:
321         case RT5640_DSP_PATH2:
322         case RT5640_DIG_INF_DATA:
323         case RT5640_REC_L1_MIXER:
324         case RT5640_REC_L2_MIXER:
325         case RT5640_REC_R1_MIXER:
326         case RT5640_REC_R2_MIXER:
327         case RT5640_HPO_MIXER:
328         case RT5640_SPK_L_MIXER:
329         case RT5640_SPK_R_MIXER:
330         case RT5640_SPO_L_MIXER:
331         case RT5640_SPO_R_MIXER:
332         case RT5640_SPO_CLSD_RATIO:
333         case RT5640_MONO_MIXER:
334         case RT5640_OUT_L1_MIXER:
335         case RT5640_OUT_L2_MIXER:
336         case RT5640_OUT_L3_MIXER:
337         case RT5640_OUT_R1_MIXER:
338         case RT5640_OUT_R2_MIXER:
339         case RT5640_OUT_R3_MIXER:
340         case RT5640_LOUT_MIXER:
341         case RT5640_PWR_DIG1:
342         case RT5640_PWR_DIG2:
343         case RT5640_PWR_ANLG1:
344         case RT5640_PWR_ANLG2:
345         case RT5640_PWR_MIXER:
346         case RT5640_PWR_VOL:
347         case RT5640_PRIV_INDEX:
348         case RT5640_PRIV_DATA:
349         case RT5640_I2S1_SDP:
350         case RT5640_I2S2_SDP:
351         case RT5640_I2S3_SDP:
352         case RT5640_ADDA_CLK1:
353         case RT5640_ADDA_CLK2:
354         case RT5640_DMIC:
355         case RT5640_GLB_CLK:
356         case RT5640_PLL_CTRL1:
357         case RT5640_PLL_CTRL2:
358         case RT5640_ASRC_1:
359         case RT5640_ASRC_2:
360         case RT5640_ASRC_3:
361         case RT5640_ASRC_4:
362         case RT5640_ASRC_5:
363         case RT5640_HP_OVCD:
364         case RT5640_CLS_D_OVCD:
365         case RT5640_CLS_D_OUT:
366         case RT5640_DEPOP_M1:
367         case RT5640_DEPOP_M2:
368         case RT5640_DEPOP_M3:
369         case RT5640_CHARGE_PUMP:
370         case RT5640_PV_DET_SPK_G:
371         case RT5640_MICBIAS:
372         case RT5640_EQ_CTRL1:
373         case RT5640_EQ_CTRL2:
374         case RT5640_WIND_FILTER:
375         case RT5640_DRC_AGC_1:
376         case RT5640_DRC_AGC_2:
377         case RT5640_DRC_AGC_3:
378         case RT5640_SVOL_ZC:
379         case RT5640_ANC_CTRL1:
380         case RT5640_ANC_CTRL2:
381         case RT5640_ANC_CTRL3:
382         case RT5640_JD_CTRL:
383         case RT5640_ANC_JD:
384         case RT5640_IRQ_CTRL1:
385         case RT5640_IRQ_CTRL2:
386         case RT5640_INT_IRQ_ST:
387         case RT5640_GPIO_CTRL1:
388         case RT5640_GPIO_CTRL2:
389         case RT5640_GPIO_CTRL3:
390         case RT5640_DSP_CTRL1:
391         case RT5640_DSP_CTRL2:
392         case RT5640_DSP_CTRL3:
393         case RT5640_DSP_CTRL4:
394         case RT5640_PGM_REG_ARR1:
395         case RT5640_PGM_REG_ARR2:
396         case RT5640_PGM_REG_ARR3:
397         case RT5640_PGM_REG_ARR4:
398         case RT5640_PGM_REG_ARR5:
399         case RT5640_SCB_FUNC:
400         case RT5640_SCB_CTRL:
401         case RT5640_BASE_BACK:
402         case RT5640_MP3_PLUS1:
403         case RT5640_MP3_PLUS2:
404         case RT5640_3D_HP:
405         case RT5640_ADJ_HPF:
406         case RT5640_HP_CALIB_AMP_DET:
407         case RT5640_HP_CALIB2:
408         case RT5640_SV_ZCD1:
409         case RT5640_SV_ZCD2:
410         case RT5640_DUMMY1:
411         case RT5640_DUMMY2:
412         case RT5640_DUMMY3:
413         case RT5640_VENDOR_ID:
414         case RT5640_VENDOR_ID1:
415         case RT5640_VENDOR_ID2:
416                 return 1;
417         default:
418                 return 0;
419         }
420 }
421
422 int rt5640_headset_detect(struct snd_soc_codec *codec, int jack_insert)
423 {
424         int jack_type;
425
426         if (jack_insert) {
427                 snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
428                         RT5640_PWR_LDO2, RT5640_PWR_LDO2);
429                 snd_soc_update_bits(codec, RT5640_PWR_ANLG2,
430                         RT5640_PWR_MB1, RT5640_PWR_MB1);
431                 snd_soc_update_bits(codec, RT5640_MICBIAS,
432                         RT5640_MIC1_OVCD_MASK | RT5640_MIC1_OVTH_MASK |
433                         RT5640_PWR_CLK25M_MASK | RT5640_PWR_MB_MASK,
434                         RT5640_MIC1_OVCD_EN | RT5640_MIC1_OVTH_600UA |
435                         RT5640_PWR_MB_PU | RT5640_PWR_CLK25M_PU);
436                 snd_soc_update_bits(codec, RT5640_DUMMY1,
437                         0x1, 0x1);
438                 msleep(50);
439                 if (snd_soc_read(codec, RT5640_IRQ_CTRL2) & 0x8)
440                         jack_type = RT5640_HEADPHO_DET;
441                 else
442                         jack_type = RT5640_HEADSET_DET;
443                 snd_soc_update_bits(codec, RT5640_IRQ_CTRL2,
444                         RT5640_MB1_OC_CLR, 0);
445         } else {
446                 snd_soc_update_bits(codec, RT5640_MICBIAS,
447                         RT5640_MIC1_OVCD_MASK,
448                         RT5640_MIC1_OVCD_DIS);
449
450                 jack_type = RT5640_NO_JACK;
451         }
452
453         return jack_type;
454 }
455 EXPORT_SYMBOL(rt5640_headset_detect);
456
457 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -4650, 150, 0);
458 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -65625, 375, 0);
459 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -3450, 150, 0);
460 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -17625, 375, 0);
461 static const DECLARE_TLV_DB_SCALE(adc_bst_tlv, 0, 1200, 0);
462
463 /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
464 static unsigned int bst_tlv[] = {
465         TLV_DB_RANGE_HEAD(7),
466         0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
467         1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
468         2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
469         3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
470         6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
471         7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
472         8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0),
473 };
474
475 static int rt5640_dmic_get(struct snd_kcontrol *kcontrol,
476                 struct snd_ctl_elem_value *ucontrol)
477 {
478         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
479         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
480
481         ucontrol->value.integer.value[0] = rt5640->dmic_en;
482
483         return 0;
484 }
485
486 static int rt5640_dmic_put(struct snd_kcontrol *kcontrol,
487                 struct snd_ctl_elem_value *ucontrol)
488 {
489         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
490         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
491
492         if (rt5640->dmic_en == ucontrol->value.integer.value[0])
493                 return 0;
494
495         rt5640->dmic_en = ucontrol->value.integer.value[0];
496         switch (rt5640->dmic_en) {
497         case RT5640_DMIC_DIS:
498                 snd_soc_update_bits(codec, RT5640_GPIO_CTRL1,
499                         RT5640_GP2_PIN_MASK | RT5640_GP3_PIN_MASK |
500                         RT5640_GP4_PIN_MASK,
501                         RT5640_GP2_PIN_GPIO2 | RT5640_GP3_PIN_GPIO3 |
502                         RT5640_GP4_PIN_GPIO4);
503                 snd_soc_update_bits(codec, RT5640_DMIC,
504                         RT5640_DMIC_1_DP_MASK | RT5640_DMIC_2_DP_MASK,
505                         RT5640_DMIC_1_DP_GPIO3 | RT5640_DMIC_2_DP_GPIO4);
506                 snd_soc_update_bits(codec, RT5640_DMIC,
507                         RT5640_DMIC_1_EN_MASK | RT5640_DMIC_2_EN_MASK,
508                         RT5640_DMIC_1_DIS | RT5640_DMIC_2_DIS);
509                 break;
510
511         case RT5640_DMIC1:
512                 snd_soc_update_bits(codec, RT5640_GPIO_CTRL1,
513                         RT5640_GP2_PIN_MASK | RT5640_GP3_PIN_MASK,
514                         RT5640_GP2_PIN_DMIC1_SCL | RT5640_GP3_PIN_DMIC1_SDA);
515                 snd_soc_update_bits(codec, RT5640_DMIC,
516                         RT5640_DMIC_1L_LH_MASK | RT5640_DMIC_1R_LH_MASK |
517                         RT5640_DMIC_1_DP_MASK,
518                         RT5640_DMIC_1L_LH_FALLING | RT5640_DMIC_1R_LH_RISING |
519                         RT5640_DMIC_1_DP_IN1P);
520                 snd_soc_update_bits(codec, RT5640_DMIC,
521                         RT5640_DMIC_1_EN_MASK, RT5640_DMIC_1_EN);
522                 break;
523
524         case RT5640_DMIC2:
525                 snd_soc_update_bits(codec, RT5640_GPIO_CTRL1,
526                         RT5640_GP2_PIN_MASK | RT5640_GP4_PIN_MASK,
527                         RT5640_GP2_PIN_DMIC1_SCL | RT5640_GP4_PIN_DMIC2_SDA);
528                 snd_soc_update_bits(codec, RT5640_DMIC,
529                         RT5640_DMIC_2L_LH_MASK | RT5640_DMIC_2R_LH_MASK |
530                         RT5640_DMIC_2_DP_MASK,
531                         RT5640_DMIC_2L_LH_FALLING | RT5640_DMIC_2R_LH_RISING |
532                         RT5640_DMIC_2_DP_IN1N);
533                 snd_soc_update_bits(codec, RT5640_DMIC,
534                         RT5640_DMIC_2_EN_MASK, RT5640_DMIC_2_EN);
535                 break;
536
537         default:
538                 return -EINVAL;
539         }
540
541         return 0;
542 }
543
544
545 /* IN1/IN2 Input Type */
546 static const char *rt5640_input_mode[] = {
547         "Single ended", "Differential"};
548
549 static const SOC_ENUM_SINGLE_DECL(
550         rt5640_in1_mode_enum, RT5640_IN1_IN2,
551         RT5640_IN_SFT1, rt5640_input_mode);
552
553 static const SOC_ENUM_SINGLE_DECL(
554         rt5640_in2_mode_enum, RT5640_IN3_IN4,
555         RT5640_IN_SFT2, rt5640_input_mode);
556
557 /* Interface data select */
558 static const char *rt5640_data_select[] = {
559         "Normal", "left copy to right", "right copy to left", "Swap"};
560
561 static const SOC_ENUM_SINGLE_DECL(rt5640_if1_dac_enum, RT5640_DIG_INF_DATA,
562                                 RT5640_IF1_DAC_SEL_SFT, rt5640_data_select);
563
564 static const SOC_ENUM_SINGLE_DECL(rt5640_if1_adc_enum, RT5640_DIG_INF_DATA,
565                                 RT5640_IF1_ADC_SEL_SFT, rt5640_data_select);
566
567 static const SOC_ENUM_SINGLE_DECL(rt5640_if2_dac_enum, RT5640_DIG_INF_DATA,
568                                 RT5640_IF2_DAC_SEL_SFT, rt5640_data_select);
569
570 static const SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_enum, RT5640_DIG_INF_DATA,
571                                 RT5640_IF2_ADC_SEL_SFT, rt5640_data_select);
572
573 static const SOC_ENUM_SINGLE_DECL(rt5640_if3_dac_enum, RT5640_DIG_INF_DATA,
574                                 RT5640_IF3_DAC_SEL_SFT, rt5640_data_select);
575
576 static const SOC_ENUM_SINGLE_DECL(rt5640_if3_adc_enum, RT5640_DIG_INF_DATA,
577                                 RT5640_IF3_ADC_SEL_SFT, rt5640_data_select);
578
579 /* Class D speaker gain ratio */
580 static const char *rt5640_clsd_spk_ratio[] = {"1.66x", "1.83x", "1.94x", "2x",
581         "2.11x", "2.22x", "2.33x", "2.44x", "2.55x", "2.66x", "2.77x"};
582
583 static const SOC_ENUM_SINGLE_DECL(
584         rt5640_clsd_spk_ratio_enum, RT5640_CLS_D_OUT,
585         RT5640_CLSD_RATIO_SFT, rt5640_clsd_spk_ratio);
586
587 /* DMIC */
588 static const char *rt5640_dmic_mode[] = {"Disable", "DMIC1", "DMIC2"};
589
590 static const SOC_ENUM_SINGLE_DECL(rt5640_dmic_enum, 0, 0, rt5640_dmic_mode);
591
592
593
594 #ifdef RT5640_REG_RW
595 #define REGVAL_MAX 0xffff
596 static unsigned int regctl_addr;
597 static int rt5640_regctl_info(struct snd_kcontrol *kcontrol,
598                         struct snd_ctl_elem_info *uinfo) {
599         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
600         uinfo->count = 2;
601         uinfo->value.integer.min = 0;
602         uinfo->value.integer.max = REGVAL_MAX;
603         return 0;
604 }
605
606 static int rt5640_regctl_get(struct snd_kcontrol *kcontrol,
607                         struct snd_ctl_elem_value *ucontrol)
608 {
609         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
610         ucontrol->value.integer.value[0] = regctl_addr;
611         ucontrol->value.integer.value[1] = snd_soc_read(codec, regctl_addr);
612         return 0;
613 }
614
615 static int rt5640_regctl_put(struct snd_kcontrol *kcontrol,
616                         struct snd_ctl_elem_value *ucontrol)
617 {
618         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
619         regctl_addr = ucontrol->value.integer.value[0];
620         if (ucontrol->value.integer.value[1] <= REGVAL_MAX)
621                 snd_soc_write(codec, regctl_addr,
622                 ucontrol->value.integer.value[1]);
623         return 0;
624 }
625 #endif
626
627
628 #define VOL_RESCALE_MAX_VOL 0x27 /* 39 */
629 #define VOL_RESCALE_MIX_RANGE 0x1F /* 31 */
630
631 static int rt5640_vol_rescale_get(struct snd_kcontrol *kcontrol,
632                 struct snd_ctl_elem_value *ucontrol)
633 {
634         struct soc_mixer_control *mc =
635                 (struct soc_mixer_control *)kcontrol->private_value;
636         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
637         unsigned int val = snd_soc_read(codec, mc->reg);
638
639         ucontrol->value.integer.value[0] = VOL_RESCALE_MAX_VOL -
640                 ((val & RT5640_L_VOL_MASK) >> mc->shift);
641         ucontrol->value.integer.value[1] = VOL_RESCALE_MAX_VOL -
642                 (val & RT5640_R_VOL_MASK);
643
644         return 0;
645 }
646
647 static int rt5640_vol_rescale_put(struct snd_kcontrol *kcontrol,
648                 struct snd_ctl_elem_value *ucontrol)
649 {
650         struct soc_mixer_control *mc =
651                 (struct soc_mixer_control *)kcontrol->private_value;
652         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
653         unsigned int val, val2;
654
655         val = VOL_RESCALE_MAX_VOL - ucontrol->value.integer.value[0];
656         val2 = VOL_RESCALE_MAX_VOL - ucontrol->value.integer.value[1];
657         return snd_soc_update_bits_locked(codec, mc->reg, RT5640_L_VOL_MASK |
658                         RT5640_R_VOL_MASK, val << mc->shift | val2);
659 }
660
661
662 static const struct snd_kcontrol_new rt5640_snd_controls[] = {
663         /* Speaker Output Volume */
664         SOC_DOUBLE("Speaker Playback Switch", RT5640_SPK_VOL,
665                 RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
666
667         SOC_DOUBLE_EXT_TLV("Speaker Playback Volume", RT5640_SPK_VOL,
668                 RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, VOL_RESCALE_MIX_RANGE, 0,
669                 rt5640_vol_rescale_get, rt5640_vol_rescale_put, out_vol_tlv),
670
671         /* Headphone Output Volume */
672         SOC_DOUBLE("HP Playback Switch", RT5640_HP_VOL,
673                 RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
674
675         SOC_DOUBLE_EXT_TLV("HP Playback Volume", RT5640_HP_VOL,
676                 RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, VOL_RESCALE_MIX_RANGE, 0,
677                 rt5640_vol_rescale_get, rt5640_vol_rescale_put, out_vol_tlv),
678
679         /* OUTPUT Control */
680         SOC_DOUBLE("OUT Playback Switch", RT5640_OUTPUT,
681                 RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
682         SOC_DOUBLE("OUT Channel Switch", RT5640_OUTPUT,
683                 RT5640_VOL_L_SFT, RT5640_VOL_R_SFT, 1, 1),
684         SOC_DOUBLE_TLV("OUT Playback Volume", RT5640_OUTPUT,
685                 RT5640_L_VOL_SFT, RT5640_R_VOL_SFT, 39, 1, out_vol_tlv),
686         /* MONO Output Control */
687         SOC_SINGLE("Mono Playback Switch", RT5640_MONO_OUT,
688                                 RT5640_L_MUTE_SFT, 1, 1),
689         /* DAC Digital Volume */
690         SOC_DOUBLE("DAC2 Playback Switch", RT5640_DAC2_CTRL,
691                 RT5640_M_DAC_L2_VOL_SFT, RT5640_M_DAC_R2_VOL_SFT, 1, 1),
692         SOC_DOUBLE_TLV("DAC1 Playback Volume", RT5640_DAC1_DIG_VOL,
693                         RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
694                         175, 0, dac_vol_tlv),
695         SOC_DOUBLE_TLV("Mono DAC Playback Volume", RT5640_DAC2_DIG_VOL,
696                         RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
697                         175, 0, dac_vol_tlv),
698         /* IN1/IN2 Control */
699         SOC_ENUM("IN1 Mode Control",  rt5640_in1_mode_enum),
700         SOC_SINGLE_TLV("IN1 Boost", RT5640_IN1_IN2,
701                 RT5640_BST_SFT1, 8, 0, bst_tlv),
702         SOC_ENUM("IN2 Mode Control", rt5640_in2_mode_enum),
703         SOC_SINGLE_TLV("IN2 Boost", RT5640_IN3_IN4,
704                 RT5640_BST_SFT2, 8, 0, bst_tlv),
705         /* INL/INR Volume Control */
706         SOC_DOUBLE_TLV("IN Capture Volume", RT5640_INL_INR_VOL,
707                         RT5640_INL_VOL_SFT, RT5640_INR_VOL_SFT,
708                         31, 1, in_vol_tlv),
709         /* ADC Digital Volume Control */
710         SOC_DOUBLE("ADC Capture Switch", RT5640_ADC_DIG_VOL,
711                 RT5640_L_MUTE_SFT, RT5640_R_MUTE_SFT, 1, 1),
712         SOC_DOUBLE_TLV("ADC Capture Volume", RT5640_ADC_DIG_VOL,
713                         RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
714                         127, 0, adc_vol_tlv),
715         SOC_DOUBLE_TLV("Mono ADC Capture Volume", RT5640_ADC_DATA,
716                         RT5640_L_VOL_SFT, RT5640_R_VOL_SFT,
717                         127, 0, adc_vol_tlv),
718         /* ADC Boost Volume Control */
719         SOC_DOUBLE_TLV("ADC Boost Gain", RT5640_ADC_BST_VOL,
720                         RT5640_ADC_L_BST_SFT, RT5640_ADC_R_BST_SFT,
721                         3, 0, adc_bst_tlv),
722         /* Class D speaker gain ratio */
723         SOC_ENUM("Class D SPK Ratio Control", rt5640_clsd_spk_ratio_enum),
724         /* DMIC */
725         SOC_ENUM_EXT("DMIC Switch", rt5640_dmic_enum,
726                 rt5640_dmic_get, rt5640_dmic_put),
727
728 #ifdef RT5640_REG_RW
729         {
730                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
731                 .name = "Register Control",
732                 .info = rt5640_regctl_info,
733                 .get = rt5640_regctl_get,
734                 .put = rt5640_regctl_put,
735         },
736 #endif
737 };
738
739 /**
740  * set_dmic_clk - Set parameter of dmic.
741  *
742  * @w: DAPM widget.
743  * @kcontrol: The kcontrol of this widget.
744  * @event: Event id.
745  *
746  * Choose dmic clock between 1MHz and 3MHz.
747  * It is better for clock to approximate 3MHz.
748  */
749 static int set_dmic_clk(struct snd_soc_dapm_widget *w,
750         struct snd_kcontrol *kcontrol, int event)
751 {
752         struct snd_soc_codec *codec = w->codec;
753         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
754         int div[] = {2, 3, 4, 6, 12}, idx = -EINVAL, i, rate, red, bound, temp;
755
756         rate = rt5640->lrck[rt5640->aif_pu] << 8;
757         red = 3000000 * 12;
758         for (i = 0; i < ARRAY_SIZE(div); i++) {
759                 bound = div[i] * 3000000;
760                 if (rate > bound)
761                         continue;
762                 temp = bound - rate;
763                 if (temp < red) {
764                         red = temp;
765                         idx = i;
766                 }
767         }
768         if (idx < 0)
769                 dev_err(codec->dev, "Failed to set DMIC clock\n");
770         else
771                 snd_soc_update_bits(codec, RT5640_DMIC, RT5640_DMIC_CLK_MASK,
772                                         idx << RT5640_DMIC_CLK_SFT);
773         return idx;
774 }
775
776 static int check_sysclk1_source(struct snd_soc_dapm_widget *source,
777                          struct snd_soc_dapm_widget *sink)
778 {
779         unsigned int val;
780
781         val = snd_soc_read(source->codec, RT5640_GLB_CLK);
782         val &= RT5640_SCLK_SRC_MASK;
783         if (val == RT5640_SCLK_SRC_PLL1 || val == RT5640_SCLK_SRC_PLL1T)
784                 return 1;
785         else
786                 return 0;
787 }
788
789 /* Digital Mixer */
790 static const struct snd_kcontrol_new rt5640_sto_adc_l_mix[] = {
791         SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
792                         RT5640_M_ADC_L1_SFT, 1, 1),
793         SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
794                         RT5640_M_ADC_L2_SFT, 1, 1),
795 };
796
797 static const struct snd_kcontrol_new rt5640_sto_adc_r_mix[] = {
798         SOC_DAPM_SINGLE("ADC1 Switch", RT5640_STO_ADC_MIXER,
799                         RT5640_M_ADC_R1_SFT, 1, 1),
800         SOC_DAPM_SINGLE("ADC2 Switch", RT5640_STO_ADC_MIXER,
801                         RT5640_M_ADC_R2_SFT, 1, 1),
802 };
803
804 static const struct snd_kcontrol_new rt5640_mono_adc_l_mix[] = {
805         SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
806                         RT5640_M_MONO_ADC_L1_SFT, 1, 1),
807         SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
808                         RT5640_M_MONO_ADC_L2_SFT, 1, 1),
809 };
810
811 static const struct snd_kcontrol_new rt5640_mono_adc_r_mix[] = {
812         SOC_DAPM_SINGLE("ADC1 Switch", RT5640_MONO_ADC_MIXER,
813                         RT5640_M_MONO_ADC_R1_SFT, 1, 1),
814         SOC_DAPM_SINGLE("ADC2 Switch", RT5640_MONO_ADC_MIXER,
815                         RT5640_M_MONO_ADC_R2_SFT, 1, 1),
816 };
817
818 static const struct snd_kcontrol_new rt5640_dac_l_mix[] = {
819         SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
820                         RT5640_M_ADCMIX_L_SFT, 1, 1),
821         SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
822                         RT5640_M_IF1_DAC_L_SFT, 1, 1),
823 };
824
825 static const struct snd_kcontrol_new rt5640_dac_r_mix[] = {
826         SOC_DAPM_SINGLE("Stereo ADC Switch", RT5640_AD_DA_MIXER,
827                         RT5640_M_ADCMIX_R_SFT, 1, 1),
828         SOC_DAPM_SINGLE("INF1 Switch", RT5640_AD_DA_MIXER,
829                         RT5640_M_IF1_DAC_R_SFT, 1, 1),
830 };
831
832 static const struct snd_kcontrol_new rt5640_sto_dac_l_mix[] = {
833         SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_STO_DAC_MIXER,
834                         RT5640_M_DAC_L1_SFT, 1, 1),
835         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_STO_DAC_MIXER,
836                         RT5640_M_DAC_L2_SFT, 1, 1),
837         SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
838                         RT5640_M_ANC_DAC_L_SFT, 1, 1),
839 };
840
841 static const struct snd_kcontrol_new rt5640_sto_dac_r_mix[] = {
842         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_STO_DAC_MIXER,
843                         RT5640_M_DAC_R1_SFT, 1, 1),
844         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_STO_DAC_MIXER,
845                         RT5640_M_DAC_R2_SFT, 1, 1),
846         SOC_DAPM_SINGLE("ANC Switch", RT5640_STO_DAC_MIXER,
847                         RT5640_M_ANC_DAC_R_SFT, 1, 1),
848 };
849
850 static const struct snd_kcontrol_new rt5640_mono_dac_l_mix[] = {
851         SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_MONO_DAC_MIXER,
852                         RT5640_M_DAC_L1_MONO_L_SFT, 1, 1),
853         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
854                         RT5640_M_DAC_L2_MONO_L_SFT, 1, 1),
855         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
856                         RT5640_M_DAC_R2_MONO_L_SFT, 1, 1),
857 };
858
859 static const struct snd_kcontrol_new rt5640_mono_dac_r_mix[] = {
860         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_MONO_DAC_MIXER,
861                         RT5640_M_DAC_R1_MONO_R_SFT, 1, 1),
862         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_DAC_MIXER,
863                         RT5640_M_DAC_R2_MONO_R_SFT, 1, 1),
864         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_DAC_MIXER,
865                         RT5640_M_DAC_L2_MONO_R_SFT, 1, 1),
866 };
867
868 static const struct snd_kcontrol_new rt5640_dig_l_mix[] = {
869         SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_DIG_MIXER,
870                         RT5640_M_STO_L_DAC_L_SFT, 1, 1),
871         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_DIG_MIXER,
872                         RT5640_M_DAC_L2_DAC_L_SFT, 1, 1),
873 };
874
875 static const struct snd_kcontrol_new rt5640_dig_r_mix[] = {
876         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_DIG_MIXER,
877                         RT5640_M_STO_R_DAC_R_SFT, 1, 1),
878         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_DIG_MIXER,
879                         RT5640_M_DAC_R2_DAC_R_SFT, 1, 1),
880 };
881
882 /* Analog Input Mixer */
883 static const struct snd_kcontrol_new rt5640_rec_l_mix[] = {
884         SOC_DAPM_SINGLE("HPOL Switch", RT5640_REC_L2_MIXER,
885                         RT5640_M_HP_L_RM_L_SFT, 1, 1),
886         SOC_DAPM_SINGLE("INL Switch", RT5640_REC_L2_MIXER,
887                         RT5640_M_IN_L_RM_L_SFT, 1, 1),
888         SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_L2_MIXER,
889                         RT5640_M_BST4_RM_L_SFT, 1, 1),
890         SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_L2_MIXER,
891                         RT5640_M_BST1_RM_L_SFT, 1, 1),
892         SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_REC_L2_MIXER,
893                         RT5640_M_OM_L_RM_L_SFT, 1, 1),
894 };
895
896 static const struct snd_kcontrol_new rt5640_rec_r_mix[] = {
897         SOC_DAPM_SINGLE("HPOR Switch", RT5640_REC_R2_MIXER,
898                         RT5640_M_HP_R_RM_R_SFT, 1, 1),
899         SOC_DAPM_SINGLE("INR Switch", RT5640_REC_R2_MIXER,
900                         RT5640_M_IN_R_RM_R_SFT, 1, 1),
901         SOC_DAPM_SINGLE("BST2 Switch", RT5640_REC_R2_MIXER,
902                         RT5640_M_BST4_RM_R_SFT, 1, 1),
903         SOC_DAPM_SINGLE("BST1 Switch", RT5640_REC_R2_MIXER,
904                         RT5640_M_BST1_RM_R_SFT, 1, 1),
905         SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_REC_R2_MIXER,
906                         RT5640_M_OM_R_RM_R_SFT, 1, 1),
907 };
908
909 /* Analog Output Mixer */
910 static const struct snd_kcontrol_new rt5640_spk_l_mix[] = {
911         SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_SPK_L_MIXER,
912                         RT5640_M_RM_L_SM_L_SFT, 1, 1),
913         SOC_DAPM_SINGLE("INL Switch", RT5640_SPK_L_MIXER,
914                         RT5640_M_IN_L_SM_L_SFT, 1, 1),
915         SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPK_L_MIXER,
916                         RT5640_M_DAC_L1_SM_L_SFT, 1, 1),
917         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_SPK_L_MIXER,
918                         RT5640_M_DAC_L2_SM_L_SFT, 1, 1),
919         SOC_DAPM_SINGLE("OUT MIXL Switch", RT5640_SPK_L_MIXER,
920                         RT5640_M_OM_L_SM_L_SFT, 1, 1),
921 };
922
923 static const struct snd_kcontrol_new rt5640_spk_r_mix[] = {
924         SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_SPK_R_MIXER,
925                         RT5640_M_RM_R_SM_R_SFT, 1, 1),
926         SOC_DAPM_SINGLE("INR Switch", RT5640_SPK_R_MIXER,
927                         RT5640_M_IN_R_SM_R_SFT, 1, 1),
928         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPK_R_MIXER,
929                         RT5640_M_DAC_R1_SM_R_SFT, 1, 1),
930         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_SPK_R_MIXER,
931                         RT5640_M_DAC_R2_SM_R_SFT, 1, 1),
932         SOC_DAPM_SINGLE("OUT MIXR Switch", RT5640_SPK_R_MIXER,
933                         RT5640_M_OM_R_SM_R_SFT, 1, 1),
934 };
935
936 static const struct snd_kcontrol_new rt5640_out_l_mix[] = {
937         SOC_DAPM_SINGLE("SPK MIXL Switch", RT5640_OUT_L3_MIXER,
938                         RT5640_M_SM_L_OM_L_SFT, 1, 1),
939         SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_L3_MIXER,
940                         RT5640_M_BST1_OM_L_SFT, 1, 1),
941         SOC_DAPM_SINGLE("INL Switch", RT5640_OUT_L3_MIXER,
942                         RT5640_M_IN_L_OM_L_SFT, 1, 1),
943         SOC_DAPM_SINGLE("REC MIXL Switch", RT5640_OUT_L3_MIXER,
944                         RT5640_M_RM_L_OM_L_SFT, 1, 1),
945         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_L3_MIXER,
946                         RT5640_M_DAC_R2_OM_L_SFT, 1, 1),
947         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_L3_MIXER,
948                         RT5640_M_DAC_L2_OM_L_SFT, 1, 1),
949         SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_OUT_L3_MIXER,
950                         RT5640_M_DAC_L1_OM_L_SFT, 1, 1),
951 };
952
953 static const struct snd_kcontrol_new rt5640_out_r_mix[] = {
954         SOC_DAPM_SINGLE("SPK MIXR Switch", RT5640_OUT_R3_MIXER,
955                         RT5640_M_SM_L_OM_R_SFT, 1, 1),
956         SOC_DAPM_SINGLE("BST2 Switch", RT5640_OUT_R3_MIXER,
957                         RT5640_M_BST4_OM_R_SFT, 1, 1),
958         SOC_DAPM_SINGLE("BST1 Switch", RT5640_OUT_R3_MIXER,
959                         RT5640_M_BST1_OM_R_SFT, 1, 1),
960         SOC_DAPM_SINGLE("INR Switch", RT5640_OUT_R3_MIXER,
961                         RT5640_M_IN_R_OM_R_SFT, 1, 1),
962         SOC_DAPM_SINGLE("REC MIXR Switch", RT5640_OUT_R3_MIXER,
963                         RT5640_M_RM_R_OM_R_SFT, 1, 1),
964         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_OUT_R3_MIXER,
965                         RT5640_M_DAC_L2_OM_R_SFT, 1, 1),
966         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_OUT_R3_MIXER,
967                         RT5640_M_DAC_R2_OM_R_SFT, 1, 1),
968         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_OUT_R3_MIXER,
969                         RT5640_M_DAC_R1_OM_R_SFT, 1, 1),
970 };
971
972 static const struct snd_kcontrol_new rt5640_spo_l_mix[] = {
973         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_L_MIXER,
974                         RT5640_M_DAC_R1_SPM_L_SFT, 1, 1),
975         SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_SPO_L_MIXER,
976                         RT5640_M_DAC_L1_SPM_L_SFT, 1, 1),
977         SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_L_MIXER,
978                         RT5640_M_SV_R_SPM_L_SFT, 1, 1),
979         SOC_DAPM_SINGLE("SPKVOL L Switch", RT5640_SPO_L_MIXER,
980                         RT5640_M_SV_L_SPM_L_SFT, 1, 1),
981         SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_L_MIXER,
982                         RT5640_M_BST1_SPM_L_SFT, 1, 1),
983 };
984
985 static const struct snd_kcontrol_new rt5640_spo_r_mix[] = {
986         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_SPO_R_MIXER,
987                         RT5640_M_DAC_R1_SPM_R_SFT, 1, 1),
988         SOC_DAPM_SINGLE("SPKVOL R Switch", RT5640_SPO_R_MIXER,
989                         RT5640_M_SV_R_SPM_R_SFT, 1, 1),
990         SOC_DAPM_SINGLE("BST1 Switch", RT5640_SPO_R_MIXER,
991                         RT5640_M_BST1_SPM_R_SFT, 1, 1),
992 };
993
994 static const struct snd_kcontrol_new rt5640_hpo_mix[] = {
995         SOC_DAPM_SINGLE("DAC2 Switch", RT5640_HPO_MIXER,
996                         RT5640_M_DAC2_HM_SFT, 1, 1),
997         SOC_DAPM_SINGLE("DAC1 Switch", RT5640_HPO_MIXER,
998                         RT5640_M_DAC1_HM_SFT, 1, 1),
999         SOC_DAPM_SINGLE("HPVOL Switch", RT5640_HPO_MIXER,
1000                         RT5640_M_HPVOL_HM_SFT, 1, 1),
1001 };
1002
1003 static const struct snd_kcontrol_new rt5640_lout_mix[] = {
1004         SOC_DAPM_SINGLE("DAC L1 Switch", RT5640_LOUT_MIXER,
1005                         RT5640_M_DAC_L1_LM_SFT, 1, 1),
1006         SOC_DAPM_SINGLE("DAC R1 Switch", RT5640_LOUT_MIXER,
1007                         RT5640_M_DAC_R1_LM_SFT, 1, 1),
1008         SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_LOUT_MIXER,
1009                         RT5640_M_OV_L_LM_SFT, 1, 1),
1010         SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_LOUT_MIXER,
1011                         RT5640_M_OV_R_LM_SFT, 1, 1),
1012 };
1013
1014 static const struct snd_kcontrol_new rt5640_mono_mix[] = {
1015         SOC_DAPM_SINGLE("DAC R2 Switch", RT5640_MONO_MIXER,
1016                         RT5640_M_DAC_R2_MM_SFT, 1, 1),
1017         SOC_DAPM_SINGLE("DAC L2 Switch", RT5640_MONO_MIXER,
1018                         RT5640_M_DAC_L2_MM_SFT, 1, 1),
1019         SOC_DAPM_SINGLE("OUTVOL R Switch", RT5640_MONO_MIXER,
1020                         RT5640_M_OV_R_MM_SFT, 1, 1),
1021         SOC_DAPM_SINGLE("OUTVOL L Switch", RT5640_MONO_MIXER,
1022                         RT5640_M_OV_L_MM_SFT, 1, 1),
1023         SOC_DAPM_SINGLE("BST1 Switch", RT5640_MONO_MIXER,
1024                         RT5640_M_BST1_MM_SFT, 1, 1),
1025 };
1026
1027 /* INL/R source */
1028 static const char *rt5640_inl_src[] = {"IN2P", "MonoP"};
1029
1030 static const SOC_ENUM_SINGLE_DECL(
1031         rt5640_inl_enum, RT5640_INL_INR_VOL,
1032         RT5640_INL_SEL_SFT, rt5640_inl_src);
1033
1034 static const struct snd_kcontrol_new rt5640_inl_mux =
1035         SOC_DAPM_ENUM("INL source", rt5640_inl_enum);
1036
1037 static const char *rt5640_inr_src[] = {"IN2N", "MonoN"};
1038
1039 static const SOC_ENUM_SINGLE_DECL(
1040         rt5640_inr_enum, RT5640_INL_INR_VOL,
1041         RT5640_INR_SEL_SFT, rt5640_inr_src);
1042
1043 static const struct snd_kcontrol_new rt5640_inr_mux =
1044         SOC_DAPM_ENUM("INR source", rt5640_inr_enum);
1045
1046 /* Stereo ADC source */
1047 static const char *rt5640_stereo_adc1_src[] = {"DIG MIX", "ADC"};
1048
1049 static const SOC_ENUM_SINGLE_DECL(
1050         rt5640_stereo_adc1_enum, RT5640_STO_ADC_MIXER,
1051         RT5640_ADC_1_SRC_SFT, rt5640_stereo_adc1_src);
1052
1053 static const struct snd_kcontrol_new rt5640_sto_adc_l1_mux =
1054         SOC_DAPM_ENUM("Stereo ADC L1 source", rt5640_stereo_adc1_enum);
1055
1056 static const struct snd_kcontrol_new rt5640_sto_adc_r1_mux =
1057         SOC_DAPM_ENUM("Stereo ADC R1 source", rt5640_stereo_adc1_enum);
1058
1059 static const char *rt5640_stereo_adc2_src[] = {"DMIC1", "DMIC2", "DIG MIX"};
1060
1061 static const SOC_ENUM_SINGLE_DECL(
1062         rt5640_stereo_adc2_enum, RT5640_STO_ADC_MIXER,
1063         RT5640_ADC_2_SRC_SFT, rt5640_stereo_adc2_src);
1064
1065 static const struct snd_kcontrol_new rt5640_sto_adc_l2_mux =
1066         SOC_DAPM_ENUM("Stereo ADC L2 source", rt5640_stereo_adc2_enum);
1067
1068 static const struct snd_kcontrol_new rt5640_sto_adc_r2_mux =
1069         SOC_DAPM_ENUM("Stereo ADC R2 source", rt5640_stereo_adc2_enum);
1070
1071 /* Mono ADC source */
1072 static const char *rt5640_mono_adc_l1_src[] = {"Mono DAC MIXL", "ADCL"};
1073
1074 static const SOC_ENUM_SINGLE_DECL(
1075         rt5640_mono_adc_l1_enum, RT5640_MONO_ADC_MIXER,
1076         RT5640_MONO_ADC_L1_SRC_SFT, rt5640_mono_adc_l1_src);
1077
1078 static const struct snd_kcontrol_new rt5640_mono_adc_l1_mux =
1079         SOC_DAPM_ENUM("Mono ADC1 left source", rt5640_mono_adc_l1_enum);
1080
1081 static const char *rt5640_mono_adc_l2_src[] = {
1082         "DMIC L1", "DMIC L2", "Mono DAC MIXL"
1083 };
1084
1085 static const SOC_ENUM_SINGLE_DECL(
1086         rt5640_mono_adc_l2_enum, RT5640_MONO_ADC_MIXER,
1087         RT5640_MONO_ADC_L2_SRC_SFT, rt5640_mono_adc_l2_src);
1088
1089 static const struct snd_kcontrol_new rt5640_mono_adc_l2_mux =
1090         SOC_DAPM_ENUM("Mono ADC2 left source", rt5640_mono_adc_l2_enum);
1091
1092 static const char *rt5640_mono_adc_r1_src[] = {"Mono DAC MIXR", "ADCR"};
1093
1094 static const SOC_ENUM_SINGLE_DECL(
1095         rt5640_mono_adc_r1_enum, RT5640_MONO_ADC_MIXER,
1096         RT5640_MONO_ADC_R1_SRC_SFT, rt5640_mono_adc_r1_src);
1097
1098 static const struct snd_kcontrol_new rt5640_mono_adc_r1_mux =
1099         SOC_DAPM_ENUM("Mono ADC1 right source", rt5640_mono_adc_r1_enum);
1100
1101 static const char *rt5640_mono_adc_r2_src[] = {
1102         "DMIC R1", "DMIC R2", "Mono DAC MIXR"
1103 };
1104
1105 static const SOC_ENUM_SINGLE_DECL(
1106         rt5640_mono_adc_r2_enum, RT5640_MONO_ADC_MIXER,
1107         RT5640_MONO_ADC_R2_SRC_SFT, rt5640_mono_adc_r2_src);
1108
1109 static const struct snd_kcontrol_new rt5640_mono_adc_r2_mux =
1110         SOC_DAPM_ENUM("Mono ADC2 right source", rt5640_mono_adc_r2_enum);
1111
1112 /* DAC2 channel source */
1113 static const char *rt5640_dac_l2_src[] = {"IF2", "IF3", "TxDC", "Base L/R"};
1114
1115 static const SOC_ENUM_SINGLE_DECL(rt5640_dac_l2_enum, RT5640_DSP_PATH2,
1116                                 RT5640_DAC_L2_SEL_SFT, rt5640_dac_l2_src);
1117
1118 static const struct snd_kcontrol_new rt5640_dac_l2_mux =
1119         SOC_DAPM_ENUM("DAC2 left channel source", rt5640_dac_l2_enum);
1120
1121 static const char *rt5640_dac_r2_src[] = {"IF2", "IF3", "TxDC"};
1122
1123 static const SOC_ENUM_SINGLE_DECL(
1124         rt5640_dac_r2_enum, RT5640_DSP_PATH2,
1125         RT5640_DAC_R2_SEL_SFT, rt5640_dac_r2_src);
1126
1127 static const struct snd_kcontrol_new rt5640_dac_r2_mux =
1128         SOC_DAPM_ENUM("DAC2 right channel source", rt5640_dac_r2_enum);
1129
1130 /* Interface 2  ADC channel source */
1131 static const char *rt5640_if2_adc_l_src[] = {"TxDP", "Mono ADC MIXL"};
1132
1133 static const SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_l_enum, RT5640_DSP_PATH2,
1134                         RT5640_IF2_ADC_L_SEL_SFT, rt5640_if2_adc_l_src);
1135
1136 static const struct snd_kcontrol_new rt5640_if2_adc_l_mux =
1137         SOC_DAPM_ENUM("IF2 ADC left channel source", rt5640_if2_adc_l_enum);
1138
1139 static const char *rt5640_if2_adc_r_src[] = {"TxDP", "Mono ADC MIXR"};
1140
1141 static const SOC_ENUM_SINGLE_DECL(rt5640_if2_adc_r_enum, RT5640_DSP_PATH2,
1142                         RT5640_IF2_ADC_R_SEL_SFT, rt5640_if2_adc_r_src);
1143
1144 static const struct snd_kcontrol_new rt5640_if2_adc_r_mux =
1145         SOC_DAPM_ENUM("IF2 ADC right channel source", rt5640_if2_adc_r_enum);
1146
1147 /* digital interface and iis interface map */
1148 static const char *rt5640_dai_iis_map[] = {"1:1|2:2|3:3", "1:1|2:3|3:2",
1149         "1:3|2:1|3:2", "1:3|2:2|3:1", "1:2|2:3|3:1",
1150         "1:2|2:1|3:3", "1:1|2:1|3:3", "1:2|2:2|3:3"};
1151
1152 static const SOC_ENUM_SINGLE_DECL(
1153         rt5640_dai_iis_map_enum, RT5640_I2S1_SDP,
1154         RT5640_I2S_IF_SFT, rt5640_dai_iis_map);
1155
1156 static const struct snd_kcontrol_new rt5640_dai_mux =
1157         SOC_DAPM_ENUM("DAI select", rt5640_dai_iis_map_enum);
1158
1159 /* SDI select */
1160 static const char *rt5640_sdi_sel[] = {"IF1", "IF2"};
1161
1162 static const SOC_ENUM_SINGLE_DECL(
1163         rt5640_sdi_sel_enum, RT5640_I2S2_SDP,
1164         RT5640_I2S2_SDI_SFT, rt5640_sdi_sel);
1165
1166 static const struct snd_kcontrol_new rt5640_sdi_mux =
1167         SOC_DAPM_ENUM("SDI select", rt5640_sdi_sel_enum);
1168
1169 static int spk_event(struct snd_soc_dapm_widget *w,
1170         struct snd_kcontrol *kcontrol, int event)
1171 {
1172         struct snd_soc_codec *codec = w->codec;
1173         static unsigned int spkl_out_enable;
1174         static unsigned int spkr_out_enable;
1175
1176         switch (event) {
1177         case SND_SOC_DAPM_POST_PMU:
1178                 pr_info("spk_event --SND_SOC_DAPM_POST_PMU\n");
1179                 snd_soc_update_bits(codec, RT5640_PWR_DIG1, 0x0001, 0x0001);
1180                 rt5640_index_update_bits(codec, 0x1c, 0xf000, 0xf000);
1181                 /* rt5640_index_write(codec, 0x1c, 0xfd21); */
1182                 break;
1183
1184         case SND_SOC_DAPM_PRE_PMD:
1185                 pr_info("spk_event --SND_SOC_DAPM_POST_PMD\n");
1186                 /* rt5640_index_write(codec, 0x1c, 0xfd00); */
1187                 rt5640_index_update_bits(codec, 0x1c, 0xf000, 0x0000);
1188                 snd_soc_update_bits(codec, RT5640_PWR_DIG1, 0x0001, 0x0000);
1189                 break;
1190
1191         default:
1192                 return 0;
1193         }
1194         return 0;
1195 }
1196
1197 static int hp_event(struct snd_soc_dapm_widget *w,
1198         struct snd_kcontrol *kcontrol, int event)
1199 {
1200         struct snd_soc_codec *codec = w->codec;
1201         static unsigned int hp_out_enable;
1202
1203         switch (event) {
1204         case SND_SOC_DAPM_POST_PMU:
1205                 pr_info("hp_event --SND_SOC_DAPM_POST_PMU\n");
1206                 break;
1207
1208         case SND_SOC_DAPM_PRE_PMD:
1209                 pr_info("hp_event --SND_SOC_DAPM_POST_PMD\n");
1210                 break;
1211
1212         default:
1213                 return 0;
1214         }
1215         return 0;
1216 }
1217
1218 static const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
1219         SND_SOC_DAPM_SUPPLY("PLL1", RT5640_PWR_ANLG2,
1220                         RT5640_PWR_PLL_BIT, 0, NULL, 0),
1221         /* Input Side */
1222         /* micbias */
1223         SND_SOC_DAPM_SUPPLY("LDO2", RT5640_PWR_ANLG1,
1224                         RT5640_PWR_LDO2_BIT, 0, NULL, 0),
1225         SND_SOC_DAPM_MICBIAS("micbias1", RT5640_PWR_ANLG2,
1226                         RT5640_PWR_MB1_BIT, 0),
1227         SND_SOC_DAPM_MICBIAS("micbias2", RT5640_PWR_ANLG2,
1228                         RT5640_PWR_MB2_BIT, 0),
1229         /* Input Lines */
1230
1231         SND_SOC_DAPM_INPUT("MIC1"),
1232         SND_SOC_DAPM_INPUT("MIC2"),
1233         SND_SOC_DAPM_INPUT("DMIC1"),
1234         SND_SOC_DAPM_INPUT("DMIC2"),
1235         SND_SOC_DAPM_INPUT("IN1P"),
1236         SND_SOC_DAPM_INPUT("IN1N"),
1237         SND_SOC_DAPM_INPUT("IN2P"),
1238         SND_SOC_DAPM_INPUT("IN2N"),
1239         SND_SOC_DAPM_INPUT("DMIC L1"),
1240         SND_SOC_DAPM_INPUT("DMIC R1"),
1241         SND_SOC_DAPM_INPUT("DMIC L2"),
1242         SND_SOC_DAPM_INPUT("DMIC R2"),
1243         SND_SOC_DAPM_SUPPLY("DMIC CLK", SND_SOC_NOPM, 0, 0,
1244                 set_dmic_clk, SND_SOC_DAPM_PRE_PMU),
1245         /* Boost */
1246         SND_SOC_DAPM_PGA("BST1", RT5640_PWR_ANLG2,
1247                 RT5640_PWR_BST1_BIT, 0, NULL, 0),
1248         SND_SOC_DAPM_PGA("BST2", RT5640_PWR_ANLG2,
1249                 RT5640_PWR_BST4_BIT, 0, NULL, 0),
1250         /* Input Volume */
1251         SND_SOC_DAPM_PGA("INL VOL", RT5640_PWR_VOL,
1252                 RT5640_PWR_IN_L_BIT, 0, NULL, 0),
1253         SND_SOC_DAPM_PGA("INR VOL", RT5640_PWR_VOL,
1254                 RT5640_PWR_IN_R_BIT, 0, NULL, 0),
1255         /* IN Mux */
1256         SND_SOC_DAPM_MUX("INL Mux", SND_SOC_NOPM, 0, 0, &rt5640_inl_mux),
1257         SND_SOC_DAPM_MUX("INR Mux", SND_SOC_NOPM, 0, 0, &rt5640_inr_mux),
1258         /* REC Mixer */
1259         SND_SOC_DAPM_MIXER("RECMIXL", RT5640_PWR_MIXER, RT5640_PWR_RM_L_BIT, 0,
1260                         rt5640_rec_l_mix, ARRAY_SIZE(rt5640_rec_l_mix)),
1261         SND_SOC_DAPM_MIXER("RECMIXR", RT5640_PWR_MIXER, RT5640_PWR_RM_R_BIT, 0,
1262                         rt5640_rec_r_mix, ARRAY_SIZE(rt5640_rec_r_mix)),
1263         /* ADCs */
1264         SND_SOC_DAPM_ADC("ADC L", NULL, RT5640_PWR_DIG1,
1265                         RT5640_PWR_ADC_L_BIT, 0),
1266         SND_SOC_DAPM_ADC("ADC R", NULL, RT5640_PWR_DIG1,
1267                         RT5640_PWR_ADC_R_BIT, 0),
1268         /* ADC Mux */
1269         SND_SOC_DAPM_MUX("Stereo ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1270                                 &rt5640_sto_adc_l2_mux),
1271         SND_SOC_DAPM_MUX("Stereo ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1272                                 &rt5640_sto_adc_r2_mux),
1273         SND_SOC_DAPM_MUX("Stereo ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1274                                 &rt5640_sto_adc_l1_mux),
1275         SND_SOC_DAPM_MUX("Stereo ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1276                                 &rt5640_sto_adc_r1_mux),
1277         SND_SOC_DAPM_MUX("Mono ADC L2 Mux", SND_SOC_NOPM, 0, 0,
1278                                 &rt5640_mono_adc_l2_mux),
1279         SND_SOC_DAPM_MUX("Mono ADC L1 Mux", SND_SOC_NOPM, 0, 0,
1280                                 &rt5640_mono_adc_l1_mux),
1281         SND_SOC_DAPM_MUX("Mono ADC R1 Mux", SND_SOC_NOPM, 0, 0,
1282                                 &rt5640_mono_adc_r1_mux),
1283         SND_SOC_DAPM_MUX("Mono ADC R2 Mux", SND_SOC_NOPM, 0, 0,
1284                                 &rt5640_mono_adc_r2_mux),
1285         /* ADC Mixer */
1286         SND_SOC_DAPM_SUPPLY("stereo filter", RT5640_PWR_DIG2,
1287                 RT5640_PWR_ADC_SF_BIT, 0, NULL, 0),
1288         SND_SOC_DAPM_MIXER("Stereo ADC MIXL", SND_SOC_NOPM, 0, 0,
1289                 rt5640_sto_adc_l_mix, ARRAY_SIZE(rt5640_sto_adc_l_mix)),
1290         SND_SOC_DAPM_MIXER("Stereo ADC MIXR", SND_SOC_NOPM, 0, 0,
1291                 rt5640_sto_adc_r_mix, ARRAY_SIZE(rt5640_sto_adc_r_mix)),
1292         SND_SOC_DAPM_SUPPLY("mono left filter", RT5640_PWR_DIG2,
1293                 RT5640_PWR_ADC_MF_L_BIT, 0, NULL, 0),
1294         SND_SOC_DAPM_MIXER("Mono ADC MIXL", SND_SOC_NOPM, 0, 0,
1295                 rt5640_mono_adc_l_mix, ARRAY_SIZE(rt5640_mono_adc_l_mix)),
1296         SND_SOC_DAPM_SUPPLY("mono right filter", RT5640_PWR_DIG2,
1297                 RT5640_PWR_ADC_MF_R_BIT, 0, NULL, 0),
1298         SND_SOC_DAPM_MIXER("Mono ADC MIXR", SND_SOC_NOPM, 0, 0,
1299                 rt5640_mono_adc_r_mix, ARRAY_SIZE(rt5640_mono_adc_r_mix)),
1300
1301         /* IF2 Mux */
1302         SND_SOC_DAPM_MUX("IF2 ADC L Mux", SND_SOC_NOPM, 0, 0,
1303                                 &rt5640_if2_adc_l_mux),
1304         SND_SOC_DAPM_MUX("IF2 ADC R Mux", SND_SOC_NOPM, 0, 0,
1305                                 &rt5640_if2_adc_r_mux),
1306
1307         /* Digital Interface */
1308         SND_SOC_DAPM_SUPPLY("I2S1", RT5640_PWR_DIG1,
1309                 RT5640_PWR_I2S1_BIT, 0, NULL, 0),
1310         SND_SOC_DAPM_PGA("IF1 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1311         SND_SOC_DAPM_PGA("IF1 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1312         SND_SOC_DAPM_PGA("IF1 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1313         SND_SOC_DAPM_PGA("IF1 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1314         SND_SOC_DAPM_PGA("IF1 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1315         SND_SOC_DAPM_PGA("IF1 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1316         SND_SOC_DAPM_SUPPLY("I2S2", RT5640_PWR_DIG1,
1317                 RT5640_PWR_I2S2_BIT, 0, NULL, 0),
1318         SND_SOC_DAPM_PGA("IF2 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1319         SND_SOC_DAPM_PGA("IF2 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1320         SND_SOC_DAPM_PGA("IF2 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1321         SND_SOC_DAPM_PGA("IF2 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1322         SND_SOC_DAPM_PGA("IF2 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1323         SND_SOC_DAPM_PGA("IF2 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1324         SND_SOC_DAPM_SUPPLY("I2S3", RT5640_PWR_DIG1,
1325                 RT5640_PWR_I2S3_BIT, 0, NULL, 0),
1326         SND_SOC_DAPM_PGA("IF3 DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
1327         SND_SOC_DAPM_PGA("IF3 DAC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1328         SND_SOC_DAPM_PGA("IF3 DAC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1329         SND_SOC_DAPM_PGA("IF3 ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
1330         SND_SOC_DAPM_PGA("IF3 ADC L", SND_SOC_NOPM, 0, 0, NULL, 0),
1331         SND_SOC_DAPM_PGA("IF3 ADC R", SND_SOC_NOPM, 0, 0, NULL, 0),
1332
1333         /* Digital Interface Select */
1334         SND_SOC_DAPM_MUX("DAI1 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1335         SND_SOC_DAPM_MUX("DAI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1336         SND_SOC_DAPM_MUX("DAI1 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1337         SND_SOC_DAPM_MUX("DAI1 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1338         SND_SOC_DAPM_MUX("SDI1 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
1339
1340         SND_SOC_DAPM_MUX("DAI2 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1341         SND_SOC_DAPM_MUX("DAI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1342         SND_SOC_DAPM_MUX("DAI2 IF1 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1343         SND_SOC_DAPM_MUX("DAI2 IF2 Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1344         SND_SOC_DAPM_MUX("SDI2 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_sdi_mux),
1345
1346         SND_SOC_DAPM_MUX("DAI3 RX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1347         SND_SOC_DAPM_MUX("DAI3 TX Mux", SND_SOC_NOPM, 0, 0, &rt5640_dai_mux),
1348
1349         /* Audio Interface */
1350         SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
1351         SND_SOC_DAPM_AIF_OUT("AIF1TX", "AIF1 Capture", 0, SND_SOC_NOPM, 0, 0),
1352         SND_SOC_DAPM_AIF_IN("AIF2RX", "AIF2 Playback", 0, SND_SOC_NOPM, 0, 0),
1353         SND_SOC_DAPM_AIF_OUT("AIF2TX", "AIF2 Capture", 0, SND_SOC_NOPM, 0, 0),
1354         SND_SOC_DAPM_AIF_IN("AIF3RX", "AIF3 Playback", 0, SND_SOC_NOPM, 0, 0),
1355         SND_SOC_DAPM_AIF_OUT("AIF3TX", "AIF3 Capture", 0, SND_SOC_NOPM, 0, 0),
1356
1357         /* Audio DSP */
1358         SND_SOC_DAPM_PGA("Audio DSP", SND_SOC_NOPM, 0, 0, NULL, 0),
1359
1360         /* ANC */
1361         SND_SOC_DAPM_PGA("ANC", SND_SOC_NOPM, 0, 0, NULL, 0),
1362
1363         /* Output Side */
1364         /* DAC mixer before sound effect  */
1365         SND_SOC_DAPM_MIXER("DAC MIXL", SND_SOC_NOPM, 0, 0,
1366                 rt5640_dac_l_mix, ARRAY_SIZE(rt5640_dac_l_mix)),
1367         SND_SOC_DAPM_MIXER("DAC MIXR", SND_SOC_NOPM, 0, 0,
1368                 rt5640_dac_r_mix, ARRAY_SIZE(rt5640_dac_r_mix)),
1369
1370         /* DAC2 channel Mux */
1371         SND_SOC_DAPM_MUX("DAC L2 Mux", SND_SOC_NOPM, 0, 0,
1372                                 &rt5640_dac_l2_mux),
1373         SND_SOC_DAPM_MUX("DAC R2 Mux", SND_SOC_NOPM, 0, 0,
1374                                 &rt5640_dac_r2_mux),
1375
1376         /* DAC Mixer */
1377         SND_SOC_DAPM_MIXER("Stereo DAC MIXL", SND_SOC_NOPM, 0, 0,
1378                 rt5640_sto_dac_l_mix, ARRAY_SIZE(rt5640_sto_dac_l_mix)),
1379         SND_SOC_DAPM_MIXER("Stereo DAC MIXR", SND_SOC_NOPM, 0, 0,
1380                 rt5640_sto_dac_r_mix, ARRAY_SIZE(rt5640_sto_dac_r_mix)),
1381         SND_SOC_DAPM_MIXER("Mono DAC MIXL", SND_SOC_NOPM, 0, 0,
1382                 rt5640_mono_dac_l_mix, ARRAY_SIZE(rt5640_mono_dac_l_mix)),
1383         SND_SOC_DAPM_MIXER("Mono DAC MIXR", SND_SOC_NOPM, 0, 0,
1384                 rt5640_mono_dac_r_mix, ARRAY_SIZE(rt5640_mono_dac_r_mix)),
1385         SND_SOC_DAPM_MIXER("DIG MIXL", SND_SOC_NOPM, 0, 0,
1386                 rt5640_dig_l_mix, ARRAY_SIZE(rt5640_dig_l_mix)),
1387         SND_SOC_DAPM_MIXER("DIG MIXR", SND_SOC_NOPM, 0, 0,
1388                 rt5640_dig_r_mix, ARRAY_SIZE(rt5640_dig_r_mix)),
1389         /* DACs */
1390         SND_SOC_DAPM_DAC("DAC L1", NULL, RT5640_PWR_DIG1,
1391                         RT5640_PWR_DAC_L1_BIT, 0),
1392         SND_SOC_DAPM_DAC("DAC L2", NULL, RT5640_PWR_DIG1,
1393                         RT5640_PWR_DAC_L2_BIT, 0),
1394         SND_SOC_DAPM_DAC("DAC R1", NULL, RT5640_PWR_DIG1,
1395                         RT5640_PWR_DAC_R1_BIT, 0),
1396         SND_SOC_DAPM_DAC("DAC R2", NULL, RT5640_PWR_DIG1,
1397                         RT5640_PWR_DAC_R2_BIT, 0),
1398         /* SPK/OUT Mixer */
1399         SND_SOC_DAPM_MIXER("SPK MIXL", RT5640_PWR_MIXER, RT5640_PWR_SM_L_BIT,
1400                 0, rt5640_spk_l_mix, ARRAY_SIZE(rt5640_spk_l_mix)),
1401         SND_SOC_DAPM_MIXER("SPK MIXR", RT5640_PWR_MIXER, RT5640_PWR_SM_R_BIT,
1402                 0, rt5640_spk_r_mix, ARRAY_SIZE(rt5640_spk_r_mix)),
1403         SND_SOC_DAPM_MIXER("OUT MIXL", RT5640_PWR_MIXER, RT5640_PWR_OM_L_BIT,
1404                 0, rt5640_out_l_mix, ARRAY_SIZE(rt5640_out_l_mix)),
1405         SND_SOC_DAPM_MIXER("OUT MIXR", RT5640_PWR_MIXER, RT5640_PWR_OM_R_BIT,
1406                 0, rt5640_out_r_mix, ARRAY_SIZE(rt5640_out_r_mix)),
1407         /* Ouput Volume */
1408         SND_SOC_DAPM_PGA("SPKVOL L", RT5640_PWR_VOL,
1409                 RT5640_PWR_SV_L_BIT, 0, NULL, 0),
1410         SND_SOC_DAPM_PGA("SPKVOL R", RT5640_PWR_VOL,
1411                 RT5640_PWR_SV_R_BIT, 0, NULL, 0),
1412         SND_SOC_DAPM_PGA("OUTVOL L", RT5640_PWR_VOL,
1413                 RT5640_PWR_OV_L_BIT, 0, NULL, 0),
1414         SND_SOC_DAPM_PGA("OUTVOL R", RT5640_PWR_VOL,
1415                 RT5640_PWR_OV_R_BIT, 0, NULL, 0),
1416         SND_SOC_DAPM_PGA("HPOVOL L", RT5640_PWR_VOL,
1417                 RT5640_PWR_HV_L_BIT, 0, NULL, 0),
1418         SND_SOC_DAPM_PGA("HPOVOL R", RT5640_PWR_VOL,
1419                 RT5640_PWR_HV_R_BIT, 0, NULL, 0),
1420         /* SPO/HPO/LOUT/Mono Mixer */
1421         SND_SOC_DAPM_MIXER("SPOL MIX", SND_SOC_NOPM, 0,
1422                 0, rt5640_spo_l_mix, ARRAY_SIZE(rt5640_spo_l_mix)),
1423         SND_SOC_DAPM_MIXER("SPOR MIX", SND_SOC_NOPM, 0,
1424                 0, rt5640_spo_r_mix, ARRAY_SIZE(rt5640_spo_r_mix)),
1425
1426         SND_SOC_DAPM_MIXER("HPOL MIX", SND_SOC_NOPM, 0, 0,
1427                 rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
1428         SND_SOC_DAPM_MIXER("HPOR MIX", SND_SOC_NOPM, 0, 0,
1429                 rt5640_hpo_mix, ARRAY_SIZE(rt5640_hpo_mix)),
1430         SND_SOC_DAPM_MIXER("LOUT MIX", RT5640_PWR_ANLG1, RT5640_PWR_LM_BIT, 0,
1431                 rt5640_lout_mix, ARRAY_SIZE(rt5640_lout_mix)),
1432         SND_SOC_DAPM_MIXER("Mono MIX", RT5640_PWR_ANLG1, RT5640_PWR_MM_BIT, 0,
1433                 rt5640_mono_mix, ARRAY_SIZE(rt5640_mono_mix)),
1434
1435         SND_SOC_DAPM_SUPPLY("Improve mono amp drv", RT5640_PWR_ANLG1,
1436                 RT5640_PWR_MA_BIT, 0, NULL, 0),
1437
1438         SND_SOC_DAPM_SUPPLY("Improve HP amp drv", RT5640_PWR_ANLG1,
1439         SND_SOC_NOPM, 0, hp_event, SND_SOC_DAPM_PRE_PMD |
1440                                         SND_SOC_DAPM_POST_PMU),
1441
1442         SND_SOC_DAPM_PGA("HP L amp", RT5640_PWR_ANLG1,
1443                 RT5640_PWR_HP_L_BIT, 0, NULL, 0),
1444
1445         SND_SOC_DAPM_PGA("HP R amp", RT5640_PWR_ANLG1,
1446                 RT5640_PWR_HP_R_BIT, 0, NULL, 0),
1447
1448         SND_SOC_DAPM_SUPPLY("Improve SPK amp drv", RT5640_PWR_DIG1,
1449                 SND_SOC_NOPM, 0, spk_event,
1450                 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
1451
1452         /* Output Lines */
1453         SND_SOC_DAPM_OUTPUT("SPOLP"),
1454         SND_SOC_DAPM_OUTPUT("SPOLN"),
1455         SND_SOC_DAPM_OUTPUT("SPORP"),
1456         SND_SOC_DAPM_OUTPUT("SPORN"),
1457         SND_SOC_DAPM_OUTPUT("HPOL"),
1458         SND_SOC_DAPM_OUTPUT("HPOR"),
1459         SND_SOC_DAPM_OUTPUT("LOUTL"),
1460         SND_SOC_DAPM_OUTPUT("LOUTR"),
1461         SND_SOC_DAPM_OUTPUT("MonoP"),
1462         SND_SOC_DAPM_OUTPUT("MonoN"),
1463 };
1464
1465 static const struct snd_soc_dapm_route rt5640_dapm_routes[] = {
1466         {"IN1P", NULL, "LDO2"},
1467         {"IN2P", NULL, "LDO2"},
1468
1469         {"IN1P", NULL, "MIC1"},
1470         {"IN1N", NULL, "MIC1"},
1471         {"IN2P", NULL, "MIC2"},
1472         {"IN2N", NULL, "MIC2"},
1473
1474         {"DMIC L1", NULL, "DMIC1"},
1475         {"DMIC R1", NULL, "DMIC1"},
1476         {"DMIC L2", NULL, "DMIC2"},
1477         {"DMIC R2", NULL, "DMIC2"},
1478
1479         {"BST1", NULL, "IN1P"},
1480         {"BST1", NULL, "IN1N"},
1481         {"BST2", NULL, "IN2P"},
1482         {"BST2", NULL, "IN2N"},
1483
1484         {"INL VOL", NULL, "IN2P"},
1485         {"INR VOL", NULL, "IN2N"},
1486
1487         {"RECMIXL", "HPOL Switch", "HPOL"},
1488         {"RECMIXL", "INL Switch", "INL VOL"},
1489         {"RECMIXL", "BST2 Switch", "BST2"},
1490         {"RECMIXL", "BST1 Switch", "BST1"},
1491         {"RECMIXL", "OUT MIXL Switch", "OUT MIXL"},
1492
1493         {"RECMIXR", "HPOR Switch", "HPOR"},
1494         {"RECMIXR", "INR Switch", "INR VOL"},
1495         {"RECMIXR", "BST2 Switch", "BST2"},
1496         {"RECMIXR", "BST1 Switch", "BST1"},
1497         {"RECMIXR", "OUT MIXR Switch", "OUT MIXR"},
1498
1499         {"ADC L", NULL, "RECMIXL"},
1500         {"ADC R", NULL, "RECMIXR"},
1501
1502         {"DMIC L1", NULL, "DMIC CLK"},
1503         {"DMIC L2", NULL, "DMIC CLK"},
1504
1505         {"Stereo ADC L2 Mux", "DMIC1", "DMIC L1"},
1506         {"Stereo ADC L2 Mux", "DMIC2", "DMIC L2"},
1507         {"Stereo ADC L2 Mux", "DIG MIX", "DIG MIXL"},
1508         {"Stereo ADC L1 Mux", "ADC", "ADC L"},
1509         {"Stereo ADC L1 Mux", "DIG MIX", "DIG MIXL"},
1510
1511         {"Stereo ADC R1 Mux", "ADC", "ADC R"},
1512         {"Stereo ADC R1 Mux", "DIG MIX", "DIG MIXR"},
1513         {"Stereo ADC R2 Mux", "DMIC1", "DMIC R1"},
1514         {"Stereo ADC R2 Mux", "DMIC2", "DMIC R2"},
1515         {"Stereo ADC R2 Mux", "DIG MIX", "DIG MIXR"},
1516
1517         {"Mono ADC L2 Mux", "DMIC L1", "DMIC L1"},
1518         {"Mono ADC L2 Mux", "DMIC L2", "DMIC L2"},
1519         {"Mono ADC L2 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1520         {"Mono ADC L1 Mux", "Mono DAC MIXL", "Mono DAC MIXL"},
1521         {"Mono ADC L1 Mux", "ADCL", "ADC L"},
1522
1523         {"Mono ADC R1 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1524         {"Mono ADC R1 Mux", "ADCR", "ADC R"},
1525         {"Mono ADC R2 Mux", "DMIC R1", "DMIC R1"},
1526         {"Mono ADC R2 Mux", "DMIC R2", "DMIC R2"},
1527         {"Mono ADC R2 Mux", "Mono DAC MIXR", "Mono DAC MIXR"},
1528
1529         {"Stereo ADC MIXL", "ADC1 Switch", "Stereo ADC L1 Mux"},
1530         {"Stereo ADC MIXL", "ADC2 Switch", "Stereo ADC L2 Mux"},
1531         {"Stereo ADC MIXL", NULL, "stereo filter"},
1532         {"stereo filter", NULL, "PLL1", check_sysclk1_source},
1533
1534         {"Stereo ADC MIXR", "ADC1 Switch", "Stereo ADC R1 Mux"},
1535         {"Stereo ADC MIXR", "ADC2 Switch", "Stereo ADC R2 Mux"},
1536         {"Stereo ADC MIXR", NULL, "stereo filter"},
1537         {"stereo filter", NULL, "PLL1", check_sysclk1_source},
1538
1539         {"Mono ADC MIXL", "ADC1 Switch", "Mono ADC L1 Mux"},
1540         {"Mono ADC MIXL", "ADC2 Switch", "Mono ADC L2 Mux"},
1541         {"Mono ADC MIXL", NULL, "mono left filter"},
1542         {"mono left filter", NULL, "PLL1", check_sysclk1_source},
1543
1544         {"Mono ADC MIXR", "ADC1 Switch", "Mono ADC R1 Mux"},
1545         {"Mono ADC MIXR", "ADC2 Switch", "Mono ADC R2 Mux"},
1546         {"Mono ADC MIXR", NULL, "mono right filter"},
1547         {"mono right filter", NULL, "PLL1", check_sysclk1_source},
1548
1549         {"IF2 ADC L Mux", "Mono ADC MIXL", "Mono ADC MIXL"},
1550         {"IF2 ADC R Mux", "Mono ADC MIXR", "Mono ADC MIXR"},
1551
1552         {"IF2 ADC L", NULL, "IF2 ADC L Mux"},
1553         {"IF2 ADC R", NULL, "IF2 ADC R Mux"},
1554         {"IF3 ADC L", NULL, "Mono ADC MIXL"},
1555         {"IF3 ADC R", NULL, "Mono ADC MIXR"},
1556         {"IF1 ADC L", NULL, "Stereo ADC MIXL"},
1557         {"IF1 ADC R", NULL, "Stereo ADC MIXR"},
1558
1559         {"IF1 ADC", NULL, "I2S1"},
1560         {"IF1 ADC", NULL, "IF1 ADC L"},
1561         {"IF1 ADC", NULL, "IF1 ADC R"},
1562         {"IF2 ADC", NULL, "I2S2"},
1563         {"IF2 ADC", NULL, "IF2 ADC L"},
1564         {"IF2 ADC", NULL, "IF2 ADC R"},
1565         {"IF3 ADC", NULL, "I2S3"},
1566         {"IF3 ADC", NULL, "IF3 ADC L"},
1567         {"IF3 ADC", NULL, "IF3 ADC R"},
1568
1569         {"DAI1 TX Mux", "1:1|2:2|3:3", "IF1 ADC"},
1570         {"DAI1 TX Mux", "1:1|2:3|3:2", "IF1 ADC"},
1571         {"DAI1 TX Mux", "1:3|2:1|3:2", "IF2 ADC"},
1572         {"DAI1 TX Mux", "1:2|2:1|3:3", "IF2 ADC"},
1573         {"DAI1 TX Mux", "1:3|2:2|3:1", "IF3 ADC"},
1574         {"DAI1 TX Mux", "1:2|2:3|3:1", "IF3 ADC"},
1575         {"DAI1 IF1 Mux", "1:1|2:1|3:3", "IF1 ADC"},
1576         {"DAI1 IF2 Mux", "1:1|2:1|3:3", "IF2 ADC"},
1577         {"SDI1 TX Mux", "IF1", "DAI1 IF1 Mux"},
1578         {"SDI1 TX Mux", "IF2", "DAI1 IF2 Mux"},
1579
1580         {"DAI2 TX Mux", "1:2|2:3|3:1", "IF1 ADC"},
1581         {"DAI2 TX Mux", "1:2|2:1|3:3", "IF1 ADC"},
1582         {"DAI2 TX Mux", "1:1|2:2|3:3", "IF2 ADC"},
1583         {"DAI2 TX Mux", "1:3|2:2|3:1", "IF2 ADC"},
1584         {"DAI2 TX Mux", "1:1|2:3|3:2", "IF3 ADC"},
1585         {"DAI2 TX Mux", "1:3|2:1|3:2", "IF3 ADC"},
1586         {"DAI2 IF1 Mux", "1:2|2:2|3:3", "IF1 ADC"},
1587         {"DAI2 IF2 Mux", "1:2|2:2|3:3", "IF2 ADC"},
1588         {"SDI2 TX Mux", "IF1", "DAI2 IF1 Mux"},
1589         {"SDI2 TX Mux", "IF2", "DAI2 IF2 Mux"},
1590
1591         {"DAI3 TX Mux", "1:3|2:1|3:2", "IF1 ADC"},
1592         {"DAI3 TX Mux", "1:3|2:2|3:1", "IF1 ADC"},
1593         {"DAI3 TX Mux", "1:1|2:3|3:2", "IF2 ADC"},
1594         {"DAI3 TX Mux", "1:2|2:3|3:1", "IF2 ADC"},
1595         {"DAI3 TX Mux", "1:1|2:2|3:3", "IF3 ADC"},
1596         {"DAI3 TX Mux", "1:2|2:1|3:3", "IF3 ADC"},
1597         {"DAI3 TX Mux", "1:1|2:1|3:3", "IF3 ADC"},
1598         {"DAI3 TX Mux", "1:2|2:2|3:3", "IF3 ADC"},
1599
1600         {"AIF1TX", NULL, "DAI1 TX Mux"},
1601         {"AIF1TX", NULL, "SDI1 TX Mux"},
1602         {"AIF2TX", NULL, "DAI2 TX Mux"},
1603         {"AIF2TX", NULL, "SDI2 TX Mux"},
1604         {"AIF3TX", NULL, "DAI3 TX Mux"},
1605
1606         {"DAI1 RX Mux", "1:1|2:2|3:3", "AIF1RX"},
1607         {"DAI1 RX Mux", "1:1|2:3|3:2", "AIF1RX"},
1608         {"DAI1 RX Mux", "1:1|2:1|3:3", "AIF1RX"},
1609         {"DAI1 RX Mux", "1:2|2:3|3:1", "AIF2RX"},
1610         {"DAI1 RX Mux", "1:2|2:1|3:3", "AIF2RX"},
1611         {"DAI1 RX Mux", "1:2|2:2|3:3", "AIF2RX"},
1612         {"DAI1 RX Mux", "1:3|2:1|3:2", "AIF3RX"},
1613         {"DAI1 RX Mux", "1:3|2:2|3:1", "AIF3RX"},
1614
1615         {"DAI2 RX Mux", "1:3|2:1|3:2", "AIF1RX"},
1616         {"DAI2 RX Mux", "1:2|2:1|3:3", "AIF1RX"},
1617         {"DAI2 RX Mux", "1:1|2:1|3:3", "AIF1RX"},
1618         {"DAI2 RX Mux", "1:1|2:2|3:3", "AIF2RX"},
1619         {"DAI2 RX Mux", "1:3|2:2|3:1", "AIF2RX"},
1620         {"DAI2 RX Mux", "1:2|2:2|3:3", "AIF2RX"},
1621         {"DAI2 RX Mux", "1:1|2:3|3:2", "AIF3RX"},
1622         {"DAI2 RX Mux", "1:2|2:3|3:1", "AIF3RX"},
1623
1624         {"DAI3 RX Mux", "1:3|2:2|3:1", "AIF1RX"},
1625         {"DAI3 RX Mux", "1:2|2:3|3:1", "AIF1RX"},
1626         {"DAI3 RX Mux", "1:1|2:3|3:2", "AIF2RX"},
1627         {"DAI3 RX Mux", "1:3|2:1|3:2", "AIF2RX"},
1628         {"DAI3 RX Mux", "1:1|2:2|3:3", "AIF3RX"},
1629         {"DAI3 RX Mux", "1:2|2:1|3:3", "AIF3RX"},
1630         {"DAI3 RX Mux", "1:1|2:1|3:3", "AIF3RX"},
1631         {"DAI3 RX Mux", "1:2|2:2|3:3", "AIF3RX"},
1632
1633         {"IF1 DAC", NULL, "I2S1"},
1634         {"IF1 DAC", NULL, "DAI1 RX Mux"},
1635         {"IF2 DAC", NULL, "I2S2"},
1636         {"IF2 DAC", NULL, "DAI2 RX Mux"},
1637         {"IF3 DAC", NULL, "I2S3"},
1638         {"IF3 DAC", NULL, "DAI3 RX Mux"},
1639
1640         {"IF1 DAC L", NULL, "IF1 DAC"},
1641         {"IF1 DAC R", NULL, "IF1 DAC"},
1642         {"IF2 DAC L", NULL, "IF2 DAC"},
1643         {"IF2 DAC R", NULL, "IF2 DAC"},
1644         {"IF3 DAC L", NULL, "IF3 DAC"},
1645         {"IF3 DAC R", NULL, "IF3 DAC"},
1646
1647         {"DAC MIXL", "Stereo ADC Switch", "Stereo ADC MIXL"},
1648         {"DAC MIXL", "INF1 Switch", "IF1 DAC L"},
1649         {"DAC MIXR", "Stereo ADC Switch", "Stereo ADC MIXR"},
1650         {"DAC MIXR", "INF1 Switch", "IF1 DAC R"},
1651
1652         {"ANC", NULL, "Stereo ADC MIXL"},
1653         {"ANC", NULL, "Stereo ADC MIXR"},
1654
1655         {"Audio DSP", NULL, "DAC MIXL"},
1656         {"Audio DSP", NULL, "DAC MIXR"},
1657
1658         {"DAC L2 Mux", "IF2", "IF2 DAC L"},
1659         {"DAC L2 Mux", "IF3", "IF3 DAC L"},
1660         {"DAC L2 Mux", "Base L/R", "Audio DSP"},
1661
1662         {"DAC R2 Mux", "IF2", "IF2 DAC R"},
1663         {"DAC R2 Mux", "IF3", "IF3 DAC R"},
1664
1665         {"Stereo DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1666         {"Stereo DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1667         {"Stereo DAC MIXL", "ANC Switch", "ANC"},
1668         {"Stereo DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1669         {"Stereo DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1670         {"Stereo DAC MIXR", "ANC Switch", "ANC"},
1671
1672         {"Mono DAC MIXL", "DAC L1 Switch", "DAC MIXL"},
1673         {"Mono DAC MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1674         {"Mono DAC MIXL", "DAC R2 Switch", "DAC R2 Mux"},
1675         {"Mono DAC MIXR", "DAC R1 Switch", "DAC MIXR"},
1676         {"Mono DAC MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1677         {"Mono DAC MIXR", "DAC L2 Switch", "DAC L2 Mux"},
1678
1679         {"DIG MIXL", "DAC L1 Switch", "DAC MIXL"},
1680         {"DIG MIXL", "DAC L2 Switch", "DAC L2 Mux"},
1681         {"DIG MIXR", "DAC R1 Switch", "DAC MIXR"},
1682         {"DIG MIXR", "DAC R2 Switch", "DAC R2 Mux"},
1683
1684         {"DAC L1", NULL, "Stereo DAC MIXL"},
1685         {"DAC L1", NULL, "PLL1", check_sysclk1_source},
1686         {"DAC R1", NULL, "Stereo DAC MIXR"},
1687         {"DAC R1", NULL, "PLL1", check_sysclk1_source},
1688         {"DAC L2", NULL, "Mono DAC MIXL"},
1689         {"DAC L2", NULL, "PLL1", check_sysclk1_source},
1690         {"DAC R2", NULL, "Mono DAC MIXR"},
1691         {"DAC R2", NULL, "PLL1", check_sysclk1_source},
1692
1693         {"SPK MIXL", "REC MIXL Switch", "RECMIXL"},
1694         {"SPK MIXL", "INL Switch", "INL VOL"},
1695         {"SPK MIXL", "DAC L1 Switch", "DAC L1"},
1696         {"SPK MIXL", "DAC L2 Switch", "DAC L2"},
1697         {"SPK MIXL", "OUT MIXL Switch", "OUT MIXL"},
1698         {"SPK MIXR", "REC MIXR Switch", "RECMIXR"},
1699         {"SPK MIXR", "INR Switch", "INR VOL"},
1700         {"SPK MIXR", "DAC R1 Switch", "DAC R1"},
1701         {"SPK MIXR", "DAC R2 Switch", "DAC R2"},
1702         {"SPK MIXR", "OUT MIXR Switch", "OUT MIXR"},
1703
1704         {"OUT MIXL", "SPK MIXL Switch", "SPK MIXL"},
1705         {"OUT MIXL", "BST1 Switch", "BST1"},
1706         {"OUT MIXL", "INL Switch", "INL VOL"},
1707         {"OUT MIXL", "REC MIXL Switch", "RECMIXL"},
1708         {"OUT MIXL", "DAC R2 Switch", "DAC R2"},
1709         {"OUT MIXL", "DAC L2 Switch", "DAC L2"},
1710         {"OUT MIXL", "DAC L1 Switch", "DAC L1"},
1711
1712         {"OUT MIXR", "SPK MIXR Switch", "SPK MIXR"},
1713         {"OUT MIXR", "BST2 Switch", "BST2"},
1714         {"OUT MIXR", "BST1 Switch", "BST1"},
1715         {"OUT MIXR", "INR Switch", "INR VOL"},
1716         {"OUT MIXR", "REC MIXR Switch", "RECMIXR"},
1717         {"OUT MIXR", "DAC L2 Switch", "DAC L2"},
1718         {"OUT MIXR", "DAC R2 Switch", "DAC R2"},
1719         {"OUT MIXR", "DAC R1 Switch", "DAC R1"},
1720
1721         {"SPKVOL L", NULL, "SPK MIXL"},
1722         {"SPKVOL R", NULL, "SPK MIXR"},
1723         {"HPOVOL L", NULL, "OUT MIXL"},
1724         {"HPOVOL R", NULL, "OUT MIXR"},
1725         {"OUTVOL L", NULL, "OUT MIXL"},
1726         {"OUTVOL R", NULL, "OUT MIXR"},
1727
1728         {"SPOL MIX", "DAC R1 Switch", "DAC R1"},
1729         {"SPOL MIX", "DAC L1 Switch", "DAC L1"},
1730         {"SPOL MIX", "SPKVOL R Switch", "SPKVOL R"},
1731         {"SPOL MIX", "SPKVOL L Switch", "SPKVOL L"},
1732         {"SPOL MIX", "BST1 Switch", "BST1"},
1733         {"SPOR MIX", "DAC R1 Switch", "DAC R1"},
1734         {"SPOR MIX", "SPKVOL R Switch", "SPKVOL R"},
1735         {"SPOR MIX", "BST1 Switch", "BST1"},
1736
1737         {"HPOL MIX", "DAC2 Switch", "DAC L2"},
1738         {"HPOL MIX", "DAC1 Switch", "DAC L1"},
1739         {"HPOL MIX", "HPVOL Switch", "HPOVOL L"},
1740         {"HPOR MIX", "DAC2 Switch", "DAC R2"},
1741         {"HPOR MIX", "DAC1 Switch", "DAC R1"},
1742         {"HPOR MIX", "HPVOL Switch", "HPOVOL R"},
1743
1744         {"LOUT MIX", "DAC L1 Switch", "DAC L1"},
1745         {"LOUT MIX", "DAC R1 Switch", "DAC R1"},
1746         {"LOUT MIX", "OUTVOL L Switch", "OUTVOL L"},
1747         {"LOUT MIX", "OUTVOL R Switch", "OUTVOL R"},
1748
1749         {"Mono MIX", "DAC R2 Switch", "DAC R2"},
1750         {"Mono MIX", "DAC L2 Switch", "DAC L2"},
1751         {"Mono MIX", "OUTVOL R Switch", "OUTVOL R"},
1752         {"Mono MIX", "OUTVOL L Switch", "OUTVOL L"},
1753         {"Mono MIX", "BST1 Switch", "BST1"},
1754
1755         {"HP L amp", NULL, "HPOL MIX"},
1756         {"HP R amp", NULL, "HPOR MIX"},
1757
1758 /*      {"HP L amp", NULL, "Improve HP amp drv"},
1759         {"HP R amp", NULL, "Improve HP amp drv"}, */
1760
1761         {"SPOLP", NULL, "SPOL MIX"},
1762         {"SPOLN", NULL, "SPOL MIX"},
1763         {"SPORP", NULL, "SPOR MIX"},
1764         {"SPORN", NULL, "SPOR MIX"},
1765
1766         {"SPOLP", NULL, "Improve SPK amp drv"},
1767         {"SPOLN", NULL, "Improve SPK amp drv"},
1768         {"SPORP", NULL, "Improve SPK amp drv"},
1769         {"SPORN", NULL, "Improve SPK amp drv"},
1770
1771         {"HPOL", NULL, "Improve HP amp drv"},
1772         {"HPOR", NULL, "Improve HP amp drv"},
1773
1774         {"HPOL", NULL, "HP L amp"},
1775         {"HPOR", NULL, "HP R amp"},
1776         {"LOUTL", NULL, "LOUT MIX"},
1777         {"LOUTR", NULL, "LOUT MIX"},
1778         {"MonoP", NULL, "Mono MIX"},
1779         {"MonoN", NULL, "Mono MIX"},
1780         {"MonoP", NULL, "Improve mono amp drv"},
1781 };
1782
1783 static int get_sdp_info(struct snd_soc_codec *codec, int dai_id)
1784 {
1785         int ret = 0, val = snd_soc_read(codec, RT5640_I2S1_SDP);
1786
1787         if (codec == NULL)
1788                 return -EINVAL;
1789
1790         val = (val & RT5640_I2S_IF_MASK) >> RT5640_I2S_IF_SFT;
1791         switch (dai_id) {
1792         case RT5640_AIF1:
1793                 if (val == RT5640_IF_123 || val == RT5640_IF_132 ||
1794                         val == RT5640_IF_113)
1795                         ret |= RT5640_U_IF1;
1796                 if (val == RT5640_IF_312 || val == RT5640_IF_213 ||
1797                         val == RT5640_IF_113)
1798                         ret |= RT5640_U_IF2;
1799                 if (val == RT5640_IF_321 || val == RT5640_IF_231)
1800                         ret |= RT5640_U_IF3;
1801                 break;
1802
1803         case RT5640_AIF2:
1804                 if (val == RT5640_IF_231 || val == RT5640_IF_213 ||
1805                         val == RT5640_IF_223)
1806                         ret |= RT5640_U_IF1;
1807                 if (val == RT5640_IF_123 || val == RT5640_IF_321 ||
1808                         val == RT5640_IF_223)
1809                         ret |= RT5640_U_IF2;
1810                 if (val == RT5640_IF_132 || val == RT5640_IF_312)
1811                         ret |= RT5640_U_IF3;
1812                 break;
1813
1814 #if (CONFIG_SND_SOC_RT5643_MODULE | CONFIG_SND_SOC_RT5643 | \
1815                         CONFIG_SND_SOC_RT5646_MODULE | CONFIG_SND_SOC_RT5646)
1816
1817         case RT5640_AIF3:
1818                 if (val == RT5640_IF_312 || val == RT5640_IF_321)
1819                         ret |= RT5640_U_IF1;
1820                 if (val == RT5640_IF_132 || val == RT5640_IF_231)
1821                         ret |= RT5640_U_IF2;
1822                 if (val == RT5640_IF_123 || val == RT5640_IF_213 ||
1823                         val == RT5640_IF_113 || val == RT5640_IF_223)
1824                         ret |= RT5640_U_IF3;
1825                 break;
1826 #endif
1827
1828         default:
1829                 ret = -EINVAL;
1830                 break;
1831         }
1832
1833         return ret;
1834 }
1835
1836 static int get_clk_info(int sclk, int rate)
1837 {
1838         int i, pd[] = {1, 2, 3, 4, 6, 8, 12, 16};
1839
1840         if (sclk <= 0 || rate <= 0)
1841                 return -EINVAL;
1842
1843         rate = rate << 8;
1844         for (i = 0; i < ARRAY_SIZE(pd); i++)
1845                 if (sclk == rate * pd[i])
1846                         return i;
1847
1848         return -EINVAL;
1849 }
1850
1851 static int rt5640_hw_params(struct snd_pcm_substream *substream,
1852         struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1853 {
1854         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1855         struct snd_soc_codec *codec = rtd->codec;
1856         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1857         unsigned int val_len = 0, val_clk, mask_clk, dai_sel;
1858         int pre_div, bclk_ms, frame_size;
1859
1860         rt5640->lrck[dai->id] = params_rate(params);
1861         pre_div = get_clk_info(rt5640->sysclk, rt5640->lrck[dai->id]);
1862         if (pre_div < 0) {
1863                 dev_err(codec->dev, "Unsupported clock setting\n");
1864                 return -EINVAL;
1865         }
1866         frame_size = snd_soc_params_to_frame_size(params);
1867         if (frame_size < 0) {
1868                 dev_err(codec->dev, "Unsupported frame size: %d\n", frame_size);
1869                 return -EINVAL;
1870         }
1871         bclk_ms = frame_size > 32 ? 1 : 0;
1872         rt5640->bclk[dai->id] = rt5640->lrck[dai->id] * (32 << bclk_ms);
1873
1874         dev_dbg(dai->dev, "bclk is %dHz and lrck is %dHz\n",
1875                 rt5640->bclk[dai->id], rt5640->lrck[dai->id]);
1876         dev_dbg(dai->dev, "bclk_ms is %d and pre_div is %d for iis %d\n",
1877                                 bclk_ms, pre_div, dai->id);
1878
1879         switch (params_format(params)) {
1880         case SNDRV_PCM_FORMAT_S16_LE:
1881                 break;
1882         case SNDRV_PCM_FORMAT_S20_3LE:
1883                 val_len |= RT5640_I2S_DL_20;
1884                 break;
1885         case SNDRV_PCM_FORMAT_S24_LE:
1886                 val_len |= RT5640_I2S_DL_24;
1887                 break;
1888         case SNDRV_PCM_FORMAT_S8:
1889                 val_len |= RT5640_I2S_DL_8;
1890                 break;
1891         default:
1892                 return -EINVAL;
1893         }
1894
1895         dai_sel = get_sdp_info(codec, dai->id);
1896         if (dai_sel < 0) {
1897                 dev_err(codec->dev, "Failed to get sdp info: %d\n", dai_sel);
1898                 return -EINVAL;
1899         }
1900         if (dai_sel & RT5640_U_IF1) {
1901                 mask_clk = RT5640_I2S_BCLK_MS1_MASK | RT5640_I2S_PD1_MASK;
1902                 val_clk = bclk_ms << RT5640_I2S_BCLK_MS1_SFT |
1903                         pre_div << RT5640_I2S_PD1_SFT;
1904                 snd_soc_update_bits(codec, RT5640_I2S1_SDP,
1905                         RT5640_I2S_DL_MASK, val_len);
1906                 snd_soc_update_bits(codec, RT5640_ADDA_CLK1, mask_clk, val_clk);
1907         }
1908         if (dai_sel & RT5640_U_IF2) {
1909                 mask_clk = RT5640_I2S_BCLK_MS2_MASK | RT5640_I2S_PD2_MASK;
1910                 val_clk = bclk_ms << RT5640_I2S_BCLK_MS2_SFT |
1911                         pre_div << RT5640_I2S_PD2_SFT;
1912                 snd_soc_update_bits(codec, RT5640_I2S2_SDP,
1913                         RT5640_I2S_DL_MASK, val_len);
1914                 snd_soc_update_bits(codec, RT5640_ADDA_CLK1, mask_clk, val_clk);
1915         }
1916 #if (CONFIG_SND_SOC_RT5643_MODULE | CONFIG_SND_SOC_RT5643 | \
1917                         CONFIG_SND_SOC_RT5646_MODULE | CONFIG_SND_SOC_RT5646)
1918         if (dai_sel & RT5640_U_IF3) {
1919                 mask_clk = RT5640_I2S_BCLK_MS3_MASK | RT5640_I2S_PD3_MASK;
1920                 val_clk = bclk_ms << RT5640_I2S_BCLK_MS3_SFT |
1921                         pre_div << RT5640_I2S_PD3_SFT;
1922                 snd_soc_update_bits(codec, RT5640_I2S3_SDP,
1923                         RT5640_I2S_DL_MASK, val_len);
1924                 snd_soc_update_bits(codec, RT5640_ADDA_CLK1, mask_clk, val_clk);
1925         }
1926 #endif
1927         return 0;
1928 }
1929
1930 static int rt5640_prepare(struct snd_pcm_substream *substream,
1931                                 struct snd_soc_dai *dai)
1932 {
1933         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1934         struct snd_soc_codec *codec = rtd->codec;
1935         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1936
1937         rt5640->aif_pu = dai->id;
1938         return 0;
1939 }
1940
1941 static int rt5640_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1942 {
1943         struct snd_soc_codec *codec = dai->codec;
1944         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
1945         unsigned int reg_val = 0, dai_sel;
1946
1947         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1948         case SND_SOC_DAIFMT_CBM_CFM:
1949                 rt5640->master[dai->id] = 1;
1950                 break;
1951         case SND_SOC_DAIFMT_CBS_CFS:
1952                 reg_val |= RT5640_I2S_MS_S;
1953                 rt5640->master[dai->id] = 0;
1954                 break;
1955         default:
1956                 return -EINVAL;
1957         }
1958
1959         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1960         case SND_SOC_DAIFMT_NB_NF:
1961                 break;
1962         case SND_SOC_DAIFMT_IB_NF:
1963                 reg_val |= RT5640_I2S_BP_INV;
1964                 break;
1965         default:
1966                 return -EINVAL;
1967         }
1968
1969         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1970         case SND_SOC_DAIFMT_I2S:
1971                 break;
1972         case SND_SOC_DAIFMT_LEFT_J:
1973                 reg_val |= RT5640_I2S_DF_LEFT;
1974                 break;
1975         case SND_SOC_DAIFMT_DSP_A:
1976                 reg_val |= RT5640_I2S_DF_PCM_A;
1977                 break;
1978         case SND_SOC_DAIFMT_DSP_B:
1979                 reg_val  |= RT5640_I2S_DF_PCM_B;
1980                 break;
1981         default:
1982                 return -EINVAL;
1983         }
1984
1985         dai_sel = get_sdp_info(codec, dai->id);
1986         if (dai_sel < 0) {
1987                 dev_err(codec->dev, "Failed to get sdp info: %d\n", dai_sel);
1988                 return -EINVAL;
1989         }
1990         if (dai_sel & RT5640_U_IF1) {
1991                 snd_soc_update_bits(codec, RT5640_I2S1_SDP,
1992                         RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
1993                         RT5640_I2S_DF_MASK, reg_val);
1994         }
1995         if (dai_sel & RT5640_U_IF2) {
1996                 snd_soc_update_bits(codec, RT5640_I2S2_SDP,
1997                         RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
1998                         RT5640_I2S_DF_MASK, reg_val);
1999         }
2000 #if (CONFIG_SND_SOC_RT5643_MODULE | CONFIG_SND_SOC_RT5643 | \
2001                         CONFIG_SND_SOC_RT5646_MODULE | CONFIG_SND_SOC_RT5646)
2002         if (dai_sel & RT5640_U_IF3) {
2003                 snd_soc_update_bits(codec, RT5640_I2S3_SDP,
2004                         RT5640_I2S_MS_MASK | RT5640_I2S_BP_MASK |
2005                         RT5640_I2S_DF_MASK, reg_val);
2006         }
2007 #endif
2008         return 0;
2009 }
2010
2011 static int rt5640_set_dai_sysclk(struct snd_soc_dai *dai,
2012                 int clk_id, unsigned int freq, int dir)
2013 {
2014         struct snd_soc_codec *codec = dai->codec;
2015         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
2016         unsigned int reg_val = 0;
2017
2018         if (freq == rt5640->sysclk && clk_id == rt5640->sysclk_src)
2019                 return 0;
2020
2021         switch (clk_id) {
2022         case RT5640_SCLK_S_MCLK:
2023                 reg_val |= RT5640_SCLK_SRC_MCLK;
2024                 break;
2025         case RT5640_SCLK_S_PLL1:
2026                 reg_val |= RT5640_SCLK_SRC_PLL1;
2027                 break;
2028         case RT5640_SCLK_S_PLL1_TK:
2029                 reg_val |= RT5640_SCLK_SRC_PLL1T;
2030                 break;
2031         case RT5640_SCLK_S_RCCLK:
2032                 reg_val |= RT5640_SCLK_SRC_RCCLK;
2033                 break;
2034         default:
2035                 dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
2036                 return -EINVAL;
2037         }
2038         snd_soc_update_bits(codec, RT5640_GLB_CLK,
2039                 RT5640_SCLK_SRC_MASK, reg_val);
2040         rt5640->sysclk = freq;
2041         rt5640->sysclk_src = clk_id;
2042
2043         dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
2044         return 0;
2045 }
2046
2047 /**
2048  * rt5640_pll_calc - Calcualte PLL M/N/K code.
2049  * @freq_in: external clock provided to codec.
2050  * @freq_out: target clock which codec works on.
2051  * @pll_code: Pointer to structure with M, N, K and bypass flag.
2052  *
2053  * Calcualte M/N/K code to configure PLL for codec. And K is assigned to 2
2054  * which make calculation more efficiently.
2055  *
2056  * Returns 0 for success or negative error code.
2057  */
2058 static int rt5640_pll_calc(const unsigned int freq_in,
2059         const unsigned int freq_out, struct rt5640_pll_code *pll_code)
2060 {
2061         int max_n = RT5640_PLL_N_MAX, max_m = RT5640_PLL_M_MAX;
2062         int n, m, red, n_t, m_t, in_t, out_t, red_t = abs(freq_out - freq_in);
2063         bool bypass = false;
2064
2065         if (RT5640_PLL_INP_MAX < freq_in || RT5640_PLL_INP_MIN > freq_in)
2066                 return -EINVAL;
2067
2068         for (n_t = 0; n_t <= max_n; n_t++) {
2069                 in_t = (freq_in >> 1) + (freq_in >> 2) * n_t;
2070                 if (in_t < 0)
2071                         continue;
2072                 if (in_t == freq_out) {
2073                         bypass = true;
2074                         n = n_t;
2075                         goto code_find;
2076                 }
2077                 for (m_t = 0; m_t <= max_m; m_t++) {
2078                         out_t = in_t / (m_t + 2);
2079                         red = abs(out_t - freq_out);
2080                         if (red < red_t) {
2081                                 n = n_t;
2082                                 m = m_t;
2083                                 if (red == 0)
2084                                         goto code_find;
2085                                 red_t = red;
2086                         }
2087                 }
2088         }
2089         pr_debug("Only get approximation about PLL\n");
2090
2091 code_find:
2092
2093         pll_code->m_bp = bypass;
2094         pll_code->m_code = m;
2095         pll_code->n_code = n;
2096         pll_code->k_code = 2;
2097         return 0;
2098 }
2099
2100 static int rt5640_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
2101                         unsigned int freq_in, unsigned int freq_out)
2102 {
2103         struct snd_soc_codec *codec = dai->codec;
2104         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
2105         struct rt5640_pll_code pll_code;
2106         int ret, dai_sel;
2107
2108         if (source == rt5640->pll_src && freq_in == rt5640->pll_in &&
2109             freq_out == rt5640->pll_out)
2110                 return 0;
2111
2112         if (!freq_in || !freq_out) {
2113                 dev_dbg(codec->dev, "PLL disabled\n");
2114
2115                 rt5640->pll_in = 0;
2116                 rt5640->pll_out = 0;
2117                 snd_soc_update_bits(codec, RT5640_GLB_CLK,
2118                         RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_MCLK);
2119                 return 0;
2120         }
2121
2122         switch (source) {
2123         case RT5640_PLL1_S_MCLK:
2124                 snd_soc_update_bits(codec, RT5640_GLB_CLK,
2125                         RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_MCLK);
2126                 break;
2127         case RT5640_PLL1_S_BCLK1:
2128         case RT5640_PLL1_S_BCLK2:
2129
2130 #if (CONFIG_SND_SOC_RT5643_MODULE | CONFIG_SND_SOC_RT5643 | \
2131                         CONFIG_SND_SOC_RT5646_MODULE | CONFIG_SND_SOC_RT5646)
2132
2133         case RT5640_PLL1_S_BCLK3:
2134
2135 #endif
2136                 dai_sel = get_sdp_info(codec, dai->id);
2137                 if (dai_sel < 0) {
2138                         dev_err(codec->dev,
2139                                 "Failed to get sdp info: %d\n", dai_sel);
2140                         return -EINVAL;
2141                 }
2142                 if (dai_sel & RT5640_U_IF1) {
2143                         snd_soc_update_bits(codec, RT5640_GLB_CLK,
2144                                 RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK1);
2145                 }
2146                 if (dai_sel & RT5640_U_IF2) {
2147                         snd_soc_update_bits(codec, RT5640_GLB_CLK,
2148                                 RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK2);
2149                 }
2150                 if (dai_sel & RT5640_U_IF3) {
2151                         snd_soc_update_bits(codec, RT5640_GLB_CLK,
2152                                 RT5640_PLL1_SRC_MASK, RT5640_PLL1_SRC_BCLK3);
2153                 }
2154                 break;
2155         default:
2156                 dev_err(codec->dev, "Unknown PLL source %d\n", source);
2157                 return -EINVAL;
2158         }
2159
2160         ret = rt5640_pll_calc(freq_in, freq_out, &pll_code);
2161         if (ret < 0) {
2162                 dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
2163                 return ret;
2164         }
2165
2166         dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=2\n", pll_code.m_bp,
2167                 (pll_code.m_bp ? 0 : pll_code.m_code), pll_code.n_code);
2168
2169         snd_soc_write(codec, RT5640_PLL_CTRL1,
2170                 pll_code.n_code << RT5640_PLL_N_SFT | pll_code.k_code);
2171         snd_soc_write(codec, RT5640_PLL_CTRL2,
2172                 (pll_code.m_bp ? 0 : pll_code.m_code) << RT5640_PLL_M_SFT |
2173                 pll_code.m_bp << RT5640_PLL_M_BP_SFT);
2174
2175         rt5640->pll_in = freq_in;
2176         rt5640->pll_out = freq_out;
2177         rt5640->pll_src = source;
2178
2179         return 0;
2180 }
2181
2182 /**
2183  * rt5640_index_show - Dump private registers.
2184  * @dev: codec device.
2185  * @attr: device attribute.
2186  * @buf: buffer for display.
2187  *
2188  * To show non-zero values of all private registers.
2189  *
2190  * Returns buffer length.
2191  */
2192 static ssize_t rt5640_index_show(struct device *dev,
2193         struct device_attribute *attr, char *buf)
2194 {
2195         struct i2c_client *client = to_i2c_client(dev);
2196         struct rt5640_priv *rt5640 = i2c_get_clientdata(client);
2197         struct snd_soc_codec *codec = rt5640->codec;
2198         unsigned int val;
2199         int cnt = 0, i;
2200
2201         cnt += sprintf(buf, "RT5640 index register\n");
2202         for (i = 0; i < 0xb4; i++) {
2203                 if (cnt + 9 >= PAGE_SIZE - 1)
2204                         break;
2205                 val = rt5640_index_read(codec, i);
2206                 if (!val)
2207                         continue;
2208                 cnt += snprintf(buf + cnt, 10, "%02x: %04x\n", i, val);
2209         }
2210
2211         if (cnt >= PAGE_SIZE)
2212                 cnt = PAGE_SIZE - 1;
2213
2214         return cnt;
2215 }
2216 static DEVICE_ATTR(index_reg, 0444, rt5640_index_show, NULL);
2217
2218 static int rt5640_set_bias_level(struct snd_soc_codec *codec,
2219                         enum snd_soc_bias_level level)
2220 {
2221         switch (level) {
2222         case SND_SOC_BIAS_ON:
2223 #ifdef RT5640_DEMO
2224                 snd_soc_update_bits(codec, RT5640_SPK_VOL,
2225                         RT5640_L_MUTE | RT5640_R_MUTE, 0);
2226                 snd_soc_update_bits(codec, RT5640_HP_VOL,
2227                         RT5640_L_MUTE | RT5640_R_MUTE, 0);
2228                 break;
2229 #endif
2230         case SND_SOC_BIAS_PREPARE:
2231 #ifdef RT5640_DEMO
2232                 snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
2233                         RT5640_PWR_VREF1 | RT5640_PWR_MB |
2234                         RT5640_PWR_BG | RT5640_PWR_VREF2,
2235                         RT5640_PWR_VREF1 | RT5640_PWR_MB |
2236                         RT5640_PWR_BG | RT5640_PWR_VREF2);
2237                 msleep(100);
2238
2239                 snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
2240                         RT5640_PWR_FV1 | RT5640_PWR_FV2,
2241                         RT5640_PWR_FV1 | RT5640_PWR_FV2);
2242
2243                 snd_soc_update_bits(codec, RT5640_PWR_ANLG2,
2244                         RT5640_PWR_MB1 | RT5640_PWR_MB2,
2245                         RT5640_PWR_MB1 | RT5640_PWR_MB2);
2246 #endif
2247                 break;
2248
2249         case SND_SOC_BIAS_STANDBY:
2250 #ifdef RT5640_DEMO
2251                 snd_soc_update_bits(codec, RT5640_SPK_VOL, RT5640_L_MUTE |
2252                         RT5640_R_MUTE, RT5640_L_MUTE | RT5640_R_MUTE);
2253                 snd_soc_update_bits(codec, RT5640_HP_VOL, RT5640_L_MUTE |
2254                         RT5640_R_MUTE, RT5640_L_MUTE | RT5640_R_MUTE);
2255
2256                 snd_soc_update_bits(codec, RT5640_PWR_ANLG2,
2257                         RT5640_PWR_MB1 | RT5640_PWR_MB2,
2258                         0);
2259 #endif
2260                 if (SND_SOC_BIAS_OFF == codec->dapm.bias_level) {
2261                         snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
2262                                 RT5640_PWR_VREF1 | RT5640_PWR_MB |
2263                                 RT5640_PWR_BG | RT5640_PWR_VREF2,
2264                                 RT5640_PWR_VREF1 | RT5640_PWR_MB |
2265                                 RT5640_PWR_BG | RT5640_PWR_VREF2);
2266                         msleep(10);
2267                         snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
2268                                 RT5640_PWR_FV1 | RT5640_PWR_FV2,
2269                                 RT5640_PWR_FV1 | RT5640_PWR_FV2);
2270                         codec->cache_only = false;
2271                         snd_soc_cache_sync(codec);
2272                 }
2273                 break;
2274
2275         case SND_SOC_BIAS_OFF:
2276 #ifdef RT5640_DEMO
2277                 snd_soc_update_bits(codec, RT5640_SPK_VOL, RT5640_L_MUTE |
2278                         RT5640_R_MUTE, RT5640_L_MUTE | RT5640_R_MUTE);
2279                 snd_soc_update_bits(codec, RT5640_HP_VOL, RT5640_L_MUTE |
2280                         RT5640_R_MUTE, RT5640_L_MUTE | RT5640_R_MUTE);
2281                 snd_soc_update_bits(codec, RT5640_OUTPUT, RT5640_L_MUTE |
2282                         RT5640_R_MUTE, RT5640_L_MUTE | RT5640_R_MUTE);
2283                 snd_soc_update_bits(codec, RT5640_MONO_OUT,
2284                         RT5640_L_MUTE, RT5640_L_MUTE);
2285 #endif
2286                 snd_soc_write(codec, RT5640_PWR_DIG1, 0x0000);
2287                 snd_soc_write(codec, RT5640_PWR_DIG2, 0x0000);
2288                 snd_soc_write(codec, RT5640_PWR_VOL, 0x0000);
2289                 snd_soc_write(codec, RT5640_PWR_MIXER, 0x0000);
2290                 snd_soc_write(codec, RT5640_PWR_ANLG1, 0x0000);
2291                 snd_soc_write(codec, RT5640_PWR_ANLG2, 0x0000);
2292                 break;
2293
2294         default:
2295                 break;
2296         }
2297         codec->dapm.bias_level = level;
2298
2299         return 0;
2300 }
2301
2302 static int rt5640_probe(struct snd_soc_codec *codec)
2303 {
2304         struct rt5640_priv *rt5640 = snd_soc_codec_get_drvdata(codec);
2305         int ret;
2306         u16 val;
2307
2308         ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
2309         if (ret != 0) {
2310                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
2311                 return ret;
2312         }
2313
2314         val = snd_soc_read(codec, RT5640_RESET);
2315         if (val != rt5640_reg[RT5640_RESET]) {
2316                 dev_err(codec->dev,
2317                         "Device with ID register %x is not a rt5640\n", val);
2318                 return -ENODEV;
2319         }
2320
2321         rt5640_reset(codec);
2322         snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
2323                 RT5640_PWR_VREF1 | RT5640_PWR_MB |
2324                 RT5640_PWR_BG | RT5640_PWR_VREF2,
2325                 RT5640_PWR_VREF1 | RT5640_PWR_MB |
2326                 RT5640_PWR_BG | RT5640_PWR_VREF2);
2327         msleep(100);
2328         snd_soc_update_bits(codec, RT5640_PWR_ANLG1,
2329                 RT5640_PWR_FV1 | RT5640_PWR_FV2,
2330                 RT5640_PWR_FV1 | RT5640_PWR_FV2);
2331         /* DMIC */
2332         if (rt5640->dmic_en == RT5640_DMIC1) {
2333                 snd_soc_update_bits(codec, RT5640_GPIO_CTRL1,
2334                         RT5640_GP2_PIN_MASK, RT5640_GP2_PIN_DMIC1_SCL);
2335                 snd_soc_update_bits(codec, RT5640_DMIC,
2336                         RT5640_DMIC_1L_LH_MASK | RT5640_DMIC_1R_LH_MASK,
2337                         RT5640_DMIC_1L_LH_FALLING | RT5640_DMIC_1R_LH_RISING);
2338         } else if (rt5640->dmic_en == RT5640_DMIC2) {
2339                 snd_soc_update_bits(codec, RT5640_GPIO_CTRL1,
2340                         RT5640_GP2_PIN_MASK, RT5640_GP2_PIN_DMIC1_SCL);
2341                 snd_soc_update_bits(codec, RT5640_DMIC,
2342                         RT5640_DMIC_2L_LH_MASK | RT5640_DMIC_2R_LH_MASK,
2343                         RT5640_DMIC_2L_LH_FALLING | RT5640_DMIC_2R_LH_RISING);
2344         }
2345
2346 #ifdef RT5640_DEMO
2347         rt5640_reg_init(codec);
2348 #endif
2349
2350
2351 #if (CONFIG_SND_SOC_RT5642_MODULE | CONFIG_SND_SOC_RT5642)
2352         rt5640_register_dsp(codec);
2353 #endif
2354
2355         codec->dapm.bias_level = SND_SOC_BIAS_STANDBY;
2356
2357         snd_soc_add_controls(codec, rt5640_snd_controls,
2358                 ARRAY_SIZE(rt5640_snd_controls));
2359
2360         rt5640->codec = codec;
2361         ret = device_create_file(codec->dev, &dev_attr_index_reg);
2362         if (ret != 0) {
2363                 dev_err(codec->dev,
2364                         "Failed to create index_reg sysfs files: %d\n", ret);
2365                 return ret;
2366         }
2367
2368         return 0;
2369 }
2370
2371 static int rt5640_remove(struct snd_soc_codec *codec)
2372 {
2373         rt5640_set_bias_level(codec, SND_SOC_BIAS_OFF);
2374         rt5640_reset(codec);
2375         snd_soc_write(codec, RT5640_PWR_ANLG1, 0);
2376
2377         return 0;
2378 }
2379 #ifdef CONFIG_PM
2380 static int rt5640_suspend(struct snd_soc_codec *codec, pm_message_t state)
2381 {
2382         rt5640_set_bias_level(codec, SND_SOC_BIAS_OFF);
2383         snd_soc_write(codec, RT5640_PWR_ANLG1, 0);
2384
2385         return 0;
2386 }
2387
2388 static int rt5640_resume(struct snd_soc_codec *codec)
2389 {
2390         rt5640_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2391
2392         return 0;
2393 }
2394 #else
2395 #define rt5640_suspend NULL
2396 #define rt5640_resume NULL
2397 #endif
2398
2399 #define RT5640_STEREO_RATES SNDRV_PCM_RATE_8000_96000
2400 #define RT5640_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
2401                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
2402
2403 struct snd_soc_dai_ops rt5640_aif_dai_ops = {
2404         .hw_params = rt5640_hw_params,
2405         .prepare = rt5640_prepare,
2406         .set_fmt = rt5640_set_dai_fmt,
2407         .set_sysclk = rt5640_set_dai_sysclk,
2408         .set_pll = rt5640_set_dai_pll,
2409 };
2410
2411 struct snd_soc_dai_driver rt5640_dai[] = {
2412         {
2413                 .name = "rt5640-aif1",
2414                 .id = RT5640_AIF1,
2415                 .playback = {
2416                         .stream_name = "AIF1 Playback",
2417                         .channels_min = 1,
2418                         .channels_max = 2,
2419                         .rates = RT5640_STEREO_RATES,
2420                         .formats = RT5640_FORMATS,
2421                 },
2422                 .capture = {
2423                         .stream_name = "AIF1 Capture",
2424                         .channels_min = 1,
2425                         .channels_max = 2,
2426                         .rates = RT5640_STEREO_RATES,
2427                         .formats = RT5640_FORMATS,
2428                 },
2429                 .ops = &rt5640_aif_dai_ops,
2430         },
2431         {
2432                 .name = "rt5640-aif2",
2433                 .id = RT5640_AIF2,
2434                 .playback = {
2435                         .stream_name = "AIF2 Playback",
2436                         .channels_min = 1,
2437                         .channels_max = 2,
2438                         .rates = RT5640_STEREO_RATES,
2439                         .formats = RT5640_FORMATS,
2440                 },
2441                 .capture = {
2442                         .stream_name = "AIF2 Capture",
2443                         .channels_min = 1,
2444                         .channels_max = 2,
2445                         .rates = RT5640_STEREO_RATES,
2446                         .formats = RT5640_FORMATS,
2447                 },
2448                 .ops = &rt5640_aif_dai_ops,
2449         },
2450 #if (CONFIG_SND_SOC_RT5643_MODULE | CONFIG_SND_SOC_RT5643 | \
2451                         CONFIG_SND_SOC_RT5646_MODULE | CONFIG_SND_SOC_RT5646)
2452         {
2453                 .name = "rt5640-aif3",
2454                 .id = RT5640_AIF3,
2455                 .playback = {
2456                         .stream_name = "AIF3 Playback",
2457                         .channels_min = 1,
2458                         .channels_max = 2,
2459                         .rates = RT5640_STEREO_RATES,
2460                         .formats = RT5640_FORMATS,
2461                 },
2462                 .capture = {
2463                         .stream_name = "AIF3 Capture",
2464                         .channels_min = 1,
2465                         .channels_max = 2,
2466                         .rates = RT5640_STEREO_RATES,
2467                         .formats = RT5640_FORMATS,
2468                 },
2469                 .ops = &rt5640_aif_dai_ops,
2470         },
2471 #endif
2472 };
2473
2474 static struct snd_soc_codec_driver soc_codec_dev_rt5640 = {
2475         .probe = rt5640_probe,
2476         .remove = rt5640_remove,
2477         .suspend = rt5640_suspend,
2478         .resume = rt5640_resume,
2479         .set_bias_level = rt5640_set_bias_level,
2480         .reg_cache_size = RT5640_VENDOR_ID2 + 1,
2481         .reg_word_size = sizeof(u16),
2482         .reg_cache_default = rt5640_reg,
2483         .volatile_register = rt5640_volatile_register,
2484         .readable_register = rt5640_readable_register,
2485         .reg_cache_step = 1,
2486         .dapm_widgets = rt5640_dapm_widgets,
2487         .num_dapm_widgets = ARRAY_SIZE(rt5640_dapm_widgets),
2488         .dapm_routes = rt5640_dapm_routes,
2489         .num_dapm_routes = ARRAY_SIZE(rt5640_dapm_routes),
2490 };
2491
2492 static const struct i2c_device_id rt5640_i2c_id[] = {
2493         { "rt5640", 0 },
2494         { }
2495 };
2496 MODULE_DEVICE_TABLE(i2c, rt5640_i2c_id);
2497
2498 static int rt5640_i2c_probe(struct i2c_client *i2c,
2499                     const struct i2c_device_id *id)
2500 {
2501         struct rt5640_priv *rt5640;
2502         int ret;
2503
2504         rt5640 = kzalloc(sizeof(struct rt5640_priv), GFP_KERNEL);
2505         if (NULL == rt5640)
2506                 return -ENOMEM;
2507
2508         i2c_set_clientdata(i2c, rt5640);
2509
2510         ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5640,
2511                         rt5640_dai, ARRAY_SIZE(rt5640_dai));
2512         if (ret < 0)
2513                 kfree(rt5640);
2514
2515         return ret;
2516 }
2517
2518 static __devexit int rt5640_i2c_remove(struct i2c_client *i2c)
2519 {
2520         snd_soc_unregister_codec(&i2c->dev);
2521         kfree(i2c_get_clientdata(i2c));
2522         return 0;
2523 }
2524
2525 struct i2c_driver rt5640_i2c_driver = {
2526         .driver = {
2527                 .name = "rt5640",
2528                 .owner = THIS_MODULE,
2529         },
2530         .probe = rt5640_i2c_probe,
2531         .remove   = __devexit_p(rt5640_i2c_remove),
2532         .id_table = rt5640_i2c_id,
2533 };
2534
2535 static int __init rt5640_modinit(void)
2536 {
2537         return i2c_add_driver(&rt5640_i2c_driver);
2538 }
2539 module_init(rt5640_modinit);
2540
2541 static void __exit rt5640_modexit(void)
2542 {
2543         i2c_del_driver(&rt5640_i2c_driver);
2544 }
2545 module_exit(rt5640_modexit);
2546
2547 MODULE_DESCRIPTION("ASoC RT5640 driver");
2548 MODULE_AUTHOR("Johnny Hsu <johnnyhsu@realtek.com>");
2549 MODULE_LICENSE("GPL");