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