UPSTREAM: ASoC: da7219: Correct BCLK inversion for DSP DAI format mode
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / rt3261-dsp.c
1 /*
2  * rt3261.c  --  RT3261 ALSA SoC DSP driver
3  *
4  * Copyright 2011 Realtek Semiconductor Corp.
5  * Author: Johnny Hsu <johnnyhsu@realtek.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/platform_device.h>
15 #include <sound/soc.h>
16 #include <sound/soc-dapm.h>
17
18 #define RTK_IOCTL
19 #ifdef RTK_IOCTL
20 #include <linux/spi/spi.h>
21 #include "rt_codec_ioctl.h"
22 #endif
23
24 #include "rt3261.h"
25 #include "rt3261-dsp.h"
26
27 #define INIT_DSP_IN_PROBE
28
29 static const u16 rt3261_dsp_init[][2] = {
30         {0x3fd2, 0x0038}, {0x229C, 0x0fa0}, {0x22d2, 0x8400}, {0x22ee, 0x0000},
31         {0x22f2, 0x0040}, {0x22f5, 0x8000}, {0x22f6, 0x0000}, {0x22f9, 0x007f},
32         {0x2310, 0x0880},
33 };
34 #define RT3261_DSP_INIT_NUM \
35         (sizeof(rt3261_dsp_init) / sizeof(rt3261_dsp_init[0]))
36
37 static const u16 rt3261_dsp_48[][2] = {
38         {0x22c8, 0x0026}, {0x22fe, 0x0fa0}, {0x22ff, 0x3893}, {0x22fa, 0x2487},
39         {0x2301, 0x0002},
40 };
41 #define RT3261_DSP_48_NUM (sizeof(rt3261_dsp_48) / sizeof(rt3261_dsp_48[0]))
42
43 static const u16 rt3261_dsp_441[][2] = {
44         {0x22c6, 0x0031}, {0x22c7, 0x0050}, {0x22c8, 0x0009}, {0x22fe, 0x0e5b},
45         {0x22ff, 0x3883}, {0x22fa, 0x2484}, {0x2301, 0x0001},
46 };
47 #define RT3261_DSP_441_NUM (sizeof(rt3261_dsp_441) / sizeof(rt3261_dsp_441[0]))
48
49 static const u16 rt3261_dsp_16[][2] = {
50         {0x22c8, 0x0026}, {0x22fa, 0x2484}, {0x2301, 0x0002},
51 };
52 #define RT3261_DSP_16_NUM (sizeof(rt3261_dsp_16) / sizeof(rt3261_dsp_16[0]))
53
54 static const u16 rt3261_dsp_rate_tab[] = {
55         0x22c6, 0x22c7, 0x22c8, 0x22fe, 0x22ff, 0x22fa, 0x2301,
56 };
57 #define RT3261_DSP_RATE_NUM (sizeof(rt3261_dsp_rate_tab) / sizeof(rt3261_dsp_rate_tab[0]))
58
59 static const u16 rt3261_dsp_aec_ns_fens[][2] = {
60         {0x22F8, 0x8005},
61         {0x2309, 0x0400},
62         {0x2310, 0x0824},
63         {0x2332, 0x0080},
64         {0x2348, 0x1000},
65         {0x2362, 0x0180},
66         {0x236E, 0x1800},
67         {0x2373, 0x3000},
68         {0x2381, 0x4000},
69         {0x2398, 0x0020},
70         {0x23B4, 0x0012},
71         {0x23BD, 0x0100},
72         {0x23D1, 0xFF80},
73         {0x23D5, 0x7FFF},
74         {0x2303, 0x0931},
75         {0x2333, 0x0008},
76         {0x2349, 0x6800},
77         {0x2363, 0x0100},
78         {0x236F, 0x0A0A},
79         {0x2374, 0x2400},
80         {0x2382, 0x0400},
81         {0x238C, 0x0400},
82         {0x23A5, 0x0006},
83         {0x23B7, 0x0008},
84         {0x23BE, 0x2400},
85         {0x23D2, 0xFF80},
86         {0x23ED, 0x0300},
87         {0x2304, 0xC31F},
88         {0x230C, 0x0900},
89         {0x2337, 0x0002},
90         {0x2360, 0x0080},
91         {0x2364, 0x0078},
92         {0x2370, 0x0F00},
93         {0x2375, 0x1800},
94         {0x2383, 0x0400},
95         {0x2396, 0x2000},
96         {0x23A6, 0x7FFF},
97         {0x23BB, 0x1000},
98         {0x23CF, 0x7FFF},
99         {0x2305, 0x0005},
100         {0x232F, 0x0080},
101         {0x2339, 0x0010},
102         {0x2361, 0x1800},
103         {0x2380, 0x7FFF},
104         {0x2384, 0x0005},
105         {0x23B3, 0x0018},
106         {0x23BC, 0x0130},
107         {0x2328, 0x7FFF},
108         {0x233A, 0x7FFF},
109         {0x233B, 0x7FFF},
110         {0x233C, 0x7FFF},
111         {0x2302, 0x0101},
112         {0x22F2, 0x0040},
113         {0x230A, 0x1B00},
114         {0x22FB, 0x0000},
115 };
116
117 #define RT3261_DSP_AEC_NUM \
118         (sizeof(rt3261_dsp_aec_ns_fens) / sizeof(rt3261_dsp_aec_ns_fens[0]))
119 static const u16 rt3261_dsp_hfbf[][2] = {
120         {0x22f8, 0x8004}, {0x22a0, 0x1205}, {0x22a1, 0x0f00}, {0x22a2, 0x1000},
121         {0x22a3, 0x1000}, {0x22a4, 0x1000}, {0x22aa, 0x0006}, {0x22ad, 0x0060},
122         {0x22ae, 0x0080}, {0x22af, 0x0000}, {0x22b0, 0x000e}, {0x22b1, 0x0010},
123         {0x22b2, 0x0006}, {0x22b3, 0x0001}, {0x22b4, 0x0010}, {0x22b5, 0x0001},
124         {0x22b7, 0x0005}, {0x22d8, 0x0017}, {0x22f9, 0x007f}, {0x2303, 0x0971},
125         {0x2304, 0x0302}, {0x2303, 0x0971}, {0x2304, 0x4302}, {0x2305, 0x102d},
126         {0x2309, 0x0400}, {0x230c, 0x0400}, {0x230d, 0x0200}, {0x232f, 0x0020},
127         {0x2332, 0x0100}, {0x2333, 0x0020}, {0x2337, 0xffff}, {0x2339, 0x0010},
128         {0x2348, 0x1000}, {0x2349, 0x1000}, {0x236e, 0x1800}, {0x236f, 0x1006},
129         {0x2370, 0x1000}, {0x2372, 0x0200}, {0x237b, 0x001e}, {0x2380, 0x7fff},
130         {0x2381, 0x4000}, {0x2382, 0x0080}, {0x2383, 0x0200}, {0x2386, 0x7f80},
131         {0x2387, 0x0040}, {0x238a, 0x0280}, {0x238c, 0x6000}, {0x238e, 0x5000},
132         {0x2396, 0x6a00}, {0x2397, 0x6000}, {0x2398, 0x00e0}, {0x23a5, 0x0005},
133         {0x23b3, 0x000f}, {0x23b4, 0x0003}, {0x23bb, 0x2000}, {0x23bc, 0x00d0},
134         {0x23bd, 0x0140}, {0x23be, 0x1000}, {0x23cf, 0x0800}, {0x23d0, 0x0400},
135         {0x23d1, 0x0100}, {0x23d2, 0x0100}, {0x23d5, 0x7c00}, {0x23ed, 0x0300},
136         {0x23ee, 0x3000}, {0x23ef, 0x2800}, {0x22fb, 0x0000},
137 };
138 #define RT3261_DSP_HFBF_NUM \
139         (sizeof(rt3261_dsp_hfbf) / sizeof(rt3261_dsp_hfbf[0]))
140
141 static const u16 rt3261_dsp_ffp[][2] = {
142         {0x22f8, 0x8005}, {0x2303, 0x1971}, {0x2304, 0x8312}, {0x2305, 0x0005},
143         {0x2309, 0x0200}, {0x230a, 0x1b00}, {0x230c, 0x0800}, {0x230d, 0x0400},
144         {0x2325, 0x5000}, {0x2326, 0x0040}, {0x232f, 0x0080}, {0x2332, 0x0100},
145         {0x2333, 0x0020}, {0x2337, 0x0001}, {0x2339, 0x0010}, {0x233c, 0x0040},
146         {0x2348, 0x1000}, {0x2349, 0x1000}, {0x2360, 0x0180}, {0x2361, 0x1800},
147         {0x2362, 0x0200}, {0x2363, 0x0200}, {0x2364, 0x0200}, {0x2365, 0x2000},
148         {0x236e, 0x1000}, {0x236f, 0x0a05}, {0x2370, 0x0f00}, {0x2372, 0x1a00},
149         {0x2373, 0x3000}, {0x2374, 0x2400}, {0x2375, 0x1800}, {0x2380, 0x7fff},
150         {0x2381, 0x4000}, {0x2382, 0x0400}, {0x2383, 0x0400}, {0x2384, 0x0005},
151         {0x2385, 0x0005}, {0x238e, 0x7000}, {0x2393, 0x4444}, {0x2394, 0x4444},
152         {0x2395, 0x4444}, {0x2396, 0x2000}, {0x2397, 0x3000}, {0x2398, 0x0020},
153         {0x23a5, 0x0006}, {0x23a6, 0x7fff}, {0x23b3, 0x000a}, {0x23b4, 0x0006},
154         {0x23b7, 0x0008}, {0x23bb, 0x1000}, {0x23bc, 0x0130}, {0x23bd, 0x0160},
155         {0x23be, 0x2400}, {0x23cf, 0x0800}, {0x23d0, 0x0400}, {0x23d1, 0xff80},
156         {0x23d2, 0xff80}, {0x23d3, 0x2000}, {0x23d4, 0x5000}, {0x23d5, 0x5000},
157         {0x23e7, 0x0c00}, {0x23e8, 0x1400}, {0x23e9, 0x6000}, {0x23ea, 0x7f00},
158         {0x23ed, 0x0300}, {0x23ee, 0x2800}, {0x22fb, 0x0000},
159 };
160 #define RT3261_DSP_FFP_NUM (sizeof(rt3261_dsp_ffp) / sizeof(rt3261_dsp_ffp[0]))
161
162 static const u16 rt3261_dsp_p3_tab[][3] = {
163         {0x4af0, 0x1000, 0x822b}, {0x90f0, 0x1001, 0x8393},
164         {0x64f0, 0x1002, 0x822b}, {0x0ff0, 0x1003, 0x26e0},
165         {0x55f0, 0x1004, 0x2200}, {0xcff0, 0x1005, 0x1a7b},
166         {0x5af0, 0x1006, 0x823a}, {0x90f0, 0x1007, 0x8393},
167         {0x64f0, 0x1008, 0x822b}, {0x0ff0, 0x1009, 0x26e0},
168         {0x03f0, 0x100a, 0x2218}, {0x0ef0, 0x100b, 0x3400},
169         {0x4ff0, 0x100c, 0x195e}, {0x00f0, 0x100d, 0x0000},
170         {0xf0f0, 0x100e, 0x8143}, {0x1ff0, 0x100f, 0x2788},
171         {0x0ef0, 0x1010, 0x3400}, {0xe0f0, 0x1011, 0x1a26},
172         {0x2cf0, 0x1012, 0x8001}, {0x0ff0, 0x1013, 0x267c},
173         {0x82f0, 0x1014, 0x1a27}, {0x3cf0, 0x1015, 0x8001},
174         {0x0ff0, 0x1016, 0x267c}, {0x82f0, 0x1017, 0x1a27},
175         {0xeff0, 0x1018, 0x1a26}, {0x01f0, 0x1019, 0x4ff0},
176         {0x5cf0, 0x101a, 0x2b81}, {0xfaf0, 0x101b, 0x2a6a},
177         {0x05f0, 0x101c, 0x4011}, {0x0ff0, 0x101d, 0x278e},
178         {0x0ef0, 0x101e, 0x3400}, {0xe1f0, 0x101f, 0x1997},
179         {0x1ff0, 0x1020, 0x1997}, {0x03f0, 0x1021, 0x2279},
180         {0xb8f0, 0x1022, 0x8206}, {0xf8f0, 0x1023, 0x0f00},
181         {0xfff0, 0x1024, 0x279e}, {0x0ff0, 0x1025, 0x2272},
182         {0x0ef0, 0x1026, 0x3400}, {0x3ff0, 0x1027, 0x199a},
183         {0x0ff0, 0x1028, 0x2262}, {0x0ff0, 0x1029, 0x2272},
184         {0x0ef0, 0x102a, 0x3400}, {0xfff0, 0x102b, 0x199a},
185         {0x7ff0, 0x102c, 0x22e2}, {0x0ef0, 0x102d, 0x3400},
186         {0xfff0, 0x102e, 0x19cb}, {0xfff0, 0x102f, 0x47ff},
187         {0xb1f0, 0x1030, 0x80b1}, {0x5ff0, 0x1031, 0x2261},
188         {0x62f0, 0x1032, 0x1903}, {0x9af0, 0x1033, 0x0d00},
189         {0xcff0, 0x1034, 0x80b1}, {0x0ff0, 0x1035, 0x0e27},
190         {0x8ff0, 0x1036, 0x9229}, {0x0ef0, 0x1037, 0x3400},
191         {0xaff0, 0x1038, 0x19f5}, {0x81f0, 0x1039, 0x8229},
192         {0x0ef0, 0x103a, 0x3400}, {0xfff0, 0x103b, 0x19f6},
193         {0x5af0, 0x103c, 0x8234}, {0xeaf0, 0x103d, 0x9113},
194         {0x0ef0, 0x103e, 0x3400}, {0x7ff0, 0x103f, 0x19ea},
195         {0x8af0, 0x1040, 0x924d}, {0x08f0, 0x1041, 0x3400},
196         {0x3ff0, 0x1042, 0x1a74}, {0x00f0, 0x1043, 0x0000},
197         {0x00f0, 0x1044, 0x0000}, {0x00f0, 0x1045, 0x0c38},
198         {0x0ff0, 0x1046, 0x2618}, {0xb0f0, 0x1047, 0x8148},
199         {0x01f0, 0x1048, 0x3700}, {0x02f0, 0x1049, 0x3a70},
200         {0x03f0, 0x104a, 0x3a78}, {0x9af0, 0x104b, 0x8229},
201         {0xd6f0, 0x104c, 0x47c4}, {0x95f0, 0x104d, 0x4361},
202         {0x0ff0, 0x104e, 0x2082}, {0x76f0, 0x104f, 0x626b},
203         {0x0ff0, 0x1050, 0x208a}, {0x0ff0, 0x1051, 0x204a},
204         {0xc9f0, 0x1052, 0x7882}, {0x75f0, 0x1053, 0x626b},
205         {0x0ff0, 0x1054, 0x208a}, {0x0ff0, 0x1055, 0x204a},
206         {0xcdf0, 0x1056, 0x7882}, {0x0ff0, 0x1057, 0x2630},
207         {0x8af0, 0x1058, 0x2b30}, {0xf4f0, 0x1059, 0x1904},
208         {0x98f0, 0x105a, 0x9229}, {0x0ef0, 0x105b, 0x3400},
209         {0xeff0, 0x105c, 0x19fd}, {0xd7f0, 0x105d, 0x40cc},
210         {0x0ef0, 0x105e, 0x3400}, {0xdff0, 0x105f, 0x1a44},
211         {0x00f0, 0x1060, 0x0000}, {0xcef0, 0x1061, 0x1507},
212         {0x90f0, 0x1062, 0x1020}, {0x5ff0, 0x1063, 0x1006},
213         {0x89f0, 0x1064, 0x608f}, {0x0ff0, 0x1065, 0x0e64},
214         {0x49f0, 0x1066, 0x1044}, {0xcff0, 0x1067, 0x2b28},
215         {0x93f0, 0x1068, 0x2a62}, {0x5ff0, 0x1069, 0x266a},
216         {0x54f0, 0x106a, 0x22a8}, {0x0af0, 0x106b, 0x0f22},
217         {0xfbf0, 0x106c, 0x0f0c}, {0x5ff0, 0x106d, 0x0d00},
218         {0x90f0, 0x106e, 0x1020}, {0x4ff0, 0x106f, 0x1006},
219         {0x8df0, 0x1070, 0x6087}, {0x0ff0, 0x1071, 0x0e64},
220         {0xb9f0, 0x1072, 0x1044}, {0xcff0, 0x1073, 0x2a63},
221         {0x5ff0, 0x1074, 0x266a}, {0x54f0, 0x1075, 0x22a8},
222         {0x0af0, 0x1076, 0x0f22}, {0xfbf0, 0x1077, 0x0f0c},
223         {0x93f0, 0x1078, 0x2aef}, {0x0ff0, 0x1079, 0x227a},
224         {0xc2f0, 0x107a, 0x1907}, {0xf5f0, 0x107b, 0x0d00},
225         {0xfdf0, 0x107c, 0x7800}, {0x0ef0, 0x107d, 0x3400},
226         {0xaff0, 0x107e, 0x1899},
227 };
228 #define RT3261_DSP_PATCH3_NUM \
229         (sizeof(rt3261_dsp_p3_tab) / sizeof(rt3261_dsp_p3_tab[0]))
230
231 static const u16 rt3261_dsp_p2_tab[][2] = {
232         {0x3fa1, 0xe7bb}, {0x3fb1, 0x5000}, {0x3fa2, 0xa26b}, {0x3fb2, 0x500e},
233         {0x3fa3, 0xa27c}, {0x3fb3, 0x2282}, {0x3fa4, 0x996e}, {0x3fb4, 0x5019},
234         {0x3fa5, 0x99a2}, {0x3fb5, 0x5021}, {0x3fa6, 0x99ae}, {0x3fb6, 0x5028},
235         {0x3fa7, 0x9cbb}, {0x3fb7, 0x502c}, {0x3fa8, 0x9900}, {0x3fb8, 0x1903},
236         {0x3fa9, 0x9f59}, {0x3fb9, 0x502f}, {0x3faa, 0x9f6e}, {0x3fba, 0x5039},
237         {0x3fab, 0x9ea2}, {0x3fbb, 0x503c}, {0x3fac, 0x9fc8}, {0x3fbc, 0x5045},
238         {0x3fad, 0xa44c}, {0x3fbd, 0x505d}, {0x3fae, 0x8983}, {0x3fbe, 0x5061},
239         {0x3faf, 0x95e3}, {0x3fbf, 0x5006}, {0x3fa0, 0xe742}, {0x3fb0, 0x5040},
240 };
241 #define RT3261_DSP_PATCH2_NUM \
242         (sizeof(rt3261_dsp_p2_tab) / sizeof(rt3261_dsp_p2_tab[0]))
243
244 /**
245  * rt3261_dsp_done - Wait until DSP is ready.
246  * @codec: SoC Audio Codec device.
247  *
248  * To check voice DSP status and confirm it's ready for next work.
249  *
250  * Returns 0 for success or negative error code.
251  */
252 static int rt3261_dsp_done(struct snd_soc_codec *codec)
253 {
254         unsigned int count = 0, dsp_val;
255
256         dsp_val = snd_soc_read(codec, RT3261_DSP_CTRL3);
257         while(dsp_val & RT3261_DSP_BUSY_MASK) {
258                 if(count > 10)
259                         return -EBUSY;
260                 dsp_val = snd_soc_read(codec, RT3261_DSP_CTRL3);
261                 count ++;
262         }
263
264         return 0;
265 }
266
267
268 /**
269  * rt3261_dsp_write - Write DSP register.
270  * @codec: SoC audio codec device.
271  * @param: DSP parameters.
272   *
273  * Modify voice DSP register for sound effect. The DSP can be controlled
274  * through DSP command format (0xfc), addr (0xc4), data (0xc5) and cmd (0xc6)
275  * register. It has to wait until the DSP is ready.
276  *
277  * Returns 0 for success or negative error code.
278  */
279 int rt3261_dsp_write(struct snd_soc_codec *codec,
280                 struct rt3261_dsp_param *param)
281 {
282         unsigned int dsp_val = snd_soc_read(codec, RT3261_DSP_CTRL3);
283         int ret;
284
285         ret = rt3261_dsp_done(codec);
286         if (ret < 0) {
287                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
288                 goto err;
289         }
290         ret = snd_soc_write(codec, RT3261_GEN_CTRL3, param->cmd_fmt);
291         if (ret < 0) {
292                 dev_err(codec->dev, "Failed to write cmd format: %d\n", ret);
293                 goto err;
294         }
295         ret = snd_soc_write(codec, RT3261_DSP_CTRL1, param->addr);
296         if (ret < 0) {
297                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
298                 goto err;
299         }
300         ret = snd_soc_write(codec, RT3261_DSP_CTRL2, param->data);
301         if (ret < 0) {
302                 dev_err(codec->dev, "Failed to write DSP data reg: %d\n", ret);
303                 goto err;
304         }
305         dsp_val &= ~(RT3261_DSP_R_EN | RT3261_DSP_CMD_MASK);
306         dsp_val |= RT3261_DSP_W_EN | param->cmd;
307         ret = snd_soc_write(codec, RT3261_DSP_CTRL3, dsp_val);
308         if (ret < 0) {
309                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
310                 goto err;
311         }
312         //mdelay(10);
313         return 0;
314
315 err:
316         return ret;
317 }
318
319 EXPORT_SYMBOL_GPL(rt3261_dsp_write);
320
321 /**
322  * rt3261_dsp_read - Read DSP register.
323  * @codec: SoC audio codec device.
324  * @reg: DSP register index.
325  *
326  * Read DSP setting value from voice DSP. The DSP can be controlled
327  * through DSP addr (0xc4), data (0xc5) and cmd (0xc6) register. Each
328  * command has to wait until the DSP is ready.
329  *
330  * Returns DSP register value or negative error code.
331  */
332 unsigned int rt3261_dsp_read(
333         struct snd_soc_codec *codec, unsigned int reg)
334 {
335         unsigned int val_h, val_l, value;
336         unsigned int dsp_val = snd_soc_read(codec, RT3261_DSP_CTRL3);
337         int ret = 0;
338
339         ret = rt3261_dsp_done(codec);
340         if (ret < 0) {
341                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
342                 goto err;
343         }
344         ret = snd_soc_write(codec, RT3261_GEN_CTRL3, 0);
345         if (ret < 0) {
346                 dev_err(codec->dev, "Failed to write fc = 0: %d\n", ret);
347                 goto err;
348         }
349         ret = snd_soc_write(codec, RT3261_DSP_CTRL1, reg);
350         if (ret < 0) {
351                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
352                 goto err;
353         }
354         dsp_val &= ~(RT3261_DSP_W_EN | RT3261_DSP_CMD_MASK);
355         dsp_val |= RT3261_DSP_R_EN | RT3261_DSP_CMD_MR;
356         ret = snd_soc_write(codec, RT3261_DSP_CTRL3, dsp_val);
357         if (ret < 0) {
358                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
359                 goto err;
360         }
361
362         /* Read DSP high byte data */
363         ret = rt3261_dsp_done(codec);
364         if (ret < 0) {
365                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
366                 goto err;
367         }
368         ret = snd_soc_write(codec, RT3261_DSP_CTRL1, RT3261_DSP_REG_DATHI);
369         if (ret < 0) {
370                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
371                 goto err;
372         }
373         dsp_val &= ~(RT3261_DSP_W_EN | RT3261_DSP_CMD_MASK);
374         dsp_val |= RT3261_DSP_R_EN | RT3261_DSP_CMD_RR;
375         ret = snd_soc_write(codec, RT3261_DSP_CTRL3, dsp_val);
376         if (ret < 0) {
377                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
378                 goto err;
379         }
380         ret = rt3261_dsp_done(codec);
381         if (ret < 0) {
382                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
383                 goto err;
384         }
385         ret = snd_soc_read(codec, RT3261_DSP_CTRL2);
386         if (ret < 0) {
387                 dev_err(codec->dev, "Failed to read DSP data reg: %d\n", ret);
388                 goto err;
389         }
390         val_h = ret;
391
392         /* Read DSP low byte data */
393         ret = snd_soc_write(codec, RT3261_DSP_CTRL1, RT3261_DSP_REG_DATLO);
394         if (ret < 0) {
395                 dev_err(codec->dev, "Failed to write DSP addr reg: %d\n", ret);
396                 goto err;
397         }
398         ret = snd_soc_write(codec, RT3261_DSP_CTRL3, dsp_val);
399         if (ret < 0) {
400                 dev_err(codec->dev, "Failed to write DSP cmd reg: %d\n", ret);
401                 goto err;
402         }
403         ret = rt3261_dsp_done(codec);
404         if (ret < 0) {
405                 dev_err(codec->dev, "DSP is busy: %d\n", ret);
406                 goto err;
407         }
408         ret = snd_soc_read(codec, RT3261_DSP_CTRL2);
409         if (ret < 0) {
410                 dev_err(codec->dev, "Failed to read DSP data reg: %d\n", ret);
411                 goto err;
412         }
413         val_l = ret;
414
415         value = ((val_h & 0xff) << 8) |(val_l & 0xff);
416         return value;
417
418 err:
419         return ret;
420 }
421 EXPORT_SYMBOL_GPL(rt3261_dsp_read);
422
423 static int rt3261_dsp_get(struct snd_kcontrol *kcontrol,
424                 struct snd_ctl_elem_value *ucontrol)
425 {
426         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
427         struct rt3261_priv *rt3261 = snd_soc_codec_get_drvdata(codec);
428
429         ucontrol->value.integer.value[0] = rt3261->dsp_sw;
430
431         return 0;
432 }
433
434 static int rt3261_dsp_put(struct snd_kcontrol *kcontrol,
435                 struct snd_ctl_elem_value *ucontrol)
436 {
437         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
438         struct rt3261_priv *rt3261 = snd_soc_codec_get_drvdata(codec);
439
440         if (rt3261->dsp_sw != ucontrol->value.integer.value[0])
441                 rt3261->dsp_sw = ucontrol->value.integer.value[0];
442
443         return 0;
444 }
445
446 /* DSP Path Control 1 */
447 static const char *rt3261_src_rxdp_mode[] = {
448         "Normal", "Divided by 3"};
449
450 static const SOC_ENUM_SINGLE_DECL(
451         rt3261_src_rxdp_enum, RT3261_DSP_PATH1,
452         RT3261_RXDP_SRC_SFT, rt3261_src_rxdp_mode);
453
454 static const char *rt3261_src_txdp_mode[] = {
455         "Normal", "Multiplied by 3"};
456
457 static const SOC_ENUM_SINGLE_DECL(
458         rt3261_src_txdp_enum, RT3261_DSP_PATH1,
459         RT3261_TXDP_SRC_SFT, rt3261_src_txdp_mode);
460
461 /* DSP data select */
462 static const char *rt3261_dsp_data_select[] = {
463         "Normal", "left copy to right", "right copy to left", "Swap"};
464
465 static const SOC_ENUM_SINGLE_DECL(rt3261_rxdc_data_enum, RT3261_DSP_PATH2,
466                                 RT3261_RXDC_SEL_SFT, rt3261_dsp_data_select);
467
468 static const SOC_ENUM_SINGLE_DECL(rt3261_rxdp_data_enum, RT3261_DSP_PATH2,
469                                 RT3261_RXDP_SEL_SFT, rt3261_dsp_data_select);
470
471 static const SOC_ENUM_SINGLE_DECL(rt3261_txdc_data_enum, RT3261_DSP_PATH2,
472                                 RT3261_TXDC_SEL_SFT, rt3261_dsp_data_select);
473
474 static const SOC_ENUM_SINGLE_DECL(rt3261_txdp_data_enum, RT3261_DSP_PATH2,
475                                 RT3261_TXDP_SEL_SFT, rt3261_dsp_data_select);
476
477 /* Sound Effect */
478 static const char *rt3261_dsp_mode[] = {
479         "Disable", "AEC+NS+FENS", "HFBF", "Far Field Pick-up"};
480
481 static const SOC_ENUM_SINGLE_DECL(rt3261_dsp_enum, 0, 0, rt3261_dsp_mode);
482
483 static const char *rt3261_rxdp2_src[] =
484         {"IF2_DAC", "Stereo_ADC"};
485
486 static const SOC_ENUM_SINGLE_DECL(
487         rt3261_rxdp2_enum, RT3261_GEN_CTRL2,
488         RT3261_RXDP2_SEL_SFT, rt3261_rxdp2_src);
489
490 static const struct snd_kcontrol_new rt3261_rxdp2_mux =
491         SOC_DAPM_ENUM("RxDP2 sel", rt3261_rxdp2_enum);
492
493 static const char *rt3261_rxdp_src[] =
494         {"RxDP2", "RxDP1"};
495
496 static const SOC_ENUM_SINGLE_DECL(
497         rt3261_rxdp_enum, RT3261_DUMMY_PR3F,
498         10, rt3261_rxdp_src);
499
500 static const struct snd_kcontrol_new rt3261_rxdp_mux =
501         SOC_DAPM_ENUM("RxDP sel", rt3261_rxdp_enum);
502
503 static const char *rt3261_rxdc_src[] =
504         {"Mono_ADC", "Stereo_ADC"};
505
506 static const SOC_ENUM_SINGLE_DECL(
507         rt3261_rxdc_enum, RT3261_GEN_CTRL2,
508         RT3261_RXDC_SRC_SFT, rt3261_rxdc_src);
509
510 static const struct snd_kcontrol_new rt3261_rxdc_mux =
511         SOC_DAPM_ENUM("RxDC sel", rt3261_rxdc_enum);
512
513 static const char *rt3261_rxdp1_src[] =
514         {"DAC1", "IF1_DAC"};
515
516 static const SOC_ENUM_SINGLE_DECL(
517         rt3261_rxdp1_enum, RT3261_DUMMY_PR3F,
518         9, rt3261_rxdp1_src);
519
520 static const struct snd_kcontrol_new rt3261_rxdp1_mux =
521         SOC_DAPM_ENUM("RxDP1 sel", rt3261_rxdp1_enum);
522         
523 static const struct snd_kcontrol_new rt3261_dsp_snd_controls[] = {
524         SOC_ENUM("RxDC input data", rt3261_rxdc_data_enum),
525         SOC_ENUM("RxDP input data", rt3261_rxdp_data_enum),
526         SOC_ENUM("TxDC input data", rt3261_txdc_data_enum),
527         SOC_ENUM("TxDP input data", rt3261_txdp_data_enum),
528         SOC_ENUM("SRC for RxDP", rt3261_src_rxdp_enum),
529         SOC_ENUM("SRC for TxDP", rt3261_src_txdp_enum),
530         /* AEC */
531         SOC_ENUM_EXT("DSP Function Switch", rt3261_dsp_enum,
532                 rt3261_dsp_get, rt3261_dsp_put),
533 };
534
535 /*static int rt3261_dsp_patch_3(struct snd_soc_codec *codec)
536 {
537         struct rt3261_dsp_param param;
538         int ret, i;
539
540         param.cmd_fmt = 0x0090;
541         param.addr = 0x0064;
542         param.data = 0x0004;
543         param.cmd = RT3261_DSP_CMD_RW;
544         ret = rt3261_dsp_write(codec, &param);
545         if (ret < 0) {
546                 dev_err(codec->dev,
547                         "Fail to set DSP 3 bytes patch entrance: %d\n", ret);
548                 goto patch_err;
549         }
550
551         param.cmd = RT3261_DSP_CMD_PE;
552         for(i = 0; i < RT3261_DSP_PATCH3_NUM; i++) {
553                 param.cmd_fmt = rt3261_dsp_p3_tab[i][0];
554                 param.addr = rt3261_dsp_p3_tab[i][1];
555                 param.data = rt3261_dsp_p3_tab[i][2];
556                 ret = rt3261_dsp_write(codec, &param);
557                 if (ret < 0) {
558                         dev_err(codec->dev, "Fail to patch Dsp: %d\n", ret);
559                         goto patch_err;
560                 }
561         }
562
563         return 0;
564
565 patch_err:
566
567         return ret;
568 }
569
570 static int rt3261_dsp_patch_2(struct snd_soc_codec *codec)
571 {
572         struct rt3261_dsp_param param;
573         int ret, i;
574
575         param.cmd_fmt = 0x0090;
576         param.addr = 0x0064;
577         param.data = 0x0000;
578         param.cmd = RT3261_DSP_CMD_RW;
579         ret = rt3261_dsp_write(codec, &param);
580         if (ret < 0) {
581                 dev_err(codec->dev,
582                         "Fail to set DSP 2 bytes patch entrance: %d\n", ret);
583                 goto patch_err;
584         }
585
586         param.cmd_fmt = 0x00e0;
587         param.cmd = RT3261_DSP_CMD_MW;
588         for(i = 0; i < RT3261_DSP_PATCH2_NUM; i++) {
589                 param.addr = rt3261_dsp_p2_tab[i][0];
590                 param.data = rt3261_dsp_p2_tab[i][1];
591                 ret = rt3261_dsp_write(codec, &param);
592                 if (ret < 0) {
593                         dev_err(codec->dev, "Fail to patch Dsp: %d\n", ret);
594                         goto patch_err;
595                 }
596         }
597
598         return 0;
599
600 patch_err:
601
602         return ret;
603 }*/
604
605 /**
606  * rt3261_dsp_patch - Write DSP patch code.
607  *
608  * @codec: SoC audio codec device.
609  *
610  * Write patch codes to DSP including 3 and 2 bytes data.
611  *
612  * Returns 0 for success or negative error code.
613  */
614 /*static int rt3261_dsp_patch(struct snd_soc_codec *codec)
615 {
616         int ret;
617
618         dev_dbg(codec->dev, "\n DSP Patch Start ......\n");
619
620         ret = snd_soc_update_bits(codec, RT3261_MICBIAS,
621                 RT3261_PWR_CLK25M_MASK, RT3261_PWR_CLK25M_PU);
622         if (ret < 0)
623                 goto patch_err;
624
625         ret = snd_soc_update_bits(codec, RT3261_GLB_CLK,
626                 RT3261_SCLK_SRC_MASK, RT3261_SCLK_SRC_RCCLK);
627         if (ret < 0)
628                 goto patch_err;
629
630         ret = snd_soc_update_bits(codec, RT3261_PWR_DIG2,
631                 RT3261_PWR_I2S_DSP, RT3261_PWR_I2S_DSP);
632         if (ret < 0)
633                 goto patch_err;
634
635         ret = snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
636                 RT3261_DSP_PD_PIN_MASK, RT3261_DSP_PD_PIN_HI);
637         if (ret < 0) {
638                 dev_err(codec->dev, "Failed to power up DSP: %d\n", ret);
639                 goto patch_err;
640         }
641
642         ret = snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
643                 RT3261_DSP_RST_PIN_MASK, RT3261_DSP_RST_PIN_LO);
644         if (ret < 0) {
645                 dev_err(codec->dev, "Failed to reset DSP: %d\n", ret);
646                 goto patch_err;
647         }
648
649         mdelay(10);
650
651         ret = snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
652                 RT3261_DSP_RST_PIN_MASK, RT3261_DSP_RST_PIN_HI);
653         if (ret < 0) {
654                 dev_err(codec->dev, "Failed to recover DSP: %d\n", ret);
655                 goto patch_err;
656         }
657
658         ret = rt3261_dsp_patch_3(codec);
659         if (ret < 0)
660                 goto patch_err;
661
662         ret = rt3261_dsp_patch_2(codec);
663         if (ret < 0)
664                 goto patch_err;
665
666         return 0;
667
668 patch_err:
669
670         return ret;
671 }
672
673 static void rt3261_do_dsp_patch(struct work_struct *work)
674 {
675         struct rt3261_priv *rt3261 =
676                 container_of(work, struct rt3261_priv, patch_work.work);
677         struct snd_soc_codec *codec = rt3261->codec;
678
679         if (rt3261_dsp_patch(codec) < 0)
680                 dev_err(codec->dev, "Patch DSP rom code Fail !!!\n");
681 }*/
682
683
684 /**
685  * rt3261_dsp_conf - Set DSP basic setting.
686  *
687  * @codec: SoC audio codec device.
688  *
689  * Set parameters of basic setting to DSP.
690  *
691  * Returns 0 for success or negative error code.
692  */
693 static int rt3261_dsp_conf(struct snd_soc_codec *codec)
694 {
695         struct rt3261_dsp_param param;
696         int ret, i;
697
698         ret = snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
699                 RT3261_DSP_PD_PIN_MASK, RT3261_DSP_PD_PIN_HI);
700         if (ret < 0) {
701                 dev_err(codec->dev, "Failed to power up DSP: %d\n", ret);
702                 goto conf_err;
703         }
704
705         ret = snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
706                 RT3261_DSP_RST_PIN_MASK, RT3261_DSP_RST_PIN_LO);
707         if (ret < 0) {
708                 dev_err(codec->dev, "Failed to reset DSP: %d\n", ret);
709                 goto conf_err;
710         }
711
712         mdelay(10);
713
714         ret = snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
715                 RT3261_DSP_RST_PIN_MASK | RT3261_DSP_CLK_MASK,
716                 RT3261_DSP_RST_PIN_HI | RT3261_DSP_CLK_384K);
717         if (ret < 0) {
718                 dev_err(codec->dev, "Failed to recover DSP: %d\n", ret);
719                 goto conf_err;
720         }
721
722         param.cmd_fmt = 0x00e0;
723         param.cmd = RT3261_DSP_CMD_MW;
724         for(i = 0; i < RT3261_DSP_INIT_NUM; i++) {
725                 param.addr = rt3261_dsp_init[i][0];
726                 param.data = rt3261_dsp_init[i][1];
727                 ret = rt3261_dsp_write(codec, &param);
728                 if (ret < 0) {
729                         dev_err(codec->dev, "Fail to config Dsp: %d\n", ret);
730                         goto conf_err;
731                 }
732         }
733
734         return 0;
735
736 conf_err:
737
738         return ret;
739 }
740
741 /**
742  * rt3261_dsp_rate - Set DSP rate setting.
743  *
744  * @codec: SoC audio codec device.
745  * @rate: Sampling rate.
746  *
747  * Set parameters of sampling rate to DSP.
748  *
749  * Returns 0 for success or negative error code.
750  */
751 static int rt3261_dsp_rate(struct snd_soc_codec *codec, int rate)
752 {
753         struct rt3261_dsp_param param;
754         int ret, i, tab_num;
755         static const unsigned short (*rate_tab)[2];
756
757         if (rate != 48000 &&  rate != 44100 && rate != 16000)
758                 return -EINVAL;
759
760         if (rate > 44100) {
761                 rate_tab = rt3261_dsp_48;
762                 tab_num = RT3261_DSP_48_NUM;
763         } else {
764                 if (rate > 16000) {
765                         rate_tab = rt3261_dsp_441;
766                         tab_num = RT3261_DSP_441_NUM;
767                 } else {
768                         rate_tab = rt3261_dsp_16;
769                         tab_num = RT3261_DSP_16_NUM;
770                 }
771         }
772
773         param.cmd_fmt = 0x00e0;
774         param.cmd = RT3261_DSP_CMD_MW;
775         for (i = 0; i < tab_num; i++) {
776                 param.addr = rate_tab[i][0];
777                 param.data = rate_tab[i][1];
778                 ret = rt3261_dsp_write(codec, &param);
779                 if (ret < 0)
780                         goto rate_err;
781         }
782
783         return 0;
784
785 rate_err:
786
787         dev_err(codec->dev, "Fail to set rate %d parameters: %d\n", rate, ret);
788         return ret;
789 }
790
791 /**
792  * rt3261_dsp_set_mode - Set DSP mode parameters.
793  *
794  * @codec: SoC audio codec device.
795  * @mode: DSP mode.
796  *
797  * Set parameters of mode to DSP.
798  * There are three modes which includes " mic AEC + NS + FENS",
799  * "HFBF" and "Far-field pickup".
800  *
801  * Returns 0 for success or negative error code.
802  */
803 static int rt3261_dsp_set_mode(struct snd_soc_codec *codec, int mode)
804 {
805         struct rt3261_dsp_param param;
806         int ret, i;
807 /*
808         unsigned short (*mode_tab)[2];
809
810         switch (mode) {
811         case RT3261_DSP_AEC_NS_FENS:
812                 dev_info(codec->dev, "AEC\n");
813                 mode_tab = rt3261_dsp_aec_ns_fens;
814                 tab_num = RT3261_DSP_AEC_NUM;
815                 break;
816
817         case RT3261_DSP_HFBF:
818                 dev_info(codec->dev, "Beamforming\n");
819                 mode_tab = rt3261_dsp_hfbf;
820                 tab_num = RT3261_DSP_HFBF_NUM;
821                 break;
822
823         case RT3261_DSP_FFP:
824                 dev_info(codec->dev, "Far Field Pick-up\n");
825                 mode_tab = rt3261_dsp_ffp;
826                 tab_num = RT3261_DSP_FFP_NUM;
827                 break;
828
829         case RT3261_DSP_DIS:
830         default:
831                 dev_info(codec->dev, "Disable\n");
832                 return 0;
833         }
834 */
835         param.cmd_fmt = 0x00e0;
836         param.cmd = RT3261_DSP_CMD_MW;
837         for (i = 0; i < RT3261_DSP_AEC_NUM; i++) {
838                 param.addr = rt3261_dsp_aec_ns_fens[i][0];
839                 param.data = rt3261_dsp_aec_ns_fens[i][1];
840                 ret = rt3261_dsp_write(codec, &param);
841                 if (ret < 0)
842                         goto mode_err;
843         }
844
845         return 0;
846
847 mode_err:
848
849         dev_err(codec->dev, "Fail to set mode %d parameters: %d\n", mode, ret);
850         return ret;
851 }
852
853 /**
854  * rt3261_dsp_snd_effect - Set DSP sound effect.
855  *
856  * Set parameters of sound effect to DSP.
857  *
858  * Returns 0 for success or negative error code.
859  */
860 static int rt3261_dsp_snd_effect(struct snd_soc_codec *codec)
861 {
862         struct rt3261_priv *rt3261 = snd_soc_codec_get_drvdata(codec);
863         int ret;
864
865         ret = rt3261_dsp_conf(codec);
866         if (ret < 0)
867                 goto effect_err;
868
869         ret = rt3261_dsp_rate(codec, rt3261->lrck[rt3261->aif_pu] ?
870                 rt3261->lrck[rt3261->aif_pu] : 44100);
871         if (ret < 0)
872                 goto effect_err;
873
874         ret = rt3261_dsp_set_mode(codec, rt3261->dsp_sw);
875         if (ret < 0)
876                 goto effect_err;
877
878         mdelay(20);
879
880         return 0;
881
882 effect_err:
883
884         return ret;
885 }
886
887 static int rt3261_dsp_event(struct snd_soc_dapm_widget *w,
888                         struct snd_kcontrol *k, int event)
889 {
890         struct snd_soc_codec *codec = w->codec;
891         struct rt3261_priv *rt3261 = snd_soc_codec_get_drvdata(codec);
892         static unsigned int power_on;
893
894         switch (event) {
895         case SND_SOC_DAPM_POST_PMD:
896                 pr_info("%s(): PMD\n", __func__);
897                 if (!power_on)
898                         return 0;
899
900                 power_on--;
901                 if (!power_on) {
902                         snd_soc_update_bits(codec, RT3261_PWR_DIG2,
903                                 RT3261_PWR_I2S_DSP, 0);
904                         snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
905                                 RT3261_DSP_PD_PIN_MASK, RT3261_DSP_PD_PIN_LO);
906                 }
907                 break;
908
909         case SND_SOC_DAPM_POST_PMU:
910                 pr_info("%s(): PMU\n", __func__);
911                 if (rt3261->dsp_sw == RT3261_DSP_DIS || 2 <= power_on)
912                         return 0;
913
914                 if (!power_on) {
915                         snd_soc_update_bits(codec, RT3261_PWR_DIG2,
916                                 RT3261_PWR_I2S_DSP, RT3261_PWR_I2S_DSP);
917 #ifdef INIT_DSP_IN_PROBE
918                         snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
919                                 RT3261_DSP_PD_PIN_MASK, RT3261_DSP_PD_PIN_HI);
920 #else
921                         rt3261_dsp_snd_effect(codec);
922 #endif
923                 }
924                 power_on++;
925                 break;
926
927         default:
928                 return 0;
929         }
930
931         return 0;
932 }
933
934 static const struct snd_soc_dapm_widget rt3261_dsp_dapm_widgets[] = {
935         SND_SOC_DAPM_PGA_E("DSP Downstream", SND_SOC_NOPM,
936                 0, 0, NULL, 0, rt3261_dsp_event,
937                 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
938         SND_SOC_DAPM_PGA_E("DSP Upstream", SND_SOC_NOPM,
939                 0, 0, NULL, 0, rt3261_dsp_event,
940                 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU),
941         SND_SOC_DAPM_MUX("RxDP Mux", SND_SOC_NOPM, 0, 0,
942                 &rt3261_rxdp_mux),
943         SND_SOC_DAPM_MUX("RxDP2 Mux", SND_SOC_NOPM, 0, 0,
944                 &rt3261_rxdp2_mux),
945         SND_SOC_DAPM_MUX("RxDP1 Mux", SND_SOC_NOPM, 0, 0,
946                 &rt3261_rxdp1_mux),
947         SND_SOC_DAPM_MUX("RxDC Mux", SND_SOC_NOPM, 0, 0,
948                 &rt3261_rxdc_mux),
949         SND_SOC_DAPM_PGA("RxDP", SND_SOC_NOPM, 0, 0, NULL, 0),
950         SND_SOC_DAPM_PGA("RxDC", SND_SOC_NOPM, 0, 0, NULL, 0),
951         SND_SOC_DAPM_PGA("TxDC", SND_SOC_NOPM, 0, 0, NULL, 0),
952         SND_SOC_DAPM_PGA("TxDP", SND_SOC_NOPM, 0, 0, NULL, 0),
953 };
954
955 static const struct snd_soc_dapm_route rt3261_dsp_dapm_routes[] = {
956         {"RxDC", NULL, "RxDC Mux"},
957         {"RxDC Mux", "Mono_ADC", "Mono ADC MIXL"},
958         {"RxDC Mux", "Mono_ADC", "Mono ADC MIXR"},
959         {"RxDC Mux", "Stereo_ADC", "Stereo ADC MIXL"},
960         {"RxDC Mux", "Stereo_ADC", "Stereo ADC MIXR"},
961         {"RxDP", NULL, "RxDP Mux"},
962         {"RxDP Mux", "RxDP2", "RxDP2 Mux"},
963         {"RxDP Mux", "RxDP1", "RxDP1 Mux"},
964         {"RxDP2 Mux", "IF2_DAC", "IF2 DAC L"},
965         {"RxDP2 Mux", "IF2_DAC", "IF2 DAC R"},
966         {"RxDP2 Mux", "Stereo_ADC", "Stereo ADC MIXL"},
967         {"RxDP2 Mux", "Stereo_ADC", "Stereo ADC MIXR"},
968         {"RxDP1 Mux", "DAC1", "Stereo DAC MIXL"},
969         {"RxDP1 Mux", "DAC1", "Stereo DAC MIXR"},
970         {"RxDP1 Mux", "IF1_DAC", "IF1 DAC L"},
971         {"RxDP1 Mux", "IF1_DAC", "IF1 DAC R"},
972
973         {"DSP Downstream", NULL, "RxDP"},
974         {"TxDC", NULL, "DSP Downstream"},
975         {"DSP Upstream", NULL, "RxDP"},
976         {"DSP Upstream", NULL, "RxDC"},
977         {"TxDP", NULL, "DSP Upstream"},
978
979         {"IF2 ADC L Mux", "TxDP", "TxDP"},
980         {"IF2 ADC R Mux", "TxDP", "TxDP"},
981         {"DAC L2 Mux", "TxDC", "TxDC"},
982         {"DAC R2 Mux", "TxDC", "TxDC"},
983 };
984
985 /**
986  * rt3261_dsp_show - Dump DSP registers.
987  * @dev: codec device.
988  * @attr: device attribute.
989  * @buf: buffer for display.
990  *
991  * To show non-zero values of all DSP registers.
992  *
993  * Returns buffer length.
994  */
995 static ssize_t rt3261_dsp_show(struct device *dev,
996         struct device_attribute *attr, char *buf)
997 {
998         struct i2c_client *client = to_i2c_client(dev);
999         struct rt3261_priv *rt3261 = i2c_get_clientdata(client);
1000         struct snd_soc_codec *codec = rt3261->codec;
1001         static const unsigned short (*rt3261_dsp_tab)[2];
1002         unsigned int val;
1003         int cnt = 0, i, tab_num;
1004
1005         switch (rt3261->dsp_sw) {
1006         case RT3261_DSP_AEC_NS_FENS:
1007                 cnt += sprintf(buf, "[ RT3261 DSP 'AEC' ]\n");
1008                 rt3261_dsp_tab = rt3261_dsp_aec_ns_fens;
1009                 tab_num = RT3261_DSP_AEC_NUM;
1010                 break;
1011
1012         case RT3261_DSP_HFBF:
1013                 cnt += sprintf(buf, "[ RT3261 DSP 'Beamforming' ]\n");
1014                 rt3261_dsp_tab = rt3261_dsp_hfbf;
1015                 tab_num = RT3261_DSP_HFBF_NUM;
1016                 break;
1017
1018         case RT3261_DSP_FFP:
1019                 cnt += sprintf(buf, "[ RT3261 DSP 'Far Field Pick-up' ]\n");
1020                 rt3261_dsp_tab = rt3261_dsp_ffp;
1021                 tab_num = RT3261_DSP_FFP_NUM;
1022                 break;
1023
1024         case RT3261_DSP_DIS:
1025         default:
1026                 cnt += sprintf(buf, "RT3261 DSP Disabled\n");
1027                 goto dsp_done;
1028         }
1029
1030         for (i = 0; i < tab_num; i++) {
1031                 if (cnt + RT3261_DSP_REG_DISP_LEN >= PAGE_SIZE)
1032                         break;
1033                 val = rt3261_dsp_read(codec, rt3261_dsp_tab[i][0]);
1034                 if (!val)
1035                         continue;
1036                 cnt += snprintf(buf + cnt, RT3261_DSP_REG_DISP_LEN,
1037                         "#rnv%04x  #rv%04x  #rd0\n\n", rt3261_dsp_tab[i][0], val);
1038         }
1039
1040         tab_num = RT3261_DSP_INIT_NUM;
1041         for (i = 0; i < tab_num; i++) {
1042                 if (cnt + RT3261_DSP_REG_DISP_LEN >= PAGE_SIZE)
1043                         break;
1044                 val = rt3261_dsp_read(codec, rt3261_dsp_init[i][0]);
1045                 if (!val)
1046                         continue;
1047                 cnt += snprintf(buf + cnt, RT3261_DSP_REG_DISP_LEN,
1048                         "#rnv%04x  #rv%04x  #rd0\n", rt3261_dsp_init[i][0], val);
1049         }
1050         for (i = 0; i < RT3261_DSP_RATE_NUM; i++) {
1051                 if (cnt + RT3261_DSP_REG_DISP_LEN >= PAGE_SIZE)
1052                         break;
1053                 val = rt3261_dsp_read(codec, rt3261_dsp_rate_tab[i]);
1054                 if (!val)
1055                         continue;
1056                 cnt += snprintf(buf + cnt, RT3261_DSP_REG_DISP_LEN,
1057                         "#rnv%04x  #rv%04x  #rd0\n", rt3261_dsp_rate_tab[i], val);
1058         }
1059
1060 dsp_done:
1061
1062         if (cnt >= PAGE_SIZE)
1063                 cnt = PAGE_SIZE - 1;
1064
1065         return cnt;
1066 }
1067
1068 static ssize_t dsp_reg_store(struct device *dev,
1069         struct device_attribute *attr, const char *buf, size_t count)
1070 {
1071         struct i2c_client *client = to_i2c_client(dev);
1072         struct rt3261_priv *rt3261 = i2c_get_clientdata(client);
1073         struct snd_soc_codec *codec = rt3261->codec;
1074         struct rt3261_dsp_param param;
1075         unsigned int val=0,addr=0;
1076         int i;
1077
1078         dev_dbg(codec->dev, "register \"%s\" count=%zu\n", buf, count);
1079
1080         for(i=0;i<count;i++) //address
1081         {
1082                 if(*(buf+i) <= '9' && *(buf+i)>='0')
1083                 {
1084                         addr = (addr << 4) | (*(buf+i)-'0');
1085                 }
1086                 else if(*(buf+i) <= 'f' && *(buf+i)>='a')
1087                 {
1088                         addr = (addr << 4) | ((*(buf+i)-'a')+0xa);
1089                 }
1090                 else if(*(buf+i) <= 'A' && *(buf+i)>='A')
1091                 {
1092                         addr = (addr << 4) | ((*(buf+i)-'A')+0xa);
1093                 }
1094                 else
1095                 {
1096                         break;
1097                 }
1098         }
1099          
1100         for(i=i+1 ;i<count;i++) //val
1101         {
1102                 if(*(buf+i) <= '9' && *(buf+i)>='0')
1103                 {
1104                         val = (val << 4) | (*(buf+i)-'0');
1105                 }
1106                 else if(*(buf+i) <= 'f' && *(buf+i)>='a')
1107                 {
1108                         val = (val << 4) | ((*(buf+i)-'a')+0xa);
1109                 }
1110                 else if(*(buf+i) <= 'F' && *(buf+i)>='A')
1111                 {
1112                         val = (val << 4) | ((*(buf+i)-'A')+0xa);
1113                         
1114                 }
1115                 else
1116                 {
1117                         break;
1118                 }
1119         }
1120         dev_dbg(codec->dev, "addr=0x%x val=0x%x\n", addr, val);
1121         if(i==count)
1122         {
1123                 dev_dbg(codec->dev, "0x%04x = 0x%04x\n",
1124                         addr, rt3261_dsp_read(codec, addr));
1125         }
1126         else
1127         {
1128                 param.cmd_fmt = 0x00e0;
1129                 param.cmd = RT3261_DSP_CMD_MW;
1130                 param.addr = addr;
1131                 param.data = val;
1132                 rt3261_dsp_write(codec, &param);
1133         }
1134
1135         return count;
1136 }
1137
1138 static DEVICE_ATTR(dsp_reg, 0666, rt3261_dsp_show, dsp_reg_store);
1139
1140 /**
1141  * rt3261_dsp_probe - register DSP for rt3261
1142  * @codec: audio codec
1143  *
1144  * To register DSP function for rt3261.
1145  *
1146  * Returns 0 for success or negative error code.
1147  */
1148 int rt3261_dsp_probe(struct snd_soc_codec *codec)
1149 {
1150         //struct rt3261_priv *rt3261;
1151         int ret;
1152
1153         if (codec == NULL)
1154                 return -EINVAL;
1155
1156         snd_soc_add_codec_controls(codec, rt3261_dsp_snd_controls,
1157                         ARRAY_SIZE(rt3261_dsp_snd_controls));
1158         snd_soc_dapm_new_controls(&codec->dapm, rt3261_dsp_dapm_widgets,
1159                         ARRAY_SIZE(rt3261_dsp_dapm_widgets));
1160         snd_soc_dapm_add_routes(&codec->dapm, rt3261_dsp_dapm_routes,
1161                         ARRAY_SIZE(rt3261_dsp_dapm_routes));
1162
1163         /* Patch DSP rom code if IC version is larger than C version */
1164
1165         ret = snd_soc_update_bits(codec, RT3261_PWR_DIG2,
1166                 RT3261_PWR_I2S_DSP, RT3261_PWR_I2S_DSP);
1167         if (ret < 0) {
1168                 dev_err(codec->dev,
1169                         "Failed to power up DSP IIS interface: %d\n", ret);
1170         }
1171
1172 #ifdef INIT_DSP_IN_PROBE
1173         rt3261_dsp_snd_effect(codec);
1174         ret = rt3261_dsp_read(codec, 0x22fb);
1175         if(ret == 0x5a5a)
1176                 pr_info("DSP init success\n");
1177         else
1178                 pr_info("DSP init failed\n");
1179 #else
1180         rt3261_dsp_conf(codec);
1181 #endif
1182         ret = rt3261_dsp_read(codec, 0x3800);
1183         pr_info("DSP version code = 0x%04x\n",ret);
1184         /*if(ret != 0x501a) {
1185                 rt3261 = snd_soc_codec_get_drvdata(codec);
1186                 INIT_DELAYED_WORK(&rt3261->patch_work, rt3261_do_dsp_patch);
1187                 schedule_delayed_work(&rt3261->patch_work,
1188                                 msecs_to_jiffies(100));
1189         }*/
1190 #ifdef INIT_DSP_IN_PROBE
1191         snd_soc_update_bits(codec, RT3261_DSP_CTRL3,
1192                 RT3261_DSP_PD_PIN_MASK, RT3261_DSP_PD_PIN_LO);
1193 #endif
1194         snd_soc_update_bits(codec, RT3261_PWR_DIG2,
1195                 RT3261_PWR_I2S_DSP, 0);
1196
1197         ret = device_create_file(codec->dev, &dev_attr_dsp_reg);
1198         if (ret != 0) {
1199                 dev_err(codec->dev,
1200                         "Failed to create index_reg sysfs files: %d\n", ret);
1201                 return ret;
1202         }
1203
1204         return 0;
1205 }
1206 EXPORT_SYMBOL_GPL(rt3261_dsp_probe);
1207
1208 #ifdef RTK_IOCTL
1209 int rt_codec_dsp_ioctl_common(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
1210 {
1211         struct rt_codec_cmd rt_codec;
1212         int *buf;
1213         int *p;
1214         int ret;
1215         struct rt3261_dsp_param param;
1216
1217         //int mask1 = 0, mask2 = 0;
1218
1219         struct rt_codec_cmd __user *_rt_codec = (struct rt_codec_cmd *)arg;
1220         struct snd_soc_codec *codec = hw->private_data;
1221         struct rt3261_priv *rt3261 = snd_soc_codec_get_drvdata(codec);
1222
1223         if (copy_from_user(&rt_codec, _rt_codec, sizeof(rt_codec))) {
1224                 dev_err(codec->dev, "copy_from_user faild\n");
1225                 return -EFAULT;
1226         }
1227         dev_dbg(codec->dev, "rt_codec.number=%zu\n", rt_codec.number);
1228         buf = kmalloc(sizeof(*buf) * rt_codec.number, GFP_KERNEL);
1229         if (buf == NULL)
1230                 return -ENOMEM;
1231         if (copy_from_user(buf, rt_codec.buf, sizeof(*buf) * rt_codec.number)) {
1232                 goto err;
1233         }
1234
1235         ret = snd_soc_update_bits(codec, RT3261_PWR_DIG2,
1236                 RT3261_PWR_I2S_DSP, RT3261_PWR_I2S_DSP);
1237         if (ret < 0) {
1238                 dev_err(codec->dev,
1239                         "Failed to power up DSP IIS interface: %d\n", ret);
1240                 goto err;
1241         }
1242
1243         switch (cmd) {
1244         case RT_READ_CODEC_DSP_IOCTL:
1245                 for (p = buf; p < buf + rt_codec.number/2; p++)
1246                         *(p+rt_codec.number/2) = rt3261_dsp_read(codec, *p);
1247                 if (copy_to_user(rt_codec.buf, buf, sizeof(*buf) * rt_codec.number))
1248                         goto err;
1249                 break;
1250
1251         case RT_WRITE_CODEC_DSP_IOCTL:
1252                 param.cmd_fmt = 0x00e0;
1253                 param.cmd = RT3261_DSP_CMD_MW;
1254                 p = buf;
1255                 param.addr = *p;
1256                 param.data = *(p+rt_codec.number/2);
1257                 if(codec == NULL) {
1258                         dev_dbg(codec->dev, "codec is null\n");
1259                         break;
1260                 }
1261                 for (p = buf; p < buf + rt_codec.number/2; p++)
1262                         rt3261_dsp_write(codec, &param);
1263                 break;
1264
1265         case RT_GET_CODEC_DSP_MODE_IOCTL:
1266                 *buf = rt3261->dsp_sw;
1267                 if (copy_to_user(rt_codec.buf, buf, sizeof(*buf) * rt_codec.number))
1268                         goto err;
1269                 break;
1270
1271         default:
1272                 dev_info(codec->dev, "unsported dsp command\n");
1273                 break;
1274         }
1275
1276         kfree(buf);
1277         return 0;
1278
1279 err:
1280         kfree(buf);
1281         return -EFAULT;
1282 }
1283 EXPORT_SYMBOL_GPL(rt_codec_dsp_ioctl_common);
1284 #endif
1285
1286 #ifdef CONFIG_PM
1287 int rt3261_dsp_suspend(struct snd_soc_codec *codec)
1288 {
1289         return 0;
1290 }
1291 EXPORT_SYMBOL_GPL(rt3261_dsp_suspend);
1292
1293 int rt3261_dsp_resume(struct snd_soc_codec *codec)
1294 {
1295         return 0;
1296 }
1297 EXPORT_SYMBOL_GPL(rt3261_dsp_resume);
1298 #endif
1299