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