xref: /openbmc/linux/sound/pci/ac97/ac97_codec.c (revision 3932b9ca)
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3  *  Universal interface for Audio Codec '97
4  *
5  *  For more details look to AC '97 component specification revision 2.2
6  *  by Intel Corporation (http://developer.intel.com).
7  *
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */
24 
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/pci.h>
29 #include <linux/module.h>
30 #include <linux/mutex.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/tlv.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/asoundef.h>
36 #include <sound/initval.h>
37 #include "ac97_id.h"
38 
39 #include "ac97_patch.c"
40 
41 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
42 MODULE_DESCRIPTION("Universal interface for Audio Codec '97");
43 MODULE_LICENSE("GPL");
44 
45 static bool enable_loopback;
46 
47 module_param(enable_loopback, bool, 0444);
48 MODULE_PARM_DESC(enable_loopback, "Enable AC97 ADC/DAC Loopback Control");
49 
50 #ifdef CONFIG_SND_AC97_POWER_SAVE
51 static int power_save = CONFIG_SND_AC97_POWER_SAVE_DEFAULT;
52 module_param(power_save, int, 0644);
53 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
54 		 "(in second, 0 = disable).");
55 #endif
56 /*
57 
58  */
59 
60 struct ac97_codec_id {
61 	unsigned int id;
62 	unsigned int mask;
63 	const char *name;
64 	int (*patch)(struct snd_ac97 *ac97);
65 	int (*mpatch)(struct snd_ac97 *ac97);
66 	unsigned int flags;
67 };
68 
69 static const struct ac97_codec_id snd_ac97_codec_id_vendors[] = {
70 { 0x41445300, 0xffffff00, "Analog Devices",	NULL,	NULL },
71 { 0x414b4d00, 0xffffff00, "Asahi Kasei",	NULL,	NULL },
72 { 0x414c4300, 0xffffff00, "Realtek",		NULL,	NULL },
73 { 0x414c4700, 0xffffff00, "Realtek",		NULL,	NULL },
74 /*
75  * This is an _inofficial_ Aztech Labs entry
76  * (value might differ from unknown official Aztech ID),
77  * currently used by the AC97 emulation of the almost-AC97 PCI168 card.
78  */
79 { 0x415a5400, 0xffffff00, "Aztech Labs (emulated)",	NULL,	NULL },
80 { 0x434d4900, 0xffffff00, "C-Media Electronics", NULL,	NULL },
81 { 0x43525900, 0xffffff00, "Cirrus Logic",	NULL,	NULL },
82 { 0x43585400, 0xffffff00, "Conexant",           NULL,	NULL },
83 { 0x44543000, 0xffffff00, "Diamond Technology", NULL,	NULL },
84 { 0x454d4300, 0xffffff00, "eMicro",		NULL,	NULL },
85 { 0x45838300, 0xffffff00, "ESS Technology",	NULL,	NULL },
86 { 0x48525300, 0xffffff00, "Intersil",		NULL,	NULL },
87 { 0x49434500, 0xffffff00, "ICEnsemble",		NULL,	NULL },
88 { 0x49544500, 0xffffff00, "ITE Tech.Inc",	NULL,	NULL },
89 { 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL },
90 { 0x50534300, 0xffffff00, "Philips",		NULL,	NULL },
91 { 0x53494c00, 0xffffff00, "Silicon Laboratory",	NULL,	NULL },
92 { 0x53544d00, 0xffffff00, "STMicroelectronics",	NULL,	NULL },
93 { 0x54524100, 0xffffff00, "TriTech",		NULL,	NULL },
94 { 0x54584e00, 0xffffff00, "Texas Instruments",	NULL,	NULL },
95 { 0x56494100, 0xffffff00, "VIA Technologies",   NULL,	NULL },
96 { 0x57454300, 0xffffff00, "Winbond",		NULL,	NULL },
97 { 0x574d4c00, 0xffffff00, "Wolfson",		NULL,	NULL },
98 { 0x594d4800, 0xffffff00, "Yamaha",		NULL,	NULL },
99 { 0x83847600, 0xffffff00, "SigmaTel",		NULL,	NULL },
100 { 0,	      0, 	  NULL,			NULL,	NULL }
101 };
102 
103 static const struct ac97_codec_id snd_ac97_codec_ids[] = {
104 { 0x41445303, 0xffffffff, "AD1819",		patch_ad1819,	NULL },
105 { 0x41445340, 0xffffffff, "AD1881",		patch_ad1881,	NULL },
106 { 0x41445348, 0xffffffff, "AD1881A",		patch_ad1881,	NULL },
107 { 0x41445360, 0xffffffff, "AD1885",		patch_ad1885,	NULL },
108 { 0x41445361, 0xffffffff, "AD1886",		patch_ad1886,	NULL },
109 { 0x41445362, 0xffffffff, "AD1887",		patch_ad1881,	NULL },
110 { 0x41445363, 0xffffffff, "AD1886A",		patch_ad1881,	NULL },
111 { 0x41445368, 0xffffffff, "AD1888",		patch_ad1888,	NULL },
112 { 0x41445370, 0xffffffff, "AD1980",		patch_ad1980,	NULL },
113 { 0x41445372, 0xffffffff, "AD1981A",		patch_ad1981a,	NULL },
114 { 0x41445374, 0xffffffff, "AD1981B",		patch_ad1981b,	NULL },
115 { 0x41445375, 0xffffffff, "AD1985",		patch_ad1985,	NULL },
116 { 0x41445378, 0xffffffff, "AD1986",		patch_ad1986,	NULL },
117 { 0x414b4d00, 0xffffffff, "AK4540",		NULL,		NULL },
118 { 0x414b4d01, 0xffffffff, "AK4542",		NULL,		NULL },
119 { 0x414b4d02, 0xffffffff, "AK4543",		NULL,		NULL },
120 { 0x414b4d06, 0xffffffff, "AK4544A",		NULL,		NULL },
121 { 0x414b4d07, 0xffffffff, "AK4545",		NULL,		NULL },
122 { 0x414c4300, 0xffffff00, "ALC100,100P", 	NULL,		NULL },
123 { 0x414c4710, 0xfffffff0, "ALC200,200P",	NULL,		NULL },
124 { 0x414c4721, 0xffffffff, "ALC650D",		NULL,	NULL }, /* already patched */
125 { 0x414c4722, 0xffffffff, "ALC650E",		NULL,	NULL }, /* already patched */
126 { 0x414c4723, 0xffffffff, "ALC650F",		NULL,	NULL }, /* already patched */
127 { 0x414c4720, 0xfffffff0, "ALC650",		patch_alc650,	NULL },
128 { 0x414c4730, 0xffffffff, "ALC101",		NULL,		NULL },
129 { 0x414c4740, 0xfffffff0, "ALC202",		NULL,		NULL },
130 { 0x414c4750, 0xfffffff0, "ALC250",		NULL,		NULL },
131 { 0x414c4760, 0xfffffff0, "ALC655",		patch_alc655,	NULL },
132 { 0x414c4770, 0xfffffff0, "ALC203",		patch_alc203,	NULL },
133 { 0x414c4781, 0xffffffff, "ALC658D",		NULL,	NULL }, /* already patched */
134 { 0x414c4780, 0xfffffff0, "ALC658",		patch_alc655,	NULL },
135 { 0x414c4790, 0xfffffff0, "ALC850",		patch_alc850,	NULL },
136 { 0x415a5401, 0xffffffff, "AZF3328",		patch_aztech_azf3328,	NULL },
137 { 0x434d4941, 0xffffffff, "CMI9738",		patch_cm9738,	NULL },
138 { 0x434d4961, 0xffffffff, "CMI9739",		patch_cm9739,	NULL },
139 { 0x434d4969, 0xffffffff, "CMI9780",		patch_cm9780,	NULL },
140 { 0x434d4978, 0xffffffff, "CMI9761A",		patch_cm9761,	NULL },
141 { 0x434d4982, 0xffffffff, "CMI9761B",		patch_cm9761,	NULL },
142 { 0x434d4983, 0xffffffff, "CMI9761A+",		patch_cm9761,	NULL },
143 { 0x43525900, 0xfffffff8, "CS4297",		NULL,		NULL },
144 { 0x43525910, 0xfffffff8, "CS4297A",		patch_cirrus_spdif,	NULL },
145 { 0x43525920, 0xfffffff8, "CS4298",		patch_cirrus_spdif,		NULL },
146 { 0x43525928, 0xfffffff8, "CS4294",		NULL,		NULL },
147 { 0x43525930, 0xfffffff8, "CS4299",		patch_cirrus_cs4299,	NULL },
148 { 0x43525948, 0xfffffff8, "CS4201",		NULL,		NULL },
149 { 0x43525958, 0xfffffff8, "CS4205",		patch_cirrus_spdif,	NULL },
150 { 0x43525960, 0xfffffff8, "CS4291",		NULL,		NULL },
151 { 0x43525970, 0xfffffff8, "CS4202",		NULL,		NULL },
152 { 0x43585421, 0xffffffff, "HSD11246",		NULL,		NULL },	// SmartMC II
153 { 0x43585428, 0xfffffff8, "Cx20468",		patch_conexant,	NULL }, // SmartAMC fixme: the mask might be different
154 { 0x43585430, 0xffffffff, "Cx20468-31",		patch_conexant, NULL },
155 { 0x43585431, 0xffffffff, "Cx20551",           patch_cx20551,  NULL },
156 { 0x44543031, 0xfffffff0, "DT0398",		NULL,		NULL },
157 { 0x454d4328, 0xffffffff, "EM28028",		NULL,		NULL },  // same as TR28028?
158 { 0x45838308, 0xffffffff, "ESS1988",		NULL,		NULL },
159 { 0x48525300, 0xffffff00, "HMP9701",		NULL,		NULL },
160 { 0x49434501, 0xffffffff, "ICE1230",		NULL,		NULL },
161 { 0x49434511, 0xffffffff, "ICE1232",		NULL,		NULL }, // alias VIA VT1611A?
162 { 0x49434514, 0xffffffff, "ICE1232A",		NULL,		NULL },
163 { 0x49434551, 0xffffffff, "VT1616", 		patch_vt1616,	NULL },
164 { 0x49434552, 0xffffffff, "VT1616i",		patch_vt1616,	NULL }, // VT1616 compatible (chipset integrated)
165 { 0x49544520, 0xffffffff, "IT2226E",		NULL,		NULL },
166 { 0x49544561, 0xffffffff, "IT2646E",		patch_it2646,	NULL },
167 { 0x4e534300, 0xffffffff, "LM4540,43,45,46,48",	NULL,		NULL }, // only guess --jk
168 { 0x4e534331, 0xffffffff, "LM4549",		NULL,		NULL },
169 { 0x4e534350, 0xffffffff, "LM4550",		patch_lm4550,  	NULL }, // volume wrap fix
170 { 0x50534304, 0xffffffff, "UCB1400",		patch_ucb1400,	NULL },
171 { 0x53494c20, 0xffffffe0, "Si3036,8",		mpatch_si3036,	mpatch_si3036, AC97_MODEM_PATCH },
172 { 0x53544d02, 0xffffffff, "ST7597",		NULL,		NULL },
173 { 0x54524102, 0xffffffff, "TR28022",		NULL,		NULL },
174 { 0x54524103, 0xffffffff, "TR28023",		NULL,		NULL },
175 { 0x54524106, 0xffffffff, "TR28026",		NULL,		NULL },
176 { 0x54524108, 0xffffffff, "TR28028",		patch_tritech_tr28028,	NULL }, // added by xin jin [07/09/99]
177 { 0x54524123, 0xffffffff, "TR28602",		NULL,		NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
178 { 0x54584e03, 0xffffffff, "TLV320AIC27",	NULL,		NULL },
179 { 0x54584e20, 0xffffffff, "TLC320AD9xC",	NULL,		NULL },
180 { 0x56494161, 0xffffffff, "VIA1612A",		NULL,		NULL }, // modified ICE1232 with S/PDIF
181 { 0x56494170, 0xffffffff, "VIA1617A",		patch_vt1617a,	NULL }, // modified VT1616 with S/PDIF
182 { 0x56494182, 0xffffffff, "VIA1618",		patch_vt1618,   NULL },
183 { 0x57454301, 0xffffffff, "W83971D",		NULL,		NULL },
184 { 0x574d4c00, 0xffffffff, "WM9701,WM9701A",	NULL,		NULL },
185 { 0x574d4C03, 0xffffffff, "WM9703,WM9707,WM9708,WM9717", patch_wolfson03, NULL},
186 { 0x574d4C04, 0xffffffff, "WM9704M,WM9704Q",	patch_wolfson04, NULL},
187 { 0x574d4C05, 0xffffffff, "WM9705,WM9710",	patch_wolfson05, NULL},
188 { 0x574d4C09, 0xffffffff, "WM9709",		NULL,		NULL},
189 { 0x574d4C12, 0xffffffff, "WM9711,WM9712,WM9715",	patch_wolfson11, NULL},
190 { 0x574d4c13, 0xffffffff, "WM9713,WM9714",	patch_wolfson13, NULL, AC97_DEFAULT_POWER_OFF},
191 { 0x594d4800, 0xffffffff, "YMF743",		patch_yamaha_ymf743,	NULL },
192 { 0x594d4802, 0xffffffff, "YMF752",		NULL,		NULL },
193 { 0x594d4803, 0xffffffff, "YMF753",		patch_yamaha_ymf753,	NULL },
194 { 0x83847600, 0xffffffff, "STAC9700,83,84",	patch_sigmatel_stac9700,	NULL },
195 { 0x83847604, 0xffffffff, "STAC9701,3,4,5",	NULL,		NULL },
196 { 0x83847605, 0xffffffff, "STAC9704",		NULL,		NULL },
197 { 0x83847608, 0xffffffff, "STAC9708,11",	patch_sigmatel_stac9708,	NULL },
198 { 0x83847609, 0xffffffff, "STAC9721,23",	patch_sigmatel_stac9721,	NULL },
199 { 0x83847644, 0xffffffff, "STAC9744",		patch_sigmatel_stac9744,	NULL },
200 { 0x83847650, 0xffffffff, "STAC9750,51",	NULL,		NULL },	// patch?
201 { 0x83847652, 0xffffffff, "STAC9752,53",	NULL,		NULL }, // patch?
202 { 0x83847656, 0xffffffff, "STAC9756,57",	patch_sigmatel_stac9756,	NULL },
203 { 0x83847658, 0xffffffff, "STAC9758,59",	patch_sigmatel_stac9758,	NULL },
204 { 0x83847666, 0xffffffff, "STAC9766,67",	NULL,		NULL }, // patch?
205 { 0, 	      0,	  NULL,			NULL,		NULL }
206 };
207 
208 
209 static void update_power_regs(struct snd_ac97 *ac97);
210 #ifdef CONFIG_SND_AC97_POWER_SAVE
211 #define ac97_is_power_save_mode(ac97) \
212 	((ac97->scaps & AC97_SCAP_POWER_SAVE) && power_save)
213 #else
214 #define ac97_is_power_save_mode(ac97) 0
215 #endif
216 
217 #define ac97_err(ac97, fmt, args...)	\
218 	dev_err((ac97)->bus->card->dev, fmt, ##args)
219 #define ac97_warn(ac97, fmt, args...)	\
220 	dev_warn((ac97)->bus->card->dev, fmt, ##args)
221 #define ac97_dbg(ac97, fmt, args...)	\
222 	dev_dbg((ac97)->bus->card->dev, fmt, ##args)
223 
224 /*
225  *  I/O routines
226  */
227 
228 static int snd_ac97_valid_reg(struct snd_ac97 *ac97, unsigned short reg)
229 {
230 	/* filter some registers for buggy codecs */
231 	switch (ac97->id) {
232 	case AC97_ID_ST_AC97_ID4:
233 		if (reg == 0x08)
234 			return 0;
235 		/* fall through */
236 	case AC97_ID_ST7597:
237 		if (reg == 0x22 || reg == 0x7a)
238 			return 1;
239 		/* fall through */
240 	case AC97_ID_AK4540:
241 	case AC97_ID_AK4542:
242 		if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
243 			return 1;
244 		return 0;
245 	case AC97_ID_AD1819:	/* AD1819 */
246 	case AC97_ID_AD1881:	/* AD1881 */
247 	case AC97_ID_AD1881A:	/* AD1881A */
248 		if (reg >= 0x3a && reg <= 0x6e)	/* 0x59 */
249 			return 0;
250 		return 1;
251 	case AC97_ID_AD1885:	/* AD1885 */
252 	case AC97_ID_AD1886:	/* AD1886 */
253 	case AC97_ID_AD1886A:	/* AD1886A - !!verify!! --jk */
254 	case AC97_ID_AD1887:	/* AD1887 - !!verify!! --jk */
255 		if (reg == 0x5a)
256 			return 1;
257 		if (reg >= 0x3c && reg <= 0x6e)	/* 0x59 */
258 			return 0;
259 		return 1;
260 	case AC97_ID_STAC9700:
261 	case AC97_ID_STAC9704:
262 	case AC97_ID_STAC9705:
263 	case AC97_ID_STAC9708:
264 	case AC97_ID_STAC9721:
265 	case AC97_ID_STAC9744:
266 	case AC97_ID_STAC9756:
267 		if (reg <= 0x3a || reg >= 0x5a)
268 			return 1;
269 		return 0;
270 	}
271 	return 1;
272 }
273 
274 /**
275  * snd_ac97_write - write a value on the given register
276  * @ac97: the ac97 instance
277  * @reg: the register to change
278  * @value: the value to set
279  *
280  * Writes a value on the given register.  This will invoke the write
281  * callback directly after the register check.
282  * This function doesn't change the register cache unlike
283  * #snd_ca97_write_cache(), so use this only when you don't want to
284  * reflect the change to the suspend/resume state.
285  */
286 void snd_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
287 {
288 	if (!snd_ac97_valid_reg(ac97, reg))
289 		return;
290 	if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) {
291 		/* Fix H/W bug of ALC100/100P */
292 		if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
293 			ac97->bus->ops->write(ac97, AC97_RESET, 0);	/* reset audio codec */
294 	}
295 	ac97->bus->ops->write(ac97, reg, value);
296 }
297 
298 EXPORT_SYMBOL(snd_ac97_write);
299 
300 /**
301  * snd_ac97_read - read a value from the given register
302  *
303  * @ac97: the ac97 instance
304  * @reg: the register to read
305  *
306  * Reads a value from the given register.  This will invoke the read
307  * callback directly after the register check.
308  *
309  * Return: The read value.
310  */
311 unsigned short snd_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
312 {
313 	if (!snd_ac97_valid_reg(ac97, reg))
314 		return 0;
315 	return ac97->bus->ops->read(ac97, reg);
316 }
317 
318 /* read a register - return the cached value if already read */
319 static inline unsigned short snd_ac97_read_cache(struct snd_ac97 *ac97, unsigned short reg)
320 {
321 	if (! test_bit(reg, ac97->reg_accessed)) {
322 		ac97->regs[reg] = ac97->bus->ops->read(ac97, reg);
323 		// set_bit(reg, ac97->reg_accessed);
324 	}
325 	return ac97->regs[reg];
326 }
327 
328 EXPORT_SYMBOL(snd_ac97_read);
329 
330 /**
331  * snd_ac97_write_cache - write a value on the given register and update the cache
332  * @ac97: the ac97 instance
333  * @reg: the register to change
334  * @value: the value to set
335  *
336  * Writes a value on the given register and updates the register
337  * cache.  The cached values are used for the cached-read and the
338  * suspend/resume.
339  */
340 void snd_ac97_write_cache(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
341 {
342 	if (!snd_ac97_valid_reg(ac97, reg))
343 		return;
344 	mutex_lock(&ac97->reg_mutex);
345 	ac97->regs[reg] = value;
346 	ac97->bus->ops->write(ac97, reg, value);
347 	set_bit(reg, ac97->reg_accessed);
348 	mutex_unlock(&ac97->reg_mutex);
349 }
350 
351 EXPORT_SYMBOL(snd_ac97_write_cache);
352 
353 /**
354  * snd_ac97_update - update the value on the given register
355  * @ac97: the ac97 instance
356  * @reg: the register to change
357  * @value: the value to set
358  *
359  * Compares the value with the register cache and updates the value
360  * only when the value is changed.
361  *
362  * Return: 1 if the value is changed, 0 if no change, or a negative
363  * code on failure.
364  */
365 int snd_ac97_update(struct snd_ac97 *ac97, unsigned short reg, unsigned short value)
366 {
367 	int change;
368 
369 	if (!snd_ac97_valid_reg(ac97, reg))
370 		return -EINVAL;
371 	mutex_lock(&ac97->reg_mutex);
372 	change = ac97->regs[reg] != value;
373 	if (change) {
374 		ac97->regs[reg] = value;
375 		ac97->bus->ops->write(ac97, reg, value);
376 	}
377 	set_bit(reg, ac97->reg_accessed);
378 	mutex_unlock(&ac97->reg_mutex);
379 	return change;
380 }
381 
382 EXPORT_SYMBOL(snd_ac97_update);
383 
384 /**
385  * snd_ac97_update_bits - update the bits on the given register
386  * @ac97: the ac97 instance
387  * @reg: the register to change
388  * @mask: the bit-mask to change
389  * @value: the value to set
390  *
391  * Updates the masked-bits on the given register only when the value
392  * is changed.
393  *
394  * Return: 1 if the bits are changed, 0 if no change, or a negative
395  * code on failure.
396  */
397 int snd_ac97_update_bits(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value)
398 {
399 	int change;
400 
401 	if (!snd_ac97_valid_reg(ac97, reg))
402 		return -EINVAL;
403 	mutex_lock(&ac97->reg_mutex);
404 	change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
405 	mutex_unlock(&ac97->reg_mutex);
406 	return change;
407 }
408 
409 EXPORT_SYMBOL(snd_ac97_update_bits);
410 
411 /* no lock version - see snd_ac97_update_bits() */
412 int snd_ac97_update_bits_nolock(struct snd_ac97 *ac97, unsigned short reg,
413 				unsigned short mask, unsigned short value)
414 {
415 	int change;
416 	unsigned short old, new;
417 
418 	old = snd_ac97_read_cache(ac97, reg);
419 	new = (old & ~mask) | (value & mask);
420 	change = old != new;
421 	if (change) {
422 		ac97->regs[reg] = new;
423 		ac97->bus->ops->write(ac97, reg, new);
424 	}
425 	set_bit(reg, ac97->reg_accessed);
426 	return change;
427 }
428 
429 static int snd_ac97_ad18xx_update_pcm_bits(struct snd_ac97 *ac97, int codec, unsigned short mask, unsigned short value)
430 {
431 	int change;
432 	unsigned short old, new, cfg;
433 
434 	mutex_lock(&ac97->page_mutex);
435 	old = ac97->spec.ad18xx.pcmreg[codec];
436 	new = (old & ~mask) | (value & mask);
437 	change = old != new;
438 	if (change) {
439 		mutex_lock(&ac97->reg_mutex);
440 		cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
441 		ac97->spec.ad18xx.pcmreg[codec] = new;
442 		/* select single codec */
443 		ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
444 				 (cfg & ~0x7000) |
445 				 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
446 		/* update PCM bits */
447 		ac97->bus->ops->write(ac97, AC97_PCM, new);
448 		/* select all codecs */
449 		ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
450 				 cfg | 0x7000);
451 		mutex_unlock(&ac97->reg_mutex);
452 	}
453 	mutex_unlock(&ac97->page_mutex);
454 	return change;
455 }
456 
457 /*
458  * Controls
459  */
460 
461 static int snd_ac97_info_enum_double(struct snd_kcontrol *kcontrol,
462 				     struct snd_ctl_elem_info *uinfo)
463 {
464 	struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
465 
466 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
467 	uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
468 	uinfo->value.enumerated.items = e->mask;
469 
470 	if (uinfo->value.enumerated.item > e->mask - 1)
471 		uinfo->value.enumerated.item = e->mask - 1;
472 	strcpy(uinfo->value.enumerated.name, e->texts[uinfo->value.enumerated.item]);
473 	return 0;
474 }
475 
476 static int snd_ac97_get_enum_double(struct snd_kcontrol *kcontrol,
477 				    struct snd_ctl_elem_value *ucontrol)
478 {
479 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
480 	struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
481 	unsigned short val, bitmask;
482 
483 	for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
484 		;
485 	val = snd_ac97_read_cache(ac97, e->reg);
486 	ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
487 	if (e->shift_l != e->shift_r)
488 		ucontrol->value.enumerated.item[1] = (val >> e->shift_r) & (bitmask - 1);
489 
490 	return 0;
491 }
492 
493 static int snd_ac97_put_enum_double(struct snd_kcontrol *kcontrol,
494 				    struct snd_ctl_elem_value *ucontrol)
495 {
496 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
497 	struct ac97_enum *e = (struct ac97_enum *)kcontrol->private_value;
498 	unsigned short val;
499 	unsigned short mask, bitmask;
500 
501 	for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
502 		;
503 	if (ucontrol->value.enumerated.item[0] > e->mask - 1)
504 		return -EINVAL;
505 	val = ucontrol->value.enumerated.item[0] << e->shift_l;
506 	mask = (bitmask - 1) << e->shift_l;
507 	if (e->shift_l != e->shift_r) {
508 		if (ucontrol->value.enumerated.item[1] > e->mask - 1)
509 			return -EINVAL;
510 		val |= ucontrol->value.enumerated.item[1] << e->shift_r;
511 		mask |= (bitmask - 1) << e->shift_r;
512 	}
513 	return snd_ac97_update_bits(ac97, e->reg, mask, val);
514 }
515 
516 /* save/restore ac97 v2.3 paging */
517 static int snd_ac97_page_save(struct snd_ac97 *ac97, int reg, struct snd_kcontrol *kcontrol)
518 {
519 	int page_save = -1;
520 	if ((kcontrol->private_value & (1<<25)) &&
521 	    (ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 &&
522 	    (reg >= 0x60 && reg < 0x70)) {
523 		unsigned short page = (kcontrol->private_value >> 26) & 0x0f;
524 		mutex_lock(&ac97->page_mutex); /* lock paging */
525 		page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
526 		snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
527 	}
528 	return page_save;
529 }
530 
531 static void snd_ac97_page_restore(struct snd_ac97 *ac97, int page_save)
532 {
533 	if (page_save >= 0) {
534 		snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
535 		mutex_unlock(&ac97->page_mutex); /* unlock paging */
536 	}
537 }
538 
539 /* volume and switch controls */
540 static int snd_ac97_info_volsw(struct snd_kcontrol *kcontrol,
541 			       struct snd_ctl_elem_info *uinfo)
542 {
543 	int mask = (kcontrol->private_value >> 16) & 0xff;
544 	int shift = (kcontrol->private_value >> 8) & 0x0f;
545 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
546 
547 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
548 	uinfo->count = shift == rshift ? 1 : 2;
549 	uinfo->value.integer.min = 0;
550 	uinfo->value.integer.max = mask;
551 	return 0;
552 }
553 
554 static int snd_ac97_get_volsw(struct snd_kcontrol *kcontrol,
555 			      struct snd_ctl_elem_value *ucontrol)
556 {
557 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
558 	int reg = kcontrol->private_value & 0xff;
559 	int shift = (kcontrol->private_value >> 8) & 0x0f;
560 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
561 	int mask = (kcontrol->private_value >> 16) & 0xff;
562 	int invert = (kcontrol->private_value >> 24) & 0x01;
563 	int page_save;
564 
565 	page_save = snd_ac97_page_save(ac97, reg, kcontrol);
566 	ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
567 	if (shift != rshift)
568 		ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask;
569 	if (invert) {
570 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
571 		if (shift != rshift)
572 			ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
573 	}
574 	snd_ac97_page_restore(ac97, page_save);
575 	return 0;
576 }
577 
578 static int snd_ac97_put_volsw(struct snd_kcontrol *kcontrol,
579 			      struct snd_ctl_elem_value *ucontrol)
580 {
581 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
582 	int reg = kcontrol->private_value & 0xff;
583 	int shift = (kcontrol->private_value >> 8) & 0x0f;
584 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
585 	int mask = (kcontrol->private_value >> 16) & 0xff;
586 	int invert = (kcontrol->private_value >> 24) & 0x01;
587 	int err, page_save;
588 	unsigned short val, val2, val_mask;
589 
590 	page_save = snd_ac97_page_save(ac97, reg, kcontrol);
591 	val = (ucontrol->value.integer.value[0] & mask);
592 	if (invert)
593 		val = mask - val;
594 	val_mask = mask << shift;
595 	val = val << shift;
596 	if (shift != rshift) {
597 		val2 = (ucontrol->value.integer.value[1] & mask);
598 		if (invert)
599 			val2 = mask - val2;
600 		val_mask |= mask << rshift;
601 		val |= val2 << rshift;
602 	}
603 	err = snd_ac97_update_bits(ac97, reg, val_mask, val);
604 	snd_ac97_page_restore(ac97, page_save);
605 #ifdef CONFIG_SND_AC97_POWER_SAVE
606 	/* check analog mixer power-down */
607 	if ((val_mask & AC97_PD_EAPD) &&
608 	    (kcontrol->private_value & (1<<30))) {
609 		if (val & AC97_PD_EAPD)
610 			ac97->power_up &= ~(1 << (reg>>1));
611 		else
612 			ac97->power_up |= 1 << (reg>>1);
613 		update_power_regs(ac97);
614 	}
615 #endif
616 	return err;
617 }
618 
619 static const struct snd_kcontrol_new snd_ac97_controls_master_mono[2] = {
620 AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
621 AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
622 };
623 
624 static const struct snd_kcontrol_new snd_ac97_controls_tone[2] = {
625 AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
626 AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
627 };
628 
629 static const struct snd_kcontrol_new snd_ac97_controls_pc_beep[2] = {
630 AC97_SINGLE("Beep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
631 AC97_SINGLE("Beep Playback Volume", AC97_PC_BEEP, 1, 15, 1)
632 };
633 
634 static const struct snd_kcontrol_new snd_ac97_controls_mic_boost =
635 	AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0);
636 
637 
638 static const char* std_rec_sel[] = {"Mic", "CD", "Video", "Aux", "Line", "Mix", "Mix Mono", "Phone"};
639 static const char* std_3d_path[] = {"pre 3D", "post 3D"};
640 static const char* std_mix[] = {"Mix", "Mic"};
641 static const char* std_mic[] = {"Mic1", "Mic2"};
642 
643 static const struct ac97_enum std_enum[] = {
644 AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, std_rec_sel),
645 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, std_3d_path),
646 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, std_mix),
647 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, std_mic),
648 };
649 
650 static const struct snd_kcontrol_new snd_ac97_control_capture_src =
651 AC97_ENUM("Capture Source", std_enum[0]);
652 
653 static const struct snd_kcontrol_new snd_ac97_control_capture_vol =
654 AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);
655 
656 static const struct snd_kcontrol_new snd_ac97_controls_mic_capture[2] = {
657 AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
658 AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
659 };
660 
661 enum {
662 	AC97_GENERAL_PCM_OUT = 0,
663 	AC97_GENERAL_STEREO_ENHANCEMENT,
664 	AC97_GENERAL_3D,
665 	AC97_GENERAL_LOUDNESS,
666 	AC97_GENERAL_MONO,
667 	AC97_GENERAL_MIC,
668 	AC97_GENERAL_LOOPBACK
669 };
670 
671 static const struct snd_kcontrol_new snd_ac97_controls_general[7] = {
672 AC97_ENUM("PCM Out Path & Mute", std_enum[1]),
673 AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
674 AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
675 AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
676 AC97_ENUM("Mono Output Select", std_enum[2]),
677 AC97_ENUM("Mic Select", std_enum[3]),
678 AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
679 };
680 
681 static const struct snd_kcontrol_new snd_ac97_controls_3d[2] = {
682 AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
683 AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
684 };
685 
686 static const struct snd_kcontrol_new snd_ac97_controls_center[2] = {
687 AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
688 AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
689 };
690 
691 static const struct snd_kcontrol_new snd_ac97_controls_lfe[2] = {
692 AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
693 AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
694 };
695 
696 static const struct snd_kcontrol_new snd_ac97_control_eapd =
697 AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);
698 
699 static const struct snd_kcontrol_new snd_ac97_controls_modem_switches[2] = {
700 AC97_SINGLE("Off-hook Switch", AC97_GPIO_STATUS, 0, 1, 0),
701 AC97_SINGLE("Caller ID Switch", AC97_GPIO_STATUS, 2, 1, 0)
702 };
703 
704 /* change the existing EAPD control as inverted */
705 static void set_inv_eapd(struct snd_ac97 *ac97, struct snd_kcontrol *kctl)
706 {
707 	kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0);
708 	snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */
709 	ac97->scaps |= AC97_SCAP_INV_EAPD;
710 }
711 
712 static int snd_ac97_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
713 {
714 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
715 	uinfo->count = 1;
716 	return 0;
717 }
718 
719 static int snd_ac97_spdif_cmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
720 {
721 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
722 					   IEC958_AES0_NONAUDIO |
723 					   IEC958_AES0_CON_EMPHASIS_5015 |
724 					   IEC958_AES0_CON_NOT_COPYRIGHT;
725 	ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
726 					   IEC958_AES1_CON_ORIGINAL;
727 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
728 	return 0;
729 }
730 
731 static int snd_ac97_spdif_pmask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
732 {
733 	/* FIXME: AC'97 spec doesn't say which bits are used for what */
734 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
735 					   IEC958_AES0_NONAUDIO |
736 					   IEC958_AES0_PRO_FS |
737 					   IEC958_AES0_PRO_EMPHASIS_5015;
738 	return 0;
739 }
740 
741 static int snd_ac97_spdif_default_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
742 {
743 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
744 
745 	mutex_lock(&ac97->reg_mutex);
746 	ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
747 	ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
748 	ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
749 	ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
750 	mutex_unlock(&ac97->reg_mutex);
751 	return 0;
752 }
753 
754 static int snd_ac97_spdif_default_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
755 {
756 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
757 	unsigned int new = 0;
758 	unsigned short val = 0;
759 	int change;
760 
761 	new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
762 	if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
763 		new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
764 		switch (new & IEC958_AES0_PRO_FS) {
765 		case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
766 		case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
767 		case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
768 		default:		       val |= 1<<12; break;
769 		}
770 		if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
771 			val |= 1<<3;
772 	} else {
773 		new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
774 		new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
775 		new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
776 		if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
777 			val |= 1<<3;
778 		if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
779 			val |= 1<<2;
780 		val |= ((new >> 8) & 0xff) << 4;	// category + original
781 		switch ((new >> 24) & 0xff) {
782 		case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
783 		case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
784 		case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
785 		default:		       val |= 1<<12; break;
786 		}
787 	}
788 
789 	mutex_lock(&ac97->reg_mutex);
790 	change = ac97->spdif_status != new;
791 	ac97->spdif_status = new;
792 
793 	if (ac97->flags & AC97_CS_SPDIF) {
794 		int x = (val >> 12) & 0x03;
795 		switch (x) {
796 		case 0: x = 1; break;  // 44.1
797 		case 2: x = 0; break;  // 48.0
798 		default: x = 0; break; // illegal.
799 		}
800 		change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
801 	} else if (ac97->flags & AC97_CX_SPDIF) {
802 		int v;
803 		v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
804 		v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
805 		change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC,
806 						      AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
807 						      v);
808 	} else if (ac97->id == AC97_ID_YMF743) {
809 		change |= snd_ac97_update_bits_nolock(ac97,
810 						      AC97_YMF7X3_DIT_CTRL,
811 						      0xff38,
812 						      ((val << 4) & 0xff00) |
813 						      ((val << 2) & 0x0038));
814 	} else {
815 		unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
816 		snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
817 
818 		change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val);
819 		if (extst & AC97_EA_SPDIF) {
820 			snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
821                 }
822 	}
823 	mutex_unlock(&ac97->reg_mutex);
824 
825 	return change;
826 }
827 
828 static int snd_ac97_put_spsa(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
829 {
830 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
831 	int reg = kcontrol->private_value & 0xff;
832 	int shift = (kcontrol->private_value >> 8) & 0xff;
833 	int mask = (kcontrol->private_value >> 16) & 0xff;
834 	// int invert = (kcontrol->private_value >> 24) & 0xff;
835 	unsigned short value, old, new;
836 	int change;
837 
838 	value = (ucontrol->value.integer.value[0] & mask);
839 
840 	mutex_lock(&ac97->reg_mutex);
841 	mask <<= shift;
842 	value <<= shift;
843 	old = snd_ac97_read_cache(ac97, reg);
844 	new = (old & ~mask) | value;
845 	change = old != new;
846 
847 	if (change) {
848 		unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
849 		snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
850 		change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
851 		if (extst & AC97_EA_SPDIF)
852 			snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
853 	}
854 	mutex_unlock(&ac97->reg_mutex);
855 	return change;
856 }
857 
858 static const struct snd_kcontrol_new snd_ac97_controls_spdif[5] = {
859 	{
860 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
861 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
862 		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
863 		.info = snd_ac97_spdif_mask_info,
864 		.get = snd_ac97_spdif_cmask_get,
865 	},
866 	{
867 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
868 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
869 		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
870 		.info = snd_ac97_spdif_mask_info,
871 		.get = snd_ac97_spdif_pmask_get,
872 	},
873 	{
874 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
875 		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
876 		.info = snd_ac97_spdif_mask_info,
877 		.get = snd_ac97_spdif_default_get,
878 		.put = snd_ac97_spdif_default_put,
879 	},
880 
881 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
882 	{
883 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
884 		.name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
885 		.info = snd_ac97_info_volsw,
886 		.get = snd_ac97_get_volsw,
887 		.put = snd_ac97_put_spsa,
888 		.private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
889 	},
890 };
891 
892 #define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
893 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
894   .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
895   .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }
896 
897 static int snd_ac97_ad18xx_pcm_info_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
898 {
899 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
900 	int mask = (kcontrol->private_value >> 16) & 0x0f;
901 	int lshift = (kcontrol->private_value >> 8) & 0x0f;
902 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
903 
904 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
905 	if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
906 		uinfo->count = 2;
907 	else
908 		uinfo->count = 1;
909 	uinfo->value.integer.min = 0;
910 	uinfo->value.integer.max = mask;
911 	return 0;
912 }
913 
914 static int snd_ac97_ad18xx_pcm_get_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
915 {
916 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
917 	int codec = kcontrol->private_value & 3;
918 	int lshift = (kcontrol->private_value >> 8) & 0x0f;
919 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
920 	int mask = (kcontrol->private_value >> 16) & 0xff;
921 
922 	ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
923 	if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
924 		ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
925 	return 0;
926 }
927 
928 static int snd_ac97_ad18xx_pcm_put_bits(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
929 {
930 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
931 	int codec = kcontrol->private_value & 3;
932 	int lshift = (kcontrol->private_value >> 8) & 0x0f;
933 	int rshift = (kcontrol->private_value >> 12) & 0x0f;
934 	int mask = (kcontrol->private_value >> 16) & 0xff;
935 	unsigned short val, valmask;
936 
937 	val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
938 	valmask = mask << lshift;
939 	if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
940 		val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
941 		valmask |= mask << rshift;
942 	}
943 	return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
944 }
945 
946 #define AD18XX_PCM_VOLUME(xname, codec) \
947 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
948   .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
949   .private_value = codec }
950 
951 static int snd_ac97_ad18xx_pcm_info_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
952 {
953 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
954 	uinfo->count = 2;
955 	uinfo->value.integer.min = 0;
956 	uinfo->value.integer.max = 31;
957 	return 0;
958 }
959 
960 static int snd_ac97_ad18xx_pcm_get_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
961 {
962 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
963 	int codec = kcontrol->private_value & 3;
964 
965 	mutex_lock(&ac97->page_mutex);
966 	ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
967 	ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
968 	mutex_unlock(&ac97->page_mutex);
969 	return 0;
970 }
971 
972 static int snd_ac97_ad18xx_pcm_put_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
973 {
974 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
975 	int codec = kcontrol->private_value & 3;
976 	unsigned short val1, val2;
977 
978 	val1 = 31 - (ucontrol->value.integer.value[0] & 31);
979 	val2 = 31 - (ucontrol->value.integer.value[1] & 31);
980 	return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
981 }
982 
983 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_pcm[2] = {
984 AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
985 AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
986 };
987 
988 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_surround[2] = {
989 AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
990 AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
991 };
992 
993 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_center[2] = {
994 AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
995 AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
996 };
997 
998 static const struct snd_kcontrol_new snd_ac97_controls_ad18xx_lfe[2] = {
999 AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
1000 AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
1001 };
1002 
1003 /*
1004  *
1005  */
1006 
1007 static void snd_ac97_powerdown(struct snd_ac97 *ac97);
1008 
1009 static int snd_ac97_bus_free(struct snd_ac97_bus *bus)
1010 {
1011 	if (bus) {
1012 		snd_ac97_bus_proc_done(bus);
1013 		kfree(bus->pcms);
1014 		if (bus->private_free)
1015 			bus->private_free(bus);
1016 		kfree(bus);
1017 	}
1018 	return 0;
1019 }
1020 
1021 static int snd_ac97_bus_dev_free(struct snd_device *device)
1022 {
1023 	struct snd_ac97_bus *bus = device->device_data;
1024 	return snd_ac97_bus_free(bus);
1025 }
1026 
1027 static int snd_ac97_free(struct snd_ac97 *ac97)
1028 {
1029 	if (ac97) {
1030 #ifdef CONFIG_SND_AC97_POWER_SAVE
1031 		cancel_delayed_work_sync(&ac97->power_work);
1032 #endif
1033 		snd_ac97_proc_done(ac97);
1034 		if (ac97->bus)
1035 			ac97->bus->codec[ac97->num] = NULL;
1036 		if (ac97->private_free)
1037 			ac97->private_free(ac97);
1038 		kfree(ac97);
1039 	}
1040 	return 0;
1041 }
1042 
1043 static int snd_ac97_dev_free(struct snd_device *device)
1044 {
1045 	struct snd_ac97 *ac97 = device->device_data;
1046 	snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
1047 	return snd_ac97_free(ac97);
1048 }
1049 
1050 static int snd_ac97_try_volume_mix(struct snd_ac97 * ac97, int reg)
1051 {
1052 	unsigned short val, mask = AC97_MUTE_MASK_MONO;
1053 
1054 	if (! snd_ac97_valid_reg(ac97, reg))
1055 		return 0;
1056 
1057 	switch (reg) {
1058 	case AC97_MASTER_TONE:
1059 		return ac97->caps & AC97_BC_BASS_TREBLE ? 1 : 0;
1060 	case AC97_HEADPHONE:
1061 		return ac97->caps & AC97_BC_HEADPHONE ? 1 : 0;
1062 	case AC97_REC_GAIN_MIC:
1063 		return ac97->caps & AC97_BC_DEDICATED_MIC ? 1 : 0;
1064 	case AC97_3D_CONTROL:
1065 		if (ac97->caps & AC97_BC_3D_TECH_ID_MASK) {
1066 			val = snd_ac97_read(ac97, reg);
1067 			/* if nonzero - fixed and we can't set it */
1068 			return val == 0;
1069 		}
1070 		return 0;
1071 	case AC97_CENTER_LFE_MASTER:	/* center */
1072 		if ((ac97->ext_id & AC97_EI_CDAC) == 0)
1073 			return 0;
1074 		break;
1075 	case AC97_CENTER_LFE_MASTER+1:	/* lfe */
1076 		if ((ac97->ext_id & AC97_EI_LDAC) == 0)
1077 			return 0;
1078 		reg = AC97_CENTER_LFE_MASTER;
1079 		mask = 0x0080;
1080 		break;
1081 	case AC97_SURROUND_MASTER:
1082 		if ((ac97->ext_id & AC97_EI_SDAC) == 0)
1083 			return 0;
1084 		break;
1085 	}
1086 
1087 	val = snd_ac97_read(ac97, reg);
1088 	if (!(val & mask)) {
1089 		/* nothing seems to be here - mute flag is not set */
1090 		/* try another test */
1091 		snd_ac97_write_cache(ac97, reg, val | mask);
1092 		val = snd_ac97_read(ac97, reg);
1093 		val = snd_ac97_read(ac97, reg);
1094 		if (!(val & mask))
1095 			return 0;	/* nothing here */
1096 	}
1097 	return 1;		/* success, useable */
1098 }
1099 
1100 static void check_volume_resolution(struct snd_ac97 *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max)
1101 {
1102 	unsigned short cbit[3] = { 0x20, 0x10, 0x01 };
1103 	unsigned char max[3] = { 63, 31, 15 };
1104 	int i;
1105 
1106 	/* first look up the static resolution table */
1107 	if (ac97->res_table) {
1108 		const struct snd_ac97_res_table *tbl;
1109 		for (tbl = ac97->res_table; tbl->reg; tbl++) {
1110 			if (tbl->reg == reg) {
1111 				*lo_max = tbl->bits & 0xff;
1112 				*hi_max = (tbl->bits >> 8) & 0xff;
1113 				return;
1114 			}
1115 		}
1116 	}
1117 
1118 	*lo_max = *hi_max = 0;
1119 	for (i = 0 ; i < ARRAY_SIZE(cbit); i++) {
1120 		unsigned short val;
1121 		snd_ac97_write(
1122 			ac97, reg,
1123 			AC97_MUTE_MASK_STEREO | cbit[i] | (cbit[i] << 8)
1124 		);
1125 		/* Do the read twice due to buffers on some ac97 codecs.
1126 		 * e.g. The STAC9704 returns exactly what you wrote to the register
1127 		 * if you read it immediately. This causes the detect routine to fail.
1128 		 */
1129 		val = snd_ac97_read(ac97, reg);
1130 		val = snd_ac97_read(ac97, reg);
1131 		if (! *lo_max && (val & 0x7f) == cbit[i])
1132 			*lo_max = max[i];
1133 		if (! *hi_max && ((val >> 8) & 0x7f) == cbit[i])
1134 			*hi_max = max[i];
1135 		if (*lo_max && *hi_max)
1136 			break;
1137 	}
1138 }
1139 
1140 static int snd_ac97_try_bit(struct snd_ac97 * ac97, int reg, int bit)
1141 {
1142 	unsigned short mask, val, orig, res;
1143 
1144 	mask = 1 << bit;
1145 	orig = snd_ac97_read(ac97, reg);
1146 	val = orig ^ mask;
1147 	snd_ac97_write(ac97, reg, val);
1148 	res = snd_ac97_read(ac97, reg);
1149 	snd_ac97_write_cache(ac97, reg, orig);
1150 	return res == val;
1151 }
1152 
1153 /* check the volume resolution of center/lfe */
1154 static void snd_ac97_change_volume_params2(struct snd_ac97 * ac97, int reg, int shift, unsigned char *max)
1155 {
1156 	unsigned short val, val1;
1157 
1158 	*max = 63;
1159 	val = AC97_MUTE_MASK_STEREO | (0x20 << shift);
1160 	snd_ac97_write(ac97, reg, val);
1161 	val1 = snd_ac97_read(ac97, reg);
1162 	if (val != val1) {
1163 		*max = 31;
1164 	}
1165 	/* reset volume to zero */
1166 	snd_ac97_write_cache(ac97, reg, AC97_MUTE_MASK_STEREO);
1167 }
1168 
1169 static inline int printable(unsigned int x)
1170 {
1171 	x &= 0xff;
1172 	if (x < ' ' || x >= 0x71) {
1173 		if (x <= 0x89)
1174 			return x - 0x71 + 'A';
1175 		return '?';
1176 	}
1177 	return x;
1178 }
1179 
1180 static struct snd_kcontrol *snd_ac97_cnew(const struct snd_kcontrol_new *_template,
1181 					  struct snd_ac97 * ac97)
1182 {
1183 	struct snd_kcontrol_new template;
1184 	memcpy(&template, _template, sizeof(template));
1185 	template.index = ac97->num;
1186 	return snd_ctl_new1(&template, ac97);
1187 }
1188 
1189 /*
1190  * create mute switch(es) for normal stereo controls
1191  */
1192 static int snd_ac97_cmute_new_stereo(struct snd_card *card, char *name, int reg,
1193 				     int check_stereo, int check_amix,
1194 				     struct snd_ac97 *ac97)
1195 {
1196 	struct snd_kcontrol *kctl;
1197 	int err;
1198 	unsigned short val, val1, mute_mask;
1199 
1200 	if (! snd_ac97_valid_reg(ac97, reg))
1201 		return 0;
1202 
1203 	mute_mask = AC97_MUTE_MASK_MONO;
1204 	val = snd_ac97_read(ac97, reg);
1205 	if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) {
1206 		/* check whether both mute bits work */
1207 		val1 = val | AC97_MUTE_MASK_STEREO;
1208 		snd_ac97_write(ac97, reg, val1);
1209 		if (val1 == snd_ac97_read(ac97, reg))
1210 			mute_mask = AC97_MUTE_MASK_STEREO;
1211 	}
1212 	if (mute_mask == AC97_MUTE_MASK_STEREO) {
1213 		struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
1214 		if (check_amix)
1215 			tmp.private_value |= (1 << 30);
1216 		tmp.index = ac97->num;
1217 		kctl = snd_ctl_new1(&tmp, ac97);
1218 	} else {
1219 		struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 15, 1, 1);
1220 		if (check_amix)
1221 			tmp.private_value |= (1 << 30);
1222 		tmp.index = ac97->num;
1223 		kctl = snd_ctl_new1(&tmp, ac97);
1224 	}
1225 	err = snd_ctl_add(card, kctl);
1226 	if (err < 0)
1227 		return err;
1228 	/* mute as default */
1229 	snd_ac97_write_cache(ac97, reg, val | mute_mask);
1230 	return 0;
1231 }
1232 
1233 /*
1234  * set dB information
1235  */
1236 static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
1237 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
1238 static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
1239 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
1240 static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
1241 
1242 static const unsigned int *find_db_scale(unsigned int maxval)
1243 {
1244 	switch (maxval) {
1245 	case 0x0f: return db_scale_4bit;
1246 	case 0x1f: return db_scale_5bit;
1247 	case 0x3f: return db_scale_6bit;
1248 	}
1249 	return NULL;
1250 }
1251 
1252 static void set_tlv_db_scale(struct snd_kcontrol *kctl, const unsigned int *tlv)
1253 {
1254 	kctl->tlv.p = tlv;
1255 	if (tlv)
1256 		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1257 }
1258 
1259 /*
1260  * create a volume for normal stereo/mono controls
1261  */
1262 static int snd_ac97_cvol_new(struct snd_card *card, char *name, int reg, unsigned int lo_max,
1263 			     unsigned int hi_max, struct snd_ac97 *ac97)
1264 {
1265 	int err;
1266 	struct snd_kcontrol *kctl;
1267 
1268 	if (! snd_ac97_valid_reg(ac97, reg))
1269 		return 0;
1270 	if (hi_max) {
1271 		/* invert */
1272 		struct snd_kcontrol_new tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1);
1273 		tmp.index = ac97->num;
1274 		kctl = snd_ctl_new1(&tmp, ac97);
1275 	} else {
1276 		/* invert */
1277 		struct snd_kcontrol_new tmp = AC97_SINGLE(name, reg, 0, lo_max, 1);
1278 		tmp.index = ac97->num;
1279 		kctl = snd_ctl_new1(&tmp, ac97);
1280 	}
1281 	if (!kctl)
1282 		return -ENOMEM;
1283 	if (reg >= AC97_PHONE && reg <= AC97_PCM)
1284 		set_tlv_db_scale(kctl, db_scale_5bit_12db_max);
1285 	else
1286 		set_tlv_db_scale(kctl, find_db_scale(lo_max));
1287 	err = snd_ctl_add(card, kctl);
1288 	if (err < 0)
1289 		return err;
1290 	snd_ac97_write_cache(
1291 		ac97, reg,
1292 		(snd_ac97_read(ac97, reg) & AC97_MUTE_MASK_STEREO)
1293 		| lo_max | (hi_max << 8)
1294 	);
1295 	return 0;
1296 }
1297 
1298 /*
1299  * create a mute-switch and a volume for normal stereo/mono controls
1300  */
1301 static int snd_ac97_cmix_new_stereo(struct snd_card *card, const char *pfx,
1302 				    int reg, int check_stereo, int check_amix,
1303 				    struct snd_ac97 *ac97)
1304 {
1305 	int err;
1306 	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1307 	unsigned char lo_max, hi_max;
1308 
1309 	if (! snd_ac97_valid_reg(ac97, reg))
1310 		return 0;
1311 
1312 	if (snd_ac97_try_bit(ac97, reg, 15)) {
1313 		sprintf(name, "%s Switch", pfx);
1314 		if ((err = snd_ac97_cmute_new_stereo(card, name, reg,
1315 						     check_stereo, check_amix,
1316 						     ac97)) < 0)
1317 			return err;
1318 	}
1319 	check_volume_resolution(ac97, reg, &lo_max, &hi_max);
1320 	if (lo_max) {
1321 		sprintf(name, "%s Volume", pfx);
1322 		if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0)
1323 			return err;
1324 	}
1325 	return 0;
1326 }
1327 
1328 #define snd_ac97_cmix_new(card, pfx, reg, acheck, ac97) \
1329 	snd_ac97_cmix_new_stereo(card, pfx, reg, 0, acheck, ac97)
1330 #define snd_ac97_cmute_new(card, name, reg, acheck, ac97) \
1331 	snd_ac97_cmute_new_stereo(card, name, reg, 0, acheck, ac97)
1332 
1333 static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97);
1334 
1335 static int snd_ac97_mixer_build(struct snd_ac97 * ac97)
1336 {
1337 	struct snd_card *card = ac97->bus->card;
1338 	struct snd_kcontrol *kctl;
1339 	int err;
1340 	unsigned int idx;
1341 	unsigned char max;
1342 
1343 	/* build master controls */
1344 	/* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
1345 	if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
1346 		if (ac97->flags & AC97_HAS_NO_MASTER_VOL)
1347 			err = snd_ac97_cmute_new(card, "Master Playback Switch",
1348 						 AC97_MASTER, 0, ac97);
1349 		else
1350 			err = snd_ac97_cmix_new(card, "Master Playback",
1351 						AC97_MASTER, 0, ac97);
1352 		if (err < 0)
1353 			return err;
1354 	}
1355 
1356 	ac97->regs[AC97_CENTER_LFE_MASTER] = AC97_MUTE_MASK_STEREO;
1357 
1358 	/* build center controls */
1359 	if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER))
1360 		&& !(ac97->flags & AC97_AD_MULTI)) {
1361 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
1362 			return err;
1363 		if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
1364 			return err;
1365 		snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max);
1366 		kctl->private_value &= ~(0xff << 16);
1367 		kctl->private_value |= (int)max << 16;
1368 		set_tlv_db_scale(kctl, find_db_scale(max));
1369 		snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
1370 	}
1371 
1372 	/* build LFE controls */
1373 	if ((snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1))
1374 		&& !(ac97->flags & AC97_AD_MULTI)) {
1375 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
1376 			return err;
1377 		if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
1378 			return err;
1379 		snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
1380 		kctl->private_value &= ~(0xff << 16);
1381 		kctl->private_value |= (int)max << 16;
1382 		set_tlv_db_scale(kctl, find_db_scale(max));
1383 		snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
1384 	}
1385 
1386 	/* build surround controls */
1387 	if ((snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER))
1388 		&& !(ac97->flags & AC97_AD_MULTI)) {
1389 		/* Surround Master (0x38) is with stereo mutes */
1390 		if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback",
1391 						    AC97_SURROUND_MASTER, 1, 0,
1392 						    ac97)) < 0)
1393 			return err;
1394 	}
1395 
1396 	/* build headphone controls */
1397 	if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) {
1398 		if ((err = snd_ac97_cmix_new(card, "Headphone Playback",
1399 					     AC97_HEADPHONE, 0, ac97)) < 0)
1400 			return err;
1401 	}
1402 
1403 	/* build master mono controls */
1404 	if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
1405 		if ((err = snd_ac97_cmix_new(card, "Master Mono Playback",
1406 					     AC97_MASTER_MONO, 0, ac97)) < 0)
1407 			return err;
1408 	}
1409 
1410 	/* build master tone controls */
1411 	if (!(ac97->flags & AC97_HAS_NO_TONE)) {
1412 		if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
1413 			for (idx = 0; idx < 2; idx++) {
1414 				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
1415 					return err;
1416 				if (ac97->id == AC97_ID_YMF743 ||
1417 				    ac97->id == AC97_ID_YMF753) {
1418 					kctl->private_value &= ~(0xff << 16);
1419 					kctl->private_value |= 7 << 16;
1420 				}
1421 			}
1422 			snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
1423 		}
1424 	}
1425 
1426 	/* build Beep controls */
1427 	if (!(ac97->flags & AC97_HAS_NO_PC_BEEP) &&
1428 		((ac97->flags & AC97_HAS_PC_BEEP) ||
1429 	    snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP))) {
1430 		for (idx = 0; idx < 2; idx++)
1431 			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
1432 				return err;
1433 		set_tlv_db_scale(kctl, db_scale_4bit);
1434 		snd_ac97_write_cache(
1435 			ac97,
1436 			AC97_PC_BEEP,
1437 			(snd_ac97_read(ac97, AC97_PC_BEEP)
1438 				| AC97_MUTE_MASK_MONO | 0x001e)
1439 		);
1440 	}
1441 
1442 	/* build Phone controls */
1443 	if (!(ac97->flags & AC97_HAS_NO_PHONE)) {
1444 		if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
1445 			if ((err = snd_ac97_cmix_new(card, "Phone Playback",
1446 						     AC97_PHONE, 1, ac97)) < 0)
1447 				return err;
1448 		}
1449 	}
1450 
1451 	/* build MIC controls */
1452 	if (!(ac97->flags & AC97_HAS_NO_MIC)) {
1453 		if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) {
1454 			if ((err = snd_ac97_cmix_new(card, "Mic Playback",
1455 						     AC97_MIC, 1, ac97)) < 0)
1456 				return err;
1457 			if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0)
1458 				return err;
1459 		}
1460 	}
1461 
1462 	/* build Line controls */
1463 	if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) {
1464 		if ((err = snd_ac97_cmix_new(card, "Line Playback",
1465 					     AC97_LINE, 1, ac97)) < 0)
1466 			return err;
1467 	}
1468 
1469 	/* build CD controls */
1470 	if (!(ac97->flags & AC97_HAS_NO_CD)) {
1471 		if (snd_ac97_try_volume_mix(ac97, AC97_CD)) {
1472 			if ((err = snd_ac97_cmix_new(card, "CD Playback",
1473 						     AC97_CD, 1, ac97)) < 0)
1474 				return err;
1475 		}
1476 	}
1477 
1478 	/* build Video controls */
1479 	if (!(ac97->flags & AC97_HAS_NO_VIDEO)) {
1480 		if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
1481 			if ((err = snd_ac97_cmix_new(card, "Video Playback",
1482 						     AC97_VIDEO, 1, ac97)) < 0)
1483 				return err;
1484 		}
1485 	}
1486 
1487 	/* build Aux controls */
1488 	if (!(ac97->flags & AC97_HAS_NO_AUX)) {
1489 		if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
1490 			if ((err = snd_ac97_cmix_new(card, "Aux Playback",
1491 						     AC97_AUX, 1, ac97)) < 0)
1492 				return err;
1493 		}
1494 	}
1495 
1496 	/* build PCM controls */
1497 	if (ac97->flags & AC97_AD_MULTI) {
1498 		unsigned short init_val;
1499 		if (ac97->flags & AC97_STEREO_MUTES)
1500 			init_val = 0x9f9f;
1501 		else
1502 			init_val = 0x9f1f;
1503 		for (idx = 0; idx < 2; idx++)
1504 			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
1505 				return err;
1506 		set_tlv_db_scale(kctl, db_scale_5bit);
1507 		ac97->spec.ad18xx.pcmreg[0] = init_val;
1508 		if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
1509 			for (idx = 0; idx < 2; idx++)
1510 				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
1511 					return err;
1512 			set_tlv_db_scale(kctl, db_scale_5bit);
1513 			ac97->spec.ad18xx.pcmreg[1] = init_val;
1514 		}
1515 		if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
1516 			for (idx = 0; idx < 2; idx++)
1517 				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
1518 					return err;
1519 			set_tlv_db_scale(kctl, db_scale_5bit);
1520 			for (idx = 0; idx < 2; idx++)
1521 				if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
1522 					return err;
1523 			set_tlv_db_scale(kctl, db_scale_5bit);
1524 			ac97->spec.ad18xx.pcmreg[2] = init_val;
1525 		}
1526 		snd_ac97_write_cache(ac97, AC97_PCM, init_val);
1527 	} else {
1528 		if (!(ac97->flags & AC97_HAS_NO_STD_PCM)) {
1529 			if (ac97->flags & AC97_HAS_NO_PCM_VOL)
1530 				err = snd_ac97_cmute_new(card,
1531 							 "PCM Playback Switch",
1532 							 AC97_PCM, 0, ac97);
1533 			else
1534 				err = snd_ac97_cmix_new(card, "PCM Playback",
1535 							AC97_PCM, 0, ac97);
1536 			if (err < 0)
1537 				return err;
1538 		}
1539 	}
1540 
1541 	/* build Capture controls */
1542 	if (!(ac97->flags & AC97_HAS_NO_REC_GAIN)) {
1543 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
1544 			return err;
1545 		if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) {
1546 			err = snd_ac97_cmute_new(card, "Capture Switch",
1547 						 AC97_REC_GAIN, 0, ac97);
1548 			if (err < 0)
1549 				return err;
1550 		}
1551 		if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
1552 			return err;
1553 		set_tlv_db_scale(kctl, db_scale_rec_gain);
1554 		snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
1555 		snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
1556 	}
1557 	/* build MIC Capture controls */
1558 	if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
1559 		for (idx = 0; idx < 2; idx++)
1560 			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
1561 				return err;
1562 		set_tlv_db_scale(kctl, db_scale_rec_gain);
1563 		snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
1564 	}
1565 
1566 	/* build PCM out path & mute control */
1567 	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
1568 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
1569 			return err;
1570 	}
1571 
1572 	/* build Simulated Stereo Enhancement control */
1573 	if (ac97->caps & AC97_BC_SIM_STEREO) {
1574 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
1575 			return err;
1576 	}
1577 
1578 	/* build 3D Stereo Enhancement control */
1579 	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
1580 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
1581 			return err;
1582 	}
1583 
1584 	/* build Loudness control */
1585 	if (ac97->caps & AC97_BC_LOUDNESS) {
1586 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
1587 			return err;
1588 	}
1589 
1590 	/* build Mono output select control */
1591 	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
1592 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
1593 			return err;
1594 	}
1595 
1596 	/* build Mic select control */
1597 	if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
1598 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
1599 			return err;
1600 	}
1601 
1602 	/* build ADC/DAC loopback control */
1603 	if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
1604 		if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
1605 			return err;
1606 	}
1607 
1608 	snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000);
1609 
1610 	/* build 3D controls */
1611 	if (ac97->build_ops->build_3d) {
1612 		ac97->build_ops->build_3d(ac97);
1613 	} else {
1614 		if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
1615 			unsigned short val;
1616 			val = 0x0707;
1617 			snd_ac97_write(ac97, AC97_3D_CONTROL, val);
1618 			val = snd_ac97_read(ac97, AC97_3D_CONTROL);
1619 			val = val == 0x0606;
1620 			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
1621 				return err;
1622 			if (val)
1623 				kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
1624 			if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
1625 				return err;
1626 			if (val)
1627 				kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
1628 			snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
1629 		}
1630 	}
1631 
1632 	/* build S/PDIF controls */
1633 
1634 	/* Hack for ASUS P5P800-VM, which does not indicate S/PDIF capability */
1635 	if (ac97->subsystem_vendor == 0x1043 &&
1636 	    ac97->subsystem_device == 0x810f)
1637 		ac97->ext_id |= AC97_EI_SPDIF;
1638 
1639 	if ((ac97->ext_id & AC97_EI_SPDIF) && !(ac97->scaps & AC97_SCAP_NO_SPDIF)) {
1640 		if (ac97->build_ops->build_spdif) {
1641 			if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
1642 				return err;
1643 		} else {
1644 			for (idx = 0; idx < 5; idx++)
1645 				if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
1646 					return err;
1647 			if (ac97->build_ops->build_post_spdif) {
1648 				if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
1649 					return err;
1650 			}
1651 			/* set default PCM S/PDIF params */
1652 			/* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1653 			snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
1654 			ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
1655 		}
1656 		ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
1657 	}
1658 
1659 	/* build chip specific controls */
1660 	if (ac97->build_ops->build_specific)
1661 		if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1662 			return err;
1663 
1664 	if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
1665 		kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97);
1666 		if (! kctl)
1667 			return -ENOMEM;
1668 		if (ac97->scaps & AC97_SCAP_INV_EAPD)
1669 			set_inv_eapd(ac97, kctl);
1670 		if ((err = snd_ctl_add(card, kctl)) < 0)
1671 			return err;
1672 	}
1673 
1674 	return 0;
1675 }
1676 
1677 static int snd_ac97_modem_build(struct snd_card *card, struct snd_ac97 * ac97)
1678 {
1679 	int err, idx;
1680 
1681 	/*
1682 	ac97_dbg(ac97, "AC97_GPIO_CFG = %x\n",
1683 	       snd_ac97_read(ac97,AC97_GPIO_CFG));
1684 	*/
1685 	snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
1686 	snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
1687 	snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
1688 	snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
1689 	snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
1690 
1691 	/* build modem switches */
1692 	for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_modem_switches); idx++)
1693 		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ac97_controls_modem_switches[idx], ac97))) < 0)
1694 			return err;
1695 
1696 	/* build chip specific controls */
1697 	if (ac97->build_ops->build_specific)
1698 		if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1699 			return err;
1700 
1701 	return 0;
1702 }
1703 
1704 static int snd_ac97_test_rate(struct snd_ac97 *ac97, int reg, int shadow_reg, int rate)
1705 {
1706 	unsigned short val;
1707 	unsigned int tmp;
1708 
1709 	tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
1710 	snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
1711 	if (shadow_reg)
1712 		snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
1713 	val = snd_ac97_read(ac97, reg);
1714 	return val == (tmp & 0xffff);
1715 }
1716 
1717 static void snd_ac97_determine_rates(struct snd_ac97 *ac97, int reg, int shadow_reg, unsigned int *r_result)
1718 {
1719 	unsigned int result = 0;
1720 	unsigned short saved;
1721 
1722 	if (ac97->bus->no_vra) {
1723 		*r_result = SNDRV_PCM_RATE_48000;
1724 		if ((ac97->flags & AC97_DOUBLE_RATE) &&
1725 		    reg == AC97_PCM_FRONT_DAC_RATE)
1726 			*r_result |= SNDRV_PCM_RATE_96000;
1727 		return;
1728 	}
1729 
1730 	saved = snd_ac97_read(ac97, reg);
1731 	if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
1732 		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1733 				     AC97_EA_DRA, 0);
1734 	/* test a non-standard rate */
1735 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
1736 		result |= SNDRV_PCM_RATE_CONTINUOUS;
1737 	/* let's try to obtain standard rates */
1738 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
1739 		result |= SNDRV_PCM_RATE_8000;
1740 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
1741 		result |= SNDRV_PCM_RATE_11025;
1742 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
1743 		result |= SNDRV_PCM_RATE_16000;
1744 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
1745 		result |= SNDRV_PCM_RATE_22050;
1746 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
1747 		result |= SNDRV_PCM_RATE_32000;
1748 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
1749 		result |= SNDRV_PCM_RATE_44100;
1750 	if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
1751 		result |= SNDRV_PCM_RATE_48000;
1752 	if ((ac97->flags & AC97_DOUBLE_RATE) &&
1753 	    reg == AC97_PCM_FRONT_DAC_RATE) {
1754 		/* test standard double rates */
1755 		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1756 				     AC97_EA_DRA, AC97_EA_DRA);
1757 		if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2))
1758 			result |= SNDRV_PCM_RATE_64000;
1759 		if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2))
1760 			result |= SNDRV_PCM_RATE_88200;
1761 		if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2))
1762 			result |= SNDRV_PCM_RATE_96000;
1763 		/* some codecs don't support variable double rates */
1764 		if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2))
1765 			result &= ~SNDRV_PCM_RATE_CONTINUOUS;
1766 		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1767 				     AC97_EA_DRA, 0);
1768 	}
1769 	/* restore the default value */
1770 	snd_ac97_write_cache(ac97, reg, saved);
1771 	if (shadow_reg)
1772 		snd_ac97_write_cache(ac97, shadow_reg, saved);
1773 	*r_result = result;
1774 }
1775 
1776 /* check AC97_SPDIF register to accept which sample rates */
1777 static unsigned int snd_ac97_determine_spdif_rates(struct snd_ac97 *ac97)
1778 {
1779 	unsigned int result = 0;
1780 	int i;
1781 	static unsigned short ctl_bits[] = {
1782 		AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
1783 	};
1784 	static unsigned int rate_bits[] = {
1785 		SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
1786 	};
1787 
1788 	for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
1789 		snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
1790 		if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
1791 			result |= rate_bits[i];
1792 	}
1793 	return result;
1794 }
1795 
1796 /* look for the codec id table matching with the given id */
1797 static const struct ac97_codec_id *look_for_codec_id(const struct ac97_codec_id *table,
1798 						     unsigned int id)
1799 {
1800 	const struct ac97_codec_id *pid;
1801 
1802 	for (pid = table; pid->id; pid++)
1803 		if (pid->id == (id & pid->mask))
1804 			return pid;
1805 	return NULL;
1806 }
1807 
1808 void snd_ac97_get_name(struct snd_ac97 *ac97, unsigned int id, char *name, int modem)
1809 {
1810 	const struct ac97_codec_id *pid;
1811 
1812 	sprintf(name, "0x%x %c%c%c", id,
1813 		printable(id >> 24),
1814 		printable(id >> 16),
1815 		printable(id >> 8));
1816 	pid = look_for_codec_id(snd_ac97_codec_id_vendors, id);
1817 	if (! pid)
1818 		return;
1819 
1820 	strcpy(name, pid->name);
1821 	if (ac97 && pid->patch) {
1822 		if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
1823 		    (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
1824 			pid->patch(ac97);
1825 	}
1826 
1827 	pid = look_for_codec_id(snd_ac97_codec_ids, id);
1828 	if (pid) {
1829 		strcat(name, " ");
1830 		strcat(name, pid->name);
1831 		if (pid->mask != 0xffffffff)
1832 			sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
1833 		if (ac97 && pid->patch) {
1834 			if ((modem && (pid->flags & AC97_MODEM_PATCH)) ||
1835 			    (! modem && ! (pid->flags & AC97_MODEM_PATCH)))
1836 				pid->patch(ac97);
1837 		}
1838 	} else
1839 		sprintf(name + strlen(name), " id %x", id & 0xff);
1840 }
1841 
1842 /**
1843  * snd_ac97_get_short_name - retrieve codec name
1844  * @ac97: the codec instance
1845  *
1846  * Return: The short identifying name of the codec.
1847  */
1848 const char *snd_ac97_get_short_name(struct snd_ac97 *ac97)
1849 {
1850 	const struct ac97_codec_id *pid;
1851 
1852 	for (pid = snd_ac97_codec_ids; pid->id; pid++)
1853 		if (pid->id == (ac97->id & pid->mask))
1854 			return pid->name;
1855 	return "unknown codec";
1856 }
1857 
1858 EXPORT_SYMBOL(snd_ac97_get_short_name);
1859 
1860 /* wait for a while until registers are accessible after RESET
1861  * return 0 if ok, negative not ready
1862  */
1863 static int ac97_reset_wait(struct snd_ac97 *ac97, int timeout, int with_modem)
1864 {
1865 	unsigned long end_time;
1866 	unsigned short val;
1867 
1868 	end_time = jiffies + timeout;
1869 	do {
1870 
1871 		/* use preliminary reads to settle the communication */
1872 		snd_ac97_read(ac97, AC97_RESET);
1873 		snd_ac97_read(ac97, AC97_VENDOR_ID1);
1874 		snd_ac97_read(ac97, AC97_VENDOR_ID2);
1875 		/* modem? */
1876 		if (with_modem) {
1877 			val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
1878 			if (val != 0xffff && (val & 1) != 0)
1879 				return 0;
1880 		}
1881 		if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) {
1882 			/* probably only Xbox issue - all registers are read as zero */
1883 			val = snd_ac97_read(ac97, AC97_VENDOR_ID1);
1884 			if (val != 0 && val != 0xffff)
1885 				return 0;
1886 		} else {
1887 			/* because the PCM or MASTER volume registers can be modified,
1888 			 * the REC_GAIN register is used for tests
1889 			 */
1890 			/* test if we can write to the record gain volume register */
1891 			snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
1892 			if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05)
1893 				return 0;
1894 		}
1895 		schedule_timeout_uninterruptible(1);
1896 	} while (time_after_eq(end_time, jiffies));
1897 	return -ENODEV;
1898 }
1899 
1900 /**
1901  * snd_ac97_bus - create an AC97 bus component
1902  * @card: the card instance
1903  * @num: the bus number
1904  * @ops: the bus callbacks table
1905  * @private_data: private data pointer for the new instance
1906  * @rbus: the pointer to store the new AC97 bus instance.
1907  *
1908  * Creates an AC97 bus component.  An struct snd_ac97_bus instance is newly
1909  * allocated and initialized.
1910  *
1911  * The ops table must include valid callbacks (at least read and
1912  * write).  The other callbacks, wait and reset, are not mandatory.
1913  *
1914  * The clock is set to 48000.  If another clock is needed, set
1915  * (*rbus)->clock manually.
1916  *
1917  * The AC97 bus instance is registered as a low-level device, so you don't
1918  * have to release it manually.
1919  *
1920  * Return: Zero if successful, or a negative error code on failure.
1921  */
1922 int snd_ac97_bus(struct snd_card *card, int num, struct snd_ac97_bus_ops *ops,
1923 		 void *private_data, struct snd_ac97_bus **rbus)
1924 {
1925 	int err;
1926 	struct snd_ac97_bus *bus;
1927 	static struct snd_device_ops dev_ops = {
1928 		.dev_free =	snd_ac97_bus_dev_free,
1929 	};
1930 
1931 	if (snd_BUG_ON(!card))
1932 		return -EINVAL;
1933 	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
1934 	if (bus == NULL)
1935 		return -ENOMEM;
1936 	bus->card = card;
1937 	bus->num = num;
1938 	bus->ops = ops;
1939 	bus->private_data = private_data;
1940 	bus->clock = 48000;
1941 	spin_lock_init(&bus->bus_lock);
1942 	snd_ac97_bus_proc_init(bus);
1943 	if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
1944 		snd_ac97_bus_free(bus);
1945 		return err;
1946 	}
1947 	if (rbus)
1948 		*rbus = bus;
1949 	return 0;
1950 }
1951 
1952 EXPORT_SYMBOL(snd_ac97_bus);
1953 
1954 /* stop no dev release warning */
1955 static void ac97_device_release(struct device * dev)
1956 {
1957 }
1958 
1959 /* register ac97 codec to bus */
1960 static int snd_ac97_dev_register(struct snd_device *device)
1961 {
1962 	struct snd_ac97 *ac97 = device->device_data;
1963 	int err;
1964 
1965 	ac97->dev.bus = &ac97_bus_type;
1966 	ac97->dev.parent = ac97->bus->card->dev;
1967 	ac97->dev.release = ac97_device_release;
1968 	dev_set_name(&ac97->dev, "%d-%d:%s",
1969 		     ac97->bus->card->number, ac97->num,
1970 		     snd_ac97_get_short_name(ac97));
1971 	if ((err = device_register(&ac97->dev)) < 0) {
1972 		ac97_err(ac97, "Can't register ac97 bus\n");
1973 		ac97->dev.bus = NULL;
1974 		return err;
1975 	}
1976 	return 0;
1977 }
1978 
1979 /* disconnect ac97 codec */
1980 static int snd_ac97_dev_disconnect(struct snd_device *device)
1981 {
1982 	struct snd_ac97 *ac97 = device->device_data;
1983 	if (ac97->dev.bus)
1984 		device_unregister(&ac97->dev);
1985 	return 0;
1986 }
1987 
1988 /* build_ops to do nothing */
1989 static const struct snd_ac97_build_ops null_build_ops;
1990 
1991 #ifdef CONFIG_SND_AC97_POWER_SAVE
1992 static void do_update_power(struct work_struct *work)
1993 {
1994 	update_power_regs(
1995 		container_of(work, struct snd_ac97, power_work.work));
1996 }
1997 #endif
1998 
1999 /**
2000  * snd_ac97_mixer - create an Codec97 component
2001  * @bus: the AC97 bus which codec is attached to
2002  * @template: the template of ac97, including index, callbacks and
2003  *         the private data.
2004  * @rac97: the pointer to store the new ac97 instance.
2005  *
2006  * Creates an Codec97 component.  An struct snd_ac97 instance is newly
2007  * allocated and initialized from the template.  The codec
2008  * is then initialized by the standard procedure.
2009  *
2010  * The template must include the codec number (num) and address (addr),
2011  * and the private data (private_data).
2012  *
2013  * The ac97 instance is registered as a low-level device, so you don't
2014  * have to release it manually.
2015  *
2016  * Return: Zero if successful, or a negative error code on failure.
2017  */
2018 int snd_ac97_mixer(struct snd_ac97_bus *bus, struct snd_ac97_template *template, struct snd_ac97 **rac97)
2019 {
2020 	int err;
2021 	struct snd_ac97 *ac97;
2022 	struct snd_card *card;
2023 	char name[64];
2024 	unsigned long end_time;
2025 	unsigned int reg;
2026 	const struct ac97_codec_id *pid;
2027 	static struct snd_device_ops ops = {
2028 		.dev_free =	snd_ac97_dev_free,
2029 		.dev_register =	snd_ac97_dev_register,
2030 		.dev_disconnect =	snd_ac97_dev_disconnect,
2031 	};
2032 
2033 	if (rac97)
2034 		*rac97 = NULL;
2035 	if (snd_BUG_ON(!bus || !template))
2036 		return -EINVAL;
2037 	if (snd_BUG_ON(template->num >= 4))
2038 		return -EINVAL;
2039 	if (bus->codec[template->num])
2040 		return -EBUSY;
2041 
2042 	card = bus->card;
2043 	ac97 = kzalloc(sizeof(*ac97), GFP_KERNEL);
2044 	if (ac97 == NULL)
2045 		return -ENOMEM;
2046 	ac97->private_data = template->private_data;
2047 	ac97->private_free = template->private_free;
2048 	ac97->bus = bus;
2049 	ac97->pci = template->pci;
2050 	ac97->num = template->num;
2051 	ac97->addr = template->addr;
2052 	ac97->scaps = template->scaps;
2053 	ac97->res_table = template->res_table;
2054 	bus->codec[ac97->num] = ac97;
2055 	mutex_init(&ac97->reg_mutex);
2056 	mutex_init(&ac97->page_mutex);
2057 #ifdef CONFIG_SND_AC97_POWER_SAVE
2058 	INIT_DELAYED_WORK(&ac97->power_work, do_update_power);
2059 #endif
2060 
2061 #ifdef CONFIG_PCI
2062 	if (ac97->pci) {
2063 		pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor);
2064 		pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device);
2065 	}
2066 #endif
2067 	if (bus->ops->reset) {
2068 		bus->ops->reset(ac97);
2069 		goto __access_ok;
2070 	}
2071 
2072 	ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
2073 	ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
2074 	if (ac97->id && ac97->id != (unsigned int)-1) {
2075 		pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
2076 		if (pid && (pid->flags & AC97_DEFAULT_POWER_OFF))
2077 			goto __access_ok;
2078 	}
2079 
2080 	/* reset to defaults */
2081 	if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO))
2082 		snd_ac97_write(ac97, AC97_RESET, 0);
2083 	if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM))
2084 		snd_ac97_write(ac97, AC97_EXTENDED_MID, 0);
2085 	if (bus->ops->wait)
2086 		bus->ops->wait(ac97);
2087 	else {
2088 		udelay(50);
2089 		if (ac97->scaps & AC97_SCAP_SKIP_AUDIO)
2090 			err = ac97_reset_wait(ac97, msecs_to_jiffies(500), 1);
2091 		else {
2092 			err = ac97_reset_wait(ac97, msecs_to_jiffies(500), 0);
2093 			if (err < 0)
2094 				err = ac97_reset_wait(ac97,
2095 						      msecs_to_jiffies(500), 1);
2096 		}
2097 		if (err < 0) {
2098 			ac97_warn(ac97, "AC'97 %d does not respond - RESET\n",
2099 				 ac97->num);
2100 			/* proceed anyway - it's often non-critical */
2101 		}
2102 	}
2103       __access_ok:
2104 	ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
2105 	ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
2106 	if (! (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) &&
2107 	    (ac97->id == 0x00000000 || ac97->id == 0xffffffff)) {
2108 		ac97_err(ac97,
2109 			 "AC'97 %d access is not valid [0x%x], removing mixer.\n",
2110 			 ac97->num, ac97->id);
2111 		snd_ac97_free(ac97);
2112 		return -EIO;
2113 	}
2114 	pid = look_for_codec_id(snd_ac97_codec_ids, ac97->id);
2115 	if (pid)
2116 		ac97->flags |= pid->flags;
2117 
2118 	/* test for AC'97 */
2119 	if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) {
2120 		/* test if we can write to the record gain volume register */
2121 		snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06);
2122 		if (((err = snd_ac97_read(ac97, AC97_REC_GAIN)) & 0x7fff) == 0x0a06)
2123 			ac97->scaps |= AC97_SCAP_AUDIO;
2124 	}
2125 	if (ac97->scaps & AC97_SCAP_AUDIO) {
2126 		ac97->caps = snd_ac97_read(ac97, AC97_RESET);
2127 		ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
2128 		if (ac97->ext_id == 0xffff)	/* invalid combination */
2129 			ac97->ext_id = 0;
2130 	}
2131 
2132 	/* test for MC'97 */
2133 	if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) {
2134 		ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2135 		if (ac97->ext_mid == 0xffff)	/* invalid combination */
2136 			ac97->ext_mid = 0;
2137 		if (ac97->ext_mid & 1)
2138 			ac97->scaps |= AC97_SCAP_MODEM;
2139 	}
2140 
2141 	if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) {
2142 		if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM)))
2143 			ac97_err(ac97,
2144 				 "AC'97 %d access error (not audio or modem codec)\n",
2145 				 ac97->num);
2146 		snd_ac97_free(ac97);
2147 		return -EACCES;
2148 	}
2149 
2150 	if (bus->ops->reset) // FIXME: always skipping?
2151 		goto __ready_ok;
2152 
2153 	/* FIXME: add powerdown control */
2154 	if (ac97_is_audio(ac97)) {
2155 		/* nothing should be in powerdown mode */
2156 		snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
2157 		if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
2158 			snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */
2159 			udelay(100);
2160 			snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
2161 		}
2162 		/* nothing should be in powerdown mode */
2163 		snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0);
2164 		end_time = jiffies + msecs_to_jiffies(5000);
2165 		do {
2166 			if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f)
2167 				goto __ready_ok;
2168 			schedule_timeout_uninterruptible(1);
2169 		} while (time_after_eq(end_time, jiffies));
2170 		ac97_warn(ac97,
2171 			  "AC'97 %d analog subsections not ready\n", ac97->num);
2172 	}
2173 
2174 	/* FIXME: add powerdown control */
2175 	if (ac97_is_modem(ac97)) {
2176 		unsigned char tmp;
2177 
2178 		/* nothing should be in powerdown mode */
2179 		/* note: it's important to set the rate at first */
2180 		tmp = AC97_MEA_GPIO;
2181 		if (ac97->ext_mid & AC97_MEI_LINE1) {
2182 			snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 8000);
2183 			tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1;
2184 		}
2185 		if (ac97->ext_mid & AC97_MEI_LINE2) {
2186 			snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 8000);
2187 			tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2;
2188 		}
2189 		if (ac97->ext_mid & AC97_MEI_HANDSET) {
2190 			snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 8000);
2191 			tmp |= AC97_MEA_HADC | AC97_MEA_HDAC;
2192 		}
2193 		snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
2194 		udelay(100);
2195 		/* nothing should be in powerdown mode */
2196 		snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0);
2197 		end_time = jiffies + msecs_to_jiffies(100);
2198 		do {
2199 			if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp)
2200 				goto __ready_ok;
2201 			schedule_timeout_uninterruptible(1);
2202 		} while (time_after_eq(end_time, jiffies));
2203 		ac97_warn(ac97,
2204 			  "MC'97 %d converters and GPIO not ready (0x%x)\n",
2205 			  ac97->num,
2206 			  snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS));
2207 	}
2208 
2209       __ready_ok:
2210 	if (ac97_is_audio(ac97))
2211 		ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT;
2212 	else
2213 		ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT;
2214 	if (ac97->ext_id & 0x01c9) {	/* L/R, MIC, SDAC, LDAC VRA support */
2215 		reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
2216 		reg |= ac97->ext_id & 0x01c0; /* LDAC/SDAC/CDAC */
2217 		if (! bus->no_vra)
2218 			reg |= ac97->ext_id & 0x0009; /* VRA/VRM */
2219 		snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
2220 	}
2221 	if ((ac97->ext_id & AC97_EI_DRA) && bus->dra) {
2222 		/* Intel controllers require double rate data to be put in
2223 		 * slots 7+8, so let's hope the codec supports it. */
2224 		snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, AC97_GP_DRSS_78);
2225 		if ((snd_ac97_read(ac97, AC97_GENERAL_PURPOSE) & AC97_GP_DRSS_MASK) == AC97_GP_DRSS_78)
2226 			ac97->flags |= AC97_DOUBLE_RATE;
2227 		/* restore to slots 10/11 to avoid the confliction with surrounds */
2228 		snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, 0);
2229 	}
2230 	if (ac97->ext_id & AC97_EI_VRA) {	/* VRA support */
2231 		snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]);
2232 		snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]);
2233 	} else {
2234 		ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000;
2235 		if (ac97->flags & AC97_DOUBLE_RATE)
2236 			ac97->rates[AC97_RATES_FRONT_DAC] |= SNDRV_PCM_RATE_96000;
2237 		ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000;
2238 	}
2239 	if (ac97->ext_id & AC97_EI_SPDIF) {
2240 		/* codec specific code (patch) should override these values */
2241 		ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000;
2242 	}
2243 	if (ac97->ext_id & AC97_EI_VRM) {	/* MIC VRA support */
2244 		snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]);
2245 	} else {
2246 		ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000;
2247 	}
2248 	if (ac97->ext_id & AC97_EI_SDAC) {	/* SDAC support */
2249 		snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]);
2250 		ac97->scaps |= AC97_SCAP_SURROUND_DAC;
2251 	}
2252 	if (ac97->ext_id & AC97_EI_LDAC) {	/* LDAC support */
2253 		snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]);
2254 		ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
2255 	}
2256 	/* additional initializations */
2257 	if (bus->ops->init)
2258 		bus->ops->init(ac97);
2259 	snd_ac97_get_name(ac97, ac97->id, name, !ac97_is_audio(ac97));
2260 	snd_ac97_get_name(NULL, ac97->id, name, !ac97_is_audio(ac97));  // ac97->id might be changed in the special setup code
2261 	if (! ac97->build_ops)
2262 		ac97->build_ops = &null_build_ops;
2263 
2264 	if (ac97_is_audio(ac97)) {
2265 		char comp[16];
2266 		if (card->mixername[0] == '\0') {
2267 			strcpy(card->mixername, name);
2268 		} else {
2269 			if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
2270 				strcat(card->mixername, ",");
2271 				strcat(card->mixername, name);
2272 			}
2273 		}
2274 		sprintf(comp, "AC97a:%08x", ac97->id);
2275 		if ((err = snd_component_add(card, comp)) < 0) {
2276 			snd_ac97_free(ac97);
2277 			return err;
2278 		}
2279 		if (snd_ac97_mixer_build(ac97) < 0) {
2280 			snd_ac97_free(ac97);
2281 			return -ENOMEM;
2282 		}
2283 	}
2284 	if (ac97_is_modem(ac97)) {
2285 		char comp[16];
2286 		if (card->mixername[0] == '\0') {
2287 			strcpy(card->mixername, name);
2288 		} else {
2289 			if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
2290 				strcat(card->mixername, ",");
2291 				strcat(card->mixername, name);
2292 			}
2293 		}
2294 		sprintf(comp, "AC97m:%08x", ac97->id);
2295 		if ((err = snd_component_add(card, comp)) < 0) {
2296 			snd_ac97_free(ac97);
2297 			return err;
2298 		}
2299 		if (snd_ac97_modem_build(card, ac97) < 0) {
2300 			snd_ac97_free(ac97);
2301 			return -ENOMEM;
2302 		}
2303 	}
2304 	if (ac97_is_audio(ac97))
2305 		update_power_regs(ac97);
2306 	snd_ac97_proc_init(ac97);
2307 	if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ac97, &ops)) < 0) {
2308 		snd_ac97_free(ac97);
2309 		return err;
2310 	}
2311 	*rac97 = ac97;
2312 	return 0;
2313 }
2314 
2315 EXPORT_SYMBOL(snd_ac97_mixer);
2316 
2317 /*
2318  * Power down the chip.
2319  *
2320  * MASTER and HEADPHONE registers are muted but the register cache values
2321  * are not changed, so that the values can be restored in snd_ac97_resume().
2322  */
2323 static void snd_ac97_powerdown(struct snd_ac97 *ac97)
2324 {
2325 	unsigned short power;
2326 
2327 	if (ac97_is_audio(ac97)) {
2328 		/* some codecs have stereo mute bits */
2329 		snd_ac97_write(ac97, AC97_MASTER, 0x9f9f);
2330 		snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f);
2331 	}
2332 
2333 	/* surround, CLFE, mic powerdown */
2334 	power = ac97->regs[AC97_EXTENDED_STATUS];
2335 	if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
2336 		power |= AC97_EA_PRJ;
2337 	if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
2338 		power |= AC97_EA_PRI | AC97_EA_PRK;
2339 	power |= AC97_EA_PRL;
2340 	snd_ac97_write(ac97, AC97_EXTENDED_STATUS, power);
2341 
2342 	/* powerdown external amplifier */
2343 	if (ac97->scaps & AC97_SCAP_INV_EAPD)
2344 		power = ac97->regs[AC97_POWERDOWN] & ~AC97_PD_EAPD;
2345 	else if (! (ac97->scaps & AC97_SCAP_EAPD_LED))
2346 		power = ac97->regs[AC97_POWERDOWN] | AC97_PD_EAPD;
2347 	power |= AC97_PD_PR6;	/* Headphone amplifier powerdown */
2348 	power |= AC97_PD_PR0 | AC97_PD_PR1;	/* ADC & DAC powerdown */
2349 	snd_ac97_write(ac97, AC97_POWERDOWN, power);
2350 	udelay(100);
2351 	power |= AC97_PD_PR2;	/* Analog Mixer powerdown (Vref on) */
2352 	snd_ac97_write(ac97, AC97_POWERDOWN, power);
2353 	if (ac97_is_power_save_mode(ac97)) {
2354 		power |= AC97_PD_PR3;	/* Analog Mixer powerdown */
2355 		snd_ac97_write(ac97, AC97_POWERDOWN, power);
2356 		udelay(100);
2357 		/* AC-link powerdown, internal Clk disable */
2358 		/* FIXME: this may cause click noises on some boards */
2359 		power |= AC97_PD_PR4 | AC97_PD_PR5;
2360 		snd_ac97_write(ac97, AC97_POWERDOWN, power);
2361 	}
2362 }
2363 
2364 
2365 struct ac97_power_reg {
2366 	unsigned short reg;
2367 	unsigned short power_reg;
2368 	unsigned short mask;
2369 };
2370 
2371 enum { PWIDX_ADC, PWIDX_FRONT, PWIDX_CLFE, PWIDX_SURR, PWIDX_MIC, PWIDX_SIZE };
2372 
2373 static struct ac97_power_reg power_regs[PWIDX_SIZE] = {
2374 	[PWIDX_ADC] = { AC97_PCM_LR_ADC_RATE, AC97_POWERDOWN, AC97_PD_PR0},
2375 	[PWIDX_FRONT] = { AC97_PCM_FRONT_DAC_RATE, AC97_POWERDOWN, AC97_PD_PR1},
2376 	[PWIDX_CLFE] = { AC97_PCM_LFE_DAC_RATE, AC97_EXTENDED_STATUS,
2377 			 AC97_EA_PRI | AC97_EA_PRK},
2378 	[PWIDX_SURR] = { AC97_PCM_SURR_DAC_RATE, AC97_EXTENDED_STATUS,
2379 			 AC97_EA_PRJ},
2380 	[PWIDX_MIC] = { AC97_PCM_MIC_ADC_RATE, AC97_EXTENDED_STATUS,
2381 			AC97_EA_PRL},
2382 };
2383 
2384 #ifdef CONFIG_SND_AC97_POWER_SAVE
2385 /**
2386  * snd_ac97_update_power - update the powerdown register
2387  * @ac97: the codec instance
2388  * @reg: the rate register, e.g. AC97_PCM_FRONT_DAC_RATE
2389  * @powerup: non-zero when power up the part
2390  *
2391  * Update the AC97 powerdown register bits of the given part.
2392  *
2393  * Return: Zero.
2394  */
2395 int snd_ac97_update_power(struct snd_ac97 *ac97, int reg, int powerup)
2396 {
2397 	int i;
2398 
2399 	if (! ac97)
2400 		return 0;
2401 
2402 	if (reg) {
2403 		/* SPDIF requires DAC power, too */
2404 		if (reg == AC97_SPDIF)
2405 			reg = AC97_PCM_FRONT_DAC_RATE;
2406 		for (i = 0; i < PWIDX_SIZE; i++) {
2407 			if (power_regs[i].reg == reg) {
2408 				if (powerup)
2409 					ac97->power_up |= (1 << i);
2410 				else
2411 					ac97->power_up &= ~(1 << i);
2412 				break;
2413 			}
2414 		}
2415 	}
2416 
2417 	if (ac97_is_power_save_mode(ac97) && !powerup)
2418 		/* adjust power-down bits after two seconds delay
2419 		 * (for avoiding loud click noises for many (OSS) apps
2420 		 *  that open/close frequently)
2421 		 */
2422 		schedule_delayed_work(&ac97->power_work,
2423 				      msecs_to_jiffies(power_save * 1000));
2424 	else {
2425 		cancel_delayed_work(&ac97->power_work);
2426 		update_power_regs(ac97);
2427 	}
2428 
2429 	return 0;
2430 }
2431 
2432 EXPORT_SYMBOL(snd_ac97_update_power);
2433 #endif /* CONFIG_SND_AC97_POWER_SAVE */
2434 
2435 static void update_power_regs(struct snd_ac97 *ac97)
2436 {
2437 	unsigned int power_up, bits;
2438 	int i;
2439 
2440 	power_up = (1 << PWIDX_FRONT) | (1 << PWIDX_ADC);
2441 	power_up |= (1 << PWIDX_MIC);
2442 	if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
2443 		power_up |= (1 << PWIDX_SURR);
2444 	if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
2445 		power_up |= (1 << PWIDX_CLFE);
2446 #ifdef CONFIG_SND_AC97_POWER_SAVE
2447 	if (ac97_is_power_save_mode(ac97))
2448 		power_up = ac97->power_up;
2449 #endif
2450 	if (power_up) {
2451 		if (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2) {
2452 			/* needs power-up analog mix and vref */
2453 			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2454 					     AC97_PD_PR3, 0);
2455 			msleep(1);
2456 			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2457 					     AC97_PD_PR2, 0);
2458 		}
2459 	}
2460 	for (i = 0; i < PWIDX_SIZE; i++) {
2461 		if (power_up & (1 << i))
2462 			bits = 0;
2463 		else
2464 			bits = power_regs[i].mask;
2465 		snd_ac97_update_bits(ac97, power_regs[i].power_reg,
2466 				     power_regs[i].mask, bits);
2467 	}
2468 	if (! power_up) {
2469 		if (! (ac97->regs[AC97_POWERDOWN] & AC97_PD_PR2)) {
2470 			/* power down analog mix and vref */
2471 			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2472 					     AC97_PD_PR2, AC97_PD_PR2);
2473 			snd_ac97_update_bits(ac97, AC97_POWERDOWN,
2474 					     AC97_PD_PR3, AC97_PD_PR3);
2475 		}
2476 	}
2477 }
2478 
2479 
2480 #ifdef CONFIG_PM
2481 /**
2482  * snd_ac97_suspend - General suspend function for AC97 codec
2483  * @ac97: the ac97 instance
2484  *
2485  * Suspends the codec, power down the chip.
2486  */
2487 void snd_ac97_suspend(struct snd_ac97 *ac97)
2488 {
2489 	if (! ac97)
2490 		return;
2491 	if (ac97->build_ops->suspend)
2492 		ac97->build_ops->suspend(ac97);
2493 #ifdef CONFIG_SND_AC97_POWER_SAVE
2494 	cancel_delayed_work_sync(&ac97->power_work);
2495 #endif
2496 	snd_ac97_powerdown(ac97);
2497 }
2498 
2499 EXPORT_SYMBOL(snd_ac97_suspend);
2500 
2501 /*
2502  * restore ac97 status
2503  */
2504 static void snd_ac97_restore_status(struct snd_ac97 *ac97)
2505 {
2506 	int i;
2507 
2508 	for (i = 2; i < 0x7c ; i += 2) {
2509 		if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
2510 			continue;
2511 		/* restore only accessible registers
2512 		 * some chip (e.g. nm256) may hang up when unsupported registers
2513 		 * are accessed..!
2514 		 */
2515 		if (test_bit(i, ac97->reg_accessed)) {
2516 			snd_ac97_write(ac97, i, ac97->regs[i]);
2517 			snd_ac97_read(ac97, i);
2518 		}
2519 	}
2520 }
2521 
2522 /*
2523  * restore IEC958 status
2524  */
2525 static void snd_ac97_restore_iec958(struct snd_ac97 *ac97)
2526 {
2527 	if (ac97->ext_id & AC97_EI_SPDIF) {
2528 		if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) {
2529 			/* reset spdif status */
2530 			snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
2531 			snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]);
2532 			if (ac97->flags & AC97_CS_SPDIF)
2533 				snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]);
2534 			else
2535 				snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]);
2536 			snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
2537 		}
2538 	}
2539 }
2540 
2541 /**
2542  * snd_ac97_resume - General resume function for AC97 codec
2543  * @ac97: the ac97 instance
2544  *
2545  * Do the standard resume procedure, power up and restoring the
2546  * old register values.
2547  */
2548 void snd_ac97_resume(struct snd_ac97 *ac97)
2549 {
2550 	unsigned long end_time;
2551 
2552 	if (! ac97)
2553 		return;
2554 
2555 	if (ac97->bus->ops->reset) {
2556 		ac97->bus->ops->reset(ac97);
2557 		goto  __reset_ready;
2558 	}
2559 
2560 	snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2561 	if (! (ac97->flags & AC97_DEFAULT_POWER_OFF)) {
2562 		if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO))
2563 			snd_ac97_write(ac97, AC97_RESET, 0);
2564 		else if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM))
2565 			snd_ac97_write(ac97, AC97_EXTENDED_MID, 0);
2566 		udelay(100);
2567 		snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2568 	}
2569 	snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0);
2570 
2571 	snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]);
2572 	if (ac97_is_audio(ac97)) {
2573 		ac97->bus->ops->write(ac97, AC97_MASTER, 0x8101);
2574 		end_time = jiffies + msecs_to_jiffies(100);
2575 		do {
2576 			if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101)
2577 				break;
2578 			schedule_timeout_uninterruptible(1);
2579 		} while (time_after_eq(end_time, jiffies));
2580 		/* FIXME: extra delay */
2581 		ac97->bus->ops->write(ac97, AC97_MASTER, AC97_MUTE_MASK_MONO);
2582 		if (snd_ac97_read(ac97, AC97_MASTER) != AC97_MUTE_MASK_MONO)
2583 			msleep(250);
2584 	} else {
2585 		end_time = jiffies + msecs_to_jiffies(100);
2586 		do {
2587 			unsigned short val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2588 			if (val != 0xffff && (val & 1) != 0)
2589 				break;
2590 			schedule_timeout_uninterruptible(1);
2591 		} while (time_after_eq(end_time, jiffies));
2592 	}
2593 __reset_ready:
2594 
2595 	if (ac97->bus->ops->init)
2596 		ac97->bus->ops->init(ac97);
2597 
2598 	if (ac97->build_ops->resume)
2599 		ac97->build_ops->resume(ac97);
2600 	else {
2601 		snd_ac97_restore_status(ac97);
2602 		snd_ac97_restore_iec958(ac97);
2603 	}
2604 }
2605 
2606 EXPORT_SYMBOL(snd_ac97_resume);
2607 #endif
2608 
2609 
2610 /*
2611  * Hardware tuning
2612  */
2613 static void set_ctl_name(char *dst, const char *src, const char *suffix)
2614 {
2615 	if (suffix)
2616 		sprintf(dst, "%s %s", src, suffix);
2617 	else
2618 		strcpy(dst, src);
2619 }
2620 
2621 /* remove the control with the given name and optional suffix */
2622 static int snd_ac97_remove_ctl(struct snd_ac97 *ac97, const char *name,
2623 			       const char *suffix)
2624 {
2625 	struct snd_ctl_elem_id id;
2626 	memset(&id, 0, sizeof(id));
2627 	set_ctl_name(id.name, name, suffix);
2628 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2629 	return snd_ctl_remove_id(ac97->bus->card, &id);
2630 }
2631 
2632 static struct snd_kcontrol *ctl_find(struct snd_ac97 *ac97, const char *name, const char *suffix)
2633 {
2634 	struct snd_ctl_elem_id sid;
2635 	memset(&sid, 0, sizeof(sid));
2636 	set_ctl_name(sid.name, name, suffix);
2637 	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2638 	return snd_ctl_find_id(ac97->bus->card, &sid);
2639 }
2640 
2641 /* rename the control with the given name and optional suffix */
2642 static int snd_ac97_rename_ctl(struct snd_ac97 *ac97, const char *src,
2643 			       const char *dst, const char *suffix)
2644 {
2645 	struct snd_kcontrol *kctl = ctl_find(ac97, src, suffix);
2646 	if (kctl) {
2647 		set_ctl_name(kctl->id.name, dst, suffix);
2648 		return 0;
2649 	}
2650 	return -ENOENT;
2651 }
2652 
2653 /* rename both Volume and Switch controls - don't check the return value */
2654 static void snd_ac97_rename_vol_ctl(struct snd_ac97 *ac97, const char *src,
2655 				    const char *dst)
2656 {
2657 	snd_ac97_rename_ctl(ac97, src, dst, "Switch");
2658 	snd_ac97_rename_ctl(ac97, src, dst, "Volume");
2659 }
2660 
2661 /* swap controls */
2662 static int snd_ac97_swap_ctl(struct snd_ac97 *ac97, const char *s1,
2663 			     const char *s2, const char *suffix)
2664 {
2665 	struct snd_kcontrol *kctl1, *kctl2;
2666 	kctl1 = ctl_find(ac97, s1, suffix);
2667 	kctl2 = ctl_find(ac97, s2, suffix);
2668 	if (kctl1 && kctl2) {
2669 		set_ctl_name(kctl1->id.name, s2, suffix);
2670 		set_ctl_name(kctl2->id.name, s1, suffix);
2671 		return 0;
2672 	}
2673 	return -ENOENT;
2674 }
2675 
2676 #if 1
2677 /* bind hp and master controls instead of using only hp control */
2678 static int bind_hp_volsw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2679 {
2680 	int err = snd_ac97_put_volsw(kcontrol, ucontrol);
2681 	if (err > 0) {
2682 		unsigned long priv_saved = kcontrol->private_value;
2683 		kcontrol->private_value = (kcontrol->private_value & ~0xff) | AC97_HEADPHONE;
2684 		snd_ac97_put_volsw(kcontrol, ucontrol);
2685 		kcontrol->private_value = priv_saved;
2686 	}
2687 	return err;
2688 }
2689 
2690 /* ac97 tune: bind Master and Headphone controls */
2691 static int tune_hp_only(struct snd_ac97 *ac97)
2692 {
2693 	struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2694 	struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
2695 	if (! msw || ! mvol)
2696 		return -ENOENT;
2697 	msw->put = bind_hp_volsw_put;
2698 	mvol->put = bind_hp_volsw_put;
2699 	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
2700 	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
2701 	return 0;
2702 }
2703 
2704 #else
2705 /* ac97 tune: use Headphone control as master */
2706 static int tune_hp_only(struct snd_ac97 *ac97)
2707 {
2708 	if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2709 		return -ENOENT;
2710 	snd_ac97_remove_ctl(ac97, "Master Playback", "Switch");
2711 	snd_ac97_remove_ctl(ac97, "Master Playback", "Volume");
2712 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2713 	return 0;
2714 }
2715 #endif
2716 
2717 /* ac97 tune: swap Headphone and Master controls */
2718 static int tune_swap_hp(struct snd_ac97 *ac97)
2719 {
2720 	if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2721 		return -ENOENT;
2722 	snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Line-Out Playback");
2723 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2724 	return 0;
2725 }
2726 
2727 /* ac97 tune: swap Surround and Master controls */
2728 static int tune_swap_surround(struct snd_ac97 *ac97)
2729 {
2730 	if (snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Switch") ||
2731 	    snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Volume"))
2732 		return -ENOENT;
2733 	return 0;
2734 }
2735 
2736 /* ac97 tune: set up mic sharing for AD codecs */
2737 static int tune_ad_sharing(struct snd_ac97 *ac97)
2738 {
2739 	unsigned short scfg;
2740 	if ((ac97->id & 0xffffff00) != 0x41445300) {
2741 		ac97_err(ac97, "ac97_quirk AD_SHARING is only for AD codecs\n");
2742 		return -EINVAL;
2743 	}
2744 	/* Turn on OMS bit to route microphone to back panel */
2745 	scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
2746 	snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200);
2747 	return 0;
2748 }
2749 
2750 static const struct snd_kcontrol_new snd_ac97_alc_jack_detect =
2751 AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0);
2752 
2753 /* ac97 tune: set up ALC jack-select */
2754 static int tune_alc_jack(struct snd_ac97 *ac97)
2755 {
2756 	if ((ac97->id & 0xffffff00) != 0x414c4700) {
2757 		ac97_err(ac97,
2758 			 "ac97_quirk ALC_JACK is only for Realtek codecs\n");
2759 		return -EINVAL;
2760 	}
2761 	snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */
2762 	snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */
2763 	if (ac97->id == AC97_ID_ALC658D)
2764 		snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800);
2765 	return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97));
2766 }
2767 
2768 /* ac97 tune: inversed EAPD bit */
2769 static int tune_inv_eapd(struct snd_ac97 *ac97)
2770 {
2771 	struct snd_kcontrol *kctl = ctl_find(ac97, "External Amplifier", NULL);
2772 	if (! kctl)
2773 		return -ENOENT;
2774 	set_inv_eapd(ac97, kctl);
2775 	return 0;
2776 }
2777 
2778 static int master_mute_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2779 {
2780 	int err = snd_ac97_put_volsw(kcontrol, ucontrol);
2781 	if (err > 0) {
2782 		struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2783 		int shift = (kcontrol->private_value >> 8) & 0x0f;
2784 		int rshift = (kcontrol->private_value >> 12) & 0x0f;
2785 		unsigned short mask;
2786 		if (shift != rshift)
2787 			mask = AC97_MUTE_MASK_STEREO;
2788 		else
2789 			mask = AC97_MUTE_MASK_MONO;
2790 		snd_ac97_update_bits(ac97, AC97_POWERDOWN, AC97_PD_EAPD,
2791 				     (ac97->regs[AC97_MASTER] & mask) == mask ?
2792 				     AC97_PD_EAPD : 0);
2793 	}
2794 	return err;
2795 }
2796 
2797 /* ac97 tune: EAPD controls mute LED bound with the master mute */
2798 static int tune_mute_led(struct snd_ac97 *ac97)
2799 {
2800 	struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2801 	if (! msw)
2802 		return -ENOENT;
2803 	msw->put = master_mute_sw_put;
2804 	snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
2805 	snd_ac97_update_bits(
2806 		ac97, AC97_POWERDOWN,
2807 		AC97_PD_EAPD, AC97_PD_EAPD /* mute LED on */
2808 	);
2809 	ac97->scaps |= AC97_SCAP_EAPD_LED;
2810 	return 0;
2811 }
2812 
2813 static int hp_master_mute_sw_put(struct snd_kcontrol *kcontrol,
2814 				 struct snd_ctl_elem_value *ucontrol)
2815 {
2816 	int err = bind_hp_volsw_put(kcontrol, ucontrol);
2817 	if (err > 0) {
2818 		struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2819 		int shift = (kcontrol->private_value >> 8) & 0x0f;
2820 		int rshift = (kcontrol->private_value >> 12) & 0x0f;
2821 		unsigned short mask;
2822 		if (shift != rshift)
2823 			mask = AC97_MUTE_MASK_STEREO;
2824 		else
2825 			mask = AC97_MUTE_MASK_MONO;
2826 		snd_ac97_update_bits(ac97, AC97_POWERDOWN, AC97_PD_EAPD,
2827 				     (ac97->regs[AC97_MASTER] & mask) == mask ?
2828 				     AC97_PD_EAPD : 0);
2829 	}
2830 	return err;
2831 }
2832 
2833 static int tune_hp_mute_led(struct snd_ac97 *ac97)
2834 {
2835 	struct snd_kcontrol *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2836 	struct snd_kcontrol *mvol = ctl_find(ac97, "Master Playback Volume", NULL);
2837 	if (! msw || ! mvol)
2838 		return -ENOENT;
2839 	msw->put = hp_master_mute_sw_put;
2840 	mvol->put = bind_hp_volsw_put;
2841 	snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
2842 	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Switch");
2843 	snd_ac97_remove_ctl(ac97, "Headphone Playback", "Volume");
2844 	snd_ac97_update_bits(
2845 		ac97, AC97_POWERDOWN,
2846 		AC97_PD_EAPD, AC97_PD_EAPD /* mute LED on */
2847 	);
2848 	return 0;
2849 }
2850 
2851 struct quirk_table {
2852 	const char *name;
2853 	int (*func)(struct snd_ac97 *);
2854 };
2855 
2856 static struct quirk_table applicable_quirks[] = {
2857 	{ "none", NULL },
2858 	{ "hp_only", tune_hp_only },
2859 	{ "swap_hp", tune_swap_hp },
2860 	{ "swap_surround", tune_swap_surround },
2861 	{ "ad_sharing", tune_ad_sharing },
2862 	{ "alc_jack", tune_alc_jack },
2863 	{ "inv_eapd", tune_inv_eapd },
2864 	{ "mute_led", tune_mute_led },
2865 	{ "hp_mute_led", tune_hp_mute_led },
2866 };
2867 
2868 /* apply the quirk with the given type */
2869 static int apply_quirk(struct snd_ac97 *ac97, int type)
2870 {
2871 	if (type <= 0)
2872 		return 0;
2873 	else if (type >= ARRAY_SIZE(applicable_quirks))
2874 		return -EINVAL;
2875 	if (applicable_quirks[type].func)
2876 		return applicable_quirks[type].func(ac97);
2877 	return 0;
2878 }
2879 
2880 /* apply the quirk with the given name */
2881 static int apply_quirk_str(struct snd_ac97 *ac97, const char *typestr)
2882 {
2883 	int i;
2884 	struct quirk_table *q;
2885 
2886 	for (i = 0; i < ARRAY_SIZE(applicable_quirks); i++) {
2887 		q = &applicable_quirks[i];
2888 		if (q->name && ! strcmp(typestr, q->name))
2889 			return apply_quirk(ac97, i);
2890 	}
2891 	/* for compatibility, accept the numbers, too */
2892 	if (*typestr >= '0' && *typestr <= '9')
2893 		return apply_quirk(ac97, (int)simple_strtoul(typestr, NULL, 10));
2894 	return -EINVAL;
2895 }
2896 
2897 /**
2898  * snd_ac97_tune_hardware - tune up the hardware
2899  * @ac97: the ac97 instance
2900  * @quirk: quirk list
2901  * @override: explicit quirk value (overrides the list if non-NULL)
2902  *
2903  * Do some workaround for each pci device, such as renaming of the
2904  * headphone (true line-out) control as "Master".
2905  * The quirk-list must be terminated with a zero-filled entry.
2906  *
2907  * Return: Zero if successful, or a negative error code on failure.
2908  */
2909 
2910 int snd_ac97_tune_hardware(struct snd_ac97 *ac97, struct ac97_quirk *quirk, const char *override)
2911 {
2912 	int result;
2913 
2914 	/* quirk overriden? */
2915 	if (override && strcmp(override, "-1") && strcmp(override, "default")) {
2916 		result = apply_quirk_str(ac97, override);
2917 		if (result < 0)
2918 			ac97_err(ac97, "applying quirk type %s failed (%d)\n",
2919 				 override, result);
2920 		return result;
2921 	}
2922 
2923 	if (! quirk)
2924 		return -EINVAL;
2925 
2926 	for (; quirk->subvendor; quirk++) {
2927 		if (quirk->subvendor != ac97->subsystem_vendor)
2928 			continue;
2929 		if ((! quirk->mask && quirk->subdevice == ac97->subsystem_device) ||
2930 		    quirk->subdevice == (quirk->mask & ac97->subsystem_device)) {
2931 			if (quirk->codec_id && quirk->codec_id != ac97->id)
2932 				continue;
2933 			ac97_dbg(ac97, "ac97 quirk for %s (%04x:%04x)\n",
2934 				 quirk->name, ac97->subsystem_vendor,
2935 				 ac97->subsystem_device);
2936 			result = apply_quirk(ac97, quirk->type);
2937 			if (result < 0)
2938 				ac97_err(ac97,
2939 					 "applying quirk type %d for %s failed (%d)\n",
2940 					 quirk->type, quirk->name, result);
2941 			return result;
2942 		}
2943 	}
2944 	return 0;
2945 }
2946 
2947 EXPORT_SYMBOL(snd_ac97_tune_hardware);
2948 
2949 /*
2950  *  INIT part
2951  */
2952 
2953 static int __init alsa_ac97_init(void)
2954 {
2955 	return 0;
2956 }
2957 
2958 static void __exit alsa_ac97_exit(void)
2959 {
2960 }
2961 
2962 module_init(alsa_ac97_init)
2963 module_exit(alsa_ac97_exit)
2964