xref: /openbmc/linux/sound/pci/hda/patch_ca0132.c (revision f59a3ee6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HD audio interface patch for Creative CA0132 chip
4  *
5  * Copyright (c) 2011, Creative Technology Ltd.
6  *
7  * Based on patch_ca0110.c
8  * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
9  */
10 
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/firmware.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/io.h>
20 #include <linux/pci.h>
21 #include <asm/io.h>
22 #include <sound/core.h>
23 #include <sound/hda_codec.h>
24 #include "hda_local.h"
25 #include "hda_auto_parser.h"
26 #include "hda_jack.h"
27 
28 #include "ca0132_regs.h"
29 
30 /* Enable this to see controls for tuning purpose. */
31 /*#define ENABLE_TUNING_CONTROLS*/
32 
33 #ifdef ENABLE_TUNING_CONTROLS
34 #include <sound/tlv.h>
35 #endif
36 
37 #define FLOAT_ZERO	0x00000000
38 #define FLOAT_ONE	0x3f800000
39 #define FLOAT_TWO	0x40000000
40 #define FLOAT_THREE     0x40400000
41 #define FLOAT_FIVE	0x40a00000
42 #define FLOAT_SIX       0x40c00000
43 #define FLOAT_EIGHT     0x41000000
44 #define FLOAT_MINUS_5	0xc0a00000
45 
46 #define UNSOL_TAG_DSP	0x16
47 
48 #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
49 #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
50 
51 #define DMA_TRANSFER_FRAME_SIZE_NWORDS		8
52 #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS	32
53 #define DMA_OVERLAY_FRAME_SIZE_NWORDS		2
54 
55 #define MASTERCONTROL				0x80
56 #define MASTERCONTROL_ALLOC_DMA_CHAN		10
57 #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS	60
58 
59 #define WIDGET_CHIP_CTRL      0x15
60 #define WIDGET_DSP_CTRL       0x16
61 
62 #define MEM_CONNID_MICIN1     3
63 #define MEM_CONNID_MICIN2     5
64 #define MEM_CONNID_MICOUT1    12
65 #define MEM_CONNID_MICOUT2    14
66 #define MEM_CONNID_WUH        10
67 #define MEM_CONNID_DSP        16
68 #define MEM_CONNID_DMIC       100
69 
70 #define SCP_SET    0
71 #define SCP_GET    1
72 
73 #define EFX_FILE   "ctefx.bin"
74 #define DESKTOP_EFX_FILE   "ctefx-desktop.bin"
75 #define R3DI_EFX_FILE  "ctefx-r3di.bin"
76 
77 #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
78 MODULE_FIRMWARE(EFX_FILE);
79 MODULE_FIRMWARE(DESKTOP_EFX_FILE);
80 MODULE_FIRMWARE(R3DI_EFX_FILE);
81 #endif
82 
83 static const char *const dirstr[2] = { "Playback", "Capture" };
84 
85 #define NUM_OF_OUTPUTS 2
86 static const char *const out_type_str[2] = { "Speakers", "Headphone" };
87 enum {
88 	SPEAKER_OUT,
89 	HEADPHONE_OUT,
90 };
91 
92 enum {
93 	DIGITAL_MIC,
94 	LINE_MIC_IN
95 };
96 
97 /* Strings for Input Source Enum Control */
98 static const char *const in_src_str[3] = { "Microphone", "Line In", "Front Microphone" };
99 #define IN_SRC_NUM_OF_INPUTS 3
100 enum {
101 	REAR_MIC,
102 	REAR_LINE_IN,
103 	FRONT_MIC,
104 };
105 
106 enum {
107 #define VNODE_START_NID    0x80
108 	VNID_SPK = VNODE_START_NID,			/* Speaker vnid */
109 	VNID_MIC,
110 	VNID_HP_SEL,
111 	VNID_AMIC1_SEL,
112 	VNID_HP_ASEL,
113 	VNID_AMIC1_ASEL,
114 	VNODE_END_NID,
115 #define VNODES_COUNT  (VNODE_END_NID - VNODE_START_NID)
116 
117 #define EFFECT_START_NID    0x90
118 #define OUT_EFFECT_START_NID    EFFECT_START_NID
119 	SURROUND = OUT_EFFECT_START_NID,
120 	CRYSTALIZER,
121 	DIALOG_PLUS,
122 	SMART_VOLUME,
123 	X_BASS,
124 	EQUALIZER,
125 	OUT_EFFECT_END_NID,
126 #define OUT_EFFECTS_COUNT  (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
127 
128 #define IN_EFFECT_START_NID  OUT_EFFECT_END_NID
129 	ECHO_CANCELLATION = IN_EFFECT_START_NID,
130 	VOICE_FOCUS,
131 	MIC_SVM,
132 	NOISE_REDUCTION,
133 	IN_EFFECT_END_NID,
134 #define IN_EFFECTS_COUNT  (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
135 
136 	VOICEFX = IN_EFFECT_END_NID,
137 	PLAY_ENHANCEMENT,
138 	CRYSTAL_VOICE,
139 	EFFECT_END_NID,
140 	OUTPUT_SOURCE_ENUM,
141 	INPUT_SOURCE_ENUM,
142 	XBASS_XOVER,
143 	EQ_PRESET_ENUM,
144 	SMART_VOLUME_ENUM,
145 	MIC_BOOST_ENUM,
146 	AE5_HEADPHONE_GAIN_ENUM,
147 	AE5_SOUND_FILTER_ENUM,
148 	ZXR_HEADPHONE_GAIN,
149 	SPEAKER_CHANNEL_CFG_ENUM,
150 	SPEAKER_FULL_RANGE_FRONT,
151 	SPEAKER_FULL_RANGE_REAR,
152 	BASS_REDIRECTION,
153 	BASS_REDIRECTION_XOVER,
154 #define EFFECTS_COUNT  (EFFECT_END_NID - EFFECT_START_NID)
155 };
156 
157 /* Effects values size*/
158 #define EFFECT_VALS_MAX_COUNT 12
159 
160 /*
161  * Default values for the effect slider controls, they are in order of their
162  * effect NID's. Surround, Crystalizer, Dialog Plus, Smart Volume, and then
163  * X-bass.
164  */
165 static const unsigned int effect_slider_defaults[] = {67, 65, 50, 74, 50};
166 /* Amount of effect level sliders for ca0132_alt controls. */
167 #define EFFECT_LEVEL_SLIDERS 5
168 
169 /* Latency introduced by DSP blocks in milliseconds. */
170 #define DSP_CAPTURE_INIT_LATENCY        0
171 #define DSP_CRYSTAL_VOICE_LATENCY       124
172 #define DSP_PLAYBACK_INIT_LATENCY       13
173 #define DSP_PLAY_ENHANCEMENT_LATENCY    30
174 #define DSP_SPEAKER_OUT_LATENCY         7
175 
176 struct ct_effect {
177 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
178 	hda_nid_t nid;
179 	int mid; /*effect module ID*/
180 	int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
181 	int direct; /* 0:output; 1:input*/
182 	int params; /* number of default non-on/off params */
183 	/*effect default values, 1st is on/off. */
184 	unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
185 };
186 
187 #define EFX_DIR_OUT 0
188 #define EFX_DIR_IN  1
189 
190 static const struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
191 	{ .name = "Surround",
192 	  .nid = SURROUND,
193 	  .mid = 0x96,
194 	  .reqs = {0, 1},
195 	  .direct = EFX_DIR_OUT,
196 	  .params = 1,
197 	  .def_vals = {0x3F800000, 0x3F2B851F}
198 	},
199 	{ .name = "Crystalizer",
200 	  .nid = CRYSTALIZER,
201 	  .mid = 0x96,
202 	  .reqs = {7, 8},
203 	  .direct = EFX_DIR_OUT,
204 	  .params = 1,
205 	  .def_vals = {0x3F800000, 0x3F266666}
206 	},
207 	{ .name = "Dialog Plus",
208 	  .nid = DIALOG_PLUS,
209 	  .mid = 0x96,
210 	  .reqs = {2, 3},
211 	  .direct = EFX_DIR_OUT,
212 	  .params = 1,
213 	  .def_vals = {0x00000000, 0x3F000000}
214 	},
215 	{ .name = "Smart Volume",
216 	  .nid = SMART_VOLUME,
217 	  .mid = 0x96,
218 	  .reqs = {4, 5, 6},
219 	  .direct = EFX_DIR_OUT,
220 	  .params = 2,
221 	  .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
222 	},
223 	{ .name = "X-Bass",
224 	  .nid = X_BASS,
225 	  .mid = 0x96,
226 	  .reqs = {24, 23, 25},
227 	  .direct = EFX_DIR_OUT,
228 	  .params = 2,
229 	  .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
230 	},
231 	{ .name = "Equalizer",
232 	  .nid = EQUALIZER,
233 	  .mid = 0x96,
234 	  .reqs = {9, 10, 11, 12, 13, 14,
235 			15, 16, 17, 18, 19, 20},
236 	  .direct = EFX_DIR_OUT,
237 	  .params = 11,
238 	  .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
239 		       0x00000000, 0x00000000, 0x00000000, 0x00000000,
240 		       0x00000000, 0x00000000, 0x00000000, 0x00000000}
241 	},
242 	{ .name = "Echo Cancellation",
243 	  .nid = ECHO_CANCELLATION,
244 	  .mid = 0x95,
245 	  .reqs = {0, 1, 2, 3},
246 	  .direct = EFX_DIR_IN,
247 	  .params = 3,
248 	  .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
249 	},
250 	{ .name = "Voice Focus",
251 	  .nid = VOICE_FOCUS,
252 	  .mid = 0x95,
253 	  .reqs = {6, 7, 8, 9},
254 	  .direct = EFX_DIR_IN,
255 	  .params = 3,
256 	  .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
257 	},
258 	{ .name = "Mic SVM",
259 	  .nid = MIC_SVM,
260 	  .mid = 0x95,
261 	  .reqs = {44, 45},
262 	  .direct = EFX_DIR_IN,
263 	  .params = 1,
264 	  .def_vals = {0x00000000, 0x3F3D70A4}
265 	},
266 	{ .name = "Noise Reduction",
267 	  .nid = NOISE_REDUCTION,
268 	  .mid = 0x95,
269 	  .reqs = {4, 5},
270 	  .direct = EFX_DIR_IN,
271 	  .params = 1,
272 	  .def_vals = {0x3F800000, 0x3F000000}
273 	},
274 	{ .name = "VoiceFX",
275 	  .nid = VOICEFX,
276 	  .mid = 0x95,
277 	  .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
278 	  .direct = EFX_DIR_IN,
279 	  .params = 8,
280 	  .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
281 		       0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
282 		       0x00000000}
283 	}
284 };
285 
286 /* Tuning controls */
287 #ifdef ENABLE_TUNING_CONTROLS
288 
289 enum {
290 #define TUNING_CTL_START_NID  0xC0
291 	WEDGE_ANGLE = TUNING_CTL_START_NID,
292 	SVM_LEVEL,
293 	EQUALIZER_BAND_0,
294 	EQUALIZER_BAND_1,
295 	EQUALIZER_BAND_2,
296 	EQUALIZER_BAND_3,
297 	EQUALIZER_BAND_4,
298 	EQUALIZER_BAND_5,
299 	EQUALIZER_BAND_6,
300 	EQUALIZER_BAND_7,
301 	EQUALIZER_BAND_8,
302 	EQUALIZER_BAND_9,
303 	TUNING_CTL_END_NID
304 #define TUNING_CTLS_COUNT  (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
305 };
306 
307 struct ct_tuning_ctl {
308 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
309 	hda_nid_t parent_nid;
310 	hda_nid_t nid;
311 	int mid; /*effect module ID*/
312 	int req; /*effect module request*/
313 	int direct; /* 0:output; 1:input*/
314 	unsigned int def_val;/*effect default values*/
315 };
316 
317 static const struct ct_tuning_ctl ca0132_tuning_ctls[] = {
318 	{ .name = "Wedge Angle",
319 	  .parent_nid = VOICE_FOCUS,
320 	  .nid = WEDGE_ANGLE,
321 	  .mid = 0x95,
322 	  .req = 8,
323 	  .direct = EFX_DIR_IN,
324 	  .def_val = 0x41F00000
325 	},
326 	{ .name = "SVM Level",
327 	  .parent_nid = MIC_SVM,
328 	  .nid = SVM_LEVEL,
329 	  .mid = 0x95,
330 	  .req = 45,
331 	  .direct = EFX_DIR_IN,
332 	  .def_val = 0x3F3D70A4
333 	},
334 	{ .name = "EQ Band0",
335 	  .parent_nid = EQUALIZER,
336 	  .nid = EQUALIZER_BAND_0,
337 	  .mid = 0x96,
338 	  .req = 11,
339 	  .direct = EFX_DIR_OUT,
340 	  .def_val = 0x00000000
341 	},
342 	{ .name = "EQ Band1",
343 	  .parent_nid = EQUALIZER,
344 	  .nid = EQUALIZER_BAND_1,
345 	  .mid = 0x96,
346 	  .req = 12,
347 	  .direct = EFX_DIR_OUT,
348 	  .def_val = 0x00000000
349 	},
350 	{ .name = "EQ Band2",
351 	  .parent_nid = EQUALIZER,
352 	  .nid = EQUALIZER_BAND_2,
353 	  .mid = 0x96,
354 	  .req = 13,
355 	  .direct = EFX_DIR_OUT,
356 	  .def_val = 0x00000000
357 	},
358 	{ .name = "EQ Band3",
359 	  .parent_nid = EQUALIZER,
360 	  .nid = EQUALIZER_BAND_3,
361 	  .mid = 0x96,
362 	  .req = 14,
363 	  .direct = EFX_DIR_OUT,
364 	  .def_val = 0x00000000
365 	},
366 	{ .name = "EQ Band4",
367 	  .parent_nid = EQUALIZER,
368 	  .nid = EQUALIZER_BAND_4,
369 	  .mid = 0x96,
370 	  .req = 15,
371 	  .direct = EFX_DIR_OUT,
372 	  .def_val = 0x00000000
373 	},
374 	{ .name = "EQ Band5",
375 	  .parent_nid = EQUALIZER,
376 	  .nid = EQUALIZER_BAND_5,
377 	  .mid = 0x96,
378 	  .req = 16,
379 	  .direct = EFX_DIR_OUT,
380 	  .def_val = 0x00000000
381 	},
382 	{ .name = "EQ Band6",
383 	  .parent_nid = EQUALIZER,
384 	  .nid = EQUALIZER_BAND_6,
385 	  .mid = 0x96,
386 	  .req = 17,
387 	  .direct = EFX_DIR_OUT,
388 	  .def_val = 0x00000000
389 	},
390 	{ .name = "EQ Band7",
391 	  .parent_nid = EQUALIZER,
392 	  .nid = EQUALIZER_BAND_7,
393 	  .mid = 0x96,
394 	  .req = 18,
395 	  .direct = EFX_DIR_OUT,
396 	  .def_val = 0x00000000
397 	},
398 	{ .name = "EQ Band8",
399 	  .parent_nid = EQUALIZER,
400 	  .nid = EQUALIZER_BAND_8,
401 	  .mid = 0x96,
402 	  .req = 19,
403 	  .direct = EFX_DIR_OUT,
404 	  .def_val = 0x00000000
405 	},
406 	{ .name = "EQ Band9",
407 	  .parent_nid = EQUALIZER,
408 	  .nid = EQUALIZER_BAND_9,
409 	  .mid = 0x96,
410 	  .req = 20,
411 	  .direct = EFX_DIR_OUT,
412 	  .def_val = 0x00000000
413 	}
414 };
415 #endif
416 
417 /* Voice FX Presets */
418 #define VOICEFX_MAX_PARAM_COUNT 9
419 
420 struct ct_voicefx {
421 	char *name;
422 	hda_nid_t nid;
423 	int mid;
424 	int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
425 };
426 
427 struct ct_voicefx_preset {
428 	char *name; /*preset name*/
429 	unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
430 };
431 
432 static const struct ct_voicefx ca0132_voicefx = {
433 	.name = "VoiceFX Capture Switch",
434 	.nid = VOICEFX,
435 	.mid = 0x95,
436 	.reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
437 };
438 
439 static const struct ct_voicefx_preset ca0132_voicefx_presets[] = {
440 	{ .name = "Neutral",
441 	  .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
442 		    0x44FA0000, 0x3F800000, 0x3F800000,
443 		    0x3F800000, 0x00000000, 0x00000000 }
444 	},
445 	{ .name = "Female2Male",
446 	  .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
447 		    0x44FA0000, 0x3F19999A, 0x3F866666,
448 		    0x3F800000, 0x00000000, 0x00000000 }
449 	},
450 	{ .name = "Male2Female",
451 	  .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
452 		    0x450AC000, 0x4017AE14, 0x3F6B851F,
453 		    0x3F800000, 0x00000000, 0x00000000 }
454 	},
455 	{ .name = "ScrappyKid",
456 	  .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
457 		    0x44FA0000, 0x40400000, 0x3F28F5C3,
458 		    0x3F800000, 0x00000000, 0x00000000 }
459 	},
460 	{ .name = "Elderly",
461 	  .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
462 		    0x44E10000, 0x3FB33333, 0x3FB9999A,
463 		    0x3F800000, 0x3E3A2E43, 0x00000000 }
464 	},
465 	{ .name = "Orc",
466 	  .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
467 		    0x45098000, 0x3F266666, 0x3FC00000,
468 		    0x3F800000, 0x00000000, 0x00000000 }
469 	},
470 	{ .name = "Elf",
471 	  .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
472 		    0x45193000, 0x3F8E147B, 0x3F75C28F,
473 		    0x3F800000, 0x00000000, 0x00000000 }
474 	},
475 	{ .name = "Dwarf",
476 	  .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
477 		    0x45007000, 0x3F451EB8, 0x3F7851EC,
478 		    0x3F800000, 0x00000000, 0x00000000 }
479 	},
480 	{ .name = "AlienBrute",
481 	  .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
482 		    0x451F6000, 0x3F266666, 0x3FA7D945,
483 		    0x3F800000, 0x3CF5C28F, 0x00000000 }
484 	},
485 	{ .name = "Robot",
486 	  .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
487 		    0x44FA0000, 0x3FB2718B, 0x3F800000,
488 		    0xBC07010E, 0x00000000, 0x00000000 }
489 	},
490 	{ .name = "Marine",
491 	  .vals = { 0x3F800000, 0x43C20000, 0x44906000,
492 		    0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
493 		    0x3F0A3D71, 0x00000000, 0x00000000 }
494 	},
495 	{ .name = "Emo",
496 	  .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
497 		    0x44FA0000, 0x3F800000, 0x3F800000,
498 		    0x3E4CCCCD, 0x00000000, 0x00000000 }
499 	},
500 	{ .name = "DeepVoice",
501 	  .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
502 		    0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
503 		    0x3F800000, 0x00000000, 0x00000000 }
504 	},
505 	{ .name = "Munchkin",
506 	  .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
507 		    0x44FA0000, 0x3F800000, 0x3F1A043C,
508 		    0x3F800000, 0x00000000, 0x00000000 }
509 	}
510 };
511 
512 /* ca0132 EQ presets, taken from Windows Sound Blaster Z Driver */
513 
514 #define EQ_PRESET_MAX_PARAM_COUNT 11
515 
516 struct ct_eq {
517 	char *name;
518 	hda_nid_t nid;
519 	int mid;
520 	int reqs[EQ_PRESET_MAX_PARAM_COUNT]; /*effect module request*/
521 };
522 
523 struct ct_eq_preset {
524 	char *name; /*preset name*/
525 	unsigned int vals[EQ_PRESET_MAX_PARAM_COUNT];
526 };
527 
528 static const struct ct_eq ca0132_alt_eq_enum = {
529 	.name = "FX: Equalizer Preset Switch",
530 	.nid = EQ_PRESET_ENUM,
531 	.mid = 0x96,
532 	.reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
533 };
534 
535 
536 static const struct ct_eq_preset ca0132_alt_eq_presets[] = {
537 	{ .name = "Flat",
538 	 .vals = { 0x00000000, 0x00000000, 0x00000000,
539 		   0x00000000, 0x00000000, 0x00000000,
540 		   0x00000000, 0x00000000, 0x00000000,
541 		   0x00000000, 0x00000000	     }
542 	},
543 	{ .name = "Acoustic",
544 	 .vals = { 0x00000000, 0x00000000, 0x3F8CCCCD,
545 		   0x40000000, 0x00000000, 0x00000000,
546 		   0x00000000, 0x00000000, 0x40000000,
547 		   0x40000000, 0x40000000	     }
548 	},
549 	{ .name = "Classical",
550 	 .vals = { 0x00000000, 0x00000000, 0x40C00000,
551 		   0x40C00000, 0x40466666, 0x00000000,
552 		   0x00000000, 0x00000000, 0x00000000,
553 		   0x40466666, 0x40466666	     }
554 	},
555 	{ .name = "Country",
556 	 .vals = { 0x00000000, 0xBF99999A, 0x00000000,
557 		   0x3FA66666, 0x3FA66666, 0x3F8CCCCD,
558 		   0x00000000, 0x00000000, 0x40000000,
559 		   0x40466666, 0x40800000	     }
560 	},
561 	{ .name = "Dance",
562 	 .vals = { 0x00000000, 0xBF99999A, 0x40000000,
563 		   0x40466666, 0x40866666, 0xBF99999A,
564 		   0xBF99999A, 0x00000000, 0x00000000,
565 		   0x40800000, 0x40800000	     }
566 	},
567 	{ .name = "Jazz",
568 	 .vals = { 0x00000000, 0x00000000, 0x00000000,
569 		   0x3F8CCCCD, 0x40800000, 0x40800000,
570 		   0x40800000, 0x00000000, 0x3F8CCCCD,
571 		   0x40466666, 0x40466666	     }
572 	},
573 	{ .name = "New Age",
574 	 .vals = { 0x00000000, 0x00000000, 0x40000000,
575 		   0x40000000, 0x00000000, 0x00000000,
576 		   0x00000000, 0x3F8CCCCD, 0x40000000,
577 		   0x40000000, 0x40000000	     }
578 	},
579 	{ .name = "Pop",
580 	 .vals = { 0x00000000, 0xBFCCCCCD, 0x00000000,
581 		   0x40000000, 0x40000000, 0x00000000,
582 		   0xBF99999A, 0xBF99999A, 0x00000000,
583 		   0x40466666, 0x40C00000	     }
584 	},
585 	{ .name = "Rock",
586 	 .vals = { 0x00000000, 0xBF99999A, 0xBF99999A,
587 		   0x3F8CCCCD, 0x40000000, 0xBF99999A,
588 		   0xBF99999A, 0x00000000, 0x00000000,
589 		   0x40800000, 0x40800000	     }
590 	},
591 	{ .name = "Vocal",
592 	 .vals = { 0x00000000, 0xC0000000, 0xBF99999A,
593 		   0xBF99999A, 0x00000000, 0x40466666,
594 		   0x40800000, 0x40466666, 0x00000000,
595 		   0x00000000, 0x3F8CCCCD	     }
596 	}
597 };
598 
599 /*
600  * DSP reqs for handling full-range speakers/bass redirection. If a speaker is
601  * set as not being full range, and bass redirection is enabled, all
602  * frequencies below the crossover frequency are redirected to the LFE
603  * channel. If the surround configuration has no LFE channel, this can't be
604  * enabled. X-Bass must be disabled when using these.
605  */
606 enum speaker_range_reqs {
607 	SPEAKER_BASS_REDIRECT            = 0x15,
608 	SPEAKER_BASS_REDIRECT_XOVER_FREQ = 0x16,
609 	/* Between 0x16-0x1a are the X-Bass reqs. */
610 	SPEAKER_FULL_RANGE_FRONT_L_R     = 0x1a,
611 	SPEAKER_FULL_RANGE_CENTER_LFE    = 0x1b,
612 	SPEAKER_FULL_RANGE_REAR_L_R      = 0x1c,
613 	SPEAKER_FULL_RANGE_SURROUND_L_R  = 0x1d,
614 	SPEAKER_BASS_REDIRECT_SUB_GAIN   = 0x1e,
615 };
616 
617 /*
618  * Definitions for the DSP req's to handle speaker tuning. These all belong to
619  * module ID 0x96, the output effects module.
620  */
621 enum speaker_tuning_reqs {
622 	/*
623 	 * Currently, this value is always set to 0.0f. However, on Windows,
624 	 * when selecting certain headphone profiles on the new Sound Blaster
625 	 * connect software, the QUERY_SPEAKER_EQ_ADDRESS req on mid 0x80 is
626 	 * sent. This gets the speaker EQ address area, which is then used to
627 	 * send over (presumably) an equalizer profile for the specific
628 	 * headphone setup. It is sent using the same method the DSP
629 	 * firmware is uploaded with, which I believe is why the 'ctspeq.bin'
630 	 * file exists in linux firmware tree but goes unused. It would also
631 	 * explain why the QUERY_SPEAKER_EQ_ADDRESS req is defined but unused.
632 	 * Once this profile is sent over, SPEAKER_TUNING_USE_SPEAKER_EQ is
633 	 * set to 1.0f.
634 	 */
635 	SPEAKER_TUNING_USE_SPEAKER_EQ           = 0x1f,
636 	SPEAKER_TUNING_ENABLE_CENTER_EQ         = 0x20,
637 	SPEAKER_TUNING_FRONT_LEFT_VOL_LEVEL     = 0x21,
638 	SPEAKER_TUNING_FRONT_RIGHT_VOL_LEVEL    = 0x22,
639 	SPEAKER_TUNING_CENTER_VOL_LEVEL         = 0x23,
640 	SPEAKER_TUNING_LFE_VOL_LEVEL            = 0x24,
641 	SPEAKER_TUNING_REAR_LEFT_VOL_LEVEL      = 0x25,
642 	SPEAKER_TUNING_REAR_RIGHT_VOL_LEVEL     = 0x26,
643 	SPEAKER_TUNING_SURROUND_LEFT_VOL_LEVEL  = 0x27,
644 	SPEAKER_TUNING_SURROUND_RIGHT_VOL_LEVEL = 0x28,
645 	/*
646 	 * Inversion is used when setting headphone virtualization to line
647 	 * out. Not sure why this is, but it's the only place it's ever used.
648 	 */
649 	SPEAKER_TUNING_FRONT_LEFT_INVERT        = 0x29,
650 	SPEAKER_TUNING_FRONT_RIGHT_INVERT       = 0x2a,
651 	SPEAKER_TUNING_CENTER_INVERT            = 0x2b,
652 	SPEAKER_TUNING_LFE_INVERT               = 0x2c,
653 	SPEAKER_TUNING_REAR_LEFT_INVERT         = 0x2d,
654 	SPEAKER_TUNING_REAR_RIGHT_INVERT        = 0x2e,
655 	SPEAKER_TUNING_SURROUND_LEFT_INVERT     = 0x2f,
656 	SPEAKER_TUNING_SURROUND_RIGHT_INVERT    = 0x30,
657 	/* Delay is used when setting surround speaker distance in Windows. */
658 	SPEAKER_TUNING_FRONT_LEFT_DELAY         = 0x31,
659 	SPEAKER_TUNING_FRONT_RIGHT_DELAY        = 0x32,
660 	SPEAKER_TUNING_CENTER_DELAY             = 0x33,
661 	SPEAKER_TUNING_LFE_DELAY                = 0x34,
662 	SPEAKER_TUNING_REAR_LEFT_DELAY          = 0x35,
663 	SPEAKER_TUNING_REAR_RIGHT_DELAY         = 0x36,
664 	SPEAKER_TUNING_SURROUND_LEFT_DELAY      = 0x37,
665 	SPEAKER_TUNING_SURROUND_RIGHT_DELAY     = 0x38,
666 	/* Of these two, only mute seems to ever be used. */
667 	SPEAKER_TUNING_MAIN_VOLUME              = 0x39,
668 	SPEAKER_TUNING_MUTE                     = 0x3a,
669 };
670 
671 /* Surround output channel count configuration structures. */
672 #define SPEAKER_CHANNEL_CFG_COUNT 5
673 enum {
674 	SPEAKER_CHANNELS_2_0,
675 	SPEAKER_CHANNELS_2_1,
676 	SPEAKER_CHANNELS_4_0,
677 	SPEAKER_CHANNELS_4_1,
678 	SPEAKER_CHANNELS_5_1,
679 };
680 
681 struct ca0132_alt_speaker_channel_cfg {
682 	char *name;
683 	unsigned int val;
684 };
685 
686 static const struct ca0132_alt_speaker_channel_cfg speaker_channel_cfgs[] = {
687 	{ .name = "2.0",
688 	  .val = FLOAT_ONE
689 	},
690 	{ .name = "2.1",
691 	  .val = FLOAT_TWO
692 	},
693 	{ .name = "4.0",
694 	  .val = FLOAT_FIVE
695 	},
696 	{ .name = "4.1",
697 	  .val = FLOAT_SIX
698 	},
699 	{ .name = "5.1",
700 	  .val = FLOAT_EIGHT
701 	}
702 };
703 
704 /*
705  * DSP volume setting structs. Req 1 is left volume, req 2 is right volume,
706  * and I don't know what the third req is, but it's always zero. I assume it's
707  * some sort of update or set command to tell the DSP there's new volume info.
708  */
709 #define DSP_VOL_OUT 0
710 #define DSP_VOL_IN  1
711 
712 struct ct_dsp_volume_ctl {
713 	hda_nid_t vnid;
714 	int mid; /* module ID*/
715 	unsigned int reqs[3]; /* scp req ID */
716 };
717 
718 static const struct ct_dsp_volume_ctl ca0132_alt_vol_ctls[] = {
719 	{ .vnid = VNID_SPK,
720 	  .mid = 0x32,
721 	  .reqs = {3, 4, 2}
722 	},
723 	{ .vnid = VNID_MIC,
724 	  .mid = 0x37,
725 	  .reqs = {2, 3, 1}
726 	}
727 };
728 
729 /* Values for ca0113_mmio_command_set for selecting output. */
730 #define AE_CA0113_OUT_SET_COMMANDS 6
731 struct ae_ca0113_output_set {
732 	unsigned int group[AE_CA0113_OUT_SET_COMMANDS];
733 	unsigned int target[AE_CA0113_OUT_SET_COMMANDS];
734 	unsigned int vals[NUM_OF_OUTPUTS][AE_CA0113_OUT_SET_COMMANDS];
735 };
736 
737 static const struct ae_ca0113_output_set ae5_ca0113_output_presets = {
738 	.group =  { 0x30, 0x30, 0x48, 0x48, 0x48, 0x30 },
739 	.target = { 0x2e, 0x30, 0x0d, 0x17, 0x19, 0x32 },
740 		    /* Speakers. */
741 	.vals =   { { 0x00, 0x00, 0x40, 0x00, 0x00, 0x3f },
742 		    /* Headphones. */
743 		    { 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00 } },
744 };
745 
746 static const struct ae_ca0113_output_set ae7_ca0113_output_presets = {
747 	.group  = { 0x30, 0x30, 0x48, 0x48, 0x48, 0x30 },
748 	.target = { 0x2e, 0x30, 0x0d, 0x17, 0x19, 0x32 },
749 		    /* Speakers. */
750 	.vals   = { { 0x00, 0x00, 0x40, 0x00, 0x00, 0x3f },
751 		    /* Headphones. */
752 		    { 0x3f, 0x3f, 0x00, 0x00, 0x02, 0x00 } },
753 };
754 
755 /* ae5 ca0113 command sequences to set headphone gain levels. */
756 #define AE5_HEADPHONE_GAIN_PRESET_MAX_COMMANDS 4
757 struct ae5_headphone_gain_set {
758 	char *name;
759 	unsigned int vals[AE5_HEADPHONE_GAIN_PRESET_MAX_COMMANDS];
760 };
761 
762 static const struct ae5_headphone_gain_set ae5_headphone_gain_presets[] = {
763 	{ .name = "Low (16-31",
764 	  .vals = { 0xff, 0x2c, 0xf5, 0x32 }
765 	},
766 	{ .name = "Medium (32-149",
767 	  .vals = { 0x38, 0xa8, 0x3e, 0x4c }
768 	},
769 	{ .name = "High (150-600",
770 	  .vals = { 0xff, 0xff, 0xff, 0x7f }
771 	}
772 };
773 
774 struct ae5_filter_set {
775 	char *name;
776 	unsigned int val;
777 };
778 
779 static const struct ae5_filter_set ae5_filter_presets[] = {
780 	{ .name = "Slow Roll Off",
781 	  .val = 0xa0
782 	},
783 	{ .name = "Minimum Phase",
784 	  .val = 0xc0
785 	},
786 	{ .name = "Fast Roll Off",
787 	  .val = 0x80
788 	}
789 };
790 
791 /*
792  * Data structures for storing audio router remapping data. These are used to
793  * remap a currently active streams ports.
794  */
795 struct chipio_stream_remap_data {
796 	unsigned int stream_id;
797 	unsigned int count;
798 
799 	unsigned int offset[16];
800 	unsigned int value[16];
801 };
802 
803 static const struct chipio_stream_remap_data stream_remap_data[] = {
804 	{ .stream_id = 0x14,
805 	  .count     = 0x04,
806 	  .offset    = { 0x00, 0x04, 0x08, 0x0c },
807 	  .value     = { 0x0001f8c0, 0x0001f9c1, 0x0001fac6, 0x0001fbc7 },
808 	},
809 	{ .stream_id = 0x0c,
810 	  .count     = 0x0c,
811 	  .offset    = { 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
812 			 0x20, 0x24, 0x28, 0x2c },
813 	  .value     = { 0x0001e0c0, 0x0001e1c1, 0x0001e4c2, 0x0001e5c3,
814 			 0x0001e2c4, 0x0001e3c5, 0x0001e8c6, 0x0001e9c7,
815 			 0x0001ecc8, 0x0001edc9, 0x0001eaca, 0x0001ebcb },
816 	},
817 	{ .stream_id = 0x0c,
818 	  .count     = 0x08,
819 	  .offset    = { 0x08, 0x0c, 0x10, 0x14, 0x20, 0x24, 0x28, 0x2c },
820 	  .value     = { 0x000140c2, 0x000141c3, 0x000150c4, 0x000151c5,
821 			 0x000142c8, 0x000143c9, 0x000152ca, 0x000153cb },
822 	}
823 };
824 
825 enum hda_cmd_vendor_io {
826 	/* for DspIO node */
827 	VENDOR_DSPIO_SCP_WRITE_DATA_LOW      = 0x000,
828 	VENDOR_DSPIO_SCP_WRITE_DATA_HIGH     = 0x100,
829 
830 	VENDOR_DSPIO_STATUS                  = 0xF01,
831 	VENDOR_DSPIO_SCP_POST_READ_DATA      = 0x702,
832 	VENDOR_DSPIO_SCP_READ_DATA           = 0xF02,
833 	VENDOR_DSPIO_DSP_INIT                = 0x703,
834 	VENDOR_DSPIO_SCP_POST_COUNT_QUERY    = 0x704,
835 	VENDOR_DSPIO_SCP_READ_COUNT          = 0xF04,
836 
837 	/* for ChipIO node */
838 	VENDOR_CHIPIO_ADDRESS_LOW            = 0x000,
839 	VENDOR_CHIPIO_ADDRESS_HIGH           = 0x100,
840 	VENDOR_CHIPIO_STREAM_FORMAT          = 0x200,
841 	VENDOR_CHIPIO_DATA_LOW               = 0x300,
842 	VENDOR_CHIPIO_DATA_HIGH              = 0x400,
843 
844 	VENDOR_CHIPIO_8051_WRITE_DIRECT      = 0x500,
845 	VENDOR_CHIPIO_8051_READ_DIRECT       = 0xD00,
846 
847 	VENDOR_CHIPIO_GET_PARAMETER          = 0xF00,
848 	VENDOR_CHIPIO_STATUS                 = 0xF01,
849 	VENDOR_CHIPIO_HIC_POST_READ          = 0x702,
850 	VENDOR_CHIPIO_HIC_READ_DATA          = 0xF03,
851 
852 	VENDOR_CHIPIO_8051_DATA_WRITE        = 0x707,
853 	VENDOR_CHIPIO_8051_DATA_READ         = 0xF07,
854 	VENDOR_CHIPIO_8051_PMEM_READ         = 0xF08,
855 	VENDOR_CHIPIO_8051_IRAM_WRITE        = 0x709,
856 	VENDOR_CHIPIO_8051_IRAM_READ         = 0xF09,
857 
858 	VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE   = 0x70A,
859 	VENDOR_CHIPIO_CT_EXTENSIONS_GET      = 0xF0A,
860 
861 	VENDOR_CHIPIO_PLL_PMU_WRITE          = 0x70C,
862 	VENDOR_CHIPIO_PLL_PMU_READ           = 0xF0C,
863 	VENDOR_CHIPIO_8051_ADDRESS_LOW       = 0x70D,
864 	VENDOR_CHIPIO_8051_ADDRESS_HIGH      = 0x70E,
865 	VENDOR_CHIPIO_FLAG_SET               = 0x70F,
866 	VENDOR_CHIPIO_FLAGS_GET              = 0xF0F,
867 	VENDOR_CHIPIO_PARAM_SET              = 0x710,
868 	VENDOR_CHIPIO_PARAM_GET              = 0xF10,
869 
870 	VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET  = 0x711,
871 	VENDOR_CHIPIO_PORT_ALLOC_SET         = 0x712,
872 	VENDOR_CHIPIO_PORT_ALLOC_GET         = 0xF12,
873 	VENDOR_CHIPIO_PORT_FREE_SET          = 0x713,
874 
875 	VENDOR_CHIPIO_PARAM_EX_ID_GET        = 0xF17,
876 	VENDOR_CHIPIO_PARAM_EX_ID_SET        = 0x717,
877 	VENDOR_CHIPIO_PARAM_EX_VALUE_GET     = 0xF18,
878 	VENDOR_CHIPIO_PARAM_EX_VALUE_SET     = 0x718,
879 
880 	VENDOR_CHIPIO_DMIC_CTL_SET           = 0x788,
881 	VENDOR_CHIPIO_DMIC_CTL_GET           = 0xF88,
882 	VENDOR_CHIPIO_DMIC_PIN_SET           = 0x789,
883 	VENDOR_CHIPIO_DMIC_PIN_GET           = 0xF89,
884 	VENDOR_CHIPIO_DMIC_MCLK_SET          = 0x78A,
885 	VENDOR_CHIPIO_DMIC_MCLK_GET          = 0xF8A,
886 
887 	VENDOR_CHIPIO_EAPD_SEL_SET           = 0x78D
888 };
889 
890 /*
891  *  Control flag IDs
892  */
893 enum control_flag_id {
894 	/* Connection manager stream setup is bypassed/enabled */
895 	CONTROL_FLAG_C_MGR                  = 0,
896 	/* DSP DMA is bypassed/enabled */
897 	CONTROL_FLAG_DMA                    = 1,
898 	/* 8051 'idle' mode is disabled/enabled */
899 	CONTROL_FLAG_IDLE_ENABLE            = 2,
900 	/* Tracker for the SPDIF-in path is bypassed/enabled */
901 	CONTROL_FLAG_TRACKER                = 3,
902 	/* DigitalOut to Spdif2Out connection is disabled/enabled */
903 	CONTROL_FLAG_SPDIF2OUT              = 4,
904 	/* Digital Microphone is disabled/enabled */
905 	CONTROL_FLAG_DMIC                   = 5,
906 	/* ADC_B rate is 48 kHz/96 kHz */
907 	CONTROL_FLAG_ADC_B_96KHZ            = 6,
908 	/* ADC_C rate is 48 kHz/96 kHz */
909 	CONTROL_FLAG_ADC_C_96KHZ            = 7,
910 	/* DAC rate is 48 kHz/96 kHz (affects all DACs) */
911 	CONTROL_FLAG_DAC_96KHZ              = 8,
912 	/* DSP rate is 48 kHz/96 kHz */
913 	CONTROL_FLAG_DSP_96KHZ              = 9,
914 	/* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
915 	CONTROL_FLAG_SRC_CLOCK_196MHZ       = 10,
916 	/* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
917 	CONTROL_FLAG_SRC_RATE_96KHZ         = 11,
918 	/* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
919 	CONTROL_FLAG_DECODE_LOOP            = 12,
920 	/* De-emphasis filter on DAC-1 disabled/enabled */
921 	CONTROL_FLAG_DAC1_DEEMPHASIS        = 13,
922 	/* De-emphasis filter on DAC-2 disabled/enabled */
923 	CONTROL_FLAG_DAC2_DEEMPHASIS        = 14,
924 	/* De-emphasis filter on DAC-3 disabled/enabled */
925 	CONTROL_FLAG_DAC3_DEEMPHASIS        = 15,
926 	/* High-pass filter on ADC_B disabled/enabled */
927 	CONTROL_FLAG_ADC_B_HIGH_PASS        = 16,
928 	/* High-pass filter on ADC_C disabled/enabled */
929 	CONTROL_FLAG_ADC_C_HIGH_PASS        = 17,
930 	/* Common mode on Port_A disabled/enabled */
931 	CONTROL_FLAG_PORT_A_COMMON_MODE     = 18,
932 	/* Common mode on Port_D disabled/enabled */
933 	CONTROL_FLAG_PORT_D_COMMON_MODE     = 19,
934 	/* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
935 	CONTROL_FLAG_PORT_A_10KOHM_LOAD     = 20,
936 	/* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
937 	CONTROL_FLAG_PORT_D_10KOHM_LOAD     = 21,
938 	/* ASI rate is 48kHz/96kHz */
939 	CONTROL_FLAG_ASI_96KHZ              = 22,
940 	/* DAC power settings able to control attached ports no/yes */
941 	CONTROL_FLAG_DACS_CONTROL_PORTS     = 23,
942 	/* Clock Stop OK reporting is disabled/enabled */
943 	CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
944 	/* Number of control flags */
945 	CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
946 };
947 
948 /*
949  * Control parameter IDs
950  */
951 enum control_param_id {
952 	/* 0: None, 1: Mic1In*/
953 	CONTROL_PARAM_VIP_SOURCE               = 1,
954 	/* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
955 	CONTROL_PARAM_SPDIF1_SOURCE            = 2,
956 	/* Port A output stage gain setting to use when 16 Ohm output
957 	 * impedance is selected*/
958 	CONTROL_PARAM_PORTA_160OHM_GAIN        = 8,
959 	/* Port D output stage gain setting to use when 16 Ohm output
960 	 * impedance is selected*/
961 	CONTROL_PARAM_PORTD_160OHM_GAIN        = 10,
962 
963 	/*
964 	 * This control param name was found in the 8051 memory, and makes
965 	 * sense given the fact the AE-5 uses it and has the ASI flag set.
966 	 */
967 	CONTROL_PARAM_ASI                      = 23,
968 
969 	/* Stream Control */
970 
971 	/* Select stream with the given ID */
972 	CONTROL_PARAM_STREAM_ID                = 24,
973 	/* Source connection point for the selected stream */
974 	CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
975 	/* Destination connection point for the selected stream */
976 	CONTROL_PARAM_STREAM_DEST_CONN_POINT   = 26,
977 	/* Number of audio channels in the selected stream */
978 	CONTROL_PARAM_STREAMS_CHANNELS         = 27,
979 	/*Enable control for the selected stream */
980 	CONTROL_PARAM_STREAM_CONTROL           = 28,
981 
982 	/* Connection Point Control */
983 
984 	/* Select connection point with the given ID */
985 	CONTROL_PARAM_CONN_POINT_ID            = 29,
986 	/* Connection point sample rate */
987 	CONTROL_PARAM_CONN_POINT_SAMPLE_RATE   = 30,
988 
989 	/* Node Control */
990 
991 	/* Select HDA node with the given ID */
992 	CONTROL_PARAM_NODE_ID                  = 31
993 };
994 
995 /*
996  *  Dsp Io Status codes
997  */
998 enum hda_vendor_status_dspio {
999 	/* Success */
1000 	VENDOR_STATUS_DSPIO_OK                       = 0x00,
1001 	/* Busy, unable to accept new command, the host must retry */
1002 	VENDOR_STATUS_DSPIO_BUSY                     = 0x01,
1003 	/* SCP command queue is full */
1004 	VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL   = 0x02,
1005 	/* SCP response queue is empty */
1006 	VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
1007 };
1008 
1009 /*
1010  *  Chip Io Status codes
1011  */
1012 enum hda_vendor_status_chipio {
1013 	/* Success */
1014 	VENDOR_STATUS_CHIPIO_OK   = 0x00,
1015 	/* Busy, unable to accept new command, the host must retry */
1016 	VENDOR_STATUS_CHIPIO_BUSY = 0x01
1017 };
1018 
1019 /*
1020  *  CA0132 sample rate
1021  */
1022 enum ca0132_sample_rate {
1023 	SR_6_000        = 0x00,
1024 	SR_8_000        = 0x01,
1025 	SR_9_600        = 0x02,
1026 	SR_11_025       = 0x03,
1027 	SR_16_000       = 0x04,
1028 	SR_22_050       = 0x05,
1029 	SR_24_000       = 0x06,
1030 	SR_32_000       = 0x07,
1031 	SR_44_100       = 0x08,
1032 	SR_48_000       = 0x09,
1033 	SR_88_200       = 0x0A,
1034 	SR_96_000       = 0x0B,
1035 	SR_144_000      = 0x0C,
1036 	SR_176_400      = 0x0D,
1037 	SR_192_000      = 0x0E,
1038 	SR_384_000      = 0x0F,
1039 
1040 	SR_COUNT        = 0x10,
1041 
1042 	SR_RATE_UNKNOWN = 0x1F
1043 };
1044 
1045 enum dsp_download_state {
1046 	DSP_DOWNLOAD_FAILED = -1,
1047 	DSP_DOWNLOAD_INIT   = 0,
1048 	DSP_DOWNLOADING     = 1,
1049 	DSP_DOWNLOADED      = 2
1050 };
1051 
1052 /* retrieve parameters from hda format */
1053 #define get_hdafmt_chs(fmt)	(fmt & 0xf)
1054 #define get_hdafmt_bits(fmt)	((fmt >> 4) & 0x7)
1055 #define get_hdafmt_rate(fmt)	((fmt >> 8) & 0x7f)
1056 #define get_hdafmt_type(fmt)	((fmt >> 15) & 0x1)
1057 
1058 /*
1059  * CA0132 specific
1060  */
1061 
1062 struct ca0132_spec {
1063 	const struct snd_kcontrol_new *mixers[5];
1064 	unsigned int num_mixers;
1065 	const struct hda_verb *base_init_verbs;
1066 	const struct hda_verb *base_exit_verbs;
1067 	const struct hda_verb *chip_init_verbs;
1068 	const struct hda_verb *desktop_init_verbs;
1069 	struct hda_verb *spec_init_verbs;
1070 	struct auto_pin_cfg autocfg;
1071 
1072 	/* Nodes configurations */
1073 	struct hda_multi_out multiout;
1074 	hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
1075 	hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
1076 	unsigned int num_outputs;
1077 	hda_nid_t input_pins[AUTO_PIN_LAST];
1078 	hda_nid_t adcs[AUTO_PIN_LAST];
1079 	hda_nid_t dig_out;
1080 	hda_nid_t dig_in;
1081 	unsigned int num_inputs;
1082 	hda_nid_t shared_mic_nid;
1083 	hda_nid_t shared_out_nid;
1084 	hda_nid_t unsol_tag_hp;
1085 	hda_nid_t unsol_tag_front_hp; /* for desktop ca0132 codecs */
1086 	hda_nid_t unsol_tag_amic1;
1087 
1088 	/* chip access */
1089 	struct mutex chipio_mutex; /* chip access mutex */
1090 	u32 curr_chip_addx;
1091 
1092 	/* DSP download related */
1093 	enum dsp_download_state dsp_state;
1094 	unsigned int dsp_stream_id;
1095 	unsigned int wait_scp;
1096 	unsigned int wait_scp_header;
1097 	unsigned int wait_num_data;
1098 	unsigned int scp_resp_header;
1099 	unsigned int scp_resp_data[4];
1100 	unsigned int scp_resp_count;
1101 	bool startup_check_entered;
1102 	bool dsp_reload;
1103 
1104 	/* mixer and effects related */
1105 	unsigned char dmic_ctl;
1106 	int cur_out_type;
1107 	int cur_mic_type;
1108 	long vnode_lvol[VNODES_COUNT];
1109 	long vnode_rvol[VNODES_COUNT];
1110 	long vnode_lswitch[VNODES_COUNT];
1111 	long vnode_rswitch[VNODES_COUNT];
1112 	long effects_switch[EFFECTS_COUNT];
1113 	long voicefx_val;
1114 	long cur_mic_boost;
1115 	/* ca0132_alt control related values */
1116 	unsigned char in_enum_val;
1117 	unsigned char out_enum_val;
1118 	unsigned char channel_cfg_val;
1119 	unsigned char speaker_range_val[2];
1120 	unsigned char mic_boost_enum_val;
1121 	unsigned char smart_volume_setting;
1122 	unsigned char bass_redirection_val;
1123 	long bass_redirect_xover_freq;
1124 	long fx_ctl_val[EFFECT_LEVEL_SLIDERS];
1125 	long xbass_xover_freq;
1126 	long eq_preset_val;
1127 	unsigned int tlv[4];
1128 	struct hda_vmaster_mute_hook vmaster_mute;
1129 	/* AE-5 Control values */
1130 	unsigned char ae5_headphone_gain_val;
1131 	unsigned char ae5_filter_val;
1132 	/* ZxR Control Values */
1133 	unsigned char zxr_gain_set;
1134 
1135 	struct hda_codec *codec;
1136 	struct delayed_work unsol_hp_work;
1137 	int quirk;
1138 
1139 #ifdef ENABLE_TUNING_CONTROLS
1140 	long cur_ctl_vals[TUNING_CTLS_COUNT];
1141 #endif
1142 	/*
1143 	 * The Recon3D, Sound Blaster Z, Sound Blaster ZxR, and Sound Blaster
1144 	 * AE-5 all use PCI region 2 to toggle GPIO and other currently unknown
1145 	 * things.
1146 	 */
1147 	bool use_pci_mmio;
1148 	void __iomem *mem_base;
1149 
1150 	/*
1151 	 * Whether or not to use the alt functions like alt_select_out,
1152 	 * alt_select_in, etc. Only used on desktop codecs for now, because of
1153 	 * surround sound support.
1154 	 */
1155 	bool use_alt_functions;
1156 
1157 	/*
1158 	 * Whether or not to use alt controls:	volume effect sliders, EQ
1159 	 * presets, smart volume presets, and new control names with FX prefix.
1160 	 * Renames PlayEnhancement and CrystalVoice too.
1161 	 */
1162 	bool use_alt_controls;
1163 };
1164 
1165 /*
1166  * CA0132 quirks table
1167  */
1168 enum {
1169 	QUIRK_NONE,
1170 	QUIRK_ALIENWARE,
1171 	QUIRK_ALIENWARE_M17XR4,
1172 	QUIRK_SBZ,
1173 	QUIRK_ZXR,
1174 	QUIRK_ZXR_DBPRO,
1175 	QUIRK_R3DI,
1176 	QUIRK_R3D,
1177 	QUIRK_AE5,
1178 	QUIRK_AE7,
1179 };
1180 
1181 #ifdef CONFIG_PCI
1182 #define ca0132_quirk(spec)		((spec)->quirk)
1183 #define ca0132_use_pci_mmio(spec)	((spec)->use_pci_mmio)
1184 #define ca0132_use_alt_functions(spec)	((spec)->use_alt_functions)
1185 #define ca0132_use_alt_controls(spec)	((spec)->use_alt_controls)
1186 #else
1187 #define ca0132_quirk(spec)		({ (void)(spec); QUIRK_NONE; })
1188 #define ca0132_use_alt_functions(spec)	({ (void)(spec); false; })
1189 #define ca0132_use_pci_mmio(spec)	({ (void)(spec); false; })
1190 #define ca0132_use_alt_controls(spec)	({ (void)(spec); false; })
1191 #endif
1192 
1193 static const struct hda_pintbl alienware_pincfgs[] = {
1194 	{ 0x0b, 0x90170110 }, /* Builtin Speaker */
1195 	{ 0x0c, 0x411111f0 }, /* N/A */
1196 	{ 0x0d, 0x411111f0 }, /* N/A */
1197 	{ 0x0e, 0x411111f0 }, /* N/A */
1198 	{ 0x0f, 0x0321101f }, /* HP */
1199 	{ 0x10, 0x411111f0 }, /* Headset?  disabled for now */
1200 	{ 0x11, 0x03a11021 }, /* Mic */
1201 	{ 0x12, 0xd5a30140 }, /* Builtin Mic */
1202 	{ 0x13, 0x411111f0 }, /* N/A */
1203 	{ 0x18, 0x411111f0 }, /* N/A */
1204 	{}
1205 };
1206 
1207 /* Sound Blaster Z pin configs taken from Windows Driver */
1208 static const struct hda_pintbl sbz_pincfgs[] = {
1209 	{ 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
1210 	{ 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1211 	{ 0x0d, 0x014510f0 }, /* Digital Out */
1212 	{ 0x0e, 0x01c510f0 }, /* SPDIF In */
1213 	{ 0x0f, 0x0221701f }, /* Port A -- BackPanel HP */
1214 	{ 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
1215 	{ 0x11, 0x01017014 }, /* Port B -- LineMicIn2 / Rear L/R */
1216 	{ 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
1217 	{ 0x13, 0x908700f0 }, /* What U Hear In*/
1218 	{ 0x18, 0x50d000f0 }, /* N/A */
1219 	{}
1220 };
1221 
1222 /* Sound Blaster ZxR pin configs taken from Windows Driver */
1223 static const struct hda_pintbl zxr_pincfgs[] = {
1224 	{ 0x0b, 0x01047110 }, /* Port G -- Lineout FRONT L/R */
1225 	{ 0x0c, 0x414510f0 }, /* SPDIF Out 1 - Disabled*/
1226 	{ 0x0d, 0x014510f0 }, /* Digital Out */
1227 	{ 0x0e, 0x41c520f0 }, /* SPDIF In - Disabled*/
1228 	{ 0x0f, 0x0122711f }, /* Port A -- BackPanel HP */
1229 	{ 0x10, 0x01017111 }, /* Port D -- Center/LFE */
1230 	{ 0x11, 0x01017114 }, /* Port B -- LineMicIn2 / Rear L/R */
1231 	{ 0x12, 0x01a271f0 }, /* Port C -- LineIn1 */
1232 	{ 0x13, 0x908700f0 }, /* What U Hear In*/
1233 	{ 0x18, 0x50d000f0 }, /* N/A */
1234 	{}
1235 };
1236 
1237 /* Recon3D pin configs taken from Windows Driver */
1238 static const struct hda_pintbl r3d_pincfgs[] = {
1239 	{ 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1240 	{ 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1241 	{ 0x0d, 0x014510f0 }, /* Digital Out */
1242 	{ 0x0e, 0x01c520f0 }, /* SPDIF In */
1243 	{ 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1244 	{ 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1245 	{ 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1246 	{ 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1247 	{ 0x13, 0x908700f0 }, /* What U Hear In*/
1248 	{ 0x18, 0x50d000f0 }, /* N/A */
1249 	{}
1250 };
1251 
1252 /* Sound Blaster AE-5 pin configs taken from Windows Driver */
1253 static const struct hda_pintbl ae5_pincfgs[] = {
1254 	{ 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
1255 	{ 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1256 	{ 0x0d, 0x014510f0 }, /* Digital Out */
1257 	{ 0x0e, 0x01c510f0 }, /* SPDIF In */
1258 	{ 0x0f, 0x01017114 }, /* Port A -- Rear L/R. */
1259 	{ 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
1260 	{ 0x11, 0x012170ff }, /* Port B -- LineMicIn2 / Rear Headphone */
1261 	{ 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
1262 	{ 0x13, 0x908700f0 }, /* What U Hear In*/
1263 	{ 0x18, 0x50d000f0 }, /* N/A */
1264 	{}
1265 };
1266 
1267 /* Recon3D integrated pin configs taken from Windows Driver */
1268 static const struct hda_pintbl r3di_pincfgs[] = {
1269 	{ 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
1270 	{ 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
1271 	{ 0x0d, 0x014510f0 }, /* Digital Out */
1272 	{ 0x0e, 0x41c520f0 }, /* SPDIF In */
1273 	{ 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
1274 	{ 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
1275 	{ 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
1276 	{ 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
1277 	{ 0x13, 0x908700f0 }, /* What U Hear In*/
1278 	{ 0x18, 0x500000f0 }, /* N/A */
1279 	{}
1280 };
1281 
1282 static const struct hda_pintbl ae7_pincfgs[] = {
1283 	{ 0x0b, 0x01017010 },
1284 	{ 0x0c, 0x014510f0 },
1285 	{ 0x0d, 0x414510f0 },
1286 	{ 0x0e, 0x01c520f0 },
1287 	{ 0x0f, 0x01017114 },
1288 	{ 0x10, 0x01017011 },
1289 	{ 0x11, 0x018170ff },
1290 	{ 0x12, 0x01a170f0 },
1291 	{ 0x13, 0x908700f0 },
1292 	{ 0x18, 0x500000f0 },
1293 	{}
1294 };
1295 
1296 static const struct snd_pci_quirk ca0132_quirks[] = {
1297 	SND_PCI_QUIRK(0x1028, 0x057b, "Alienware M17x R4", QUIRK_ALIENWARE_M17XR4),
1298 	SND_PCI_QUIRK(0x1028, 0x0685, "Alienware 15 2015", QUIRK_ALIENWARE),
1299 	SND_PCI_QUIRK(0x1028, 0x0688, "Alienware 17 2015", QUIRK_ALIENWARE),
1300 	SND_PCI_QUIRK(0x1028, 0x0708, "Alienware 15 R2 2016", QUIRK_ALIENWARE),
1301 	SND_PCI_QUIRK(0x1102, 0x0010, "Sound Blaster Z", QUIRK_SBZ),
1302 	SND_PCI_QUIRK(0x1102, 0x0023, "Sound Blaster Z", QUIRK_SBZ),
1303 	SND_PCI_QUIRK(0x1102, 0x0027, "Sound Blaster Z", QUIRK_SBZ),
1304 	SND_PCI_QUIRK(0x1102, 0x0033, "Sound Blaster ZxR", QUIRK_SBZ),
1305 	SND_PCI_QUIRK(0x1458, 0xA016, "Recon3Di", QUIRK_R3DI),
1306 	SND_PCI_QUIRK(0x1458, 0xA026, "Gigabyte G1.Sniper Z97", QUIRK_R3DI),
1307 	SND_PCI_QUIRK(0x1458, 0xA036, "Gigabyte GA-Z170X-Gaming 7", QUIRK_R3DI),
1308 	SND_PCI_QUIRK(0x3842, 0x1038, "EVGA X99 Classified", QUIRK_R3DI),
1309 	SND_PCI_QUIRK(0x1102, 0x0013, "Recon3D", QUIRK_R3D),
1310 	SND_PCI_QUIRK(0x1102, 0x0018, "Recon3D", QUIRK_R3D),
1311 	SND_PCI_QUIRK(0x1102, 0x0051, "Sound Blaster AE-5", QUIRK_AE5),
1312 	SND_PCI_QUIRK(0x1102, 0x0191, "Sound Blaster AE-5 Plus", QUIRK_AE5),
1313 	SND_PCI_QUIRK(0x1102, 0x0081, "Sound Blaster AE-7", QUIRK_AE7),
1314 	{}
1315 };
1316 
1317 /* Output selection quirk info structures. */
1318 #define MAX_QUIRK_MMIO_GPIO_SET_VALS 3
1319 #define MAX_QUIRK_SCP_SET_VALS 2
1320 struct ca0132_alt_out_set_info {
1321 	unsigned int dac2port; /* ParamID 0x0d value. */
1322 
1323 	bool has_hda_gpio;
1324 	char hda_gpio_pin;
1325 	char hda_gpio_set;
1326 
1327 	unsigned int mmio_gpio_count;
1328 	char mmio_gpio_pin[MAX_QUIRK_MMIO_GPIO_SET_VALS];
1329 	char mmio_gpio_set[MAX_QUIRK_MMIO_GPIO_SET_VALS];
1330 
1331 	unsigned int scp_cmds_count;
1332 	unsigned int scp_cmd_mid[MAX_QUIRK_SCP_SET_VALS];
1333 	unsigned int scp_cmd_req[MAX_QUIRK_SCP_SET_VALS];
1334 	unsigned int scp_cmd_val[MAX_QUIRK_SCP_SET_VALS];
1335 
1336 	bool has_chipio_write;
1337 	unsigned int chipio_write_addr;
1338 	unsigned int chipio_write_data;
1339 };
1340 
1341 struct ca0132_alt_out_set_quirk_data {
1342 	int quirk_id;
1343 
1344 	bool has_headphone_gain;
1345 	bool is_ae_series;
1346 
1347 	struct ca0132_alt_out_set_info out_set_info[NUM_OF_OUTPUTS];
1348 };
1349 
1350 static const struct ca0132_alt_out_set_quirk_data quirk_out_set_data[] = {
1351 	{ .quirk_id = QUIRK_R3DI,
1352 	  .has_headphone_gain = false,
1353 	  .is_ae_series       = false,
1354 	  .out_set_info = {
1355 		/* Speakers. */
1356 		{ .dac2port         = 0x24,
1357 		  .has_hda_gpio     = true,
1358 		  .hda_gpio_pin     = 2,
1359 		  .hda_gpio_set     = 1,
1360 		  .mmio_gpio_count  = 0,
1361 		  .scp_cmds_count   = 0,
1362 		  .has_chipio_write = false,
1363 		},
1364 		/* Headphones. */
1365 		{ .dac2port         = 0x21,
1366 		  .has_hda_gpio     = true,
1367 		  .hda_gpio_pin     = 2,
1368 		  .hda_gpio_set     = 0,
1369 		  .mmio_gpio_count  = 0,
1370 		  .scp_cmds_count   = 0,
1371 		  .has_chipio_write = false,
1372 		} },
1373 	},
1374 	{ .quirk_id = QUIRK_R3D,
1375 	  .has_headphone_gain = false,
1376 	  .is_ae_series       = false,
1377 	  .out_set_info = {
1378 		/* Speakers. */
1379 		{ .dac2port         = 0x24,
1380 		  .has_hda_gpio     = false,
1381 		  .mmio_gpio_count  = 1,
1382 		  .mmio_gpio_pin    = { 1 },
1383 		  .mmio_gpio_set    = { 1 },
1384 		  .scp_cmds_count   = 0,
1385 		  .has_chipio_write = false,
1386 		},
1387 		/* Headphones. */
1388 		{ .dac2port         = 0x21,
1389 		  .has_hda_gpio     = false,
1390 		  .mmio_gpio_count  = 1,
1391 		  .mmio_gpio_pin    = { 1 },
1392 		  .mmio_gpio_set    = { 0 },
1393 		  .scp_cmds_count   = 0,
1394 		  .has_chipio_write = false,
1395 		} },
1396 	},
1397 	{ .quirk_id = QUIRK_SBZ,
1398 	  .has_headphone_gain = false,
1399 	  .is_ae_series       = false,
1400 	  .out_set_info = {
1401 		/* Speakers. */
1402 		{ .dac2port         = 0x18,
1403 		  .has_hda_gpio     = false,
1404 		  .mmio_gpio_count  = 3,
1405 		  .mmio_gpio_pin    = { 7, 4, 1 },
1406 		  .mmio_gpio_set    = { 0, 1, 1 },
1407 		  .scp_cmds_count   = 0,
1408 		  .has_chipio_write = false, },
1409 		/* Headphones. */
1410 		{ .dac2port         = 0x12,
1411 		  .has_hda_gpio     = false,
1412 		  .mmio_gpio_count  = 3,
1413 		  .mmio_gpio_pin    = { 7, 4, 1 },
1414 		  .mmio_gpio_set    = { 1, 1, 0 },
1415 		  .scp_cmds_count   = 0,
1416 		  .has_chipio_write = false,
1417 		} },
1418 	},
1419 	{ .quirk_id = QUIRK_ZXR,
1420 	  .has_headphone_gain = true,
1421 	  .is_ae_series       = false,
1422 	  .out_set_info = {
1423 		/* Speakers. */
1424 		{ .dac2port         = 0x24,
1425 		  .has_hda_gpio     = false,
1426 		  .mmio_gpio_count  = 3,
1427 		  .mmio_gpio_pin    = { 2, 3, 5 },
1428 		  .mmio_gpio_set    = { 1, 1, 0 },
1429 		  .scp_cmds_count   = 0,
1430 		  .has_chipio_write = false,
1431 		},
1432 		/* Headphones. */
1433 		{ .dac2port         = 0x21,
1434 		  .has_hda_gpio     = false,
1435 		  .mmio_gpio_count  = 3,
1436 		  .mmio_gpio_pin    = { 2, 3, 5 },
1437 		  .mmio_gpio_set    = { 0, 1, 1 },
1438 		  .scp_cmds_count   = 0,
1439 		  .has_chipio_write = false,
1440 		} },
1441 	},
1442 	{ .quirk_id = QUIRK_AE5,
1443 	  .has_headphone_gain = true,
1444 	  .is_ae_series       = true,
1445 	  .out_set_info = {
1446 		/* Speakers. */
1447 		{ .dac2port          = 0xa4,
1448 		  .has_hda_gpio      = false,
1449 		  .mmio_gpio_count   = 0,
1450 		  .scp_cmds_count    = 2,
1451 		  .scp_cmd_mid       = { 0x96, 0x96 },
1452 		  .scp_cmd_req       = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
1453 					 SPEAKER_TUNING_FRONT_RIGHT_INVERT },
1454 		  .scp_cmd_val       = { FLOAT_ZERO, FLOAT_ZERO },
1455 		  .has_chipio_write  = true,
1456 		  .chipio_write_addr = 0x0018b03c,
1457 		  .chipio_write_data = 0x00000012
1458 		},
1459 		/* Headphones. */
1460 		{ .dac2port          = 0xa1,
1461 		  .has_hda_gpio      = false,
1462 		  .mmio_gpio_count   = 0,
1463 		  .scp_cmds_count    = 2,
1464 		  .scp_cmd_mid       = { 0x96, 0x96 },
1465 		  .scp_cmd_req       = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
1466 					 SPEAKER_TUNING_FRONT_RIGHT_INVERT },
1467 		  .scp_cmd_val       = { FLOAT_ONE, FLOAT_ONE },
1468 		  .has_chipio_write  = true,
1469 		  .chipio_write_addr = 0x0018b03c,
1470 		  .chipio_write_data = 0x00000012
1471 		} },
1472 	},
1473 	{ .quirk_id = QUIRK_AE7,
1474 	  .has_headphone_gain = true,
1475 	  .is_ae_series       = true,
1476 	  .out_set_info = {
1477 		/* Speakers. */
1478 		{ .dac2port          = 0x58,
1479 		  .has_hda_gpio      = false,
1480 		  .mmio_gpio_count   = 1,
1481 		  .mmio_gpio_pin     = { 0 },
1482 		  .mmio_gpio_set     = { 1 },
1483 		  .scp_cmds_count    = 2,
1484 		  .scp_cmd_mid       = { 0x96, 0x96 },
1485 		  .scp_cmd_req       = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
1486 					 SPEAKER_TUNING_FRONT_RIGHT_INVERT },
1487 		  .scp_cmd_val       = { FLOAT_ZERO, FLOAT_ZERO },
1488 		  .has_chipio_write  = true,
1489 		  .chipio_write_addr = 0x0018b03c,
1490 		  .chipio_write_data = 0x00000000
1491 		},
1492 		/* Headphones. */
1493 		{ .dac2port          = 0x58,
1494 		  .has_hda_gpio      = false,
1495 		  .mmio_gpio_count   = 1,
1496 		  .mmio_gpio_pin     = { 0 },
1497 		  .mmio_gpio_set     = { 1 },
1498 		  .scp_cmds_count    = 2,
1499 		  .scp_cmd_mid       = { 0x96, 0x96 },
1500 		  .scp_cmd_req       = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
1501 					 SPEAKER_TUNING_FRONT_RIGHT_INVERT },
1502 		  .scp_cmd_val       = { FLOAT_ONE, FLOAT_ONE },
1503 		  .has_chipio_write  = true,
1504 		  .chipio_write_addr = 0x0018b03c,
1505 		  .chipio_write_data = 0x00000010
1506 		} },
1507 	}
1508 };
1509 
1510 /*
1511  * CA0132 codec access
1512  */
1513 static unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
1514 		unsigned int verb, unsigned int parm, unsigned int *res)
1515 {
1516 	unsigned int response;
1517 	response = snd_hda_codec_read(codec, nid, 0, verb, parm);
1518 	*res = response;
1519 
1520 	return ((response == -1) ? -1 : 0);
1521 }
1522 
1523 static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
1524 		unsigned short converter_format, unsigned int *res)
1525 {
1526 	return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
1527 				converter_format & 0xffff, res);
1528 }
1529 
1530 static int codec_set_converter_stream_channel(struct hda_codec *codec,
1531 				hda_nid_t nid, unsigned char stream,
1532 				unsigned char channel, unsigned int *res)
1533 {
1534 	unsigned char converter_stream_channel = 0;
1535 
1536 	converter_stream_channel = (stream << 4) | (channel & 0x0f);
1537 	return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
1538 				converter_stream_channel, res);
1539 }
1540 
1541 /* Chip access helper function */
1542 static int chipio_send(struct hda_codec *codec,
1543 		       unsigned int reg,
1544 		       unsigned int data)
1545 {
1546 	unsigned int res;
1547 	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
1548 
1549 	/* send bits of data specified by reg */
1550 	do {
1551 		res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1552 					 reg, data);
1553 		if (res == VENDOR_STATUS_CHIPIO_OK)
1554 			return 0;
1555 		msleep(20);
1556 	} while (time_before(jiffies, timeout));
1557 
1558 	return -EIO;
1559 }
1560 
1561 /*
1562  * Write chip address through the vendor widget -- NOT protected by the Mutex!
1563  */
1564 static int chipio_write_address(struct hda_codec *codec,
1565 				unsigned int chip_addx)
1566 {
1567 	struct ca0132_spec *spec = codec->spec;
1568 	int res;
1569 
1570 	if (spec->curr_chip_addx == chip_addx)
1571 			return 0;
1572 
1573 	/* send low 16 bits of the address */
1574 	res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
1575 			  chip_addx & 0xffff);
1576 
1577 	if (res != -EIO) {
1578 		/* send high 16 bits of the address */
1579 		res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
1580 				  chip_addx >> 16);
1581 	}
1582 
1583 	spec->curr_chip_addx = (res < 0) ? ~0U : chip_addx;
1584 
1585 	return res;
1586 }
1587 
1588 /*
1589  * Write data through the vendor widget -- NOT protected by the Mutex!
1590  */
1591 static int chipio_write_data(struct hda_codec *codec, unsigned int data)
1592 {
1593 	struct ca0132_spec *spec = codec->spec;
1594 	int res;
1595 
1596 	/* send low 16 bits of the data */
1597 	res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
1598 
1599 	if (res != -EIO) {
1600 		/* send high 16 bits of the data */
1601 		res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
1602 				  data >> 16);
1603 	}
1604 
1605 	/*If no error encountered, automatically increment the address
1606 	as per chip behaviour*/
1607 	spec->curr_chip_addx = (res != -EIO) ?
1608 					(spec->curr_chip_addx + 4) : ~0U;
1609 	return res;
1610 }
1611 
1612 /*
1613  * Write multiple data through the vendor widget -- NOT protected by the Mutex!
1614  */
1615 static int chipio_write_data_multiple(struct hda_codec *codec,
1616 				      const u32 *data,
1617 				      unsigned int count)
1618 {
1619 	int status = 0;
1620 
1621 	if (data == NULL) {
1622 		codec_dbg(codec, "chipio_write_data null ptr\n");
1623 		return -EINVAL;
1624 	}
1625 
1626 	while ((count-- != 0) && (status == 0))
1627 		status = chipio_write_data(codec, *data++);
1628 
1629 	return status;
1630 }
1631 
1632 
1633 /*
1634  * Read data through the vendor widget -- NOT protected by the Mutex!
1635  */
1636 static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
1637 {
1638 	struct ca0132_spec *spec = codec->spec;
1639 	int res;
1640 
1641 	/* post read */
1642 	res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
1643 
1644 	if (res != -EIO) {
1645 		/* read status */
1646 		res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
1647 	}
1648 
1649 	if (res != -EIO) {
1650 		/* read data */
1651 		*data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1652 					   VENDOR_CHIPIO_HIC_READ_DATA,
1653 					   0);
1654 	}
1655 
1656 	/*If no error encountered, automatically increment the address
1657 	as per chip behaviour*/
1658 	spec->curr_chip_addx = (res != -EIO) ?
1659 					(spec->curr_chip_addx + 4) : ~0U;
1660 	return res;
1661 }
1662 
1663 /*
1664  * Write given value to the given address through the chip I/O widget.
1665  * protected by the Mutex
1666  */
1667 static int chipio_write(struct hda_codec *codec,
1668 		unsigned int chip_addx, const unsigned int data)
1669 {
1670 	struct ca0132_spec *spec = codec->spec;
1671 	int err;
1672 
1673 	mutex_lock(&spec->chipio_mutex);
1674 
1675 	/* write the address, and if successful proceed to write data */
1676 	err = chipio_write_address(codec, chip_addx);
1677 	if (err < 0)
1678 		goto exit;
1679 
1680 	err = chipio_write_data(codec, data);
1681 	if (err < 0)
1682 		goto exit;
1683 
1684 exit:
1685 	mutex_unlock(&spec->chipio_mutex);
1686 	return err;
1687 }
1688 
1689 /*
1690  * Write given value to the given address through the chip I/O widget.
1691  * not protected by the Mutex
1692  */
1693 static int chipio_write_no_mutex(struct hda_codec *codec,
1694 		unsigned int chip_addx, const unsigned int data)
1695 {
1696 	int err;
1697 
1698 
1699 	/* write the address, and if successful proceed to write data */
1700 	err = chipio_write_address(codec, chip_addx);
1701 	if (err < 0)
1702 		goto exit;
1703 
1704 	err = chipio_write_data(codec, data);
1705 	if (err < 0)
1706 		goto exit;
1707 
1708 exit:
1709 	return err;
1710 }
1711 
1712 /*
1713  * Write multiple values to the given address through the chip I/O widget.
1714  * protected by the Mutex
1715  */
1716 static int chipio_write_multiple(struct hda_codec *codec,
1717 				 u32 chip_addx,
1718 				 const u32 *data,
1719 				 unsigned int count)
1720 {
1721 	struct ca0132_spec *spec = codec->spec;
1722 	int status;
1723 
1724 	mutex_lock(&spec->chipio_mutex);
1725 	status = chipio_write_address(codec, chip_addx);
1726 	if (status < 0)
1727 		goto error;
1728 
1729 	status = chipio_write_data_multiple(codec, data, count);
1730 error:
1731 	mutex_unlock(&spec->chipio_mutex);
1732 
1733 	return status;
1734 }
1735 
1736 /*
1737  * Read the given address through the chip I/O widget
1738  * protected by the Mutex
1739  */
1740 static int chipio_read(struct hda_codec *codec,
1741 		unsigned int chip_addx, unsigned int *data)
1742 {
1743 	struct ca0132_spec *spec = codec->spec;
1744 	int err;
1745 
1746 	mutex_lock(&spec->chipio_mutex);
1747 
1748 	/* write the address, and if successful proceed to write data */
1749 	err = chipio_write_address(codec, chip_addx);
1750 	if (err < 0)
1751 		goto exit;
1752 
1753 	err = chipio_read_data(codec, data);
1754 	if (err < 0)
1755 		goto exit;
1756 
1757 exit:
1758 	mutex_unlock(&spec->chipio_mutex);
1759 	return err;
1760 }
1761 
1762 /*
1763  * Set chip control flags through the chip I/O widget.
1764  */
1765 static void chipio_set_control_flag(struct hda_codec *codec,
1766 				    enum control_flag_id flag_id,
1767 				    bool flag_state)
1768 {
1769 	unsigned int val;
1770 	unsigned int flag_bit;
1771 
1772 	flag_bit = (flag_state ? 1 : 0);
1773 	val = (flag_bit << 7) | (flag_id);
1774 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1775 			    VENDOR_CHIPIO_FLAG_SET, val);
1776 }
1777 
1778 /*
1779  * Set chip parameters through the chip I/O widget.
1780  */
1781 static void chipio_set_control_param(struct hda_codec *codec,
1782 		enum control_param_id param_id, int param_val)
1783 {
1784 	struct ca0132_spec *spec = codec->spec;
1785 	int val;
1786 
1787 	if ((param_id < 32) && (param_val < 8)) {
1788 		val = (param_val << 5) | (param_id);
1789 		snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1790 				    VENDOR_CHIPIO_PARAM_SET, val);
1791 	} else {
1792 		mutex_lock(&spec->chipio_mutex);
1793 		if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1794 			snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1795 					    VENDOR_CHIPIO_PARAM_EX_ID_SET,
1796 					    param_id);
1797 			snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1798 					    VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1799 					    param_val);
1800 		}
1801 		mutex_unlock(&spec->chipio_mutex);
1802 	}
1803 }
1804 
1805 /*
1806  * Set chip parameters through the chip I/O widget. NO MUTEX.
1807  */
1808 static void chipio_set_control_param_no_mutex(struct hda_codec *codec,
1809 		enum control_param_id param_id, int param_val)
1810 {
1811 	int val;
1812 
1813 	if ((param_id < 32) && (param_val < 8)) {
1814 		val = (param_val << 5) | (param_id);
1815 		snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1816 				    VENDOR_CHIPIO_PARAM_SET, val);
1817 	} else {
1818 		if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
1819 			snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1820 					    VENDOR_CHIPIO_PARAM_EX_ID_SET,
1821 					    param_id);
1822 			snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1823 					    VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
1824 					    param_val);
1825 		}
1826 	}
1827 }
1828 /*
1829  * Connect stream to a source point, and then connect
1830  * that source point to a destination point.
1831  */
1832 static void chipio_set_stream_source_dest(struct hda_codec *codec,
1833 				int streamid, int source_point, int dest_point)
1834 {
1835 	chipio_set_control_param_no_mutex(codec,
1836 			CONTROL_PARAM_STREAM_ID, streamid);
1837 	chipio_set_control_param_no_mutex(codec,
1838 			CONTROL_PARAM_STREAM_SOURCE_CONN_POINT, source_point);
1839 	chipio_set_control_param_no_mutex(codec,
1840 			CONTROL_PARAM_STREAM_DEST_CONN_POINT, dest_point);
1841 }
1842 
1843 /*
1844  * Set number of channels in the selected stream.
1845  */
1846 static void chipio_set_stream_channels(struct hda_codec *codec,
1847 				int streamid, unsigned int channels)
1848 {
1849 	chipio_set_control_param_no_mutex(codec,
1850 			CONTROL_PARAM_STREAM_ID, streamid);
1851 	chipio_set_control_param_no_mutex(codec,
1852 			CONTROL_PARAM_STREAMS_CHANNELS, channels);
1853 }
1854 
1855 /*
1856  * Enable/Disable audio stream.
1857  */
1858 static void chipio_set_stream_control(struct hda_codec *codec,
1859 				int streamid, int enable)
1860 {
1861 	chipio_set_control_param_no_mutex(codec,
1862 			CONTROL_PARAM_STREAM_ID, streamid);
1863 	chipio_set_control_param_no_mutex(codec,
1864 			CONTROL_PARAM_STREAM_CONTROL, enable);
1865 }
1866 
1867 /*
1868  * Get ChipIO audio stream's status.
1869  */
1870 static void chipio_get_stream_control(struct hda_codec *codec,
1871 				int streamid, unsigned int *enable)
1872 {
1873 	chipio_set_control_param_no_mutex(codec,
1874 			CONTROL_PARAM_STREAM_ID, streamid);
1875 	*enable = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1876 			   VENDOR_CHIPIO_PARAM_GET,
1877 			   CONTROL_PARAM_STREAM_CONTROL);
1878 }
1879 
1880 /*
1881  * Set sampling rate of the connection point. NO MUTEX.
1882  */
1883 static void chipio_set_conn_rate_no_mutex(struct hda_codec *codec,
1884 				int connid, enum ca0132_sample_rate rate)
1885 {
1886 	chipio_set_control_param_no_mutex(codec,
1887 			CONTROL_PARAM_CONN_POINT_ID, connid);
1888 	chipio_set_control_param_no_mutex(codec,
1889 			CONTROL_PARAM_CONN_POINT_SAMPLE_RATE, rate);
1890 }
1891 
1892 /*
1893  * Set sampling rate of the connection point.
1894  */
1895 static void chipio_set_conn_rate(struct hda_codec *codec,
1896 				int connid, enum ca0132_sample_rate rate)
1897 {
1898 	chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
1899 	chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
1900 				 rate);
1901 }
1902 
1903 /*
1904  * Writes to the 8051's internal address space directly instead of indirectly,
1905  * giving access to the special function registers located at addresses
1906  * 0x80-0xFF.
1907  */
1908 static void chipio_8051_write_direct(struct hda_codec *codec,
1909 		unsigned int addr, unsigned int data)
1910 {
1911 	unsigned int verb;
1912 
1913 	verb = VENDOR_CHIPIO_8051_WRITE_DIRECT | data;
1914 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, verb, addr);
1915 }
1916 
1917 /*
1918  * Writes to the 8051's exram, which has 16-bits of address space.
1919  * Data at addresses 0x2000-0x7fff is mirrored to 0x8000-0xdfff.
1920  * Data at 0x8000-0xdfff can also be used as program memory for the 8051 by
1921  * setting the pmem bank selection SFR.
1922  * 0xe000-0xffff is always mapped as program memory, with only 0xf000-0xffff
1923  * being writable.
1924  */
1925 static void chipio_8051_set_address(struct hda_codec *codec, unsigned int addr)
1926 {
1927 	unsigned int tmp;
1928 
1929 	/* Lower 8-bits. */
1930 	tmp = addr & 0xff;
1931 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1932 			    VENDOR_CHIPIO_8051_ADDRESS_LOW, tmp);
1933 
1934 	/* Upper 8-bits. */
1935 	tmp = (addr >> 8) & 0xff;
1936 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1937 			    VENDOR_CHIPIO_8051_ADDRESS_HIGH, tmp);
1938 }
1939 
1940 static void chipio_8051_set_data(struct hda_codec *codec, unsigned int data)
1941 {
1942 	/* 8-bits of data. */
1943 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1944 			    VENDOR_CHIPIO_8051_DATA_WRITE, data & 0xff);
1945 }
1946 
1947 static unsigned int chipio_8051_get_data(struct hda_codec *codec)
1948 {
1949 	return snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
1950 				   VENDOR_CHIPIO_8051_DATA_READ, 0);
1951 }
1952 
1953 /* PLL_PMU writes share the lower address register of the 8051 exram writes. */
1954 static void chipio_8051_set_data_pll(struct hda_codec *codec, unsigned int data)
1955 {
1956 	/* 8-bits of data. */
1957 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
1958 			    VENDOR_CHIPIO_PLL_PMU_WRITE, data & 0xff);
1959 }
1960 
1961 static void chipio_8051_write_exram(struct hda_codec *codec,
1962 		unsigned int addr, unsigned int data)
1963 {
1964 	struct ca0132_spec *spec = codec->spec;
1965 
1966 	mutex_lock(&spec->chipio_mutex);
1967 
1968 	chipio_8051_set_address(codec, addr);
1969 	chipio_8051_set_data(codec, data);
1970 
1971 	mutex_unlock(&spec->chipio_mutex);
1972 }
1973 
1974 static void chipio_8051_write_exram_no_mutex(struct hda_codec *codec,
1975 		unsigned int addr, unsigned int data)
1976 {
1977 	chipio_8051_set_address(codec, addr);
1978 	chipio_8051_set_data(codec, data);
1979 }
1980 
1981 /* Readback data from the 8051's exram. No mutex. */
1982 static void chipio_8051_read_exram(struct hda_codec *codec,
1983 		unsigned int addr, unsigned int *data)
1984 {
1985 	chipio_8051_set_address(codec, addr);
1986 	*data = chipio_8051_get_data(codec);
1987 }
1988 
1989 static void chipio_8051_write_pll_pmu(struct hda_codec *codec,
1990 		unsigned int addr, unsigned int data)
1991 {
1992 	struct ca0132_spec *spec = codec->spec;
1993 
1994 	mutex_lock(&spec->chipio_mutex);
1995 
1996 	chipio_8051_set_address(codec, addr & 0xff);
1997 	chipio_8051_set_data_pll(codec, data);
1998 
1999 	mutex_unlock(&spec->chipio_mutex);
2000 }
2001 
2002 static void chipio_8051_write_pll_pmu_no_mutex(struct hda_codec *codec,
2003 		unsigned int addr, unsigned int data)
2004 {
2005 	chipio_8051_set_address(codec, addr & 0xff);
2006 	chipio_8051_set_data_pll(codec, data);
2007 }
2008 
2009 /*
2010  * Enable clocks.
2011  */
2012 static void chipio_enable_clocks(struct hda_codec *codec)
2013 {
2014 	struct ca0132_spec *spec = codec->spec;
2015 
2016 	mutex_lock(&spec->chipio_mutex);
2017 
2018 	chipio_8051_write_pll_pmu_no_mutex(codec, 0x00, 0xff);
2019 	chipio_8051_write_pll_pmu_no_mutex(codec, 0x05, 0x0b);
2020 	chipio_8051_write_pll_pmu_no_mutex(codec, 0x06, 0xff);
2021 
2022 	mutex_unlock(&spec->chipio_mutex);
2023 }
2024 
2025 /*
2026  * CA0132 DSP IO stuffs
2027  */
2028 static int dspio_send(struct hda_codec *codec, unsigned int reg,
2029 		      unsigned int data)
2030 {
2031 	int res;
2032 	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
2033 
2034 	/* send bits of data specified by reg to dsp */
2035 	do {
2036 		res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
2037 		if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
2038 			return res;
2039 		msleep(20);
2040 	} while (time_before(jiffies, timeout));
2041 
2042 	return -EIO;
2043 }
2044 
2045 /*
2046  * Wait for DSP to be ready for commands
2047  */
2048 static void dspio_write_wait(struct hda_codec *codec)
2049 {
2050 	int status;
2051 	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
2052 
2053 	do {
2054 		status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
2055 						VENDOR_DSPIO_STATUS, 0);
2056 		if ((status == VENDOR_STATUS_DSPIO_OK) ||
2057 		    (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
2058 			break;
2059 		msleep(1);
2060 	} while (time_before(jiffies, timeout));
2061 }
2062 
2063 /*
2064  * Write SCP data to DSP
2065  */
2066 static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
2067 {
2068 	struct ca0132_spec *spec = codec->spec;
2069 	int status;
2070 
2071 	dspio_write_wait(codec);
2072 
2073 	mutex_lock(&spec->chipio_mutex);
2074 	status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
2075 			    scp_data & 0xffff);
2076 	if (status < 0)
2077 		goto error;
2078 
2079 	status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
2080 				    scp_data >> 16);
2081 	if (status < 0)
2082 		goto error;
2083 
2084 	/* OK, now check if the write itself has executed*/
2085 	status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
2086 				    VENDOR_DSPIO_STATUS, 0);
2087 error:
2088 	mutex_unlock(&spec->chipio_mutex);
2089 
2090 	return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
2091 			-EIO : 0;
2092 }
2093 
2094 /*
2095  * Write multiple SCP data to DSP
2096  */
2097 static int dspio_write_multiple(struct hda_codec *codec,
2098 				unsigned int *buffer, unsigned int size)
2099 {
2100 	int status = 0;
2101 	unsigned int count;
2102 
2103 	if (buffer == NULL)
2104 		return -EINVAL;
2105 
2106 	count = 0;
2107 	while (count < size) {
2108 		status = dspio_write(codec, *buffer++);
2109 		if (status != 0)
2110 			break;
2111 		count++;
2112 	}
2113 
2114 	return status;
2115 }
2116 
2117 static int dspio_read(struct hda_codec *codec, unsigned int *data)
2118 {
2119 	int status;
2120 
2121 	status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
2122 	if (status == -EIO)
2123 		return status;
2124 
2125 	status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
2126 	if (status == -EIO ||
2127 	    status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
2128 		return -EIO;
2129 
2130 	*data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
2131 				   VENDOR_DSPIO_SCP_READ_DATA, 0);
2132 
2133 	return 0;
2134 }
2135 
2136 static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
2137 			       unsigned int *buf_size, unsigned int size_count)
2138 {
2139 	int status = 0;
2140 	unsigned int size = *buf_size;
2141 	unsigned int count;
2142 	unsigned int skip_count;
2143 	unsigned int dummy;
2144 
2145 	if (buffer == NULL)
2146 		return -1;
2147 
2148 	count = 0;
2149 	while (count < size && count < size_count) {
2150 		status = dspio_read(codec, buffer++);
2151 		if (status != 0)
2152 			break;
2153 		count++;
2154 	}
2155 
2156 	skip_count = count;
2157 	if (status == 0) {
2158 		while (skip_count < size) {
2159 			status = dspio_read(codec, &dummy);
2160 			if (status != 0)
2161 				break;
2162 			skip_count++;
2163 		}
2164 	}
2165 	*buf_size = count;
2166 
2167 	return status;
2168 }
2169 
2170 /*
2171  * Construct the SCP header using corresponding fields
2172  */
2173 static inline unsigned int
2174 make_scp_header(unsigned int target_id, unsigned int source_id,
2175 		unsigned int get_flag, unsigned int req,
2176 		unsigned int device_flag, unsigned int resp_flag,
2177 		unsigned int error_flag, unsigned int data_size)
2178 {
2179 	unsigned int header = 0;
2180 
2181 	header = (data_size & 0x1f) << 27;
2182 	header |= (error_flag & 0x01) << 26;
2183 	header |= (resp_flag & 0x01) << 25;
2184 	header |= (device_flag & 0x01) << 24;
2185 	header |= (req & 0x7f) << 17;
2186 	header |= (get_flag & 0x01) << 16;
2187 	header |= (source_id & 0xff) << 8;
2188 	header |= target_id & 0xff;
2189 
2190 	return header;
2191 }
2192 
2193 /*
2194  * Extract corresponding fields from SCP header
2195  */
2196 static inline void
2197 extract_scp_header(unsigned int header,
2198 		   unsigned int *target_id, unsigned int *source_id,
2199 		   unsigned int *get_flag, unsigned int *req,
2200 		   unsigned int *device_flag, unsigned int *resp_flag,
2201 		   unsigned int *error_flag, unsigned int *data_size)
2202 {
2203 	if (data_size)
2204 		*data_size = (header >> 27) & 0x1f;
2205 	if (error_flag)
2206 		*error_flag = (header >> 26) & 0x01;
2207 	if (resp_flag)
2208 		*resp_flag = (header >> 25) & 0x01;
2209 	if (device_flag)
2210 		*device_flag = (header >> 24) & 0x01;
2211 	if (req)
2212 		*req = (header >> 17) & 0x7f;
2213 	if (get_flag)
2214 		*get_flag = (header >> 16) & 0x01;
2215 	if (source_id)
2216 		*source_id = (header >> 8) & 0xff;
2217 	if (target_id)
2218 		*target_id = header & 0xff;
2219 }
2220 
2221 #define SCP_MAX_DATA_WORDS  (16)
2222 
2223 /* Structure to contain any SCP message */
2224 struct scp_msg {
2225 	unsigned int hdr;
2226 	unsigned int data[SCP_MAX_DATA_WORDS];
2227 };
2228 
2229 static void dspio_clear_response_queue(struct hda_codec *codec)
2230 {
2231 	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
2232 	unsigned int dummy = 0;
2233 	int status;
2234 
2235 	/* clear all from the response queue */
2236 	do {
2237 		status = dspio_read(codec, &dummy);
2238 	} while (status == 0 && time_before(jiffies, timeout));
2239 }
2240 
2241 static int dspio_get_response_data(struct hda_codec *codec)
2242 {
2243 	struct ca0132_spec *spec = codec->spec;
2244 	unsigned int data = 0;
2245 	unsigned int count;
2246 
2247 	if (dspio_read(codec, &data) < 0)
2248 		return -EIO;
2249 
2250 	if ((data & 0x00ffffff) == spec->wait_scp_header) {
2251 		spec->scp_resp_header = data;
2252 		spec->scp_resp_count = data >> 27;
2253 		count = spec->wait_num_data;
2254 		dspio_read_multiple(codec, spec->scp_resp_data,
2255 				    &spec->scp_resp_count, count);
2256 		return 0;
2257 	}
2258 
2259 	return -EIO;
2260 }
2261 
2262 /*
2263  * Send SCP message to DSP
2264  */
2265 static int dspio_send_scp_message(struct hda_codec *codec,
2266 				  unsigned char *send_buf,
2267 				  unsigned int send_buf_size,
2268 				  unsigned char *return_buf,
2269 				  unsigned int return_buf_size,
2270 				  unsigned int *bytes_returned)
2271 {
2272 	struct ca0132_spec *spec = codec->spec;
2273 	int status;
2274 	unsigned int scp_send_size = 0;
2275 	unsigned int total_size;
2276 	bool waiting_for_resp = false;
2277 	unsigned int header;
2278 	struct scp_msg *ret_msg;
2279 	unsigned int resp_src_id, resp_target_id;
2280 	unsigned int data_size, src_id, target_id, get_flag, device_flag;
2281 
2282 	if (bytes_returned)
2283 		*bytes_returned = 0;
2284 
2285 	/* get scp header from buffer */
2286 	header = *((unsigned int *)send_buf);
2287 	extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
2288 			   &device_flag, NULL, NULL, &data_size);
2289 	scp_send_size = data_size + 1;
2290 	total_size = (scp_send_size * 4);
2291 
2292 	if (send_buf_size < total_size)
2293 		return -EINVAL;
2294 
2295 	if (get_flag || device_flag) {
2296 		if (!return_buf || return_buf_size < 4 || !bytes_returned)
2297 			return -EINVAL;
2298 
2299 		spec->wait_scp_header = *((unsigned int *)send_buf);
2300 
2301 		/* swap source id with target id */
2302 		resp_target_id = src_id;
2303 		resp_src_id = target_id;
2304 		spec->wait_scp_header &= 0xffff0000;
2305 		spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
2306 		spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
2307 		spec->wait_scp = 1;
2308 		waiting_for_resp = true;
2309 	}
2310 
2311 	status = dspio_write_multiple(codec, (unsigned int *)send_buf,
2312 				      scp_send_size);
2313 	if (status < 0) {
2314 		spec->wait_scp = 0;
2315 		return status;
2316 	}
2317 
2318 	if (waiting_for_resp) {
2319 		unsigned long timeout = jiffies + msecs_to_jiffies(1000);
2320 		memset(return_buf, 0, return_buf_size);
2321 		do {
2322 			msleep(20);
2323 		} while (spec->wait_scp && time_before(jiffies, timeout));
2324 		waiting_for_resp = false;
2325 		if (!spec->wait_scp) {
2326 			ret_msg = (struct scp_msg *)return_buf;
2327 			memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
2328 			memcpy(&ret_msg->data, spec->scp_resp_data,
2329 			       spec->wait_num_data);
2330 			*bytes_returned = (spec->scp_resp_count + 1) * 4;
2331 			status = 0;
2332 		} else {
2333 			status = -EIO;
2334 		}
2335 		spec->wait_scp = 0;
2336 	}
2337 
2338 	return status;
2339 }
2340 
2341 /**
2342  * dspio_scp - Prepare and send the SCP message to DSP
2343  * @codec: the HDA codec
2344  * @mod_id: ID of the DSP module to send the command
2345  * @src_id: ID of the source
2346  * @req: ID of request to send to the DSP module
2347  * @dir: SET or GET
2348  * @data: pointer to the data to send with the request, request specific
2349  * @len: length of the data, in bytes
2350  * @reply: point to the buffer to hold data returned for a reply
2351  * @reply_len: length of the reply buffer returned from GET
2352  *
2353  * Returns zero or a negative error code.
2354  */
2355 static int dspio_scp(struct hda_codec *codec,
2356 		int mod_id, int src_id, int req, int dir, const void *data,
2357 		unsigned int len, void *reply, unsigned int *reply_len)
2358 {
2359 	int status = 0;
2360 	struct scp_msg scp_send, scp_reply;
2361 	unsigned int ret_bytes, send_size, ret_size;
2362 	unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
2363 	unsigned int reply_data_size;
2364 
2365 	memset(&scp_send, 0, sizeof(scp_send));
2366 	memset(&scp_reply, 0, sizeof(scp_reply));
2367 
2368 	if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
2369 		return -EINVAL;
2370 
2371 	if (dir == SCP_GET && reply == NULL) {
2372 		codec_dbg(codec, "dspio_scp get but has no buffer\n");
2373 		return -EINVAL;
2374 	}
2375 
2376 	if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
2377 		codec_dbg(codec, "dspio_scp bad resp buf len parms\n");
2378 		return -EINVAL;
2379 	}
2380 
2381 	scp_send.hdr = make_scp_header(mod_id, src_id, (dir == SCP_GET), req,
2382 				       0, 0, 0, len/sizeof(unsigned int));
2383 	if (data != NULL && len > 0) {
2384 		len = min((unsigned int)(sizeof(scp_send.data)), len);
2385 		memcpy(scp_send.data, data, len);
2386 	}
2387 
2388 	ret_bytes = 0;
2389 	send_size = sizeof(unsigned int) + len;
2390 	status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
2391 					send_size, (unsigned char *)&scp_reply,
2392 					sizeof(scp_reply), &ret_bytes);
2393 
2394 	if (status < 0) {
2395 		codec_dbg(codec, "dspio_scp: send scp msg failed\n");
2396 		return status;
2397 	}
2398 
2399 	/* extract send and reply headers members */
2400 	extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
2401 			   NULL, NULL, NULL, NULL, NULL);
2402 	extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
2403 			   &reply_resp_flag, &reply_error_flag,
2404 			   &reply_data_size);
2405 
2406 	if (!send_get_flag)
2407 		return 0;
2408 
2409 	if (reply_resp_flag && !reply_error_flag) {
2410 		ret_size = (ret_bytes - sizeof(scp_reply.hdr))
2411 					/ sizeof(unsigned int);
2412 
2413 		if (*reply_len < ret_size*sizeof(unsigned int)) {
2414 			codec_dbg(codec, "reply too long for buf\n");
2415 			return -EINVAL;
2416 		} else if (ret_size != reply_data_size) {
2417 			codec_dbg(codec, "RetLen and HdrLen .NE.\n");
2418 			return -EINVAL;
2419 		} else if (!reply) {
2420 			codec_dbg(codec, "NULL reply\n");
2421 			return -EINVAL;
2422 		} else {
2423 			*reply_len = ret_size*sizeof(unsigned int);
2424 			memcpy(reply, scp_reply.data, *reply_len);
2425 		}
2426 	} else {
2427 		codec_dbg(codec, "reply ill-formed or errflag set\n");
2428 		return -EIO;
2429 	}
2430 
2431 	return status;
2432 }
2433 
2434 /*
2435  * Set DSP parameters
2436  */
2437 static int dspio_set_param(struct hda_codec *codec, int mod_id,
2438 			int src_id, int req, const void *data, unsigned int len)
2439 {
2440 	return dspio_scp(codec, mod_id, src_id, req, SCP_SET, data, len, NULL,
2441 			NULL);
2442 }
2443 
2444 static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
2445 			int req, const unsigned int data)
2446 {
2447 	return dspio_set_param(codec, mod_id, 0x20, req, &data,
2448 			sizeof(unsigned int));
2449 }
2450 
2451 /*
2452  * Allocate a DSP DMA channel via an SCP message
2453  */
2454 static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
2455 {
2456 	int status = 0;
2457 	unsigned int size = sizeof(dma_chan);
2458 
2459 	codec_dbg(codec, "     dspio_alloc_dma_chan() -- begin\n");
2460 	status = dspio_scp(codec, MASTERCONTROL, 0x20,
2461 			MASTERCONTROL_ALLOC_DMA_CHAN, SCP_GET, NULL, 0,
2462 			dma_chan, &size);
2463 
2464 	if (status < 0) {
2465 		codec_dbg(codec, "dspio_alloc_dma_chan: SCP Failed\n");
2466 		return status;
2467 	}
2468 
2469 	if ((*dma_chan + 1) == 0) {
2470 		codec_dbg(codec, "no free dma channels to allocate\n");
2471 		return -EBUSY;
2472 	}
2473 
2474 	codec_dbg(codec, "dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
2475 	codec_dbg(codec, "     dspio_alloc_dma_chan() -- complete\n");
2476 
2477 	return status;
2478 }
2479 
2480 /*
2481  * Free a DSP DMA via an SCP message
2482  */
2483 static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
2484 {
2485 	int status = 0;
2486 	unsigned int dummy = 0;
2487 
2488 	codec_dbg(codec, "     dspio_free_dma_chan() -- begin\n");
2489 	codec_dbg(codec, "dspio_free_dma_chan: chan=%d\n", dma_chan);
2490 
2491 	status = dspio_scp(codec, MASTERCONTROL, 0x20,
2492 			MASTERCONTROL_ALLOC_DMA_CHAN, SCP_SET, &dma_chan,
2493 			sizeof(dma_chan), NULL, &dummy);
2494 
2495 	if (status < 0) {
2496 		codec_dbg(codec, "dspio_free_dma_chan: SCP Failed\n");
2497 		return status;
2498 	}
2499 
2500 	codec_dbg(codec, "     dspio_free_dma_chan() -- complete\n");
2501 
2502 	return status;
2503 }
2504 
2505 /*
2506  * (Re)start the DSP
2507  */
2508 static int dsp_set_run_state(struct hda_codec *codec)
2509 {
2510 	unsigned int dbg_ctrl_reg;
2511 	unsigned int halt_state;
2512 	int err;
2513 
2514 	err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
2515 	if (err < 0)
2516 		return err;
2517 
2518 	halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
2519 		      DSP_DBGCNTL_STATE_LOBIT;
2520 
2521 	if (halt_state != 0) {
2522 		dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
2523 				  DSP_DBGCNTL_SS_MASK);
2524 		err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
2525 				   dbg_ctrl_reg);
2526 		if (err < 0)
2527 			return err;
2528 
2529 		dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
2530 				DSP_DBGCNTL_EXEC_MASK;
2531 		err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
2532 				   dbg_ctrl_reg);
2533 		if (err < 0)
2534 			return err;
2535 	}
2536 
2537 	return 0;
2538 }
2539 
2540 /*
2541  * Reset the DSP
2542  */
2543 static int dsp_reset(struct hda_codec *codec)
2544 {
2545 	unsigned int res;
2546 	int retry = 20;
2547 
2548 	codec_dbg(codec, "dsp_reset\n");
2549 	do {
2550 		res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
2551 		retry--;
2552 	} while (res == -EIO && retry);
2553 
2554 	if (!retry) {
2555 		codec_dbg(codec, "dsp_reset timeout\n");
2556 		return -EIO;
2557 	}
2558 
2559 	return 0;
2560 }
2561 
2562 /*
2563  * Convert chip address to DSP address
2564  */
2565 static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
2566 					bool *code, bool *yram)
2567 {
2568 	*code = *yram = false;
2569 
2570 	if (UC_RANGE(chip_addx, 1)) {
2571 		*code = true;
2572 		return UC_OFF(chip_addx);
2573 	} else if (X_RANGE_ALL(chip_addx, 1)) {
2574 		return X_OFF(chip_addx);
2575 	} else if (Y_RANGE_ALL(chip_addx, 1)) {
2576 		*yram = true;
2577 		return Y_OFF(chip_addx);
2578 	}
2579 
2580 	return INVALID_CHIP_ADDRESS;
2581 }
2582 
2583 /*
2584  * Check if the DSP DMA is active
2585  */
2586 static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
2587 {
2588 	unsigned int dma_chnlstart_reg;
2589 
2590 	chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
2591 
2592 	return ((dma_chnlstart_reg & (1 <<
2593 			(DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
2594 }
2595 
2596 static int dsp_dma_setup_common(struct hda_codec *codec,
2597 				unsigned int chip_addx,
2598 				unsigned int dma_chan,
2599 				unsigned int port_map_mask,
2600 				bool ovly)
2601 {
2602 	int status = 0;
2603 	unsigned int chnl_prop;
2604 	unsigned int dsp_addx;
2605 	unsigned int active;
2606 	bool code, yram;
2607 
2608 	codec_dbg(codec, "-- dsp_dma_setup_common() -- Begin ---------\n");
2609 
2610 	if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
2611 		codec_dbg(codec, "dma chan num invalid\n");
2612 		return -EINVAL;
2613 	}
2614 
2615 	if (dsp_is_dma_active(codec, dma_chan)) {
2616 		codec_dbg(codec, "dma already active\n");
2617 		return -EBUSY;
2618 	}
2619 
2620 	dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2621 
2622 	if (dsp_addx == INVALID_CHIP_ADDRESS) {
2623 		codec_dbg(codec, "invalid chip addr\n");
2624 		return -ENXIO;
2625 	}
2626 
2627 	chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
2628 	active = 0;
2629 
2630 	codec_dbg(codec, "   dsp_dma_setup_common()    start reg pgm\n");
2631 
2632 	if (ovly) {
2633 		status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
2634 				     &chnl_prop);
2635 
2636 		if (status < 0) {
2637 			codec_dbg(codec, "read CHNLPROP Reg fail\n");
2638 			return status;
2639 		}
2640 		codec_dbg(codec, "dsp_dma_setup_common() Read CHNLPROP\n");
2641 	}
2642 
2643 	if (!code)
2644 		chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2645 	else
2646 		chnl_prop |=  (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
2647 
2648 	chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
2649 
2650 	status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
2651 	if (status < 0) {
2652 		codec_dbg(codec, "write CHNLPROP Reg fail\n");
2653 		return status;
2654 	}
2655 	codec_dbg(codec, "   dsp_dma_setup_common()    Write CHNLPROP\n");
2656 
2657 	if (ovly) {
2658 		status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
2659 				     &active);
2660 
2661 		if (status < 0) {
2662 			codec_dbg(codec, "read ACTIVE Reg fail\n");
2663 			return status;
2664 		}
2665 		codec_dbg(codec, "dsp_dma_setup_common() Read ACTIVE\n");
2666 	}
2667 
2668 	active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
2669 		DSPDMAC_ACTIVE_AAR_MASK;
2670 
2671 	status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
2672 	if (status < 0) {
2673 		codec_dbg(codec, "write ACTIVE Reg fail\n");
2674 		return status;
2675 	}
2676 
2677 	codec_dbg(codec, "   dsp_dma_setup_common()    Write ACTIVE\n");
2678 
2679 	status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
2680 			      port_map_mask);
2681 	if (status < 0) {
2682 		codec_dbg(codec, "write AUDCHSEL Reg fail\n");
2683 		return status;
2684 	}
2685 	codec_dbg(codec, "   dsp_dma_setup_common()    Write AUDCHSEL\n");
2686 
2687 	status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
2688 			DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
2689 	if (status < 0) {
2690 		codec_dbg(codec, "write IRQCNT Reg fail\n");
2691 		return status;
2692 	}
2693 	codec_dbg(codec, "   dsp_dma_setup_common()    Write IRQCNT\n");
2694 
2695 	codec_dbg(codec,
2696 		   "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
2697 		   "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
2698 		   chip_addx, dsp_addx, dma_chan,
2699 		   port_map_mask, chnl_prop, active);
2700 
2701 	codec_dbg(codec, "-- dsp_dma_setup_common() -- Complete ------\n");
2702 
2703 	return 0;
2704 }
2705 
2706 /*
2707  * Setup the DSP DMA per-transfer-specific registers
2708  */
2709 static int dsp_dma_setup(struct hda_codec *codec,
2710 			unsigned int chip_addx,
2711 			unsigned int count,
2712 			unsigned int dma_chan)
2713 {
2714 	int status = 0;
2715 	bool code, yram;
2716 	unsigned int dsp_addx;
2717 	unsigned int addr_field;
2718 	unsigned int incr_field;
2719 	unsigned int base_cnt;
2720 	unsigned int cur_cnt;
2721 	unsigned int dma_cfg = 0;
2722 	unsigned int adr_ofs = 0;
2723 	unsigned int xfr_cnt = 0;
2724 	const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
2725 						DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
2726 
2727 	codec_dbg(codec, "-- dsp_dma_setup() -- Begin ---------\n");
2728 
2729 	if (count > max_dma_count) {
2730 		codec_dbg(codec, "count too big\n");
2731 		return -EINVAL;
2732 	}
2733 
2734 	dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
2735 	if (dsp_addx == INVALID_CHIP_ADDRESS) {
2736 		codec_dbg(codec, "invalid chip addr\n");
2737 		return -ENXIO;
2738 	}
2739 
2740 	codec_dbg(codec, "   dsp_dma_setup()    start reg pgm\n");
2741 
2742 	addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
2743 	incr_field   = 0;
2744 
2745 	if (!code) {
2746 		addr_field <<= 1;
2747 		if (yram)
2748 			addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
2749 
2750 		incr_field  = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
2751 	}
2752 
2753 	dma_cfg = addr_field + incr_field;
2754 	status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
2755 				dma_cfg);
2756 	if (status < 0) {
2757 		codec_dbg(codec, "write DMACFG Reg fail\n");
2758 		return status;
2759 	}
2760 	codec_dbg(codec, "   dsp_dma_setup()    Write DMACFG\n");
2761 
2762 	adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
2763 							(code ? 0 : 1));
2764 
2765 	status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
2766 				adr_ofs);
2767 	if (status < 0) {
2768 		codec_dbg(codec, "write DSPADROFS Reg fail\n");
2769 		return status;
2770 	}
2771 	codec_dbg(codec, "   dsp_dma_setup()    Write DSPADROFS\n");
2772 
2773 	base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
2774 
2775 	cur_cnt  = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
2776 
2777 	xfr_cnt = base_cnt | cur_cnt;
2778 
2779 	status = chipio_write(codec,
2780 				DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
2781 	if (status < 0) {
2782 		codec_dbg(codec, "write XFRCNT Reg fail\n");
2783 		return status;
2784 	}
2785 	codec_dbg(codec, "   dsp_dma_setup()    Write XFRCNT\n");
2786 
2787 	codec_dbg(codec,
2788 		   "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
2789 		   "ADROFS=0x%x, XFRCNT=0x%x\n",
2790 		   chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
2791 
2792 	codec_dbg(codec, "-- dsp_dma_setup() -- Complete ---------\n");
2793 
2794 	return 0;
2795 }
2796 
2797 /*
2798  * Start the DSP DMA
2799  */
2800 static int dsp_dma_start(struct hda_codec *codec,
2801 			 unsigned int dma_chan, bool ovly)
2802 {
2803 	unsigned int reg = 0;
2804 	int status = 0;
2805 
2806 	codec_dbg(codec, "-- dsp_dma_start() -- Begin ---------\n");
2807 
2808 	if (ovly) {
2809 		status = chipio_read(codec,
2810 				     DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
2811 
2812 		if (status < 0) {
2813 			codec_dbg(codec, "read CHNLSTART reg fail\n");
2814 			return status;
2815 		}
2816 		codec_dbg(codec, "-- dsp_dma_start()    Read CHNLSTART\n");
2817 
2818 		reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2819 				DSPDMAC_CHNLSTART_DIS_MASK);
2820 	}
2821 
2822 	status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2823 			reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
2824 	if (status < 0) {
2825 		codec_dbg(codec, "write CHNLSTART reg fail\n");
2826 		return status;
2827 	}
2828 	codec_dbg(codec, "-- dsp_dma_start() -- Complete ---------\n");
2829 
2830 	return status;
2831 }
2832 
2833 /*
2834  * Stop the DSP DMA
2835  */
2836 static int dsp_dma_stop(struct hda_codec *codec,
2837 			unsigned int dma_chan, bool ovly)
2838 {
2839 	unsigned int reg = 0;
2840 	int status = 0;
2841 
2842 	codec_dbg(codec, "-- dsp_dma_stop() -- Begin ---------\n");
2843 
2844 	if (ovly) {
2845 		status = chipio_read(codec,
2846 				     DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
2847 
2848 		if (status < 0) {
2849 			codec_dbg(codec, "read CHNLSTART reg fail\n");
2850 			return status;
2851 		}
2852 		codec_dbg(codec, "-- dsp_dma_stop()    Read CHNLSTART\n");
2853 		reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
2854 				DSPDMAC_CHNLSTART_DIS_MASK);
2855 	}
2856 
2857 	status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
2858 			reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
2859 	if (status < 0) {
2860 		codec_dbg(codec, "write CHNLSTART reg fail\n");
2861 		return status;
2862 	}
2863 	codec_dbg(codec, "-- dsp_dma_stop() -- Complete ---------\n");
2864 
2865 	return status;
2866 }
2867 
2868 /**
2869  * dsp_allocate_router_ports - Allocate router ports
2870  *
2871  * @codec: the HDA codec
2872  * @num_chans: number of channels in the stream
2873  * @ports_per_channel: number of ports per channel
2874  * @start_device: start device
2875  * @port_map: pointer to the port list to hold the allocated ports
2876  *
2877  * Returns zero or a negative error code.
2878  */
2879 static int dsp_allocate_router_ports(struct hda_codec *codec,
2880 				     unsigned int num_chans,
2881 				     unsigned int ports_per_channel,
2882 				     unsigned int start_device,
2883 				     unsigned int *port_map)
2884 {
2885 	int status = 0;
2886 	int res;
2887 	u8 val;
2888 
2889 	status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2890 	if (status < 0)
2891 		return status;
2892 
2893 	val = start_device << 6;
2894 	val |= (ports_per_channel - 1) << 4;
2895 	val |= num_chans - 1;
2896 
2897 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2898 			    VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
2899 			    val);
2900 
2901 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2902 			    VENDOR_CHIPIO_PORT_ALLOC_SET,
2903 			    MEM_CONNID_DSP);
2904 
2905 	status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2906 	if (status < 0)
2907 		return status;
2908 
2909 	res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
2910 				VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
2911 
2912 	*port_map = res;
2913 
2914 	return (res < 0) ? res : 0;
2915 }
2916 
2917 /*
2918  * Free router ports
2919  */
2920 static int dsp_free_router_ports(struct hda_codec *codec)
2921 {
2922 	int status = 0;
2923 
2924 	status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2925 	if (status < 0)
2926 		return status;
2927 
2928 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
2929 			    VENDOR_CHIPIO_PORT_FREE_SET,
2930 			    MEM_CONNID_DSP);
2931 
2932 	status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
2933 
2934 	return status;
2935 }
2936 
2937 /*
2938  * Allocate DSP ports for the download stream
2939  */
2940 static int dsp_allocate_ports(struct hda_codec *codec,
2941 			unsigned int num_chans,
2942 			unsigned int rate_multi, unsigned int *port_map)
2943 {
2944 	int status;
2945 
2946 	codec_dbg(codec, "     dsp_allocate_ports() -- begin\n");
2947 
2948 	if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
2949 		codec_dbg(codec, "bad rate multiple\n");
2950 		return -EINVAL;
2951 	}
2952 
2953 	status = dsp_allocate_router_ports(codec, num_chans,
2954 					   rate_multi, 0, port_map);
2955 
2956 	codec_dbg(codec, "     dsp_allocate_ports() -- complete\n");
2957 
2958 	return status;
2959 }
2960 
2961 static int dsp_allocate_ports_format(struct hda_codec *codec,
2962 			const unsigned short fmt,
2963 			unsigned int *port_map)
2964 {
2965 	unsigned int num_chans;
2966 
2967 	unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
2968 	unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
2969 	unsigned int rate_multi = sample_rate_mul / sample_rate_div;
2970 
2971 	if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
2972 		codec_dbg(codec, "bad rate multiple\n");
2973 		return -EINVAL;
2974 	}
2975 
2976 	num_chans = get_hdafmt_chs(fmt) + 1;
2977 
2978 	return dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
2979 }
2980 
2981 /*
2982  * free DSP ports
2983  */
2984 static int dsp_free_ports(struct hda_codec *codec)
2985 {
2986 	int status;
2987 
2988 	codec_dbg(codec, "     dsp_free_ports() -- begin\n");
2989 
2990 	status = dsp_free_router_ports(codec);
2991 	if (status < 0) {
2992 		codec_dbg(codec, "free router ports fail\n");
2993 		return status;
2994 	}
2995 	codec_dbg(codec, "     dsp_free_ports() -- complete\n");
2996 
2997 	return status;
2998 }
2999 
3000 /*
3001  *  HDA DMA engine stuffs for DSP code download
3002  */
3003 struct dma_engine {
3004 	struct hda_codec *codec;
3005 	unsigned short m_converter_format;
3006 	struct snd_dma_buffer *dmab;
3007 	unsigned int buf_size;
3008 };
3009 
3010 
3011 enum dma_state {
3012 	DMA_STATE_STOP  = 0,
3013 	DMA_STATE_RUN   = 1
3014 };
3015 
3016 static int dma_convert_to_hda_format(struct hda_codec *codec,
3017 		unsigned int sample_rate,
3018 		unsigned short channels,
3019 		unsigned short *hda_format)
3020 {
3021 	unsigned int format_val;
3022 
3023 	format_val = snd_hdac_calc_stream_format(sample_rate,
3024 				channels, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
3025 
3026 	if (hda_format)
3027 		*hda_format = (unsigned short)format_val;
3028 
3029 	return 0;
3030 }
3031 
3032 /*
3033  *  Reset DMA for DSP download
3034  */
3035 static int dma_reset(struct dma_engine *dma)
3036 {
3037 	struct hda_codec *codec = dma->codec;
3038 	struct ca0132_spec *spec = codec->spec;
3039 	int status;
3040 
3041 	if (dma->dmab->area)
3042 		snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
3043 
3044 	status = snd_hda_codec_load_dsp_prepare(codec,
3045 			dma->m_converter_format,
3046 			dma->buf_size,
3047 			dma->dmab);
3048 	if (status < 0)
3049 		return status;
3050 	spec->dsp_stream_id = status;
3051 	return 0;
3052 }
3053 
3054 static int dma_set_state(struct dma_engine *dma, enum dma_state state)
3055 {
3056 	bool cmd;
3057 
3058 	switch (state) {
3059 	case DMA_STATE_STOP:
3060 		cmd = false;
3061 		break;
3062 	case DMA_STATE_RUN:
3063 		cmd = true;
3064 		break;
3065 	default:
3066 		return 0;
3067 	}
3068 
3069 	snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
3070 	return 0;
3071 }
3072 
3073 static unsigned int dma_get_buffer_size(struct dma_engine *dma)
3074 {
3075 	return dma->dmab->bytes;
3076 }
3077 
3078 static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
3079 {
3080 	return dma->dmab->area;
3081 }
3082 
3083 static int dma_xfer(struct dma_engine *dma,
3084 		const unsigned int *data,
3085 		unsigned int count)
3086 {
3087 	memcpy(dma->dmab->area, data, count);
3088 	return 0;
3089 }
3090 
3091 static void dma_get_converter_format(
3092 		struct dma_engine *dma,
3093 		unsigned short *format)
3094 {
3095 	if (format)
3096 		*format = dma->m_converter_format;
3097 }
3098 
3099 static unsigned int dma_get_stream_id(struct dma_engine *dma)
3100 {
3101 	struct ca0132_spec *spec = dma->codec->spec;
3102 
3103 	return spec->dsp_stream_id;
3104 }
3105 
3106 struct dsp_image_seg {
3107 	u32 magic;
3108 	u32 chip_addr;
3109 	u32 count;
3110 	u32 data[];
3111 };
3112 
3113 static const u32 g_magic_value = 0x4c46584d;
3114 static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
3115 
3116 static bool is_valid(const struct dsp_image_seg *p)
3117 {
3118 	return p->magic == g_magic_value;
3119 }
3120 
3121 static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
3122 {
3123 	return g_chip_addr_magic_value == p->chip_addr;
3124 }
3125 
3126 static bool is_last(const struct dsp_image_seg *p)
3127 {
3128 	return p->count == 0;
3129 }
3130 
3131 static size_t dsp_sizeof(const struct dsp_image_seg *p)
3132 {
3133 	return struct_size(p, data, p->count);
3134 }
3135 
3136 static const struct dsp_image_seg *get_next_seg_ptr(
3137 				const struct dsp_image_seg *p)
3138 {
3139 	return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
3140 }
3141 
3142 /*
3143  * CA0132 chip DSP transfer stuffs.  For DSP download.
3144  */
3145 #define INVALID_DMA_CHANNEL (~0U)
3146 
3147 /*
3148  * Program a list of address/data pairs via the ChipIO widget.
3149  * The segment data is in the format of successive pairs of words.
3150  * These are repeated as indicated by the segment's count field.
3151  */
3152 static int dspxfr_hci_write(struct hda_codec *codec,
3153 			const struct dsp_image_seg *fls)
3154 {
3155 	int status;
3156 	const u32 *data;
3157 	unsigned int count;
3158 
3159 	if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
3160 		codec_dbg(codec, "hci_write invalid params\n");
3161 		return -EINVAL;
3162 	}
3163 
3164 	count = fls->count;
3165 	data = (u32 *)(fls->data);
3166 	while (count >= 2) {
3167 		status = chipio_write(codec, data[0], data[1]);
3168 		if (status < 0) {
3169 			codec_dbg(codec, "hci_write chipio failed\n");
3170 			return status;
3171 		}
3172 		count -= 2;
3173 		data  += 2;
3174 	}
3175 	return 0;
3176 }
3177 
3178 /**
3179  * dspxfr_one_seg - Write a block of data into DSP code or data RAM using pre-allocated DMA engine.
3180  *
3181  * @codec: the HDA codec
3182  * @fls: pointer to a fast load image
3183  * @reloc: Relocation address for loading single-segment overlays, or 0 for
3184  *	   no relocation
3185  * @dma_engine: pointer to DMA engine to be used for DSP download
3186  * @dma_chan: The number of DMA channels used for DSP download
3187  * @port_map_mask: port mapping
3188  * @ovly: TRUE if overlay format is required
3189  *
3190  * Returns zero or a negative error code.
3191  */
3192 static int dspxfr_one_seg(struct hda_codec *codec,
3193 			const struct dsp_image_seg *fls,
3194 			unsigned int reloc,
3195 			struct dma_engine *dma_engine,
3196 			unsigned int dma_chan,
3197 			unsigned int port_map_mask,
3198 			bool ovly)
3199 {
3200 	int status = 0;
3201 	bool comm_dma_setup_done = false;
3202 	const unsigned int *data;
3203 	unsigned int chip_addx;
3204 	unsigned int words_to_write;
3205 	unsigned int buffer_size_words;
3206 	unsigned char *buffer_addx;
3207 	unsigned short hda_format;
3208 	unsigned int sample_rate_div;
3209 	unsigned int sample_rate_mul;
3210 	unsigned int num_chans;
3211 	unsigned int hda_frame_size_words;
3212 	unsigned int remainder_words;
3213 	const u32 *data_remainder;
3214 	u32 chip_addx_remainder;
3215 	unsigned int run_size_words;
3216 	const struct dsp_image_seg *hci_write = NULL;
3217 	unsigned long timeout;
3218 	bool dma_active;
3219 
3220 	if (fls == NULL)
3221 		return -EINVAL;
3222 	if (is_hci_prog_list_seg(fls)) {
3223 		hci_write = fls;
3224 		fls = get_next_seg_ptr(fls);
3225 	}
3226 
3227 	if (hci_write && (!fls || is_last(fls))) {
3228 		codec_dbg(codec, "hci_write\n");
3229 		return dspxfr_hci_write(codec, hci_write);
3230 	}
3231 
3232 	if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
3233 		codec_dbg(codec, "Invalid Params\n");
3234 		return -EINVAL;
3235 	}
3236 
3237 	data = fls->data;
3238 	chip_addx = fls->chip_addr;
3239 	words_to_write = fls->count;
3240 
3241 	if (!words_to_write)
3242 		return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
3243 	if (reloc)
3244 		chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
3245 
3246 	if (!UC_RANGE(chip_addx, words_to_write) &&
3247 	    !X_RANGE_ALL(chip_addx, words_to_write) &&
3248 	    !Y_RANGE_ALL(chip_addx, words_to_write)) {
3249 		codec_dbg(codec, "Invalid chip_addx Params\n");
3250 		return -EINVAL;
3251 	}
3252 
3253 	buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
3254 					sizeof(u32);
3255 
3256 	buffer_addx = dma_get_buffer_addr(dma_engine);
3257 
3258 	if (buffer_addx == NULL) {
3259 		codec_dbg(codec, "dma_engine buffer NULL\n");
3260 		return -EINVAL;
3261 	}
3262 
3263 	dma_get_converter_format(dma_engine, &hda_format);
3264 	sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
3265 	sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
3266 	num_chans = get_hdafmt_chs(hda_format) + 1;
3267 
3268 	hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
3269 			(num_chans * sample_rate_mul / sample_rate_div));
3270 
3271 	if (hda_frame_size_words == 0) {
3272 		codec_dbg(codec, "frmsz zero\n");
3273 		return -EINVAL;
3274 	}
3275 
3276 	buffer_size_words = min(buffer_size_words,
3277 				(unsigned int)(UC_RANGE(chip_addx, 1) ?
3278 				65536 : 32768));
3279 	buffer_size_words -= buffer_size_words % hda_frame_size_words;
3280 	codec_dbg(codec,
3281 		   "chpadr=0x%08x frmsz=%u nchan=%u "
3282 		   "rate_mul=%u div=%u bufsz=%u\n",
3283 		   chip_addx, hda_frame_size_words, num_chans,
3284 		   sample_rate_mul, sample_rate_div, buffer_size_words);
3285 
3286 	if (buffer_size_words < hda_frame_size_words) {
3287 		codec_dbg(codec, "dspxfr_one_seg:failed\n");
3288 		return -EINVAL;
3289 	}
3290 
3291 	remainder_words = words_to_write % hda_frame_size_words;
3292 	data_remainder = data;
3293 	chip_addx_remainder = chip_addx;
3294 
3295 	data += remainder_words;
3296 	chip_addx += remainder_words*sizeof(u32);
3297 	words_to_write -= remainder_words;
3298 
3299 	while (words_to_write != 0) {
3300 		run_size_words = min(buffer_size_words, words_to_write);
3301 		codec_dbg(codec, "dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
3302 			    words_to_write, run_size_words, remainder_words);
3303 		dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
3304 		if (!comm_dma_setup_done) {
3305 			status = dsp_dma_stop(codec, dma_chan, ovly);
3306 			if (status < 0)
3307 				return status;
3308 			status = dsp_dma_setup_common(codec, chip_addx,
3309 						dma_chan, port_map_mask, ovly);
3310 			if (status < 0)
3311 				return status;
3312 			comm_dma_setup_done = true;
3313 		}
3314 
3315 		status = dsp_dma_setup(codec, chip_addx,
3316 						run_size_words, dma_chan);
3317 		if (status < 0)
3318 			return status;
3319 		status = dsp_dma_start(codec, dma_chan, ovly);
3320 		if (status < 0)
3321 			return status;
3322 		if (!dsp_is_dma_active(codec, dma_chan)) {
3323 			codec_dbg(codec, "dspxfr:DMA did not start\n");
3324 			return -EIO;
3325 		}
3326 		status = dma_set_state(dma_engine, DMA_STATE_RUN);
3327 		if (status < 0)
3328 			return status;
3329 		if (remainder_words != 0) {
3330 			status = chipio_write_multiple(codec,
3331 						chip_addx_remainder,
3332 						data_remainder,
3333 						remainder_words);
3334 			if (status < 0)
3335 				return status;
3336 			remainder_words = 0;
3337 		}
3338 		if (hci_write) {
3339 			status = dspxfr_hci_write(codec, hci_write);
3340 			if (status < 0)
3341 				return status;
3342 			hci_write = NULL;
3343 		}
3344 
3345 		timeout = jiffies + msecs_to_jiffies(2000);
3346 		do {
3347 			dma_active = dsp_is_dma_active(codec, dma_chan);
3348 			if (!dma_active)
3349 				break;
3350 			msleep(20);
3351 		} while (time_before(jiffies, timeout));
3352 		if (dma_active)
3353 			break;
3354 
3355 		codec_dbg(codec, "+++++ DMA complete\n");
3356 		dma_set_state(dma_engine, DMA_STATE_STOP);
3357 		status = dma_reset(dma_engine);
3358 
3359 		if (status < 0)
3360 			return status;
3361 
3362 		data += run_size_words;
3363 		chip_addx += run_size_words*sizeof(u32);
3364 		words_to_write -= run_size_words;
3365 	}
3366 
3367 	if (remainder_words != 0) {
3368 		status = chipio_write_multiple(codec, chip_addx_remainder,
3369 					data_remainder, remainder_words);
3370 	}
3371 
3372 	return status;
3373 }
3374 
3375 /**
3376  * dspxfr_image - Write the entire DSP image of a DSP code/data overlay to DSP memories
3377  *
3378  * @codec: the HDA codec
3379  * @fls_data: pointer to a fast load image
3380  * @reloc: Relocation address for loading single-segment overlays, or 0 for
3381  *	   no relocation
3382  * @sample_rate: sampling rate of the stream used for DSP download
3383  * @channels: channels of the stream used for DSP download
3384  * @ovly: TRUE if overlay format is required
3385  *
3386  * Returns zero or a negative error code.
3387  */
3388 static int dspxfr_image(struct hda_codec *codec,
3389 			const struct dsp_image_seg *fls_data,
3390 			unsigned int reloc,
3391 			unsigned int sample_rate,
3392 			unsigned short channels,
3393 			bool ovly)
3394 {
3395 	struct ca0132_spec *spec = codec->spec;
3396 	int status;
3397 	unsigned short hda_format = 0;
3398 	unsigned int response;
3399 	unsigned char stream_id = 0;
3400 	struct dma_engine *dma_engine;
3401 	unsigned int dma_chan;
3402 	unsigned int port_map_mask;
3403 
3404 	if (fls_data == NULL)
3405 		return -EINVAL;
3406 
3407 	dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
3408 	if (!dma_engine)
3409 		return -ENOMEM;
3410 
3411 	dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
3412 	if (!dma_engine->dmab) {
3413 		kfree(dma_engine);
3414 		return -ENOMEM;
3415 	}
3416 
3417 	dma_engine->codec = codec;
3418 	dma_convert_to_hda_format(codec, sample_rate, channels, &hda_format);
3419 	dma_engine->m_converter_format = hda_format;
3420 	dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
3421 			DSP_DMA_WRITE_BUFLEN_INIT) * 2;
3422 
3423 	dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
3424 
3425 	status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
3426 					hda_format, &response);
3427 
3428 	if (status < 0) {
3429 		codec_dbg(codec, "set converter format fail\n");
3430 		goto exit;
3431 	}
3432 
3433 	status = snd_hda_codec_load_dsp_prepare(codec,
3434 				dma_engine->m_converter_format,
3435 				dma_engine->buf_size,
3436 				dma_engine->dmab);
3437 	if (status < 0)
3438 		goto exit;
3439 	spec->dsp_stream_id = status;
3440 
3441 	if (ovly) {
3442 		status = dspio_alloc_dma_chan(codec, &dma_chan);
3443 		if (status < 0) {
3444 			codec_dbg(codec, "alloc dmachan fail\n");
3445 			dma_chan = INVALID_DMA_CHANNEL;
3446 			goto exit;
3447 		}
3448 	}
3449 
3450 	port_map_mask = 0;
3451 	status = dsp_allocate_ports_format(codec, hda_format,
3452 					&port_map_mask);
3453 	if (status < 0) {
3454 		codec_dbg(codec, "alloc ports fail\n");
3455 		goto exit;
3456 	}
3457 
3458 	stream_id = dma_get_stream_id(dma_engine);
3459 	status = codec_set_converter_stream_channel(codec,
3460 			WIDGET_CHIP_CTRL, stream_id, 0, &response);
3461 	if (status < 0) {
3462 		codec_dbg(codec, "set stream chan fail\n");
3463 		goto exit;
3464 	}
3465 
3466 	while ((fls_data != NULL) && !is_last(fls_data)) {
3467 		if (!is_valid(fls_data)) {
3468 			codec_dbg(codec, "FLS check fail\n");
3469 			status = -EINVAL;
3470 			goto exit;
3471 		}
3472 		status = dspxfr_one_seg(codec, fls_data, reloc,
3473 					dma_engine, dma_chan,
3474 					port_map_mask, ovly);
3475 		if (status < 0)
3476 			break;
3477 
3478 		if (is_hci_prog_list_seg(fls_data))
3479 			fls_data = get_next_seg_ptr(fls_data);
3480 
3481 		if ((fls_data != NULL) && !is_last(fls_data))
3482 			fls_data = get_next_seg_ptr(fls_data);
3483 	}
3484 
3485 	if (port_map_mask != 0)
3486 		status = dsp_free_ports(codec);
3487 
3488 	if (status < 0)
3489 		goto exit;
3490 
3491 	status = codec_set_converter_stream_channel(codec,
3492 				WIDGET_CHIP_CTRL, 0, 0, &response);
3493 
3494 exit:
3495 	if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
3496 		dspio_free_dma_chan(codec, dma_chan);
3497 
3498 	if (dma_engine->dmab->area)
3499 		snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
3500 	kfree(dma_engine->dmab);
3501 	kfree(dma_engine);
3502 
3503 	return status;
3504 }
3505 
3506 /*
3507  * CA0132 DSP download stuffs.
3508  */
3509 static void dspload_post_setup(struct hda_codec *codec)
3510 {
3511 	struct ca0132_spec *spec = codec->spec;
3512 	codec_dbg(codec, "---- dspload_post_setup ------\n");
3513 	if (!ca0132_use_alt_functions(spec)) {
3514 		/*set DSP speaker to 2.0 configuration*/
3515 		chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
3516 		chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
3517 
3518 		/*update write pointer*/
3519 		chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
3520 	}
3521 }
3522 
3523 /**
3524  * dspload_image - Download DSP from a DSP Image Fast Load structure.
3525  *
3526  * @codec: the HDA codec
3527  * @fls: pointer to a fast load image
3528  * @ovly: TRUE if overlay format is required
3529  * @reloc: Relocation address for loading single-segment overlays, or 0 for
3530  *	   no relocation
3531  * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
3532  * @router_chans: number of audio router channels to be allocated (0 means use
3533  *		  internal defaults; max is 32)
3534  *
3535  * Download DSP from a DSP Image Fast Load structure. This structure is a
3536  * linear, non-constant sized element array of structures, each of which
3537  * contain the count of the data to be loaded, the data itself, and the
3538  * corresponding starting chip address of the starting data location.
3539  * Returns zero or a negative error code.
3540  */
3541 static int dspload_image(struct hda_codec *codec,
3542 			const struct dsp_image_seg *fls,
3543 			bool ovly,
3544 			unsigned int reloc,
3545 			bool autostart,
3546 			int router_chans)
3547 {
3548 	int status = 0;
3549 	unsigned int sample_rate;
3550 	unsigned short channels;
3551 
3552 	codec_dbg(codec, "---- dspload_image begin ------\n");
3553 	if (router_chans == 0) {
3554 		if (!ovly)
3555 			router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
3556 		else
3557 			router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
3558 	}
3559 
3560 	sample_rate = 48000;
3561 	channels = (unsigned short)router_chans;
3562 
3563 	while (channels > 16) {
3564 		sample_rate *= 2;
3565 		channels /= 2;
3566 	}
3567 
3568 	do {
3569 		codec_dbg(codec, "Ready to program DMA\n");
3570 		if (!ovly)
3571 			status = dsp_reset(codec);
3572 
3573 		if (status < 0)
3574 			break;
3575 
3576 		codec_dbg(codec, "dsp_reset() complete\n");
3577 		status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
3578 				      ovly);
3579 
3580 		if (status < 0)
3581 			break;
3582 
3583 		codec_dbg(codec, "dspxfr_image() complete\n");
3584 		if (autostart && !ovly) {
3585 			dspload_post_setup(codec);
3586 			status = dsp_set_run_state(codec);
3587 		}
3588 
3589 		codec_dbg(codec, "LOAD FINISHED\n");
3590 	} while (0);
3591 
3592 	return status;
3593 }
3594 
3595 #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
3596 static bool dspload_is_loaded(struct hda_codec *codec)
3597 {
3598 	unsigned int data = 0;
3599 	int status = 0;
3600 
3601 	status = chipio_read(codec, 0x40004, &data);
3602 	if ((status < 0) || (data != 1))
3603 		return false;
3604 
3605 	return true;
3606 }
3607 #else
3608 #define dspload_is_loaded(codec)	false
3609 #endif
3610 
3611 static bool dspload_wait_loaded(struct hda_codec *codec)
3612 {
3613 	unsigned long timeout = jiffies + msecs_to_jiffies(2000);
3614 
3615 	do {
3616 		if (dspload_is_loaded(codec)) {
3617 			codec_info(codec, "ca0132 DSP downloaded and running\n");
3618 			return true;
3619 		}
3620 		msleep(20);
3621 	} while (time_before(jiffies, timeout));
3622 
3623 	codec_err(codec, "ca0132 failed to download DSP\n");
3624 	return false;
3625 }
3626 
3627 /*
3628  * ca0113 related functions. The ca0113 acts as the HDA bus for the pci-e
3629  * based cards, and has a second mmio region, region2, that's used for special
3630  * commands.
3631  */
3632 
3633 /*
3634  * For cards with PCI-E region2 (Sound Blaster Z/ZxR, Recon3D, and AE-5)
3635  * the mmio address 0x320 is used to set GPIO pins. The format for the data
3636  * The first eight bits are just the number of the pin. So far, I've only seen
3637  * this number go to 7.
3638  * AE-5 note: The AE-5 seems to use pins 2 and 3 to somehow set the color value
3639  * of the on-card LED. It seems to use pin 2 for data, then toggles 3 to on and
3640  * then off to send that bit.
3641  */
3642 static void ca0113_mmio_gpio_set(struct hda_codec *codec, unsigned int gpio_pin,
3643 		bool enable)
3644 {
3645 	struct ca0132_spec *spec = codec->spec;
3646 	unsigned short gpio_data;
3647 
3648 	gpio_data = gpio_pin & 0xF;
3649 	gpio_data |= ((enable << 8) & 0x100);
3650 
3651 	writew(gpio_data, spec->mem_base + 0x320);
3652 }
3653 
3654 /*
3655  * Special pci region2 commands that are only used by the AE-5. They follow
3656  * a set format, and require reads at certain points to seemingly 'clear'
3657  * the response data. My first tests didn't do these reads, and would cause
3658  * the card to get locked up until the memory was read. These commands
3659  * seem to work with three distinct values that I've taken to calling group,
3660  * target-id, and value.
3661  */
3662 static void ca0113_mmio_command_set(struct hda_codec *codec, unsigned int group,
3663 		unsigned int target, unsigned int value)
3664 {
3665 	struct ca0132_spec *spec = codec->spec;
3666 	unsigned int write_val;
3667 
3668 	writel(0x0000007e, spec->mem_base + 0x210);
3669 	readl(spec->mem_base + 0x210);
3670 	writel(0x0000005a, spec->mem_base + 0x210);
3671 	readl(spec->mem_base + 0x210);
3672 	readl(spec->mem_base + 0x210);
3673 
3674 	writel(0x00800005, spec->mem_base + 0x20c);
3675 	writel(group, spec->mem_base + 0x804);
3676 
3677 	writel(0x00800005, spec->mem_base + 0x20c);
3678 	write_val = (target & 0xff);
3679 	write_val |= (value << 8);
3680 
3681 
3682 	writel(write_val, spec->mem_base + 0x204);
3683 	/*
3684 	 * Need delay here or else it goes too fast and works inconsistently.
3685 	 */
3686 	msleep(20);
3687 
3688 	readl(spec->mem_base + 0x860);
3689 	readl(spec->mem_base + 0x854);
3690 	readl(spec->mem_base + 0x840);
3691 
3692 	writel(0x00800004, spec->mem_base + 0x20c);
3693 	writel(0x00000000, spec->mem_base + 0x210);
3694 	readl(spec->mem_base + 0x210);
3695 	readl(spec->mem_base + 0x210);
3696 }
3697 
3698 /*
3699  * This second type of command is used for setting the sound filter type.
3700  */
3701 static void ca0113_mmio_command_set_type2(struct hda_codec *codec,
3702 		unsigned int group, unsigned int target, unsigned int value)
3703 {
3704 	struct ca0132_spec *spec = codec->spec;
3705 	unsigned int write_val;
3706 
3707 	writel(0x0000007e, spec->mem_base + 0x210);
3708 	readl(spec->mem_base + 0x210);
3709 	writel(0x0000005a, spec->mem_base + 0x210);
3710 	readl(spec->mem_base + 0x210);
3711 	readl(spec->mem_base + 0x210);
3712 
3713 	writel(0x00800003, spec->mem_base + 0x20c);
3714 	writel(group, spec->mem_base + 0x804);
3715 
3716 	writel(0x00800005, spec->mem_base + 0x20c);
3717 	write_val = (target & 0xff);
3718 	write_val |= (value << 8);
3719 
3720 
3721 	writel(write_val, spec->mem_base + 0x204);
3722 	msleep(20);
3723 	readl(spec->mem_base + 0x860);
3724 	readl(spec->mem_base + 0x854);
3725 	readl(spec->mem_base + 0x840);
3726 
3727 	writel(0x00800004, spec->mem_base + 0x20c);
3728 	writel(0x00000000, spec->mem_base + 0x210);
3729 	readl(spec->mem_base + 0x210);
3730 	readl(spec->mem_base + 0x210);
3731 }
3732 
3733 /*
3734  * Setup GPIO for the other variants of Core3D.
3735  */
3736 
3737 /*
3738  * Sets up the GPIO pins so that they are discoverable. If this isn't done,
3739  * the card shows as having no GPIO pins.
3740  */
3741 static void ca0132_gpio_init(struct hda_codec *codec)
3742 {
3743 	struct ca0132_spec *spec = codec->spec;
3744 
3745 	switch (ca0132_quirk(spec)) {
3746 	case QUIRK_SBZ:
3747 	case QUIRK_AE5:
3748 	case QUIRK_AE7:
3749 		snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3750 		snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
3751 		snd_hda_codec_write(codec, 0x01, 0, 0x790, 0x23);
3752 		break;
3753 	case QUIRK_R3DI:
3754 		snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
3755 		snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5B);
3756 		break;
3757 	default:
3758 		break;
3759 	}
3760 
3761 }
3762 
3763 /* Sets the GPIO for audio output. */
3764 static void ca0132_gpio_setup(struct hda_codec *codec)
3765 {
3766 	struct ca0132_spec *spec = codec->spec;
3767 
3768 	switch (ca0132_quirk(spec)) {
3769 	case QUIRK_SBZ:
3770 		snd_hda_codec_write(codec, 0x01, 0,
3771 				AC_VERB_SET_GPIO_DIRECTION, 0x07);
3772 		snd_hda_codec_write(codec, 0x01, 0,
3773 				AC_VERB_SET_GPIO_MASK, 0x07);
3774 		snd_hda_codec_write(codec, 0x01, 0,
3775 				AC_VERB_SET_GPIO_DATA, 0x04);
3776 		snd_hda_codec_write(codec, 0x01, 0,
3777 				AC_VERB_SET_GPIO_DATA, 0x06);
3778 		break;
3779 	case QUIRK_R3DI:
3780 		snd_hda_codec_write(codec, 0x01, 0,
3781 				AC_VERB_SET_GPIO_DIRECTION, 0x1E);
3782 		snd_hda_codec_write(codec, 0x01, 0,
3783 				AC_VERB_SET_GPIO_MASK, 0x1F);
3784 		snd_hda_codec_write(codec, 0x01, 0,
3785 				AC_VERB_SET_GPIO_DATA, 0x0C);
3786 		break;
3787 	default:
3788 		break;
3789 	}
3790 }
3791 
3792 /*
3793  * GPIO control functions for the Recon3D integrated.
3794  */
3795 
3796 enum r3di_gpio_bit {
3797 	/* Bit 1 - Switch between front/rear mic. 0 = rear, 1 = front */
3798 	R3DI_MIC_SELECT_BIT = 1,
3799 	/* Bit 2 - Switch between headphone/line out. 0 = Headphone, 1 = Line */
3800 	R3DI_OUT_SELECT_BIT = 2,
3801 	/*
3802 	 * I dunno what this actually does, but it stays on until the dsp
3803 	 * is downloaded.
3804 	 */
3805 	R3DI_GPIO_DSP_DOWNLOADING = 3,
3806 	/*
3807 	 * Same as above, no clue what it does, but it comes on after the dsp
3808 	 * is downloaded.
3809 	 */
3810 	R3DI_GPIO_DSP_DOWNLOADED = 4
3811 };
3812 
3813 enum r3di_mic_select {
3814 	/* Set GPIO bit 1 to 0 for rear mic */
3815 	R3DI_REAR_MIC = 0,
3816 	/* Set GPIO bit 1 to 1 for front microphone*/
3817 	R3DI_FRONT_MIC = 1
3818 };
3819 
3820 enum r3di_out_select {
3821 	/* Set GPIO bit 2 to 0 for headphone */
3822 	R3DI_HEADPHONE_OUT = 0,
3823 	/* Set GPIO bit 2 to 1 for speaker */
3824 	R3DI_LINE_OUT = 1
3825 };
3826 enum r3di_dsp_status {
3827 	/* Set GPIO bit 3 to 1 until DSP is downloaded */
3828 	R3DI_DSP_DOWNLOADING = 0,
3829 	/* Set GPIO bit 4 to 1 once DSP is downloaded */
3830 	R3DI_DSP_DOWNLOADED = 1
3831 };
3832 
3833 
3834 static void r3di_gpio_mic_set(struct hda_codec *codec,
3835 		enum r3di_mic_select cur_mic)
3836 {
3837 	unsigned int cur_gpio;
3838 
3839 	/* Get the current GPIO Data setup */
3840 	cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3841 
3842 	switch (cur_mic) {
3843 	case R3DI_REAR_MIC:
3844 		cur_gpio &= ~(1 << R3DI_MIC_SELECT_BIT);
3845 		break;
3846 	case R3DI_FRONT_MIC:
3847 		cur_gpio |= (1 << R3DI_MIC_SELECT_BIT);
3848 		break;
3849 	}
3850 	snd_hda_codec_write(codec, codec->core.afg, 0,
3851 			    AC_VERB_SET_GPIO_DATA, cur_gpio);
3852 }
3853 
3854 static void r3di_gpio_dsp_status_set(struct hda_codec *codec,
3855 		enum r3di_dsp_status dsp_status)
3856 {
3857 	unsigned int cur_gpio;
3858 
3859 	/* Get the current GPIO Data setup */
3860 	cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3861 
3862 	switch (dsp_status) {
3863 	case R3DI_DSP_DOWNLOADING:
3864 		cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADING);
3865 		snd_hda_codec_write(codec, codec->core.afg, 0,
3866 				AC_VERB_SET_GPIO_DATA, cur_gpio);
3867 		break;
3868 	case R3DI_DSP_DOWNLOADED:
3869 		/* Set DOWNLOADING bit to 0. */
3870 		cur_gpio &= ~(1 << R3DI_GPIO_DSP_DOWNLOADING);
3871 
3872 		snd_hda_codec_write(codec, codec->core.afg, 0,
3873 				AC_VERB_SET_GPIO_DATA, cur_gpio);
3874 
3875 		cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADED);
3876 		break;
3877 	}
3878 
3879 	snd_hda_codec_write(codec, codec->core.afg, 0,
3880 			    AC_VERB_SET_GPIO_DATA, cur_gpio);
3881 }
3882 
3883 /*
3884  * PCM callbacks
3885  */
3886 static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3887 			struct hda_codec *codec,
3888 			unsigned int stream_tag,
3889 			unsigned int format,
3890 			struct snd_pcm_substream *substream)
3891 {
3892 	struct ca0132_spec *spec = codec->spec;
3893 
3894 	snd_hda_codec_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
3895 
3896 	return 0;
3897 }
3898 
3899 static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3900 			struct hda_codec *codec,
3901 			struct snd_pcm_substream *substream)
3902 {
3903 	struct ca0132_spec *spec = codec->spec;
3904 
3905 	if (spec->dsp_state == DSP_DOWNLOADING)
3906 		return 0;
3907 
3908 	/*If Playback effects are on, allow stream some time to flush
3909 	 *effects tail*/
3910 	if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3911 		msleep(50);
3912 
3913 	snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
3914 
3915 	return 0;
3916 }
3917 
3918 static unsigned int ca0132_playback_pcm_delay(struct hda_pcm_stream *info,
3919 			struct hda_codec *codec,
3920 			struct snd_pcm_substream *substream)
3921 {
3922 	struct ca0132_spec *spec = codec->spec;
3923 	unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
3924 	struct snd_pcm_runtime *runtime = substream->runtime;
3925 
3926 	if (spec->dsp_state != DSP_DOWNLOADED)
3927 		return 0;
3928 
3929 	/* Add latency if playback enhancement and either effect is enabled. */
3930 	if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
3931 		if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
3932 		    (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
3933 			latency += DSP_PLAY_ENHANCEMENT_LATENCY;
3934 	}
3935 
3936 	/* Applying Speaker EQ adds latency as well. */
3937 	if (spec->cur_out_type == SPEAKER_OUT)
3938 		latency += DSP_SPEAKER_OUT_LATENCY;
3939 
3940 	return (latency * runtime->rate) / 1000;
3941 }
3942 
3943 /*
3944  * Digital out
3945  */
3946 static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3947 					struct hda_codec *codec,
3948 					struct snd_pcm_substream *substream)
3949 {
3950 	struct ca0132_spec *spec = codec->spec;
3951 	return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3952 }
3953 
3954 static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3955 			struct hda_codec *codec,
3956 			unsigned int stream_tag,
3957 			unsigned int format,
3958 			struct snd_pcm_substream *substream)
3959 {
3960 	struct ca0132_spec *spec = codec->spec;
3961 	return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3962 					     stream_tag, format, substream);
3963 }
3964 
3965 static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3966 			struct hda_codec *codec,
3967 			struct snd_pcm_substream *substream)
3968 {
3969 	struct ca0132_spec *spec = codec->spec;
3970 	return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3971 }
3972 
3973 static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3974 					 struct hda_codec *codec,
3975 					 struct snd_pcm_substream *substream)
3976 {
3977 	struct ca0132_spec *spec = codec->spec;
3978 	return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3979 }
3980 
3981 /*
3982  * Analog capture
3983  */
3984 static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3985 					struct hda_codec *codec,
3986 					unsigned int stream_tag,
3987 					unsigned int format,
3988 					struct snd_pcm_substream *substream)
3989 {
3990 	snd_hda_codec_setup_stream(codec, hinfo->nid,
3991 				   stream_tag, 0, format);
3992 
3993 	return 0;
3994 }
3995 
3996 static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3997 			struct hda_codec *codec,
3998 			struct snd_pcm_substream *substream)
3999 {
4000 	struct ca0132_spec *spec = codec->spec;
4001 
4002 	if (spec->dsp_state == DSP_DOWNLOADING)
4003 		return 0;
4004 
4005 	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4006 	return 0;
4007 }
4008 
4009 static unsigned int ca0132_capture_pcm_delay(struct hda_pcm_stream *info,
4010 			struct hda_codec *codec,
4011 			struct snd_pcm_substream *substream)
4012 {
4013 	struct ca0132_spec *spec = codec->spec;
4014 	unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
4015 	struct snd_pcm_runtime *runtime = substream->runtime;
4016 
4017 	if (spec->dsp_state != DSP_DOWNLOADED)
4018 		return 0;
4019 
4020 	if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
4021 		latency += DSP_CRYSTAL_VOICE_LATENCY;
4022 
4023 	return (latency * runtime->rate) / 1000;
4024 }
4025 
4026 /*
4027  * Controls stuffs.
4028  */
4029 
4030 /*
4031  * Mixer controls helpers.
4032  */
4033 #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
4034 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4035 	  .name = xname, \
4036 	  .subdevice = HDA_SUBDEV_AMP_FLAG, \
4037 	  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
4038 			SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
4039 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
4040 	  .info = ca0132_volume_info, \
4041 	  .get = ca0132_volume_get, \
4042 	  .put = ca0132_volume_put, \
4043 	  .tlv = { .c = ca0132_volume_tlv }, \
4044 	  .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
4045 
4046 /*
4047  * Creates a mixer control that uses defaults of HDA_CODEC_VOL except for the
4048  * volume put, which is used for setting the DSP volume. This was done because
4049  * the ca0132 functions were taking too much time and causing lag.
4050  */
4051 #define CA0132_ALT_CODEC_VOL_MONO(xname, nid, channel, dir) \
4052 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4053 	  .name = xname, \
4054 	  .subdevice = HDA_SUBDEV_AMP_FLAG, \
4055 	  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
4056 			SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
4057 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
4058 	  .info = snd_hda_mixer_amp_volume_info, \
4059 	  .get = snd_hda_mixer_amp_volume_get, \
4060 	  .put = ca0132_alt_volume_put, \
4061 	  .tlv = { .c = snd_hda_mixer_amp_tlv }, \
4062 	  .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
4063 
4064 #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
4065 	{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4066 	  .name = xname, \
4067 	  .subdevice = HDA_SUBDEV_AMP_FLAG, \
4068 	  .info = snd_hda_mixer_amp_switch_info, \
4069 	  .get = ca0132_switch_get, \
4070 	  .put = ca0132_switch_put, \
4071 	  .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
4072 
4073 /* stereo */
4074 #define CA0132_CODEC_VOL(xname, nid, dir) \
4075 	CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
4076 #define CA0132_ALT_CODEC_VOL(xname, nid, dir) \
4077 	CA0132_ALT_CODEC_VOL_MONO(xname, nid, 3, dir)
4078 #define CA0132_CODEC_MUTE(xname, nid, dir) \
4079 	CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
4080 
4081 /* lookup tables */
4082 /*
4083  * Lookup table with decibel values for the DSP. When volume is changed in
4084  * Windows, the DSP is also sent the dB value in floating point. In Windows,
4085  * these values have decimal points, probably because the Windows driver
4086  * actually uses floating point. We can't here, so I made a lookup table of
4087  * values -90 to 9. -90 is the lowest decibel value for both the ADC's and the
4088  * DAC's, and 9 is the maximum.
4089  */
4090 static const unsigned int float_vol_db_lookup[] = {
4091 0xC2B40000, 0xC2B20000, 0xC2B00000, 0xC2AE0000, 0xC2AC0000, 0xC2AA0000,
4092 0xC2A80000, 0xC2A60000, 0xC2A40000, 0xC2A20000, 0xC2A00000, 0xC29E0000,
4093 0xC29C0000, 0xC29A0000, 0xC2980000, 0xC2960000, 0xC2940000, 0xC2920000,
4094 0xC2900000, 0xC28E0000, 0xC28C0000, 0xC28A0000, 0xC2880000, 0xC2860000,
4095 0xC2840000, 0xC2820000, 0xC2800000, 0xC27C0000, 0xC2780000, 0xC2740000,
4096 0xC2700000, 0xC26C0000, 0xC2680000, 0xC2640000, 0xC2600000, 0xC25C0000,
4097 0xC2580000, 0xC2540000, 0xC2500000, 0xC24C0000, 0xC2480000, 0xC2440000,
4098 0xC2400000, 0xC23C0000, 0xC2380000, 0xC2340000, 0xC2300000, 0xC22C0000,
4099 0xC2280000, 0xC2240000, 0xC2200000, 0xC21C0000, 0xC2180000, 0xC2140000,
4100 0xC2100000, 0xC20C0000, 0xC2080000, 0xC2040000, 0xC2000000, 0xC1F80000,
4101 0xC1F00000, 0xC1E80000, 0xC1E00000, 0xC1D80000, 0xC1D00000, 0xC1C80000,
4102 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
4103 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
4104 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
4105 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
4106 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
4107 0x40C00000, 0x40E00000, 0x41000000, 0x41100000
4108 };
4109 
4110 /*
4111  * This table counts from float 0 to 1 in increments of .01, which is
4112  * useful for a few different sliders.
4113  */
4114 static const unsigned int float_zero_to_one_lookup[] = {
4115 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
4116 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
4117 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
4118 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
4119 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
4120 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
4121 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
4122 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
4123 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
4124 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
4125 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
4126 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
4127 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
4128 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
4129 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
4130 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
4131 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
4132 };
4133 
4134 /*
4135  * This table counts from float 10 to 1000, which is the range of the x-bass
4136  * crossover slider in Windows.
4137  */
4138 static const unsigned int float_xbass_xover_lookup[] = {
4139 0x41200000, 0x41A00000, 0x41F00000, 0x42200000, 0x42480000, 0x42700000,
4140 0x428C0000, 0x42A00000, 0x42B40000, 0x42C80000, 0x42DC0000, 0x42F00000,
4141 0x43020000, 0x430C0000, 0x43160000, 0x43200000, 0x432A0000, 0x43340000,
4142 0x433E0000, 0x43480000, 0x43520000, 0x435C0000, 0x43660000, 0x43700000,
4143 0x437A0000, 0x43820000, 0x43870000, 0x438C0000, 0x43910000, 0x43960000,
4144 0x439B0000, 0x43A00000, 0x43A50000, 0x43AA0000, 0x43AF0000, 0x43B40000,
4145 0x43B90000, 0x43BE0000, 0x43C30000, 0x43C80000, 0x43CD0000, 0x43D20000,
4146 0x43D70000, 0x43DC0000, 0x43E10000, 0x43E60000, 0x43EB0000, 0x43F00000,
4147 0x43F50000, 0x43FA0000, 0x43FF0000, 0x44020000, 0x44048000, 0x44070000,
4148 0x44098000, 0x440C0000, 0x440E8000, 0x44110000, 0x44138000, 0x44160000,
4149 0x44188000, 0x441B0000, 0x441D8000, 0x44200000, 0x44228000, 0x44250000,
4150 0x44278000, 0x442A0000, 0x442C8000, 0x442F0000, 0x44318000, 0x44340000,
4151 0x44368000, 0x44390000, 0x443B8000, 0x443E0000, 0x44408000, 0x44430000,
4152 0x44458000, 0x44480000, 0x444A8000, 0x444D0000, 0x444F8000, 0x44520000,
4153 0x44548000, 0x44570000, 0x44598000, 0x445C0000, 0x445E8000, 0x44610000,
4154 0x44638000, 0x44660000, 0x44688000, 0x446B0000, 0x446D8000, 0x44700000,
4155 0x44728000, 0x44750000, 0x44778000, 0x447A0000
4156 };
4157 
4158 /* The following are for tuning of products */
4159 #ifdef ENABLE_TUNING_CONTROLS
4160 
4161 static const unsigned int voice_focus_vals_lookup[] = {
4162 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
4163 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
4164 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
4165 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
4166 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
4167 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
4168 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
4169 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
4170 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
4171 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
4172 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
4173 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
4174 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
4175 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
4176 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
4177 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
4178 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
4179 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
4180 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
4181 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
4182 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
4183 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
4184 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
4185 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
4186 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
4187 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
4188 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
4189 };
4190 
4191 static const unsigned int mic_svm_vals_lookup[] = {
4192 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
4193 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
4194 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
4195 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
4196 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
4197 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
4198 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
4199 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
4200 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
4201 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
4202 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
4203 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
4204 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
4205 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
4206 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
4207 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
4208 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
4209 };
4210 
4211 static const unsigned int equalizer_vals_lookup[] = {
4212 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
4213 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
4214 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
4215 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
4216 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
4217 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
4218 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
4219 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
4220 0x41C00000
4221 };
4222 
4223 static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
4224 			  const unsigned int *lookup, int idx)
4225 {
4226 	int i = 0;
4227 
4228 	for (i = 0; i < TUNING_CTLS_COUNT; i++)
4229 		if (nid == ca0132_tuning_ctls[i].nid)
4230 			break;
4231 
4232 	snd_hda_power_up(codec);
4233 	dspio_set_param(codec, ca0132_tuning_ctls[i].mid, 0x20,
4234 			ca0132_tuning_ctls[i].req,
4235 			&(lookup[idx]), sizeof(unsigned int));
4236 	snd_hda_power_down(codec);
4237 
4238 	return 1;
4239 }
4240 
4241 static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
4242 			  struct snd_ctl_elem_value *ucontrol)
4243 {
4244 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4245 	struct ca0132_spec *spec = codec->spec;
4246 	hda_nid_t nid = get_amp_nid(kcontrol);
4247 	long *valp = ucontrol->value.integer.value;
4248 	int idx = nid - TUNING_CTL_START_NID;
4249 
4250 	*valp = spec->cur_ctl_vals[idx];
4251 	return 0;
4252 }
4253 
4254 static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
4255 			      struct snd_ctl_elem_info *uinfo)
4256 {
4257 	int chs = get_amp_channels(kcontrol);
4258 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4259 	uinfo->count = chs == 3 ? 2 : 1;
4260 	uinfo->value.integer.min = 20;
4261 	uinfo->value.integer.max = 180;
4262 	uinfo->value.integer.step = 1;
4263 
4264 	return 0;
4265 }
4266 
4267 static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
4268 				struct snd_ctl_elem_value *ucontrol)
4269 {
4270 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4271 	struct ca0132_spec *spec = codec->spec;
4272 	hda_nid_t nid = get_amp_nid(kcontrol);
4273 	long *valp = ucontrol->value.integer.value;
4274 	int idx;
4275 
4276 	idx = nid - TUNING_CTL_START_NID;
4277 	/* any change? */
4278 	if (spec->cur_ctl_vals[idx] == *valp)
4279 		return 0;
4280 
4281 	spec->cur_ctl_vals[idx] = *valp;
4282 
4283 	idx = *valp - 20;
4284 	tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
4285 
4286 	return 1;
4287 }
4288 
4289 static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
4290 			      struct snd_ctl_elem_info *uinfo)
4291 {
4292 	int chs = get_amp_channels(kcontrol);
4293 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4294 	uinfo->count = chs == 3 ? 2 : 1;
4295 	uinfo->value.integer.min = 0;
4296 	uinfo->value.integer.max = 100;
4297 	uinfo->value.integer.step = 1;
4298 
4299 	return 0;
4300 }
4301 
4302 static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
4303 				struct snd_ctl_elem_value *ucontrol)
4304 {
4305 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4306 	struct ca0132_spec *spec = codec->spec;
4307 	hda_nid_t nid = get_amp_nid(kcontrol);
4308 	long *valp = ucontrol->value.integer.value;
4309 	int idx;
4310 
4311 	idx = nid - TUNING_CTL_START_NID;
4312 	/* any change? */
4313 	if (spec->cur_ctl_vals[idx] == *valp)
4314 		return 0;
4315 
4316 	spec->cur_ctl_vals[idx] = *valp;
4317 
4318 	idx = *valp;
4319 	tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
4320 
4321 	return 0;
4322 }
4323 
4324 static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
4325 			      struct snd_ctl_elem_info *uinfo)
4326 {
4327 	int chs = get_amp_channels(kcontrol);
4328 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4329 	uinfo->count = chs == 3 ? 2 : 1;
4330 	uinfo->value.integer.min = 0;
4331 	uinfo->value.integer.max = 48;
4332 	uinfo->value.integer.step = 1;
4333 
4334 	return 0;
4335 }
4336 
4337 static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
4338 				struct snd_ctl_elem_value *ucontrol)
4339 {
4340 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4341 	struct ca0132_spec *spec = codec->spec;
4342 	hda_nid_t nid = get_amp_nid(kcontrol);
4343 	long *valp = ucontrol->value.integer.value;
4344 	int idx;
4345 
4346 	idx = nid - TUNING_CTL_START_NID;
4347 	/* any change? */
4348 	if (spec->cur_ctl_vals[idx] == *valp)
4349 		return 0;
4350 
4351 	spec->cur_ctl_vals[idx] = *valp;
4352 
4353 	idx = *valp;
4354 	tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
4355 
4356 	return 1;
4357 }
4358 
4359 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
4360 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(eq_db_scale, -2400, 100, 0);
4361 
4362 static int add_tuning_control(struct hda_codec *codec,
4363 				hda_nid_t pnid, hda_nid_t nid,
4364 				const char *name, int dir)
4365 {
4366 	char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
4367 	int type = dir ? HDA_INPUT : HDA_OUTPUT;
4368 	struct snd_kcontrol_new knew =
4369 		HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
4370 
4371 	knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
4372 			SNDRV_CTL_ELEM_ACCESS_TLV_READ;
4373 	knew.tlv.c = 0;
4374 	knew.tlv.p = 0;
4375 	switch (pnid) {
4376 	case VOICE_FOCUS:
4377 		knew.info = voice_focus_ctl_info;
4378 		knew.get = tuning_ctl_get;
4379 		knew.put = voice_focus_ctl_put;
4380 		knew.tlv.p = voice_focus_db_scale;
4381 		break;
4382 	case MIC_SVM:
4383 		knew.info = mic_svm_ctl_info;
4384 		knew.get = tuning_ctl_get;
4385 		knew.put = mic_svm_ctl_put;
4386 		break;
4387 	case EQUALIZER:
4388 		knew.info = equalizer_ctl_info;
4389 		knew.get = tuning_ctl_get;
4390 		knew.put = equalizer_ctl_put;
4391 		knew.tlv.p = eq_db_scale;
4392 		break;
4393 	default:
4394 		return 0;
4395 	}
4396 	knew.private_value =
4397 		HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
4398 	sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
4399 	return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
4400 }
4401 
4402 static int add_tuning_ctls(struct hda_codec *codec)
4403 {
4404 	int i;
4405 	int err;
4406 
4407 	for (i = 0; i < TUNING_CTLS_COUNT; i++) {
4408 		err = add_tuning_control(codec,
4409 					ca0132_tuning_ctls[i].parent_nid,
4410 					ca0132_tuning_ctls[i].nid,
4411 					ca0132_tuning_ctls[i].name,
4412 					ca0132_tuning_ctls[i].direct);
4413 		if (err < 0)
4414 			return err;
4415 	}
4416 
4417 	return 0;
4418 }
4419 
4420 static void ca0132_init_tuning_defaults(struct hda_codec *codec)
4421 {
4422 	struct ca0132_spec *spec = codec->spec;
4423 	int i;
4424 
4425 	/* Wedge Angle defaults to 30.  10 below is 30 - 20.  20 is min. */
4426 	spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
4427 	/* SVM level defaults to 0.74. */
4428 	spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
4429 
4430 	/* EQ defaults to 0dB. */
4431 	for (i = 2; i < TUNING_CTLS_COUNT; i++)
4432 		spec->cur_ctl_vals[i] = 24;
4433 }
4434 #endif /*ENABLE_TUNING_CONTROLS*/
4435 
4436 /*
4437  * Select the active output.
4438  * If autodetect is enabled, output will be selected based on jack detection.
4439  * If jack inserted, headphone will be selected, else built-in speakers
4440  * If autodetect is disabled, output will be selected based on selection.
4441  */
4442 static int ca0132_select_out(struct hda_codec *codec)
4443 {
4444 	struct ca0132_spec *spec = codec->spec;
4445 	unsigned int pin_ctl;
4446 	int jack_present;
4447 	int auto_jack;
4448 	unsigned int tmp;
4449 	int err;
4450 
4451 	codec_dbg(codec, "ca0132_select_out\n");
4452 
4453 	snd_hda_power_up_pm(codec);
4454 
4455 	auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
4456 
4457 	if (auto_jack)
4458 		jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp);
4459 	else
4460 		jack_present =
4461 			spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
4462 
4463 	if (jack_present)
4464 		spec->cur_out_type = HEADPHONE_OUT;
4465 	else
4466 		spec->cur_out_type = SPEAKER_OUT;
4467 
4468 	if (spec->cur_out_type == SPEAKER_OUT) {
4469 		codec_dbg(codec, "ca0132_select_out speaker\n");
4470 		/*speaker out config*/
4471 		tmp = FLOAT_ONE;
4472 		err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
4473 		if (err < 0)
4474 			goto exit;
4475 		/*enable speaker EQ*/
4476 		tmp = FLOAT_ONE;
4477 		err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
4478 		if (err < 0)
4479 			goto exit;
4480 
4481 		/* Setup EAPD */
4482 		snd_hda_codec_write(codec, spec->out_pins[1], 0,
4483 				    VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
4484 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4485 				    AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4486 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4487 				    VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
4488 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4489 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
4490 
4491 		/* disable headphone node */
4492 		pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4493 					AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4494 		snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4495 				    pin_ctl & ~PIN_HP);
4496 		/* enable speaker node */
4497 		pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4498 				AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4499 		snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4500 				    pin_ctl | PIN_OUT);
4501 	} else {
4502 		codec_dbg(codec, "ca0132_select_out hp\n");
4503 		/*headphone out config*/
4504 		tmp = FLOAT_ZERO;
4505 		err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
4506 		if (err < 0)
4507 			goto exit;
4508 		/*disable speaker EQ*/
4509 		tmp = FLOAT_ZERO;
4510 		err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
4511 		if (err < 0)
4512 			goto exit;
4513 
4514 		/* Setup EAPD */
4515 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4516 				    VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
4517 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4518 				    AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4519 		snd_hda_codec_write(codec, spec->out_pins[1], 0,
4520 				    VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
4521 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4522 				    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
4523 
4524 		/* disable speaker*/
4525 		pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4526 					AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4527 		snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4528 				    pin_ctl & ~PIN_HP);
4529 		/* enable headphone*/
4530 		pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4531 					AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4532 		snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4533 				    pin_ctl | PIN_HP);
4534 	}
4535 
4536 exit:
4537 	snd_hda_power_down_pm(codec);
4538 
4539 	return err < 0 ? err : 0;
4540 }
4541 
4542 static int ae5_headphone_gain_set(struct hda_codec *codec, long val);
4543 static int zxr_headphone_gain_set(struct hda_codec *codec, long val);
4544 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
4545 
4546 static void ae5_mmio_select_out(struct hda_codec *codec)
4547 {
4548 	struct ca0132_spec *spec = codec->spec;
4549 	const struct ae_ca0113_output_set *out_cmds;
4550 	unsigned int i;
4551 
4552 	if (ca0132_quirk(spec) == QUIRK_AE5)
4553 		out_cmds = &ae5_ca0113_output_presets;
4554 	else
4555 		out_cmds = &ae7_ca0113_output_presets;
4556 
4557 	for (i = 0; i < AE_CA0113_OUT_SET_COMMANDS; i++)
4558 		ca0113_mmio_command_set(codec, out_cmds->group[i],
4559 				out_cmds->target[i],
4560 				out_cmds->vals[spec->cur_out_type][i]);
4561 }
4562 
4563 static int ca0132_alt_set_full_range_speaker(struct hda_codec *codec)
4564 {
4565 	struct ca0132_spec *spec = codec->spec;
4566 	int quirk = ca0132_quirk(spec);
4567 	unsigned int tmp;
4568 	int err;
4569 
4570 	/* 2.0/4.0 setup has no LFE channel, so setting full-range does nothing. */
4571 	if (spec->channel_cfg_val == SPEAKER_CHANNELS_4_0
4572 			|| spec->channel_cfg_val == SPEAKER_CHANNELS_2_0)
4573 		return 0;
4574 
4575 	/* Set front L/R full range. Zero for full-range, one for redirection. */
4576 	tmp = spec->speaker_range_val[0] ? FLOAT_ZERO : FLOAT_ONE;
4577 	err = dspio_set_uint_param(codec, 0x96,
4578 			SPEAKER_FULL_RANGE_FRONT_L_R, tmp);
4579 	if (err < 0)
4580 		return err;
4581 
4582 	/* When setting full-range rear, both rear and center/lfe are set. */
4583 	tmp = spec->speaker_range_val[1] ? FLOAT_ZERO : FLOAT_ONE;
4584 	err = dspio_set_uint_param(codec, 0x96,
4585 			SPEAKER_FULL_RANGE_CENTER_LFE, tmp);
4586 	if (err < 0)
4587 		return err;
4588 
4589 	err = dspio_set_uint_param(codec, 0x96,
4590 			SPEAKER_FULL_RANGE_REAR_L_R, tmp);
4591 	if (err < 0)
4592 		return err;
4593 
4594 	/*
4595 	 * Only the AE series cards set this value when setting full-range,
4596 	 * and it's always 1.0f.
4597 	 */
4598 	if (quirk == QUIRK_AE5 || quirk == QUIRK_AE7) {
4599 		err = dspio_set_uint_param(codec, 0x96,
4600 				SPEAKER_FULL_RANGE_SURROUND_L_R, FLOAT_ONE);
4601 		if (err < 0)
4602 			return err;
4603 	}
4604 
4605 	return 0;
4606 }
4607 
4608 static int ca0132_alt_surround_set_bass_redirection(struct hda_codec *codec,
4609 		bool val)
4610 {
4611 	struct ca0132_spec *spec = codec->spec;
4612 	unsigned int tmp;
4613 	int err;
4614 
4615 	if (val && spec->channel_cfg_val != SPEAKER_CHANNELS_4_0 &&
4616 			spec->channel_cfg_val != SPEAKER_CHANNELS_2_0)
4617 		tmp = FLOAT_ONE;
4618 	else
4619 		tmp = FLOAT_ZERO;
4620 
4621 	err = dspio_set_uint_param(codec, 0x96, SPEAKER_BASS_REDIRECT, tmp);
4622 	if (err < 0)
4623 		return err;
4624 
4625 	/* If it is enabled, make sure to set the crossover frequency. */
4626 	if (tmp) {
4627 		tmp = float_xbass_xover_lookup[spec->xbass_xover_freq];
4628 		err = dspio_set_uint_param(codec, 0x96,
4629 				SPEAKER_BASS_REDIRECT_XOVER_FREQ, tmp);
4630 		if (err < 0)
4631 			return err;
4632 	}
4633 
4634 	return 0;
4635 }
4636 
4637 /*
4638  * These are the commands needed to setup output on each of the different card
4639  * types.
4640  */
4641 static void ca0132_alt_select_out_get_quirk_data(struct hda_codec *codec,
4642 		const struct ca0132_alt_out_set_quirk_data **quirk_data)
4643 {
4644 	struct ca0132_spec *spec = codec->spec;
4645 	int quirk = ca0132_quirk(spec);
4646 	unsigned int i;
4647 
4648 	*quirk_data = NULL;
4649 	for (i = 0; i < ARRAY_SIZE(quirk_out_set_data); i++) {
4650 		if (quirk_out_set_data[i].quirk_id == quirk) {
4651 			*quirk_data = &quirk_out_set_data[i];
4652 			return;
4653 		}
4654 	}
4655 }
4656 
4657 static int ca0132_alt_select_out_quirk_set(struct hda_codec *codec)
4658 {
4659 	const struct ca0132_alt_out_set_quirk_data *quirk_data;
4660 	const struct ca0132_alt_out_set_info *out_info;
4661 	struct ca0132_spec *spec = codec->spec;
4662 	unsigned int i, gpio_data;
4663 	int err;
4664 
4665 	ca0132_alt_select_out_get_quirk_data(codec, &quirk_data);
4666 	if (!quirk_data)
4667 		return 0;
4668 
4669 	out_info = &quirk_data->out_set_info[spec->cur_out_type];
4670 	if (quirk_data->is_ae_series)
4671 		ae5_mmio_select_out(codec);
4672 
4673 	if (out_info->has_hda_gpio) {
4674 		gpio_data = snd_hda_codec_read(codec, codec->core.afg, 0,
4675 				AC_VERB_GET_GPIO_DATA, 0);
4676 
4677 		if (out_info->hda_gpio_set)
4678 			gpio_data |= (1 << out_info->hda_gpio_pin);
4679 		else
4680 			gpio_data &= ~(1 << out_info->hda_gpio_pin);
4681 
4682 		snd_hda_codec_write(codec, codec->core.afg, 0,
4683 				    AC_VERB_SET_GPIO_DATA, gpio_data);
4684 	}
4685 
4686 	if (out_info->mmio_gpio_count) {
4687 		for (i = 0; i < out_info->mmio_gpio_count; i++) {
4688 			ca0113_mmio_gpio_set(codec, out_info->mmio_gpio_pin[i],
4689 					out_info->mmio_gpio_set[i]);
4690 		}
4691 	}
4692 
4693 	if (out_info->scp_cmds_count) {
4694 		for (i = 0; i < out_info->scp_cmds_count; i++) {
4695 			err = dspio_set_uint_param(codec,
4696 					out_info->scp_cmd_mid[i],
4697 					out_info->scp_cmd_req[i],
4698 					out_info->scp_cmd_val[i]);
4699 			if (err < 0)
4700 				return err;
4701 		}
4702 	}
4703 
4704 	chipio_set_control_param(codec, 0x0d, out_info->dac2port);
4705 
4706 	if (out_info->has_chipio_write) {
4707 		chipio_write(codec, out_info->chipio_write_addr,
4708 				out_info->chipio_write_data);
4709 	}
4710 
4711 	if (quirk_data->has_headphone_gain) {
4712 		if (spec->cur_out_type != HEADPHONE_OUT) {
4713 			if (quirk_data->is_ae_series)
4714 				ae5_headphone_gain_set(codec, 2);
4715 			else
4716 				zxr_headphone_gain_set(codec, 0);
4717 		} else {
4718 			if (quirk_data->is_ae_series)
4719 				ae5_headphone_gain_set(codec,
4720 						spec->ae5_headphone_gain_val);
4721 			else
4722 				zxr_headphone_gain_set(codec,
4723 						spec->zxr_gain_set);
4724 		}
4725 	}
4726 
4727 	return 0;
4728 }
4729 
4730 static void ca0132_set_out_node_pincfg(struct hda_codec *codec, hda_nid_t nid,
4731 		bool out_enable, bool hp_enable)
4732 {
4733 	unsigned int pin_ctl;
4734 
4735 	pin_ctl = snd_hda_codec_read(codec, nid, 0,
4736 			AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4737 
4738 	pin_ctl = hp_enable ? pin_ctl | PIN_HP_AMP : pin_ctl & ~PIN_HP_AMP;
4739 	pin_ctl = out_enable ? pin_ctl | PIN_OUT : pin_ctl & ~PIN_OUT;
4740 	snd_hda_set_pin_ctl(codec, nid, pin_ctl);
4741 }
4742 
4743 /*
4744  * This function behaves similarly to the ca0132_select_out funciton above,
4745  * except with a few differences. It adds the ability to select the current
4746  * output with an enumerated control "output source" if the auto detect
4747  * mute switch is set to off. If the auto detect mute switch is enabled, it
4748  * will detect either headphone or lineout(SPEAKER_OUT) from jack detection.
4749  * It also adds the ability to auto-detect the front headphone port.
4750  */
4751 static int ca0132_alt_select_out(struct hda_codec *codec)
4752 {
4753 	struct ca0132_spec *spec = codec->spec;
4754 	unsigned int tmp, outfx_set;
4755 	int jack_present;
4756 	int auto_jack;
4757 	int err;
4758 	/* Default Headphone is rear headphone */
4759 	hda_nid_t headphone_nid = spec->out_pins[1];
4760 
4761 	codec_dbg(codec, "%s\n", __func__);
4762 
4763 	snd_hda_power_up_pm(codec);
4764 
4765 	auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
4766 
4767 	/*
4768 	 * If headphone rear or front is plugged in, set to headphone.
4769 	 * If neither is plugged in, set to rear line out. Only if
4770 	 * hp/speaker auto detect is enabled.
4771 	 */
4772 	if (auto_jack) {
4773 		jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp) ||
4774 			   snd_hda_jack_detect(codec, spec->unsol_tag_front_hp);
4775 
4776 		if (jack_present)
4777 			spec->cur_out_type = HEADPHONE_OUT;
4778 		else
4779 			spec->cur_out_type = SPEAKER_OUT;
4780 	} else
4781 		spec->cur_out_type = spec->out_enum_val;
4782 
4783 	outfx_set = spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID];
4784 
4785 	/* Begin DSP output switch, mute DSP volume. */
4786 	err = dspio_set_uint_param(codec, 0x96, SPEAKER_TUNING_MUTE, FLOAT_ONE);
4787 	if (err < 0)
4788 		goto exit;
4789 
4790 	if (ca0132_alt_select_out_quirk_set(codec) < 0)
4791 		goto exit;
4792 
4793 	switch (spec->cur_out_type) {
4794 	case SPEAKER_OUT:
4795 		codec_dbg(codec, "%s speaker\n", __func__);
4796 
4797 		/* Enable EAPD */
4798 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4799 			AC_VERB_SET_EAPD_BTLENABLE, 0x01);
4800 
4801 		/* Disable headphone node. */
4802 		ca0132_set_out_node_pincfg(codec, spec->out_pins[1], 0, 0);
4803 		/* Set front L-R to output. */
4804 		ca0132_set_out_node_pincfg(codec, spec->out_pins[0], 1, 0);
4805 		/* Set Center/LFE to output. */
4806 		ca0132_set_out_node_pincfg(codec, spec->out_pins[2], 1, 0);
4807 		/* Set rear surround to output. */
4808 		ca0132_set_out_node_pincfg(codec, spec->out_pins[3], 1, 0);
4809 
4810 		/*
4811 		 * Without PlayEnhancement being enabled, if we've got a 2.0
4812 		 * setup, set it to floating point eight to disable any DSP
4813 		 * processing effects.
4814 		 */
4815 		if (!outfx_set && spec->channel_cfg_val == SPEAKER_CHANNELS_2_0)
4816 			tmp = FLOAT_EIGHT;
4817 		else
4818 			tmp = speaker_channel_cfgs[spec->channel_cfg_val].val;
4819 
4820 		err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
4821 		if (err < 0)
4822 			goto exit;
4823 
4824 		break;
4825 	case HEADPHONE_OUT:
4826 		codec_dbg(codec, "%s hp\n", __func__);
4827 		snd_hda_codec_write(codec, spec->out_pins[0], 0,
4828 			AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4829 
4830 		/* Disable all speaker nodes. */
4831 		ca0132_set_out_node_pincfg(codec, spec->out_pins[0], 0, 0);
4832 		ca0132_set_out_node_pincfg(codec, spec->out_pins[2], 0, 0);
4833 		ca0132_set_out_node_pincfg(codec, spec->out_pins[3], 0, 0);
4834 
4835 		/* enable headphone, either front or rear */
4836 		if (snd_hda_jack_detect(codec, spec->unsol_tag_front_hp))
4837 			headphone_nid = spec->out_pins[2];
4838 		else if (snd_hda_jack_detect(codec, spec->unsol_tag_hp))
4839 			headphone_nid = spec->out_pins[1];
4840 
4841 		ca0132_set_out_node_pincfg(codec, headphone_nid, 1, 1);
4842 
4843 		if (outfx_set)
4844 			err = dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4845 		else
4846 			err = dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ZERO);
4847 
4848 		if (err < 0)
4849 			goto exit;
4850 		break;
4851 	}
4852 	/*
4853 	 * If output effects are enabled, set the X-Bass effect value again to
4854 	 * make sure that it's properly enabled/disabled for speaker
4855 	 * configurations with an LFE channel.
4856 	 */
4857 	if (outfx_set)
4858 		ca0132_effects_set(codec, X_BASS,
4859 			spec->effects_switch[X_BASS - EFFECT_START_NID]);
4860 
4861 	/* Set speaker EQ bypass attenuation to 0. */
4862 	err = dspio_set_uint_param(codec, 0x8f, 0x01, FLOAT_ZERO);
4863 	if (err < 0)
4864 		goto exit;
4865 
4866 	/*
4867 	 * Although unused on all cards but the AE series, this is always set
4868 	 * to zero when setting the output.
4869 	 */
4870 	err = dspio_set_uint_param(codec, 0x96,
4871 			SPEAKER_TUNING_USE_SPEAKER_EQ, FLOAT_ZERO);
4872 	if (err < 0)
4873 		goto exit;
4874 
4875 	if (spec->cur_out_type == SPEAKER_OUT)
4876 		err = ca0132_alt_surround_set_bass_redirection(codec,
4877 				spec->bass_redirection_val);
4878 	else
4879 		err = ca0132_alt_surround_set_bass_redirection(codec, 0);
4880 
4881 	/* Unmute DSP now that we're done with output selection. */
4882 	err = dspio_set_uint_param(codec, 0x96,
4883 			SPEAKER_TUNING_MUTE, FLOAT_ZERO);
4884 	if (err < 0)
4885 		goto exit;
4886 
4887 	if (spec->cur_out_type == SPEAKER_OUT) {
4888 		err = ca0132_alt_set_full_range_speaker(codec);
4889 		if (err < 0)
4890 			goto exit;
4891 	}
4892 
4893 exit:
4894 	snd_hda_power_down_pm(codec);
4895 
4896 	return err < 0 ? err : 0;
4897 }
4898 
4899 static void ca0132_unsol_hp_delayed(struct work_struct *work)
4900 {
4901 	struct ca0132_spec *spec = container_of(
4902 		to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
4903 	struct hda_jack_tbl *jack;
4904 
4905 	if (ca0132_use_alt_functions(spec))
4906 		ca0132_alt_select_out(spec->codec);
4907 	else
4908 		ca0132_select_out(spec->codec);
4909 
4910 	jack = snd_hda_jack_tbl_get(spec->codec, spec->unsol_tag_hp);
4911 	if (jack) {
4912 		jack->block_report = 0;
4913 		snd_hda_jack_report_sync(spec->codec);
4914 	}
4915 }
4916 
4917 static void ca0132_set_dmic(struct hda_codec *codec, int enable);
4918 static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
4919 static void resume_mic1(struct hda_codec *codec, unsigned int oldval);
4920 static int stop_mic1(struct hda_codec *codec);
4921 static int ca0132_cvoice_switch_set(struct hda_codec *codec);
4922 static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val);
4923 
4924 /*
4925  * Select the active VIP source
4926  */
4927 static int ca0132_set_vipsource(struct hda_codec *codec, int val)
4928 {
4929 	struct ca0132_spec *spec = codec->spec;
4930 	unsigned int tmp;
4931 
4932 	if (spec->dsp_state != DSP_DOWNLOADED)
4933 		return 0;
4934 
4935 	/* if CrystalVoice if off, vipsource should be 0 */
4936 	if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4937 	    (val == 0)) {
4938 		chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4939 		chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4940 		chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4941 		if (spec->cur_mic_type == DIGITAL_MIC)
4942 			tmp = FLOAT_TWO;
4943 		else
4944 			tmp = FLOAT_ONE;
4945 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4946 		tmp = FLOAT_ZERO;
4947 		dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4948 	} else {
4949 		chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4950 		chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4951 		if (spec->cur_mic_type == DIGITAL_MIC)
4952 			tmp = FLOAT_TWO;
4953 		else
4954 			tmp = FLOAT_ONE;
4955 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4956 		tmp = FLOAT_ONE;
4957 		dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4958 		msleep(20);
4959 		chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
4960 	}
4961 
4962 	return 1;
4963 }
4964 
4965 static int ca0132_alt_set_vipsource(struct hda_codec *codec, int val)
4966 {
4967 	struct ca0132_spec *spec = codec->spec;
4968 	unsigned int tmp;
4969 
4970 	if (spec->dsp_state != DSP_DOWNLOADED)
4971 		return 0;
4972 
4973 	codec_dbg(codec, "%s\n", __func__);
4974 
4975 	chipio_set_stream_control(codec, 0x03, 0);
4976 	chipio_set_stream_control(codec, 0x04, 0);
4977 
4978 	/* if CrystalVoice is off, vipsource should be 0 */
4979 	if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4980 	    (val == 0) || spec->in_enum_val == REAR_LINE_IN) {
4981 		codec_dbg(codec, "%s: off.", __func__);
4982 		chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4983 
4984 		tmp = FLOAT_ZERO;
4985 		dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4986 
4987 		chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4988 		chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4989 		if (ca0132_quirk(spec) == QUIRK_R3DI)
4990 			chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4991 
4992 
4993 		if (spec->in_enum_val == REAR_LINE_IN)
4994 			tmp = FLOAT_ZERO;
4995 		else {
4996 			if (ca0132_quirk(spec) == QUIRK_SBZ)
4997 				tmp = FLOAT_THREE;
4998 			else
4999 				tmp = FLOAT_ONE;
5000 		}
5001 
5002 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5003 
5004 	} else {
5005 		codec_dbg(codec, "%s: on.", __func__);
5006 		chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
5007 		chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
5008 		if (ca0132_quirk(spec) == QUIRK_R3DI)
5009 			chipio_set_conn_rate(codec, 0x0F, SR_16_000);
5010 
5011 		if (spec->effects_switch[VOICE_FOCUS - EFFECT_START_NID])
5012 			tmp = FLOAT_TWO;
5013 		else
5014 			tmp = FLOAT_ONE;
5015 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5016 
5017 		tmp = FLOAT_ONE;
5018 		dspio_set_uint_param(codec, 0x80, 0x05, tmp);
5019 
5020 		msleep(20);
5021 		chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
5022 	}
5023 
5024 	chipio_set_stream_control(codec, 0x03, 1);
5025 	chipio_set_stream_control(codec, 0x04, 1);
5026 
5027 	return 1;
5028 }
5029 
5030 /*
5031  * Select the active microphone.
5032  * If autodetect is enabled, mic will be selected based on jack detection.
5033  * If jack inserted, ext.mic will be selected, else built-in mic
5034  * If autodetect is disabled, mic will be selected based on selection.
5035  */
5036 static int ca0132_select_mic(struct hda_codec *codec)
5037 {
5038 	struct ca0132_spec *spec = codec->spec;
5039 	int jack_present;
5040 	int auto_jack;
5041 
5042 	codec_dbg(codec, "ca0132_select_mic\n");
5043 
5044 	snd_hda_power_up_pm(codec);
5045 
5046 	auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
5047 
5048 	if (auto_jack)
5049 		jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_amic1);
5050 	else
5051 		jack_present =
5052 			spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
5053 
5054 	if (jack_present)
5055 		spec->cur_mic_type = LINE_MIC_IN;
5056 	else
5057 		spec->cur_mic_type = DIGITAL_MIC;
5058 
5059 	if (spec->cur_mic_type == DIGITAL_MIC) {
5060 		/* enable digital Mic */
5061 		chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
5062 		ca0132_set_dmic(codec, 1);
5063 		ca0132_mic_boost_set(codec, 0);
5064 		/* set voice focus */
5065 		ca0132_effects_set(codec, VOICE_FOCUS,
5066 				   spec->effects_switch
5067 				   [VOICE_FOCUS - EFFECT_START_NID]);
5068 	} else {
5069 		/* disable digital Mic */
5070 		chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
5071 		ca0132_set_dmic(codec, 0);
5072 		ca0132_mic_boost_set(codec, spec->cur_mic_boost);
5073 		/* disable voice focus */
5074 		ca0132_effects_set(codec, VOICE_FOCUS, 0);
5075 	}
5076 
5077 	snd_hda_power_down_pm(codec);
5078 
5079 	return 0;
5080 }
5081 
5082 /*
5083  * Select the active input.
5084  * Mic detection isn't used, because it's kind of pointless on the SBZ.
5085  * The front mic has no jack-detection, so the only way to switch to it
5086  * is to do it manually in alsamixer.
5087  */
5088 static int ca0132_alt_select_in(struct hda_codec *codec)
5089 {
5090 	struct ca0132_spec *spec = codec->spec;
5091 	unsigned int tmp;
5092 
5093 	codec_dbg(codec, "%s\n", __func__);
5094 
5095 	snd_hda_power_up_pm(codec);
5096 
5097 	chipio_set_stream_control(codec, 0x03, 0);
5098 	chipio_set_stream_control(codec, 0x04, 0);
5099 
5100 	spec->cur_mic_type = spec->in_enum_val;
5101 
5102 	switch (spec->cur_mic_type) {
5103 	case REAR_MIC:
5104 		switch (ca0132_quirk(spec)) {
5105 		case QUIRK_SBZ:
5106 		case QUIRK_R3D:
5107 			ca0113_mmio_gpio_set(codec, 0, false);
5108 			tmp = FLOAT_THREE;
5109 			break;
5110 		case QUIRK_ZXR:
5111 			tmp = FLOAT_THREE;
5112 			break;
5113 		case QUIRK_R3DI:
5114 			r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
5115 			tmp = FLOAT_ONE;
5116 			break;
5117 		case QUIRK_AE5:
5118 			ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
5119 			tmp = FLOAT_THREE;
5120 			break;
5121 		case QUIRK_AE7:
5122 			ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
5123 			tmp = FLOAT_THREE;
5124 			chipio_set_conn_rate(codec, MEM_CONNID_MICIN2,
5125 					SR_96_000);
5126 			chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2,
5127 					SR_96_000);
5128 			dspio_set_uint_param(codec, 0x80, 0x01, FLOAT_ZERO);
5129 			break;
5130 		default:
5131 			tmp = FLOAT_ONE;
5132 			break;
5133 		}
5134 
5135 		chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
5136 		chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
5137 		if (ca0132_quirk(spec) == QUIRK_R3DI)
5138 			chipio_set_conn_rate(codec, 0x0F, SR_96_000);
5139 
5140 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5141 
5142 		chipio_set_stream_control(codec, 0x03, 1);
5143 		chipio_set_stream_control(codec, 0x04, 1);
5144 		switch (ca0132_quirk(spec)) {
5145 		case QUIRK_SBZ:
5146 			chipio_write(codec, 0x18B098, 0x0000000C);
5147 			chipio_write(codec, 0x18B09C, 0x0000000C);
5148 			break;
5149 		case QUIRK_ZXR:
5150 			chipio_write(codec, 0x18B098, 0x0000000C);
5151 			chipio_write(codec, 0x18B09C, 0x000000CC);
5152 			break;
5153 		case QUIRK_AE5:
5154 			chipio_write(codec, 0x18B098, 0x0000000C);
5155 			chipio_write(codec, 0x18B09C, 0x0000004C);
5156 			break;
5157 		default:
5158 			break;
5159 		}
5160 		ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
5161 		break;
5162 	case REAR_LINE_IN:
5163 		ca0132_mic_boost_set(codec, 0);
5164 		switch (ca0132_quirk(spec)) {
5165 		case QUIRK_SBZ:
5166 		case QUIRK_R3D:
5167 			ca0113_mmio_gpio_set(codec, 0, false);
5168 			break;
5169 		case QUIRK_R3DI:
5170 			r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
5171 			break;
5172 		case QUIRK_AE5:
5173 			ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
5174 			break;
5175 		case QUIRK_AE7:
5176 			ca0113_mmio_command_set(codec, 0x30, 0x28, 0x3f);
5177 			chipio_set_conn_rate(codec, MEM_CONNID_MICIN2,
5178 					SR_96_000);
5179 			chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2,
5180 					SR_96_000);
5181 			dspio_set_uint_param(codec, 0x80, 0x01, FLOAT_ZERO);
5182 			break;
5183 		default:
5184 			break;
5185 		}
5186 
5187 		chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
5188 		chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
5189 		if (ca0132_quirk(spec) == QUIRK_R3DI)
5190 			chipio_set_conn_rate(codec, 0x0F, SR_96_000);
5191 
5192 		if (ca0132_quirk(spec) == QUIRK_AE7)
5193 			tmp = FLOAT_THREE;
5194 		else
5195 			tmp = FLOAT_ZERO;
5196 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5197 
5198 		switch (ca0132_quirk(spec)) {
5199 		case QUIRK_SBZ:
5200 		case QUIRK_AE5:
5201 			chipio_write(codec, 0x18B098, 0x00000000);
5202 			chipio_write(codec, 0x18B09C, 0x00000000);
5203 			break;
5204 		default:
5205 			break;
5206 		}
5207 		chipio_set_stream_control(codec, 0x03, 1);
5208 		chipio_set_stream_control(codec, 0x04, 1);
5209 		break;
5210 	case FRONT_MIC:
5211 		switch (ca0132_quirk(spec)) {
5212 		case QUIRK_SBZ:
5213 		case QUIRK_R3D:
5214 			ca0113_mmio_gpio_set(codec, 0, true);
5215 			ca0113_mmio_gpio_set(codec, 5, false);
5216 			tmp = FLOAT_THREE;
5217 			break;
5218 		case QUIRK_R3DI:
5219 			r3di_gpio_mic_set(codec, R3DI_FRONT_MIC);
5220 			tmp = FLOAT_ONE;
5221 			break;
5222 		case QUIRK_AE5:
5223 			ca0113_mmio_command_set(codec, 0x30, 0x28, 0x3f);
5224 			tmp = FLOAT_THREE;
5225 			break;
5226 		default:
5227 			tmp = FLOAT_ONE;
5228 			break;
5229 		}
5230 
5231 		chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
5232 		chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
5233 		if (ca0132_quirk(spec) == QUIRK_R3DI)
5234 			chipio_set_conn_rate(codec, 0x0F, SR_96_000);
5235 
5236 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5237 
5238 		chipio_set_stream_control(codec, 0x03, 1);
5239 		chipio_set_stream_control(codec, 0x04, 1);
5240 
5241 		switch (ca0132_quirk(spec)) {
5242 		case QUIRK_SBZ:
5243 			chipio_write(codec, 0x18B098, 0x0000000C);
5244 			chipio_write(codec, 0x18B09C, 0x000000CC);
5245 			break;
5246 		case QUIRK_AE5:
5247 			chipio_write(codec, 0x18B098, 0x0000000C);
5248 			chipio_write(codec, 0x18B09C, 0x0000004C);
5249 			break;
5250 		default:
5251 			break;
5252 		}
5253 		ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
5254 		break;
5255 	}
5256 	ca0132_cvoice_switch_set(codec);
5257 
5258 	snd_hda_power_down_pm(codec);
5259 	return 0;
5260 }
5261 
5262 /*
5263  * Check if VNODE settings take effect immediately.
5264  */
5265 static bool ca0132_is_vnode_effective(struct hda_codec *codec,
5266 				     hda_nid_t vnid,
5267 				     hda_nid_t *shared_nid)
5268 {
5269 	struct ca0132_spec *spec = codec->spec;
5270 	hda_nid_t nid;
5271 
5272 	switch (vnid) {
5273 	case VNID_SPK:
5274 		nid = spec->shared_out_nid;
5275 		break;
5276 	case VNID_MIC:
5277 		nid = spec->shared_mic_nid;
5278 		break;
5279 	default:
5280 		return false;
5281 	}
5282 
5283 	if (shared_nid)
5284 		*shared_nid = nid;
5285 
5286 	return true;
5287 }
5288 
5289 /*
5290 * The following functions are control change helpers.
5291 * They return 0 if no changed.  Return 1 if changed.
5292 */
5293 static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
5294 {
5295 	struct ca0132_spec *spec = codec->spec;
5296 	unsigned int tmp;
5297 
5298 	/* based on CrystalVoice state to enable VoiceFX. */
5299 	if (enable) {
5300 		tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
5301 			FLOAT_ONE : FLOAT_ZERO;
5302 	} else {
5303 		tmp = FLOAT_ZERO;
5304 	}
5305 
5306 	dspio_set_uint_param(codec, ca0132_voicefx.mid,
5307 			     ca0132_voicefx.reqs[0], tmp);
5308 
5309 	return 1;
5310 }
5311 
5312 /*
5313  * Set the effects parameters
5314  */
5315 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
5316 {
5317 	struct ca0132_spec *spec = codec->spec;
5318 	unsigned int on, tmp, channel_cfg;
5319 	int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
5320 	int err = 0;
5321 	int idx = nid - EFFECT_START_NID;
5322 
5323 	if ((idx < 0) || (idx >= num_fx))
5324 		return 0; /* no changed */
5325 
5326 	/* for out effect, qualify with PE */
5327 	if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
5328 		/* if PE if off, turn off out effects. */
5329 		if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
5330 			val = 0;
5331 		if (spec->cur_out_type == SPEAKER_OUT && nid == X_BASS) {
5332 			channel_cfg = spec->channel_cfg_val;
5333 			if (channel_cfg != SPEAKER_CHANNELS_2_0 &&
5334 					channel_cfg != SPEAKER_CHANNELS_4_0)
5335 				val = 0;
5336 		}
5337 	}
5338 
5339 	/* for in effect, qualify with CrystalVoice */
5340 	if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
5341 		/* if CrystalVoice if off, turn off in effects. */
5342 		if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
5343 			val = 0;
5344 
5345 		/* Voice Focus applies to 2-ch Mic, Digital Mic */
5346 		if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
5347 			val = 0;
5348 
5349 		/* If Voice Focus on SBZ, set to two channel. */
5350 		if ((nid == VOICE_FOCUS) && ca0132_use_pci_mmio(spec)
5351 				&& (spec->cur_mic_type != REAR_LINE_IN)) {
5352 			if (spec->effects_switch[CRYSTAL_VOICE -
5353 						 EFFECT_START_NID]) {
5354 
5355 				if (spec->effects_switch[VOICE_FOCUS -
5356 							 EFFECT_START_NID]) {
5357 					tmp = FLOAT_TWO;
5358 					val = 1;
5359 				} else
5360 					tmp = FLOAT_ONE;
5361 
5362 				dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5363 			}
5364 		}
5365 		/*
5366 		 * For SBZ noise reduction, there's an extra command
5367 		 * to module ID 0x47. No clue why.
5368 		 */
5369 		if ((nid == NOISE_REDUCTION) && ca0132_use_pci_mmio(spec)
5370 				&& (spec->cur_mic_type != REAR_LINE_IN)) {
5371 			if (spec->effects_switch[CRYSTAL_VOICE -
5372 						 EFFECT_START_NID]) {
5373 				if (spec->effects_switch[NOISE_REDUCTION -
5374 							 EFFECT_START_NID])
5375 					tmp = FLOAT_ONE;
5376 				else
5377 					tmp = FLOAT_ZERO;
5378 			} else
5379 				tmp = FLOAT_ZERO;
5380 
5381 			dspio_set_uint_param(codec, 0x47, 0x00, tmp);
5382 		}
5383 
5384 		/* If rear line in disable effects. */
5385 		if (ca0132_use_alt_functions(spec) &&
5386 				spec->in_enum_val == REAR_LINE_IN)
5387 			val = 0;
5388 	}
5389 
5390 	codec_dbg(codec, "ca0132_effect_set: nid=0x%x, val=%ld\n",
5391 		    nid, val);
5392 
5393 	on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
5394 	err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
5395 				   ca0132_effects[idx].reqs[0], on);
5396 
5397 	if (err < 0)
5398 		return 0; /* no changed */
5399 
5400 	return 1;
5401 }
5402 
5403 /*
5404  * Turn on/off Playback Enhancements
5405  */
5406 static int ca0132_pe_switch_set(struct hda_codec *codec)
5407 {
5408 	struct ca0132_spec *spec = codec->spec;
5409 	hda_nid_t nid;
5410 	int i, ret = 0;
5411 
5412 	codec_dbg(codec, "ca0132_pe_switch_set: val=%ld\n",
5413 		    spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
5414 
5415 	if (ca0132_use_alt_functions(spec))
5416 		ca0132_alt_select_out(codec);
5417 
5418 	i = OUT_EFFECT_START_NID - EFFECT_START_NID;
5419 	nid = OUT_EFFECT_START_NID;
5420 	/* PE affects all out effects */
5421 	for (; nid < OUT_EFFECT_END_NID; nid++, i++)
5422 		ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
5423 
5424 	return ret;
5425 }
5426 
5427 /* Check if Mic1 is streaming, if so, stop streaming */
5428 static int stop_mic1(struct hda_codec *codec)
5429 {
5430 	struct ca0132_spec *spec = codec->spec;
5431 	unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
5432 						 AC_VERB_GET_CONV, 0);
5433 	if (oldval != 0)
5434 		snd_hda_codec_write(codec, spec->adcs[0], 0,
5435 				    AC_VERB_SET_CHANNEL_STREAMID,
5436 				    0);
5437 	return oldval;
5438 }
5439 
5440 /* Resume Mic1 streaming if it was stopped. */
5441 static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
5442 {
5443 	struct ca0132_spec *spec = codec->spec;
5444 	/* Restore the previous stream and channel */
5445 	if (oldval != 0)
5446 		snd_hda_codec_write(codec, spec->adcs[0], 0,
5447 				    AC_VERB_SET_CHANNEL_STREAMID,
5448 				    oldval);
5449 }
5450 
5451 /*
5452  * Turn on/off CrystalVoice
5453  */
5454 static int ca0132_cvoice_switch_set(struct hda_codec *codec)
5455 {
5456 	struct ca0132_spec *spec = codec->spec;
5457 	hda_nid_t nid;
5458 	int i, ret = 0;
5459 	unsigned int oldval;
5460 
5461 	codec_dbg(codec, "ca0132_cvoice_switch_set: val=%ld\n",
5462 		    spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
5463 
5464 	i = IN_EFFECT_START_NID - EFFECT_START_NID;
5465 	nid = IN_EFFECT_START_NID;
5466 	/* CrystalVoice affects all in effects */
5467 	for (; nid < IN_EFFECT_END_NID; nid++, i++)
5468 		ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
5469 
5470 	/* including VoiceFX */
5471 	ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
5472 
5473 	/* set correct vipsource */
5474 	oldval = stop_mic1(codec);
5475 	if (ca0132_use_alt_functions(spec))
5476 		ret |= ca0132_alt_set_vipsource(codec, 1);
5477 	else
5478 		ret |= ca0132_set_vipsource(codec, 1);
5479 	resume_mic1(codec, oldval);
5480 	return ret;
5481 }
5482 
5483 static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
5484 {
5485 	struct ca0132_spec *spec = codec->spec;
5486 	int ret = 0;
5487 
5488 	if (val) /* on */
5489 		ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
5490 					HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
5491 	else /* off */
5492 		ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
5493 					HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
5494 
5495 	return ret;
5496 }
5497 
5498 static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val)
5499 {
5500 	struct ca0132_spec *spec = codec->spec;
5501 	int ret = 0;
5502 
5503 	ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
5504 				HDA_INPUT, 0, HDA_AMP_VOLMASK, val);
5505 	return ret;
5506 }
5507 
5508 static int ae5_headphone_gain_set(struct hda_codec *codec, long val)
5509 {
5510 	unsigned int i;
5511 
5512 	for (i = 0; i < 4; i++)
5513 		ca0113_mmio_command_set(codec, 0x48, 0x11 + i,
5514 				ae5_headphone_gain_presets[val].vals[i]);
5515 	return 0;
5516 }
5517 
5518 /*
5519  * gpio pin 1 is a relay that switches on/off, apparently setting the headphone
5520  * amplifier to handle a 600 ohm load.
5521  */
5522 static int zxr_headphone_gain_set(struct hda_codec *codec, long val)
5523 {
5524 	ca0113_mmio_gpio_set(codec, 1, val);
5525 
5526 	return 0;
5527 }
5528 
5529 static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
5530 				struct snd_ctl_elem_value *ucontrol)
5531 {
5532 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5533 	hda_nid_t nid = get_amp_nid(kcontrol);
5534 	hda_nid_t shared_nid = 0;
5535 	bool effective;
5536 	int ret = 0;
5537 	struct ca0132_spec *spec = codec->spec;
5538 	int auto_jack;
5539 
5540 	if (nid == VNID_HP_SEL) {
5541 		auto_jack =
5542 			spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
5543 		if (!auto_jack) {
5544 			if (ca0132_use_alt_functions(spec))
5545 				ca0132_alt_select_out(codec);
5546 			else
5547 				ca0132_select_out(codec);
5548 		}
5549 		return 1;
5550 	}
5551 
5552 	if (nid == VNID_AMIC1_SEL) {
5553 		auto_jack =
5554 			spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
5555 		if (!auto_jack)
5556 			ca0132_select_mic(codec);
5557 		return 1;
5558 	}
5559 
5560 	if (nid == VNID_HP_ASEL) {
5561 		if (ca0132_use_alt_functions(spec))
5562 			ca0132_alt_select_out(codec);
5563 		else
5564 			ca0132_select_out(codec);
5565 		return 1;
5566 	}
5567 
5568 	if (nid == VNID_AMIC1_ASEL) {
5569 		ca0132_select_mic(codec);
5570 		return 1;
5571 	}
5572 
5573 	/* if effective conditions, then update hw immediately. */
5574 	effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
5575 	if (effective) {
5576 		int dir = get_amp_direction(kcontrol);
5577 		int ch = get_amp_channels(kcontrol);
5578 		unsigned long pval;
5579 
5580 		mutex_lock(&codec->control_mutex);
5581 		pval = kcontrol->private_value;
5582 		kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
5583 								0, dir);
5584 		ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
5585 		kcontrol->private_value = pval;
5586 		mutex_unlock(&codec->control_mutex);
5587 	}
5588 
5589 	return ret;
5590 }
5591 /* End of control change helpers. */
5592 
5593 static void ca0132_alt_bass_redirection_xover_set(struct hda_codec *codec,
5594 		long idx)
5595 {
5596 	snd_hda_power_up(codec);
5597 
5598 	dspio_set_param(codec, 0x96, 0x20, SPEAKER_BASS_REDIRECT_XOVER_FREQ,
5599 			&(float_xbass_xover_lookup[idx]), sizeof(unsigned int));
5600 
5601 	snd_hda_power_down(codec);
5602 }
5603 
5604 /*
5605  * Below I've added controls to mess with the effect levels, I've only enabled
5606  * them on the Sound Blaster Z, but they would probably also work on the
5607  * Chromebook. I figured they were probably tuned specifically for it, and left
5608  * out for a reason.
5609  */
5610 
5611 /* Sets DSP effect level from the sliders above the controls */
5612 
5613 static int ca0132_alt_slider_ctl_set(struct hda_codec *codec, hda_nid_t nid,
5614 			  const unsigned int *lookup, int idx)
5615 {
5616 	int i = 0;
5617 	unsigned int y;
5618 	/*
5619 	 * For X_BASS, req 2 is actually crossover freq instead of
5620 	 * effect level
5621 	 */
5622 	if (nid == X_BASS)
5623 		y = 2;
5624 	else
5625 		y = 1;
5626 
5627 	snd_hda_power_up(codec);
5628 	if (nid == XBASS_XOVER) {
5629 		for (i = 0; i < OUT_EFFECTS_COUNT; i++)
5630 			if (ca0132_effects[i].nid == X_BASS)
5631 				break;
5632 
5633 		dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
5634 				ca0132_effects[i].reqs[1],
5635 				&(lookup[idx - 1]), sizeof(unsigned int));
5636 	} else {
5637 		/* Find the actual effect structure */
5638 		for (i = 0; i < OUT_EFFECTS_COUNT; i++)
5639 			if (nid == ca0132_effects[i].nid)
5640 				break;
5641 
5642 		dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
5643 				ca0132_effects[i].reqs[y],
5644 				&(lookup[idx]), sizeof(unsigned int));
5645 	}
5646 
5647 	snd_hda_power_down(codec);
5648 
5649 	return 0;
5650 }
5651 
5652 static int ca0132_alt_xbass_xover_slider_ctl_get(struct snd_kcontrol *kcontrol,
5653 			  struct snd_ctl_elem_value *ucontrol)
5654 {
5655 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5656 	struct ca0132_spec *spec = codec->spec;
5657 	long *valp = ucontrol->value.integer.value;
5658 	hda_nid_t nid = get_amp_nid(kcontrol);
5659 
5660 	if (nid == BASS_REDIRECTION_XOVER)
5661 		*valp = spec->bass_redirect_xover_freq;
5662 	else
5663 		*valp = spec->xbass_xover_freq;
5664 
5665 	return 0;
5666 }
5667 
5668 static int ca0132_alt_slider_ctl_get(struct snd_kcontrol *kcontrol,
5669 			  struct snd_ctl_elem_value *ucontrol)
5670 {
5671 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5672 	struct ca0132_spec *spec = codec->spec;
5673 	hda_nid_t nid = get_amp_nid(kcontrol);
5674 	long *valp = ucontrol->value.integer.value;
5675 	int idx = nid - OUT_EFFECT_START_NID;
5676 
5677 	*valp = spec->fx_ctl_val[idx];
5678 	return 0;
5679 }
5680 
5681 /*
5682  * The X-bass crossover starts at 10hz, so the min is 1. The
5683  * frequency is set in multiples of 10.
5684  */
5685 static int ca0132_alt_xbass_xover_slider_info(struct snd_kcontrol *kcontrol,
5686 		struct snd_ctl_elem_info *uinfo)
5687 {
5688 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5689 	uinfo->count = 1;
5690 	uinfo->value.integer.min = 1;
5691 	uinfo->value.integer.max = 100;
5692 	uinfo->value.integer.step = 1;
5693 
5694 	return 0;
5695 }
5696 
5697 static int ca0132_alt_effect_slider_info(struct snd_kcontrol *kcontrol,
5698 		struct snd_ctl_elem_info *uinfo)
5699 {
5700 	int chs = get_amp_channels(kcontrol);
5701 
5702 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5703 	uinfo->count = chs == 3 ? 2 : 1;
5704 	uinfo->value.integer.min = 0;
5705 	uinfo->value.integer.max = 100;
5706 	uinfo->value.integer.step = 1;
5707 
5708 	return 0;
5709 }
5710 
5711 static int ca0132_alt_xbass_xover_slider_put(struct snd_kcontrol *kcontrol,
5712 				struct snd_ctl_elem_value *ucontrol)
5713 {
5714 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5715 	struct ca0132_spec *spec = codec->spec;
5716 	hda_nid_t nid = get_amp_nid(kcontrol);
5717 	long *valp = ucontrol->value.integer.value;
5718 	long *cur_val;
5719 	int idx;
5720 
5721 	if (nid == BASS_REDIRECTION_XOVER)
5722 		cur_val = &spec->bass_redirect_xover_freq;
5723 	else
5724 		cur_val = &spec->xbass_xover_freq;
5725 
5726 	/* any change? */
5727 	if (*cur_val == *valp)
5728 		return 0;
5729 
5730 	*cur_val = *valp;
5731 
5732 	idx = *valp;
5733 	if (nid == BASS_REDIRECTION_XOVER)
5734 		ca0132_alt_bass_redirection_xover_set(codec, *cur_val);
5735 	else
5736 		ca0132_alt_slider_ctl_set(codec, nid, float_xbass_xover_lookup, idx);
5737 
5738 	return 0;
5739 }
5740 
5741 static int ca0132_alt_effect_slider_put(struct snd_kcontrol *kcontrol,
5742 				struct snd_ctl_elem_value *ucontrol)
5743 {
5744 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5745 	struct ca0132_spec *spec = codec->spec;
5746 	hda_nid_t nid = get_amp_nid(kcontrol);
5747 	long *valp = ucontrol->value.integer.value;
5748 	int idx;
5749 
5750 	idx = nid - EFFECT_START_NID;
5751 	/* any change? */
5752 	if (spec->fx_ctl_val[idx] == *valp)
5753 		return 0;
5754 
5755 	spec->fx_ctl_val[idx] = *valp;
5756 
5757 	idx = *valp;
5758 	ca0132_alt_slider_ctl_set(codec, nid, float_zero_to_one_lookup, idx);
5759 
5760 	return 0;
5761 }
5762 
5763 
5764 /*
5765  * Mic Boost Enum for alternative ca0132 codecs. I didn't like that the original
5766  * only has off or full 30 dB, and didn't like making a volume slider that has
5767  * traditional 0-100 in alsamixer that goes in big steps. I like enum better.
5768  */
5769 #define MIC_BOOST_NUM_OF_STEPS 4
5770 #define MIC_BOOST_ENUM_MAX_STRLEN 10
5771 
5772 static int ca0132_alt_mic_boost_info(struct snd_kcontrol *kcontrol,
5773 				 struct snd_ctl_elem_info *uinfo)
5774 {
5775 	char *sfx = "dB";
5776 	char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5777 
5778 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5779 	uinfo->count = 1;
5780 	uinfo->value.enumerated.items = MIC_BOOST_NUM_OF_STEPS;
5781 	if (uinfo->value.enumerated.item >= MIC_BOOST_NUM_OF_STEPS)
5782 		uinfo->value.enumerated.item = MIC_BOOST_NUM_OF_STEPS - 1;
5783 	sprintf(namestr, "%d %s", (uinfo->value.enumerated.item * 10), sfx);
5784 	strcpy(uinfo->value.enumerated.name, namestr);
5785 	return 0;
5786 }
5787 
5788 static int ca0132_alt_mic_boost_get(struct snd_kcontrol *kcontrol,
5789 				struct snd_ctl_elem_value *ucontrol)
5790 {
5791 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5792 	struct ca0132_spec *spec = codec->spec;
5793 
5794 	ucontrol->value.enumerated.item[0] = spec->mic_boost_enum_val;
5795 	return 0;
5796 }
5797 
5798 static int ca0132_alt_mic_boost_put(struct snd_kcontrol *kcontrol,
5799 				struct snd_ctl_elem_value *ucontrol)
5800 {
5801 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5802 	struct ca0132_spec *spec = codec->spec;
5803 	int sel = ucontrol->value.enumerated.item[0];
5804 	unsigned int items = MIC_BOOST_NUM_OF_STEPS;
5805 
5806 	if (sel >= items)
5807 		return 0;
5808 
5809 	codec_dbg(codec, "ca0132_alt_mic_boost: boost=%d\n",
5810 		    sel);
5811 
5812 	spec->mic_boost_enum_val = sel;
5813 
5814 	if (spec->in_enum_val != REAR_LINE_IN)
5815 		ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
5816 
5817 	return 1;
5818 }
5819 
5820 /*
5821  * Sound BlasterX AE-5 Headphone Gain Controls.
5822  */
5823 #define AE5_HEADPHONE_GAIN_MAX 3
5824 static int ae5_headphone_gain_info(struct snd_kcontrol *kcontrol,
5825 				 struct snd_ctl_elem_info *uinfo)
5826 {
5827 	char *sfx = " Ohms)";
5828 	char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5829 
5830 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5831 	uinfo->count = 1;
5832 	uinfo->value.enumerated.items = AE5_HEADPHONE_GAIN_MAX;
5833 	if (uinfo->value.enumerated.item >= AE5_HEADPHONE_GAIN_MAX)
5834 		uinfo->value.enumerated.item = AE5_HEADPHONE_GAIN_MAX - 1;
5835 	sprintf(namestr, "%s %s",
5836 		ae5_headphone_gain_presets[uinfo->value.enumerated.item].name,
5837 		sfx);
5838 	strcpy(uinfo->value.enumerated.name, namestr);
5839 	return 0;
5840 }
5841 
5842 static int ae5_headphone_gain_get(struct snd_kcontrol *kcontrol,
5843 				struct snd_ctl_elem_value *ucontrol)
5844 {
5845 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5846 	struct ca0132_spec *spec = codec->spec;
5847 
5848 	ucontrol->value.enumerated.item[0] = spec->ae5_headphone_gain_val;
5849 	return 0;
5850 }
5851 
5852 static int ae5_headphone_gain_put(struct snd_kcontrol *kcontrol,
5853 				struct snd_ctl_elem_value *ucontrol)
5854 {
5855 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5856 	struct ca0132_spec *spec = codec->spec;
5857 	int sel = ucontrol->value.enumerated.item[0];
5858 	unsigned int items = AE5_HEADPHONE_GAIN_MAX;
5859 
5860 	if (sel >= items)
5861 		return 0;
5862 
5863 	codec_dbg(codec, "ae5_headphone_gain: boost=%d\n",
5864 		    sel);
5865 
5866 	spec->ae5_headphone_gain_val = sel;
5867 
5868 	if (spec->out_enum_val == HEADPHONE_OUT)
5869 		ae5_headphone_gain_set(codec, spec->ae5_headphone_gain_val);
5870 
5871 	return 1;
5872 }
5873 
5874 /*
5875  * Sound BlasterX AE-5 sound filter enumerated control.
5876  */
5877 #define AE5_SOUND_FILTER_MAX 3
5878 
5879 static int ae5_sound_filter_info(struct snd_kcontrol *kcontrol,
5880 				 struct snd_ctl_elem_info *uinfo)
5881 {
5882 	char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5883 
5884 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5885 	uinfo->count = 1;
5886 	uinfo->value.enumerated.items = AE5_SOUND_FILTER_MAX;
5887 	if (uinfo->value.enumerated.item >= AE5_SOUND_FILTER_MAX)
5888 		uinfo->value.enumerated.item = AE5_SOUND_FILTER_MAX - 1;
5889 	sprintf(namestr, "%s",
5890 			ae5_filter_presets[uinfo->value.enumerated.item].name);
5891 	strcpy(uinfo->value.enumerated.name, namestr);
5892 	return 0;
5893 }
5894 
5895 static int ae5_sound_filter_get(struct snd_kcontrol *kcontrol,
5896 				struct snd_ctl_elem_value *ucontrol)
5897 {
5898 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5899 	struct ca0132_spec *spec = codec->spec;
5900 
5901 	ucontrol->value.enumerated.item[0] = spec->ae5_filter_val;
5902 	return 0;
5903 }
5904 
5905 static int ae5_sound_filter_put(struct snd_kcontrol *kcontrol,
5906 				struct snd_ctl_elem_value *ucontrol)
5907 {
5908 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5909 	struct ca0132_spec *spec = codec->spec;
5910 	int sel = ucontrol->value.enumerated.item[0];
5911 	unsigned int items = AE5_SOUND_FILTER_MAX;
5912 
5913 	if (sel >= items)
5914 		return 0;
5915 
5916 	codec_dbg(codec, "ae5_sound_filter: %s\n",
5917 			ae5_filter_presets[sel].name);
5918 
5919 	spec->ae5_filter_val = sel;
5920 
5921 	ca0113_mmio_command_set_type2(codec, 0x48, 0x07,
5922 			ae5_filter_presets[sel].val);
5923 
5924 	return 1;
5925 }
5926 
5927 /*
5928  * Input Select Control for alternative ca0132 codecs. This exists because
5929  * front microphone has no auto-detect, and we need a way to set the rear
5930  * as line-in
5931  */
5932 static int ca0132_alt_input_source_info(struct snd_kcontrol *kcontrol,
5933 				 struct snd_ctl_elem_info *uinfo)
5934 {
5935 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5936 	uinfo->count = 1;
5937 	uinfo->value.enumerated.items = IN_SRC_NUM_OF_INPUTS;
5938 	if (uinfo->value.enumerated.item >= IN_SRC_NUM_OF_INPUTS)
5939 		uinfo->value.enumerated.item = IN_SRC_NUM_OF_INPUTS - 1;
5940 	strcpy(uinfo->value.enumerated.name,
5941 			in_src_str[uinfo->value.enumerated.item]);
5942 	return 0;
5943 }
5944 
5945 static int ca0132_alt_input_source_get(struct snd_kcontrol *kcontrol,
5946 				struct snd_ctl_elem_value *ucontrol)
5947 {
5948 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5949 	struct ca0132_spec *spec = codec->spec;
5950 
5951 	ucontrol->value.enumerated.item[0] = spec->in_enum_val;
5952 	return 0;
5953 }
5954 
5955 static int ca0132_alt_input_source_put(struct snd_kcontrol *kcontrol,
5956 				struct snd_ctl_elem_value *ucontrol)
5957 {
5958 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5959 	struct ca0132_spec *spec = codec->spec;
5960 	int sel = ucontrol->value.enumerated.item[0];
5961 	unsigned int items = IN_SRC_NUM_OF_INPUTS;
5962 
5963 	/*
5964 	 * The AE-7 has no front microphone, so limit items to 2: rear mic and
5965 	 * line-in.
5966 	 */
5967 	if (ca0132_quirk(spec) == QUIRK_AE7)
5968 		items = 2;
5969 
5970 	if (sel >= items)
5971 		return 0;
5972 
5973 	codec_dbg(codec, "ca0132_alt_input_select: sel=%d, preset=%s\n",
5974 		    sel, in_src_str[sel]);
5975 
5976 	spec->in_enum_val = sel;
5977 
5978 	ca0132_alt_select_in(codec);
5979 
5980 	return 1;
5981 }
5982 
5983 /* Sound Blaster Z Output Select Control */
5984 static int ca0132_alt_output_select_get_info(struct snd_kcontrol *kcontrol,
5985 				 struct snd_ctl_elem_info *uinfo)
5986 {
5987 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5988 	uinfo->count = 1;
5989 	uinfo->value.enumerated.items = NUM_OF_OUTPUTS;
5990 	if (uinfo->value.enumerated.item >= NUM_OF_OUTPUTS)
5991 		uinfo->value.enumerated.item = NUM_OF_OUTPUTS - 1;
5992 	strcpy(uinfo->value.enumerated.name,
5993 			out_type_str[uinfo->value.enumerated.item]);
5994 	return 0;
5995 }
5996 
5997 static int ca0132_alt_output_select_get(struct snd_kcontrol *kcontrol,
5998 				struct snd_ctl_elem_value *ucontrol)
5999 {
6000 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6001 	struct ca0132_spec *spec = codec->spec;
6002 
6003 	ucontrol->value.enumerated.item[0] = spec->out_enum_val;
6004 	return 0;
6005 }
6006 
6007 static int ca0132_alt_output_select_put(struct snd_kcontrol *kcontrol,
6008 				struct snd_ctl_elem_value *ucontrol)
6009 {
6010 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6011 	struct ca0132_spec *spec = codec->spec;
6012 	int sel = ucontrol->value.enumerated.item[0];
6013 	unsigned int items = NUM_OF_OUTPUTS;
6014 	unsigned int auto_jack;
6015 
6016 	if (sel >= items)
6017 		return 0;
6018 
6019 	codec_dbg(codec, "ca0132_alt_output_select: sel=%d, preset=%s\n",
6020 		    sel, out_type_str[sel]);
6021 
6022 	spec->out_enum_val = sel;
6023 
6024 	auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
6025 
6026 	if (!auto_jack)
6027 		ca0132_alt_select_out(codec);
6028 
6029 	return 1;
6030 }
6031 
6032 /* Select surround output type: 2.1, 4.0, 4.1, or 5.1. */
6033 static int ca0132_alt_speaker_channel_cfg_get_info(struct snd_kcontrol *kcontrol,
6034 				 struct snd_ctl_elem_info *uinfo)
6035 {
6036 	unsigned int items = SPEAKER_CHANNEL_CFG_COUNT;
6037 
6038 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6039 	uinfo->count = 1;
6040 	uinfo->value.enumerated.items = items;
6041 	if (uinfo->value.enumerated.item >= items)
6042 		uinfo->value.enumerated.item = items - 1;
6043 	strcpy(uinfo->value.enumerated.name,
6044 			speaker_channel_cfgs[uinfo->value.enumerated.item].name);
6045 	return 0;
6046 }
6047 
6048 static int ca0132_alt_speaker_channel_cfg_get(struct snd_kcontrol *kcontrol,
6049 				struct snd_ctl_elem_value *ucontrol)
6050 {
6051 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6052 	struct ca0132_spec *spec = codec->spec;
6053 
6054 	ucontrol->value.enumerated.item[0] = spec->channel_cfg_val;
6055 	return 0;
6056 }
6057 
6058 static int ca0132_alt_speaker_channel_cfg_put(struct snd_kcontrol *kcontrol,
6059 				struct snd_ctl_elem_value *ucontrol)
6060 {
6061 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6062 	struct ca0132_spec *spec = codec->spec;
6063 	int sel = ucontrol->value.enumerated.item[0];
6064 	unsigned int items = SPEAKER_CHANNEL_CFG_COUNT;
6065 
6066 	if (sel >= items)
6067 		return 0;
6068 
6069 	codec_dbg(codec, "ca0132_alt_speaker_channels: sel=%d, channels=%s\n",
6070 		    sel, speaker_channel_cfgs[sel].name);
6071 
6072 	spec->channel_cfg_val = sel;
6073 
6074 	if (spec->out_enum_val == SPEAKER_OUT)
6075 		ca0132_alt_select_out(codec);
6076 
6077 	return 1;
6078 }
6079 
6080 /*
6081  * Smart Volume output setting control. Three different settings, Normal,
6082  * which takes the value from the smart volume slider. The two others, loud
6083  * and night, disregard the slider value and have uneditable values.
6084  */
6085 #define NUM_OF_SVM_SETTINGS 3
6086 static const char *const out_svm_set_enum_str[3] = {"Normal", "Loud", "Night" };
6087 
6088 static int ca0132_alt_svm_setting_info(struct snd_kcontrol *kcontrol,
6089 				 struct snd_ctl_elem_info *uinfo)
6090 {
6091 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6092 	uinfo->count = 1;
6093 	uinfo->value.enumerated.items = NUM_OF_SVM_SETTINGS;
6094 	if (uinfo->value.enumerated.item >= NUM_OF_SVM_SETTINGS)
6095 		uinfo->value.enumerated.item = NUM_OF_SVM_SETTINGS - 1;
6096 	strcpy(uinfo->value.enumerated.name,
6097 			out_svm_set_enum_str[uinfo->value.enumerated.item]);
6098 	return 0;
6099 }
6100 
6101 static int ca0132_alt_svm_setting_get(struct snd_kcontrol *kcontrol,
6102 				struct snd_ctl_elem_value *ucontrol)
6103 {
6104 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6105 	struct ca0132_spec *spec = codec->spec;
6106 
6107 	ucontrol->value.enumerated.item[0] = spec->smart_volume_setting;
6108 	return 0;
6109 }
6110 
6111 static int ca0132_alt_svm_setting_put(struct snd_kcontrol *kcontrol,
6112 				struct snd_ctl_elem_value *ucontrol)
6113 {
6114 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6115 	struct ca0132_spec *spec = codec->spec;
6116 	int sel = ucontrol->value.enumerated.item[0];
6117 	unsigned int items = NUM_OF_SVM_SETTINGS;
6118 	unsigned int idx = SMART_VOLUME - EFFECT_START_NID;
6119 	unsigned int tmp;
6120 
6121 	if (sel >= items)
6122 		return 0;
6123 
6124 	codec_dbg(codec, "ca0132_alt_svm_setting: sel=%d, preset=%s\n",
6125 		    sel, out_svm_set_enum_str[sel]);
6126 
6127 	spec->smart_volume_setting = sel;
6128 
6129 	switch (sel) {
6130 	case 0:
6131 		tmp = FLOAT_ZERO;
6132 		break;
6133 	case 1:
6134 		tmp = FLOAT_ONE;
6135 		break;
6136 	case 2:
6137 		tmp = FLOAT_TWO;
6138 		break;
6139 	default:
6140 		tmp = FLOAT_ZERO;
6141 		break;
6142 	}
6143 	/* Req 2 is the Smart Volume Setting req. */
6144 	dspio_set_uint_param(codec, ca0132_effects[idx].mid,
6145 			ca0132_effects[idx].reqs[2], tmp);
6146 	return 1;
6147 }
6148 
6149 /* Sound Blaster Z EQ preset controls */
6150 static int ca0132_alt_eq_preset_info(struct snd_kcontrol *kcontrol,
6151 				 struct snd_ctl_elem_info *uinfo)
6152 {
6153 	unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
6154 
6155 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6156 	uinfo->count = 1;
6157 	uinfo->value.enumerated.items = items;
6158 	if (uinfo->value.enumerated.item >= items)
6159 		uinfo->value.enumerated.item = items - 1;
6160 	strcpy(uinfo->value.enumerated.name,
6161 		ca0132_alt_eq_presets[uinfo->value.enumerated.item].name);
6162 	return 0;
6163 }
6164 
6165 static int ca0132_alt_eq_preset_get(struct snd_kcontrol *kcontrol,
6166 				struct snd_ctl_elem_value *ucontrol)
6167 {
6168 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6169 	struct ca0132_spec *spec = codec->spec;
6170 
6171 	ucontrol->value.enumerated.item[0] = spec->eq_preset_val;
6172 	return 0;
6173 }
6174 
6175 static int ca0132_alt_eq_preset_put(struct snd_kcontrol *kcontrol,
6176 				struct snd_ctl_elem_value *ucontrol)
6177 {
6178 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6179 	struct ca0132_spec *spec = codec->spec;
6180 	int i, err = 0;
6181 	int sel = ucontrol->value.enumerated.item[0];
6182 	unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
6183 
6184 	if (sel >= items)
6185 		return 0;
6186 
6187 	codec_dbg(codec, "%s: sel=%d, preset=%s\n", __func__, sel,
6188 			ca0132_alt_eq_presets[sel].name);
6189 	/*
6190 	 * Idx 0 is default.
6191 	 * Default needs to qualify with CrystalVoice state.
6192 	 */
6193 	for (i = 0; i < EQ_PRESET_MAX_PARAM_COUNT; i++) {
6194 		err = dspio_set_uint_param(codec, ca0132_alt_eq_enum.mid,
6195 				ca0132_alt_eq_enum.reqs[i],
6196 				ca0132_alt_eq_presets[sel].vals[i]);
6197 		if (err < 0)
6198 			break;
6199 	}
6200 
6201 	if (err >= 0)
6202 		spec->eq_preset_val = sel;
6203 
6204 	return 1;
6205 }
6206 
6207 static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
6208 				 struct snd_ctl_elem_info *uinfo)
6209 {
6210 	unsigned int items = ARRAY_SIZE(ca0132_voicefx_presets);
6211 
6212 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6213 	uinfo->count = 1;
6214 	uinfo->value.enumerated.items = items;
6215 	if (uinfo->value.enumerated.item >= items)
6216 		uinfo->value.enumerated.item = items - 1;
6217 	strcpy(uinfo->value.enumerated.name,
6218 	       ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
6219 	return 0;
6220 }
6221 
6222 static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
6223 				struct snd_ctl_elem_value *ucontrol)
6224 {
6225 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6226 	struct ca0132_spec *spec = codec->spec;
6227 
6228 	ucontrol->value.enumerated.item[0] = spec->voicefx_val;
6229 	return 0;
6230 }
6231 
6232 static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
6233 				struct snd_ctl_elem_value *ucontrol)
6234 {
6235 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6236 	struct ca0132_spec *spec = codec->spec;
6237 	int i, err = 0;
6238 	int sel = ucontrol->value.enumerated.item[0];
6239 
6240 	if (sel >= ARRAY_SIZE(ca0132_voicefx_presets))
6241 		return 0;
6242 
6243 	codec_dbg(codec, "ca0132_voicefx_put: sel=%d, preset=%s\n",
6244 		    sel, ca0132_voicefx_presets[sel].name);
6245 
6246 	/*
6247 	 * Idx 0 is default.
6248 	 * Default needs to qualify with CrystalVoice state.
6249 	 */
6250 	for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
6251 		err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
6252 				ca0132_voicefx.reqs[i],
6253 				ca0132_voicefx_presets[sel].vals[i]);
6254 		if (err < 0)
6255 			break;
6256 	}
6257 
6258 	if (err >= 0) {
6259 		spec->voicefx_val = sel;
6260 		/* enable voice fx */
6261 		ca0132_voicefx_set(codec, (sel ? 1 : 0));
6262 	}
6263 
6264 	return 1;
6265 }
6266 
6267 static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
6268 				struct snd_ctl_elem_value *ucontrol)
6269 {
6270 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6271 	struct ca0132_spec *spec = codec->spec;
6272 	hda_nid_t nid = get_amp_nid(kcontrol);
6273 	int ch = get_amp_channels(kcontrol);
6274 	long *valp = ucontrol->value.integer.value;
6275 
6276 	/* vnode */
6277 	if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
6278 		if (ch & 1) {
6279 			*valp = spec->vnode_lswitch[nid - VNODE_START_NID];
6280 			valp++;
6281 		}
6282 		if (ch & 2) {
6283 			*valp = spec->vnode_rswitch[nid - VNODE_START_NID];
6284 			valp++;
6285 		}
6286 		return 0;
6287 	}
6288 
6289 	/* effects, include PE and CrystalVoice */
6290 	if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
6291 		*valp = spec->effects_switch[nid - EFFECT_START_NID];
6292 		return 0;
6293 	}
6294 
6295 	/* mic boost */
6296 	if (nid == spec->input_pins[0]) {
6297 		*valp = spec->cur_mic_boost;
6298 		return 0;
6299 	}
6300 
6301 	if (nid == ZXR_HEADPHONE_GAIN) {
6302 		*valp = spec->zxr_gain_set;
6303 		return 0;
6304 	}
6305 
6306 	if (nid == SPEAKER_FULL_RANGE_FRONT || nid == SPEAKER_FULL_RANGE_REAR) {
6307 		*valp = spec->speaker_range_val[nid - SPEAKER_FULL_RANGE_FRONT];
6308 		return 0;
6309 	}
6310 
6311 	if (nid == BASS_REDIRECTION) {
6312 		*valp = spec->bass_redirection_val;
6313 		return 0;
6314 	}
6315 
6316 	return 0;
6317 }
6318 
6319 static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
6320 			     struct snd_ctl_elem_value *ucontrol)
6321 {
6322 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6323 	struct ca0132_spec *spec = codec->spec;
6324 	hda_nid_t nid = get_amp_nid(kcontrol);
6325 	int ch = get_amp_channels(kcontrol);
6326 	long *valp = ucontrol->value.integer.value;
6327 	int changed = 1;
6328 
6329 	codec_dbg(codec, "ca0132_switch_put: nid=0x%x, val=%ld\n",
6330 		    nid, *valp);
6331 
6332 	snd_hda_power_up(codec);
6333 	/* vnode */
6334 	if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
6335 		if (ch & 1) {
6336 			spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
6337 			valp++;
6338 		}
6339 		if (ch & 2) {
6340 			spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
6341 			valp++;
6342 		}
6343 		changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
6344 		goto exit;
6345 	}
6346 
6347 	/* PE */
6348 	if (nid == PLAY_ENHANCEMENT) {
6349 		spec->effects_switch[nid - EFFECT_START_NID] = *valp;
6350 		changed = ca0132_pe_switch_set(codec);
6351 		goto exit;
6352 	}
6353 
6354 	/* CrystalVoice */
6355 	if (nid == CRYSTAL_VOICE) {
6356 		spec->effects_switch[nid - EFFECT_START_NID] = *valp;
6357 		changed = ca0132_cvoice_switch_set(codec);
6358 		goto exit;
6359 	}
6360 
6361 	/* out and in effects */
6362 	if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
6363 	    ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
6364 		spec->effects_switch[nid - EFFECT_START_NID] = *valp;
6365 		changed = ca0132_effects_set(codec, nid, *valp);
6366 		goto exit;
6367 	}
6368 
6369 	/* mic boost */
6370 	if (nid == spec->input_pins[0]) {
6371 		spec->cur_mic_boost = *valp;
6372 		if (ca0132_use_alt_functions(spec)) {
6373 			if (spec->in_enum_val != REAR_LINE_IN)
6374 				changed = ca0132_mic_boost_set(codec, *valp);
6375 		} else {
6376 			/* Mic boost does not apply to Digital Mic */
6377 			if (spec->cur_mic_type != DIGITAL_MIC)
6378 				changed = ca0132_mic_boost_set(codec, *valp);
6379 		}
6380 
6381 		goto exit;
6382 	}
6383 
6384 	if (nid == ZXR_HEADPHONE_GAIN) {
6385 		spec->zxr_gain_set = *valp;
6386 		if (spec->cur_out_type == HEADPHONE_OUT)
6387 			changed = zxr_headphone_gain_set(codec, *valp);
6388 		else
6389 			changed = 0;
6390 
6391 		goto exit;
6392 	}
6393 
6394 	if (nid == SPEAKER_FULL_RANGE_FRONT || nid == SPEAKER_FULL_RANGE_REAR) {
6395 		spec->speaker_range_val[nid - SPEAKER_FULL_RANGE_FRONT] = *valp;
6396 		if (spec->cur_out_type == SPEAKER_OUT)
6397 			ca0132_alt_set_full_range_speaker(codec);
6398 
6399 		changed = 0;
6400 	}
6401 
6402 	if (nid == BASS_REDIRECTION) {
6403 		spec->bass_redirection_val = *valp;
6404 		if (spec->cur_out_type == SPEAKER_OUT)
6405 			ca0132_alt_surround_set_bass_redirection(codec, *valp);
6406 
6407 		changed = 0;
6408 	}
6409 
6410 exit:
6411 	snd_hda_power_down(codec);
6412 	return changed;
6413 }
6414 
6415 /*
6416  * Volume related
6417  */
6418 /*
6419  * Sets the internal DSP decibel level to match the DAC for output, and the
6420  * ADC for input. Currently only the SBZ sets dsp capture volume level, and
6421  * all alternative codecs set DSP playback volume.
6422  */
6423 static void ca0132_alt_dsp_volume_put(struct hda_codec *codec, hda_nid_t nid)
6424 {
6425 	struct ca0132_spec *spec = codec->spec;
6426 	unsigned int dsp_dir;
6427 	unsigned int lookup_val;
6428 
6429 	if (nid == VNID_SPK)
6430 		dsp_dir = DSP_VOL_OUT;
6431 	else
6432 		dsp_dir = DSP_VOL_IN;
6433 
6434 	lookup_val = spec->vnode_lvol[nid - VNODE_START_NID];
6435 
6436 	dspio_set_uint_param(codec,
6437 		ca0132_alt_vol_ctls[dsp_dir].mid,
6438 		ca0132_alt_vol_ctls[dsp_dir].reqs[0],
6439 		float_vol_db_lookup[lookup_val]);
6440 
6441 	lookup_val = spec->vnode_rvol[nid - VNODE_START_NID];
6442 
6443 	dspio_set_uint_param(codec,
6444 		ca0132_alt_vol_ctls[dsp_dir].mid,
6445 		ca0132_alt_vol_ctls[dsp_dir].reqs[1],
6446 		float_vol_db_lookup[lookup_val]);
6447 
6448 	dspio_set_uint_param(codec,
6449 		ca0132_alt_vol_ctls[dsp_dir].mid,
6450 		ca0132_alt_vol_ctls[dsp_dir].reqs[2], FLOAT_ZERO);
6451 }
6452 
6453 static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
6454 			      struct snd_ctl_elem_info *uinfo)
6455 {
6456 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6457 	struct ca0132_spec *spec = codec->spec;
6458 	hda_nid_t nid = get_amp_nid(kcontrol);
6459 	int ch = get_amp_channels(kcontrol);
6460 	int dir = get_amp_direction(kcontrol);
6461 	unsigned long pval;
6462 	int err;
6463 
6464 	switch (nid) {
6465 	case VNID_SPK:
6466 		/* follow shared_out info */
6467 		nid = spec->shared_out_nid;
6468 		mutex_lock(&codec->control_mutex);
6469 		pval = kcontrol->private_value;
6470 		kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6471 		err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
6472 		kcontrol->private_value = pval;
6473 		mutex_unlock(&codec->control_mutex);
6474 		break;
6475 	case VNID_MIC:
6476 		/* follow shared_mic info */
6477 		nid = spec->shared_mic_nid;
6478 		mutex_lock(&codec->control_mutex);
6479 		pval = kcontrol->private_value;
6480 		kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6481 		err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
6482 		kcontrol->private_value = pval;
6483 		mutex_unlock(&codec->control_mutex);
6484 		break;
6485 	default:
6486 		err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
6487 	}
6488 	return err;
6489 }
6490 
6491 static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
6492 				struct snd_ctl_elem_value *ucontrol)
6493 {
6494 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6495 	struct ca0132_spec *spec = codec->spec;
6496 	hda_nid_t nid = get_amp_nid(kcontrol);
6497 	int ch = get_amp_channels(kcontrol);
6498 	long *valp = ucontrol->value.integer.value;
6499 
6500 	/* store the left and right volume */
6501 	if (ch & 1) {
6502 		*valp = spec->vnode_lvol[nid - VNODE_START_NID];
6503 		valp++;
6504 	}
6505 	if (ch & 2) {
6506 		*valp = spec->vnode_rvol[nid - VNODE_START_NID];
6507 		valp++;
6508 	}
6509 	return 0;
6510 }
6511 
6512 static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
6513 				struct snd_ctl_elem_value *ucontrol)
6514 {
6515 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6516 	struct ca0132_spec *spec = codec->spec;
6517 	hda_nid_t nid = get_amp_nid(kcontrol);
6518 	int ch = get_amp_channels(kcontrol);
6519 	long *valp = ucontrol->value.integer.value;
6520 	hda_nid_t shared_nid = 0;
6521 	bool effective;
6522 	int changed = 1;
6523 
6524 	/* store the left and right volume */
6525 	if (ch & 1) {
6526 		spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
6527 		valp++;
6528 	}
6529 	if (ch & 2) {
6530 		spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
6531 		valp++;
6532 	}
6533 
6534 	/* if effective conditions, then update hw immediately. */
6535 	effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
6536 	if (effective) {
6537 		int dir = get_amp_direction(kcontrol);
6538 		unsigned long pval;
6539 
6540 		snd_hda_power_up(codec);
6541 		mutex_lock(&codec->control_mutex);
6542 		pval = kcontrol->private_value;
6543 		kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
6544 								0, dir);
6545 		changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
6546 		kcontrol->private_value = pval;
6547 		mutex_unlock(&codec->control_mutex);
6548 		snd_hda_power_down(codec);
6549 	}
6550 
6551 	return changed;
6552 }
6553 
6554 /*
6555  * This function is the same as the one above, because using an if statement
6556  * inside of the above volume control for the DSP volume would cause too much
6557  * lag. This is a lot more smooth.
6558  */
6559 static int ca0132_alt_volume_put(struct snd_kcontrol *kcontrol,
6560 				struct snd_ctl_elem_value *ucontrol)
6561 {
6562 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6563 	struct ca0132_spec *spec = codec->spec;
6564 	hda_nid_t nid = get_amp_nid(kcontrol);
6565 	int ch = get_amp_channels(kcontrol);
6566 	long *valp = ucontrol->value.integer.value;
6567 	hda_nid_t vnid = 0;
6568 	int changed;
6569 
6570 	switch (nid) {
6571 	case 0x02:
6572 		vnid = VNID_SPK;
6573 		break;
6574 	case 0x07:
6575 		vnid = VNID_MIC;
6576 		break;
6577 	}
6578 
6579 	/* store the left and right volume */
6580 	if (ch & 1) {
6581 		spec->vnode_lvol[vnid - VNODE_START_NID] = *valp;
6582 		valp++;
6583 	}
6584 	if (ch & 2) {
6585 		spec->vnode_rvol[vnid - VNODE_START_NID] = *valp;
6586 		valp++;
6587 	}
6588 
6589 	snd_hda_power_up(codec);
6590 	ca0132_alt_dsp_volume_put(codec, vnid);
6591 	mutex_lock(&codec->control_mutex);
6592 	changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
6593 	mutex_unlock(&codec->control_mutex);
6594 	snd_hda_power_down(codec);
6595 
6596 	return changed;
6597 }
6598 
6599 static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
6600 			     unsigned int size, unsigned int __user *tlv)
6601 {
6602 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6603 	struct ca0132_spec *spec = codec->spec;
6604 	hda_nid_t nid = get_amp_nid(kcontrol);
6605 	int ch = get_amp_channels(kcontrol);
6606 	int dir = get_amp_direction(kcontrol);
6607 	unsigned long pval;
6608 	int err;
6609 
6610 	switch (nid) {
6611 	case VNID_SPK:
6612 		/* follow shared_out tlv */
6613 		nid = spec->shared_out_nid;
6614 		mutex_lock(&codec->control_mutex);
6615 		pval = kcontrol->private_value;
6616 		kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6617 		err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
6618 		kcontrol->private_value = pval;
6619 		mutex_unlock(&codec->control_mutex);
6620 		break;
6621 	case VNID_MIC:
6622 		/* follow shared_mic tlv */
6623 		nid = spec->shared_mic_nid;
6624 		mutex_lock(&codec->control_mutex);
6625 		pval = kcontrol->private_value;
6626 		kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6627 		err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
6628 		kcontrol->private_value = pval;
6629 		mutex_unlock(&codec->control_mutex);
6630 		break;
6631 	default:
6632 		err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
6633 	}
6634 	return err;
6635 }
6636 
6637 /* Add volume slider control for effect level */
6638 static int ca0132_alt_add_effect_slider(struct hda_codec *codec, hda_nid_t nid,
6639 					const char *pfx, int dir)
6640 {
6641 	char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6642 	int type = dir ? HDA_INPUT : HDA_OUTPUT;
6643 	struct snd_kcontrol_new knew =
6644 		HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
6645 
6646 	sprintf(namestr, "FX: %s %s Volume", pfx, dirstr[dir]);
6647 
6648 	knew.tlv.c = NULL;
6649 
6650 	switch (nid) {
6651 	case XBASS_XOVER:
6652 		knew.info = ca0132_alt_xbass_xover_slider_info;
6653 		knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
6654 		knew.put = ca0132_alt_xbass_xover_slider_put;
6655 		break;
6656 	default:
6657 		knew.info = ca0132_alt_effect_slider_info;
6658 		knew.get = ca0132_alt_slider_ctl_get;
6659 		knew.put = ca0132_alt_effect_slider_put;
6660 		knew.private_value =
6661 			HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
6662 		break;
6663 	}
6664 
6665 	return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
6666 }
6667 
6668 /*
6669  * Added FX: prefix for the alternative codecs, because otherwise the surround
6670  * effect would conflict with the Surround sound volume control. Also seems more
6671  * clear as to what the switches do. Left alone for others.
6672  */
6673 static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
6674 			 const char *pfx, int dir)
6675 {
6676 	struct ca0132_spec *spec = codec->spec;
6677 	char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6678 	int type = dir ? HDA_INPUT : HDA_OUTPUT;
6679 	struct snd_kcontrol_new knew =
6680 		CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
6681 	/* If using alt_controls, add FX: prefix. But, don't add FX:
6682 	 * prefix to OutFX or InFX enable controls.
6683 	 */
6684 	if (ca0132_use_alt_controls(spec) && (nid <= IN_EFFECT_END_NID))
6685 		sprintf(namestr, "FX: %s %s Switch", pfx, dirstr[dir]);
6686 	else
6687 		sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
6688 
6689 	return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
6690 }
6691 
6692 static int add_voicefx(struct hda_codec *codec)
6693 {
6694 	struct snd_kcontrol_new knew =
6695 		HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
6696 				    VOICEFX, 1, 0, HDA_INPUT);
6697 	knew.info = ca0132_voicefx_info;
6698 	knew.get = ca0132_voicefx_get;
6699 	knew.put = ca0132_voicefx_put;
6700 	return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
6701 }
6702 
6703 /* Create the EQ Preset control */
6704 static int add_ca0132_alt_eq_presets(struct hda_codec *codec)
6705 {
6706 	struct snd_kcontrol_new knew =
6707 		HDA_CODEC_MUTE_MONO(ca0132_alt_eq_enum.name,
6708 				    EQ_PRESET_ENUM, 1, 0, HDA_OUTPUT);
6709 	knew.info = ca0132_alt_eq_preset_info;
6710 	knew.get = ca0132_alt_eq_preset_get;
6711 	knew.put = ca0132_alt_eq_preset_put;
6712 	return snd_hda_ctl_add(codec, EQ_PRESET_ENUM,
6713 				snd_ctl_new1(&knew, codec));
6714 }
6715 
6716 /*
6717  * Add enumerated control for the three different settings of the smart volume
6718  * output effect. Normal just uses the slider value, and loud and night are
6719  * their own things that ignore that value.
6720  */
6721 static int ca0132_alt_add_svm_enum(struct hda_codec *codec)
6722 {
6723 	struct snd_kcontrol_new knew =
6724 		HDA_CODEC_MUTE_MONO("FX: Smart Volume Setting",
6725 				    SMART_VOLUME_ENUM, 1, 0, HDA_OUTPUT);
6726 	knew.info = ca0132_alt_svm_setting_info;
6727 	knew.get = ca0132_alt_svm_setting_get;
6728 	knew.put = ca0132_alt_svm_setting_put;
6729 	return snd_hda_ctl_add(codec, SMART_VOLUME_ENUM,
6730 				snd_ctl_new1(&knew, codec));
6731 
6732 }
6733 
6734 /*
6735  * Create an Output Select enumerated control for codecs with surround
6736  * out capabilities.
6737  */
6738 static int ca0132_alt_add_output_enum(struct hda_codec *codec)
6739 {
6740 	struct snd_kcontrol_new knew =
6741 		HDA_CODEC_MUTE_MONO("Output Select",
6742 				    OUTPUT_SOURCE_ENUM, 1, 0, HDA_OUTPUT);
6743 	knew.info = ca0132_alt_output_select_get_info;
6744 	knew.get = ca0132_alt_output_select_get;
6745 	knew.put = ca0132_alt_output_select_put;
6746 	return snd_hda_ctl_add(codec, OUTPUT_SOURCE_ENUM,
6747 				snd_ctl_new1(&knew, codec));
6748 }
6749 
6750 /*
6751  * Add a control for selecting channel count on speaker output. Setting this
6752  * allows the DSP to do bass redirection and channel upmixing on surround
6753  * configurations.
6754  */
6755 static int ca0132_alt_add_speaker_channel_cfg_enum(struct hda_codec *codec)
6756 {
6757 	struct snd_kcontrol_new knew =
6758 		HDA_CODEC_MUTE_MONO("Surround Channel Config",
6759 				    SPEAKER_CHANNEL_CFG_ENUM, 1, 0, HDA_OUTPUT);
6760 	knew.info = ca0132_alt_speaker_channel_cfg_get_info;
6761 	knew.get = ca0132_alt_speaker_channel_cfg_get;
6762 	knew.put = ca0132_alt_speaker_channel_cfg_put;
6763 	return snd_hda_ctl_add(codec, SPEAKER_CHANNEL_CFG_ENUM,
6764 				snd_ctl_new1(&knew, codec));
6765 }
6766 
6767 /*
6768  * Full range front stereo and rear surround switches. When these are set to
6769  * full range, the lower frequencies from these channels are no longer
6770  * redirected to the LFE channel.
6771  */
6772 static int ca0132_alt_add_front_full_range_switch(struct hda_codec *codec)
6773 {
6774 	struct snd_kcontrol_new knew =
6775 		CA0132_CODEC_MUTE_MONO("Full-Range Front Speakers",
6776 				    SPEAKER_FULL_RANGE_FRONT, 1, HDA_OUTPUT);
6777 
6778 	return snd_hda_ctl_add(codec, SPEAKER_FULL_RANGE_FRONT,
6779 				snd_ctl_new1(&knew, codec));
6780 }
6781 
6782 static int ca0132_alt_add_rear_full_range_switch(struct hda_codec *codec)
6783 {
6784 	struct snd_kcontrol_new knew =
6785 		CA0132_CODEC_MUTE_MONO("Full-Range Rear Speakers",
6786 				    SPEAKER_FULL_RANGE_REAR, 1, HDA_OUTPUT);
6787 
6788 	return snd_hda_ctl_add(codec, SPEAKER_FULL_RANGE_REAR,
6789 				snd_ctl_new1(&knew, codec));
6790 }
6791 
6792 /*
6793  * Bass redirection redirects audio below the crossover frequency to the LFE
6794  * channel on speakers that are set as not being full-range. On configurations
6795  * without an LFE channel, it does nothing. Bass redirection seems to be the
6796  * replacement for X-Bass on configurations with an LFE channel.
6797  */
6798 static int ca0132_alt_add_bass_redirection_crossover(struct hda_codec *codec)
6799 {
6800 	const char *namestr = "Bass Redirection Crossover";
6801 	struct snd_kcontrol_new knew =
6802 		HDA_CODEC_VOLUME_MONO(namestr, BASS_REDIRECTION_XOVER, 1, 0,
6803 				HDA_OUTPUT);
6804 
6805 	knew.tlv.c = NULL;
6806 	knew.info = ca0132_alt_xbass_xover_slider_info;
6807 	knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
6808 	knew.put = ca0132_alt_xbass_xover_slider_put;
6809 
6810 	return snd_hda_ctl_add(codec, BASS_REDIRECTION_XOVER,
6811 			snd_ctl_new1(&knew, codec));
6812 }
6813 
6814 static int ca0132_alt_add_bass_redirection_switch(struct hda_codec *codec)
6815 {
6816 	const char *namestr = "Bass Redirection";
6817 	struct snd_kcontrol_new knew =
6818 		CA0132_CODEC_MUTE_MONO(namestr, BASS_REDIRECTION, 1,
6819 				HDA_OUTPUT);
6820 
6821 	return snd_hda_ctl_add(codec, BASS_REDIRECTION,
6822 			snd_ctl_new1(&knew, codec));
6823 }
6824 
6825 /*
6826  * Create an Input Source enumerated control for the alternate ca0132 codecs
6827  * because the front microphone has no auto-detect, and Line-in has to be set
6828  * somehow.
6829  */
6830 static int ca0132_alt_add_input_enum(struct hda_codec *codec)
6831 {
6832 	struct snd_kcontrol_new knew =
6833 		HDA_CODEC_MUTE_MONO("Input Source",
6834 				    INPUT_SOURCE_ENUM, 1, 0, HDA_INPUT);
6835 	knew.info = ca0132_alt_input_source_info;
6836 	knew.get = ca0132_alt_input_source_get;
6837 	knew.put = ca0132_alt_input_source_put;
6838 	return snd_hda_ctl_add(codec, INPUT_SOURCE_ENUM,
6839 				snd_ctl_new1(&knew, codec));
6840 }
6841 
6842 /*
6843  * Add mic boost enumerated control. Switches through 0dB to 30dB. This adds
6844  * more control than the original mic boost, which is either full 30dB or off.
6845  */
6846 static int ca0132_alt_add_mic_boost_enum(struct hda_codec *codec)
6847 {
6848 	struct snd_kcontrol_new knew =
6849 		HDA_CODEC_MUTE_MONO("Mic Boost Capture Switch",
6850 				    MIC_BOOST_ENUM, 1, 0, HDA_INPUT);
6851 	knew.info = ca0132_alt_mic_boost_info;
6852 	knew.get = ca0132_alt_mic_boost_get;
6853 	knew.put = ca0132_alt_mic_boost_put;
6854 	return snd_hda_ctl_add(codec, MIC_BOOST_ENUM,
6855 				snd_ctl_new1(&knew, codec));
6856 
6857 }
6858 
6859 /*
6860  * Add headphone gain enumerated control for the AE-5. This switches between
6861  * three modes, low, medium, and high. When non-headphone outputs are selected,
6862  * it is automatically set to high. This is the same behavior as Windows.
6863  */
6864 static int ae5_add_headphone_gain_enum(struct hda_codec *codec)
6865 {
6866 	struct snd_kcontrol_new knew =
6867 		HDA_CODEC_MUTE_MONO("AE-5: Headphone Gain",
6868 				    AE5_HEADPHONE_GAIN_ENUM, 1, 0, HDA_OUTPUT);
6869 	knew.info = ae5_headphone_gain_info;
6870 	knew.get = ae5_headphone_gain_get;
6871 	knew.put = ae5_headphone_gain_put;
6872 	return snd_hda_ctl_add(codec, AE5_HEADPHONE_GAIN_ENUM,
6873 				snd_ctl_new1(&knew, codec));
6874 }
6875 
6876 /*
6877  * Add sound filter enumerated control for the AE-5. This adds three different
6878  * settings: Slow Roll Off, Minimum Phase, and Fast Roll Off. From what I've
6879  * read into it, it changes the DAC's interpolation filter.
6880  */
6881 static int ae5_add_sound_filter_enum(struct hda_codec *codec)
6882 {
6883 	struct snd_kcontrol_new knew =
6884 		HDA_CODEC_MUTE_MONO("AE-5: Sound Filter",
6885 				    AE5_SOUND_FILTER_ENUM, 1, 0, HDA_OUTPUT);
6886 	knew.info = ae5_sound_filter_info;
6887 	knew.get = ae5_sound_filter_get;
6888 	knew.put = ae5_sound_filter_put;
6889 	return snd_hda_ctl_add(codec, AE5_SOUND_FILTER_ENUM,
6890 				snd_ctl_new1(&knew, codec));
6891 }
6892 
6893 static int zxr_add_headphone_gain_switch(struct hda_codec *codec)
6894 {
6895 	struct snd_kcontrol_new knew =
6896 		CA0132_CODEC_MUTE_MONO("ZxR: 600 Ohm Gain",
6897 				    ZXR_HEADPHONE_GAIN, 1, HDA_OUTPUT);
6898 
6899 	return snd_hda_ctl_add(codec, ZXR_HEADPHONE_GAIN,
6900 				snd_ctl_new1(&knew, codec));
6901 }
6902 
6903 /*
6904  * Need to create follower controls for the alternate codecs that have surround
6905  * capabilities.
6906  */
6907 static const char * const ca0132_alt_follower_pfxs[] = {
6908 	"Front", "Surround", "Center", "LFE", NULL,
6909 };
6910 
6911 /*
6912  * Also need special channel map, because the default one is incorrect.
6913  * I think this has to do with the pin for rear surround being 0x11,
6914  * and the center/lfe being 0x10. Usually the pin order is the opposite.
6915  */
6916 static const struct snd_pcm_chmap_elem ca0132_alt_chmaps[] = {
6917 	{ .channels = 2,
6918 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
6919 	{ .channels = 4,
6920 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
6921 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
6922 	{ .channels = 6,
6923 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
6924 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
6925 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
6926 	{ }
6927 };
6928 
6929 /* Add the correct chmap for streams with 6 channels. */
6930 static void ca0132_alt_add_chmap_ctls(struct hda_codec *codec)
6931 {
6932 	int err = 0;
6933 	struct hda_pcm *pcm;
6934 
6935 	list_for_each_entry(pcm, &codec->pcm_list_head, list) {
6936 		struct hda_pcm_stream *hinfo =
6937 			&pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
6938 		struct snd_pcm_chmap *chmap;
6939 		const struct snd_pcm_chmap_elem *elem;
6940 
6941 		elem = ca0132_alt_chmaps;
6942 		if (hinfo->channels_max == 6) {
6943 			err = snd_pcm_add_chmap_ctls(pcm->pcm,
6944 					SNDRV_PCM_STREAM_PLAYBACK,
6945 					elem, hinfo->channels_max, 0, &chmap);
6946 			if (err < 0)
6947 				codec_dbg(codec, "snd_pcm_add_chmap_ctls failed!");
6948 		}
6949 	}
6950 }
6951 
6952 /*
6953  * When changing Node IDs for Mixer Controls below, make sure to update
6954  * Node IDs in ca0132_config() as well.
6955  */
6956 static const struct snd_kcontrol_new ca0132_mixer[] = {
6957 	CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
6958 	CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
6959 	CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
6960 	CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
6961 	HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
6962 	HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
6963 	HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
6964 	HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
6965 	CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
6966 			       0x12, 1, HDA_INPUT),
6967 	CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
6968 			       VNID_HP_SEL, 1, HDA_OUTPUT),
6969 	CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
6970 			       VNID_AMIC1_SEL, 1, HDA_INPUT),
6971 	CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
6972 			       VNID_HP_ASEL, 1, HDA_OUTPUT),
6973 	CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
6974 			       VNID_AMIC1_ASEL, 1, HDA_INPUT),
6975 	{ } /* end */
6976 };
6977 
6978 /*
6979  * Desktop specific control mixer. Removes auto-detect for mic, and adds
6980  * surround controls. Also sets both the Front Playback and Capture Volume
6981  * controls to alt so they set the DSP's decibel level.
6982  */
6983 static const struct snd_kcontrol_new desktop_mixer[] = {
6984 	CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
6985 	CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
6986 	HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
6987 	HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
6988 	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
6989 	HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
6990 	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
6991 	HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
6992 	CA0132_ALT_CODEC_VOL("Capture Volume", 0x07, HDA_INPUT),
6993 	CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
6994 	HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
6995 	HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
6996 	CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
6997 				VNID_HP_ASEL, 1, HDA_OUTPUT),
6998 	{ } /* end */
6999 };
7000 
7001 /*
7002  * Same as the Sound Blaster Z, except doesn't use the alt volume for capture
7003  * because it doesn't set decibel levels for the DSP for capture.
7004  */
7005 static const struct snd_kcontrol_new r3di_mixer[] = {
7006 	CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
7007 	CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
7008 	HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
7009 	HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
7010 	HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
7011 	HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
7012 	HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
7013 	HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
7014 	CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
7015 	CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
7016 	HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
7017 	HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
7018 	CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
7019 				VNID_HP_ASEL, 1, HDA_OUTPUT),
7020 	{ } /* end */
7021 };
7022 
7023 static int ca0132_build_controls(struct hda_codec *codec)
7024 {
7025 	struct ca0132_spec *spec = codec->spec;
7026 	int i, num_fx, num_sliders;
7027 	int err = 0;
7028 
7029 	/* Add Mixer controls */
7030 	for (i = 0; i < spec->num_mixers; i++) {
7031 		err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
7032 		if (err < 0)
7033 			return err;
7034 	}
7035 	/* Setup vmaster with surround followers for desktop ca0132 devices */
7036 	if (ca0132_use_alt_functions(spec)) {
7037 		snd_hda_set_vmaster_tlv(codec, spec->dacs[0], HDA_OUTPUT,
7038 					spec->tlv);
7039 		snd_hda_add_vmaster(codec, "Master Playback Volume",
7040 					spec->tlv, ca0132_alt_follower_pfxs,
7041 					"Playback Volume", 0);
7042 		err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
7043 					    NULL, ca0132_alt_follower_pfxs,
7044 					    "Playback Switch",
7045 					    true, 0, &spec->vmaster_mute.sw_kctl);
7046 		if (err < 0)
7047 			return err;
7048 	}
7049 
7050 	/* Add in and out effects controls.
7051 	 * VoiceFX, PE and CrystalVoice are added separately.
7052 	 */
7053 	num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
7054 	for (i = 0; i < num_fx; i++) {
7055 		/* Desktop cards break if Echo Cancellation is used. */
7056 		if (ca0132_use_pci_mmio(spec)) {
7057 			if (i == (ECHO_CANCELLATION - IN_EFFECT_START_NID +
7058 						OUT_EFFECTS_COUNT))
7059 				continue;
7060 		}
7061 
7062 		err = add_fx_switch(codec, ca0132_effects[i].nid,
7063 				    ca0132_effects[i].name,
7064 				    ca0132_effects[i].direct);
7065 		if (err < 0)
7066 			return err;
7067 	}
7068 	/*
7069 	 * If codec has use_alt_controls set to true, add effect level sliders,
7070 	 * EQ presets, and Smart Volume presets. Also, change names to add FX
7071 	 * prefix, and change PlayEnhancement and CrystalVoice to match.
7072 	 */
7073 	if (ca0132_use_alt_controls(spec)) {
7074 		err = ca0132_alt_add_svm_enum(codec);
7075 		if (err < 0)
7076 			return err;
7077 
7078 		err = add_ca0132_alt_eq_presets(codec);
7079 		if (err < 0)
7080 			return err;
7081 
7082 		err = add_fx_switch(codec, PLAY_ENHANCEMENT,
7083 					"Enable OutFX", 0);
7084 		if (err < 0)
7085 			return err;
7086 
7087 		err = add_fx_switch(codec, CRYSTAL_VOICE,
7088 					"Enable InFX", 1);
7089 		if (err < 0)
7090 			return err;
7091 
7092 		num_sliders = OUT_EFFECTS_COUNT - 1;
7093 		for (i = 0; i < num_sliders; i++) {
7094 			err = ca0132_alt_add_effect_slider(codec,
7095 					    ca0132_effects[i].nid,
7096 					    ca0132_effects[i].name,
7097 					    ca0132_effects[i].direct);
7098 			if (err < 0)
7099 				return err;
7100 		}
7101 
7102 		err = ca0132_alt_add_effect_slider(codec, XBASS_XOVER,
7103 					"X-Bass Crossover", EFX_DIR_OUT);
7104 
7105 		if (err < 0)
7106 			return err;
7107 	} else {
7108 		err = add_fx_switch(codec, PLAY_ENHANCEMENT,
7109 					"PlayEnhancement", 0);
7110 		if (err < 0)
7111 			return err;
7112 
7113 		err = add_fx_switch(codec, CRYSTAL_VOICE,
7114 					"CrystalVoice", 1);
7115 		if (err < 0)
7116 			return err;
7117 	}
7118 	err = add_voicefx(codec);
7119 	if (err < 0)
7120 		return err;
7121 
7122 	/*
7123 	 * If the codec uses alt_functions, you need the enumerated controls
7124 	 * to select the new outputs and inputs, plus add the new mic boost
7125 	 * setting control.
7126 	 */
7127 	if (ca0132_use_alt_functions(spec)) {
7128 		err = ca0132_alt_add_output_enum(codec);
7129 		if (err < 0)
7130 			return err;
7131 		err = ca0132_alt_add_speaker_channel_cfg_enum(codec);
7132 		if (err < 0)
7133 			return err;
7134 		err = ca0132_alt_add_front_full_range_switch(codec);
7135 		if (err < 0)
7136 			return err;
7137 		err = ca0132_alt_add_rear_full_range_switch(codec);
7138 		if (err < 0)
7139 			return err;
7140 		err = ca0132_alt_add_bass_redirection_crossover(codec);
7141 		if (err < 0)
7142 			return err;
7143 		err = ca0132_alt_add_bass_redirection_switch(codec);
7144 		if (err < 0)
7145 			return err;
7146 		err = ca0132_alt_add_mic_boost_enum(codec);
7147 		if (err < 0)
7148 			return err;
7149 		/*
7150 		 * ZxR only has microphone input, there is no front panel
7151 		 * header on the card, and aux-in is handled by the DBPro board.
7152 		 */
7153 		if (ca0132_quirk(spec) != QUIRK_ZXR) {
7154 			err = ca0132_alt_add_input_enum(codec);
7155 			if (err < 0)
7156 				return err;
7157 		}
7158 	}
7159 
7160 	switch (ca0132_quirk(spec)) {
7161 	case QUIRK_AE5:
7162 	case QUIRK_AE7:
7163 		err = ae5_add_headphone_gain_enum(codec);
7164 		if (err < 0)
7165 			return err;
7166 		err = ae5_add_sound_filter_enum(codec);
7167 		if (err < 0)
7168 			return err;
7169 		break;
7170 	case QUIRK_ZXR:
7171 		err = zxr_add_headphone_gain_switch(codec);
7172 		if (err < 0)
7173 			return err;
7174 		break;
7175 	default:
7176 		break;
7177 	}
7178 
7179 #ifdef ENABLE_TUNING_CONTROLS
7180 	add_tuning_ctls(codec);
7181 #endif
7182 
7183 	err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
7184 	if (err < 0)
7185 		return err;
7186 
7187 	if (spec->dig_out) {
7188 		err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
7189 						    spec->dig_out);
7190 		if (err < 0)
7191 			return err;
7192 		err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
7193 		if (err < 0)
7194 			return err;
7195 		/* spec->multiout.share_spdif = 1; */
7196 	}
7197 
7198 	if (spec->dig_in) {
7199 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
7200 		if (err < 0)
7201 			return err;
7202 	}
7203 
7204 	if (ca0132_use_alt_functions(spec))
7205 		ca0132_alt_add_chmap_ctls(codec);
7206 
7207 	return 0;
7208 }
7209 
7210 static int dbpro_build_controls(struct hda_codec *codec)
7211 {
7212 	struct ca0132_spec *spec = codec->spec;
7213 	int err = 0;
7214 
7215 	if (spec->dig_out) {
7216 		err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
7217 				spec->dig_out);
7218 		if (err < 0)
7219 			return err;
7220 	}
7221 
7222 	if (spec->dig_in) {
7223 		err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
7224 		if (err < 0)
7225 			return err;
7226 	}
7227 
7228 	return 0;
7229 }
7230 
7231 /*
7232  * PCM
7233  */
7234 static const struct hda_pcm_stream ca0132_pcm_analog_playback = {
7235 	.substreams = 1,
7236 	.channels_min = 2,
7237 	.channels_max = 6,
7238 	.ops = {
7239 		.prepare = ca0132_playback_pcm_prepare,
7240 		.cleanup = ca0132_playback_pcm_cleanup,
7241 		.get_delay = ca0132_playback_pcm_delay,
7242 	},
7243 };
7244 
7245 static const struct hda_pcm_stream ca0132_pcm_analog_capture = {
7246 	.substreams = 1,
7247 	.channels_min = 2,
7248 	.channels_max = 2,
7249 	.ops = {
7250 		.prepare = ca0132_capture_pcm_prepare,
7251 		.cleanup = ca0132_capture_pcm_cleanup,
7252 		.get_delay = ca0132_capture_pcm_delay,
7253 	},
7254 };
7255 
7256 static const struct hda_pcm_stream ca0132_pcm_digital_playback = {
7257 	.substreams = 1,
7258 	.channels_min = 2,
7259 	.channels_max = 2,
7260 	.ops = {
7261 		.open = ca0132_dig_playback_pcm_open,
7262 		.close = ca0132_dig_playback_pcm_close,
7263 		.prepare = ca0132_dig_playback_pcm_prepare,
7264 		.cleanup = ca0132_dig_playback_pcm_cleanup
7265 	},
7266 };
7267 
7268 static const struct hda_pcm_stream ca0132_pcm_digital_capture = {
7269 	.substreams = 1,
7270 	.channels_min = 2,
7271 	.channels_max = 2,
7272 };
7273 
7274 static int ca0132_build_pcms(struct hda_codec *codec)
7275 {
7276 	struct ca0132_spec *spec = codec->spec;
7277 	struct hda_pcm *info;
7278 
7279 	info = snd_hda_codec_pcm_new(codec, "CA0132 Analog");
7280 	if (!info)
7281 		return -ENOMEM;
7282 	if (ca0132_use_alt_functions(spec)) {
7283 		info->own_chmap = true;
7284 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap
7285 			= ca0132_alt_chmaps;
7286 	}
7287 	info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
7288 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
7289 	info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
7290 		spec->multiout.max_channels;
7291 	info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
7292 	info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7293 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
7294 
7295 	/* With the DSP enabled, desktops don't use this ADC. */
7296 	if (!ca0132_use_alt_functions(spec)) {
7297 		info = snd_hda_codec_pcm_new(codec, "CA0132 Analog Mic-In2");
7298 		if (!info)
7299 			return -ENOMEM;
7300 		info->stream[SNDRV_PCM_STREAM_CAPTURE] =
7301 			ca0132_pcm_analog_capture;
7302 		info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7303 		info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
7304 	}
7305 
7306 	info = snd_hda_codec_pcm_new(codec, "CA0132 What U Hear");
7307 	if (!info)
7308 		return -ENOMEM;
7309 	info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
7310 	info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7311 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
7312 
7313 	if (!spec->dig_out && !spec->dig_in)
7314 		return 0;
7315 
7316 	info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
7317 	if (!info)
7318 		return -ENOMEM;
7319 	info->pcm_type = HDA_PCM_TYPE_SPDIF;
7320 	if (spec->dig_out) {
7321 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
7322 			ca0132_pcm_digital_playback;
7323 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
7324 	}
7325 	if (spec->dig_in) {
7326 		info->stream[SNDRV_PCM_STREAM_CAPTURE] =
7327 			ca0132_pcm_digital_capture;
7328 		info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
7329 	}
7330 
7331 	return 0;
7332 }
7333 
7334 static int dbpro_build_pcms(struct hda_codec *codec)
7335 {
7336 	struct ca0132_spec *spec = codec->spec;
7337 	struct hda_pcm *info;
7338 
7339 	info = snd_hda_codec_pcm_new(codec, "CA0132 Alt Analog");
7340 	if (!info)
7341 		return -ENOMEM;
7342 	info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
7343 	info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7344 	info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
7345 
7346 
7347 	if (!spec->dig_out && !spec->dig_in)
7348 		return 0;
7349 
7350 	info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
7351 	if (!info)
7352 		return -ENOMEM;
7353 	info->pcm_type = HDA_PCM_TYPE_SPDIF;
7354 	if (spec->dig_out) {
7355 		info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
7356 			ca0132_pcm_digital_playback;
7357 		info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
7358 	}
7359 	if (spec->dig_in) {
7360 		info->stream[SNDRV_PCM_STREAM_CAPTURE] =
7361 			ca0132_pcm_digital_capture;
7362 		info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
7363 	}
7364 
7365 	return 0;
7366 }
7367 
7368 static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
7369 {
7370 	if (pin) {
7371 		snd_hda_set_pin_ctl(codec, pin, PIN_HP);
7372 		if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
7373 			snd_hda_codec_write(codec, pin, 0,
7374 					    AC_VERB_SET_AMP_GAIN_MUTE,
7375 					    AMP_OUT_UNMUTE);
7376 	}
7377 	if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
7378 		snd_hda_codec_write(codec, dac, 0,
7379 				    AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
7380 }
7381 
7382 static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
7383 {
7384 	if (pin) {
7385 		snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
7386 		if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
7387 			snd_hda_codec_write(codec, pin, 0,
7388 					    AC_VERB_SET_AMP_GAIN_MUTE,
7389 					    AMP_IN_UNMUTE(0));
7390 	}
7391 	if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
7392 		snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
7393 				    AMP_IN_UNMUTE(0));
7394 
7395 		/* init to 0 dB and unmute. */
7396 		snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
7397 					 HDA_AMP_VOLMASK, 0x5a);
7398 		snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
7399 					 HDA_AMP_MUTE, 0);
7400 	}
7401 }
7402 
7403 static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
7404 {
7405 	unsigned int caps;
7406 
7407 	caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
7408 				  AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
7409 	snd_hda_override_amp_caps(codec, nid, dir, caps);
7410 }
7411 
7412 /*
7413  * Switch between Digital built-in mic and analog mic.
7414  */
7415 static void ca0132_set_dmic(struct hda_codec *codec, int enable)
7416 {
7417 	struct ca0132_spec *spec = codec->spec;
7418 	unsigned int tmp;
7419 	u8 val;
7420 	unsigned int oldval;
7421 
7422 	codec_dbg(codec, "ca0132_set_dmic: enable=%d\n", enable);
7423 
7424 	oldval = stop_mic1(codec);
7425 	ca0132_set_vipsource(codec, 0);
7426 	if (enable) {
7427 		/* set DMic input as 2-ch */
7428 		tmp = FLOAT_TWO;
7429 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7430 
7431 		val = spec->dmic_ctl;
7432 		val |= 0x80;
7433 		snd_hda_codec_write(codec, spec->input_pins[0], 0,
7434 				    VENDOR_CHIPIO_DMIC_CTL_SET, val);
7435 
7436 		if (!(spec->dmic_ctl & 0x20))
7437 			chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
7438 	} else {
7439 		/* set AMic input as mono */
7440 		tmp = FLOAT_ONE;
7441 		dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7442 
7443 		val = spec->dmic_ctl;
7444 		/* clear bit7 and bit5 to disable dmic */
7445 		val &= 0x5f;
7446 		snd_hda_codec_write(codec, spec->input_pins[0], 0,
7447 				    VENDOR_CHIPIO_DMIC_CTL_SET, val);
7448 
7449 		if (!(spec->dmic_ctl & 0x20))
7450 			chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
7451 	}
7452 	ca0132_set_vipsource(codec, 1);
7453 	resume_mic1(codec, oldval);
7454 }
7455 
7456 /*
7457  * Initialization for Digital Mic.
7458  */
7459 static void ca0132_init_dmic(struct hda_codec *codec)
7460 {
7461 	struct ca0132_spec *spec = codec->spec;
7462 	u8 val;
7463 
7464 	/* Setup Digital Mic here, but don't enable.
7465 	 * Enable based on jack detect.
7466 	 */
7467 
7468 	/* MCLK uses MPIO1, set to enable.
7469 	 * Bit 2-0: MPIO select
7470 	 * Bit   3: set to disable
7471 	 * Bit 7-4: reserved
7472 	 */
7473 	val = 0x01;
7474 	snd_hda_codec_write(codec, spec->input_pins[0], 0,
7475 			    VENDOR_CHIPIO_DMIC_MCLK_SET, val);
7476 
7477 	/* Data1 uses MPIO3. Data2 not use
7478 	 * Bit 2-0: Data1 MPIO select
7479 	 * Bit   3: set disable Data1
7480 	 * Bit 6-4: Data2 MPIO select
7481 	 * Bit   7: set disable Data2
7482 	 */
7483 	val = 0x83;
7484 	snd_hda_codec_write(codec, spec->input_pins[0], 0,
7485 			    VENDOR_CHIPIO_DMIC_PIN_SET, val);
7486 
7487 	/* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
7488 	 * Bit 3-0: Channel mask
7489 	 * Bit   4: set for 48KHz, clear for 32KHz
7490 	 * Bit   5: mode
7491 	 * Bit   6: set to select Data2, clear for Data1
7492 	 * Bit   7: set to enable DMic, clear for AMic
7493 	 */
7494 	if (ca0132_quirk(spec) == QUIRK_ALIENWARE_M17XR4)
7495 		val = 0x33;
7496 	else
7497 		val = 0x23;
7498 	/* keep a copy of dmic ctl val for enable/disable dmic purpuse */
7499 	spec->dmic_ctl = val;
7500 	snd_hda_codec_write(codec, spec->input_pins[0], 0,
7501 			    VENDOR_CHIPIO_DMIC_CTL_SET, val);
7502 }
7503 
7504 /*
7505  * Initialization for Analog Mic 2
7506  */
7507 static void ca0132_init_analog_mic2(struct hda_codec *codec)
7508 {
7509 	struct ca0132_spec *spec = codec->spec;
7510 
7511 	mutex_lock(&spec->chipio_mutex);
7512 
7513 	chipio_8051_write_exram_no_mutex(codec, 0x1920, 0x00);
7514 	chipio_8051_write_exram_no_mutex(codec, 0x192d, 0x00);
7515 
7516 	mutex_unlock(&spec->chipio_mutex);
7517 }
7518 
7519 static void ca0132_refresh_widget_caps(struct hda_codec *codec)
7520 {
7521 	struct ca0132_spec *spec = codec->spec;
7522 	int i;
7523 
7524 	codec_dbg(codec, "ca0132_refresh_widget_caps.\n");
7525 	snd_hda_codec_update_widgets(codec);
7526 
7527 	for (i = 0; i < spec->multiout.num_dacs; i++)
7528 		refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
7529 
7530 	for (i = 0; i < spec->num_outputs; i++)
7531 		refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
7532 
7533 	for (i = 0; i < spec->num_inputs; i++) {
7534 		refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
7535 		refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
7536 	}
7537 }
7538 
7539 
7540 /* If there is an active channel for some reason, find it and free it. */
7541 static void ca0132_alt_free_active_dma_channels(struct hda_codec *codec)
7542 {
7543 	unsigned int i, tmp;
7544 	int status;
7545 
7546 	/* Read active DSPDMAC channel register. */
7547 	status = chipio_read(codec, DSPDMAC_CHNLSTART_MODULE_OFFSET, &tmp);
7548 	if (status >= 0) {
7549 		/* AND against 0xfff to get the active channel bits. */
7550 		tmp = tmp & 0xfff;
7551 
7552 		/* If there are no active channels, nothing to free. */
7553 		if (!tmp)
7554 			return;
7555 	} else {
7556 		codec_dbg(codec, "%s: Failed to read active DSP DMA channel register.\n",
7557 				__func__);
7558 		return;
7559 	}
7560 
7561 	/*
7562 	 * Check each DSP DMA channel for activity, and if the channel is
7563 	 * active, free it.
7564 	 */
7565 	for (i = 0; i < DSPDMAC_DMA_CFG_CHANNEL_COUNT; i++) {
7566 		if (dsp_is_dma_active(codec, i)) {
7567 			status = dspio_free_dma_chan(codec, i);
7568 			if (status < 0)
7569 				codec_dbg(codec, "%s: Failed to free active DSP DMA channel %d.\n",
7570 						__func__, i);
7571 		}
7572 	}
7573 }
7574 
7575 /*
7576  * In the case of CT_EXTENSIONS_ENABLE being set to 1, and the DSP being in
7577  * use, audio is no longer routed directly to the DAC/ADC from the HDA stream.
7578  * Instead, audio is now routed through the DSP's DMA controllers, which
7579  * the DSP is tasked with setting up itself. Through debugging, it seems the
7580  * cause of most of the no-audio on startup issues were due to improperly
7581  * configured DSP DMA channels.
7582  *
7583  * Normally, the DSP configures these the first time an HDA audio stream is
7584  * started post DSP firmware download. That is why creating a 'dummy' stream
7585  * worked in fixing the audio in some cases. This works most of the time, but
7586  * sometimes if a stream is started/stopped before the DSP can setup the DMA
7587  * configuration registers, it ends up in a broken state. Issues can also
7588  * arise if streams are started in an unusual order, i.e the audio output dma
7589  * channel being sandwiched between the mic1 and mic2 dma channels.
7590  *
7591  * The solution to this is to make sure that the DSP has no DMA channels
7592  * in use post DSP firmware download, and then to manually start each default
7593  * DSP stream that uses the DMA channels. These are 0x0c, the audio output
7594  * stream, 0x03, analog mic 1, and 0x04, analog mic 2.
7595  */
7596 static void ca0132_alt_start_dsp_audio_streams(struct hda_codec *codec)
7597 {
7598 	static const unsigned int dsp_dma_stream_ids[] = { 0x0c, 0x03, 0x04 };
7599 	struct ca0132_spec *spec = codec->spec;
7600 	unsigned int i, tmp;
7601 
7602 	/*
7603 	 * Check if any of the default streams are active, and if they are,
7604 	 * stop them.
7605 	 */
7606 	mutex_lock(&spec->chipio_mutex);
7607 
7608 	for (i = 0; i < ARRAY_SIZE(dsp_dma_stream_ids); i++) {
7609 		chipio_get_stream_control(codec, dsp_dma_stream_ids[i], &tmp);
7610 
7611 		if (tmp) {
7612 			chipio_set_stream_control(codec,
7613 					dsp_dma_stream_ids[i], 0);
7614 		}
7615 	}
7616 
7617 	mutex_unlock(&spec->chipio_mutex);
7618 
7619 	/*
7620 	 * If all DSP streams are inactive, there should be no active DSP DMA
7621 	 * channels. Check and make sure this is the case, and if it isn't,
7622 	 * free any active channels.
7623 	 */
7624 	ca0132_alt_free_active_dma_channels(codec);
7625 
7626 	mutex_lock(&spec->chipio_mutex);
7627 
7628 	/* Make sure stream 0x0c is six channels. */
7629 	chipio_set_stream_channels(codec, 0x0c, 6);
7630 
7631 	for (i = 0; i < ARRAY_SIZE(dsp_dma_stream_ids); i++) {
7632 		chipio_set_stream_control(codec,
7633 				dsp_dma_stream_ids[i], 1);
7634 
7635 		/* Give the DSP some time to setup the DMA channel. */
7636 		msleep(75);
7637 	}
7638 
7639 	mutex_unlock(&spec->chipio_mutex);
7640 }
7641 
7642 /*
7643  * The region of ChipIO memory from 0x190000-0x1903fc is a sort of 'audio
7644  * router', where each entry represents a 48khz audio channel, with a format
7645  * of an 8-bit destination, an 8-bit source, and an unknown 2-bit number
7646  * value. The 2-bit number value is seemingly 0 if inactive, 1 if active,
7647  * and 3 if it's using Sample Rate Converter ports.
7648  * An example is:
7649  * 0x0001f8c0
7650  * In this case, f8 is the destination, and c0 is the source. The number value
7651  * is 1.
7652  * This region of memory is normally managed internally by the 8051, where
7653  * the region of exram memory from 0x1477-0x1575 has each byte represent an
7654  * entry within the 0x190000 range, and when a range of entries is in use, the
7655  * ending value is overwritten with 0xff.
7656  * 0x1578 in exram is a table of 0x25 entries, corresponding to the ChipIO
7657  * streamID's, where each entry is a starting 0x190000 port offset.
7658  * 0x159d in exram is the same as 0x1578, except it contains the ending port
7659  * offset for the corresponding streamID.
7660  *
7661  * On certain cards, such as the SBZ/ZxR/AE7, these are originally setup by
7662  * the 8051, then manually overwritten to remap the ports to work with the
7663  * new DACs.
7664  *
7665  * Currently known portID's:
7666  * 0x00-0x1f: HDA audio stream input/output ports.
7667  * 0x80-0xbf: Sample rate converter input/outputs. Only valid ports seem to
7668  *            have the lower-nibble set to 0x1, 0x2, and 0x9.
7669  * 0xc0-0xdf: DSP DMA input/output ports. Dynamically assigned.
7670  * 0xe0-0xff: DAC/ADC audio input/output ports.
7671  *
7672  * Currently known streamID's:
7673  * 0x03: Mic1 ADC to DSP.
7674  * 0x04: Mic2 ADC to DSP.
7675  * 0x05: HDA node 0x02 audio stream to DSP.
7676  * 0x0f: DSP Mic exit to HDA node 0x07.
7677  * 0x0c: DSP processed audio to DACs.
7678  * 0x14: DAC0, front L/R.
7679  *
7680  * It is possible to route the HDA audio streams directly to the DAC and
7681  * bypass the DSP entirely, with the only downside being that since the DSP
7682  * does volume control, the only volume control you'll get is through PCM on
7683  * the PC side, in the same way volume is handled for optical out. This may be
7684  * useful for debugging.
7685  */
7686 static void chipio_remap_stream(struct hda_codec *codec,
7687 		const struct chipio_stream_remap_data *remap_data)
7688 {
7689 	unsigned int i, stream_offset;
7690 
7691 	/* Get the starting port for the stream to be remapped. */
7692 	chipio_8051_read_exram(codec, 0x1578 + remap_data->stream_id,
7693 			&stream_offset);
7694 
7695 	/*
7696 	 * Check if the stream's port value is 0xff, because the 8051 may not
7697 	 * have gotten around to setting up the stream yet. Wait until it's
7698 	 * setup to remap it's ports.
7699 	 */
7700 	if (stream_offset == 0xff) {
7701 		for (i = 0; i < 5; i++) {
7702 			msleep(25);
7703 
7704 			chipio_8051_read_exram(codec, 0x1578 + remap_data->stream_id,
7705 					&stream_offset);
7706 
7707 			if (stream_offset != 0xff)
7708 				break;
7709 		}
7710 	}
7711 
7712 	if (stream_offset == 0xff) {
7713 		codec_info(codec, "%s: Stream 0x%02x ports aren't allocated, remap failed!\n",
7714 				__func__, remap_data->stream_id);
7715 		return;
7716 	}
7717 
7718 	/* Offset isn't in bytes, its in 32-bit words, so multiply it by 4. */
7719 	stream_offset *= 0x04;
7720 	stream_offset += 0x190000;
7721 
7722 	for (i = 0; i < remap_data->count; i++) {
7723 		chipio_write_no_mutex(codec,
7724 				stream_offset + remap_data->offset[i],
7725 				remap_data->value[i]);
7726 	}
7727 
7728 	/* Update stream map configuration. */
7729 	chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
7730 }
7731 
7732 /*
7733  * Default speaker tuning values setup for alternative codecs.
7734  */
7735 static const unsigned int sbz_default_delay_values[] = {
7736 	/* Non-zero values are floating point 0.000198. */
7737 	0x394f9e38, 0x394f9e38, 0x00000000, 0x00000000, 0x00000000, 0x00000000
7738 };
7739 
7740 static const unsigned int zxr_default_delay_values[] = {
7741 	/* Non-zero values are floating point 0.000220. */
7742 	0x00000000, 0x00000000, 0x3966afcd, 0x3966afcd, 0x3966afcd, 0x3966afcd
7743 };
7744 
7745 static const unsigned int ae5_default_delay_values[] = {
7746 	/* Non-zero values are floating point 0.000100. */
7747 	0x00000000, 0x00000000, 0x38d1b717, 0x38d1b717, 0x38d1b717, 0x38d1b717
7748 };
7749 
7750 /*
7751  * If we never change these, probably only need them on initialization.
7752  */
7753 static void ca0132_alt_init_speaker_tuning(struct hda_codec *codec)
7754 {
7755 	struct ca0132_spec *spec = codec->spec;
7756 	unsigned int i, tmp, start_req, end_req;
7757 	const unsigned int *values;
7758 
7759 	switch (ca0132_quirk(spec)) {
7760 	case QUIRK_SBZ:
7761 		values = sbz_default_delay_values;
7762 		break;
7763 	case QUIRK_ZXR:
7764 		values = zxr_default_delay_values;
7765 		break;
7766 	case QUIRK_AE5:
7767 	case QUIRK_AE7:
7768 		values = ae5_default_delay_values;
7769 		break;
7770 	default:
7771 		values = sbz_default_delay_values;
7772 		break;
7773 	}
7774 
7775 	tmp = FLOAT_ZERO;
7776 	dspio_set_uint_param(codec, 0x96, SPEAKER_TUNING_ENABLE_CENTER_EQ, tmp);
7777 
7778 	start_req = SPEAKER_TUNING_FRONT_LEFT_VOL_LEVEL;
7779 	end_req = SPEAKER_TUNING_REAR_RIGHT_VOL_LEVEL;
7780 	for (i = start_req; i < end_req + 1; i++)
7781 		dspio_set_uint_param(codec, 0x96, i, tmp);
7782 
7783 	start_req = SPEAKER_TUNING_FRONT_LEFT_INVERT;
7784 	end_req = SPEAKER_TUNING_REAR_RIGHT_INVERT;
7785 	for (i = start_req; i < end_req + 1; i++)
7786 		dspio_set_uint_param(codec, 0x96, i, tmp);
7787 
7788 
7789 	for (i = 0; i < 6; i++)
7790 		dspio_set_uint_param(codec, 0x96,
7791 				SPEAKER_TUNING_FRONT_LEFT_DELAY + i, values[i]);
7792 }
7793 
7794 /*
7795  * Initialize mic for non-chromebook ca0132 implementations.
7796  */
7797 static void ca0132_alt_init_analog_mics(struct hda_codec *codec)
7798 {
7799 	struct ca0132_spec *spec = codec->spec;
7800 	unsigned int tmp;
7801 
7802 	/* Mic 1 Setup */
7803 	chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
7804 	chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
7805 	if (ca0132_quirk(spec) == QUIRK_R3DI) {
7806 		chipio_set_conn_rate(codec, 0x0F, SR_96_000);
7807 		tmp = FLOAT_ONE;
7808 	} else
7809 		tmp = FLOAT_THREE;
7810 	dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7811 
7812 	/* Mic 2 setup (not present on desktop cards) */
7813 	chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
7814 	chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
7815 	if (ca0132_quirk(spec) == QUIRK_R3DI)
7816 		chipio_set_conn_rate(codec, 0x0F, SR_96_000);
7817 	tmp = FLOAT_ZERO;
7818 	dspio_set_uint_param(codec, 0x80, 0x01, tmp);
7819 }
7820 
7821 /*
7822  * Sets the source of stream 0x14 to connpointID 0x48, and the destination
7823  * connpointID to 0x91. If this isn't done, the destination is 0x71, and
7824  * you get no sound. I'm guessing this has to do with the Sound Blaster Z
7825  * having an updated DAC, which changes the destination to that DAC.
7826  */
7827 static void sbz_connect_streams(struct hda_codec *codec)
7828 {
7829 	struct ca0132_spec *spec = codec->spec;
7830 
7831 	mutex_lock(&spec->chipio_mutex);
7832 
7833 	codec_dbg(codec, "Connect Streams entered, mutex locked and loaded.\n");
7834 
7835 	/* This value is 0x43 for 96khz, and 0x83 for 192khz. */
7836 	chipio_write_no_mutex(codec, 0x18a020, 0x00000043);
7837 
7838 	/* Setup stream 0x14 with it's source and destination points */
7839 	chipio_set_stream_source_dest(codec, 0x14, 0x48, 0x91);
7840 	chipio_set_conn_rate_no_mutex(codec, 0x48, SR_96_000);
7841 	chipio_set_conn_rate_no_mutex(codec, 0x91, SR_96_000);
7842 	chipio_set_stream_channels(codec, 0x14, 2);
7843 	chipio_set_stream_control(codec, 0x14, 1);
7844 
7845 	codec_dbg(codec, "Connect Streams exited, mutex released.\n");
7846 
7847 	mutex_unlock(&spec->chipio_mutex);
7848 }
7849 
7850 /*
7851  * Write data through ChipIO to setup proper stream destinations.
7852  * Not sure how it exactly works, but it seems to direct data
7853  * to different destinations. Example is f8 to c0, e0 to c0.
7854  * All I know is, if you don't set these, you get no sound.
7855  */
7856 static void sbz_chipio_startup_data(struct hda_codec *codec)
7857 {
7858 	const struct chipio_stream_remap_data *dsp_out_remap_data;
7859 	struct ca0132_spec *spec = codec->spec;
7860 
7861 	mutex_lock(&spec->chipio_mutex);
7862 	codec_dbg(codec, "Startup Data entered, mutex locked and loaded.\n");
7863 
7864 	/* Remap DAC0's output ports. */
7865 	chipio_remap_stream(codec, &stream_remap_data[0]);
7866 
7867 	/* Remap DSP audio output stream ports. */
7868 	switch (ca0132_quirk(spec)) {
7869 	case QUIRK_SBZ:
7870 		dsp_out_remap_data = &stream_remap_data[1];
7871 		break;
7872 
7873 	case QUIRK_ZXR:
7874 		dsp_out_remap_data = &stream_remap_data[2];
7875 		break;
7876 
7877 	default:
7878 		dsp_out_remap_data = NULL;
7879 		break;
7880 	}
7881 
7882 	if (dsp_out_remap_data)
7883 		chipio_remap_stream(codec, dsp_out_remap_data);
7884 
7885 	codec_dbg(codec, "Startup Data exited, mutex released.\n");
7886 	mutex_unlock(&spec->chipio_mutex);
7887 }
7888 
7889 static void ca0132_alt_dsp_initial_mic_setup(struct hda_codec *codec)
7890 {
7891 	struct ca0132_spec *spec = codec->spec;
7892 	unsigned int tmp;
7893 
7894 	chipio_set_stream_control(codec, 0x03, 0);
7895 	chipio_set_stream_control(codec, 0x04, 0);
7896 
7897 	chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
7898 	chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
7899 
7900 	tmp = FLOAT_THREE;
7901 	dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7902 
7903 	chipio_set_stream_control(codec, 0x03, 1);
7904 	chipio_set_stream_control(codec, 0x04, 1);
7905 
7906 	switch (ca0132_quirk(spec)) {
7907 	case QUIRK_SBZ:
7908 		chipio_write(codec, 0x18b098, 0x0000000c);
7909 		chipio_write(codec, 0x18b09C, 0x0000000c);
7910 		break;
7911 	case QUIRK_AE5:
7912 		chipio_write(codec, 0x18b098, 0x0000000c);
7913 		chipio_write(codec, 0x18b09c, 0x0000004c);
7914 		break;
7915 	default:
7916 		break;
7917 	}
7918 }
7919 
7920 static void ae5_post_dsp_register_set(struct hda_codec *codec)
7921 {
7922 	struct ca0132_spec *spec = codec->spec;
7923 
7924 	chipio_8051_write_direct(codec, 0x93, 0x10);
7925 	chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
7926 
7927 	writeb(0xff, spec->mem_base + 0x304);
7928 	writeb(0xff, spec->mem_base + 0x304);
7929 	writeb(0xff, spec->mem_base + 0x304);
7930 	writeb(0xff, spec->mem_base + 0x304);
7931 	writeb(0x00, spec->mem_base + 0x100);
7932 	writeb(0xff, spec->mem_base + 0x304);
7933 	writeb(0x00, spec->mem_base + 0x100);
7934 	writeb(0xff, spec->mem_base + 0x304);
7935 	writeb(0x00, spec->mem_base + 0x100);
7936 	writeb(0xff, spec->mem_base + 0x304);
7937 	writeb(0x00, spec->mem_base + 0x100);
7938 	writeb(0xff, spec->mem_base + 0x304);
7939 
7940 	ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x3f);
7941 	ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x3f);
7942 	ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
7943 }
7944 
7945 static void ae5_post_dsp_param_setup(struct hda_codec *codec)
7946 {
7947 	/*
7948 	 * Param3 in the 8051's memory is represented by the ascii string 'mch'
7949 	 * which seems to be 'multichannel'. This is also mentioned in the
7950 	 * AE-5's registry values in Windows.
7951 	 */
7952 	chipio_set_control_param(codec, 3, 0);
7953 	/*
7954 	 * I believe ASI is 'audio serial interface' and that it's used to
7955 	 * change colors on the external LED strip connected to the AE-5.
7956 	 */
7957 	chipio_set_control_flag(codec, CONTROL_FLAG_ASI_96KHZ, 1);
7958 
7959 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x724, 0x83);
7960 	chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
7961 
7962 	chipio_8051_write_exram(codec, 0xfa92, 0x22);
7963 }
7964 
7965 static void ae5_post_dsp_pll_setup(struct hda_codec *codec)
7966 {
7967 	chipio_8051_write_pll_pmu(codec, 0x41, 0xc8);
7968 	chipio_8051_write_pll_pmu(codec, 0x45, 0xcc);
7969 	chipio_8051_write_pll_pmu(codec, 0x40, 0xcb);
7970 	chipio_8051_write_pll_pmu(codec, 0x43, 0xc7);
7971 	chipio_8051_write_pll_pmu(codec, 0x51, 0x8d);
7972 }
7973 
7974 static void ae5_post_dsp_stream_setup(struct hda_codec *codec)
7975 {
7976 	struct ca0132_spec *spec = codec->spec;
7977 
7978 	mutex_lock(&spec->chipio_mutex);
7979 
7980 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x81);
7981 
7982 	chipio_set_conn_rate_no_mutex(codec, 0x70, SR_96_000);
7983 
7984 	chipio_set_stream_source_dest(codec, 0x5, 0x43, 0x0);
7985 
7986 	chipio_set_stream_source_dest(codec, 0x18, 0x9, 0xd0);
7987 	chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
7988 	chipio_set_stream_channels(codec, 0x18, 6);
7989 	chipio_set_stream_control(codec, 0x18, 1);
7990 
7991 	chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 4);
7992 
7993 	chipio_8051_write_pll_pmu_no_mutex(codec, 0x43, 0xc7);
7994 
7995 	ca0113_mmio_command_set(codec, 0x48, 0x01, 0x80);
7996 
7997 	mutex_unlock(&spec->chipio_mutex);
7998 }
7999 
8000 static void ae5_post_dsp_startup_data(struct hda_codec *codec)
8001 {
8002 	struct ca0132_spec *spec = codec->spec;
8003 
8004 	mutex_lock(&spec->chipio_mutex);
8005 
8006 	chipio_write_no_mutex(codec, 0x189000, 0x0001f101);
8007 	chipio_write_no_mutex(codec, 0x189004, 0x0001f101);
8008 	chipio_write_no_mutex(codec, 0x189024, 0x00014004);
8009 	chipio_write_no_mutex(codec, 0x189028, 0x0002000f);
8010 
8011 	ca0113_mmio_command_set(codec, 0x48, 0x0a, 0x05);
8012 	chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
8013 	ca0113_mmio_command_set(codec, 0x48, 0x0b, 0x12);
8014 	ca0113_mmio_command_set(codec, 0x48, 0x04, 0x00);
8015 	ca0113_mmio_command_set(codec, 0x48, 0x06, 0x48);
8016 	ca0113_mmio_command_set(codec, 0x48, 0x0a, 0x05);
8017 	ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
8018 	ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
8019 	ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
8020 	ca0113_mmio_gpio_set(codec, 0, true);
8021 	ca0113_mmio_gpio_set(codec, 1, true);
8022 	ca0113_mmio_command_set(codec, 0x48, 0x07, 0x80);
8023 
8024 	chipio_write_no_mutex(codec, 0x18b03c, 0x00000012);
8025 
8026 	ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
8027 	ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
8028 
8029 	mutex_unlock(&spec->chipio_mutex);
8030 }
8031 
8032 static void ae7_post_dsp_setup_ports(struct hda_codec *codec)
8033 {
8034 	struct ca0132_spec *spec = codec->spec;
8035 
8036 	mutex_lock(&spec->chipio_mutex);
8037 
8038 	/* Seems to share the same port remapping as the SBZ. */
8039 	chipio_remap_stream(codec, &stream_remap_data[1]);
8040 
8041 	ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
8042 	ca0113_mmio_command_set(codec, 0x48, 0x0d, 0x40);
8043 	ca0113_mmio_command_set(codec, 0x48, 0x17, 0x00);
8044 	ca0113_mmio_command_set(codec, 0x48, 0x19, 0x00);
8045 	ca0113_mmio_command_set(codec, 0x48, 0x11, 0xff);
8046 	ca0113_mmio_command_set(codec, 0x48, 0x12, 0xff);
8047 	ca0113_mmio_command_set(codec, 0x48, 0x13, 0xff);
8048 	ca0113_mmio_command_set(codec, 0x48, 0x14, 0x7f);
8049 
8050 	mutex_unlock(&spec->chipio_mutex);
8051 }
8052 
8053 static void ae7_post_dsp_asi_stream_setup(struct hda_codec *codec)
8054 {
8055 	struct ca0132_spec *spec = codec->spec;
8056 
8057 	mutex_lock(&spec->chipio_mutex);
8058 
8059 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x81);
8060 	ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
8061 
8062 	chipio_set_conn_rate_no_mutex(codec, 0x70, SR_96_000);
8063 
8064 	chipio_set_stream_source_dest(codec, 0x05, 0x43, 0x00);
8065 	chipio_set_stream_source_dest(codec, 0x18, 0x09, 0xd0);
8066 
8067 	chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
8068 	chipio_set_stream_channels(codec, 0x18, 6);
8069 	chipio_set_stream_control(codec, 0x18, 1);
8070 
8071 	chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 4);
8072 
8073 	mutex_unlock(&spec->chipio_mutex);
8074 }
8075 
8076 static void ae7_post_dsp_pll_setup(struct hda_codec *codec)
8077 {
8078 	static const unsigned int addr[] = {
8079 		0x41, 0x45, 0x40, 0x43, 0x51
8080 	};
8081 	static const unsigned int data[] = {
8082 		0xc8, 0xcc, 0xcb, 0xc7, 0x8d
8083 	};
8084 	unsigned int i;
8085 
8086 	for (i = 0; i < ARRAY_SIZE(addr); i++)
8087 		chipio_8051_write_pll_pmu_no_mutex(codec, addr[i], data[i]);
8088 }
8089 
8090 static void ae7_post_dsp_asi_setup_ports(struct hda_codec *codec)
8091 {
8092 	struct ca0132_spec *spec = codec->spec;
8093 	static const unsigned int target[] = {
8094 		0x0b, 0x04, 0x06, 0x0a, 0x0c, 0x11, 0x12, 0x13, 0x14
8095 	};
8096 	static const unsigned int data[] = {
8097 		0x12, 0x00, 0x48, 0x05, 0x5f, 0xff, 0xff, 0xff, 0x7f
8098 	};
8099 	unsigned int i;
8100 
8101 	mutex_lock(&spec->chipio_mutex);
8102 
8103 	chipio_8051_write_pll_pmu_no_mutex(codec, 0x43, 0xc7);
8104 
8105 	chipio_write_no_mutex(codec, 0x189000, 0x0001f101);
8106 	chipio_write_no_mutex(codec, 0x189004, 0x0001f101);
8107 	chipio_write_no_mutex(codec, 0x189024, 0x00014004);
8108 	chipio_write_no_mutex(codec, 0x189028, 0x0002000f);
8109 
8110 	ae7_post_dsp_pll_setup(codec);
8111 	chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
8112 
8113 	for (i = 0; i < ARRAY_SIZE(target); i++)
8114 		ca0113_mmio_command_set(codec, 0x48, target[i], data[i]);
8115 
8116 	ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
8117 	ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
8118 	ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
8119 
8120 	chipio_set_stream_source_dest(codec, 0x21, 0x64, 0x56);
8121 	chipio_set_stream_channels(codec, 0x21, 2);
8122 	chipio_set_conn_rate_no_mutex(codec, 0x56, SR_8_000);
8123 
8124 	chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_NODE_ID, 0x09);
8125 	/*
8126 	 * In the 8051's memory, this param is referred to as 'n2sid', which I
8127 	 * believe is 'node to streamID'. It seems to be a way to assign a
8128 	 * stream to a given HDA node.
8129 	 */
8130 	chipio_set_control_param_no_mutex(codec, 0x20, 0x21);
8131 
8132 	chipio_write_no_mutex(codec, 0x18b038, 0x00000088);
8133 
8134 	/*
8135 	 * Now, at this point on Windows, an actual stream is setup and
8136 	 * seemingly sends data to the HDA node 0x09, which is the digital
8137 	 * audio input node. This is left out here, because obviously I don't
8138 	 * know what data is being sent. Interestingly, the AE-5 seems to go
8139 	 * through the motions of getting here and never actually takes this
8140 	 * step, but the AE-7 does.
8141 	 */
8142 
8143 	ca0113_mmio_gpio_set(codec, 0, 1);
8144 	ca0113_mmio_gpio_set(codec, 1, 1);
8145 
8146 	ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
8147 	chipio_write_no_mutex(codec, 0x18b03c, 0x00000000);
8148 	ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
8149 	ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
8150 
8151 	chipio_set_stream_source_dest(codec, 0x05, 0x43, 0x00);
8152 	chipio_set_stream_source_dest(codec, 0x18, 0x09, 0xd0);
8153 
8154 	chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
8155 	chipio_set_stream_channels(codec, 0x18, 6);
8156 
8157 	/*
8158 	 * Runs again, this has been repeated a few times, but I'm just
8159 	 * following what the Windows driver does.
8160 	 */
8161 	ae7_post_dsp_pll_setup(codec);
8162 	chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
8163 
8164 	mutex_unlock(&spec->chipio_mutex);
8165 }
8166 
8167 /*
8168  * The Windows driver has commands that seem to setup ASI, which I believe to
8169  * be some sort of audio serial interface. My current speculation is that it's
8170  * related to communicating with the new DAC.
8171  */
8172 static void ae7_post_dsp_asi_setup(struct hda_codec *codec)
8173 {
8174 	chipio_8051_write_direct(codec, 0x93, 0x10);
8175 
8176 	chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
8177 
8178 	ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
8179 	ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
8180 
8181 	chipio_set_control_param(codec, 3, 3);
8182 	chipio_set_control_flag(codec, CONTROL_FLAG_ASI_96KHZ, 1);
8183 
8184 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x724, 0x83);
8185 	chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
8186 	snd_hda_codec_write(codec, 0x17, 0, 0x794, 0x00);
8187 
8188 	chipio_8051_write_exram(codec, 0xfa92, 0x22);
8189 
8190 	ae7_post_dsp_pll_setup(codec);
8191 	ae7_post_dsp_asi_stream_setup(codec);
8192 
8193 	chipio_8051_write_pll_pmu(codec, 0x43, 0xc7);
8194 
8195 	ae7_post_dsp_asi_setup_ports(codec);
8196 }
8197 
8198 /*
8199  * Setup default parameters for DSP
8200  */
8201 static void ca0132_setup_defaults(struct hda_codec *codec)
8202 {
8203 	struct ca0132_spec *spec = codec->spec;
8204 	unsigned int tmp;
8205 	int num_fx;
8206 	int idx, i;
8207 
8208 	if (spec->dsp_state != DSP_DOWNLOADED)
8209 		return;
8210 
8211 	/* out, in effects + voicefx */
8212 	num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8213 	for (idx = 0; idx < num_fx; idx++) {
8214 		for (i = 0; i <= ca0132_effects[idx].params; i++) {
8215 			dspio_set_uint_param(codec, ca0132_effects[idx].mid,
8216 					     ca0132_effects[idx].reqs[i],
8217 					     ca0132_effects[idx].def_vals[i]);
8218 		}
8219 	}
8220 
8221 	/*remove DSP headroom*/
8222 	tmp = FLOAT_ZERO;
8223 	dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8224 
8225 	/*set speaker EQ bypass attenuation*/
8226 	dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
8227 
8228 	/* set AMic1 and AMic2 as mono mic */
8229 	tmp = FLOAT_ONE;
8230 	dspio_set_uint_param(codec, 0x80, 0x00, tmp);
8231 	dspio_set_uint_param(codec, 0x80, 0x01, tmp);
8232 
8233 	/* set AMic1 as CrystalVoice input */
8234 	tmp = FLOAT_ONE;
8235 	dspio_set_uint_param(codec, 0x80, 0x05, tmp);
8236 
8237 	/* set WUH source */
8238 	tmp = FLOAT_TWO;
8239 	dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8240 }
8241 
8242 /*
8243  * Setup default parameters for Recon3D/Recon3Di DSP.
8244  */
8245 
8246 static void r3d_setup_defaults(struct hda_codec *codec)
8247 {
8248 	struct ca0132_spec *spec = codec->spec;
8249 	unsigned int tmp;
8250 	int num_fx;
8251 	int idx, i;
8252 
8253 	if (spec->dsp_state != DSP_DOWNLOADED)
8254 		return;
8255 
8256 	ca0132_alt_init_analog_mics(codec);
8257 	ca0132_alt_start_dsp_audio_streams(codec);
8258 
8259 	/*remove DSP headroom*/
8260 	tmp = FLOAT_ZERO;
8261 	dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8262 
8263 	/* set WUH source */
8264 	tmp = FLOAT_TWO;
8265 	dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8266 	chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8267 
8268 	/* Set speaker source? */
8269 	dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8270 
8271 	if (ca0132_quirk(spec) == QUIRK_R3DI)
8272 		r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADED);
8273 
8274 	/* Disable mute on Center/LFE. */
8275 	if (ca0132_quirk(spec) == QUIRK_R3D) {
8276 		ca0113_mmio_gpio_set(codec, 2, false);
8277 		ca0113_mmio_gpio_set(codec, 4, true);
8278 	}
8279 
8280 	/* Setup effect defaults */
8281 	num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8282 	for (idx = 0; idx < num_fx; idx++) {
8283 		for (i = 0; i <= ca0132_effects[idx].params; i++) {
8284 			dspio_set_uint_param(codec,
8285 					ca0132_effects[idx].mid,
8286 					ca0132_effects[idx].reqs[i],
8287 					ca0132_effects[idx].def_vals[i]);
8288 		}
8289 	}
8290 }
8291 
8292 /*
8293  * Setup default parameters for the Sound Blaster Z DSP. A lot more going on
8294  * than the Chromebook setup.
8295  */
8296 static void sbz_setup_defaults(struct hda_codec *codec)
8297 {
8298 	struct ca0132_spec *spec = codec->spec;
8299 	unsigned int tmp;
8300 	int num_fx;
8301 	int idx, i;
8302 
8303 	if (spec->dsp_state != DSP_DOWNLOADED)
8304 		return;
8305 
8306 	ca0132_alt_init_analog_mics(codec);
8307 	ca0132_alt_start_dsp_audio_streams(codec);
8308 	sbz_connect_streams(codec);
8309 	sbz_chipio_startup_data(codec);
8310 
8311 	/*
8312 	 * Sets internal input loopback to off, used to have a switch to
8313 	 * enable input loopback, but turned out to be way too buggy.
8314 	 */
8315 	tmp = FLOAT_ONE;
8316 	dspio_set_uint_param(codec, 0x37, 0x08, tmp);
8317 	dspio_set_uint_param(codec, 0x37, 0x10, tmp);
8318 
8319 	/*remove DSP headroom*/
8320 	tmp = FLOAT_ZERO;
8321 	dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8322 
8323 	/* set WUH source */
8324 	tmp = FLOAT_TWO;
8325 	dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8326 	chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8327 
8328 	/* Set speaker source? */
8329 	dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8330 
8331 	ca0132_alt_dsp_initial_mic_setup(codec);
8332 
8333 	/* out, in effects + voicefx */
8334 	num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8335 	for (idx = 0; idx < num_fx; idx++) {
8336 		for (i = 0; i <= ca0132_effects[idx].params; i++) {
8337 			dspio_set_uint_param(codec,
8338 					ca0132_effects[idx].mid,
8339 					ca0132_effects[idx].reqs[i],
8340 					ca0132_effects[idx].def_vals[i]);
8341 		}
8342 	}
8343 
8344 	ca0132_alt_init_speaker_tuning(codec);
8345 }
8346 
8347 /*
8348  * Setup default parameters for the Sound BlasterX AE-5 DSP.
8349  */
8350 static void ae5_setup_defaults(struct hda_codec *codec)
8351 {
8352 	struct ca0132_spec *spec = codec->spec;
8353 	unsigned int tmp;
8354 	int num_fx;
8355 	int idx, i;
8356 
8357 	if (spec->dsp_state != DSP_DOWNLOADED)
8358 		return;
8359 
8360 	ca0132_alt_init_analog_mics(codec);
8361 	ca0132_alt_start_dsp_audio_streams(codec);
8362 
8363 	/* New, unknown SCP req's */
8364 	tmp = FLOAT_ZERO;
8365 	dspio_set_uint_param(codec, 0x96, 0x29, tmp);
8366 	dspio_set_uint_param(codec, 0x96, 0x2a, tmp);
8367 	dspio_set_uint_param(codec, 0x80, 0x0d, tmp);
8368 	dspio_set_uint_param(codec, 0x80, 0x0e, tmp);
8369 
8370 	ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
8371 	ca0113_mmio_gpio_set(codec, 0, false);
8372 	ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
8373 
8374 	/* Internal loopback off */
8375 	tmp = FLOAT_ONE;
8376 	dspio_set_uint_param(codec, 0x37, 0x08, tmp);
8377 	dspio_set_uint_param(codec, 0x37, 0x10, tmp);
8378 
8379 	/*remove DSP headroom*/
8380 	tmp = FLOAT_ZERO;
8381 	dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8382 
8383 	/* set WUH source */
8384 	tmp = FLOAT_TWO;
8385 	dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8386 	chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8387 
8388 	/* Set speaker source? */
8389 	dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8390 
8391 	ca0132_alt_dsp_initial_mic_setup(codec);
8392 	ae5_post_dsp_register_set(codec);
8393 	ae5_post_dsp_param_setup(codec);
8394 	ae5_post_dsp_pll_setup(codec);
8395 	ae5_post_dsp_stream_setup(codec);
8396 	ae5_post_dsp_startup_data(codec);
8397 
8398 	/* out, in effects + voicefx */
8399 	num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8400 	for (idx = 0; idx < num_fx; idx++) {
8401 		for (i = 0; i <= ca0132_effects[idx].params; i++) {
8402 			dspio_set_uint_param(codec,
8403 					ca0132_effects[idx].mid,
8404 					ca0132_effects[idx].reqs[i],
8405 					ca0132_effects[idx].def_vals[i]);
8406 		}
8407 	}
8408 
8409 	ca0132_alt_init_speaker_tuning(codec);
8410 }
8411 
8412 /*
8413  * Setup default parameters for the Sound Blaster AE-7 DSP.
8414  */
8415 static void ae7_setup_defaults(struct hda_codec *codec)
8416 {
8417 	struct ca0132_spec *spec = codec->spec;
8418 	unsigned int tmp;
8419 	int num_fx;
8420 	int idx, i;
8421 
8422 	if (spec->dsp_state != DSP_DOWNLOADED)
8423 		return;
8424 
8425 	ca0132_alt_init_analog_mics(codec);
8426 	ca0132_alt_start_dsp_audio_streams(codec);
8427 	ae7_post_dsp_setup_ports(codec);
8428 
8429 	tmp = FLOAT_ZERO;
8430 	dspio_set_uint_param(codec, 0x96,
8431 			SPEAKER_TUNING_FRONT_LEFT_INVERT, tmp);
8432 	dspio_set_uint_param(codec, 0x96,
8433 			SPEAKER_TUNING_FRONT_RIGHT_INVERT, tmp);
8434 
8435 	ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
8436 
8437 	/* New, unknown SCP req's */
8438 	dspio_set_uint_param(codec, 0x80, 0x0d, tmp);
8439 	dspio_set_uint_param(codec, 0x80, 0x0e, tmp);
8440 
8441 	ca0113_mmio_gpio_set(codec, 0, false);
8442 
8443 	/* Internal loopback off */
8444 	tmp = FLOAT_ONE;
8445 	dspio_set_uint_param(codec, 0x37, 0x08, tmp);
8446 	dspio_set_uint_param(codec, 0x37, 0x10, tmp);
8447 
8448 	/*remove DSP headroom*/
8449 	tmp = FLOAT_ZERO;
8450 	dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8451 
8452 	/* set WUH source */
8453 	tmp = FLOAT_TWO;
8454 	dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8455 	chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8456 
8457 	/* Set speaker source? */
8458 	dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8459 	ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
8460 
8461 	/*
8462 	 * This is the second time we've called this, but this is seemingly
8463 	 * what Windows does.
8464 	 */
8465 	ca0132_alt_init_analog_mics(codec);
8466 
8467 	ae7_post_dsp_asi_setup(codec);
8468 
8469 	/*
8470 	 * Not sure why, but these are both set to 1. They're only set to 0
8471 	 * upon shutdown.
8472 	 */
8473 	ca0113_mmio_gpio_set(codec, 0, true);
8474 	ca0113_mmio_gpio_set(codec, 1, true);
8475 
8476 	/* Volume control related. */
8477 	ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x04);
8478 	ca0113_mmio_command_set(codec, 0x48, 0x10, 0x04);
8479 	ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x80);
8480 
8481 	/* out, in effects + voicefx */
8482 	num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8483 	for (idx = 0; idx < num_fx; idx++) {
8484 		for (i = 0; i <= ca0132_effects[idx].params; i++) {
8485 			dspio_set_uint_param(codec,
8486 					ca0132_effects[idx].mid,
8487 					ca0132_effects[idx].reqs[i],
8488 					ca0132_effects[idx].def_vals[i]);
8489 		}
8490 	}
8491 
8492 	ca0132_alt_init_speaker_tuning(codec);
8493 }
8494 
8495 /*
8496  * Initialization of flags in chip
8497  */
8498 static void ca0132_init_flags(struct hda_codec *codec)
8499 {
8500 	struct ca0132_spec *spec = codec->spec;
8501 
8502 	if (ca0132_use_alt_functions(spec)) {
8503 		chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, 1);
8504 		chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, 1);
8505 		chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, 1);
8506 		chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, 1);
8507 		chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, 1);
8508 		chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
8509 		chipio_set_control_flag(codec, CONTROL_FLAG_SPDIF2OUT, 0);
8510 		chipio_set_control_flag(codec,
8511 				CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
8512 		chipio_set_control_flag(codec,
8513 				CONTROL_FLAG_PORT_A_10KOHM_LOAD, 1);
8514 	} else {
8515 		chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
8516 		chipio_set_control_flag(codec,
8517 				CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
8518 		chipio_set_control_flag(codec,
8519 				CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
8520 		chipio_set_control_flag(codec,
8521 				CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
8522 		chipio_set_control_flag(codec,
8523 				CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
8524 		chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
8525 	}
8526 }
8527 
8528 /*
8529  * Initialization of parameters in chip
8530  */
8531 static void ca0132_init_params(struct hda_codec *codec)
8532 {
8533 	struct ca0132_spec *spec = codec->spec;
8534 
8535 	if (ca0132_use_alt_functions(spec)) {
8536 		chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8537 		chipio_set_conn_rate(codec, 0x0B, SR_48_000);
8538 		chipio_set_control_param(codec, CONTROL_PARAM_SPDIF1_SOURCE, 0);
8539 		chipio_set_control_param(codec, 0, 0);
8540 		chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
8541 	}
8542 
8543 	chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
8544 	chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
8545 }
8546 
8547 static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
8548 {
8549 	chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
8550 	chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
8551 	chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
8552 	chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
8553 	chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
8554 	chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
8555 
8556 	chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
8557 	chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
8558 	chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8559 }
8560 
8561 static bool ca0132_download_dsp_images(struct hda_codec *codec)
8562 {
8563 	bool dsp_loaded = false;
8564 	struct ca0132_spec *spec = codec->spec;
8565 	const struct dsp_image_seg *dsp_os_image;
8566 	const struct firmware *fw_entry = NULL;
8567 	/*
8568 	 * Alternate firmwares for different variants. The Recon3Di apparently
8569 	 * can use the default firmware, but I'll leave the option in case
8570 	 * it needs it again.
8571 	 */
8572 	switch (ca0132_quirk(spec)) {
8573 	case QUIRK_SBZ:
8574 	case QUIRK_R3D:
8575 	case QUIRK_AE5:
8576 		if (request_firmware(&fw_entry, DESKTOP_EFX_FILE,
8577 					codec->card->dev) != 0)
8578 			codec_dbg(codec, "Desktop firmware not found.");
8579 		else
8580 			codec_dbg(codec, "Desktop firmware selected.");
8581 		break;
8582 	case QUIRK_R3DI:
8583 		if (request_firmware(&fw_entry, R3DI_EFX_FILE,
8584 					codec->card->dev) != 0)
8585 			codec_dbg(codec, "Recon3Di alt firmware not detected.");
8586 		else
8587 			codec_dbg(codec, "Recon3Di firmware selected.");
8588 		break;
8589 	default:
8590 		break;
8591 	}
8592 	/*
8593 	 * Use default ctefx.bin if no alt firmware is detected, or if none
8594 	 * exists for your particular codec.
8595 	 */
8596 	if (!fw_entry) {
8597 		codec_dbg(codec, "Default firmware selected.");
8598 		if (request_firmware(&fw_entry, EFX_FILE,
8599 					codec->card->dev) != 0)
8600 			return false;
8601 	}
8602 
8603 	dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
8604 	if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
8605 		codec_err(codec, "ca0132 DSP load image failed\n");
8606 		goto exit_download;
8607 	}
8608 
8609 	dsp_loaded = dspload_wait_loaded(codec);
8610 
8611 exit_download:
8612 	release_firmware(fw_entry);
8613 
8614 	return dsp_loaded;
8615 }
8616 
8617 static void ca0132_download_dsp(struct hda_codec *codec)
8618 {
8619 	struct ca0132_spec *spec = codec->spec;
8620 
8621 #ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
8622 	return; /* NOP */
8623 #endif
8624 
8625 	if (spec->dsp_state == DSP_DOWNLOAD_FAILED)
8626 		return; /* don't retry failures */
8627 
8628 	chipio_enable_clocks(codec);
8629 	if (spec->dsp_state != DSP_DOWNLOADED) {
8630 		spec->dsp_state = DSP_DOWNLOADING;
8631 
8632 		if (!ca0132_download_dsp_images(codec))
8633 			spec->dsp_state = DSP_DOWNLOAD_FAILED;
8634 		else
8635 			spec->dsp_state = DSP_DOWNLOADED;
8636 	}
8637 
8638 	/* For codecs using alt functions, this is already done earlier */
8639 	if (spec->dsp_state == DSP_DOWNLOADED && !ca0132_use_alt_functions(spec))
8640 		ca0132_set_dsp_msr(codec, true);
8641 }
8642 
8643 static void ca0132_process_dsp_response(struct hda_codec *codec,
8644 					struct hda_jack_callback *callback)
8645 {
8646 	struct ca0132_spec *spec = codec->spec;
8647 
8648 	codec_dbg(codec, "ca0132_process_dsp_response\n");
8649 	snd_hda_power_up_pm(codec);
8650 	if (spec->wait_scp) {
8651 		if (dspio_get_response_data(codec) >= 0)
8652 			spec->wait_scp = 0;
8653 	}
8654 
8655 	dspio_clear_response_queue(codec);
8656 	snd_hda_power_down_pm(codec);
8657 }
8658 
8659 static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
8660 {
8661 	struct ca0132_spec *spec = codec->spec;
8662 	struct hda_jack_tbl *tbl;
8663 
8664 	/* Delay enabling the HP amp, to let the mic-detection
8665 	 * state machine run.
8666 	 */
8667 	tbl = snd_hda_jack_tbl_get(codec, cb->nid);
8668 	if (tbl)
8669 		tbl->block_report = 1;
8670 	schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(500));
8671 }
8672 
8673 static void amic_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
8674 {
8675 	struct ca0132_spec *spec = codec->spec;
8676 
8677 	if (ca0132_use_alt_functions(spec))
8678 		ca0132_alt_select_in(codec);
8679 	else
8680 		ca0132_select_mic(codec);
8681 }
8682 
8683 static void ca0132_setup_unsol(struct hda_codec *codec)
8684 {
8685 	struct ca0132_spec *spec = codec->spec;
8686 	snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_hp, hp_callback);
8687 	snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_amic1,
8688 					    amic_callback);
8689 	snd_hda_jack_detect_enable_callback(codec, UNSOL_TAG_DSP,
8690 					    ca0132_process_dsp_response);
8691 	/* Front headphone jack detection */
8692 	if (ca0132_use_alt_functions(spec))
8693 		snd_hda_jack_detect_enable_callback(codec,
8694 			spec->unsol_tag_front_hp, hp_callback);
8695 }
8696 
8697 /*
8698  * Verbs tables.
8699  */
8700 
8701 /* Sends before DSP download. */
8702 static const struct hda_verb ca0132_base_init_verbs[] = {
8703 	/*enable ct extension*/
8704 	{0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
8705 	{}
8706 };
8707 
8708 /* Send at exit. */
8709 static const struct hda_verb ca0132_base_exit_verbs[] = {
8710 	/*set afg to D3*/
8711 	{0x01, AC_VERB_SET_POWER_STATE, 0x03},
8712 	/*disable ct extension*/
8713 	{0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
8714 	{}
8715 };
8716 
8717 /* Other verbs tables. Sends after DSP download. */
8718 
8719 static const struct hda_verb ca0132_init_verbs0[] = {
8720 	/* chip init verbs */
8721 	{0x15, 0x70D, 0xF0},
8722 	{0x15, 0x70E, 0xFE},
8723 	{0x15, 0x707, 0x75},
8724 	{0x15, 0x707, 0xD3},
8725 	{0x15, 0x707, 0x09},
8726 	{0x15, 0x707, 0x53},
8727 	{0x15, 0x707, 0xD4},
8728 	{0x15, 0x707, 0xEF},
8729 	{0x15, 0x707, 0x75},
8730 	{0x15, 0x707, 0xD3},
8731 	{0x15, 0x707, 0x09},
8732 	{0x15, 0x707, 0x02},
8733 	{0x15, 0x707, 0x37},
8734 	{0x15, 0x707, 0x78},
8735 	{0x15, 0x53C, 0xCE},
8736 	{0x15, 0x575, 0xC9},
8737 	{0x15, 0x53D, 0xCE},
8738 	{0x15, 0x5B7, 0xC9},
8739 	{0x15, 0x70D, 0xE8},
8740 	{0x15, 0x70E, 0xFE},
8741 	{0x15, 0x707, 0x02},
8742 	{0x15, 0x707, 0x68},
8743 	{0x15, 0x707, 0x62},
8744 	{0x15, 0x53A, 0xCE},
8745 	{0x15, 0x546, 0xC9},
8746 	{0x15, 0x53B, 0xCE},
8747 	{0x15, 0x5E8, 0xC9},
8748 	{}
8749 };
8750 
8751 /* Extra init verbs for desktop cards. */
8752 static const struct hda_verb ca0132_init_verbs1[] = {
8753 	{0x15, 0x70D, 0x20},
8754 	{0x15, 0x70E, 0x19},
8755 	{0x15, 0x707, 0x00},
8756 	{0x15, 0x539, 0xCE},
8757 	{0x15, 0x546, 0xC9},
8758 	{0x15, 0x70D, 0xB7},
8759 	{0x15, 0x70E, 0x09},
8760 	{0x15, 0x707, 0x10},
8761 	{0x15, 0x70D, 0xAF},
8762 	{0x15, 0x70E, 0x09},
8763 	{0x15, 0x707, 0x01},
8764 	{0x15, 0x707, 0x05},
8765 	{0x15, 0x70D, 0x73},
8766 	{0x15, 0x70E, 0x09},
8767 	{0x15, 0x707, 0x14},
8768 	{0x15, 0x6FF, 0xC4},
8769 	{}
8770 };
8771 
8772 static void ca0132_init_chip(struct hda_codec *codec)
8773 {
8774 	struct ca0132_spec *spec = codec->spec;
8775 	int num_fx;
8776 	int i;
8777 	unsigned int on;
8778 
8779 	mutex_init(&spec->chipio_mutex);
8780 
8781 	/*
8782 	 * The Windows driver always does this upon startup, which seems to
8783 	 * clear out any previous configuration. This should help issues where
8784 	 * a boot into Windows prior to a boot into Linux breaks things. Also,
8785 	 * Windows always sends the reset twice.
8786 	 */
8787 	if (ca0132_use_alt_functions(spec)) {
8788 		chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
8789 		chipio_write_no_mutex(codec, 0x18b0a4, 0x000000c2);
8790 
8791 		snd_hda_codec_write(codec, codec->core.afg, 0,
8792 			    AC_VERB_SET_CODEC_RESET, 0);
8793 		snd_hda_codec_write(codec, codec->core.afg, 0,
8794 			    AC_VERB_SET_CODEC_RESET, 0);
8795 	}
8796 
8797 	spec->cur_out_type = SPEAKER_OUT;
8798 	if (!ca0132_use_alt_functions(spec))
8799 		spec->cur_mic_type = DIGITAL_MIC;
8800 	else
8801 		spec->cur_mic_type = REAR_MIC;
8802 
8803 	spec->cur_mic_boost = 0;
8804 
8805 	for (i = 0; i < VNODES_COUNT; i++) {
8806 		spec->vnode_lvol[i] = 0x5a;
8807 		spec->vnode_rvol[i] = 0x5a;
8808 		spec->vnode_lswitch[i] = 0;
8809 		spec->vnode_rswitch[i] = 0;
8810 	}
8811 
8812 	/*
8813 	 * Default states for effects are in ca0132_effects[].
8814 	 */
8815 	num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
8816 	for (i = 0; i < num_fx; i++) {
8817 		on = (unsigned int)ca0132_effects[i].reqs[0];
8818 		spec->effects_switch[i] = on ? 1 : 0;
8819 	}
8820 	/*
8821 	 * Sets defaults for the effect slider controls, only for alternative
8822 	 * ca0132 codecs. Also sets x-bass crossover frequency to 80hz.
8823 	 */
8824 	if (ca0132_use_alt_controls(spec)) {
8825 		/* Set speakers to default to full range. */
8826 		spec->speaker_range_val[0] = 1;
8827 		spec->speaker_range_val[1] = 1;
8828 
8829 		spec->xbass_xover_freq = 8;
8830 		for (i = 0; i < EFFECT_LEVEL_SLIDERS; i++)
8831 			spec->fx_ctl_val[i] = effect_slider_defaults[i];
8832 
8833 		spec->bass_redirect_xover_freq = 8;
8834 	}
8835 
8836 	spec->voicefx_val = 0;
8837 	spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
8838 	spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
8839 
8840 	/*
8841 	 * The ZxR doesn't have a front panel header, and it's line-in is on
8842 	 * the daughter board. So, there is no input enum control, and we need
8843 	 * to make sure that spec->in_enum_val is set properly.
8844 	 */
8845 	if (ca0132_quirk(spec) == QUIRK_ZXR)
8846 		spec->in_enum_val = REAR_MIC;
8847 
8848 #ifdef ENABLE_TUNING_CONTROLS
8849 	ca0132_init_tuning_defaults(codec);
8850 #endif
8851 }
8852 
8853 /*
8854  * Recon3Di exit specific commands.
8855  */
8856 /* prevents popping noise on shutdown */
8857 static void r3di_gpio_shutdown(struct hda_codec *codec)
8858 {
8859 	snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 0x00);
8860 }
8861 
8862 /*
8863  * Sound Blaster Z exit specific commands.
8864  */
8865 static void sbz_region2_exit(struct hda_codec *codec)
8866 {
8867 	struct ca0132_spec *spec = codec->spec;
8868 	unsigned int i;
8869 
8870 	for (i = 0; i < 4; i++)
8871 		writeb(0x0, spec->mem_base + 0x100);
8872 	for (i = 0; i < 8; i++)
8873 		writeb(0xb3, spec->mem_base + 0x304);
8874 
8875 	ca0113_mmio_gpio_set(codec, 0, false);
8876 	ca0113_mmio_gpio_set(codec, 1, false);
8877 	ca0113_mmio_gpio_set(codec, 4, true);
8878 	ca0113_mmio_gpio_set(codec, 5, false);
8879 	ca0113_mmio_gpio_set(codec, 7, false);
8880 }
8881 
8882 static void sbz_set_pin_ctl_default(struct hda_codec *codec)
8883 {
8884 	static const hda_nid_t pins[] = {0x0B, 0x0C, 0x0E, 0x12, 0x13};
8885 	unsigned int i;
8886 
8887 	snd_hda_codec_write(codec, 0x11, 0,
8888 			AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
8889 
8890 	for (i = 0; i < ARRAY_SIZE(pins); i++)
8891 		snd_hda_codec_write(codec, pins[i], 0,
8892 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00);
8893 }
8894 
8895 static void ca0132_clear_unsolicited(struct hda_codec *codec)
8896 {
8897 	static const hda_nid_t pins[] = {0x0B, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13};
8898 	unsigned int i;
8899 
8900 	for (i = 0; i < ARRAY_SIZE(pins); i++) {
8901 		snd_hda_codec_write(codec, pins[i], 0,
8902 				AC_VERB_SET_UNSOLICITED_ENABLE, 0x00);
8903 	}
8904 }
8905 
8906 /* On shutdown, sends commands in sets of three */
8907 static void sbz_gpio_shutdown_commands(struct hda_codec *codec, int dir,
8908 							int mask, int data)
8909 {
8910 	if (dir >= 0)
8911 		snd_hda_codec_write(codec, 0x01, 0,
8912 				AC_VERB_SET_GPIO_DIRECTION, dir);
8913 	if (mask >= 0)
8914 		snd_hda_codec_write(codec, 0x01, 0,
8915 				AC_VERB_SET_GPIO_MASK, mask);
8916 
8917 	if (data >= 0)
8918 		snd_hda_codec_write(codec, 0x01, 0,
8919 				AC_VERB_SET_GPIO_DATA, data);
8920 }
8921 
8922 static void zxr_dbpro_power_state_shutdown(struct hda_codec *codec)
8923 {
8924 	static const hda_nid_t pins[] = {0x05, 0x0c, 0x09, 0x0e, 0x08, 0x11, 0x01};
8925 	unsigned int i;
8926 
8927 	for (i = 0; i < ARRAY_SIZE(pins); i++)
8928 		snd_hda_codec_write(codec, pins[i], 0,
8929 				AC_VERB_SET_POWER_STATE, 0x03);
8930 }
8931 
8932 static void sbz_exit_chip(struct hda_codec *codec)
8933 {
8934 	chipio_set_stream_control(codec, 0x03, 0);
8935 	chipio_set_stream_control(codec, 0x04, 0);
8936 
8937 	/* Mess with GPIO */
8938 	sbz_gpio_shutdown_commands(codec, 0x07, 0x07, -1);
8939 	sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x05);
8940 	sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x01);
8941 
8942 	chipio_set_stream_control(codec, 0x14, 0);
8943 	chipio_set_stream_control(codec, 0x0C, 0);
8944 
8945 	chipio_set_conn_rate(codec, 0x41, SR_192_000);
8946 	chipio_set_conn_rate(codec, 0x91, SR_192_000);
8947 
8948 	chipio_write(codec, 0x18a020, 0x00000083);
8949 
8950 	sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x03);
8951 	sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x07);
8952 	sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x06);
8953 
8954 	chipio_set_stream_control(codec, 0x0C, 0);
8955 
8956 	chipio_set_control_param(codec, 0x0D, 0x24);
8957 
8958 	ca0132_clear_unsolicited(codec);
8959 	sbz_set_pin_ctl_default(codec);
8960 
8961 	snd_hda_codec_write(codec, 0x0B, 0,
8962 		AC_VERB_SET_EAPD_BTLENABLE, 0x00);
8963 
8964 	sbz_region2_exit(codec);
8965 }
8966 
8967 static void r3d_exit_chip(struct hda_codec *codec)
8968 {
8969 	ca0132_clear_unsolicited(codec);
8970 	snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
8971 	snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5b);
8972 }
8973 
8974 static void ae5_exit_chip(struct hda_codec *codec)
8975 {
8976 	chipio_set_stream_control(codec, 0x03, 0);
8977 	chipio_set_stream_control(codec, 0x04, 0);
8978 
8979 	ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
8980 	ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
8981 	ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
8982 	ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
8983 	ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
8984 	ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x00);
8985 	ca0113_mmio_gpio_set(codec, 0, false);
8986 	ca0113_mmio_gpio_set(codec, 1, false);
8987 
8988 	snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
8989 	snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
8990 
8991 	chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
8992 
8993 	chipio_set_stream_control(codec, 0x18, 0);
8994 	chipio_set_stream_control(codec, 0x0c, 0);
8995 
8996 	snd_hda_codec_write(codec, 0x01, 0, 0x724, 0x83);
8997 }
8998 
8999 static void ae7_exit_chip(struct hda_codec *codec)
9000 {
9001 	chipio_set_stream_control(codec, 0x18, 0);
9002 	chipio_set_stream_source_dest(codec, 0x21, 0xc8, 0xc8);
9003 	chipio_set_stream_channels(codec, 0x21, 0);
9004 	chipio_set_control_param(codec, CONTROL_PARAM_NODE_ID, 0x09);
9005 	chipio_set_control_param(codec, 0x20, 0x01);
9006 
9007 	chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
9008 
9009 	chipio_set_stream_control(codec, 0x18, 0);
9010 	chipio_set_stream_control(codec, 0x0c, 0);
9011 
9012 	ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
9013 	snd_hda_codec_write(codec, 0x15, 0, 0x724, 0x83);
9014 	ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
9015 	ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
9016 	ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x00);
9017 	ca0113_mmio_gpio_set(codec, 0, false);
9018 	ca0113_mmio_gpio_set(codec, 1, false);
9019 	ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
9020 
9021 	snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
9022 	snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
9023 }
9024 
9025 static void zxr_exit_chip(struct hda_codec *codec)
9026 {
9027 	chipio_set_stream_control(codec, 0x03, 0);
9028 	chipio_set_stream_control(codec, 0x04, 0);
9029 	chipio_set_stream_control(codec, 0x14, 0);
9030 	chipio_set_stream_control(codec, 0x0C, 0);
9031 
9032 	chipio_set_conn_rate(codec, 0x41, SR_192_000);
9033 	chipio_set_conn_rate(codec, 0x91, SR_192_000);
9034 
9035 	chipio_write(codec, 0x18a020, 0x00000083);
9036 
9037 	snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
9038 	snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
9039 
9040 	ca0132_clear_unsolicited(codec);
9041 	sbz_set_pin_ctl_default(codec);
9042 	snd_hda_codec_write(codec, 0x0B, 0, AC_VERB_SET_EAPD_BTLENABLE, 0x00);
9043 
9044 	ca0113_mmio_gpio_set(codec, 5, false);
9045 	ca0113_mmio_gpio_set(codec, 2, false);
9046 	ca0113_mmio_gpio_set(codec, 3, false);
9047 	ca0113_mmio_gpio_set(codec, 0, false);
9048 	ca0113_mmio_gpio_set(codec, 4, true);
9049 	ca0113_mmio_gpio_set(codec, 0, true);
9050 	ca0113_mmio_gpio_set(codec, 5, true);
9051 	ca0113_mmio_gpio_set(codec, 2, false);
9052 	ca0113_mmio_gpio_set(codec, 3, false);
9053 }
9054 
9055 static void ca0132_exit_chip(struct hda_codec *codec)
9056 {
9057 	/* put any chip cleanup stuffs here. */
9058 
9059 	if (dspload_is_loaded(codec))
9060 		dsp_reset(codec);
9061 }
9062 
9063 /*
9064  * This fixes a problem that was hard to reproduce. Very rarely, I would
9065  * boot up, and there would be no sound, but the DSP indicated it had loaded
9066  * properly. I did a few memory dumps to see if anything was different, and
9067  * there were a few areas of memory uninitialized with a1a2a3a4. This function
9068  * checks if those areas are uninitialized, and if they are, it'll attempt to
9069  * reload the card 3 times. Usually it fixes by the second.
9070  */
9071 static void sbz_dsp_startup_check(struct hda_codec *codec)
9072 {
9073 	struct ca0132_spec *spec = codec->spec;
9074 	unsigned int dsp_data_check[4];
9075 	unsigned int cur_address = 0x390;
9076 	unsigned int i;
9077 	unsigned int failure = 0;
9078 	unsigned int reload = 3;
9079 
9080 	if (spec->startup_check_entered)
9081 		return;
9082 
9083 	spec->startup_check_entered = true;
9084 
9085 	for (i = 0; i < 4; i++) {
9086 		chipio_read(codec, cur_address, &dsp_data_check[i]);
9087 		cur_address += 0x4;
9088 	}
9089 	for (i = 0; i < 4; i++) {
9090 		if (dsp_data_check[i] == 0xa1a2a3a4)
9091 			failure = 1;
9092 	}
9093 
9094 	codec_dbg(codec, "Startup Check: %d ", failure);
9095 	if (failure)
9096 		codec_info(codec, "DSP not initialized properly. Attempting to fix.");
9097 	/*
9098 	 * While the failure condition is true, and we haven't reached our
9099 	 * three reload limit, continue trying to reload the driver and
9100 	 * fix the issue.
9101 	 */
9102 	while (failure && (reload != 0)) {
9103 		codec_info(codec, "Reloading... Tries left: %d", reload);
9104 		sbz_exit_chip(codec);
9105 		spec->dsp_state = DSP_DOWNLOAD_INIT;
9106 		codec->patch_ops.init(codec);
9107 		failure = 0;
9108 		for (i = 0; i < 4; i++) {
9109 			chipio_read(codec, cur_address, &dsp_data_check[i]);
9110 			cur_address += 0x4;
9111 		}
9112 		for (i = 0; i < 4; i++) {
9113 			if (dsp_data_check[i] == 0xa1a2a3a4)
9114 				failure = 1;
9115 		}
9116 		reload--;
9117 	}
9118 
9119 	if (!failure && reload < 3)
9120 		codec_info(codec, "DSP fixed.");
9121 
9122 	if (!failure)
9123 		return;
9124 
9125 	codec_info(codec, "DSP failed to initialize properly. Either try a full shutdown or a suspend to clear the internal memory.");
9126 }
9127 
9128 /*
9129  * This is for the extra volume verbs 0x797 (left) and 0x798 (right). These add
9130  * extra precision for decibel values. If you had the dB value in floating point
9131  * you would take the value after the decimal point, multiply by 64, and divide
9132  * by 2. So for 8.59, it's (59 * 64) / 100. Useful if someone wanted to
9133  * implement fixed point or floating point dB volumes. For now, I'll set them
9134  * to 0 just incase a value has lingered from a boot into Windows.
9135  */
9136 static void ca0132_alt_vol_setup(struct hda_codec *codec)
9137 {
9138 	snd_hda_codec_write(codec, 0x02, 0, 0x797, 0x00);
9139 	snd_hda_codec_write(codec, 0x02, 0, 0x798, 0x00);
9140 	snd_hda_codec_write(codec, 0x03, 0, 0x797, 0x00);
9141 	snd_hda_codec_write(codec, 0x03, 0, 0x798, 0x00);
9142 	snd_hda_codec_write(codec, 0x04, 0, 0x797, 0x00);
9143 	snd_hda_codec_write(codec, 0x04, 0, 0x798, 0x00);
9144 	snd_hda_codec_write(codec, 0x07, 0, 0x797, 0x00);
9145 	snd_hda_codec_write(codec, 0x07, 0, 0x798, 0x00);
9146 }
9147 
9148 /*
9149  * Extra commands that don't really fit anywhere else.
9150  */
9151 static void sbz_pre_dsp_setup(struct hda_codec *codec)
9152 {
9153 	struct ca0132_spec *spec = codec->spec;
9154 
9155 	writel(0x00820680, spec->mem_base + 0x01C);
9156 	writel(0x00820680, spec->mem_base + 0x01C);
9157 
9158 	chipio_write(codec, 0x18b0a4, 0x000000c2);
9159 
9160 	snd_hda_codec_write(codec, 0x11, 0,
9161 			AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
9162 }
9163 
9164 static void r3d_pre_dsp_setup(struct hda_codec *codec)
9165 {
9166 	chipio_write(codec, 0x18b0a4, 0x000000c2);
9167 
9168 	chipio_8051_write_exram(codec, 0x1c1e, 0x5b);
9169 
9170 	snd_hda_codec_write(codec, 0x11, 0,
9171 			AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
9172 }
9173 
9174 static void r3di_pre_dsp_setup(struct hda_codec *codec)
9175 {
9176 	chipio_write(codec, 0x18b0a4, 0x000000c2);
9177 
9178 	chipio_8051_write_exram(codec, 0x1c1e, 0x5b);
9179 	chipio_8051_write_exram(codec, 0x1920, 0x00);
9180 	chipio_8051_write_exram(codec, 0x1921, 0x40);
9181 
9182 	snd_hda_codec_write(codec, 0x11, 0,
9183 			AC_VERB_SET_PIN_WIDGET_CONTROL, 0x04);
9184 }
9185 
9186 /*
9187  * The ZxR seems to use alternative DAC's for the surround channels, which
9188  * require PLL PMU setup for the clock rate, I'm guessing. Without setting
9189  * this up, we get no audio out of the surround jacks.
9190  */
9191 static void zxr_pre_dsp_setup(struct hda_codec *codec)
9192 {
9193 	static const unsigned int addr[] = { 0x43, 0x40, 0x41, 0x42, 0x45 };
9194 	static const unsigned int data[] = { 0x08, 0x0c, 0x0b, 0x07, 0x0d };
9195 	unsigned int i;
9196 
9197 	chipio_write(codec, 0x189000, 0x0001f100);
9198 	msleep(50);
9199 	chipio_write(codec, 0x18900c, 0x0001f100);
9200 	msleep(50);
9201 
9202 	/*
9203 	 * This writes a RET instruction at the entry point of the function at
9204 	 * 0xfa92 in exram. This function seems to have something to do with
9205 	 * ASI. Might be some way to prevent the card from reconfiguring the
9206 	 * ASI stuff itself.
9207 	 */
9208 	chipio_8051_write_exram(codec, 0xfa92, 0x22);
9209 
9210 	chipio_8051_write_pll_pmu(codec, 0x51, 0x98);
9211 
9212 	snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x82);
9213 	chipio_set_control_param(codec, CONTROL_PARAM_ASI, 3);
9214 
9215 	chipio_write(codec, 0x18902c, 0x00000000);
9216 	msleep(50);
9217 	chipio_write(codec, 0x18902c, 0x00000003);
9218 	msleep(50);
9219 
9220 	for (i = 0; i < ARRAY_SIZE(addr); i++)
9221 		chipio_8051_write_pll_pmu(codec, addr[i], data[i]);
9222 }
9223 
9224 /*
9225  * These are sent before the DSP is downloaded. Not sure
9226  * what they do, or if they're necessary. Could possibly
9227  * be removed. Figure they're better to leave in.
9228  */
9229 static const unsigned int ca0113_mmio_init_address_sbz[] = {
9230 	0x400, 0x408, 0x40c, 0x01c, 0xc0c, 0xc00, 0xc04, 0xc0c, 0xc0c, 0xc0c,
9231 	0xc0c, 0xc08, 0xc08, 0xc08, 0xc08, 0xc08, 0xc04
9232 };
9233 
9234 static const unsigned int ca0113_mmio_init_data_sbz[] = {
9235 	0x00000030, 0x00000000, 0x00000003, 0x00000003, 0x00000003,
9236 	0x00000003, 0x000000c1, 0x000000f1, 0x00000001, 0x000000c7,
9237 	0x000000c1, 0x00000080
9238 };
9239 
9240 static const unsigned int ca0113_mmio_init_data_zxr[] = {
9241 	0x00000030, 0x00000000, 0x00000000, 0x00000003, 0x00000003,
9242 	0x00000003, 0x00000001, 0x000000f1, 0x00000001, 0x000000c7,
9243 	0x000000c1, 0x00000080
9244 };
9245 
9246 static const unsigned int ca0113_mmio_init_address_ae5[] = {
9247 	0x400, 0x42c, 0x46c, 0x4ac, 0x4ec, 0x43c, 0x47c, 0x4bc, 0x4fc, 0x408,
9248 	0x100, 0x410, 0x40c, 0x100, 0x100, 0x830, 0x86c, 0x800, 0x86c, 0x800,
9249 	0x804, 0x20c, 0x01c, 0xc0c, 0xc00, 0xc04, 0xc0c, 0xc0c, 0xc0c, 0xc0c,
9250 	0xc08, 0xc08, 0xc08, 0xc08, 0xc08, 0xc04, 0x01c
9251 };
9252 
9253 static const unsigned int ca0113_mmio_init_data_ae5[] = {
9254 	0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
9255 	0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
9256 	0x00000600, 0x00000014, 0x00000001, 0x0000060f, 0x0000070f,
9257 	0x00000aff, 0x00000000, 0x0000006b, 0x00000001, 0x0000006b,
9258 	0x00000057, 0x00800000, 0x00880680, 0x00000080, 0x00000030,
9259 	0x00000000, 0x00000000, 0x00000003, 0x00000003, 0x00000003,
9260 	0x00000001, 0x000000f1, 0x00000001, 0x000000c7, 0x000000c1,
9261 	0x00000080, 0x00880680
9262 };
9263 
9264 static void ca0132_mmio_init_sbz(struct hda_codec *codec)
9265 {
9266 	struct ca0132_spec *spec = codec->spec;
9267 	unsigned int tmp[2], i, count, cur_addr;
9268 	const unsigned int *addr, *data;
9269 
9270 	addr = ca0113_mmio_init_address_sbz;
9271 	for (i = 0; i < 3; i++)
9272 		writel(0x00000000, spec->mem_base + addr[i]);
9273 
9274 	cur_addr = i;
9275 	switch (ca0132_quirk(spec)) {
9276 	case QUIRK_ZXR:
9277 		tmp[0] = 0x00880480;
9278 		tmp[1] = 0x00000080;
9279 		break;
9280 	case QUIRK_SBZ:
9281 		tmp[0] = 0x00820680;
9282 		tmp[1] = 0x00000083;
9283 		break;
9284 	case QUIRK_R3D:
9285 		tmp[0] = 0x00880680;
9286 		tmp[1] = 0x00000083;
9287 		break;
9288 	default:
9289 		tmp[0] = 0x00000000;
9290 		tmp[1] = 0x00000000;
9291 		break;
9292 	}
9293 
9294 	for (i = 0; i < 2; i++)
9295 		writel(tmp[i], spec->mem_base + addr[cur_addr + i]);
9296 
9297 	cur_addr += i;
9298 
9299 	switch (ca0132_quirk(spec)) {
9300 	case QUIRK_ZXR:
9301 		count = ARRAY_SIZE(ca0113_mmio_init_data_zxr);
9302 		data = ca0113_mmio_init_data_zxr;
9303 		break;
9304 	default:
9305 		count = ARRAY_SIZE(ca0113_mmio_init_data_sbz);
9306 		data = ca0113_mmio_init_data_sbz;
9307 		break;
9308 	}
9309 
9310 	for (i = 0; i < count; i++)
9311 		writel(data[i], spec->mem_base + addr[cur_addr + i]);
9312 }
9313 
9314 static void ca0132_mmio_init_ae5(struct hda_codec *codec)
9315 {
9316 	struct ca0132_spec *spec = codec->spec;
9317 	const unsigned int *addr, *data;
9318 	unsigned int i, count;
9319 
9320 	addr = ca0113_mmio_init_address_ae5;
9321 	data = ca0113_mmio_init_data_ae5;
9322 	count = ARRAY_SIZE(ca0113_mmio_init_data_ae5);
9323 
9324 	if (ca0132_quirk(spec) == QUIRK_AE7) {
9325 		writel(0x00000680, spec->mem_base + 0x1c);
9326 		writel(0x00880680, spec->mem_base + 0x1c);
9327 	}
9328 
9329 	for (i = 0; i < count; i++) {
9330 		/*
9331 		 * AE-7 shares all writes with the AE-5, except that it writes
9332 		 * a different value to 0x20c.
9333 		 */
9334 		if (i == 21 && ca0132_quirk(spec) == QUIRK_AE7) {
9335 			writel(0x00800001, spec->mem_base + addr[i]);
9336 			continue;
9337 		}
9338 
9339 		writel(data[i], spec->mem_base + addr[i]);
9340 	}
9341 
9342 	if (ca0132_quirk(spec) == QUIRK_AE5)
9343 		writel(0x00880680, spec->mem_base + 0x1c);
9344 }
9345 
9346 static void ca0132_mmio_init(struct hda_codec *codec)
9347 {
9348 	struct ca0132_spec *spec = codec->spec;
9349 
9350 	switch (ca0132_quirk(spec)) {
9351 	case QUIRK_R3D:
9352 	case QUIRK_SBZ:
9353 	case QUIRK_ZXR:
9354 		ca0132_mmio_init_sbz(codec);
9355 		break;
9356 	case QUIRK_AE5:
9357 		ca0132_mmio_init_ae5(codec);
9358 		break;
9359 	default:
9360 		break;
9361 	}
9362 }
9363 
9364 static const unsigned int ca0132_ae5_register_set_addresses[] = {
9365 	0x304, 0x304, 0x304, 0x304, 0x100, 0x304, 0x100, 0x304, 0x100, 0x304,
9366 	0x100, 0x304, 0x86c, 0x800, 0x86c, 0x800, 0x804
9367 };
9368 
9369 static const unsigned char ca0132_ae5_register_set_data[] = {
9370 	0x0f, 0x0e, 0x1f, 0x0c, 0x3f, 0x08, 0x7f, 0x00, 0xff, 0x00, 0x6b,
9371 	0x01, 0x6b, 0x57
9372 };
9373 
9374 /*
9375  * This function writes to some SFR's, does some region2 writes, and then
9376  * eventually resets the codec with the 0x7ff verb. Not quite sure why it does
9377  * what it does.
9378  */
9379 static void ae5_register_set(struct hda_codec *codec)
9380 {
9381 	struct ca0132_spec *spec = codec->spec;
9382 	unsigned int count = ARRAY_SIZE(ca0132_ae5_register_set_addresses);
9383 	const unsigned int *addr = ca0132_ae5_register_set_addresses;
9384 	const unsigned char *data = ca0132_ae5_register_set_data;
9385 	unsigned int i, cur_addr;
9386 	unsigned char tmp[3];
9387 
9388 	if (ca0132_quirk(spec) == QUIRK_AE7)
9389 		chipio_8051_write_pll_pmu(codec, 0x41, 0xc8);
9390 
9391 	chipio_8051_write_direct(codec, 0x93, 0x10);
9392 	chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
9393 
9394 	if (ca0132_quirk(spec) == QUIRK_AE7) {
9395 		tmp[0] = 0x03;
9396 		tmp[1] = 0x03;
9397 		tmp[2] = 0x07;
9398 	} else {
9399 		tmp[0] = 0x0f;
9400 		tmp[1] = 0x0f;
9401 		tmp[2] = 0x0f;
9402 	}
9403 
9404 	for (i = cur_addr = 0; i < 3; i++, cur_addr++)
9405 		writeb(tmp[i], spec->mem_base + addr[cur_addr]);
9406 
9407 	/*
9408 	 * First writes are in single bytes, final are in 4 bytes. So, we use
9409 	 * writeb, then writel.
9410 	 */
9411 	for (i = 0; cur_addr < 12; i++, cur_addr++)
9412 		writeb(data[i], spec->mem_base + addr[cur_addr]);
9413 
9414 	for (; cur_addr < count; i++, cur_addr++)
9415 		writel(data[i], spec->mem_base + addr[cur_addr]);
9416 
9417 	writel(0x00800001, spec->mem_base + 0x20c);
9418 
9419 	if (ca0132_quirk(spec) == QUIRK_AE7) {
9420 		ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
9421 		ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
9422 	} else {
9423 		ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x3f);
9424 	}
9425 
9426 	chipio_8051_write_direct(codec, 0x90, 0x00);
9427 	chipio_8051_write_direct(codec, 0x90, 0x10);
9428 
9429 	if (ca0132_quirk(spec) == QUIRK_AE5)
9430 		ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
9431 }
9432 
9433 /*
9434  * Extra init functions for alternative ca0132 codecs. Done
9435  * here so they don't clutter up the main ca0132_init function
9436  * anymore than they have to.
9437  */
9438 static void ca0132_alt_init(struct hda_codec *codec)
9439 {
9440 	struct ca0132_spec *spec = codec->spec;
9441 
9442 	ca0132_alt_vol_setup(codec);
9443 
9444 	switch (ca0132_quirk(spec)) {
9445 	case QUIRK_SBZ:
9446 		codec_dbg(codec, "SBZ alt_init");
9447 		ca0132_gpio_init(codec);
9448 		sbz_pre_dsp_setup(codec);
9449 		snd_hda_sequence_write(codec, spec->chip_init_verbs);
9450 		snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9451 		break;
9452 	case QUIRK_R3DI:
9453 		codec_dbg(codec, "R3DI alt_init");
9454 		ca0132_gpio_init(codec);
9455 		ca0132_gpio_setup(codec);
9456 		r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADING);
9457 		r3di_pre_dsp_setup(codec);
9458 		snd_hda_sequence_write(codec, spec->chip_init_verbs);
9459 		snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x6FF, 0xC4);
9460 		break;
9461 	case QUIRK_R3D:
9462 		r3d_pre_dsp_setup(codec);
9463 		snd_hda_sequence_write(codec, spec->chip_init_verbs);
9464 		snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9465 		break;
9466 	case QUIRK_AE5:
9467 		ca0132_gpio_init(codec);
9468 		chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
9469 		chipio_write(codec, 0x18b030, 0x00000020);
9470 		snd_hda_sequence_write(codec, spec->chip_init_verbs);
9471 		snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9472 		ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
9473 		break;
9474 	case QUIRK_AE7:
9475 		ca0132_gpio_init(codec);
9476 		chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
9477 		snd_hda_sequence_write(codec, spec->chip_init_verbs);
9478 		snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9479 		chipio_write(codec, 0x18b008, 0x000000f8);
9480 		chipio_write(codec, 0x18b008, 0x000000f0);
9481 		chipio_write(codec, 0x18b030, 0x00000020);
9482 		ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
9483 		break;
9484 	case QUIRK_ZXR:
9485 		chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
9486 		snd_hda_sequence_write(codec, spec->chip_init_verbs);
9487 		snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9488 		zxr_pre_dsp_setup(codec);
9489 		break;
9490 	default:
9491 		break;
9492 	}
9493 }
9494 
9495 static int ca0132_init(struct hda_codec *codec)
9496 {
9497 	struct ca0132_spec *spec = codec->spec;
9498 	struct auto_pin_cfg *cfg = &spec->autocfg;
9499 	int i;
9500 	bool dsp_loaded;
9501 
9502 	/*
9503 	 * If the DSP is already downloaded, and init has been entered again,
9504 	 * there's only two reasons for it. One, the codec has awaken from a
9505 	 * suspended state, and in that case dspload_is_loaded will return
9506 	 * false, and the init will be ran again. The other reason it gets
9507 	 * re entered is on startup for some reason it triggers a suspend and
9508 	 * resume state. In this case, it will check if the DSP is downloaded,
9509 	 * and not run the init function again. For codecs using alt_functions,
9510 	 * it will check if the DSP is loaded properly.
9511 	 */
9512 	if (spec->dsp_state == DSP_DOWNLOADED) {
9513 		dsp_loaded = dspload_is_loaded(codec);
9514 		if (!dsp_loaded) {
9515 			spec->dsp_reload = true;
9516 			spec->dsp_state = DSP_DOWNLOAD_INIT;
9517 		} else {
9518 			if (ca0132_quirk(spec) == QUIRK_SBZ)
9519 				sbz_dsp_startup_check(codec);
9520 			return 0;
9521 		}
9522 	}
9523 
9524 	if (spec->dsp_state != DSP_DOWNLOAD_FAILED)
9525 		spec->dsp_state = DSP_DOWNLOAD_INIT;
9526 	spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
9527 
9528 	if (ca0132_use_pci_mmio(spec))
9529 		ca0132_mmio_init(codec);
9530 
9531 	snd_hda_power_up_pm(codec);
9532 
9533 	if (ca0132_quirk(spec) == QUIRK_AE5 || ca0132_quirk(spec) == QUIRK_AE7)
9534 		ae5_register_set(codec);
9535 
9536 	ca0132_init_params(codec);
9537 	ca0132_init_flags(codec);
9538 
9539 	snd_hda_sequence_write(codec, spec->base_init_verbs);
9540 
9541 	if (ca0132_use_alt_functions(spec))
9542 		ca0132_alt_init(codec);
9543 
9544 	ca0132_download_dsp(codec);
9545 
9546 	ca0132_refresh_widget_caps(codec);
9547 
9548 	switch (ca0132_quirk(spec)) {
9549 	case QUIRK_R3DI:
9550 	case QUIRK_R3D:
9551 		r3d_setup_defaults(codec);
9552 		break;
9553 	case QUIRK_SBZ:
9554 	case QUIRK_ZXR:
9555 		sbz_setup_defaults(codec);
9556 		break;
9557 	case QUIRK_AE5:
9558 		ae5_setup_defaults(codec);
9559 		break;
9560 	case QUIRK_AE7:
9561 		ae7_setup_defaults(codec);
9562 		break;
9563 	default:
9564 		ca0132_setup_defaults(codec);
9565 		ca0132_init_analog_mic2(codec);
9566 		ca0132_init_dmic(codec);
9567 		break;
9568 	}
9569 
9570 	for (i = 0; i < spec->num_outputs; i++)
9571 		init_output(codec, spec->out_pins[i], spec->dacs[0]);
9572 
9573 	init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
9574 
9575 	for (i = 0; i < spec->num_inputs; i++)
9576 		init_input(codec, spec->input_pins[i], spec->adcs[i]);
9577 
9578 	init_input(codec, cfg->dig_in_pin, spec->dig_in);
9579 
9580 	if (!ca0132_use_alt_functions(spec)) {
9581 		snd_hda_sequence_write(codec, spec->chip_init_verbs);
9582 		snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
9583 			    VENDOR_CHIPIO_PARAM_EX_ID_SET, 0x0D);
9584 		snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
9585 			    VENDOR_CHIPIO_PARAM_EX_VALUE_SET, 0x20);
9586 	}
9587 
9588 	if (ca0132_quirk(spec) == QUIRK_SBZ)
9589 		ca0132_gpio_setup(codec);
9590 
9591 	snd_hda_sequence_write(codec, spec->spec_init_verbs);
9592 	if (ca0132_use_alt_functions(spec)) {
9593 		ca0132_alt_select_out(codec);
9594 		ca0132_alt_select_in(codec);
9595 	} else {
9596 		ca0132_select_out(codec);
9597 		ca0132_select_mic(codec);
9598 	}
9599 
9600 	snd_hda_jack_report_sync(codec);
9601 
9602 	/*
9603 	 * Re set the PlayEnhancement switch on a resume event, because the
9604 	 * controls will not be reloaded.
9605 	 */
9606 	if (spec->dsp_reload) {
9607 		spec->dsp_reload = false;
9608 		ca0132_pe_switch_set(codec);
9609 	}
9610 
9611 	snd_hda_power_down_pm(codec);
9612 
9613 	return 0;
9614 }
9615 
9616 static int dbpro_init(struct hda_codec *codec)
9617 {
9618 	struct ca0132_spec *spec = codec->spec;
9619 	struct auto_pin_cfg *cfg = &spec->autocfg;
9620 	unsigned int i;
9621 
9622 	init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
9623 	init_input(codec, cfg->dig_in_pin, spec->dig_in);
9624 
9625 	for (i = 0; i < spec->num_inputs; i++)
9626 		init_input(codec, spec->input_pins[i], spec->adcs[i]);
9627 
9628 	return 0;
9629 }
9630 
9631 static void ca0132_free(struct hda_codec *codec)
9632 {
9633 	struct ca0132_spec *spec = codec->spec;
9634 
9635 	cancel_delayed_work_sync(&spec->unsol_hp_work);
9636 	snd_hda_power_up(codec);
9637 	switch (ca0132_quirk(spec)) {
9638 	case QUIRK_SBZ:
9639 		sbz_exit_chip(codec);
9640 		break;
9641 	case QUIRK_ZXR:
9642 		zxr_exit_chip(codec);
9643 		break;
9644 	case QUIRK_R3D:
9645 		r3d_exit_chip(codec);
9646 		break;
9647 	case QUIRK_AE5:
9648 		ae5_exit_chip(codec);
9649 		break;
9650 	case QUIRK_AE7:
9651 		ae7_exit_chip(codec);
9652 		break;
9653 	case QUIRK_R3DI:
9654 		r3di_gpio_shutdown(codec);
9655 		break;
9656 	default:
9657 		break;
9658 	}
9659 
9660 	snd_hda_sequence_write(codec, spec->base_exit_verbs);
9661 	ca0132_exit_chip(codec);
9662 
9663 	snd_hda_power_down(codec);
9664 #ifdef CONFIG_PCI
9665 	if (spec->mem_base)
9666 		pci_iounmap(codec->bus->pci, spec->mem_base);
9667 #endif
9668 	kfree(spec->spec_init_verbs);
9669 	kfree(codec->spec);
9670 }
9671 
9672 static void dbpro_free(struct hda_codec *codec)
9673 {
9674 	struct ca0132_spec *spec = codec->spec;
9675 
9676 	zxr_dbpro_power_state_shutdown(codec);
9677 
9678 	kfree(spec->spec_init_verbs);
9679 	kfree(codec->spec);
9680 }
9681 
9682 #ifdef CONFIG_PM
9683 static int ca0132_suspend(struct hda_codec *codec)
9684 {
9685 	struct ca0132_spec *spec = codec->spec;
9686 
9687 	cancel_delayed_work_sync(&spec->unsol_hp_work);
9688 	return 0;
9689 }
9690 #endif
9691 
9692 static const struct hda_codec_ops ca0132_patch_ops = {
9693 	.build_controls = ca0132_build_controls,
9694 	.build_pcms = ca0132_build_pcms,
9695 	.init = ca0132_init,
9696 	.free = ca0132_free,
9697 	.unsol_event = snd_hda_jack_unsol_event,
9698 #ifdef CONFIG_PM
9699 	.suspend = ca0132_suspend,
9700 #endif
9701 };
9702 
9703 static const struct hda_codec_ops dbpro_patch_ops = {
9704 	.build_controls = dbpro_build_controls,
9705 	.build_pcms = dbpro_build_pcms,
9706 	.init = dbpro_init,
9707 	.free = dbpro_free,
9708 };
9709 
9710 static void ca0132_config(struct hda_codec *codec)
9711 {
9712 	struct ca0132_spec *spec = codec->spec;
9713 
9714 	spec->dacs[0] = 0x2;
9715 	spec->dacs[1] = 0x3;
9716 	spec->dacs[2] = 0x4;
9717 
9718 	spec->multiout.dac_nids = spec->dacs;
9719 	spec->multiout.num_dacs = 3;
9720 
9721 	if (!ca0132_use_alt_functions(spec))
9722 		spec->multiout.max_channels = 2;
9723 	else
9724 		spec->multiout.max_channels = 6;
9725 
9726 	switch (ca0132_quirk(spec)) {
9727 	case QUIRK_ALIENWARE:
9728 		codec_dbg(codec, "%s: QUIRK_ALIENWARE applied.\n", __func__);
9729 		snd_hda_apply_pincfgs(codec, alienware_pincfgs);
9730 		break;
9731 	case QUIRK_SBZ:
9732 		codec_dbg(codec, "%s: QUIRK_SBZ applied.\n", __func__);
9733 		snd_hda_apply_pincfgs(codec, sbz_pincfgs);
9734 		break;
9735 	case QUIRK_ZXR:
9736 		codec_dbg(codec, "%s: QUIRK_ZXR applied.\n", __func__);
9737 		snd_hda_apply_pincfgs(codec, zxr_pincfgs);
9738 		break;
9739 	case QUIRK_R3D:
9740 		codec_dbg(codec, "%s: QUIRK_R3D applied.\n", __func__);
9741 		snd_hda_apply_pincfgs(codec, r3d_pincfgs);
9742 		break;
9743 	case QUIRK_R3DI:
9744 		codec_dbg(codec, "%s: QUIRK_R3DI applied.\n", __func__);
9745 		snd_hda_apply_pincfgs(codec, r3di_pincfgs);
9746 		break;
9747 	case QUIRK_AE5:
9748 		codec_dbg(codec, "%s: QUIRK_AE5 applied.\n", __func__);
9749 		snd_hda_apply_pincfgs(codec, ae5_pincfgs);
9750 		break;
9751 	case QUIRK_AE7:
9752 		codec_dbg(codec, "%s: QUIRK_AE7 applied.\n", __func__);
9753 		snd_hda_apply_pincfgs(codec, ae7_pincfgs);
9754 		break;
9755 	default:
9756 		break;
9757 	}
9758 
9759 	switch (ca0132_quirk(spec)) {
9760 	case QUIRK_ALIENWARE:
9761 		spec->num_outputs = 2;
9762 		spec->out_pins[0] = 0x0b; /* speaker out */
9763 		spec->out_pins[1] = 0x0f;
9764 		spec->shared_out_nid = 0x2;
9765 		spec->unsol_tag_hp = 0x0f;
9766 
9767 		spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
9768 		spec->adcs[1] = 0x8; /* analog mic2 */
9769 		spec->adcs[2] = 0xa; /* what u hear */
9770 
9771 		spec->num_inputs = 3;
9772 		spec->input_pins[0] = 0x12;
9773 		spec->input_pins[1] = 0x11;
9774 		spec->input_pins[2] = 0x13;
9775 		spec->shared_mic_nid = 0x7;
9776 		spec->unsol_tag_amic1 = 0x11;
9777 		break;
9778 	case QUIRK_SBZ:
9779 	case QUIRK_R3D:
9780 		spec->num_outputs = 2;
9781 		spec->out_pins[0] = 0x0B; /* Line out */
9782 		spec->out_pins[1] = 0x0F; /* Rear headphone out */
9783 		spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
9784 		spec->out_pins[3] = 0x11; /* Rear surround */
9785 		spec->shared_out_nid = 0x2;
9786 		spec->unsol_tag_hp = spec->out_pins[1];
9787 		spec->unsol_tag_front_hp = spec->out_pins[2];
9788 
9789 		spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
9790 		spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
9791 		spec->adcs[2] = 0xa; /* what u hear */
9792 
9793 		spec->num_inputs = 2;
9794 		spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9795 		spec->input_pins[1] = 0x13; /* What U Hear */
9796 		spec->shared_mic_nid = 0x7;
9797 		spec->unsol_tag_amic1 = spec->input_pins[0];
9798 
9799 		/* SPDIF I/O */
9800 		spec->dig_out = 0x05;
9801 		spec->multiout.dig_out_nid = spec->dig_out;
9802 		spec->dig_in = 0x09;
9803 		break;
9804 	case QUIRK_ZXR:
9805 		spec->num_outputs = 2;
9806 		spec->out_pins[0] = 0x0B; /* Line out */
9807 		spec->out_pins[1] = 0x0F; /* Rear headphone out */
9808 		spec->out_pins[2] = 0x10; /* Center/LFE */
9809 		spec->out_pins[3] = 0x11; /* Rear surround */
9810 		spec->shared_out_nid = 0x2;
9811 		spec->unsol_tag_hp = spec->out_pins[1];
9812 		spec->unsol_tag_front_hp = spec->out_pins[2];
9813 
9814 		spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
9815 		spec->adcs[1] = 0x8; /* Not connected, no front mic */
9816 		spec->adcs[2] = 0xa; /* what u hear */
9817 
9818 		spec->num_inputs = 2;
9819 		spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9820 		spec->input_pins[1] = 0x13; /* What U Hear */
9821 		spec->shared_mic_nid = 0x7;
9822 		spec->unsol_tag_amic1 = spec->input_pins[0];
9823 		break;
9824 	case QUIRK_ZXR_DBPRO:
9825 		spec->adcs[0] = 0x8; /* ZxR DBPro Aux In */
9826 
9827 		spec->num_inputs = 1;
9828 		spec->input_pins[0] = 0x11; /* RCA Line-in */
9829 
9830 		spec->dig_out = 0x05;
9831 		spec->multiout.dig_out_nid = spec->dig_out;
9832 
9833 		spec->dig_in = 0x09;
9834 		break;
9835 	case QUIRK_AE5:
9836 	case QUIRK_AE7:
9837 		spec->num_outputs = 2;
9838 		spec->out_pins[0] = 0x0B; /* Line out */
9839 		spec->out_pins[1] = 0x11; /* Rear headphone out */
9840 		spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
9841 		spec->out_pins[3] = 0x0F; /* Rear surround */
9842 		spec->shared_out_nid = 0x2;
9843 		spec->unsol_tag_hp = spec->out_pins[1];
9844 		spec->unsol_tag_front_hp = spec->out_pins[2];
9845 
9846 		spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
9847 		spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
9848 		spec->adcs[2] = 0xa; /* what u hear */
9849 
9850 		spec->num_inputs = 2;
9851 		spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9852 		spec->input_pins[1] = 0x13; /* What U Hear */
9853 		spec->shared_mic_nid = 0x7;
9854 		spec->unsol_tag_amic1 = spec->input_pins[0];
9855 
9856 		/* SPDIF I/O */
9857 		spec->dig_out = 0x05;
9858 		spec->multiout.dig_out_nid = spec->dig_out;
9859 		break;
9860 	case QUIRK_R3DI:
9861 		spec->num_outputs = 2;
9862 		spec->out_pins[0] = 0x0B; /* Line out */
9863 		spec->out_pins[1] = 0x0F; /* Rear headphone out */
9864 		spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
9865 		spec->out_pins[3] = 0x11; /* Rear surround */
9866 		spec->shared_out_nid = 0x2;
9867 		spec->unsol_tag_hp = spec->out_pins[1];
9868 		spec->unsol_tag_front_hp = spec->out_pins[2];
9869 
9870 		spec->adcs[0] = 0x07; /* Rear Mic / Line-in */
9871 		spec->adcs[1] = 0x08; /* Front Mic, but only if no DSP */
9872 		spec->adcs[2] = 0x0a; /* what u hear */
9873 
9874 		spec->num_inputs = 2;
9875 		spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9876 		spec->input_pins[1] = 0x13; /* What U Hear */
9877 		spec->shared_mic_nid = 0x7;
9878 		spec->unsol_tag_amic1 = spec->input_pins[0];
9879 
9880 		/* SPDIF I/O */
9881 		spec->dig_out = 0x05;
9882 		spec->multiout.dig_out_nid = spec->dig_out;
9883 		break;
9884 	default:
9885 		spec->num_outputs = 2;
9886 		spec->out_pins[0] = 0x0b; /* speaker out */
9887 		spec->out_pins[1] = 0x10; /* headphone out */
9888 		spec->shared_out_nid = 0x2;
9889 		spec->unsol_tag_hp = spec->out_pins[1];
9890 
9891 		spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
9892 		spec->adcs[1] = 0x8; /* analog mic2 */
9893 		spec->adcs[2] = 0xa; /* what u hear */
9894 
9895 		spec->num_inputs = 3;
9896 		spec->input_pins[0] = 0x12;
9897 		spec->input_pins[1] = 0x11;
9898 		spec->input_pins[2] = 0x13;
9899 		spec->shared_mic_nid = 0x7;
9900 		spec->unsol_tag_amic1 = spec->input_pins[0];
9901 
9902 		/* SPDIF I/O */
9903 		spec->dig_out = 0x05;
9904 		spec->multiout.dig_out_nid = spec->dig_out;
9905 		spec->dig_in = 0x09;
9906 		break;
9907 	}
9908 }
9909 
9910 static int ca0132_prepare_verbs(struct hda_codec *codec)
9911 {
9912 /* Verbs + terminator (an empty element) */
9913 #define NUM_SPEC_VERBS 2
9914 	struct ca0132_spec *spec = codec->spec;
9915 
9916 	spec->chip_init_verbs = ca0132_init_verbs0;
9917 	/*
9918 	 * Since desktop cards use pci_mmio, this can be used to determine
9919 	 * whether or not to use these verbs instead of a separate bool.
9920 	 */
9921 	if (ca0132_use_pci_mmio(spec))
9922 		spec->desktop_init_verbs = ca0132_init_verbs1;
9923 	spec->spec_init_verbs = kcalloc(NUM_SPEC_VERBS,
9924 					sizeof(struct hda_verb),
9925 					GFP_KERNEL);
9926 	if (!spec->spec_init_verbs)
9927 		return -ENOMEM;
9928 
9929 	/* config EAPD */
9930 	spec->spec_init_verbs[0].nid = 0x0b;
9931 	spec->spec_init_verbs[0].param = 0x78D;
9932 	spec->spec_init_verbs[0].verb = 0x00;
9933 
9934 	/* Previously commented configuration */
9935 	/*
9936 	spec->spec_init_verbs[2].nid = 0x0b;
9937 	spec->spec_init_verbs[2].param = AC_VERB_SET_EAPD_BTLENABLE;
9938 	spec->spec_init_verbs[2].verb = 0x02;
9939 
9940 	spec->spec_init_verbs[3].nid = 0x10;
9941 	spec->spec_init_verbs[3].param = 0x78D;
9942 	spec->spec_init_verbs[3].verb = 0x02;
9943 
9944 	spec->spec_init_verbs[4].nid = 0x10;
9945 	spec->spec_init_verbs[4].param = AC_VERB_SET_EAPD_BTLENABLE;
9946 	spec->spec_init_verbs[4].verb = 0x02;
9947 	*/
9948 
9949 	/* Terminator: spec->spec_init_verbs[NUM_SPEC_VERBS-1] */
9950 	return 0;
9951 }
9952 
9953 /*
9954  * The Sound Blaster ZxR shares the same PCI subsystem ID as some regular
9955  * Sound Blaster Z cards. However, they have different HDA codec subsystem
9956  * ID's. So, we check for the ZxR's subsystem ID, as well as the DBPro
9957  * daughter boards ID.
9958  */
9959 static void sbz_detect_quirk(struct hda_codec *codec)
9960 {
9961 	struct ca0132_spec *spec = codec->spec;
9962 
9963 	switch (codec->core.subsystem_id) {
9964 	case 0x11020033:
9965 		spec->quirk = QUIRK_ZXR;
9966 		break;
9967 	case 0x1102003f:
9968 		spec->quirk = QUIRK_ZXR_DBPRO;
9969 		break;
9970 	default:
9971 		spec->quirk = QUIRK_SBZ;
9972 		break;
9973 	}
9974 }
9975 
9976 static int patch_ca0132(struct hda_codec *codec)
9977 {
9978 	struct ca0132_spec *spec;
9979 	int err;
9980 	const struct snd_pci_quirk *quirk;
9981 
9982 	codec_dbg(codec, "patch_ca0132\n");
9983 
9984 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
9985 	if (!spec)
9986 		return -ENOMEM;
9987 	codec->spec = spec;
9988 	spec->codec = codec;
9989 
9990 	/* Detect codec quirk */
9991 	quirk = snd_pci_quirk_lookup(codec->bus->pci, ca0132_quirks);
9992 	if (quirk)
9993 		spec->quirk = quirk->value;
9994 	else
9995 		spec->quirk = QUIRK_NONE;
9996 	if (ca0132_quirk(spec) == QUIRK_SBZ)
9997 		sbz_detect_quirk(codec);
9998 
9999 	if (ca0132_quirk(spec) == QUIRK_ZXR_DBPRO)
10000 		codec->patch_ops = dbpro_patch_ops;
10001 	else
10002 		codec->patch_ops = ca0132_patch_ops;
10003 
10004 	codec->pcm_format_first = 1;
10005 	codec->no_sticky_stream = 1;
10006 
10007 
10008 	spec->dsp_state = DSP_DOWNLOAD_INIT;
10009 	spec->num_mixers = 1;
10010 
10011 	/* Set which mixers each quirk uses. */
10012 	switch (ca0132_quirk(spec)) {
10013 	case QUIRK_SBZ:
10014 		spec->mixers[0] = desktop_mixer;
10015 		snd_hda_codec_set_name(codec, "Sound Blaster Z");
10016 		break;
10017 	case QUIRK_ZXR:
10018 		spec->mixers[0] = desktop_mixer;
10019 		snd_hda_codec_set_name(codec, "Sound Blaster ZxR");
10020 		break;
10021 	case QUIRK_ZXR_DBPRO:
10022 		break;
10023 	case QUIRK_R3D:
10024 		spec->mixers[0] = desktop_mixer;
10025 		snd_hda_codec_set_name(codec, "Recon3D");
10026 		break;
10027 	case QUIRK_R3DI:
10028 		spec->mixers[0] = r3di_mixer;
10029 		snd_hda_codec_set_name(codec, "Recon3Di");
10030 		break;
10031 	case QUIRK_AE5:
10032 		spec->mixers[0] = desktop_mixer;
10033 		snd_hda_codec_set_name(codec, "Sound BlasterX AE-5");
10034 		break;
10035 	case QUIRK_AE7:
10036 		spec->mixers[0] = desktop_mixer;
10037 		snd_hda_codec_set_name(codec, "Sound Blaster AE-7");
10038 		break;
10039 	default:
10040 		spec->mixers[0] = ca0132_mixer;
10041 		break;
10042 	}
10043 
10044 	/* Setup whether or not to use alt functions/controls/pci_mmio */
10045 	switch (ca0132_quirk(spec)) {
10046 	case QUIRK_SBZ:
10047 	case QUIRK_R3D:
10048 	case QUIRK_AE5:
10049 	case QUIRK_AE7:
10050 	case QUIRK_ZXR:
10051 		spec->use_alt_controls = true;
10052 		spec->use_alt_functions = true;
10053 		spec->use_pci_mmio = true;
10054 		break;
10055 	case QUIRK_R3DI:
10056 		spec->use_alt_controls = true;
10057 		spec->use_alt_functions = true;
10058 		spec->use_pci_mmio = false;
10059 		break;
10060 	default:
10061 		spec->use_alt_controls = false;
10062 		spec->use_alt_functions = false;
10063 		spec->use_pci_mmio = false;
10064 		break;
10065 	}
10066 
10067 #ifdef CONFIG_PCI
10068 	if (spec->use_pci_mmio) {
10069 		spec->mem_base = pci_iomap(codec->bus->pci, 2, 0xC20);
10070 		if (spec->mem_base == NULL) {
10071 			codec_warn(codec, "pci_iomap failed! Setting quirk to QUIRK_NONE.");
10072 			spec->quirk = QUIRK_NONE;
10073 		}
10074 	}
10075 #endif
10076 
10077 	spec->base_init_verbs = ca0132_base_init_verbs;
10078 	spec->base_exit_verbs = ca0132_base_exit_verbs;
10079 
10080 	INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
10081 
10082 	ca0132_init_chip(codec);
10083 
10084 	ca0132_config(codec);
10085 
10086 	err = ca0132_prepare_verbs(codec);
10087 	if (err < 0)
10088 		goto error;
10089 
10090 	err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
10091 	if (err < 0)
10092 		goto error;
10093 
10094 	ca0132_setup_unsol(codec);
10095 
10096 	return 0;
10097 
10098  error:
10099 	ca0132_free(codec);
10100 	return err;
10101 }
10102 
10103 /*
10104  * patch entries
10105  */
10106 static const struct hda_device_id snd_hda_id_ca0132[] = {
10107 	HDA_CODEC_ENTRY(0x11020011, "CA0132", patch_ca0132),
10108 	{} /* terminator */
10109 };
10110 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_ca0132);
10111 
10112 MODULE_LICENSE("GPL");
10113 MODULE_DESCRIPTION("Creative Sound Core3D codec");
10114 
10115 static struct hda_codec_driver ca0132_driver = {
10116 	.id = snd_hda_id_ca0132,
10117 };
10118 
10119 module_hda_codec_driver(ca0132_driver);
10120