ca0106_mixer.c 22.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *  Copyright (c) 2004 James Courtier-Dutton <James@superbug.demon.co.uk>
 *  Driver CA0106 chips. e.g. Sound Blaster Audigy LS and Live 24bit
4
 *  Version: 0.0.18
L
Linus Torvalds 已提交
5 6 7 8 9 10 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 38 39
 *
 *  FEATURES currently supported:
 *    See ca0106_main.c for features.
 * 
 *  Changelog:
 *    Support interrupts per period.
 *    Removed noise from Center/LFE channel when in Analog mode.
 *    Rename and remove mixer controls.
 *  0.0.6
 *    Use separate card based DMA buffer for periods table list.
 *  0.0.7
 *    Change remove and rename ctrls into lists.
 *  0.0.8
 *    Try to fix capture sources.
 *  0.0.9
 *    Fix AC3 output.
 *    Enable S32_LE format support.
 *  0.0.10
 *    Enable playback 48000 and 96000 rates. (Rates other that these do not work, even with "plug:front".)
 *  0.0.11
 *    Add Model name recognition.
 *  0.0.12
 *    Correct interrupt timing. interrupt at end of period, instead of in the middle of a playback period.
 *    Remove redundent "voice" handling.
 *  0.0.13
 *    Single trigger call for multi channels.
 *  0.0.14
 *    Set limits based on what the sound card hardware can do.
 *    playback periods_min=2, periods_max=8
 *    capture hw constraints require period_size = n * 64 bytes.
 *    playback hw constraints require period_size = n * 64 bytes.
 *  0.0.15
 *    Separated ca0106.c into separate functional .c files.
 *  0.0.16
 *    Modified Copyright message.
40 41
 *  0.0.17
 *    Implement Mic and Line in Capture.
42 43
 *  0.0.18
 *    Add support for mute control on SB Live 24bit (cards w/ SPI DAC)
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 *
 *  This code was initally based on code from ALSA's emu10k1x.c which is:
 *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
 *
 *   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/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/moduleparam.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/pcm.h>
#include <sound/ac97_codec.h>
#include <sound/info.h>
74
#include <sound/tlv.h>
75
#include <asm/io.h>
L
Linus Torvalds 已提交
76 77 78

#include "ca0106.h"

79 80
static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale1, -5175, 25, 1);
static const DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale2, -10350, 50, 1);
81

82
#define snd_ca0106_shared_spdif_info	snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
83

84 85
static int snd_ca0106_shared_spdif_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
86
{
87
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
88 89 90 91 92

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

93 94
static int snd_ca0106_shared_spdif_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
95
{
96
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	unsigned int val;
	int change = 0;
	u32 mask;

	val = ucontrol->value.enumerated.item[0] ;
	change = (emu->spdif_enable != val);
	if (change) {
		emu->spdif_enable = val;
		if (val == 1) {
			/* Digital */
			snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
			snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x0b000000);
			snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
				snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) & ~0x1000);
			mask = inl(emu->port + GPIO) & ~0x101;
			outl(mask, emu->port + GPIO);

		} else {
			/* Analog */
			snd_ca0106_ptr_write(emu, SPDIF_SELECT1, 0, 0xf);
117
			snd_ca0106_ptr_write(emu, SPDIF_SELECT2, 0, 0x000f0000);
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126
			snd_ca0106_ptr_write(emu, CAPTURE_CONTROL, 0,
				snd_ca0106_ptr_read(emu, CAPTURE_CONTROL, 0) | 0x1000);
			mask = inl(emu->port + GPIO) | 0x101;
			outl(mask, emu->port + GPIO);
		}
	}
        return change;
}

127 128
static int snd_ca0106_capture_source_info(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
129
{
T
Takashi Iwai 已提交
130
	static char *texts[6] = {
131
		"IEC958 out", "i2s mixer out", "IEC958 in", "i2s in", "AC97 in", "SRC out"
T
Takashi Iwai 已提交
132
	};
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141 142

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

143 144
static int snd_ca0106_capture_source_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
145
{
146
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
147 148 149 150 151

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

152 153
static int snd_ca0106_capture_source_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
154
{
155
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	unsigned int val;
	int change = 0;
	u32 mask;
	u32 source;

	val = ucontrol->value.enumerated.item[0] ;
	change = (emu->capture_source != val);
	if (change) {
		emu->capture_source = val;
		source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
		mask = snd_ca0106_ptr_read(emu, CAPTURE_SOURCE, 0) & 0xffff;
		snd_ca0106_ptr_write(emu, CAPTURE_SOURCE, 0, source | mask);
	}
        return change;
}

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
static int snd_ca0106_i2c_capture_source_info(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_info *uinfo)
{
	static char *texts[6] = {
		"Phone", "Mic", "Line in", "Aux"
	};

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

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

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

static int snd_ca0106_i2c_capture_source_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
{
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int source_id;
	unsigned int ngain, ogain;
	int change = 0;
	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] ;
	change = (emu->i2c_capture_source != source_id);
	if (change) {
		snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
		ngain = emu->i2c_capture_volume[source_id][0]; /* Left */
		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][0]; /* Left */
		if (ngain != ogain)
			snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCL, ((ngain) & 0xff));
		ngain = emu->i2c_capture_volume[source_id][1]; /* Left */
		ogain = emu->i2c_capture_volume[emu->i2c_capture_source][1]; /* Left */
		if (ngain != ogain)
			snd_ca0106_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
		source = 1 << source_id;
		snd_ca0106_i2c_write(emu, ADC_MUX, source); /* Set source */
		emu->i2c_capture_source = source_id;
	}
        return change;
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241
static int snd_ca0106_capture_line_in_side_out_info(struct snd_kcontrol *kcontrol,
					       struct snd_ctl_elem_info *uinfo)
{
	static char *texts[2] = { "Side out", "Line in" };

	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;
}

242 243
static int snd_ca0106_capture_mic_line_in_info(struct snd_kcontrol *kcontrol,
					       struct snd_ctl_elem_info *uinfo)
244 245 246 247 248 249 250 251 252 253 254 255
{
	static char *texts[2] = { "Line in", "Mic in" };

	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;
}

256 257
static int snd_ca0106_capture_mic_line_in_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
258
{
259
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
260 261 262 263 264

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

265 266
static int snd_ca0106_capture_mic_line_in_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
267
{
268
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
269 270 271 272 273 274 275 276 277
	unsigned int val;
	int change = 0;
	u32 tmp;

	val = ucontrol->value.enumerated.item[0] ;
	change = (emu->capture_mic_line_in != val);
	if (change) {
		emu->capture_mic_line_in = val;
		if (val) {
278
			//snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
279 280 281
			tmp = inl(emu->port+GPIO) & ~0x400;
			tmp = tmp | 0x400;
			outl(tmp, emu->port+GPIO);
282
			//snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_MIC);
283
		} else {
284
			//snd_ca0106_i2c_write(emu, ADC_MUX, 0); /* Mute input */
285 286
			tmp = inl(emu->port+GPIO) & ~0x400;
			outl(tmp, emu->port+GPIO);
287
			//snd_ca0106_i2c_write(emu, ADC_MUX, ADC_MUX_LINEIN);
288 289 290 291 292
		}
	}
        return change;
}

293
static struct snd_kcontrol_new snd_ca0106_capture_mic_line_in __devinitdata =
294 295
{
	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
296
	.name =		"Shared Mic/Line in Capture Switch",
297 298 299 300 301
	.info =		snd_ca0106_capture_mic_line_in_info,
	.get =		snd_ca0106_capture_mic_line_in_get,
	.put =		snd_ca0106_capture_mic_line_in_put
};

302 303 304 305 306 307 308 309 310 311
static struct snd_kcontrol_new snd_ca0106_capture_line_in_side_out __devinitdata =
{
	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
	.name =		"Shared Line in/Side out Capture Switch",
	.info =		snd_ca0106_capture_line_in_side_out_info,
	.get =		snd_ca0106_capture_mic_line_in_get,
	.put =		snd_ca0106_capture_mic_line_in_put
};


312 313
static int snd_ca0106_spdif_info(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
314 315 316 317 318 319
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

320 321
static int snd_ca0106_spdif_get(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
322
{
323
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332
	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);

	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;
        return 0;
}

333 334
static int snd_ca0106_spdif_get_mask(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342
{
	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;
}

343 344
static int snd_ca0106_spdif_put(struct snd_kcontrol *kcontrol,
                                 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
345
{
346
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
	int change;
	unsigned int val;

	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);
	change = val != emu->spdif_bits[idx];
	if (change) {
		snd_ca0106_ptr_write(emu, SPCS0 + idx, 0, val);
		emu->spdif_bits[idx] = val;
	}
        return change;
}

363 364
static int snd_ca0106_volume_info(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
365 366 367 368 369 370 371 372
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 2;
        uinfo->value.integer.min = 0;
        uinfo->value.integer.max = 255;
        return 0;
}

373 374
static int snd_ca0106_volume_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
375
{
376
        struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
377
        unsigned int value;
T
Takashi Iwai 已提交
378 379 380 381
	int channel_id, reg;

	channel_id = (kcontrol->private_value >> 8) & 0xff;
	reg = kcontrol->private_value & 0xff;
L
Linus Torvalds 已提交
382 383 384 385 386 387 388

        value = snd_ca0106_ptr_read(emu, reg, channel_id);
        ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
        ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
        return 0;
}

389 390
static int snd_ca0106_volume_put(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
391
{
392
        struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
T
Takashi Iwai 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        unsigned int oval, nval;
	int channel_id, reg;

	channel_id = (kcontrol->private_value >> 8) & 0xff;
	reg = kcontrol->private_value & 0xff;

	oval = snd_ca0106_ptr_read(emu, reg, channel_id);
	nval = ((0xff - ucontrol->value.integer.value[0]) << 24) |
		((0xff - ucontrol->value.integer.value[1]) << 16);
        nval |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
		((0xff - ucontrol->value.integer.value[1]) );
	if (oval == nval)
		return 0;
	snd_ca0106_ptr_write(emu, reg, channel_id, nval);
	return 1;
}

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
static int snd_ca0106_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_ca0106_i2c_volume_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
        struct snd_ca0106 *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_ca0106_i2c_volume_put(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
        struct snd_ca0106 *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_ca0106_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_ca0106_i2c_write(emu, ADC_ATTEN_ADCR, ((ngain) & 0xff));
		emu->i2c_capture_volume[source_id][1] = ucontrol->value.integer.value[1];
		change = 1;
	}

	return change;
}

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 497 498 499 500 501 502
#define spi_mute_info	snd_ctl_boolean_mono_info

static int spi_mute_get(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_value *ucontrol)
{
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int reg = kcontrol->private_value >> SPI_REG_SHIFT;
	unsigned int bit = kcontrol->private_value & SPI_REG_MASK;

	ucontrol->value.integer.value[0] = !(emu->spi_dac_reg[reg] & bit);
	return 0;
}

static int spi_mute_put(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_value *ucontrol)
{
	struct snd_ca0106 *emu = snd_kcontrol_chip(kcontrol);
	unsigned int reg = kcontrol->private_value >> SPI_REG_SHIFT;
	unsigned int bit = kcontrol->private_value & SPI_REG_MASK;
	int ret;

	ret = emu->spi_dac_reg[reg] & bit;
	if (ucontrol->value.integer.value[0]) {
		if (!ret)	/* bit already cleared, do nothing */
			return 0;
		emu->spi_dac_reg[reg] &= ~bit;
	} else {
		if (ret)	/* bit already set, do nothing */
			return 0;
		emu->spi_dac_reg[reg] |= bit;
	}

	ret = snd_ca0106_spi_write(emu, emu->spi_dac_reg[reg]);
	return ret ? -1 : 1;
}

T
Takashi Iwai 已提交
503 504 505
#define CA_VOLUME(xname,chid,reg) \
{								\
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
506 507
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |		\
	          SNDRV_CTL_ELEM_ACCESS_TLV_READ,		\
508 509 510
	.info =	 snd_ca0106_volume_info,			\
	.get =   snd_ca0106_volume_get,				\
	.put =   snd_ca0106_volume_put,				\
511
	.tlv = { .p = snd_ca0106_db_scale1 },			\
T
Takashi Iwai 已提交
512 513 514
	.private_value = ((chid) << 8) | (reg)			\
}

515
static struct snd_kcontrol_new snd_ca0106_volume_ctls[] __devinitdata = {
T
Takashi Iwai 已提交
516 517 518 519 520 521 522 523 524
	CA_VOLUME("Analog Front Playback Volume",
		  CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME2),
        CA_VOLUME("Analog Rear Playback Volume",
		  CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME2),
	CA_VOLUME("Analog Center/LFE Playback Volume",
		  CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME2),
        CA_VOLUME("Analog Side Playback Volume",
		  CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME2),

525
        CA_VOLUME("IEC958 Front Playback Volume",
T
Takashi Iwai 已提交
526
		  CONTROL_FRONT_CHANNEL, PLAYBACK_VOLUME1),
527
	CA_VOLUME("IEC958 Rear Playback Volume",
T
Takashi Iwai 已提交
528
		  CONTROL_REAR_CHANNEL, PLAYBACK_VOLUME1),
529
	CA_VOLUME("IEC958 Center/LFE Playback Volume",
T
Takashi Iwai 已提交
530
		  CONTROL_CENTER_LFE_CHANNEL, PLAYBACK_VOLUME1),
531
	CA_VOLUME("IEC958 Unknown Playback Volume",
T
Takashi Iwai 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
		  CONTROL_UNKNOWN_CHANNEL, PLAYBACK_VOLUME1),

        CA_VOLUME("CAPTURE feedback Playback Volume",
		  1, CAPTURE_CONTROL),

	{
		.access =	SNDRV_CTL_ELEM_ACCESS_READ,
		.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
		.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
		.count =	4,
		.info =         snd_ca0106_spdif_info,
		.get =          snd_ca0106_spdif_get_mask
	},
	{
		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
547
		.name =		"IEC958 Playback Switch",
T
Takashi Iwai 已提交
548 549 550 551 552 553
		.info =		snd_ca0106_shared_spdif_info,
		.get =		snd_ca0106_shared_spdif_get,
		.put =		snd_ca0106_shared_spdif_put
	},
	{
		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
554
		.name =		"Digital Source Capture Enum",
T
Takashi Iwai 已提交
555 556 557 558
		.info =		snd_ca0106_capture_source_info,
		.get =		snd_ca0106_capture_source_get,
		.put =		snd_ca0106_capture_source_put
	},
559 560
	{
		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
561
		.name =		"Analog Source Capture Enum",
562 563 564 565
		.info =		snd_ca0106_i2c_capture_source_info,
		.get =		snd_ca0106_i2c_capture_source_get,
		.put =		snd_ca0106_i2c_capture_source_put
	},
T
Takashi Iwai 已提交
566 567 568 569 570 571 572 573
	{
		.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
		.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
		.count =	4,
		.info =         snd_ca0106_spdif_info,
		.get =          snd_ca0106_spdif_get,
		.put =          snd_ca0106_spdif_put
	},
L
Linus Torvalds 已提交
574 575
};

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
#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_ca0106_i2c_volume_info,			\
	.get =   snd_ca0106_i2c_volume_get,			\
	.put =   snd_ca0106_i2c_volume_put,			\
	.tlv = { .p = snd_ca0106_db_scale2 },			\
	.private_value = chid					\
}

static struct snd_kcontrol_new snd_ca0106_volume_i2c_adc_ctls[] __devinitdata = {
        I2C_VOLUME("Phone Capture Volume", 0),
        I2C_VOLUME("Mic Capture Volume", 1),
        I2C_VOLUME("Line in Capture Volume", 2),
        I2C_VOLUME("Aux Capture Volume", 3),
};

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
#define SPI_SWITCH(xname,reg,bit) \
{								\
	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,		\
	.info	= spi_mute_info,				\
	.get	= spi_mute_get,					\
	.put	= spi_mute_put,					\
	.private_value = (reg<<SPI_REG_SHIFT) | (1<<bit)	\
}

static struct snd_kcontrol_new snd_ca0106_volume_spi_dac_ctls[]
__devinitdata = {
	SPI_SWITCH("Analog Front Playback Switch",
		   SPI_DMUTE4_REG, SPI_DMUTE4_BIT),
	SPI_SWITCH("Analog Rear Playback Switch",
		   SPI_DMUTE0_REG, SPI_DMUTE0_BIT),
	SPI_SWITCH("Analog Center/LFE Playback Switch",
		   SPI_DMUTE2_REG, SPI_DMUTE2_BIT),
	SPI_SWITCH("Analog Side Playback Switch",
		   SPI_DMUTE1_REG, SPI_DMUTE1_BIT),
};

617
static int __devinit remove_ctl(struct snd_card *card, const char *name)
L
Linus Torvalds 已提交
618
{
619
	struct snd_ctl_elem_id id;
L
Linus Torvalds 已提交
620 621 622 623 624 625
	memset(&id, 0, sizeof(id));
	strcpy(id.name, name);
	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	return snd_ctl_remove_id(card, &id);
}

626
static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card, const char *name)
L
Linus Torvalds 已提交
627
{
628
	struct snd_ctl_elem_id sid;
L
Linus Torvalds 已提交
629 630 631 632 633 634 635
	memset(&sid, 0, sizeof(sid));
	/* FIXME: strcpy is bad. */
	strcpy(sid.name, name);
	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	return snd_ctl_find_id(card, &sid);
}

636
static int __devinit rename_ctl(struct snd_card *card, const char *src, const char *dst)
L
Linus Torvalds 已提交
637
{
638
	struct snd_kcontrol *kctl = ctl_find(card, src);
L
Linus Torvalds 已提交
639 640 641 642 643 644 645
	if (kctl) {
		strcpy(kctl->id.name, dst);
		return 0;
	}
	return -ENOENT;
}

646
int __devinit snd_ca0106_mixer(struct snd_ca0106 *emu)
L
Linus Torvalds 已提交
647
{
T
Takashi Iwai 已提交
648
	int i, err;
649
        struct snd_card *card = emu->card;
L
Linus Torvalds 已提交
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
	char **c;
	static char *ca0106_remove_ctls[] = {
		"Master Mono Playback Switch",
		"Master Mono Playback Volume",
		"3D Control - Switch",
		"3D Control Sigmatel - Depth",
		"PCM Playback Switch",
		"PCM Playback Volume",
		"CD Playback Switch",
		"CD Playback Volume",
		"Phone Playback Switch",
		"Phone Playback Volume",
		"Video Playback Switch",
		"Video Playback Volume",
		"PC Speaker Playback Switch",
		"PC Speaker Playback Volume",
		"Mono Output Select",
		"Capture Source",
		"Capture Switch",
		"Capture Volume",
		"External Amplifier",
		"Sigmatel 4-Speaker Stereo Playback Switch",
		"Sigmatel Surround Phase Inversion Playback ",
		NULL
	};
	static char *ca0106_rename_ctls[] = {
		"Master Playback Switch", "Capture Switch",
		"Master Playback Volume", "Capture Volume",
		"Line Playback Switch", "AC97 Line Capture Switch",
		"Line Playback Volume", "AC97 Line Capture Volume",
		"Aux Playback Switch", "AC97 Aux Capture Switch",
		"Aux Playback Volume", "AC97 Aux Capture Volume",
		"Mic Playback Switch", "AC97 Mic Capture Switch",
		"Mic Playback Volume", "AC97 Mic Capture Volume",
		"Mic Select", "AC97 Mic Select",
		"Mic Boost (+20dB)", "AC97 Mic Boost (+20dB)",
		NULL
	};
#if 1
T
Takashi Iwai 已提交
689
	for (c = ca0106_remove_ctls; *c; c++)
L
Linus Torvalds 已提交
690
		remove_ctl(card, *c);
T
Takashi Iwai 已提交
691
	for (c = ca0106_rename_ctls; *c; c += 2)
L
Linus Torvalds 已提交
692 693 694
		rename_ctl(card, c[0], c[1]);
#endif

T
Takashi Iwai 已提交
695 696 697 698 699
	for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_ctls); i++) {
		err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_ctls[i], emu));
		if (err < 0)
			return err;
	}
700
	if (emu->details->i2c_adc == 1) {
701 702 703 704 705
		for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_i2c_adc_ctls); i++) {
			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_i2c_adc_ctls[i], emu));
			if (err < 0)
				return err;
		}
706 707 708 709
		if (emu->details->gpio_type == 1)
			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_mic_line_in, emu));
		else  /* gpio_type == 2 */
			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_capture_line_in_side_out, emu));
T
Takashi Iwai 已提交
710
		if (err < 0)
711 712
			return err;
	}
713 714 715 716 717 718 719
	if (emu->details->spi_dac == 1) {
		for (i = 0; i < ARRAY_SIZE(snd_ca0106_volume_spi_dac_ctls); i++) {
			err = snd_ctl_add(card, snd_ctl_new1(&snd_ca0106_volume_spi_dac_ctls[i], emu));
			if (err < 0)
				return err;
		}
	}
L
Linus Torvalds 已提交
720 721 722
        return 0;
}