blob: be93231b0f58f956b2a126803ed554fc2f773d66 [file] [log] [blame]
Bard Liaodf7c5212016-09-09 10:33:10 +08001/*
Bard Liao73444722016-10-21 11:02:28 +08002 * rt5663.c -- RT5663 ALSA SoC audio codec driver
Bard Liaodf7c5212016-09-09 10:33:10 +08003 *
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
Bard Liao73444722016-10-21 11:02:28 +080033#define RT5663_DEVICE_ID_2 0x6451
34#define RT5663_DEVICE_ID_1 0x6406
Bard Liaodf7c5212016-09-09 10:33:10 +080035
36enum {
Bard Liao73444722016-10-21 11:02:28 +080037 CODEC_VER_1,
38 CODEC_VER_0,
Bard Liaodf7c5212016-09-09 10:33:10 +080039};
40
41struct rt5663_priv {
42 struct snd_soc_codec *codec;
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +080043 struct rt5663_platform_data pdata;
Bard Liaodf7c5212016-09-09 10:33:10 +080044 struct regmap *regmap;
45 struct delayed_work jack_detect_work;
46 struct snd_soc_jack *hs_jack;
47 struct timer_list btn_check_timer;
48
Bard Liao73444722016-10-21 11:02:28 +080049 int codec_ver;
Bard Liaodf7c5212016-09-09 10:33:10 +080050 int sysclk;
51 int sysclk_src;
52 int lrck;
53
54 int pll_src;
55 int pll_in;
56 int pll_out;
57
58 int jack_type;
59};
60
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +080061static const struct reg_sequence rt5663_patch_list[] = {
oder_chiou@realtek.com1d5c5b62017-07-19 10:20:23 +080062 { 0x002a, 0x8020 },
oder_chiou@realtek.comd26ed932017-07-18 10:12:58 +080063 { 0x0086, 0x0028 },
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +080064};
65
Bard Liao73444722016-10-21 11:02:28 +080066static const struct reg_default rt5663_v2_reg[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +080067 { 0x0000, 0x0000 },
68 { 0x0001, 0xc8c8 },
69 { 0x0002, 0x8080 },
70 { 0x0003, 0x8000 },
71 { 0x0004, 0xc80a },
72 { 0x0005, 0x0000 },
73 { 0x0006, 0x0000 },
74 { 0x0007, 0x0000 },
75 { 0x000a, 0x0000 },
76 { 0x000b, 0x0000 },
77 { 0x000c, 0x0000 },
78 { 0x000d, 0x0000 },
79 { 0x000f, 0x0808 },
80 { 0x0010, 0x4000 },
81 { 0x0011, 0x0000 },
82 { 0x0012, 0x1404 },
83 { 0x0013, 0x1000 },
84 { 0x0014, 0xa00a },
85 { 0x0015, 0x0404 },
86 { 0x0016, 0x0404 },
87 { 0x0017, 0x0011 },
88 { 0x0018, 0xafaf },
89 { 0x0019, 0xafaf },
90 { 0x001a, 0xafaf },
91 { 0x001b, 0x0011 },
92 { 0x001c, 0x2f2f },
93 { 0x001d, 0x2f2f },
94 { 0x001e, 0x2f2f },
95 { 0x001f, 0x0000 },
96 { 0x0020, 0x0000 },
97 { 0x0021, 0x0000 },
98 { 0x0022, 0x5757 },
99 { 0x0023, 0x0039 },
100 { 0x0024, 0x000b },
101 { 0x0026, 0xc0c0 },
102 { 0x0027, 0xc0c0 },
103 { 0x0028, 0xc0c0 },
104 { 0x0029, 0x8080 },
105 { 0x002a, 0xaaaa },
106 { 0x002b, 0xaaaa },
107 { 0x002c, 0xaba8 },
108 { 0x002d, 0x0000 },
109 { 0x002e, 0x0000 },
110 { 0x002f, 0x0000 },
111 { 0x0030, 0x0000 },
112 { 0x0031, 0x5000 },
113 { 0x0032, 0x0000 },
114 { 0x0033, 0x0000 },
115 { 0x0034, 0x0000 },
116 { 0x0035, 0x0000 },
117 { 0x003a, 0x0000 },
118 { 0x003b, 0x0000 },
119 { 0x003c, 0x00ff },
120 { 0x003d, 0x0000 },
121 { 0x003e, 0x00ff },
122 { 0x003f, 0x0000 },
123 { 0x0040, 0x0000 },
124 { 0x0041, 0x00ff },
125 { 0x0042, 0x0000 },
126 { 0x0043, 0x00ff },
127 { 0x0044, 0x0c0c },
128 { 0x0049, 0xc00b },
129 { 0x004a, 0x0000 },
130 { 0x004b, 0x031f },
131 { 0x004d, 0x0000 },
132 { 0x004e, 0x001f },
133 { 0x004f, 0x0000 },
134 { 0x0050, 0x001f },
135 { 0x0052, 0xf000 },
136 { 0x0061, 0x0000 },
137 { 0x0062, 0x0000 },
138 { 0x0063, 0x003e },
139 { 0x0064, 0x0000 },
140 { 0x0065, 0x0000 },
141 { 0x0066, 0x003f },
142 { 0x0067, 0x0000 },
143 { 0x006b, 0x0000 },
144 { 0x006d, 0xff00 },
145 { 0x006e, 0x2808 },
146 { 0x006f, 0x000a },
147 { 0x0070, 0x8000 },
148 { 0x0071, 0x8000 },
149 { 0x0072, 0x8000 },
150 { 0x0073, 0x7000 },
151 { 0x0074, 0x7770 },
152 { 0x0075, 0x0002 },
153 { 0x0076, 0x0001 },
154 { 0x0078, 0x00f0 },
155 { 0x0079, 0x0000 },
156 { 0x007a, 0x0000 },
157 { 0x007b, 0x0000 },
158 { 0x007c, 0x0000 },
159 { 0x007d, 0x0123 },
160 { 0x007e, 0x4500 },
161 { 0x007f, 0x8003 },
162 { 0x0080, 0x0000 },
163 { 0x0081, 0x0000 },
164 { 0x0082, 0x0000 },
165 { 0x0083, 0x0000 },
166 { 0x0084, 0x0000 },
167 { 0x0085, 0x0000 },
168 { 0x0086, 0x0008 },
169 { 0x0087, 0x0000 },
170 { 0x0088, 0x0000 },
171 { 0x0089, 0x0000 },
172 { 0x008a, 0x0000 },
173 { 0x008b, 0x0000 },
174 { 0x008c, 0x0003 },
175 { 0x008e, 0x0060 },
176 { 0x008f, 0x1000 },
177 { 0x0091, 0x0c26 },
178 { 0x0092, 0x0073 },
179 { 0x0093, 0x0000 },
180 { 0x0094, 0x0080 },
181 { 0x0098, 0x0000 },
182 { 0x0099, 0x0000 },
183 { 0x009a, 0x0007 },
184 { 0x009f, 0x0000 },
185 { 0x00a0, 0x0000 },
186 { 0x00a1, 0x0002 },
187 { 0x00a2, 0x0001 },
188 { 0x00a3, 0x0002 },
189 { 0x00a4, 0x0001 },
190 { 0x00ae, 0x2040 },
191 { 0x00af, 0x0000 },
192 { 0x00b6, 0x0000 },
193 { 0x00b7, 0x0000 },
194 { 0x00b8, 0x0000 },
195 { 0x00b9, 0x0000 },
196 { 0x00ba, 0x0002 },
197 { 0x00bb, 0x0000 },
198 { 0x00be, 0x0000 },
199 { 0x00c0, 0x0000 },
200 { 0x00c1, 0x0aaa },
201 { 0x00c2, 0xaa80 },
202 { 0x00c3, 0x0003 },
203 { 0x00c4, 0x0000 },
204 { 0x00d0, 0x0000 },
205 { 0x00d1, 0x2244 },
206 { 0x00d2, 0x0000 },
207 { 0x00d3, 0x3300 },
208 { 0x00d4, 0x2200 },
209 { 0x00d9, 0x0809 },
210 { 0x00da, 0x0000 },
211 { 0x00db, 0x0008 },
212 { 0x00dc, 0x00c0 },
213 { 0x00dd, 0x6724 },
214 { 0x00de, 0x3131 },
215 { 0x00df, 0x0008 },
216 { 0x00e0, 0x4000 },
217 { 0x00e1, 0x3131 },
218 { 0x00e2, 0x600c },
219 { 0x00ea, 0xb320 },
220 { 0x00eb, 0x0000 },
221 { 0x00ec, 0xb300 },
222 { 0x00ed, 0x0000 },
223 { 0x00ee, 0xb320 },
224 { 0x00ef, 0x0000 },
225 { 0x00f0, 0x0201 },
226 { 0x00f1, 0x0ddd },
227 { 0x00f2, 0x0ddd },
228 { 0x00f6, 0x0000 },
229 { 0x00f7, 0x0000 },
230 { 0x00f8, 0x0000 },
231 { 0x00fa, 0x0000 },
232 { 0x00fb, 0x0000 },
233 { 0x00fc, 0x0000 },
234 { 0x00fd, 0x0000 },
235 { 0x00fe, 0x10ec },
236 { 0x00ff, 0x6451 },
237 { 0x0100, 0xaaaa },
238 { 0x0101, 0x000a },
239 { 0x010a, 0xaaaa },
240 { 0x010b, 0xa0a0 },
241 { 0x010c, 0xaeae },
242 { 0x010d, 0xaaaa },
243 { 0x010e, 0xaaaa },
244 { 0x010f, 0xaaaa },
245 { 0x0110, 0xe002 },
246 { 0x0111, 0xa602 },
247 { 0x0112, 0xaaaa },
248 { 0x0113, 0x2000 },
249 { 0x0117, 0x0f00 },
250 { 0x0125, 0x0420 },
251 { 0x0132, 0x0000 },
252 { 0x0133, 0x0000 },
253 { 0x0136, 0x5555 },
254 { 0x0137, 0x5540 },
255 { 0x0138, 0x3700 },
256 { 0x0139, 0x79a1 },
257 { 0x013a, 0x2020 },
258 { 0x013b, 0x2020 },
259 { 0x013c, 0x2005 },
260 { 0x013f, 0x0000 },
261 { 0x0145, 0x0002 },
262 { 0x0146, 0x0000 },
263 { 0x0147, 0x0000 },
264 { 0x0148, 0x0000 },
265 { 0x0160, 0x4ec0 },
266 { 0x0161, 0x0080 },
267 { 0x0162, 0x0200 },
268 { 0x0163, 0x0800 },
269 { 0x0164, 0x0000 },
270 { 0x0165, 0x0000 },
271 { 0x0166, 0x0000 },
272 { 0x0167, 0x000f },
273 { 0x0168, 0x000f },
274 { 0x0170, 0x4e80 },
275 { 0x0171, 0x0080 },
276 { 0x0172, 0x0200 },
277 { 0x0173, 0x0800 },
278 { 0x0174, 0x00ff },
279 { 0x0175, 0x0000 },
280 { 0x0190, 0x4131 },
281 { 0x0191, 0x4131 },
282 { 0x0192, 0x4131 },
283 { 0x0193, 0x4131 },
284 { 0x0194, 0x0000 },
285 { 0x0195, 0x0000 },
286 { 0x0196, 0x0000 },
287 { 0x0197, 0x0000 },
288 { 0x0198, 0x0000 },
289 { 0x0199, 0x0000 },
290 { 0x01a0, 0x1e64 },
291 { 0x01a1, 0x06a3 },
292 { 0x01a2, 0x0000 },
293 { 0x01a3, 0x0000 },
294 { 0x01a4, 0x0000 },
295 { 0x01a5, 0x0000 },
296 { 0x01a6, 0x0000 },
297 { 0x01a7, 0x0000 },
298 { 0x01a8, 0x0000 },
299 { 0x01a9, 0x0000 },
300 { 0x01aa, 0x0000 },
301 { 0x01ab, 0x0000 },
302 { 0x01b5, 0x0000 },
303 { 0x01b6, 0x01c3 },
304 { 0x01b7, 0x02a0 },
305 { 0x01b8, 0x03e9 },
306 { 0x01b9, 0x1389 },
307 { 0x01ba, 0xc351 },
308 { 0x01bb, 0x0009 },
309 { 0x01bc, 0x0018 },
310 { 0x01bd, 0x002a },
311 { 0x01be, 0x004c },
312 { 0x01bf, 0x0097 },
313 { 0x01c0, 0x433d },
314 { 0x01c1, 0x0000 },
315 { 0x01c2, 0x0000 },
316 { 0x01c3, 0x0000 },
317 { 0x01c4, 0x0000 },
318 { 0x01c5, 0x0000 },
319 { 0x01c6, 0x0000 },
320 { 0x01c7, 0x0000 },
321 { 0x01c8, 0x40af },
322 { 0x01c9, 0x0702 },
323 { 0x01ca, 0x0000 },
324 { 0x01cb, 0x0000 },
325 { 0x01cc, 0x5757 },
326 { 0x01cd, 0x5757 },
327 { 0x01ce, 0x5757 },
328 { 0x01cf, 0x5757 },
329 { 0x01d0, 0x5757 },
330 { 0x01d1, 0x5757 },
331 { 0x01d2, 0x5757 },
332 { 0x01d3, 0x5757 },
333 { 0x01d4, 0x5757 },
334 { 0x01d5, 0x5757 },
335 { 0x01d6, 0x003c },
336 { 0x01da, 0x0000 },
337 { 0x01db, 0x0000 },
338 { 0x01dc, 0x0000 },
339 { 0x01de, 0x7c00 },
340 { 0x01df, 0x0320 },
341 { 0x01e0, 0x06a1 },
342 { 0x01e1, 0x0000 },
343 { 0x01e2, 0x0000 },
344 { 0x01e3, 0x0000 },
345 { 0x01e4, 0x0000 },
346 { 0x01e5, 0x0000 },
347 { 0x01e6, 0x0001 },
348 { 0x01e7, 0x0000 },
349 { 0x01e8, 0x0000 },
350 { 0x01ea, 0x0000 },
351 { 0x01eb, 0x0000 },
352 { 0x01ec, 0x0000 },
353 { 0x01ed, 0x0000 },
354 { 0x01ee, 0x0000 },
355 { 0x01ef, 0x0000 },
356 { 0x01f0, 0x0000 },
357 { 0x01f1, 0x0000 },
358 { 0x01f2, 0x0000 },
359 { 0x01f3, 0x0000 },
360 { 0x01f4, 0x0000 },
361 { 0x0200, 0x0000 },
362 { 0x0201, 0x0000 },
363 { 0x0202, 0x0000 },
364 { 0x0203, 0x0000 },
365 { 0x0204, 0x0000 },
366 { 0x0205, 0x0000 },
367 { 0x0206, 0x0000 },
368 { 0x0207, 0x0000 },
369 { 0x0208, 0x0000 },
370 { 0x0210, 0x60b1 },
371 { 0x0211, 0xa000 },
372 { 0x0212, 0x024c },
373 { 0x0213, 0xf7ff },
374 { 0x0214, 0x024c },
375 { 0x0215, 0x0102 },
376 { 0x0216, 0x00a3 },
377 { 0x0217, 0x0048 },
378 { 0x0218, 0x92c0 },
379 { 0x0219, 0x0000 },
380 { 0x021a, 0x00c8 },
381 { 0x021b, 0x0020 },
382 { 0x02fa, 0x0000 },
383 { 0x02fb, 0x0000 },
384 { 0x02fc, 0x0000 },
385 { 0x02ff, 0x0110 },
386 { 0x0300, 0x001f },
387 { 0x0301, 0x032c },
388 { 0x0302, 0x5f21 },
389 { 0x0303, 0x4000 },
390 { 0x0304, 0x4000 },
391 { 0x0305, 0x06d5 },
392 { 0x0306, 0x8000 },
393 { 0x0307, 0x0700 },
394 { 0x0310, 0x4560 },
395 { 0x0311, 0xa4a8 },
396 { 0x0312, 0x7418 },
397 { 0x0313, 0x0000 },
398 { 0x0314, 0x0006 },
399 { 0x0315, 0xffff },
400 { 0x0316, 0xc400 },
401 { 0x0317, 0x0000 },
402 { 0x0330, 0x00a6 },
403 { 0x0331, 0x04c3 },
404 { 0x0332, 0x27c8 },
405 { 0x0333, 0xbf50 },
406 { 0x0334, 0x0045 },
407 { 0x0335, 0x0007 },
408 { 0x0336, 0x7418 },
409 { 0x0337, 0x0501 },
410 { 0x0338, 0x0000 },
411 { 0x0339, 0x0010 },
412 { 0x033a, 0x1010 },
413 { 0x03c0, 0x7e00 },
414 { 0x03c1, 0x8000 },
415 { 0x03c2, 0x8000 },
416 { 0x03c3, 0x8000 },
417 { 0x03c4, 0x8000 },
418 { 0x03c5, 0x8000 },
419 { 0x03c6, 0x8000 },
420 { 0x03c7, 0x8000 },
421 { 0x03c8, 0x8000 },
422 { 0x03c9, 0x8000 },
423 { 0x03ca, 0x8000 },
424 { 0x03cb, 0x8000 },
425 { 0x03cc, 0x8000 },
426 { 0x03d0, 0x0000 },
427 { 0x03d1, 0x0000 },
428 { 0x03d2, 0x0000 },
429 { 0x03d3, 0x0000 },
430 { 0x03d4, 0x2000 },
431 { 0x03d5, 0x2000 },
432 { 0x03d6, 0x0000 },
433 { 0x03d7, 0x0000 },
434 { 0x03d8, 0x2000 },
435 { 0x03d9, 0x2000 },
436 { 0x03da, 0x2000 },
437 { 0x03db, 0x2000 },
438 { 0x03dc, 0x0000 },
439 { 0x03dd, 0x0000 },
440 { 0x03de, 0x0000 },
441 { 0x03df, 0x2000 },
442 { 0x03e0, 0x0000 },
443 { 0x03e1, 0x0000 },
444 { 0x03e2, 0x0000 },
445 { 0x03e3, 0x0000 },
446 { 0x03e4, 0x0000 },
447 { 0x03e5, 0x0000 },
448 { 0x03e6, 0x0000 },
449 { 0x03e7, 0x0000 },
450 { 0x03e8, 0x0000 },
451 { 0x03e9, 0x0000 },
452 { 0x03ea, 0x0000 },
453 { 0x03eb, 0x0000 },
454 { 0x03ec, 0x0000 },
455 { 0x03ed, 0x0000 },
456 { 0x03ee, 0x0000 },
457 { 0x03ef, 0x0000 },
458 { 0x03f0, 0x0800 },
459 { 0x03f1, 0x0800 },
460 { 0x03f2, 0x0800 },
461 { 0x03f3, 0x0800 },
462 { 0x03fe, 0x0000 },
463 { 0x03ff, 0x0000 },
464 { 0x07f0, 0x0000 },
465 { 0x07fa, 0x0000 },
466};
467
468static const struct reg_default rt5663_reg[] = {
469 { 0x0000, 0x0000 },
470 { 0x0002, 0x0008 },
471 { 0x0005, 0x1000 },
472 { 0x0006, 0x1000 },
473 { 0x000a, 0x0000 },
474 { 0x0010, 0x000f },
475 { 0x0015, 0x42c1 },
476 { 0x0016, 0x0000 },
477 { 0x0018, 0x000b },
478 { 0x0019, 0xafaf },
479 { 0x001c, 0x2f2f },
480 { 0x001f, 0x0000 },
481 { 0x0022, 0x5757 },
482 { 0x0023, 0x0039 },
483 { 0x0026, 0xc0c0 },
484 { 0x0029, 0x8080 },
oder_chiou@realtek.com1d5c5b62017-07-19 10:20:23 +0800485 { 0x002a, 0x8020 },
Bard Liaodf7c5212016-09-09 10:33:10 +0800486 { 0x002c, 0x000c },
487 { 0x002d, 0x0000 },
488 { 0x0040, 0x0808 },
489 { 0x0061, 0x0000 },
490 { 0x0062, 0x0000 },
491 { 0x0063, 0x003e },
492 { 0x0064, 0x0000 },
493 { 0x0065, 0x0000 },
494 { 0x0066, 0x0000 },
495 { 0x006b, 0x0000 },
496 { 0x006e, 0x0000 },
497 { 0x006f, 0x0000 },
498 { 0x0070, 0x8020 },
499 { 0x0073, 0x1000 },
500 { 0x0074, 0xe400 },
501 { 0x0075, 0x0002 },
502 { 0x0076, 0x0001 },
503 { 0x0077, 0x00f0 },
504 { 0x0078, 0x0000 },
505 { 0x0079, 0x0000 },
506 { 0x007a, 0x0123 },
507 { 0x007b, 0x8003 },
508 { 0x0080, 0x0000 },
509 { 0x0081, 0x0000 },
510 { 0x0082, 0x0000 },
511 { 0x0083, 0x0000 },
512 { 0x0084, 0x0000 },
oder_chiou@realtek.comd26ed932017-07-18 10:12:58 +0800513 { 0x0086, 0x0028 },
Bard Liaodf7c5212016-09-09 10:33:10 +0800514 { 0x0087, 0x0000 },
515 { 0x008a, 0x0000 },
516 { 0x008b, 0x0000 },
517 { 0x008c, 0x0003 },
518 { 0x008e, 0x0004 },
519 { 0x008f, 0x1000 },
520 { 0x0090, 0x0646 },
521 { 0x0091, 0x0e3e },
522 { 0x0092, 0x1071 },
523 { 0x0093, 0x0000 },
524 { 0x0094, 0x0080 },
525 { 0x0097, 0x0000 },
526 { 0x0098, 0x0000 },
527 { 0x009a, 0x0000 },
528 { 0x009f, 0x0000 },
529 { 0x00ae, 0x2000 },
530 { 0x00af, 0x0000 },
531 { 0x00b6, 0x0000 },
532 { 0x00b7, 0x0000 },
533 { 0x00b8, 0x0000 },
534 { 0x00ba, 0x0000 },
535 { 0x00bb, 0x0000 },
536 { 0x00be, 0x0000 },
537 { 0x00bf, 0x0000 },
538 { 0x00c0, 0x0000 },
539 { 0x00c1, 0x0000 },
540 { 0x00c5, 0x0000 },
541 { 0x00cb, 0xa02f },
542 { 0x00cc, 0x0000 },
543 { 0x00cd, 0x0e02 },
544 { 0x00d9, 0x08f9 },
545 { 0x00db, 0x0008 },
546 { 0x00dc, 0x00c0 },
547 { 0x00dd, 0x6724 },
548 { 0x00de, 0x3131 },
549 { 0x00df, 0x0008 },
550 { 0x00e0, 0x4000 },
551 { 0x00e1, 0x3131 },
552 { 0x00e2, 0x0043 },
553 { 0x00e4, 0x400b },
554 { 0x00e5, 0x8031 },
555 { 0x00e6, 0x3080 },
556 { 0x00e7, 0x4100 },
557 { 0x00e8, 0x1400 },
558 { 0x00e9, 0xe00a },
559 { 0x00ea, 0x0404 },
560 { 0x00eb, 0x0404 },
561 { 0x00ec, 0xb320 },
562 { 0x00ed, 0x0000 },
563 { 0x00f4, 0x0000 },
564 { 0x00f6, 0x0000 },
565 { 0x00f8, 0x0000 },
566 { 0x00fa, 0x8000 },
567 { 0x00fd, 0x0001 },
568 { 0x00fe, 0x10ec },
569 { 0x00ff, 0x6406 },
570 { 0x0100, 0xa0a0 },
571 { 0x0108, 0x4444 },
572 { 0x0109, 0x4444 },
573 { 0x010a, 0xaaaa },
574 { 0x010b, 0x00a0 },
575 { 0x010c, 0x8aaa },
576 { 0x010d, 0xaaaa },
577 { 0x010e, 0x2aaa },
578 { 0x010f, 0x002a },
579 { 0x0110, 0xa0a4 },
580 { 0x0111, 0x4602 },
581 { 0x0112, 0x0101 },
582 { 0x0113, 0x2000 },
583 { 0x0114, 0x0000 },
584 { 0x0116, 0x0000 },
585 { 0x0117, 0x0f00 },
586 { 0x0118, 0x0006 },
587 { 0x0125, 0x2224 },
588 { 0x0126, 0x5550 },
589 { 0x0127, 0x0400 },
590 { 0x0128, 0x7711 },
591 { 0x0132, 0x0004 },
592 { 0x0137, 0x5441 },
593 { 0x0139, 0x79a1 },
594 { 0x013a, 0x30c0 },
595 { 0x013b, 0x2000 },
596 { 0x013c, 0x2005 },
597 { 0x013d, 0x30c0 },
598 { 0x013e, 0x0000 },
599 { 0x0140, 0x3700 },
600 { 0x0141, 0x1f00 },
601 { 0x0144, 0x0000 },
602 { 0x0145, 0x0002 },
603 { 0x0146, 0x0000 },
604 { 0x0160, 0x0e80 },
605 { 0x0161, 0x0020 },
606 { 0x0162, 0x0080 },
607 { 0x0163, 0x0800 },
608 { 0x0164, 0x0000 },
609 { 0x0165, 0x0000 },
610 { 0x0166, 0x0000 },
611 { 0x0167, 0x1417 },
612 { 0x0168, 0x0017 },
613 { 0x0169, 0x0017 },
614 { 0x0180, 0x2000 },
615 { 0x0181, 0x0000 },
616 { 0x0182, 0x0000 },
617 { 0x0183, 0x2000 },
618 { 0x0184, 0x0000 },
619 { 0x0185, 0x0000 },
620 { 0x01b0, 0x4b30 },
621 { 0x01b1, 0x0000 },
622 { 0x01b2, 0xd870 },
623 { 0x01b3, 0x0000 },
624 { 0x01b4, 0x0030 },
625 { 0x01b5, 0x5757 },
626 { 0x01b6, 0x5757 },
627 { 0x01b7, 0x5757 },
628 { 0x01b8, 0x5757 },
629 { 0x01c0, 0x433d },
630 { 0x01c1, 0x0540 },
631 { 0x01c2, 0x0000 },
632 { 0x01c3, 0x0000 },
633 { 0x01c4, 0x0000 },
634 { 0x01c5, 0x0009 },
635 { 0x01c6, 0x0018 },
636 { 0x01c7, 0x002a },
637 { 0x01c8, 0x004c },
638 { 0x01c9, 0x0097 },
639 { 0x01ca, 0x01c3 },
640 { 0x01cb, 0x03e9 },
641 { 0x01cc, 0x1389 },
642 { 0x01cd, 0xc351 },
643 { 0x01ce, 0x0000 },
644 { 0x01cf, 0x0000 },
645 { 0x01d0, 0x0000 },
646 { 0x01d1, 0x0000 },
647 { 0x01d2, 0x0000 },
648 { 0x01d3, 0x003c },
649 { 0x01d4, 0x5757 },
650 { 0x01d5, 0x5757 },
651 { 0x01d6, 0x5757 },
652 { 0x01d7, 0x5757 },
653 { 0x01d8, 0x5757 },
654 { 0x01d9, 0x5757 },
655 { 0x01da, 0x0000 },
656 { 0x01db, 0x0000 },
657 { 0x01dd, 0x0009 },
658 { 0x01de, 0x7f00 },
659 { 0x01df, 0x00c8 },
660 { 0x01e0, 0x0691 },
661 { 0x01e1, 0x0000 },
662 { 0x01e2, 0x0000 },
663 { 0x01e3, 0x0000 },
664 { 0x01e4, 0x0000 },
665 { 0x01e5, 0x0040 },
666 { 0x01e6, 0x0000 },
667 { 0x01e7, 0x0000 },
668 { 0x01e8, 0x0000 },
669 { 0x01ea, 0x0000 },
670 { 0x01eb, 0x0000 },
671 { 0x01ec, 0x0000 },
672 { 0x01ed, 0x0000 },
673 { 0x01ee, 0x0000 },
674 { 0x01ef, 0x0000 },
675 { 0x01f0, 0x0000 },
676 { 0x01f1, 0x0000 },
677 { 0x01f2, 0x0000 },
678 { 0x0200, 0x0000 },
679 { 0x0201, 0x2244 },
680 { 0x0202, 0xaaaa },
681 { 0x0250, 0x8010 },
682 { 0x0251, 0x0000 },
683 { 0x0252, 0x028a },
684 { 0x02fa, 0x0000 },
685 { 0x02fb, 0x0000 },
686 { 0x02fc, 0x0000 },
687 { 0x0300, 0x0000 },
688 { 0x03d0, 0x0000 },
689 { 0x03d1, 0x0000 },
690 { 0x03d2, 0x0000 },
691 { 0x03d3, 0x0000 },
692 { 0x03d4, 0x2000 },
693 { 0x03d5, 0x2000 },
694 { 0x03d6, 0x0000 },
695 { 0x03d7, 0x0000 },
696 { 0x03d8, 0x2000 },
697 { 0x03d9, 0x2000 },
698 { 0x03da, 0x2000 },
699 { 0x03db, 0x2000 },
700 { 0x03dc, 0x0000 },
701 { 0x03dd, 0x0000 },
702 { 0x03de, 0x0000 },
703 { 0x03df, 0x2000 },
704 { 0x03e0, 0x0000 },
705 { 0x03e1, 0x0000 },
706 { 0x03e2, 0x0000 },
707 { 0x03e3, 0x0000 },
708 { 0x03e4, 0x0000 },
709 { 0x03e5, 0x0000 },
710 { 0x03e6, 0x0000 },
711 { 0x03e7, 0x0000 },
712 { 0x03e8, 0x0000 },
713 { 0x03e9, 0x0000 },
714 { 0x03ea, 0x0000 },
715 { 0x03eb, 0x0000 },
716 { 0x03ec, 0x0000 },
717 { 0x03ed, 0x0000 },
718 { 0x03ee, 0x0000 },
719 { 0x03ef, 0x0000 },
720 { 0x03f0, 0x0800 },
721 { 0x03f1, 0x0800 },
722 { 0x03f2, 0x0800 },
723 { 0x03f3, 0x0800 },
724};
725
726static bool rt5663_volatile_register(struct device *dev, unsigned int reg)
727{
728 switch (reg) {
729 case RT5663_RESET:
730 case RT5663_SIL_DET_CTL:
731 case RT5663_HP_IMP_GAIN_2:
732 case RT5663_AD_DA_MIXER:
733 case RT5663_FRAC_DIV_2:
734 case RT5663_MICBIAS_1:
735 case RT5663_ASRC_11_2:
736 case RT5663_ADC_EQ_1:
737 case RT5663_INT_ST_1:
738 case RT5663_INT_ST_2:
Bard Liao73444722016-10-21 11:02:28 +0800739 case RT5663_GPIO_STA1:
Bard Liaodf7c5212016-09-09 10:33:10 +0800740 case RT5663_SIN_GEN_1:
741 case RT5663_IL_CMD_1:
742 case RT5663_IL_CMD_5:
743 case RT5663_IL_CMD_PWRSAV1:
744 case RT5663_EM_JACK_TYPE_1:
745 case RT5663_EM_JACK_TYPE_2:
746 case RT5663_EM_JACK_TYPE_3:
747 case RT5663_JD_CTRL2:
748 case RT5663_VENDOR_ID:
749 case RT5663_VENDOR_ID_1:
750 case RT5663_VENDOR_ID_2:
751 case RT5663_PLL_INT_REG:
752 case RT5663_SOFT_RAMP:
753 case RT5663_STO_DRE_1:
754 case RT5663_STO_DRE_5:
755 case RT5663_STO_DRE_6:
756 case RT5663_STO_DRE_7:
757 case RT5663_MIC_DECRO_1:
758 case RT5663_MIC_DECRO_4:
759 case RT5663_HP_IMP_SEN_1:
760 case RT5663_HP_IMP_SEN_3:
761 case RT5663_HP_IMP_SEN_4:
762 case RT5663_HP_IMP_SEN_5:
763 case RT5663_HP_CALIB_1_1:
764 case RT5663_HP_CALIB_9:
765 case RT5663_HP_CALIB_ST1:
766 case RT5663_HP_CALIB_ST2:
767 case RT5663_HP_CALIB_ST3:
768 case RT5663_HP_CALIB_ST4:
769 case RT5663_HP_CALIB_ST5:
770 case RT5663_HP_CALIB_ST6:
771 case RT5663_HP_CALIB_ST7:
772 case RT5663_HP_CALIB_ST8:
773 case RT5663_HP_CALIB_ST9:
774 case RT5663_ANA_JD:
775 return true;
776 default:
777 return false;
778 }
779}
780
781static bool rt5663_readable_register(struct device *dev, unsigned int reg)
782{
783 switch (reg) {
784 case RT5663_RESET:
785 case RT5663_HP_OUT_EN:
786 case RT5663_HP_LCH_DRE:
787 case RT5663_HP_RCH_DRE:
788 case RT5663_CALIB_BST:
789 case RT5663_RECMIX:
790 case RT5663_SIL_DET_CTL:
791 case RT5663_PWR_SAV_SILDET:
792 case RT5663_SIDETONE_CTL:
793 case RT5663_STO1_DAC_DIG_VOL:
794 case RT5663_STO1_ADC_DIG_VOL:
795 case RT5663_STO1_BOOST:
796 case RT5663_HP_IMP_GAIN_1:
797 case RT5663_HP_IMP_GAIN_2:
798 case RT5663_STO1_ADC_MIXER:
799 case RT5663_AD_DA_MIXER:
800 case RT5663_STO_DAC_MIXER:
801 case RT5663_DIG_SIDE_MIXER:
802 case RT5663_BYPASS_STO_DAC:
803 case RT5663_CALIB_REC_MIX:
804 case RT5663_PWR_DIG_1:
805 case RT5663_PWR_DIG_2:
806 case RT5663_PWR_ANLG_1:
807 case RT5663_PWR_ANLG_2:
808 case RT5663_PWR_ANLG_3:
809 case RT5663_PWR_MIXER:
810 case RT5663_SIG_CLK_DET:
811 case RT5663_PRE_DIV_GATING_1:
812 case RT5663_PRE_DIV_GATING_2:
813 case RT5663_I2S1_SDP:
814 case RT5663_ADDA_CLK_1:
815 case RT5663_ADDA_RST:
816 case RT5663_FRAC_DIV_1:
817 case RT5663_FRAC_DIV_2:
818 case RT5663_TDM_1:
819 case RT5663_TDM_2:
820 case RT5663_TDM_3:
821 case RT5663_TDM_4:
822 case RT5663_TDM_5:
823 case RT5663_GLB_CLK:
824 case RT5663_PLL_1:
825 case RT5663_PLL_2:
826 case RT5663_ASRC_1:
827 case RT5663_ASRC_2:
828 case RT5663_ASRC_4:
829 case RT5663_DUMMY_REG:
830 case RT5663_ASRC_8:
831 case RT5663_ASRC_9:
832 case RT5663_ASRC_11:
833 case RT5663_DEPOP_1:
834 case RT5663_DEPOP_2:
835 case RT5663_DEPOP_3:
836 case RT5663_HP_CHARGE_PUMP_1:
837 case RT5663_HP_CHARGE_PUMP_2:
838 case RT5663_MICBIAS_1:
839 case RT5663_RC_CLK:
840 case RT5663_ASRC_11_2:
841 case RT5663_DUMMY_REG_2:
842 case RT5663_REC_PATH_GAIN:
843 case RT5663_AUTO_1MRC_CLK:
844 case RT5663_ADC_EQ_1:
845 case RT5663_ADC_EQ_2:
846 case RT5663_IRQ_1:
847 case RT5663_IRQ_2:
848 case RT5663_IRQ_3:
849 case RT5663_IRQ_4:
850 case RT5663_IRQ_5:
851 case RT5663_INT_ST_1:
852 case RT5663_INT_ST_2:
853 case RT5663_GPIO_1:
854 case RT5663_GPIO_2:
Bard Liao73444722016-10-21 11:02:28 +0800855 case RT5663_GPIO_STA1:
Bard Liaodf7c5212016-09-09 10:33:10 +0800856 case RT5663_SIN_GEN_1:
857 case RT5663_SIN_GEN_2:
858 case RT5663_SIN_GEN_3:
859 case RT5663_SOF_VOL_ZC1:
860 case RT5663_IL_CMD_1:
861 case RT5663_IL_CMD_2:
862 case RT5663_IL_CMD_3:
863 case RT5663_IL_CMD_4:
864 case RT5663_IL_CMD_5:
865 case RT5663_IL_CMD_6:
866 case RT5663_IL_CMD_7:
867 case RT5663_IL_CMD_8:
868 case RT5663_IL_CMD_PWRSAV1:
869 case RT5663_IL_CMD_PWRSAV2:
870 case RT5663_EM_JACK_TYPE_1:
871 case RT5663_EM_JACK_TYPE_2:
872 case RT5663_EM_JACK_TYPE_3:
873 case RT5663_EM_JACK_TYPE_4:
874 case RT5663_EM_JACK_TYPE_5:
875 case RT5663_EM_JACK_TYPE_6:
876 case RT5663_STO1_HPF_ADJ1:
877 case RT5663_STO1_HPF_ADJ2:
878 case RT5663_FAST_OFF_MICBIAS:
879 case RT5663_JD_CTRL1:
880 case RT5663_JD_CTRL2:
881 case RT5663_DIG_MISC:
882 case RT5663_VENDOR_ID:
883 case RT5663_VENDOR_ID_1:
884 case RT5663_VENDOR_ID_2:
885 case RT5663_DIG_VOL_ZCD:
886 case RT5663_ANA_BIAS_CUR_1:
887 case RT5663_ANA_BIAS_CUR_2:
888 case RT5663_ANA_BIAS_CUR_3:
889 case RT5663_ANA_BIAS_CUR_4:
890 case RT5663_ANA_BIAS_CUR_5:
891 case RT5663_ANA_BIAS_CUR_6:
892 case RT5663_BIAS_CUR_5:
893 case RT5663_BIAS_CUR_6:
894 case RT5663_BIAS_CUR_7:
895 case RT5663_BIAS_CUR_8:
896 case RT5663_DACREF_LDO:
897 case RT5663_DUMMY_REG_3:
898 case RT5663_BIAS_CUR_9:
899 case RT5663_DUMMY_REG_4:
900 case RT5663_VREFADJ_OP:
901 case RT5663_VREF_RECMIX:
902 case RT5663_CHARGE_PUMP_1:
903 case RT5663_CHARGE_PUMP_1_2:
904 case RT5663_CHARGE_PUMP_1_3:
905 case RT5663_CHARGE_PUMP_2:
906 case RT5663_DIG_IN_PIN1:
907 case RT5663_PAD_DRV_CTL:
908 case RT5663_PLL_INT_REG:
909 case RT5663_CHOP_DAC_L:
910 case RT5663_CHOP_ADC:
911 case RT5663_CALIB_ADC:
912 case RT5663_CHOP_DAC_R:
913 case RT5663_DUMMY_CTL_DACLR:
914 case RT5663_DUMMY_REG_5:
915 case RT5663_SOFT_RAMP:
916 case RT5663_TEST_MODE_1:
917 case RT5663_TEST_MODE_2:
918 case RT5663_TEST_MODE_3:
919 case RT5663_STO_DRE_1:
920 case RT5663_STO_DRE_2:
921 case RT5663_STO_DRE_3:
922 case RT5663_STO_DRE_4:
923 case RT5663_STO_DRE_5:
924 case RT5663_STO_DRE_6:
925 case RT5663_STO_DRE_7:
926 case RT5663_STO_DRE_8:
927 case RT5663_STO_DRE_9:
928 case RT5663_STO_DRE_10:
929 case RT5663_MIC_DECRO_1:
930 case RT5663_MIC_DECRO_2:
931 case RT5663_MIC_DECRO_3:
932 case RT5663_MIC_DECRO_4:
933 case RT5663_MIC_DECRO_5:
934 case RT5663_MIC_DECRO_6:
935 case RT5663_HP_DECRO_1:
936 case RT5663_HP_DECRO_2:
937 case RT5663_HP_DECRO_3:
938 case RT5663_HP_DECRO_4:
939 case RT5663_HP_DECOUP:
940 case RT5663_HP_IMP_SEN_MAP8:
941 case RT5663_HP_IMP_SEN_MAP9:
942 case RT5663_HP_IMP_SEN_MAP10:
943 case RT5663_HP_IMP_SEN_MAP11:
944 case RT5663_HP_IMP_SEN_1:
945 case RT5663_HP_IMP_SEN_2:
946 case RT5663_HP_IMP_SEN_3:
947 case RT5663_HP_IMP_SEN_4:
948 case RT5663_HP_IMP_SEN_5:
949 case RT5663_HP_IMP_SEN_6:
950 case RT5663_HP_IMP_SEN_7:
951 case RT5663_HP_IMP_SEN_8:
952 case RT5663_HP_IMP_SEN_9:
953 case RT5663_HP_IMP_SEN_10:
954 case RT5663_HP_IMP_SEN_11:
955 case RT5663_HP_IMP_SEN_12:
956 case RT5663_HP_IMP_SEN_13:
957 case RT5663_HP_IMP_SEN_14:
958 case RT5663_HP_IMP_SEN_15:
959 case RT5663_HP_IMP_SEN_16:
960 case RT5663_HP_IMP_SEN_17:
961 case RT5663_HP_IMP_SEN_18:
962 case RT5663_HP_IMP_SEN_19:
963 case RT5663_HP_IMPSEN_DIG5:
964 case RT5663_HP_IMPSEN_MAP1:
965 case RT5663_HP_IMPSEN_MAP2:
966 case RT5663_HP_IMPSEN_MAP3:
967 case RT5663_HP_IMPSEN_MAP4:
968 case RT5663_HP_IMPSEN_MAP5:
969 case RT5663_HP_IMPSEN_MAP7:
970 case RT5663_HP_LOGIC_1:
971 case RT5663_HP_LOGIC_2:
972 case RT5663_HP_CALIB_1:
973 case RT5663_HP_CALIB_1_1:
974 case RT5663_HP_CALIB_2:
975 case RT5663_HP_CALIB_3:
976 case RT5663_HP_CALIB_4:
977 case RT5663_HP_CALIB_5:
978 case RT5663_HP_CALIB_5_1:
979 case RT5663_HP_CALIB_6:
980 case RT5663_HP_CALIB_7:
981 case RT5663_HP_CALIB_9:
982 case RT5663_HP_CALIB_10:
983 case RT5663_HP_CALIB_11:
984 case RT5663_HP_CALIB_ST1:
985 case RT5663_HP_CALIB_ST2:
986 case RT5663_HP_CALIB_ST3:
987 case RT5663_HP_CALIB_ST4:
988 case RT5663_HP_CALIB_ST5:
989 case RT5663_HP_CALIB_ST6:
990 case RT5663_HP_CALIB_ST7:
991 case RT5663_HP_CALIB_ST8:
992 case RT5663_HP_CALIB_ST9:
993 case RT5663_HP_AMP_DET:
994 case RT5663_DUMMY_REG_6:
995 case RT5663_HP_BIAS:
996 case RT5663_CBJ_1:
997 case RT5663_CBJ_2:
998 case RT5663_CBJ_3:
999 case RT5663_DUMMY_1:
1000 case RT5663_DUMMY_2:
1001 case RT5663_DUMMY_3:
1002 case RT5663_ANA_JD:
1003 case RT5663_ADC_LCH_LPF1_A1:
1004 case RT5663_ADC_RCH_LPF1_A1:
1005 case RT5663_ADC_LCH_LPF1_H0:
1006 case RT5663_ADC_RCH_LPF1_H0:
1007 case RT5663_ADC_LCH_BPF1_A1:
1008 case RT5663_ADC_RCH_BPF1_A1:
1009 case RT5663_ADC_LCH_BPF1_A2:
1010 case RT5663_ADC_RCH_BPF1_A2:
1011 case RT5663_ADC_LCH_BPF1_H0:
1012 case RT5663_ADC_RCH_BPF1_H0:
1013 case RT5663_ADC_LCH_BPF2_A1:
1014 case RT5663_ADC_RCH_BPF2_A1:
1015 case RT5663_ADC_LCH_BPF2_A2:
1016 case RT5663_ADC_RCH_BPF2_A2:
1017 case RT5663_ADC_LCH_BPF2_H0:
1018 case RT5663_ADC_RCH_BPF2_H0:
1019 case RT5663_ADC_LCH_BPF3_A1:
1020 case RT5663_ADC_RCH_BPF3_A1:
1021 case RT5663_ADC_LCH_BPF3_A2:
1022 case RT5663_ADC_RCH_BPF3_A2:
1023 case RT5663_ADC_LCH_BPF3_H0:
1024 case RT5663_ADC_RCH_BPF3_H0:
1025 case RT5663_ADC_LCH_BPF4_A1:
1026 case RT5663_ADC_RCH_BPF4_A1:
1027 case RT5663_ADC_LCH_BPF4_A2:
1028 case RT5663_ADC_RCH_BPF4_A2:
1029 case RT5663_ADC_LCH_BPF4_H0:
1030 case RT5663_ADC_RCH_BPF4_H0:
1031 case RT5663_ADC_LCH_HPF1_A1:
1032 case RT5663_ADC_RCH_HPF1_A1:
1033 case RT5663_ADC_LCH_HPF1_H0:
1034 case RT5663_ADC_RCH_HPF1_H0:
1035 case RT5663_ADC_EQ_PRE_VOL_L:
1036 case RT5663_ADC_EQ_PRE_VOL_R:
1037 case RT5663_ADC_EQ_POST_VOL_L:
1038 case RT5663_ADC_EQ_POST_VOL_R:
1039 return true;
1040 default:
1041 return false;
1042 }
1043}
1044
Bard Liao73444722016-10-21 11:02:28 +08001045static bool rt5663_v2_volatile_register(struct device *dev, unsigned int reg)
Bard Liaodf7c5212016-09-09 10:33:10 +08001046{
1047 switch (reg) {
1048 case RT5663_RESET:
Bard Liao73444722016-10-21 11:02:28 +08001049 case RT5663_CBJ_TYPE_2:
1050 case RT5663_PDM_OUT_CTL:
1051 case RT5663_PDM_I2C_DATA_CTL1:
1052 case RT5663_PDM_I2C_DATA_CTL4:
1053 case RT5663_ALC_BK_GAIN:
Bard Liaodf7c5212016-09-09 10:33:10 +08001054 case RT5663_PLL_2:
1055 case RT5663_MICBIAS_1:
1056 case RT5663_ADC_EQ_1:
1057 case RT5663_INT_ST_1:
Bard Liao73444722016-10-21 11:02:28 +08001058 case RT5663_GPIO_STA2:
Bard Liaodf7c5212016-09-09 10:33:10 +08001059 case RT5663_IL_CMD_1:
1060 case RT5663_IL_CMD_5:
Bard Liao73444722016-10-21 11:02:28 +08001061 case RT5663_A_JD_CTRL:
Bard Liaodf7c5212016-09-09 10:33:10 +08001062 case RT5663_JD_CTRL2:
1063 case RT5663_VENDOR_ID:
1064 case RT5663_VENDOR_ID_1:
1065 case RT5663_VENDOR_ID_2:
1066 case RT5663_STO_DRE_1:
1067 case RT5663_STO_DRE_5:
1068 case RT5663_STO_DRE_6:
1069 case RT5663_STO_DRE_7:
Bard Liao73444722016-10-21 11:02:28 +08001070 case RT5663_MONO_DYNA_6:
1071 case RT5663_STO1_SIL_DET:
1072 case RT5663_MONOL_SIL_DET:
1073 case RT5663_MONOR_SIL_DET:
1074 case RT5663_STO2_DAC_SIL:
1075 case RT5663_MONO_AMP_CAL_ST1:
1076 case RT5663_MONO_AMP_CAL_ST2:
1077 case RT5663_MONO_AMP_CAL_ST3:
1078 case RT5663_MONO_AMP_CAL_ST4:
Bard Liaodf7c5212016-09-09 10:33:10 +08001079 case RT5663_HP_IMP_SEN_2:
1080 case RT5663_HP_IMP_SEN_3:
1081 case RT5663_HP_IMP_SEN_4:
1082 case RT5663_HP_IMP_SEN_10:
1083 case RT5663_HP_CALIB_1:
1084 case RT5663_HP_CALIB_10:
1085 case RT5663_HP_CALIB_ST1:
1086 case RT5663_HP_CALIB_ST4:
1087 case RT5663_HP_CALIB_ST5:
1088 case RT5663_HP_CALIB_ST6:
1089 case RT5663_HP_CALIB_ST7:
1090 case RT5663_HP_CALIB_ST8:
1091 case RT5663_HP_CALIB_ST9:
Bard Liao73444722016-10-21 11:02:28 +08001092 case RT5663_HP_CALIB_ST10:
1093 case RT5663_HP_CALIB_ST11:
Bard Liaodf7c5212016-09-09 10:33:10 +08001094 return true;
1095 default:
1096 return false;
1097 }
1098}
1099
Bard Liao73444722016-10-21 11:02:28 +08001100static bool rt5663_v2_readable_register(struct device *dev, unsigned int reg)
Bard Liaodf7c5212016-09-09 10:33:10 +08001101{
1102 switch (reg) {
Bard Liao73444722016-10-21 11:02:28 +08001103 case RT5663_LOUT_CTRL:
1104 case RT5663_HP_AMP_2:
1105 case RT5663_MONO_OUT:
1106 case RT5663_MONO_GAIN:
1107 case RT5663_AEC_BST:
1108 case RT5663_IN1_IN2:
1109 case RT5663_IN3_IN4:
1110 case RT5663_INL1_INR1:
1111 case RT5663_CBJ_TYPE_2:
1112 case RT5663_CBJ_TYPE_3:
1113 case RT5663_CBJ_TYPE_4:
1114 case RT5663_CBJ_TYPE_5:
1115 case RT5663_CBJ_TYPE_8:
1116 case RT5663_DAC3_DIG_VOL:
1117 case RT5663_DAC3_CTRL:
1118 case RT5663_MONO_ADC_DIG_VOL:
1119 case RT5663_STO2_ADC_DIG_VOL:
1120 case RT5663_MONO_ADC_BST_GAIN:
1121 case RT5663_STO2_ADC_BST_GAIN:
1122 case RT5663_SIDETONE_CTRL:
1123 case RT5663_MONO1_ADC_MIXER:
1124 case RT5663_STO2_ADC_MIXER:
1125 case RT5663_MONO_DAC_MIXER:
1126 case RT5663_DAC2_SRC_CTRL:
1127 case RT5663_IF_3_4_DATA_CTL:
1128 case RT5663_IF_5_DATA_CTL:
1129 case RT5663_PDM_OUT_CTL:
1130 case RT5663_PDM_I2C_DATA_CTL1:
1131 case RT5663_PDM_I2C_DATA_CTL2:
1132 case RT5663_PDM_I2C_DATA_CTL3:
1133 case RT5663_PDM_I2C_DATA_CTL4:
1134 case RT5663_RECMIX1_NEW:
1135 case RT5663_RECMIX1L_0:
1136 case RT5663_RECMIX1L:
1137 case RT5663_RECMIX1R_0:
1138 case RT5663_RECMIX1R:
1139 case RT5663_RECMIX2_NEW:
1140 case RT5663_RECMIX2_L_2:
1141 case RT5663_RECMIX2_R:
1142 case RT5663_RECMIX2_R_2:
1143 case RT5663_CALIB_REC_LR:
1144 case RT5663_ALC_BK_GAIN:
1145 case RT5663_MONOMIX_GAIN:
1146 case RT5663_MONOMIX_IN_GAIN:
1147 case RT5663_OUT_MIXL_GAIN:
1148 case RT5663_OUT_LMIX_IN_GAIN:
1149 case RT5663_OUT_RMIX_IN_GAIN:
1150 case RT5663_OUT_RMIX_IN_GAIN1:
1151 case RT5663_LOUT_MIXER_CTRL:
1152 case RT5663_PWR_VOL:
1153 case RT5663_ADCDAC_RST:
1154 case RT5663_I2S34_SDP:
1155 case RT5663_I2S5_SDP:
1156 case RT5663_TDM_6:
1157 case RT5663_TDM_7:
1158 case RT5663_TDM_8:
1159 case RT5663_TDM_9:
1160 case RT5663_ASRC_3:
1161 case RT5663_ASRC_6:
1162 case RT5663_ASRC_7:
1163 case RT5663_PLL_TRK_13:
1164 case RT5663_I2S_M_CLK_CTL:
1165 case RT5663_FDIV_I2S34_M_CLK:
1166 case RT5663_FDIV_I2S34_M_CLK2:
1167 case RT5663_FDIV_I2S5_M_CLK:
1168 case RT5663_FDIV_I2S5_M_CLK2:
1169 case RT5663_V2_IRQ_4:
1170 case RT5663_GPIO_3:
1171 case RT5663_GPIO_4:
1172 case RT5663_GPIO_STA2:
1173 case RT5663_HP_AMP_DET1:
1174 case RT5663_HP_AMP_DET2:
1175 case RT5663_HP_AMP_DET3:
1176 case RT5663_MID_BD_HP_AMP:
1177 case RT5663_LOW_BD_HP_AMP:
1178 case RT5663_SOF_VOL_ZC2:
1179 case RT5663_ADC_STO2_ADJ1:
1180 case RT5663_ADC_STO2_ADJ2:
1181 case RT5663_A_JD_CTRL:
1182 case RT5663_JD1_TRES_CTRL:
1183 case RT5663_JD2_TRES_CTRL:
1184 case RT5663_V2_JD_CTRL2:
1185 case RT5663_DUM_REG_2:
1186 case RT5663_DUM_REG_3:
Bard Liaodf7c5212016-09-09 10:33:10 +08001187 case RT5663_VENDOR_ID:
1188 case RT5663_VENDOR_ID_1:
1189 case RT5663_VENDOR_ID_2:
Bard Liao73444722016-10-21 11:02:28 +08001190 case RT5663_DACADC_DIG_VOL2:
1191 case RT5663_DIG_IN_PIN2:
1192 case RT5663_PAD_DRV_CTL1:
1193 case RT5663_SOF_RAM_DEPOP:
1194 case RT5663_VOL_TEST:
1195 case RT5663_TEST_MODE_4:
1196 case RT5663_TEST_MODE_5:
Bard Liaodf7c5212016-09-09 10:33:10 +08001197 case RT5663_STO_DRE_9:
Bard Liao73444722016-10-21 11:02:28 +08001198 case RT5663_MONO_DYNA_1:
1199 case RT5663_MONO_DYNA_2:
1200 case RT5663_MONO_DYNA_3:
1201 case RT5663_MONO_DYNA_4:
1202 case RT5663_MONO_DYNA_5:
1203 case RT5663_MONO_DYNA_6:
1204 case RT5663_STO1_SIL_DET:
1205 case RT5663_MONOL_SIL_DET:
1206 case RT5663_MONOR_SIL_DET:
1207 case RT5663_STO2_DAC_SIL:
1208 case RT5663_PWR_SAV_CTL1:
1209 case RT5663_PWR_SAV_CTL2:
1210 case RT5663_PWR_SAV_CTL3:
1211 case RT5663_PWR_SAV_CTL4:
1212 case RT5663_PWR_SAV_CTL5:
1213 case RT5663_PWR_SAV_CTL6:
1214 case RT5663_MONO_AMP_CAL1:
1215 case RT5663_MONO_AMP_CAL2:
1216 case RT5663_MONO_AMP_CAL3:
1217 case RT5663_MONO_AMP_CAL4:
1218 case RT5663_MONO_AMP_CAL5:
1219 case RT5663_MONO_AMP_CAL6:
1220 case RT5663_MONO_AMP_CAL7:
1221 case RT5663_MONO_AMP_CAL_ST1:
1222 case RT5663_MONO_AMP_CAL_ST2:
1223 case RT5663_MONO_AMP_CAL_ST3:
1224 case RT5663_MONO_AMP_CAL_ST4:
1225 case RT5663_MONO_AMP_CAL_ST5:
1226 case RT5663_V2_HP_IMP_SEN_13:
1227 case RT5663_V2_HP_IMP_SEN_14:
1228 case RT5663_V2_HP_IMP_SEN_6:
1229 case RT5663_V2_HP_IMP_SEN_7:
1230 case RT5663_V2_HP_IMP_SEN_8:
1231 case RT5663_V2_HP_IMP_SEN_9:
1232 case RT5663_V2_HP_IMP_SEN_10:
1233 case RT5663_HP_LOGIC_3:
1234 case RT5663_HP_CALIB_ST10:
1235 case RT5663_HP_CALIB_ST11:
1236 case RT5663_PRO_REG_TBL_4:
1237 case RT5663_PRO_REG_TBL_5:
1238 case RT5663_PRO_REG_TBL_6:
1239 case RT5663_PRO_REG_TBL_7:
1240 case RT5663_PRO_REG_TBL_8:
1241 case RT5663_PRO_REG_TBL_9:
1242 case RT5663_SAR_ADC_INL_1:
1243 case RT5663_SAR_ADC_INL_2:
1244 case RT5663_SAR_ADC_INL_3:
1245 case RT5663_SAR_ADC_INL_4:
1246 case RT5663_SAR_ADC_INL_5:
1247 case RT5663_SAR_ADC_INL_6:
1248 case RT5663_SAR_ADC_INL_7:
1249 case RT5663_SAR_ADC_INL_8:
1250 case RT5663_SAR_ADC_INL_9:
1251 case RT5663_SAR_ADC_INL_10:
1252 case RT5663_SAR_ADC_INL_11:
1253 case RT5663_SAR_ADC_INL_12:
1254 case RT5663_DRC_CTRL_1:
1255 case RT5663_DRC1_CTRL_2:
1256 case RT5663_DRC1_CTRL_3:
1257 case RT5663_DRC1_CTRL_4:
1258 case RT5663_DRC1_CTRL_5:
1259 case RT5663_DRC1_CTRL_6:
1260 case RT5663_DRC1_HD_CTRL_1:
1261 case RT5663_DRC1_HD_CTRL_2:
1262 case RT5663_DRC1_PRI_REG_1:
1263 case RT5663_DRC1_PRI_REG_2:
1264 case RT5663_DRC1_PRI_REG_3:
1265 case RT5663_DRC1_PRI_REG_4:
1266 case RT5663_DRC1_PRI_REG_5:
1267 case RT5663_DRC1_PRI_REG_6:
1268 case RT5663_DRC1_PRI_REG_7:
1269 case RT5663_DRC1_PRI_REG_8:
1270 case RT5663_ALC_PGA_CTL_1:
1271 case RT5663_ALC_PGA_CTL_2:
1272 case RT5663_ALC_PGA_CTL_3:
1273 case RT5663_ALC_PGA_CTL_4:
1274 case RT5663_ALC_PGA_CTL_5:
1275 case RT5663_ALC_PGA_CTL_6:
1276 case RT5663_ALC_PGA_CTL_7:
1277 case RT5663_ALC_PGA_CTL_8:
1278 case RT5663_ALC_PGA_REG_1:
1279 case RT5663_ALC_PGA_REG_2:
1280 case RT5663_ALC_PGA_REG_3:
1281 case RT5663_ADC_EQ_RECOV_1:
1282 case RT5663_ADC_EQ_RECOV_2:
1283 case RT5663_ADC_EQ_RECOV_3:
1284 case RT5663_ADC_EQ_RECOV_4:
1285 case RT5663_ADC_EQ_RECOV_5:
1286 case RT5663_ADC_EQ_RECOV_6:
1287 case RT5663_ADC_EQ_RECOV_7:
1288 case RT5663_ADC_EQ_RECOV_8:
1289 case RT5663_ADC_EQ_RECOV_9:
1290 case RT5663_ADC_EQ_RECOV_10:
1291 case RT5663_ADC_EQ_RECOV_11:
1292 case RT5663_ADC_EQ_RECOV_12:
1293 case RT5663_ADC_EQ_RECOV_13:
1294 case RT5663_VID_HIDDEN:
1295 case RT5663_VID_CUSTOMER:
1296 case RT5663_SCAN_MODE:
1297 case RT5663_I2C_BYPA:
Bard Liaodf7c5212016-09-09 10:33:10 +08001298 return true;
1299 case RT5663_TDM_1:
1300 case RT5663_DEPOP_3:
1301 case RT5663_ASRC_11_2:
1302 case RT5663_INT_ST_2:
Bard Liao73444722016-10-21 11:02:28 +08001303 case RT5663_GPIO_STA1:
Bard Liaodf7c5212016-09-09 10:33:10 +08001304 case RT5663_SIN_GEN_1:
1305 case RT5663_SIN_GEN_2:
1306 case RT5663_SIN_GEN_3:
1307 case RT5663_IL_CMD_PWRSAV1:
1308 case RT5663_IL_CMD_PWRSAV2:
1309 case RT5663_EM_JACK_TYPE_1:
1310 case RT5663_EM_JACK_TYPE_2:
1311 case RT5663_EM_JACK_TYPE_3:
1312 case RT5663_EM_JACK_TYPE_4:
1313 case RT5663_FAST_OFF_MICBIAS:
1314 case RT5663_ANA_BIAS_CUR_1:
1315 case RT5663_ANA_BIAS_CUR_2:
1316 case RT5663_BIAS_CUR_9:
1317 case RT5663_DUMMY_REG_4:
1318 case RT5663_VREF_RECMIX:
1319 case RT5663_CHARGE_PUMP_1_2:
1320 case RT5663_CHARGE_PUMP_1_3:
1321 case RT5663_CHARGE_PUMP_2:
1322 case RT5663_CHOP_DAC_R:
1323 case RT5663_DUMMY_CTL_DACLR:
1324 case RT5663_DUMMY_REG_5:
1325 case RT5663_SOFT_RAMP:
1326 case RT5663_TEST_MODE_1:
1327 case RT5663_STO_DRE_10:
1328 case RT5663_MIC_DECRO_1:
1329 case RT5663_MIC_DECRO_2:
1330 case RT5663_MIC_DECRO_3:
1331 case RT5663_MIC_DECRO_4:
1332 case RT5663_MIC_DECRO_5:
1333 case RT5663_MIC_DECRO_6:
1334 case RT5663_HP_DECRO_1:
1335 case RT5663_HP_DECRO_2:
1336 case RT5663_HP_DECRO_3:
1337 case RT5663_HP_DECRO_4:
1338 case RT5663_HP_DECOUP:
1339 case RT5663_HP_IMPSEN_MAP4:
1340 case RT5663_HP_IMPSEN_MAP5:
1341 case RT5663_HP_IMPSEN_MAP7:
1342 case RT5663_HP_CALIB_1:
1343 case RT5663_CBJ_1:
1344 case RT5663_CBJ_2:
1345 case RT5663_CBJ_3:
1346 return false;
1347 default:
1348 return rt5663_readable_register(dev, reg);
1349 }
1350}
1351
1352static const DECLARE_TLV_DB_SCALE(rt5663_hp_vol_tlv, -2400, 150, 0);
Bard Liao73444722016-10-21 11:02:28 +08001353static const DECLARE_TLV_DB_SCALE(rt5663_v2_hp_vol_tlv, -2250, 150, 0);
Bard Liaodf7c5212016-09-09 10:33:10 +08001354static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -6525, 75, 0);
1355static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -1725, 75, 0);
1356
1357/* {0, +20, +24, +30, +35, +40, +44, +50, +52} dB */
1358static const DECLARE_TLV_DB_RANGE(in_bst_tlv,
1359 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
1360 1, 1, TLV_DB_SCALE_ITEM(2000, 0, 0),
1361 2, 2, TLV_DB_SCALE_ITEM(2400, 0, 0),
1362 3, 5, TLV_DB_SCALE_ITEM(3000, 500, 0),
1363 6, 6, TLV_DB_SCALE_ITEM(4400, 0, 0),
1364 7, 7, TLV_DB_SCALE_ITEM(5000, 0, 0),
1365 8, 8, TLV_DB_SCALE_ITEM(5200, 0, 0)
1366);
1367
1368/* Interface data select */
1369static const char * const rt5663_if1_adc_data_select[] = {
1370 "L/R", "R/L", "L/L", "R/R"
1371};
1372
Wei Yongjun66d7c262016-09-17 01:34:09 +00001373static SOC_ENUM_SINGLE_DECL(rt5663_if1_adc_enum, RT5663_TDM_2,
Bard Liaodf7c5212016-09-09 10:33:10 +08001374 RT5663_DATA_SWAP_ADCDAT1_SHIFT, rt5663_if1_adc_data_select);
1375
1376static void rt5663_enable_push_button_irq(struct snd_soc_codec *codec,
1377 bool enable)
1378{
1379 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
1380
1381 if (enable) {
1382 snd_soc_update_bits(codec, RT5663_IL_CMD_6,
Bard Liao73444722016-10-21 11:02:28 +08001383 RT5663_EN_4BTN_INL_MASK, RT5663_EN_4BTN_INL_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08001384 /* reset in-line command */
1385 snd_soc_update_bits(codec, RT5663_IL_CMD_6,
Bard Liao73444722016-10-21 11:02:28 +08001386 RT5663_RESET_4BTN_INL_MASK,
1387 RT5663_RESET_4BTN_INL_RESET);
Bard Liaodf7c5212016-09-09 10:33:10 +08001388 snd_soc_update_bits(codec, RT5663_IL_CMD_6,
Bard Liao73444722016-10-21 11:02:28 +08001389 RT5663_RESET_4BTN_INL_MASK,
1390 RT5663_RESET_4BTN_INL_NOR);
1391 switch (rt5663->codec_ver) {
1392 case CODEC_VER_1:
Bard Liaodf7c5212016-09-09 10:33:10 +08001393 snd_soc_update_bits(codec, RT5663_IRQ_3,
Bard Liao73444722016-10-21 11:02:28 +08001394 RT5663_V2_EN_IRQ_INLINE_MASK,
1395 RT5663_V2_EN_IRQ_INLINE_NOR);
Bard Liaodf7c5212016-09-09 10:33:10 +08001396 break;
Bard Liao73444722016-10-21 11:02:28 +08001397 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08001398 snd_soc_update_bits(codec, RT5663_IRQ_2,
1399 RT5663_EN_IRQ_INLINE_MASK,
1400 RT5663_EN_IRQ_INLINE_NOR);
1401 break;
1402 default:
Bard Liao73444722016-10-21 11:02:28 +08001403 dev_err(codec->dev, "Unknown CODEC Version\n");
Bard Liaodf7c5212016-09-09 10:33:10 +08001404 }
1405 } else {
Bard Liao73444722016-10-21 11:02:28 +08001406 switch (rt5663->codec_ver) {
1407 case CODEC_VER_1:
Bard Liaodf7c5212016-09-09 10:33:10 +08001408 snd_soc_update_bits(codec, RT5663_IRQ_3,
Bard Liao73444722016-10-21 11:02:28 +08001409 RT5663_V2_EN_IRQ_INLINE_MASK,
1410 RT5663_V2_EN_IRQ_INLINE_BYP);
Bard Liaodf7c5212016-09-09 10:33:10 +08001411 break;
Bard Liao73444722016-10-21 11:02:28 +08001412 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08001413 snd_soc_update_bits(codec, RT5663_IRQ_2,
1414 RT5663_EN_IRQ_INLINE_MASK,
1415 RT5663_EN_IRQ_INLINE_BYP);
1416 break;
1417 default:
Bard Liao73444722016-10-21 11:02:28 +08001418 dev_err(codec->dev, "Unknown CODEC Version\n");
Bard Liaodf7c5212016-09-09 10:33:10 +08001419 }
1420 snd_soc_update_bits(codec, RT5663_IL_CMD_6,
Bard Liao73444722016-10-21 11:02:28 +08001421 RT5663_EN_4BTN_INL_MASK, RT5663_EN_4BTN_INL_DIS);
Bard Liaodf7c5212016-09-09 10:33:10 +08001422 /* reset in-line command */
1423 snd_soc_update_bits(codec, RT5663_IL_CMD_6,
Bard Liao73444722016-10-21 11:02:28 +08001424 RT5663_RESET_4BTN_INL_MASK,
1425 RT5663_RESET_4BTN_INL_RESET);
Bard Liaodf7c5212016-09-09 10:33:10 +08001426 snd_soc_update_bits(codec, RT5663_IL_CMD_6,
Bard Liao73444722016-10-21 11:02:28 +08001427 RT5663_RESET_4BTN_INL_MASK,
1428 RT5663_RESET_4BTN_INL_NOR);
Bard Liaodf7c5212016-09-09 10:33:10 +08001429 }
1430}
1431
1432/**
Bard Liao73444722016-10-21 11:02:28 +08001433 * rt5663_v2_jack_detect - Detect headset.
Bard Liaodf7c5212016-09-09 10:33:10 +08001434 * @codec: SoC audio codec device.
1435 * @jack_insert: Jack insert or not.
1436 *
1437 * Detect whether is headset or not when jack inserted.
1438 *
1439 * Returns detect status.
1440 */
1441
Bard Liao73444722016-10-21 11:02:28 +08001442static int rt5663_v2_jack_detect(struct snd_soc_codec *codec, int jack_insert)
Bard Liaodf7c5212016-09-09 10:33:10 +08001443{
1444 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
Bard Liao73444722016-10-21 11:02:28 +08001445 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
Bard Liaodf7c5212016-09-09 10:33:10 +08001446 int val, i = 0, sleep_time[5] = {300, 150, 100, 50, 30};
1447
1448 dev_dbg(codec->dev, "%s jack_insert:%d\n", __func__, jack_insert);
1449 if (jack_insert) {
Bard Liao73444722016-10-21 11:02:28 +08001450 snd_soc_write(codec, RT5663_CBJ_TYPE_2, 0x8040);
1451 snd_soc_write(codec, RT5663_CBJ_TYPE_3, 0x1484);
Bard Liaodf7c5212016-09-09 10:33:10 +08001452
1453 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
1454 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS2");
1455 snd_soc_dapm_force_enable_pin(dapm, "Mic Det Power");
1456 snd_soc_dapm_force_enable_pin(dapm, "CBJ Power");
1457 snd_soc_dapm_sync(dapm);
1458 snd_soc_update_bits(codec, RT5663_RC_CLK,
Bard Liao73444722016-10-21 11:02:28 +08001459 RT5663_DIG_1M_CLK_MASK, RT5663_DIG_1M_CLK_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08001460 snd_soc_update_bits(codec, RT5663_RECMIX, 0x8, 0x8);
1461
1462 while (i < 5) {
1463 msleep(sleep_time[i]);
Bard Liao73444722016-10-21 11:02:28 +08001464 val = snd_soc_read(codec, RT5663_CBJ_TYPE_2) & 0x0003;
Bard Liaodf7c5212016-09-09 10:33:10 +08001465 if (val == 0x1 || val == 0x2 || val == 0x3)
1466 break;
1467 dev_dbg(codec->dev, "%s: MX-0011 val=%x sleep %d\n",
1468 __func__, val, sleep_time[i]);
1469 i++;
1470 }
1471 dev_dbg(codec->dev, "%s val = %d\n", __func__, val);
1472 switch (val) {
1473 case 1:
1474 case 2:
Bard Liao73444722016-10-21 11:02:28 +08001475 rt5663->jack_type = SND_JACK_HEADSET;
Bard Liaodf7c5212016-09-09 10:33:10 +08001476 rt5663_enable_push_button_irq(codec, true);
1477 break;
1478 default:
1479 snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
1480 snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
1481 snd_soc_dapm_disable_pin(dapm, "Mic Det Power");
1482 snd_soc_dapm_disable_pin(dapm, "CBJ Power");
1483 snd_soc_dapm_sync(dapm);
Bard Liao73444722016-10-21 11:02:28 +08001484 rt5663->jack_type = SND_JACK_HEADPHONE;
Bard Liaodf7c5212016-09-09 10:33:10 +08001485 break;
1486 }
1487 } else {
1488 snd_soc_update_bits(codec, RT5663_RECMIX, 0x8, 0x0);
1489
Bard Liao73444722016-10-21 11:02:28 +08001490 if (rt5663->jack_type == SND_JACK_HEADSET) {
Bard Liaodf7c5212016-09-09 10:33:10 +08001491 rt5663_enable_push_button_irq(codec, false);
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 }
Bard Liao73444722016-10-21 11:02:28 +08001498 rt5663->jack_type = 0;
Bard Liaodf7c5212016-09-09 10:33:10 +08001499 }
1500
Bard Liao73444722016-10-21 11:02:28 +08001501 dev_dbg(codec->dev, "jack_type = %d\n", rt5663->jack_type);
1502 return rt5663->jack_type;
Bard Liaodf7c5212016-09-09 10:33:10 +08001503}
1504
1505/**
1506 * rt5663_jack_detect - Detect headset.
1507 * @codec: SoC audio codec device.
1508 * @jack_insert: Jack insert or not.
1509 *
1510 * Detect whether is headset or not when jack inserted.
1511 *
1512 * Returns detect status.
1513 */
1514static int rt5663_jack_detect(struct snd_soc_codec *codec, int jack_insert)
1515{
1516 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
oder_chiou@realtek.com8f244122017-07-07 16:58:58 +08001517 int val, i = 0;
Bard Liaodf7c5212016-09-09 10:33:10 +08001518
1519 dev_dbg(codec->dev, "%s jack_insert:%d\n", __func__, jack_insert);
1520
1521 if (jack_insert) {
1522 snd_soc_update_bits(codec, RT5663_DIG_MISC,
Bard Liao73444722016-10-21 11:02:28 +08001523 RT5663_DIG_GATE_CTRL_MASK, RT5663_DIG_GATE_CTRL_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08001524 snd_soc_update_bits(codec, RT5663_HP_CHARGE_PUMP_1,
Bard Liao73444722016-10-21 11:02:28 +08001525 RT5663_SI_HP_MASK | RT5663_OSW_HP_L_MASK |
1526 RT5663_OSW_HP_R_MASK, RT5663_SI_HP_EN |
1527 RT5663_OSW_HP_L_DIS | RT5663_OSW_HP_R_DIS);
Bard Liaodf7c5212016-09-09 10:33:10 +08001528 snd_soc_update_bits(codec, RT5663_DUMMY_1,
1529 RT5663_EMB_CLK_MASK | RT5663_HPA_CPL_BIAS_MASK |
1530 RT5663_HPA_CPR_BIAS_MASK, RT5663_EMB_CLK_EN |
1531 RT5663_HPA_CPL_BIAS_1 | RT5663_HPA_CPR_BIAS_1);
1532 snd_soc_update_bits(codec, RT5663_CBJ_1,
1533 RT5663_INBUF_CBJ_BST1_MASK | RT5663_CBJ_SENSE_BST1_MASK,
1534 RT5663_INBUF_CBJ_BST1_ON | RT5663_CBJ_SENSE_BST1_L);
1535 snd_soc_update_bits(codec, RT5663_IL_CMD_2,
1536 RT5663_PWR_MIC_DET_MASK, RT5663_PWR_MIC_DET_ON);
1537 /* BST1 power on for JD */
1538 snd_soc_update_bits(codec, RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08001539 RT5663_PWR_BST1_MASK, RT5663_PWR_BST1_ON);
Bard Liaodf7c5212016-09-09 10:33:10 +08001540 snd_soc_update_bits(codec, RT5663_EM_JACK_TYPE_1,
1541 RT5663_CBJ_DET_MASK | RT5663_EXT_JD_MASK |
1542 RT5663_POL_EXT_JD_MASK, RT5663_CBJ_DET_EN |
1543 RT5663_EXT_JD_EN | RT5663_POL_EXT_JD_EN);
1544 snd_soc_update_bits(codec, RT5663_PWR_ANLG_1,
Bard Liao73444722016-10-21 11:02:28 +08001545 RT5663_PWR_MB_MASK | RT5663_LDO1_DVO_MASK |
1546 RT5663_AMP_HP_MASK, RT5663_PWR_MB |
1547 RT5663_LDO1_DVO_0_9V | RT5663_AMP_HP_3X);
Bard Liaodf7c5212016-09-09 10:33:10 +08001548 snd_soc_update_bits(codec, RT5663_AUTO_1MRC_CLK,
Bard Liao73444722016-10-21 11:02:28 +08001549 RT5663_IRQ_POW_SAV_MASK, RT5663_IRQ_POW_SAV_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08001550 snd_soc_update_bits(codec, RT5663_IRQ_1,
1551 RT5663_EN_IRQ_JD1_MASK, RT5663_EN_IRQ_JD1_EN);
oder_chiou@realtek.com8f244122017-07-07 16:58:58 +08001552
1553 while (true) {
1554 regmap_read(rt5663->regmap, RT5663_INT_ST_2, &val);
1555 if (!(val & 0x80))
1556 usleep_range(10000, 10005);
1557 else
Bard Liaodf7c5212016-09-09 10:33:10 +08001558 break;
oder_chiou@realtek.com8f244122017-07-07 16:58:58 +08001559
1560 if (i > 200)
1561 break;
1562 i++;
Bard Liaodf7c5212016-09-09 10:33:10 +08001563 }
oder_chiou@realtek.com8f244122017-07-07 16:58:58 +08001564
1565 val = snd_soc_read(codec, RT5663_EM_JACK_TYPE_2) & 0x0003;
Bard Liaodf7c5212016-09-09 10:33:10 +08001566 dev_dbg(codec->dev, "%s val = %d\n", __func__, val);
oder_chiou@realtek.com8f244122017-07-07 16:58:58 +08001567
Bard Liaodf7c5212016-09-09 10:33:10 +08001568 switch (val) {
1569 case 1:
1570 case 2:
1571 rt5663->jack_type = SND_JACK_HEADSET;
1572 rt5663_enable_push_button_irq(codec, true);
oder_chiou@realtek.com278982b2017-08-02 16:01:27 +08001573
1574 if (rt5663->pdata.dc_offset_l_manual_mic) {
1575 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_2,
1576 rt5663->pdata.dc_offset_l_manual_mic >>
1577 16);
1578 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_3,
1579 rt5663->pdata.dc_offset_l_manual_mic &
1580 0xffff);
1581 }
1582
1583 if (rt5663->pdata.dc_offset_r_manual_mic) {
1584 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_5,
1585 rt5663->pdata.dc_offset_r_manual_mic >>
1586 16);
1587 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_6,
1588 rt5663->pdata.dc_offset_r_manual_mic &
1589 0xffff);
1590 }
Bard Liaodf7c5212016-09-09 10:33:10 +08001591 break;
1592 default:
1593 rt5663->jack_type = SND_JACK_HEADPHONE;
oder_chiou@realtek.com278982b2017-08-02 16:01:27 +08001594
1595 if (rt5663->pdata.dc_offset_l_manual) {
1596 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_2,
1597 rt5663->pdata.dc_offset_l_manual >> 16);
1598 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_3,
1599 rt5663->pdata.dc_offset_l_manual &
1600 0xffff);
1601 }
1602
1603 if (rt5663->pdata.dc_offset_r_manual) {
1604 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_5,
1605 rt5663->pdata.dc_offset_r_manual >> 16);
1606 regmap_write(rt5663->regmap, RT5663_MIC_DECRO_6,
1607 rt5663->pdata.dc_offset_r_manual &
1608 0xffff);
1609 }
Bard Liaodf7c5212016-09-09 10:33:10 +08001610 break;
1611 }
1612 } else {
1613 if (rt5663->jack_type == SND_JACK_HEADSET)
1614 rt5663_enable_push_button_irq(codec, false);
1615 rt5663->jack_type = 0;
1616 }
1617
1618 dev_dbg(codec->dev, "jack_type = %d\n", rt5663->jack_type);
1619 return rt5663->jack_type;
1620}
1621
Wei Yongjun66d7c262016-09-17 01:34:09 +00001622static int rt5663_button_detect(struct snd_soc_codec *codec)
Bard Liaodf7c5212016-09-09 10:33:10 +08001623{
1624 int btn_type, val;
1625
1626 val = snd_soc_read(codec, RT5663_IL_CMD_5);
1627 dev_dbg(codec->dev, "%s: val=0x%x\n", __func__, val);
1628 btn_type = val & 0xfff0;
1629 snd_soc_write(codec, RT5663_IL_CMD_5, val);
1630
1631 return btn_type;
1632}
1633
1634static irqreturn_t rt5663_irq(int irq, void *data)
1635{
1636 struct rt5663_priv *rt5663 = data;
1637
1638 dev_dbg(rt5663->codec->dev, "%s IRQ queue work\n", __func__);
1639
1640 queue_delayed_work(system_wq, &rt5663->jack_detect_work,
1641 msecs_to_jiffies(250));
1642
1643 return IRQ_HANDLED;
1644}
1645
1646int rt5663_set_jack_detect(struct snd_soc_codec *codec,
1647 struct snd_soc_jack *hs_jack)
1648{
1649 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
1650
1651 rt5663->hs_jack = hs_jack;
1652
1653 rt5663_irq(0, rt5663);
1654
1655 return 0;
1656}
1657EXPORT_SYMBOL_GPL(rt5663_set_jack_detect);
1658
1659static bool rt5663_check_jd_status(struct snd_soc_codec *codec)
1660{
1661 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
1662 int val = snd_soc_read(codec, RT5663_INT_ST_1);
1663
1664 dev_dbg(codec->dev, "%s val=%x\n", __func__, val);
1665
1666 /* JD1 */
Bard Liao73444722016-10-21 11:02:28 +08001667 switch (rt5663->codec_ver) {
1668 case CODEC_VER_1:
Bard Liaodf7c5212016-09-09 10:33:10 +08001669 return !(val & 0x2000);
Bard Liao73444722016-10-21 11:02:28 +08001670 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08001671 return !(val & 0x1000);
1672 default:
Bard Liao73444722016-10-21 11:02:28 +08001673 dev_err(codec->dev, "Unknown CODEC Version\n");
Bard Liaodf7c5212016-09-09 10:33:10 +08001674 }
1675
1676 return false;
1677}
1678
1679static void rt5663_jack_detect_work(struct work_struct *work)
1680{
1681 struct rt5663_priv *rt5663 =
1682 container_of(work, struct rt5663_priv, jack_detect_work.work);
1683 struct snd_soc_codec *codec = rt5663->codec;
1684 int btn_type, report = 0;
1685
1686 if (!codec)
1687 return;
1688
1689 if (rt5663_check_jd_status(codec)) {
1690 /* jack in */
1691 if (rt5663->jack_type == 0) {
1692 /* jack was out, report jack type */
Bard Liao73444722016-10-21 11:02:28 +08001693 switch (rt5663->codec_ver) {
1694 case CODEC_VER_1:
1695 report = rt5663_v2_jack_detect(
1696 rt5663->codec, 1);
Bard Liaodf7c5212016-09-09 10:33:10 +08001697 break;
Bard Liao73444722016-10-21 11:02:28 +08001698 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08001699 report = rt5663_jack_detect(rt5663->codec, 1);
1700 break;
1701 default:
Bard Liao73444722016-10-21 11:02:28 +08001702 dev_err(codec->dev, "Unknown CODEC Version\n");
Bard Liaodf7c5212016-09-09 10:33:10 +08001703 }
1704 } else {
1705 /* jack is already in, report button event */
1706 report = SND_JACK_HEADSET;
1707 btn_type = rt5663_button_detect(rt5663->codec);
1708 /**
1709 * rt5663 can report three kinds of button behavior,
1710 * one click, double click and hold. However,
1711 * currently we will report button pressed/released
1712 * event. So all the three button behaviors are
1713 * treated as button pressed.
1714 */
1715 switch (btn_type) {
1716 case 0x8000:
1717 case 0x4000:
1718 case 0x2000:
1719 report |= SND_JACK_BTN_0;
1720 break;
1721 case 0x1000:
1722 case 0x0800:
1723 case 0x0400:
1724 report |= SND_JACK_BTN_1;
1725 break;
1726 case 0x0200:
1727 case 0x0100:
1728 case 0x0080:
1729 report |= SND_JACK_BTN_2;
1730 break;
1731 case 0x0040:
1732 case 0x0020:
1733 case 0x0010:
1734 report |= SND_JACK_BTN_3;
1735 break;
1736 case 0x0000: /* unpressed */
1737 break;
1738 default:
1739 btn_type = 0;
1740 dev_err(rt5663->codec->dev,
1741 "Unexpected button code 0x%04x\n",
1742 btn_type);
1743 break;
1744 }
1745 /* button release or spurious interrput*/
1746 if (btn_type == 0)
1747 report = rt5663->jack_type;
1748 }
1749 } else {
1750 /* jack out */
Bard Liao73444722016-10-21 11:02:28 +08001751 switch (rt5663->codec_ver) {
1752 case CODEC_VER_1:
1753 report = rt5663_v2_jack_detect(rt5663->codec, 0);
Bard Liaodf7c5212016-09-09 10:33:10 +08001754 break;
Bard Liao73444722016-10-21 11:02:28 +08001755 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08001756 report = rt5663_jack_detect(rt5663->codec, 0);
1757 break;
1758 default:
Bard Liao73444722016-10-21 11:02:28 +08001759 dev_err(codec->dev, "Unknown CODEC Version\n");
Bard Liaodf7c5212016-09-09 10:33:10 +08001760 }
1761 }
1762 dev_dbg(codec->dev, "%s jack report: 0x%04x\n", __func__, report);
1763 snd_soc_jack_report(rt5663->hs_jack, report, SND_JACK_HEADSET |
1764 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
1765 SND_JACK_BTN_2 | SND_JACK_BTN_3);
1766}
1767
1768static const struct snd_kcontrol_new rt5663_snd_controls[] = {
1769 /* DAC Digital Volume */
1770 SOC_DOUBLE_TLV("DAC Playback Volume", RT5663_STO1_DAC_DIG_VOL,
Bard Liao73444722016-10-21 11:02:28 +08001771 RT5663_DAC_L1_VOL_SHIFT + 1, RT5663_DAC_R1_VOL_SHIFT + 1,
Bard Liaodf7c5212016-09-09 10:33:10 +08001772 87, 0, dac_vol_tlv),
1773 /* ADC Digital Volume Control */
1774 SOC_DOUBLE("ADC Capture Switch", RT5663_STO1_ADC_DIG_VOL,
Bard Liao73444722016-10-21 11:02:28 +08001775 RT5663_ADC_L_MUTE_SHIFT, RT5663_ADC_R_MUTE_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001776 SOC_DOUBLE_TLV("ADC Capture Volume", RT5663_STO1_ADC_DIG_VOL,
Bard Liao73444722016-10-21 11:02:28 +08001777 RT5663_ADC_L_VOL_SHIFT + 1, RT5663_ADC_R_VOL_SHIFT + 1,
Bard Liaodf7c5212016-09-09 10:33:10 +08001778 63, 0, adc_vol_tlv),
1779};
1780
Bard Liao73444722016-10-21 11:02:28 +08001781static const struct snd_kcontrol_new rt5663_v2_specific_controls[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08001782 /* Headphone Output Volume */
1783 SOC_DOUBLE_R_TLV("Headphone Playback Volume", RT5663_HP_LCH_DRE,
Bard Liao73444722016-10-21 11:02:28 +08001784 RT5663_HP_RCH_DRE, RT5663_GAIN_HP_SHIFT, 15, 1,
1785 rt5663_v2_hp_vol_tlv),
Bard Liaodf7c5212016-09-09 10:33:10 +08001786 /* Mic Boost Volume */
Bard Liao73444722016-10-21 11:02:28 +08001787 SOC_SINGLE_TLV("IN1 Capture Volume", RT5663_AEC_BST,
1788 RT5663_GAIN_CBJ_SHIFT, 8, 0, in_bst_tlv),
Bard Liaodf7c5212016-09-09 10:33:10 +08001789};
1790
1791static const struct snd_kcontrol_new rt5663_specific_controls[] = {
1792 /* Headphone Output Volume */
1793 SOC_DOUBLE_R_TLV("Headphone Playback Volume", RT5663_STO_DRE_9,
1794 RT5663_STO_DRE_10, RT5663_DRE_GAIN_HP_SHIFT, 23, 1,
1795 rt5663_hp_vol_tlv),
1796 /* Mic Boost Volume*/
1797 SOC_SINGLE_TLV("IN1 Capture Volume", RT5663_CBJ_2,
1798 RT5663_GAIN_BST1_SHIFT, 8, 0, in_bst_tlv),
1799 /* Data Swap for Slot0/1 in ADCDAT1 */
1800 SOC_ENUM("IF1 ADC Data Swap", rt5663_if1_adc_enum),
1801};
1802
1803static int rt5663_is_sys_clk_from_pll(struct snd_soc_dapm_widget *w,
1804 struct snd_soc_dapm_widget *sink)
1805{
1806 unsigned int val;
1807 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
1808
1809 val = snd_soc_read(codec, RT5663_GLB_CLK);
1810 val &= RT5663_SCLK_SRC_MASK;
1811 if (val == RT5663_SCLK_SRC_PLL1)
1812 return 1;
1813 else
1814 return 0;
1815}
1816
1817static int rt5663_is_using_asrc(struct snd_soc_dapm_widget *w,
1818 struct snd_soc_dapm_widget *sink)
1819{
1820 unsigned int reg, shift, val;
1821 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
1822 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
1823
Bard Liao73444722016-10-21 11:02:28 +08001824 if (rt5663->codec_ver == CODEC_VER_1) {
Bard Liaodf7c5212016-09-09 10:33:10 +08001825 switch (w->shift) {
Bard Liao73444722016-10-21 11:02:28 +08001826 case RT5663_ADC_STO1_ASRC_SHIFT:
1827 reg = RT5663_ASRC_3;
1828 shift = RT5663_V2_AD_STO1_TRACK_SHIFT;
Bard Liaodf7c5212016-09-09 10:33:10 +08001829 break;
Bard Liao73444722016-10-21 11:02:28 +08001830 case RT5663_DAC_STO1_ASRC_SHIFT:
Bard Liaodf7c5212016-09-09 10:33:10 +08001831 reg = RT5663_ASRC_2;
Bard Liao73444722016-10-21 11:02:28 +08001832 shift = RT5663_DA_STO1_TRACK_SHIFT;
Bard Liaodf7c5212016-09-09 10:33:10 +08001833 break;
1834 default:
1835 return 0;
1836 }
1837 } else {
1838 switch (w->shift) {
1839 case RT5663_ADC_STO1_ASRC_SHIFT:
1840 reg = RT5663_ASRC_2;
1841 shift = RT5663_AD_STO1_TRACK_SHIFT;
1842 break;
1843 case RT5663_DAC_STO1_ASRC_SHIFT:
1844 reg = RT5663_ASRC_2;
1845 shift = RT5663_DA_STO1_TRACK_SHIFT;
1846 break;
1847 default:
1848 return 0;
1849 }
1850 }
1851
1852 val = (snd_soc_read(codec, reg) >> shift) & 0x7;
1853
1854 if (val)
1855 return 1;
1856
1857 return 0;
1858}
1859
1860static int rt5663_i2s_use_asrc(struct snd_soc_dapm_widget *source,
1861 struct snd_soc_dapm_widget *sink)
1862{
1863 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
1864 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
1865 int da_asrc_en, ad_asrc_en;
1866
1867 da_asrc_en = (snd_soc_read(codec, RT5663_ASRC_2) &
1868 RT5663_DA_STO1_TRACK_MASK) ? 1 : 0;
Bard Liao73444722016-10-21 11:02:28 +08001869 switch (rt5663->codec_ver) {
1870 case CODEC_VER_1:
1871 ad_asrc_en = (snd_soc_read(codec, RT5663_ASRC_3) &
1872 RT5663_V2_AD_STO1_TRACK_MASK) ? 1 : 0;
Bard Liaodf7c5212016-09-09 10:33:10 +08001873 break;
Bard Liao73444722016-10-21 11:02:28 +08001874 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08001875 ad_asrc_en = (snd_soc_read(codec, RT5663_ASRC_2) &
1876 RT5663_AD_STO1_TRACK_MASK) ? 1 : 0;
1877 break;
1878 default:
Bard Liao73444722016-10-21 11:02:28 +08001879 dev_err(codec->dev, "Unknown CODEC Version\n");
Arnd Bergmann56efaed2016-09-15 17:42:21 +02001880 return 1;
Bard Liaodf7c5212016-09-09 10:33:10 +08001881 }
1882
1883 if (da_asrc_en || ad_asrc_en)
1884 if (rt5663->sysclk > rt5663->lrck * 384)
1885 return 1;
1886
1887 dev_err(codec->dev, "sysclk < 384 x fs, disable i2s asrc\n");
1888
1889 return 0;
1890}
1891
1892/**
1893 * rt5663_sel_asrc_clk_src - select ASRC clock source for a set of filters
1894 * @codec: SoC audio codec device.
1895 * @filter_mask: mask of filters.
1896 * @clk_src: clock source
1897 *
Bard Liao73444722016-10-21 11:02:28 +08001898 * The ASRC function is for asynchronous MCLK and LRCK. Also, since RT5663 can
Bard Liaodf7c5212016-09-09 10:33:10 +08001899 * only support standard 32fs or 64fs i2s format, ASRC should be enabled to
1900 * support special i2s clock format such as Intel's 100fs(100 * sampling rate).
1901 * ASRC function will track i2s clock and generate a corresponding system clock
1902 * for codec. This function provides an API to select the clock source for a
1903 * set of filters specified by the mask. And the codec driver will turn on ASRC
1904 * for these filters if ASRC is selected as their clock source.
1905 */
1906int rt5663_sel_asrc_clk_src(struct snd_soc_codec *codec,
1907 unsigned int filter_mask, unsigned int clk_src)
1908{
Bard Liao73444722016-10-21 11:02:28 +08001909 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
Bard Liaodf7c5212016-09-09 10:33:10 +08001910 unsigned int asrc2_mask = 0;
1911 unsigned int asrc2_value = 0;
1912 unsigned int asrc3_mask = 0;
1913 unsigned int asrc3_value = 0;
1914
1915 switch (clk_src) {
1916 case RT5663_CLK_SEL_SYS:
1917 case RT5663_CLK_SEL_I2S1_ASRC:
1918 break;
1919
1920 default:
1921 return -EINVAL;
1922 }
1923
1924 if (filter_mask & RT5663_DA_STEREO_FILTER) {
Bard Liao73444722016-10-21 11:02:28 +08001925 asrc2_mask |= RT5663_DA_STO1_TRACK_MASK;
1926 asrc2_value |= clk_src << RT5663_DA_STO1_TRACK_SHIFT;
Bard Liaodf7c5212016-09-09 10:33:10 +08001927 }
1928
1929 if (filter_mask & RT5663_AD_STEREO_FILTER) {
Bard Liao73444722016-10-21 11:02:28 +08001930 switch (rt5663->codec_ver) {
1931 case CODEC_VER_1:
1932 asrc3_mask |= RT5663_V2_AD_STO1_TRACK_MASK;
1933 asrc3_value |= clk_src << RT5663_V2_AD_STO1_TRACK_SHIFT;
Bard Liaodf7c5212016-09-09 10:33:10 +08001934 break;
Bard Liao73444722016-10-21 11:02:28 +08001935 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08001936 asrc2_mask |= RT5663_AD_STO1_TRACK_MASK;
1937 asrc2_value |= clk_src << RT5663_AD_STO1_TRACK_SHIFT;
1938 break;
1939 default:
Bard Liao73444722016-10-21 11:02:28 +08001940 dev_err(codec->dev, "Unknown CODEC Version\n");
Bard Liaodf7c5212016-09-09 10:33:10 +08001941 }
1942 }
1943
1944 if (asrc2_mask)
1945 snd_soc_update_bits(codec, RT5663_ASRC_2, asrc2_mask,
1946 asrc2_value);
1947
1948 if (asrc3_mask)
Bard Liao73444722016-10-21 11:02:28 +08001949 snd_soc_update_bits(codec, RT5663_ASRC_3, asrc3_mask,
Bard Liaodf7c5212016-09-09 10:33:10 +08001950 asrc3_value);
1951
1952 return 0;
1953}
1954EXPORT_SYMBOL_GPL(rt5663_sel_asrc_clk_src);
1955
1956/* Analog Mixer */
Bard Liao73444722016-10-21 11:02:28 +08001957static const struct snd_kcontrol_new rt5663_recmix1l[] = {
1958 SOC_DAPM_SINGLE("BST2 Switch", RT5663_RECMIX1L,
1959 RT5663_RECMIX1L_BST2_SHIFT, 1, 1),
1960 SOC_DAPM_SINGLE("BST1 CBJ Switch", RT5663_RECMIX1L,
1961 RT5663_RECMIX1L_BST1_CBJ_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001962};
1963
Bard Liao73444722016-10-21 11:02:28 +08001964static const struct snd_kcontrol_new rt5663_recmix1r[] = {
1965 SOC_DAPM_SINGLE("BST2 Switch", RT5663_RECMIX1R,
1966 RT5663_RECMIX1R_BST2_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001967};
1968
1969/* Digital Mixer */
1970static const struct snd_kcontrol_new rt5663_sto1_adc_l_mix[] = {
1971 SOC_DAPM_SINGLE("ADC1 Switch", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001972 RT5663_M_STO1_ADC_L1_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001973 SOC_DAPM_SINGLE("ADC2 Switch", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001974 RT5663_M_STO1_ADC_L2_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001975};
1976
Bard Liao73444722016-10-21 11:02:28 +08001977static const struct snd_kcontrol_new rt5663_sto1_adc_r_mix[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08001978 SOC_DAPM_SINGLE("ADC1 Switch", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001979 RT5663_M_STO1_ADC_R1_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001980 SOC_DAPM_SINGLE("ADC2 Switch", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001981 RT5663_M_STO1_ADC_R2_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001982};
1983
1984static const struct snd_kcontrol_new rt5663_adda_l_mix[] = {
1985 SOC_DAPM_SINGLE("ADC L Switch", RT5663_AD_DA_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001986 RT5663_M_ADCMIX_L_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001987 SOC_DAPM_SINGLE("DAC L Switch", RT5663_AD_DA_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001988 RT5663_M_DAC1_L_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001989};
1990
1991static const struct snd_kcontrol_new rt5663_adda_r_mix[] = {
1992 SOC_DAPM_SINGLE("ADC R Switch", RT5663_AD_DA_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001993 RT5663_M_ADCMIX_R_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001994 SOC_DAPM_SINGLE("DAC R Switch", RT5663_AD_DA_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08001995 RT5663_M_DAC1_R_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08001996};
1997
1998static const struct snd_kcontrol_new rt5663_sto1_dac_l_mix[] = {
1999 SOC_DAPM_SINGLE("DAC L Switch", RT5663_STO_DAC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08002000 RT5663_M_DAC_L1_STO_L_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08002001};
2002
2003static const struct snd_kcontrol_new rt5663_sto1_dac_r_mix[] = {
oder_chiou@realtek.com1d5c5b62017-07-19 10:20:23 +08002004 SOC_DAPM_SINGLE("DAC R Switch", RT5663_STO_DAC_MIXER,
2005 RT5663_M_DAC_R1_STO_R_SHIFT, 1, 1),
Bard Liaodf7c5212016-09-09 10:33:10 +08002006};
2007
2008/* Out Switch */
Bard Liao73444722016-10-21 11:02:28 +08002009static const struct snd_kcontrol_new rt5663_hpo_switch =
2010 SOC_DAPM_SINGLE_AUTODISABLE("Switch", RT5663_HP_AMP_2,
2011 RT5663_EN_DAC_HPO_SHIFT, 1, 0);
Bard Liaodf7c5212016-09-09 10:33:10 +08002012
2013/* Stereo ADC source */
Bard Liao73444722016-10-21 11:02:28 +08002014static const char * const rt5663_sto1_adc_src[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08002015 "ADC L", "ADC R"
2016};
2017
Bard Liao73444722016-10-21 11:02:28 +08002018static SOC_ENUM_SINGLE_DECL(rt5663_sto1_adcl_enum, RT5663_STO1_ADC_MIXER,
2019 RT5663_STO1_ADC_L_SRC_SHIFT, rt5663_sto1_adc_src);
Bard Liaodf7c5212016-09-09 10:33:10 +08002020
Bard Liao73444722016-10-21 11:02:28 +08002021static const struct snd_kcontrol_new rt5663_sto1_adcl_mux =
2022 SOC_DAPM_ENUM("STO1 ADC L Mux", rt5663_sto1_adcl_enum);
Bard Liaodf7c5212016-09-09 10:33:10 +08002023
Bard Liao73444722016-10-21 11:02:28 +08002024static SOC_ENUM_SINGLE_DECL(rt5663_sto1_adcr_enum, RT5663_STO1_ADC_MIXER,
2025 RT5663_STO1_ADC_R_SRC_SHIFT, rt5663_sto1_adc_src);
Bard Liaodf7c5212016-09-09 10:33:10 +08002026
Bard Liao73444722016-10-21 11:02:28 +08002027static const struct snd_kcontrol_new rt5663_sto1_adcr_mux =
2028 SOC_DAPM_ENUM("STO1 ADC R Mux", rt5663_sto1_adcr_enum);
Bard Liaodf7c5212016-09-09 10:33:10 +08002029
2030/* RT5663: Analog DACL1 input source */
2031static const char * const rt5663_alg_dacl_src[] = {
2032 "DAC L", "STO DAC MIXL"
2033};
2034
2035static SOC_ENUM_SINGLE_DECL(rt5663_alg_dacl_enum, RT5663_BYPASS_STO_DAC,
2036 RT5663_DACL1_SRC_SHIFT, rt5663_alg_dacl_src);
2037
2038static const struct snd_kcontrol_new rt5663_alg_dacl_mux =
2039 SOC_DAPM_ENUM("DAC L Mux", rt5663_alg_dacl_enum);
2040
2041/* RT5663: Analog DACR1 input source */
2042static const char * const rt5663_alg_dacr_src[] = {
2043 "DAC R", "STO DAC MIXR"
2044};
2045
2046static SOC_ENUM_SINGLE_DECL(rt5663_alg_dacr_enum, RT5663_BYPASS_STO_DAC,
2047 RT5663_DACR1_SRC_SHIFT, rt5663_alg_dacr_src);
2048
2049static const struct snd_kcontrol_new rt5663_alg_dacr_mux =
2050 SOC_DAPM_ENUM("DAC R Mux", rt5663_alg_dacr_enum);
2051
2052static int rt5663_hp_event(struct snd_soc_dapm_widget *w,
2053 struct snd_kcontrol *kcontrol, int event)
2054{
2055 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2056 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2057
2058 switch (event) {
2059 case SND_SOC_DAPM_POST_PMU:
Bard Liao73444722016-10-21 11:02:28 +08002060 if (rt5663->codec_ver == CODEC_VER_1) {
Bard Liaodf7c5212016-09-09 10:33:10 +08002061 snd_soc_update_bits(codec, RT5663_HP_CHARGE_PUMP_1,
Bard Liao73444722016-10-21 11:02:28 +08002062 RT5663_SEL_PM_HP_SHIFT, RT5663_SEL_PM_HP_HIGH);
Bard Liaodf7c5212016-09-09 10:33:10 +08002063 snd_soc_update_bits(codec, RT5663_HP_LOGIC_2,
Bard Liao73444722016-10-21 11:02:28 +08002064 RT5663_HP_SIG_SRC1_MASK,
2065 RT5663_HP_SIG_SRC1_SILENCE);
Bard Liaodf7c5212016-09-09 10:33:10 +08002066 } else {
2067 snd_soc_write(codec, RT5663_DEPOP_2, 0x3003);
Bard Liaodf7c5212016-09-09 10:33:10 +08002068 snd_soc_update_bits(codec, RT5663_HP_CHARGE_PUMP_1,
Bard Liao73444722016-10-21 11:02:28 +08002069 RT5663_OVCD_HP_MASK, RT5663_OVCD_HP_DIS);
Bard Liaodf7c5212016-09-09 10:33:10 +08002070 snd_soc_write(codec, RT5663_HP_CHARGE_PUMP_2, 0x1371);
2071 snd_soc_write(codec, RT5663_HP_BIAS, 0xabba);
2072 snd_soc_write(codec, RT5663_CHARGE_PUMP_1, 0x2224);
2073 snd_soc_write(codec, RT5663_ANA_BIAS_CUR_1, 0x7766);
2074 snd_soc_write(codec, RT5663_HP_BIAS, 0xafaa);
2075 snd_soc_write(codec, RT5663_CHARGE_PUMP_2, 0x7777);
2076 snd_soc_update_bits(codec, RT5663_DEPOP_1, 0x3000,
2077 0x3000);
2078 }
2079 break;
2080
2081 case SND_SOC_DAPM_PRE_PMD:
Bard Liao73444722016-10-21 11:02:28 +08002082 if (rt5663->codec_ver == CODEC_VER_1) {
Bard Liaodf7c5212016-09-09 10:33:10 +08002083 snd_soc_update_bits(codec, RT5663_HP_LOGIC_2,
Bard Liao73444722016-10-21 11:02:28 +08002084 RT5663_HP_SIG_SRC1_MASK,
2085 RT5663_HP_SIG_SRC1_REG);
Bard Liaodf7c5212016-09-09 10:33:10 +08002086 } else {
2087 snd_soc_update_bits(codec, RT5663_DEPOP_1, 0x3000, 0x0);
2088 snd_soc_update_bits(codec, RT5663_HP_CHARGE_PUMP_1,
Bard Liao73444722016-10-21 11:02:28 +08002089 RT5663_OVCD_HP_MASK, RT5663_OVCD_HP_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08002090 }
2091 break;
2092
2093 default:
2094 return 0;
2095 }
2096
2097 return 0;
2098}
2099
oder_chiou@realtek.com13257342017-07-07 16:58:59 +08002100static int rt5663_charge_pump_event(struct snd_soc_dapm_widget *w,
2101 struct snd_kcontrol *kcontrol, int event)
2102{
2103 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2104 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2105
2106 switch (event) {
2107 case SND_SOC_DAPM_PRE_PMU:
2108 if (rt5663->codec_ver == CODEC_VER_0)
2109 snd_soc_update_bits(codec, RT5663_DEPOP_1, 0x003b,
2110 0x003b);
2111 break;
2112
2113 case SND_SOC_DAPM_POST_PMD:
2114 if (rt5663->codec_ver == CODEC_VER_0)
2115 snd_soc_update_bits(codec, RT5663_DEPOP_1, 0x003b, 0);
2116 break;
2117
2118 default:
2119 return 0;
2120 }
2121
2122 return 0;
2123}
2124
Bard Liao73444722016-10-21 11:02:28 +08002125static int rt5663_bst2_power(struct snd_soc_dapm_widget *w,
Bard Liaodf7c5212016-09-09 10:33:10 +08002126 struct snd_kcontrol *kcontrol, int event)
2127{
2128 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2129
2130 switch (event) {
2131 case SND_SOC_DAPM_POST_PMU:
2132 snd_soc_update_bits(codec, RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08002133 RT5663_PWR_BST2_MASK | RT5663_PWR_BST2_OP_MASK,
2134 RT5663_PWR_BST2 | RT5663_PWR_BST2_OP);
Bard Liaodf7c5212016-09-09 10:33:10 +08002135 break;
2136
2137 case SND_SOC_DAPM_PRE_PMD:
2138 snd_soc_update_bits(codec, RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08002139 RT5663_PWR_BST2_MASK | RT5663_PWR_BST2_OP_MASK, 0);
Bard Liaodf7c5212016-09-09 10:33:10 +08002140 break;
2141
2142 default:
2143 return 0;
2144 }
2145
2146 return 0;
2147}
2148
2149static int rt5663_pre_div_power(struct snd_soc_dapm_widget *w,
2150 struct snd_kcontrol *kcontrol, int event)
2151{
2152 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
2153
2154 switch (event) {
2155 case SND_SOC_DAPM_POST_PMU:
2156 snd_soc_write(codec, RT5663_PRE_DIV_GATING_1, 0xff00);
2157 snd_soc_write(codec, RT5663_PRE_DIV_GATING_2, 0xfffc);
2158 break;
2159
2160 case SND_SOC_DAPM_PRE_PMD:
2161 snd_soc_write(codec, RT5663_PRE_DIV_GATING_1, 0x0000);
2162 snd_soc_write(codec, RT5663_PRE_DIV_GATING_2, 0x0000);
2163 break;
2164
2165 default:
2166 return 0;
2167 }
2168
2169 return 0;
2170}
2171
2172static const struct snd_soc_dapm_widget rt5663_dapm_widgets[] = {
Bard Liao73444722016-10-21 11:02:28 +08002173 SND_SOC_DAPM_SUPPLY("PLL", RT5663_PWR_ANLG_3, RT5663_PWR_PLL_SHIFT, 0,
Bard Liaodf7c5212016-09-09 10:33:10 +08002174 NULL, 0),
2175
2176 /* micbias */
2177 SND_SOC_DAPM_MICBIAS("MICBIAS1", RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08002178 RT5663_PWR_MB1_SHIFT, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002179 SND_SOC_DAPM_MICBIAS("MICBIAS2", RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08002180 RT5663_PWR_MB2_SHIFT, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002181
2182 /* Input Lines */
2183 SND_SOC_DAPM_INPUT("IN1P"),
2184 SND_SOC_DAPM_INPUT("IN1N"),
2185
2186 /* REC Mixer Power */
2187 SND_SOC_DAPM_SUPPLY("RECMIX1L Power", RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08002188 RT5663_PWR_RECMIX1_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002189
2190 /* ADCs */
2191 SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
2192 SND_SOC_DAPM_SUPPLY("ADC L Power", RT5663_PWR_DIG_1,
Bard Liao73444722016-10-21 11:02:28 +08002193 RT5663_PWR_ADC_L1_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002194 SND_SOC_DAPM_SUPPLY("ADC Clock", RT5663_CHOP_ADC,
Bard Liao73444722016-10-21 11:02:28 +08002195 RT5663_CKGEN_ADCC_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002196
2197 /* ADC Mixer */
2198 SND_SOC_DAPM_MIXER("STO1 ADC MIXL", SND_SOC_NOPM,
2199 0, 0, rt5663_sto1_adc_l_mix,
2200 ARRAY_SIZE(rt5663_sto1_adc_l_mix)),
2201
2202 /* ADC Filter Power */
2203 SND_SOC_DAPM_SUPPLY("STO1 ADC Filter", RT5663_PWR_DIG_2,
Bard Liao73444722016-10-21 11:02:28 +08002204 RT5663_PWR_ADC_S1F_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002205
2206 /* Digital Interface */
Bard Liao73444722016-10-21 11:02:28 +08002207 SND_SOC_DAPM_SUPPLY("I2S", RT5663_PWR_DIG_1, RT5663_PWR_I2S1_SHIFT, 0,
Bard Liaodf7c5212016-09-09 10:33:10 +08002208 NULL, 0),
2209 SND_SOC_DAPM_PGA("IF DAC", SND_SOC_NOPM, 0, 0, NULL, 0),
2210 SND_SOC_DAPM_PGA("IF1 DAC1 L", SND_SOC_NOPM, 0, 0, NULL, 0),
2211 SND_SOC_DAPM_PGA("IF1 DAC1 R", SND_SOC_NOPM, 0, 0, NULL, 0),
2212 SND_SOC_DAPM_PGA("IF1 ADC1", SND_SOC_NOPM, 0, 0, NULL, 0),
2213 SND_SOC_DAPM_PGA("IF ADC", SND_SOC_NOPM, 0, 0, NULL, 0),
2214
2215 /* Audio Interface */
2216 SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM, 0, 0),
2217 SND_SOC_DAPM_AIF_OUT("AIFTX", "AIF Capture", 0, SND_SOC_NOPM, 0, 0),
2218
2219 /* DAC mixer before sound effect */
2220 SND_SOC_DAPM_MIXER("ADDA MIXL", SND_SOC_NOPM, 0, 0, rt5663_adda_l_mix,
2221 ARRAY_SIZE(rt5663_adda_l_mix)),
2222 SND_SOC_DAPM_MIXER("ADDA MIXR", SND_SOC_NOPM, 0, 0, rt5663_adda_r_mix,
2223 ARRAY_SIZE(rt5663_adda_r_mix)),
2224 SND_SOC_DAPM_PGA("DAC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
2225 SND_SOC_DAPM_PGA("DAC R1", SND_SOC_NOPM, 0, 0, NULL, 0),
2226
2227 /* DAC Mixer */
2228 SND_SOC_DAPM_SUPPLY("STO1 DAC Filter", RT5663_PWR_DIG_2,
Bard Liao73444722016-10-21 11:02:28 +08002229 RT5663_PWR_DAC_S1F_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002230 SND_SOC_DAPM_MIXER("STO1 DAC MIXL", SND_SOC_NOPM, 0, 0,
2231 rt5663_sto1_dac_l_mix, ARRAY_SIZE(rt5663_sto1_dac_l_mix)),
2232 SND_SOC_DAPM_MIXER("STO1 DAC MIXR", SND_SOC_NOPM, 0, 0,
2233 rt5663_sto1_dac_r_mix, ARRAY_SIZE(rt5663_sto1_dac_r_mix)),
2234
2235 /* DACs */
2236 SND_SOC_DAPM_SUPPLY("STO1 DAC L Power", RT5663_PWR_DIG_1,
Bard Liao73444722016-10-21 11:02:28 +08002237 RT5663_PWR_DAC_L1_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002238 SND_SOC_DAPM_SUPPLY("STO1 DAC R Power", RT5663_PWR_DIG_1,
Bard Liao73444722016-10-21 11:02:28 +08002239 RT5663_PWR_DAC_R1_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002240 SND_SOC_DAPM_DAC("DAC L", NULL, SND_SOC_NOPM, 0, 0),
2241 SND_SOC_DAPM_DAC("DAC R", NULL, SND_SOC_NOPM, 0, 0),
2242
2243 /* Headphone*/
oder_chiou@realtek.com13257342017-07-07 16:58:59 +08002244 SND_SOC_DAPM_SUPPLY("HP Charge Pump", SND_SOC_NOPM, 0, 0,
2245 rt5663_charge_pump_event, SND_SOC_DAPM_PRE_PMU |
2246 SND_SOC_DAPM_POST_PMD),
Bard Liaodf7c5212016-09-09 10:33:10 +08002247 SND_SOC_DAPM_PGA_S("HP Amp", 1, SND_SOC_NOPM, 0, 0, rt5663_hp_event,
2248 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
2249
2250 /* Output Lines */
2251 SND_SOC_DAPM_OUTPUT("HPOL"),
2252 SND_SOC_DAPM_OUTPUT("HPOR"),
2253};
2254
Bard Liao73444722016-10-21 11:02:28 +08002255static const struct snd_soc_dapm_widget rt5663_v2_specific_dapm_widgets[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08002256 SND_SOC_DAPM_SUPPLY("LDO2", RT5663_PWR_ANLG_3,
Bard Liao73444722016-10-21 11:02:28 +08002257 RT5663_PWR_LDO2_SHIFT, 0, NULL, 0),
2258 SND_SOC_DAPM_SUPPLY("Mic Det Power", RT5663_PWR_VOL,
2259 RT5663_V2_PWR_MIC_DET_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002260 SND_SOC_DAPM_SUPPLY("LDO DAC", RT5663_PWR_DIG_1,
Bard Liao73444722016-10-21 11:02:28 +08002261 RT5663_PWR_LDO_DACREF_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002262
2263 /* ASRC */
2264 SND_SOC_DAPM_SUPPLY("I2S ASRC", RT5663_ASRC_1,
Bard Liao73444722016-10-21 11:02:28 +08002265 RT5663_I2S1_ASRC_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002266 SND_SOC_DAPM_SUPPLY("DAC ASRC", RT5663_ASRC_1,
Bard Liao73444722016-10-21 11:02:28 +08002267 RT5663_DAC_STO1_ASRC_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002268 SND_SOC_DAPM_SUPPLY("ADC ASRC", RT5663_ASRC_1,
Bard Liao73444722016-10-21 11:02:28 +08002269 RT5663_ADC_STO1_ASRC_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002270
2271 /* Input Lines */
2272 SND_SOC_DAPM_INPUT("IN2P"),
2273 SND_SOC_DAPM_INPUT("IN2N"),
2274
2275 /* Boost */
2276 SND_SOC_DAPM_PGA("BST1 CBJ", SND_SOC_NOPM, 0, 0, NULL, 0),
2277 SND_SOC_DAPM_SUPPLY("CBJ Power", RT5663_PWR_ANLG_3,
Bard Liao73444722016-10-21 11:02:28 +08002278 RT5663_PWR_CBJ_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002279 SND_SOC_DAPM_PGA("BST2", SND_SOC_NOPM, 0, 0, NULL, 0),
2280 SND_SOC_DAPM_SUPPLY("BST2 Power", SND_SOC_NOPM, 0, 0,
Bard Liao73444722016-10-21 11:02:28 +08002281 rt5663_bst2_power, SND_SOC_DAPM_PRE_PMD |
Bard Liaodf7c5212016-09-09 10:33:10 +08002282 SND_SOC_DAPM_POST_PMU),
2283
2284 /* REC Mixer */
Bard Liao73444722016-10-21 11:02:28 +08002285 SND_SOC_DAPM_MIXER("RECMIX1L", SND_SOC_NOPM, 0, 0, rt5663_recmix1l,
2286 ARRAY_SIZE(rt5663_recmix1l)),
2287 SND_SOC_DAPM_MIXER("RECMIX1R", SND_SOC_NOPM, 0, 0, rt5663_recmix1r,
2288 ARRAY_SIZE(rt5663_recmix1r)),
Bard Liaodf7c5212016-09-09 10:33:10 +08002289 SND_SOC_DAPM_SUPPLY("RECMIX1R Power", RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08002290 RT5663_PWR_RECMIX2_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002291
2292 /* ADC */
2293 SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
2294 SND_SOC_DAPM_SUPPLY("ADC R Power", RT5663_PWR_DIG_1,
Bard Liao73444722016-10-21 11:02:28 +08002295 RT5663_PWR_ADC_R1_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002296
2297 /* ADC Mux */
2298 SND_SOC_DAPM_PGA("STO1 ADC L1", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08002299 RT5663_STO1_ADC_L1_SRC_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002300 SND_SOC_DAPM_PGA("STO1 ADC R1", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08002301 RT5663_STO1_ADC_R1_SRC_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002302 SND_SOC_DAPM_PGA("STO1 ADC L2", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08002303 RT5663_STO1_ADC_L2_SRC_SHIFT, 1, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002304 SND_SOC_DAPM_PGA("STO1 ADC R2", RT5663_STO1_ADC_MIXER,
Bard Liao73444722016-10-21 11:02:28 +08002305 RT5663_STO1_ADC_R2_SRC_SHIFT, 1, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002306
2307 SND_SOC_DAPM_MUX("STO1 ADC L Mux", SND_SOC_NOPM, 0, 0,
Bard Liao73444722016-10-21 11:02:28 +08002308 &rt5663_sto1_adcl_mux),
Bard Liaodf7c5212016-09-09 10:33:10 +08002309 SND_SOC_DAPM_MUX("STO1 ADC R Mux", SND_SOC_NOPM, 0, 0,
Bard Liao73444722016-10-21 11:02:28 +08002310 &rt5663_sto1_adcr_mux),
Bard Liaodf7c5212016-09-09 10:33:10 +08002311
2312 /* ADC Mix */
2313 SND_SOC_DAPM_MIXER("STO1 ADC MIXR", SND_SOC_NOPM, 0, 0,
Bard Liao73444722016-10-21 11:02:28 +08002314 rt5663_sto1_adc_r_mix, ARRAY_SIZE(rt5663_sto1_adc_r_mix)),
Bard Liaodf7c5212016-09-09 10:33:10 +08002315
2316 /* Analog DAC Clock */
2317 SND_SOC_DAPM_SUPPLY("DAC Clock", RT5663_CHOP_DAC_L,
Bard Liao73444722016-10-21 11:02:28 +08002318 RT5663_CKGEN_DAC1_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002319
2320 /* Headphone out */
2321 SND_SOC_DAPM_SWITCH("HPO Playback", SND_SOC_NOPM, 0, 0,
Bard Liao73444722016-10-21 11:02:28 +08002322 &rt5663_hpo_switch),
Bard Liaodf7c5212016-09-09 10:33:10 +08002323};
2324
2325static const struct snd_soc_dapm_widget rt5663_specific_dapm_widgets[] = {
2326 /* System Clock Pre Divider Gating */
2327 SND_SOC_DAPM_SUPPLY("Pre Div Power", SND_SOC_NOPM, 0, 0,
2328 rt5663_pre_div_power, SND_SOC_DAPM_POST_PMU |
2329 SND_SOC_DAPM_PRE_PMD),
2330
2331 /* LDO */
2332 SND_SOC_DAPM_SUPPLY("LDO ADC", RT5663_PWR_DIG_1,
Bard Liao73444722016-10-21 11:02:28 +08002333 RT5663_PWR_LDO_DACREF_SHIFT, 0, NULL, 0),
Bard Liaodf7c5212016-09-09 10:33:10 +08002334
2335 /* ASRC */
2336 SND_SOC_DAPM_SUPPLY("I2S ASRC", RT5663_ASRC_1,
2337 RT5663_I2S1_ASRC_SHIFT, 0, NULL, 0),
2338 SND_SOC_DAPM_SUPPLY("DAC ASRC", RT5663_ASRC_1,
2339 RT5663_DAC_STO1_ASRC_SHIFT, 0, NULL, 0),
2340 SND_SOC_DAPM_SUPPLY("ADC ASRC", RT5663_ASRC_1,
2341 RT5663_ADC_STO1_ASRC_SHIFT, 0, NULL, 0),
2342
2343 /* Boost */
2344 SND_SOC_DAPM_PGA("BST1", SND_SOC_NOPM, 0, 0, NULL, 0),
2345
2346 /* STO ADC */
2347 SND_SOC_DAPM_PGA("STO1 ADC L1", SND_SOC_NOPM, 0, 0, NULL, 0),
2348 SND_SOC_DAPM_PGA("STO1 ADC L2", SND_SOC_NOPM, 0, 0, NULL, 0),
2349
2350 /* Analog DAC source */
2351 SND_SOC_DAPM_MUX("DAC L Mux", SND_SOC_NOPM, 0, 0, &rt5663_alg_dacl_mux),
2352 SND_SOC_DAPM_MUX("DAC R Mux", SND_SOC_NOPM, 0, 0, &rt5663_alg_dacr_mux),
2353};
2354
2355static const struct snd_soc_dapm_route rt5663_dapm_routes[] = {
2356 /* PLL */
2357 { "I2S", NULL, "PLL", rt5663_is_sys_clk_from_pll },
2358
2359 /* ASRC */
2360 { "STO1 ADC Filter", NULL, "ADC ASRC", rt5663_is_using_asrc },
2361 { "STO1 DAC Filter", NULL, "DAC ASRC", rt5663_is_using_asrc },
2362 { "I2S", NULL, "I2S ASRC", rt5663_i2s_use_asrc },
2363
2364 { "ADC L", NULL, "ADC L Power" },
2365 { "ADC L", NULL, "ADC Clock" },
2366
2367 { "STO1 ADC L2", NULL, "STO1 DAC MIXL" },
2368
2369 { "STO1 ADC MIXL", "ADC1 Switch", "STO1 ADC L1" },
2370 { "STO1 ADC MIXL", "ADC2 Switch", "STO1 ADC L2" },
2371 { "STO1 ADC MIXL", NULL, "STO1 ADC Filter" },
2372
2373 { "IF1 ADC1", NULL, "STO1 ADC MIXL" },
2374 { "IF ADC", NULL, "IF1 ADC1" },
2375 { "AIFTX", NULL, "IF ADC" },
2376 { "AIFTX", NULL, "I2S" },
2377
2378 { "AIFRX", NULL, "I2S" },
2379 { "IF DAC", NULL, "AIFRX" },
2380 { "IF1 DAC1 L", NULL, "IF DAC" },
2381 { "IF1 DAC1 R", NULL, "IF DAC" },
2382
2383 { "ADDA MIXL", "ADC L Switch", "STO1 ADC MIXL" },
2384 { "ADDA MIXL", "DAC L Switch", "IF1 DAC1 L" },
2385 { "ADDA MIXL", NULL, "STO1 DAC Filter" },
2386 { "ADDA MIXL", NULL, "STO1 DAC L Power" },
2387 { "ADDA MIXR", "DAC R Switch", "IF1 DAC1 R" },
2388 { "ADDA MIXR", NULL, "STO1 DAC Filter" },
2389 { "ADDA MIXR", NULL, "STO1 DAC R Power" },
2390
2391 { "DAC L1", NULL, "ADDA MIXL" },
2392 { "DAC R1", NULL, "ADDA MIXR" },
2393
2394 { "STO1 DAC MIXL", "DAC L Switch", "DAC L1" },
Bard Liaodf7c5212016-09-09 10:33:10 +08002395 { "STO1 DAC MIXL", NULL, "STO1 DAC L Power" },
2396 { "STO1 DAC MIXL", NULL, "STO1 DAC Filter" },
2397 { "STO1 DAC MIXR", "DAC R Switch", "DAC R1" },
Bard Liaodf7c5212016-09-09 10:33:10 +08002398 { "STO1 DAC MIXR", NULL, "STO1 DAC R Power" },
2399 { "STO1 DAC MIXR", NULL, "STO1 DAC Filter" },
2400
oder_chiou@realtek.com13257342017-07-07 16:58:59 +08002401 { "HP Amp", NULL, "HP Charge Pump" },
Bard Liaodf7c5212016-09-09 10:33:10 +08002402 { "HP Amp", NULL, "DAC L" },
2403 { "HP Amp", NULL, "DAC R" },
2404};
2405
Bard Liao73444722016-10-21 11:02:28 +08002406static const struct snd_soc_dapm_route rt5663_v2_specific_dapm_routes[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08002407 { "MICBIAS1", NULL, "LDO2" },
2408 { "MICBIAS2", NULL, "LDO2" },
2409
2410 { "BST1 CBJ", NULL, "IN1P" },
2411 { "BST1 CBJ", NULL, "IN1N" },
2412 { "BST1 CBJ", NULL, "CBJ Power" },
2413
2414 { "BST2", NULL, "IN2P" },
2415 { "BST2", NULL, "IN2N" },
2416 { "BST2", NULL, "BST2 Power" },
2417
2418 { "RECMIX1L", "BST2 Switch", "BST2" },
2419 { "RECMIX1L", "BST1 CBJ Switch", "BST1 CBJ" },
2420 { "RECMIX1L", NULL, "RECMIX1L Power" },
2421 { "RECMIX1R", "BST2 Switch", "BST2" },
2422 { "RECMIX1R", NULL, "RECMIX1R Power" },
2423
2424 { "ADC L", NULL, "RECMIX1L" },
2425 { "ADC R", NULL, "RECMIX1R" },
2426 { "ADC R", NULL, "ADC R Power" },
2427 { "ADC R", NULL, "ADC Clock" },
2428
2429 { "STO1 ADC L Mux", "ADC L", "ADC L" },
2430 { "STO1 ADC L Mux", "ADC R", "ADC R" },
2431 { "STO1 ADC L1", NULL, "STO1 ADC L Mux" },
2432
2433 { "STO1 ADC R Mux", "ADC L", "ADC L" },
2434 { "STO1 ADC R Mux", "ADC R", "ADC R" },
2435 { "STO1 ADC R1", NULL, "STO1 ADC R Mux" },
2436 { "STO1 ADC R2", NULL, "STO1 DAC MIXR" },
2437
2438 { "STO1 ADC MIXR", "ADC1 Switch", "STO1 ADC R1" },
2439 { "STO1 ADC MIXR", "ADC2 Switch", "STO1 ADC R2" },
2440 { "STO1 ADC MIXR", NULL, "STO1 ADC Filter" },
2441
2442 { "IF1 ADC1", NULL, "STO1 ADC MIXR" },
2443
2444 { "ADDA MIXR", "ADC R Switch", "STO1 ADC MIXR" },
2445
2446 { "DAC L", NULL, "STO1 DAC MIXL" },
2447 { "DAC L", NULL, "LDO DAC" },
2448 { "DAC L", NULL, "DAC Clock" },
2449 { "DAC R", NULL, "STO1 DAC MIXR" },
2450 { "DAC R", NULL, "LDO DAC" },
2451 { "DAC R", NULL, "DAC Clock" },
2452
2453 { "HPO Playback", "Switch", "HP Amp" },
2454 { "HPOL", NULL, "HPO Playback" },
2455 { "HPOR", NULL, "HPO Playback" },
2456};
2457
2458static const struct snd_soc_dapm_route rt5663_specific_dapm_routes[] = {
2459 { "I2S", NULL, "Pre Div Power" },
2460
2461 { "BST1", NULL, "IN1P" },
2462 { "BST1", NULL, "IN1N" },
2463 { "BST1", NULL, "RECMIX1L Power" },
2464
2465 { "ADC L", NULL, "BST1" },
2466
2467 { "STO1 ADC L1", NULL, "ADC L" },
2468
2469 { "DAC L Mux", "DAC L", "DAC L1" },
2470 { "DAC L Mux", "STO DAC MIXL", "STO1 DAC MIXL" },
2471 { "DAC R Mux", "DAC R", "DAC R1"},
2472 { "DAC R Mux", "STO DAC MIXR", "STO1 DAC MIXR" },
2473
2474 { "DAC L", NULL, "DAC L Mux" },
2475 { "DAC R", NULL, "DAC R Mux" },
2476
2477 { "HPOL", NULL, "HP Amp" },
2478 { "HPOR", NULL, "HP Amp" },
2479};
2480
2481static int rt5663_hw_params(struct snd_pcm_substream *substream,
2482 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
2483{
2484 struct snd_soc_codec *codec = dai->codec;
2485 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2486 unsigned int val_len = 0;
2487 int pre_div;
2488
2489 rt5663->lrck = params_rate(params);
2490
2491 dev_dbg(dai->dev, "bclk is %dHz and sysclk is %dHz\n",
2492 rt5663->lrck, rt5663->sysclk);
2493
2494 pre_div = rl6231_get_clk_info(rt5663->sysclk, rt5663->lrck);
2495 if (pre_div < 0) {
2496 dev_err(codec->dev, "Unsupported clock setting %d for DAI %d\n",
2497 rt5663->lrck, dai->id);
2498 return -EINVAL;
2499 }
2500
2501 dev_dbg(dai->dev, "pre_div is %d for iis %d\n", pre_div, dai->id);
2502
2503 switch (params_width(params)) {
2504 case 8:
Bard Liao73444722016-10-21 11:02:28 +08002505 val_len = RT5663_I2S_DL_8;
Bard Liaodf7c5212016-09-09 10:33:10 +08002506 break;
2507 case 16:
Bard Liao73444722016-10-21 11:02:28 +08002508 val_len = RT5663_I2S_DL_16;
Bard Liaodf7c5212016-09-09 10:33:10 +08002509 break;
2510 case 20:
Bard Liao73444722016-10-21 11:02:28 +08002511 val_len = RT5663_I2S_DL_20;
Bard Liaodf7c5212016-09-09 10:33:10 +08002512 break;
2513 case 24:
Bard Liao73444722016-10-21 11:02:28 +08002514 val_len = RT5663_I2S_DL_24;
Bard Liaodf7c5212016-09-09 10:33:10 +08002515 break;
2516 default:
2517 return -EINVAL;
2518 }
2519
2520 snd_soc_update_bits(codec, RT5663_I2S1_SDP,
Bard Liao73444722016-10-21 11:02:28 +08002521 RT5663_I2S_DL_MASK, val_len);
Bard Liaodf7c5212016-09-09 10:33:10 +08002522
2523 snd_soc_update_bits(codec, RT5663_ADDA_CLK_1,
Bard Liao73444722016-10-21 11:02:28 +08002524 RT5663_I2S_PD1_MASK, pre_div << RT5663_I2S_PD1_SHIFT);
Bard Liaodf7c5212016-09-09 10:33:10 +08002525
2526 return 0;
2527}
2528
2529static int rt5663_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2530{
2531 struct snd_soc_codec *codec = dai->codec;
2532 unsigned int reg_val = 0;
2533
2534 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
2535 case SND_SOC_DAIFMT_CBM_CFM:
2536 break;
2537 case SND_SOC_DAIFMT_CBS_CFS:
Bard Liao73444722016-10-21 11:02:28 +08002538 reg_val |= RT5663_I2S_MS_S;
Bard Liaodf7c5212016-09-09 10:33:10 +08002539 break;
2540 default:
2541 return -EINVAL;
2542 }
2543
2544 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2545 case SND_SOC_DAIFMT_NB_NF:
2546 break;
2547 case SND_SOC_DAIFMT_IB_NF:
Bard Liao73444722016-10-21 11:02:28 +08002548 reg_val |= RT5663_I2S_BP_INV;
Bard Liaodf7c5212016-09-09 10:33:10 +08002549 break;
2550 default:
2551 return -EINVAL;
2552 }
2553
2554 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
2555 case SND_SOC_DAIFMT_I2S:
2556 break;
2557 case SND_SOC_DAIFMT_LEFT_J:
Bard Liao73444722016-10-21 11:02:28 +08002558 reg_val |= RT5663_I2S_DF_LEFT;
Bard Liaodf7c5212016-09-09 10:33:10 +08002559 break;
2560 case SND_SOC_DAIFMT_DSP_A:
Bard Liao73444722016-10-21 11:02:28 +08002561 reg_val |= RT5663_I2S_DF_PCM_A;
Bard Liaodf7c5212016-09-09 10:33:10 +08002562 break;
2563 case SND_SOC_DAIFMT_DSP_B:
Bard Liao73444722016-10-21 11:02:28 +08002564 reg_val |= RT5663_I2S_DF_PCM_B;
Bard Liaodf7c5212016-09-09 10:33:10 +08002565 break;
2566 default:
2567 return -EINVAL;
2568 }
2569
Bard Liao73444722016-10-21 11:02:28 +08002570 snd_soc_update_bits(codec, RT5663_I2S1_SDP, RT5663_I2S_MS_MASK |
2571 RT5663_I2S_BP_MASK | RT5663_I2S_DF_MASK, reg_val);
Bard Liaodf7c5212016-09-09 10:33:10 +08002572
2573 return 0;
2574}
2575
2576static int rt5663_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
2577 unsigned int freq, int dir)
2578{
2579 struct snd_soc_codec *codec = dai->codec;
2580 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2581 unsigned int reg_val = 0;
2582
2583 if (freq == rt5663->sysclk && clk_id == rt5663->sysclk_src)
2584 return 0;
2585
2586 switch (clk_id) {
2587 case RT5663_SCLK_S_MCLK:
2588 reg_val |= RT5663_SCLK_SRC_MCLK;
2589 break;
2590 case RT5663_SCLK_S_PLL1:
2591 reg_val |= RT5663_SCLK_SRC_PLL1;
2592 break;
2593 case RT5663_SCLK_S_RCCLK:
2594 reg_val |= RT5663_SCLK_SRC_RCCLK;
2595 break;
2596 default:
2597 dev_err(codec->dev, "Invalid clock id (%d)\n", clk_id);
2598 return -EINVAL;
2599 }
Bard Liao73444722016-10-21 11:02:28 +08002600 snd_soc_update_bits(codec, RT5663_GLB_CLK, RT5663_SCLK_SRC_MASK,
Bard Liaodf7c5212016-09-09 10:33:10 +08002601 reg_val);
2602 rt5663->sysclk = freq;
2603 rt5663->sysclk_src = clk_id;
2604
2605 dev_dbg(codec->dev, "Sysclk is %dHz and clock id is %d\n",
2606 freq, clk_id);
2607
2608 return 0;
2609}
2610
2611static int rt5663_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
2612 unsigned int freq_in, unsigned int freq_out)
2613{
2614 struct snd_soc_codec *codec = dai->codec;
2615 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2616 struct rl6231_pll_code pll_code;
2617 int ret;
2618 int mask, shift, val;
2619
2620 if (source == rt5663->pll_src && freq_in == rt5663->pll_in &&
2621 freq_out == rt5663->pll_out)
2622 return 0;
2623
2624 if (!freq_in || !freq_out) {
2625 dev_dbg(codec->dev, "PLL disabled\n");
2626
2627 rt5663->pll_in = 0;
2628 rt5663->pll_out = 0;
2629 snd_soc_update_bits(codec, RT5663_GLB_CLK,
2630 RT5663_SCLK_SRC_MASK, RT5663_SCLK_SRC_MCLK);
2631 return 0;
2632 }
2633
Bard Liao73444722016-10-21 11:02:28 +08002634 switch (rt5663->codec_ver) {
2635 case CODEC_VER_1:
2636 mask = RT5663_V2_PLL1_SRC_MASK;
2637 shift = RT5663_V2_PLL1_SRC_SHIFT;
Bard Liaodf7c5212016-09-09 10:33:10 +08002638 break;
Bard Liao73444722016-10-21 11:02:28 +08002639 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08002640 mask = RT5663_PLL1_SRC_MASK;
2641 shift = RT5663_PLL1_SRC_SHIFT;
2642 break;
2643 default:
Bard Liao73444722016-10-21 11:02:28 +08002644 dev_err(codec->dev, "Unknown CODEC Version\n");
Arnd Bergmann56efaed2016-09-15 17:42:21 +02002645 return -EINVAL;
Bard Liaodf7c5212016-09-09 10:33:10 +08002646 }
2647
2648 switch (source) {
2649 case RT5663_PLL1_S_MCLK:
2650 val = 0x0;
2651 break;
2652 case RT5663_PLL1_S_BCLK1:
2653 val = 0x1;
2654 break;
2655 default:
2656 dev_err(codec->dev, "Unknown PLL source %d\n", source);
2657 return -EINVAL;
2658 }
2659 snd_soc_update_bits(codec, RT5663_GLB_CLK, mask, (val << shift));
2660
2661 ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
2662 if (ret < 0) {
2663 dev_err(codec->dev, "Unsupport input clock %d\n", freq_in);
2664 return ret;
2665 }
2666
2667 dev_dbg(codec->dev, "bypass=%d m=%d n=%d k=%d\n", pll_code.m_bp,
2668 (pll_code.m_bp ? 0 : pll_code.m_code), pll_code.n_code,
2669 pll_code.k_code);
2670
2671 snd_soc_write(codec, RT5663_PLL_1,
Bard Liao73444722016-10-21 11:02:28 +08002672 pll_code.n_code << RT5663_PLL_N_SHIFT | pll_code.k_code);
Bard Liaodf7c5212016-09-09 10:33:10 +08002673 snd_soc_write(codec, RT5663_PLL_2,
Bard Liao73444722016-10-21 11:02:28 +08002674 (pll_code.m_bp ? 0 : pll_code.m_code) << RT5663_PLL_M_SHIFT |
2675 pll_code.m_bp << RT5663_PLL_M_BP_SHIFT);
Bard Liaodf7c5212016-09-09 10:33:10 +08002676
2677 rt5663->pll_in = freq_in;
2678 rt5663->pll_out = freq_out;
2679 rt5663->pll_src = source;
2680
2681 return 0;
2682}
2683
2684static int rt5663_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
2685 unsigned int rx_mask, int slots, int slot_width)
2686{
2687 struct snd_soc_codec *codec = dai->codec;
2688 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2689 unsigned int val = 0, reg;
2690
2691 if (rx_mask || tx_mask)
Bard Liao73444722016-10-21 11:02:28 +08002692 val |= RT5663_TDM_MODE_TDM;
Bard Liaodf7c5212016-09-09 10:33:10 +08002693
2694 switch (slots) {
2695 case 4:
Bard Liao73444722016-10-21 11:02:28 +08002696 val |= RT5663_TDM_IN_CH_4;
2697 val |= RT5663_TDM_OUT_CH_4;
Bard Liaodf7c5212016-09-09 10:33:10 +08002698 break;
2699 case 6:
Bard Liao73444722016-10-21 11:02:28 +08002700 val |= RT5663_TDM_IN_CH_6;
2701 val |= RT5663_TDM_OUT_CH_6;
Bard Liaodf7c5212016-09-09 10:33:10 +08002702 break;
2703 case 8:
Bard Liao73444722016-10-21 11:02:28 +08002704 val |= RT5663_TDM_IN_CH_8;
2705 val |= RT5663_TDM_OUT_CH_8;
Bard Liaodf7c5212016-09-09 10:33:10 +08002706 break;
2707 case 2:
2708 break;
2709 default:
2710 return -EINVAL;
2711 }
2712
2713 switch (slot_width) {
2714 case 20:
Bard Liao73444722016-10-21 11:02:28 +08002715 val |= RT5663_TDM_IN_LEN_20;
2716 val |= RT5663_TDM_OUT_LEN_20;
Bard Liaodf7c5212016-09-09 10:33:10 +08002717 break;
2718 case 24:
Bard Liao73444722016-10-21 11:02:28 +08002719 val |= RT5663_TDM_IN_LEN_24;
2720 val |= RT5663_TDM_OUT_LEN_24;
Bard Liaodf7c5212016-09-09 10:33:10 +08002721 break;
2722 case 32:
Bard Liao73444722016-10-21 11:02:28 +08002723 val |= RT5663_TDM_IN_LEN_32;
2724 val |= RT5663_TDM_OUT_LEN_32;
Bard Liaodf7c5212016-09-09 10:33:10 +08002725 break;
2726 case 16:
2727 break;
2728 default:
2729 return -EINVAL;
2730 }
2731
Bard Liao73444722016-10-21 11:02:28 +08002732 switch (rt5663->codec_ver) {
2733 case CODEC_VER_1:
Bard Liaodf7c5212016-09-09 10:33:10 +08002734 reg = RT5663_TDM_2;
2735 break;
Bard Liao73444722016-10-21 11:02:28 +08002736 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08002737 reg = RT5663_TDM_1;
2738 break;
2739 default:
Bard Liao73444722016-10-21 11:02:28 +08002740 dev_err(codec->dev, "Unknown CODEC Version\n");
Arnd Bergmann56efaed2016-09-15 17:42:21 +02002741 return -EINVAL;
Bard Liaodf7c5212016-09-09 10:33:10 +08002742 }
2743
Bard Liao73444722016-10-21 11:02:28 +08002744 snd_soc_update_bits(codec, reg, RT5663_TDM_MODE_MASK |
2745 RT5663_TDM_IN_CH_MASK | RT5663_TDM_OUT_CH_MASK |
2746 RT5663_TDM_IN_LEN_MASK | RT5663_TDM_OUT_LEN_MASK, val);
Bard Liaodf7c5212016-09-09 10:33:10 +08002747
2748 return 0;
2749}
2750
2751static int rt5663_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
2752{
2753 struct snd_soc_codec *codec = dai->codec;
2754 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2755 unsigned int reg;
2756
2757 dev_dbg(codec->dev, "%s ratio = %d\n", __func__, ratio);
2758
Bard Liao73444722016-10-21 11:02:28 +08002759 if (rt5663->codec_ver == CODEC_VER_1)
2760 reg = RT5663_TDM_9;
Bard Liaodf7c5212016-09-09 10:33:10 +08002761 else
2762 reg = RT5663_TDM_5;
2763
2764 switch (ratio) {
2765 case 32:
2766 snd_soc_update_bits(codec, reg,
2767 RT5663_TDM_LENGTN_MASK,
2768 RT5663_TDM_LENGTN_16);
2769 break;
2770 case 40:
2771 snd_soc_update_bits(codec, reg,
2772 RT5663_TDM_LENGTN_MASK,
2773 RT5663_TDM_LENGTN_20);
2774 break;
2775 case 48:
2776 snd_soc_update_bits(codec, reg,
2777 RT5663_TDM_LENGTN_MASK,
2778 RT5663_TDM_LENGTN_24);
2779 break;
2780 case 64:
2781 snd_soc_update_bits(codec, reg,
2782 RT5663_TDM_LENGTN_MASK,
2783 RT5663_TDM_LENGTN_32);
2784 break;
2785 default:
2786 dev_err(codec->dev, "Invalid ratio!\n");
2787 return -EINVAL;
2788 }
2789
2790 return 0;
2791}
2792
2793static int rt5663_set_bias_level(struct snd_soc_codec *codec,
2794 enum snd_soc_bias_level level)
2795{
2796 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2797
2798 switch (level) {
2799 case SND_SOC_BIAS_ON:
2800 snd_soc_update_bits(codec, RT5663_PWR_ANLG_1,
Bard Liao73444722016-10-21 11:02:28 +08002801 RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK,
2802 RT5663_PWR_FV1 | RT5663_PWR_FV2);
Bard Liaodf7c5212016-09-09 10:33:10 +08002803 break;
2804
2805 case SND_SOC_BIAS_PREPARE:
Bard Liao73444722016-10-21 11:02:28 +08002806 if (rt5663->codec_ver == CODEC_VER_1) {
Bard Liaodf7c5212016-09-09 10:33:10 +08002807 snd_soc_update_bits(codec, RT5663_DIG_MISC,
Bard Liao73444722016-10-21 11:02:28 +08002808 RT5663_DIG_GATE_CTRL_MASK,
2809 RT5663_DIG_GATE_CTRL_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08002810 snd_soc_update_bits(codec, RT5663_SIG_CLK_DET,
Bard Liao73444722016-10-21 11:02:28 +08002811 RT5663_EN_ANA_CLK_DET_MASK |
2812 RT5663_PWR_CLK_DET_MASK,
2813 RT5663_EN_ANA_CLK_DET_AUTO |
2814 RT5663_PWR_CLK_DET_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08002815 }
2816 break;
2817
2818 case SND_SOC_BIAS_STANDBY:
Bard Liao73444722016-10-21 11:02:28 +08002819 if (rt5663->codec_ver == CODEC_VER_1)
Bard Liaodf7c5212016-09-09 10:33:10 +08002820 snd_soc_update_bits(codec, RT5663_DIG_MISC,
Bard Liao73444722016-10-21 11:02:28 +08002821 RT5663_DIG_GATE_CTRL_MASK,
2822 RT5663_DIG_GATE_CTRL_DIS);
Bard Liaodf7c5212016-09-09 10:33:10 +08002823 snd_soc_update_bits(codec, RT5663_PWR_ANLG_1,
Bard Liao73444722016-10-21 11:02:28 +08002824 RT5663_PWR_VREF1_MASK | RT5663_PWR_VREF2_MASK |
2825 RT5663_PWR_FV1_MASK | RT5663_PWR_FV2_MASK |
2826 RT5663_PWR_MB_MASK, RT5663_PWR_VREF1 |
2827 RT5663_PWR_VREF2 | RT5663_PWR_MB);
Bard Liaodf7c5212016-09-09 10:33:10 +08002828 usleep_range(10000, 10005);
Bard Liao73444722016-10-21 11:02:28 +08002829 if (rt5663->codec_ver == CODEC_VER_1) {
Bard Liaodf7c5212016-09-09 10:33:10 +08002830 snd_soc_update_bits(codec, RT5663_SIG_CLK_DET,
Bard Liao73444722016-10-21 11:02:28 +08002831 RT5663_EN_ANA_CLK_DET_MASK |
2832 RT5663_PWR_CLK_DET_MASK,
2833 RT5663_EN_ANA_CLK_DET_DIS |
2834 RT5663_PWR_CLK_DET_DIS);
Bard Liaodf7c5212016-09-09 10:33:10 +08002835 }
2836 break;
2837
2838 case SND_SOC_BIAS_OFF:
2839 snd_soc_update_bits(codec, RT5663_PWR_ANLG_1,
Bard Liao73444722016-10-21 11:02:28 +08002840 RT5663_PWR_VREF1_MASK | RT5663_PWR_VREF2_MASK |
2841 RT5663_PWR_FV1 | RT5663_PWR_FV2, 0x0);
Bard Liaodf7c5212016-09-09 10:33:10 +08002842 break;
2843
2844 default:
2845 break;
2846 }
2847
2848 return 0;
2849}
2850
2851static int rt5663_probe(struct snd_soc_codec *codec)
2852{
2853 struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
2854 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2855
2856 rt5663->codec = codec;
2857
Bard Liao73444722016-10-21 11:02:28 +08002858 switch (rt5663->codec_ver) {
2859 case CODEC_VER_1:
Bard Liaodf7c5212016-09-09 10:33:10 +08002860 snd_soc_dapm_new_controls(dapm,
Bard Liao73444722016-10-21 11:02:28 +08002861 rt5663_v2_specific_dapm_widgets,
2862 ARRAY_SIZE(rt5663_v2_specific_dapm_widgets));
Bard Liaodf7c5212016-09-09 10:33:10 +08002863 snd_soc_dapm_add_routes(dapm,
Bard Liao73444722016-10-21 11:02:28 +08002864 rt5663_v2_specific_dapm_routes,
2865 ARRAY_SIZE(rt5663_v2_specific_dapm_routes));
2866 snd_soc_add_codec_controls(codec, rt5663_v2_specific_controls,
2867 ARRAY_SIZE(rt5663_v2_specific_controls));
Bard Liaodf7c5212016-09-09 10:33:10 +08002868 break;
Bard Liao73444722016-10-21 11:02:28 +08002869 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08002870 snd_soc_dapm_new_controls(dapm,
2871 rt5663_specific_dapm_widgets,
2872 ARRAY_SIZE(rt5663_specific_dapm_widgets));
2873 snd_soc_dapm_add_routes(dapm,
2874 rt5663_specific_dapm_routes,
2875 ARRAY_SIZE(rt5663_specific_dapm_routes));
2876 snd_soc_add_codec_controls(codec, rt5663_specific_controls,
2877 ARRAY_SIZE(rt5663_specific_controls));
2878 break;
2879 }
2880
2881 return 0;
2882}
2883
2884static int rt5663_remove(struct snd_soc_codec *codec)
2885{
2886 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2887
2888 regmap_write(rt5663->regmap, RT5663_RESET, 0);
2889
2890 return 0;
2891}
2892
2893#ifdef CONFIG_PM
2894static int rt5663_suspend(struct snd_soc_codec *codec)
2895{
2896 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2897
2898 regcache_cache_only(rt5663->regmap, true);
2899 regcache_mark_dirty(rt5663->regmap);
2900
2901 return 0;
2902}
2903
2904static int rt5663_resume(struct snd_soc_codec *codec)
2905{
2906 struct rt5663_priv *rt5663 = snd_soc_codec_get_drvdata(codec);
2907
2908 regcache_cache_only(rt5663->regmap, false);
2909 regcache_sync(rt5663->regmap);
2910
Oder Chiou17616ce2017-06-12 11:02:17 +08002911 rt5663_irq(0, rt5663);
2912
Bard Liaodf7c5212016-09-09 10:33:10 +08002913 return 0;
2914}
2915#else
2916#define rt5663_suspend NULL
2917#define rt5663_resume NULL
2918#endif
2919
2920#define RT5663_STEREO_RATES SNDRV_PCM_RATE_8000_192000
2921#define RT5663_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
2922 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
2923
Gustavo A. R. Silva6ccf3a62017-07-13 15:37:36 -05002924static const struct snd_soc_dai_ops rt5663_aif_dai_ops = {
Bard Liaodf7c5212016-09-09 10:33:10 +08002925 .hw_params = rt5663_hw_params,
2926 .set_fmt = rt5663_set_dai_fmt,
2927 .set_sysclk = rt5663_set_dai_sysclk,
2928 .set_pll = rt5663_set_dai_pll,
2929 .set_tdm_slot = rt5663_set_tdm_slot,
2930 .set_bclk_ratio = rt5663_set_bclk_ratio,
2931};
2932
Wei Yongjun66d7c262016-09-17 01:34:09 +00002933static struct snd_soc_dai_driver rt5663_dai[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08002934 {
2935 .name = "rt5663-aif",
2936 .id = RT5663_AIF,
2937 .playback = {
2938 .stream_name = "AIF Playback",
2939 .channels_min = 1,
2940 .channels_max = 2,
2941 .rates = RT5663_STEREO_RATES,
2942 .formats = RT5663_FORMATS,
2943 },
2944 .capture = {
2945 .stream_name = "AIF Capture",
2946 .channels_min = 1,
2947 .channels_max = 2,
2948 .rates = RT5663_STEREO_RATES,
2949 .formats = RT5663_FORMATS,
2950 },
2951 .ops = &rt5663_aif_dai_ops,
2952 },
2953};
2954
2955static struct snd_soc_codec_driver soc_codec_dev_rt5663 = {
2956 .probe = rt5663_probe,
2957 .remove = rt5663_remove,
2958 .suspend = rt5663_suspend,
2959 .resume = rt5663_resume,
2960 .set_bias_level = rt5663_set_bias_level,
2961 .idle_bias_off = true,
2962 .component_driver = {
2963 .controls = rt5663_snd_controls,
2964 .num_controls = ARRAY_SIZE(rt5663_snd_controls),
2965 .dapm_widgets = rt5663_dapm_widgets,
2966 .num_dapm_widgets = ARRAY_SIZE(rt5663_dapm_widgets),
2967 .dapm_routes = rt5663_dapm_routes,
2968 .num_dapm_routes = ARRAY_SIZE(rt5663_dapm_routes),
2969 }
2970};
2971
Bard Liao73444722016-10-21 11:02:28 +08002972static const struct regmap_config rt5663_v2_regmap = {
Bard Liaodf7c5212016-09-09 10:33:10 +08002973 .reg_bits = 16,
2974 .val_bits = 16,
2975 .use_single_rw = true,
2976 .max_register = 0x07fa,
Bard Liao73444722016-10-21 11:02:28 +08002977 .volatile_reg = rt5663_v2_volatile_register,
2978 .readable_reg = rt5663_v2_readable_register,
Bard Liaodf7c5212016-09-09 10:33:10 +08002979 .cache_type = REGCACHE_RBTREE,
Bard Liao73444722016-10-21 11:02:28 +08002980 .reg_defaults = rt5663_v2_reg,
2981 .num_reg_defaults = ARRAY_SIZE(rt5663_v2_reg),
Bard Liaodf7c5212016-09-09 10:33:10 +08002982};
2983
2984static const struct regmap_config rt5663_regmap = {
2985 .reg_bits = 16,
2986 .val_bits = 16,
2987 .use_single_rw = true,
2988 .max_register = 0x03f3,
2989 .volatile_reg = rt5663_volatile_register,
2990 .readable_reg = rt5663_readable_register,
2991 .cache_type = REGCACHE_RBTREE,
2992 .reg_defaults = rt5663_reg,
2993 .num_reg_defaults = ARRAY_SIZE(rt5663_reg),
2994};
2995
2996static const struct regmap_config temp_regmap = {
2997 .name = "nocache",
2998 .reg_bits = 16,
2999 .val_bits = 16,
3000 .use_single_rw = true,
3001 .max_register = 0x03f3,
3002 .cache_type = REGCACHE_NONE,
3003};
3004
3005static const struct i2c_device_id rt5663_i2c_id[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08003006 { "rt5663", 0 },
3007 {}
3008};
3009MODULE_DEVICE_TABLE(i2c, rt5663_i2c_id);
3010
3011#if defined(CONFIG_OF)
3012static const struct of_device_id rt5663_of_match[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08003013 { .compatible = "realtek,rt5663", },
3014 {},
3015};
3016MODULE_DEVICE_TABLE(of, rt5663_of_match);
3017#endif
3018
3019#ifdef CONFIG_ACPI
3020static struct acpi_device_id rt5663_acpi_match[] = {
Bard Liaodf7c5212016-09-09 10:33:10 +08003021 { "10EC5663", 0},
3022 {},
3023};
3024MODULE_DEVICE_TABLE(acpi, rt5663_acpi_match);
3025#endif
3026
Bard Liao73444722016-10-21 11:02:28 +08003027static void rt5663_v2_calibrate(struct rt5663_priv *rt5663)
Bard Liaodf7c5212016-09-09 10:33:10 +08003028{
Bard Liao73444722016-10-21 11:02:28 +08003029 regmap_write(rt5663->regmap, RT5663_BIAS_CUR_8, 0xa402);
3030 regmap_write(rt5663->regmap, RT5663_PWR_DIG_1, 0x0100);
3031 regmap_write(rt5663->regmap, RT5663_RECMIX, 0x4040);
3032 regmap_write(rt5663->regmap, RT5663_DIG_MISC, 0x0001);
3033 regmap_write(rt5663->regmap, RT5663_RC_CLK, 0x0380);
3034 regmap_write(rt5663->regmap, RT5663_GLB_CLK, 0x8000);
3035 regmap_write(rt5663->regmap, RT5663_ADDA_CLK_1, 0x1000);
3036 regmap_write(rt5663->regmap, RT5663_CHOP_DAC_L, 0x3030);
3037 regmap_write(rt5663->regmap, RT5663_CALIB_ADC, 0x3c05);
3038 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xa23e);
Bard Liaodf7c5212016-09-09 10:33:10 +08003039 msleep(40);
Bard Liao73444722016-10-21 11:02:28 +08003040 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xf23e);
3041 regmap_write(rt5663->regmap, RT5663_HP_CALIB_2, 0x0321);
3042 regmap_write(rt5663->regmap, RT5663_HP_CALIB_1, 0xfc00);
Bard Liaodf7c5212016-09-09 10:33:10 +08003043 msleep(500);
3044}
3045
Bard Liao73444722016-10-21 11:02:28 +08003046static void rt5663_calibrate(struct rt5663_priv *rt5663)
Bard Liaodf7c5212016-09-09 10:33:10 +08003047{
3048 int value, count;
3049
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003050 regmap_write(rt5663->regmap, RT5663_RESET, 0x0000);
Bard Liaodf7c5212016-09-09 10:33:10 +08003051 msleep(20);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003052 regmap_write(rt5663->regmap, RT5663_ANA_BIAS_CUR_4, 0x00a1);
3053 regmap_write(rt5663->regmap, RT5663_RC_CLK, 0x0380);
3054 regmap_write(rt5663->regmap, RT5663_GLB_CLK, 0x8000);
3055 regmap_write(rt5663->regmap, RT5663_ADDA_CLK_1, 0x1000);
3056 regmap_write(rt5663->regmap, RT5663_VREF_RECMIX, 0x0032);
3057 regmap_write(rt5663->regmap, RT5663_HP_IMP_SEN_19, 0x000c);
3058 regmap_write(rt5663->regmap, RT5663_DUMMY_1, 0x0324);
3059 regmap_write(rt5663->regmap, RT5663_DIG_MISC, 0x8001);
3060 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xa23b);
3061 msleep(30);
3062 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xf23b);
3063 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_2, 0x8000);
3064 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_3, 0x0008);
Bard Liao73444722016-10-21 11:02:28 +08003065 regmap_write(rt5663->regmap, RT5663_PRE_DIV_GATING_1, 0xffff);
3066 regmap_write(rt5663->regmap, RT5663_PRE_DIV_GATING_2, 0xffff);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003067 regmap_write(rt5663->regmap, RT5663_CBJ_1, 0x8c10);
3068 regmap_write(rt5663->regmap, RT5663_IL_CMD_2, 0x00c1);
3069 regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_1, 0xb880);
3070 regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_2, 0x4110);
3071 regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_2, 0x4118);
3072
3073 count = 0;
3074 while (true) {
3075 regmap_read(rt5663->regmap, RT5663_INT_ST_2, &value);
3076 if (!(value & 0x80))
3077 usleep_range(10000, 10005);
3078 else
3079 break;
3080
Colin Ian King09b88522017-07-10 16:20:45 +01003081 if (++count > 200)
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003082 break;
3083 }
3084
3085 regmap_write(rt5663->regmap, RT5663_HP_IMP_SEN_19, 0x0000);
Bard Liao73444722016-10-21 11:02:28 +08003086 regmap_write(rt5663->regmap, RT5663_DEPOP_2, 0x3003);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003087 regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x0038);
Bard Liao73444722016-10-21 11:02:28 +08003088 regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x003b);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003089 regmap_write(rt5663->regmap, RT5663_PWR_DIG_2, 0x8400);
3090 regmap_write(rt5663->regmap, RT5663_PWR_DIG_1, 0x8df8);
3091 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_2, 0x8003);
3092 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_3, 0x018c);
Bard Liao73444722016-10-21 11:02:28 +08003093 regmap_write(rt5663->regmap, RT5663_HP_CHARGE_PUMP_1, 0x1e32);
Bard Liao73444722016-10-21 11:02:28 +08003094 regmap_write(rt5663->regmap, RT5663_DACREF_LDO, 0x3b0b);
oder_chiou@realtek.comc1bbaff2017-08-01 19:14:02 +08003095 msleep(40);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003096 regmap_write(rt5663->regmap, RT5663_STO_DAC_MIXER, 0x0000);
Bard Liao73444722016-10-21 11:02:28 +08003097 regmap_write(rt5663->regmap, RT5663_BYPASS_STO_DAC, 0x000c);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003098 regmap_write(rt5663->regmap, RT5663_HP_BIAS, 0xafaa);
Bard Liao73444722016-10-21 11:02:28 +08003099 regmap_write(rt5663->regmap, RT5663_CHARGE_PUMP_1, 0x2224);
3100 regmap_write(rt5663->regmap, RT5663_HP_OUT_EN, 0x8088);
3101 regmap_write(rt5663->regmap, RT5663_STO_DRE_9, 0x0017);
3102 regmap_write(rt5663->regmap, RT5663_STO_DRE_10, 0x0017);
3103 regmap_write(rt5663->regmap, RT5663_STO1_ADC_MIXER, 0x4040);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003104 regmap_write(rt5663->regmap, RT5663_CHOP_ADC, 0x3000);
Bard Liao73444722016-10-21 11:02:28 +08003105 regmap_write(rt5663->regmap, RT5663_RECMIX, 0x0005);
3106 regmap_write(rt5663->regmap, RT5663_ADDA_RST, 0xc000);
3107 regmap_write(rt5663->regmap, RT5663_STO1_HPF_ADJ1, 0x3320);
3108 regmap_write(rt5663->regmap, RT5663_HP_CALIB_2, 0x00c9);
3109 regmap_write(rt5663->regmap, RT5663_DUMMY_1, 0x004c);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003110 regmap_write(rt5663->regmap, RT5663_ANA_BIAS_CUR_1, 0x1111);
3111 regmap_write(rt5663->regmap, RT5663_BIAS_CUR_8, 0x4402);
3112 regmap_write(rt5663->regmap, RT5663_CHARGE_PUMP_2, 0x3311);
Bard Liao73444722016-10-21 11:02:28 +08003113 regmap_write(rt5663->regmap, RT5663_HP_CALIB_1, 0x0069);
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003114 regmap_write(rt5663->regmap, RT5663_HP_CALIB_3, 0x06ce);
3115 regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0x6800);
3116 regmap_write(rt5663->regmap, RT5663_CHARGE_PUMP_2, 0x1100);
3117 regmap_write(rt5663->regmap, RT5663_HP_CALIB_7, 0x0057);
3118 regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0xe800);
3119
Bard Liaodf7c5212016-09-09 10:33:10 +08003120 count = 0;
3121 while (true) {
Bard Liao73444722016-10-21 11:02:28 +08003122 regmap_read(rt5663->regmap, RT5663_HP_CALIB_1_1, &value);
Bard Liaodf7c5212016-09-09 10:33:10 +08003123 if (value & 0x8000)
3124 usleep_range(10000, 10005);
3125 else
3126 break;
3127
3128 if (count > 200)
3129 return;
3130 count++;
3131 }
oder_chiou@realtek.com7d8e00c2017-07-07 16:58:57 +08003132
3133 regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0x6200);
3134 regmap_write(rt5663->regmap, RT5663_HP_CALIB_7, 0x0059);
3135 regmap_write(rt5663->regmap, RT5663_HP_CALIB_1_1, 0xe200);
3136
3137 count = 0;
3138 while (true) {
3139 regmap_read(rt5663->regmap, RT5663_HP_CALIB_1_1, &value);
3140 if (value & 0x8000)
3141 usleep_range(10000, 10005);
3142 else
3143 break;
3144
3145 if (count > 200)
3146 return;
3147 count++;
3148 }
3149
3150 regmap_write(rt5663->regmap, RT5663_EM_JACK_TYPE_1, 0xb8e0);
3151 usleep_range(10000, 10005);
3152 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0x003b);
3153 usleep_range(10000, 10005);
3154 regmap_write(rt5663->regmap, RT5663_PWR_DIG_1, 0x0000);
3155 usleep_range(10000, 10005);
3156 regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x000b);
3157 usleep_range(10000, 10005);
3158 regmap_write(rt5663->regmap, RT5663_DEPOP_1, 0x0008);
3159 usleep_range(10000, 10005);
3160 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_2, 0x0000);
3161 usleep_range(10000, 10005);
Bard Liaodf7c5212016-09-09 10:33:10 +08003162}
3163
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +08003164static int rt5663_parse_dp(struct rt5663_priv *rt5663, struct device *dev)
3165{
3166 device_property_read_u32(dev, "realtek,dc_offset_l_manual",
3167 &rt5663->pdata.dc_offset_l_manual);
3168 device_property_read_u32(dev, "realtek,dc_offset_r_manual",
3169 &rt5663->pdata.dc_offset_r_manual);
oder_chiou@realtek.com278982b2017-08-02 16:01:27 +08003170 device_property_read_u32(dev, "realtek,dc_offset_l_manual_mic",
3171 &rt5663->pdata.dc_offset_l_manual_mic);
3172 device_property_read_u32(dev, "realtek,dc_offset_r_manual_mic",
3173 &rt5663->pdata.dc_offset_r_manual_mic);
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +08003174
3175 return 0;
3176}
3177
Bard Liaodf7c5212016-09-09 10:33:10 +08003178static int rt5663_i2c_probe(struct i2c_client *i2c,
3179 const struct i2c_device_id *id)
3180{
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +08003181 struct rt5663_platform_data *pdata = dev_get_platdata(&i2c->dev);
Bard Liaodf7c5212016-09-09 10:33:10 +08003182 struct rt5663_priv *rt5663;
3183 int ret;
3184 unsigned int val;
3185 struct regmap *regmap;
3186
3187 rt5663 = devm_kzalloc(&i2c->dev, sizeof(struct rt5663_priv),
3188 GFP_KERNEL);
3189
3190 if (rt5663 == NULL)
3191 return -ENOMEM;
3192
3193 i2c_set_clientdata(i2c, rt5663);
3194
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +08003195 if (pdata)
3196 rt5663->pdata = *pdata;
3197 else
3198 rt5663_parse_dp(rt5663, &i2c->dev);
3199
Bard Liaodf7c5212016-09-09 10:33:10 +08003200 regmap = devm_regmap_init_i2c(i2c, &temp_regmap);
3201 if (IS_ERR(regmap)) {
3202 ret = PTR_ERR(regmap);
3203 dev_err(&i2c->dev, "Failed to allocate temp register map: %d\n",
3204 ret);
3205 return ret;
3206 }
3207 regmap_read(regmap, RT5663_VENDOR_ID_2, &val);
3208 switch (val) {
Bard Liao73444722016-10-21 11:02:28 +08003209 case RT5663_DEVICE_ID_2:
3210 rt5663->regmap = devm_regmap_init_i2c(i2c, &rt5663_v2_regmap);
3211 rt5663->codec_ver = CODEC_VER_1;
Bard Liaodf7c5212016-09-09 10:33:10 +08003212 break;
Bard Liao73444722016-10-21 11:02:28 +08003213 case RT5663_DEVICE_ID_1:
Bard Liaodf7c5212016-09-09 10:33:10 +08003214 rt5663->regmap = devm_regmap_init_i2c(i2c, &rt5663_regmap);
Bard Liao73444722016-10-21 11:02:28 +08003215 rt5663->codec_ver = CODEC_VER_0;
Bard Liaodf7c5212016-09-09 10:33:10 +08003216 break;
3217 default:
3218 dev_err(&i2c->dev,
Bard Liao73444722016-10-21 11:02:28 +08003219 "Device with ID register %#x is not rt5663\n",
Bard Liaodf7c5212016-09-09 10:33:10 +08003220 val);
3221 return -ENODEV;
3222 }
3223
3224 if (IS_ERR(rt5663->regmap)) {
3225 ret = PTR_ERR(rt5663->regmap);
3226 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
3227 ret);
3228 return ret;
3229 }
3230
3231 /* reset and calibrate */
3232 regmap_write(rt5663->regmap, RT5663_RESET, 0);
3233 regcache_cache_bypass(rt5663->regmap, true);
Bard Liao73444722016-10-21 11:02:28 +08003234 switch (rt5663->codec_ver) {
3235 case CODEC_VER_1:
3236 rt5663_v2_calibrate(rt5663);
Bard Liaodf7c5212016-09-09 10:33:10 +08003237 break;
Bard Liao73444722016-10-21 11:02:28 +08003238 case CODEC_VER_0:
Bard Liaodf7c5212016-09-09 10:33:10 +08003239 rt5663_calibrate(rt5663);
3240 break;
3241 default:
3242 dev_err(&i2c->dev, "%s:Unknown codec type\n", __func__);
3243 }
3244 regcache_cache_bypass(rt5663->regmap, false);
3245 regmap_write(rt5663->regmap, RT5663_RESET, 0);
3246 dev_dbg(&i2c->dev, "calibrate done\n");
3247
oder_chiou@realtek.com450f0f62017-07-10 11:14:56 +08003248 switch (rt5663->codec_ver) {
3249 case CODEC_VER_1:
3250 break;
3251 case CODEC_VER_0:
3252 ret = regmap_register_patch(rt5663->regmap, rt5663_patch_list,
3253 ARRAY_SIZE(rt5663_patch_list));
3254 if (ret != 0)
3255 dev_warn(&i2c->dev,
3256 "Failed to apply regmap patch: %d\n", ret);
3257 break;
3258 default:
3259 dev_err(&i2c->dev, "%s:Unknown codec type\n", __func__);
3260 }
3261
Bard Liaodf7c5212016-09-09 10:33:10 +08003262 /* GPIO1 as IRQ */
Bard Liao73444722016-10-21 11:02:28 +08003263 regmap_update_bits(rt5663->regmap, RT5663_GPIO_1, RT5663_GP1_PIN_MASK,
3264 RT5663_GP1_PIN_IRQ);
Bard Liaodf7c5212016-09-09 10:33:10 +08003265 /* 4btn inline command debounce */
3266 regmap_update_bits(rt5663->regmap, RT5663_IL_CMD_5,
Bard Liao73444722016-10-21 11:02:28 +08003267 RT5663_4BTN_CLK_DEB_MASK, RT5663_4BTN_CLK_DEB_65MS);
Bard Liaodf7c5212016-09-09 10:33:10 +08003268
Bard Liao73444722016-10-21 11:02:28 +08003269 switch (rt5663->codec_ver) {
3270 case CODEC_VER_1:
Bard Liaodf7c5212016-09-09 10:33:10 +08003271 regmap_write(rt5663->regmap, RT5663_BIAS_CUR_8, 0xa402);
3272 /* JD1 */
3273 regmap_update_bits(rt5663->regmap, RT5663_AUTO_1MRC_CLK,
Bard Liao73444722016-10-21 11:02:28 +08003274 RT5663_IRQ_POW_SAV_MASK | RT5663_IRQ_POW_SAV_JD1_MASK,
3275 RT5663_IRQ_POW_SAV_EN | RT5663_IRQ_POW_SAV_JD1_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08003276 regmap_update_bits(rt5663->regmap, RT5663_PWR_ANLG_2,
Bard Liao73444722016-10-21 11:02:28 +08003277 RT5663_PWR_JD1_MASK, RT5663_PWR_JD1);
Bard Liaodf7c5212016-09-09 10:33:10 +08003278 regmap_update_bits(rt5663->regmap, RT5663_IRQ_1,
Bard Liao73444722016-10-21 11:02:28 +08003279 RT5663_EN_CB_JD_MASK, RT5663_EN_CB_JD_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08003280
3281 regmap_update_bits(rt5663->regmap, RT5663_HP_LOGIC_2,
Bard Liao73444722016-10-21 11:02:28 +08003282 RT5663_HP_SIG_SRC1_MASK, RT5663_HP_SIG_SRC1_REG);
Bard Liaodf7c5212016-09-09 10:33:10 +08003283 regmap_update_bits(rt5663->regmap, RT5663_RECMIX,
Bard Liao73444722016-10-21 11:02:28 +08003284 RT5663_VREF_BIAS_MASK | RT5663_CBJ_DET_MASK |
3285 RT5663_DET_TYPE_MASK, RT5663_VREF_BIAS_REG |
3286 RT5663_CBJ_DET_EN | RT5663_DET_TYPE_QFN);
Bard Liaodf7c5212016-09-09 10:33:10 +08003287 /* Set GPIO4 and GPIO8 as input for combo jack */
3288 regmap_update_bits(rt5663->regmap, RT5663_GPIO_2,
Bard Liao73444722016-10-21 11:02:28 +08003289 RT5663_GP4_PIN_CONF_MASK, RT5663_GP4_PIN_CONF_INPUT);
3290 regmap_update_bits(rt5663->regmap, RT5663_GPIO_3,
3291 RT5663_GP8_PIN_CONF_MASK, RT5663_GP8_PIN_CONF_INPUT);
Bard Liaodf7c5212016-09-09 10:33:10 +08003292 regmap_update_bits(rt5663->regmap, RT5663_PWR_ANLG_1,
Bard Liao73444722016-10-21 11:02:28 +08003293 RT5663_LDO1_DVO_MASK | RT5663_AMP_HP_MASK,
3294 RT5663_LDO1_DVO_0_9V | RT5663_AMP_HP_3X);
Bard Liaodf7c5212016-09-09 10:33:10 +08003295 break;
Bard Liao73444722016-10-21 11:02:28 +08003296 case CODEC_VER_0:
Jack Yu7e7e76b2016-10-03 10:43:27 +08003297 regmap_update_bits(rt5663->regmap, RT5663_DIG_MISC,
Bard Liao73444722016-10-21 11:02:28 +08003298 RT5663_DIG_GATE_CTRL_MASK, RT5663_DIG_GATE_CTRL_EN);
Jack Yu7e7e76b2016-10-03 10:43:27 +08003299 regmap_update_bits(rt5663->regmap, RT5663_AUTO_1MRC_CLK,
Oder Chiouaf2728e2017-06-06 14:59:54 +08003300 RT5663_IRQ_MANUAL_MASK, RT5663_IRQ_MANUAL_EN);
Jack Yu7e7e76b2016-10-03 10:43:27 +08003301 regmap_update_bits(rt5663->regmap, RT5663_IRQ_1,
3302 RT5663_EN_IRQ_JD1_MASK, RT5663_EN_IRQ_JD1_EN);
3303 regmap_update_bits(rt5663->regmap, RT5663_GPIO_1,
3304 RT5663_GPIO1_TYPE_MASK, RT5663_GPIO1_TYPE_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08003305 regmap_write(rt5663->regmap, RT5663_VREF_RECMIX, 0x0032);
3306 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xa2be);
3307 msleep(20);
3308 regmap_write(rt5663->regmap, RT5663_PWR_ANLG_1, 0xf2be);
3309 regmap_update_bits(rt5663->regmap, RT5663_GPIO_2,
Jack Yu7e7e76b2016-10-03 10:43:27 +08003310 RT5663_GP1_PIN_CONF_MASK | RT5663_SEL_GPIO1_MASK,
3311 RT5663_GP1_PIN_CONF_OUTPUT | RT5663_SEL_GPIO1_EN);
Bard Liaodf7c5212016-09-09 10:33:10 +08003312 /* DACREF LDO control */
3313 regmap_update_bits(rt5663->regmap, RT5663_DACREF_LDO, 0x3e0e,
3314 0x3a0a);
3315 regmap_update_bits(rt5663->regmap, RT5663_RECMIX,
3316 RT5663_RECMIX1_BST1_MASK, RT5663_RECMIX1_BST1_ON);
3317 regmap_update_bits(rt5663->regmap, RT5663_TDM_2,
3318 RT5663_DATA_SWAP_ADCDAT1_MASK,
3319 RT5663_DATA_SWAP_ADCDAT1_LL);
3320 break;
3321 default:
3322 dev_err(&i2c->dev, "%s:Unknown codec type\n", __func__);
3323 }
3324
3325 INIT_DELAYED_WORK(&rt5663->jack_detect_work, rt5663_jack_detect_work);
3326
3327 if (i2c->irq) {
3328 ret = request_irq(i2c->irq, rt5663_irq,
3329 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
3330 | IRQF_ONESHOT, "rt5663", rt5663);
3331 if (ret)
3332 dev_err(&i2c->dev, "%s Failed to reguest IRQ: %d\n",
3333 __func__, ret);
3334 }
3335
3336 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rt5663,
3337 rt5663_dai, ARRAY_SIZE(rt5663_dai));
3338
3339 if (ret) {
3340 if (i2c->irq)
3341 free_irq(i2c->irq, rt5663);
3342 }
3343
3344 return ret;
3345}
3346
3347static int rt5663_i2c_remove(struct i2c_client *i2c)
3348{
3349 struct rt5663_priv *rt5663 = i2c_get_clientdata(i2c);
3350
3351 if (i2c->irq)
3352 free_irq(i2c->irq, rt5663);
3353
3354 snd_soc_unregister_codec(&i2c->dev);
3355
3356 return 0;
3357}
3358
Wei Yongjun66d7c262016-09-17 01:34:09 +00003359static void rt5663_i2c_shutdown(struct i2c_client *client)
Bard Liaodf7c5212016-09-09 10:33:10 +08003360{
3361 struct rt5663_priv *rt5663 = i2c_get_clientdata(client);
3362
3363 regmap_write(rt5663->regmap, RT5663_RESET, 0);
3364}
3365
Wei Yongjun66d7c262016-09-17 01:34:09 +00003366static struct i2c_driver rt5663_i2c_driver = {
Bard Liaodf7c5212016-09-09 10:33:10 +08003367 .driver = {
3368 .name = "rt5663",
Bard Liaodf7c5212016-09-09 10:33:10 +08003369 .acpi_match_table = ACPI_PTR(rt5663_acpi_match),
3370 .of_match_table = of_match_ptr(rt5663_of_match),
3371 },
3372 .probe = rt5663_i2c_probe,
3373 .remove = rt5663_i2c_remove,
3374 .shutdown = rt5663_i2c_shutdown,
3375 .id_table = rt5663_i2c_id,
3376};
3377module_i2c_driver(rt5663_i2c_driver);
3378
3379MODULE_DESCRIPTION("ASoC RT5663 driver");
3380MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
3381MODULE_LICENSE("GPL v2");