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