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