Debian Codegraph @

sound/hda/codecs/ca0132.c

raw
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 <[email protected]>
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_write(codec, 0x01, 0,
3759 AC_VERB_SET_GPIO_DIRECTION, 0x07);
3760 snd_hda_codec_write(codec, 0x01, 0,
3761 AC_VERB_SET_GPIO_MASK, 0x07);
3762 snd_hda_codec_write(codec, 0x01, 0,
3763 AC_VERB_SET_GPIO_DATA, 0x04);
3764 snd_hda_codec_write(codec, 0x01, 0,
3765 AC_VERB_SET_GPIO_DATA, 0x06);
3766 break;
3767 case QUIRK_R3DI:
3768 snd_hda_codec_write(codec, 0x01, 0,
3769 AC_VERB_SET_GPIO_DIRECTION, 0x1E);
3770 snd_hda_codec_write(codec, 0x01, 0,
3771 AC_VERB_SET_GPIO_MASK, 0x1F);
3772 snd_hda_codec_write(codec, 0x01, 0,
3773 AC_VERB_SET_GPIO_DATA, 0x0C);
3774 break;
3775 default:
3776 break;
3777 }
3778 }
3779
3780 /*
3781 * GPIO control functions for the Recon3D integrated.
3782 */
3783
3784 enum r3di_gpio_bit {
3785 /* Bit 1 - Switch between front/rear mic. 0 = rear, 1 = front */
3786 R3DI_MIC_SELECT_BIT = 1,
3787 /* Bit 2 - Switch between headphone/line out. 0 = Headphone, 1 = Line */
3788 R3DI_OUT_SELECT_BIT = 2,
3789 /*
3790 * I dunno what this actually does, but it stays on until the dsp
3791 * is downloaded.
3792 */
3793 R3DI_GPIO_DSP_DOWNLOADING = 3,
3794 /*
3795 * Same as above, no clue what it does, but it comes on after the dsp
3796 * is downloaded.
3797 */
3798 R3DI_GPIO_DSP_DOWNLOADED = 4
3799 };
3800
3801 enum r3di_mic_select {
3802 /* Set GPIO bit 1 to 0 for rear mic */
3803 R3DI_REAR_MIC = 0,
3804 /* Set GPIO bit 1 to 1 for front microphone*/
3805 R3DI_FRONT_MIC = 1
3806 };
3807
3808 enum r3di_out_select {
3809 /* Set GPIO bit 2 to 0 for headphone */
3810 R3DI_HEADPHONE_OUT = 0,
3811 /* Set GPIO bit 2 to 1 for speaker */
3812 R3DI_LINE_OUT = 1
3813 };
3814 enum r3di_dsp_status {
3815 /* Set GPIO bit 3 to 1 until DSP is downloaded */
3816 R3DI_DSP_DOWNLOADING = 0,
3817 /* Set GPIO bit 4 to 1 once DSP is downloaded */
3818 R3DI_DSP_DOWNLOADED = 1
3819 };
3820
3821
3822 static void r3di_gpio_mic_set(struct hda_codec *codec,
3823 enum r3di_mic_select cur_mic)
3824 {
3825 unsigned int cur_gpio;
3826
3827 /* Get the current GPIO Data setup */
3828 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3829
3830 switch (cur_mic) {
3831 case R3DI_REAR_MIC:
3832 cur_gpio &= ~(1 << R3DI_MIC_SELECT_BIT);
3833 break;
3834 case R3DI_FRONT_MIC:
3835 cur_gpio |= (1 << R3DI_MIC_SELECT_BIT);
3836 break;
3837 }
3838 snd_hda_codec_write(codec, codec->core.afg, 0,
3839 AC_VERB_SET_GPIO_DATA, cur_gpio);
3840 }
3841
3842 static void r3di_gpio_dsp_status_set(struct hda_codec *codec,
3843 enum r3di_dsp_status dsp_status)
3844 {
3845 unsigned int cur_gpio;
3846
3847 /* Get the current GPIO Data setup */
3848 cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
3849
3850 switch (dsp_status) {
3851 case R3DI_DSP_DOWNLOADING:
3852 cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADING);
3853 snd_hda_codec_write(codec, codec->core.afg, 0,
3854 AC_VERB_SET_GPIO_DATA, cur_gpio);
3855 break;
3856 case R3DI_DSP_DOWNLOADED:
3857 /* Set DOWNLOADING bit to 0. */
3858 cur_gpio &= ~(1 << R3DI_GPIO_DSP_DOWNLOADING);
3859
3860 snd_hda_codec_write(codec, codec->core.afg, 0,
3861 AC_VERB_SET_GPIO_DATA, cur_gpio);
3862
3863 cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADED);
3864 break;
3865 }
3866
3867 snd_hda_codec_write(codec, codec->core.afg, 0,
3868 AC_VERB_SET_GPIO_DATA, cur_gpio);
3869 }
3870
3871 /*
3872 * PCM callbacks
3873 */
3874 static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3875 struct hda_codec *codec,
3876 unsigned int stream_tag,
3877 unsigned int format,
3878 struct snd_pcm_substream *substream)
3879 {
3880 struct ca0132_spec *spec = codec->spec;
3881
3882 snd_hda_codec_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
3883
3884 return 0;
3885 }
3886
3887 static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3888 struct hda_codec *codec,
3889 struct snd_pcm_substream *substream)
3890 {
3891 struct ca0132_spec *spec = codec->spec;
3892
3893 if (spec->dsp_state == DSP_DOWNLOADING)
3894 return 0;
3895
3896 /*If Playback effects are on, allow stream some time to flush
3897 *effects tail*/
3898 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
3899 msleep(50);
3900
3901 snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
3902
3903 return 0;
3904 }
3905
3906 static unsigned int ca0132_playback_pcm_delay(struct hda_pcm_stream *info,
3907 struct hda_codec *codec,
3908 struct snd_pcm_substream *substream)
3909 {
3910 struct ca0132_spec *spec = codec->spec;
3911 unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
3912 struct snd_pcm_runtime *runtime = substream->runtime;
3913
3914 if (spec->dsp_state != DSP_DOWNLOADED)
3915 return 0;
3916
3917 /* Add latency if playback enhancement and either effect is enabled. */
3918 if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
3919 if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
3920 (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
3921 latency += DSP_PLAY_ENHANCEMENT_LATENCY;
3922 }
3923
3924 /* Applying Speaker EQ adds latency as well. */
3925 if (spec->cur_out_type == SPEAKER_OUT)
3926 latency += DSP_SPEAKER_OUT_LATENCY;
3927
3928 return (latency * runtime->rate) / 1000;
3929 }
3930
3931 /*
3932 * Digital out
3933 */
3934 static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3935 struct hda_codec *codec,
3936 struct snd_pcm_substream *substream)
3937 {
3938 struct ca0132_spec *spec = codec->spec;
3939 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3940 }
3941
3942 static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3943 struct hda_codec *codec,
3944 unsigned int stream_tag,
3945 unsigned int format,
3946 struct snd_pcm_substream *substream)
3947 {
3948 struct ca0132_spec *spec = codec->spec;
3949 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3950 stream_tag, format, substream);
3951 }
3952
3953 static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3954 struct hda_codec *codec,
3955 struct snd_pcm_substream *substream)
3956 {
3957 struct ca0132_spec *spec = codec->spec;
3958 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3959 }
3960
3961 static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3962 struct hda_codec *codec,
3963 struct snd_pcm_substream *substream)
3964 {
3965 struct ca0132_spec *spec = codec->spec;
3966 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3967 }
3968
3969 /*
3970 * Analog capture
3971 */
3972 static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3973 struct hda_codec *codec,
3974 unsigned int stream_tag,
3975 unsigned int format,
3976 struct snd_pcm_substream *substream)
3977 {
3978 snd_hda_codec_setup_stream(codec, hinfo->nid,
3979 stream_tag, 0, format);
3980
3981 return 0;
3982 }
3983
3984 static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3985 struct hda_codec *codec,
3986 struct snd_pcm_substream *substream)
3987 {
3988 struct ca0132_spec *spec = codec->spec;
3989
3990 if (spec->dsp_state == DSP_DOWNLOADING)
3991 return 0;
3992
3993 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3994 return 0;
3995 }
3996
3997 static unsigned int ca0132_capture_pcm_delay(struct hda_pcm_stream *info,
3998 struct hda_codec *codec,
3999 struct snd_pcm_substream *substream)
4000 {
4001 struct ca0132_spec *spec = codec->spec;
4002 unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
4003 struct snd_pcm_runtime *runtime = substream->runtime;
4004
4005 if (spec->dsp_state != DSP_DOWNLOADED)
4006 return 0;
4007
4008 if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
4009 latency += DSP_CRYSTAL_VOICE_LATENCY;
4010
4011 return (latency * runtime->rate) / 1000;
4012 }
4013
4014 /*
4015 * Controls stuffs.
4016 */
4017
4018 /*
4019 * Mixer controls helpers.
4020 */
4021 #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
4022 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4023 .name = xname, \
4024 .subdevice = HDA_SUBDEV_AMP_FLAG, \
4025 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
4026 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
4027 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
4028 .info = ca0132_volume_info, \
4029 .get = ca0132_volume_get, \
4030 .put = ca0132_volume_put, \
4031 .tlv = { .c = ca0132_volume_tlv }, \
4032 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
4033
4034 /*
4035 * Creates a mixer control that uses defaults of HDA_CODEC_VOL except for the
4036 * volume put, which is used for setting the DSP volume. This was done because
4037 * the ca0132 functions were taking too much time and causing lag.
4038 */
4039 #define CA0132_ALT_CODEC_VOL_MONO(xname, nid, channel, dir) \
4040 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4041 .name = xname, \
4042 .subdevice = HDA_SUBDEV_AMP_FLAG, \
4043 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
4044 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
4045 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
4046 .info = snd_hda_mixer_amp_volume_info, \
4047 .get = snd_hda_mixer_amp_volume_get, \
4048 .put = ca0132_alt_volume_put, \
4049 .tlv = { .c = snd_hda_mixer_amp_tlv }, \
4050 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
4051
4052 #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
4053 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
4054 .name = xname, \
4055 .subdevice = HDA_SUBDEV_AMP_FLAG, \
4056 .info = snd_hda_mixer_amp_switch_info, \
4057 .get = ca0132_switch_get, \
4058 .put = ca0132_switch_put, \
4059 .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
4060
4061 /* stereo */
4062 #define CA0132_CODEC_VOL(xname, nid, dir) \
4063 CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
4064 #define CA0132_ALT_CODEC_VOL(xname, nid, dir) \
4065 CA0132_ALT_CODEC_VOL_MONO(xname, nid, 3, dir)
4066 #define CA0132_CODEC_MUTE(xname, nid, dir) \
4067 CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
4068
4069 /* lookup tables */
4070 /*
4071 * Lookup table with decibel values for the DSP. When volume is changed in
4072 * Windows, the DSP is also sent the dB value in floating point. In Windows,
4073 * these values have decimal points, probably because the Windows driver
4074 * actually uses floating point. We can't here, so I made a lookup table of
4075 * values -90 to 9. -90 is the lowest decibel value for both the ADC's and the
4076 * DAC's, and 9 is the maximum.
4077 */
4078 static const unsigned int float_vol_db_lookup[] = {
4079 0xC2B40000, 0xC2B20000, 0xC2B00000, 0xC2AE0000, 0xC2AC0000, 0xC2AA0000,
4080 0xC2A80000, 0xC2A60000, 0xC2A40000, 0xC2A20000, 0xC2A00000, 0xC29E0000,
4081 0xC29C0000, 0xC29A0000, 0xC2980000, 0xC2960000, 0xC2940000, 0xC2920000,
4082 0xC2900000, 0xC28E0000, 0xC28C0000, 0xC28A0000, 0xC2880000, 0xC2860000,
4083 0xC2840000, 0xC2820000, 0xC2800000, 0xC27C0000, 0xC2780000, 0xC2740000,
4084 0xC2700000, 0xC26C0000, 0xC2680000, 0xC2640000, 0xC2600000, 0xC25C0000,
4085 0xC2580000, 0xC2540000, 0xC2500000, 0xC24C0000, 0xC2480000, 0xC2440000,
4086 0xC2400000, 0xC23C0000, 0xC2380000, 0xC2340000, 0xC2300000, 0xC22C0000,
4087 0xC2280000, 0xC2240000, 0xC2200000, 0xC21C0000, 0xC2180000, 0xC2140000,
4088 0xC2100000, 0xC20C0000, 0xC2080000, 0xC2040000, 0xC2000000, 0xC1F80000,
4089 0xC1F00000, 0xC1E80000, 0xC1E00000, 0xC1D80000, 0xC1D00000, 0xC1C80000,
4090 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
4091 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
4092 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
4093 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
4094 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
4095 0x40C00000, 0x40E00000, 0x41000000, 0x41100000
4096 };
4097
4098 /*
4099 * This table counts from float 0 to 1 in increments of .01, which is
4100 * useful for a few different sliders.
4101 */
4102 static const unsigned int float_zero_to_one_lookup[] = {
4103 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
4104 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
4105 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
4106 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
4107 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
4108 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
4109 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
4110 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
4111 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
4112 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
4113 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
4114 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
4115 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
4116 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
4117 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
4118 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
4119 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
4120 };
4121
4122 /*
4123 * This table counts from float 10 to 1000, which is the range of the x-bass
4124 * crossover slider in Windows.
4125 */
4126 static const unsigned int float_xbass_xover_lookup[] = {
4127 0x41200000, 0x41A00000, 0x41F00000, 0x42200000, 0x42480000, 0x42700000,
4128 0x428C0000, 0x42A00000, 0x42B40000, 0x42C80000, 0x42DC0000, 0x42F00000,
4129 0x43020000, 0x430C0000, 0x43160000, 0x43200000, 0x432A0000, 0x43340000,
4130 0x433E0000, 0x43480000, 0x43520000, 0x435C0000, 0x43660000, 0x43700000,
4131 0x437A0000, 0x43820000, 0x43870000, 0x438C0000, 0x43910000, 0x43960000,
4132 0x439B0000, 0x43A00000, 0x43A50000, 0x43AA0000, 0x43AF0000, 0x43B40000,
4133 0x43B90000, 0x43BE0000, 0x43C30000, 0x43C80000, 0x43CD0000, 0x43D20000,
4134 0x43D70000, 0x43DC0000, 0x43E10000, 0x43E60000, 0x43EB0000, 0x43F00000,
4135 0x43F50000, 0x43FA0000, 0x43FF0000, 0x44020000, 0x44048000, 0x44070000,
4136 0x44098000, 0x440C0000, 0x440E8000, 0x44110000, 0x44138000, 0x44160000,
4137 0x44188000, 0x441B0000, 0x441D8000, 0x44200000, 0x44228000, 0x44250000,
4138 0x44278000, 0x442A0000, 0x442C8000, 0x442F0000, 0x44318000, 0x44340000,
4139 0x44368000, 0x44390000, 0x443B8000, 0x443E0000, 0x44408000, 0x44430000,
4140 0x44458000, 0x44480000, 0x444A8000, 0x444D0000, 0x444F8000, 0x44520000,
4141 0x44548000, 0x44570000, 0x44598000, 0x445C0000, 0x445E8000, 0x44610000,
4142 0x44638000, 0x44660000, 0x44688000, 0x446B0000, 0x446D8000, 0x44700000,
4143 0x44728000, 0x44750000, 0x44778000, 0x447A0000
4144 };
4145
4146 /* The following are for tuning of products */
4147 #ifdef ENABLE_TUNING_CONTROLS
4148
4149 static const unsigned int voice_focus_vals_lookup[] = {
4150 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
4151 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
4152 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
4153 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
4154 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
4155 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
4156 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
4157 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
4158 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
4159 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
4160 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
4161 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
4162 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
4163 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
4164 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
4165 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
4166 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
4167 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
4168 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
4169 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
4170 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
4171 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
4172 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
4173 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
4174 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
4175 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
4176 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
4177 };
4178
4179 static const unsigned int mic_svm_vals_lookup[] = {
4180 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
4181 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
4182 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
4183 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
4184 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
4185 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
4186 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
4187 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
4188 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
4189 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
4190 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
4191 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
4192 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
4193 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
4194 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
4195 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
4196 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
4197 };
4198
4199 static const unsigned int equalizer_vals_lookup[] = {
4200 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
4201 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
4202 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
4203 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
4204 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
4205 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
4206 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
4207 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
4208 0x41C00000
4209 };
4210
4211 static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
4212 const unsigned int *lookup, int idx)
4213 {
4214 int i;
4215
4216 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
4217 if (nid == ca0132_tuning_ctls[i].nid) {
4218 CLASS(snd_hda_power, pm)(codec);
4219 dspio_set_param(codec, ca0132_tuning_ctls[i].mid, 0x20,
4220 ca0132_tuning_ctls[i].req,
4221 &(lookup[idx]), sizeof(unsigned int));
4222 return 1;
4223 }
4224 }
4225
4226 return -EINVAL;
4227 }
4228
4229 static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
4230 struct snd_ctl_elem_value *ucontrol)
4231 {
4232 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4233 struct ca0132_spec *spec = codec->spec;
4234 hda_nid_t nid = get_amp_nid(kcontrol);
4235 long *valp = ucontrol->value.integer.value;
4236 int idx = nid - TUNING_CTL_START_NID;
4237
4238 *valp = spec->cur_ctl_vals[idx];
4239 return 0;
4240 }
4241
4242 static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
4243 struct snd_ctl_elem_info *uinfo)
4244 {
4245 int chs = get_amp_channels(kcontrol);
4246 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4247 uinfo->count = chs == 3 ? 2 : 1;
4248 uinfo->value.integer.min = 20;
4249 uinfo->value.integer.max = 180;
4250 uinfo->value.integer.step = 1;
4251
4252 return 0;
4253 }
4254
4255 static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
4256 struct snd_ctl_elem_value *ucontrol)
4257 {
4258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4259 struct ca0132_spec *spec = codec->spec;
4260 hda_nid_t nid = get_amp_nid(kcontrol);
4261 long *valp = ucontrol->value.integer.value;
4262 int idx;
4263
4264 idx = nid - TUNING_CTL_START_NID;
4265 /* any change? */
4266 if (spec->cur_ctl_vals[idx] == *valp)
4267 return 0;
4268
4269 spec->cur_ctl_vals[idx] = *valp;
4270
4271 idx = *valp - 20;
4272 tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
4273
4274 return 1;
4275 }
4276
4277 static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
4278 struct snd_ctl_elem_info *uinfo)
4279 {
4280 int chs = get_amp_channels(kcontrol);
4281 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4282 uinfo->count = chs == 3 ? 2 : 1;
4283 uinfo->value.integer.min = 0;
4284 uinfo->value.integer.max = 100;
4285 uinfo->value.integer.step = 1;
4286
4287 return 0;
4288 }
4289
4290 static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
4291 struct snd_ctl_elem_value *ucontrol)
4292 {
4293 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4294 struct ca0132_spec *spec = codec->spec;
4295 hda_nid_t nid = get_amp_nid(kcontrol);
4296 long *valp = ucontrol->value.integer.value;
4297 int idx;
4298
4299 idx = nid - TUNING_CTL_START_NID;
4300 /* any change? */
4301 if (spec->cur_ctl_vals[idx] == *valp)
4302 return 0;
4303
4304 spec->cur_ctl_vals[idx] = *valp;
4305
4306 idx = *valp;
4307 tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
4308
4309 return 0;
4310 }
4311
4312 static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
4313 struct snd_ctl_elem_info *uinfo)
4314 {
4315 int chs = get_amp_channels(kcontrol);
4316 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4317 uinfo->count = chs == 3 ? 2 : 1;
4318 uinfo->value.integer.min = 0;
4319 uinfo->value.integer.max = 48;
4320 uinfo->value.integer.step = 1;
4321
4322 return 0;
4323 }
4324
4325 static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
4326 struct snd_ctl_elem_value *ucontrol)
4327 {
4328 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4329 struct ca0132_spec *spec = codec->spec;
4330 hda_nid_t nid = get_amp_nid(kcontrol);
4331 long *valp = ucontrol->value.integer.value;
4332 int idx;
4333
4334 idx = nid - TUNING_CTL_START_NID;
4335 /* any change? */
4336 if (spec->cur_ctl_vals[idx] == *valp)
4337 return 0;
4338
4339 spec->cur_ctl_vals[idx] = *valp;
4340
4341 idx = *valp;
4342 tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
4343
4344 return 1;
4345 }
4346
4347 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
4348 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(eq_db_scale, -2400, 100, 0);
4349
4350 static int add_tuning_control(struct hda_codec *codec,
4351 hda_nid_t pnid, hda_nid_t nid,
4352 const char *name, int dir)
4353 {
4354 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
4355 int type = dir ? HDA_INPUT : HDA_OUTPUT;
4356 struct snd_kcontrol_new knew =
4357 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
4358
4359 knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
4360 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
4361 knew.tlv.c = NULL;
4362 knew.tlv.p = NULL;
4363 switch (pnid) {
4364 case VOICE_FOCUS:
4365 knew.info = voice_focus_ctl_info;
4366 knew.get = tuning_ctl_get;
4367 knew.put = voice_focus_ctl_put;
4368 knew.tlv.p = voice_focus_db_scale;
4369 break;
4370 case MIC_SVM:
4371 knew.info = mic_svm_ctl_info;
4372 knew.get = tuning_ctl_get;
4373 knew.put = mic_svm_ctl_put;
4374 break;
4375 case EQUALIZER:
4376 knew.info = equalizer_ctl_info;
4377 knew.get = tuning_ctl_get;
4378 knew.put = equalizer_ctl_put;
4379 knew.tlv.p = eq_db_scale;
4380 break;
4381 default:
4382 return 0;
4383 }
4384 knew.private_value =
4385 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
4386 snprintf(namestr, sizeof(namestr), "%s %s Volume", name, dirstr[dir]);
4387 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
4388 }
4389
4390 static int add_tuning_ctls(struct hda_codec *codec)
4391 {
4392 int i;
4393 int err;
4394
4395 for (i = 0; i < TUNING_CTLS_COUNT; i++) {
4396 err = add_tuning_control(codec,
4397 ca0132_tuning_ctls[i].parent_nid,
4398 ca0132_tuning_ctls[i].nid,
4399 ca0132_tuning_ctls[i].name,
4400 ca0132_tuning_ctls[i].direct);
4401 if (err < 0)
4402 return err;
4403 }
4404
4405 return 0;
4406 }
4407
4408 static void ca0132_init_tuning_defaults(struct hda_codec *codec)
4409 {
4410 struct ca0132_spec *spec = codec->spec;
4411 int i;
4412
4413 /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
4414 spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
4415 /* SVM level defaults to 0.74. */
4416 spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
4417
4418 /* EQ defaults to 0dB. */
4419 for (i = 2; i < TUNING_CTLS_COUNT; i++)
4420 spec->cur_ctl_vals[i] = 24;
4421 }
4422 #endif /*ENABLE_TUNING_CONTROLS*/
4423
4424 /*
4425 * Select the active output.
4426 * If autodetect is enabled, output will be selected based on jack detection.
4427 * If jack inserted, headphone will be selected, else built-in speakers
4428 * If autodetect is disabled, output will be selected based on selection.
4429 */
4430 static int ca0132_select_out(struct hda_codec *codec)
4431 {
4432 struct ca0132_spec *spec = codec->spec;
4433 unsigned int pin_ctl;
4434 int jack_present;
4435 int auto_jack;
4436 unsigned int tmp;
4437 int err;
4438
4439 codec_dbg(codec, "ca0132_select_out\n");
4440
4441 CLASS(snd_hda_power_pm, pm)(codec);
4442
4443 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
4444
4445 if (auto_jack)
4446 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp);
4447 else
4448 jack_present =
4449 spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
4450
4451 if (jack_present)
4452 spec->cur_out_type = HEADPHONE_OUT;
4453 else
4454 spec->cur_out_type = SPEAKER_OUT;
4455
4456 if (spec->cur_out_type == SPEAKER_OUT) {
4457 codec_dbg(codec, "ca0132_select_out speaker\n");
4458 /*speaker out config*/
4459 tmp = FLOAT_ONE;
4460 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
4461 if (err < 0)
4462 return err;
4463 /*enable speaker EQ*/
4464 tmp = FLOAT_ONE;
4465 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
4466 if (err < 0)
4467 return err;
4468
4469 /* Setup EAPD */
4470 snd_hda_codec_write(codec, spec->out_pins[1], 0,
4471 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
4472 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4473 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4474 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4475 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
4476 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4477 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
4478
4479 /* disable headphone node */
4480 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4481 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4482 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4483 pin_ctl & ~PIN_HP);
4484 /* enable speaker node */
4485 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4486 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4487 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4488 pin_ctl | PIN_OUT);
4489 } else {
4490 codec_dbg(codec, "ca0132_select_out hp\n");
4491 /*headphone out config*/
4492 tmp = FLOAT_ZERO;
4493 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
4494 if (err < 0)
4495 return err;
4496 /*disable speaker EQ*/
4497 tmp = FLOAT_ZERO;
4498 err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
4499 if (err < 0)
4500 return err;
4501
4502 /* Setup EAPD */
4503 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4504 VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
4505 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4506 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4507 snd_hda_codec_write(codec, spec->out_pins[1], 0,
4508 VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
4509 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4510 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
4511
4512 /* disable speaker*/
4513 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
4514 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4515 snd_hda_set_pin_ctl(codec, spec->out_pins[0],
4516 pin_ctl & ~PIN_HP);
4517 /* enable headphone*/
4518 pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
4519 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4520 snd_hda_set_pin_ctl(codec, spec->out_pins[1],
4521 pin_ctl | PIN_HP);
4522 }
4523
4524 return 0;
4525 }
4526
4527 static int ae5_headphone_gain_set(struct hda_codec *codec, long val);
4528 static int zxr_headphone_gain_set(struct hda_codec *codec, long val);
4529 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
4530
4531 static void ae5_mmio_select_out(struct hda_codec *codec)
4532 {
4533 struct ca0132_spec *spec = codec->spec;
4534 const struct ae_ca0113_output_set *out_cmds;
4535 unsigned int i;
4536
4537 if (ca0132_quirk(spec) == QUIRK_AE5)
4538 out_cmds = &ae5_ca0113_output_presets;
4539 else
4540 out_cmds = &ae7_ca0113_output_presets;
4541
4542 for (i = 0; i < AE_CA0113_OUT_SET_COMMANDS; i++)
4543 ca0113_mmio_command_set(codec, out_cmds->group[i],
4544 out_cmds->target[i],
4545 out_cmds->vals[spec->cur_out_type][i]);
4546 }
4547
4548 static int ca0132_alt_set_full_range_speaker(struct hda_codec *codec)
4549 {
4550 struct ca0132_spec *spec = codec->spec;
4551 int quirk = ca0132_quirk(spec);
4552 unsigned int tmp;
4553 int err;
4554
4555 /* 2.0/4.0 setup has no LFE channel, so setting full-range does nothing. */
4556 if (spec->channel_cfg_val == SPEAKER_CHANNELS_4_0
4557 || spec->channel_cfg_val == SPEAKER_CHANNELS_2_0)
4558 return 0;
4559
4560 /* Set front L/R full range. Zero for full-range, one for redirection. */
4561 tmp = spec->speaker_range_val[0] ? FLOAT_ZERO : FLOAT_ONE;
4562 err = dspio_set_uint_param(codec, 0x96,
4563 SPEAKER_FULL_RANGE_FRONT_L_R, tmp);
4564 if (err < 0)
4565 return err;
4566
4567 /* When setting full-range rear, both rear and center/lfe are set. */
4568 tmp = spec->speaker_range_val[1] ? FLOAT_ZERO : FLOAT_ONE;
4569 err = dspio_set_uint_param(codec, 0x96,
4570 SPEAKER_FULL_RANGE_CENTER_LFE, tmp);
4571 if (err < 0)
4572 return err;
4573
4574 err = dspio_set_uint_param(codec, 0x96,
4575 SPEAKER_FULL_RANGE_REAR_L_R, tmp);
4576 if (err < 0)
4577 return err;
4578
4579 /*
4580 * Only the AE series cards set this value when setting full-range,
4581 * and it's always 1.0f.
4582 */
4583 if (quirk == QUIRK_AE5 || quirk == QUIRK_AE7) {
4584 err = dspio_set_uint_param(codec, 0x96,
4585 SPEAKER_FULL_RANGE_SURROUND_L_R, FLOAT_ONE);
4586 if (err < 0)
4587 return err;
4588 }
4589
4590 return 0;
4591 }
4592
4593 static int ca0132_alt_surround_set_bass_redirection(struct hda_codec *codec,
4594 bool val)
4595 {
4596 struct ca0132_spec *spec = codec->spec;
4597 unsigned int tmp;
4598 int err;
4599
4600 if (val && spec->channel_cfg_val != SPEAKER_CHANNELS_4_0 &&
4601 spec->channel_cfg_val != SPEAKER_CHANNELS_2_0)
4602 tmp = FLOAT_ONE;
4603 else
4604 tmp = FLOAT_ZERO;
4605
4606 err = dspio_set_uint_param(codec, 0x96, SPEAKER_BASS_REDIRECT, tmp);
4607 if (err < 0)
4608 return err;
4609
4610 /* If it is enabled, make sure to set the crossover frequency. */
4611 if (tmp) {
4612 tmp = float_xbass_xover_lookup[spec->xbass_xover_freq];
4613 err = dspio_set_uint_param(codec, 0x96,
4614 SPEAKER_BASS_REDIRECT_XOVER_FREQ, tmp);
4615 if (err < 0)
4616 return err;
4617 }
4618
4619 return 0;
4620 }
4621
4622 /*
4623 * These are the commands needed to setup output on each of the different card
4624 * types.
4625 */
4626 static void ca0132_alt_select_out_get_quirk_data(struct hda_codec *codec,
4627 const struct ca0132_alt_out_set_quirk_data **quirk_data)
4628 {
4629 struct ca0132_spec *spec = codec->spec;
4630 int quirk = ca0132_quirk(spec);
4631 unsigned int i;
4632
4633 *quirk_data = NULL;
4634 for (i = 0; i < ARRAY_SIZE(quirk_out_set_data); i++) {
4635 if (quirk_out_set_data[i].quirk_id == quirk) {
4636 *quirk_data = &quirk_out_set_data[i];
4637 return;
4638 }
4639 }
4640 }
4641
4642 static int ca0132_alt_select_out_quirk_set(struct hda_codec *codec)
4643 {
4644 const struct ca0132_alt_out_set_quirk_data *quirk_data;
4645 const struct ca0132_alt_out_set_info *out_info;
4646 struct ca0132_spec *spec = codec->spec;
4647 unsigned int i, gpio_data;
4648 int err;
4649
4650 ca0132_alt_select_out_get_quirk_data(codec, &quirk_data);
4651 if (!quirk_data)
4652 return 0;
4653
4654 out_info = &quirk_data->out_set_info[spec->cur_out_type];
4655 if (quirk_data->is_ae_series)
4656 ae5_mmio_select_out(codec);
4657
4658 if (out_info->has_hda_gpio) {
4659 gpio_data = snd_hda_codec_read(codec, codec->core.afg, 0,
4660 AC_VERB_GET_GPIO_DATA, 0);
4661
4662 if (out_info->hda_gpio_set)
4663 gpio_data |= (1 << out_info->hda_gpio_pin);
4664 else
4665 gpio_data &= ~(1 << out_info->hda_gpio_pin);
4666
4667 snd_hda_codec_write(codec, codec->core.afg, 0,
4668 AC_VERB_SET_GPIO_DATA, gpio_data);
4669 }
4670
4671 if (out_info->mmio_gpio_count) {
4672 for (i = 0; i < out_info->mmio_gpio_count; i++) {
4673 ca0113_mmio_gpio_set(codec, out_info->mmio_gpio_pin[i],
4674 out_info->mmio_gpio_set[i]);
4675 }
4676 }
4677
4678 if (out_info->scp_cmds_count) {
4679 for (i = 0; i < out_info->scp_cmds_count; i++) {
4680 err = dspio_set_uint_param(codec,
4681 out_info->scp_cmd_mid[i],
4682 out_info->scp_cmd_req[i],
4683 out_info->scp_cmd_val[i]);
4684 if (err < 0)
4685 return err;
4686 }
4687 }
4688
4689 chipio_set_control_param(codec, 0x0d, out_info->dac2port);
4690
4691 if (out_info->has_chipio_write) {
4692 chipio_write(codec, out_info->chipio_write_addr,
4693 out_info->chipio_write_data);
4694 }
4695
4696 if (quirk_data->has_headphone_gain) {
4697 if (spec->cur_out_type != HEADPHONE_OUT) {
4698 if (quirk_data->is_ae_series)
4699 ae5_headphone_gain_set(codec, 2);
4700 else
4701 zxr_headphone_gain_set(codec, 0);
4702 } else {
4703 if (quirk_data->is_ae_series)
4704 ae5_headphone_gain_set(codec,
4705 spec->ae5_headphone_gain_val);
4706 else
4707 zxr_headphone_gain_set(codec,
4708 spec->zxr_gain_set);
4709 }
4710 }
4711
4712 return 0;
4713 }
4714
4715 static void ca0132_set_out_node_pincfg(struct hda_codec *codec, hda_nid_t nid,
4716 bool out_enable, bool hp_enable)
4717 {
4718 unsigned int pin_ctl;
4719
4720 pin_ctl = snd_hda_codec_read(codec, nid, 0,
4721 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4722
4723 pin_ctl = hp_enable ? pin_ctl | PIN_HP_AMP : pin_ctl & ~PIN_HP_AMP;
4724 pin_ctl = out_enable ? pin_ctl | PIN_OUT : pin_ctl & ~PIN_OUT;
4725 snd_hda_set_pin_ctl(codec, nid, pin_ctl);
4726 }
4727
4728 /*
4729 * This function behaves similarly to the ca0132_select_out funciton above,
4730 * except with a few differences. It adds the ability to select the current
4731 * output with an enumerated control "output source" if the auto detect
4732 * mute switch is set to off. If the auto detect mute switch is enabled, it
4733 * will detect either headphone or lineout(SPEAKER_OUT) from jack detection.
4734 * It also adds the ability to auto-detect the front headphone port.
4735 */
4736 static int ca0132_alt_select_out(struct hda_codec *codec)
4737 {
4738 struct ca0132_spec *spec = codec->spec;
4739 unsigned int tmp, outfx_set;
4740 int jack_present;
4741 int auto_jack;
4742 int err;
4743 /* Default Headphone is rear headphone */
4744 hda_nid_t headphone_nid = spec->out_pins[1];
4745
4746 codec_dbg(codec, "%s\n", __func__);
4747
4748 CLASS(snd_hda_power_pm, pm)(codec);
4749
4750 auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
4751
4752 /*
4753 * If headphone rear or front is plugged in, set to headphone.
4754 * If neither is plugged in, set to rear line out. Only if
4755 * hp/speaker auto detect is enabled.
4756 */
4757 if (auto_jack) {
4758 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp) ||
4759 snd_hda_jack_detect(codec, spec->unsol_tag_front_hp);
4760
4761 if (jack_present)
4762 spec->cur_out_type = HEADPHONE_OUT;
4763 else
4764 spec->cur_out_type = SPEAKER_OUT;
4765 } else
4766 spec->cur_out_type = spec->out_enum_val;
4767
4768 outfx_set = spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID];
4769
4770 /* Begin DSP output switch, mute DSP volume. */
4771 err = dspio_set_uint_param(codec, 0x96, SPEAKER_TUNING_MUTE, FLOAT_ONE);
4772 if (err < 0)
4773 return err;
4774
4775 err = ca0132_alt_select_out_quirk_set(codec);
4776 if (err < 0)
4777 return err;
4778
4779 switch (spec->cur_out_type) {
4780 case SPEAKER_OUT:
4781 codec_dbg(codec, "%s speaker\n", __func__);
4782
4783 /* Enable EAPD */
4784 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4785 AC_VERB_SET_EAPD_BTLENABLE, 0x01);
4786
4787 /* Disable headphone node. */
4788 ca0132_set_out_node_pincfg(codec, spec->out_pins[1], 0, 0);
4789 /* Set front L-R to output. */
4790 ca0132_set_out_node_pincfg(codec, spec->out_pins[0], 1, 0);
4791 /* Set Center/LFE to output. */
4792 ca0132_set_out_node_pincfg(codec, spec->out_pins[2], 1, 0);
4793 /* Set rear surround to output. */
4794 ca0132_set_out_node_pincfg(codec, spec->out_pins[3], 1, 0);
4795
4796 /*
4797 * Without PlayEnhancement being enabled, if we've got a 2.0
4798 * setup, set it to floating point eight to disable any DSP
4799 * processing effects.
4800 */
4801 if (!outfx_set && spec->channel_cfg_val == SPEAKER_CHANNELS_2_0)
4802 tmp = FLOAT_EIGHT;
4803 else
4804 tmp = speaker_channel_cfgs[spec->channel_cfg_val].val;
4805
4806 err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
4807 if (err < 0)
4808 return err;
4809
4810 break;
4811 case HEADPHONE_OUT:
4812 codec_dbg(codec, "%s hp\n", __func__);
4813 snd_hda_codec_write(codec, spec->out_pins[0], 0,
4814 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
4815
4816 /* Disable all speaker nodes. */
4817 ca0132_set_out_node_pincfg(codec, spec->out_pins[0], 0, 0);
4818 ca0132_set_out_node_pincfg(codec, spec->out_pins[2], 0, 0);
4819 ca0132_set_out_node_pincfg(codec, spec->out_pins[3], 0, 0);
4820
4821 /* enable headphone, either front or rear */
4822 if (snd_hda_jack_detect(codec, spec->unsol_tag_front_hp))
4823 headphone_nid = spec->out_pins[2];
4824 else if (snd_hda_jack_detect(codec, spec->unsol_tag_hp))
4825 headphone_nid = spec->out_pins[1];
4826
4827 ca0132_set_out_node_pincfg(codec, headphone_nid, 1, 1);
4828
4829 if (outfx_set)
4830 err = dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
4831 else
4832 err = dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ZERO);
4833
4834 if (err < 0)
4835 return err;
4836 break;
4837 }
4838 /*
4839 * If output effects are enabled, set the X-Bass effect value again to
4840 * make sure that it's properly enabled/disabled for speaker
4841 * configurations with an LFE channel.
4842 */
4843 if (outfx_set)
4844 ca0132_effects_set(codec, X_BASS,
4845 spec->effects_switch[X_BASS - EFFECT_START_NID]);
4846
4847 /* Set speaker EQ bypass attenuation to 0. */
4848 err = dspio_set_uint_param(codec, 0x8f, 0x01, FLOAT_ZERO);
4849 if (err < 0)
4850 return err;
4851
4852 /*
4853 * Although unused on all cards but the AE series, this is always set
4854 * to zero when setting the output.
4855 */
4856 err = dspio_set_uint_param(codec, 0x96,
4857 SPEAKER_TUNING_USE_SPEAKER_EQ, FLOAT_ZERO);
4858 if (err < 0)
4859 return err;
4860
4861 if (spec->cur_out_type == SPEAKER_OUT)
4862 err = ca0132_alt_surround_set_bass_redirection(codec,
4863 spec->bass_redirection_val);
4864 else
4865 err = ca0132_alt_surround_set_bass_redirection(codec, 0);
4866 if (err < 0)
4867 return err;
4868
4869 /* Unmute DSP now that we're done with output selection. */
4870 err = dspio_set_uint_param(codec, 0x96,
4871 SPEAKER_TUNING_MUTE, FLOAT_ZERO);
4872 if (err < 0)
4873 return err;
4874
4875 if (spec->cur_out_type == SPEAKER_OUT) {
4876 err = ca0132_alt_set_full_range_speaker(codec);
4877 if (err < 0)
4878 return err;
4879 }
4880
4881 return 0;
4882 }
4883
4884 static void ca0132_unsol_hp_delayed(struct work_struct *work)
4885 {
4886 struct ca0132_spec *spec = container_of(
4887 to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
4888 struct hda_jack_tbl *jack;
4889
4890 if (ca0132_use_alt_functions(spec))
4891 ca0132_alt_select_out(spec->codec);
4892 else
4893 ca0132_select_out(spec->codec);
4894
4895 jack = snd_hda_jack_tbl_get(spec->codec, spec->unsol_tag_hp);
4896 if (jack) {
4897 jack->block_report = 0;
4898 snd_hda_jack_report_sync(spec->codec);
4899 }
4900 }
4901
4902 static void ca0132_set_dmic(struct hda_codec *codec, int enable);
4903 static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
4904 static void resume_mic1(struct hda_codec *codec, unsigned int oldval);
4905 static int stop_mic1(struct hda_codec *codec);
4906 static int ca0132_cvoice_switch_set(struct hda_codec *codec);
4907 static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val);
4908
4909 /*
4910 * Select the active VIP source
4911 */
4912 static int ca0132_set_vipsource(struct hda_codec *codec, int val)
4913 {
4914 struct ca0132_spec *spec = codec->spec;
4915 unsigned int tmp;
4916
4917 if (spec->dsp_state != DSP_DOWNLOADED)
4918 return 0;
4919
4920 /* if CrystalVoice if off, vipsource should be 0 */
4921 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4922 (val == 0)) {
4923 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4924 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4925 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_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_ZERO;
4932 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4933 } else {
4934 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4935 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4936 if (spec->cur_mic_type == DIGITAL_MIC)
4937 tmp = FLOAT_TWO;
4938 else
4939 tmp = FLOAT_ONE;
4940 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4941 tmp = FLOAT_ONE;
4942 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4943 msleep(20);
4944 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
4945 }
4946
4947 return 1;
4948 }
4949
4950 static int ca0132_alt_set_vipsource(struct hda_codec *codec, int val)
4951 {
4952 struct ca0132_spec *spec = codec->spec;
4953 unsigned int tmp;
4954
4955 if (spec->dsp_state != DSP_DOWNLOADED)
4956 return 0;
4957
4958 codec_dbg(codec, "%s\n", __func__);
4959
4960 chipio_set_stream_control(codec, 0x03, 0);
4961 chipio_set_stream_control(codec, 0x04, 0);
4962
4963 /* if CrystalVoice is off, vipsource should be 0 */
4964 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
4965 (val == 0) || spec->in_enum_val == REAR_LINE_IN) {
4966 codec_dbg(codec, "%s: off.", __func__);
4967 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
4968
4969 tmp = FLOAT_ZERO;
4970 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
4971
4972 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
4973 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
4974 if (ca0132_quirk(spec) == QUIRK_R3DI)
4975 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
4976
4977
4978 if (spec->in_enum_val == REAR_LINE_IN)
4979 tmp = FLOAT_ZERO;
4980 else {
4981 if (ca0132_quirk(spec) == QUIRK_SBZ)
4982 tmp = FLOAT_THREE;
4983 else
4984 tmp = FLOAT_ONE;
4985 }
4986
4987 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
4988
4989 } else {
4990 codec_dbg(codec, "%s: on.", __func__);
4991 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
4992 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
4993 if (ca0132_quirk(spec) == QUIRK_R3DI)
4994 chipio_set_conn_rate(codec, 0x0F, SR_16_000);
4995
4996 if (spec->effects_switch[VOICE_FOCUS - EFFECT_START_NID])
4997 tmp = FLOAT_TWO;
4998 else
4999 tmp = FLOAT_ONE;
5000 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5001
5002 tmp = FLOAT_ONE;
5003 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
5004
5005 msleep(20);
5006 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
5007 }
5008
5009 chipio_set_stream_control(codec, 0x03, 1);
5010 chipio_set_stream_control(codec, 0x04, 1);
5011
5012 return 1;
5013 }
5014
5015 /*
5016 * Select the active microphone.
5017 * If autodetect is enabled, mic will be selected based on jack detection.
5018 * If jack inserted, ext.mic will be selected, else built-in mic
5019 * If autodetect is disabled, mic will be selected based on selection.
5020 */
5021 static int ca0132_select_mic(struct hda_codec *codec)
5022 {
5023 struct ca0132_spec *spec = codec->spec;
5024 int jack_present;
5025 int auto_jack;
5026
5027 codec_dbg(codec, "ca0132_select_mic\n");
5028
5029 CLASS(snd_hda_power_pm, pm)(codec);
5030
5031 auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
5032
5033 if (auto_jack)
5034 jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_amic1);
5035 else
5036 jack_present =
5037 spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
5038
5039 if (jack_present)
5040 spec->cur_mic_type = LINE_MIC_IN;
5041 else
5042 spec->cur_mic_type = DIGITAL_MIC;
5043
5044 if (spec->cur_mic_type == DIGITAL_MIC) {
5045 /* enable digital Mic */
5046 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
5047 ca0132_set_dmic(codec, 1);
5048 ca0132_mic_boost_set(codec, 0);
5049 /* set voice focus */
5050 ca0132_effects_set(codec, VOICE_FOCUS,
5051 spec->effects_switch
5052 [VOICE_FOCUS - EFFECT_START_NID]);
5053 } else {
5054 /* disable digital Mic */
5055 chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
5056 ca0132_set_dmic(codec, 0);
5057 ca0132_mic_boost_set(codec, spec->cur_mic_boost);
5058 /* disable voice focus */
5059 ca0132_effects_set(codec, VOICE_FOCUS, 0);
5060 }
5061
5062 return 0;
5063 }
5064
5065 /*
5066 * Select the active input.
5067 * Mic detection isn't used, because it's kind of pointless on the SBZ.
5068 * The front mic has no jack-detection, so the only way to switch to it
5069 * is to do it manually in alsamixer.
5070 */
5071 static int ca0132_alt_select_in(struct hda_codec *codec)
5072 {
5073 struct ca0132_spec *spec = codec->spec;
5074 unsigned int tmp;
5075
5076 codec_dbg(codec, "%s\n", __func__);
5077
5078 CLASS(snd_hda_power_pm, pm)(codec);
5079
5080 chipio_set_stream_control(codec, 0x03, 0);
5081 chipio_set_stream_control(codec, 0x04, 0);
5082
5083 spec->cur_mic_type = spec->in_enum_val;
5084
5085 switch (spec->cur_mic_type) {
5086 case REAR_MIC:
5087 switch (ca0132_quirk(spec)) {
5088 case QUIRK_SBZ:
5089 case QUIRK_R3D:
5090 ca0113_mmio_gpio_set(codec, 0, false);
5091 tmp = FLOAT_THREE;
5092 break;
5093 case QUIRK_ZXR:
5094 tmp = FLOAT_THREE;
5095 break;
5096 case QUIRK_R3DI:
5097 r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
5098 tmp = FLOAT_ONE;
5099 break;
5100 case QUIRK_AE5:
5101 ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
5102 tmp = FLOAT_THREE;
5103 break;
5104 case QUIRK_AE7:
5105 ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
5106 tmp = FLOAT_THREE;
5107 chipio_set_conn_rate(codec, MEM_CONNID_MICIN2,
5108 SR_96_000);
5109 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2,
5110 SR_96_000);
5111 dspio_set_uint_param(codec, 0x80, 0x01, FLOAT_ZERO);
5112 break;
5113 default:
5114 tmp = FLOAT_ONE;
5115 break;
5116 }
5117
5118 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
5119 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
5120 if (ca0132_quirk(spec) == QUIRK_R3DI)
5121 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
5122
5123 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5124
5125 chipio_set_stream_control(codec, 0x03, 1);
5126 chipio_set_stream_control(codec, 0x04, 1);
5127 switch (ca0132_quirk(spec)) {
5128 case QUIRK_SBZ:
5129 chipio_write(codec, 0x18B098, 0x0000000C);
5130 chipio_write(codec, 0x18B09C, 0x0000000C);
5131 break;
5132 case QUIRK_ZXR:
5133 chipio_write(codec, 0x18B098, 0x0000000C);
5134 chipio_write(codec, 0x18B09C, 0x000000CC);
5135 break;
5136 case QUIRK_AE5:
5137 chipio_write(codec, 0x18B098, 0x0000000C);
5138 chipio_write(codec, 0x18B09C, 0x0000004C);
5139 break;
5140 default:
5141 break;
5142 }
5143 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
5144 break;
5145 case REAR_LINE_IN:
5146 ca0132_mic_boost_set(codec, 0);
5147 switch (ca0132_quirk(spec)) {
5148 case QUIRK_SBZ:
5149 case QUIRK_R3D:
5150 ca0113_mmio_gpio_set(codec, 0, false);
5151 break;
5152 case QUIRK_R3DI:
5153 r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
5154 break;
5155 case QUIRK_AE5:
5156 ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
5157 break;
5158 case QUIRK_AE7:
5159 ca0113_mmio_command_set(codec, 0x30, 0x28, 0x3f);
5160 chipio_set_conn_rate(codec, MEM_CONNID_MICIN2,
5161 SR_96_000);
5162 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2,
5163 SR_96_000);
5164 dspio_set_uint_param(codec, 0x80, 0x01, FLOAT_ZERO);
5165 break;
5166 default:
5167 break;
5168 }
5169
5170 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
5171 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
5172 if (ca0132_quirk(spec) == QUIRK_R3DI)
5173 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
5174
5175 if (ca0132_quirk(spec) == QUIRK_AE7)
5176 tmp = FLOAT_THREE;
5177 else
5178 tmp = FLOAT_ZERO;
5179 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5180
5181 switch (ca0132_quirk(spec)) {
5182 case QUIRK_SBZ:
5183 case QUIRK_AE5:
5184 chipio_write(codec, 0x18B098, 0x00000000);
5185 chipio_write(codec, 0x18B09C, 0x00000000);
5186 break;
5187 default:
5188 break;
5189 }
5190 chipio_set_stream_control(codec, 0x03, 1);
5191 chipio_set_stream_control(codec, 0x04, 1);
5192 break;
5193 case FRONT_MIC:
5194 switch (ca0132_quirk(spec)) {
5195 case QUIRK_SBZ:
5196 case QUIRK_R3D:
5197 ca0113_mmio_gpio_set(codec, 0, true);
5198 ca0113_mmio_gpio_set(codec, 5, false);
5199 tmp = FLOAT_THREE;
5200 break;
5201 case QUIRK_R3DI:
5202 r3di_gpio_mic_set(codec, R3DI_FRONT_MIC);
5203 tmp = FLOAT_ONE;
5204 break;
5205 case QUIRK_AE5:
5206 ca0113_mmio_command_set(codec, 0x30, 0x28, 0x3f);
5207 tmp = FLOAT_THREE;
5208 break;
5209 default:
5210 tmp = FLOAT_ONE;
5211 break;
5212 }
5213
5214 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
5215 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
5216 if (ca0132_quirk(spec) == QUIRK_R3DI)
5217 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
5218
5219 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5220
5221 chipio_set_stream_control(codec, 0x03, 1);
5222 chipio_set_stream_control(codec, 0x04, 1);
5223
5224 switch (ca0132_quirk(spec)) {
5225 case QUIRK_SBZ:
5226 chipio_write(codec, 0x18B098, 0x0000000C);
5227 chipio_write(codec, 0x18B09C, 0x000000CC);
5228 break;
5229 case QUIRK_AE5:
5230 chipio_write(codec, 0x18B098, 0x0000000C);
5231 chipio_write(codec, 0x18B09C, 0x0000004C);
5232 break;
5233 default:
5234 break;
5235 }
5236 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
5237 break;
5238 }
5239 ca0132_cvoice_switch_set(codec);
5240
5241 return 0;
5242 }
5243
5244 /*
5245 * Check if VNODE settings take effect immediately.
5246 */
5247 static bool ca0132_is_vnode_effective(struct hda_codec *codec,
5248 hda_nid_t vnid,
5249 hda_nid_t *shared_nid)
5250 {
5251 struct ca0132_spec *spec = codec->spec;
5252 hda_nid_t nid;
5253
5254 switch (vnid) {
5255 case VNID_SPK:
5256 nid = spec->shared_out_nid;
5257 break;
5258 case VNID_MIC:
5259 nid = spec->shared_mic_nid;
5260 break;
5261 default:
5262 return false;
5263 }
5264
5265 if (shared_nid)
5266 *shared_nid = nid;
5267
5268 return true;
5269 }
5270
5271 /*
5272 * The following functions are control change helpers.
5273 * They return 0 if no changed. Return 1 if changed.
5274 */
5275 static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
5276 {
5277 struct ca0132_spec *spec = codec->spec;
5278 unsigned int tmp;
5279
5280 /* based on CrystalVoice state to enable VoiceFX. */
5281 if (enable) {
5282 tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
5283 FLOAT_ONE : FLOAT_ZERO;
5284 } else {
5285 tmp = FLOAT_ZERO;
5286 }
5287
5288 dspio_set_uint_param(codec, ca0132_voicefx.mid,
5289 ca0132_voicefx.reqs[0], tmp);
5290
5291 return 1;
5292 }
5293
5294 /*
5295 * Set the effects parameters
5296 */
5297 static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
5298 {
5299 struct ca0132_spec *spec = codec->spec;
5300 unsigned int on, tmp, channel_cfg;
5301 int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
5302 int err = 0;
5303 int idx = nid - EFFECT_START_NID;
5304
5305 if ((idx < 0) || (idx >= num_fx))
5306 return 0; /* no changed */
5307
5308 /* for out effect, qualify with PE */
5309 if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
5310 /* if PE if off, turn off out effects. */
5311 if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
5312 val = 0;
5313 if (spec->cur_out_type == SPEAKER_OUT && nid == X_BASS) {
5314 channel_cfg = spec->channel_cfg_val;
5315 if (channel_cfg != SPEAKER_CHANNELS_2_0 &&
5316 channel_cfg != SPEAKER_CHANNELS_4_0)
5317 val = 0;
5318 }
5319 }
5320
5321 /* for in effect, qualify with CrystalVoice */
5322 if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
5323 /* if CrystalVoice if off, turn off in effects. */
5324 if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
5325 val = 0;
5326
5327 /* Voice Focus applies to 2-ch Mic, Digital Mic */
5328 if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
5329 val = 0;
5330
5331 /* If Voice Focus on SBZ, set to two channel. */
5332 if ((nid == VOICE_FOCUS) && ca0132_use_pci_mmio(spec)
5333 && (spec->cur_mic_type != REAR_LINE_IN)) {
5334 if (spec->effects_switch[CRYSTAL_VOICE -
5335 EFFECT_START_NID]) {
5336
5337 if (spec->effects_switch[VOICE_FOCUS -
5338 EFFECT_START_NID]) {
5339 tmp = FLOAT_TWO;
5340 val = 1;
5341 } else
5342 tmp = FLOAT_ONE;
5343
5344 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
5345 }
5346 }
5347 /*
5348 * For SBZ noise reduction, there's an extra command
5349 * to module ID 0x47. No clue why.
5350 */
5351 if ((nid == NOISE_REDUCTION) && ca0132_use_pci_mmio(spec)
5352 && (spec->cur_mic_type != REAR_LINE_IN)) {
5353 if (spec->effects_switch[CRYSTAL_VOICE -
5354 EFFECT_START_NID]) {
5355 if (spec->effects_switch[NOISE_REDUCTION -
5356 EFFECT_START_NID])
5357 tmp = FLOAT_ONE;
5358 else
5359 tmp = FLOAT_ZERO;
5360 } else
5361 tmp = FLOAT_ZERO;
5362
5363 dspio_set_uint_param(codec, 0x47, 0x00, tmp);
5364 }
5365
5366 /* If rear line in disable effects. */
5367 if (ca0132_use_alt_functions(spec) &&
5368 spec->in_enum_val == REAR_LINE_IN)
5369 val = 0;
5370 }
5371
5372 codec_dbg(codec, "ca0132_effect_set: nid=0x%x, val=%ld\n",
5373 nid, val);
5374
5375 on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
5376 err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
5377 ca0132_effects[idx].reqs[0], on);
5378
5379 if (err < 0)
5380 return 0; /* no changed */
5381
5382 return 1;
5383 }
5384
5385 /*
5386 * Turn on/off Playback Enhancements
5387 */
5388 static int ca0132_pe_switch_set(struct hda_codec *codec)
5389 {
5390 struct ca0132_spec *spec = codec->spec;
5391 hda_nid_t nid;
5392 int i, ret = 0;
5393
5394 codec_dbg(codec, "ca0132_pe_switch_set: val=%ld\n",
5395 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
5396
5397 if (ca0132_use_alt_functions(spec))
5398 ca0132_alt_select_out(codec);
5399
5400 i = OUT_EFFECT_START_NID - EFFECT_START_NID;
5401 nid = OUT_EFFECT_START_NID;
5402 /* PE affects all out effects */
5403 for (; nid < OUT_EFFECT_END_NID; nid++, i++)
5404 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
5405
5406 return ret;
5407 }
5408
5409 /* Check if Mic1 is streaming, if so, stop streaming */
5410 static int stop_mic1(struct hda_codec *codec)
5411 {
5412 struct ca0132_spec *spec = codec->spec;
5413 unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
5414 AC_VERB_GET_CONV, 0);
5415 if (oldval != 0)
5416 snd_hda_codec_write(codec, spec->adcs[0], 0,
5417 AC_VERB_SET_CHANNEL_STREAMID,
5418 0);
5419 return oldval;
5420 }
5421
5422 /* Resume Mic1 streaming if it was stopped. */
5423 static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
5424 {
5425 struct ca0132_spec *spec = codec->spec;
5426 /* Restore the previous stream and channel */
5427 if (oldval != 0)
5428 snd_hda_codec_write(codec, spec->adcs[0], 0,
5429 AC_VERB_SET_CHANNEL_STREAMID,
5430 oldval);
5431 }
5432
5433 /*
5434 * Turn on/off CrystalVoice
5435 */
5436 static int ca0132_cvoice_switch_set(struct hda_codec *codec)
5437 {
5438 struct ca0132_spec *spec = codec->spec;
5439 hda_nid_t nid;
5440 int i, ret = 0;
5441 unsigned int oldval;
5442
5443 codec_dbg(codec, "ca0132_cvoice_switch_set: val=%ld\n",
5444 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
5445
5446 i = IN_EFFECT_START_NID - EFFECT_START_NID;
5447 nid = IN_EFFECT_START_NID;
5448 /* CrystalVoice affects all in effects */
5449 for (; nid < IN_EFFECT_END_NID; nid++, i++)
5450 ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
5451
5452 /* including VoiceFX */
5453 ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
5454
5455 /* set correct vipsource */
5456 oldval = stop_mic1(codec);
5457 if (ca0132_use_alt_functions(spec))
5458 ret |= ca0132_alt_set_vipsource(codec, 1);
5459 else
5460 ret |= ca0132_set_vipsource(codec, 1);
5461 resume_mic1(codec, oldval);
5462 return ret;
5463 }
5464
5465 static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
5466 {
5467 struct ca0132_spec *spec = codec->spec;
5468 int ret = 0;
5469
5470 if (val) /* on */
5471 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
5472 HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
5473 else /* off */
5474 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
5475 HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
5476
5477 return ret;
5478 }
5479
5480 static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val)
5481 {
5482 struct ca0132_spec *spec = codec->spec;
5483 int ret = 0;
5484
5485 ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
5486 HDA_INPUT, 0, HDA_AMP_VOLMASK, val);
5487 return ret;
5488 }
5489
5490 static int ae5_headphone_gain_set(struct hda_codec *codec, long val)
5491 {
5492 unsigned int i;
5493
5494 for (i = 0; i < 4; i++)
5495 ca0113_mmio_command_set(codec, 0x48, 0x11 + i,
5496 ae5_headphone_gain_presets[val].vals[i]);
5497 return 0;
5498 }
5499
5500 /*
5501 * gpio pin 1 is a relay that switches on/off, apparently setting the headphone
5502 * amplifier to handle a 600 ohm load.
5503 */
5504 static int zxr_headphone_gain_set(struct hda_codec *codec, long val)
5505 {
5506 ca0113_mmio_gpio_set(codec, 1, val);
5507
5508 return 0;
5509 }
5510
5511 /*
5512 * Manual output selection (HP/Speaker Playback Switch or alt Output Select)
5513 * is meaningful only when HP/Speaker auto-detect is disabled, since the
5514 * select_out path always prefers jack presence when auto-detect is on. When
5515 * the user explicitly chooses an output, turn auto-detect off so the manual
5516 * choice actually takes effect, and notify userspace so the auto-detect
5517 * control reflects the new state.
5518 */
5519 static void ca0132_disable_hp_auto_detect(struct hda_codec *codec)
5520 {
5521 struct ca0132_spec *spec = codec->spec;
5522 struct snd_kcontrol *kctl;
5523
5524 if (!spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID])
5525 return;
5526
5527 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID] = 0;
5528 kctl = snd_hda_find_mixer_ctl(codec,
5529 "HP/Speaker Auto Detect Playback Switch");
5530 if (kctl)
5531 snd_ctl_notify(codec->card, SNDRV_CTL_EVENT_MASK_VALUE,
5532 &kctl->id);
5533 }
5534
5535 static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
5536 struct snd_ctl_elem_value *ucontrol)
5537 {
5538 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5539 hda_nid_t nid = get_amp_nid(kcontrol);
5540 hda_nid_t shared_nid = 0;
5541 bool effective;
5542 int ret = 0;
5543 struct ca0132_spec *spec = codec->spec;
5544 int auto_jack;
5545
5546 if (nid == VNID_HP_SEL) {
5547 ca0132_disable_hp_auto_detect(codec);
5548 if (ca0132_use_alt_functions(spec))
5549 ca0132_alt_select_out(codec);
5550 else
5551 ca0132_select_out(codec);
5552 return 1;
5553 }
5554
5555 if (nid == VNID_AMIC1_SEL) {
5556 auto_jack =
5557 spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
5558 if (!auto_jack)
5559 ca0132_select_mic(codec);
5560 return 1;
5561 }
5562
5563 if (nid == VNID_HP_ASEL) {
5564 if (ca0132_use_alt_functions(spec))
5565 ca0132_alt_select_out(codec);
5566 else
5567 ca0132_select_out(codec);
5568 return 1;
5569 }
5570
5571 if (nid == VNID_AMIC1_ASEL) {
5572 ca0132_select_mic(codec);
5573 return 1;
5574 }
5575
5576 /* if effective conditions, then update hw immediately. */
5577 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
5578 if (effective) {
5579 int dir = get_amp_direction(kcontrol);
5580 int ch = get_amp_channels(kcontrol);
5581 unsigned long pval;
5582
5583 guard(mutex)(&codec->control_mutex);
5584 pval = kcontrol->private_value;
5585 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
5586 0, dir);
5587 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
5588 kcontrol->private_value = pval;
5589 }
5590
5591 return ret;
5592 }
5593 /* End of control change helpers. */
5594
5595 static void ca0132_alt_bass_redirection_xover_set(struct hda_codec *codec,
5596 long idx)
5597 {
5598 CLASS(snd_hda_power, pm)(codec);
5599
5600 dspio_set_param(codec, 0x96, 0x20, SPEAKER_BASS_REDIRECT_XOVER_FREQ,
5601 &(float_xbass_xover_lookup[idx]), sizeof(unsigned int));
5602 }
5603
5604 /*
5605 * Below I've added controls to mess with the effect levels, I've only enabled
5606 * them on the Sound Blaster Z, but they would probably also work on the
5607 * Chromebook. I figured they were probably tuned specifically for it, and left
5608 * out for a reason.
5609 */
5610
5611 /* Sets DSP effect level from the sliders above the controls */
5612
5613 static int ca0132_alt_slider_ctl_set(struct hda_codec *codec, hda_nid_t nid,
5614 const unsigned int *lookup, int idx)
5615 {
5616 int i = 0;
5617 unsigned int y;
5618 /*
5619 * For X_BASS, req 2 is actually crossover freq instead of
5620 * effect level
5621 */
5622 if (nid == X_BASS)
5623 y = 2;
5624 else
5625 y = 1;
5626
5627 CLASS(snd_hda_power, pm)(codec);
5628 if (nid == XBASS_XOVER) {
5629 for (i = 0; i < OUT_EFFECTS_COUNT; i++)
5630 if (ca0132_effects[i].nid == X_BASS)
5631 break;
5632
5633 dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
5634 ca0132_effects[i].reqs[1],
5635 &(lookup[idx - 1]), sizeof(unsigned int));
5636 } else {
5637 /* Find the actual effect structure */
5638 for (i = 0; i < OUT_EFFECTS_COUNT; i++)
5639 if (nid == ca0132_effects[i].nid)
5640 break;
5641
5642 dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
5643 ca0132_effects[i].reqs[y],
5644 &(lookup[idx]), sizeof(unsigned int));
5645 }
5646
5647 return 0;
5648 }
5649
5650 static int ca0132_alt_xbass_xover_slider_ctl_get(struct snd_kcontrol *kcontrol,
5651 struct snd_ctl_elem_value *ucontrol)
5652 {
5653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5654 struct ca0132_spec *spec = codec->spec;
5655 long *valp = ucontrol->value.integer.value;
5656 hda_nid_t nid = get_amp_nid(kcontrol);
5657
5658 if (nid == BASS_REDIRECTION_XOVER)
5659 *valp = spec->bass_redirect_xover_freq;
5660 else
5661 *valp = spec->xbass_xover_freq;
5662
5663 return 0;
5664 }
5665
5666 static int ca0132_alt_slider_ctl_get(struct snd_kcontrol *kcontrol,
5667 struct snd_ctl_elem_value *ucontrol)
5668 {
5669 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5670 struct ca0132_spec *spec = codec->spec;
5671 hda_nid_t nid = get_amp_nid(kcontrol);
5672 long *valp = ucontrol->value.integer.value;
5673 int idx = nid - OUT_EFFECT_START_NID;
5674
5675 *valp = spec->fx_ctl_val[idx];
5676 return 0;
5677 }
5678
5679 /*
5680 * The X-bass crossover starts at 10hz, so the min is 1. The
5681 * frequency is set in multiples of 10.
5682 */
5683 static int ca0132_alt_xbass_xover_slider_info(struct snd_kcontrol *kcontrol,
5684 struct snd_ctl_elem_info *uinfo)
5685 {
5686 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5687 uinfo->count = 1;
5688 uinfo->value.integer.min = 1;
5689 uinfo->value.integer.max = 100;
5690 uinfo->value.integer.step = 1;
5691
5692 return 0;
5693 }
5694
5695 static int ca0132_alt_effect_slider_info(struct snd_kcontrol *kcontrol,
5696 struct snd_ctl_elem_info *uinfo)
5697 {
5698 int chs = get_amp_channels(kcontrol);
5699
5700 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5701 uinfo->count = chs == 3 ? 2 : 1;
5702 uinfo->value.integer.min = 0;
5703 uinfo->value.integer.max = 100;
5704 uinfo->value.integer.step = 1;
5705
5706 return 0;
5707 }
5708
5709 static int ca0132_alt_xbass_xover_slider_put(struct snd_kcontrol *kcontrol,
5710 struct snd_ctl_elem_value *ucontrol)
5711 {
5712 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5713 struct ca0132_spec *spec = codec->spec;
5714 hda_nid_t nid = get_amp_nid(kcontrol);
5715 long *valp = ucontrol->value.integer.value;
5716 long *cur_val;
5717 int idx;
5718
5719 if (nid == BASS_REDIRECTION_XOVER)
5720 cur_val = &spec->bass_redirect_xover_freq;
5721 else
5722 cur_val = &spec->xbass_xover_freq;
5723
5724 /* any change? */
5725 if (*cur_val == *valp)
5726 return 0;
5727
5728 *cur_val = *valp;
5729
5730 idx = *valp;
5731 if (nid == BASS_REDIRECTION_XOVER)
5732 ca0132_alt_bass_redirection_xover_set(codec, *cur_val);
5733 else
5734 ca0132_alt_slider_ctl_set(codec, nid, float_xbass_xover_lookup, idx);
5735
5736 return 0;
5737 }
5738
5739 static int ca0132_alt_effect_slider_put(struct snd_kcontrol *kcontrol,
5740 struct snd_ctl_elem_value *ucontrol)
5741 {
5742 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5743 struct ca0132_spec *spec = codec->spec;
5744 hda_nid_t nid = get_amp_nid(kcontrol);
5745 long *valp = ucontrol->value.integer.value;
5746 int idx;
5747
5748 idx = nid - EFFECT_START_NID;
5749 /* any change? */
5750 if (spec->fx_ctl_val[idx] == *valp)
5751 return 0;
5752
5753 spec->fx_ctl_val[idx] = *valp;
5754
5755 idx = *valp;
5756 ca0132_alt_slider_ctl_set(codec, nid, float_zero_to_one_lookup, idx);
5757
5758 return 0;
5759 }
5760
5761
5762 /*
5763 * Mic Boost Enum for alternative ca0132 codecs. I didn't like that the original
5764 * only has off or full 30 dB, and didn't like making a volume slider that has
5765 * traditional 0-100 in alsamixer that goes in big steps. I like enum better.
5766 */
5767 #define MIC_BOOST_NUM_OF_STEPS 4
5768 #define MIC_BOOST_ENUM_MAX_STRLEN 10
5769
5770 static int ca0132_alt_mic_boost_info(struct snd_kcontrol *kcontrol,
5771 struct snd_ctl_elem_info *uinfo)
5772 {
5773 const char *sfx = "dB";
5774 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5775
5776 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5777 uinfo->count = 1;
5778 uinfo->value.enumerated.items = MIC_BOOST_NUM_OF_STEPS;
5779 if (uinfo->value.enumerated.item >= MIC_BOOST_NUM_OF_STEPS)
5780 uinfo->value.enumerated.item = MIC_BOOST_NUM_OF_STEPS - 1;
5781 sprintf(namestr, "%d %s", (uinfo->value.enumerated.item * 10), sfx);
5782 strscpy(uinfo->value.enumerated.name, namestr);
5783 return 0;
5784 }
5785
5786 static int ca0132_alt_mic_boost_get(struct snd_kcontrol *kcontrol,
5787 struct snd_ctl_elem_value *ucontrol)
5788 {
5789 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5790 struct ca0132_spec *spec = codec->spec;
5791
5792 ucontrol->value.enumerated.item[0] = spec->mic_boost_enum_val;
5793 return 0;
5794 }
5795
5796 static int ca0132_alt_mic_boost_put(struct snd_kcontrol *kcontrol,
5797 struct snd_ctl_elem_value *ucontrol)
5798 {
5799 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5800 struct ca0132_spec *spec = codec->spec;
5801 int sel = ucontrol->value.enumerated.item[0];
5802 unsigned int items = MIC_BOOST_NUM_OF_STEPS;
5803
5804 if (sel >= items)
5805 return 0;
5806
5807 codec_dbg(codec, "ca0132_alt_mic_boost: boost=%d\n",
5808 sel);
5809
5810 spec->mic_boost_enum_val = sel;
5811
5812 if (spec->in_enum_val != REAR_LINE_IN)
5813 ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
5814
5815 return 1;
5816 }
5817
5818 /*
5819 * Sound BlasterX AE-5 Headphone Gain Controls.
5820 */
5821 #define AE5_HEADPHONE_GAIN_MAX 3
5822 static int ae5_headphone_gain_info(struct snd_kcontrol *kcontrol,
5823 struct snd_ctl_elem_info *uinfo)
5824 {
5825 const char *sfx = " Ohms)";
5826 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5827
5828 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5829 uinfo->count = 1;
5830 uinfo->value.enumerated.items = AE5_HEADPHONE_GAIN_MAX;
5831 if (uinfo->value.enumerated.item >= AE5_HEADPHONE_GAIN_MAX)
5832 uinfo->value.enumerated.item = AE5_HEADPHONE_GAIN_MAX - 1;
5833 sprintf(namestr, "%s %s",
5834 ae5_headphone_gain_presets[uinfo->value.enumerated.item].name,
5835 sfx);
5836 strscpy(uinfo->value.enumerated.name, namestr);
5837 return 0;
5838 }
5839
5840 static int ae5_headphone_gain_get(struct snd_kcontrol *kcontrol,
5841 struct snd_ctl_elem_value *ucontrol)
5842 {
5843 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5844 struct ca0132_spec *spec = codec->spec;
5845
5846 ucontrol->value.enumerated.item[0] = spec->ae5_headphone_gain_val;
5847 return 0;
5848 }
5849
5850 static int ae5_headphone_gain_put(struct snd_kcontrol *kcontrol,
5851 struct snd_ctl_elem_value *ucontrol)
5852 {
5853 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5854 struct ca0132_spec *spec = codec->spec;
5855 int sel = ucontrol->value.enumerated.item[0];
5856 unsigned int items = AE5_HEADPHONE_GAIN_MAX;
5857
5858 if (sel >= items)
5859 return 0;
5860
5861 codec_dbg(codec, "ae5_headphone_gain: boost=%d\n",
5862 sel);
5863
5864 spec->ae5_headphone_gain_val = sel;
5865
5866 if (spec->out_enum_val == HEADPHONE_OUT)
5867 ae5_headphone_gain_set(codec, spec->ae5_headphone_gain_val);
5868
5869 return 1;
5870 }
5871
5872 /*
5873 * Sound BlasterX AE-5 sound filter enumerated control.
5874 */
5875 #define AE5_SOUND_FILTER_MAX 3
5876
5877 static int ae5_sound_filter_info(struct snd_kcontrol *kcontrol,
5878 struct snd_ctl_elem_info *uinfo)
5879 {
5880 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5881
5882 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5883 uinfo->count = 1;
5884 uinfo->value.enumerated.items = AE5_SOUND_FILTER_MAX;
5885 if (uinfo->value.enumerated.item >= AE5_SOUND_FILTER_MAX)
5886 uinfo->value.enumerated.item = AE5_SOUND_FILTER_MAX - 1;
5887 sprintf(namestr, "%s",
5888 ae5_filter_presets[uinfo->value.enumerated.item].name);
5889 strscpy(uinfo->value.enumerated.name, namestr);
5890 return 0;
5891 }
5892
5893 static int ae5_sound_filter_get(struct snd_kcontrol *kcontrol,
5894 struct snd_ctl_elem_value *ucontrol)
5895 {
5896 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5897 struct ca0132_spec *spec = codec->spec;
5898
5899 ucontrol->value.enumerated.item[0] = spec->ae5_filter_val;
5900 return 0;
5901 }
5902
5903 static int ae5_sound_filter_put(struct snd_kcontrol *kcontrol,
5904 struct snd_ctl_elem_value *ucontrol)
5905 {
5906 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5907 struct ca0132_spec *spec = codec->spec;
5908 int sel = ucontrol->value.enumerated.item[0];
5909 unsigned int items = AE5_SOUND_FILTER_MAX;
5910
5911 if (sel >= items)
5912 return 0;
5913
5914 codec_dbg(codec, "ae5_sound_filter: %s\n",
5915 ae5_filter_presets[sel].name);
5916
5917 spec->ae5_filter_val = sel;
5918
5919 ca0113_mmio_command_set_type2(codec, 0x48, 0x07,
5920 ae5_filter_presets[sel].val);
5921
5922 return 1;
5923 }
5924
5925 /*
5926 * Input Select Control for alternative ca0132 codecs. This exists because
5927 * front microphone has no auto-detect, and we need a way to set the rear
5928 * as line-in
5929 */
5930 static int ca0132_alt_input_source_info(struct snd_kcontrol *kcontrol,
5931 struct snd_ctl_elem_info *uinfo)
5932 {
5933 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5934 uinfo->count = 1;
5935 uinfo->value.enumerated.items = IN_SRC_NUM_OF_INPUTS;
5936 if (uinfo->value.enumerated.item >= IN_SRC_NUM_OF_INPUTS)
5937 uinfo->value.enumerated.item = IN_SRC_NUM_OF_INPUTS - 1;
5938 strscpy(uinfo->value.enumerated.name,
5939 in_src_str[uinfo->value.enumerated.item]);
5940 return 0;
5941 }
5942
5943 static int ca0132_alt_input_source_get(struct snd_kcontrol *kcontrol,
5944 struct snd_ctl_elem_value *ucontrol)
5945 {
5946 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5947 struct ca0132_spec *spec = codec->spec;
5948
5949 ucontrol->value.enumerated.item[0] = spec->in_enum_val;
5950 return 0;
5951 }
5952
5953 static int ca0132_alt_input_source_put(struct snd_kcontrol *kcontrol,
5954 struct snd_ctl_elem_value *ucontrol)
5955 {
5956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5957 struct ca0132_spec *spec = codec->spec;
5958 int sel = ucontrol->value.enumerated.item[0];
5959 unsigned int items = IN_SRC_NUM_OF_INPUTS;
5960
5961 /*
5962 * The AE-7 has no front microphone, so limit items to 2: rear mic and
5963 * line-in.
5964 */
5965 if (ca0132_quirk(spec) == QUIRK_AE7)
5966 items = 2;
5967
5968 if (sel >= items)
5969 return 0;
5970
5971 codec_dbg(codec, "ca0132_alt_input_select: sel=%d, preset=%s\n",
5972 sel, in_src_str[sel]);
5973
5974 spec->in_enum_val = sel;
5975
5976 ca0132_alt_select_in(codec);
5977
5978 return 1;
5979 }
5980
5981 /* Sound Blaster Z Output Select Control */
5982 static int ca0132_alt_output_select_get_info(struct snd_kcontrol *kcontrol,
5983 struct snd_ctl_elem_info *uinfo)
5984 {
5985 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5986 uinfo->count = 1;
5987 uinfo->value.enumerated.items = NUM_OF_OUTPUTS;
5988 if (uinfo->value.enumerated.item >= NUM_OF_OUTPUTS)
5989 uinfo->value.enumerated.item = NUM_OF_OUTPUTS - 1;
5990 strscpy(uinfo->value.enumerated.name,
5991 out_type_str[uinfo->value.enumerated.item]);
5992 return 0;
5993 }
5994
5995 static int ca0132_alt_output_select_get(struct snd_kcontrol *kcontrol,
5996 struct snd_ctl_elem_value *ucontrol)
5997 {
5998 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5999 struct ca0132_spec *spec = codec->spec;
6000
6001 ucontrol->value.enumerated.item[0] = spec->out_enum_val;
6002 return 0;
6003 }
6004
6005 static int ca0132_alt_output_select_put(struct snd_kcontrol *kcontrol,
6006 struct snd_ctl_elem_value *ucontrol)
6007 {
6008 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6009 struct ca0132_spec *spec = codec->spec;
6010 int sel = ucontrol->value.enumerated.item[0];
6011 unsigned int items = NUM_OF_OUTPUTS;
6012
6013 if (sel >= items)
6014 return 0;
6015
6016 codec_dbg(codec, "ca0132_alt_output_select: sel=%d, preset=%s\n",
6017 sel, out_type_str[sel]);
6018
6019 spec->out_enum_val = sel;
6020
6021 ca0132_disable_hp_auto_detect(codec);
6022 ca0132_alt_select_out(codec);
6023
6024 return 1;
6025 }
6026
6027 /* Select surround output type: 2.1, 4.0, 4.1, or 5.1. */
6028 static int ca0132_alt_speaker_channel_cfg_get_info(struct snd_kcontrol *kcontrol,
6029 struct snd_ctl_elem_info *uinfo)
6030 {
6031 unsigned int items = SPEAKER_CHANNEL_CFG_COUNT;
6032
6033 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6034 uinfo->count = 1;
6035 uinfo->value.enumerated.items = items;
6036 if (uinfo->value.enumerated.item >= items)
6037 uinfo->value.enumerated.item = items - 1;
6038 strscpy(uinfo->value.enumerated.name,
6039 speaker_channel_cfgs[uinfo->value.enumerated.item].name);
6040 return 0;
6041 }
6042
6043 static int ca0132_alt_speaker_channel_cfg_get(struct snd_kcontrol *kcontrol,
6044 struct snd_ctl_elem_value *ucontrol)
6045 {
6046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6047 struct ca0132_spec *spec = codec->spec;
6048
6049 ucontrol->value.enumerated.item[0] = spec->channel_cfg_val;
6050 return 0;
6051 }
6052
6053 static int ca0132_alt_speaker_channel_cfg_put(struct snd_kcontrol *kcontrol,
6054 struct snd_ctl_elem_value *ucontrol)
6055 {
6056 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6057 struct ca0132_spec *spec = codec->spec;
6058 int sel = ucontrol->value.enumerated.item[0];
6059 unsigned int items = SPEAKER_CHANNEL_CFG_COUNT;
6060
6061 if (sel >= items)
6062 return 0;
6063
6064 codec_dbg(codec, "ca0132_alt_speaker_channels: sel=%d, channels=%s\n",
6065 sel, speaker_channel_cfgs[sel].name);
6066
6067 spec->channel_cfg_val = sel;
6068
6069 if (spec->out_enum_val == SPEAKER_OUT)
6070 ca0132_alt_select_out(codec);
6071
6072 return 1;
6073 }
6074
6075 /*
6076 * Smart Volume output setting control. Three different settings, Normal,
6077 * which takes the value from the smart volume slider. The two others, loud
6078 * and night, disregard the slider value and have uneditable values.
6079 */
6080 #define NUM_OF_SVM_SETTINGS 3
6081 static const char *const out_svm_set_enum_str[3] = {"Normal", "Loud", "Night" };
6082
6083 static int ca0132_alt_svm_setting_info(struct snd_kcontrol *kcontrol,
6084 struct snd_ctl_elem_info *uinfo)
6085 {
6086 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6087 uinfo->count = 1;
6088 uinfo->value.enumerated.items = NUM_OF_SVM_SETTINGS;
6089 if (uinfo->value.enumerated.item >= NUM_OF_SVM_SETTINGS)
6090 uinfo->value.enumerated.item = NUM_OF_SVM_SETTINGS - 1;
6091 strscpy(uinfo->value.enumerated.name,
6092 out_svm_set_enum_str[uinfo->value.enumerated.item]);
6093 return 0;
6094 }
6095
6096 static int ca0132_alt_svm_setting_get(struct snd_kcontrol *kcontrol,
6097 struct snd_ctl_elem_value *ucontrol)
6098 {
6099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6100 struct ca0132_spec *spec = codec->spec;
6101
6102 ucontrol->value.enumerated.item[0] = spec->smart_volume_setting;
6103 return 0;
6104 }
6105
6106 static int ca0132_alt_svm_setting_put(struct snd_kcontrol *kcontrol,
6107 struct snd_ctl_elem_value *ucontrol)
6108 {
6109 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6110 struct ca0132_spec *spec = codec->spec;
6111 int sel = ucontrol->value.enumerated.item[0];
6112 unsigned int items = NUM_OF_SVM_SETTINGS;
6113 unsigned int idx = SMART_VOLUME - EFFECT_START_NID;
6114 unsigned int tmp;
6115
6116 if (sel >= items)
6117 return 0;
6118
6119 codec_dbg(codec, "ca0132_alt_svm_setting: sel=%d, preset=%s\n",
6120 sel, out_svm_set_enum_str[sel]);
6121
6122 spec->smart_volume_setting = sel;
6123
6124 switch (sel) {
6125 case 0:
6126 tmp = FLOAT_ZERO;
6127 break;
6128 case 1:
6129 tmp = FLOAT_ONE;
6130 break;
6131 case 2:
6132 tmp = FLOAT_TWO;
6133 break;
6134 default:
6135 tmp = FLOAT_ZERO;
6136 break;
6137 }
6138 /* Req 2 is the Smart Volume Setting req. */
6139 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
6140 ca0132_effects[idx].reqs[2], tmp);
6141 return 1;
6142 }
6143
6144 /* Sound Blaster Z EQ preset controls */
6145 static int ca0132_alt_eq_preset_info(struct snd_kcontrol *kcontrol,
6146 struct snd_ctl_elem_info *uinfo)
6147 {
6148 unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
6149
6150 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6151 uinfo->count = 1;
6152 uinfo->value.enumerated.items = items;
6153 if (uinfo->value.enumerated.item >= items)
6154 uinfo->value.enumerated.item = items - 1;
6155 strscpy(uinfo->value.enumerated.name,
6156 ca0132_alt_eq_presets[uinfo->value.enumerated.item].name);
6157 return 0;
6158 }
6159
6160 static int ca0132_alt_eq_preset_get(struct snd_kcontrol *kcontrol,
6161 struct snd_ctl_elem_value *ucontrol)
6162 {
6163 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6164 struct ca0132_spec *spec = codec->spec;
6165
6166 ucontrol->value.enumerated.item[0] = spec->eq_preset_val;
6167 return 0;
6168 }
6169
6170 static int ca0132_alt_eq_preset_put(struct snd_kcontrol *kcontrol,
6171 struct snd_ctl_elem_value *ucontrol)
6172 {
6173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6174 struct ca0132_spec *spec = codec->spec;
6175 int i, err = 0;
6176 int sel = ucontrol->value.enumerated.item[0];
6177 unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
6178
6179 if (sel >= items)
6180 return 0;
6181
6182 codec_dbg(codec, "%s: sel=%d, preset=%s\n", __func__, sel,
6183 ca0132_alt_eq_presets[sel].name);
6184 /*
6185 * Idx 0 is default.
6186 * Default needs to qualify with CrystalVoice state.
6187 */
6188 for (i = 0; i < EQ_PRESET_MAX_PARAM_COUNT; i++) {
6189 err = dspio_set_uint_param(codec, ca0132_alt_eq_enum.mid,
6190 ca0132_alt_eq_enum.reqs[i],
6191 ca0132_alt_eq_presets[sel].vals[i]);
6192 if (err < 0)
6193 break;
6194 }
6195
6196 if (err >= 0)
6197 spec->eq_preset_val = sel;
6198
6199 return 1;
6200 }
6201
6202 static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
6203 struct snd_ctl_elem_info *uinfo)
6204 {
6205 unsigned int items = ARRAY_SIZE(ca0132_voicefx_presets);
6206
6207 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
6208 uinfo->count = 1;
6209 uinfo->value.enumerated.items = items;
6210 if (uinfo->value.enumerated.item >= items)
6211 uinfo->value.enumerated.item = items - 1;
6212 strscpy(uinfo->value.enumerated.name,
6213 ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
6214 return 0;
6215 }
6216
6217 static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
6218 struct snd_ctl_elem_value *ucontrol)
6219 {
6220 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6221 struct ca0132_spec *spec = codec->spec;
6222
6223 ucontrol->value.enumerated.item[0] = spec->voicefx_val;
6224 return 0;
6225 }
6226
6227 static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
6228 struct snd_ctl_elem_value *ucontrol)
6229 {
6230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6231 struct ca0132_spec *spec = codec->spec;
6232 int i, err = 0;
6233 int sel = ucontrol->value.enumerated.item[0];
6234
6235 if (sel >= ARRAY_SIZE(ca0132_voicefx_presets))
6236 return 0;
6237
6238 codec_dbg(codec, "ca0132_voicefx_put: sel=%d, preset=%s\n",
6239 sel, ca0132_voicefx_presets[sel].name);
6240
6241 /*
6242 * Idx 0 is default.
6243 * Default needs to qualify with CrystalVoice state.
6244 */
6245 for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
6246 err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
6247 ca0132_voicefx.reqs[i],
6248 ca0132_voicefx_presets[sel].vals[i]);
6249 if (err < 0)
6250 break;
6251 }
6252
6253 if (err >= 0) {
6254 spec->voicefx_val = sel;
6255 /* enable voice fx */
6256 ca0132_voicefx_set(codec, (sel ? 1 : 0));
6257 }
6258
6259 return 1;
6260 }
6261
6262 static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
6263 struct snd_ctl_elem_value *ucontrol)
6264 {
6265 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6266 struct ca0132_spec *spec = codec->spec;
6267 hda_nid_t nid = get_amp_nid(kcontrol);
6268 int ch = get_amp_channels(kcontrol);
6269 long *valp = ucontrol->value.integer.value;
6270
6271 /* vnode */
6272 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
6273 if (ch & 1) {
6274 *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
6275 valp++;
6276 }
6277 if (ch & 2) {
6278 *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
6279 valp++;
6280 }
6281 return 0;
6282 }
6283
6284 /* effects, include PE and CrystalVoice */
6285 if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
6286 *valp = spec->effects_switch[nid - EFFECT_START_NID];
6287 return 0;
6288 }
6289
6290 /* mic boost */
6291 if (nid == spec->input_pins[0]) {
6292 *valp = spec->cur_mic_boost;
6293 return 0;
6294 }
6295
6296 if (nid == ZXR_HEADPHONE_GAIN) {
6297 *valp = spec->zxr_gain_set;
6298 return 0;
6299 }
6300
6301 if (nid == SPEAKER_FULL_RANGE_FRONT || nid == SPEAKER_FULL_RANGE_REAR) {
6302 *valp = spec->speaker_range_val[nid - SPEAKER_FULL_RANGE_FRONT];
6303 return 0;
6304 }
6305
6306 if (nid == BASS_REDIRECTION) {
6307 *valp = spec->bass_redirection_val;
6308 return 0;
6309 }
6310
6311 return 0;
6312 }
6313
6314 static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
6315 struct snd_ctl_elem_value *ucontrol)
6316 {
6317 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6318 struct ca0132_spec *spec = codec->spec;
6319 hda_nid_t nid = get_amp_nid(kcontrol);
6320 int ch = get_amp_channels(kcontrol);
6321 long *valp = ucontrol->value.integer.value;
6322
6323 codec_dbg(codec, "ca0132_switch_put: nid=0x%x, val=%ld\n",
6324 nid, *valp);
6325
6326 CLASS(snd_hda_power, pm)(codec);
6327 /* vnode */
6328 if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
6329 if (ch & 1) {
6330 spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
6331 valp++;
6332 }
6333 if (ch & 2) {
6334 spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
6335 valp++;
6336 }
6337 return ca0132_vnode_switch_set(kcontrol, ucontrol);
6338 }
6339
6340 /* PE */
6341 if (nid == PLAY_ENHANCEMENT) {
6342 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
6343 return ca0132_pe_switch_set(codec);
6344 }
6345
6346 /* CrystalVoice */
6347 if (nid == CRYSTAL_VOICE) {
6348 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
6349 return ca0132_cvoice_switch_set(codec);
6350 }
6351
6352 /* out and in effects */
6353 if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
6354 ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
6355 spec->effects_switch[nid - EFFECT_START_NID] = *valp;
6356 return ca0132_effects_set(codec, nid, *valp);
6357 }
6358
6359 /* mic boost */
6360 if (nid == spec->input_pins[0]) {
6361 spec->cur_mic_boost = *valp;
6362 if (ca0132_use_alt_functions(spec)) {
6363 if (spec->in_enum_val != REAR_LINE_IN)
6364 return ca0132_mic_boost_set(codec, *valp);
6365 } else {
6366 /* Mic boost does not apply to Digital Mic */
6367 if (spec->cur_mic_type != DIGITAL_MIC)
6368 return ca0132_mic_boost_set(codec, *valp);
6369 }
6370
6371 return 1;
6372 }
6373
6374 if (nid == ZXR_HEADPHONE_GAIN) {
6375 spec->zxr_gain_set = *valp;
6376 if (spec->cur_out_type == HEADPHONE_OUT)
6377 return zxr_headphone_gain_set(codec, *valp);
6378 else
6379 return 0;
6380 }
6381
6382 if (nid == SPEAKER_FULL_RANGE_FRONT || nid == SPEAKER_FULL_RANGE_REAR) {
6383 spec->speaker_range_val[nid - SPEAKER_FULL_RANGE_FRONT] = *valp;
6384 if (spec->cur_out_type == SPEAKER_OUT)
6385 ca0132_alt_set_full_range_speaker(codec);
6386
6387 return 0;
6388 }
6389
6390 if (nid == BASS_REDIRECTION) {
6391 spec->bass_redirection_val = *valp;
6392 if (spec->cur_out_type == SPEAKER_OUT)
6393 ca0132_alt_surround_set_bass_redirection(codec, *valp);
6394
6395 return 0;
6396 }
6397
6398 return 1;
6399 }
6400
6401 /*
6402 * Volume related
6403 */
6404 /*
6405 * Sets the internal DSP decibel level to match the DAC for output, and the
6406 * ADC for input. Currently only the SBZ sets dsp capture volume level, and
6407 * all alternative codecs set DSP playback volume.
6408 */
6409 static void ca0132_alt_dsp_volume_put(struct hda_codec *codec, hda_nid_t nid)
6410 {
6411 struct ca0132_spec *spec = codec->spec;
6412 unsigned int dsp_dir;
6413 unsigned int lookup_val;
6414
6415 if (nid == VNID_SPK)
6416 dsp_dir = DSP_VOL_OUT;
6417 else
6418 dsp_dir = DSP_VOL_IN;
6419
6420 lookup_val = spec->vnode_lvol[nid - VNODE_START_NID];
6421
6422 dspio_set_uint_param(codec,
6423 ca0132_alt_vol_ctls[dsp_dir].mid,
6424 ca0132_alt_vol_ctls[dsp_dir].reqs[0],
6425 float_vol_db_lookup[lookup_val]);
6426
6427 lookup_val = spec->vnode_rvol[nid - VNODE_START_NID];
6428
6429 dspio_set_uint_param(codec,
6430 ca0132_alt_vol_ctls[dsp_dir].mid,
6431 ca0132_alt_vol_ctls[dsp_dir].reqs[1],
6432 float_vol_db_lookup[lookup_val]);
6433
6434 dspio_set_uint_param(codec,
6435 ca0132_alt_vol_ctls[dsp_dir].mid,
6436 ca0132_alt_vol_ctls[dsp_dir].reqs[2], FLOAT_ZERO);
6437 }
6438
6439 static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
6440 struct snd_ctl_elem_info *uinfo)
6441 {
6442 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6443 struct ca0132_spec *spec = codec->spec;
6444 hda_nid_t nid = get_amp_nid(kcontrol);
6445 int ch = get_amp_channels(kcontrol);
6446 int dir = get_amp_direction(kcontrol);
6447 unsigned long pval;
6448 int err;
6449
6450 switch (nid) {
6451 case VNID_SPK:
6452 /* follow shared_out info */
6453 nid = spec->shared_out_nid;
6454 scoped_guard(mutex, &codec->control_mutex) {
6455 pval = kcontrol->private_value;
6456 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6457 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
6458 kcontrol->private_value = pval;
6459 }
6460 break;
6461 case VNID_MIC:
6462 /* follow shared_mic info */
6463 nid = spec->shared_mic_nid;
6464 scoped_guard(mutex, &codec->control_mutex) {
6465 pval = kcontrol->private_value;
6466 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6467 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
6468 kcontrol->private_value = pval;
6469 }
6470 break;
6471 default:
6472 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
6473 }
6474 return err;
6475 }
6476
6477 static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
6478 struct snd_ctl_elem_value *ucontrol)
6479 {
6480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6481 struct ca0132_spec *spec = codec->spec;
6482 hda_nid_t nid = get_amp_nid(kcontrol);
6483 int ch = get_amp_channels(kcontrol);
6484 long *valp = ucontrol->value.integer.value;
6485
6486 /* store the left and right volume */
6487 if (ch & 1) {
6488 *valp = spec->vnode_lvol[nid - VNODE_START_NID];
6489 valp++;
6490 }
6491 if (ch & 2) {
6492 *valp = spec->vnode_rvol[nid - VNODE_START_NID];
6493 valp++;
6494 }
6495 return 0;
6496 }
6497
6498 static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
6499 struct snd_ctl_elem_value *ucontrol)
6500 {
6501 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6502 struct ca0132_spec *spec = codec->spec;
6503 hda_nid_t nid = get_amp_nid(kcontrol);
6504 int ch = get_amp_channels(kcontrol);
6505 long *valp = ucontrol->value.integer.value;
6506 hda_nid_t shared_nid = 0;
6507 bool effective;
6508 int changed = 1;
6509
6510 /* store the left and right volume */
6511 if (ch & 1) {
6512 spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
6513 valp++;
6514 }
6515 if (ch & 2) {
6516 spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
6517 valp++;
6518 }
6519
6520 /* if effective conditions, then update hw immediately. */
6521 effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
6522 if (effective) {
6523 int dir = get_amp_direction(kcontrol);
6524 unsigned long pval;
6525
6526 CLASS(snd_hda_power, pm)(codec);
6527 guard(mutex)(&codec->control_mutex);
6528 pval = kcontrol->private_value;
6529 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
6530 0, dir);
6531 changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
6532 kcontrol->private_value = pval;
6533 }
6534
6535 return changed;
6536 }
6537
6538 /*
6539 * This function is the same as the one above, because using an if statement
6540 * inside of the above volume control for the DSP volume would cause too much
6541 * lag. This is a lot more smooth.
6542 */
6543 static int ca0132_alt_volume_put(struct snd_kcontrol *kcontrol,
6544 struct snd_ctl_elem_value *ucontrol)
6545 {
6546 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6547 struct ca0132_spec *spec = codec->spec;
6548 hda_nid_t nid = get_amp_nid(kcontrol);
6549 int ch = get_amp_channels(kcontrol);
6550 long *valp = ucontrol->value.integer.value;
6551 hda_nid_t vnid = 0;
6552
6553 switch (nid) {
6554 case 0x02:
6555 vnid = VNID_SPK;
6556 break;
6557 case 0x07:
6558 vnid = VNID_MIC;
6559 break;
6560 }
6561
6562 /* store the left and right volume */
6563 if (ch & 1) {
6564 spec->vnode_lvol[vnid - VNODE_START_NID] = *valp;
6565 valp++;
6566 }
6567 if (ch & 2) {
6568 spec->vnode_rvol[vnid - VNODE_START_NID] = *valp;
6569 valp++;
6570 }
6571
6572 CLASS(snd_hda_power, pm)(codec);
6573 ca0132_alt_dsp_volume_put(codec, vnid);
6574 guard(mutex)(&codec->control_mutex);
6575 return snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
6576 }
6577
6578 static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
6579 unsigned int size, unsigned int __user *tlv)
6580 {
6581 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
6582 struct ca0132_spec *spec = codec->spec;
6583 hda_nid_t nid = get_amp_nid(kcontrol);
6584 int ch = get_amp_channels(kcontrol);
6585 int dir = get_amp_direction(kcontrol);
6586 unsigned long pval;
6587 int err;
6588
6589 switch (nid) {
6590 case VNID_SPK:
6591 /* follow shared_out tlv */
6592 nid = spec->shared_out_nid;
6593 scoped_guard(mutex, &codec->control_mutex) {
6594 pval = kcontrol->private_value;
6595 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6596 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
6597 kcontrol->private_value = pval;
6598 }
6599 break;
6600 case VNID_MIC:
6601 /* follow shared_mic tlv */
6602 nid = spec->shared_mic_nid;
6603 scoped_guard(mutex, &codec->control_mutex) {
6604 pval = kcontrol->private_value;
6605 kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
6606 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
6607 kcontrol->private_value = pval;
6608 }
6609 break;
6610 default:
6611 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
6612 }
6613 return err;
6614 }
6615
6616 /* Add volume slider control for effect level */
6617 static int ca0132_alt_add_effect_slider(struct hda_codec *codec, hda_nid_t nid,
6618 const char *pfx, int dir)
6619 {
6620 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6621 int type = dir ? HDA_INPUT : HDA_OUTPUT;
6622 struct snd_kcontrol_new knew =
6623 HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
6624
6625 sprintf(namestr, "FX: %s %s Volume", pfx, dirstr[dir]);
6626
6627 knew.tlv.c = NULL;
6628
6629 switch (nid) {
6630 case XBASS_XOVER:
6631 knew.info = ca0132_alt_xbass_xover_slider_info;
6632 knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
6633 knew.put = ca0132_alt_xbass_xover_slider_put;
6634 break;
6635 default:
6636 knew.info = ca0132_alt_effect_slider_info;
6637 knew.get = ca0132_alt_slider_ctl_get;
6638 knew.put = ca0132_alt_effect_slider_put;
6639 knew.private_value =
6640 HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
6641 break;
6642 }
6643
6644 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
6645 }
6646
6647 /*
6648 * Added FX: prefix for the alternative codecs, because otherwise the surround
6649 * effect would conflict with the Surround sound volume control. Also seems more
6650 * clear as to what the switches do. Left alone for others.
6651 */
6652 static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
6653 const char *pfx, int dir)
6654 {
6655 struct ca0132_spec *spec = codec->spec;
6656 char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6657 int type = dir ? HDA_INPUT : HDA_OUTPUT;
6658 struct snd_kcontrol_new knew =
6659 CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
6660 /* If using alt_controls, add FX: prefix. But, don't add FX:
6661 * prefix to OutFX or InFX enable controls.
6662 */
6663 if (ca0132_use_alt_controls(spec) && (nid <= IN_EFFECT_END_NID))
6664 sprintf(namestr, "FX: %s %s Switch", pfx, dirstr[dir]);
6665 else
6666 sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
6667
6668 return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
6669 }
6670
6671 static int add_voicefx(struct hda_codec *codec)
6672 {
6673 struct snd_kcontrol_new knew =
6674 HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
6675 VOICEFX, 1, 0, HDA_INPUT);
6676 knew.info = ca0132_voicefx_info;
6677 knew.get = ca0132_voicefx_get;
6678 knew.put = ca0132_voicefx_put;
6679 return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
6680 }
6681
6682 /* Create the EQ Preset control */
6683 static int add_ca0132_alt_eq_presets(struct hda_codec *codec)
6684 {
6685 struct snd_kcontrol_new knew =
6686 HDA_CODEC_MUTE_MONO(ca0132_alt_eq_enum.name,
6687 EQ_PRESET_ENUM, 1, 0, HDA_OUTPUT);
6688 knew.info = ca0132_alt_eq_preset_info;
6689 knew.get = ca0132_alt_eq_preset_get;
6690 knew.put = ca0132_alt_eq_preset_put;
6691 return snd_hda_ctl_add(codec, EQ_PRESET_ENUM,
6692 snd_ctl_new1(&knew, codec));
6693 }
6694
6695 /*
6696 * Add enumerated control for the three different settings of the smart volume
6697 * output effect. Normal just uses the slider value, and loud and night are
6698 * their own things that ignore that value.
6699 */
6700 static int ca0132_alt_add_svm_enum(struct hda_codec *codec)
6701 {
6702 struct snd_kcontrol_new knew =
6703 HDA_CODEC_MUTE_MONO("FX: Smart Volume Setting",
6704 SMART_VOLUME_ENUM, 1, 0, HDA_OUTPUT);
6705 knew.info = ca0132_alt_svm_setting_info;
6706 knew.get = ca0132_alt_svm_setting_get;
6707 knew.put = ca0132_alt_svm_setting_put;
6708 return snd_hda_ctl_add(codec, SMART_VOLUME_ENUM,
6709 snd_ctl_new1(&knew, codec));
6710
6711 }
6712
6713 /*
6714 * Create an Output Select enumerated control for codecs with surround
6715 * out capabilities.
6716 */
6717 static int ca0132_alt_add_output_enum(struct hda_codec *codec)
6718 {
6719 struct snd_kcontrol_new knew =
6720 HDA_CODEC_MUTE_MONO("Output Select",
6721 OUTPUT_SOURCE_ENUM, 1, 0, HDA_OUTPUT);
6722 knew.info = ca0132_alt_output_select_get_info;
6723 knew.get = ca0132_alt_output_select_get;
6724 knew.put = ca0132_alt_output_select_put;
6725 return snd_hda_ctl_add(codec, OUTPUT_SOURCE_ENUM,
6726 snd_ctl_new1(&knew, codec));
6727 }
6728
6729 /*
6730 * Add a control for selecting channel count on speaker output. Setting this
6731 * allows the DSP to do bass redirection and channel upmixing on surround
6732 * configurations.
6733 */
6734 static int ca0132_alt_add_speaker_channel_cfg_enum(struct hda_codec *codec)
6735 {
6736 struct snd_kcontrol_new knew =
6737 HDA_CODEC_MUTE_MONO("Surround Channel Config",
6738 SPEAKER_CHANNEL_CFG_ENUM, 1, 0, HDA_OUTPUT);
6739 knew.info = ca0132_alt_speaker_channel_cfg_get_info;
6740 knew.get = ca0132_alt_speaker_channel_cfg_get;
6741 knew.put = ca0132_alt_speaker_channel_cfg_put;
6742 return snd_hda_ctl_add(codec, SPEAKER_CHANNEL_CFG_ENUM,
6743 snd_ctl_new1(&knew, codec));
6744 }
6745
6746 /*
6747 * Full range front stereo and rear surround switches. When these are set to
6748 * full range, the lower frequencies from these channels are no longer
6749 * redirected to the LFE channel.
6750 */
6751 static int ca0132_alt_add_front_full_range_switch(struct hda_codec *codec)
6752 {
6753 struct snd_kcontrol_new knew =
6754 CA0132_CODEC_MUTE_MONO("Full-Range Front Speakers",
6755 SPEAKER_FULL_RANGE_FRONT, 1, HDA_OUTPUT);
6756
6757 return snd_hda_ctl_add(codec, SPEAKER_FULL_RANGE_FRONT,
6758 snd_ctl_new1(&knew, codec));
6759 }
6760
6761 static int ca0132_alt_add_rear_full_range_switch(struct hda_codec *codec)
6762 {
6763 struct snd_kcontrol_new knew =
6764 CA0132_CODEC_MUTE_MONO("Full-Range Rear Speakers",
6765 SPEAKER_FULL_RANGE_REAR, 1, HDA_OUTPUT);
6766
6767 return snd_hda_ctl_add(codec, SPEAKER_FULL_RANGE_REAR,
6768 snd_ctl_new1(&knew, codec));
6769 }
6770
6771 /*
6772 * Bass redirection redirects audio below the crossover frequency to the LFE
6773 * channel on speakers that are set as not being full-range. On configurations
6774 * without an LFE channel, it does nothing. Bass redirection seems to be the
6775 * replacement for X-Bass on configurations with an LFE channel.
6776 */
6777 static int ca0132_alt_add_bass_redirection_crossover(struct hda_codec *codec)
6778 {
6779 const char *namestr = "Bass Redirection Crossover";
6780 struct snd_kcontrol_new knew =
6781 HDA_CODEC_VOLUME_MONO(namestr, BASS_REDIRECTION_XOVER, 1, 0,
6782 HDA_OUTPUT);
6783
6784 knew.tlv.c = NULL;
6785 knew.info = ca0132_alt_xbass_xover_slider_info;
6786 knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
6787 knew.put = ca0132_alt_xbass_xover_slider_put;
6788
6789 return snd_hda_ctl_add(codec, BASS_REDIRECTION_XOVER,
6790 snd_ctl_new1(&knew, codec));
6791 }
6792
6793 static int ca0132_alt_add_bass_redirection_switch(struct hda_codec *codec)
6794 {
6795 const char *namestr = "Bass Redirection";
6796 struct snd_kcontrol_new knew =
6797 CA0132_CODEC_MUTE_MONO(namestr, BASS_REDIRECTION, 1,
6798 HDA_OUTPUT);
6799
6800 return snd_hda_ctl_add(codec, BASS_REDIRECTION,
6801 snd_ctl_new1(&knew, codec));
6802 }
6803
6804 /*
6805 * Create an Input Source enumerated control for the alternate ca0132 codecs
6806 * because the front microphone has no auto-detect, and Line-in has to be set
6807 * somehow.
6808 */
6809 static int ca0132_alt_add_input_enum(struct hda_codec *codec)
6810 {
6811 struct snd_kcontrol_new knew =
6812 HDA_CODEC_MUTE_MONO("Input Source",
6813 INPUT_SOURCE_ENUM, 1, 0, HDA_INPUT);
6814 knew.info = ca0132_alt_input_source_info;
6815 knew.get = ca0132_alt_input_source_get;
6816 knew.put = ca0132_alt_input_source_put;
6817 return snd_hda_ctl_add(codec, INPUT_SOURCE_ENUM,
6818 snd_ctl_new1(&knew, codec));
6819 }
6820
6821 /*
6822 * Add mic boost enumerated control. Switches through 0dB to 30dB. This adds
6823 * more control than the original mic boost, which is either full 30dB or off.
6824 */
6825 static int ca0132_alt_add_mic_boost_enum(struct hda_codec *codec)
6826 {
6827 struct snd_kcontrol_new knew =
6828 HDA_CODEC_MUTE_MONO("Mic Boost Capture Switch",
6829 MIC_BOOST_ENUM, 1, 0, HDA_INPUT);
6830 knew.info = ca0132_alt_mic_boost_info;
6831 knew.get = ca0132_alt_mic_boost_get;
6832 knew.put = ca0132_alt_mic_boost_put;
6833 return snd_hda_ctl_add(codec, MIC_BOOST_ENUM,
6834 snd_ctl_new1(&knew, codec));
6835
6836 }
6837
6838 /*
6839 * Add headphone gain enumerated control for the AE-5. This switches between
6840 * three modes, low, medium, and high. When non-headphone outputs are selected,
6841 * it is automatically set to high. This is the same behavior as Windows.
6842 */
6843 static int ae5_add_headphone_gain_enum(struct hda_codec *codec)
6844 {
6845 struct snd_kcontrol_new knew =
6846 HDA_CODEC_MUTE_MONO("AE-5: Headphone Gain",
6847 AE5_HEADPHONE_GAIN_ENUM, 1, 0, HDA_OUTPUT);
6848 knew.info = ae5_headphone_gain_info;
6849 knew.get = ae5_headphone_gain_get;
6850 knew.put = ae5_headphone_gain_put;
6851 return snd_hda_ctl_add(codec, AE5_HEADPHONE_GAIN_ENUM,
6852 snd_ctl_new1(&knew, codec));
6853 }
6854
6855 /*
6856 * Add sound filter enumerated control for the AE-5. This adds three different
6857 * settings: Slow Roll Off, Minimum Phase, and Fast Roll Off. From what I've
6858 * read into it, it changes the DAC's interpolation filter.
6859 */
6860 static int ae5_add_sound_filter_enum(struct hda_codec *codec)
6861 {
6862 struct snd_kcontrol_new knew =
6863 HDA_CODEC_MUTE_MONO("AE-5: Sound Filter",
6864 AE5_SOUND_FILTER_ENUM, 1, 0, HDA_OUTPUT);
6865 knew.info = ae5_sound_filter_info;
6866 knew.get = ae5_sound_filter_get;
6867 knew.put = ae5_sound_filter_put;
6868 return snd_hda_ctl_add(codec, AE5_SOUND_FILTER_ENUM,
6869 snd_ctl_new1(&knew, codec));
6870 }
6871
6872 static int zxr_add_headphone_gain_switch(struct hda_codec *codec)
6873 {
6874 struct snd_kcontrol_new knew =
6875 CA0132_CODEC_MUTE_MONO("ZxR: 600 Ohm Gain",
6876 ZXR_HEADPHONE_GAIN, 1, HDA_OUTPUT);
6877
6878 return snd_hda_ctl_add(codec, ZXR_HEADPHONE_GAIN,
6879 snd_ctl_new1(&knew, codec));
6880 }
6881
6882 /*
6883 * Need to create follower controls for the alternate codecs that have surround
6884 * capabilities.
6885 */
6886 static const char * const ca0132_alt_follower_pfxs[] = {
6887 "Front", "Surround", "Center", "LFE", NULL,
6888 };
6889
6890 /*
6891 * Also need special channel map, because the default one is incorrect.
6892 * I think this has to do with the pin for rear surround being 0x11,
6893 * and the center/lfe being 0x10. Usually the pin order is the opposite.
6894 */
6895 static const struct snd_pcm_chmap_elem ca0132_alt_chmaps[] = {
6896 { .channels = 2,
6897 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
6898 { .channels = 4,
6899 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
6900 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
6901 { .channels = 6,
6902 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
6903 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
6904 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
6905 { }
6906 };
6907
6908 /* Add the correct chmap for streams with 6 channels. */
6909 static void ca0132_alt_add_chmap_ctls(struct hda_codec *codec)
6910 {
6911 int err = 0;
6912 struct hda_pcm *pcm;
6913
6914 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
6915 struct hda_pcm_stream *hinfo =
6916 &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
6917 struct snd_pcm_chmap *chmap;
6918 const struct snd_pcm_chmap_elem *elem;
6919
6920 elem = ca0132_alt_chmaps;
6921 if (hinfo->channels_max == 6) {
6922 err = snd_pcm_add_chmap_ctls(pcm->pcm,
6923 SNDRV_PCM_STREAM_PLAYBACK,
6924 elem, hinfo->channels_max, 0, &chmap);
6925 if (err < 0)
6926 codec_dbg(codec, "snd_pcm_add_chmap_ctls failed!");
6927 }
6928 }
6929 }
6930
6931 /*
6932 * When changing Node IDs for Mixer Controls below, make sure to update
6933 * Node IDs in ca0132_config() as well.
6934 */
6935 static const struct snd_kcontrol_new ca0132_mixer[] = {
6936 CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
6937 CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
6938 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
6939 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
6940 HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
6941 HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
6942 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
6943 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
6944 CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
6945 0x12, 1, HDA_INPUT),
6946 CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
6947 VNID_HP_SEL, 1, HDA_OUTPUT),
6948 CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
6949 VNID_AMIC1_SEL, 1, HDA_INPUT),
6950 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
6951 VNID_HP_ASEL, 1, HDA_OUTPUT),
6952 CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
6953 VNID_AMIC1_ASEL, 1, HDA_INPUT),
6954 { } /* end */
6955 };
6956
6957 /*
6958 * Desktop specific control mixer. Removes auto-detect for mic, and adds
6959 * surround controls. Also sets both the Front Playback and Capture Volume
6960 * controls to alt so they set the DSP's decibel level.
6961 */
6962 static const struct snd_kcontrol_new desktop_mixer[] = {
6963 CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
6964 CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
6965 HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
6966 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
6967 HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
6968 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
6969 HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
6970 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
6971 CA0132_ALT_CODEC_VOL("Capture Volume", 0x07, HDA_INPUT),
6972 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
6973 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
6974 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
6975 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
6976 VNID_HP_ASEL, 1, HDA_OUTPUT),
6977 { } /* end */
6978 };
6979
6980 /*
6981 * Same as the Sound Blaster Z, except doesn't use the alt volume for capture
6982 * because it doesn't set decibel levels for the DSP for capture.
6983 */
6984 static const struct snd_kcontrol_new r3di_mixer[] = {
6985 CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
6986 CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
6987 HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
6988 HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
6989 HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
6990 HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
6991 HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
6992 HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
6993 CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
6994 CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
6995 HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
6996 HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
6997 CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
6998 VNID_HP_ASEL, 1, HDA_OUTPUT),
6999 { } /* end */
7000 };
7001
7002 static int ca0132_build_controls(struct hda_codec *codec)
7003 {
7004 struct ca0132_spec *spec = codec->spec;
7005 int i, num_fx, num_sliders;
7006 int err = 0;
7007
7008 /* Add Mixer controls */
7009 for (i = 0; i < spec->num_mixers; i++) {
7010 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
7011 if (err < 0)
7012 return err;
7013 }
7014 /* Setup vmaster with surround followers for desktop ca0132 devices */
7015 if (ca0132_use_alt_functions(spec)) {
7016 snd_hda_set_vmaster_tlv(codec, spec->dacs[0], HDA_OUTPUT,
7017 spec->tlv);
7018 snd_hda_add_vmaster(codec, "Master Playback Volume",
7019 spec->tlv, ca0132_alt_follower_pfxs,
7020 "Playback Volume", 0);
7021 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
7022 NULL, ca0132_alt_follower_pfxs,
7023 "Playback Switch",
7024 true, 0, &spec->vmaster_mute.sw_kctl);
7025 if (err < 0)
7026 return err;
7027 }
7028
7029 /* Add in and out effects controls.
7030 * VoiceFX, PE and CrystalVoice are added separately.
7031 */
7032 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
7033 for (i = 0; i < num_fx; i++) {
7034 /* Desktop cards break if Echo Cancellation is used. */
7035 if (ca0132_use_pci_mmio(spec)) {
7036 if (i == (ECHO_CANCELLATION - IN_EFFECT_START_NID +
7037 OUT_EFFECTS_COUNT))
7038 continue;
7039 }
7040
7041 err = add_fx_switch(codec, ca0132_effects[i].nid,
7042 ca0132_effects[i].name,
7043 ca0132_effects[i].direct);
7044 if (err < 0)
7045 return err;
7046 }
7047 /*
7048 * If codec has use_alt_controls set to true, add effect level sliders,
7049 * EQ presets, and Smart Volume presets. Also, change names to add FX
7050 * prefix, and change PlayEnhancement and CrystalVoice to match.
7051 */
7052 if (ca0132_use_alt_controls(spec)) {
7053 err = ca0132_alt_add_svm_enum(codec);
7054 if (err < 0)
7055 return err;
7056
7057 err = add_ca0132_alt_eq_presets(codec);
7058 if (err < 0)
7059 return err;
7060
7061 err = add_fx_switch(codec, PLAY_ENHANCEMENT,
7062 "Enable OutFX", 0);
7063 if (err < 0)
7064 return err;
7065
7066 err = add_fx_switch(codec, CRYSTAL_VOICE,
7067 "Enable InFX", 1);
7068 if (err < 0)
7069 return err;
7070
7071 num_sliders = OUT_EFFECTS_COUNT - 1;
7072 for (i = 0; i < num_sliders; i++) {
7073 err = ca0132_alt_add_effect_slider(codec,
7074 ca0132_effects[i].nid,
7075 ca0132_effects[i].name,
7076 ca0132_effects[i].direct);
7077 if (err < 0)
7078 return err;
7079 }
7080
7081 err = ca0132_alt_add_effect_slider(codec, XBASS_XOVER,
7082 "X-Bass Crossover", EFX_DIR_OUT);
7083
7084 if (err < 0)
7085 return err;
7086 } else {
7087 err = add_fx_switch(codec, PLAY_ENHANCEMENT,
7088 "PlayEnhancement", 0);
7089 if (err < 0)
7090 return err;
7091
7092 err = add_fx_switch(codec, CRYSTAL_VOICE,
7093 "CrystalVoice", 1);
7094 if (err < 0)
7095 return err;
7096 }
7097 err = add_voicefx(codec);
7098 if (err < 0)
7099 return err;
7100
7101 /*
7102 * If the codec uses alt_functions, you need the enumerated controls
7103 * to select the new outputs and inputs, plus add the new mic boost
7104 * setting control.
7105 */
7106 if (ca0132_use_alt_functions(spec)) {
7107 err = ca0132_alt_add_output_enum(codec);
7108 if (err < 0)
7109 return err;
7110 err = ca0132_alt_add_speaker_channel_cfg_enum(codec);
7111 if (err < 0)
7112 return err;
7113 err = ca0132_alt_add_front_full_range_switch(codec);
7114 if (err < 0)
7115 return err;
7116 err = ca0132_alt_add_rear_full_range_switch(codec);
7117 if (err < 0)
7118 return err;
7119 err = ca0132_alt_add_bass_redirection_crossover(codec);
7120 if (err < 0)
7121 return err;
7122 err = ca0132_alt_add_bass_redirection_switch(codec);
7123 if (err < 0)
7124 return err;
7125 err = ca0132_alt_add_mic_boost_enum(codec);
7126 if (err < 0)
7127 return err;
7128 /*
7129 * ZxR only has microphone input, there is no front panel
7130 * header on the card, and aux-in is handled by the DBPro board.
7131 */
7132 if (ca0132_quirk(spec) != QUIRK_ZXR) {
7133 err = ca0132_alt_add_input_enum(codec);
7134 if (err < 0)
7135 return err;
7136 }
7137 }
7138
7139 switch (ca0132_quirk(spec)) {
7140 case QUIRK_AE5:
7141 case QUIRK_AE7:
7142 err = ae5_add_headphone_gain_enum(codec);
7143 if (err < 0)
7144 return err;
7145 err = ae5_add_sound_filter_enum(codec);
7146 if (err < 0)
7147 return err;
7148 break;
7149 case QUIRK_ZXR:
7150 err = zxr_add_headphone_gain_switch(codec);
7151 if (err < 0)
7152 return err;
7153 break;
7154 default:
7155 break;
7156 }
7157
7158 #ifdef ENABLE_TUNING_CONTROLS
7159 add_tuning_ctls(codec);
7160 #endif
7161
7162 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
7163 if (err < 0)
7164 return err;
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 err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
7172 if (err < 0)
7173 return err;
7174 /* spec->multiout.share_spdif = 1; */
7175 }
7176
7177 if (spec->dig_in) {
7178 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
7179 if (err < 0)
7180 return err;
7181 }
7182
7183 if (ca0132_use_alt_functions(spec))
7184 ca0132_alt_add_chmap_ctls(codec);
7185
7186 return 0;
7187 }
7188
7189 static int dbpro_build_controls(struct hda_codec *codec)
7190 {
7191 struct ca0132_spec *spec = codec->spec;
7192 int err = 0;
7193
7194 if (spec->dig_out) {
7195 err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
7196 spec->dig_out);
7197 if (err < 0)
7198 return err;
7199 }
7200
7201 if (spec->dig_in) {
7202 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
7203 if (err < 0)
7204 return err;
7205 }
7206
7207 return 0;
7208 }
7209
7210 /*
7211 * PCM
7212 */
7213 static const struct hda_pcm_stream ca0132_pcm_analog_playback = {
7214 .substreams = 1,
7215 .channels_min = 2,
7216 .channels_max = 6,
7217 .ops = {
7218 .prepare = ca0132_playback_pcm_prepare,
7219 .cleanup = ca0132_playback_pcm_cleanup,
7220 .get_delay = ca0132_playback_pcm_delay,
7221 },
7222 };
7223
7224 static const struct hda_pcm_stream ca0132_pcm_analog_capture = {
7225 .substreams = 1,
7226 .channels_min = 2,
7227 .channels_max = 2,
7228 .ops = {
7229 .prepare = ca0132_capture_pcm_prepare,
7230 .cleanup = ca0132_capture_pcm_cleanup,
7231 .get_delay = ca0132_capture_pcm_delay,
7232 },
7233 };
7234
7235 static const struct hda_pcm_stream ca0132_pcm_digital_playback = {
7236 .substreams = 1,
7237 .channels_min = 2,
7238 .channels_max = 2,
7239 .ops = {
7240 .open = ca0132_dig_playback_pcm_open,
7241 .close = ca0132_dig_playback_pcm_close,
7242 .prepare = ca0132_dig_playback_pcm_prepare,
7243 .cleanup = ca0132_dig_playback_pcm_cleanup
7244 },
7245 };
7246
7247 static const struct hda_pcm_stream ca0132_pcm_digital_capture = {
7248 .substreams = 1,
7249 .channels_min = 2,
7250 .channels_max = 2,
7251 };
7252
7253 static int ca0132_build_pcms(struct hda_codec *codec)
7254 {
7255 struct ca0132_spec *spec = codec->spec;
7256 struct hda_pcm *info;
7257
7258 info = snd_hda_codec_pcm_new(codec, "CA0132 Analog");
7259 if (!info)
7260 return -ENOMEM;
7261 if (ca0132_use_alt_functions(spec)) {
7262 info->own_chmap = true;
7263 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap
7264 = ca0132_alt_chmaps;
7265 }
7266 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
7267 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
7268 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
7269 spec->multiout.max_channels;
7270 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
7271 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7272 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
7273
7274 /* With the DSP enabled, desktops don't use this ADC. */
7275 if (!ca0132_use_alt_functions(spec)) {
7276 info = snd_hda_codec_pcm_new(codec, "CA0132 Analog Mic-In2");
7277 if (!info)
7278 return -ENOMEM;
7279 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
7280 ca0132_pcm_analog_capture;
7281 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7282 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
7283 }
7284
7285 info = snd_hda_codec_pcm_new(codec, "CA0132 What U Hear");
7286 if (!info)
7287 return -ENOMEM;
7288 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
7289 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7290 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
7291
7292 if (!spec->dig_out && !spec->dig_in)
7293 return 0;
7294
7295 info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
7296 if (!info)
7297 return -ENOMEM;
7298 info->pcm_type = HDA_PCM_TYPE_SPDIF;
7299 if (spec->dig_out) {
7300 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
7301 ca0132_pcm_digital_playback;
7302 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
7303 }
7304 if (spec->dig_in) {
7305 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
7306 ca0132_pcm_digital_capture;
7307 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
7308 }
7309
7310 return 0;
7311 }
7312
7313 static int dbpro_build_pcms(struct hda_codec *codec)
7314 {
7315 struct ca0132_spec *spec = codec->spec;
7316 struct hda_pcm *info;
7317
7318 info = snd_hda_codec_pcm_new(codec, "CA0132 Alt Analog");
7319 if (!info)
7320 return -ENOMEM;
7321 info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
7322 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
7323 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
7324
7325
7326 if (!spec->dig_out && !spec->dig_in)
7327 return 0;
7328
7329 info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
7330 if (!info)
7331 return -ENOMEM;
7332 info->pcm_type = HDA_PCM_TYPE_SPDIF;
7333 if (spec->dig_out) {
7334 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
7335 ca0132_pcm_digital_playback;
7336 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
7337 }
7338 if (spec->dig_in) {
7339 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
7340 ca0132_pcm_digital_capture;
7341 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
7342 }
7343
7344 return 0;
7345 }
7346
7347 static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
7348 {
7349 if (pin) {
7350 snd_hda_set_pin_ctl(codec, pin, PIN_HP);
7351 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
7352 snd_hda_codec_write(codec, pin, 0,
7353 AC_VERB_SET_AMP_GAIN_MUTE,
7354 AMP_OUT_UNMUTE);
7355 }
7356 if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
7357 snd_hda_codec_write(codec, dac, 0,
7358 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
7359 }
7360
7361 static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
7362 {
7363 if (pin) {
7364 snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
7365 if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
7366 snd_hda_codec_write(codec, pin, 0,
7367 AC_VERB_SET_AMP_GAIN_MUTE,
7368 AMP_IN_UNMUTE(0));
7369 }
7370 if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
7371 snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
7372 AMP_IN_UNMUTE(0));
7373
7374 /* init to 0 dB and unmute. */
7375 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
7376 HDA_AMP_VOLMASK, 0x5a);
7377 snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
7378 HDA_AMP_MUTE, 0);
7379 }
7380 }
7381
7382 static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
7383 {
7384 unsigned int caps;
7385
7386 caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
7387 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
7388 snd_hda_override_amp_caps(codec, nid, dir, caps);
7389 }
7390
7391 /*
7392 * Switch between Digital built-in mic and analog mic.
7393 */
7394 static void ca0132_set_dmic(struct hda_codec *codec, int enable)
7395 {
7396 struct ca0132_spec *spec = codec->spec;
7397 unsigned int tmp;
7398 u8 val;
7399 unsigned int oldval;
7400
7401 codec_dbg(codec, "ca0132_set_dmic: enable=%d\n", enable);
7402
7403 oldval = stop_mic1(codec);
7404 ca0132_set_vipsource(codec, 0);
7405 if (enable) {
7406 /* set DMic input as 2-ch */
7407 tmp = FLOAT_TWO;
7408 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7409
7410 val = spec->dmic_ctl;
7411 val |= 0x80;
7412 snd_hda_codec_write(codec, spec->input_pins[0], 0,
7413 VENDOR_CHIPIO_DMIC_CTL_SET, val);
7414
7415 if (!(spec->dmic_ctl & 0x20))
7416 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
7417 } else {
7418 /* set AMic input as mono */
7419 tmp = FLOAT_ONE;
7420 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7421
7422 val = spec->dmic_ctl;
7423 /* clear bit7 and bit5 to disable dmic */
7424 val &= 0x5f;
7425 snd_hda_codec_write(codec, spec->input_pins[0], 0,
7426 VENDOR_CHIPIO_DMIC_CTL_SET, val);
7427
7428 if (!(spec->dmic_ctl & 0x20))
7429 chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
7430 }
7431 ca0132_set_vipsource(codec, 1);
7432 resume_mic1(codec, oldval);
7433 }
7434
7435 /*
7436 * Initialization for Digital Mic.
7437 */
7438 static void ca0132_init_dmic(struct hda_codec *codec)
7439 {
7440 struct ca0132_spec *spec = codec->spec;
7441 u8 val;
7442
7443 /* Setup Digital Mic here, but don't enable.
7444 * Enable based on jack detect.
7445 */
7446
7447 /* MCLK uses MPIO1, set to enable.
7448 * Bit 2-0: MPIO select
7449 * Bit 3: set to disable
7450 * Bit 7-4: reserved
7451 */
7452 val = 0x01;
7453 snd_hda_codec_write(codec, spec->input_pins[0], 0,
7454 VENDOR_CHIPIO_DMIC_MCLK_SET, val);
7455
7456 /* Data1 uses MPIO3. Data2 not use
7457 * Bit 2-0: Data1 MPIO select
7458 * Bit 3: set disable Data1
7459 * Bit 6-4: Data2 MPIO select
7460 * Bit 7: set disable Data2
7461 */
7462 val = 0x83;
7463 snd_hda_codec_write(codec, spec->input_pins[0], 0,
7464 VENDOR_CHIPIO_DMIC_PIN_SET, val);
7465
7466 /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
7467 * Bit 3-0: Channel mask
7468 * Bit 4: set for 48KHz, clear for 32KHz
7469 * Bit 5: mode
7470 * Bit 6: set to select Data2, clear for Data1
7471 * Bit 7: set to enable DMic, clear for AMic
7472 */
7473 if (ca0132_quirk(spec) == QUIRK_ALIENWARE_M17XR4)
7474 val = 0x33;
7475 else
7476 val = 0x23;
7477 /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
7478 spec->dmic_ctl = val;
7479 snd_hda_codec_write(codec, spec->input_pins[0], 0,
7480 VENDOR_CHIPIO_DMIC_CTL_SET, val);
7481 }
7482
7483 /*
7484 * Initialization for Analog Mic 2
7485 */
7486 static void ca0132_init_analog_mic2(struct hda_codec *codec)
7487 {
7488 struct ca0132_spec *spec = codec->spec;
7489
7490 guard(mutex)(&spec->chipio_mutex);
7491
7492 chipio_8051_write_exram_no_mutex(codec, 0x1920, 0x00);
7493 chipio_8051_write_exram_no_mutex(codec, 0x192d, 0x00);
7494 }
7495
7496 static void ca0132_refresh_widget_caps(struct hda_codec *codec)
7497 {
7498 struct ca0132_spec *spec = codec->spec;
7499 int i;
7500
7501 codec_dbg(codec, "ca0132_refresh_widget_caps.\n");
7502 snd_hda_codec_update_widgets(codec);
7503
7504 for (i = 0; i < spec->multiout.num_dacs; i++)
7505 refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
7506
7507 for (i = 0; i < spec->num_outputs; i++)
7508 refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
7509
7510 for (i = 0; i < spec->num_inputs; i++) {
7511 refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
7512 refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
7513 }
7514 }
7515
7516
7517 /* If there is an active channel for some reason, find it and free it. */
7518 static void ca0132_alt_free_active_dma_channels(struct hda_codec *codec)
7519 {
7520 unsigned int i, tmp;
7521 int status;
7522
7523 /* Read active DSPDMAC channel register. */
7524 status = chipio_read(codec, DSPDMAC_CHNLSTART_MODULE_OFFSET, &tmp);
7525 if (status >= 0) {
7526 /* AND against 0xfff to get the active channel bits. */
7527 tmp = tmp & 0xfff;
7528
7529 /* If there are no active channels, nothing to free. */
7530 if (!tmp)
7531 return;
7532 } else {
7533 codec_dbg(codec, "%s: Failed to read active DSP DMA channel register.\n",
7534 __func__);
7535 return;
7536 }
7537
7538 /*
7539 * Check each DSP DMA channel for activity, and if the channel is
7540 * active, free it.
7541 */
7542 for (i = 0; i < DSPDMAC_DMA_CFG_CHANNEL_COUNT; i++) {
7543 if (dsp_is_dma_active(codec, i)) {
7544 status = dspio_free_dma_chan(codec, i);
7545 if (status < 0)
7546 codec_dbg(codec, "%s: Failed to free active DSP DMA channel %d.\n",
7547 __func__, i);
7548 }
7549 }
7550 }
7551
7552 /*
7553 * In the case of CT_EXTENSIONS_ENABLE being set to 1, and the DSP being in
7554 * use, audio is no longer routed directly to the DAC/ADC from the HDA stream.
7555 * Instead, audio is now routed through the DSP's DMA controllers, which
7556 * the DSP is tasked with setting up itself. Through debugging, it seems the
7557 * cause of most of the no-audio on startup issues were due to improperly
7558 * configured DSP DMA channels.
7559 *
7560 * Normally, the DSP configures these the first time an HDA audio stream is
7561 * started post DSP firmware download. That is why creating a 'dummy' stream
7562 * worked in fixing the audio in some cases. This works most of the time, but
7563 * sometimes if a stream is started/stopped before the DSP can setup the DMA
7564 * configuration registers, it ends up in a broken state. Issues can also
7565 * arise if streams are started in an unusual order, i.e the audio output dma
7566 * channel being sandwiched between the mic1 and mic2 dma channels.
7567 *
7568 * The solution to this is to make sure that the DSP has no DMA channels
7569 * in use post DSP firmware download, and then to manually start each default
7570 * DSP stream that uses the DMA channels. These are 0x0c, the audio output
7571 * stream, 0x03, analog mic 1, and 0x04, analog mic 2.
7572 */
7573 static void ca0132_alt_start_dsp_audio_streams(struct hda_codec *codec)
7574 {
7575 static const unsigned int dsp_dma_stream_ids[] = { 0x0c, 0x03, 0x04 };
7576 struct ca0132_spec *spec = codec->spec;
7577 unsigned int i, tmp;
7578
7579 /*
7580 * Check if any of the default streams are active, and if they are,
7581 * stop them.
7582 */
7583 scoped_guard(mutex, &spec->chipio_mutex) {
7584 for (i = 0; i < ARRAY_SIZE(dsp_dma_stream_ids); i++) {
7585 chipio_get_stream_control(codec, dsp_dma_stream_ids[i], &tmp);
7586
7587 if (tmp) {
7588 chipio_set_stream_control(codec,
7589 dsp_dma_stream_ids[i], 0);
7590 }
7591 }
7592 }
7593
7594 /*
7595 * If all DSP streams are inactive, there should be no active DSP DMA
7596 * channels. Check and make sure this is the case, and if it isn't,
7597 * free any active channels.
7598 */
7599 ca0132_alt_free_active_dma_channels(codec);
7600
7601 guard(mutex)(&spec->chipio_mutex);
7602
7603 /* Make sure stream 0x0c is six channels. */
7604 chipio_set_stream_channels(codec, 0x0c, 6);
7605
7606 for (i = 0; i < ARRAY_SIZE(dsp_dma_stream_ids); i++) {
7607 chipio_set_stream_control(codec,
7608 dsp_dma_stream_ids[i], 1);
7609
7610 /* Give the DSP some time to setup the DMA channel. */
7611 msleep(75);
7612 }
7613 }
7614
7615 /*
7616 * The region of ChipIO memory from 0x190000-0x1903fc is a sort of 'audio
7617 * router', where each entry represents a 48khz audio channel, with a format
7618 * of an 8-bit destination, an 8-bit source, and an unknown 2-bit number
7619 * value. The 2-bit number value is seemingly 0 if inactive, 1 if active,
7620 * and 3 if it's using Sample Rate Converter ports.
7621 * An example is:
7622 * 0x0001f8c0
7623 * In this case, f8 is the destination, and c0 is the source. The number value
7624 * is 1.
7625 * This region of memory is normally managed internally by the 8051, where
7626 * the region of exram memory from 0x1477-0x1575 has each byte represent an
7627 * entry within the 0x190000 range, and when a range of entries is in use, the
7628 * ending value is overwritten with 0xff.
7629 * 0x1578 in exram is a table of 0x25 entries, corresponding to the ChipIO
7630 * streamID's, where each entry is a starting 0x190000 port offset.
7631 * 0x159d in exram is the same as 0x1578, except it contains the ending port
7632 * offset for the corresponding streamID.
7633 *
7634 * On certain cards, such as the SBZ/ZxR/AE7, these are originally setup by
7635 * the 8051, then manually overwritten to remap the ports to work with the
7636 * new DACs.
7637 *
7638 * Currently known portID's:
7639 * 0x00-0x1f: HDA audio stream input/output ports.
7640 * 0x80-0xbf: Sample rate converter input/outputs. Only valid ports seem to
7641 * have the lower-nibble set to 0x1, 0x2, and 0x9.
7642 * 0xc0-0xdf: DSP DMA input/output ports. Dynamically assigned.
7643 * 0xe0-0xff: DAC/ADC audio input/output ports.
7644 *
7645 * Currently known streamID's:
7646 * 0x03: Mic1 ADC to DSP.
7647 * 0x04: Mic2 ADC to DSP.
7648 * 0x05: HDA node 0x02 audio stream to DSP.
7649 * 0x0f: DSP Mic exit to HDA node 0x07.
7650 * 0x0c: DSP processed audio to DACs.
7651 * 0x14: DAC0, front L/R.
7652 *
7653 * It is possible to route the HDA audio streams directly to the DAC and
7654 * bypass the DSP entirely, with the only downside being that since the DSP
7655 * does volume control, the only volume control you'll get is through PCM on
7656 * the PC side, in the same way volume is handled for optical out. This may be
7657 * useful for debugging.
7658 */
7659 static void chipio_remap_stream(struct hda_codec *codec,
7660 const struct chipio_stream_remap_data *remap_data)
7661 {
7662 unsigned int i, stream_offset;
7663
7664 /* Get the starting port for the stream to be remapped. */
7665 chipio_8051_read_exram(codec, 0x1578 + remap_data->stream_id,
7666 &stream_offset);
7667
7668 /*
7669 * Check if the stream's port value is 0xff, because the 8051 may not
7670 * have gotten around to setting up the stream yet. Wait until it's
7671 * setup to remap it's ports.
7672 */
7673 if (stream_offset == 0xff) {
7674 for (i = 0; i < 5; i++) {
7675 msleep(25);
7676
7677 chipio_8051_read_exram(codec, 0x1578 + remap_data->stream_id,
7678 &stream_offset);
7679
7680 if (stream_offset != 0xff)
7681 break;
7682 }
7683 }
7684
7685 if (stream_offset == 0xff) {
7686 codec_info(codec, "%s: Stream 0x%02x ports aren't allocated, remap failed!\n",
7687 __func__, remap_data->stream_id);
7688 return;
7689 }
7690
7691 /* Offset isn't in bytes, its in 32-bit words, so multiply it by 4. */
7692 stream_offset *= 0x04;
7693 stream_offset += 0x190000;
7694
7695 for (i = 0; i < remap_data->count; i++) {
7696 chipio_write_no_mutex(codec,
7697 stream_offset + remap_data->offset[i],
7698 remap_data->value[i]);
7699 }
7700
7701 /* Update stream map configuration. */
7702 chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
7703 }
7704
7705 /*
7706 * Default speaker tuning values setup for alternative codecs.
7707 */
7708 static const unsigned int sbz_default_delay_values[] = {
7709 /* Non-zero values are floating point 0.000198. */
7710 0x394f9e38, 0x394f9e38, 0x00000000, 0x00000000, 0x00000000, 0x00000000
7711 };
7712
7713 static const unsigned int zxr_default_delay_values[] = {
7714 /* Non-zero values are floating point 0.000220. */
7715 0x00000000, 0x00000000, 0x3966afcd, 0x3966afcd, 0x3966afcd, 0x3966afcd
7716 };
7717
7718 static const unsigned int ae5_default_delay_values[] = {
7719 /* Non-zero values are floating point 0.000100. */
7720 0x00000000, 0x00000000, 0x38d1b717, 0x38d1b717, 0x38d1b717, 0x38d1b717
7721 };
7722
7723 /*
7724 * If we never change these, probably only need them on initialization.
7725 */
7726 static void ca0132_alt_init_speaker_tuning(struct hda_codec *codec)
7727 {
7728 struct ca0132_spec *spec = codec->spec;
7729 unsigned int i, tmp, start_req, end_req;
7730 const unsigned int *values;
7731
7732 switch (ca0132_quirk(spec)) {
7733 case QUIRK_SBZ:
7734 values = sbz_default_delay_values;
7735 break;
7736 case QUIRK_ZXR:
7737 values = zxr_default_delay_values;
7738 break;
7739 case QUIRK_AE5:
7740 case QUIRK_AE7:
7741 values = ae5_default_delay_values;
7742 break;
7743 default:
7744 values = sbz_default_delay_values;
7745 break;
7746 }
7747
7748 tmp = FLOAT_ZERO;
7749 dspio_set_uint_param(codec, 0x96, SPEAKER_TUNING_ENABLE_CENTER_EQ, tmp);
7750
7751 start_req = SPEAKER_TUNING_FRONT_LEFT_VOL_LEVEL;
7752 end_req = SPEAKER_TUNING_REAR_RIGHT_VOL_LEVEL;
7753 for (i = start_req; i < end_req + 1; i++)
7754 dspio_set_uint_param(codec, 0x96, i, tmp);
7755
7756 start_req = SPEAKER_TUNING_FRONT_LEFT_INVERT;
7757 end_req = SPEAKER_TUNING_REAR_RIGHT_INVERT;
7758 for (i = start_req; i < end_req + 1; i++)
7759 dspio_set_uint_param(codec, 0x96, i, tmp);
7760
7761
7762 for (i = 0; i < 6; i++)
7763 dspio_set_uint_param(codec, 0x96,
7764 SPEAKER_TUNING_FRONT_LEFT_DELAY + i, values[i]);
7765 }
7766
7767 /*
7768 * Initialize mic for non-chromebook ca0132 implementations.
7769 */
7770 static void ca0132_alt_init_analog_mics(struct hda_codec *codec)
7771 {
7772 struct ca0132_spec *spec = codec->spec;
7773 unsigned int tmp;
7774
7775 /* Mic 1 Setup */
7776 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
7777 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
7778 if (ca0132_quirk(spec) == QUIRK_R3DI) {
7779 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
7780 tmp = FLOAT_ONE;
7781 } else
7782 tmp = FLOAT_THREE;
7783 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7784
7785 /* Mic 2 setup (not present on desktop cards) */
7786 chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
7787 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
7788 if (ca0132_quirk(spec) == QUIRK_R3DI)
7789 chipio_set_conn_rate(codec, 0x0F, SR_96_000);
7790 tmp = FLOAT_ZERO;
7791 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
7792 }
7793
7794 /*
7795 * Sets the source of stream 0x14 to connpointID 0x48, and the destination
7796 * connpointID to 0x91. If this isn't done, the destination is 0x71, and
7797 * you get no sound. I'm guessing this has to do with the Sound Blaster Z
7798 * having an updated DAC, which changes the destination to that DAC.
7799 */
7800 static void sbz_connect_streams(struct hda_codec *codec)
7801 {
7802 struct ca0132_spec *spec = codec->spec;
7803
7804 guard(mutex)(&spec->chipio_mutex);
7805
7806 codec_dbg(codec, "Connect Streams entered, mutex locked and loaded.\n");
7807
7808 /* This value is 0x43 for 96khz, and 0x83 for 192khz. */
7809 chipio_write_no_mutex(codec, 0x18a020, 0x00000043);
7810
7811 /* Setup stream 0x14 with it's source and destination points */
7812 chipio_set_stream_source_dest(codec, 0x14, 0x48, 0x91);
7813 chipio_set_conn_rate_no_mutex(codec, 0x48, SR_96_000);
7814 chipio_set_conn_rate_no_mutex(codec, 0x91, SR_96_000);
7815 chipio_set_stream_channels(codec, 0x14, 2);
7816 chipio_set_stream_control(codec, 0x14, 1);
7817
7818 codec_dbg(codec, "Connect Streams exited, mutex released.\n");
7819 }
7820
7821 /*
7822 * Write data through ChipIO to setup proper stream destinations.
7823 * Not sure how it exactly works, but it seems to direct data
7824 * to different destinations. Example is f8 to c0, e0 to c0.
7825 * All I know is, if you don't set these, you get no sound.
7826 */
7827 static void sbz_chipio_startup_data(struct hda_codec *codec)
7828 {
7829 const struct chipio_stream_remap_data *dsp_out_remap_data;
7830 struct ca0132_spec *spec = codec->spec;
7831
7832 guard(mutex)(&spec->chipio_mutex);
7833 codec_dbg(codec, "Startup Data entered, mutex locked and loaded.\n");
7834
7835 /* Remap DAC0's output ports. */
7836 chipio_remap_stream(codec, &stream_remap_data[0]);
7837
7838 /* Remap DSP audio output stream ports. */
7839 switch (ca0132_quirk(spec)) {
7840 case QUIRK_SBZ:
7841 dsp_out_remap_data = &stream_remap_data[1];
7842 break;
7843
7844 case QUIRK_ZXR:
7845 dsp_out_remap_data = &stream_remap_data[2];
7846 break;
7847
7848 default:
7849 dsp_out_remap_data = NULL;
7850 break;
7851 }
7852
7853 if (dsp_out_remap_data)
7854 chipio_remap_stream(codec, dsp_out_remap_data);
7855
7856 codec_dbg(codec, "Startup Data exited, mutex released.\n");
7857 }
7858
7859 static void ca0132_alt_dsp_initial_mic_setup(struct hda_codec *codec)
7860 {
7861 struct ca0132_spec *spec = codec->spec;
7862 unsigned int tmp;
7863
7864 chipio_set_stream_control(codec, 0x03, 0);
7865 chipio_set_stream_control(codec, 0x04, 0);
7866
7867 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
7868 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
7869
7870 tmp = FLOAT_THREE;
7871 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
7872
7873 chipio_set_stream_control(codec, 0x03, 1);
7874 chipio_set_stream_control(codec, 0x04, 1);
7875
7876 switch (ca0132_quirk(spec)) {
7877 case QUIRK_SBZ:
7878 chipio_write(codec, 0x18b098, 0x0000000c);
7879 chipio_write(codec, 0x18b09C, 0x0000000c);
7880 break;
7881 case QUIRK_AE5:
7882 chipio_write(codec, 0x18b098, 0x0000000c);
7883 chipio_write(codec, 0x18b09c, 0x0000004c);
7884 break;
7885 default:
7886 break;
7887 }
7888 }
7889
7890 static void ae5_post_dsp_register_set(struct hda_codec *codec)
7891 {
7892 struct ca0132_spec *spec = codec->spec;
7893
7894 chipio_8051_write_direct(codec, 0x93, 0x10);
7895 chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
7896
7897 writeb(0xff, spec->mem_base + 0x304);
7898 writeb(0xff, spec->mem_base + 0x304);
7899 writeb(0xff, spec->mem_base + 0x304);
7900 writeb(0xff, spec->mem_base + 0x304);
7901 writeb(0x00, spec->mem_base + 0x100);
7902 writeb(0xff, spec->mem_base + 0x304);
7903 writeb(0x00, spec->mem_base + 0x100);
7904 writeb(0xff, spec->mem_base + 0x304);
7905 writeb(0x00, spec->mem_base + 0x100);
7906 writeb(0xff, spec->mem_base + 0x304);
7907 writeb(0x00, spec->mem_base + 0x100);
7908 writeb(0xff, spec->mem_base + 0x304);
7909
7910 ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x3f);
7911 ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x3f);
7912 ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
7913 }
7914
7915 static void ae5_post_dsp_param_setup(struct hda_codec *codec)
7916 {
7917 /*
7918 * Param3 in the 8051's memory is represented by the ascii string 'mch'
7919 * which seems to be 'multichannel'. This is also mentioned in the
7920 * AE-5's registry values in Windows.
7921 */
7922 chipio_set_control_param(codec, 3, 0);
7923 /*
7924 * I believe ASI is 'audio serial interface' and that it's used to
7925 * change colors on the external LED strip connected to the AE-5.
7926 */
7927 chipio_set_control_flag(codec, CONTROL_FLAG_ASI_96KHZ, 1);
7928
7929 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x724, 0x83);
7930 chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
7931
7932 chipio_8051_write_exram(codec, 0xfa92, 0x22);
7933 }
7934
7935 static void ae5_post_dsp_pll_setup(struct hda_codec *codec)
7936 {
7937 chipio_8051_write_pll_pmu(codec, 0x41, 0xc8);
7938 chipio_8051_write_pll_pmu(codec, 0x45, 0xcc);
7939 chipio_8051_write_pll_pmu(codec, 0x40, 0xcb);
7940 chipio_8051_write_pll_pmu(codec, 0x43, 0xc7);
7941 chipio_8051_write_pll_pmu(codec, 0x51, 0x8d);
7942 }
7943
7944 static void ae5_post_dsp_stream_setup(struct hda_codec *codec)
7945 {
7946 struct ca0132_spec *spec = codec->spec;
7947
7948 guard(mutex)(&spec->chipio_mutex);
7949
7950 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x81);
7951
7952 chipio_set_conn_rate_no_mutex(codec, 0x70, SR_96_000);
7953
7954 chipio_set_stream_source_dest(codec, 0x5, 0x43, 0x0);
7955
7956 chipio_set_stream_source_dest(codec, 0x18, 0x9, 0xd0);
7957 chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
7958 chipio_set_stream_channels(codec, 0x18, 6);
7959 chipio_set_stream_control(codec, 0x18, 1);
7960
7961 chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 4);
7962
7963 chipio_8051_write_pll_pmu_no_mutex(codec, 0x43, 0xc7);
7964
7965 ca0113_mmio_command_set(codec, 0x48, 0x01, 0x80);
7966 }
7967
7968 static void ae5_post_dsp_startup_data(struct hda_codec *codec)
7969 {
7970 struct ca0132_spec *spec = codec->spec;
7971
7972 guard(mutex)(&spec->chipio_mutex);
7973
7974 chipio_write_no_mutex(codec, 0x189000, 0x0001f101);
7975 chipio_write_no_mutex(codec, 0x189004, 0x0001f101);
7976 chipio_write_no_mutex(codec, 0x189024, 0x00014004);
7977 chipio_write_no_mutex(codec, 0x189028, 0x0002000f);
7978
7979 ca0113_mmio_command_set(codec, 0x48, 0x0a, 0x05);
7980 chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
7981 ca0113_mmio_command_set(codec, 0x48, 0x0b, 0x12);
7982 ca0113_mmio_command_set(codec, 0x48, 0x04, 0x00);
7983 ca0113_mmio_command_set(codec, 0x48, 0x06, 0x48);
7984 ca0113_mmio_command_set(codec, 0x48, 0x0a, 0x05);
7985 ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
7986 ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
7987 ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
7988 ca0113_mmio_gpio_set(codec, 0, true);
7989 ca0113_mmio_gpio_set(codec, 1, true);
7990 ca0113_mmio_command_set(codec, 0x48, 0x07, 0x80);
7991
7992 chipio_write_no_mutex(codec, 0x18b03c, 0x00000012);
7993
7994 ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
7995 ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
7996 }
7997
7998 static void ae7_post_dsp_setup_ports(struct hda_codec *codec)
7999 {
8000 struct ca0132_spec *spec = codec->spec;
8001
8002 guard(mutex)(&spec->chipio_mutex);
8003
8004 /* Seems to share the same port remapping as the SBZ. */
8005 chipio_remap_stream(codec, &stream_remap_data[1]);
8006
8007 ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
8008 ca0113_mmio_command_set(codec, 0x48, 0x0d, 0x40);
8009 ca0113_mmio_command_set(codec, 0x48, 0x17, 0x00);
8010 ca0113_mmio_command_set(codec, 0x48, 0x19, 0x00);
8011 ca0113_mmio_command_set(codec, 0x48, 0x11, 0xff);
8012 ca0113_mmio_command_set(codec, 0x48, 0x12, 0xff);
8013 ca0113_mmio_command_set(codec, 0x48, 0x13, 0xff);
8014 ca0113_mmio_command_set(codec, 0x48, 0x14, 0x7f);
8015 }
8016
8017 static void ae7_post_dsp_asi_stream_setup(struct hda_codec *codec)
8018 {
8019 struct ca0132_spec *spec = codec->spec;
8020
8021 guard(mutex)(&spec->chipio_mutex);
8022
8023 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x81);
8024 ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
8025
8026 chipio_set_conn_rate_no_mutex(codec, 0x70, SR_96_000);
8027
8028 chipio_set_stream_source_dest(codec, 0x05, 0x43, 0x00);
8029 chipio_set_stream_source_dest(codec, 0x18, 0x09, 0xd0);
8030
8031 chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
8032 chipio_set_stream_channels(codec, 0x18, 6);
8033 chipio_set_stream_control(codec, 0x18, 1);
8034
8035 chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 4);
8036 }
8037
8038 static void ae7_post_dsp_pll_setup(struct hda_codec *codec)
8039 {
8040 static const unsigned int addr[] = {
8041 0x41, 0x45, 0x40, 0x43, 0x51
8042 };
8043 static const unsigned int data[] = {
8044 0xc8, 0xcc, 0xcb, 0xc7, 0x8d
8045 };
8046 unsigned int i;
8047
8048 for (i = 0; i < ARRAY_SIZE(addr); i++)
8049 chipio_8051_write_pll_pmu_no_mutex(codec, addr[i], data[i]);
8050 }
8051
8052 static void ae7_post_dsp_asi_setup_ports(struct hda_codec *codec)
8053 {
8054 struct ca0132_spec *spec = codec->spec;
8055 static const unsigned int target[] = {
8056 0x0b, 0x04, 0x06, 0x0a, 0x0c, 0x11, 0x12, 0x13, 0x14
8057 };
8058 static const unsigned int data[] = {
8059 0x12, 0x00, 0x48, 0x05, 0x5f, 0xff, 0xff, 0xff, 0x7f
8060 };
8061 unsigned int i;
8062
8063 guard(mutex)(&spec->chipio_mutex);
8064
8065 chipio_8051_write_pll_pmu_no_mutex(codec, 0x43, 0xc7);
8066
8067 chipio_write_no_mutex(codec, 0x189000, 0x0001f101);
8068 chipio_write_no_mutex(codec, 0x189004, 0x0001f101);
8069 chipio_write_no_mutex(codec, 0x189024, 0x00014004);
8070 chipio_write_no_mutex(codec, 0x189028, 0x0002000f);
8071
8072 ae7_post_dsp_pll_setup(codec);
8073 chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
8074
8075 for (i = 0; i < ARRAY_SIZE(target); i++)
8076 ca0113_mmio_command_set(codec, 0x48, target[i], data[i]);
8077
8078 ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
8079 ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
8080 ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
8081
8082 chipio_set_stream_source_dest(codec, 0x21, 0x64, 0x56);
8083 chipio_set_stream_channels(codec, 0x21, 2);
8084 chipio_set_conn_rate_no_mutex(codec, 0x56, SR_8_000);
8085
8086 chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_NODE_ID, 0x09);
8087 /*
8088 * In the 8051's memory, this param is referred to as 'n2sid', which I
8089 * believe is 'node to streamID'. It seems to be a way to assign a
8090 * stream to a given HDA node.
8091 */
8092 chipio_set_control_param_no_mutex(codec, 0x20, 0x21);
8093
8094 chipio_write_no_mutex(codec, 0x18b038, 0x00000088);
8095
8096 /*
8097 * Now, at this point on Windows, an actual stream is setup and
8098 * seemingly sends data to the HDA node 0x09, which is the digital
8099 * audio input node. This is left out here, because obviously I don't
8100 * know what data is being sent. Interestingly, the AE-5 seems to go
8101 * through the motions of getting here and never actually takes this
8102 * step, but the AE-7 does.
8103 */
8104
8105 ca0113_mmio_gpio_set(codec, 0, 1);
8106 ca0113_mmio_gpio_set(codec, 1, 1);
8107
8108 ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
8109 chipio_write_no_mutex(codec, 0x18b03c, 0x00000000);
8110 ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
8111 ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
8112
8113 chipio_set_stream_source_dest(codec, 0x05, 0x43, 0x00);
8114 chipio_set_stream_source_dest(codec, 0x18, 0x09, 0xd0);
8115
8116 chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
8117 chipio_set_stream_channels(codec, 0x18, 6);
8118
8119 /*
8120 * Runs again, this has been repeated a few times, but I'm just
8121 * following what the Windows driver does.
8122 */
8123 ae7_post_dsp_pll_setup(codec);
8124 chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
8125 }
8126
8127 /*
8128 * The Windows driver has commands that seem to setup ASI, which I believe to
8129 * be some sort of audio serial interface. My current speculation is that it's
8130 * related to communicating with the new DAC.
8131 */
8132 static void ae7_post_dsp_asi_setup(struct hda_codec *codec)
8133 {
8134 chipio_8051_write_direct(codec, 0x93, 0x10);
8135
8136 chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
8137
8138 ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
8139 ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
8140
8141 chipio_set_control_param(codec, 3, 3);
8142 chipio_set_control_flag(codec, CONTROL_FLAG_ASI_96KHZ, 1);
8143
8144 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x724, 0x83);
8145 chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
8146 snd_hda_codec_write(codec, 0x17, 0, 0x794, 0x00);
8147
8148 chipio_8051_write_exram(codec, 0xfa92, 0x22);
8149
8150 ae7_post_dsp_pll_setup(codec);
8151 ae7_post_dsp_asi_stream_setup(codec);
8152
8153 chipio_8051_write_pll_pmu(codec, 0x43, 0xc7);
8154
8155 ae7_post_dsp_asi_setup_ports(codec);
8156 }
8157
8158 /*
8159 * Setup default parameters for DSP
8160 */
8161 static void ca0132_setup_defaults(struct hda_codec *codec)
8162 {
8163 struct ca0132_spec *spec = codec->spec;
8164 unsigned int tmp;
8165 int num_fx;
8166 int idx, i;
8167
8168 if (spec->dsp_state != DSP_DOWNLOADED)
8169 return;
8170
8171 /* out, in effects + voicefx */
8172 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8173 for (idx = 0; idx < num_fx; idx++) {
8174 for (i = 0; i <= ca0132_effects[idx].params; i++) {
8175 dspio_set_uint_param(codec, ca0132_effects[idx].mid,
8176 ca0132_effects[idx].reqs[i],
8177 ca0132_effects[idx].def_vals[i]);
8178 }
8179 }
8180
8181 /*remove DSP headroom*/
8182 tmp = FLOAT_ZERO;
8183 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8184
8185 /*set speaker EQ bypass attenuation*/
8186 dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
8187
8188 /* set AMic1 and AMic2 as mono mic */
8189 tmp = FLOAT_ONE;
8190 dspio_set_uint_param(codec, 0x80, 0x00, tmp);
8191 dspio_set_uint_param(codec, 0x80, 0x01, tmp);
8192
8193 /* set AMic1 as CrystalVoice input */
8194 tmp = FLOAT_ONE;
8195 dspio_set_uint_param(codec, 0x80, 0x05, tmp);
8196
8197 /* set WUH source */
8198 tmp = FLOAT_TWO;
8199 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8200 }
8201
8202 /*
8203 * Setup default parameters for Recon3D/Recon3Di DSP.
8204 */
8205
8206 static void r3d_setup_defaults(struct hda_codec *codec)
8207 {
8208 struct ca0132_spec *spec = codec->spec;
8209 unsigned int tmp;
8210 int num_fx;
8211 int idx, i;
8212
8213 if (spec->dsp_state != DSP_DOWNLOADED)
8214 return;
8215
8216 ca0132_alt_init_analog_mics(codec);
8217 ca0132_alt_start_dsp_audio_streams(codec);
8218
8219 /*remove DSP headroom*/
8220 tmp = FLOAT_ZERO;
8221 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8222
8223 /* set WUH source */
8224 tmp = FLOAT_TWO;
8225 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8226 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8227
8228 /* Set speaker source? */
8229 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8230
8231 if (ca0132_quirk(spec) == QUIRK_R3DI)
8232 r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADED);
8233
8234 /* Disable mute on Center/LFE. */
8235 if (ca0132_quirk(spec) == QUIRK_R3D) {
8236 ca0113_mmio_gpio_set(codec, 2, false);
8237 ca0113_mmio_gpio_set(codec, 4, true);
8238 }
8239
8240 /* Setup effect defaults */
8241 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8242 for (idx = 0; idx < num_fx; idx++) {
8243 for (i = 0; i <= ca0132_effects[idx].params; i++) {
8244 dspio_set_uint_param(codec,
8245 ca0132_effects[idx].mid,
8246 ca0132_effects[idx].reqs[i],
8247 ca0132_effects[idx].def_vals[i]);
8248 }
8249 }
8250 }
8251
8252 /*
8253 * Setup default parameters for the Sound Blaster Z DSP. A lot more going on
8254 * than the Chromebook setup.
8255 */
8256 static void sbz_setup_defaults(struct hda_codec *codec)
8257 {
8258 struct ca0132_spec *spec = codec->spec;
8259 unsigned int tmp;
8260 int num_fx;
8261 int idx, i;
8262
8263 if (spec->dsp_state != DSP_DOWNLOADED)
8264 return;
8265
8266 ca0132_alt_init_analog_mics(codec);
8267 ca0132_alt_start_dsp_audio_streams(codec);
8268 sbz_connect_streams(codec);
8269 sbz_chipio_startup_data(codec);
8270
8271 /*
8272 * Sets internal input loopback to off, used to have a switch to
8273 * enable input loopback, but turned out to be way too buggy.
8274 */
8275 tmp = FLOAT_ONE;
8276 dspio_set_uint_param(codec, 0x37, 0x08, tmp);
8277 dspio_set_uint_param(codec, 0x37, 0x10, tmp);
8278
8279 /*remove DSP headroom*/
8280 tmp = FLOAT_ZERO;
8281 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8282
8283 /* set WUH source */
8284 tmp = FLOAT_TWO;
8285 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8286 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8287
8288 /* Set speaker source? */
8289 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8290
8291 ca0132_alt_dsp_initial_mic_setup(codec);
8292
8293 /* out, in effects + voicefx */
8294 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8295 for (idx = 0; idx < num_fx; idx++) {
8296 for (i = 0; i <= ca0132_effects[idx].params; i++) {
8297 dspio_set_uint_param(codec,
8298 ca0132_effects[idx].mid,
8299 ca0132_effects[idx].reqs[i],
8300 ca0132_effects[idx].def_vals[i]);
8301 }
8302 }
8303
8304 ca0132_alt_init_speaker_tuning(codec);
8305 }
8306
8307 /*
8308 * Setup default parameters for the Sound BlasterX AE-5 DSP.
8309 */
8310 static void ae5_setup_defaults(struct hda_codec *codec)
8311 {
8312 struct ca0132_spec *spec = codec->spec;
8313 unsigned int tmp;
8314 int num_fx;
8315 int idx, i;
8316
8317 if (spec->dsp_state != DSP_DOWNLOADED)
8318 return;
8319
8320 ca0132_alt_init_analog_mics(codec);
8321 ca0132_alt_start_dsp_audio_streams(codec);
8322
8323 /* New, unknown SCP req's */
8324 tmp = FLOAT_ZERO;
8325 dspio_set_uint_param(codec, 0x96, 0x29, tmp);
8326 dspio_set_uint_param(codec, 0x96, 0x2a, tmp);
8327 dspio_set_uint_param(codec, 0x80, 0x0d, tmp);
8328 dspio_set_uint_param(codec, 0x80, 0x0e, tmp);
8329
8330 ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
8331 ca0113_mmio_gpio_set(codec, 0, false);
8332 ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
8333
8334 /* Internal loopback off */
8335 tmp = FLOAT_ONE;
8336 dspio_set_uint_param(codec, 0x37, 0x08, tmp);
8337 dspio_set_uint_param(codec, 0x37, 0x10, tmp);
8338
8339 /*remove DSP headroom*/
8340 tmp = FLOAT_ZERO;
8341 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8342
8343 /* set WUH source */
8344 tmp = FLOAT_TWO;
8345 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8346 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8347
8348 /* Set speaker source? */
8349 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8350
8351 ca0132_alt_dsp_initial_mic_setup(codec);
8352 ae5_post_dsp_register_set(codec);
8353 ae5_post_dsp_param_setup(codec);
8354 ae5_post_dsp_pll_setup(codec);
8355 ae5_post_dsp_stream_setup(codec);
8356 ae5_post_dsp_startup_data(codec);
8357
8358 /* out, in effects + voicefx */
8359 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8360 for (idx = 0; idx < num_fx; idx++) {
8361 for (i = 0; i <= ca0132_effects[idx].params; i++) {
8362 dspio_set_uint_param(codec,
8363 ca0132_effects[idx].mid,
8364 ca0132_effects[idx].reqs[i],
8365 ca0132_effects[idx].def_vals[i]);
8366 }
8367 }
8368
8369 ca0132_alt_init_speaker_tuning(codec);
8370 }
8371
8372 /*
8373 * Setup default parameters for the Sound Blaster AE-7 DSP.
8374 */
8375 static void ae7_setup_defaults(struct hda_codec *codec)
8376 {
8377 struct ca0132_spec *spec = codec->spec;
8378 unsigned int tmp;
8379 int num_fx;
8380 int idx, i;
8381
8382 if (spec->dsp_state != DSP_DOWNLOADED)
8383 return;
8384
8385 ca0132_alt_init_analog_mics(codec);
8386 ca0132_alt_start_dsp_audio_streams(codec);
8387 ae7_post_dsp_setup_ports(codec);
8388
8389 tmp = FLOAT_ZERO;
8390 dspio_set_uint_param(codec, 0x96,
8391 SPEAKER_TUNING_FRONT_LEFT_INVERT, tmp);
8392 dspio_set_uint_param(codec, 0x96,
8393 SPEAKER_TUNING_FRONT_RIGHT_INVERT, tmp);
8394
8395 ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
8396
8397 /* New, unknown SCP req's */
8398 dspio_set_uint_param(codec, 0x80, 0x0d, tmp);
8399 dspio_set_uint_param(codec, 0x80, 0x0e, tmp);
8400
8401 ca0113_mmio_gpio_set(codec, 0, false);
8402
8403 /* Internal loopback off */
8404 tmp = FLOAT_ONE;
8405 dspio_set_uint_param(codec, 0x37, 0x08, tmp);
8406 dspio_set_uint_param(codec, 0x37, 0x10, tmp);
8407
8408 /*remove DSP headroom*/
8409 tmp = FLOAT_ZERO;
8410 dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
8411
8412 /* set WUH source */
8413 tmp = FLOAT_TWO;
8414 dspio_set_uint_param(codec, 0x31, 0x00, tmp);
8415 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8416
8417 /* Set speaker source? */
8418 dspio_set_uint_param(codec, 0x32, 0x00, tmp);
8419 ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
8420
8421 /*
8422 * This is the second time we've called this, but this is seemingly
8423 * what Windows does.
8424 */
8425 ca0132_alt_init_analog_mics(codec);
8426
8427 ae7_post_dsp_asi_setup(codec);
8428
8429 /*
8430 * Not sure why, but these are both set to 1. They're only set to 0
8431 * upon shutdown.
8432 */
8433 ca0113_mmio_gpio_set(codec, 0, true);
8434 ca0113_mmio_gpio_set(codec, 1, true);
8435
8436 /* Volume control related. */
8437 ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x04);
8438 ca0113_mmio_command_set(codec, 0x48, 0x10, 0x04);
8439 ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x80);
8440
8441 /* out, in effects + voicefx */
8442 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
8443 for (idx = 0; idx < num_fx; idx++) {
8444 for (i = 0; i <= ca0132_effects[idx].params; i++) {
8445 dspio_set_uint_param(codec,
8446 ca0132_effects[idx].mid,
8447 ca0132_effects[idx].reqs[i],
8448 ca0132_effects[idx].def_vals[i]);
8449 }
8450 }
8451
8452 ca0132_alt_init_speaker_tuning(codec);
8453 }
8454
8455 /*
8456 * Initialization of flags in chip
8457 */
8458 static void ca0132_init_flags(struct hda_codec *codec)
8459 {
8460 struct ca0132_spec *spec = codec->spec;
8461
8462 if (ca0132_use_alt_functions(spec)) {
8463 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, 1);
8464 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, 1);
8465 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, 1);
8466 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, 1);
8467 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, 1);
8468 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
8469 chipio_set_control_flag(codec, CONTROL_FLAG_SPDIF2OUT, 0);
8470 chipio_set_control_flag(codec,
8471 CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
8472 chipio_set_control_flag(codec,
8473 CONTROL_FLAG_PORT_A_10KOHM_LOAD, 1);
8474 } else {
8475 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
8476 chipio_set_control_flag(codec,
8477 CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
8478 chipio_set_control_flag(codec,
8479 CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
8480 chipio_set_control_flag(codec,
8481 CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
8482 chipio_set_control_flag(codec,
8483 CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
8484 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
8485 }
8486 }
8487
8488 /*
8489 * Initialization of parameters in chip
8490 */
8491 static void ca0132_init_params(struct hda_codec *codec)
8492 {
8493 struct ca0132_spec *spec = codec->spec;
8494
8495 if (ca0132_use_alt_functions(spec)) {
8496 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8497 chipio_set_conn_rate(codec, 0x0B, SR_48_000);
8498 chipio_set_control_param(codec, CONTROL_PARAM_SPDIF1_SOURCE, 0);
8499 chipio_set_control_param(codec, 0, 0);
8500 chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
8501 }
8502
8503 chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
8504 chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
8505 }
8506
8507 static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
8508 {
8509 chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
8510 chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
8511 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
8512 chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
8513 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
8514 chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
8515
8516 chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
8517 chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
8518 chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
8519 }
8520
8521 static bool ca0132_download_dsp_images(struct hda_codec *codec)
8522 {
8523 bool dsp_loaded = false;
8524 struct ca0132_spec *spec = codec->spec;
8525 const struct dsp_image_seg *dsp_os_image;
8526 const struct firmware *fw_entry = NULL;
8527 /*
8528 * Alternate firmwares for different variants. The Recon3Di apparently
8529 * can use the default firmware, but I'll leave the option in case
8530 * it needs it again.
8531 */
8532 switch (ca0132_quirk(spec)) {
8533 case QUIRK_SBZ:
8534 case QUIRK_R3D:
8535 case QUIRK_AE5:
8536 if (request_firmware(&fw_entry, DESKTOP_EFX_FILE,
8537 codec->card->dev) != 0)
8538 codec_dbg(codec, "Desktop firmware not found.");
8539 else
8540 codec_dbg(codec, "Desktop firmware selected.");
8541 break;
8542 case QUIRK_R3DI:
8543 if (request_firmware(&fw_entry, R3DI_EFX_FILE,
8544 codec->card->dev) != 0)
8545 codec_dbg(codec, "Recon3Di alt firmware not detected.");
8546 else
8547 codec_dbg(codec, "Recon3Di firmware selected.");
8548 break;
8549 default:
8550 break;
8551 }
8552 /*
8553 * Use default ctefx.bin if no alt firmware is detected, or if none
8554 * exists for your particular codec.
8555 */
8556 if (!fw_entry) {
8557 codec_dbg(codec, "Default firmware selected.");
8558 if (request_firmware(&fw_entry, EFX_FILE,
8559 codec->card->dev) != 0)
8560 return false;
8561 }
8562
8563 dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
8564 if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
8565 codec_err(codec, "ca0132 DSP load image failed\n");
8566 goto exit_download;
8567 }
8568
8569 dsp_loaded = dspload_wait_loaded(codec);
8570
8571 exit_download:
8572 release_firmware(fw_entry);
8573
8574 return dsp_loaded;
8575 }
8576
8577 static void ca0132_download_dsp(struct hda_codec *codec)
8578 {
8579 struct ca0132_spec *spec = codec->spec;
8580
8581 #ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
8582 return; /* NOP */
8583 #endif
8584
8585 if (spec->dsp_state == DSP_DOWNLOAD_FAILED)
8586 return; /* don't retry failures */
8587
8588 chipio_enable_clocks(codec);
8589 if (spec->dsp_state != DSP_DOWNLOADED) {
8590 spec->dsp_state = DSP_DOWNLOADING;
8591
8592 if (!ca0132_download_dsp_images(codec))
8593 spec->dsp_state = DSP_DOWNLOAD_FAILED;
8594 else
8595 spec->dsp_state = DSP_DOWNLOADED;
8596 }
8597
8598 /* For codecs using alt functions, this is already done earlier */
8599 if (spec->dsp_state == DSP_DOWNLOADED && !ca0132_use_alt_functions(spec))
8600 ca0132_set_dsp_msr(codec, true);
8601 }
8602
8603 static void ca0132_process_dsp_response(struct hda_codec *codec,
8604 struct hda_jack_callback *callback)
8605 {
8606 struct ca0132_spec *spec = codec->spec;
8607
8608 codec_dbg(codec, "ca0132_process_dsp_response\n");
8609 CLASS(snd_hda_power_pm, pm)(codec);
8610 if (spec->wait_scp) {
8611 if (dspio_get_response_data(codec) >= 0)
8612 spec->wait_scp = 0;
8613 }
8614
8615 dspio_clear_response_queue(codec);
8616 }
8617
8618 static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
8619 {
8620 struct ca0132_spec *spec = codec->spec;
8621 struct hda_jack_tbl *tbl;
8622
8623 /* Delay enabling the HP amp, to let the mic-detection
8624 * state machine run.
8625 */
8626 tbl = snd_hda_jack_tbl_get(codec, cb->nid);
8627 if (tbl)
8628 tbl->block_report = 1;
8629 schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(500));
8630 }
8631
8632 static void amic_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
8633 {
8634 struct ca0132_spec *spec = codec->spec;
8635
8636 if (ca0132_use_alt_functions(spec))
8637 ca0132_alt_select_in(codec);
8638 else
8639 ca0132_select_mic(codec);
8640 }
8641
8642 static void ca0132_setup_unsol(struct hda_codec *codec)
8643 {
8644 struct ca0132_spec *spec = codec->spec;
8645 snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_hp, hp_callback);
8646 snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_amic1,
8647 amic_callback);
8648 snd_hda_jack_detect_enable_callback(codec, UNSOL_TAG_DSP,
8649 ca0132_process_dsp_response);
8650 /* Front headphone jack detection */
8651 if (ca0132_use_alt_functions(spec))
8652 snd_hda_jack_detect_enable_callback(codec,
8653 spec->unsol_tag_front_hp, hp_callback);
8654 }
8655
8656 /*
8657 * Verbs tables.
8658 */
8659
8660 /* Sends before DSP download. */
8661 static const struct hda_verb ca0132_base_init_verbs[] = {
8662 /*enable ct extension*/
8663 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
8664 {}
8665 };
8666
8667 /* Send at exit. */
8668 static const struct hda_verb ca0132_base_exit_verbs[] = {
8669 /*set afg to D3*/
8670 {0x01, AC_VERB_SET_POWER_STATE, 0x03},
8671 /*disable ct extension*/
8672 {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
8673 {}
8674 };
8675
8676 /* Other verbs tables. Sends after DSP download. */
8677
8678 static const struct hda_verb ca0132_init_verbs0[] = {
8679 /* chip init verbs */
8680 {0x15, 0x70D, 0xF0},
8681 {0x15, 0x70E, 0xFE},
8682 {0x15, 0x707, 0x75},
8683 {0x15, 0x707, 0xD3},
8684 {0x15, 0x707, 0x09},
8685 {0x15, 0x707, 0x53},
8686 {0x15, 0x707, 0xD4},
8687 {0x15, 0x707, 0xEF},
8688 {0x15, 0x707, 0x75},
8689 {0x15, 0x707, 0xD3},
8690 {0x15, 0x707, 0x09},
8691 {0x15, 0x707, 0x02},
8692 {0x15, 0x707, 0x37},
8693 {0x15, 0x707, 0x78},
8694 {0x15, 0x53C, 0xCE},
8695 {0x15, 0x575, 0xC9},
8696 {0x15, 0x53D, 0xCE},
8697 {0x15, 0x5B7, 0xC9},
8698 {0x15, 0x70D, 0xE8},
8699 {0x15, 0x70E, 0xFE},
8700 {0x15, 0x707, 0x02},
8701 {0x15, 0x707, 0x68},
8702 {0x15, 0x707, 0x62},
8703 {0x15, 0x53A, 0xCE},
8704 {0x15, 0x546, 0xC9},
8705 {0x15, 0x53B, 0xCE},
8706 {0x15, 0x5E8, 0xC9},
8707 {}
8708 };
8709
8710 /* Extra init verbs for desktop cards. */
8711 static const struct hda_verb ca0132_init_verbs1[] = {
8712 {0x15, 0x70D, 0x20},
8713 {0x15, 0x70E, 0x19},
8714 {0x15, 0x707, 0x00},
8715 {0x15, 0x539, 0xCE},
8716 {0x15, 0x546, 0xC9},
8717 {0x15, 0x70D, 0xB7},
8718 {0x15, 0x70E, 0x09},
8719 {0x15, 0x707, 0x10},
8720 {0x15, 0x70D, 0xAF},
8721 {0x15, 0x70E, 0x09},
8722 {0x15, 0x707, 0x01},
8723 {0x15, 0x707, 0x05},
8724 {0x15, 0x70D, 0x73},
8725 {0x15, 0x70E, 0x09},
8726 {0x15, 0x707, 0x14},
8727 {0x15, 0x6FF, 0xC4},
8728 {}
8729 };
8730
8731 static void ca0132_init_chip(struct hda_codec *codec)
8732 {
8733 struct ca0132_spec *spec = codec->spec;
8734 int num_fx;
8735 int i;
8736 unsigned int on;
8737
8738 mutex_init(&spec->chipio_mutex);
8739
8740 /*
8741 * The Windows driver always does this upon startup, which seems to
8742 * clear out any previous configuration. This should help issues where
8743 * a boot into Windows prior to a boot into Linux breaks things. Also,
8744 * Windows always sends the reset twice.
8745 */
8746 if (ca0132_use_alt_functions(spec)) {
8747 chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
8748 chipio_write_no_mutex(codec, 0x18b0a4, 0x000000c2);
8749
8750 snd_hda_codec_write(codec, codec->core.afg, 0,
8751 AC_VERB_SET_CODEC_RESET, 0);
8752 snd_hda_codec_write(codec, codec->core.afg, 0,
8753 AC_VERB_SET_CODEC_RESET, 0);
8754 }
8755
8756 spec->cur_out_type = SPEAKER_OUT;
8757 if (!ca0132_use_alt_functions(spec))
8758 spec->cur_mic_type = DIGITAL_MIC;
8759 else
8760 spec->cur_mic_type = REAR_MIC;
8761
8762 spec->cur_mic_boost = 0;
8763
8764 for (i = 0; i < VNODES_COUNT; i++) {
8765 spec->vnode_lvol[i] = 0x5a;
8766 spec->vnode_rvol[i] = 0x5a;
8767 spec->vnode_lswitch[i] = 0;
8768 spec->vnode_rswitch[i] = 0;
8769 }
8770
8771 /*
8772 * Default states for effects are in ca0132_effects[].
8773 */
8774 num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
8775 for (i = 0; i < num_fx; i++) {
8776 on = (unsigned int)ca0132_effects[i].reqs[0];
8777 spec->effects_switch[i] = on ? 1 : 0;
8778 }
8779 /*
8780 * Sets defaults for the effect slider controls, only for alternative
8781 * ca0132 codecs. Also sets x-bass crossover frequency to 80hz.
8782 */
8783 if (ca0132_use_alt_controls(spec)) {
8784 /* Set speakers to default to full range. */
8785 spec->speaker_range_val[0] = 1;
8786 spec->speaker_range_val[1] = 1;
8787
8788 spec->xbass_xover_freq = 8;
8789 for (i = 0; i < EFFECT_LEVEL_SLIDERS; i++)
8790 spec->fx_ctl_val[i] = effect_slider_defaults[i];
8791
8792 spec->bass_redirect_xover_freq = 8;
8793 }
8794
8795 spec->voicefx_val = 0;
8796 spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
8797 spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
8798
8799 /*
8800 * The ZxR doesn't have a front panel header, and it's line-in is on
8801 * the daughter board. So, there is no input enum control, and we need
8802 * to make sure that spec->in_enum_val is set properly.
8803 */
8804 if (ca0132_quirk(spec) == QUIRK_ZXR)
8805 spec->in_enum_val = REAR_MIC;
8806
8807 #ifdef ENABLE_TUNING_CONTROLS
8808 ca0132_init_tuning_defaults(codec);
8809 #endif
8810 }
8811
8812 /*
8813 * Recon3Di exit specific commands.
8814 */
8815 /* prevents popping noise on shutdown */
8816 static void r3di_gpio_shutdown(struct hda_codec *codec)
8817 {
8818 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 0x00);
8819 }
8820
8821 /*
8822 * Sound Blaster Z exit specific commands.
8823 */
8824 static void sbz_region2_exit(struct hda_codec *codec)
8825 {
8826 struct ca0132_spec *spec = codec->spec;
8827 unsigned int i;
8828
8829 for (i = 0; i < 4; i++)
8830 writeb(0x0, spec->mem_base + 0x100);
8831 for (i = 0; i < 8; i++)
8832 writeb(0xb3, spec->mem_base + 0x304);
8833
8834 ca0113_mmio_gpio_set(codec, 0, false);
8835 ca0113_mmio_gpio_set(codec, 1, false);
8836 ca0113_mmio_gpio_set(codec, 4, true);
8837 ca0113_mmio_gpio_set(codec, 5, false);
8838 ca0113_mmio_gpio_set(codec, 7, false);
8839 }
8840
8841 static void sbz_set_pin_ctl_default(struct hda_codec *codec)
8842 {
8843 static const hda_nid_t pins[] = {0x0B, 0x0C, 0x0E, 0x12, 0x13};
8844 unsigned int i;
8845
8846 snd_hda_codec_write(codec, 0x11, 0,
8847 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
8848
8849 for (i = 0; i < ARRAY_SIZE(pins); i++)
8850 snd_hda_codec_write(codec, pins[i], 0,
8851 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00);
8852 }
8853
8854 static void ca0132_clear_unsolicited(struct hda_codec *codec)
8855 {
8856 static const hda_nid_t pins[] = {0x0B, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13};
8857 unsigned int i;
8858
8859 for (i = 0; i < ARRAY_SIZE(pins); i++) {
8860 snd_hda_codec_write(codec, pins[i], 0,
8861 AC_VERB_SET_UNSOLICITED_ENABLE, 0x00);
8862 }
8863 }
8864
8865 /* On shutdown, sends commands in sets of three */
8866 static void sbz_gpio_shutdown_commands(struct hda_codec *codec, int dir,
8867 int mask, int data)
8868 {
8869 if (dir >= 0)
8870 snd_hda_codec_write(codec, 0x01, 0,
8871 AC_VERB_SET_GPIO_DIRECTION, dir);
8872 if (mask >= 0)
8873 snd_hda_codec_write(codec, 0x01, 0,
8874 AC_VERB_SET_GPIO_MASK, mask);
8875
8876 if (data >= 0)
8877 snd_hda_codec_write(codec, 0x01, 0,
8878 AC_VERB_SET_GPIO_DATA, data);
8879 }
8880
8881 static void zxr_dbpro_power_state_shutdown(struct hda_codec *codec)
8882 {
8883 static const hda_nid_t pins[] = {0x05, 0x0c, 0x09, 0x0e, 0x08, 0x11, 0x01};
8884 unsigned int i;
8885
8886 for (i = 0; i < ARRAY_SIZE(pins); i++)
8887 snd_hda_codec_write(codec, pins[i], 0,
8888 AC_VERB_SET_POWER_STATE, 0x03);
8889 }
8890
8891 static void sbz_exit_chip(struct hda_codec *codec)
8892 {
8893 chipio_set_stream_control(codec, 0x03, 0);
8894 chipio_set_stream_control(codec, 0x04, 0);
8895
8896 /* Mess with GPIO */
8897 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, -1);
8898 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x05);
8899 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x01);
8900
8901 chipio_set_stream_control(codec, 0x14, 0);
8902 chipio_set_stream_control(codec, 0x0C, 0);
8903
8904 chipio_set_conn_rate(codec, 0x41, SR_192_000);
8905 chipio_set_conn_rate(codec, 0x91, SR_192_000);
8906
8907 chipio_write(codec, 0x18a020, 0x00000083);
8908
8909 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x03);
8910 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x07);
8911 sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x06);
8912
8913 chipio_set_stream_control(codec, 0x0C, 0);
8914
8915 chipio_set_control_param(codec, 0x0D, 0x24);
8916
8917 ca0132_clear_unsolicited(codec);
8918 sbz_set_pin_ctl_default(codec);
8919
8920 snd_hda_codec_write(codec, 0x0B, 0,
8921 AC_VERB_SET_EAPD_BTLENABLE, 0x00);
8922
8923 sbz_region2_exit(codec);
8924 }
8925
8926 static void r3d_exit_chip(struct hda_codec *codec)
8927 {
8928 ca0132_clear_unsolicited(codec);
8929 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
8930 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5b);
8931 }
8932
8933 static void ae5_exit_chip(struct hda_codec *codec)
8934 {
8935 chipio_set_stream_control(codec, 0x03, 0);
8936 chipio_set_stream_control(codec, 0x04, 0);
8937
8938 ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
8939 ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
8940 ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
8941 ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
8942 ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
8943 ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x00);
8944 ca0113_mmio_gpio_set(codec, 0, false);
8945 ca0113_mmio_gpio_set(codec, 1, false);
8946
8947 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
8948 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
8949
8950 chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
8951
8952 chipio_set_stream_control(codec, 0x18, 0);
8953 chipio_set_stream_control(codec, 0x0c, 0);
8954
8955 snd_hda_codec_write(codec, 0x01, 0, 0x724, 0x83);
8956 }
8957
8958 static void ae7_exit_chip(struct hda_codec *codec)
8959 {
8960 chipio_set_stream_control(codec, 0x18, 0);
8961 chipio_set_stream_source_dest(codec, 0x21, 0xc8, 0xc8);
8962 chipio_set_stream_channels(codec, 0x21, 0);
8963 chipio_set_control_param(codec, CONTROL_PARAM_NODE_ID, 0x09);
8964 chipio_set_control_param(codec, 0x20, 0x01);
8965
8966 chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
8967
8968 chipio_set_stream_control(codec, 0x18, 0);
8969 chipio_set_stream_control(codec, 0x0c, 0);
8970
8971 ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
8972 snd_hda_codec_write(codec, 0x15, 0, 0x724, 0x83);
8973 ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
8974 ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
8975 ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x00);
8976 ca0113_mmio_gpio_set(codec, 0, false);
8977 ca0113_mmio_gpio_set(codec, 1, false);
8978 ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
8979
8980 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
8981 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
8982 }
8983
8984 static void zxr_exit_chip(struct hda_codec *codec)
8985 {
8986 chipio_set_stream_control(codec, 0x03, 0);
8987 chipio_set_stream_control(codec, 0x04, 0);
8988 chipio_set_stream_control(codec, 0x14, 0);
8989 chipio_set_stream_control(codec, 0x0C, 0);
8990
8991 chipio_set_conn_rate(codec, 0x41, SR_192_000);
8992 chipio_set_conn_rate(codec, 0x91, SR_192_000);
8993
8994 chipio_write(codec, 0x18a020, 0x00000083);
8995
8996 snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
8997 snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
8998
8999 ca0132_clear_unsolicited(codec);
9000 sbz_set_pin_ctl_default(codec);
9001 snd_hda_codec_write(codec, 0x0B, 0, AC_VERB_SET_EAPD_BTLENABLE, 0x00);
9002
9003 ca0113_mmio_gpio_set(codec, 5, false);
9004 ca0113_mmio_gpio_set(codec, 2, false);
9005 ca0113_mmio_gpio_set(codec, 3, false);
9006 ca0113_mmio_gpio_set(codec, 0, false);
9007 ca0113_mmio_gpio_set(codec, 4, true);
9008 ca0113_mmio_gpio_set(codec, 0, true);
9009 ca0113_mmio_gpio_set(codec, 5, true);
9010 ca0113_mmio_gpio_set(codec, 2, false);
9011 ca0113_mmio_gpio_set(codec, 3, false);
9012 }
9013
9014 static void ca0132_exit_chip(struct hda_codec *codec)
9015 {
9016 /* put any chip cleanup stuffs here. */
9017
9018 if (dspload_is_loaded(codec))
9019 dsp_reset(codec);
9020 }
9021
9022 /*
9023 * This fixes a problem that was hard to reproduce. Very rarely, I would
9024 * boot up, and there would be no sound, but the DSP indicated it had loaded
9025 * properly. I did a few memory dumps to see if anything was different, and
9026 * there were a few areas of memory uninitialized with a1a2a3a4. This function
9027 * checks if those areas are uninitialized, and if they are, it'll attempt to
9028 * reload the card 3 times. Usually it fixes by the second.
9029 */
9030 static void sbz_dsp_startup_check(struct hda_codec *codec)
9031 {
9032 struct ca0132_spec *spec = codec->spec;
9033 unsigned int dsp_data_check[4];
9034 unsigned int cur_address = 0x390;
9035 unsigned int i;
9036 unsigned int failure = 0;
9037 unsigned int reload = 3;
9038
9039 if (spec->startup_check_entered)
9040 return;
9041
9042 spec->startup_check_entered = true;
9043
9044 for (i = 0; i < 4; i++) {
9045 chipio_read(codec, cur_address, &dsp_data_check[i]);
9046 cur_address += 0x4;
9047 }
9048 for (i = 0; i < 4; i++) {
9049 if (dsp_data_check[i] == 0xa1a2a3a4)
9050 failure = 1;
9051 }
9052
9053 codec_dbg(codec, "Startup Check: %d ", failure);
9054 if (failure)
9055 codec_info(codec, "DSP not initialized properly. Attempting to fix.");
9056 /*
9057 * While the failure condition is true, and we haven't reached our
9058 * three reload limit, continue trying to reload the driver and
9059 * fix the issue.
9060 */
9061 while (failure && (reload != 0)) {
9062 codec_info(codec, "Reloading... Tries left: %d", reload);
9063 sbz_exit_chip(codec);
9064 spec->dsp_state = DSP_DOWNLOAD_INIT;
9065 snd_hda_codec_init(codec);
9066 failure = 0;
9067 for (i = 0; i < 4; i++) {
9068 chipio_read(codec, cur_address, &dsp_data_check[i]);
9069 cur_address += 0x4;
9070 }
9071 for (i = 0; i < 4; i++) {
9072 if (dsp_data_check[i] == 0xa1a2a3a4)
9073 failure = 1;
9074 }
9075 reload--;
9076 }
9077
9078 if (!failure && reload < 3)
9079 codec_info(codec, "DSP fixed.");
9080
9081 if (!failure)
9082 return;
9083
9084 codec_info(codec, "DSP failed to initialize properly. Either try a full shutdown or a suspend to clear the internal memory.");
9085 }
9086
9087 /*
9088 * This is for the extra volume verbs 0x797 (left) and 0x798 (right). These add
9089 * extra precision for decibel values. If you had the dB value in floating point
9090 * you would take the value after the decimal point, multiply by 64, and divide
9091 * by 2. So for 8.59, it's (59 * 64) / 100. Useful if someone wanted to
9092 * implement fixed point or floating point dB volumes. For now, I'll set them
9093 * to 0 just incase a value has lingered from a boot into Windows.
9094 */
9095 static void ca0132_alt_vol_setup(struct hda_codec *codec)
9096 {
9097 snd_hda_codec_write(codec, 0x02, 0, 0x797, 0x00);
9098 snd_hda_codec_write(codec, 0x02, 0, 0x798, 0x00);
9099 snd_hda_codec_write(codec, 0x03, 0, 0x797, 0x00);
9100 snd_hda_codec_write(codec, 0x03, 0, 0x798, 0x00);
9101 snd_hda_codec_write(codec, 0x04, 0, 0x797, 0x00);
9102 snd_hda_codec_write(codec, 0x04, 0, 0x798, 0x00);
9103 snd_hda_codec_write(codec, 0x07, 0, 0x797, 0x00);
9104 snd_hda_codec_write(codec, 0x07, 0, 0x798, 0x00);
9105 }
9106
9107 /*
9108 * Extra commands that don't really fit anywhere else.
9109 */
9110 static void sbz_pre_dsp_setup(struct hda_codec *codec)
9111 {
9112 struct ca0132_spec *spec = codec->spec;
9113
9114 writel(0x00820680, spec->mem_base + 0x01C);
9115 writel(0x00820680, spec->mem_base + 0x01C);
9116
9117 chipio_write(codec, 0x18b0a4, 0x000000c2);
9118
9119 snd_hda_codec_write(codec, 0x11, 0,
9120 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
9121 }
9122
9123 static void r3d_pre_dsp_setup(struct hda_codec *codec)
9124 {
9125 chipio_write(codec, 0x18b0a4, 0x000000c2);
9126
9127 chipio_8051_write_exram(codec, 0x1c1e, 0x5b);
9128
9129 snd_hda_codec_write(codec, 0x11, 0,
9130 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
9131 }
9132
9133 static void r3di_pre_dsp_setup(struct hda_codec *codec)
9134 {
9135 chipio_write(codec, 0x18b0a4, 0x000000c2);
9136
9137 chipio_8051_write_exram(codec, 0x1c1e, 0x5b);
9138 chipio_8051_write_exram(codec, 0x1920, 0x00);
9139 chipio_8051_write_exram(codec, 0x1921, 0x40);
9140
9141 snd_hda_codec_write(codec, 0x11, 0,
9142 AC_VERB_SET_PIN_WIDGET_CONTROL, 0x04);
9143 }
9144
9145 /*
9146 * The ZxR seems to use alternative DAC's for the surround channels, which
9147 * require PLL PMU setup for the clock rate, I'm guessing. Without setting
9148 * this up, we get no audio out of the surround jacks.
9149 */
9150 static void zxr_pre_dsp_setup(struct hda_codec *codec)
9151 {
9152 static const unsigned int addr[] = { 0x43, 0x40, 0x41, 0x42, 0x45 };
9153 static const unsigned int data[] = { 0x08, 0x0c, 0x0b, 0x07, 0x0d };
9154 unsigned int i;
9155
9156 chipio_write(codec, 0x189000, 0x0001f100);
9157 msleep(50);
9158 chipio_write(codec, 0x18900c, 0x0001f100);
9159 msleep(50);
9160
9161 /*
9162 * This writes a RET instruction at the entry point of the function at
9163 * 0xfa92 in exram. This function seems to have something to do with
9164 * ASI. Might be some way to prevent the card from reconfiguring the
9165 * ASI stuff itself.
9166 */
9167 chipio_8051_write_exram(codec, 0xfa92, 0x22);
9168
9169 chipio_8051_write_pll_pmu(codec, 0x51, 0x98);
9170
9171 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x82);
9172 chipio_set_control_param(codec, CONTROL_PARAM_ASI, 3);
9173
9174 chipio_write(codec, 0x18902c, 0x00000000);
9175 msleep(50);
9176 chipio_write(codec, 0x18902c, 0x00000003);
9177 msleep(50);
9178
9179 for (i = 0; i < ARRAY_SIZE(addr); i++)
9180 chipio_8051_write_pll_pmu(codec, addr[i], data[i]);
9181 }
9182
9183 /*
9184 * These are sent before the DSP is downloaded. Not sure
9185 * what they do, or if they're necessary. Could possibly
9186 * be removed. Figure they're better to leave in.
9187 */
9188 static const unsigned int ca0113_mmio_init_address_sbz[] = {
9189 0x400, 0x408, 0x40c, 0x01c, 0xc0c, 0xc00, 0xc04, 0xc0c, 0xc0c, 0xc0c,
9190 0xc0c, 0xc08, 0xc08, 0xc08, 0xc08, 0xc08, 0xc04
9191 };
9192
9193 static const unsigned int ca0113_mmio_init_data_sbz[] = {
9194 0x00000030, 0x00000000, 0x00000003, 0x00000003, 0x00000003,
9195 0x00000003, 0x000000c1, 0x000000f1, 0x00000001, 0x000000c7,
9196 0x000000c1, 0x00000080
9197 };
9198
9199 static const unsigned int ca0113_mmio_init_data_zxr[] = {
9200 0x00000030, 0x00000000, 0x00000000, 0x00000003, 0x00000003,
9201 0x00000003, 0x00000001, 0x000000f1, 0x00000001, 0x000000c7,
9202 0x000000c1, 0x00000080
9203 };
9204
9205 static const unsigned int ca0113_mmio_init_address_ae5[] = {
9206 0x400, 0x42c, 0x46c, 0x4ac, 0x4ec, 0x43c, 0x47c, 0x4bc, 0x4fc, 0x408,
9207 0x100, 0x410, 0x40c, 0x100, 0x100, 0x830, 0x86c, 0x800, 0x86c, 0x800,
9208 0x804, 0x20c, 0x01c, 0xc0c, 0xc00, 0xc04, 0xc0c, 0xc0c, 0xc0c, 0xc0c,
9209 0xc08, 0xc08, 0xc08, 0xc08, 0xc08, 0xc04, 0x01c
9210 };
9211
9212 static const unsigned int ca0113_mmio_init_data_ae5[] = {
9213 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
9214 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
9215 0x00000600, 0x00000014, 0x00000001, 0x0000060f, 0x0000070f,
9216 0x00000aff, 0x00000000, 0x0000006b, 0x00000001, 0x0000006b,
9217 0x00000057, 0x00800000, 0x00880680, 0x00000080, 0x00000030,
9218 0x00000000, 0x00000000, 0x00000003, 0x00000003, 0x00000003,
9219 0x00000001, 0x000000f1, 0x00000001, 0x000000c7, 0x000000c1,
9220 0x00000080, 0x00880680
9221 };
9222
9223 static void ca0132_mmio_init_sbz(struct hda_codec *codec)
9224 {
9225 struct ca0132_spec *spec = codec->spec;
9226 unsigned int tmp[2], i, count, cur_addr;
9227 const unsigned int *addr, *data;
9228
9229 addr = ca0113_mmio_init_address_sbz;
9230 for (i = 0; i < 3; i++)
9231 writel(0x00000000, spec->mem_base + addr[i]);
9232
9233 cur_addr = i;
9234 switch (ca0132_quirk(spec)) {
9235 case QUIRK_ZXR:
9236 tmp[0] = 0x00880480;
9237 tmp[1] = 0x00000080;
9238 break;
9239 case QUIRK_SBZ:
9240 tmp[0] = 0x00820680;
9241 tmp[1] = 0x00000083;
9242 break;
9243 case QUIRK_R3D:
9244 tmp[0] = 0x00880680;
9245 tmp[1] = 0x00000083;
9246 break;
9247 default:
9248 tmp[0] = 0x00000000;
9249 tmp[1] = 0x00000000;
9250 break;
9251 }
9252
9253 for (i = 0; i < 2; i++)
9254 writel(tmp[i], spec->mem_base + addr[cur_addr + i]);
9255
9256 cur_addr += i;
9257
9258 switch (ca0132_quirk(spec)) {
9259 case QUIRK_ZXR:
9260 count = ARRAY_SIZE(ca0113_mmio_init_data_zxr);
9261 data = ca0113_mmio_init_data_zxr;
9262 break;
9263 default:
9264 count = ARRAY_SIZE(ca0113_mmio_init_data_sbz);
9265 data = ca0113_mmio_init_data_sbz;
9266 break;
9267 }
9268
9269 for (i = 0; i < count; i++)
9270 writel(data[i], spec->mem_base + addr[cur_addr + i]);
9271 }
9272
9273 static void ca0132_mmio_init_ae5(struct hda_codec *codec)
9274 {
9275 struct ca0132_spec *spec = codec->spec;
9276 const unsigned int *addr, *data;
9277 unsigned int i, count;
9278
9279 addr = ca0113_mmio_init_address_ae5;
9280 data = ca0113_mmio_init_data_ae5;
9281 count = ARRAY_SIZE(ca0113_mmio_init_data_ae5);
9282
9283 if (ca0132_quirk(spec) == QUIRK_AE7) {
9284 writel(0x00000680, spec->mem_base + 0x1c);
9285 writel(0x00880680, spec->mem_base + 0x1c);
9286 }
9287
9288 for (i = 0; i < count; i++) {
9289 /*
9290 * AE-7 shares all writes with the AE-5, except that it writes
9291 * a different value to 0x20c.
9292 */
9293 if (i == 21 && ca0132_quirk(spec) == QUIRK_AE7) {
9294 writel(0x00800001, spec->mem_base + addr[i]);
9295 continue;
9296 }
9297
9298 writel(data[i], spec->mem_base + addr[i]);
9299 }
9300
9301 if (ca0132_quirk(spec) == QUIRK_AE5)
9302 writel(0x00880680, spec->mem_base + 0x1c);
9303 }
9304
9305 static void ca0132_mmio_init(struct hda_codec *codec)
9306 {
9307 struct ca0132_spec *spec = codec->spec;
9308
9309 switch (ca0132_quirk(spec)) {
9310 case QUIRK_R3D:
9311 case QUIRK_SBZ:
9312 case QUIRK_ZXR:
9313 ca0132_mmio_init_sbz(codec);
9314 break;
9315 case QUIRK_AE5:
9316 ca0132_mmio_init_ae5(codec);
9317 break;
9318 default:
9319 break;
9320 }
9321 }
9322
9323 static const unsigned int ca0132_ae5_register_set_addresses[] = {
9324 0x304, 0x304, 0x304, 0x304, 0x100, 0x304, 0x100, 0x304, 0x100, 0x304,
9325 0x100, 0x304, 0x86c, 0x800, 0x86c, 0x800, 0x804
9326 };
9327
9328 static const unsigned char ca0132_ae5_register_set_data[] = {
9329 0x0f, 0x0e, 0x1f, 0x0c, 0x3f, 0x08, 0x7f, 0x00, 0xff, 0x00, 0x6b,
9330 0x01, 0x6b, 0x57
9331 };
9332
9333 /*
9334 * This function writes to some SFR's, does some region2 writes, and then
9335 * eventually resets the codec with the 0x7ff verb. Not quite sure why it does
9336 * what it does.
9337 */
9338 static void ae5_register_set(struct hda_codec *codec)
9339 {
9340 struct ca0132_spec *spec = codec->spec;
9341 unsigned int count = ARRAY_SIZE(ca0132_ae5_register_set_addresses);
9342 const unsigned int *addr = ca0132_ae5_register_set_addresses;
9343 const unsigned char *data = ca0132_ae5_register_set_data;
9344 unsigned int i, cur_addr;
9345 unsigned char tmp[3];
9346
9347 if (ca0132_quirk(spec) == QUIRK_AE7)
9348 chipio_8051_write_pll_pmu(codec, 0x41, 0xc8);
9349
9350 chipio_8051_write_direct(codec, 0x93, 0x10);
9351 chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
9352
9353 if (ca0132_quirk(spec) == QUIRK_AE7) {
9354 tmp[0] = 0x03;
9355 tmp[1] = 0x03;
9356 tmp[2] = 0x07;
9357 } else {
9358 tmp[0] = 0x0f;
9359 tmp[1] = 0x0f;
9360 tmp[2] = 0x0f;
9361 }
9362
9363 for (i = cur_addr = 0; i < 3; i++, cur_addr++)
9364 writeb(tmp[i], spec->mem_base + addr[cur_addr]);
9365
9366 /*
9367 * First writes are in single bytes, final are in 4 bytes. So, we use
9368 * writeb, then writel.
9369 */
9370 for (i = 0; cur_addr < 12; i++, cur_addr++)
9371 writeb(data[i], spec->mem_base + addr[cur_addr]);
9372
9373 for (; cur_addr < count; i++, cur_addr++)
9374 writel(data[i], spec->mem_base + addr[cur_addr]);
9375
9376 writel(0x00800001, spec->mem_base + 0x20c);
9377
9378 if (ca0132_quirk(spec) == QUIRK_AE7) {
9379 ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
9380 ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
9381 } else {
9382 ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x3f);
9383 }
9384
9385 chipio_8051_write_direct(codec, 0x90, 0x00);
9386 chipio_8051_write_direct(codec, 0x90, 0x10);
9387
9388 if (ca0132_quirk(spec) == QUIRK_AE5)
9389 ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
9390 }
9391
9392 /*
9393 * Extra init functions for alternative ca0132 codecs. Done
9394 * here so they don't clutter up the main ca0132_init function
9395 * anymore than they have to.
9396 */
9397 static void ca0132_alt_init(struct hda_codec *codec)
9398 {
9399 struct ca0132_spec *spec = codec->spec;
9400
9401 ca0132_alt_vol_setup(codec);
9402
9403 switch (ca0132_quirk(spec)) {
9404 case QUIRK_SBZ:
9405 codec_dbg(codec, "SBZ alt_init");
9406 ca0132_gpio_init(codec);
9407 sbz_pre_dsp_setup(codec);
9408 snd_hda_sequence_write(codec, spec->chip_init_verbs);
9409 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9410 break;
9411 case QUIRK_R3DI:
9412 codec_dbg(codec, "R3DI alt_init");
9413 ca0132_gpio_init(codec);
9414 ca0132_gpio_setup(codec);
9415 r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADING);
9416 r3di_pre_dsp_setup(codec);
9417 snd_hda_sequence_write(codec, spec->chip_init_verbs);
9418 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x6FF, 0xC4);
9419 break;
9420 case QUIRK_R3D:
9421 r3d_pre_dsp_setup(codec);
9422 snd_hda_sequence_write(codec, spec->chip_init_verbs);
9423 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9424 break;
9425 case QUIRK_AE5:
9426 ca0132_gpio_init(codec);
9427 chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
9428 chipio_write(codec, 0x18b030, 0x00000020);
9429 snd_hda_sequence_write(codec, spec->chip_init_verbs);
9430 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9431 ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
9432 break;
9433 case QUIRK_AE7:
9434 ca0132_gpio_init(codec);
9435 chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
9436 snd_hda_sequence_write(codec, spec->chip_init_verbs);
9437 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9438 chipio_write(codec, 0x18b008, 0x000000f8);
9439 chipio_write(codec, 0x18b008, 0x000000f0);
9440 chipio_write(codec, 0x18b030, 0x00000020);
9441 ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
9442 break;
9443 case QUIRK_ZXR:
9444 chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
9445 snd_hda_sequence_write(codec, spec->chip_init_verbs);
9446 snd_hda_sequence_write(codec, spec->desktop_init_verbs);
9447 zxr_pre_dsp_setup(codec);
9448 break;
9449 default:
9450 break;
9451 }
9452 }
9453
9454 static int ca0132_init(struct hda_codec *codec)
9455 {
9456 struct ca0132_spec *spec = codec->spec;
9457 struct auto_pin_cfg *cfg = &spec->autocfg;
9458 int i;
9459 bool dsp_loaded;
9460
9461 /*
9462 * If the DSP is already downloaded, and init has been entered again,
9463 * there's only two reasons for it. One, the codec has awaken from a
9464 * suspended state, and in that case dspload_is_loaded will return
9465 * false, and the init will be ran again. The other reason it gets
9466 * re entered is on startup for some reason it triggers a suspend and
9467 * resume state. In this case, it will check if the DSP is downloaded,
9468 * and not run the init function again. For codecs using alt_functions,
9469 * it will check if the DSP is loaded properly.
9470 */
9471 if (spec->dsp_state == DSP_DOWNLOADED) {
9472 dsp_loaded = dspload_is_loaded(codec);
9473 if (!dsp_loaded) {
9474 spec->dsp_reload = true;
9475 spec->dsp_state = DSP_DOWNLOAD_INIT;
9476 } else {
9477 if (ca0132_quirk(spec) == QUIRK_SBZ)
9478 sbz_dsp_startup_check(codec);
9479 return 0;
9480 }
9481 }
9482
9483 if (spec->dsp_state != DSP_DOWNLOAD_FAILED)
9484 spec->dsp_state = DSP_DOWNLOAD_INIT;
9485 spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
9486
9487 if (ca0132_use_pci_mmio(spec))
9488 ca0132_mmio_init(codec);
9489
9490 CLASS(snd_hda_power_pm, pm)(codec);
9491
9492 if (ca0132_quirk(spec) == QUIRK_AE5 || ca0132_quirk(spec) == QUIRK_AE7)
9493 ae5_register_set(codec);
9494
9495 ca0132_init_params(codec);
9496 ca0132_init_flags(codec);
9497
9498 snd_hda_sequence_write(codec, spec->base_init_verbs);
9499
9500 if (ca0132_use_alt_functions(spec))
9501 ca0132_alt_init(codec);
9502
9503 ca0132_download_dsp(codec);
9504
9505 ca0132_refresh_widget_caps(codec);
9506
9507 switch (ca0132_quirk(spec)) {
9508 case QUIRK_R3DI:
9509 case QUIRK_R3D:
9510 r3d_setup_defaults(codec);
9511 break;
9512 case QUIRK_SBZ:
9513 case QUIRK_ZXR:
9514 sbz_setup_defaults(codec);
9515 break;
9516 case QUIRK_AE5:
9517 ae5_setup_defaults(codec);
9518 break;
9519 case QUIRK_AE7:
9520 ae7_setup_defaults(codec);
9521 break;
9522 default:
9523 ca0132_setup_defaults(codec);
9524 ca0132_init_analog_mic2(codec);
9525 ca0132_init_dmic(codec);
9526 break;
9527 }
9528
9529 for (i = 0; i < spec->num_outputs; i++)
9530 init_output(codec, spec->out_pins[i], spec->dacs[0]);
9531
9532 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
9533
9534 for (i = 0; i < spec->num_inputs; i++)
9535 init_input(codec, spec->input_pins[i], spec->adcs[i]);
9536
9537 init_input(codec, cfg->dig_in_pin, spec->dig_in);
9538
9539 if (!ca0132_use_alt_functions(spec)) {
9540 snd_hda_sequence_write(codec, spec->chip_init_verbs);
9541 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
9542 VENDOR_CHIPIO_PARAM_EX_ID_SET, 0x0D);
9543 snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
9544 VENDOR_CHIPIO_PARAM_EX_VALUE_SET, 0x20);
9545 }
9546
9547 if (ca0132_quirk(spec) == QUIRK_SBZ)
9548 ca0132_gpio_setup(codec);
9549
9550 snd_hda_sequence_write(codec, spec->spec_init_verbs);
9551 if (ca0132_use_alt_functions(spec)) {
9552 ca0132_alt_select_out(codec);
9553 ca0132_alt_select_in(codec);
9554 } else {
9555 ca0132_select_out(codec);
9556 ca0132_select_mic(codec);
9557 }
9558
9559 snd_hda_jack_report_sync(codec);
9560
9561 /*
9562 * Re set the PlayEnhancement switch on a resume event, because the
9563 * controls will not be reloaded.
9564 */
9565 if (spec->dsp_reload) {
9566 spec->dsp_reload = false;
9567 ca0132_pe_switch_set(codec);
9568 }
9569
9570 return 0;
9571 }
9572
9573 static int dbpro_init(struct hda_codec *codec)
9574 {
9575 struct ca0132_spec *spec = codec->spec;
9576 struct auto_pin_cfg *cfg = &spec->autocfg;
9577 unsigned int i;
9578
9579 init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
9580 init_input(codec, cfg->dig_in_pin, spec->dig_in);
9581
9582 for (i = 0; i < spec->num_inputs; i++)
9583 init_input(codec, spec->input_pins[i], spec->adcs[i]);
9584
9585 return 0;
9586 }
9587
9588 static void ca0132_free(struct hda_codec *codec)
9589 {
9590 struct ca0132_spec *spec = codec->spec;
9591
9592 cancel_delayed_work_sync(&spec->unsol_hp_work);
9593 snd_hda_power_up(codec);
9594 switch (ca0132_quirk(spec)) {
9595 case QUIRK_SBZ:
9596 sbz_exit_chip(codec);
9597 break;
9598 case QUIRK_ZXR:
9599 zxr_exit_chip(codec);
9600 break;
9601 case QUIRK_R3D:
9602 r3d_exit_chip(codec);
9603 break;
9604 case QUIRK_AE5:
9605 ae5_exit_chip(codec);
9606 break;
9607 case QUIRK_AE7:
9608 ae7_exit_chip(codec);
9609 break;
9610 case QUIRK_R3DI:
9611 r3di_gpio_shutdown(codec);
9612 break;
9613 default:
9614 break;
9615 }
9616
9617 snd_hda_sequence_write(codec, spec->base_exit_verbs);
9618 ca0132_exit_chip(codec);
9619
9620 snd_hda_power_down(codec);
9621 #ifdef CONFIG_PCI
9622 if (spec->mem_base)
9623 pci_iounmap(codec->bus->pci, spec->mem_base);
9624 #endif
9625 kfree(spec->spec_init_verbs);
9626 kfree(codec->spec);
9627 }
9628
9629 static void dbpro_free(struct hda_codec *codec)
9630 {
9631 struct ca0132_spec *spec = codec->spec;
9632
9633 zxr_dbpro_power_state_shutdown(codec);
9634
9635 kfree(spec->spec_init_verbs);
9636 kfree(codec->spec);
9637 }
9638
9639 static void ca0132_config(struct hda_codec *codec)
9640 {
9641 struct ca0132_spec *spec = codec->spec;
9642
9643 spec->dacs[0] = 0x2;
9644 spec->dacs[1] = 0x3;
9645 spec->dacs[2] = 0x4;
9646
9647 spec->multiout.dac_nids = spec->dacs;
9648 spec->multiout.num_dacs = 3;
9649
9650 if (!ca0132_use_alt_functions(spec))
9651 spec->multiout.max_channels = 2;
9652 else
9653 spec->multiout.max_channels = 6;
9654
9655 switch (ca0132_quirk(spec)) {
9656 case QUIRK_ALIENWARE:
9657 codec_dbg(codec, "%s: QUIRK_ALIENWARE applied.\n", __func__);
9658 snd_hda_apply_pincfgs(codec, alienware_pincfgs);
9659 break;
9660 case QUIRK_SBZ:
9661 codec_dbg(codec, "%s: QUIRK_SBZ applied.\n", __func__);
9662 snd_hda_apply_pincfgs(codec, sbz_pincfgs);
9663 break;
9664 case QUIRK_ZXR:
9665 codec_dbg(codec, "%s: QUIRK_ZXR applied.\n", __func__);
9666 snd_hda_apply_pincfgs(codec, zxr_pincfgs);
9667 break;
9668 case QUIRK_R3D:
9669 codec_dbg(codec, "%s: QUIRK_R3D applied.\n", __func__);
9670 snd_hda_apply_pincfgs(codec, r3d_pincfgs);
9671 break;
9672 case QUIRK_R3DI:
9673 codec_dbg(codec, "%s: QUIRK_R3DI applied.\n", __func__);
9674 snd_hda_apply_pincfgs(codec, r3di_pincfgs);
9675 break;
9676 case QUIRK_AE5:
9677 codec_dbg(codec, "%s: QUIRK_AE5 applied.\n", __func__);
9678 snd_hda_apply_pincfgs(codec, ae5_pincfgs);
9679 break;
9680 case QUIRK_AE7:
9681 codec_dbg(codec, "%s: QUIRK_AE7 applied.\n", __func__);
9682 snd_hda_apply_pincfgs(codec, ae7_pincfgs);
9683 break;
9684 default:
9685 break;
9686 }
9687
9688 switch (ca0132_quirk(spec)) {
9689 case QUIRK_ALIENWARE:
9690 spec->num_outputs = 2;
9691 spec->out_pins[0] = 0x0b; /* speaker out */
9692 spec->out_pins[1] = 0x0f;
9693 spec->shared_out_nid = 0x2;
9694 spec->unsol_tag_hp = 0x0f;
9695
9696 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
9697 spec->adcs[1] = 0x8; /* analog mic2 */
9698 spec->adcs[2] = 0xa; /* what u hear */
9699
9700 spec->num_inputs = 3;
9701 spec->input_pins[0] = 0x12;
9702 spec->input_pins[1] = 0x11;
9703 spec->input_pins[2] = 0x13;
9704 spec->shared_mic_nid = 0x7;
9705 spec->unsol_tag_amic1 = 0x11;
9706 break;
9707 case QUIRK_SBZ:
9708 case QUIRK_R3D:
9709 spec->num_outputs = 2;
9710 spec->out_pins[0] = 0x0B; /* Line out */
9711 spec->out_pins[1] = 0x0F; /* Rear headphone out */
9712 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
9713 spec->out_pins[3] = 0x11; /* Rear surround */
9714 spec->shared_out_nid = 0x2;
9715 spec->unsol_tag_hp = spec->out_pins[1];
9716 spec->unsol_tag_front_hp = spec->out_pins[2];
9717
9718 spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
9719 spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
9720 spec->adcs[2] = 0xa; /* what u hear */
9721
9722 spec->num_inputs = 2;
9723 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9724 spec->input_pins[1] = 0x13; /* What U Hear */
9725 spec->shared_mic_nid = 0x7;
9726 spec->unsol_tag_amic1 = spec->input_pins[0];
9727
9728 /* SPDIF I/O */
9729 spec->dig_out = 0x05;
9730 spec->multiout.dig_out_nid = spec->dig_out;
9731 spec->dig_in = 0x09;
9732 break;
9733 case QUIRK_ZXR:
9734 spec->num_outputs = 2;
9735 spec->out_pins[0] = 0x0B; /* Line out */
9736 spec->out_pins[1] = 0x0F; /* Rear headphone out */
9737 spec->out_pins[2] = 0x10; /* Center/LFE */
9738 spec->out_pins[3] = 0x11; /* Rear surround */
9739 spec->shared_out_nid = 0x2;
9740 spec->unsol_tag_hp = spec->out_pins[1];
9741 spec->unsol_tag_front_hp = spec->out_pins[2];
9742
9743 spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
9744 spec->adcs[1] = 0x8; /* Not connected, no front mic */
9745 spec->adcs[2] = 0xa; /* what u hear */
9746
9747 spec->num_inputs = 2;
9748 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9749 spec->input_pins[1] = 0x13; /* What U Hear */
9750 spec->shared_mic_nid = 0x7;
9751 spec->unsol_tag_amic1 = spec->input_pins[0];
9752 break;
9753 case QUIRK_ZXR_DBPRO:
9754 spec->adcs[0] = 0x8; /* ZxR DBPro Aux In */
9755
9756 spec->num_inputs = 1;
9757 spec->input_pins[0] = 0x11; /* RCA Line-in */
9758
9759 spec->dig_out = 0x05;
9760 spec->multiout.dig_out_nid = spec->dig_out;
9761
9762 spec->dig_in = 0x09;
9763 break;
9764 case QUIRK_AE5:
9765 case QUIRK_AE7:
9766 spec->num_outputs = 2;
9767 spec->out_pins[0] = 0x0B; /* Line out */
9768 spec->out_pins[1] = 0x11; /* Rear headphone out */
9769 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
9770 spec->out_pins[3] = 0x0F; /* Rear surround */
9771 spec->shared_out_nid = 0x2;
9772 spec->unsol_tag_hp = spec->out_pins[1];
9773 spec->unsol_tag_front_hp = spec->out_pins[2];
9774
9775 spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
9776 spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
9777 spec->adcs[2] = 0xa; /* what u hear */
9778
9779 spec->num_inputs = 2;
9780 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9781 spec->input_pins[1] = 0x13; /* What U Hear */
9782 spec->shared_mic_nid = 0x7;
9783 spec->unsol_tag_amic1 = spec->input_pins[0];
9784
9785 /* SPDIF I/O */
9786 spec->dig_out = 0x05;
9787 spec->multiout.dig_out_nid = spec->dig_out;
9788 break;
9789 case QUIRK_R3DI:
9790 spec->num_outputs = 2;
9791 spec->out_pins[0] = 0x0B; /* Line out */
9792 spec->out_pins[1] = 0x0F; /* Rear headphone out */
9793 spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
9794 spec->out_pins[3] = 0x11; /* Rear surround */
9795 spec->shared_out_nid = 0x2;
9796 spec->unsol_tag_hp = spec->out_pins[1];
9797 spec->unsol_tag_front_hp = spec->out_pins[2];
9798
9799 spec->adcs[0] = 0x07; /* Rear Mic / Line-in */
9800 spec->adcs[1] = 0x08; /* Front Mic, but only if no DSP */
9801 spec->adcs[2] = 0x0a; /* what u hear */
9802
9803 spec->num_inputs = 2;
9804 spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
9805 spec->input_pins[1] = 0x13; /* What U Hear */
9806 spec->shared_mic_nid = 0x7;
9807 spec->unsol_tag_amic1 = spec->input_pins[0];
9808
9809 /* SPDIF I/O */
9810 spec->dig_out = 0x05;
9811 spec->multiout.dig_out_nid = spec->dig_out;
9812 break;
9813 default:
9814 spec->num_outputs = 2;
9815 spec->out_pins[0] = 0x0b; /* speaker out */
9816 spec->out_pins[1] = 0x10; /* headphone out */
9817 spec->shared_out_nid = 0x2;
9818 spec->unsol_tag_hp = spec->out_pins[1];
9819
9820 spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
9821 spec->adcs[1] = 0x8; /* analog mic2 */
9822 spec->adcs[2] = 0xa; /* what u hear */
9823
9824 spec->num_inputs = 3;
9825 spec->input_pins[0] = 0x12;
9826 spec->input_pins[1] = 0x11;
9827 spec->input_pins[2] = 0x13;
9828 spec->shared_mic_nid = 0x7;
9829 spec->unsol_tag_amic1 = spec->input_pins[0];
9830
9831 /* SPDIF I/O */
9832 spec->dig_out = 0x05;
9833 spec->multiout.dig_out_nid = spec->dig_out;
9834 spec->dig_in = 0x09;
9835 break;
9836 }
9837
9838 /* Default HP/Speaker auto-detect from headphone pin verb: enable if the
9839 * pin config indicates presence detect (not AC_DEFCFG_MISC_NO_PRESENCE).
9840 */
9841 if (spec->unsol_tag_hp &&
9842 (snd_hda_query_pin_caps(codec, spec->unsol_tag_hp) & AC_PINCAP_PRES_DETECT) &&
9843 !(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, spec->unsol_tag_hp)) &
9844 AC_DEFCFG_MISC_NO_PRESENCE))
9845 spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID] = 1;
9846 }
9847
9848 static int ca0132_prepare_verbs(struct hda_codec *codec)
9849 {
9850 /* Verbs + terminator (an empty element) */
9851 #define NUM_SPEC_VERBS 2
9852 struct ca0132_spec *spec = codec->spec;
9853
9854 spec->chip_init_verbs = ca0132_init_verbs0;
9855 /*
9856 * Since desktop cards use pci_mmio, this can be used to determine
9857 * whether or not to use these verbs instead of a separate bool.
9858 */
9859 if (ca0132_use_pci_mmio(spec))
9860 spec->desktop_init_verbs = ca0132_init_verbs1;
9861 spec->spec_init_verbs = kzalloc_objs(struct hda_verb, NUM_SPEC_VERBS);
9862 if (!spec->spec_init_verbs)
9863 return -ENOMEM;
9864
9865 /* config EAPD */
9866 spec->spec_init_verbs[0].nid = 0x0b;
9867 spec->spec_init_verbs[0].param = 0x78D;
9868 spec->spec_init_verbs[0].verb = 0x00;
9869
9870 /* Previously commented configuration */
9871 /*
9872 spec->spec_init_verbs[2].nid = 0x0b;
9873 spec->spec_init_verbs[2].param = AC_VERB_SET_EAPD_BTLENABLE;
9874 spec->spec_init_verbs[2].verb = 0x02;
9875
9876 spec->spec_init_verbs[3].nid = 0x10;
9877 spec->spec_init_verbs[3].param = 0x78D;
9878 spec->spec_init_verbs[3].verb = 0x02;
9879
9880 spec->spec_init_verbs[4].nid = 0x10;
9881 spec->spec_init_verbs[4].param = AC_VERB_SET_EAPD_BTLENABLE;
9882 spec->spec_init_verbs[4].verb = 0x02;
9883 */
9884
9885 /* Terminator: spec->spec_init_verbs[NUM_SPEC_VERBS-1] */
9886 return 0;
9887 }
9888
9889 /*
9890 * The Sound Blaster ZxR shares the same PCI subsystem ID as some regular
9891 * Sound Blaster Z cards. However, they have different HDA codec subsystem
9892 * ID's. So, we check for the ZxR's subsystem ID, as well as the DBPro
9893 * daughter boards ID.
9894 */
9895 static void sbz_detect_quirk(struct hda_codec *codec)
9896 {
9897 switch (codec->core.subsystem_id) {
9898 case 0x11020033:
9899 codec->fixup_id = QUIRK_ZXR;
9900 break;
9901 case 0x1102003f:
9902 codec->fixup_id = QUIRK_ZXR_DBPRO;
9903 break;
9904 default:
9905 codec->fixup_id = QUIRK_SBZ;
9906 break;
9907 }
9908 }
9909
9910 static void ca0132_codec_remove(struct hda_codec *codec)
9911 {
9912 struct ca0132_spec *spec = codec->spec;
9913
9914 if (ca0132_quirk(spec) == QUIRK_ZXR_DBPRO)
9915 return dbpro_free(codec);
9916 else
9917 return ca0132_free(codec);
9918 }
9919
9920 static int ca0132_codec_probe(struct hda_codec *codec,
9921 const struct hda_device_id *id)
9922 {
9923 struct ca0132_spec *spec;
9924 int err;
9925
9926 codec_dbg(codec, "%s\n", __func__);
9927
9928 spec = kzalloc_obj(*spec);
9929 if (!spec)
9930 return -ENOMEM;
9931 codec->spec = spec;
9932 spec->codec = codec;
9933
9934 /* Detect codec quirk */
9935 snd_hda_pick_fixup(codec, ca0132_quirk_models, ca0132_quirks, NULL);
9936 if (ca0132_quirk(spec) == QUIRK_SBZ)
9937 sbz_detect_quirk(codec);
9938
9939 codec->pcm_format_first = 1;
9940 codec->no_sticky_stream = 1;
9941
9942
9943 spec->dsp_state = DSP_DOWNLOAD_INIT;
9944 spec->num_mixers = 1;
9945
9946 /* Set which mixers each quirk uses. */
9947 switch (ca0132_quirk(spec)) {
9948 case QUIRK_SBZ:
9949 spec->mixers[0] = desktop_mixer;
9950 snd_hda_codec_set_name(codec, "Sound Blaster Z");
9951 break;
9952 case QUIRK_ZXR:
9953 spec->mixers[0] = desktop_mixer;
9954 snd_hda_codec_set_name(codec, "Sound Blaster ZxR");
9955 break;
9956 case QUIRK_ZXR_DBPRO:
9957 break;
9958 case QUIRK_R3D:
9959 spec->mixers[0] = desktop_mixer;
9960 snd_hda_codec_set_name(codec, "Recon3D");
9961 break;
9962 case QUIRK_R3DI:
9963 spec->mixers[0] = r3di_mixer;
9964 snd_hda_codec_set_name(codec, "Recon3Di");
9965 break;
9966 case QUIRK_AE5:
9967 spec->mixers[0] = desktop_mixer;
9968 snd_hda_codec_set_name(codec, "Sound BlasterX AE-5");
9969 break;
9970 case QUIRK_AE7:
9971 spec->mixers[0] = desktop_mixer;
9972 snd_hda_codec_set_name(codec, "Sound Blaster AE-7");
9973 break;
9974 default:
9975 spec->mixers[0] = ca0132_mixer;
9976 break;
9977 }
9978
9979 /* Setup whether or not to use alt functions/controls/pci_mmio */
9980 switch (ca0132_quirk(spec)) {
9981 case QUIRK_SBZ:
9982 case QUIRK_R3D:
9983 case QUIRK_AE5:
9984 case QUIRK_AE7:
9985 case QUIRK_ZXR:
9986 spec->use_alt_controls = true;
9987 spec->use_alt_functions = true;
9988 spec->use_pci_mmio = true;
9989 break;
9990 case QUIRK_R3DI:
9991 spec->use_alt_controls = true;
9992 spec->use_alt_functions = true;
9993 spec->use_pci_mmio = false;
9994 break;
9995 default:
9996 spec->use_alt_controls = false;
9997 spec->use_alt_functions = false;
9998 spec->use_pci_mmio = false;
9999 break;
10000 }
10001
10002 #ifdef CONFIG_PCI
10003 if (spec->use_pci_mmio) {
10004 spec->mem_base = pci_iomap(codec->bus->pci, 2, 0xC20);
10005 if (spec->mem_base == NULL) {
10006 codec_warn(codec, "pci_iomap failed! Setting quirk to QUIRK_NONE.");
10007 codec->fixup_id = QUIRK_NONE;
10008 }
10009 }
10010 #endif
10011
10012 spec->base_init_verbs = ca0132_base_init_verbs;
10013 spec->base_exit_verbs = ca0132_base_exit_verbs;
10014
10015 INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
10016
10017 ca0132_init_chip(codec);
10018
10019 ca0132_config(codec);
10020
10021 err = ca0132_prepare_verbs(codec);
10022 if (err < 0)
10023 goto error;
10024
10025 err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
10026 if (err < 0)
10027 goto error;
10028
10029 ca0132_setup_unsol(codec);
10030
10031 return 0;
10032
10033 error:
10034 ca0132_codec_remove(codec);
10035 return err;
10036 }
10037
10038 static int ca0132_codec_build_controls(struct hda_codec *codec)
10039 {
10040 struct ca0132_spec *spec = codec->spec;
10041
10042 if (ca0132_quirk(spec) == QUIRK_ZXR_DBPRO)
10043 return dbpro_build_controls(codec);
10044 else
10045 return ca0132_build_controls(codec);
10046 }
10047
10048 static int ca0132_codec_build_pcms(struct hda_codec *codec)
10049 {
10050 struct ca0132_spec *spec = codec->spec;
10051
10052 if (ca0132_quirk(spec) == QUIRK_ZXR_DBPRO)
10053 return dbpro_build_pcms(codec);
10054 else
10055 return ca0132_build_pcms(codec);
10056 }
10057
10058 static int ca0132_codec_init(struct hda_codec *codec)
10059 {
10060 struct ca0132_spec *spec = codec->spec;
10061
10062 if (ca0132_quirk(spec) == QUIRK_ZXR_DBPRO)
10063 return dbpro_init(codec);
10064 else
10065 return ca0132_init(codec);
10066 }
10067
10068 static int ca0132_codec_suspend(struct hda_codec *codec)
10069 {
10070 struct ca0132_spec *spec = codec->spec;
10071
10072 cancel_delayed_work_sync(&spec->unsol_hp_work);
10073 return 0;
10074 }
10075
10076 static const struct hda_codec_ops ca0132_codec_ops = {
10077 .probe = ca0132_codec_probe,
10078 .remove = ca0132_codec_remove,
10079 .build_controls = ca0132_codec_build_controls,
10080 .build_pcms = ca0132_codec_build_pcms,
10081 .init = ca0132_codec_init,
10082 .unsol_event = snd_hda_jack_unsol_event,
10083 .suspend = ca0132_codec_suspend,
10084 };
10085
10086 /*
10087 * driver entries
10088 */
10089 static const struct hda_device_id snd_hda_id_ca0132[] = {
10090 HDA_CODEC_ID(0x11020011, "CA0132"),
10091 {} /* terminator */
10092 };
10093 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_ca0132);
10094
10095 MODULE_LICENSE("GPL");
10096 MODULE_DESCRIPTION("Creative Sound Core3D codec");
10097
10098 static struct hda_codec_driver ca0132_driver = {
10099 .id = snd_hda_id_ca0132,
10100 .ops = &ca0132_codec_ops,
10101 };
10102
10103 module_hda_codec_driver(ca0132_driver);