ac97_codec.c 91.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 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
/*
 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
 *  Universal interface for Audio Codec '97
 *
 *  For more details look to AC '97 component specification revision 2.2
 *  by Intel Corporation (http://developer.intel.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/slab.h>
#include <linux/pci.h>
#include <linux/moduleparam.h>
31
#include <linux/mutex.h>
L
Linus Torvalds 已提交
32 33
#include <sound/core.h>
#include <sound/pcm.h>
34
#include <sound/tlv.h>
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <sound/ac97_codec.h>
#include <sound/asoundef.h>
#include <sound/initval.h>
#include "ac97_local.h"
#include "ac97_id.h"
#include "ac97_patch.h"

MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
MODULE_DESCRIPTION("Universal interface for Audio Codec '97");
MODULE_LICENSE("GPL");

static int enable_loopback;

module_param(enable_loopback, bool, 0444);
MODULE_PARM_DESC(enable_loopback, "Enable AC97 ADC/DAC Loopback Control");

51 52 53 54 55
#ifdef CONFIG_SND_AC97_POWER_SAVE
static int power_save;
module_param(power_save, bool, 0644);
MODULE_PARM_DESC(power_save, "Enable AC97 power-saving control");
#endif
L
Linus Torvalds 已提交
56 57 58 59
/*

 */

60
struct ac97_codec_id {
L
Linus Torvalds 已提交
61 62 63
	unsigned int id;
	unsigned int mask;
	const char *name;
64 65
	int (*patch)(struct snd_ac97 *ac97);
	int (*mpatch)(struct snd_ac97 *ac97);
L
Linus Torvalds 已提交
66
	unsigned int flags;
67
};
L
Linus Torvalds 已提交
68

69
static const struct ac97_codec_id snd_ac97_codec_id_vendors[] = {
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{ 0x414b4d00, 0xffffff00, "Asahi Kasei",	NULL,	NULL },
{ 0x41445300, 0xffffff00, "Analog Devices",	NULL,	NULL },
{ 0x414c4300, 0xffffff00, "Realtek",		NULL,	NULL },
{ 0x414c4700, 0xffffff00, "Realtek",		NULL,	NULL },
{ 0x434d4900, 0xffffff00, "C-Media Electronics", NULL,	NULL },
{ 0x43525900, 0xffffff00, "Cirrus Logic",	NULL,	NULL },
{ 0x43585400, 0xffffff00, "Conexant",           NULL,	NULL },
{ 0x44543000, 0xffffff00, "Diamond Technology", NULL,	NULL },
{ 0x454d4300, 0xffffff00, "eMicro",		NULL,	NULL },
{ 0x45838300, 0xffffff00, "ESS Technology",	NULL,	NULL },
{ 0x48525300, 0xffffff00, "Intersil",		NULL,	NULL },
{ 0x49434500, 0xffffff00, "ICEnsemble",		NULL,	NULL },
{ 0x49544500, 0xffffff00, "ITE Tech.Inc",	NULL,	NULL },
{ 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL },
{ 0x50534300, 0xffffff00, "Philips",		NULL,	NULL },
{ 0x53494c00, 0xffffff00, "Silicon Laboratory",	NULL,	NULL },
{ 0x54524100, 0xffffff00, "TriTech",		NULL,	NULL },
{ 0x54584e00, 0xffffff00, "Texas Instruments",	NULL,	NULL },
{ 0x56494100, 0xffffff00, "VIA Technologies",   NULL,	NULL },
{ 0x57454300, 0xffffff00, "Winbond",		NULL,	NULL },
{ 0x574d4c00, 0xffffff00, "Wolfson",		NULL,	NULL },
{ 0x594d4800, 0xffffff00, "Yamaha",		NULL,	NULL },
{ 0x83847600, 0xffffff00, "SigmaTel",		NULL,	NULL },
{ 0,	      0, 	  NULL,			NULL,	NULL }
};

96
static const struct ac97_codec_id snd_ac97_codec_ids[] = {
L
Linus Torvalds 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{ 0x414b4d00, 0xffffffff, "AK4540",		NULL,		NULL },
{ 0x414b4d01, 0xffffffff, "AK4542",		NULL,		NULL },
{ 0x414b4d02, 0xffffffff, "AK4543",		NULL,		NULL },
{ 0x414b4d06, 0xffffffff, "AK4544A",		NULL,		NULL },
{ 0x414b4d07, 0xffffffff, "AK4545",		NULL,		NULL },
{ 0x41445303, 0xffffffff, "AD1819",		patch_ad1819,	NULL },
{ 0x41445340, 0xffffffff, "AD1881",		patch_ad1881,	NULL },
{ 0x41445348, 0xffffffff, "AD1881A",		patch_ad1881,	NULL },
{ 0x41445360, 0xffffffff, "AD1885",		patch_ad1885,	NULL },
{ 0x41445361, 0xffffffff, "AD1886",		patch_ad1886,	NULL },
{ 0x41445362, 0xffffffff, "AD1887",		patch_ad1881,	NULL },
{ 0x41445363, 0xffffffff, "AD1886A",		patch_ad1881,	NULL },
{ 0x41445368, 0xffffffff, "AD1888",		patch_ad1888,	NULL },
{ 0x41445370, 0xffffffff, "AD1980",		patch_ad1980,	NULL },
{ 0x41445372, 0xffffffff, "AD1981A",		patch_ad1981a,	NULL },
{ 0x41445374, 0xffffffff, "AD1981B",		patch_ad1981b,	NULL },
{ 0x41445375, 0xffffffff, "AD1985",		patch_ad1985,	NULL },
114
{ 0x41445378, 0xffffffff, "AD1986",		patch_ad1986,	NULL },
L
Linus Torvalds 已提交
115 116 117 118 119 120 121
{ 0x414c4300, 0xffffff00, "ALC100,100P", 	NULL,		NULL },
{ 0x414c4710, 0xfffffff0, "ALC200,200P",	NULL,		NULL },
{ 0x414c4721, 0xffffffff, "ALC650D",		NULL,	NULL }, /* already patched */
{ 0x414c4722, 0xffffffff, "ALC650E",		NULL,	NULL }, /* already patched */
{ 0x414c4723, 0xffffffff, "ALC650F",		NULL,	NULL }, /* already patched */
{ 0x414c4720, 0xfffffff0, "ALC650",		patch_alc650,	NULL },
{ 0x414c4760, 0xfffffff0, "ALC655",		patch_alc655,	NULL },
T
Takashi Iwai 已提交
122
{ 0x414c4781, 0xffffffff, "ALC658D",		NULL,	NULL }, /* already patched */
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130
{ 0x414c4780, 0xfffffff0, "ALC658",		patch_alc655,	NULL },
{ 0x414c4790, 0xfffffff0, "ALC850",		patch_alc850,	NULL },
{ 0x414c4730, 0xffffffff, "ALC101",		NULL,		NULL },
{ 0x414c4740, 0xfffffff0, "ALC202",		NULL,		NULL },
{ 0x414c4750, 0xfffffff0, "ALC250",		NULL,		NULL },
{ 0x414c4770, 0xfffffff0, "ALC203",		NULL,		NULL },
{ 0x434d4941, 0xffffffff, "CMI9738",		patch_cm9738,	NULL },
{ 0x434d4961, 0xffffffff, "CMI9739",		patch_cm9739,	NULL },
131
{ 0x434d4969, 0xffffffff, "CMI9780",		patch_cm9780,	NULL },
132 133 134
{ 0x434d4978, 0xffffffff, "CMI9761A",		patch_cm9761,	NULL },
{ 0x434d4982, 0xffffffff, "CMI9761B",		patch_cm9761,	NULL },
{ 0x434d4983, 0xffffffff, "CMI9761A+",		patch_cm9761,	NULL },
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146
{ 0x43525900, 0xfffffff8, "CS4297",		NULL,		NULL },
{ 0x43525910, 0xfffffff8, "CS4297A",		patch_cirrus_spdif,	NULL },
{ 0x43525920, 0xfffffff8, "CS4298",		patch_cirrus_spdif,		NULL },
{ 0x43525928, 0xfffffff8, "CS4294",		NULL,		NULL },
{ 0x43525930, 0xfffffff8, "CS4299",		patch_cirrus_cs4299,	NULL },
{ 0x43525948, 0xfffffff8, "CS4201",		NULL,		NULL },
{ 0x43525958, 0xfffffff8, "CS4205",		patch_cirrus_spdif,	NULL },
{ 0x43525960, 0xfffffff8, "CS4291",		NULL,		NULL },
{ 0x43525970, 0xfffffff8, "CS4202",		NULL,		NULL },
{ 0x43585421, 0xffffffff, "HSD11246",		NULL,		NULL },	// SmartMC II
{ 0x43585428, 0xfffffff8, "Cx20468",		patch_conexant,	NULL }, // SmartAMC fixme: the mask might be different
{ 0x44543031, 0xfffffff0, "DT0398",		NULL,		NULL },
147
{ 0x454d4328, 0xffffffff, "EM28028",		NULL,		NULL },  // same as TR28028?
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158
{ 0x45838308, 0xffffffff, "ESS1988",		NULL,		NULL },
{ 0x48525300, 0xffffff00, "HMP9701",		NULL,		NULL },
{ 0x49434501, 0xffffffff, "ICE1230",		NULL,		NULL },
{ 0x49434511, 0xffffffff, "ICE1232",		NULL,		NULL }, // alias VIA VT1611A?
{ 0x49434514, 0xffffffff, "ICE1232A",		NULL,		NULL },
{ 0x49434551, 0xffffffff, "VT1616", 		patch_vt1616,	NULL }, 
{ 0x49434552, 0xffffffff, "VT1616i",		patch_vt1616,	NULL }, // VT1616 compatible (chipset integrated)
{ 0x49544520, 0xffffffff, "IT2226E",		NULL,		NULL },
{ 0x49544561, 0xffffffff, "IT2646E",		patch_it2646,	NULL },
{ 0x4e534300, 0xffffffff, "LM4540,43,45,46,48",	NULL,		NULL }, // only guess --jk
{ 0x4e534331, 0xffffffff, "LM4549",		NULL,		NULL },
159
{ 0x4e534350, 0xffffffff, "LM4550",		patch_lm4550,  	NULL }, // volume wrap fix 
160
{ 0x50534304, 0xffffffff, "UCB1400",		patch_ucb1400,	NULL },
161
{ 0x53494c20, 0xffffffe0, "Si3036,8",		mpatch_si3036,	mpatch_si3036, AC97_MODEM_PATCH },
L
Linus Torvalds 已提交
162 163 164 165 166 167
{ 0x54524102, 0xffffffff, "TR28022",		NULL,		NULL },
{ 0x54524106, 0xffffffff, "TR28026",		NULL,		NULL },
{ 0x54524108, 0xffffffff, "TR28028",		patch_tritech_tr28028,	NULL }, // added by xin jin [07/09/99]
{ 0x54524123, 0xffffffff, "TR28602",		NULL,		NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
{ 0x54584e20, 0xffffffff, "TLC320AD9xC",	NULL,		NULL },
{ 0x56494161, 0xffffffff, "VIA1612A",		NULL,		NULL }, // modified ICE1232 with S/PDIF
168
{ 0x56494170, 0xffffffff, "VIA1617A",		patch_vt1617a,	NULL }, // modified VT1616 with S/PDIF
169
{ 0x56494182, 0xffffffff, "VIA1618",		NULL,		NULL },
L
Linus Torvalds 已提交
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
{ 0x57454301, 0xffffffff, "W83971D",		NULL,		NULL },
{ 0x574d4c00, 0xffffffff, "WM9701A",		NULL,		NULL },
{ 0x574d4C03, 0xffffffff, "WM9703,WM9707,WM9708,WM9717", patch_wolfson03, NULL},
{ 0x574d4C04, 0xffffffff, "WM9704M,WM9704Q",	patch_wolfson04, NULL},
{ 0x574d4C05, 0xffffffff, "WM9705,WM9710",	patch_wolfson05, NULL},
{ 0x574d4C09, 0xffffffff, "WM9709",		NULL,		NULL},
{ 0x574d4C12, 0xffffffff, "WM9711,WM9712",	patch_wolfson11, NULL},
{ 0x574d4c13, 0xffffffff, "WM9713,WM9714",	patch_wolfson13, NULL, AC97_DEFAULT_POWER_OFF},
{ 0x594d4800, 0xffffffff, "YMF743",		NULL,		NULL },
{ 0x594d4802, 0xffffffff, "YMF752",		NULL,		NULL },
{ 0x594d4803, 0xffffffff, "YMF753",		patch_yamaha_ymf753,	NULL },
{ 0x83847600, 0xffffffff, "STAC9700,83,84",	patch_sigmatel_stac9700,	NULL },
{ 0x83847604, 0xffffffff, "STAC9701,3,4,5",	NULL,		NULL },
{ 0x83847605, 0xffffffff, "STAC9704",		NULL,		NULL },
{ 0x83847608, 0xffffffff, "STAC9708,11",	patch_sigmatel_stac9708,	NULL },
{ 0x83847609, 0xffffffff, "STAC9721,23",	patch_sigmatel_stac9721,	NULL },
{ 0x83847644, 0xffffffff, "STAC9744",		patch_sigmatel_stac9744,	NULL },
{ 0x83847650, 0xffffffff, "STAC9750,51",	NULL,		NULL },	// patch?
{ 0x83847652, 0xffffffff, "STAC9752,53",	NULL,		NULL }, // patch?
{ 0x83847656, 0xffffffff, "STAC9756,57",	patch_sigmatel_stac9756,	NULL },
{ 0x83847658, 0xffffffff, "STAC9758,59",	patch_sigmatel_stac9758,	NULL },
{ 0x83847666, 0xffffffff, "STAC9766,67",	NULL,		NULL }, // patch?
{ 0, 	      0,	  NULL,			NULL,		NULL }
};


196
static void update_power_regs(struct snd_ac97 *ac97);
197 198 199 200 201 202 203
#ifdef CONFIG_SND_AC97_POWER_SAVE
#define ac97_is_power_save_mode(ac97) \
	((ac97->scaps & AC97_SCAP_POWER_SAVE) && power_save)
#else
#define ac97_is_power_save_mode(ac97) 0
#endif

204

L
Linus Torvalds 已提交
205 206 207 208
/*
 *  I/O routines
 */

209
static int snd_ac97_valid_reg(struct snd_ac97 *ac97, unsigned short reg)
L
Linus Torvalds 已提交
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
{
	/* filter some registers for buggy codecs */
	switch (ac97->id) {
	case AC97_ID_AK4540:
	case AC97_ID_AK4542:
		if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
			return 1;
		return 0;
	case AC97_ID_AD1819:	/* AD1819 */
	case AC97_ID_AD1881:	/* AD1881 */
	case AC97_ID_AD1881A:	/* AD1881A */
		if (reg >= 0x3a && reg <= 0x6e)	/* 0x59 */
			return 0;
		return 1;
	case AC97_ID_AD1885:	/* AD1885 */
	case AC97_ID_AD1886:	/* AD1886 */
	case AC97_ID_AD1886A:	/* AD1886A - !!verify!! --jk */
	case AC97_ID_AD1887:	/* AD1887 - !!verify!! --jk */
		if (reg == 0x5a)
			return 1;
		if (reg >= 0x3c && reg <= 0x6e)	/* 0x59 */
			return 0;
		return 1;
	case AC97_ID_STAC9700:
	case AC97_ID_STAC9704:
	case AC97_ID_STAC9705:
	case AC97_ID_STAC9708:
	case AC97_ID_STAC9721:
	case AC97_ID_STAC9744:
	case AC97_ID_STAC9756:
		if (reg <= 0x3a || reg >= 0x5a)
			return 1;
		return 0;
	}
	return 1;
}

/**
 * snd_ac97_write - write a value on the given register
 * @ac97: the ac97 instance
 * @reg: the register to change
 * @value: the value to set
 *
 * Writes a value on the given register.  This will invoke the write
 * callback directly after the register check.
 * This function doesn't change the register cache unlike
 * #snd_ca97_write_cache(), so use this only when you don't want to
 * reflect the change to the suspend/resume state.
 */
259
void snd_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269 270
{
	if (!snd_ac97_valid_reg(ac97, reg))
		return;
	if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) {
		/* Fix H/W bug of ALC100/100P */
		if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
			ac97->bus->ops->write(ac97, AC97_RESET, 0);	/* reset audio codec */
	}
	ac97->bus->ops->write(ac97, reg, value);
}

271 272
EXPORT_SYMBOL(snd_ac97_write);

L
Linus Torvalds 已提交
273 274 275 276 277 278 279 280 281 282 283
/**
 * snd_ac97_read - read a value from the given register
 * 
 * @ac97: the ac97 instance
 * @reg: the register to read
 *
 * Reads a value from the given register.  This will invoke the read
 * callback directly after the register check.
 *
 * Returns the read value.
 */
284
unsigned short snd_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
L
Linus Torvalds 已提交
285 286 287 288 289 290 291
{
	if (!snd_ac97_valid_reg(ac97, reg))
		return 0;
	return ac97->bus->ops->read(ac97, reg);
}

/* read a register - return the cached value if already read */
292
static inline unsigned short snd_ac97_read_cache(struct snd_ac97 *ac97, unsigned short reg)
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300
{
	if (! test_bit(reg, ac97->reg_accessed)) {
		ac97->regs[reg] = ac97->bus->ops->read(ac97, reg);
		// set_bit(reg, ac97->reg_accessed);
	}
	return ac97->regs[reg];
}

301 302
EXPORT_SYMBOL(snd_ac97_read);

L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312
/**
 * snd_ac97_write_cache - write a value on the given register and update the cache
 * @ac97: the ac97 instance
 * @reg: the register to change
 * @value: the value to set
 *
 * Writes a value on the given register and updates the register
 * cache.  The cached values are used for the cached-read and the
 * suspend/resume.
 */
313
void snd_ac97_write_cache(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
L
Linus Torvalds 已提交
314 315 316
{
	if (!snd_ac97_valid_reg(ac97, reg))
		return;
317
	mutex_lock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
318 319 320
	ac97->regs[reg] = value;
	ac97->bus->ops->write(ac97, reg, value);
	set_bit(reg, ac97->reg_accessed);
321
	mutex_unlock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
322 323
}

324 325
EXPORT_SYMBOL(snd_ac97_write_cache);

L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334 335 336 337
/**
 * snd_ac97_update - update the value on the given register
 * @ac97: the ac97 instance
 * @reg: the register to change
 * @value: the value to set
 *
 * Compares the value with the register cache and updates the value
 * only when the value is changed.
 *
 * Returns 1 if the value is changed, 0 if no change, or a negative
 * code on failure.
 */
338
int snd_ac97_update(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
L
Linus Torvalds 已提交
339 340 341 342 343
{
	int change;

	if (!snd_ac97_valid_reg(ac97, reg))
		return -EINVAL;
344
	mutex_lock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
345 346 347 348 349
	change = ac97->regs[reg] != value;
	if (change) {
		ac97->regs[reg] = value;
		ac97->bus->ops->write(ac97, reg, value);
	}
T
Takashi Iwai 已提交
350
	set_bit(reg, ac97->reg_accessed);
351
	mutex_unlock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
352 353 354
	return change;
}

355 356
EXPORT_SYMBOL(snd_ac97_update);

L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369
/**
 * snd_ac97_update_bits - update the bits on the given register
 * @ac97: the ac97 instance
 * @reg: the register to change
 * @mask: the bit-mask to change
 * @value: the value to set
 *
 * Updates the masked-bits on the given register only when the value
 * is changed.
 *
 * Returns 1 if the bits are changed, 0 if no change, or a negative
 * code on failure.
 */
370
int snd_ac97_update_bits(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value)
L
Linus Torvalds 已提交
371 372 373 374 375
{
	int change;

	if (!snd_ac97_valid_reg(ac97, reg))
		return -EINVAL;
376
	mutex_lock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
377
	change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
378
	mutex_unlock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
379 380 381
	return change;
}

382 383
EXPORT_SYMBOL(snd_ac97_update_bits);

L
Linus Torvalds 已提交
384
/* no lock version - see snd_ac97_updat_bits() */
385
int snd_ac97_update_bits_nolock(struct snd_ac97 *ac97, unsigned short reg,
L
Linus Torvalds 已提交
386 387 388 389 390 391
				unsigned short mask, unsigned short value)
{
	int change;
	unsigned short old, new;

	old = snd_ac97_read_cache(ac97, reg);
392
	new = (old & ~mask) | (value & mask);
L
Linus Torvalds 已提交
393 394 395 396 397
	change = old != new;
	if (change) {
		ac97->regs[reg] = new;
		ac97->bus->ops->write(ac97, reg, new);
	}
T
Takashi Iwai 已提交
398
	set_bit(reg, ac97->reg_accessed);
L
Linus Torvalds 已提交
399 400 401
	return change;
}

402
static int snd_ac97_ad18xx_update_pcm_bits(struct snd_ac97 *ac97, int codec, unsigned short mask, unsigned short value)
L
Linus Torvalds 已提交
403 404 405 406
{
	int change;
	unsigned short old, new, cfg;

407
	mutex_lock(&ac97->page_mutex);
L
Linus Torvalds 已提交
408
	old = ac97->spec.ad18xx.pcmreg[codec];
409
	new = (old & ~mask) | (value & mask);
L
Linus Torvalds 已提交
410 411
	change = old != new;
	if (change) {
412
		mutex_lock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421 422 423
		cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
		ac97->spec.ad18xx.pcmreg[codec] = new;
		/* select single codec */
		ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
				 (cfg & ~0x7000) |
				 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
		/* update PCM bits */
		ac97->bus->ops->write(ac97, AC97_PCM, new);
		/* select all codecs */
		ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
				 cfg | 0x7000);
424
		mutex_unlock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
425
	}
426
	mutex_unlock(&ac97->page_mutex);
L
Linus Torvalds 已提交
427 428 429 430 431 432 433
	return change;
}

/*
 * Controls
 */

434
int snd_ac97_info_enum_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447
{
	struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
	
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
	uinfo->value.enumerated.items = e->mask;
	
	if (uinfo->value.enumerated.item > e->mask - 1)
		uinfo->value.enumerated.item = e->mask - 1;
	strcpy(uinfo->value.enumerated.name, e->texts[uinfo->value.enumerated.item]);
	return 0;
}

448
int snd_ac97_get_enum_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
449
{
450
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
451
	struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
452
	unsigned short val, bitmask;
L
Linus Torvalds 已提交
453
	
454
	for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
455
		;
L
Linus Torvalds 已提交
456
	val = snd_ac97_read_cache(ac97, e->reg);
457
	ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
L
Linus Torvalds 已提交
458
	if (e->shift_l != e->shift_r)
459
		ucontrol->value.enumerated.item[1] = (val >> e->shift_r) & (bitmask - 1);
L
Linus Torvalds 已提交
460 461 462 463

	return 0;
}

464
int snd_ac97_put_enum_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
465
{
466
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
467 468
	struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
	unsigned short val;
469
	unsigned short mask, bitmask;
L
Linus Torvalds 已提交
470
	
471
	for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
472
		;
L
Linus Torvalds 已提交
473 474 475
	if (ucontrol->value.enumerated.item[0] > e->mask - 1)
		return -EINVAL;
	val = ucontrol->value.enumerated.item[0] << e->shift_l;
476
	mask = (bitmask - 1) << e->shift_l;
L
Linus Torvalds 已提交
477 478 479 480
	if (e->shift_l != e->shift_r) {
		if (ucontrol->value.enumerated.item[1] > e->mask - 1)
			return -EINVAL;
		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
481
		mask |= (bitmask - 1) << e->shift_r;
L
Linus Torvalds 已提交
482 483 484 485 486
	}
	return snd_ac97_update_bits(ac97, e->reg, mask, val);
}

/* save/restore ac97 v2.3 paging */
487
static int snd_ac97_page_save(struct snd_ac97 *ac97, int reg, struct snd_kcontrol *kcontrol)
L
Linus Torvalds 已提交
488 489 490 491 492 493
{
	int page_save = -1;
	if ((kcontrol->private_value & (1<<25)) &&
	    (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 &&
	    (reg >= 0x60 && reg < 0x70)) {
		unsigned short page = (kcontrol->private_value >> 26) & 0x0f;
494
		mutex_lock(&ac97->page_mutex); /* lock paging */
L
Linus Torvalds 已提交
495 496 497 498 499 500
		page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
		snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
	}
	return page_save;
}

501
static void snd_ac97_page_restore(struct snd_ac97 *ac97, int page_save)
L
Linus Torvalds 已提交
502 503 504
{
	if (page_save >= 0) {
		snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
505
		mutex_unlock(&ac97->page_mutex); /* unlock paging */
L
Linus Torvalds 已提交
506 507 508 509
	}
}

/* volume and switch controls */
510
int snd_ac97_info_volsw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
511 512 513 514 515 516 517 518 519 520 521 522
{
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0x0f;
	int rshift = (kcontrol->private_value >> 12) & 0x0f;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = shift == rshift ? 1 : 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}

523
int snd_ac97_get_volsw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
524
{
525
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0x0f;
	int rshift = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0x01;
	int page_save;

	page_save = snd_ac97_page_save(ac97, reg, kcontrol);
	ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
	if (shift != rshift)
		ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask;
	if (invert) {
		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
		if (shift != rshift)
			ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
	}
	snd_ac97_page_restore(ac97, page_save);
	return 0;
}

546
int snd_ac97_put_volsw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
547
{
548
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0x0f;
	int rshift = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0x01;
	int err, page_save;
	unsigned short val, val2, val_mask;
	
	page_save = snd_ac97_page_save(ac97, reg, kcontrol);
	val = (ucontrol->value.integer.value[0] & mask);
	if (invert)
		val = mask - val;
	val_mask = mask << shift;
	val = val << shift;
	if (shift != rshift) {
		val2 = (ucontrol->value.integer.value[1] & mask);
		if (invert)
			val2 = mask - val2;
		val_mask |= mask << rshift;
		val |= val2 << rshift;
	}
	err = snd_ac97_update_bits(ac97, reg, val_mask, val);
	snd_ac97_page_restore(ac97, page_save);
572 573 574 575 576 577 578 579
#ifdef CONFIG_SND_AC97_POWER_SAVE
	/* check analog mixer power-down */
	if ((val_mask & 0x8000) &&
	    (kcontrol->private_value & (1<<30))) {
		if (val & 0x8000)
			ac97->power_up &= ~(1 << (reg>>1));
		else
			ac97->power_up |= 1 << (reg>>1);
T
Takashi Iwai 已提交
580
		update_power_regs(ac97);
581 582
	}
#endif
L
Linus Torvalds 已提交
583 584 585
	return err;
}

586
static const struct snd_kcontrol_new snd_ac97_controls_master_mono[2] = {
L
Linus Torvalds 已提交
587 588 589 590
AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
};

591
static const struct snd_kcontrol_new snd_ac97_controls_tone[2] = {
L
Linus Torvalds 已提交
592 593 594 595
AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
};

596
static const struct snd_kcontrol_new snd_ac97_controls_pc_beep[2] = {
L
Linus Torvalds 已提交
597 598 599 600
AC97_SINGLE("PC Speaker Playback Switch", AC97_PC_BEEP, 15, 1, 1),
AC97_SINGLE("PC Speaker Playback Volume", AC97_PC_BEEP, 1, 15, 1)
};

601
static const struct snd_kcontrol_new snd_ac97_controls_mic_boost =
602
	AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0);
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616


static const char* std_rec_sel[] = {"Mic", "CD", "Video", "Aux", "Line", "Mix", "Mix Mono", "Phone"};
static const char* std_3d_path[] = {"pre 3D", "post 3D"};
static const char* std_mix[] = {"Mix", "Mic"};
static const char* std_mic[] = {"Mic1", "Mic2"};

static const struct ac97_enum std_enum[] = {
AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, std_rec_sel),
AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, std_3d_path),
AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, std_mix),
AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, std_mic),
};

617
static const struct snd_kcontrol_new snd_ac97_control_capture_src = 
L
Linus Torvalds 已提交
618 619
AC97_ENUM("Capture Source", std_enum[0]); 

620
static const struct snd_kcontrol_new snd_ac97_control_capture_vol =
L
Linus Torvalds 已提交
621 622
AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);

623
static const struct snd_kcontrol_new snd_ac97_controls_mic_capture[2] = {
L
Linus Torvalds 已提交
624 625 626 627
AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
};

628
enum {
L
Linus Torvalds 已提交
629 630 631 632 633 634 635
	AC97_GENERAL_PCM_OUT = 0,
	AC97_GENERAL_STEREO_ENHANCEMENT,
	AC97_GENERAL_3D,
	AC97_GENERAL_LOUDNESS,
	AC97_GENERAL_MONO,
	AC97_GENERAL_MIC,
	AC97_GENERAL_LOOPBACK
636
};
L
Linus Torvalds 已提交
637

638
static const struct snd_kcontrol_new snd_ac97_controls_general[7] = {
L
Linus Torvalds 已提交
639 640 641 642 643
AC97_ENUM("PCM Out Path & Mute", std_enum[1]),
AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
AC97_ENUM("Mono Output Select", std_enum[2]),
644
AC97_ENUM("Mic Select", std_enum[3]),
L
Linus Torvalds 已提交
645 646 647
AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
};

648
const struct snd_kcontrol_new snd_ac97_controls_3d[2] = {
L
Linus Torvalds 已提交
649 650 651 652
AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
};

653
static const struct snd_kcontrol_new snd_ac97_controls_center[2] = {
L
Linus Torvalds 已提交
654 655 656 657
AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
};

658
static const struct snd_kcontrol_new snd_ac97_controls_lfe[2] = {
L
Linus Torvalds 已提交
659 660 661 662
AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
};

663
static const struct snd_kcontrol_new snd_ac97_control_eapd =
L
Linus Torvalds 已提交
664 665
AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);

666
static const struct snd_kcontrol_new snd_ac97_controls_modem_switches[2] = {
667 668 669 670
AC97_SINGLE("Off-hook Switch", AC97_GPIO_STATUS, 0, 1, 0),
AC97_SINGLE("Caller ID Switch", AC97_GPIO_STATUS, 2, 1, 0)
};

L
Linus Torvalds 已提交
671
/* change the existing EAPD control as inverted */
672
static void set_inv_eapd(struct snd_ac97 *ac97, struct snd_kcontrol *kctl)
L
Linus Torvalds 已提交
673 674 675 676 677 678
{
	kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0);
	snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */
	ac97->scaps |= AC97_SCAP_INV_EAPD;
}

679
static int snd_ac97_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
680 681 682 683 684 685
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}
                        
686
static int snd_ac97_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
687 688 689 690 691 692 693 694 695 696 697
{
	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
					   IEC958_AES0_NONAUDIO |
					   IEC958_AES0_CON_EMPHASIS_5015 |
					   IEC958_AES0_CON_NOT_COPYRIGHT;
	ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
					   IEC958_AES1_CON_ORIGINAL;
	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
	return 0;
}
                        
698
static int snd_ac97_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
699 700 701 702 703 704 705 706 707
{
	/* FIXME: AC'97 spec doesn't say which bits are used for what */
	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
					   IEC958_AES0_NONAUDIO |
					   IEC958_AES0_PRO_FS |
					   IEC958_AES0_PRO_EMPHASIS_5015;
	return 0;
}

708
static int snd_ac97_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
709
{
710
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
711

712
	mutex_lock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
713 714 715 716
	ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
	ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
	ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
	ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
717
	mutex_unlock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
718 719 720
	return 0;
}
                        
721
static int snd_ac97_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
722
{
723
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
	unsigned int new = 0;
	unsigned short val = 0;
	int change;

	new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
	if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
		new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
		switch (new & IEC958_AES0_PRO_FS) {
		case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
		case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
		case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
		default:		       val |= 1<<12; break;
		}
		if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
			val |= 1<<3;
	} else {
		new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
		new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
		new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
		if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
			val |= 1<<3;
		if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
			val |= 1<<2;
		val |= ((new >> 8) & 0xff) << 4;	// category + original
		switch ((new >> 24) & 0xff) {
		case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
		case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
		case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
		default:		       val |= 1<<12; break;
		}
	}

756
	mutex_lock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	change = ac97->spdif_status != new;
	ac97->spdif_status = new;

	if (ac97->flags & AC97_CS_SPDIF) {
		int x = (val >> 12) & 0x03;
		switch (x) {
		case 0: x = 1; break;  // 44.1
		case 2: x = 0; break;  // 48.0
		default: x = 0; break; // illegal.
		}
		change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
	} else if (ac97->flags & AC97_CX_SPDIF) {
		int v;
		v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
		v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
		change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC, 
						      AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
						      v);
	} else {
		unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
		snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */

		change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val);
		if (extst & AC97_EA_SPDIF) {
			snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
                }
	}
784
	mutex_unlock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
785 786 787 788

	return change;
}

789
static int snd_ac97_put_spsa(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
790
{
791
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
792 793 794 795 796 797 798 799 800
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0xff;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	// int invert = (kcontrol->private_value >> 24) & 0xff;
	unsigned short value, old, new;
	int change;

	value = (ucontrol->value.integer.value[0] & mask);

801
	mutex_lock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814
	mask <<= shift;
	value <<= shift;
	old = snd_ac97_read_cache(ac97, reg);
	new = (old & ~mask) | value;
	change = old != new;

	if (change) {
		unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
		snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
		change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
		if (extst & AC97_EA_SPDIF)
			snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
	}
815
	mutex_unlock(&ac97->reg_mutex);
L
Linus Torvalds 已提交
816 817 818
	return change;
}

819
const struct snd_kcontrol_new snd_ac97_controls_spdif[5] = {
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	{
		.access = SNDRV_CTL_ELEM_ACCESS_READ,
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
		.info = snd_ac97_spdif_mask_info,
		.get = snd_ac97_spdif_cmask_get,
	},
	{
		.access = SNDRV_CTL_ELEM_ACCESS_READ,
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
		.info = snd_ac97_spdif_mask_info,
		.get = snd_ac97_spdif_pmask_get,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
		.info = snd_ac97_spdif_mask_info,
		.get = snd_ac97_spdif_default_get,
		.put = snd_ac97_spdif_default_put,
	},

	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
		.info = snd_ac97_info_volsw,
		.get = snd_ac97_get_volsw,
		.put = snd_ac97_put_spsa,
		.private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
	},
};

#define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
  .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
  .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }

858
static int snd_ac97_ad18xx_pcm_info_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
859
{
860
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874
	int mask = (kcontrol->private_value >> 16) & 0x0f;
	int lshift = (kcontrol->private_value >> 8) & 0x0f;
	int rshift = (kcontrol->private_value >> 12) & 0x0f;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
		uinfo->count = 2;
	else
		uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}

875
static int snd_ac97_ad18xx_pcm_get_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
876
{
877
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886 887 888
	int codec = kcontrol->private_value & 3;
	int lshift = (kcontrol->private_value >> 8) & 0x0f;
	int rshift = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	
	ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
	if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
		ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
	return 0;
}

889
static int snd_ac97_ad18xx_pcm_put_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
890
{
891
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
	int codec = kcontrol->private_value & 3;
	int lshift = (kcontrol->private_value >> 8) & 0x0f;
	int rshift = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	unsigned short val, valmask;
	
	val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
	valmask = mask << lshift;
	if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
		val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
		valmask |= mask << rshift;
	}
	return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
}

#define AD18XX_PCM_VOLUME(xname, codec) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
  .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
  .private_value = codec }

912
static int snd_ac97_ad18xx_pcm_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
913 914 915 916 917 918 919 920
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 31;
	return 0;
}

921
static int snd_ac97_ad18xx_pcm_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
922
{
923
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
924 925
	int codec = kcontrol->private_value & 3;
	
926
	mutex_lock(&ac97->page_mutex);
L
Linus Torvalds 已提交
927 928
	ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
	ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
929
	mutex_unlock(&ac97->page_mutex);
L
Linus Torvalds 已提交
930 931 932
	return 0;
}

933
static int snd_ac97_ad18xx_pcm_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
934
{
935
	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
936 937 938 939 940 941 942 943
	int codec = kcontrol->private_value & 3;
	unsigned short val1, val2;
	
	val1 = 31 - (ucontrol->value.integer.value[0] & 31);
	val2 = 31 - (ucontrol->value.integer.value[1] & 31);
	return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
}

944
static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_pcm[2] = {
L
Linus Torvalds 已提交
945 946 947 948
AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
};

949
static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_surround[2] = {
L
Linus Torvalds 已提交
950 951 952 953
AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
};

954
static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_center[2] = {
L
Linus Torvalds 已提交
955 956 957 958
AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
};

959
static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_lfe[2] = {
L
Linus Torvalds 已提交
960 961 962 963 964 965 966 967
AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
};

/*
 *
 */

968
static void snd_ac97_powerdown(struct snd_ac97 *ac97);
L
Linus Torvalds 已提交
969

970
static int snd_ac97_bus_free(struct snd_ac97_bus *bus)
L
Linus Torvalds 已提交
971 972 973 974 975 976 977 978 979 980 981
{
	if (bus) {
		snd_ac97_bus_proc_done(bus);
		kfree(bus->pcms);
		if (bus->private_free)
			bus->private_free(bus);
		kfree(bus);
	}
	return 0;
}

982
static int snd_ac97_bus_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
983
{
984
	struct snd_ac97_bus *bus = device->device_data;
L
Linus Torvalds 已提交
985 986 987
	return snd_ac97_bus_free(bus);
}

988
static int snd_ac97_free(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
989 990
{
	if (ac97) {
991
#ifdef CONFIG_SND_AC97_POWER_SAVE
992
		cancel_delayed_work(&ac97->power_work);
993
		flush_scheduled_work();
994
#endif
L
Linus Torvalds 已提交
995
		snd_ac97_proc_done(ac97);
996
		if (ac97->bus)
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003 1004
			ac97->bus->codec[ac97->num] = NULL;
		if (ac97->private_free)
			ac97->private_free(ac97);
		kfree(ac97);
	}
	return 0;
}

1005
static int snd_ac97_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
1006
{
1007
	struct snd_ac97 *ac97 = device->device_data;
L
Linus Torvalds 已提交
1008 1009 1010 1011
	snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
	return snd_ac97_free(ac97);
}

1012
static int snd_ac97_try_volume_mix(struct snd_ac97 * ac97, int reg)
L
Linus Torvalds 已提交
1013 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 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
{
	unsigned short val, mask = 0x8000;

	if (! snd_ac97_valid_reg(ac97, reg))
		return 0;

	switch (reg) {
	case AC97_MASTER_TONE:
		return ac97->caps & 0x04 ? 1 : 0;
	case AC97_HEADPHONE:
		return ac97->caps & 0x10 ? 1 : 0;
	case AC97_REC_GAIN_MIC:
		return ac97->caps & 0x01 ? 1 : 0;
	case AC97_3D_CONTROL:
		if (ac97->caps & 0x7c00) {
			val = snd_ac97_read(ac97, reg);
			/* if nonzero - fixed and we can't set it */
			return val == 0;
		}
		return 0;
	case AC97_CENTER_LFE_MASTER:	/* center */
		if ((ac97->ext_id & AC97_EI_CDAC) == 0)
			return 0;
		break;
	case AC97_CENTER_LFE_MASTER+1:	/* lfe */
		if ((ac97->ext_id & AC97_EI_LDAC) == 0)
			return 0;
		reg = AC97_CENTER_LFE_MASTER;
		mask = 0x0080;
		break;
	case AC97_SURROUND_MASTER:
		if ((ac97->ext_id & AC97_EI_SDAC) == 0)
			return 0;
		break;
	}

	val = snd_ac97_read(ac97, reg);
	if (!(val & mask)) {
		/* nothing seems to be here - mute flag is not set */
		/* try another test */
		snd_ac97_write_cache(ac97, reg, val | mask);
		val = snd_ac97_read(ac97, reg);
1055
		val = snd_ac97_read(ac97, reg);
L
Linus Torvalds 已提交
1056 1057 1058 1059 1060 1061
		if (!(val & mask))
			return 0;	/* nothing here */
	}
	return 1;		/* success, useable */
}

1062
static void check_volume_resolution(struct snd_ac97 *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max)
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067
{
	unsigned short cbit[3] = { 0x20, 0x10, 0x01 };
	unsigned char max[3] = { 63, 31, 15 };
	int i;

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
	/* first look up the static resolution table */
	if (ac97->res_table) {
		const struct snd_ac97_res_table *tbl;
		for (tbl = ac97->res_table; tbl->reg; tbl++) {
			if (tbl->reg == reg) {
				*lo_max = tbl->bits & 0xff;
				*hi_max = (tbl->bits >> 8) & 0xff;
				return;
			}
		}
	}

L
Linus Torvalds 已提交
1080 1081 1082 1083
	*lo_max = *hi_max = 0;
	for (i = 0 ; i < ARRAY_SIZE(cbit); i++) {
		unsigned short val;
		snd_ac97_write(ac97, reg, 0x8080 | cbit[i] | (cbit[i] << 8));
1084 1085 1086 1087 1088
		/* Do the read twice due to buffers on some ac97 codecs.
		 * e.g. The STAC9704 returns exactly what you wrote the the register
		 * if you read it immediately. This causes the detect routine to fail.
		 */
		val = snd_ac97_read(ac97, reg);
L
Linus Torvalds 已提交
1089
		val = snd_ac97_read(ac97, reg);
1090
		if (! *lo_max && (val & 0x7f) == cbit[i])
L
Linus Torvalds 已提交
1091
			*lo_max = max[i];
1092
		if (! *hi_max && ((val >> 8) & 0x7f) == cbit[i])
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098
			*hi_max = max[i];
		if (*lo_max && *hi_max)
			break;
	}
}

1099
int snd_ac97_try_bit(struct snd_ac97 * ac97, int reg, int bit)
L
Linus Torvalds 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
{
	unsigned short mask, val, orig, res;

	mask = 1 << bit;
	orig = snd_ac97_read(ac97, reg);
	val = orig ^ mask;
	snd_ac97_write(ac97, reg, val);
	res = snd_ac97_read(ac97, reg);
	snd_ac97_write_cache(ac97, reg, orig);
	return res == val;
}

/* check the volume resolution of center/lfe */
1113
static void snd_ac97_change_volume_params2(struct snd_ac97 * ac97, int reg, int shift, unsigned char *max)
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
{
	unsigned short val, val1;

	*max = 63;
	val = 0x8080 | (0x20 << shift);
	snd_ac97_write(ac97, reg, val);
	val1 = snd_ac97_read(ac97, reg);
	if (val != val1) {
		*max = 31;
	}
	/* reset volume to zero */
	snd_ac97_write_cache(ac97, reg, 0x8080);
}

static inline int printable(unsigned int x)
{
	x &= 0xff;
	if (x < ' ' || x >= 0x71) {
		if (x <= 0x89)
			return x - 0x71 + 'A';
		return '?';
	}
	return x;
}

1139
struct snd_kcontrol *snd_ac97_cnew(const struct snd_kcontrol_new *_template, struct snd_ac97 * ac97)
L
Linus Torvalds 已提交
1140
{
1141
	struct snd_kcontrol_new template;
L
Linus Torvalds 已提交
1142 1143 1144 1145 1146 1147 1148 1149
	memcpy(&template, _template, sizeof(template));
	template.index = ac97->num;
	return snd_ctl_new1(&template, ac97);
}

/*
 * create mute switch(es) for normal stereo controls
 */
1150 1151 1152
static int snd_ac97_cmute_new_stereo(struct snd_card *card, char *name, int reg,
				     int check_stereo, int check_amix,
				     struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1153
{
1154
	struct snd_kcontrol *kctl;
L
Linus Torvalds 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	int err;
	unsigned short val, val1, mute_mask;

	if (! snd_ac97_valid_reg(ac97, reg))
		return 0;

	mute_mask = 0x8000;
	val = snd_ac97_read(ac97, reg);
	if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) {
		/* check whether both mute bits work */
		val1 = val | 0x8080;
		snd_ac97_write(ac97, reg, val1);
		if (val1 == snd_ac97_read(ac97, reg))
			mute_mask = 0x8080;
	}
	if (mute_mask == 0x8080) {
1171
		struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
1172 1173
		if (check_amix)
			tmp.private_value |= (1 << 30);
L
Linus Torvalds 已提交
1174 1175 1176
		tmp.index = ac97->num;
		kctl = snd_ctl_new1(&tmp, ac97);
	} else {
1177
		struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 15, 1, 1);
1178 1179
		if (check_amix)
			tmp.private_value |= (1 << 30);
L
Linus Torvalds 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
		tmp.index = ac97->num;
		kctl = snd_ctl_new1(&tmp, ac97);
	}
	err = snd_ctl_add(card, kctl);
	if (err < 0)
		return err;
	/* mute as default */
	snd_ac97_write_cache(ac97, reg, val | mute_mask);
	return 0;
}

1191 1192 1193
/*
 * set dB information
 */
1194 1195 1196 1197 1198
static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
1199

1200
static const unsigned int *find_db_scale(unsigned int maxval)
1201 1202 1203 1204 1205 1206 1207 1208 1209
{
	switch (maxval) {
	case 0x0f: return db_scale_4bit;
	case 0x1f: return db_scale_5bit;
	case 0x3f: return db_scale_6bit;
	}
	return NULL;
}

1210 1211
static void set_tlv_db_scale(struct snd_kcontrol *kctl, const unsigned int *tlv)
{
1212 1213 1214 1215 1216
	kctl->tlv.p = tlv;
	if (tlv)
		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
}

L
Linus Torvalds 已提交
1217 1218 1219
/*
 * create a volume for normal stereo/mono controls
 */
1220 1221
static int snd_ac97_cvol_new(struct snd_card *card, char *name, int reg, unsigned int lo_max,
			     unsigned int hi_max, struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1222 1223
{
	int err;
1224
	struct snd_kcontrol *kctl;
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229

	if (! snd_ac97_valid_reg(ac97, reg))
		return 0;
	if (hi_max) {
		/* invert */
1230
		struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1);
L
Linus Torvalds 已提交
1231 1232 1233 1234
		tmp.index = ac97->num;
		kctl = snd_ctl_new1(&tmp, ac97);
	} else {
		/* invert */
1235
		struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 0, lo_max, 1);
L
Linus Torvalds 已提交
1236 1237 1238
		tmp.index = ac97->num;
		kctl = snd_ctl_new1(&tmp, ac97);
	}
1239 1240 1241 1242
	if (reg >= AC97_PHONE && reg <= AC97_PCM)
		set_tlv_db_scale(kctl, db_scale_5bit_12db_max);
	else
		set_tlv_db_scale(kctl, find_db_scale(lo_max));
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	err = snd_ctl_add(card, kctl);
	if (err < 0)
		return err;
	snd_ac97_write_cache(ac97, reg,
			     (snd_ac97_read(ac97, reg) & 0x8080) |
			     lo_max | (hi_max << 8));
	return 0;
}

/*
 * create a mute-switch and a volume for normal stereo/mono controls
 */
1255 1256 1257
static int snd_ac97_cmix_new_stereo(struct snd_card *card, const char *pfx,
				    int reg, int check_stereo, int check_amix,
				    struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
{
	int err;
	char name[44];
	unsigned char lo_max, hi_max;

	if (! snd_ac97_valid_reg(ac97, reg))
		return 0;

	if (snd_ac97_try_bit(ac97, reg, 15)) {
		sprintf(name, "%s Switch", pfx);
1268 1269 1270
		if ((err = snd_ac97_cmute_new_stereo(card, name, reg,
						     check_stereo, check_amix,
						     ac97)) < 0)
L
Linus Torvalds 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
			return err;
	}
	check_volume_resolution(ac97, reg, &lo_max, &hi_max);
	if (lo_max) {
		sprintf(name, "%s Volume", pfx);
		if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0)
			return err;
	}
	return 0;
}

1282 1283 1284 1285
#define snd_ac97_cmix_new(card, pfx, reg, acheck, ac97) \
	snd_ac97_cmix_new_stereo(card, pfx, reg, 0, acheck, ac97)
#define snd_ac97_cmute_new(card, name, reg, acheck, ac97) \
	snd_ac97_cmute_new_stereo(card, name, reg, 0, acheck, ac97)
L
Linus Torvalds 已提交
1286

1287
static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97);
L
Linus Torvalds 已提交
1288

1289
static int snd_ac97_mixer_build(struct snd_ac97 * ac97)
L
Linus Torvalds 已提交
1290
{
1291 1292
	struct snd_card *card = ac97->bus->card;
	struct snd_kcontrol *kctl;
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297 1298 1299 1300
	int err;
	unsigned int idx;
	unsigned char max;

	/* build master controls */
	/* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
	if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
		if (ac97->flags & AC97_HAS_NO_MASTER_VOL)
1301 1302
			err = snd_ac97_cmute_new(card, "Master Playback Switch",
						 AC97_MASTER, 0, ac97);
L
Linus Torvalds 已提交
1303
		else
1304 1305
			err = snd_ac97_cmix_new(card, "Master Playback",
						AC97_MASTER, 0, ac97);
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312
		if (err < 0)
			return err;
	}

	ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080;

	/* build center controls */
1313 1314
	if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER)) 
		&& !(ac97->flags & AC97_AD_MULTI)) {
L
Linus Torvalds 已提交
1315 1316 1317 1318 1319 1320 1321
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
			return err;
		if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
			return err;
		snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max);
		kctl->private_value &= ~(0xff << 16);
		kctl->private_value |= (int)max << 16;
1322
		set_tlv_db_scale(kctl, find_db_scale(max));
L
Linus Torvalds 已提交
1323 1324 1325 1326
		snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
	}

	/* build LFE controls */
1327 1328
	if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1))
		&& !(ac97->flags & AC97_AD_MULTI)) {
L
Linus Torvalds 已提交
1329 1330 1331 1332 1333 1334 1335
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
			return err;
		if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
			return err;
		snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
		kctl->private_value &= ~(0xff << 16);
		kctl->private_value |= (int)max << 16;
1336
		set_tlv_db_scale(kctl, find_db_scale(max));
L
Linus Torvalds 已提交
1337 1338 1339 1340
		snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
	}

	/* build surround controls */
1341 1342
	if ((snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER)) 
		&& !(ac97->flags & AC97_AD_MULTI)) {
L
Linus Torvalds 已提交
1343
		/* Surround Master (0x38) is with stereo mutes */
1344 1345 1346
		if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback",
						    AC97_SURROUND_MASTER, 1, 0,
						    ac97)) < 0)
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351
			return err;
	}

	/* build headphone controls */
	if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) {
1352 1353
		if ((err = snd_ac97_cmix_new(card, "Headphone Playback",
					     AC97_HEADPHONE, 0, ac97)) < 0)
L
Linus Torvalds 已提交
1354 1355 1356 1357 1358
			return err;
	}
	
	/* build master mono controls */
	if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
1359 1360
		if ((err = snd_ac97_cmix_new(card, "Master Mono Playback",
					     AC97_MASTER_MONO, 0, ac97)) < 0)
L
Linus Torvalds 已提交
1361 1362 1363 1364
			return err;
	}
	
	/* build master tone controls */
1365 1366 1367 1368 1369 1370 1371 1372 1373
	if (!(ac97->flags & AC97_HAS_NO_TONE)) {
		if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
			for (idx = 0; idx < 2; idx++) {
				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
					return err;
				if (ac97->id == AC97_ID_YMF753) {
					kctl->private_value &= ~(0xff << 16);
					kctl->private_value |= 7 << 16;
				}
L
Linus Torvalds 已提交
1374
			}
1375
			snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
L
Linus Torvalds 已提交
1376 1377 1378 1379 1380 1381 1382 1383
		}
	}
	
	/* build PC Speaker controls */
	if (!(ac97->flags & AC97_HAS_NO_PC_BEEP) && 
		((ac97->flags & AC97_HAS_PC_BEEP) ||
	    snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP))) {
		for (idx = 0; idx < 2; idx++)
1384
			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
L
Linus Torvalds 已提交
1385
				return err;
1386
		set_tlv_db_scale(kctl, db_scale_4bit);
L
Linus Torvalds 已提交
1387 1388 1389 1390 1391 1392 1393
		snd_ac97_write_cache(ac97, AC97_PC_BEEP,
				     snd_ac97_read(ac97, AC97_PC_BEEP) | 0x801e);
	}
	
	/* build Phone controls */
	if (!(ac97->flags & AC97_HAS_NO_PHONE)) {
		if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
1394 1395
			if ((err = snd_ac97_cmix_new(card, "Phone Playback",
						     AC97_PHONE, 1, ac97)) < 0)
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400
				return err;
		}
	}
	
	/* build MIC controls */
1401 1402
	if (!(ac97->flags & AC97_HAS_NO_MIC)) {
		if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) {
1403 1404
			if ((err = snd_ac97_cmix_new(card, "Mic Playback",
						     AC97_MIC, 1, ac97)) < 0)
1405 1406 1407 1408
				return err;
			if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0)
				return err;
		}
L
Linus Torvalds 已提交
1409 1410 1411 1412
	}

	/* build Line controls */
	if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) {
1413 1414
		if ((err = snd_ac97_cmix_new(card, "Line Playback",
					     AC97_LINE, 1, ac97)) < 0)
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419 1420
			return err;
	}
	
	/* build CD controls */
	if (!(ac97->flags & AC97_HAS_NO_CD)) {
		if (snd_ac97_try_volume_mix(ac97, AC97_CD)) {
1421 1422
			if ((err = snd_ac97_cmix_new(card, "CD Playback",
						     AC97_CD, 1, ac97)) < 0)
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429
				return err;
		}
	}
	
	/* build Video controls */
	if (!(ac97->flags & AC97_HAS_NO_VIDEO)) {
		if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
1430 1431
			if ((err = snd_ac97_cmix_new(card, "Video Playback",
						     AC97_VIDEO, 1, ac97)) < 0)
L
Linus Torvalds 已提交
1432 1433 1434 1435 1436
				return err;
		}
	}

	/* build Aux controls */
1437 1438
	if (!(ac97->flags & AC97_HAS_NO_AUX)) {
		if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
1439 1440
			if ((err = snd_ac97_cmix_new(card, "Aux Playback",
						     AC97_AUX, 1, ac97)) < 0)
1441 1442
				return err;
		}
L
Linus Torvalds 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
	}

	/* build PCM controls */
	if (ac97->flags & AC97_AD_MULTI) {
		unsigned short init_val;
		if (ac97->flags & AC97_STEREO_MUTES)
			init_val = 0x9f9f;
		else
			init_val = 0x9f1f;
		for (idx = 0; idx < 2; idx++)
1453
			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
L
Linus Torvalds 已提交
1454
				return err;
1455
		set_tlv_db_scale(kctl, db_scale_5bit);
L
Linus Torvalds 已提交
1456 1457 1458
		ac97->spec.ad18xx.pcmreg[0] = init_val;
		if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
			for (idx = 0; idx < 2; idx++)
1459
				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
L
Linus Torvalds 已提交
1460
					return err;
1461
			set_tlv_db_scale(kctl, db_scale_5bit);
L
Linus Torvalds 已提交
1462 1463 1464 1465
			ac97->spec.ad18xx.pcmreg[1] = init_val;
		}
		if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
			for (idx = 0; idx < 2; idx++)
1466
				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
L
Linus Torvalds 已提交
1467
					return err;
1468
			set_tlv_db_scale(kctl, db_scale_5bit);
L
Linus Torvalds 已提交
1469
			for (idx = 0; idx < 2; idx++)
1470
				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
L
Linus Torvalds 已提交
1471
					return err;
1472
			set_tlv_db_scale(kctl, db_scale_5bit);
L
Linus Torvalds 已提交
1473 1474 1475 1476
			ac97->spec.ad18xx.pcmreg[2] = init_val;
		}
		snd_ac97_write_cache(ac97, AC97_PCM, init_val);
	} else {
1477 1478
		if (!(ac97->flags & AC97_HAS_NO_STD_PCM)) {
			if (ac97->flags & AC97_HAS_NO_PCM_VOL)
1479 1480 1481
				err = snd_ac97_cmute_new(card,
							 "PCM Playback Switch",
							 AC97_PCM, 0, ac97);
1482
			else
1483 1484
				err = snd_ac97_cmix_new(card, "PCM Playback",
							AC97_PCM, 0, ac97);
1485 1486 1487
			if (err < 0)
				return err;
		}
L
Linus Torvalds 已提交
1488 1489 1490 1491 1492 1493 1494
	}

	/* build Capture controls */
	if (!(ac97->flags & AC97_HAS_NO_REC_GAIN)) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
			return err;
		if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) {
1495 1496 1497
			err = snd_ac97_cmute_new(card, "Capture Switch",
						 AC97_REC_GAIN, 0, ac97);
			if (err < 0)
L
Linus Torvalds 已提交
1498 1499
				return err;
		}
1500
		if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
L
Linus Torvalds 已提交
1501
			return err;
1502
		set_tlv_db_scale(kctl, db_scale_rec_gain);
L
Linus Torvalds 已提交
1503 1504 1505 1506 1507 1508
		snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
		snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
	}
	/* build MIC Capture controls */
	if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
		for (idx = 0; idx < 2; idx++)
1509
			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
L
Linus Torvalds 已提交
1510
				return err;
1511
		set_tlv_db_scale(kctl, db_scale_rec_gain);
L
Linus Torvalds 已提交
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 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
		snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
	}

	/* build PCM out path & mute control */
	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
			return err;
	}

	/* build Simulated Stereo Enhancement control */
	if (ac97->caps & 0x0008) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
			return err;
	}

	/* build 3D Stereo Enhancement control */
	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
			return err;
	}

	/* build Loudness control */
	if (ac97->caps & 0x0020) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
			return err;
	}

	/* build Mono output select control */
	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
			return err;
	}

	/* build Mic select control */
	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
			return err;
	}

	/* build ADC/DAC loopback control */
	if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
			return err;
	}

	snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000);

	/* build 3D controls */
	if (ac97->build_ops->build_3d) {
		ac97->build_ops->build_3d(ac97);
	} else {
		if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
			unsigned short val;
			val = 0x0707;
			snd_ac97_write(ac97, AC97_3D_CONTROL, val);
			val = snd_ac97_read(ac97, AC97_3D_CONTROL);
			val = val == 0x0606;
			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
				return err;
			if (val)
				kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
				return err;
			if (val)
				kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
			snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
		}
	}

	/* build S/PDIF controls */
1582 1583 1584 1585 1586 1587

	/* Hack for ASUS P5P800-VM, which does not indicate S/PDIF capability */
	if (ac97->subsystem_vendor == 0x1043 &&
	    ac97->subsystem_device == 0x810f)
		ac97->ext_id |= AC97_EI_SPDIF;

L
Linus Torvalds 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	if ((ac97->ext_id & AC97_EI_SPDIF) && !(ac97->scaps & AC97_SCAP_NO_SPDIF)) {
		if (ac97->build_ops->build_spdif) {
			if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
				return err;
		} else {
			for (idx = 0; idx < 5; idx++)
				if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
					return err;
			if (ac97->build_ops->build_post_spdif) {
				if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
					return err;
			}
			/* set default PCM S/PDIF params */
			/* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
			snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
			ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
		}
		ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
	}
	
	/* build chip specific controls */
	if (ac97->build_ops->build_specific)
		if ((err = ac97->build_ops->build_specific(ac97)) < 0)
			return err;

	if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
		kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97);
		if (! kctl)
			return -ENOMEM;
		if (ac97->scaps & AC97_SCAP_INV_EAPD)
			set_inv_eapd(ac97, kctl);
		if ((err = snd_ctl_add(card, kctl)) < 0)
			return err;
	}

	return 0;
}

1626
static int snd_ac97_modem_build(struct snd_card *card, struct snd_ac97 * ac97)
L
Linus Torvalds 已提交
1627
{
1628 1629
	int err, idx;

L
Linus Torvalds 已提交
1630 1631 1632 1633 1634 1635
	//printk("AC97_GPIO_CFG = %x\n",snd_ac97_read(ac97,AC97_GPIO_CFG));
	snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
	snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
	snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
	snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
	snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
1636 1637 1638

	/* build modem switches */
	for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_modem_switches); idx++)
1639
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ac97_controls_modem_switches[idx], ac97))) < 0)
1640 1641 1642 1643 1644 1645 1646
			return err;

	/* build chip specific controls */
	if (ac97->build_ops->build_specific)
		if ((err = ac97->build_ops->build_specific(ac97)) < 0)
			return err;

L
Linus Torvalds 已提交
1647 1648 1649
	return 0;
}

1650
static int snd_ac97_test_rate(struct snd_ac97 *ac97, int reg, int shadow_reg, int rate)
L
Linus Torvalds 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
{
	unsigned short val;
	unsigned int tmp;

	tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
	snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
	if (shadow_reg)
		snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
	val = snd_ac97_read(ac97, reg);
	return val == (tmp & 0xffff);
}

1663
static void snd_ac97_determine_rates(struct snd_ac97 *ac97, int reg, int shadow_reg, unsigned int *r_result)
L
Linus Torvalds 已提交
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
{
	unsigned int result = 0;
	unsigned short saved;

	if (ac97->bus->no_vra) {
		*r_result = SNDRV_PCM_RATE_48000;
		if ((ac97->flags & AC97_DOUBLE_RATE) &&
		    reg == AC97_PCM_FRONT_DAC_RATE)
			*r_result |= SNDRV_PCM_RATE_96000;
		return;
	}

	saved = snd_ac97_read(ac97, reg);
	if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
				     AC97_EA_DRA, 0);
	/* test a non-standard rate */
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
		result |= SNDRV_PCM_RATE_CONTINUOUS;
	/* let's try to obtain standard rates */
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
		result |= SNDRV_PCM_RATE_8000;
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
		result |= SNDRV_PCM_RATE_11025;
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
		result |= SNDRV_PCM_RATE_16000;
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
		result |= SNDRV_PCM_RATE_22050;
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
		result |= SNDRV_PCM_RATE_32000;
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
		result |= SNDRV_PCM_RATE_44100;
	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
		result |= SNDRV_PCM_RATE_48000;
	if ((ac97->flags & AC97_DOUBLE_RATE) &&
	    reg == AC97_PCM_FRONT_DAC_RATE) {
		/* test standard double rates */
		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
				     AC97_EA_DRA, AC97_EA_DRA);
		if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2))
			result |= SNDRV_PCM_RATE_64000;
		if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2))
			result |= SNDRV_PCM_RATE_88200;
		if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2))
			result |= SNDRV_PCM_RATE_96000;
		/* some codecs don't support variable double rates */
		if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2))
			result &= ~SNDRV_PCM_RATE_CONTINUOUS;
		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
				     AC97_EA_DRA, 0);
	}
	/* restore the default value */
	snd_ac97_write_cache(ac97, reg, saved);
	if (shadow_reg)
		snd_ac97_write_cache(ac97, shadow_reg, saved);
	*r_result = result;
}

/* check AC97_SPDIF register to accept which sample rates */
1723
static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
{
	unsigned int result = 0;
	int i;
	static unsigned short ctl_bits[] = {
		AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
	};
	static unsigned int rate_bits[] = {
		SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
	};

	for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
		snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
		if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
			result |= rate_bits[i];
	}
	return result;
}

/* look for the codec id table matching with the given id */
1743 1744
static const struct ac97_codec_id *look_for_codec_id(const struct ac97_codec_id *table,
						     unsigned int id)
L
Linus Torvalds 已提交
1745
{
1746
	const struct ac97_codec_id *pid;
L
Linus Torvalds 已提交
1747 1748 1749 1750 1751 1752 1753

	for (pid = table; pid->id; pid++)
		if (pid->id == (id & pid->mask))
			return pid;
	return NULL;
}

1754
void snd_ac97_get_name(struct snd_ac97 *ac97, unsigned int id, char *name, int modem)
L
Linus Torvalds 已提交
1755
{
1756
	const struct ac97_codec_id *pid;
L
Linus Torvalds 已提交
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

	sprintf(name, "0x%x %c%c%c", id,
		printable(id >> 24),
		printable(id >> 16),
		printable(id >> 8));
	pid = look_for_codec_id(snd_ac97_codec_id_vendors, id);
	if (! pid)
		return;

	strcpy(name, pid->name);
	if (ac97 && pid->patch) {
		if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
		    (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
			pid->patch(ac97);
	} 

	pid = look_for_codec_id(snd_ac97_codec_ids, id);
	if (pid) {
		strcat(name, " ");
		strcat(name, pid->name);
		if (pid->mask != 0xffffffff)
			sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
		if (ac97 && pid->patch) {
			if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
			    (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
				pid->patch(ac97);
		}
	} else
		sprintf(name + strlen(name), " id %x", id & 0xff);
}

/**
 * snd_ac97_get_short_name - retrieve codec name
 * @ac97: the codec instance
 *
 * Returns the short identifying name of the codec.
 */
1794
const char *snd_ac97_get_short_name(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1795
{
1796
	const struct ac97_codec_id *pid;
L
Linus Torvalds 已提交
1797 1798 1799 1800 1801 1802 1803

	for (pid = snd_ac97_codec_ids; pid->id; pid++)
		if (pid->id == (ac97->id & pid->mask))
			return pid->name;
	return "unknown codec";
}

1804
EXPORT_SYMBOL(snd_ac97_get_short_name);
L
Linus Torvalds 已提交
1805 1806 1807 1808

/* wait for a while until registers are accessible after RESET
 * return 0 if ok, negative not ready
 */
1809
static int ac97_reset_wait(struct snd_ac97 *ac97, int timeout, int with_modem)
L
Linus Torvalds 已提交
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
{
	unsigned long end_time;
	unsigned short val;

	end_time = jiffies + timeout;
	do {
		
		/* use preliminary reads to settle the communication */
		snd_ac97_read(ac97, AC97_RESET);
		snd_ac97_read(ac97, AC97_VENDOR_ID1);
		snd_ac97_read(ac97, AC97_VENDOR_ID2);
		/* modem? */
		if (with_modem) {
			val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
			if (val != 0xffff && (val & 1) != 0)
				return 0;
		}
		if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) {
			/* probably only Xbox issue - all registers are read as zero */
			val = snd_ac97_read(ac97, AC97_VENDOR_ID1);
			if (val != 0 && val != 0xffff)
				return 0;
		} else {
			/* because the PCM or MASTER volume registers can be modified,
			 * the REC_GAIN register is used for tests
			 */
			/* test if we can write to the record gain volume register */
			snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
			if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05)
				return 0;
		}
1841
		schedule_timeout_uninterruptible(1);
L
Linus Torvalds 已提交
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
	} while (time_after_eq(end_time, jiffies));
	return -ENODEV;
}

/**
 * snd_ac97_bus - create an AC97 bus component
 * @card: the card instance
 * @num: the bus number
 * @ops: the bus callbacks table
 * @private_data: private data pointer for the new instance
 * @rbus: the pointer to store the new AC97 bus instance.
 *
1854
 * Creates an AC97 bus component.  An struct snd_ac97_bus instance is newly
L
Linus Torvalds 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
 * allocated and initialized.
 *
 * The ops table must include valid callbacks (at least read and
 * write).  The other callbacks, wait and reset, are not mandatory.
 * 
 * The clock is set to 48000.  If another clock is needed, set
 * (*rbus)->clock manually.
 *
 * The AC97 bus instance is registered as a low-level device, so you don't
 * have to release it manually.
 *
 * Returns zero if successful, or a negative error code on failure.
 */
1868 1869
int snd_ac97_bus(struct snd_card *card, int num, struct snd_ac97_bus_ops *ops,
		 void *private_data, struct snd_ac97_bus **rbus)
L
Linus Torvalds 已提交
1870 1871
{
	int err;
1872 1873
	struct snd_ac97_bus *bus;
	static struct snd_device_ops dev_ops = {
L
Linus Torvalds 已提交
1874 1875 1876 1877 1878
		.dev_free =	snd_ac97_bus_dev_free,
	};

	snd_assert(card != NULL, return -EINVAL);
	snd_assert(rbus != NULL, return -EINVAL);
1879
	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
L
Linus Torvalds 已提交
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
	if (bus == NULL)
		return -ENOMEM;
	bus->card = card;
	bus->num = num;
	bus->ops = ops;
	bus->private_data = private_data;
	bus->clock = 48000;
	spin_lock_init(&bus->bus_lock);
	snd_ac97_bus_proc_init(bus);
	if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
		snd_ac97_bus_free(bus);
		return err;
	}
	*rbus = bus;
	return 0;
}

1897 1898
EXPORT_SYMBOL(snd_ac97_bus);

1899 1900 1901 1902 1903 1904
/* stop no dev release warning */
static void ac97_device_release(struct device * dev)
{
}

/* register ac97 codec to bus */
1905
static int snd_ac97_dev_register(struct snd_device *device)
1906
{
1907
	struct snd_ac97 *ac97 = device->device_data;
1908 1909 1910 1911 1912
	int err;

	ac97->dev.bus = &ac97_bus_type;
	ac97->dev.parent = ac97->bus->card->dev;
	ac97->dev.release = ac97_device_release;
1913 1914 1915
	snprintf(ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
		 ac97->bus->card->number, ac97->num,
		 snd_ac97_get_short_name(ac97));
1916 1917 1918 1919 1920 1921 1922 1923
	if ((err = device_register(&ac97->dev)) < 0) {
		snd_printk(KERN_ERR "Can't register ac97 bus\n");
		ac97->dev.bus = NULL;
		return err;
	}
	return 0;
}

1924 1925
/* disconnect ac97 codec */
static int snd_ac97_dev_disconnect(struct snd_device *device)
1926
{
1927
	struct snd_ac97 *ac97 = device->device_data;
1928 1929
	if (ac97->dev.bus)
		device_unregister(&ac97->dev);
1930
	return 0;
1931 1932
}

L
Linus Torvalds 已提交
1933 1934 1935
/* build_ops to do nothing */
static struct snd_ac97_build_ops null_build_ops;

1936
#ifdef CONFIG_SND_AC97_POWER_SAVE
D
David Howells 已提交
1937
static void do_update_power(struct work_struct *work)
1938
{
D
David Howells 已提交
1939 1940
	update_power_regs(
		container_of(work, struct snd_ac97, power_work.work));
1941 1942 1943
}
#endif

L
Linus Torvalds 已提交
1944 1945 1946 1947 1948 1949 1950
/**
 * snd_ac97_mixer - create an Codec97 component
 * @bus: the AC97 bus which codec is attached to
 * @template: the template of ac97, including index, callbacks and
 *         the private data.
 * @rac97: the pointer to store the new ac97 instance.
 *
1951
 * Creates an Codec97 component.  An struct snd_ac97 instance is newly
L
Linus Torvalds 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
 * allocated and initialized from the template.  The codec
 * is then initialized by the standard procedure.
 *
 * The template must include the codec number (num) and address (addr),
 * and the private data (private_data).
 * 
 * The ac97 instance is registered as a low-level device, so you don't
 * have to release it manually.
 *
 * Returns zero if successful, or a negative error code on failure.
 */
1963
int snd_ac97_mixer(struct snd_ac97_bus *bus, struct snd_ac97_template *template, struct snd_ac97 **rac97)
L
Linus Torvalds 已提交
1964 1965
{
	int err;
1966 1967
	struct snd_ac97 *ac97;
	struct snd_card *card;
L
Linus Torvalds 已提交
1968 1969 1970
	char name[64];
	unsigned long end_time;
	unsigned int reg;
1971 1972
	const struct ac97_codec_id *pid;
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
1973
		.dev_free =	snd_ac97_dev_free,
1974
		.dev_register =	snd_ac97_dev_register,
1975
		.dev_disconnect =	snd_ac97_dev_disconnect,
L
Linus Torvalds 已提交
1976 1977 1978 1979 1980 1981 1982 1983
	};

	snd_assert(rac97 != NULL, return -EINVAL);
	*rac97 = NULL;
	snd_assert(bus != NULL && template != NULL, return -EINVAL);
	snd_assert(template->num < 4 && bus->codec[template->num] == NULL, return -EINVAL);

	card = bus->card;
1984
	ac97 = kzalloc(sizeof(*ac97), GFP_KERNEL);
L
Linus Torvalds 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993
	if (ac97 == NULL)
		return -ENOMEM;
	ac97->private_data = template->private_data;
	ac97->private_free = template->private_free;
	ac97->bus = bus;
	ac97->pci = template->pci;
	ac97->num = template->num;
	ac97->addr = template->addr;
	ac97->scaps = template->scaps;
1994
	ac97->res_table = template->res_table;
L
Linus Torvalds 已提交
1995
	bus->codec[ac97->num] = ac97;
1996 1997
	mutex_init(&ac97->reg_mutex);
	mutex_init(&ac97->page_mutex);
1998
#ifdef CONFIG_SND_AC97_POWER_SAVE
D
David Howells 已提交
1999
	INIT_DELAYED_WORK(&ac97->power_work, do_update_power);
2000
#endif
L
Linus Torvalds 已提交
2001

2002
#ifdef CONFIG_PCI
L
Linus Torvalds 已提交
2003 2004 2005 2006
	if (ac97->pci) {
		pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor);
		pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device);
	}
2007
#endif
L
Linus Torvalds 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
	if (bus->ops->reset) {
		bus->ops->reset(ac97);
		goto __access_ok;
	}

	ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
	ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
	if (ac97->id && ac97->id != (unsigned int)-1) {
		pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
		if (pid && (pid->flags & AC97_DEFAULT_POWER_OFF))
			goto __access_ok;
	}

S
Sasha Khapyorsky 已提交
2021 2022 2023 2024 2025
	/* reset to defaults */
	if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO))
		snd_ac97_write(ac97, AC97_RESET, 0);
	if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM))
		snd_ac97_write(ac97, AC97_EXTENDED_MID, 0);
L
Linus Torvalds 已提交
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
	if (bus->ops->wait)
		bus->ops->wait(ac97);
	else {
		udelay(50);
		if (ac97->scaps & AC97_SCAP_SKIP_AUDIO)
			err = ac97_reset_wait(ac97, HZ/2, 1);
		else {
			err = ac97_reset_wait(ac97, HZ/2, 0);
			if (err < 0)
				err = ac97_reset_wait(ac97, HZ/2, 1);
		}
		if (err < 0) {
			snd_printk(KERN_WARNING "AC'97 %d does not respond - RESET\n", ac97->num);
			/* proceed anyway - it's often non-critical */
		}
	}
      __access_ok:
	ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
	ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
	if (! (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) &&
	    (ac97->id == 0x00000000 || ac97->id == 0xffffffff)) {
		snd_printk(KERN_ERR "AC'97 %d access is not valid [0x%x], removing mixer.\n", ac97->num, ac97->id);
		snd_ac97_free(ac97);
		return -EIO;
	}
	pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
	if (pid)
		ac97->flags |= pid->flags;
	
	/* test for AC'97 */
	if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) {
		/* test if we can write to the record gain volume register */
		snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06);
		if (((err = snd_ac97_read(ac97, AC97_REC_GAIN)) & 0x7fff) == 0x0a06)
			ac97->scaps |= AC97_SCAP_AUDIO;
	}
	if (ac97->scaps & AC97_SCAP_AUDIO) {
		ac97->caps = snd_ac97_read(ac97, AC97_RESET);
		ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
		if (ac97->ext_id == 0xffff)	/* invalid combination */
			ac97->ext_id = 0;
	}

	/* test for MC'97 */
	if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) {
		ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
		if (ac97->ext_mid == 0xffff)	/* invalid combination */
			ac97->ext_mid = 0;
		if (ac97->ext_mid & 1)
			ac97->scaps |= AC97_SCAP_MODEM;
	}

	if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) {
		if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM)))
			snd_printk(KERN_ERR "AC'97 %d access error (not audio or modem codec)\n", ac97->num);
		snd_ac97_free(ac97);
		return -EACCES;
	}

	if (bus->ops->reset) // FIXME: always skipping?
		goto __ready_ok;

	/* FIXME: add powerdown control */
	if (ac97_is_audio(ac97)) {
		/* nothing should be in powerdown mode */
		snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
		if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
			snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */
			udelay(100);
			snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
		}
		/* nothing should be in powerdown mode */
		snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0);
		end_time = jiffies + (HZ / 10);
		do {
			if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f)
				goto __ready_ok;
2103
			schedule_timeout_uninterruptible(1);
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
		} while (time_after_eq(end_time, jiffies));
		snd_printk(KERN_WARNING "AC'97 %d analog subsections not ready\n", ac97->num);
	}

	/* FIXME: add powerdown control */
	if (ac97_is_modem(ac97)) {
		unsigned char tmp;

		/* nothing should be in powerdown mode */
		/* note: it's important to set the rate at first */
		tmp = AC97_MEA_GPIO;
		if (ac97->ext_mid & AC97_MEI_LINE1) {
2116
			snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 8000);
L
Linus Torvalds 已提交
2117 2118 2119
			tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1;
		}
		if (ac97->ext_mid & AC97_MEI_LINE2) {
2120
			snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 8000);
L
Linus Torvalds 已提交
2121 2122 2123
			tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2;
		}
		if (ac97->ext_mid & AC97_MEI_HANDSET) {
2124
			snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 8000);
L
Linus Torvalds 已提交
2125 2126
			tmp |= AC97_MEA_HADC | AC97_MEA_HDAC;
		}
2127
		snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
L
Linus Torvalds 已提交
2128 2129
		udelay(100);
		/* nothing should be in powerdown mode */
2130
		snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
L
Linus Torvalds 已提交
2131 2132 2133 2134
		end_time = jiffies + (HZ / 10);
		do {
			if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp)
				goto __ready_ok;
2135
			schedule_timeout_uninterruptible(1);
L
Linus Torvalds 已提交
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
		} while (time_after_eq(end_time, jiffies));
		snd_printk(KERN_WARNING "MC'97 %d converters and GPIO not ready (0x%x)\n", ac97->num, snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS));
	}
	
      __ready_ok:
	if (ac97_is_audio(ac97))
		ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT;
	else
		ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT;
	if (ac97->ext_id & 0x01c9) {	/* L/R, MIC, SDAC, LDAC VRA support */
		reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
		reg |= ac97->ext_id & 0x01c0; /* LDAC/SDAC/CDAC */
		if (! bus->no_vra)
			reg |= ac97->ext_id & 0x0009; /* VRA/VRM */
		snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
	}
	if ((ac97->ext_id & AC97_EI_DRA) && bus->dra) {
		/* Intel controllers require double rate data to be put in
		 * slots 7+8, so let's hope the codec supports it. */
		snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, AC97_GP_DRSS_78);
		if ((snd_ac97_read(ac97, AC97_GENERAL_PURPOSE) & AC97_GP_DRSS_MASK) == AC97_GP_DRSS_78)
			ac97->flags |= AC97_DOUBLE_RATE;
2158 2159
		/* restore to slots 10/11 to avoid the confliction with surrounds */
		snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, 0);
L
Linus Torvalds 已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
	}
	if (ac97->ext_id & AC97_EI_VRA) {	/* VRA support */
		snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]);
		snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]);
	} else {
		ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000;
		if (ac97->flags & AC97_DOUBLE_RATE)
			ac97->rates[AC97_RATES_FRONT_DAC] |= SNDRV_PCM_RATE_96000;
		ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000;
	}
	if (ac97->ext_id & AC97_EI_SPDIF) {
		/* codec specific code (patch) should override these values */
		ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000;
	}
	if (ac97->ext_id & AC97_EI_VRM) {	/* MIC VRA support */
		snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]);
	} else {
		ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000;
	}
	if (ac97->ext_id & AC97_EI_SDAC) {	/* SDAC support */
		snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]);
		ac97->scaps |= AC97_SCAP_SURROUND_DAC;
	}
	if (ac97->ext_id & AC97_EI_LDAC) {	/* LDAC support */
		snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]);
		ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
	}
	/* additional initializations */
	if (bus->ops->init)
		bus->ops->init(ac97);
	snd_ac97_get_name(ac97, ac97->id, name, !ac97_is_audio(ac97));
	snd_ac97_get_name(NULL, ac97->id, name, !ac97_is_audio(ac97));  // ac97->id might be changed in the special setup code
	if (! ac97->build_ops)
		ac97->build_ops = &null_build_ops;

	if (ac97_is_audio(ac97)) {
		char comp[16];
		if (card->mixername[0] == '\0') {
			strcpy(card->mixername, name);
		} else {
			if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
				strcat(card->mixername, ",");
				strcat(card->mixername, name);
			}
		}
		sprintf(comp, "AC97a:%08x", ac97->id);
		if ((err = snd_component_add(card, comp)) < 0) {
			snd_ac97_free(ac97);
			return err;
		}
		if (snd_ac97_mixer_build(ac97) < 0) {
			snd_ac97_free(ac97);
			return -ENOMEM;
		}
	}
	if (ac97_is_modem(ac97)) {
		char comp[16];
		if (card->mixername[0] == '\0') {
			strcpy(card->mixername, name);
		} else {
			if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
				strcat(card->mixername, ",");
				strcat(card->mixername, name);
			}
		}
		sprintf(comp, "AC97m:%08x", ac97->id);
		if ((err = snd_component_add(card, comp)) < 0) {
			snd_ac97_free(ac97);
			return err;
		}
		if (snd_ac97_modem_build(card, ac97) < 0) {
			snd_ac97_free(ac97);
			return -ENOMEM;
		}
	}
2235 2236
	if (ac97_is_audio(ac97))
		update_power_regs(ac97);
L
Linus Torvalds 已提交
2237 2238 2239 2240 2241 2242 2243 2244 2245
	snd_ac97_proc_init(ac97);
	if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ac97, &ops)) < 0) {
		snd_ac97_free(ac97);
		return err;
	}
	*rac97 = ac97;
	return 0;
}

2246
EXPORT_SYMBOL(snd_ac97_mixer);
L
Linus Torvalds 已提交
2247 2248 2249 2250 2251 2252 2253

/*
 * Power down the chip.
 *
 * MASTER and HEADPHONE registers are muted but the register cache values
 * are not changed, so that the values can be restored in snd_ac97_resume().
 */
2254
static void snd_ac97_powerdown(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2255 2256 2257 2258 2259 2260 2261 2262 2263
{
	unsigned short power;

	if (ac97_is_audio(ac97)) {
		/* some codecs have stereo mute bits */
		snd_ac97_write(ac97, AC97_MASTER, 0x9f9f);
		snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f);
	}

2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
	/* surround, CLFE, mic powerdown */
	power = ac97->regs[AC97_EXTENDED_STATUS];
	if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
		power |= AC97_EA_PRJ;
	if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
		power |= AC97_EA_PRI | AC97_EA_PRK;
	power |= AC97_EA_PRL;
	snd_ac97_write(ac97, AC97_EXTENDED_STATUS, power);

	/* powerdown external amplifier */
	if (ac97->scaps & AC97_SCAP_INV_EAPD)
		power = ac97->regs[AC97_POWERDOWN] & ~AC97_PD_EAPD;
	else if (! (ac97->scaps & AC97_SCAP_EAPD_LED))
		power = ac97->regs[AC97_POWERDOWN] | AC97_PD_EAPD;
	power |= AC97_PD_PR6;	/* Headphone amplifier powerdown */
	power |= AC97_PD_PR0 | AC97_PD_PR1;	/* ADC & DAC powerdown */
L
Linus Torvalds 已提交
2280 2281
	snd_ac97_write(ac97, AC97_POWERDOWN, power);
	udelay(100);
2282
	power |= AC97_PD_PR2 | AC97_PD_PR3;	/* Analog Mixer powerdown */
L
Linus Torvalds 已提交
2283
	snd_ac97_write(ac97, AC97_POWERDOWN, power);
2284
	if (ac97_is_power_save_mode(ac97)) {
2285 2286 2287 2288 2289 2290
		udelay(100);
		/* AC-link powerdown, internal Clk disable */
		/* FIXME: this may cause click noises on some boards */
		power |= AC97_PD_PR4 | AC97_PD_PR5;
		snd_ac97_write(ac97, AC97_POWERDOWN, power);
	}
L
Linus Torvalds 已提交
2291 2292 2293
}


2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
struct ac97_power_reg {
	unsigned short reg;
	unsigned short power_reg;
	unsigned short mask;
};

enum { PWIDX_ADC, PWIDX_FRONT, PWIDX_CLFE, PWIDX_SURR, PWIDX_MIC, PWIDX_SIZE };

static struct ac97_power_reg power_regs[PWIDX_SIZE] = {
	[PWIDX_ADC] = { AC97_PCM_LR_ADC_RATE, AC97_POWERDOWN, AC97_PD_PR0},
	[PWIDX_FRONT] = { AC97_PCM_FRONT_DAC_RATE, AC97_POWERDOWN, AC97_PD_PR1},
	[PWIDX_CLFE] = { AC97_PCM_LFE_DAC_RATE, AC97_EXTENDED_STATUS,
			 AC97_EA_PRI | AC97_EA_PRK},
	[PWIDX_SURR] = { AC97_PCM_SURR_DAC_RATE, AC97_EXTENDED_STATUS,
			 AC97_EA_PRJ},
	[PWIDX_MIC] = { AC97_PCM_MIC_ADC_RATE, AC97_EXTENDED_STATUS,
			AC97_EA_PRL},
};

#ifdef CONFIG_SND_AC97_POWER_SAVE
/**
 * snd_ac97_update_power - update the powerdown register
 * @ac97: the codec instance
 * @reg: the rate register, e.g. AC97_PCM_FRONT_DAC_RATE
 * @powerup: non-zero when power up the part
 *
 * Update the AC97 powerdown register bits of the given part.
 */
int snd_ac97_update_power(struct snd_ac97 *ac97, int reg, int powerup)
{
	int i;

	if (! ac97)
		return 0;

	if (reg) {
		/* SPDIF requires DAC power, too */
		if (reg == AC97_SPDIF)
			reg = AC97_PCM_FRONT_DAC_RATE;
		for (i = 0; i < PWIDX_SIZE; i++) {
			if (power_regs[i].reg == reg) {
				if (powerup)
					ac97->power_up |= (1 << i);
				else
					ac97->power_up &= ~(1 << i);
				break;
			}
		}
	}

2344
	if (ac97_is_power_save_mode(ac97) && !powerup)
2345 2346 2347 2348
		/* adjust power-down bits after two seconds delay
		 * (for avoiding loud click noises for many (OSS) apps
		 *  that open/close frequently)
		 */
2349 2350 2351
		schedule_delayed_work(&ac97->power_work, HZ*2);
	else {
		cancel_delayed_work(&ac97->power_work);
2352
		update_power_regs(ac97);
2353
	}
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365

	return 0;
}

EXPORT_SYMBOL(snd_ac97_update_power);
#endif /* CONFIG_SND_AC97_POWER_SAVE */

static void update_power_regs(struct snd_ac97 *ac97)
{
	unsigned int power_up, bits;
	int i;

2366 2367 2368 2369 2370 2371
	power_up = (1 << PWIDX_FRONT) | (1 << PWIDX_ADC);
	power_up |= (1 << PWIDX_MIC);
	if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
		power_up |= (1 << PWIDX_SURR);
	if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
		power_up |= (1 << PWIDX_CLFE);
2372
#ifdef CONFIG_SND_AC97_POWER_SAVE
2373
	if (ac97_is_power_save_mode(ac97))
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
		power_up = ac97->power_up;
#endif
	if (power_up) {
		if (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2) {
			/* needs power-up analog mix and vref */
			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
					     AC97_PD_PR3, 0);
			msleep(1);
			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
					     AC97_PD_PR2, 0);
		}
	}
	for (i = 0; i < PWIDX_SIZE; i++) {
		if (power_up & (1 << i))
			bits = 0;
		else
			bits = power_regs[i].mask;
		snd_ac97_update_bits(ac97, power_regs[i].power_reg,
				     power_regs[i].mask, bits);
	}
	if (! power_up) {
		if (! (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2)) {
			/* power down analog mix and vref */
			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
					     AC97_PD_PR2, AC97_PD_PR2);
			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
					     AC97_PD_PR3, AC97_PD_PR3);
		}
	}
}


L
Linus Torvalds 已提交
2406 2407 2408 2409 2410 2411 2412
#ifdef CONFIG_PM
/**
 * snd_ac97_suspend - General suspend function for AC97 codec
 * @ac97: the ac97 instance
 *
 * Suspends the codec, power down the chip.
 */
2413
void snd_ac97_suspend(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2414
{
2415 2416
	if (! ac97)
		return;
L
Linus Torvalds 已提交
2417 2418
	if (ac97->build_ops->suspend)
		ac97->build_ops->suspend(ac97);
2419 2420 2421 2422
#ifdef CONFIG_SND_AC97_POWER_SAVE
	cancel_delayed_work(&ac97->power_work);
	flush_scheduled_work();
#endif
L
Linus Torvalds 已提交
2423 2424 2425
	snd_ac97_powerdown(ac97);
}

2426 2427
EXPORT_SYMBOL(snd_ac97_suspend);

L
Linus Torvalds 已提交
2428 2429 2430
/*
 * restore ac97 status
 */
2431
void snd_ac97_restore_status(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
{
	int i;

	for (i = 2; i < 0x7c ; i += 2) {
		if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
			continue;
		/* restore only accessible registers
		 * some chip (e.g. nm256) may hang up when unsupported registers
		 * are accessed..!
		 */
		if (test_bit(i, ac97->reg_accessed)) {
			snd_ac97_write(ac97, i, ac97->regs[i]);
			snd_ac97_read(ac97, i);
		}
	}
}

/*
 * restore IEC958 status
 */
2452
void snd_ac97_restore_iec958(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474
{
	if (ac97->ext_id & AC97_EI_SPDIF) {
		if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) {
			/* reset spdif status */
			snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
			snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]);
			if (ac97->flags & AC97_CS_SPDIF)
				snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]);
			else
				snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]);
			snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
		}
	}
}

/**
 * snd_ac97_resume - General resume function for AC97 codec
 * @ac97: the ac97 instance
 *
 * Do the standard resume procedure, power up and restoring the
 * old register values.
 */
2475
void snd_ac97_resume(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2476
{
2477
	unsigned long end_time;
L
Linus Torvalds 已提交
2478

2479 2480 2481
	if (! ac97)
		return;

L
Linus Torvalds 已提交
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
	if (ac97->bus->ops->reset) {
		ac97->bus->ops->reset(ac97);
		goto  __reset_ready;
	}

	snd_ac97_write(ac97, AC97_POWERDOWN, 0);
	if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
		snd_ac97_write(ac97, AC97_RESET, 0);
		udelay(100);
		snd_ac97_write(ac97, AC97_POWERDOWN, 0);
	}
	snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0);

	snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]);
	if (ac97_is_audio(ac97)) {
		ac97->bus->ops->write(ac97, AC97_MASTER, 0x8101);
2498 2499
		end_time = jiffies + msecs_to_jiffies(100);
		do {
L
Linus Torvalds 已提交
2500 2501
			if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101)
				break;
2502
			schedule_timeout_uninterruptible(1);
2503
		} while (time_after_eq(end_time, jiffies));
L
Linus Torvalds 已提交
2504 2505
		/* FIXME: extra delay */
		ac97->bus->ops->write(ac97, AC97_MASTER, 0x8000);
2506 2507
		if (snd_ac97_read(ac97, AC97_MASTER) != 0x8000)
			msleep(250);
L
Linus Torvalds 已提交
2508
	} else {
2509 2510
		end_time = jiffies + msecs_to_jiffies(100);
		do {
L
Linus Torvalds 已提交
2511 2512 2513
			unsigned short val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
			if (val != 0xffff && (val & 1) != 0)
				break;
2514
			schedule_timeout_uninterruptible(1);
2515
		} while (time_after_eq(end_time, jiffies));
L
Linus Torvalds 已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
	}
__reset_ready:

	if (ac97->bus->ops->init)
		ac97->bus->ops->init(ac97);

	if (ac97->build_ops->resume)
		ac97->build_ops->resume(ac97);
	else {
		snd_ac97_restore_status(ac97);
		snd_ac97_restore_iec958(ac97);
	}
}
2529 2530

EXPORT_SYMBOL(snd_ac97_resume);
L
Linus Torvalds 已提交
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
#endif


/*
 * Hardware tuning
 */
static void set_ctl_name(char *dst, const char *src, const char *suffix)
{
	if (suffix)
		sprintf(dst, "%s %s", src, suffix);
	else
		strcpy(dst, src);
}	

/* remove the control with the given name and optional suffix */
2546
int snd_ac97_remove_ctl(struct snd_ac97 *ac97, const char *name, const char *suffix)
L
Linus Torvalds 已提交
2547
{
2548
	struct snd_ctl_elem_id id;
L
Linus Torvalds 已提交
2549 2550 2551 2552 2553 2554
	memset(&id, 0, sizeof(id));
	set_ctl_name(id.name, name, suffix);
	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	return snd_ctl_remove_id(ac97->bus->card, &id);
}

2555
static struct snd_kcontrol *ctl_find(struct snd_ac97 *ac97, const char *name, const char *suffix)
L
Linus Torvalds 已提交
2556
{
2557
	struct snd_ctl_elem_id sid;
L
Linus Torvalds 已提交
2558 2559 2560 2561 2562 2563 2564
	memset(&sid, 0, sizeof(sid));
	set_ctl_name(sid.name, name, suffix);
	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	return snd_ctl_find_id(ac97->bus->card, &sid);
}

/* rename the control with the given name and optional suffix */
2565
int snd_ac97_rename_ctl(struct snd_ac97 *ac97, const char *src, const char *dst, const char *suffix)
L
Linus Torvalds 已提交
2566
{
2567
	struct snd_kcontrol *kctl = ctl_find(ac97, src, suffix);
L
Linus Torvalds 已提交
2568 2569 2570 2571 2572 2573 2574 2575
	if (kctl) {
		set_ctl_name(kctl->id.name, dst, suffix);
		return 0;
	}
	return -ENOENT;
}

/* rename both Volume and Switch controls - don't check the return value */
2576
void snd_ac97_rename_vol_ctl(struct snd_ac97 *ac97, const char *src, const char *dst)
L
Linus Torvalds 已提交
2577 2578 2579 2580 2581 2582
{
	snd_ac97_rename_ctl(ac97, src, dst, "Switch");
	snd_ac97_rename_ctl(ac97, src, dst, "Volume");
}

/* swap controls */
2583
int snd_ac97_swap_ctl(struct snd_ac97 *ac97, const char *s1, const char *s2, const char *suffix)
L
Linus Torvalds 已提交
2584
{
2585
	struct snd_kcontrol *kctl1, *kctl2;
L
Linus Torvalds 已提交
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
	kctl1 = ctl_find(ac97, s1, suffix);
	kctl2 = ctl_find(ac97, s2, suffix);
	if (kctl1 && kctl2) {
		set_ctl_name(kctl1->id.name, s2, suffix);
		set_ctl_name(kctl2->id.name, s1, suffix);
		return 0;
	}
	return -ENOENT;
}

#if 1
/* bind hp and master controls instead of using only hp control */
2598
static int bind_hp_volsw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
{
	int err = snd_ac97_put_volsw(kcontrol, ucontrol);
	if (err > 0) {
		unsigned long priv_saved = kcontrol->private_value;
		kcontrol->private_value = (kcontrol->private_value & ~0xff) | AC97_HEADPHONE;
		snd_ac97_put_volsw(kcontrol, ucontrol);
		kcontrol->private_value = priv_saved;
	}
	return err;
}

/* ac97 tune: bind Master and Headphone controls */
2611
static int tune_hp_only(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2612
{
2613 2614
	struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
	struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
L
Linus Torvalds 已提交
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
	if (! msw || ! mvol)
		return -ENOENT;
	msw->put = bind_hp_volsw_put;
	mvol->put = bind_hp_volsw_put;
	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
	return 0;
}

#else
/* ac97 tune: use Headphone control as master */
2626
static int tune_hp_only(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
{
	if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
		return -ENOENT;
	snd_ac97_remove_ctl(ac97, "Master Playback", "Switch");
	snd_ac97_remove_ctl(ac97, "Master Playback", "Volume");
	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
	return 0;
}
#endif

/* ac97 tune: swap Headphone and Master controls */
2638
static int tune_swap_hp(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2639 2640 2641 2642 2643 2644 2645 2646 2647
{
	if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
		return -ENOENT;
	snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Line-Out Playback");
	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
	return 0;
}

/* ac97 tune: swap Surround and Master controls */
2648
static int tune_swap_surround(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2649 2650 2651 2652 2653 2654 2655 2656
{
	if (snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Switch") ||
	    snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Volume"))
		return -ENOENT;
	return 0;
}

/* ac97 tune: set up mic sharing for AD codecs */
2657
static int tune_ad_sharing(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
{
	unsigned short scfg;
	if ((ac97->id & 0xffffff00) != 0x41445300) {
		snd_printk(KERN_ERR "ac97_quirk AD_SHARING is only for AD codecs\n");
		return -EINVAL;
	}
	/* Turn on OMS bit to route microphone to back panel */
	scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
	snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200);
	return 0;
}

2670
static const struct snd_kcontrol_new snd_ac97_alc_jack_detect = 
L
Linus Torvalds 已提交
2671 2672 2673
AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0);

/* ac97 tune: set up ALC jack-select */
2674
static int tune_alc_jack(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2675 2676 2677 2678 2679 2680 2681
{
	if ((ac97->id & 0xffffff00) != 0x414c4700) {
		snd_printk(KERN_ERR "ac97_quirk ALC_JACK is only for Realtek codecs\n");
		return -EINVAL;
	}
	snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */
	snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */
2682 2683
	if (ac97->id == AC97_ID_ALC658D)
		snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800);
L
Linus Torvalds 已提交
2684 2685 2686 2687
	return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97));
}

/* ac97 tune: inversed EAPD bit */
2688
static int tune_inv_eapd(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2689
{
2690
	struct snd_kcontrol *kctl = ctl_find(ac97, "External Amplifier", NULL);
L
Linus Torvalds 已提交
2691 2692 2693 2694 2695 2696
	if (! kctl)
		return -ENOENT;
	set_inv_eapd(ac97, kctl);
	return 0;
}

2697
static int master_mute_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2698 2699 2700
{
	int err = snd_ac97_put_volsw(kcontrol, ucontrol);
	if (err > 0) {
2701
		struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
		int shift = (kcontrol->private_value >> 8) & 0x0f;
		int rshift = (kcontrol->private_value >> 12) & 0x0f;
		unsigned short mask;
		if (shift != rshift)
			mask = 0x8080;
		else
			mask = 0x8000;
		snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
				     (ac97->regs[AC97_MASTER] & mask) == mask ?
				     0x8000 : 0);
	}
	return err;
}

/* ac97 tune: EAPD controls mute LED bound with the master mute */
2717
static int tune_mute_led(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
2718
{
2719
	struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
L
Linus Torvalds 已提交
2720 2721 2722 2723 2724
	if (! msw)
		return -ENOENT;
	msw->put = master_mute_sw_put;
	snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
	snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
2725
	ac97->scaps |= AC97_SCAP_EAPD_LED;
L
Linus Torvalds 已提交
2726 2727 2728
	return 0;
}

2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
static int hp_master_mute_sw_put(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	int err = bind_hp_volsw_put(kcontrol, ucontrol);
	if (err > 0) {
		struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
		int shift = (kcontrol->private_value >> 8) & 0x0f;
		int rshift = (kcontrol->private_value >> 12) & 0x0f;
		unsigned short mask;
		if (shift != rshift)
			mask = 0x8080;
		else
			mask = 0x8000;
		snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
				     (ac97->regs[AC97_MASTER] & mask) == mask ?
				     0x8000 : 0);
	}
	return err;
}

static int tune_hp_mute_led(struct snd_ac97 *ac97)
{
	struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
	struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
	if (! msw || ! mvol)
		return -ENOENT;
	msw->put = hp_master_mute_sw_put;
	mvol->put = bind_hp_volsw_put;
	snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
	snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
	return 0;
}

L
Linus Torvalds 已提交
2764 2765
struct quirk_table {
	const char *name;
2766
	int (*func)(struct snd_ac97 *);
L
Linus Torvalds 已提交
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
};

static struct quirk_table applicable_quirks[] = {
	{ "none", NULL },
	{ "hp_only", tune_hp_only },
	{ "swap_hp", tune_swap_hp },
	{ "swap_surround", tune_swap_surround },
	{ "ad_sharing", tune_ad_sharing },
	{ "alc_jack", tune_alc_jack },
	{ "inv_eapd", tune_inv_eapd },
	{ "mute_led", tune_mute_led },
2778
	{ "hp_mute_led", tune_hp_mute_led },
L
Linus Torvalds 已提交
2779 2780 2781
};

/* apply the quirk with the given type */
2782
static int apply_quirk(struct snd_ac97 *ac97, int type)
L
Linus Torvalds 已提交
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
{
	if (type <= 0)
		return 0;
	else if (type >= ARRAY_SIZE(applicable_quirks))
		return -EINVAL;
	if (applicable_quirks[type].func)
		return applicable_quirks[type].func(ac97);
	return 0;
}

/* apply the quirk with the given name */
2794
static int apply_quirk_str(struct snd_ac97 *ac97, const char *typestr)
L
Linus Torvalds 已提交
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
{
	int i;
	struct quirk_table *q;

	for (i = 0; i < ARRAY_SIZE(applicable_quirks); i++) {
		q = &applicable_quirks[i];
		if (q->name && ! strcmp(typestr, q->name))
			return apply_quirk(ac97, i);
	}
	/* for compatibility, accept the numbers, too */
	if (*typestr >= '0' && *typestr <= '9')
		return apply_quirk(ac97, (int)simple_strtoul(typestr, NULL, 10));
	return -EINVAL;
}

/**
 * snd_ac97_tune_hardware - tune up the hardware
 * @ac97: the ac97 instance
 * @quirk: quirk list
 * @override: explicit quirk value (overrides the list if non-NULL)
 *
 * Do some workaround for each pci device, such as renaming of the
 * headphone (true line-out) control as "Master".
 * The quirk-list must be terminated with a zero-filled entry.
 *
 * Returns zero if successful, or a negative error code on failure.
 */

2823
int snd_ac97_tune_hardware(struct snd_ac97 *ac97, struct ac97_quirk *quirk, const char *override)
L
Linus Torvalds 已提交
2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
{
	int result;

	/* quirk overriden? */
	if (override && strcmp(override, "-1") && strcmp(override, "default")) {
		result = apply_quirk_str(ac97, override);
		if (result < 0)
			snd_printk(KERN_ERR "applying quirk type %s failed (%d)\n", override, result);
		return result;
	}

2835 2836 2837
	if (! quirk)
		return -EINVAL;

2838 2839
	for (; quirk->subvendor; quirk++) {
		if (quirk->subvendor != ac97->subsystem_vendor)
L
Linus Torvalds 已提交
2840
			continue;
2841 2842
		if ((! quirk->mask && quirk->subdevice == ac97->subsystem_device) ||
		    quirk->subdevice == (quirk->mask & ac97->subsystem_device)) {
L
Linus Torvalds 已提交
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
			if (quirk->codec_id && quirk->codec_id != ac97->id)
				continue;
			snd_printdd("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, ac97->subsystem_device);
			result = apply_quirk(ac97, quirk->type);
			if (result < 0)
				snd_printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
			return result;
		}
	}
	return 0;
}

EXPORT_SYMBOL(snd_ac97_tune_hardware);

/*
 *  INIT part
 */

static int __init alsa_ac97_init(void)
{
	return 0;
}

static void __exit alsa_ac97_exit(void)
{
}

module_init(alsa_ac97_init)
module_exit(alsa_ac97_exit)