xref: /linux/sound/soc/codecs/rt5663.c (revision 8675e8d3d1b413dc0e6165d2ce09de4335f7f57a)
1 /*
2  * rt5663.c  --  RT5663 ALSA SoC audio codec driver
3  *
4  * Copyright 2016 Realtek Semiconductor Corp.
5  * Author: Jack Yu <jack.yu@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 #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 <linux/acpi.h>
20 #include <linux/workqueue.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/jack.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 
30 #include "rt5663.h"
31 #include "rl6231.h"
32 
33 #define RT5663_DEVICE_ID_2 0x6451
34 #define RT5663_DEVICE_ID_1 0x6406
35 
36 enum {
37 	CODEC_VER_1,
38 	CODEC_VER_0,
39 };
40 
41 struct impedance_mapping_table {
42 	unsigned int imp_min;
43 	unsigned int imp_max;
44 	unsigned int vol;
45 	unsigned int dc_offset_l_manual;
46 	unsigned int dc_offset_r_manual;
47 	unsigned int dc_offset_l_manual_mic;
48 	unsigned int dc_offset_r_manual_mic;
49 };
50 
51 struct rt5663_priv {
52 	struct snd_soc_component *component;
53 	struct rt5663_platform_data pdata;
54 	struct regmap *regmap;
55 	struct delayed_work jack_detect_work, jd_unplug_work;
56 	struct snd_soc_jack *hs_jack;
57 	struct timer_list btn_check_timer;
58 	struct impedance_mapping_table *imp_table;
59 
60 	int codec_ver;
61 	int sysclk;
62 	int sysclk_src;
63 	int lrck;
64 
65 	int pll_src;
66 	int pll_in;
67 	int pll_out;
68 
69 	int jack_type;
70 };
71 
72 static const struct reg_sequence rt5663_patch_list[] = {
73 	{ 0x002a, 0x8020 },
74 	{ 0x0086, 0x0028 },
75 	{ 0x0117, 0x0f28 },
76 	{ 0x02fb, 0x8089 },
77 };
78 
79 static const struct reg_default rt5663_v2_reg[] = {
80 	{ 0x0000, 0x0000 },
81 	{ 0x0001, 0xc8c8 },
82 	{ 0x0002, 0x8080 },
83 	{ 0x0003, 0x8000 },
84 	{ 0x0004, 0xc80a },
85 	{ 0x0005, 0x0000 },
86 	{ 0x0006, 0x0000 },
87 	{ 0x0007, 0x0000 },
88 	{ 0x000a, 0x0000 },
89 	{ 0x000b, 0x0000 },
90 	{ 0x000c, 0x0000 },
91 	{ 0x000d, 0x0000 },
92 	{ 0x000f, 0x0808 },
93 	{ 0x0010, 0x4000 },
94 	{ 0x0011, 0x0000 },
95 	{ 0x0012, 0x1404 },
96 	{ 0x0013, 0x1000 },
97 	{ 0x0014, 0xa00a },
98 	{ 0x0015, 0x0404 },
99 	{ 0x0016, 0x0404 },
100 	{ 0x0017, 0x0011 },
101 	{ 0x0018, 0xafaf },
102 	{ 0x0019, 0xafaf },
103 	{ 0x001a, 0xafaf },
104 	{ 0x001b, 0x0011 },
105 	{ 0x001c, 0x2f2f },
106 	{ 0x001d, 0x2f2f },
107 	{ 0x001e, 0x2f2f },
108 	{ 0x001f, 0x0000 },
109 	{ 0x0020, 0x0000 },
110 	{ 0x0021, 0x0000 },
111 	{ 0x0022, 0x5757 },
112 	{ 0x0023, 0x0039 },
113 	{ 0x0024, 0x000b },
114 	{ 0x0026, 0xc0c0 },
115 	{ 0x0027, 0xc0c0 },
116 	{ 0x0028, 0xc0c0 },
117 	{ 0x0029, 0x8080 },
118 	{ 0x002a, 0xaaaa },
119 	{ 0x002b, 0xaaaa },
120 	{ 0x002c, 0xaba8 },
121 	{ 0x002d, 0x0000 },
122 	{ 0x002e, 0x0000 },
123 	{ 0x002f, 0x0000 },
124 	{ 0x0030, 0x0000 },
125 	{ 0x0031, 0x5000 },
126 	{ 0x0032, 0x0000 },
127 	{ 0x0033, 0x0000 },
128 	{ 0x0034, 0x0000 },
129 	{ 0x0035, 0x0000 },
130 	{ 0x003a, 0x0000 },
131 	{ 0x003b, 0x0000 },
132 	{ 0x003c, 0x00ff },
133 	{ 0x003d, 0x0000 },
134 	{ 0x003e, 0x00ff },
135 	{ 0x003f, 0x0000 },
136 	{ 0x0040, 0x0000 },
137 	{ 0x0041, 0x00ff },
138 	{ 0x0042, 0x0000 },
139 	{ 0x0043, 0x00ff },
140 	{ 0x0044, 0x0c0c },
141 	{ 0x0049, 0xc00b },
142 	{ 0x004a, 0x0000 },
143 	{ 0x004b, 0x031f },
144 	{ 0x004d, 0x0000 },
145 	{ 0x004e, 0x001f },
146 	{ 0x004f, 0x0000 },
147 	{ 0x0050, 0x001f },
148 	{ 0x0052, 0xf000 },
149 	{ 0x0061, 0x0000 },
150 	{ 0x0062, 0x0000 },
151 	{ 0x0063, 0x003e },
152 	{ 0x0064, 0x0000 },
153 	{ 0x0065, 0x0000 },
154 	{ 0x0066, 0x003f },
155 	{ 0x0067, 0x0000 },
156 	{ 0x006b, 0x0000 },
157 	{ 0x006d, 0xff00 },
158 	{ 0x006e, 0x2808 },
159 	{ 0x006f, 0x000a },
160 	{ 0x0070, 0x8000 },
161 	{ 0x0071, 0x8000 },
162 	{ 0x0072, 0x8000 },
163 	{ 0x0073, 0x7000 },
164 	{ 0x0074, 0x7770 },
165 	{ 0x0075, 0x0002 },
166 	{ 0x0076, 0x0001 },
167 	{ 0x0078, 0x00f0 },
168 	{ 0x0079, 0x0000 },
169 	{ 0x007a, 0x0000 },
170 	{ 0x007b, 0x0000 },
171 	{ 0x007c, 0x0000 },
172 	{ 0x007d, 0x0123 },
173 	{ 0x007e, 0x4500 },
174 	{ 0x007f, 0x8003 },
175 	{ 0x0080, 0x0000 },
176 	{ 0x0081, 0x0000 },
177 	{ 0x0082, 0x0000 },
178 	{ 0x0083, 0x0000 },
179 	{ 0x0084, 0x0000 },
180 	{ 0x0085, 0x0000 },
181 	{ 0x0086, 0x0008 },
182 	{ 0x0087, 0x0000 },
183 	{ 0x0088, 0x0000 },
184 	{ 0x0089, 0x0000 },
185 	{ 0x008a, 0x0000 },
186 	{ 0x008b, 0x0000 },
187 	{ 0x008c, 0x0003 },
188 	{ 0x008e, 0x0060 },
189 	{ 0x008f, 0x1000 },
190 	{ 0x0091, 0x0c26 },
191 	{ 0x0092, 0x0073 },
192 	{ 0x0093, 0x0000 },
193 	{ 0x0094, 0x0080 },
194 	{ 0x0098, 0x0000 },
195 	{ 0x0099, 0x0000 },
196 	{ 0x009a, 0x0007 },
197 	{ 0x009f, 0x0000 },
198 	{ 0x00a0, 0x0000 },
199 	{ 0x00a1, 0x0002 },
200 	{ 0x00a2, 0x0001 },
201 	{ 0x00a3, 0x0002 },
202 	{ 0x00a4, 0x0001 },
203 	{ 0x00ae, 0x2040 },
204 	{ 0x00af, 0x0000 },
205 	{ 0x00b6, 0x0000 },
206 	{ 0x00b7, 0x0000 },
207 	{ 0x00b8, 0x0000 },
208 	{ 0x00b9, 0x0000 },
209 	{ 0x00ba, 0x0002 },
210 	{ 0x00bb, 0x0000 },
211 	{ 0x00be, 0x0000 },
212 	{ 0x00c0, 0x0000 },
213 	{ 0x00c1, 0x0aaa },
214 	{ 0x00c2, 0xaa80 },
215 	{ 0x00c3, 0x0003 },
216 	{ 0x00c4, 0x0000 },
217 	{ 0x00d0, 0x0000 },
218 	{ 0x00d1, 0x2244 },
219 	{ 0x00d2, 0x0000 },
220 	{ 0x00d3, 0x3300 },
221 	{ 0x00d4, 0x2200 },
222 	{ 0x00d9, 0x0809 },
223 	{ 0x00da, 0x0000 },
224 	{ 0x00db, 0x0008 },
225 	{ 0x00dc, 0x00c0 },
226 	{ 0x00dd, 0x6724 },
227 	{ 0x00de, 0x3131 },
228 	{ 0x00df, 0x0008 },
229 	{ 0x00e0, 0x4000 },
230 	{ 0x00e1, 0x3131 },
231 	{ 0x00e2, 0x600c },
232 	{ 0x00ea, 0xb320 },
233 	{ 0x00eb, 0x0000 },
234 	{ 0x00ec, 0xb300 },
235 	{ 0x00ed, 0x0000 },
236 	{ 0x00ee, 0xb320 },
237 	{ 0x00ef, 0x0000 },
238 	{ 0x00f0, 0x0201 },
239 	{ 0x00f1, 0x0ddd },
240 	{ 0x00f2, 0x0ddd },
241 	{ 0x00f6, 0x0000 },
242 	{ 0x00f7, 0x0000 },
243 	{ 0x00f8, 0x0000 },
244 	{ 0x00fa, 0x0000 },
245 	{ 0x00fb, 0x0000 },
246 	{ 0x00fc, 0x0000 },
247 	{ 0x00fd, 0x0000 },
248 	{ 0x00fe, 0x10ec },
249 	{ 0x00ff, 0x6451 },
250 	{ 0x0100, 0xaaaa },
251 	{ 0x0101, 0x000a },
252 	{ 0x010a, 0xaaaa },
253 	{ 0x010b, 0xa0a0 },
254 	{ 0x010c, 0xaeae },
255 	{ 0x010d, 0xaaaa },
256 	{ 0x010e, 0xaaaa },
257 	{ 0x010f, 0xaaaa },
258 	{ 0x0110, 0xe002 },
259 	{ 0x0111, 0xa602 },
260 	{ 0x0112, 0xaaaa },
261 	{ 0x0113, 0x2000 },
262 	{ 0x0117, 0x0f00 },
263 	{ 0x0125, 0x0420 },
264 	{ 0x0132, 0x0000 },
265 	{ 0x0133, 0x0000 },
266 	{ 0x0136, 0x5555 },
267 	{ 0x0137, 0x5540 },
268 	{ 0x0138, 0x3700 },
269 	{ 0x0139, 0x79a1 },
270 	{ 0x013a, 0x2020 },
271 	{ 0x013b, 0x2020 },
272 	{ 0x013c, 0x2005 },
273 	{ 0x013f, 0x0000 },
274 	{ 0x0145, 0x0002 },
275 	{ 0x0146, 0x0000 },
276 	{ 0x0147, 0x0000 },
277 	{ 0x0148, 0x0000 },
278 	{ 0x0160, 0x4ec0 },
279 	{ 0x0161, 0x0080 },
280 	{ 0x0162, 0x0200 },
281 	{ 0x0163, 0x0800 },
282 	{ 0x0164, 0x0000 },
283 	{ 0x0165, 0x0000 },
284 	{ 0x0166, 0x0000 },
285 	{ 0x0167, 0x000f },
286 	{ 0x0168, 0x000f },
287 	{ 0x0170, 0x4e80 },
288 	{ 0x0171, 0x0080 },
289 	{ 0x0172, 0x0200 },
290 	{ 0x0173, 0x0800 },
291 	{ 0x0174, 0x00ff },
292 	{ 0x0175, 0x0000 },
293 	{ 0x0190, 0x4131 },
294 	{ 0x0191, 0x4131 },
295 	{ 0x0192, 0x4131 },
296 	{ 0x0193, 0x4131 },
297 	{ 0x0194, 0x0000 },
298 	{ 0x0195, 0x0000 },
299 	{ 0x0196, 0x0000 },
300 	{ 0x0197, 0x0000 },
301 	{ 0x0198, 0x0000 },
302 	{ 0x0199, 0x0000 },
303 	{ 0x01a0, 0x1e64 },
304 	{ 0x01a1, 0x06a3 },
305 	{ 0x01a2, 0x0000 },
306 	{ 0x01a3, 0x0000 },
307 	{ 0x01a4, 0x0000 },
308 	{ 0x01a5, 0x0000 },
309 	{ 0x01a6, 0x0000 },
310 	{ 0x01a7, 0x0000 },
311 	{ 0x01a8, 0x0000 },
312 	{ 0x01a9, 0x0000 },
313 	{ 0x01aa, 0x0000 },
314 	{ 0x01ab, 0x0000 },
315 	{ 0x01b5, 0x0000 },
316 	{ 0x01b6, 0x01c3 },
317 	{ 0x01b7, 0x02a0 },
318 	{ 0x01b8, 0x03e9 },
319 	{ 0x01b9, 0x1389 },
320 	{ 0x01ba, 0xc351 },
321 	{ 0x01bb, 0x0009 },
322 	{ 0x01bc, 0x0018 },
323 	{ 0x01bd, 0x002a },
324 	{ 0x01be, 0x004c },
325 	{ 0x01bf, 0x0097 },
326 	{ 0x01c0, 0x433d },
327 	{ 0x01c1, 0x0000 },
328 	{ 0x01c2, 0x0000 },
329 	{ 0x01c3, 0x0000 },
330 	{ 0x01c4, 0x0000 },
331 	{ 0x01c5, 0x0000 },
332 	{ 0x01c6, 0x0000 },
333 	{ 0x01c7, 0x0000 },
334 	{ 0x01c8, 0x40af },
335 	{ 0x01c9, 0x0702 },
336 	{ 0x01ca, 0x0000 },
337 	{ 0x01cb, 0x0000 },
338 	{ 0x01cc, 0x5757 },
339 	{ 0x01cd, 0x5757 },
340 	{ 0x01ce, 0x5757 },
341 	{ 0x01cf, 0x5757 },
342 	{ 0x01d0, 0x5757 },
343 	{ 0x01d1, 0x5757 },
344 	{ 0x01d2, 0x5757 },
345 	{ 0x01d3, 0x5757 },
346 	{ 0x01d4, 0x5757 },
347 	{ 0x01d5, 0x5757 },
348 	{ 0x01d6, 0x003c },
349 	{ 0x01da, 0x0000 },
350 	{ 0x01db, 0x0000 },
351 	{ 0x01dc, 0x0000 },
352 	{ 0x01de, 0x7c00 },
353 	{ 0x01df, 0x0320 },
354 	{ 0x01e0, 0x06a1 },
355 	{ 0x01e1, 0x0000 },
356 	{ 0x01e2, 0x0000 },
357 	{ 0x01e3, 0x0000 },
358 	{ 0x01e4, 0x0000 },
359 	{ 0x01e5, 0x0000 },
360 	{ 0x01e6, 0x0001 },
361 	{ 0x01e7, 0x0000 },
362 	{ 0x01e8, 0x0000 },
363 	{ 0x01ea, 0x0000 },
364 	{ 0x01eb, 0x0000 },
365 	{ 0x01ec, 0x0000 },
366 	{ 0x01ed, 0x0000 },
367 	{ 0x01ee, 0x0000 },
368 	{ 0x01ef, 0x0000 },
369 	{ 0x01f0, 0x0000 },
370 	{ 0x01f1, 0x0000 },
371 	{ 0x01f2, 0x0000 },
372 	{ 0x01f3, 0x0000 },
373 	{ 0x01f4, 0x0000 },
374 	{ 0x0200, 0x0000 },
375 	{ 0x0201, 0x0000 },
376 	{ 0x0202, 0x0000 },
377 	{ 0x0203, 0x0000 },
378 	{ 0x0204, 0x0000 },
379 	{ 0x0205, 0x0000 },
380 	{ 0x0206, 0x0000 },
381 	{ 0x0207, 0x0000 },
382 	{ 0x0208, 0x0000 },
383 	{ 0x0210, 0x60b1 },
384 	{ 0x0211, 0xa000 },
385 	{ 0x0212, 0x024c },
386 	{ 0x0213, 0xf7ff },
387 	{ 0x0214, 0x024c },
388 	{ 0x0215, 0x0102 },
389 	{ 0x0216, 0x00a3 },
390 	{ 0x0217, 0x0048 },
391 	{ 0x0218, 0x92c0 },
392 	{ 0x0219, 0x0000 },
393 	{ 0x021a, 0x00c8 },
394 	{ 0x021b, 0x0020 },
395 	{ 0x02fa, 0x0000 },
396 	{ 0x02fb, 0x0000 },
397 	{ 0x02fc, 0x0000 },
398 	{ 0x02ff, 0x0110 },
399 	{ 0x0300, 0x001f },
400 	{ 0x0301, 0x032c },
401 	{ 0x0302, 0x5f21 },
402 	{ 0x0303, 0x4000 },
403 	{ 0x0304, 0x4000 },
404 	{ 0x0305, 0x06d5 },
405 	{ 0x0306, 0x8000 },
406 	{ 0x0307, 0x0700 },
407 	{ 0x0310, 0x4560 },
408 	{ 0x0311, 0xa4a8 },
409 	{ 0x0312, 0x7418 },
410 	{ 0x0313, 0x0000 },
411 	{ 0x0314, 0x0006 },
412 	{ 0x0315, 0xffff },
413 	{ 0x0316, 0xc400 },
414 	{ 0x0317, 0x0000 },
415 	{ 0x0330, 0x00a6 },
416 	{ 0x0331, 0x04c3 },
417 	{ 0x0332, 0x27c8 },
418 	{ 0x0333, 0xbf50 },
419 	{ 0x0334, 0x0045 },
420 	{ 0x0335, 0x0007 },
421 	{ 0x0336, 0x7418 },
422 	{ 0x0337, 0x0501 },
423 	{ 0x0338, 0x0000 },
424 	{ 0x0339, 0x0010 },
425 	{ 0x033a, 0x1010 },
426 	{ 0x03c0, 0x7e00 },
427 	{ 0x03c1, 0x8000 },
428 	{ 0x03c2, 0x8000 },
429 	{ 0x03c3, 0x8000 },
430 	{ 0x03c4, 0x8000 },
431 	{ 0x03c5, 0x8000 },
432 	{ 0x03c6, 0x8000 },
433 	{ 0x03c7, 0x8000 },
434 	{ 0x03c8, 0x8000 },
435 	{ 0x03c9, 0x8000 },
436 	{ 0x03ca, 0x8000 },
437 	{ 0x03cb, 0x8000 },
438 	{ 0x03cc, 0x8000 },
439 	{ 0x03d0, 0x0000 },
440 	{ 0x03d1, 0x0000 },
441 	{ 0x03d2, 0x0000 },
442 	{ 0x03d3, 0x0000 },
443 	{ 0x03d4, 0x2000 },
444 	{ 0x03d5, 0x2000 },
445 	{ 0x03d6, 0x0000 },
446 	{ 0x03d7, 0x0000 },
447 	{ 0x03d8, 0x2000 },
448 	{ 0x03d9, 0x2000 },
449 	{ 0x03da, 0x2000 },
450 	{ 0x03db, 0x2000 },
451 	{ 0x03dc, 0x0000 },
452 	{ 0x03dd, 0x0000 },
453 	{ 0x03de, 0x0000 },
454 	{ 0x03df, 0x2000 },
455 	{ 0x03e0, 0x0000 },
456 	{ 0x03e1, 0x0000 },
457 	{ 0x03e2, 0x0000 },
458 	{ 0x03e3, 0x0000 },
459 	{ 0x03e4, 0x0000 },
460 	{ 0x03e5, 0x0000 },
461 	{ 0x03e6, 0x0000 },
462 	{ 0x03e7, 0x0000 },
463 	{ 0x03e8, 0x0000 },
464 	{ 0x03e9, 0x0000 },
465 	{ 0x03ea, 0x0000 },
466 	{ 0x03eb, 0x0000 },
467 	{ 0x03ec, 0x0000 },
468 	{ 0x03ed, 0x0000 },
469 	{ 0x03ee, 0x0000 },
470 	{ 0x03ef, 0x0000 },
471 	{ 0x03f0, 0x0800 },
472 	{ 0x03f1, 0x0800 },
473 	{ 0x03f2, 0x0800 },
474 	{ 0x03f3, 0x0800 },
475 	{ 0x03fe, 0x0000 },
476 	{ 0x03ff, 0x0000 },
477 	{ 0x07f0, 0x0000 },
478 	{ 0x07fa, 0x0000 },
479 };
480 
481 static const struct reg_default rt5663_reg[] = {
482 	{ 0x0000, 0x0000 },
483 	{ 0x0002, 0x0008 },
484 	{ 0x0005, 0x1000 },
485 	{ 0x0006, 0x1000 },
486 	{ 0x000a, 0x0000 },
487 	{ 0x0010, 0x000f },
488 	{ 0x0015, 0x42f1 },
489 	{ 0x0016, 0x0000 },
490 	{ 0x0018, 0x000b },
491 	{ 0x0019, 0xafaf },
492 	{ 0x001c, 0x2f2f },
493 	{ 0x001f, 0x0000 },
494 	{ 0x0022, 0x5757 },
495 	{ 0x0023, 0x0039 },
496 	{ 0x0026, 0xc0c0 },
497 	{ 0x0029, 0x8080 },
498 	{ 0x002a, 0x8020 },
499 	{ 0x002c, 0x000c },
500 	{ 0x002d, 0x0000 },
501 	{ 0x0040, 0x0808 },
502 	{ 0x0061, 0x0000 },
503 	{ 0x0062, 0x0000 },
504 	{ 0x0063, 0x003e },
505 	{ 0x0064, 0x0000 },
506 	{ 0x0065, 0x0000 },
507 	{ 0x0066, 0x0000 },
508 	{ 0x006b, 0x0000 },
509 	{ 0x006e, 0x0000 },
510 	{ 0x006f, 0x0000 },
511 	{ 0x0070, 0x8020 },
512 	{ 0x0073, 0x1000 },
513 	{ 0x0074, 0xe400 },
514 	{ 0x0075, 0x0002 },
515 	{ 0x0076, 0x0001 },
516 	{ 0x0077, 0x00f0 },
517 	{ 0x0078, 0x0000 },
518 	{ 0x0079, 0x0000 },
519 	{ 0x007a, 0x0123 },
520 	{ 0x007b, 0x8003 },
521 	{ 0x0080, 0x0000 },
522 	{ 0x0081, 0x0000 },
523 	{ 0x0082, 0x0000 },
524 	{ 0x0083, 0x0000 },
525 	{ 0x0084, 0x0000 },
526 	{ 0x0086, 0x0028 },
527 	{ 0x0087, 0x0000 },
528 	{ 0x008a, 0x0000 },
529 	{ 0x008b, 0x0000 },
530 	{ 0x008c, 0x0003 },
531 	{ 0x008e, 0x0008 },
532 	{ 0x008f, 0x1000 },
533 	{ 0x0090, 0x0646 },
534 	{ 0x0091, 0x0e3e },
535 	{ 0x0092, 0x1071 },
536 	{ 0x0093, 0x0000 },
537 	{ 0x0094, 0x0080 },
538 	{ 0x0097, 0x0000 },
539 	{ 0x0098, 0x0000 },
540 	{ 0x009a, 0x0000 },
541 	{ 0x009f, 0x0000 },
542 	{ 0x00ae, 0x6000 },
543 	{ 0x00af, 0x0000 },
544 	{ 0x00b6, 0x0000 },
545 	{ 0x00b7, 0x0000 },
546 	{ 0x00b8, 0x0000 },
547 	{ 0x00ba, 0x0000 },
548 	{ 0x00bb, 0x0000 },
549 	{ 0x00be, 0x0000 },
550 	{ 0x00bf, 0x0000 },
551 	{ 0x00c0, 0x0000 },
552 	{ 0x00c1, 0x0000 },
553 	{ 0x00c5, 0x0000 },
554 	{ 0x00cb, 0xa02f },
555 	{ 0x00cc, 0x0000 },
556 	{ 0x00cd, 0x0e02 },
557 	{ 0x00d9, 0x08f9 },
558 	{ 0x00db, 0x0008 },
559 	{ 0x00dc, 0x00c0 },
560 	{ 0x00dd, 0x6729 },
561 	{ 0x00de, 0x3131 },
562 	{ 0x00df, 0x0008 },
563 	{ 0x00e0, 0x4000 },
564 	{ 0x00e1, 0x3131 },
565 	{ 0x00e2, 0x0043 },
566 	{ 0x00e4, 0x400b },
567 	{ 0x00e5, 0x8031 },
568 	{ 0x00e6, 0x3080 },
569 	{ 0x00e7, 0x4100 },
570 	{ 0x00e8, 0x1400 },
571 	{ 0x00e9, 0xe00a },
572 	{ 0x00ea, 0x0404 },
573 	{ 0x00eb, 0x0404 },
574 	{ 0x00ec, 0xb320 },
575 	{ 0x00ed, 0x0000 },
576 	{ 0x00f4, 0x0000 },
577 	{ 0x00f6, 0x0000 },
578 	{ 0x00f8, 0x0000 },
579 	{ 0x00fa, 0x8000 },
580 	{ 0x00fd, 0x0001 },
581 	{ 0x00fe, 0x10ec },
582 	{ 0x00ff, 0x6406 },
583 	{ 0x0100, 0xa0a0 },
584 	{ 0x0108, 0x4444 },
585 	{ 0x0109, 0x4444 },
586 	{ 0x010a, 0xaaaa },
587 	{ 0x010b, 0x00a0 },
588 	{ 0x010c, 0x8aaa },
589 	{ 0x010d, 0xaaaa },
590 	{ 0x010e, 0x2aaa },
591 	{ 0x010f, 0x002a },
592 	{ 0x0110, 0xa0a4 },
593 	{ 0x0111, 0x4602 },
594 	{ 0x0112, 0x0101 },
595 	{ 0x0113, 0x2000 },
596 	{ 0x0114, 0x0000 },
597 	{ 0x0116, 0x0000 },
598 	{ 0x0117, 0x0f28 },
599 	{ 0x0118, 0x0006 },
600 	{ 0x0125, 0x2424 },
601 	{ 0x0126, 0x5550 },
602 	{ 0x0127, 0x0400 },
603 	{ 0x0128, 0x7711 },
604 	{ 0x0132, 0x0004 },
605 	{ 0x0137, 0x5441 },
606 	{ 0x0139, 0x79a1 },
607 	{ 0x013a, 0x30c0 },
608 	{ 0x013b, 0x2000 },
609 	{ 0x013c, 0x2005 },
610 	{ 0x013d, 0x30c0 },
611 	{ 0x013e, 0x0000 },
612 	{ 0x0140, 0x3700 },
613 	{ 0x0141, 0x1f00 },
614 	{ 0x0144, 0x0000 },
615 	{ 0x0145, 0x0002 },
616 	{ 0x0146, 0x0000 },
617 	{ 0x0160, 0x0e80 },
618 	{ 0x0161, 0x0080 },
619 	{ 0x0162, 0x0200 },
620 	{ 0x0163, 0x0800 },
621 	{ 0x0164, 0x0000 },
622 	{ 0x0165, 0x0000 },
623 	{ 0x0166, 0x0000 },
624 	{ 0x0167, 0x1417 },
625 	{ 0x0168, 0x0017 },
626 	{ 0x0169, 0x0017 },
627 	{ 0x0180, 0x2000 },
628 	{ 0x0181, 0x0000 },
629 	{ 0x0182, 0x0000 },
630 	{ 0x0183, 0x2000 },
631 	{ 0x0184, 0x0000 },
632 	{ 0x0185, 0x0000 },
633 	{ 0x01b0, 0x4b30 },
634 	{ 0x01b1, 0x0000 },
635 	{ 0x01b2, 0xd870 },
636 	{ 0x01b3, 0x0000 },
637 	{ 0x01b4, 0x0030 },
638 	{ 0x01b5, 0x5757 },
639 	{ 0x01b6, 0x5757 },
640 	{ 0x01b7, 0x5757 },
641 	{ 0x01b8, 0x5757 },
642 	{ 0x01c0, 0x433d },
643 	{ 0x01c1, 0x0540 },
644 	{ 0x01c2, 0x0000 },
645 	{ 0x01c3, 0x0000 },
646 	{ 0x01c4, 0x0000 },
647 	{ 0x01c5, 0x0009 },
648 	{ 0x01c6, 0x0018 },
649 	{ 0x01c7, 0x002a },
650 	{ 0x01c8, 0x004c },
651 	{ 0x01c9, 0x0097 },
652 	{ 0x01ca, 0x01c3 },
653 	{ 0x01cb, 0x03e9 },
654 	{ 0x01cc, 0x1389 },
655 	{ 0x01cd, 0xc351 },
656 	{ 0x01ce, 0x0000 },
657 	{ 0x01cf, 0x0000 },
658 	{ 0x01d0, 0x0000 },
659 	{ 0x01d1, 0x0000 },
660 	{ 0x01d2, 0x0000 },
661 	{ 0x01d3, 0x003c },
662 	{ 0x01d4, 0x5757 },
663 	{ 0x01d5, 0x5757 },
664 	{ 0x01d6, 0x5757 },
665 	{ 0x01d7, 0x5757 },
666 	{ 0x01d8, 0x5757 },
667 	{ 0x01d9, 0x5757 },
668 	{ 0x01da, 0x0000 },
669 	{ 0x01db, 0x0000 },
670 	{ 0x01dd, 0x0009 },
671 	{ 0x01de, 0x7f00 },
672 	{ 0x01df, 0x00c8 },
673 	{ 0x01e0, 0x0691 },
674 	{ 0x01e1, 0x0000 },
675 	{ 0x01e2, 0x0000 },
676 	{ 0x01e3, 0x0000 },
677 	{ 0x01e4, 0x0000 },
678 	{ 0x01e5, 0x0040 },
679 	{ 0x01e6, 0x0000 },
680 	{ 0x01e7, 0x0000 },
681 	{ 0x01e8, 0x0000 },
682 	{ 0x01ea, 0x0000 },
683 	{ 0x01eb, 0x0000 },
684 	{ 0x01ec, 0x0000 },
685 	{ 0x01ed, 0x0000 },
686 	{ 0x01ee, 0x0000 },
687 	{ 0x01ef, 0x0000 },
688 	{ 0x01f0, 0x0000 },
689 	{ 0x01f1, 0x0000 },
690 	{ 0x01f2, 0x0000 },
691 	{ 0x0200, 0x0000 },
692 	{ 0x0201, 0x2244 },
693 	{ 0x0202, 0xaaaa },
694 	{ 0x0250, 0x8010 },
695 	{ 0x0251, 0x0000 },
696 	{ 0x0252, 0x028a },
697 	{ 0x02fa, 0x0000 },
698 	{ 0x02fb, 0x8089 },
699 	{ 0x02fc, 0x0300 },
700 	{ 0x0300, 0x0000 },
701 	{ 0x03d0, 0x0000 },
702 	{ 0x03d1, 0x0000 },
703 	{ 0x03d2, 0x0000 },
704 	{ 0x03d3, 0x0000 },
705 	{ 0x03d4, 0x2000 },
706 	{ 0x03d5, 0x2000 },
707 	{ 0x03d6, 0x0000 },
708 	{ 0x03d7, 0x0000 },
709 	{ 0x03d8, 0x2000 },
710 	{ 0x03d9, 0x2000 },
711 	{ 0x03da, 0x2000 },
712 	{ 0x03db, 0x2000 },
713 	{ 0x03dc, 0x0000 },
714 	{ 0x03dd, 0x0000 },
715 	{ 0x03de, 0x0000 },
716 	{ 0x03df, 0x2000 },
717 	{ 0x03e0, 0x0000 },
718 	{ 0x03e1, 0x0000 },
719 	{ 0x03e2, 0x0000 },
720 	{ 0x03e3, 0x0000 },
721 	{ 0x03e4, 0x0000 },
722 	{ 0x03e5, 0x0000 },
723 	{ 0x03e6, 0x0000 },
724 	{ 0x03e7, 0x0000 },
725 	{ 0x03e8, 0x0000 },
726 	{ 0x03e9, 0x0000 },
727 	{ 0x03ea, 0x0000 },
728 	{ 0x03eb, 0x0000 },
729 	{ 0x03ec, 0x0000 },
730 	{ 0x03ed, 0x0000 },
731 	{ 0x03ee, 0x0000 },
732 	{ 0x03ef, 0x0000 },
733 	{ 0x03f0, 0x0800 },
734 	{ 0x03f1, 0x0800 },
735 	{ 0x03f2, 0x0800 },
736 	{ 0x03f3, 0x0800 },
737 };
738 
739 static bool rt5663_volatile_register(struct device *dev, unsigned int reg)
740 {
741 	switch (reg) {
742 	case RT5663_RESET:
743 	case RT5663_SIL_DET_CTL:
744 	case RT5663_HP_IMP_GAIN_2:
745 	case RT5663_AD_DA_MIXER:
746 	case RT5663_FRAC_DIV_2:
747 	case RT5663_MICBIAS_1:
748 	case RT5663_ASRC_11_2:
749 	case RT5663_ADC_EQ_1:
750 	case RT5663_INT_ST_1:
751 	case RT5663_INT_ST_2:
752 	case RT5663_GPIO_STA1:
753 	case RT5663_SIN_GEN_1:
754 	case RT5663_IL_CMD_1:
755 	case RT5663_IL_CMD_5:
756 	case RT5663_IL_CMD_PWRSAV1:
757 	case RT5663_EM_JACK_TYPE_1:
758 	case RT5663_EM_JACK_TYPE_2:
759 	case RT5663_EM_JACK_TYPE_3:
760 	case RT5663_JD_CTRL2:
761 	case RT5663_VENDOR_ID:
762 	case RT5663_VENDOR_ID_1:
763 	case RT5663_VENDOR_ID_2:
764 	case RT5663_PLL_INT_REG:
765 	case RT5663_SOFT_RAMP:
766 	case RT5663_STO_DRE_1:
767 	case RT5663_STO_DRE_5:
768 	case RT5663_STO_DRE_6:
769 	case RT5663_STO_DRE_7:
770 	case RT5663_MIC_DECRO_1:
771 	case RT5663_MIC_DECRO_4:
772 	case RT5663_HP_IMP_SEN_1:
773 	case RT5663_HP_IMP_SEN_3:
774 	case RT5663_HP_IMP_SEN_4:
775 	case RT5663_HP_IMP_SEN_5:
776 	case RT5663_HP_CALIB_1_1:
777 	case RT5663_HP_CALIB_9:
778 	case RT5663_HP_CALIB_ST1:
779 	case RT5663_HP_CALIB_ST2:
780 	case RT5663_HP_CALIB_ST3:
781 	case RT5663_HP_CALIB_ST4:
782 	case RT5663_HP_CALIB_ST5:
783 	case RT5663_HP_CALIB_ST6:
784 	case RT5663_HP_CALIB_ST7:
785 	case RT5663_HP_CALIB_ST8:
786 	case RT5663_HP_CALIB_ST9:
787 	case RT5663_ANA_JD:
788 		return true;
789 	default:
790 		return false;
791 	}
792 }
793 
794 static bool rt5663_readable_register(struct device *dev, unsigned int reg)
795 {
796 	switch (reg) {
797 	case RT5663_RESET:
798 	case RT5663_HP_OUT_EN:
799 	case RT5663_HP_LCH_DRE:
800 	case RT5663_HP_RCH_DRE:
801 	case RT5663_CALIB_BST:
802 	case RT5663_RECMIX:
803 	case RT5663_SIL_DET_CTL:
804 	case RT5663_PWR_SAV_SILDET:
805 	case RT5663_SIDETONE_CTL:
806 	case RT5663_STO1_DAC_DIG_VOL:
807 	case RT5663_STO1_ADC_DIG_VOL:
808 	case RT5663_STO1_BOOST:
809 	case RT5663_HP_IMP_GAIN_1:
810 	case RT5663_HP_IMP_GAIN_2:
811 	case RT5663_STO1_ADC_MIXER:
812 	case RT5663_AD_DA_MIXER:
813 	case RT5663_STO_DAC_MIXER:
814 	case RT5663_DIG_SIDE_MIXER:
815 	case RT5663_BYPASS_STO_DAC:
816 	case RT5663_CALIB_REC_MIX:
817 	case RT5663_PWR_DIG_1:
818 	case RT5663_PWR_DIG_2:
819 	case RT5663_PWR_ANLG_1:
820 	case RT5663_PWR_ANLG_2:
821 	case RT5663_PWR_ANLG_3:
822 	case RT5663_PWR_MIXER:
823 	case RT5663_SIG_CLK_DET:
824 	case RT5663_PRE_DIV_GATING_1:
825 	case RT5663_PRE_DIV_GATING_2:
826 	case RT5663_I2S1_SDP:
827 	case RT5663_ADDA_CLK_1:
828 	case RT5663_ADDA_RST:
829 	case RT5663_FRAC_DIV_1:
830 	case RT5663_FRAC_DIV_2:
831 	case RT5663_TDM_1:
832 	case RT5663_TDM_2:
833 	case RT5663_TDM_3:
834 	case RT5663_TDM_4:
835 	case RT5663_TDM_5:
836 	case RT5663_GLB_CLK:
837 	case RT5663_PLL_1:
838 	case RT5663_PLL_2:
839 	case RT5663_ASRC_1:
840 	case RT5663_ASRC_2:
841 	case RT5663_ASRC_4:
842 	case RT5663_DUMMY_REG:
843 	case RT5663_ASRC_8:
844 	case RT5663_ASRC_9:
845 	case RT5663_ASRC_11:
846 	case RT5663_DEPOP_1:
847 	case RT5663_DEPOP_2:
848 	case RT5663_DEPOP_3:
849 	case RT5663_HP_CHARGE_PUMP_1:
850 	case RT5663_HP_CHARGE_PUMP_2:
851 	case RT5663_MICBIAS_1:
852 	case RT5663_RC_CLK:
853 	case RT5663_ASRC_11_2:
854 	case RT5663_DUMMY_REG_2:
855 	case RT5663_REC_PATH_GAIN:
856 	case RT5663_AUTO_1MRC_CLK:
857 	case RT5663_ADC_EQ_1:
858 	case RT5663_ADC_EQ_2:
859 	case RT5663_IRQ_1:
860 	case RT5663_IRQ_2:
861 	case RT5663_IRQ_3:
862 	case RT5663_IRQ_4:
863 	case RT5663_IRQ_5:
864 	case RT5663_INT_ST_1:
865 	case RT5663_INT_ST_2:
866 	case RT5663_GPIO_1:
867 	case RT5663_GPIO_2:
868 	case RT5663_GPIO_STA1:
869 	case RT5663_SIN_GEN_1:
870 	case RT5663_SIN_GEN_2:
871 	case RT5663_SIN_GEN_3:
872 	case RT5663_SOF_VOL_ZC1:
873 	case RT5663_IL_CMD_1:
874 	case RT5663_IL_CMD_2:
875 	case RT5663_IL_CMD_3:
876 	case RT5663_IL_CMD_4:
877 	case RT5663_IL_CMD_5:
878 	case RT5663_IL_CMD_6:
879 	case RT5663_IL_CMD_7:
880 	case RT5663_IL_CMD_8:
881 	case RT5663_IL_CMD_PWRSAV1:
882 	case RT5663_IL_CMD_PWRSAV2:
883 	case RT5663_EM_JACK_TYPE_1:
884 	case RT5663_EM_JACK_TYPE_2:
885 	case RT5663_EM_JACK_TYPE_3:
886 	case RT5663_EM_JACK_TYPE_4:
887 	case RT5663_EM_JACK_TYPE_5:
888 	case RT5663_EM_JACK_TYPE_6:
889 	case RT5663_STO1_HPF_ADJ1:
890 	case RT5663_STO1_HPF_ADJ2:
891 	case RT5663_FAST_OFF_MICBIAS:
892 	case RT5663_JD_CTRL1:
893 	case RT5663_JD_CTRL2:
894 	case RT5663_DIG_MISC:
895 	case RT5663_VENDOR_ID:
896 	case RT5663_VENDOR_ID_1:
897 	case RT5663_VENDOR_ID_2:
898 	case RT5663_DIG_VOL_ZCD:
899 	case RT5663_ANA_BIAS_CUR_1:
900 	case RT5663_ANA_BIAS_CUR_2:
901 	case RT5663_ANA_BIAS_CUR_3:
902 	case RT5663_ANA_BIAS_CUR_4:
903 	case RT5663_ANA_BIAS_CUR_5:
904 	case RT5663_ANA_BIAS_CUR_6:
905 	case RT5663_BIAS_CUR_5:
906 	case RT5663_BIAS_CUR_6:
907 	case RT5663_BIAS_CUR_7:
908 	case RT5663_BIAS_CUR_8:
909 	case RT5663_DACREF_LDO:
910 	case RT5663_DUMMY_REG_3:
911 	case RT5663_BIAS_CUR_9:
912 	case RT5663_DUMMY_REG_4:
913 	case RT5663_VREFADJ_OP:
914 	case RT5663_VREF_RECMIX:
915 	case RT5663_CHARGE_PUMP_1:
916 	case RT5663_CHARGE_PUMP_1_2:
917 	case RT5663_CHARGE_PUMP_1_3:
918 	case RT5663_CHARGE_PUMP_2:
919 	case RT5663_DIG_IN_PIN1:
920 	case RT5663_PAD_DRV_CTL:
921 	case RT5663_PLL_INT_REG:
922 	case RT5663_CHOP_DAC_L:
923 	case RT5663_CHOP_ADC:
924 	case RT5663_CALIB_ADC:
925 	case RT5663_CHOP_DAC_R:
926 	case RT5663_DUMMY_CTL_DACLR:
927 	case RT5663_DUMMY_REG_5:
928 	case RT5663_SOFT_RAMP:
929 	case RT5663_TEST_MODE_1:
930 	case RT5663_TEST_MODE_2:
931 	case RT5663_TEST_MODE_3:
932 	case RT5663_STO_DRE_1:
933 	case RT5663_STO_DRE_2:
934 	case RT5663_STO_DRE_3:
935 	case RT5663_STO_DRE_4:
936 	case RT5663_STO_DRE_5:
937 	case RT5663_STO_DRE_6:
938 	case RT5663_STO_DRE_7:
939 	case RT5663_STO_DRE_8:
940 	case RT5663_STO_DRE_9:
941 	case RT5663_STO_DRE_10:
942 	case RT5663_MIC_DECRO_1:
943 	case RT5663_MIC_DECRO_2:
944 	case RT5663_MIC_DECRO_3:
945 	case RT5663_MIC_DECRO_4:
946 	case RT5663_MIC_DECRO_5:
947 	case RT5663_MIC_DECRO_6:
948 	case RT5663_HP_DECRO_1:
949 	case RT5663_HP_DECRO_2:
950 	case RT5663_HP_DECRO_3:
951 	case RT5663_HP_DECRO_4:
952 	case RT5663_HP_DECOUP:
953 	case RT5663_HP_IMP_SEN_MAP8:
954 	case RT5663_HP_IMP_SEN_MAP9:
955 	case RT5663_HP_IMP_SEN_MAP10:
956 	case RT5663_HP_IMP_SEN_MAP11:
957 	case RT5663_HP_IMP_SEN_1:
958 	case RT5663_HP_IMP_SEN_2:
959 	case RT5663_HP_IMP_SEN_3:
960 	case RT5663_HP_IMP_SEN_4:
961 	case RT5663_HP_IMP_SEN_5:
962 	case RT5663_HP_IMP_SEN_6:
963 	case RT5663_HP_IMP_SEN_7:
964 	case RT5663_HP_IMP_SEN_8:
965 	case RT5663_HP_IMP_SEN_9:
966 	case RT5663_HP_IMP_SEN_10:
967 	case RT5663_HP_IMP_SEN_11:
968 	case RT5663_HP_IMP_SEN_12:
969 	case RT5663_HP_IMP_SEN_13:
970 	case RT5663_HP_IMP_SEN_14:
971 	case RT5663_HP_IMP_SEN_15:
972 	case RT5663_HP_IMP_SEN_16:
973 	case RT5663_HP_IMP_SEN_17:
974 	case RT5663_HP_IMP_SEN_18:
975 	case RT5663_HP_IMP_SEN_19:
976 	case RT5663_HP_IMPSEN_DIG5:
977 	case RT5663_HP_IMPSEN_MAP1:
978 	case RT5663_HP_IMPSEN_MAP2:
979 	case RT5663_HP_IMPSEN_MAP3:
980 	case RT5663_HP_IMPSEN_MAP4:
981 	case RT5663_HP_IMPSEN_MAP5:
982 	case RT5663_HP_IMPSEN_MAP7:
983 	case RT5663_HP_LOGIC_1:
984 	case RT5663_HP_LOGIC_2:
985 	case RT5663_HP_CALIB_1:
986 	case RT5663_HP_CALIB_1_1:
987 	case RT5663_HP_CALIB_2:
988 	case RT5663_HP_CALIB_3:
989 	case RT5663_HP_CALIB_4:
990 	case RT5663_HP_CALIB_5:
991 	case RT5663_HP_CALIB_5_1:
992 	case RT5663_HP_CALIB_6:
993 	case RT5663_HP_CALIB_7:
994 	case RT5663_HP_CALIB_9:
995 	case RT5663_HP_CALIB_10:
996 	case RT5663_HP_CALIB_11:
997 	case RT5663_HP_CALIB_ST1:
998 	case RT5663_HP_CALIB_ST2:
999 	case RT5663_HP_CALIB_ST3:
1000 	case RT5663_HP_CALIB_ST4:
1001 	case RT5663_HP_CALIB_ST5:
1002 	case RT5663_HP_CALIB_ST6:
1003 	case RT5663_HP_CALIB_ST7:
1004 	case RT5663_HP_CALIB_ST8:
1005 	case RT5663_HP_CALIB_ST9:
1006 	case RT5663_HP_AMP_DET:
1007 	case RT5663_DUMMY_REG_6:
1008 	case RT5663_HP_BIAS:
1009 	case RT5663_CBJ_1:
1010 	case RT5663_CBJ_2:
1011 	case RT5663_CBJ_3:
1012 	case RT5663_DUMMY_1:
1013 	case RT5663_DUMMY_2:
1014 	case RT5663_DUMMY_3:
1015 	case RT5663_ANA_JD:
1016 	case RT5663_ADC_LCH_LPF1_A1:
1017 	case RT5663_ADC_RCH_LPF1_A1:
1018 	case RT5663_ADC_LCH_LPF1_H0:
1019 	case RT5663_ADC_RCH_LPF1_H0:
1020 	case RT5663_ADC_LCH_BPF1_A1:
1021 	case RT5663_ADC_RCH_BPF1_A1:
1022 	case RT5663_ADC_LCH_BPF1_A2:
1023 	case RT5663_ADC_RCH_BPF1_A2:
1024 	case RT5663_ADC_LCH_BPF1_H0:
1025 	case RT5663_ADC_RCH_BPF1_H0:
1026 	case RT5663_ADC_LCH_BPF2_A1:
1027 	case RT5663_ADC_RCH_BPF2_A1:
1028 	case RT5663_ADC_LCH_BPF2_A2:
1029 	case RT5663_ADC_RCH_BPF2_A2:
1030 	case RT5663_ADC_LCH_BPF2_H0:
1031 	case RT5663_ADC_RCH_BPF2_H0:
1032 	case RT5663_ADC_LCH_BPF3_A1:
1033 	case RT5663_ADC_RCH_BPF3_A1:
1034 	case RT5663_ADC_LCH_BPF3_A2:
1035 	case RT5663_ADC_RCH_BPF3_A2:
1036 	case RT5663_ADC_LCH_BPF3_H0:
1037 	case RT5663_ADC_RCH_BPF3_H0:
1038 	case RT5663_ADC_LCH_BPF4_A1:
1039 	case RT5663_ADC_RCH_BPF4_A1:
1040 	case RT5663_ADC_LCH_BPF4_A2:
1041 	case RT5663_ADC_RCH_BPF4_A2:
1042 	case RT5663_ADC_LCH_BPF4_H0:
1043 	case RT5663_ADC_RCH_BPF4_H0:
1044 	case RT5663_ADC_LCH_HPF1_A1:
1045 	case RT5663_ADC_RCH_HPF1_A1:
1046 	case RT5663_ADC_LCH_HPF1_H0:
1047 	case RT5663_ADC_RCH_HPF1_H0:
1048 	case RT5663_ADC_EQ_PRE_VOL_L:
1049 	case RT5663_ADC_EQ_PRE_VOL_R:
1050 	case RT5663_ADC_EQ_POST_VOL_L:
1051 	case RT5663_ADC_EQ_POST_VOL_R:
1052 		return true;
1053 	default:
1054 		return false;
1055 	}
1056 }
1057 
1058 static bool rt5663_v2_volatile_register(struct device *dev, unsigned int reg)
1059 {
1060 	switch (reg) {
1061 	case RT5663_RESET:
1062 	case RT5663_CBJ_TYPE_2:
1063 	case RT5663_PDM_OUT_CTL:
1064 	case RT5663_PDM_I2C_DATA_CTL1:
1065 	case RT5663_PDM_I2C_DATA_CTL4:
1066 	case RT5663_ALC_BK_GAIN:
1067 	case RT5663_PLL_2:
1068 	case RT5663_MICBIAS_1:
1069 	case RT5663_ADC_EQ_1:
1070 	case RT5663_INT_ST_1:
1071 	case RT5663_GPIO_STA2:
1072 	case RT5663_IL_CMD_1:
1073 	case RT5663_IL_CMD_5:
1074 	case RT5663_A_JD_CTRL:
1075 	case RT5663_JD_CTRL2:
1076 	case RT5663_VENDOR_ID:
1077 	case RT5663_VENDOR_ID_1:
1078 	case RT5663_VENDOR_ID_2:
1079 	case RT5663_STO_DRE_1:
1080 	case RT5663_STO_DRE_5:
1081 	case RT5663_STO_DRE_6:
1082 	case RT5663_STO_DRE_7:
1083 	case RT5663_MONO_DYNA_6:
1084 	case RT5663_STO1_SIL_DET:
1085 	case RT5663_MONOL_SIL_DET:
1086 	case RT5663_MONOR_SIL_DET:
1087 	case RT5663_STO2_DAC_SIL:
1088 	case RT5663_MONO_AMP_CAL_ST1:
1089 	case RT5663_MONO_AMP_CAL_ST2:
1090 	case RT5663_MONO_AMP_CAL_ST3:
1091 	case RT5663_MONO_AMP_CAL_ST4:
1092 	case RT5663_HP_IMP_SEN_2:
1093 	case RT5663_HP_IMP_SEN_3:
1094 	case RT5663_HP_IMP_SEN_4:
1095 	case RT5663_HP_IMP_SEN_10:
1096 	case RT5663_HP_CALIB_1:
1097 	case RT5663_HP_CALIB_10:
1098 	case RT5663_HP_CALIB_ST1:
1099 	case RT5663_HP_CALIB_ST4:
1100 	case RT5663_HP_CALIB_ST5:
1101 	case RT5663_HP_CALIB_ST6:
1102 	case RT5663_HP_CALIB_ST7:
1103 	case RT5663_HP_CALIB_ST8:
1104 	case RT5663_HP_CALIB_ST9:
1105 	case RT5663_HP_CALIB_ST10:
1106 	case RT5663_HP_CALIB_ST11:
1107 		return true;
1108 	default:
1109 		return false;
1110 	}
1111 }
1112 
1113 static bool rt5663_v2_readable_register(struct device *dev, unsigned int reg)
1114 {
1115 	switch (reg) {
1116 	case RT5663_LOUT_CTRL:
1117 	case RT5663_HP_AMP_2:
1118 	case RT5663_MONO_OUT:
1119 	case RT5663_MONO_GAIN:
1120 	case RT5663_AEC_BST:
1121 	case RT5663_IN1_IN2:
1122 	case RT5663_IN3_IN4:
1123 	case RT5663_INL1_INR1:
1124 	case RT5663_CBJ_TYPE_2:
1125 	case RT5663_CBJ_TYPE_3:
1126 	case RT5663_CBJ_TYPE_4:
1127 	case RT5663_CBJ_TYPE_5:
1128 	case RT5663_CBJ_TYPE_8:
1129 	case RT5663_DAC3_DIG_VOL:
1130 	case RT5663_DAC3_CTRL:
1131 	case RT5663_MONO_ADC_DIG_VOL:
1132 	case RT5663_STO2_ADC_DIG_VOL:
1133 	case RT5663_MONO_ADC_BST_GAIN:
1134 	case RT5663_STO2_ADC_BST_GAIN:
1135 	case RT5663_SIDETONE_CTRL:
1136 	case RT5663_MONO1_ADC_MIXER:
1137 	case RT5663_STO2_ADC_MIXER:
1138 	case RT5663_MONO_DAC_MIXER:
1139 	case RT5663_DAC2_SRC_CTRL:
1140 	case RT5663_IF_3_4_DATA_CTL:
1141 	case RT5663_IF_5_DATA_CTL:
1142 	case RT5663_PDM_OUT_CTL:
1143 	case RT5663_PDM_I2C_DATA_CTL1:
1144 	case RT5663_PDM_I2C_DATA_CTL2:
1145 	case RT5663_PDM_I2C_DATA_CTL3:
1146 	case RT5663_PDM_I2C_DATA_CTL4:
1147 	case RT5663_RECMIX1_NEW:
1148 	case RT5663_RECMIX1L_0:
1149 	case RT5663_RECMIX1L:
1150 	case RT5663_RECMIX1R_0:
1151 	case RT5663_RECMIX1R:
1152 	case RT5663_RECMIX2_NEW:
1153 	case RT5663_RECMIX2_L_2:
1154 	case RT5663_RECMIX2_R:
1155 	case RT5663_RECMIX2_R_2:
1156 	case RT5663_CALIB_REC_LR:
1157 	case RT5663_ALC_BK_GAIN:
1158 	case RT5663_MONOMIX_GAIN:
1159 	case RT5663_MONOMIX_IN_GAIN:
1160 	case RT5663_OUT_MIXL_GAIN:
1161 	case RT5663_OUT_LMIX_IN_GAIN:
1162 	case RT5663_OUT_RMIX_IN_GAIN:
1163 	case RT5663_OUT_RMIX_IN_GAIN1:
1164 	case RT5663_LOUT_MIXER_CTRL:
1165 	case RT5663_PWR_VOL:
1166 	case RT5663_ADCDAC_RST:
1167 	case RT5663_I2S34_SDP:
1168 	case RT5663_I2S5_SDP:
1169 	case RT5663_TDM_6:
1170 	case RT5663_TDM_7:
1171 	case RT5663_TDM_8:
1172 	case RT5663_TDM_9:
1173 	case RT5663_ASRC_3:
1174 	case RT5663_ASRC_6:
1175 	case RT5663_ASRC_7:
1176 	case RT5663_PLL_TRK_13:
1177 	case RT5663_I2S_M_CLK_CTL:
1178 	case RT5663_FDIV_I2S34_M_CLK:
1179 	case RT5663_FDIV_I2S34_M_CLK2:
1180 	case RT5663_FDIV_I2S5_M_CLK:
1181 	case RT5663_FDIV_I2S5_M_CLK2:
1182 	case RT5663_V2_IRQ_4:
1183 	case RT5663_GPIO_3:
1184 	case RT5663_GPIO_4:
1185 	case RT5663_GPIO_STA2:
1186 	case RT5663_HP_AMP_DET1:
1187 	case RT5663_HP_AMP_DET2:
1188 	case RT5663_HP_AMP_DET3:
1189 	case RT5663_MID_BD_HP_AMP:
1190 	case RT5663_LOW_BD_HP_AMP:
1191 	case RT5663_SOF_VOL_ZC2:
1192 	case RT5663_ADC_STO2_ADJ1:
1193 	case RT5663_ADC_STO2_ADJ2:
1194 	case RT5663_A_JD_CTRL:
1195 	case RT5663_JD1_TRES_CTRL:
1196 	case RT5663_JD2_TRES_CTRL:
1197 	case RT5663_V2_JD_CTRL2:
1198 	case RT5663_DUM_REG_2:
1199 	case RT5663_DUM_REG_3:
1200 	case RT5663_VENDOR_ID:
1201 	case RT5663_VENDOR_ID_1:
1202 	case RT5663_VENDOR_ID_2:
1203 	case RT5663_DACADC_DIG_VOL2:
1204 	case RT5663_DIG_IN_PIN2:
1205 	case RT5663_PAD_DRV_CTL1:
1206 	case RT5663_SOF_RAM_DEPOP:
1207 	case RT5663_VOL_TEST:
1208 	case RT5663_TEST_MODE_4:
1209 	case RT5663_TEST_MODE_5:
1210 	case RT5663_STO_DRE_9:
1211 	case RT5663_MONO_DYNA_1:
1212 	case RT5663_MONO_DYNA_2:
1213 	case RT5663_MONO_DYNA_3:
1214 	case RT5663_MONO_DYNA_4:
1215 	case RT5663_MONO_DYNA_5:
1216 	case RT5663_MONO_DYNA_6:
1217 	case RT5663_STO1_SIL_DET:
1218 	case RT5663_MONOL_SIL_DET:
1219 	case RT5663_MONOR_SIL_DET:
1220 	case RT5663_STO2_DAC_SIL:
1221 	case RT5663_PWR_SAV_CTL1:
1222 	case RT5663_PWR_SAV_CTL2:
1223 	case RT5663_PWR_SAV_CTL3:
1224 	case RT5663_PWR_SAV_CTL4:
1225 	case RT5663_PWR_SAV_CTL5:
1226 	case RT5663_PWR_SAV_CTL6:
1227 	case RT5663_MONO_AMP_CAL1:
1228 	case RT5663_MONO_AMP_CAL2:
1229 	case RT5663_MONO_AMP_CAL3:
1230 	case RT5663_MONO_AMP_CAL4:
1231 	case RT5663_MONO_AMP_CAL5:
1232 	case RT5663_MONO_AMP_CAL6:
1233 	case RT5663_MONO_AMP_CAL7:
1234 	case RT5663_MONO_AMP_CAL_ST1:
1235 	case RT5663_MONO_AMP_CAL_ST2:
1236 	case RT5663_MONO_AMP_CAL_ST3:
1237 	case RT5663_MONO_AMP_CAL_ST4:
1238 	case RT5663_MONO_AMP_CAL_ST5:
1239 	case RT5663_V2_HP_IMP_SEN_13:
1240 	case RT5663_V2_HP_IMP_SEN_14:
1241 	case RT5663_V2_HP_IMP_SEN_6:
1242 	case RT5663_V2_HP_IMP_SEN_7:
1243 	case RT5663_V2_HP_IMP_SEN_8:
1244 	case RT5663_V2_HP_IMP_SEN_9:
1245 	case RT5663_V2_HP_IMP_SEN_10:
1246 	case RT5663_HP_LOGIC_3:
1247 	case RT5663_HP_CALIB_ST10:
1248 	case RT5663_HP_CALIB_ST11:
1249 	case RT5663_PRO_REG_TBL_4:
1250 	case RT5663_PRO_REG_TBL_5:
1251 	case RT5663_PRO_REG_TBL_6:
1252 	case RT5663_PRO_REG_TBL_7:
1253 	case RT5663_PRO_REG_TBL_8:
1254 	case RT5663_PRO_REG_TBL_9:
1255 	case RT5663_SAR_ADC_INL_1:
1256 	case RT5663_SAR_ADC_INL_2:
1257 	case RT5663_SAR_ADC_INL_3:
1258 	case RT5663_SAR_ADC_INL_4:
1259 	case RT5663_SAR_ADC_INL_5:
1260 	case RT5663_SAR_ADC_INL_6:
1261 	case RT5663_SAR_ADC_INL_7:
1262 	case RT5663_SAR_ADC_INL_8:
1263 	case RT5663_SAR_ADC_INL_9:
1264 	case RT5663_SAR_ADC_INL_10:
1265 	case RT5663_SAR_ADC_INL_11:
1266 	case RT5663_SAR_ADC_INL_12:
1267 	case RT5663_DRC_CTRL_1:
1268 	case RT5663_DRC1_CTRL_2:
1269 	case RT5663_DRC1_CTRL_3:
1270 	case RT5663_DRC1_CTRL_4:
1271 	case RT5663_DRC1_CTRL_5:
1272 	case RT5663_DRC1_CTRL_6:
1273 	case RT5663_DRC1_HD_CTRL_1:
1274 	case RT5663_DRC1_HD_CTRL_2:
1275 	case RT5663_DRC1_PRI_REG_1:
1276 	case RT5663_DRC1_PRI_REG_2:
1277 	case RT5663_DRC1_PRI_REG_3:
1278 	case RT5663_DRC1_PRI_REG_4:
1279 	case RT5663_DRC1_PRI_REG_5:
1280 	case RT5663_DRC1_PRI_REG_6:
1281 	case RT5663_DRC1_PRI_REG_7:
1282 	case RT5663_DRC1_PRI_REG_8:
1283 	case RT5663_ALC_PGA_CTL_1:
1284 	case RT5663_ALC_PGA_CTL_2:
1285 	case RT5663_ALC_PGA_CTL_3:
1286 	case RT5663_ALC_PGA_CTL_4:
1287 	case RT5663_ALC_PGA_CTL_5:
1288 	case RT5663_ALC_PGA_CTL_6:
1289 	case RT5663_ALC_PGA_CTL_7:
1290 	case RT5663_ALC_PGA_CTL_8:
1291 	case RT5663_ALC_PGA_REG_1:
1292 	case RT5663_ALC_PGA_REG_2:
1293 	case RT5663_ALC_PGA_REG_3:
1294 	case RT5663_ADC_EQ_RECOV_1:
1295 	case RT5663_ADC_EQ_RECOV_2:
1296 	case RT5663_ADC_EQ_RECOV_3:
1297 	case RT5663_ADC_EQ_RECOV_4:
1298 	case RT5663_ADC_EQ_RECOV_5:
1299 	case RT5663_ADC_EQ_RECOV_6:
1300 	case RT5663_ADC_EQ_RECOV_7:
1301 	case RT5663_ADC_EQ_RECOV_8:
1302 	case RT5663_ADC_EQ_RECOV_9:
1303 	case RT5663_ADC_EQ_RECOV_10:
1304 	case RT5663_ADC_EQ_RECOV_11:
1305 	case RT5663_ADC_EQ_RECOV_12:
1306 	case RT5663_ADC_EQ_RECOV_13:
1307 	case RT5663_VID_HIDDEN:
1308 	case RT5663_VID_CUSTOMER:
1309 	case RT5663_SCAN_MODE:
1310 	case RT5663_I2C_BYPA:
1311 		return true;
1312 	case RT5663_TDM_1:
1313 	case RT5663_DEPOP_3:
1314 	case RT5663_ASRC_11_2:
1315 	case RT5663_INT_ST_2:
1316 	case RT5663_GPIO_STA1:
1317 	case RT5663_SIN_GEN_1:
1318 	case RT5663_SIN_GEN_2:
1319 	case RT5663_SIN_GEN_3:
1320 	case RT5663_IL_CMD_PWRSAV1:
1321 	case RT5663_IL_CMD_PWRSAV2:
1322 	case RT5663_EM_JACK_TYPE_1:
1323 	case RT5663_EM_JACK_TYPE_2:
1324 	case RT5663_EM_JACK_TYPE_3:
1325 	case RT5663_EM_JACK_TYPE_4:
1326 	case RT5663_FAST_OFF_MICBIAS:
1327 	case RT5663_ANA_BIAS_CUR_1:
1328 	case RT5663_ANA_BIAS_CUR_2:
1329 	case RT5663_BIAS_CUR_9:
1330 	case RT5663_DUMMY_REG_4:
1331 	case RT5663_VREF_RECMIX:
1332 	case RT5663_CHARGE_PUMP_1_2:
1333 	case RT5663_CHARGE_PUMP_1_3:
1334 	case RT5663_CHARGE_PUMP_2:
1335 	case RT5663_CHOP_DAC_R:
1336 	case RT5663_DUMMY_CTL_DACLR:
1337 	case RT5663_DUMMY_REG_5:
1338 	case RT5663_SOFT_RAMP:
1339 	case RT5663_TEST_MODE_1:
1340 	case RT5663_STO_DRE_10:
1341 	case RT5663_MIC_DECRO_1:
1342 	case RT5663_MIC_DECRO_2:
1343 	case RT5663_MIC_DECRO_3:
1344 	case RT5663_MIC_DECRO_4:
1345 	case RT5663_MIC_DECRO_5:
1346 	case RT5663_MIC_DECRO_6:
1347 	case RT5663_HP_DECRO_1:
1348 	case RT5663_HP_DECRO_2:
1349 	case RT5663_HP_DECRO_3:
1350 	case RT5663_HP_DECRO_4:
1351 	case RT5663_HP_DECOUP:
1352 	case RT5663_HP_IMPSEN_MAP4:
1353 	case RT5663_HP_IMPSEN_MAP5:
1354 	case RT5663_HP_IMPSEN_MAP7:
1355 	case RT5663_HP_CALIB_1:
1356 	case RT5663_CBJ_1:
1357 	case RT5663_CBJ_2:
1358 	case RT5663_CBJ_3:
1359 		return false;
1360 	default:
1361 		return rt5663_readable_register(dev, reg);
1362 	}
1363 }
1364 
1365 static const DECLARE_TLV_DB_SCALE(rt5663_hp_vol_tlv, -2400, 150, 0);
1366 static const DECLARE_TLV_DB_SCALE(rt5663_v2_hp_vol_tlv, -2250, 150, 0);
1367 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -6525, 75, 0);
1368 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -1725, 75, 0);
1369 
1370 /* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
1371 static const DECLARE_TLV_DB_RANGE(in_bst_tlv,
1372 	0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
1373 	1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
1374 	2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
1375 	3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
1376 	6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
1377 	7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
1378 	8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
1379 );
1380 
1381 /* Interface data select */
1382 static const char * const rt5663_if1_adc_data_select[] = {
1383 	"L/R", "R/L", "L/L", "R/R"
1384 };
1385 
1386 static SOC_ENUM_SINGLE_DECL(rt5663_if1_adc_enum, RT5663_TDM_2,
1387 	RT5663_DATA_SWAP_ADCDAT1_SHIFT, rt5663_if1_adc_data_select);
1388 
1389 static void rt5663_enable_push_button_irq(struct snd_soc_component *component,
1390 	bool enable)
1391 {
1392 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
1393 
1394 	if (enable) {
1395 		snd_soc_component_update_bits(component, RT5663_IL_CMD_6,
1396 			RT5663_EN_4BTN_INL_MASK, RT5663_EN_4BTN_INL_EN);
1397 		/* reset in-line command */
1398 		snd_soc_component_update_bits(component, RT5663_IL_CMD_6,
1399 			RT5663_RESET_4BTN_INL_MASK,
1400 			RT5663_RESET_4BTN_INL_RESET);
1401 		snd_soc_component_update_bits(component, RT5663_IL_CMD_6,
1402 			RT5663_RESET_4BTN_INL_MASK,
1403 			RT5663_RESET_4BTN_INL_NOR);
1404 		switch (rt5663->codec_ver) {
1405 		case CODEC_VER_1:
1406 			snd_soc_component_update_bits(component, RT5663_IRQ_3,
1407 				RT5663_V2_EN_IRQ_INLINE_MASK,
1408 				RT5663_V2_EN_IRQ_INLINE_NOR);
1409 			break;
1410 		case CODEC_VER_0:
1411 			snd_soc_component_update_bits(component, RT5663_IRQ_2,
1412 				RT5663_EN_IRQ_INLINE_MASK,
1413 				RT5663_EN_IRQ_INLINE_NOR);
1414 			break;
1415 		default:
1416 			dev_err(component->dev, "Unknown CODEC Version\n");
1417 		}
1418 	} else {
1419 		switch (rt5663->codec_ver) {
1420 		case CODEC_VER_1:
1421 			snd_soc_component_update_bits(component, RT5663_IRQ_3,
1422 				RT5663_V2_EN_IRQ_INLINE_MASK,
1423 				RT5663_V2_EN_IRQ_INLINE_BYP);
1424 			break;
1425 		case CODEC_VER_0:
1426 			snd_soc_component_update_bits(component, RT5663_IRQ_2,
1427 				RT5663_EN_IRQ_INLINE_MASK,
1428 				RT5663_EN_IRQ_INLINE_BYP);
1429 			break;
1430 		default:
1431 			dev_err(component->dev, "Unknown CODEC Version\n");
1432 		}
1433 		snd_soc_component_update_bits(component, RT5663_IL_CMD_6,
1434 			RT5663_EN_4BTN_INL_MASK, RT5663_EN_4BTN_INL_DIS);
1435 		/* reset in-line command */
1436 		snd_soc_component_update_bits(component, RT5663_IL_CMD_6,
1437 			RT5663_RESET_4BTN_INL_MASK,
1438 			RT5663_RESET_4BTN_INL_RESET);
1439 		snd_soc_component_update_bits(component, RT5663_IL_CMD_6,
1440 			RT5663_RESET_4BTN_INL_MASK,
1441 			RT5663_RESET_4BTN_INL_NOR);
1442 	}
1443 }
1444 
1445 /**
1446  * rt5663_v2_jack_detect - Detect headset.
1447  * @component: SoC audio component device.
1448  * @jack_insert: Jack insert or not.
1449  *
1450  * Detect whether is headset or not when jack inserted.
1451  *
1452  * Returns detect status.
1453  */
1454 
1455 static int rt5663_v2_jack_detect(struct snd_soc_component *component, int jack_insert)
1456 {
1457 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1458 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
1459 	int val, i = 0, sleep_time[5] = {300, 150, 100, 50, 30};
1460 
1461 	dev_dbg(component->dev, "%s jack_insert:%d\n", __func__, jack_insert);
1462 	if (jack_insert) {
1463 		snd_soc_component_write(component, RT5663_CBJ_TYPE_2, 0x8040);
1464 		snd_soc_component_write(component, RT5663_CBJ_TYPE_3, 0x1484);
1465 
1466 		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
1467 		snd_soc_dapm_force_enable_pin(dapm, "MICBIAS2");
1468 		snd_soc_dapm_force_enable_pin(dapm, "Mic Det Power");
1469 		snd_soc_dapm_force_enable_pin(dapm, "CBJ Power");
1470 		snd_soc_dapm_sync(dapm);
1471 		snd_soc_component_update_bits(component, RT5663_RC_CLK,
1472 			RT5663_DIG_1M_CLK_MASK, RT5663_DIG_1M_CLK_EN);
1473 		snd_soc_component_update_bits(component, RT5663_RECMIX, 0x8, 0x8);
1474 
1475 		while (i < 5) {
1476 			msleep(sleep_time[i]);
1477 			val = snd_soc_component_read32(component, RT5663_CBJ_TYPE_2) & 0x0003;
1478 			if (val == 0x1 || val == 0x2 || val == 0x3)
1479 				break;
1480 			dev_dbg(component->dev, "%s: MX-0011 val=%x sleep %d\n",
1481 				__func__, val, sleep_time[i]);
1482 			i++;
1483 		}
1484 		dev_dbg(component->dev, "%s val = %d\n", __func__, val);
1485 		switch (val) {
1486 		case 1:
1487 		case 2:
1488 			rt5663->jack_type = SND_JACK_HEADSET;
1489 			rt5663_enable_push_button_irq(component, true);
1490 			break;
1491 		default:
1492 			snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
1493 			snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
1494 			snd_soc_dapm_disable_pin(dapm, "Mic Det Power");
1495 			snd_soc_dapm_disable_pin(dapm, "CBJ Power");
1496 			snd_soc_dapm_sync(dapm);
1497 			rt5663->jack_type = SND_JACK_HEADPHONE;
1498 			break;
1499 		}
1500 	} else {
1501 		snd_soc_component_update_bits(component, RT5663_RECMIX, 0x8, 0x0);
1502 
1503 		if (rt5663->jack_type == SND_JACK_HEADSET) {
1504 			rt5663_enable_push_button_irq(component, false);
1505 			snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
1506 			snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
1507 			snd_soc_dapm_disable_pin(dapm, "Mic Det Power");
1508 			snd_soc_dapm_disable_pin(dapm, "CBJ Power");
1509 			snd_soc_dapm_sync(dapm);
1510 		}
1511 		rt5663->jack_type = 0;
1512 	}
1513 
1514 	dev_dbg(component->dev, "jack_type = %d\n", rt5663->jack_type);
1515 	return rt5663->jack_type;
1516 }
1517 
1518 /**
1519  * rt5663_jack_detect - Detect headset.
1520  * @component: SoC audio component device.
1521  * @jack_insert: Jack insert or not.
1522  *
1523  * Detect whether is headset or not when jack inserted.
1524  *
1525  * Returns detect status.
1526  */
1527 static int rt5663_jack_detect(struct snd_soc_component *component, int jack_insert)
1528 {
1529 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
1530 	int val, i = 0;
1531 
1532 	dev_dbg(component->dev, "%s jack_insert:%d\n", __func__, jack_insert);
1533 
1534 	if (jack_insert) {
1535 		snd_soc_component_update_bits(component, RT5663_DIG_MISC,
1536 			RT5663_DIG_GATE_CTRL_MASK, RT5663_DIG_GATE_CTRL_EN);
1537 		snd_soc_component_update_bits(component, RT5663_HP_CHARGE_PUMP_1,
1538 			RT5663_SI_HP_MASK | RT5663_OSW_HP_L_MASK |
1539 			RT5663_OSW_HP_R_MASK, RT5663_SI_HP_EN |
1540 			RT5663_OSW_HP_L_DIS | RT5663_OSW_HP_R_DIS);
1541 		snd_soc_component_update_bits(component, RT5663_DUMMY_1,
1542 			RT5663_EMB_CLK_MASK | RT5663_HPA_CPL_BIAS_MASK |
1543 			RT5663_HPA_CPR_BIAS_MASK, RT5663_EMB_CLK_EN |
1544 			RT5663_HPA_CPL_BIAS_1 | RT5663_HPA_CPR_BIAS_1);
1545 		snd_soc_component_update_bits(component, RT5663_CBJ_1,
1546 			RT5663_INBUF_CBJ_BST1_MASK | RT5663_CBJ_SENSE_BST1_MASK,
1547 			RT5663_INBUF_CBJ_BST1_ON | RT5663_CBJ_SENSE_BST1_L);
1548 		snd_soc_component_update_bits(component, RT5663_IL_CMD_2,
1549 			RT5663_PWR_MIC_DET_MASK, RT5663_PWR_MIC_DET_ON);
1550 		/* BST1 power on for JD */
1551 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_2,
1552 			RT5663_PWR_BST1_MASK, RT5663_PWR_BST1_ON);
1553 		snd_soc_component_update_bits(component, RT5663_EM_JACK_TYPE_1,
1554 			RT5663_CBJ_DET_MASK | RT5663_EXT_JD_MASK |
1555 			RT5663_POL_EXT_JD_MASK, RT5663_CBJ_DET_EN |
1556 			RT5663_EXT_JD_EN | RT5663_POL_EXT_JD_EN);
1557 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
1558 			RT5663_PWR_MB_MASK | RT5663_LDO1_DVO_MASK |
1559 			RT5663_AMP_HP_MASK, RT5663_PWR_MB |
1560 			RT5663_LDO1_DVO_0_9V | RT5663_AMP_HP_3X);
1561 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
1562 			RT5663_PWR_VREF1_MASK | RT5663_PWR_VREF2_MASK |
1563 			RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK,
1564 			RT5663_PWR_VREF1 | RT5663_PWR_VREF2);
1565 		msleep(20);
1566 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
1567 			RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK,
1568 			RT5663_PWR_FV1 | RT5663_PWR_FV2);
1569 		snd_soc_component_update_bits(component, RT5663_AUTO_1MRC_CLK,
1570 			RT5663_IRQ_POW_SAV_MASK, RT5663_IRQ_POW_SAV_EN);
1571 		snd_soc_component_update_bits(component, RT5663_IRQ_1,
1572 			RT5663_EN_IRQ_JD1_MASK, RT5663_EN_IRQ_JD1_EN);
1573 		snd_soc_component_update_bits(component, RT5663_EM_JACK_TYPE_1,
1574 			RT5663_EM_JD_MASK, RT5663_EM_JD_RST);
1575 		snd_soc_component_update_bits(component, RT5663_EM_JACK_TYPE_1,
1576 			RT5663_EM_JD_MASK, RT5663_EM_JD_NOR);
1577 
1578 		while (true) {
1579 			regmap_read(rt5663->regmap, RT5663_INT_ST_2, &val);
1580 			if (!(val & 0x80))
1581 				usleep_range(10000, 10005);
1582 			else
1583 				break;
1584 
1585 			if (i > 200)
1586 				break;
1587 			i++;
1588 		}
1589 
1590 		val = snd_soc_component_read32(component, RT5663_EM_JACK_TYPE_2) & 0x0003;
1591 		dev_dbg(component->dev, "%s val = %d\n", __func__, val);
1592 
1593 		snd_soc_component_update_bits(component, RT5663_HP_CHARGE_PUMP_1,
1594 			RT5663_OSW_HP_L_MASK | RT5663_OSW_HP_R_MASK,
1595 			RT5663_OSW_HP_L_EN | RT5663_OSW_HP_R_EN);
1596 
1597 		switch (val) {
1598 		case 1:
1599 		case 2:
1600 			rt5663->jack_type = SND_JACK_HEADSET;
1601 			rt5663_enable_push_button_irq(component, true);
1602 
1603 			if (rt5663->pdata.impedance_sensing_num)
1604 				break;
1605 
1606 			if (rt5663->pdata.dc_offset_l_manual_mic) {
1607 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_2,
1608 					rt5663->pdata.dc_offset_l_manual_mic >>
1609 					16);
1610 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_3,
1611 					rt5663->pdata.dc_offset_l_manual_mic &
1612 					0xffff);
1613 			}
1614 
1615 			if (rt5663->pdata.dc_offset_r_manual_mic) {
1616 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_5,
1617 					rt5663->pdata.dc_offset_r_manual_mic >>
1618 					16);
1619 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_6,
1620 					rt5663->pdata.dc_offset_r_manual_mic &
1621 					0xffff);
1622 			}
1623 			break;
1624 		default:
1625 			rt5663->jack_type = SND_JACK_HEADPHONE;
1626 			snd_soc_component_update_bits(component,
1627 				RT5663_PWR_ANLG_1,
1628 				RT5663_PWR_MB_MASK | RT5663_PWR_VREF1_MASK |
1629 				RT5663_PWR_VREF2_MASK, 0);
1630 			if (rt5663->pdata.impedance_sensing_num)
1631 				break;
1632 
1633 			if (rt5663->pdata.dc_offset_l_manual) {
1634 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_2,
1635 					rt5663->pdata.dc_offset_l_manual >> 16);
1636 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_3,
1637 					rt5663->pdata.dc_offset_l_manual &
1638 					0xffff);
1639 			}
1640 
1641 			if (rt5663->pdata.dc_offset_r_manual) {
1642 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_5,
1643 					rt5663->pdata.dc_offset_r_manual >> 16);
1644 				regmap_write(rt5663->regmap, RT5663_MIC_DECRO_6,
1645 					rt5663->pdata.dc_offset_r_manual &
1646 					0xffff);
1647 			}
1648 			break;
1649 		}
1650 	} else {
1651 		if (rt5663->jack_type == SND_JACK_HEADSET)
1652 			rt5663_enable_push_button_irq(component, false);
1653 		rt5663->jack_type = 0;
1654 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
1655 			RT5663_PWR_MB_MASK | RT5663_PWR_VREF1_MASK |
1656 			RT5663_PWR_VREF2_MASK, 0);
1657 	}
1658 
1659 	dev_dbg(component->dev, "jack_type = %d\n", rt5663->jack_type);
1660 	return rt5663->jack_type;
1661 }
1662 
1663 static int rt5663_impedance_sensing(struct snd_soc_component *component)
1664 {
1665 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
1666 	unsigned int value, i, reg84, reg26, reg2fa, reg91, reg10, reg80;
1667 
1668 	for (i = 0; i < rt5663->pdata.impedance_sensing_num; i++) {
1669 		if (rt5663->imp_table[i].vol == 7)
1670 			break;
1671 	}
1672 
1673 	if (rt5663->jack_type == SND_JACK_HEADSET) {
1674 		snd_soc_component_write(component, RT5663_MIC_DECRO_2,
1675 			rt5663->imp_table[i].dc_offset_l_manual_mic >> 16);
1676 		snd_soc_component_write(component, RT5663_MIC_DECRO_3,
1677 			rt5663->imp_table[i].dc_offset_l_manual_mic & 0xffff);
1678 		snd_soc_component_write(component, RT5663_MIC_DECRO_5,
1679 			rt5663->imp_table[i].dc_offset_r_manual_mic >> 16);
1680 		snd_soc_component_write(component, RT5663_MIC_DECRO_6,
1681 			rt5663->imp_table[i].dc_offset_r_manual_mic & 0xffff);
1682 	} else {
1683 		snd_soc_component_write(component, RT5663_MIC_DECRO_2,
1684 			rt5663->imp_table[i].dc_offset_l_manual >> 16);
1685 		snd_soc_component_write(component, RT5663_MIC_DECRO_3,
1686 			rt5663->imp_table[i].dc_offset_l_manual & 0xffff);
1687 		snd_soc_component_write(component, RT5663_MIC_DECRO_5,
1688 			rt5663->imp_table[i].dc_offset_r_manual >> 16);
1689 		snd_soc_component_write(component, RT5663_MIC_DECRO_6,
1690 			rt5663->imp_table[i].dc_offset_r_manual & 0xffff);
1691 	}
1692 
1693 	reg84 = snd_soc_component_read32(component, RT5663_ASRC_2);
1694 	reg26 = snd_soc_component_read32(component, RT5663_STO1_ADC_MIXER);
1695 	reg2fa = snd_soc_component_read32(component, RT5663_DUMMY_1);
1696 	reg91 = snd_soc_component_read32(component, RT5663_HP_CHARGE_PUMP_1);
1697 	reg10 = snd_soc_component_read32(component, RT5663_RECMIX);
1698 	reg80 = snd_soc_component_read32(component, RT5663_GLB_CLK);
1699 
1700 	snd_soc_component_update_bits(component, RT5663_STO_DRE_1, 0x8000, 0);
1701 	snd_soc_component_write(component, RT5663_ASRC_2, 0);
1702 	snd_soc_component_write(component, RT5663_STO1_ADC_MIXER, 0x4040);
1703 	snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
1704 		RT5663_PWR_VREF1_MASK | RT5663_PWR_VREF2_MASK |
1705 		RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK,
1706 		RT5663_PWR_VREF1 | RT5663_PWR_VREF2);
1707 	usleep_range(10000, 10005);
1708 	snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
1709 		RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK,
1710 		RT5663_PWR_FV1 | RT5663_PWR_FV2);
1711 	snd_soc_component_update_bits(component, RT5663_GLB_CLK, RT5663_SCLK_SRC_MASK,
1712 		RT5663_SCLK_SRC_RCCLK);
1713 	snd_soc_component_update_bits(component, RT5663_RC_CLK, RT5663_DIG_25M_CLK_MASK,
1714 		RT5663_DIG_25M_CLK_EN);
1715 	snd_soc_component_update_bits(component, RT5663_ADDA_CLK_1, RT5663_I2S_PD1_MASK, 0);
1716 	snd_soc_component_write(component, RT5663_PRE_DIV_GATING_1, 0xff00);
1717 	snd_soc_component_write(component, RT5663_PRE_DIV_GATING_2, 0xfffc);
1718 	snd_soc_component_write(component, RT5663_HP_CHARGE_PUMP_1, 0x1232);
1719 	snd_soc_component_write(component, RT5663_HP_LOGIC_2, 0x0005);
1720 	snd_soc_component_write(component, RT5663_DEPOP_2, 0x3003);
1721 	snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0030, 0x0030);
1722 	snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0003, 0x0003);
1723 	snd_soc_component_update_bits(component, RT5663_PWR_DIG_2,
1724 		RT5663_PWR_ADC_S1F | RT5663_PWR_DAC_S1F,
1725 		RT5663_PWR_ADC_S1F | RT5663_PWR_DAC_S1F);
1726 	snd_soc_component_update_bits(component, RT5663_PWR_DIG_1,
1727 		RT5663_PWR_DAC_L1 | RT5663_PWR_DAC_R1 |
1728 		RT5663_PWR_LDO_DACREF_MASK | RT5663_PWR_ADC_L1 |
1729 		RT5663_PWR_ADC_R1,
1730 		RT5663_PWR_DAC_L1 | RT5663_PWR_DAC_R1 |
1731 		RT5663_PWR_LDO_DACREF_ON | RT5663_PWR_ADC_L1 |
1732 		RT5663_PWR_ADC_R1);
1733 	msleep(40);
1734 	snd_soc_component_update_bits(component, RT5663_PWR_ANLG_2,
1735 		RT5663_PWR_RECMIX1 | RT5663_PWR_RECMIX2,
1736 		RT5663_PWR_RECMIX1 | RT5663_PWR_RECMIX2);
1737 	msleep(30);
1738 	snd_soc_component_write(component, RT5663_HP_CHARGE_PUMP_2, 0x1371);
1739 	snd_soc_component_write(component, RT5663_STO_DAC_MIXER, 0);
1740 	snd_soc_component_write(component, RT5663_BYPASS_STO_DAC, 0x000c);
1741 	snd_soc_component_write(component, RT5663_HP_BIAS, 0xafaa);
1742 	snd_soc_component_write(component, RT5663_CHARGE_PUMP_1, 0x2224);
1743 	snd_soc_component_write(component, RT5663_HP_OUT_EN, 0x8088);
1744 	snd_soc_component_write(component, RT5663_CHOP_ADC, 0x3000);
1745 	snd_soc_component_write(component, RT5663_ADDA_RST, 0xc000);
1746 	snd_soc_component_write(component, RT5663_STO1_HPF_ADJ1, 0x3320);
1747 	snd_soc_component_write(component, RT5663_HP_CALIB_2, 0x00c9);
1748 	snd_soc_component_write(component, RT5663_DUMMY_1, 0x004c);
1749 	snd_soc_component_write(component, RT5663_ANA_BIAS_CUR_1, 0x7733);
1750 	snd_soc_component_write(component, RT5663_CHARGE_PUMP_2, 0x7777);
1751 	snd_soc_component_write(component, RT5663_STO_DRE_9, 0x0007);
1752 	snd_soc_component_write(component, RT5663_STO_DRE_10, 0x0007);
1753 	snd_soc_component_write(component, RT5663_DUMMY_2, 0x02a4);
1754 	snd_soc_component_write(component, RT5663_RECMIX, 0x0005);
1755 	snd_soc_component_write(component, RT5663_HP_IMP_SEN_1, 0x4334);
1756 	snd_soc_component_update_bits(component, RT5663_IRQ_3, 0x0004, 0x0004);
1757 	snd_soc_component_write(component, RT5663_HP_LOGIC_1, 0x2200);
1758 	snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x3000, 0x3000);
1759 	snd_soc_component_write(component, RT5663_HP_LOGIC_1, 0x6200);
1760 
1761 	for (i = 0; i < 100; i++) {
1762 		msleep(20);
1763 		if (snd_soc_component_read32(component, RT5663_INT_ST_1) & 0x2)
1764 			break;
1765 	}
1766 
1767 	value = snd_soc_component_read32(component, RT5663_HP_IMP_SEN_4);
1768 
1769 	snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x3000, 0);
1770 	snd_soc_component_write(component, RT5663_INT_ST_1, 0);
1771 	snd_soc_component_write(component, RT5663_HP_LOGIC_1, 0);
1772 	snd_soc_component_update_bits(component, RT5663_RC_CLK, RT5663_DIG_25M_CLK_MASK,
1773 		RT5663_DIG_25M_CLK_DIS);
1774 	snd_soc_component_write(component, RT5663_GLB_CLK, reg80);
1775 	snd_soc_component_write(component, RT5663_RECMIX, reg10);
1776 	snd_soc_component_write(component, RT5663_DUMMY_2, 0x00a4);
1777 	snd_soc_component_write(component, RT5663_DUMMY_1, reg2fa);
1778 	snd_soc_component_write(component, RT5663_HP_CALIB_2, 0x00c8);
1779 	snd_soc_component_write(component, RT5663_STO1_HPF_ADJ1, 0xb320);
1780 	snd_soc_component_write(component, RT5663_ADDA_RST, 0xe400);
1781 	snd_soc_component_write(component, RT5663_CHOP_ADC, 0x2000);
1782 	snd_soc_component_write(component, RT5663_HP_OUT_EN, 0x0008);
1783 	snd_soc_component_update_bits(component, RT5663_PWR_ANLG_2,
1784 		RT5663_PWR_RECMIX1 | RT5663_PWR_RECMIX2, 0);
1785 	snd_soc_component_update_bits(component, RT5663_PWR_DIG_1,
1786 		RT5663_PWR_DAC_L1 | RT5663_PWR_DAC_R1 |
1787 		RT5663_PWR_LDO_DACREF_MASK | RT5663_PWR_ADC_L1 |
1788 		RT5663_PWR_ADC_R1, 0);
1789 	snd_soc_component_update_bits(component, RT5663_PWR_DIG_2,
1790 		RT5663_PWR_ADC_S1F | RT5663_PWR_DAC_S1F, 0);
1791 	snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0003, 0);
1792 	snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0030, 0);
1793 	snd_soc_component_write(component, RT5663_HP_LOGIC_2, 0);
1794 	snd_soc_component_write(component, RT5663_HP_CHARGE_PUMP_1, reg91);
1795 	snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
1796 		RT5663_PWR_VREF1_MASK | RT5663_PWR_VREF2_MASK, 0);
1797 	snd_soc_component_write(component, RT5663_STO1_ADC_MIXER, reg26);
1798 	snd_soc_component_write(component, RT5663_ASRC_2, reg84);
1799 
1800 	for (i = 0; i < rt5663->pdata.impedance_sensing_num; i++) {
1801 		if (value >= rt5663->imp_table[i].imp_min &&
1802 			value <= rt5663->imp_table[i].imp_max)
1803 			break;
1804 	}
1805 
1806 	snd_soc_component_update_bits(component, RT5663_STO_DRE_9, RT5663_DRE_GAIN_HP_MASK,
1807 		rt5663->imp_table[i].vol);
1808 	snd_soc_component_update_bits(component, RT5663_STO_DRE_10, RT5663_DRE_GAIN_HP_MASK,
1809 		rt5663->imp_table[i].vol);
1810 
1811 	if (rt5663->jack_type == SND_JACK_HEADSET) {
1812 		snd_soc_component_write(component, RT5663_MIC_DECRO_2,
1813 			rt5663->imp_table[i].dc_offset_l_manual_mic >> 16);
1814 		snd_soc_component_write(component, RT5663_MIC_DECRO_3,
1815 			rt5663->imp_table[i].dc_offset_l_manual_mic & 0xffff);
1816 		snd_soc_component_write(component, RT5663_MIC_DECRO_5,
1817 			rt5663->imp_table[i].dc_offset_r_manual_mic >> 16);
1818 		snd_soc_component_write(component, RT5663_MIC_DECRO_6,
1819 			rt5663->imp_table[i].dc_offset_r_manual_mic & 0xffff);
1820 	} else {
1821 		snd_soc_component_write(component, RT5663_MIC_DECRO_2,
1822 			rt5663->imp_table[i].dc_offset_l_manual >> 16);
1823 		snd_soc_component_write(component, RT5663_MIC_DECRO_3,
1824 			rt5663->imp_table[i].dc_offset_l_manual & 0xffff);
1825 		snd_soc_component_write(component, RT5663_MIC_DECRO_5,
1826 			rt5663->imp_table[i].dc_offset_r_manual >> 16);
1827 		snd_soc_component_write(component, RT5663_MIC_DECRO_6,
1828 			rt5663->imp_table[i].dc_offset_r_manual & 0xffff);
1829 	}
1830 
1831 	return 0;
1832 }
1833 
1834 static int rt5663_button_detect(struct snd_soc_component *component)
1835 {
1836 	int btn_type, val;
1837 
1838 	val = snd_soc_component_read32(component, RT5663_IL_CMD_5);
1839 	dev_dbg(component->dev, "%s: val=0x%x\n", __func__, val);
1840 	btn_type = val & 0xfff0;
1841 	snd_soc_component_write(component, RT5663_IL_CMD_5, val);
1842 
1843 	return btn_type;
1844 }
1845 
1846 static irqreturn_t rt5663_irq(int irq, void *data)
1847 {
1848 	struct rt5663_priv *rt5663 = data;
1849 
1850 	dev_dbg(regmap_get_device(rt5663->regmap), "%s IRQ queue work\n",
1851 		__func__);
1852 
1853 	queue_delayed_work(system_wq, &rt5663->jack_detect_work,
1854 		msecs_to_jiffies(250));
1855 
1856 	return IRQ_HANDLED;
1857 }
1858 
1859 int rt5663_set_jack_detect(struct snd_soc_component *component,
1860 	struct snd_soc_jack *hs_jack)
1861 {
1862 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
1863 
1864 	rt5663->hs_jack = hs_jack;
1865 
1866 	rt5663_irq(0, rt5663);
1867 
1868 	return 0;
1869 }
1870 EXPORT_SYMBOL_GPL(rt5663_set_jack_detect);
1871 
1872 static bool rt5663_check_jd_status(struct snd_soc_component *component)
1873 {
1874 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
1875 	int val = snd_soc_component_read32(component, RT5663_INT_ST_1);
1876 
1877 	dev_dbg(component->dev, "%s val=%x\n", __func__, val);
1878 
1879 	/* JD1 */
1880 	switch (rt5663->codec_ver) {
1881 	case CODEC_VER_1:
1882 		return !(val & 0x2000);
1883 	case CODEC_VER_0:
1884 		return !(val & 0x1000);
1885 	default:
1886 		dev_err(component->dev, "Unknown CODEC Version\n");
1887 	}
1888 
1889 	return false;
1890 }
1891 
1892 static void rt5663_jack_detect_work(struct work_struct *work)
1893 {
1894 	struct rt5663_priv *rt5663 =
1895 		container_of(work, struct rt5663_priv, jack_detect_work.work);
1896 	struct snd_soc_component *component = rt5663->component;
1897 	int btn_type, report = 0;
1898 
1899 	if (!component)
1900 		return;
1901 
1902 	if (rt5663_check_jd_status(component)) {
1903 		/* jack in */
1904 		if (rt5663->jack_type == 0) {
1905 			/* jack was out, report jack type */
1906 			switch (rt5663->codec_ver) {
1907 			case CODEC_VER_1:
1908 				report = rt5663_v2_jack_detect(
1909 						rt5663->component, 1);
1910 				break;
1911 			case CODEC_VER_0:
1912 				report = rt5663_jack_detect(rt5663->component, 1);
1913 				if (rt5663->pdata.impedance_sensing_num)
1914 					rt5663_impedance_sensing(rt5663->component);
1915 				break;
1916 			default:
1917 				dev_err(component->dev, "Unknown CODEC Version\n");
1918 			}
1919 
1920 			/* Delay the jack insert report to avoid pop noise */
1921 			msleep(30);
1922 		} else {
1923 			/* jack is already in, report button event */
1924 			report = SND_JACK_HEADSET;
1925 			btn_type = rt5663_button_detect(rt5663->component);
1926 			/**
1927 			 * rt5663 can report three kinds of button behavior,
1928 			 * one click, double click and hold. However,
1929 			 * currently we will report button pressed/released
1930 			 * event. So all the three button behaviors are
1931 			 * treated as button pressed.
1932 			 */
1933 			switch (btn_type) {
1934 			case 0x8000:
1935 			case 0x4000:
1936 			case 0x2000:
1937 				report |= SND_JACK_BTN_0;
1938 				break;
1939 			case 0x1000:
1940 			case 0x0800:
1941 			case 0x0400:
1942 				report |= SND_JACK_BTN_1;
1943 				break;
1944 			case 0x0200:
1945 			case 0x0100:
1946 			case 0x0080:
1947 				report |= SND_JACK_BTN_2;
1948 				break;
1949 			case 0x0040:
1950 			case 0x0020:
1951 			case 0x0010:
1952 				report |= SND_JACK_BTN_3;
1953 				break;
1954 			case 0x0000: /* unpressed */
1955 				break;
1956 			default:
1957 				btn_type = 0;
1958 				dev_err(rt5663->component->dev,
1959 					"Unexpected button code 0x%04x\n",
1960 					btn_type);
1961 				break;
1962 			}
1963 			/* button release or spurious interrput*/
1964 			if (btn_type == 0) {
1965 				report =  rt5663->jack_type;
1966 				cancel_delayed_work_sync(
1967 					&rt5663->jd_unplug_work);
1968 			} else {
1969 				queue_delayed_work(system_wq,
1970 					&rt5663->jd_unplug_work,
1971 					msecs_to_jiffies(500));
1972 			}
1973 		}
1974 	} else {
1975 		/* jack out */
1976 		switch (rt5663->codec_ver) {
1977 		case CODEC_VER_1:
1978 			report = rt5663_v2_jack_detect(rt5663->component, 0);
1979 			break;
1980 		case CODEC_VER_0:
1981 			report = rt5663_jack_detect(rt5663->component, 0);
1982 			break;
1983 		default:
1984 			dev_err(component->dev, "Unknown CODEC Version\n");
1985 		}
1986 	}
1987 	dev_dbg(component->dev, "%s jack report: 0x%04x\n", __func__, report);
1988 	snd_soc_jack_report(rt5663->hs_jack, report, SND_JACK_HEADSET |
1989 			    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
1990 			    SND_JACK_BTN_2 | SND_JACK_BTN_3);
1991 }
1992 
1993 static void rt5663_jd_unplug_work(struct work_struct *work)
1994 {
1995 	struct rt5663_priv *rt5663 =
1996 		container_of(work, struct rt5663_priv, jd_unplug_work.work);
1997 	struct snd_soc_component *component = rt5663->component;
1998 
1999 	if (!component)
2000 		return;
2001 
2002 	if (!rt5663_check_jd_status(component)) {
2003 		/* jack out */
2004 		switch (rt5663->codec_ver) {
2005 		case CODEC_VER_1:
2006 			rt5663_v2_jack_detect(rt5663->component, 0);
2007 			break;
2008 		case CODEC_VER_0:
2009 			rt5663_jack_detect(rt5663->component, 0);
2010 			break;
2011 		default:
2012 			dev_err(component->dev, "Unknown CODEC Version\n");
2013 		}
2014 
2015 		snd_soc_jack_report(rt5663->hs_jack, 0, SND_JACK_HEADSET |
2016 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
2017 				    SND_JACK_BTN_2 | SND_JACK_BTN_3);
2018 	} else {
2019 		queue_delayed_work(system_wq, &rt5663->jd_unplug_work,
2020 			msecs_to_jiffies(500));
2021 	}
2022 }
2023 
2024 static const struct snd_kcontrol_new rt5663_snd_controls[] = {
2025 	/* DAC Digital Volume */
2026 	SOC_DOUBLE_TLV("DAC Playback Volume", RT5663_STO1_DAC_DIG_VOL,
2027 		RT5663_DAC_L1_VOL_SHIFT + 1, RT5663_DAC_R1_VOL_SHIFT + 1,
2028 		87, 0, dac_vol_tlv),
2029 	/* ADC Digital Volume Control */
2030 	SOC_DOUBLE("ADC Capture Switch", RT5663_STO1_ADC_DIG_VOL,
2031 		RT5663_ADC_L_MUTE_SHIFT, RT5663_ADC_R_MUTE_SHIFT, 1, 1),
2032 	SOC_DOUBLE_TLV("ADC Capture Volume", RT5663_STO1_ADC_DIG_VOL,
2033 		RT5663_ADC_L_VOL_SHIFT + 1, RT5663_ADC_R_VOL_SHIFT + 1,
2034 		63, 0, adc_vol_tlv),
2035 };
2036 
2037 static const struct snd_kcontrol_new rt5663_v2_specific_controls[] = {
2038 	/* Headphone Output Volume */
2039 	SOC_DOUBLE_R_TLV("Headphone Playback Volume", RT5663_HP_LCH_DRE,
2040 		RT5663_HP_RCH_DRE, RT5663_GAIN_HP_SHIFT, 15, 1,
2041 		rt5663_v2_hp_vol_tlv),
2042 	/* Mic Boost Volume */
2043 	SOC_SINGLE_TLV("IN1 Capture Volume", RT5663_AEC_BST,
2044 		RT5663_GAIN_CBJ_SHIFT, 8, 0, in_bst_tlv),
2045 };
2046 
2047 static const struct snd_kcontrol_new rt5663_specific_controls[] = {
2048 	/* Mic Boost Volume*/
2049 	SOC_SINGLE_TLV("IN1 Capture Volume", RT5663_CBJ_2,
2050 		RT5663_GAIN_BST1_SHIFT, 8, 0, in_bst_tlv),
2051 	/* Data Swap for Slot0/1 in ADCDAT1 */
2052 	SOC_ENUM("IF1 ADC Data Swap", rt5663_if1_adc_enum),
2053 };
2054 
2055 static const struct snd_kcontrol_new rt5663_hpvol_controls[] = {
2056 	/* Headphone Output Volume */
2057 	SOC_DOUBLE_R_TLV("Headphone Playback Volume", RT5663_STO_DRE_9,
2058 		RT5663_STO_DRE_10, RT5663_DRE_GAIN_HP_SHIFT, 23, 1,
2059 		rt5663_hp_vol_tlv),
2060 };
2061 
2062 static int rt5663_is_sys_clk_from_pll(struct snd_soc_dapm_widget *w,
2063 	struct snd_soc_dapm_widget *sink)
2064 {
2065 	unsigned int val;
2066 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2067 
2068 	val = snd_soc_component_read32(component, RT5663_GLB_CLK);
2069 	val &= RT5663_SCLK_SRC_MASK;
2070 	if (val == RT5663_SCLK_SRC_PLL1)
2071 		return 1;
2072 	else
2073 		return 0;
2074 }
2075 
2076 static int rt5663_is_using_asrc(struct snd_soc_dapm_widget *w,
2077 	struct snd_soc_dapm_widget *sink)
2078 {
2079 	unsigned int reg, shift, val;
2080 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2081 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2082 
2083 	if (rt5663->codec_ver == CODEC_VER_1) {
2084 		switch (w->shift) {
2085 		case RT5663_ADC_STO1_ASRC_SHIFT:
2086 			reg = RT5663_ASRC_3;
2087 			shift = RT5663_V2_AD_STO1_TRACK_SHIFT;
2088 			break;
2089 		case RT5663_DAC_STO1_ASRC_SHIFT:
2090 			reg = RT5663_ASRC_2;
2091 			shift = RT5663_DA_STO1_TRACK_SHIFT;
2092 			break;
2093 		default:
2094 			return 0;
2095 		}
2096 	} else {
2097 		switch (w->shift) {
2098 		case RT5663_ADC_STO1_ASRC_SHIFT:
2099 			reg = RT5663_ASRC_2;
2100 			shift = RT5663_AD_STO1_TRACK_SHIFT;
2101 			break;
2102 		case RT5663_DAC_STO1_ASRC_SHIFT:
2103 			reg = RT5663_ASRC_2;
2104 			shift = RT5663_DA_STO1_TRACK_SHIFT;
2105 			break;
2106 		default:
2107 			return 0;
2108 		}
2109 	}
2110 
2111 	val = (snd_soc_component_read32(component, reg) >> shift) & 0x7;
2112 
2113 	if (val)
2114 		return 1;
2115 
2116 	return 0;
2117 }
2118 
2119 static int rt5663_i2s_use_asrc(struct snd_soc_dapm_widget *source,
2120 	struct snd_soc_dapm_widget *sink)
2121 {
2122 	struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
2123 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2124 	int da_asrc_en, ad_asrc_en;
2125 
2126 	da_asrc_en = (snd_soc_component_read32(component, RT5663_ASRC_2) &
2127 		RT5663_DA_STO1_TRACK_MASK) ? 1 : 0;
2128 	switch (rt5663->codec_ver) {
2129 	case CODEC_VER_1:
2130 		ad_asrc_en = (snd_soc_component_read32(component, RT5663_ASRC_3) &
2131 			RT5663_V2_AD_STO1_TRACK_MASK) ? 1 : 0;
2132 		break;
2133 	case CODEC_VER_0:
2134 		ad_asrc_en = (snd_soc_component_read32(component, RT5663_ASRC_2) &
2135 			RT5663_AD_STO1_TRACK_MASK) ? 1 : 0;
2136 		break;
2137 	default:
2138 		dev_err(component->dev, "Unknown CODEC Version\n");
2139 		return 1;
2140 	}
2141 
2142 	if (da_asrc_en || ad_asrc_en)
2143 		if (rt5663->sysclk > rt5663->lrck * 384)
2144 			return 1;
2145 
2146 	dev_err(component->dev, "sysclk < 384 x fs, disable i2s asrc\n");
2147 
2148 	return 0;
2149 }
2150 
2151 /**
2152  * rt5663_sel_asrc_clk_src - select ASRC clock source for a set of filters
2153  * @component: SoC audio component device.
2154  * @filter_mask: mask of filters.
2155  * @clk_src: clock source
2156  *
2157  * The ASRC function is for asynchronous MCLK and LRCK. Also, since RT5663 can
2158  * only support standard 32fs or 64fs i2s format, ASRC should be enabled to
2159  * support special i2s clock format such as Intel's 100fs(100 * sampling rate).
2160  * ASRC function will track i2s clock and generate a corresponding system clock
2161  * for codec. This function provides an API to select the clock source for a
2162  * set of filters specified by the mask. And the codec driver will turn on ASRC
2163  * for these filters if ASRC is selected as their clock source.
2164  */
2165 int rt5663_sel_asrc_clk_src(struct snd_soc_component *component,
2166 		unsigned int filter_mask, unsigned int clk_src)
2167 {
2168 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2169 	unsigned int asrc2_mask = 0;
2170 	unsigned int asrc2_value = 0;
2171 	unsigned int asrc3_mask = 0;
2172 	unsigned int asrc3_value = 0;
2173 
2174 	switch (clk_src) {
2175 	case RT5663_CLK_SEL_SYS:
2176 	case RT5663_CLK_SEL_I2S1_ASRC:
2177 		break;
2178 
2179 	default:
2180 		return -EINVAL;
2181 	}
2182 
2183 	if (filter_mask & RT5663_DA_STEREO_FILTER) {
2184 		asrc2_mask |= RT5663_DA_STO1_TRACK_MASK;
2185 		asrc2_value |= clk_src << RT5663_DA_STO1_TRACK_SHIFT;
2186 	}
2187 
2188 	if (filter_mask & RT5663_AD_STEREO_FILTER) {
2189 		switch (rt5663->codec_ver) {
2190 		case CODEC_VER_1:
2191 			asrc3_mask |= RT5663_V2_AD_STO1_TRACK_MASK;
2192 			asrc3_value |= clk_src << RT5663_V2_AD_STO1_TRACK_SHIFT;
2193 			break;
2194 		case CODEC_VER_0:
2195 			asrc2_mask |= RT5663_AD_STO1_TRACK_MASK;
2196 			asrc2_value |= clk_src << RT5663_AD_STO1_TRACK_SHIFT;
2197 			break;
2198 		default:
2199 			dev_err(component->dev, "Unknown CODEC Version\n");
2200 		}
2201 	}
2202 
2203 	if (asrc2_mask)
2204 		snd_soc_component_update_bits(component, RT5663_ASRC_2, asrc2_mask,
2205 			asrc2_value);
2206 
2207 	if (asrc3_mask)
2208 		snd_soc_component_update_bits(component, RT5663_ASRC_3, asrc3_mask,
2209 			asrc3_value);
2210 
2211 	return 0;
2212 }
2213 EXPORT_SYMBOL_GPL(rt5663_sel_asrc_clk_src);
2214 
2215 /* Analog Mixer */
2216 static const struct snd_kcontrol_new rt5663_recmix1l[] = {
2217 	SOC_DAPM_SINGLE("BST2 Switch", RT5663_RECMIX1L,
2218 		RT5663_RECMIX1L_BST2_SHIFT, 1, 1),
2219 	SOC_DAPM_SINGLE("BST1 CBJ Switch", RT5663_RECMIX1L,
2220 		RT5663_RECMIX1L_BST1_CBJ_SHIFT, 1, 1),
2221 };
2222 
2223 static const struct snd_kcontrol_new rt5663_recmix1r[] = {
2224 	SOC_DAPM_SINGLE("BST2 Switch", RT5663_RECMIX1R,
2225 		RT5663_RECMIX1R_BST2_SHIFT, 1, 1),
2226 };
2227 
2228 /* Digital Mixer */
2229 static const struct snd_kcontrol_new rt5663_sto1_adc_l_mix[] = {
2230 	SOC_DAPM_SINGLE("ADC1 Switch", RT5663_STO1_ADC_MIXER,
2231 			RT5663_M_STO1_ADC_L1_SHIFT, 1, 1),
2232 	SOC_DAPM_SINGLE("ADC2 Switch", RT5663_STO1_ADC_MIXER,
2233 			RT5663_M_STO1_ADC_L2_SHIFT, 1, 1),
2234 };
2235 
2236 static const struct snd_kcontrol_new rt5663_sto1_adc_r_mix[] = {
2237 	SOC_DAPM_SINGLE("ADC1 Switch", RT5663_STO1_ADC_MIXER,
2238 			RT5663_M_STO1_ADC_R1_SHIFT, 1, 1),
2239 	SOC_DAPM_SINGLE("ADC2 Switch", RT5663_STO1_ADC_MIXER,
2240 			RT5663_M_STO1_ADC_R2_SHIFT, 1, 1),
2241 };
2242 
2243 static const struct snd_kcontrol_new rt5663_adda_l_mix[] = {
2244 	SOC_DAPM_SINGLE("ADC L Switch", RT5663_AD_DA_MIXER,
2245 			RT5663_M_ADCMIX_L_SHIFT, 1, 1),
2246 	SOC_DAPM_SINGLE("DAC L Switch", RT5663_AD_DA_MIXER,
2247 			RT5663_M_DAC1_L_SHIFT, 1, 1),
2248 };
2249 
2250 static const struct snd_kcontrol_new rt5663_adda_r_mix[] = {
2251 	SOC_DAPM_SINGLE("ADC R Switch", RT5663_AD_DA_MIXER,
2252 			RT5663_M_ADCMIX_R_SHIFT, 1, 1),
2253 	SOC_DAPM_SINGLE("DAC R Switch", RT5663_AD_DA_MIXER,
2254 			RT5663_M_DAC1_R_SHIFT, 1, 1),
2255 };
2256 
2257 static const struct snd_kcontrol_new rt5663_sto1_dac_l_mix[] = {
2258 	SOC_DAPM_SINGLE("DAC L Switch", RT5663_STO_DAC_MIXER,
2259 			RT5663_M_DAC_L1_STO_L_SHIFT, 1, 1),
2260 };
2261 
2262 static const struct snd_kcontrol_new rt5663_sto1_dac_r_mix[] = {
2263 	SOC_DAPM_SINGLE("DAC R Switch", RT5663_STO_DAC_MIXER,
2264 			RT5663_M_DAC_R1_STO_R_SHIFT, 1, 1),
2265 };
2266 
2267 /* Out Switch */
2268 static const struct snd_kcontrol_new rt5663_hpo_switch =
2269 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5663_HP_AMP_2,
2270 		RT5663_EN_DAC_HPO_SHIFT, 1, 0);
2271 
2272 /* Stereo ADC source */
2273 static const char * const rt5663_sto1_adc_src[] = {
2274 	"ADC L", "ADC R"
2275 };
2276 
2277 static SOC_ENUM_SINGLE_DECL(rt5663_sto1_adcl_enum, RT5663_STO1_ADC_MIXER,
2278 	RT5663_STO1_ADC_L_SRC_SHIFT, rt5663_sto1_adc_src);
2279 
2280 static const struct snd_kcontrol_new rt5663_sto1_adcl_mux =
2281 	SOC_DAPM_ENUM("STO1 ADC L Mux", rt5663_sto1_adcl_enum);
2282 
2283 static SOC_ENUM_SINGLE_DECL(rt5663_sto1_adcr_enum, RT5663_STO1_ADC_MIXER,
2284 	RT5663_STO1_ADC_R_SRC_SHIFT, rt5663_sto1_adc_src);
2285 
2286 static const struct snd_kcontrol_new rt5663_sto1_adcr_mux =
2287 	SOC_DAPM_ENUM("STO1 ADC R Mux", rt5663_sto1_adcr_enum);
2288 
2289 /* RT5663: Analog DACL1 input source */
2290 static const char * const rt5663_alg_dacl_src[] = {
2291 	"DAC L", "STO DAC MIXL"
2292 };
2293 
2294 static SOC_ENUM_SINGLE_DECL(rt5663_alg_dacl_enum, RT5663_BYPASS_STO_DAC,
2295 	RT5663_DACL1_SRC_SHIFT, rt5663_alg_dacl_src);
2296 
2297 static const struct snd_kcontrol_new rt5663_alg_dacl_mux =
2298 	SOC_DAPM_ENUM("DAC L Mux", rt5663_alg_dacl_enum);
2299 
2300 /* RT5663: Analog DACR1 input source */
2301 static const char * const rt5663_alg_dacr_src[] = {
2302 	"DAC R", "STO DAC MIXR"
2303 };
2304 
2305 static SOC_ENUM_SINGLE_DECL(rt5663_alg_dacr_enum, RT5663_BYPASS_STO_DAC,
2306 	RT5663_DACR1_SRC_SHIFT, rt5663_alg_dacr_src);
2307 
2308 static const struct snd_kcontrol_new rt5663_alg_dacr_mux =
2309 	SOC_DAPM_ENUM("DAC R Mux", rt5663_alg_dacr_enum);
2310 
2311 static int rt5663_hp_event(struct snd_soc_dapm_widget *w,
2312 	struct snd_kcontrol *kcontrol, int event)
2313 {
2314 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2315 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2316 
2317 	switch (event) {
2318 	case SND_SOC_DAPM_POST_PMU:
2319 		if (rt5663->codec_ver == CODEC_VER_1) {
2320 			snd_soc_component_update_bits(component, RT5663_HP_CHARGE_PUMP_1,
2321 				RT5663_SEL_PM_HP_SHIFT, RT5663_SEL_PM_HP_HIGH);
2322 			snd_soc_component_update_bits(component, RT5663_HP_LOGIC_2,
2323 				RT5663_HP_SIG_SRC1_MASK,
2324 				RT5663_HP_SIG_SRC1_SILENCE);
2325 		} else {
2326 			snd_soc_component_update_bits(component,
2327 				RT5663_DACREF_LDO, 0x3e0e, 0x3a0a);
2328 			snd_soc_component_write(component, RT5663_DEPOP_2, 0x3003);
2329 			snd_soc_component_update_bits(component, RT5663_HP_CHARGE_PUMP_1,
2330 				RT5663_OVCD_HP_MASK, RT5663_OVCD_HP_DIS);
2331 			snd_soc_component_write(component, RT5663_HP_CHARGE_PUMP_2, 0x1371);
2332 			snd_soc_component_write(component, RT5663_HP_BIAS, 0xabba);
2333 			snd_soc_component_write(component, RT5663_CHARGE_PUMP_1, 0x2224);
2334 			snd_soc_component_write(component, RT5663_ANA_BIAS_CUR_1, 0x7766);
2335 			snd_soc_component_write(component, RT5663_HP_BIAS, 0xafaa);
2336 			snd_soc_component_write(component, RT5663_CHARGE_PUMP_2, 0x7777);
2337 			snd_soc_component_update_bits(component, RT5663_STO_DRE_1, 0x8000,
2338 				0x8000);
2339 			snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x3000,
2340 				0x3000);
2341 		}
2342 		break;
2343 
2344 	case SND_SOC_DAPM_PRE_PMD:
2345 		if (rt5663->codec_ver == CODEC_VER_1) {
2346 			snd_soc_component_update_bits(component, RT5663_HP_LOGIC_2,
2347 				RT5663_HP_SIG_SRC1_MASK,
2348 				RT5663_HP_SIG_SRC1_REG);
2349 		} else {
2350 			snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x3000, 0x0);
2351 			snd_soc_component_update_bits(component, RT5663_HP_CHARGE_PUMP_1,
2352 				RT5663_OVCD_HP_MASK, RT5663_OVCD_HP_EN);
2353 			snd_soc_component_update_bits(component,
2354 				RT5663_DACREF_LDO, 0x3e0e, 0);
2355 		}
2356 		break;
2357 
2358 	default:
2359 		return 0;
2360 	}
2361 
2362 	return 0;
2363 }
2364 
2365 static int rt5663_charge_pump_event(struct snd_soc_dapm_widget *w,
2366 	struct snd_kcontrol *kcontrol, int event)
2367 {
2368 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2369 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2370 
2371 	switch (event) {
2372 	case SND_SOC_DAPM_PRE_PMU:
2373 		if (rt5663->codec_ver == CODEC_VER_0) {
2374 			snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0030,
2375 				0x0030);
2376 			snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0003,
2377 				0x0003);
2378 		}
2379 		break;
2380 
2381 	case SND_SOC_DAPM_POST_PMD:
2382 		if (rt5663->codec_ver == CODEC_VER_0) {
2383 			snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0003, 0);
2384 			snd_soc_component_update_bits(component, RT5663_DEPOP_1, 0x0030, 0);
2385 		}
2386 		break;
2387 
2388 	default:
2389 		return 0;
2390 	}
2391 
2392 	return 0;
2393 }
2394 
2395 static int rt5663_bst2_power(struct snd_soc_dapm_widget *w,
2396 	struct snd_kcontrol *kcontrol, int event)
2397 {
2398 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2399 
2400 	switch (event) {
2401 	case SND_SOC_DAPM_POST_PMU:
2402 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_2,
2403 			RT5663_PWR_BST2_MASK | RT5663_PWR_BST2_OP_MASK,
2404 			RT5663_PWR_BST2 | RT5663_PWR_BST2_OP);
2405 		break;
2406 
2407 	case SND_SOC_DAPM_PRE_PMD:
2408 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_2,
2409 			RT5663_PWR_BST2_MASK | RT5663_PWR_BST2_OP_MASK, 0);
2410 		break;
2411 
2412 	default:
2413 		return 0;
2414 	}
2415 
2416 	return 0;
2417 }
2418 
2419 static int rt5663_pre_div_power(struct snd_soc_dapm_widget *w,
2420 	struct snd_kcontrol *kcontrol, int event)
2421 {
2422 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
2423 
2424 	switch (event) {
2425 	case SND_SOC_DAPM_POST_PMU:
2426 		snd_soc_component_write(component, RT5663_PRE_DIV_GATING_1, 0xff00);
2427 		snd_soc_component_write(component, RT5663_PRE_DIV_GATING_2, 0xfffc);
2428 		break;
2429 
2430 	case SND_SOC_DAPM_PRE_PMD:
2431 		snd_soc_component_write(component, RT5663_PRE_DIV_GATING_1, 0x0000);
2432 		snd_soc_component_write(component, RT5663_PRE_DIV_GATING_2, 0x0000);
2433 		break;
2434 
2435 	default:
2436 		return 0;
2437 	}
2438 
2439 	return 0;
2440 }
2441 
2442 static const struct snd_soc_dapm_widget rt5663_dapm_widgets[] = {
2443 	SND_SOC_DAPM_SUPPLY("PLL", RT5663_PWR_ANLG_3, RT5663_PWR_PLL_SHIFT, 0,
2444 		NULL, 0),
2445 
2446 	/* micbias */
2447 	SND_SOC_DAPM_MICBIAS("MICBIAS1", RT5663_PWR_ANLG_2,
2448 		RT5663_PWR_MB1_SHIFT, 0),
2449 	SND_SOC_DAPM_MICBIAS("MICBIAS2", RT5663_PWR_ANLG_2,
2450 		RT5663_PWR_MB2_SHIFT, 0),
2451 
2452 	/* Input Lines */
2453 	SND_SOC_DAPM_INPUT("IN1P"),
2454 	SND_SOC_DAPM_INPUT("IN1N"),
2455 
2456 	/* REC Mixer Power */
2457 	SND_SOC_DAPM_SUPPLY("RECMIX1L Power", RT5663_PWR_ANLG_2,
2458 		RT5663_PWR_RECMIX1_SHIFT, 0, NULL, 0),
2459 
2460 	/* ADCs */
2461 	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
2462 	SND_SOC_DAPM_SUPPLY("ADC L Power", RT5663_PWR_DIG_1,
2463 		RT5663_PWR_ADC_L1_SHIFT, 0, NULL, 0),
2464 	SND_SOC_DAPM_SUPPLY("ADC Clock", RT5663_CHOP_ADC,
2465 		RT5663_CKGEN_ADCC_SHIFT, 0, NULL, 0),
2466 
2467 	/* ADC Mixer */
2468 	SND_SOC_DAPM_MIXER("STO1 ADC MIXL", SND_SOC_NOPM,
2469 		0, 0, rt5663_sto1_adc_l_mix,
2470 		ARRAY_SIZE(rt5663_sto1_adc_l_mix)),
2471 
2472 	/* ADC Filter Power */
2473 	SND_SOC_DAPM_SUPPLY("STO1 ADC Filter", RT5663_PWR_DIG_2,
2474 		RT5663_PWR_ADC_S1F_SHIFT, 0, NULL, 0),
2475 
2476 	/* Digital Interface */
2477 	SND_SOC_DAPM_SUPPLY("I2S", RT5663_PWR_DIG_1, RT5663_PWR_I2S1_SHIFT, 0,
2478 		NULL, 0),
2479 	SND_SOC_DAPM_PGA("IF DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
2480 	SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0),
2481 	SND_SOC_DAPM_PGA("IF1 DAC1 R", SND_SOC_NOPM, 0, 0, NULL, 0),
2482 	SND_SOC_DAPM_PGA("IF1 ADC1", SND_SOC_NOPM, 0, 0, NULL, 0),
2483 	SND_SOC_DAPM_PGA("IF ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
2484 
2485 	/* Audio Interface */
2486 	SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM, 0, 0),
2487 	SND_SOC_DAPM_AIF_OUT("AIFTX", "AIF Capture", 0, SND_SOC_NOPM, 0, 0),
2488 
2489 	/* DAC mixer before sound effect  */
2490 	SND_SOC_DAPM_MIXER("ADDA MIXL", SND_SOC_NOPM, 0, 0, rt5663_adda_l_mix,
2491 		ARRAY_SIZE(rt5663_adda_l_mix)),
2492 	SND_SOC_DAPM_MIXER("ADDA MIXR", SND_SOC_NOPM, 0, 0, rt5663_adda_r_mix,
2493 		ARRAY_SIZE(rt5663_adda_r_mix)),
2494 	SND_SOC_DAPM_PGA("DAC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
2495 	SND_SOC_DAPM_PGA("DAC R1", SND_SOC_NOPM, 0, 0, NULL, 0),
2496 
2497 	/* DAC Mixer */
2498 	SND_SOC_DAPM_SUPPLY("STO1 DAC Filter", RT5663_PWR_DIG_2,
2499 		RT5663_PWR_DAC_S1F_SHIFT, 0, NULL, 0),
2500 	SND_SOC_DAPM_MIXER("STO1 DAC MIXL", SND_SOC_NOPM, 0, 0,
2501 		rt5663_sto1_dac_l_mix, ARRAY_SIZE(rt5663_sto1_dac_l_mix)),
2502 	SND_SOC_DAPM_MIXER("STO1 DAC MIXR", SND_SOC_NOPM, 0, 0,
2503 		rt5663_sto1_dac_r_mix, ARRAY_SIZE(rt5663_sto1_dac_r_mix)),
2504 
2505 	/* DACs */
2506 	SND_SOC_DAPM_SUPPLY("STO1 DAC L Power", RT5663_PWR_DIG_1,
2507 		RT5663_PWR_DAC_L1_SHIFT, 0, NULL, 0),
2508 	SND_SOC_DAPM_SUPPLY("STO1 DAC R Power", RT5663_PWR_DIG_1,
2509 		RT5663_PWR_DAC_R1_SHIFT, 0, NULL, 0),
2510 	SND_SOC_DAPM_DAC("DAC L", NULL, SND_SOC_NOPM, 0, 0),
2511 	SND_SOC_DAPM_DAC("DAC R", NULL, SND_SOC_NOPM, 0, 0),
2512 
2513 	/* Headphone*/
2514 	SND_SOC_DAPM_SUPPLY("HP Charge Pump", SND_SOC_NOPM, 0, 0,
2515 		rt5663_charge_pump_event, SND_SOC_DAPM_PRE_PMU |
2516 		SND_SOC_DAPM_POST_PMD),
2517 	SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0, rt5663_hp_event,
2518 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
2519 
2520 	/* Output Lines */
2521 	SND_SOC_DAPM_OUTPUT("HPOL"),
2522 	SND_SOC_DAPM_OUTPUT("HPOR"),
2523 };
2524 
2525 static const struct snd_soc_dapm_widget rt5663_v2_specific_dapm_widgets[] = {
2526 	SND_SOC_DAPM_SUPPLY("LDO2", RT5663_PWR_ANLG_3,
2527 		RT5663_PWR_LDO2_SHIFT, 0, NULL, 0),
2528 	SND_SOC_DAPM_SUPPLY("Mic Det Power", RT5663_PWR_VOL,
2529 		RT5663_V2_PWR_MIC_DET_SHIFT, 0, NULL, 0),
2530 	SND_SOC_DAPM_SUPPLY("LDO DAC", RT5663_PWR_DIG_1,
2531 		RT5663_PWR_LDO_DACREF_SHIFT, 0, NULL, 0),
2532 
2533 	/* ASRC */
2534 	SND_SOC_DAPM_SUPPLY("I2S ASRC", RT5663_ASRC_1,
2535 		RT5663_I2S1_ASRC_SHIFT, 0, NULL, 0),
2536 	SND_SOC_DAPM_SUPPLY("DAC ASRC", RT5663_ASRC_1,
2537 		RT5663_DAC_STO1_ASRC_SHIFT, 0, NULL, 0),
2538 	SND_SOC_DAPM_SUPPLY("ADC ASRC", RT5663_ASRC_1,
2539 		RT5663_ADC_STO1_ASRC_SHIFT, 0, NULL, 0),
2540 
2541 	/* Input Lines */
2542 	SND_SOC_DAPM_INPUT("IN2P"),
2543 	SND_SOC_DAPM_INPUT("IN2N"),
2544 
2545 	/* Boost */
2546 	SND_SOC_DAPM_PGA("BST1 CBJ", SND_SOC_NOPM, 0, 0, NULL, 0),
2547 	SND_SOC_DAPM_SUPPLY("CBJ Power", RT5663_PWR_ANLG_3,
2548 		RT5663_PWR_CBJ_SHIFT, 0, NULL, 0),
2549 	SND_SOC_DAPM_PGA("BST2", SND_SOC_NOPM, 0, 0, NULL, 0),
2550 	SND_SOC_DAPM_SUPPLY("BST2 Power", SND_SOC_NOPM, 0, 0,
2551 		rt5663_bst2_power, SND_SOC_DAPM_PRE_PMD |
2552 		SND_SOC_DAPM_POST_PMU),
2553 
2554 	/* REC Mixer */
2555 	SND_SOC_DAPM_MIXER("RECMIX1L", SND_SOC_NOPM, 0, 0, rt5663_recmix1l,
2556 		ARRAY_SIZE(rt5663_recmix1l)),
2557 	SND_SOC_DAPM_MIXER("RECMIX1R", SND_SOC_NOPM, 0, 0, rt5663_recmix1r,
2558 		ARRAY_SIZE(rt5663_recmix1r)),
2559 	SND_SOC_DAPM_SUPPLY("RECMIX1R Power", RT5663_PWR_ANLG_2,
2560 		RT5663_PWR_RECMIX2_SHIFT, 0, NULL, 0),
2561 
2562 	/* ADC */
2563 	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
2564 	SND_SOC_DAPM_SUPPLY("ADC R Power", RT5663_PWR_DIG_1,
2565 		RT5663_PWR_ADC_R1_SHIFT, 0, NULL, 0),
2566 
2567 	/* ADC Mux */
2568 	SND_SOC_DAPM_PGA("STO1 ADC L1", RT5663_STO1_ADC_MIXER,
2569 		RT5663_STO1_ADC_L1_SRC_SHIFT, 0, NULL, 0),
2570 	SND_SOC_DAPM_PGA("STO1 ADC R1", RT5663_STO1_ADC_MIXER,
2571 		RT5663_STO1_ADC_R1_SRC_SHIFT, 0, NULL, 0),
2572 	SND_SOC_DAPM_PGA("STO1 ADC L2", RT5663_STO1_ADC_MIXER,
2573 		RT5663_STO1_ADC_L2_SRC_SHIFT, 1, NULL, 0),
2574 	SND_SOC_DAPM_PGA("STO1 ADC R2", RT5663_STO1_ADC_MIXER,
2575 		RT5663_STO1_ADC_R2_SRC_SHIFT, 1, NULL, 0),
2576 
2577 	SND_SOC_DAPM_MUX("STO1 ADC L Mux", SND_SOC_NOPM, 0, 0,
2578 		&rt5663_sto1_adcl_mux),
2579 	SND_SOC_DAPM_MUX("STO1 ADC R Mux", SND_SOC_NOPM, 0, 0,
2580 		&rt5663_sto1_adcr_mux),
2581 
2582 	/* ADC Mix */
2583 	SND_SOC_DAPM_MIXER("STO1 ADC MIXR", SND_SOC_NOPM, 0, 0,
2584 		rt5663_sto1_adc_r_mix, ARRAY_SIZE(rt5663_sto1_adc_r_mix)),
2585 
2586 	/* Analog DAC Clock */
2587 	SND_SOC_DAPM_SUPPLY("DAC Clock", RT5663_CHOP_DAC_L,
2588 		RT5663_CKGEN_DAC1_SHIFT, 0, NULL, 0),
2589 
2590 	/* Headphone out */
2591 	SND_SOC_DAPM_SWITCH("HPO Playback", SND_SOC_NOPM, 0, 0,
2592 		&rt5663_hpo_switch),
2593 };
2594 
2595 static const struct snd_soc_dapm_widget rt5663_specific_dapm_widgets[] = {
2596 	/* System Clock Pre Divider Gating */
2597 	SND_SOC_DAPM_SUPPLY("Pre Div Power", SND_SOC_NOPM, 0, 0,
2598 		rt5663_pre_div_power, SND_SOC_DAPM_POST_PMU |
2599 		SND_SOC_DAPM_PRE_PMD),
2600 
2601 	/* LDO */
2602 	SND_SOC_DAPM_SUPPLY("LDO ADC", RT5663_PWR_DIG_1,
2603 		RT5663_PWR_LDO_DACREF_SHIFT, 0, NULL, 0),
2604 
2605 	/* ASRC */
2606 	SND_SOC_DAPM_SUPPLY("I2S ASRC", RT5663_ASRC_1,
2607 		RT5663_I2S1_ASRC_SHIFT, 0, NULL, 0),
2608 	SND_SOC_DAPM_SUPPLY("DAC ASRC", RT5663_ASRC_1,
2609 		RT5663_DAC_STO1_ASRC_SHIFT, 0, NULL, 0),
2610 	SND_SOC_DAPM_SUPPLY("ADC ASRC", RT5663_ASRC_1,
2611 		RT5663_ADC_STO1_ASRC_SHIFT, 0, NULL, 0),
2612 
2613 	/* Boost */
2614 	SND_SOC_DAPM_PGA("BST1", SND_SOC_NOPM, 0, 0, NULL, 0),
2615 
2616 	/* STO ADC */
2617 	SND_SOC_DAPM_PGA("STO1 ADC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
2618 	SND_SOC_DAPM_PGA("STO1 ADC L2", SND_SOC_NOPM, 0, 0, NULL, 0),
2619 
2620 	/* Analog DAC source */
2621 	SND_SOC_DAPM_MUX("DAC L Mux", SND_SOC_NOPM, 0, 0, &rt5663_alg_dacl_mux),
2622 	SND_SOC_DAPM_MUX("DAC R Mux", SND_SOC_NOPM, 0, 0, &rt5663_alg_dacr_mux),
2623 };
2624 
2625 static const struct snd_soc_dapm_route rt5663_dapm_routes[] = {
2626 	/* PLL */
2627 	{ "I2S", NULL, "PLL", rt5663_is_sys_clk_from_pll },
2628 
2629 	/* ASRC */
2630 	{ "STO1 ADC Filter", NULL, "ADC ASRC", rt5663_is_using_asrc },
2631 	{ "STO1 DAC Filter", NULL, "DAC ASRC", rt5663_is_using_asrc },
2632 	{ "I2S", NULL, "I2S ASRC", rt5663_i2s_use_asrc },
2633 
2634 	{ "ADC L", NULL, "ADC L Power" },
2635 	{ "ADC L", NULL, "ADC Clock" },
2636 
2637 	{ "STO1 ADC L2", NULL, "STO1 DAC MIXL" },
2638 
2639 	{ "STO1 ADC MIXL", "ADC1 Switch", "STO1 ADC L1" },
2640 	{ "STO1 ADC MIXL", "ADC2 Switch", "STO1 ADC L2" },
2641 	{ "STO1 ADC MIXL", NULL, "STO1 ADC Filter" },
2642 
2643 	{ "IF1 ADC1", NULL, "STO1 ADC MIXL" },
2644 	{ "IF ADC", NULL, "IF1 ADC1" },
2645 	{ "AIFTX", NULL, "IF ADC" },
2646 	{ "AIFTX", NULL, "I2S" },
2647 
2648 	{ "AIFRX", NULL, "I2S" },
2649 	{ "IF DAC", NULL, "AIFRX" },
2650 	{ "IF1 DAC1 L", NULL, "IF DAC" },
2651 	{ "IF1 DAC1 R", NULL, "IF DAC" },
2652 
2653 	{ "ADDA MIXL", "ADC L Switch", "STO1 ADC MIXL" },
2654 	{ "ADDA MIXL", "DAC L Switch", "IF1 DAC1 L" },
2655 	{ "ADDA MIXL", NULL, "STO1 DAC Filter" },
2656 	{ "ADDA MIXL", NULL, "STO1 DAC L Power" },
2657 	{ "ADDA MIXR", "DAC R Switch", "IF1 DAC1 R" },
2658 	{ "ADDA MIXR", NULL, "STO1 DAC Filter" },
2659 	{ "ADDA MIXR", NULL, "STO1 DAC R Power" },
2660 
2661 	{ "DAC L1", NULL, "ADDA MIXL" },
2662 	{ "DAC R1", NULL, "ADDA MIXR" },
2663 
2664 	{ "STO1 DAC MIXL", "DAC L Switch", "DAC L1" },
2665 	{ "STO1 DAC MIXL", NULL, "STO1 DAC L Power" },
2666 	{ "STO1 DAC MIXL", NULL, "STO1 DAC Filter" },
2667 	{ "STO1 DAC MIXR", "DAC R Switch", "DAC R1" },
2668 	{ "STO1 DAC MIXR", NULL, "STO1 DAC R Power" },
2669 	{ "STO1 DAC MIXR", NULL, "STO1 DAC Filter" },
2670 
2671 	{ "HP Amp", NULL, "HP Charge Pump" },
2672 	{ "HP Amp", NULL, "DAC L" },
2673 	{ "HP Amp", NULL, "DAC R" },
2674 };
2675 
2676 static const struct snd_soc_dapm_route rt5663_v2_specific_dapm_routes[] = {
2677 	{ "MICBIAS1", NULL, "LDO2" },
2678 	{ "MICBIAS2", NULL, "LDO2" },
2679 
2680 	{ "BST1 CBJ", NULL, "IN1P" },
2681 	{ "BST1 CBJ", NULL, "IN1N" },
2682 	{ "BST1 CBJ", NULL, "CBJ Power" },
2683 
2684 	{ "BST2", NULL, "IN2P" },
2685 	{ "BST2", NULL, "IN2N" },
2686 	{ "BST2", NULL, "BST2 Power" },
2687 
2688 	{ "RECMIX1L", "BST2 Switch", "BST2" },
2689 	{ "RECMIX1L", "BST1 CBJ Switch", "BST1 CBJ" },
2690 	{ "RECMIX1L", NULL, "RECMIX1L Power" },
2691 	{ "RECMIX1R", "BST2 Switch", "BST2" },
2692 	{ "RECMIX1R", NULL, "RECMIX1R Power" },
2693 
2694 	{ "ADC L", NULL, "RECMIX1L" },
2695 	{ "ADC R", NULL, "RECMIX1R" },
2696 	{ "ADC R", NULL, "ADC R Power" },
2697 	{ "ADC R", NULL, "ADC Clock" },
2698 
2699 	{ "STO1 ADC L Mux", "ADC L", "ADC L" },
2700 	{ "STO1 ADC L Mux", "ADC R", "ADC R" },
2701 	{ "STO1 ADC L1", NULL, "STO1 ADC L Mux" },
2702 
2703 	{ "STO1 ADC R Mux", "ADC L", "ADC L" },
2704 	{ "STO1 ADC R Mux", "ADC R", "ADC R" },
2705 	{ "STO1 ADC R1", NULL, "STO1 ADC R Mux" },
2706 	{ "STO1 ADC R2", NULL, "STO1 DAC MIXR" },
2707 
2708 	{ "STO1 ADC MIXR", "ADC1 Switch", "STO1 ADC R1" },
2709 	{ "STO1 ADC MIXR", "ADC2 Switch", "STO1 ADC R2" },
2710 	{ "STO1 ADC MIXR", NULL, "STO1 ADC Filter" },
2711 
2712 	{ "IF1 ADC1", NULL, "STO1 ADC MIXR" },
2713 
2714 	{ "ADDA MIXR", "ADC R Switch", "STO1 ADC MIXR" },
2715 
2716 	{ "DAC L", NULL, "STO1 DAC MIXL" },
2717 	{ "DAC L", NULL, "LDO DAC" },
2718 	{ "DAC L", NULL, "DAC Clock" },
2719 	{ "DAC R", NULL, "STO1 DAC MIXR" },
2720 	{ "DAC R", NULL, "LDO DAC" },
2721 	{ "DAC R", NULL, "DAC Clock" },
2722 
2723 	{ "HPO Playback", "Switch", "HP Amp" },
2724 	{ "HPOL", NULL, "HPO Playback" },
2725 	{ "HPOR", NULL, "HPO Playback" },
2726 };
2727 
2728 static const struct snd_soc_dapm_route rt5663_specific_dapm_routes[] = {
2729 	{ "I2S", NULL, "Pre Div Power" },
2730 
2731 	{ "BST1", NULL, "IN1P" },
2732 	{ "BST1", NULL, "IN1N" },
2733 	{ "BST1", NULL, "RECMIX1L Power" },
2734 
2735 	{ "ADC L", NULL, "BST1" },
2736 
2737 	{ "STO1 ADC L1", NULL, "ADC L" },
2738 
2739 	{ "DAC L Mux", "DAC L",  "DAC L1" },
2740 	{ "DAC L Mux", "STO DAC MIXL", "STO1 DAC MIXL" },
2741 	{ "DAC R Mux", "DAC R",  "DAC R1"},
2742 	{ "DAC R Mux", "STO DAC MIXR", "STO1 DAC MIXR" },
2743 
2744 	{ "DAC L", NULL, "DAC L Mux" },
2745 	{ "DAC R", NULL, "DAC R Mux" },
2746 
2747 	{ "HPOL", NULL, "HP Amp" },
2748 	{ "HPOR", NULL, "HP Amp" },
2749 };
2750 
2751 static int rt5663_hw_params(struct snd_pcm_substream *substream,
2752 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
2753 {
2754 	struct snd_soc_component *component = dai->component;
2755 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2756 	unsigned int val_len = 0;
2757 	int pre_div;
2758 
2759 	rt5663->lrck = params_rate(params);
2760 
2761 	dev_dbg(dai->dev, "bclk is %dHz and sysclk is %dHz\n",
2762 		rt5663->lrck, rt5663->sysclk);
2763 
2764 	pre_div = rl6231_get_clk_info(rt5663->sysclk, rt5663->lrck);
2765 	if (pre_div < 0) {
2766 		dev_err(component->dev, "Unsupported clock setting %d for DAI %d\n",
2767 			rt5663->lrck, dai->id);
2768 		return -EINVAL;
2769 	}
2770 
2771 	dev_dbg(dai->dev, "pre_div is %d for iis %d\n", pre_div, dai->id);
2772 
2773 	switch (params_width(params)) {
2774 	case 8:
2775 		val_len = RT5663_I2S_DL_8;
2776 		break;
2777 	case 16:
2778 		val_len = RT5663_I2S_DL_16;
2779 		break;
2780 	case 20:
2781 		val_len = RT5663_I2S_DL_20;
2782 		break;
2783 	case 24:
2784 		val_len = RT5663_I2S_DL_24;
2785 		break;
2786 	default:
2787 		return -EINVAL;
2788 	}
2789 
2790 	snd_soc_component_update_bits(component, RT5663_I2S1_SDP,
2791 		RT5663_I2S_DL_MASK, val_len);
2792 
2793 	snd_soc_component_update_bits(component, RT5663_ADDA_CLK_1,
2794 		RT5663_I2S_PD1_MASK, pre_div << RT5663_I2S_PD1_SHIFT);
2795 
2796 	return 0;
2797 }
2798 
2799 static int rt5663_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2800 {
2801 	struct snd_soc_component *component = dai->component;
2802 	unsigned int reg_val = 0;
2803 
2804 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2805 	case SND_SOC_DAIFMT_CBM_CFM:
2806 		break;
2807 	case SND_SOC_DAIFMT_CBS_CFS:
2808 		reg_val |= RT5663_I2S_MS_S;
2809 		break;
2810 	default:
2811 		return -EINVAL;
2812 	}
2813 
2814 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2815 	case SND_SOC_DAIFMT_NB_NF:
2816 		break;
2817 	case SND_SOC_DAIFMT_IB_NF:
2818 		reg_val |= RT5663_I2S_BP_INV;
2819 		break;
2820 	default:
2821 		return -EINVAL;
2822 	}
2823 
2824 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2825 	case SND_SOC_DAIFMT_I2S:
2826 		break;
2827 	case SND_SOC_DAIFMT_LEFT_J:
2828 		reg_val |= RT5663_I2S_DF_LEFT;
2829 		break;
2830 	case SND_SOC_DAIFMT_DSP_A:
2831 		reg_val |= RT5663_I2S_DF_PCM_A;
2832 		break;
2833 	case SND_SOC_DAIFMT_DSP_B:
2834 		reg_val |= RT5663_I2S_DF_PCM_B;
2835 		break;
2836 	default:
2837 		return -EINVAL;
2838 	}
2839 
2840 	snd_soc_component_update_bits(component, RT5663_I2S1_SDP, RT5663_I2S_MS_MASK |
2841 		RT5663_I2S_BP_MASK | RT5663_I2S_DF_MASK, reg_val);
2842 
2843 	return 0;
2844 }
2845 
2846 static int rt5663_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
2847 	unsigned int freq, int dir)
2848 {
2849 	struct snd_soc_component *component = dai->component;
2850 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2851 	unsigned int reg_val = 0;
2852 
2853 	if (freq == rt5663->sysclk && clk_id == rt5663->sysclk_src)
2854 		return 0;
2855 
2856 	switch (clk_id) {
2857 	case RT5663_SCLK_S_MCLK:
2858 		reg_val |= RT5663_SCLK_SRC_MCLK;
2859 		break;
2860 	case RT5663_SCLK_S_PLL1:
2861 		reg_val |= RT5663_SCLK_SRC_PLL1;
2862 		break;
2863 	case RT5663_SCLK_S_RCCLK:
2864 		reg_val |= RT5663_SCLK_SRC_RCCLK;
2865 		break;
2866 	default:
2867 		dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
2868 		return -EINVAL;
2869 	}
2870 	snd_soc_component_update_bits(component, RT5663_GLB_CLK, RT5663_SCLK_SRC_MASK,
2871 		reg_val);
2872 	rt5663->sysclk = freq;
2873 	rt5663->sysclk_src = clk_id;
2874 
2875 	dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n",
2876 		freq, clk_id);
2877 
2878 	return 0;
2879 }
2880 
2881 static int rt5663_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
2882 			unsigned int freq_in, unsigned int freq_out)
2883 {
2884 	struct snd_soc_component *component = dai->component;
2885 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2886 	struct rl6231_pll_code pll_code;
2887 	int ret;
2888 	int mask, shift, val;
2889 
2890 	if (source == rt5663->pll_src && freq_in == rt5663->pll_in &&
2891 	    freq_out == rt5663->pll_out)
2892 		return 0;
2893 
2894 	if (!freq_in || !freq_out) {
2895 		dev_dbg(component->dev, "PLL disabled\n");
2896 
2897 		rt5663->pll_in = 0;
2898 		rt5663->pll_out = 0;
2899 		snd_soc_component_update_bits(component, RT5663_GLB_CLK,
2900 			RT5663_SCLK_SRC_MASK, RT5663_SCLK_SRC_MCLK);
2901 		return 0;
2902 	}
2903 
2904 	switch (rt5663->codec_ver) {
2905 	case CODEC_VER_1:
2906 		mask = RT5663_V2_PLL1_SRC_MASK;
2907 		shift = RT5663_V2_PLL1_SRC_SHIFT;
2908 		break;
2909 	case CODEC_VER_0:
2910 		mask = RT5663_PLL1_SRC_MASK;
2911 		shift = RT5663_PLL1_SRC_SHIFT;
2912 		break;
2913 	default:
2914 		dev_err(component->dev, "Unknown CODEC Version\n");
2915 		return -EINVAL;
2916 	}
2917 
2918 	switch (source) {
2919 	case RT5663_PLL1_S_MCLK:
2920 		val = 0x0;
2921 		break;
2922 	case RT5663_PLL1_S_BCLK1:
2923 		val = 0x1;
2924 		break;
2925 	default:
2926 		dev_err(component->dev, "Unknown PLL source %d\n", source);
2927 		return -EINVAL;
2928 	}
2929 	snd_soc_component_update_bits(component, RT5663_GLB_CLK, mask, (val << shift));
2930 
2931 	ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
2932 	if (ret < 0) {
2933 		dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
2934 		return ret;
2935 	}
2936 
2937 	dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n", pll_code.m_bp,
2938 		(pll_code.m_bp ? 0 : pll_code.m_code), pll_code.n_code,
2939 		pll_code.k_code);
2940 
2941 	snd_soc_component_write(component, RT5663_PLL_1,
2942 		pll_code.n_code << RT5663_PLL_N_SHIFT | pll_code.k_code);
2943 	snd_soc_component_write(component, RT5663_PLL_2,
2944 		(pll_code.m_bp ? 0 : pll_code.m_code) << RT5663_PLL_M_SHIFT |
2945 		pll_code.m_bp << RT5663_PLL_M_BP_SHIFT);
2946 
2947 	rt5663->pll_in = freq_in;
2948 	rt5663->pll_out = freq_out;
2949 	rt5663->pll_src = source;
2950 
2951 	return 0;
2952 }
2953 
2954 static int rt5663_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
2955 	unsigned int rx_mask, int slots, int slot_width)
2956 {
2957 	struct snd_soc_component *component = dai->component;
2958 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
2959 	unsigned int val = 0, reg;
2960 
2961 	if (rx_mask || tx_mask)
2962 		val |= RT5663_TDM_MODE_TDM;
2963 
2964 	switch (slots) {
2965 	case 4:
2966 		val |= RT5663_TDM_IN_CH_4;
2967 		val |= RT5663_TDM_OUT_CH_4;
2968 		break;
2969 	case 6:
2970 		val |= RT5663_TDM_IN_CH_6;
2971 		val |= RT5663_TDM_OUT_CH_6;
2972 		break;
2973 	case 8:
2974 		val |= RT5663_TDM_IN_CH_8;
2975 		val |= RT5663_TDM_OUT_CH_8;
2976 		break;
2977 	case 2:
2978 		break;
2979 	default:
2980 		return -EINVAL;
2981 	}
2982 
2983 	switch (slot_width) {
2984 	case 20:
2985 		val |= RT5663_TDM_IN_LEN_20;
2986 		val |= RT5663_TDM_OUT_LEN_20;
2987 		break;
2988 	case 24:
2989 		val |= RT5663_TDM_IN_LEN_24;
2990 		val |= RT5663_TDM_OUT_LEN_24;
2991 		break;
2992 	case 32:
2993 		val |= RT5663_TDM_IN_LEN_32;
2994 		val |= RT5663_TDM_OUT_LEN_32;
2995 		break;
2996 	case 16:
2997 		break;
2998 	default:
2999 		return -EINVAL;
3000 	}
3001 
3002 	switch (rt5663->codec_ver) {
3003 	case CODEC_VER_1:
3004 		reg = RT5663_TDM_2;
3005 		break;
3006 	case CODEC_VER_0:
3007 		reg = RT5663_TDM_1;
3008 		break;
3009 	default:
3010 		dev_err(component->dev, "Unknown CODEC Version\n");
3011 		return -EINVAL;
3012 	}
3013 
3014 	snd_soc_component_update_bits(component, reg, RT5663_TDM_MODE_MASK |
3015 		RT5663_TDM_IN_CH_MASK | RT5663_TDM_OUT_CH_MASK |
3016 		RT5663_TDM_IN_LEN_MASK | RT5663_TDM_OUT_LEN_MASK, val);
3017 
3018 	return 0;
3019 }
3020 
3021 static int rt5663_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3022 {
3023 	struct snd_soc_component *component = dai->component;
3024 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
3025 	unsigned int reg;
3026 
3027 	dev_dbg(component->dev, "%s ratio = %d\n", __func__, ratio);
3028 
3029 	if (rt5663->codec_ver == CODEC_VER_1)
3030 		reg = RT5663_TDM_9;
3031 	else
3032 		reg = RT5663_TDM_5;
3033 
3034 	switch (ratio) {
3035 	case 32:
3036 		snd_soc_component_update_bits(component, reg,
3037 			RT5663_TDM_LENGTN_MASK,
3038 			RT5663_TDM_LENGTN_16);
3039 		break;
3040 	case 40:
3041 		snd_soc_component_update_bits(component, reg,
3042 			RT5663_TDM_LENGTN_MASK,
3043 			RT5663_TDM_LENGTN_20);
3044 		break;
3045 	case 48:
3046 		snd_soc_component_update_bits(component, reg,
3047 			RT5663_TDM_LENGTN_MASK,
3048 			RT5663_TDM_LENGTN_24);
3049 		break;
3050 	case 64:
3051 		snd_soc_component_update_bits(component, reg,
3052 			RT5663_TDM_LENGTN_MASK,
3053 			RT5663_TDM_LENGTN_32);
3054 		break;
3055 	default:
3056 		dev_err(component->dev, "Invalid ratio!\n");
3057 		return -EINVAL;
3058 	}
3059 
3060 	return 0;
3061 }
3062 
3063 static int rt5663_set_bias_level(struct snd_soc_component *component,
3064 			enum snd_soc_bias_level level)
3065 {
3066 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
3067 
3068 	switch (level) {
3069 	case SND_SOC_BIAS_ON:
3070 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
3071 			RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK,
3072 			RT5663_PWR_FV1 | RT5663_PWR_FV2);
3073 		break;
3074 
3075 	case SND_SOC_BIAS_PREPARE:
3076 		if (rt5663->codec_ver == CODEC_VER_1) {
3077 			snd_soc_component_update_bits(component, RT5663_DIG_MISC,
3078 				RT5663_DIG_GATE_CTRL_MASK,
3079 				RT5663_DIG_GATE_CTRL_EN);
3080 			snd_soc_component_update_bits(component, RT5663_SIG_CLK_DET,
3081 				RT5663_EN_ANA_CLK_DET_MASK |
3082 				RT5663_PWR_CLK_DET_MASK,
3083 				RT5663_EN_ANA_CLK_DET_AUTO |
3084 				RT5663_PWR_CLK_DET_EN);
3085 		}
3086 		break;
3087 
3088 	case SND_SOC_BIAS_STANDBY:
3089 		if (rt5663->codec_ver == CODEC_VER_1)
3090 			snd_soc_component_update_bits(component, RT5663_DIG_MISC,
3091 				RT5663_DIG_GATE_CTRL_MASK,
3092 				RT5663_DIG_GATE_CTRL_DIS);
3093 		snd_soc_component_update_bits(component, RT5663_PWR_ANLG_1,
3094 			RT5663_PWR_VREF1_MASK | RT5663_PWR_VREF2_MASK |
3095 			RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK |
3096 			RT5663_PWR_MB_MASK, RT5663_PWR_VREF1 |
3097 			RT5663_PWR_VREF2 | RT5663_PWR_MB);
3098 		usleep_range(10000, 10005);
3099 		if (rt5663->codec_ver == CODEC_VER_1) {
3100 			snd_soc_component_update_bits(component, RT5663_SIG_CLK_DET,
3101 				RT5663_EN_ANA_CLK_DET_MASK |
3102 				RT5663_PWR_CLK_DET_MASK,
3103 				RT5663_EN_ANA_CLK_DET_DIS |
3104 				RT5663_PWR_CLK_DET_DIS);
3105 		}
3106 		break;
3107 
3108 	case SND_SOC_BIAS_OFF:
3109 		if (rt5663->jack_type != SND_JACK_HEADSET)
3110 			snd_soc_component_update_bits(component,
3111 				RT5663_PWR_ANLG_1,
3112 				RT5663_PWR_VREF1_MASK | RT5663_PWR_VREF2_MASK |
3113 				RT5663_PWR_FV1 | RT5663_PWR_FV2 |
3114 				RT5663_PWR_MB_MASK, 0);
3115 		else
3116 			snd_soc_component_update_bits(component,
3117 				RT5663_PWR_ANLG_1,
3118 				RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK,
3119 				RT5663_PWR_FV1 | RT5663_PWR_FV2);
3120 		break;
3121 
3122 	default:
3123 		break;
3124 	}
3125 
3126 	return 0;
3127 }
3128 
3129 static int rt5663_probe(struct snd_soc_component *component)
3130 {
3131 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
3132 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
3133 
3134 	rt5663->component = component;
3135 
3136 	switch (rt5663->codec_ver) {
3137 	case CODEC_VER_1:
3138 		snd_soc_dapm_new_controls(dapm,
3139 			rt5663_v2_specific_dapm_widgets,
3140 			ARRAY_SIZE(rt5663_v2_specific_dapm_widgets));
3141 		snd_soc_dapm_add_routes(dapm,
3142 			rt5663_v2_specific_dapm_routes,
3143 			ARRAY_SIZE(rt5663_v2_specific_dapm_routes));
3144 		snd_soc_add_component_controls(component, rt5663_v2_specific_controls,
3145 			ARRAY_SIZE(rt5663_v2_specific_controls));
3146 		break;
3147 	case CODEC_VER_0:
3148 		snd_soc_dapm_new_controls(dapm,
3149 			rt5663_specific_dapm_widgets,
3150 			ARRAY_SIZE(rt5663_specific_dapm_widgets));
3151 		snd_soc_dapm_add_routes(dapm,
3152 			rt5663_specific_dapm_routes,
3153 			ARRAY_SIZE(rt5663_specific_dapm_routes));
3154 		snd_soc_add_component_controls(component, rt5663_specific_controls,
3155 			ARRAY_SIZE(rt5663_specific_controls));
3156 
3157 		if (!rt5663->imp_table)
3158 			snd_soc_add_component_controls(component, rt5663_hpvol_controls,
3159 				ARRAY_SIZE(rt5663_hpvol_controls));
3160 		break;
3161 	}
3162 
3163 	return 0;
3164 }
3165 
3166 static void rt5663_remove(struct snd_soc_component *component)
3167 {
3168 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
3169 
3170 	regmap_write(rt5663->regmap, RT5663_RESET, 0);
3171 }
3172 
3173 #ifdef CONFIG_PM
3174 static int rt5663_suspend(struct snd_soc_component *component)
3175 {
3176 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
3177 
3178 	regcache_cache_only(rt5663->regmap, true);
3179 	regcache_mark_dirty(rt5663->regmap);
3180 
3181 	return 0;
3182 }
3183 
3184 static int rt5663_resume(struct snd_soc_component *component)
3185 {
3186 	struct rt5663_priv *rt5663 = snd_soc_component_get_drvdata(component);
3187 
3188 	regcache_cache_only(rt5663->regmap, false);
3189 	regcache_sync(rt5663->regmap);
3190 
3191 	rt5663_irq(0, rt5663);
3192 
3193 	return 0;
3194 }
3195 #else
3196 #define rt5663_suspend NULL
3197 #define rt5663_resume NULL
3198 #endif
3199 
3200 #define RT5663_STEREO_RATES SNDRV_PCM_RATE_8000_192000
3201 #define RT5663_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
3202 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
3203 
3204 static const struct snd_soc_dai_ops rt5663_aif_dai_ops = {
3205 	.hw_params = rt5663_hw_params,
3206 	.set_fmt = rt5663_set_dai_fmt,
3207 	.set_sysclk = rt5663_set_dai_sysclk,
3208 	.set_pll = rt5663_set_dai_pll,
3209 	.set_tdm_slot = rt5663_set_tdm_slot,
3210 	.set_bclk_ratio = rt5663_set_bclk_ratio,
3211 };
3212 
3213 static struct snd_soc_dai_driver rt5663_dai[] = {
3214 	{
3215 		.name = "rt5663-aif",
3216 		.id = RT5663_AIF,
3217 		.playback = {
3218 			.stream_name = "AIF Playback",
3219 			.channels_min = 1,
3220 			.channels_max = 2,
3221 			.rates = RT5663_STEREO_RATES,
3222 			.formats = RT5663_FORMATS,
3223 		},
3224 		.capture = {
3225 			.stream_name = "AIF Capture",
3226 			.channels_min = 1,
3227 			.channels_max = 2,
3228 			.rates = RT5663_STEREO_RATES,
3229 			.formats = RT5663_FORMATS,
3230 		},
3231 		.ops = &rt5663_aif_dai_ops,
3232 	},
3233 };
3234 
3235 static const struct snd_soc_component_driver soc_component_dev_rt5663 = {
3236 	.probe			= rt5663_probe,
3237 	.remove			= rt5663_remove,
3238 	.suspend		= rt5663_suspend,
3239 	.resume			= rt5663_resume,
3240 	.set_bias_level		= rt5663_set_bias_level,
3241 	.controls		= rt5663_snd_controls,
3242 	.num_controls		= ARRAY_SIZE(rt5663_snd_controls),
3243 	.dapm_widgets		= rt5663_dapm_widgets,
3244 	.num_dapm_widgets	= ARRAY_SIZE(rt5663_dapm_widgets),
3245 	.dapm_routes		= rt5663_dapm_routes,
3246 	.num_dapm_routes	= ARRAY_SIZE(rt5663_dapm_routes),
3247 	.use_pmdown_time	= 1,
3248 	.endianness		= 1,
3249 	.non_legacy_dai_naming	= 1,
3250 
3251 };
3252 
3253 static const struct regmap_config rt5663_v2_regmap = {
3254 	.reg_bits = 16,
3255 	.val_bits = 16,
3256 	.use_single_rw = true,
3257 	.max_register = 0x07fa,
3258 	.volatile_reg = rt5663_v2_volatile_register,
3259 	.readable_reg = rt5663_v2_readable_register,
3260 	.cache_type = REGCACHE_RBTREE,
3261 	.reg_defaults = rt5663_v2_reg,
3262 	.num_reg_defaults = ARRAY_SIZE(rt5663_v2_reg),
3263 };
3264 
3265 static const struct regmap_config rt5663_regmap = {
3266 	.reg_bits = 16,
3267 	.val_bits = 16,
3268 	.use_single_rw = true,
3269 	.max_register = 0x03f3,
3270 	.volatile_reg = rt5663_volatile_register,
3271 	.readable_reg = rt5663_readable_register,
3272 	.cache_type = REGCACHE_RBTREE,
3273 	.reg_defaults = rt5663_reg,
3274 	.num_reg_defaults = ARRAY_SIZE(rt5663_reg),
3275 };
3276 
3277 static const struct regmap_config temp_regmap = {
3278 	.name = "nocache",
3279 	.reg_bits = 16,
3280 	.val_bits = 16,
3281 	.use_single_rw = true,
3282 	.max_register = 0x03f3,
3283 	.cache_type = REGCACHE_NONE,
3284 };
3285 
3286 static const struct i2c_device_id rt5663_i2c_id[] = {
3287 	{ "rt5663", 0 },
3288 	{}
3289 };
3290 MODULE_DEVICE_TABLE(i2c, rt5663_i2c_id);
3291 
3292 #if defined(CONFIG_OF)
3293 static const struct of_device_id rt5663_of_match[] = {
3294 	{ .compatible = "realtek,rt5663", },
3295 	{},
3296 };
3297 MODULE_DEVICE_TABLE(of, rt5663_of_match);
3298 #endif
3299 
3300 #ifdef CONFIG_ACPI
3301 static const struct acpi_device_id rt5663_acpi_match[] = {
3302 	{ "10EC5663", 0},
3303 	{},
3304 };
3305 MODULE_DEVICE_TABLE(acpi, rt5663_acpi_match);
3306 #endif
3307 
3308 static void rt5663_v2_calibrate(struct rt5663_priv *rt5663)
3309 {
3310 	regmap_write(rt5663->regmap, RT5663_BIAS_CUR_8, 0xa402);
3311 	regmap_write(rt5663->regmap, RT5663_PWR_DIG_1, 0x0100);
3312 	regmap_write(rt5663->regmap, RT5663_RECMIX, 0x4040);
3313 	regmap_write(rt5663->regmap, RT5663_DIG_MISC, 0x0001);
3314 	regmap_write(rt5663->regmap, RT5663_RC_CLK, 0x0380);
3315 	regmap_write(rt5663->regmap, RT5663_GLB_CLK, 0x8000);
3316 	regmap_write(rt5663->regmap, RT5663_ADDA_CLK_1, 0x1000);
3317 	regmap_write(rt5663->regmap, RT5663_CHOP_DAC_L, 0x3030);
3318 	regmap_write(rt5663->regmap, RT5663_CALIB_ADC, 0x3c05);
3319 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xa23e);
3320 	msleep(40);
3321 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xf23e);
3322 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_2, 0x0321);
3323 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_1, 0xfc00);
3324 	msleep(500);
3325 }
3326 
3327 static void rt5663_calibrate(struct rt5663_priv *rt5663)
3328 {
3329 	int value, count;
3330 
3331 	regmap_write(rt5663->regmap, RT5663_RESET, 0x0000);
3332 	msleep(20);
3333 	regmap_write(rt5663->regmap, RT5663_ANA_BIAS_CUR_4, 0x00a1);
3334 	regmap_write(rt5663->regmap, RT5663_RC_CLK, 0x0380);
3335 	regmap_write(rt5663->regmap, RT5663_GLB_CLK, 0x8000);
3336 	regmap_write(rt5663->regmap, RT5663_ADDA_CLK_1, 0x1000);
3337 	regmap_write(rt5663->regmap, RT5663_VREF_RECMIX, 0x0032);
3338 	regmap_write(rt5663->regmap, RT5663_HP_IMP_SEN_19, 0x000c);
3339 	regmap_write(rt5663->regmap, RT5663_DUMMY_1, 0x0324);
3340 	regmap_write(rt5663->regmap, RT5663_DIG_MISC, 0x8001);
3341 	regmap_write(rt5663->regmap, RT5663_VREFADJ_OP, 0x0f28);
3342 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xa23b);
3343 	msleep(30);
3344 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xf23b);
3345 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_2, 0x8000);
3346 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_3, 0x0008);
3347 	regmap_write(rt5663->regmap, RT5663_PRE_DIV_GATING_1, 0xffff);
3348 	regmap_write(rt5663->regmap, RT5663_PRE_DIV_GATING_2, 0xffff);
3349 	regmap_write(rt5663->regmap, RT5663_CBJ_1, 0x8c10);
3350 	regmap_write(rt5663->regmap, RT5663_IL_CMD_2, 0x00c1);
3351 	regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_1, 0xb880);
3352 	regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_2, 0x4110);
3353 	regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_2, 0x4118);
3354 
3355 	count = 0;
3356 	while (true) {
3357 		regmap_read(rt5663->regmap, RT5663_INT_ST_2, &value);
3358 		if (!(value & 0x80))
3359 			usleep_range(10000, 10005);
3360 		else
3361 			break;
3362 
3363 		if (++count > 200)
3364 			break;
3365 	}
3366 
3367 	regmap_write(rt5663->regmap, RT5663_HP_IMP_SEN_19, 0x0000);
3368 	regmap_write(rt5663->regmap, RT5663_DEPOP_2, 0x3003);
3369 	regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x0038);
3370 	regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x003b);
3371 	regmap_write(rt5663->regmap, RT5663_PWR_DIG_2, 0x8400);
3372 	regmap_write(rt5663->regmap, RT5663_PWR_DIG_1, 0x8df8);
3373 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_2, 0x8003);
3374 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_3, 0x018c);
3375 	regmap_write(rt5663->regmap, RT5663_HP_CHARGE_PUMP_1, 0x1e32);
3376 	regmap_write(rt5663->regmap, RT5663_DUMMY_2, 0x8089);
3377 	regmap_write(rt5663->regmap, RT5663_DACREF_LDO, 0x3b0b);
3378 	msleep(40);
3379 	regmap_write(rt5663->regmap, RT5663_STO_DAC_MIXER, 0x0000);
3380 	regmap_write(rt5663->regmap, RT5663_BYPASS_STO_DAC, 0x000c);
3381 	regmap_write(rt5663->regmap, RT5663_HP_BIAS, 0xafaa);
3382 	regmap_write(rt5663->regmap, RT5663_CHARGE_PUMP_1, 0x2224);
3383 	regmap_write(rt5663->regmap, RT5663_HP_OUT_EN, 0x8088);
3384 	regmap_write(rt5663->regmap, RT5663_STO_DRE_9, 0x0017);
3385 	regmap_write(rt5663->regmap, RT5663_STO_DRE_10, 0x0017);
3386 	regmap_write(rt5663->regmap, RT5663_STO1_ADC_MIXER, 0x4040);
3387 	regmap_write(rt5663->regmap, RT5663_CHOP_ADC, 0x3000);
3388 	regmap_write(rt5663->regmap, RT5663_RECMIX, 0x0005);
3389 	regmap_write(rt5663->regmap, RT5663_ADDA_RST, 0xc000);
3390 	regmap_write(rt5663->regmap, RT5663_STO1_HPF_ADJ1, 0x3320);
3391 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_2, 0x00c9);
3392 	regmap_write(rt5663->regmap, RT5663_DUMMY_1, 0x004c);
3393 	regmap_write(rt5663->regmap, RT5663_ANA_BIAS_CUR_1, 0x1111);
3394 	regmap_write(rt5663->regmap, RT5663_BIAS_CUR_8, 0x4402);
3395 	regmap_write(rt5663->regmap, RT5663_CHARGE_PUMP_2, 0x3311);
3396 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_1, 0x0069);
3397 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_3, 0x06ce);
3398 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0x6800);
3399 	regmap_write(rt5663->regmap, RT5663_CHARGE_PUMP_2, 0x1100);
3400 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_7, 0x0057);
3401 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0xe800);
3402 
3403 	count = 0;
3404 	while (true) {
3405 		regmap_read(rt5663->regmap, RT5663_HP_CALIB_1_1, &value);
3406 		if (value & 0x8000)
3407 			usleep_range(10000, 10005);
3408 		else
3409 			break;
3410 
3411 		if (count > 200)
3412 			return;
3413 		count++;
3414 	}
3415 
3416 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0x6200);
3417 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_7, 0x0059);
3418 	regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0xe200);
3419 
3420 	count = 0;
3421 	while (true) {
3422 		regmap_read(rt5663->regmap, RT5663_HP_CALIB_1_1, &value);
3423 		if (value & 0x8000)
3424 			usleep_range(10000, 10005);
3425 		else
3426 			break;
3427 
3428 		if (count > 200)
3429 			return;
3430 		count++;
3431 	}
3432 
3433 	regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_1, 0xb8e0);
3434 	usleep_range(10000, 10005);
3435 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0x003b);
3436 	usleep_range(10000, 10005);
3437 	regmap_write(rt5663->regmap, RT5663_PWR_DIG_1, 0x0000);
3438 	usleep_range(10000, 10005);
3439 	regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x000b);
3440 	usleep_range(10000, 10005);
3441 	regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x0008);
3442 	usleep_range(10000, 10005);
3443 	regmap_write(rt5663->regmap, RT5663_PWR_ANLG_2, 0x0000);
3444 	usleep_range(10000, 10005);
3445 }
3446 
3447 static int rt5663_parse_dp(struct rt5663_priv *rt5663, struct device *dev)
3448 {
3449 	int table_size;
3450 
3451 	device_property_read_u32(dev, "realtek,dc_offset_l_manual",
3452 		&rt5663->pdata.dc_offset_l_manual);
3453 	device_property_read_u32(dev, "realtek,dc_offset_r_manual",
3454 		&rt5663->pdata.dc_offset_r_manual);
3455 	device_property_read_u32(dev, "realtek,dc_offset_l_manual_mic",
3456 		&rt5663->pdata.dc_offset_l_manual_mic);
3457 	device_property_read_u32(dev, "realtek,dc_offset_r_manual_mic",
3458 		&rt5663->pdata.dc_offset_r_manual_mic);
3459 	device_property_read_u32(dev, "realtek,impedance_sensing_num",
3460 		&rt5663->pdata.impedance_sensing_num);
3461 
3462 	if (rt5663->pdata.impedance_sensing_num) {
3463 		table_size = sizeof(struct impedance_mapping_table) *
3464 			rt5663->pdata.impedance_sensing_num;
3465 		rt5663->imp_table = devm_kzalloc(dev, table_size, GFP_KERNEL);
3466 		device_property_read_u32_array(dev,
3467 			"realtek,impedance_sensing_table",
3468 			(u32 *)rt5663->imp_table, table_size);
3469 	}
3470 
3471 	return 0;
3472 }
3473 
3474 static int rt5663_i2c_probe(struct i2c_client *i2c,
3475 		    const struct i2c_device_id *id)
3476 {
3477 	struct rt5663_platform_data *pdata = dev_get_platdata(&i2c->dev);
3478 	struct rt5663_priv *rt5663;
3479 	int ret;
3480 	unsigned int val;
3481 	struct regmap *regmap;
3482 
3483 	rt5663 = devm_kzalloc(&i2c->dev, sizeof(struct rt5663_priv),
3484 		GFP_KERNEL);
3485 
3486 	if (rt5663 == NULL)
3487 		return -ENOMEM;
3488 
3489 	i2c_set_clientdata(i2c, rt5663);
3490 
3491 	if (pdata)
3492 		rt5663->pdata = *pdata;
3493 	else
3494 		rt5663_parse_dp(rt5663, &i2c->dev);
3495 
3496 	regmap = devm_regmap_init_i2c(i2c, &temp_regmap);
3497 	if (IS_ERR(regmap)) {
3498 		ret = PTR_ERR(regmap);
3499 		dev_err(&i2c->dev, "Failed to allocate temp register map: %d\n",
3500 			ret);
3501 		return ret;
3502 	}
3503 
3504 	ret = regmap_read(regmap, RT5663_VENDOR_ID_2, &val);
3505 	if (ret || (val != RT5663_DEVICE_ID_2 && val != RT5663_DEVICE_ID_1)) {
3506 		dev_err(&i2c->dev,
3507 			"Device with ID register %#x is not rt5663, retry one time.\n",
3508 			val);
3509 		msleep(100);
3510 		regmap_read(regmap, RT5663_VENDOR_ID_2, &val);
3511 	}
3512 
3513 	switch (val) {
3514 	case RT5663_DEVICE_ID_2:
3515 		rt5663->regmap = devm_regmap_init_i2c(i2c, &rt5663_v2_regmap);
3516 		rt5663->codec_ver = CODEC_VER_1;
3517 		break;
3518 	case RT5663_DEVICE_ID_1:
3519 		rt5663->regmap = devm_regmap_init_i2c(i2c, &rt5663_regmap);
3520 		rt5663->codec_ver = CODEC_VER_0;
3521 		break;
3522 	default:
3523 		dev_err(&i2c->dev,
3524 			"Device with ID register %#x is not rt5663\n",
3525 			val);
3526 		return -ENODEV;
3527 	}
3528 
3529 	if (IS_ERR(rt5663->regmap)) {
3530 		ret = PTR_ERR(rt5663->regmap);
3531 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
3532 			ret);
3533 		return ret;
3534 	}
3535 
3536 	/* reset and calibrate */
3537 	regmap_write(rt5663->regmap, RT5663_RESET, 0);
3538 	regcache_cache_bypass(rt5663->regmap, true);
3539 	switch (rt5663->codec_ver) {
3540 	case CODEC_VER_1:
3541 		rt5663_v2_calibrate(rt5663);
3542 		break;
3543 	case CODEC_VER_0:
3544 		rt5663_calibrate(rt5663);
3545 		break;
3546 	default:
3547 		dev_err(&i2c->dev, "%s:Unknown codec type\n", __func__);
3548 	}
3549 	regcache_cache_bypass(rt5663->regmap, false);
3550 	regmap_write(rt5663->regmap, RT5663_RESET, 0);
3551 	dev_dbg(&i2c->dev, "calibrate done\n");
3552 
3553 	switch (rt5663->codec_ver) {
3554 	case CODEC_VER_1:
3555 		break;
3556 	case CODEC_VER_0:
3557 		ret = regmap_register_patch(rt5663->regmap, rt5663_patch_list,
3558 					    ARRAY_SIZE(rt5663_patch_list));
3559 		if (ret != 0)
3560 			dev_warn(&i2c->dev,
3561 				"Failed to apply regmap patch: %d\n", ret);
3562 		break;
3563 	default:
3564 		dev_err(&i2c->dev, "%s:Unknown codec type\n", __func__);
3565 	}
3566 
3567 	/* GPIO1 as IRQ */
3568 	regmap_update_bits(rt5663->regmap, RT5663_GPIO_1, RT5663_GP1_PIN_MASK,
3569 		RT5663_GP1_PIN_IRQ);
3570 	/* 4btn inline command debounce */
3571 	regmap_update_bits(rt5663->regmap, RT5663_IL_CMD_5,
3572 		RT5663_4BTN_CLK_DEB_MASK, RT5663_4BTN_CLK_DEB_65MS);
3573 
3574 	switch (rt5663->codec_ver) {
3575 	case CODEC_VER_1:
3576 		regmap_write(rt5663->regmap, RT5663_BIAS_CUR_8, 0xa402);
3577 		/* JD1 */
3578 		regmap_update_bits(rt5663->regmap, RT5663_AUTO_1MRC_CLK,
3579 			RT5663_IRQ_POW_SAV_MASK | RT5663_IRQ_POW_SAV_JD1_MASK,
3580 			RT5663_IRQ_POW_SAV_EN | RT5663_IRQ_POW_SAV_JD1_EN);
3581 		regmap_update_bits(rt5663->regmap, RT5663_PWR_ANLG_2,
3582 			RT5663_PWR_JD1_MASK, RT5663_PWR_JD1);
3583 		regmap_update_bits(rt5663->regmap, RT5663_IRQ_1,
3584 			RT5663_EN_CB_JD_MASK, RT5663_EN_CB_JD_EN);
3585 
3586 		regmap_update_bits(rt5663->regmap, RT5663_HP_LOGIC_2,
3587 			RT5663_HP_SIG_SRC1_MASK, RT5663_HP_SIG_SRC1_REG);
3588 		regmap_update_bits(rt5663->regmap, RT5663_RECMIX,
3589 			RT5663_VREF_BIAS_MASK | RT5663_CBJ_DET_MASK |
3590 			RT5663_DET_TYPE_MASK, RT5663_VREF_BIAS_REG |
3591 			RT5663_CBJ_DET_EN | RT5663_DET_TYPE_QFN);
3592 		/* Set GPIO4 and GPIO8 as input for combo jack */
3593 		regmap_update_bits(rt5663->regmap, RT5663_GPIO_2,
3594 			RT5663_GP4_PIN_CONF_MASK, RT5663_GP4_PIN_CONF_INPUT);
3595 		regmap_update_bits(rt5663->regmap, RT5663_GPIO_3,
3596 			RT5663_GP8_PIN_CONF_MASK, RT5663_GP8_PIN_CONF_INPUT);
3597 		regmap_update_bits(rt5663->regmap, RT5663_PWR_ANLG_1,
3598 			RT5663_LDO1_DVO_MASK | RT5663_AMP_HP_MASK,
3599 			RT5663_LDO1_DVO_0_9V | RT5663_AMP_HP_3X);
3600 			break;
3601 	case CODEC_VER_0:
3602 		regmap_update_bits(rt5663->regmap, RT5663_DIG_MISC,
3603 			RT5663_DIG_GATE_CTRL_MASK, RT5663_DIG_GATE_CTRL_EN);
3604 		regmap_update_bits(rt5663->regmap, RT5663_AUTO_1MRC_CLK,
3605 			RT5663_IRQ_MANUAL_MASK, RT5663_IRQ_MANUAL_EN);
3606 		regmap_update_bits(rt5663->regmap, RT5663_IRQ_1,
3607 			RT5663_EN_IRQ_JD1_MASK, RT5663_EN_IRQ_JD1_EN);
3608 		regmap_update_bits(rt5663->regmap, RT5663_GPIO_1,
3609 			RT5663_GPIO1_TYPE_MASK, RT5663_GPIO1_TYPE_EN);
3610 		regmap_write(rt5663->regmap, RT5663_VREF_RECMIX, 0x0032);
3611 		regmap_update_bits(rt5663->regmap, RT5663_GPIO_2,
3612 			RT5663_GP1_PIN_CONF_MASK | RT5663_SEL_GPIO1_MASK,
3613 			RT5663_GP1_PIN_CONF_OUTPUT | RT5663_SEL_GPIO1_EN);
3614 		regmap_update_bits(rt5663->regmap, RT5663_RECMIX,
3615 			RT5663_RECMIX1_BST1_MASK, RT5663_RECMIX1_BST1_ON);
3616 		regmap_update_bits(rt5663->regmap, RT5663_TDM_2,
3617 			RT5663_DATA_SWAP_ADCDAT1_MASK,
3618 			RT5663_DATA_SWAP_ADCDAT1_LL);
3619 			break;
3620 	default:
3621 		dev_err(&i2c->dev, "%s:Unknown codec type\n", __func__);
3622 	}
3623 
3624 	INIT_DELAYED_WORK(&rt5663->jack_detect_work, rt5663_jack_detect_work);
3625 	INIT_DELAYED_WORK(&rt5663->jd_unplug_work, rt5663_jd_unplug_work);
3626 
3627 	if (i2c->irq) {
3628 		ret = request_irq(i2c->irq, rt5663_irq,
3629 			IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
3630 			| IRQF_ONESHOT, "rt5663", rt5663);
3631 		if (ret)
3632 			dev_err(&i2c->dev, "%s Failed to reguest IRQ: %d\n",
3633 				__func__, ret);
3634 	}
3635 
3636 	ret = devm_snd_soc_register_component(&i2c->dev,
3637 			&soc_component_dev_rt5663,
3638 			rt5663_dai, ARRAY_SIZE(rt5663_dai));
3639 
3640 	if (ret) {
3641 		if (i2c->irq)
3642 			free_irq(i2c->irq, rt5663);
3643 	}
3644 
3645 	return ret;
3646 }
3647 
3648 static int rt5663_i2c_remove(struct i2c_client *i2c)
3649 {
3650 	struct rt5663_priv *rt5663 = i2c_get_clientdata(i2c);
3651 
3652 	if (i2c->irq)
3653 		free_irq(i2c->irq, rt5663);
3654 
3655 	return 0;
3656 }
3657 
3658 static void rt5663_i2c_shutdown(struct i2c_client *client)
3659 {
3660 	struct rt5663_priv *rt5663 = i2c_get_clientdata(client);
3661 
3662 	regmap_write(rt5663->regmap, RT5663_RESET, 0);
3663 }
3664 
3665 static struct i2c_driver rt5663_i2c_driver = {
3666 	.driver = {
3667 		.name = "rt5663",
3668 		.acpi_match_table = ACPI_PTR(rt5663_acpi_match),
3669 		.of_match_table = of_match_ptr(rt5663_of_match),
3670 	},
3671 	.probe = rt5663_i2c_probe,
3672 	.remove = rt5663_i2c_remove,
3673 	.shutdown = rt5663_i2c_shutdown,
3674 	.id_table = rt5663_i2c_id,
3675 };
3676 module_i2c_driver(rt5663_i2c_driver);
3677 
3678 MODULE_DESCRIPTION("ASoC RT5663 driver");
3679 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
3680 MODULE_LICENSE("GPL v2");
3681