emumixer.c 55.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
/*
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
 *                   Takashi Iwai <tiwai@suse.de>
 *                   Creative Labs, Inc.
 *  Routines for control of EMU10K1 chips / mixer routines
 *  Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
 *
8
9
10
 *  Copyright (c) by James Courtier-Dutton <James@superbug.co.uk>
 *  	Added EMU 1010 support.
 *
Linus Torvalds's avatar
Linus Torvalds committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 *  BUGS:
 *    --
 *
 *  TODO:
 *    --
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <sound/driver.h>
#include <linux/time.h>
#include <linux/init.h>
#include <sound/core.h>
#include <sound/emu10k1.h>
38
#include <linux/delay.h>
39
40
41
#include <sound/tlv.h>

#include "p17v.h"
Linus Torvalds's avatar
Linus Torvalds committed
42
43
44

#define AC97_ID_STAC9758	0x83847658

45
static const DECLARE_TLV_DB_SCALE(snd_audigy_db_scale2, -10350, 50, 1); /* WM8775 gain scale */
46

47
static int snd_emu10k1_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
48
49
50
51
52
53
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

54
55
static int snd_emu10k1_spdif_get(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
56
{
57
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
58
59
60
61
62
63
64
65
66
67
68
69
	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
	unsigned long flags;

	spin_lock_irqsave(&emu->reg_lock, flags);
	ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
	ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
	ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
	ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

70
71
static int snd_emu10k1_spdif_get_mask(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
72
73
74
75
76
77
78
79
{
	ucontrol->value.iec958.status[0] = 0xff;
	ucontrol->value.iec958.status[1] = 0xff;
	ucontrol->value.iec958.status[2] = 0xff;
	ucontrol->value.iec958.status[3] = 0xff;
	return 0;
}

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
static char *emu1010_src_texts[] = { 
	"Silence",
	"Dock Mic A",
	"Dock Mic B",
	"Dock ADC1 Left",
	"Dock ADC1 Right",
	"Dock ADC2 Left",
	"Dock ADC2 Right",
	"Dock ADC3 Left",
	"Dock ADC3 Right",
	"0202 ADC Left",
	"0202 ADC Right",
	"0202 SPDIF Left",
	"0202 SPDIF Right",
	"ADAT 0",
	"ADAT 1",
	"ADAT 2",
	"ADAT 3",
	"ADAT 4",
	"ADAT 5",
	"ADAT 6",
	"ADAT 7",
	"DSP 0",
	"DSP 1",
	"DSP 2",
	"DSP 3",
	"DSP 4",
	"DSP 5",
	"DSP 6",
	"DSP 7",
	"DSP 8",
	"DSP 9",
	"DSP 10",
	"DSP 11",
	"DSP 12",
	"DSP 13",
	"DSP 14",
	"DSP 15",
	"DSP 16",
	"DSP 17",
	"DSP 18",
	"DSP 19",
	"DSP 20",
	"DSP 21",
	"DSP 22",
	"DSP 23",
	"DSP 24",
	"DSP 25",
	"DSP 26",
	"DSP 27",
	"DSP 28",
	"DSP 29",
	"DSP 30",
	"DSP 31",
};

static unsigned int emu1010_src_regs[] = {
	EMU_SRC_SILENCE,/* 0 */
	EMU_SRC_DOCK_MIC_A1, /* 1 */
	EMU_SRC_DOCK_MIC_B1, /* 2 */
	EMU_SRC_DOCK_ADC1_LEFT1, /* 3 */
	EMU_SRC_DOCK_ADC1_RIGHT1, /* 4 */
	EMU_SRC_DOCK_ADC2_LEFT1, /* 5 */
	EMU_SRC_DOCK_ADC2_RIGHT1, /* 6 */
	EMU_SRC_DOCK_ADC3_LEFT1, /* 7 */
	EMU_SRC_DOCK_ADC3_RIGHT1, /* 8 */
	EMU_SRC_HAMOA_ADC_LEFT1, /* 9 */
	EMU_SRC_HAMOA_ADC_RIGHT1, /* 10 */
	EMU_SRC_HANA_SPDIF_LEFT1, /* 11 */
	EMU_SRC_HANA_SPDIF_RIGHT1, /* 12 */
	EMU_SRC_HANA_ADAT, /* 13 */
	EMU_SRC_HANA_ADAT+1, /* 14 */
	EMU_SRC_HANA_ADAT+2, /* 15 */
	EMU_SRC_HANA_ADAT+3, /* 16 */
	EMU_SRC_HANA_ADAT+4, /* 17 */
	EMU_SRC_HANA_ADAT+5, /* 18 */
	EMU_SRC_HANA_ADAT+6, /* 19 */
	EMU_SRC_HANA_ADAT+7, /* 20 */
	EMU_SRC_ALICE_EMU32A, /* 21 */
	EMU_SRC_ALICE_EMU32A+1, /* 22 */
	EMU_SRC_ALICE_EMU32A+2, /* 23 */
	EMU_SRC_ALICE_EMU32A+3, /* 24 */
	EMU_SRC_ALICE_EMU32A+4, /* 25 */
	EMU_SRC_ALICE_EMU32A+5, /* 26 */
	EMU_SRC_ALICE_EMU32A+6, /* 27 */
	EMU_SRC_ALICE_EMU32A+7, /* 28 */
	EMU_SRC_ALICE_EMU32A+8, /* 29 */
	EMU_SRC_ALICE_EMU32A+9, /* 30 */
	EMU_SRC_ALICE_EMU32A+0xa, /* 31 */
	EMU_SRC_ALICE_EMU32A+0xb, /* 32 */
	EMU_SRC_ALICE_EMU32A+0xc, /* 33 */
	EMU_SRC_ALICE_EMU32A+0xd, /* 34 */
	EMU_SRC_ALICE_EMU32A+0xe, /* 35 */
	EMU_SRC_ALICE_EMU32A+0xf, /* 36 */
	EMU_SRC_ALICE_EMU32B, /* 37 */
	EMU_SRC_ALICE_EMU32B+1, /* 38 */
	EMU_SRC_ALICE_EMU32B+2, /* 39 */
	EMU_SRC_ALICE_EMU32B+3, /* 40 */
	EMU_SRC_ALICE_EMU32B+4, /* 41 */
	EMU_SRC_ALICE_EMU32B+5, /* 42 */
	EMU_SRC_ALICE_EMU32B+6, /* 43 */
	EMU_SRC_ALICE_EMU32B+7, /* 44 */
	EMU_SRC_ALICE_EMU32B+8, /* 45 */
	EMU_SRC_ALICE_EMU32B+9, /* 46 */
	EMU_SRC_ALICE_EMU32B+0xa, /* 47 */
	EMU_SRC_ALICE_EMU32B+0xb, /* 48 */
	EMU_SRC_ALICE_EMU32B+0xc, /* 49 */
	EMU_SRC_ALICE_EMU32B+0xd, /* 50 */
	EMU_SRC_ALICE_EMU32B+0xe, /* 51 */
	EMU_SRC_ALICE_EMU32B+0xf, /* 52 */
};

static unsigned int emu1010_output_dst[] = {
	EMU_DST_DOCK_DAC1_LEFT1, /* 0 */
	EMU_DST_DOCK_DAC1_RIGHT1, /* 1 */
	EMU_DST_DOCK_DAC2_LEFT1, /* 2 */
	EMU_DST_DOCK_DAC2_RIGHT1, /* 3 */
	EMU_DST_DOCK_DAC3_LEFT1, /* 4 */
	EMU_DST_DOCK_DAC3_RIGHT1, /* 5 */
	EMU_DST_DOCK_DAC4_LEFT1, /* 6 */
	EMU_DST_DOCK_DAC4_RIGHT1, /* 7 */
	EMU_DST_DOCK_PHONES_LEFT1, /* 8 */
	EMU_DST_DOCK_PHONES_RIGHT1, /* 9 */
	EMU_DST_DOCK_SPDIF_LEFT1, /* 10 */
	EMU_DST_DOCK_SPDIF_RIGHT1, /* 11 */
	EMU_DST_HANA_SPDIF_LEFT1, /* 12 */
	EMU_DST_HANA_SPDIF_RIGHT1, /* 13 */
	EMU_DST_HAMOA_DAC_LEFT1, /* 14 */
	EMU_DST_HAMOA_DAC_RIGHT1, /* 15 */
	EMU_DST_HANA_ADAT, /* 16 */
	EMU_DST_HANA_ADAT+1, /* 17 */
	EMU_DST_HANA_ADAT+2, /* 18 */
	EMU_DST_HANA_ADAT+3, /* 19 */
	EMU_DST_HANA_ADAT+4, /* 20 */
	EMU_DST_HANA_ADAT+5, /* 21 */
	EMU_DST_HANA_ADAT+6, /* 22 */
	EMU_DST_HANA_ADAT+7, /* 23 */
};

static unsigned int emu1010_input_dst[] = {
	EMU_DST_ALICE2_EMU32_0,
	EMU_DST_ALICE2_EMU32_1,
	EMU_DST_ALICE2_EMU32_2,
	EMU_DST_ALICE2_EMU32_3,
	EMU_DST_ALICE2_EMU32_4,
	EMU_DST_ALICE2_EMU32_5,
	EMU_DST_ALICE2_EMU32_6,
	EMU_DST_ALICE2_EMU32_7,
	EMU_DST_ALICE2_EMU32_8,
	EMU_DST_ALICE2_EMU32_9,
	EMU_DST_ALICE2_EMU32_A,
	EMU_DST_ALICE2_EMU32_B,
	EMU_DST_ALICE2_EMU32_C,
	EMU_DST_ALICE2_EMU32_D,
	EMU_DST_ALICE2_EMU32_E,
	EMU_DST_ALICE2_EMU32_F,
	EMU_DST_ALICE_I2S0_LEFT,
	EMU_DST_ALICE_I2S0_RIGHT,
	EMU_DST_ALICE_I2S1_LEFT,
	EMU_DST_ALICE_I2S1_RIGHT,
	EMU_DST_ALICE_I2S2_LEFT,
	EMU_DST_ALICE_I2S2_RIGHT,
};

static int snd_emu1010_input_output_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 53;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, emu1010_src_texts[uinfo->value.enumerated.item]);
	return 0;
}

static int snd_emu1010_output_source_get(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	int channel;

	channel = (kcontrol->private_value) & 0xff;
	ucontrol->value.enumerated.item[0] = emu->emu1010.output_source[channel];
	return 0;
}

static int snd_emu1010_output_source_put(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	int change = 0;
	unsigned int val;
	int channel;

	channel = (kcontrol->private_value) & 0xff;
	if (emu->emu1010.output_source[channel] != ucontrol->value.enumerated.item[0]) {
		val = emu->emu1010.output_source[channel] = ucontrol->value.enumerated.item[0];
		change = 1;
		snd_emu1010_fpga_link_dst_src_write(emu,
			emu1010_output_dst[channel], emu1010_src_regs[val]);
	}
	return change;
}

static int snd_emu1010_input_source_get(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	int channel;

	channel = (kcontrol->private_value) & 0xff;
	ucontrol->value.enumerated.item[0] = emu->emu1010.input_source[channel];
	return 0;
}

static int snd_emu1010_input_source_put(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	int change = 0;
	unsigned int val;
	int channel;

	channel = (kcontrol->private_value) & 0xff;
	if (emu->emu1010.input_source[channel] != ucontrol->value.enumerated.item[0]) {
		val = emu->emu1010.input_source[channel] = ucontrol->value.enumerated.item[0];
		change = 1;
		snd_emu1010_fpga_link_dst_src_write(emu,
			emu1010_input_dst[channel], emu1010_src_regs[val]);
	}
	return change;
}

#define EMU1010_SOURCE_OUTPUT(xname,chid) \
{								\
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
	.info =  snd_emu1010_input_output_source_info,		\
	.get =   snd_emu1010_output_source_get,			\
	.put =   snd_emu1010_output_source_put,			\
	.private_value = chid					\
}

static struct snd_kcontrol_new snd_emu1010_output_enum_ctls[] __devinitdata = {
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
	EMU1010_SOURCE_OUTPUT("Dock DAC1 Left Playback Enum", 0),
	EMU1010_SOURCE_OUTPUT("Dock DAC1 Right Playback Enum", 1),
	EMU1010_SOURCE_OUTPUT("Dock DAC2 Left Playback Enum", 2),
	EMU1010_SOURCE_OUTPUT("Dock DAC2 Right Playback Enum", 3),
	EMU1010_SOURCE_OUTPUT("Dock DAC3 Left Playback Enum", 4),
	EMU1010_SOURCE_OUTPUT("Dock DAC3 Right Playback Enum", 5),
	EMU1010_SOURCE_OUTPUT("Dock DAC4 Left Playback Enum", 6),
	EMU1010_SOURCE_OUTPUT("Dock DAC4 Right Playback Enum", 7),
	EMU1010_SOURCE_OUTPUT("Dock Phones Left Playback Enum", 8),
	EMU1010_SOURCE_OUTPUT("Dock Phones Right Playback Enum", 9),
	EMU1010_SOURCE_OUTPUT("Dock SPDIF Left Playback Enum", 0xa),
	EMU1010_SOURCE_OUTPUT("Dock SPDIF Right Playback Enum", 0xb),
	EMU1010_SOURCE_OUTPUT("1010 SPDIF Left Playback Enum", 0xc),
	EMU1010_SOURCE_OUTPUT("1010 SPDIF Right Playback Enum", 0xd),
	EMU1010_SOURCE_OUTPUT("0202 DAC Left Playback Enum", 0xe),
	EMU1010_SOURCE_OUTPUT("0202 DAC Right Playback Enum", 0xf),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 0 Playback Enum", 0x10),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 1 Playback Enum", 0x11),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 2 Playback Enum", 0x12),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 3 Playback Enum", 0x13),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 4 Playback Enum", 0x14),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 5 Playback Enum", 0x15),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 6 Playback Enum", 0x16),
	EMU1010_SOURCE_OUTPUT("1010 ADAT 7 Playback Enum", 0x17),
348
349
350
351
352
353
354
355
356
357
358
359
360
};

#define EMU1010_SOURCE_INPUT(xname,chid) \
{								\
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
	.info =  snd_emu1010_input_output_source_info,		\
	.get =   snd_emu1010_input_source_get,			\
	.put =   snd_emu1010_input_source_put,			\
	.private_value = chid					\
}

static struct snd_kcontrol_new snd_emu1010_input_enum_ctls[] __devinitdata = {
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
	EMU1010_SOURCE_INPUT("DSP 0 Capture Enum", 0),
	EMU1010_SOURCE_INPUT("DSP 1 Capture Enum", 1),
	EMU1010_SOURCE_INPUT("DSP 2 Capture Enum", 2),
	EMU1010_SOURCE_INPUT("DSP 3 Capture Enum", 3),
	EMU1010_SOURCE_INPUT("DSP 4 Capture Enum", 4),
	EMU1010_SOURCE_INPUT("DSP 5 Capture Enum", 5),
	EMU1010_SOURCE_INPUT("DSP 6 Capture Enum", 6),
	EMU1010_SOURCE_INPUT("DSP 7 Capture Enum", 7),
	EMU1010_SOURCE_INPUT("DSP 8 Capture Enum", 8),
	EMU1010_SOURCE_INPUT("DSP 9 Capture Enum", 9),
	EMU1010_SOURCE_INPUT("DSP A Capture Enum", 0xa),
	EMU1010_SOURCE_INPUT("DSP B Capture Enum", 0xb),
	EMU1010_SOURCE_INPUT("DSP C Capture Enum", 0xc),
	EMU1010_SOURCE_INPUT("DSP D Capture Enum", 0xd),
	EMU1010_SOURCE_INPUT("DSP E Capture Enum", 0xe),
	EMU1010_SOURCE_INPUT("DSP F Capture Enum", 0xf),
	EMU1010_SOURCE_INPUT("DSP 10 Capture Enum", 0x10),
	EMU1010_SOURCE_INPUT("DSP 11 Capture Enum", 0x11),
	EMU1010_SOURCE_INPUT("DSP 12 Capture Enum", 0x12),
	EMU1010_SOURCE_INPUT("DSP 13 Capture Enum", 0x13),
	EMU1010_SOURCE_INPUT("DSP 14 Capture Enum", 0x14),
	EMU1010_SOURCE_INPUT("DSP 15 Capture Enum", 0x15),
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
};




static int snd_emu1010_adc_pads_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	return 0;
}

static int snd_emu1010_adc_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int mask = kcontrol->private_value & 0xff;
	ucontrol->value.integer.value[0] = (emu->emu1010.adc_pads & mask) ? 1 : 0;
	return 0;
}

static int snd_emu1010_adc_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int mask = kcontrol->private_value & 0xff;
	unsigned int val, cache;
	val = ucontrol->value.integer.value[0];
	cache = emu->emu1010.adc_pads;
	if (val == 1) 
		cache = cache | mask;
	else
		cache = cache & ~mask;
	if (cache != emu->emu1010.adc_pads) {
		snd_emu1010_fpga_write(emu, EMU_HANA_ADC_PADS, cache );
	        emu->emu1010.adc_pads = cache;
	}

	return 0;
}



#define EMU1010_ADC_PADS(xname,chid) \
{								\
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
	.info =  snd_emu1010_adc_pads_info,			\
	.get =   snd_emu1010_adc_pads_get,			\
	.put =   snd_emu1010_adc_pads_put,			\
	.private_value = chid					\
}

static struct snd_kcontrol_new snd_emu1010_adc_pads[] __devinitdata = {
	EMU1010_ADC_PADS("ADC1 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD1),
	EMU1010_ADC_PADS("ADC2 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD2),
	EMU1010_ADC_PADS("ADC3 14dB PAD Audio Dock Capture Switch", EMU_HANA_DOCK_ADC_PAD3),
	EMU1010_ADC_PADS("ADC1 14dB PAD 0202 Capture Switch", EMU_HANA_0202_ADC_PAD1),
};

static int snd_emu1010_dac_pads_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	return 0;
}

static int snd_emu1010_dac_pads_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int mask = kcontrol->private_value & 0xff;
	ucontrol->value.integer.value[0] = (emu->emu1010.dac_pads & mask) ? 1 : 0;
	return 0;
}

static int snd_emu1010_dac_pads_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int mask = kcontrol->private_value & 0xff;
	unsigned int val, cache;
	val = ucontrol->value.integer.value[0];
	cache = emu->emu1010.dac_pads;
	if (val == 1) 
		cache = cache | mask;
	else
		cache = cache & ~mask;
	if (cache != emu->emu1010.dac_pads) {
		snd_emu1010_fpga_write(emu, EMU_HANA_DAC_PADS, cache );
	        emu->emu1010.dac_pads = cache;
	}

	return 0;
}



#define EMU1010_DAC_PADS(xname,chid) \
{								\
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
	.info =  snd_emu1010_dac_pads_info,			\
	.get =   snd_emu1010_dac_pads_get,			\
	.put =   snd_emu1010_dac_pads_put,			\
	.private_value = chid					\
}

static struct snd_kcontrol_new snd_emu1010_dac_pads[] __devinitdata = {
	EMU1010_DAC_PADS("DAC1 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD1),
	EMU1010_DAC_PADS("DAC2 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD2),
	EMU1010_DAC_PADS("DAC3 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD3),
	EMU1010_DAC_PADS("DAC4 Audio Dock 14dB PAD Playback Switch", EMU_HANA_DOCK_DAC_PAD4),
	EMU1010_DAC_PADS("DAC1 0202 14dB PAD Playback Switch", EMU_HANA_0202_DAC_PAD1),
497
498
};

499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

static int snd_emu1010_internal_clock_info(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
{
	static char *texts[2] = {
		"44100", "48000"
	};

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 2;
	if (uinfo->value.enumerated.item > 1)
                uinfo->value.enumerated.item = 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

static int snd_emu1010_internal_clock_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = emu->emu1010.internal_clock;
	return 0;
}

static int snd_emu1010_internal_clock_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int val;
	int change = 0;

	val = ucontrol->value.enumerated.item[0] ;
	change = (emu->emu1010.internal_clock != val);
	if (change) {
		emu->emu1010.internal_clock = val;
		switch (val) {
		case 0:
			/* 44100 */
			/* Mute all */
			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
			/* Default fallback clock 48kHz */
			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_44_1K );
			/* Word Clock source, Internal 44.1kHz x1 */
			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
			EMU_HANA_WCLOCK_INT_44_1K | EMU_HANA_WCLOCK_1X );
			/* Set LEDs on Audio Dock */
			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2,
				EMU_HANA_DOCK_LEDS_2_44K | EMU_HANA_DOCK_LEDS_2_LOCK );
			/* Allow DLL to settle */
550
			msleep(10);
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
			/* Unmute all */
			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
			break;
		case 1:
			/* 48000 */
			/* Mute all */
			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_MUTE );
			/* Default fallback clock 48kHz */
			snd_emu1010_fpga_write(emu, EMU_HANA_DEFCLOCK, EMU_HANA_DEFCLOCK_48K );
			/* Word Clock source, Internal 48kHz x1 */
			snd_emu1010_fpga_write(emu, EMU_HANA_WCLOCK,
				EMU_HANA_WCLOCK_INT_48K | EMU_HANA_WCLOCK_1X );
			/* Set LEDs on Audio Dock */
			snd_emu1010_fpga_write(emu, EMU_HANA_DOCK_LEDS_2,
				EMU_HANA_DOCK_LEDS_2_48K | EMU_HANA_DOCK_LEDS_2_LOCK );
			/* Allow DLL to settle */
567
			msleep(10);
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
			/* Unmute all */
			snd_emu1010_fpga_write(emu, EMU_HANA_UNMUTE, EMU_UNMUTE );
			break;
		}
	}
        return change;
}

static struct snd_kcontrol_new snd_emu1010_internal_clock =
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
	.name =         "Clock Internal Rate",
	.count =	1,
	.info =         snd_emu1010_internal_clock_info,
	.get =          snd_emu1010_internal_clock_get,
	.put =          snd_emu1010_internal_clock_put
};

587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
static int snd_audigy_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
{
#if 0
	static char *texts[4] = {
		"Unknown1", "Unknown2", "Mic", "Line"
	};
#endif
	static char *texts[2] = {
		"Mic", "Line"
	};

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 2;
	if (uinfo->value.enumerated.item > 1)
                uinfo->value.enumerated.item = 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

static int snd_audigy_i2c_capture_source_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = emu->i2c_capture_source;
	return 0;
}

static int snd_audigy_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int source_id;
	unsigned int ngain, ogain;
	u32 gpio;
	int change = 0;
	unsigned long flags;
	u32 source;
	/* If the capture source has changed,
	 * update the capture volume from the cached value
	 * for the particular source.
	 */
	source_id = ucontrol->value.enumerated.item[0]; /* Use 2 and 3 */
	change = (emu->i2c_capture_source != source_id);
	if (change) {
		snd_emu10k1_i2c_write(emu, ADC_MUX, 0); /* Mute input */
		spin_lock_irqsave(&emu->emu_lock, flags);
		gpio = inl(emu->port + A_IOCFG);
		if (source_id==0)
			outl(gpio | 0x4, emu->port + A_IOCFG);
		else
			outl(gpio & ~0x4, emu->port + A_IOCFG);
		spin_unlock_irqrestore(&emu->emu_lock, flags);

		ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
		if (ngain != ogain)
			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
		ngain = emu->i2c_capture_volume[source_id][1]; /* Right */
		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Right */
		if (ngain != ogain)
			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));

		source = 1 << (source_id + 2);
		snd_emu10k1_i2c_write(emu, ADC_MUX, source); /* Set source */
		emu->i2c_capture_source = source_id;
	}
        return change;
}

static struct snd_kcontrol_new snd_audigy_i2c_capture_source =
{
		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
		.name =		"Capture Source",
		.info =		snd_audigy_i2c_capture_source_info,
		.get =		snd_audigy_i2c_capture_source_get,
		.put =		snd_audigy_i2c_capture_source_put
};

static int snd_audigy_i2c_volume_info(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 255;
	return 0;
}

static int snd_audigy_i2c_volume_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	int source_id;

	source_id = kcontrol->private_value;

	ucontrol->value.integer.value[0] = emu->i2c_capture_volume[source_id][0];
	ucontrol->value.integer.value[1] = emu->i2c_capture_volume[source_id][1];
	return 0;
}

static int snd_audigy_i2c_volume_put(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int ogain;
	unsigned int ngain;
	int source_id;
	int change = 0;

	source_id = kcontrol->private_value;
	ogain = emu->i2c_capture_volume[source_id][0]; /* Left */
	ngain = ucontrol->value.integer.value[0];
	if (ngain > 0xff)
		return 0;
	if (ogain != ngain) {
		if (emu->i2c_capture_source == source_id)
			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff) );
		emu->i2c_capture_volume[source_id][0] = ucontrol->value.integer.value[0];
		change = 1;
	}
	ogain = emu->i2c_capture_volume[source_id][1]; /* Right */
	ngain = ucontrol->value.integer.value[1];
	if (ngain > 0xff)
		return 0;
	if (ogain != ngain) {
		if (emu->i2c_capture_source == source_id)
			snd_emu10k1_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
		emu->i2c_capture_volume[source_id][1] = ucontrol->value.integer.value[1];
		change = 1;
	}

	return change;
}

#define I2C_VOLUME(xname,chid) \
{								\
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |		\
	          SNDRV_CTL_ELEM_ACCESS_TLV_READ,		\
	.info =  snd_audigy_i2c_volume_info,			\
	.get =   snd_audigy_i2c_volume_get,			\
	.put =   snd_audigy_i2c_volume_put,			\
	.tlv = { .p = snd_audigy_db_scale2 },			\
	.private_value = chid					\
}


static struct snd_kcontrol_new snd_audigy_i2c_volume_ctls[] __devinitdata = {
	I2C_VOLUME("Mic Capture Volume", 0),
	I2C_VOLUME("Line Capture Volume", 0)
};

Takashi Iwai's avatar
Takashi Iwai committed
743
#if 0
744
static int snd_audigy_spdif_output_rate_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
745
746
747
748
749
750
751
752
753
754
755
756
{
	static char *texts[] = {"44100", "48000", "96000"};

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 3;
	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

757
758
static int snd_audigy_spdif_output_rate_get(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
759
{
760
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
	unsigned int tmp;
	unsigned long flags;
	

	spin_lock_irqsave(&emu->reg_lock, flags);
	tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
	switch (tmp & A_SPDIF_RATE_MASK) {
	case A_SPDIF_44100:
		ucontrol->value.enumerated.item[0] = 0;
		break;
	case A_SPDIF_48000:
		ucontrol->value.enumerated.item[0] = 1;
		break;
	case A_SPDIF_96000:
		ucontrol->value.enumerated.item[0] = 2;
		break;
	default:
		ucontrol->value.enumerated.item[0] = 1;
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

784
785
static int snd_audigy_spdif_output_rate_put(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
786
{
787
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
	int change;
	unsigned int reg, val, tmp;
	unsigned long flags;

	switch(ucontrol->value.enumerated.item[0]) {
	case 0:
		val = A_SPDIF_44100;
		break;
	case 1:
		val = A_SPDIF_48000;
		break;
	case 2:
		val = A_SPDIF_96000;
		break;
	default:
		val = A_SPDIF_48000;
		break;
	}

	
	spin_lock_irqsave(&emu->reg_lock, flags);
	reg = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, 0);
	tmp = reg & ~A_SPDIF_RATE_MASK;
	tmp |= val;
	if ((change = (tmp != reg)))
		snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, 0, tmp);
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

818
static struct snd_kcontrol_new snd_audigy_spdif_output_rate =
Linus Torvalds's avatar
Linus Torvalds committed
819
820
821
822
823
824
825
826
827
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
	.iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
	.name =         "Audigy SPDIF Output Sample Rate",
	.count =	1,
	.info =         snd_audigy_spdif_output_rate_info,
	.get =          snd_audigy_spdif_output_rate_get,
	.put =          snd_audigy_spdif_output_rate_put
};
Takashi Iwai's avatar
Takashi Iwai committed
828
#endif
Linus Torvalds's avatar
Linus Torvalds committed
829

830
831
static int snd_emu10k1_spdif_put(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
832
{
833
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
	int change;
	unsigned int val;
	unsigned long flags;

	val = (ucontrol->value.iec958.status[0] << 0) |
	      (ucontrol->value.iec958.status[1] << 8) |
	      (ucontrol->value.iec958.status[2] << 16) |
	      (ucontrol->value.iec958.status[3] << 24);
	spin_lock_irqsave(&emu->reg_lock, flags);
	change = val != emu->spdif_bits[idx];
	if (change) {
		snd_emu10k1_ptr_write(emu, SPCS0 + idx, 0, val);
		emu->spdif_bits[idx] = val;
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

853
static struct snd_kcontrol_new snd_emu10k1_spdif_mask_control =
Linus Torvalds's avatar
Linus Torvalds committed
854
855
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
856
	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
857
858
859
860
861
862
	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
	.count =	4,
	.info =         snd_emu10k1_spdif_info,
	.get =          snd_emu10k1_spdif_get_mask
};

863
static struct snd_kcontrol_new snd_emu10k1_spdif_control =
Linus Torvalds's avatar
Linus Torvalds committed
864
{
865
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
866
867
868
869
870
871
872
873
	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
	.count =	4,
	.info =         snd_emu10k1_spdif_info,
	.get =          snd_emu10k1_spdif_get,
	.put =          snd_emu10k1_spdif_put
};


874
static void update_emu10k1_fxrt(struct snd_emu10k1 *emu, int voice, unsigned char *route)
Linus Torvalds's avatar
Linus Torvalds committed
875
876
877
878
879
880
881
882
883
884
885
886
{
	if (emu->audigy) {
		snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
				      snd_emu10k1_compose_audigy_fxrt1(route));
		snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
				      snd_emu10k1_compose_audigy_fxrt2(route));
	} else {
		snd_emu10k1_ptr_write(emu, FXRT, voice,
				      snd_emu10k1_compose_send_routing(route));
	}
}

887
static void update_emu10k1_send_volume(struct snd_emu10k1 *emu, int voice, unsigned char *volume)
Linus Torvalds's avatar
Linus Torvalds committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
{
	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_A, voice, volume[0]);
	snd_emu10k1_ptr_write(emu, PTRX_FXSENDAMOUNT_B, voice, volume[1]);
	snd_emu10k1_ptr_write(emu, PSST_FXSENDAMOUNT_C, voice, volume[2]);
	snd_emu10k1_ptr_write(emu, DSL_FXSENDAMOUNT_D, voice, volume[3]);
	if (emu->audigy) {
		unsigned int val = ((unsigned int)volume[4] << 24) |
			((unsigned int)volume[5] << 16) |
			((unsigned int)volume[6] << 8) |
			(unsigned int)volume[7];
		snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice, val);
	}
}

/* PCM stream controls */

904
static int snd_emu10k1_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
905
{
906
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
907
908
909
910
911
912
913
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = emu->audigy ? 3*8 : 3*4;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
	return 0;
}

914
915
static int snd_emu10k1_send_routing_get(struct snd_kcontrol *kcontrol,
                                        struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
916
917
{
	unsigned long flags;
918
919
920
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
921
922
923
924
925
926
927
928
929
930
931
932
933
	int voice, idx;
	int num_efx = emu->audigy ? 8 : 4;
	int mask = emu->audigy ? 0x3f : 0x0f;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (voice = 0; voice < 3; voice++)
		for (idx = 0; idx < num_efx; idx++)
			ucontrol->value.integer.value[(voice * num_efx) + idx] = 
				mix->send_routing[voice][idx] & mask;
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

934
935
static int snd_emu10k1_send_routing_put(struct snd_kcontrol *kcontrol,
                                        struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
936
937
{
	unsigned long flags;
938
939
940
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
	int change = 0, voice, idx, val;
	int num_efx = emu->audigy ? 8 : 4;
	int mask = emu->audigy ? 0x3f : 0x0f;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (voice = 0; voice < 3; voice++)
		for (idx = 0; idx < num_efx; idx++) {
			val = ucontrol->value.integer.value[(voice * num_efx) + idx] & mask;
			if (mix->send_routing[voice][idx] != val) {
				mix->send_routing[voice][idx] = val;
				change = 1;
			}
		}	
	if (change && mix->epcm) {
		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
					    &mix->send_routing[1][0]);
			update_emu10k1_fxrt(emu, mix->epcm->voices[1]->number,
					    &mix->send_routing[2][0]);
		} else if (mix->epcm->voices[0]) {
			update_emu10k1_fxrt(emu, mix->epcm->voices[0]->number,
					    &mix->send_routing[0][0]);
		}
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

969
static struct snd_kcontrol_new snd_emu10k1_send_routing_control =
Linus Torvalds's avatar
Linus Torvalds committed
970
971
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
972
	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
973
974
975
976
977
978
979
	.name =         "EMU10K1 PCM Send Routing",
	.count =	32,
	.info =         snd_emu10k1_send_routing_info,
	.get =          snd_emu10k1_send_routing_get,
	.put =          snd_emu10k1_send_routing_put
};

980
static int snd_emu10k1_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
981
{
982
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
983
984
985
986
987
988
989
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = emu->audigy ? 3*8 : 3*4;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 255;
	return 0;
}

990
991
static int snd_emu10k1_send_volume_get(struct snd_kcontrol *kcontrol,
                                       struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
992
993
{
	unsigned long flags;
994
995
996
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
997
998
999
1000
1001
1002
1003
1004
1005
1006
	int idx;
	int num_efx = emu->audigy ? 8 : 4;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < 3*num_efx; idx++)
		ucontrol->value.integer.value[idx] = mix->send_volume[idx/num_efx][idx%num_efx];
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

1007
1008
static int snd_emu10k1_send_volume_put(struct snd_kcontrol *kcontrol,
                                       struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1009
1010
{
	unsigned long flags;
1011
1012
1013
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
	int change = 0, idx, val;
	int num_efx = emu->audigy ? 8 : 4;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < 3*num_efx; idx++) {
		val = ucontrol->value.integer.value[idx] & 255;
		if (mix->send_volume[idx/num_efx][idx%num_efx] != val) {
			mix->send_volume[idx/num_efx][idx%num_efx] = val;
			change = 1;
		}
	}
	if (change && mix->epcm) {
		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
						   &mix->send_volume[1][0]);
			update_emu10k1_send_volume(emu, mix->epcm->voices[1]->number,
						   &mix->send_volume[2][0]);
		} else if (mix->epcm->voices[0]) {
			update_emu10k1_send_volume(emu, mix->epcm->voices[0]->number,
						   &mix->send_volume[0][0]);
		}
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

1040
static struct snd_kcontrol_new snd_emu10k1_send_volume_control =
Linus Torvalds's avatar
Linus Torvalds committed
1041
1042
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1043
	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
1044
1045
1046
1047
1048
1049
1050
	.name =         "EMU10K1 PCM Send Volume",
	.count =	32,
	.info =         snd_emu10k1_send_volume_info,
	.get =          snd_emu10k1_send_volume_get,
	.put =          snd_emu10k1_send_volume_put
};

1051
static int snd_emu10k1_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1052
1053
1054
1055
1056
1057
1058
1059
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 3;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 0xffff;
	return 0;
}

1060
1061
static int snd_emu10k1_attn_get(struct snd_kcontrol *kcontrol,
                                struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1062
{
1063
1064
1065
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
	unsigned long flags;
	int idx;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < 3; idx++)
		ucontrol->value.integer.value[idx] = mix->attn[idx];
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

1076
1077
static int snd_emu10k1_attn_put(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1078
1079
{
	unsigned long flags;
1080
1081
1082
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
	int change = 0, idx, val;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < 3; idx++) {
		val = ucontrol->value.integer.value[idx] & 0xffff;
		if (mix->attn[idx] != val) {
			mix->attn[idx] = val;
			change = 1;
		}
	}
	if (change && mix->epcm) {
		if (mix->epcm->voices[0] && mix->epcm->voices[1]) {
			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[1]);
			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[1]->number, mix->attn[2]);
		} else if (mix->epcm->voices[0]) {
			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[0]->number, mix->attn[0]);
		}
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

1105
static struct snd_kcontrol_new snd_emu10k1_attn_control =
Linus Torvalds's avatar
Linus Torvalds committed
1106
1107
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1108
	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds's avatar
Linus Torvalds committed
1109
1110
1111
1112
1113
1114
1115
1116
1117
	.name =         "EMU10K1 PCM Volume",
	.count =	32,
	.info =         snd_emu10k1_attn_info,
	.get =          snd_emu10k1_attn_get,
	.put =          snd_emu10k1_attn_put
};

/* Mutichannel PCM stream controls */

1118
static int snd_emu10k1_efx_send_routing_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1119
{
1120
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1121
1122
1123
1124
1125
1126
1127
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = emu->audigy ? 8 : 4;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = emu->audigy ? 0x3f : 0x0f;
	return 0;
}

1128
1129
static int snd_emu10k1_efx_send_routing_get(struct snd_kcontrol *kcontrol,
                                        struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1130
1131
{
	unsigned long flags;
1132
1133
1134
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
	int idx;
	int num_efx = emu->audigy ? 8 : 4;
	int mask = emu->audigy ? 0x3f : 0x0f;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < num_efx; idx++)
		ucontrol->value.integer.value[idx] = 
			mix->send_routing[0][idx] & mask;
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

1147
1148
static int snd_emu10k1_efx_send_routing_put(struct snd_kcontrol *kcontrol,
                                        struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1149
1150
{
	unsigned long flags;
1151
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1152
	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1153
	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
Linus Torvalds's avatar
Linus Torvalds committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
	int change = 0, idx, val;
	int num_efx = emu->audigy ? 8 : 4;
	int mask = emu->audigy ? 0x3f : 0x0f;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < num_efx; idx++) {
		val = ucontrol->value.integer.value[idx] & mask;
		if (mix->send_routing[0][idx] != val) {
			mix->send_routing[0][idx] = val;
			change = 1;
		}
	}	

	if (change && mix->epcm) {
		if (mix->epcm->voices[ch]) {
			update_emu10k1_fxrt(emu, mix->epcm->voices[ch]->number,
					&mix->send_routing[0][0]);
		}
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

1177
static struct snd_kcontrol_new snd_emu10k1_efx_send_routing_control =
Linus Torvalds's avatar
Linus Torvalds committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
	.name =         "Multichannel PCM Send Routing",
	.count =	16,
	.info =         snd_emu10k1_efx_send_routing_info,
	.get =          snd_emu10k1_efx_send_routing_get,
	.put =          snd_emu10k1_efx_send_routing_put
};

1188
static int snd_emu10k1_efx_send_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1189
{
1190
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1191
1192
1193
1194
1195
1196
1197
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = emu->audigy ? 8 : 4;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 255;
	return 0;
}

1198
1199
static int snd_emu10k1_efx_send_volume_get(struct snd_kcontrol *kcontrol,
                                       struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1200
1201
{
	unsigned long flags;
1202
1203
1204
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
	int idx;
	int num_efx = emu->audigy ? 8 : 4;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < num_efx; idx++)
		ucontrol->value.integer.value[idx] = mix->send_volume[0][idx];
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

1215
1216
static int snd_emu10k1_efx_send_volume_put(struct snd_kcontrol *kcontrol,
                                       struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1217
1218
{
	unsigned long flags;
1219
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1220
	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1221
	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
Linus Torvalds's avatar
Linus Torvalds committed
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
	int change = 0, idx, val;
	int num_efx = emu->audigy ? 8 : 4;

	spin_lock_irqsave(&emu->reg_lock, flags);
	for (idx = 0; idx < num_efx; idx++) {
		val = ucontrol->value.integer.value[idx] & 255;
		if (mix->send_volume[0][idx] != val) {
			mix->send_volume[0][idx] = val;
			change = 1;
		}
	}
	if (change && mix->epcm) {
		if (mix->epcm->voices[ch]) {
			update_emu10k1_send_volume(emu, mix->epcm->voices[ch]->number,
						   &mix->send_volume[0][0]);
		}
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}


1244
static struct snd_kcontrol_new snd_emu10k1_efx_send_volume_control =
Linus Torvalds's avatar
Linus Torvalds committed
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
	.name =         "Multichannel PCM Send Volume",
	.count =	16,
	.info =         snd_emu10k1_efx_send_volume_info,
	.get =          snd_emu10k1_efx_send_volume_get,
	.put =          snd_emu10k1_efx_send_volume_put
};

1255
static int snd_emu10k1_efx_attn_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1256
1257
1258
1259
1260
1261
1262
1263
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 0xffff;
	return 0;
}

1264
1265
static int snd_emu10k1_efx_attn_get(struct snd_kcontrol *kcontrol,
                                struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1266
{
1267
1268
1269
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
	struct snd_emu10k1_pcm_mixer *mix =
		&emu->efx_pcm_mixer[snd_ctl_get_ioffidx(kcontrol, &ucontrol->id)];
Linus Torvalds's avatar
Linus Torvalds committed
1270
1271
1272
1273
1274
1275
1276
1277
	unsigned long flags;

	spin_lock_irqsave(&emu->reg_lock, flags);
	ucontrol->value.integer.value[0] = mix->attn[0];
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return 0;
}

1278
1279
static int snd_emu10k1_efx_attn_put(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1280
1281
{
	unsigned long flags;
1282
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1283
	int ch = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1284
	struct snd_emu10k1_pcm_mixer *mix = &emu->efx_pcm_mixer[ch];
Linus Torvalds's avatar
Linus Torvalds committed
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
	int change = 0, val;

	spin_lock_irqsave(&emu->reg_lock, flags);
	val = ucontrol->value.integer.value[0] & 0xffff;
	if (mix->attn[0] != val) {
		mix->attn[0] = val;
		change = 1;
	}
	if (change && mix->epcm) {
		if (mix->epcm->voices[ch]) {
			snd_emu10k1_ptr_write(emu, VTFT_VOLUMETARGET, mix->epcm->voices[ch]->number, mix->attn[0]);
		}
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

1302
static struct snd_kcontrol_new snd_emu10k1_efx_attn_control =
Linus Torvalds's avatar
Linus Torvalds committed
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
	.name =         "Multichannel PCM Volume",
	.count =	16,
	.info =         snd_emu10k1_efx_attn_info,
	.get =          snd_emu10k1_efx_attn_get,
	.put =          snd_emu10k1_efx_attn_put
};

1313
static int snd_emu10k1_shared_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
1314
1315
1316
1317
1318
1319
1320
1321
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	return 0;
}

1322
1323
static int snd_emu10k1_shared_spdif_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1324
{
1325
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1326
1327
1328
1329
1330
1331
1332
1333

	if (emu->audigy)
		ucontrol->value.integer.value[0] = inl(emu->port + A_IOCFG) & A_IOCFG_GPOUT0 ? 1 : 0;
	else
		ucontrol->value.integer.value[0] = inl(emu->port + HCFG) & HCFG_GPOUT0 ? 1 : 0;
	return 0;
}

1334
1335
static int snd_emu10k1_shared_spdif_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
1336
1337
{
	unsigned long flags;
1338
	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
1339
1340
1341
1342
	unsigned int reg, val;
	int change = 0;

	spin_lock_irqsave(&emu->reg_lock, flags);
1343
1344
1345
	if ( emu->card_capabilities->i2c_adc) {
		/* Do nothing for Audigy 2 ZS Notebook */
	} else if (emu->audigy) {
Linus Torvalds's avatar
Linus Torvalds committed
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
		reg = inl(emu->port + A_IOCFG);
		val = ucontrol->value.integer.value[0] ? A_IOCFG_GPOUT0 : 0;
		change = (reg & A_IOCFG_GPOUT0) != val;
		if (change) {
			reg &= ~A_IOCFG_GPOUT0;
			reg |= val;
			outl(reg | val, emu->port + A_IOCFG);
		}
	}
	reg = inl(emu->port + HCFG);
	val = ucontrol->value.integer.value[0] ? HCFG_GPOUT0 : 0;
	change |= (reg & HCFG_GPOUT0) != val;
	if (change) {
		reg &= ~HCFG_GPOUT0;
		reg |= val;
		outl(reg | val, emu->port + HCFG);
	}
	spin_unlock_irqrestore(&emu->reg_lock, flags);
	return change;
}

1367
static struct snd_kcontrol_new snd_emu10k1_shared_spdif __devinitdata =
Linus Torvalds's avatar
Linus Torvalds committed
1368
1369
1370
1371
1372
1373
1374
1375
{
	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
	.name =		"SB Live Analog/Digital Output Jack",
	.info =		snd_emu10k1_shared_spdif_info,
	.get =		snd_emu10k1_shared_spdif_get,
	.put =		snd_emu10k1_shared_spdif_put
};

1376
static struct snd_kcontrol_new snd_audigy_shared_spdif __devinitdata =
Linus Torvalds's avatar
Linus Torvalds committed
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
{
	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
	.name =		"Audigy Analog/Digital Output Jack",
	.info =		snd_emu10k1_shared_spdif_info,
	.get =		snd_emu10k1_shared_spdif_get,
	.put =		snd_emu10k1_shared_spdif_put
};

/*
 */
1387
static void snd_emu10k1_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds's avatar
Linus Torvalds committed
1388
{
1389
	struct snd_emu10k1 *emu = ac97->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1390
1391
1392
1393
1394
	emu->ac97 = NULL;
}

/*
 */
1395
static int remove_ctl(struct snd_card *card, const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
1396
{
1397
	struct snd_ctl_elem_id id;
Linus Torvalds's avatar
Linus Torvalds committed
1398
1399
1400
1401
1402
1403
	memset(&id, 0, sizeof(id));
	strcpy(id.name, name);
	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	return snd_ctl_remove_id(card, &id);
}

1404
static struct snd_kcontrol *ctl_find(struct snd_card *card, const char *name)
Linus Torvalds's avatar
Linus Torvalds committed
1405
{
1406
	struct snd_ctl_elem_id sid;
Linus Torvalds's avatar
Linus Torvalds committed
1407
1408
1409
1410
1411
1412
	memset(&sid, 0, sizeof(sid));
	strcpy(sid.name, name);
	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	return snd_ctl_find_id(card, &sid);
}

1413
static int rename_ctl(struct snd_card *card, const char *src, const char *dst)
Linus Torvalds's avatar
Linus Torvalds committed
1414
{
1415
	struct snd_kcontrol *kctl = ctl_find(card, src);
Linus Torvalds's avatar
Linus Torvalds committed
1416
1417
1418
1419
1420
1421
1422
	if (kctl) {
		strcpy(kctl->id.name, dst);
		return 0;
	}
	return -ENOENT;
}

1423
int __devinit snd_emu10k1_mixer(struct snd_emu10k1 *emu,
1424
				int pcm_device, int multi_device)
Linus Torvalds's avatar
Linus Torvalds committed
1425
1426
{
	int err, pcm;
1427
1428
	struct snd_kcontrol *kctl;
	struct snd_card *card = emu->card;
Linus Torvalds's avatar
Linus Torvalds committed
1429
1430
1431
1432
1433
1434
1435
	char **c;
	static char *emu10k1_remove_ctls[] = {
		/* no AC97 mono, surround, center/lfe */
		"Master Mono Playback Switch",
		"Master Mono Playback Volume",
		"PCM Out Path & Mute",
		"Mono Output Select",
1436
1437
		"Front Playback Switch",
		"Front Playback Volume",
Linus Torvalds's avatar
Linus Torvalds committed
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
		"Surround Playback Switch",
		"Surround Playback Volume",
		"Center Playback Switch",
		"Center Playback Volume",
		"LFE Playback Switch",
		"LFE Playback Volume",
		NULL
	};
	static char *emu10k1_rename_ctls[] = {
		"Surround Digital Playback Volume", "Surround Playback Volume",
		"Center Digital Playback Volume", "Center Playback Volume",
		"LFE Digital Playback Volume", "LFE Playback Volume",
		NULL
	};
	static char *audigy_remove_ctls[] = {
		/* Master/PCM controls on ac97 of Audigy has no effect */
1454
1455
		/* On the Audigy2 the AC97 playback is piped into
		 * the Philips ADC for 24bit capture */
Linus Torvalds's avatar
Linus Torvalds committed
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
		"PCM Playback Switch",
		"PCM Playback Volume",
		"Master Mono Playback Switch",
		"Master Mono Playback Volume",
		"Master Playback Switch",
		"Master Playback Volume",
		"PCM Out Path & Mute",
		"Mono Output Select",
		/* remove unused AC97 capture controls */
		"Capture Source",
		"Capture Switch",
		"Capture Volume",
		"Mic Select",
		"Video Playback Switch",
		"Video Playback Volume",
		"Mic Playback Switch",
		"Mic Playback Volume",
		NULL
	};
	static char *audigy_rename_ctls[] = {
		/* use conventional names */
		"Wave Playback Volume", "PCM Playback Volume",
		/* "Wave Capture Volume", "PCM Capture Volume", */
		"Wave Master Playback Volume", "Master Playback Volume",
		"AMic Playback Volume", "Mic Playback Volume",
		NULL
	};
1483
1484
1485
1486
1487
1488
	static char *audigy_rename_ctls_i2c_adc[] = {
		//"Analog Mix Capture Volume","OLD Analog Mix Capture Volume",
		"Line Capture Volume", "Analog Mix Capture Volume",
		"Wave Playback Volume", "OLD PCM Playback Volume",
		"Wave Master Playback Volume", "Master Playback Volume",
		"AMic Playback Volume", "Old Mic Playback Volume",
1489
		"CD Capture Volume", "IEC958 Optical Capture Volume",
1490
1491
1492
1493
1494
1495
1496
1497
		NULL
	};
	static char *audigy_remove_ctls_i2c_adc[] = {
		/* On the Audigy2 ZS Notebook
		 * Capture via WM8775  */
		"Mic Capture Volume",
		"Analog Mix Capture Volume",
		"Aux Capture Volume",
1498
		"IEC958 Optical Capture Volume",
1499
1500
		NULL
	};
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
	static char *audigy_remove_ctls_1361t_adc[] = {
		/* On the Audigy2 the AC97 playback is piped into
		 * the Philips ADC for 24bit capture */
		"PCM Playback Switch",
		"PCM Playback Volume",
		"Master Mono Playback Switch",
		"Master Mono Playback Volume",
		"Capture Source",
		"Capture Switch",
		"Capture Volume",
		"Mic Capture Volume",
		"Headphone Playback Switch",
		"Headphone Playback Volume",
		"3D Control - Center",
		"3D Control - Depth",
		"3D Control - Switch",
		"Line2 Playback Volume",
		"Line2 Capture Volume",
		NULL
	};
	static char *audigy_rename_ctls_1361t_adc[] = {
		"Master Playback Switch", "Master Capture Switch",
		"Master Playback Volume", "Master Capture Volume",
		"Wave Master Playback Volume", "Master Playback Volume",
		"PC Speaker Playback Switch", "PC Speaker Capture Switch",
		"PC Speaker Playback Volume", "PC Speaker Capture Volume",
		"Phone Playback Switch", "Phone Capture Switch",
		"Phone Playback Volume", "Phone Capture Volume",
		"Mic Playback Switch", "Mic Capture Switch",
		"Mic Playback Volume", "Mic Capture Volume",
		"Line Playback Switch", "Line Capture Switch",
		"Line Playback Volume", "Line Capture Volume",
		"CD Playback Switch", "CD Capture Switch",
		"CD Playback Volume", "CD Capture Volume",
		"Aux Playback Switch", "Aux Capture Switch",
		"Aux Playback Volume", "Aux Capture Volume",
		"Video Playback Switch", "Video Capture Switch",
		"Video Playback Volume", "Video Capture Volume",

		NULL
	};
Linus Torvalds's avatar
Linus Torvalds committed
1542

Lee Revell's avatar
Lee Revell committed
1543
	if (emu->card_capabilities->ac97_chip) {
1544
1545
1546
		struct snd_ac97_bus *pbus;
		struct snd_ac97_template ac97;
		static struct snd_ac97_bus_ops ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1547
1548
1549
1550
			.write = snd_emu10k1_ac97_write,
			.read = snd_emu10k1_ac97_read,
		};

1551
1552
		if ((err = snd_ac97_bus(emu->card, 0, &ops, NULL, &pbus)) < 0)
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
1553
1554
1555
1556
1557
1558
		pbus->no_vra = 1; /* we don't need VRA */
		
		memset(&ac97, 0, sizeof(ac97));
		ac97.private_data = emu;
		ac97.private_free = snd_emu10k1_mixer_free_ac97;
		ac97.scaps = AC97_SCAP_NO_SPDIF;
1559
1560
1561
1562
1563
1564
1565
1566
		if ((err = snd_ac97_mixer(pbus, &ac97, &emu->ac97)) < 0) {
			if (emu->card_capabilities->ac97_chip == 1)
				return err;
			snd_printd(KERN_INFO "emu10k1: AC97 is optional on this board\n");
			snd_printd(KERN_INFO"          Proceeding without ac97 mixers...\n");
			snd_device_free(emu->card, pbus);
			goto no_ac97; /* FIXME: get rid of ugly gotos.. */
		}
Linus Torvalds's avatar
Linus Torvalds committed
1567
1568
		if (emu->audigy) {
			/* set master volume to 0 dB */
1569
			snd_ac97_write_cache(emu->ac97, AC97_MASTER, 0x0000);
Linus Torvalds's avatar
Linus Torvalds committed
1570
			/* set capture source to mic */
1571
			snd_ac97_write_cache(emu->ac97, AC97_REC_SEL, 0x0000);
1572
1573
1574
1575
			if (emu->card_capabilities->adc_1361t)
				c = audigy_remove_ctls_1361t_adc;
			else 
				c = audigy_remove_ctls;
Linus Torvalds's avatar
Linus Torvalds committed
1576
1577
1578
1579
1580
1581
1582
1583
1584
		} else {
			/*
			 * Credits for cards based on STAC9758:
			 *   James Courtier-Dutton <James@superbug.demon.co.uk>
			 *   Voluspa <voluspa@comhem.se>
			 */
			if (emu->ac97->id == AC97_ID_STAC9758) {
				emu->rear_ac97 = 1;
				snd_emu10k1_ptr_write(emu, AC97SLOT, 0, AC97SLOT_CNTR|AC97SLOT_LFE|AC97SLOT_REAR_LEFT|AC97SLOT_REAR_RIGHT);
1585
				snd_ac97_write_cache(emu->ac97, AC97_HEADPHONE, 0x0202);
Linus Torvalds's avatar
Linus Torvalds committed
1586
1587
			}
			/* remove unused AC97 controls */
1588
1589
			snd_ac97_write_cache(emu->ac97, AC97_SURROUND_MASTER, 0x0202);
			snd_ac97_write_cache(emu->ac97, AC97_CENTER_LFE_MASTER, 0x0202);
Linus Torvalds's avatar
Linus Torvalds committed
1590
1591
1592
1593
			c = emu10k1_remove_ctls;
		}
		for (; *c; c++)
			remove_ctl(card, *c);
1594
1595
1596
1597
	} else if (emu->card_capabilities->i2c_adc) {
		c = audigy_remove_ctls_i2c_adc;
		for (; *c; c++)
			remove_ctl(card, *c);
Linus Torvalds's avatar
Linus Torvalds committed
1598
	} else {
1599
	no_ac97:
Lee Revell's avatar
Lee Revell committed
1600
		if (emu->card_capabilities->ecard)
Linus Torvalds's avatar
Linus Torvalds committed
1601
1602
1603
1604
1605
1606
1607
1608
			strcpy(emu->card->mixername, "EMU APS");
		else if (emu->audigy)
			strcpy(emu->card->mixername, "SB Audigy");
		else
			strcpy(emu->card->mixername, "Emu10k1");
	}

	if (emu->audigy)
1609
1610
		if (emu->card_capabilities->adc_1361t)
			c = audigy_rename_ctls_1361t_adc;
1611
1612
		else if (emu->card_capabilities->i2c_adc)
			c = audigy_rename_ctls_i2c_adc;
1613
1614
		else
			c = audigy_rename_ctls;
Linus Torvalds's avatar
Linus Torvalds committed
1615
1616
1617
1618
	else
		c = emu10k1_rename_ctls;
	for (; *c; c += 2)
		rename_ctl(card, c[0], c[1]);
1619

1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
	if (emu->card_capabilities->subsystem == 0x20071102) {  /* Audigy 4 Pro */
		rename_ctl(card, "Line2 Capture Volume", "Line1/Mic Capture Volume");
		rename_ctl(card, "Analog Mix Capture Volume", "Line2 Capture Volume");
		rename_ctl(card, "Aux2 Capture Volume", "Line3 Capture Volume");
		rename_ctl(card, "Mic Capture Volume", "Unknown1 Capture Volume");
		remove_ctl(card, "Headphone Playback Switch");
		remove_ctl(card, "Headphone Playback Volume");
		remove_ctl(card, "3D Control - Center");
		remove_ctl(card, "3D Control - Depth");
		remove_ctl(card, "3D Control - Switch");
	}
Linus Torvalds's avatar
Linus Torvalds committed
1631
1632
	if ((kctl = emu->ctl_send_routing = snd_ctl_new1(&snd_emu10k1_send_routing_control, emu)) == NULL)
		return -ENOMEM;
1633
	kctl->id.device = pcm_device;
Linus Torvalds's avatar
Linus Torvalds committed
1634
1635
1636
1637
	if ((err = snd_ctl_add(card, kctl)))
		return err;
	if ((kctl = emu->ctl_send_volume = snd_ctl_new1(&snd_emu10k1_send_volume_control, emu)) == NULL)
		return -ENOMEM;
1638
	kctl->id.device = pcm_device;
Linus Torvalds's avatar
Linus Torvalds committed
1639
1640
1641
1642
	if ((err = snd_ctl_add(card, kctl)))
		return err;
	if ((kctl = emu->ctl_attn = snd_ctl_new1(&snd_emu10k1_attn_control, emu)) == NULL)
		return -ENOMEM;
1643
	kctl->id.device = pcm_device;
Linus Torvalds's avatar
Linus Torvalds committed
1644
1645
1646
1647
1648
	if ((err = snd_ctl_add(card, kctl)))
		return err;

	if ((kctl = emu->ctl_efx_send_routing = snd_ctl_new1(&snd_emu10k1_efx_send_routing_control, emu)) == NULL)
		return -ENOMEM;
1649
	kctl->id.device = multi_device;
Linus Torvalds's avatar
Linus Torvalds committed
1650
1651
1652
1653
1654
	if ((err = snd_ctl_add(card, kctl)))
		return err;
	
	if ((kctl = emu->ctl_efx_send_volume = snd_ctl_new1(&snd_emu10k1_efx_send_volume_control, emu)) == NULL)
		return -ENOMEM;
1655
	kctl->id.device = multi_device;
Linus Torvalds's avatar
Linus Torvalds committed
1656
1657
1658
1659
1660
	if ((err = snd_ctl_add(card, kctl)))
		return err;
	
	if ((kctl = emu->ctl_efx_attn = snd_ctl_new1(&snd_emu10k1_efx_attn_control, emu)) == NULL)
		return -ENOMEM;
1661
	kctl->id.device = multi_device;
Linus Torvalds's avatar
Linus Torvalds committed
1662
1663
1664
1665
1666
	if ((err = snd_ctl_add(card, kctl)))
		return err;

	/* initialize the routing and volume table for each pcm playback stream */
	for (pcm = 0; pcm < 32; pcm++) {
1667
		struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds's avatar
Linus Torvalds committed
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
		int v;
		
		mix = &emu->pcm_mixer[pcm];
		mix->epcm = NULL;

		for (v = 0; v < 4; v++)
			mix->send_routing[0][v] = 
				mix->send_routing[1][v] = 
				mix->send_routing[2][v] = v;
		
		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
		mix->send_volume[0][0] = mix->send_volume[0][1] =
		mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
		
		mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
	}
	
	/* initialize the routing and volume table for the multichannel playback stream */
	for (pcm = 0; pcm < NUM_EFX_PLAYBACK; pcm++) {
1687
		struct snd_emu10k1_pcm_mixer *mix;
Linus Torvalds's avatar
Linus Torvalds committed
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
		int v;
		
		mix = &emu->efx_pcm_mixer[pcm];
		mix->epcm = NULL;

		mix->send_routing[0][0] = pcm;
		mix->send_routing[0][1] = (pcm == 0) ? 1 : 0;
		for (v = 0; v < 2; v++)
			mix->send_routing[0][2+v] = 13+v;
		if (emu->audigy)
			for (v = 0; v < 4; v++)
				mix->send_routing[0][4+v] = 60+v;
		
		memset(&mix->send_volume, 0, sizeof(mix->send_volume));
		mix->send_volume[0][0]  = 255;
		
		mix->attn[0] = 0xffff;
	}
	
Lee Revell's avatar
Lee Revell committed
1707
	if (! emu->card_capabilities->ecard) { /* FIXME: APS has these controls? */
Linus Torvalds's avatar
Linus Torvalds committed
1708
1709
1710
		/* sb live! and audigy */
		if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_mask_control, emu)) == NULL)
			return -ENOMEM;
1711
1712
		if (!emu->audigy)
			kctl->id.device = emu->pcm_efx->device;
Linus Torvalds's avatar
Linus Torvalds committed
1713
1714
1715
1716
		if ((err = snd_ctl_add(card, kctl)))
			return err;
		if ((kctl = snd_ctl_new1(&snd_emu10k1_spdif_control, emu)) == NULL)
			return -ENOMEM;
1717
1718
		if (!emu->audigy)
			kctl->id.device = emu->pcm_efx->device;
Linus Torvalds's avatar
Linus Torvalds committed
1719
1720
1721
1722
		if ((err = snd_ctl_add(card, kctl)))
			return err;
	}

1723
	if ( emu->card_capabilities->emu1010) {
1724
1725
		;  /* Disable the snd_audigy_spdif_shared_spdif */
	} else if (emu->audigy) {
Linus Torvalds's avatar
Linus Torvalds committed
1726
1727
1728
1729
		if ((kctl = snd_ctl_new1(&snd_audigy_shared_spdif, emu)) == NULL)
			return -ENOMEM;
		if ((err = snd_ctl_add(card, kctl)))
			return err;
1730
#if 0
Linus Torvalds's avatar
Linus Torvalds committed
1731
1732
1733
1734
		if ((kctl = snd_ctl_new1(&snd_audigy_spdif_output_rate, emu)) == NULL)
			return -ENOMEM;
		if ((err = snd_ctl_add(card, kctl)))
			return err;
1735
#endif
Lee Revell's avatar
Lee Revell committed
1736
	} else if (! emu->card_capabilities->ecard) {
Linus Torvalds's avatar
Linus Torvalds committed
1737
1738
1739
1740
1741
1742
		/* sb live! */
		if ((kctl = snd_ctl_new1(&snd_emu10k1_shared_spdif, emu)) == NULL)
			return -ENOMEM;
		if ((err = snd_ctl_add(card, kctl)))
			return err;
	}
Lee Revell's avatar
Lee Revell committed
1743
	if (emu->card_capabilities->ca0151_chip) { /* P16V */
Linus Torvalds's avatar
Linus Torvalds committed
1744
1745