2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Universal interface for Audio Codec '97
5 * For more details look to AC '97 component specification revision 2.2
6 * by Intel Corporation (http://developer.intel.com).
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.
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.
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
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 <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/ac97_codec.h>
34 #include <sound/asoundef.h>
35 #include <sound/initval.h>
36 #include "ac97_local.h"
38 #include "ac97_patch.h"
40 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
41 MODULE_DESCRIPTION("Universal interface for Audio Codec '97");
42 MODULE_LICENSE("GPL");
44 static int enable_loopback;
46 module_param(enable_loopback, bool, 0444);
47 MODULE_PARM_DESC(enable_loopback, "Enable AC97 ADC/DAC Loopback Control");
48 MODULE_PARM_SYNTAX(enable_loopback, SNDRV_BOOLEAN_FALSE_DESC);
60 int (*patch)(ac97_t *ac97);
61 int (*mpatch)(ac97_t *ac97);
64 static const ac97_codec_id_t snd_ac97_codec_id_vendors[] = {
65 { 0x414b4d00, 0xffffff00, "Asahi Kasei", NULL, NULL },
66 { 0x41445300, 0xffffff00, "Analog Devices", NULL, NULL },
67 { 0x414c4300, 0xffffff00, "Realtek", NULL, NULL },
68 { 0x414c4700, 0xffffff00, "Realtek", NULL, NULL },
69 { 0x434d4900, 0xffffff00, "C-Media Electronics", NULL, NULL },
70 { 0x43525900, 0xffffff00, "Cirrus Logic", NULL, NULL },
71 { 0x43585400, 0xffffff00, "Conexant", NULL, NULL },
72 { 0x44543000, 0xffffff00, "Diamond Technology", NULL, NULL },
73 { 0x454d4300, 0xffffff00, "eMicro", NULL, NULL },
74 { 0x45838300, 0xffffff00, "ESS Technology", NULL, NULL },
75 { 0x48525300, 0xffffff00, "Intersil", NULL, NULL },
76 { 0x49434500, 0xffffff00, "ICEnsemble", NULL, NULL },
77 { 0x49544500, 0xffffff00, "ITE Tech.Inc", NULL, NULL },
78 { 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL },
79 { 0x50534300, 0xffffff00, "Philips", NULL, NULL },
80 { 0x53494c00, 0xffffff00, "Silicon Laboratory", NULL, NULL },
81 { 0x54524100, 0xffffff00, "TriTech", NULL, NULL },
82 { 0x54584e00, 0xffffff00, "Texas Instruments", NULL, NULL },
83 { 0x56494100, 0xffffff00, "VIA Technologies", NULL, NULL },
84 { 0x57454300, 0xffffff00, "Winbond", NULL, NULL },
85 { 0x574d4c00, 0xffffff00, "Wolfson", NULL, NULL },
86 { 0x594d4800, 0xffffff00, "Yamaha", NULL, NULL },
87 { 0x83847600, 0xffffff00, "SigmaTel", NULL, NULL },
88 { 0, 0, NULL, NULL, NULL }
91 static const ac97_codec_id_t snd_ac97_codec_ids[] = {
92 { 0x414b4d00, 0xffffffff, "AK4540", NULL, NULL },
93 { 0x414b4d01, 0xffffffff, "AK4542", NULL, NULL },
94 { 0x414b4d02, 0xffffffff, "AK4543", NULL, NULL },
95 { 0x414b4d06, 0xffffffff, "AK4544A", NULL, NULL },
96 { 0x414b4d07, 0xffffffff, "AK4545", NULL, NULL },
97 { 0x41445303, 0xffffffff, "AD1819", patch_ad1819, NULL },
98 { 0x41445340, 0xffffffff, "AD1881", patch_ad1881, NULL },
99 { 0x41445348, 0xffffffff, "AD1881A", patch_ad1881, NULL },
100 { 0x41445360, 0xffffffff, "AD1885", patch_ad1885, NULL },
101 { 0x41445361, 0xffffffff, "AD1886", patch_ad1886, NULL },
102 { 0x41445362, 0xffffffff, "AD1887", patch_ad1881, NULL },
103 { 0x41445363, 0xffffffff, "AD1886A", patch_ad1881, NULL },
104 { 0x41445368, 0xffffffff, "AD1888", patch_ad1888, NULL },
105 { 0x41445370, 0xffffffff, "AD1980", patch_ad1980, NULL },
106 { 0x41445372, 0xffffffff, "AD1981A", patch_ad1981a, NULL },
107 { 0x41445374, 0xffffffff, "AD1981B", patch_ad1981b, NULL },
108 { 0x41445375, 0xffffffff, "AD1985", patch_ad1985, NULL },
109 { 0x414c4300, 0xffffff00, "ALC100/100P", NULL, NULL },
110 { 0x414c4710, 0xfffffff0, "ALC200/200P", NULL, NULL },
111 { 0x414c4720, 0xfffffff0, "ALC650", patch_alc650, NULL },
112 { 0x414c4721, 0xfffffff0, "ALC650D", patch_alc650, NULL },
113 { 0x414c4722, 0xfffffff0, "ALC650E", patch_alc650, NULL },
114 { 0x414c4723, 0xfffffff0, "ALC650F", patch_alc650, NULL },
115 { 0x414c4760, 0xfffffff0, "ALC655", patch_alc655, NULL },
116 { 0x414c4780, 0xfffffff0, "ALC658", patch_alc655, NULL },
117 { 0x414c4730, 0xffffffff, "ALC101", NULL, NULL },
118 { 0x414c4740, 0xfffffff0, "ALC202", NULL, NULL },
119 { 0x414c4750, 0xfffffff0, "ALC250", NULL, NULL },
120 { 0x414c4770, 0xfffffff0, "ALC203", NULL, NULL },
121 { 0x434d4941, 0xffffffff, "CMI9738", patch_cm9738, NULL },
122 { 0x434d4961, 0xffffffff, "CMI9739", patch_cm9739, NULL },
123 { 0x43525900, 0xfffffff8, "CS4297", NULL, NULL },
124 { 0x43525910, 0xfffffff8, "CS4297A", patch_cirrus_spdif, NULL },
125 { 0x43525920, 0xfffffff8, "CS4294/4298", NULL, NULL },
126 { 0x43525928, 0xfffffff8, "CS4294", NULL, NULL },
127 { 0x43525930, 0xfffffff8, "CS4299", patch_cirrus_cs4299, NULL },
128 { 0x43525948, 0xfffffff8, "CS4201", NULL, NULL },
129 { 0x43525958, 0xfffffff8, "CS4205", patch_cirrus_spdif, NULL },
130 { 0x43525960, 0xfffffff8, "CS4291", NULL, NULL },
131 { 0x43525970, 0xfffffff8, "CS4202", NULL, NULL },
132 { 0x43585421, 0xffffffff, "HSD11246", NULL, NULL }, // SmartMC II
133 { 0x43585428, 0xfffffff8, "Cx20468", patch_conexant, NULL }, // SmartAMC fixme: the mask might be different
134 { 0x44543031, 0xfffffff0, "DT0398", NULL, NULL },
135 { 0x454d4328, 0xffffffff, "28028", NULL, NULL }, // same as TR28028?
136 { 0x45838308, 0xffffffff, "ESS1988", NULL, NULL },
137 { 0x48525300, 0xffffff00, "HMP9701", NULL, NULL },
138 { 0x49434501, 0xffffffff, "ICE1230", NULL, NULL },
139 { 0x49434511, 0xffffffff, "ICE1232", NULL, NULL }, // alias VIA VT1611A?
140 { 0x49434514, 0xffffffff, "ICE1232A", NULL, NULL },
141 { 0x49434551, 0xffffffff, "VT1616", patch_vt1616, NULL },
142 { 0x49434552, 0xffffffff, "VT1616i", patch_vt1616, NULL }, // VT1616 compatible (chipset integrated)
143 { 0x49544520, 0xffffffff, "IT2226E", NULL, NULL },
144 { 0x49544561, 0xffffffff, "IT2646E", patch_it2646, NULL },
145 { 0x4e534300, 0xffffffff, "LM4540/43/45/46/48", NULL, NULL }, // only guess --jk
146 { 0x4e534331, 0xffffffff, "LM4549", NULL, NULL },
147 { 0x4e534350, 0xffffffff, "LM4550", NULL, NULL },
148 { 0x50534304, 0xffffffff, "UCB1400", NULL, NULL },
149 { 0x53494c20, 0xffffffe0, "Si3036/8", NULL, mpatch_si3036 },
150 { 0x54524102, 0xffffffff, "TR28022", NULL, NULL },
151 { 0x54524106, 0xffffffff, "TR28026", NULL, NULL },
152 { 0x54524108, 0xffffffff, "TR28028", patch_tritech_tr28028, NULL }, // added by xin jin [07/09/99]
153 { 0x54524123, 0xffffffff, "TR28602", NULL, NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
154 { 0x54584e20, 0xffffffff, "TLC320AD9xC", NULL, NULL },
155 { 0x56494161, 0xffffffff, "VIA1612A", NULL, NULL }, // modified ICE1232 with S/PDIF
156 { 0x57454301, 0xffffffff, "W83971D", NULL, NULL },
157 { 0x574d4c00, 0xffffffff, "WM9701A", NULL, NULL },
158 { 0x574d4C03, 0xffffffff, "WM9703/WM9707/WM9708/WM9717", patch_wolfson03, NULL},
159 { 0x574d4C04, 0xffffffff, "WM9704M/WM9704Q", patch_wolfson04, NULL},
160 { 0x574d4C05, 0xffffffff, "WM9705/WM9710", patch_wolfson05, NULL},
161 { 0x574d4C09, 0xffffffff, "WM9709", NULL, NULL},
162 { 0x574d4C12, 0xffffffff, "WM9711/WM9712", patch_wolfson11, NULL},
163 { 0x594d4800, 0xffffffff, "YMF743", NULL, NULL },
164 { 0x594d4802, 0xffffffff, "YMF752", NULL, NULL },
165 { 0x594d4803, 0xffffffff, "YMF753", patch_yamaha_ymf753, NULL },
166 { 0x83847600, 0xffffffff, "STAC9700/83/84", patch_sigmatel_stac9700, NULL },
167 { 0x83847604, 0xffffffff, "STAC9701/3/4/5", NULL, NULL },
168 { 0x83847605, 0xffffffff, "STAC9704", NULL, NULL },
169 { 0x83847608, 0xffffffff, "STAC9708/11", patch_sigmatel_stac9708, NULL },
170 { 0x83847609, 0xffffffff, "STAC9721/23", patch_sigmatel_stac9721, NULL },
171 { 0x83847644, 0xffffffff, "STAC9744", patch_sigmatel_stac9744, NULL },
172 { 0x83847650, 0xffffffff, "STAC9750/51", NULL, NULL }, // patch?
173 { 0x83847652, 0xffffffff, "STAC9752/53", NULL, NULL }, // patch?
174 { 0x83847656, 0xffffffff, "STAC9756/57", patch_sigmatel_stac9756, NULL },
175 { 0x83847658, 0xffffffff, "STAC9758/59", patch_sigmatel_stac9758, NULL },
176 { 0x83847666, 0xffffffff, "STAC9766/67", NULL, NULL }, // patch?
177 { 0, 0, NULL, NULL, NULL }
180 const char *snd_ac97_stereo_enhancements[] =
182 /* 0 */ "No 3D Stereo Enhancement",
183 /* 1 */ "Analog Devices Phat Stereo",
184 /* 2 */ "Creative Stereo Enhancement",
185 /* 3 */ "National Semi 3D Stereo Enhancement",
186 /* 4 */ "YAMAHA Ymersion",
187 /* 5 */ "BBE 3D Stereo Enhancement",
188 /* 6 */ "Crystal Semi 3D Stereo Enhancement",
189 /* 7 */ "Qsound QXpander",
190 /* 8 */ "Spatializer 3D Stereo Enhancement",
191 /* 9 */ "SRS 3D Stereo Enhancement",
192 /* 10 */ "Platform Tech 3D Stereo Enhancement",
193 /* 11 */ "AKM 3D Audio",
194 /* 12 */ "Aureal Stereo Enhancement",
195 /* 13 */ "Aztech 3D Enhancement",
196 /* 14 */ "Binaura 3D Audio Enhancement",
197 /* 15 */ "ESS Technology Stereo Enhancement",
198 /* 16 */ "Harman International VMAx",
199 /* 17 */ "Nvidea/IC Ensemble/KS Waves 3D Stereo Enhancement",
200 /* 18 */ "Philips Incredible Sound",
201 /* 19 */ "Texas Instruments 3D Stereo Enhancement",
202 /* 20 */ "VLSI Technology 3D Stereo Enhancement",
203 /* 21 */ "TriTech 3D Stereo Enhancement",
204 /* 22 */ "Realtek 3D Stereo Enhancement",
205 /* 23 */ "Samsung 3D Stereo Enhancement",
206 /* 24 */ "Wolfson Microelectronics 3D Enhancement",
207 /* 25 */ "Delta Integration 3D Enhancement",
208 /* 26 */ "SigmaTel 3D Enhancement",
209 /* 27 */ "IC Ensemble/KS Waves",
210 /* 28 */ "Rockwell 3D Stereo Enhancement",
211 /* 29 */ "Reserved 29",
212 /* 30 */ "Reserved 30",
213 /* 31 */ "Reserved 31"
220 static int snd_ac97_valid_reg(ac97_t *ac97, unsigned short reg)
222 if (ac97->limited_regs && ! test_bit(reg, ac97->reg_accessed))
225 /* filter some registers for buggy codecs */
229 if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
232 case AC97_ID_AD1819: /* AD1819 */
233 case AC97_ID_AD1881: /* AD1881 */
234 case AC97_ID_AD1881A: /* AD1881A */
235 if (reg >= 0x3a && reg <= 0x6e) /* 0x59 */
238 case AC97_ID_AD1885: /* AD1885 */
239 case AC97_ID_AD1886: /* AD1886 */
240 case AC97_ID_AD1886A: /* AD1886A - !!verify!! --jk */
241 case AC97_ID_AD1887: /* AD1887 - !!verify!! --jk */
244 if (reg >= 0x3c && reg <= 0x6e) /* 0x59 */
247 case AC97_ID_STAC9700:
248 case AC97_ID_STAC9704:
249 case AC97_ID_STAC9705:
250 case AC97_ID_STAC9708:
251 case AC97_ID_STAC9721:
252 case AC97_ID_STAC9744:
253 case AC97_ID_STAC9756:
254 if (reg <= 0x3a || reg >= 0x5a)
262 * snd_ac97_write - write a value on the given register
263 * @ac97: the ac97 instance
264 * @reg: the register to change
265 * @value: the value to set
267 * Writes a value on the given register. This will invoke the write
268 * callback directly after the register check.
269 * This function doesn't change the register cache unlike
270 * #snd_ca97_write_cache(), so use this only when you don't want to
271 * reflect the change to the suspend/resume state.
273 void snd_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short value)
275 if (!snd_ac97_valid_reg(ac97, reg))
277 if ((ac97->id & 0xffffff00) == 0x414c4300) {
278 /* Fix H/W bug of ALC100/100P */
279 if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
280 ac97->bus->write(ac97, AC97_RESET, 0); /* reset audio codec */
282 ac97->bus->write(ac97, reg, value);
286 * snd_ac97_read - read a value from the given register
288 * @ac97: the ac97 instance
289 * @reg: the register to read
291 * Reads a value from the given register. This will invoke the read
292 * callback directly after the register check.
294 * Returns the read value.
296 unsigned short snd_ac97_read(ac97_t *ac97, unsigned short reg)
298 if (!snd_ac97_valid_reg(ac97, reg))
300 return ac97->bus->read(ac97, reg);
303 /* read a register - return the cached value if already read */
304 static inline unsigned short snd_ac97_read_cache(ac97_t *ac97, unsigned short reg)
306 if (! test_bit(reg, ac97->reg_accessed)) {
307 ac97->regs[reg] = ac97->bus->read(ac97, reg);
308 // set_bit(reg, ac97->reg_accessed);
310 return ac97->regs[reg];
314 * snd_ac97_write_cache - write a value on the given register and update the cache
315 * @ac97: the ac97 instance
316 * @reg: the register to change
317 * @value: the value to set
319 * Writes a value on the given register and updates the register
320 * cache. The cached values are used for the cached-read and the
323 void snd_ac97_write_cache(ac97_t *ac97, unsigned short reg, unsigned short value)
325 if (!snd_ac97_valid_reg(ac97, reg))
327 spin_lock(&ac97->reg_lock);
328 ac97->regs[reg] = value;
329 spin_unlock(&ac97->reg_lock);
330 ac97->bus->write(ac97, reg, value);
331 set_bit(reg, ac97->reg_accessed);
335 * snd_ac97_update - update the value on the given register
336 * @ac97: the ac97 instance
337 * @reg: the register to change
338 * @value: the value to set
340 * Compares the value with the register cache and updates the value
341 * only when the value is changed.
343 * Returns 1 if the value is changed, 0 if no change, or a negative
346 int snd_ac97_update(ac97_t *ac97, unsigned short reg, unsigned short value)
350 if (!snd_ac97_valid_reg(ac97, reg))
352 spin_lock(&ac97->reg_lock);
353 change = ac97->regs[reg] != value;
355 ac97->regs[reg] = value;
356 spin_unlock(&ac97->reg_lock);
357 ac97->bus->write(ac97, reg, value);
359 spin_unlock(&ac97->reg_lock);
364 * snd_ac97_update_bits - update the bits on the given register
365 * @ac97: the ac97 instance
366 * @reg: the register to change
367 * @mask: the bit-mask to change
368 * @value: the value to set
370 * Updates the masked-bits on the given register only when the value
373 * Returns 1 if the bits are changed, 0 if no change, or a negative
376 int snd_ac97_update_bits(ac97_t *ac97, unsigned short reg, unsigned short mask, unsigned short value)
379 unsigned short old, new;
381 if (!snd_ac97_valid_reg(ac97, reg))
383 spin_lock(&ac97->reg_lock);
384 old = snd_ac97_read_cache(ac97, reg);
385 new = (old & ~mask) | value;
388 ac97->regs[reg] = new;
389 spin_unlock(&ac97->reg_lock);
390 ac97->bus->write(ac97, reg, new);
392 spin_unlock(&ac97->reg_lock);
396 static int snd_ac97_ad18xx_update_pcm_bits(ac97_t *ac97, int codec, unsigned short mask, unsigned short value)
399 unsigned short old, new, cfg;
401 down(&ac97->spec.ad18xx.mutex);
402 spin_lock(&ac97->reg_lock);
403 old = ac97->spec.ad18xx.pcmreg[codec];
404 new = (old & ~mask) | value;
405 cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
408 ac97->spec.ad18xx.pcmreg[codec] = new;
409 spin_unlock(&ac97->reg_lock);
410 /* select single codec */
411 ac97->bus->write(ac97, AC97_AD_SERIAL_CFG,
413 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
414 /* update PCM bits */
415 ac97->bus->write(ac97, AC97_PCM, new);
416 /* select all codecs */
417 ac97->bus->write(ac97, AC97_AD_SERIAL_CFG,
420 spin_unlock(&ac97->reg_lock);
421 up(&ac97->spec.ad18xx.mutex);
429 static int snd_ac97_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
431 static char *texts[8] = {
432 "Mic", "CD", "Video", "Aux", "Line",
433 "Mix", "Mix Mono", "Phone"
436 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
438 uinfo->value.enumerated.items = 8;
439 if (uinfo->value.enumerated.item > 7)
440 uinfo->value.enumerated.item = 7;
441 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
445 static int snd_ac97_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
447 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
450 val = snd_ac97_read_cache(ac97, AC97_REC_SEL);
451 ucontrol->value.enumerated.item[0] = (val >> 8) & 7;
452 ucontrol->value.enumerated.item[1] = (val >> 0) & 7;
456 static int snd_ac97_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
458 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
461 if (ucontrol->value.enumerated.item[0] > 7 ||
462 ucontrol->value.enumerated.item[1] > 7)
464 val = (ucontrol->value.enumerated.item[0] << 8) |
465 (ucontrol->value.enumerated.item[1] << 0);
466 return snd_ac97_update(ac97, AC97_REC_SEL, val);
469 #define AC97_ENUM_DOUBLE(xname, reg, shift, invert) \
470 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_info_enum_double, \
471 .get = snd_ac97_get_enum_double, .put = snd_ac97_put_enum_double, \
472 .private_value = reg | (shift << 8) | (invert << 24) }
474 static int snd_ac97_info_enum_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
476 static char *texts1[2] = { "pre 3D", "post 3D" };
477 static char *texts2[2] = { "Mix", "Mic" };
478 static char *texts3[2] = { "Mic1", "Mic2" };
480 int reg = kcontrol->private_value & 0xff;
481 int shift = (kcontrol->private_value >> 8) & 0xff;
484 case AC97_GENERAL_PURPOSE:
486 case 15: texts = texts1; break;
487 case 9: texts = texts2; break;
488 case 8: texts = texts3; break;
491 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
493 uinfo->value.enumerated.items = 2;
494 if (uinfo->value.enumerated.item > 1)
495 uinfo->value.enumerated.item = 1;
496 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
500 static int snd_ac97_get_enum_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
502 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
504 int reg = kcontrol->private_value & 0xff;
505 int shift = (kcontrol->private_value >> 8) & 0xff;
506 int invert = (kcontrol->private_value >> 24) & 0xff;
508 val = (snd_ac97_read_cache(ac97, reg) >> shift) & 1;
511 ucontrol->value.enumerated.item[0] = val;
515 static int snd_ac97_put_enum_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
517 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
519 int reg = kcontrol->private_value & 0xff;
520 int shift = (kcontrol->private_value >> 8) & 0xff;
521 int invert = (kcontrol->private_value >> 24) & 0xff;
523 if (ucontrol->value.enumerated.item[0] > 1)
525 val = !!ucontrol->value.enumerated.item[0];
528 return snd_ac97_update_bits(ac97, reg, 1 << shift, val << shift);
531 int snd_ac97_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
533 int mask = (kcontrol->private_value >> 16) & 0xff;
535 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
537 uinfo->value.integer.min = 0;
538 uinfo->value.integer.max = mask;
542 int snd_ac97_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
544 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
545 int reg = kcontrol->private_value & 0xff;
546 int shift = (kcontrol->private_value >> 8) & 0xff;
547 int mask = (kcontrol->private_value >> 16) & 0xff;
548 int invert = (kcontrol->private_value >> 24) & 0xff;
550 ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
552 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
556 int snd_ac97_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
558 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
559 int reg = kcontrol->private_value & 0xff;
560 int shift = (kcontrol->private_value >> 8) & 0xff;
561 int mask = (kcontrol->private_value >> 16) & 0xff;
562 int invert = (kcontrol->private_value >> 24) & 0xff;
565 val = (ucontrol->value.integer.value[0] & mask);
568 return snd_ac97_update_bits(ac97, reg, mask << shift, val << shift);
571 #define AC97_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
572 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .info = snd_ac97_info_double, \
573 .get = snd_ac97_get_double, .put = snd_ac97_put_double, \
574 .private_value = (reg) | ((shift_left) << 8) | ((shift_right) << 12) | ((mask) << 16) | ((invert) << 24) }
576 static int snd_ac97_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
578 int mask = (kcontrol->private_value >> 16) & 0xff;
580 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
582 uinfo->value.integer.min = 0;
583 uinfo->value.integer.max = mask;
587 static int snd_ac97_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
589 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
590 int reg = kcontrol->private_value & 0xff;
591 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
592 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
593 int mask = (kcontrol->private_value >> 16) & 0xff;
594 int invert = (kcontrol->private_value >> 24) & 0xff;
596 spin_lock(&ac97->reg_lock);
597 ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift_left) & mask;
598 ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> shift_right) & mask;
599 spin_unlock(&ac97->reg_lock);
601 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
602 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
607 static int snd_ac97_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
609 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
610 int reg = kcontrol->private_value & 0xff;
611 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
612 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
613 int mask = (kcontrol->private_value >> 16) & 0xff;
614 int invert = (kcontrol->private_value >> 24) & 0xff;
615 unsigned short val1, val2;
617 val1 = ucontrol->value.integer.value[0] & mask;
618 val2 = ucontrol->value.integer.value[1] & mask;
623 return snd_ac97_update_bits(ac97, reg,
624 (mask << shift_left) | (mask << shift_right),
625 (val1 << shift_left) | (val2 << shift_right));
628 static const snd_kcontrol_new_t snd_ac97_controls_master_mono[2] = {
629 AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
630 AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
633 static const snd_kcontrol_new_t snd_ac97_controls_tone[2] = {
634 AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
635 AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
638 static const snd_kcontrol_new_t snd_ac97_controls_pc_beep[2] = {
639 AC97_SINGLE("PC Speaker Playback Switch", AC97_PC_BEEP, 15, 1, 1),
640 AC97_SINGLE("PC Speaker Playback Volume", AC97_PC_BEEP, 1, 15, 1)
643 static const snd_kcontrol_new_t snd_ac97_controls_phone[2] = {
644 AC97_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
645 AC97_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 15, 1)
648 static const snd_kcontrol_new_t snd_ac97_controls_mic[3] = {
649 AC97_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
650 AC97_SINGLE("Mic Playback Volume", AC97_MIC, 0, 15, 1),
651 AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0)
654 static const snd_kcontrol_new_t snd_ac97_control_capture_src = {
655 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
656 .name = "Capture Source",
657 .info = snd_ac97_info_mux,
658 .get = snd_ac97_get_mux,
659 .put = snd_ac97_put_mux,
662 static const snd_kcontrol_new_t snd_ac97_control_capture_vol =
663 AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);
665 static const snd_kcontrol_new_t snd_ac97_controls_mic_capture[2] = {
666 AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
667 AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
671 AC97_GENERAL_PCM_OUT = 0,
672 AC97_GENERAL_STEREO_ENHANCEMENT,
674 AC97_GENERAL_LOUDNESS,
677 AC97_GENERAL_LOOPBACK
678 } ac97_general_index_t;
680 static const snd_kcontrol_new_t snd_ac97_controls_general[7] = {
681 AC97_ENUM_DOUBLE("PCM Out Path & Mute", AC97_GENERAL_PURPOSE, 15, 0),
682 AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
683 AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
684 AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
685 AC97_ENUM_DOUBLE("Mono Output Select", AC97_GENERAL_PURPOSE, 9, 0),
686 AC97_ENUM_DOUBLE("Mic Select", AC97_GENERAL_PURPOSE, 8, 0),
687 AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
690 const snd_kcontrol_new_t snd_ac97_controls_3d[2] = {
691 AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
692 AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
695 static const snd_kcontrol_new_t snd_ac97_controls_center[2] = {
696 AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
697 AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
700 static const snd_kcontrol_new_t snd_ac97_controls_lfe[2] = {
701 AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
702 AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
705 static const snd_kcontrol_new_t snd_ac97_controls_surround[2] = {
706 AC97_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
707 AC97_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
710 static const snd_kcontrol_new_t snd_ac97_control_eapd =
711 AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);
713 static int snd_ac97_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
715 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
720 static int snd_ac97_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
722 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
723 IEC958_AES0_NONAUDIO |
724 IEC958_AES0_CON_EMPHASIS_5015 |
725 IEC958_AES0_CON_NOT_COPYRIGHT;
726 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
727 IEC958_AES1_CON_ORIGINAL;
728 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
732 static int snd_ac97_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
734 /* FIXME: AC'97 spec doesn't say which bits are used for what */
735 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
736 IEC958_AES0_NONAUDIO |
738 IEC958_AES0_PRO_EMPHASIS_5015;
742 static int snd_ac97_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
744 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
746 spin_lock(&ac97->reg_lock);
747 ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
748 ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
749 ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
750 ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
751 spin_unlock(&ac97->reg_lock);
755 static int snd_ac97_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
757 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
758 unsigned int new = 0;
759 unsigned short val = 0;
762 spin_lock(&ac97->reg_lock);
763 new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
764 if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
765 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
766 switch (new & IEC958_AES0_PRO_FS) {
767 case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
768 case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
769 case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
770 default: val |= 1<<12; break;
772 if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
775 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
776 new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
777 new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
778 if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
780 if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
782 val |= ((new >> 8) & 0xff) << 4; // category + original
783 switch ((new >> 24) & 0xff) {
784 case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
785 case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
786 case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
787 default: val |= 1<<12; break;
791 change = ac97->spdif_status != new;
792 ac97->spdif_status = new;
793 spin_unlock(&ac97->reg_lock);
795 if (ac97->flags & AC97_CS_SPDIF) {
796 int x = (val >> 12) & 0x03;
798 case 0: x = 1; break; // 44.1
799 case 2: x = 0; break; // 48.0
800 default: x = 0; break; // illegal.
802 change |= snd_ac97_update_bits(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
803 } else if (ac97->flags & AC97_CX_SPDIF) {
805 v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
806 v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
807 change |= snd_ac97_update_bits(ac97, AC97_CXR_AUDIO_MISC,
808 AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
811 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
812 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
814 change |= snd_ac97_update_bits(ac97, AC97_SPDIF, 0x3fff, val);
815 if (extst & AC97_EA_SPDIF) {
816 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
823 static int snd_ac97_put_spsa(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
825 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
826 int reg = kcontrol->private_value & 0xff;
827 int shift = (kcontrol->private_value >> 8) & 0xff;
828 int mask = (kcontrol->private_value >> 16) & 0xff;
829 // int invert = (kcontrol->private_value >> 24) & 0xff;
830 unsigned short value, old, new;
832 value = (ucontrol->value.integer.value[0] & mask);
836 spin_lock(&ac97->reg_lock);
837 old = snd_ac97_read_cache(ac97, reg);
838 new = (old & ~mask) | value;
839 spin_unlock(&ac97->reg_lock);
843 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
844 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
845 change = snd_ac97_update_bits(ac97, reg, mask, value);
846 if (extst & AC97_EA_SPDIF)
847 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
853 const snd_kcontrol_new_t snd_ac97_controls_spdif[5] = {
855 .access = SNDRV_CTL_ELEM_ACCESS_READ,
856 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
857 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
858 .info = snd_ac97_spdif_mask_info,
859 .get = snd_ac97_spdif_cmask_get,
862 .access = SNDRV_CTL_ELEM_ACCESS_READ,
863 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
864 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
865 .info = snd_ac97_spdif_mask_info,
866 .get = snd_ac97_spdif_pmask_get,
869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
870 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
871 .info = snd_ac97_spdif_mask_info,
872 .get = snd_ac97_spdif_default_get,
873 .put = snd_ac97_spdif_default_put,
876 AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
878 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
879 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
880 .info = snd_ac97_info_single,
881 .get = snd_ac97_get_single,
882 .put = snd_ac97_put_spsa,
883 .private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
887 #define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
888 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
889 .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
890 .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }
892 static int snd_ac97_ad18xx_pcm_info_bits(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
894 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
895 int mask = (kcontrol->private_value >> 16) & 0x0f;
896 int lshift = (kcontrol->private_value >> 8) & 0x0f;
897 int rshift = (kcontrol->private_value >> 12) & 0x0f;
899 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
900 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
904 uinfo->value.integer.min = 0;
905 uinfo->value.integer.max = mask;
909 static int snd_ac97_ad18xx_pcm_get_bits(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
911 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
912 int codec = kcontrol->private_value & 3;
913 int lshift = (kcontrol->private_value >> 8) & 0x0f;
914 int rshift = (kcontrol->private_value >> 12) & 0x0f;
915 int mask = (kcontrol->private_value >> 16) & 0xff;
917 ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
918 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
919 ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
923 static int snd_ac97_ad18xx_pcm_put_bits(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
925 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
926 int codec = kcontrol->private_value & 3;
927 int lshift = (kcontrol->private_value >> 8) & 0x0f;
928 int rshift = (kcontrol->private_value >> 12) & 0x0f;
929 int mask = (kcontrol->private_value >> 16) & 0xff;
930 unsigned short val, valmask;
932 val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
933 valmask = mask << lshift;
934 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
935 val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
936 valmask |= mask << rshift;
938 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
941 #define AD18XX_PCM_VOLUME(xname, codec) \
942 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
943 .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
944 .private_value = codec }
946 static int snd_ac97_ad18xx_pcm_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
948 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
950 uinfo->value.integer.min = 0;
951 uinfo->value.integer.max = 31;
955 static int snd_ac97_ad18xx_pcm_get_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
957 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
958 int codec = kcontrol->private_value & 3;
960 spin_lock(&ac97->reg_lock);
961 ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
962 ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
963 spin_unlock(&ac97->reg_lock);
967 static int snd_ac97_ad18xx_pcm_put_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
969 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
970 int codec = kcontrol->private_value & 3;
971 unsigned short val1, val2;
973 val1 = 31 - (ucontrol->value.integer.value[0] & 31);
974 val2 = 31 - (ucontrol->value.integer.value[1] & 31);
975 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
978 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_pcm[2] = {
979 AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
980 AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
983 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_surround[2] = {
984 AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
985 AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
988 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_center[2] = {
989 AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
990 AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
993 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_lfe[2] = {
994 AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
995 AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
1002 static void snd_ac97_powerdown(ac97_t *ac97);
1004 static int snd_ac97_bus_free(ac97_bus_t *bus)
1007 snd_ac97_bus_proc_done(bus);
1010 if (bus->private_free)
1011 bus->private_free(bus);
1012 snd_magic_kfree(bus);
1017 static int snd_ac97_bus_dev_free(snd_device_t *device)
1019 ac97_bus_t *bus = snd_magic_cast(ac97_bus_t, device->device_data, return -ENXIO);
1020 return snd_ac97_bus_free(bus);
1023 static int snd_ac97_free(ac97_t *ac97)
1026 snd_ac97_proc_done(ac97);
1028 ac97->bus->codec[ac97->num] = NULL;
1029 if (ac97->private_free)
1030 ac97->private_free(ac97);
1031 snd_magic_kfree(ac97);
1036 static int snd_ac97_dev_free(snd_device_t *device)
1038 ac97_t *ac97 = snd_magic_cast(ac97_t, device->device_data, return -ENXIO);
1039 snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
1040 return snd_ac97_free(ac97);
1043 static int snd_ac97_try_volume_mix(ac97_t * ac97, int reg)
1045 unsigned short val, mask = 0x8000;
1047 if (! snd_ac97_valid_reg(ac97, reg))
1051 case AC97_MASTER_TONE:
1052 return ac97->caps & 0x04 ? 1 : 0;
1053 case AC97_HEADPHONE:
1054 return ac97->caps & 0x10 ? 1 : 0;
1055 case AC97_REC_GAIN_MIC:
1056 return ac97->caps & 0x01 ? 1 : 0;
1057 case AC97_3D_CONTROL:
1058 if (ac97->caps & 0x7c00) {
1059 val = snd_ac97_read(ac97, reg);
1060 /* if nonzero - fixed and we can't set it */
1064 case AC97_CENTER_LFE_MASTER: /* center */
1065 if ((ac97->ext_id & AC97_EI_CDAC) == 0)
1068 case AC97_CENTER_LFE_MASTER+1: /* lfe */
1069 if ((ac97->ext_id & AC97_EI_LDAC) == 0)
1071 reg = AC97_CENTER_LFE_MASTER;
1074 case AC97_SURROUND_MASTER:
1075 if ((ac97->ext_id & AC97_EI_SDAC) == 0)
1080 if (ac97->limited_regs && test_bit(reg, ac97->reg_accessed))
1081 return 1; /* allow without check */
1083 val = snd_ac97_read(ac97, reg);
1084 if (!(val & mask)) {
1085 /* nothing seems to be here - mute flag is not set */
1086 /* try another test */
1087 snd_ac97_write_cache(ac97, reg, val | mask);
1088 val = snd_ac97_read(ac97, reg);
1090 return 0; /* nothing here */
1092 return 1; /* success, useable */
1095 int snd_ac97_try_bit(ac97_t * ac97, int reg, int bit)
1097 unsigned short mask, val, orig, res;
1100 orig = snd_ac97_read(ac97, reg);
1102 snd_ac97_write(ac97, reg, val);
1103 res = snd_ac97_read(ac97, reg);
1104 snd_ac97_write_cache(ac97, reg, orig);
1108 static void snd_ac97_change_volume_params1(ac97_t * ac97, int reg, unsigned char *max)
1110 unsigned short val, val1;
1113 val = 0x8000 | 0x0020;
1114 snd_ac97_write(ac97, reg, val);
1115 val1 = snd_ac97_read(ac97, reg);
1119 /* reset volume to zero */
1120 snd_ac97_write_cache(ac97, reg, 0x8000);
1123 static void snd_ac97_change_volume_params2(ac97_t * ac97, int reg, int shift, unsigned char *max)
1125 unsigned short val, val1;
1128 val = 0x8080 | (0x20 << shift);
1129 snd_ac97_write(ac97, reg, val);
1130 val1 = snd_ac97_read(ac97, reg);
1134 /* reset volume to zero */
1135 snd_ac97_write_cache(ac97, reg, 0x8080);
1138 static void snd_ac97_change_volume_params3(ac97_t * ac97, int reg, unsigned char *max)
1140 unsigned short val, val1;
1143 val = 0x8000 | 0x0010;
1144 snd_ac97_write(ac97, reg, val);
1145 val1 = snd_ac97_read(ac97, reg);
1149 /* reset volume to zero */
1150 snd_ac97_write_cache(ac97, reg, 0x8000);
1153 static inline int printable(unsigned int x)
1156 if (x < ' ' || x >= 0x71) {
1158 return x - 0x71 + 'A';
1164 snd_kcontrol_t *snd_ac97_cnew(const snd_kcontrol_new_t *_template, ac97_t * ac97)
1166 snd_kcontrol_new_t template;
1167 memcpy(&template, _template, sizeof(template));
1168 snd_runtime_check(!template.index, return NULL);
1169 template.index = ac97->num;
1170 return snd_ctl_new1(&template, ac97);
1174 * create mute switch(es) for normal stereo controls
1176 static int snd_ac97_cmute_new(snd_card_t *card, char *name, int reg, ac97_t *ac97)
1178 snd_kcontrol_t *kctl;
1181 if (ac97->flags & AC97_STEREO_MUTES) {
1182 /* check whether both mute bits work */
1183 unsigned short val, val1;
1184 val = snd_ac97_read(ac97, reg);
1185 val1 = val | 0x8080;
1186 snd_ac97_write(ac97, reg, val1);
1187 if (val1 == snd_ac97_read(ac97, reg))
1189 snd_ac97_write(ac97, reg, val);
1192 snd_kcontrol_new_t tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
1193 tmp.index = ac97->num;
1194 kctl = snd_ctl_new1(&tmp, ac97);
1196 snd_kcontrol_new_t tmp = AC97_SINGLE(name, reg, 15, 1, 1);
1197 tmp.index = ac97->num;
1198 kctl = snd_ctl_new1(&tmp, ac97);
1200 return snd_ctl_add(card, kctl);
1204 * create volumes for normal stereo controls
1206 static int snd_ac97_cvol_new(snd_card_t *card, char *name, int reg, unsigned int max, ac97_t *ac97)
1209 snd_kcontrol_new_t tmp = AC97_DOUBLE(name, reg, 8, 0, (unsigned int)max, 1);
1210 tmp.index = ac97->num;
1211 if ((err = snd_ctl_add(card, snd_ctl_new1(&tmp, ac97))) < 0)
1213 snd_ac97_write_cache(ac97, reg,
1214 ((ac97->flags & AC97_STEREO_MUTES) ? 0x8080 : 0x8000) |
1215 (unsigned short)max | ((unsigned short)max << 8));
1220 * create mute-switch and volumes for normal stereo controls
1222 static int snd_ac97_cmix_new(snd_card_t *card, const char *pfx, int reg, int check_res, ac97_t *ac97)
1228 sprintf(name, "%s Switch", pfx);
1229 if ((err = snd_ac97_cmute_new(card, name, reg, ac97)) < 0)
1231 sprintf(name, "%s Volume", pfx);
1233 snd_ac97_change_volume_params1(ac97, reg, &max);
1235 max = 31; /* 5bit */
1236 if ((err = snd_ac97_cvol_new(card, name, reg, max, ac97)) < 0)
1242 static int snd_ac97_mixer_build(ac97_t * ac97)
1244 snd_card_t *card = ac97->bus->card;
1245 snd_kcontrol_t *kctl;
1250 /* build master controls */
1251 /* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
1252 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
1253 if ((err = snd_ac97_cmix_new(card, "Master Playback", AC97_MASTER, 1, ac97)) < 0)
1257 ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080;
1259 /* build center controls */
1260 if (snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER)) {
1261 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
1263 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
1265 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 0, &max);
1266 kctl->private_value &= ~(0xff << 16);
1267 kctl->private_value |= (int)max << 16;
1268 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
1271 /* build LFE controls */
1272 if (snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1)) {
1273 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
1275 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
1277 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
1278 kctl->private_value &= ~(0xff << 16);
1279 kctl->private_value |= (int)max << 16;
1280 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
1283 /* build surround controls */
1284 if (snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER)) {
1285 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_surround[0], ac97))) < 0)
1287 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_surround[1], ac97))) < 0)
1289 snd_ac97_change_volume_params2(ac97, AC97_SURROUND_MASTER, 0, &max);
1290 kctl->private_value &= ~(0xff << 16);
1291 kctl->private_value |= (int)max << 16;
1292 snd_ac97_write_cache(ac97, AC97_SURROUND_MASTER, 0x8080 | max | (max << 8));
1295 /* build headphone controls */
1296 if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE) || ac97->id == AC97_ID_STAC9708) {
1297 const char *name = ac97->id == AC97_ID_STAC9708 ?
1298 "Sigmatel Surround Playback" :
1299 "Headphone Playback";
1300 if ((err = snd_ac97_cmix_new(card, name, AC97_HEADPHONE, 1, ac97)) < 0)
1304 /* build master mono controls */
1305 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
1306 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_master_mono[0], ac97))) < 0)
1308 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_master_mono[1], ac97))) < 0)
1310 snd_ac97_change_volume_params1(ac97, AC97_MASTER_MONO, &max);
1311 kctl->private_value &= ~(0xff << 16);
1312 kctl->private_value |= (int)max << 16;
1313 snd_ac97_write_cache(ac97, AC97_MASTER_MONO, 0x8000 | max);
1316 /* build master tone controls */
1317 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
1318 for (idx = 0; idx < 2; idx++) {
1319 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
1321 if (ac97->id == AC97_ID_YMF753) {
1322 kctl->private_value &= ~(0xff << 16);
1323 kctl->private_value |= 7 << 16;
1326 snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
1329 /* build PC Speaker controls */
1330 if ((ac97->flags & AC97_HAS_PC_BEEP) ||
1331 snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP)) {
1332 for (idx = 0; idx < 2; idx++)
1333 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
1335 snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x801e);
1338 /* build Phone controls */
1339 if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
1340 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_phone[0], ac97))) < 0)
1342 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_phone[1], ac97))) < 0)
1344 snd_ac97_change_volume_params3(ac97, AC97_PHONE, &max);
1345 kctl->private_value &= ~(0xff << 16);
1346 kctl->private_value |= (int)max << 16;
1347 snd_ac97_write_cache(ac97, AC97_PHONE, 0x8000 | max);
1350 /* build MIC controls */
1351 snd_ac97_change_volume_params3(ac97, AC97_MIC, &max);
1352 for (idx = 0; idx < 3; idx++) {
1353 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_mic[idx], ac97))) < 0)
1355 if (idx == 1) { // volume
1356 kctl->private_value &= ~(0xff << 16);
1357 kctl->private_value |= (int)max << 16;
1360 snd_ac97_write_cache(ac97, AC97_MIC, 0x8000 | max);
1362 /* build Line controls */
1363 if ((err = snd_ac97_cmix_new(card, "Line Playback", AC97_LINE, 0, ac97)) < 0)
1366 /* build CD controls */
1367 if ((err = snd_ac97_cmix_new(card, "CD Playback", AC97_CD, 0, ac97)) < 0)
1370 /* build Video controls */
1371 if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
1372 if ((err = snd_ac97_cmix_new(card, "Video Playback", AC97_VIDEO, 0, ac97)) < 0)
1376 /* build Aux controls */
1377 if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
1378 if ((err = snd_ac97_cmix_new(card, "Aux Playback", AC97_AUX, 0, ac97)) < 0)
1382 /* build PCM controls */
1383 if (ac97->flags & AC97_AD_MULTI) {
1384 unsigned short init_val;
1385 if (ac97->flags & AC97_STEREO_MUTES)
1389 for (idx = 0; idx < 2; idx++)
1390 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
1392 ac97->spec.ad18xx.pcmreg[0] = init_val;
1393 if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
1394 for (idx = 0; idx < 2; idx++)
1395 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
1397 ac97->spec.ad18xx.pcmreg[1] = init_val;
1399 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
1400 for (idx = 0; idx < 2; idx++)
1401 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
1403 for (idx = 0; idx < 2; idx++)
1404 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
1406 ac97->spec.ad18xx.pcmreg[2] = init_val;
1408 snd_ac97_write_cache(ac97, AC97_PCM, init_val);
1410 if ((err = snd_ac97_cmute_new(card, "PCM Playback Switch", AC97_PCM, ac97)) < 0)
1412 /* FIXME: C-Media chips have no PCM volume!! */
1413 if (/*ac97->id == 0x434d4941 ||*/
1414 ac97->id == 0x434d4942 ||
1415 ac97->id == 0x434d4961)
1416 snd_ac97_write_cache(ac97, AC97_PCM, 0x9f1f);
1418 if ((err = snd_ac97_cvol_new(card, "PCM Playback Volume", AC97_PCM, 31, ac97)) < 0)
1423 /* build Capture controls */
1424 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
1426 if ((err = snd_ac97_cmute_new(card, "Capture Switch", AC97_REC_GAIN, ac97)) < 0)
1428 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
1430 snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
1431 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
1433 /* build MIC Capture controls */
1434 if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
1435 for (idx = 0; idx < 2; idx++)
1436 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
1438 snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
1441 /* build PCM out path & mute control */
1442 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
1443 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
1447 /* build Simulated Stereo Enhancement control */
1448 if (ac97->caps & 0x0008) {
1449 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
1453 /* build 3D Stereo Enhancement control */
1454 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
1455 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
1459 /* build Loudness control */
1460 if (ac97->caps & 0x0020) {
1461 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
1465 /* build Mono output select control */
1466 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
1467 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
1471 /* build Mic select control */
1472 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
1473 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
1477 /* build ADC/DAC loopback control */
1478 if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
1479 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
1483 snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0x0000);
1485 /* build 3D controls */
1486 if (ac97->build_ops && ac97->build_ops->build_3d) {
1487 ac97->build_ops->build_3d(ac97);
1489 if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
1492 snd_ac97_write(ac97, AC97_3D_CONTROL, val);
1493 val = snd_ac97_read(ac97, AC97_3D_CONTROL);
1494 val = val == 0x0606;
1495 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
1498 kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
1499 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
1502 kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
1503 snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
1507 /* build S/PDIF controls */
1508 if (ac97->ext_id & AC97_EI_SPDIF) {
1509 if (ac97->build_ops && ac97->build_ops->build_spdif) {
1510 if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
1513 for (idx = 0; idx < 5; idx++)
1514 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
1516 if (ac97->build_ops && ac97->build_ops->build_post_spdif) {
1517 if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
1520 /* set default PCM S/PDIF params */
1521 /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1522 snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
1524 ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
1527 /* build chip specific controls */
1528 if (ac97->build_ops && ac97->build_ops->build_specific)
1529 if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1532 if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
1533 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_eapd, ac97))) < 0)
1540 static int snd_ac97_modem_build(snd_card_t * card, ac97_t * ac97)
1543 //printk("AC97_GPIO_CFG = %x\n",snd_ac97_read(ac97,AC97_GPIO_CFG));
1544 snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
1545 snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
1546 snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
1547 snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
1548 snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
1552 static int snd_ac97_test_rate(ac97_t *ac97, int reg, int shadow_reg, int rate)
1557 tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
1558 snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
1560 snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
1561 val = snd_ac97_read(ac97, reg);
1562 return val == (tmp & 0xffff);
1565 static void snd_ac97_determine_rates(ac97_t *ac97, int reg, int shadow_reg, unsigned int *r_result)
1567 unsigned int result = 0;
1569 /* test a non-standard rate */
1570 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
1571 result |= SNDRV_PCM_RATE_CONTINUOUS;
1572 /* let's try to obtain standard rates */
1573 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
1574 result |= SNDRV_PCM_RATE_8000;
1575 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
1576 result |= SNDRV_PCM_RATE_11025;
1577 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
1578 result |= SNDRV_PCM_RATE_16000;
1579 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
1580 result |= SNDRV_PCM_RATE_22050;
1581 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
1582 result |= SNDRV_PCM_RATE_32000;
1583 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
1584 result |= SNDRV_PCM_RATE_44100;
1585 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
1586 result |= SNDRV_PCM_RATE_48000;
1590 /* check AC97_SPDIF register to accept which sample rates */
1591 static unsigned int snd_ac97_determine_spdif_rates(ac97_t *ac97)
1593 unsigned int result = 0;
1595 static unsigned short ctl_bits[] = {
1596 AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
1598 static unsigned int rate_bits[] = {
1599 SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
1602 for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
1603 snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
1604 if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
1605 result |= rate_bits[i];
1610 void snd_ac97_get_name(ac97_t *ac97, unsigned int id, char *name, int modem)
1612 const ac97_codec_id_t *pid;
1614 sprintf(name, "0x%x %c%c%c", id,
1615 printable(id >> 24),
1616 printable(id >> 16),
1617 printable(id >> 8));
1618 for (pid = snd_ac97_codec_id_vendors; pid->id; pid++)
1619 if (pid->id == (id & pid->mask)) {
1620 strcpy(name, pid->name);
1622 if (!modem && pid->patch)
1624 else if (modem && pid->mpatch)
1632 for (pid = snd_ac97_codec_ids; pid->id; pid++)
1633 if (pid->id == (id & pid->mask)) {
1635 strcat(name, pid->name);
1636 if (pid->mask != 0xffffffff)
1637 sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
1639 if (!modem && pid->patch)
1641 else if (modem && pid->mpatch)
1646 sprintf(name + strlen(name), " id %x", id & 0xff);
1650 /* wait for a while until registers are accessible after RESET
1651 * return 0 if ok, negative not ready
1653 static int ac97_reset_wait(ac97_t *ac97, int timeout, int with_modem)
1655 unsigned long end_time;
1656 end_time = jiffies + timeout;
1658 unsigned short ext_mid;
1660 /* use preliminary reads to settle the communication */
1661 snd_ac97_read(ac97, AC97_RESET);
1662 snd_ac97_read(ac97, AC97_VENDOR_ID1);
1663 snd_ac97_read(ac97, AC97_VENDOR_ID2);
1666 ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
1667 if (ext_mid != 0xffff && (ext_mid & 1) != 0)
1670 /* because the PCM or MASTER volume registers can be modified,
1671 * the REC_GAIN register is used for tests
1673 /* test if we can write to the record gain volume register */
1674 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
1675 if (snd_ac97_read(ac97, AC97_REC_GAIN) == 0x8a05)
1677 set_current_state(TASK_UNINTERRUPTIBLE);
1678 schedule_timeout(HZ/100);
1679 } while (time_after_eq(end_time, jiffies));
1684 * snd_ac97_bus - create an AC97 bus component
1685 * @card: the card instance
1686 * @_bus: the template of AC97 bus, callbacks and
1688 * @rbus: the pointer to store the new AC97 bus instance.
1690 * Creates an AC97 bus component. An ac97_bus_t instance is newly
1691 * allocated and initialized from the template (_bus).
1693 * The template must include the valid callbacks (at least read and
1694 * write), the bus number (num), and the private data (private_data).
1695 * The other callbacks, wait and reset, are not mandatory.
1697 * The clock is set to 48000. If another clock is needed, set
1698 * bus->clock manually.
1700 * The AC97 bus instance is registered as a low-level device, so you don't
1701 * have to release it manually.
1703 * Returns zero if successful, or a negative error code on failure.
1705 int snd_ac97_bus(snd_card_t * card, ac97_bus_t * _bus, ac97_bus_t ** rbus)
1709 static snd_device_ops_t ops = {
1710 .dev_free = snd_ac97_bus_dev_free,
1713 snd_assert(card != NULL, return -EINVAL);
1714 snd_assert(_bus != NULL && rbus != NULL, return -EINVAL);
1715 bus = snd_magic_kmalloc(ac97_bus_t, 0, GFP_KERNEL);
1720 if (bus->clock == 0)
1722 spin_lock_init(&bus->bus_lock);
1723 snd_ac97_bus_proc_init(bus);
1724 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, bus, &ops)) < 0) {
1725 snd_ac97_bus_free(bus);
1733 * snd_ac97_mixer - create an Codec97 component
1734 * @bus: the AC97 bus which codec is attached to
1735 * @_ac97: the template of ac97, including index, callbacks and
1737 * @rac97: the pointer to store the new ac97 instance.
1739 * Creates an Codec97 component. An ac97_t instance is newly
1740 * allocated and initialized from the template (_ac97). The codec
1741 * is then initialized by the standard procedure.
1743 * The template must include the valid callbacks (at least read and
1744 * write), the codec number (num) and address (addr), and the private
1745 * data (private_data). The other callbacks, wait and reset, are not
1748 * The ac97 instance is registered as a low-level device, so you don't
1749 * have to release it manually.
1751 * Returns zero if successful, or a negative error code on failure.
1753 int snd_ac97_mixer(ac97_bus_t * bus, ac97_t * _ac97, ac97_t ** rac97)
1759 unsigned long end_time;
1761 static snd_device_ops_t ops = {
1762 .dev_free = snd_ac97_dev_free,
1765 snd_assert(rac97 != NULL, return -EINVAL);
1767 snd_assert(bus != NULL && _ac97 != NULL, return -EINVAL);
1768 snd_assert(_ac97->num < 4 && bus->codec[_ac97->num] == NULL, return -EINVAL);
1770 ac97 = snd_magic_kmalloc(ac97_t, 0, GFP_KERNEL);
1775 bus->codec[ac97->num] = ac97;
1776 spin_lock_init(&ac97->reg_lock);
1779 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor);
1780 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device);
1787 snd_ac97_write(ac97, AC97_RESET, 0); /* reset to defaults */
1792 if (ac97_reset_wait(ac97, HZ/2, 0) < 0 &&
1793 ac97_reset_wait(ac97, HZ/2, 1) < 0) {
1794 snd_printk(KERN_WARNING "AC'97 %d does not respond - RESET\n", ac97->num);
1795 /* proceed anyway - it's often non-critical */
1799 ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
1800 ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
1801 if (ac97->id == 0x00000000 || ac97->id == 0xffffffff) {
1802 snd_printk(KERN_ERR "AC'97 %d access is not valid [0x%x], removing mixer.\n", ac97->num, ac97->id);
1803 snd_ac97_free(ac97);
1806 /* AC97 audio codec chip revision detection. */
1807 /* Currently only Realtek ALC650 detection implemented. */
1808 switch(ac97->id & 0xfffffff0) {
1809 case 0x414c4720: /* ALC650 */
1810 reg = snd_ac97_read(ac97, AC97_ALC650_REVISION);
1811 if (((reg & 0x3f) >= 0) && ((reg & 0x3f) < 3))
1812 ac97->id = 0x414c4720; /* Old version */
1813 else if (((reg & 0x3f) >= 3) && ((reg & 0x3f) < 0x10))
1814 ac97->id = 0x414c4721; /* D version */
1815 else if ((reg&0x30) == 0x10)
1816 ac97->id = 0x414c4722; /* E version */
1817 else if ((reg&0x30) == 0x20)
1818 ac97->id = 0x414c4723; /* F version */
1821 /* test for AC'97 */
1822 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) {
1823 /* test if we can write to the record gain volume register */
1824 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06);
1825 if ((err = snd_ac97_read(ac97, AC97_REC_GAIN)) == 0x8a06)
1826 ac97->scaps |= AC97_SCAP_AUDIO;
1828 if (ac97->scaps & AC97_SCAP_AUDIO) {
1829 ac97->caps = snd_ac97_read(ac97, AC97_RESET);
1830 ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
1831 if (ac97->ext_id == 0xffff) /* invalid combination */
1835 /* test for MC'97 */
1836 if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) {
1837 ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
1838 if (ac97->ext_mid == 0xffff) /* invalid combination */
1840 if (ac97->ext_mid & 1)
1841 ac97->scaps |= AC97_SCAP_MODEM;
1844 if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) {
1845 if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM)))
1846 snd_printk(KERN_ERR "AC'97 %d access error (not audio or modem codec)\n", ac97->num);
1847 snd_ac97_free(ac97);
1851 if (bus->reset) // FIXME: always skipping?
1854 /* FIXME: add powerdown control */
1855 if (ac97_is_audio(ac97)) {
1856 /* nothing should be in powerdown mode */
1857 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
1858 snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */
1860 /* nothing should be in powerdown mode */
1861 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
1862 snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0);
1863 end_time = jiffies + (HZ / 10);
1865 if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f)
1867 set_current_state(TASK_UNINTERRUPTIBLE);
1868 schedule_timeout(HZ/10);
1869 } while (time_after_eq(end_time, jiffies));
1870 snd_printk(KERN_ERR "AC'97 %d analog subsections not ready\n", ac97->num);
1873 /* FIXME: add powerdown control */
1874 if (ac97_is_modem(ac97)) {
1877 /* nothing should be in powerdown mode */
1878 /* note: it's important to set the rate at first */
1879 tmp = AC97_MEA_GPIO;
1880 if (ac97->ext_mid & AC97_MEI_LINE1) {
1881 snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 12000);
1882 tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1;
1884 if (ac97->ext_mid & AC97_MEI_LINE2) {
1885 snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 12000);
1886 tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2;
1888 if (ac97->ext_mid & AC97_MEI_HANDSET) {
1889 snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 12000);
1890 tmp |= AC97_MEA_HADC | AC97_MEA_HDAC;
1892 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0xff00 & ~(tmp << 8));
1894 /* nothing should be in powerdown mode */
1895 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0xff00 & ~(tmp << 8));
1896 end_time = jiffies + (HZ / 10);
1898 if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp)
1900 set_current_state(TASK_UNINTERRUPTIBLE);
1901 schedule_timeout(HZ/10);
1902 } while (time_after_eq(end_time, jiffies));
1903 snd_printk(KERN_ERR "MC'97 %d converters and GPIO not ready (0x%x)\n", ac97->num, snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS));
1907 if (ac97_is_audio(ac97))
1908 ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT;
1910 ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT;
1911 if (ac97->ext_id & 0x0189) /* L/R, MIC, SDAC, LDAC VRA support */
1912 snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, ac97->ext_id & 0x0189);
1913 if (ac97->ext_id & AC97_EI_VRA) { /* VRA support */
1914 snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]);
1915 snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]);
1917 ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000;
1918 ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000;
1920 if (ac97->ext_id & AC97_EI_SPDIF) {
1921 /* codec specific code (patch) should override these values */
1922 if (ac97->flags & AC97_CS_SPDIF)
1923 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100;
1924 else if (ac97->id == AC97_ID_CM9739)
1925 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000;
1927 ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
1929 if (ac97->ext_id & AC97_EI_VRM) { /* MIC VRA support */
1930 snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]);
1932 ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000;
1934 if (ac97->ext_id & AC97_EI_SDAC) { /* SDAC support */
1935 snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]);
1936 ac97->scaps |= AC97_SCAP_SURROUND_DAC;
1938 if (ac97->ext_id & AC97_EI_LDAC) { /* LDAC support */
1939 snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]);
1940 ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
1942 /* additional initializations */
1945 snd_ac97_get_name(ac97, ac97->id, name, 0);
1946 snd_ac97_get_name(NULL, ac97->id, name, 0); // ac97->id might be changed in the special setup code
1947 if (ac97_is_audio(ac97)) {
1948 if (card->mixername[0] == '\0') {
1949 strcpy(card->mixername, name);
1951 if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
1952 strcat(card->mixername, ",");
1953 strcat(card->mixername, name);
1956 if ((err = snd_component_add(card, "AC97a")) < 0) {
1957 snd_ac97_free(ac97);
1960 if (snd_ac97_mixer_build(ac97) < 0) {
1961 snd_ac97_free(ac97);
1965 if (ac97_is_modem(ac97)) {
1966 if (card->mixername[0] == '\0') {
1967 strcpy(card->mixername, name);
1969 if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
1970 strcat(card->mixername, ",");
1971 strcat(card->mixername, name);
1974 if ((err = snd_component_add(card, "AC97m")) < 0) {
1975 snd_ac97_free(ac97);
1978 if (snd_ac97_modem_build(card, ac97) < 0) {
1979 snd_ac97_free(ac97);
1983 /* make sure the proper powerdown bits are cleared */
1985 reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
1986 if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
1987 reg &= ~AC97_EA_PRJ;
1988 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
1989 reg &= ~(AC97_EA_PRI | AC97_EA_PRK);
1990 snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
1992 snd_ac97_proc_init(ac97);
1993 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ac97, &ops)) < 0) {
1994 snd_ac97_free(ac97);
2003 * Power down the chip.
2005 * MASTER and HEADPHONE registers are muted but the register cache values
2006 * are not changed, so that the values can be restored in snd_ac97_resume().
2008 static void snd_ac97_powerdown(ac97_t *ac97)
2010 unsigned short power;
2012 if (ac97_is_audio(ac97)) {
2013 /* some codecs have stereo mute bits */
2014 snd_ac97_write(ac97, AC97_MASTER, 0x9f9f);
2015 snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f);
2018 power = ac97->regs[AC97_POWERDOWN] | 0x8000; /* EAPD */
2019 power |= 0x4000; /* Headphone amplifier powerdown */
2020 power |= 0x0300; /* ADC & DAC powerdown */
2021 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2023 power |= 0x0400; /* Analog Mixer powerdown (Vref on) */
2024 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2027 /* FIXME: this causes click noises on some boards at resume */
2028 power |= 0x3800; /* AC-link powerdown, internal Clk disable */
2029 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2036 * snd_ac97_suspend - General suspend function for AC97 codec
2037 * @ac97: the ac97 instance
2039 * Suspends the codec, power down the chip.
2041 void snd_ac97_suspend(ac97_t *ac97)
2043 snd_ac97_powerdown(ac97);
2047 * snd_ac97_resume - General resume function for AC97 codec
2048 * @ac97: the ac97 instance
2050 * Do the standard resume procedure, power up and restoring the
2051 * old register values.
2053 void snd_ac97_resume(ac97_t *ac97)
2055 int i, is_ad18xx, codec;
2057 if (ac97->bus->reset) {
2058 ac97->bus->reset(ac97);
2062 snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2063 snd_ac97_write(ac97, AC97_RESET, 0);
2065 snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2066 snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0);
2068 snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]);
2069 ac97->bus->write(ac97, AC97_MASTER, 0x8101);
2070 for (i = 0; i < 10; i++) {
2071 if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101)
2073 set_current_state(TASK_UNINTERRUPTIBLE);
2074 schedule_timeout(1);
2076 /* FIXME: extra delay */
2077 ac97->bus->write(ac97, AC97_MASTER, 0x8000);
2078 if (snd_ac97_read(ac97, AC97_MASTER) != 0x8000) {
2079 set_current_state(TASK_UNINTERRUPTIBLE);
2080 schedule_timeout(HZ/4);
2084 if (ac97->bus->init)
2085 ac97->bus->init(ac97);
2087 is_ad18xx = (ac97->flags & AC97_AD_MULTI);
2089 /* restore the AD18xx codec configurations */
2090 for (codec = 0; codec < 3; codec++) {
2091 if (! ac97->spec.ad18xx.id[codec])
2093 /* select single codec */
2094 snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
2095 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
2096 ac97->bus->write(ac97, AC97_AD_CODEC_CFG, ac97->spec.ad18xx.codec_cfg[codec]);
2098 /* select all codecs */
2099 snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
2102 /* restore ac97 status */
2103 for (i = 2; i < 0x7c ; i += 2) {
2104 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
2106 /* restore only accessible registers
2107 * some chip (e.g. nm256) may hang up when unsupported registers
2110 if (test_bit(i, ac97->reg_accessed)) {
2112 /* handle multi codecs for AD18xx */
2113 if (i == AC97_PCM) {
2114 for (codec = 0; codec < 3; codec++) {
2115 if (! ac97->spec.ad18xx.id[codec])
2117 /* select single codec */
2118 snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
2119 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
2120 /* update PCM bits */
2121 ac97->bus->write(ac97, AC97_PCM, ac97->spec.ad18xx.pcmreg[codec]);
2123 /* select all codecs */
2124 snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
2126 } else if (i == AC97_AD_TEST ||
2127 i == AC97_AD_CODEC_CFG ||
2128 i == AC97_AD_SERIAL_CFG)
2129 continue; /* ignore */
2131 snd_ac97_write(ac97, i, ac97->regs[i]);
2132 snd_ac97_read(ac97, i);
2136 if (ac97->ext_id & AC97_EI_SPDIF) {
2137 if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) {
2138 /* reset spdif status */
2139 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
2140 snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]);
2141 if (ac97->flags & AC97_CS_SPDIF)
2142 snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]);
2144 snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]);
2145 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
2154 int snd_ac97_remove_ctl(ac97_t *ac97, const char *name)
2156 snd_ctl_elem_id_t id;
2157 memset(&id, 0, sizeof(id));
2158 strcpy(id.name, name);
2159 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2160 return snd_ctl_remove_id(ac97->bus->card, &id);
2163 static snd_kcontrol_t *ctl_find(ac97_t *ac97, const char *name)
2165 snd_ctl_elem_id_t sid;
2166 memset(&sid, 0, sizeof(sid));
2167 strcpy(sid.name, name);
2168 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2169 return snd_ctl_find_id(ac97->bus->card, &sid);
2172 int snd_ac97_rename_ctl(ac97_t *ac97, const char *src, const char *dst)
2174 snd_kcontrol_t *kctl = ctl_find(ac97, src);
2176 strcpy(kctl->id.name, dst);
2182 int snd_ac97_swap_ctl(ac97_t *ac97, const char *s1, const char *s2)
2184 snd_kcontrol_t *kctl1, *kctl2;
2185 kctl1 = ctl_find(ac97, s1);
2186 kctl2 = ctl_find(ac97, s2);
2187 if (kctl1 && kctl2) {
2188 strcpy(kctl1->id.name, s2);
2189 strcpy(kctl2->id.name, s1);
2195 static int swap_headphone(ac97_t *ac97, int remove_master)
2197 /* FIXME: error checks.. */
2198 if (remove_master) {
2199 if (ctl_find(ac97, "Headphone Playback Switch") == NULL)
2201 snd_ac97_remove_ctl(ac97, "Master Playback Switch");
2202 snd_ac97_remove_ctl(ac97, "Master Playback Volume");
2204 snd_ac97_rename_ctl(ac97, "Master Playback Switch", "Line-Out Playback Switch");
2205 snd_ac97_rename_ctl(ac97, "Master Playback Volume", "Line-Out Playback Volume");
2207 snd_ac97_rename_ctl(ac97, "Headphone Playback Switch", "Master Playback Switch");
2208 snd_ac97_rename_ctl(ac97, "Headphone Playback Volume", "Master Playback Volume");
2212 static int swap_surround(ac97_t *ac97)
2214 /* FIXME: error checks.. */
2215 snd_ac97_swap_ctl(ac97, "Master Playback Switch", "Surround Playback Switch");
2216 snd_ac97_swap_ctl(ac97, "Master Playback Volume", "Surround Playback Volume");
2220 static int tune_ad_sharing(ac97_t *ac97)
2222 unsigned short scfg;
2223 if ((ac97->id & 0xffffff00) != 0x41445300) {
2224 snd_printk(KERN_ERR "ac97_quirk AD_SHARING is only for AD codecs\n");
2227 /* Turn on OMS bit to route microphone to back panel */
2228 scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
2229 snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200);
2233 static const snd_kcontrol_new_t snd_ac97_alc_jack_detect =
2234 AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0);
2236 static int tune_alc_jack(ac97_t *ac97)
2238 if ((ac97->id & 0xffffff00) != 0x414c4700) {
2239 snd_printk(KERN_ERR "ac97_quirk ALC_JACK is only for Realtek codecs\n");
2242 snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */
2243 snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */
2244 return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97));
2247 static int apply_quirk(ac97_t *ac97, int quirk)
2250 case AC97_TUNE_NONE:
2252 case AC97_TUNE_HP_ONLY:
2253 return swap_headphone(ac97, 1);
2254 case AC97_TUNE_SWAP_HP:
2255 return swap_headphone(ac97, 0);
2256 case AC97_TUNE_SWAP_SURROUND:
2257 return swap_surround(ac97);
2258 case AC97_TUNE_AD_SHARING:
2259 return tune_ad_sharing(ac97);
2260 case AC97_TUNE_ALC_JACK:
2261 return tune_alc_jack(ac97);
2267 * snd_ac97_tune_hardware - tune up the hardware
2268 * @ac97: the ac97 instance
2269 * @quirk: quirk list
2270 * @override: explicit quirk value (overrides the list if not AC97_TUNE_DEFAULT)
2272 * Do some workaround for each pci device, such as renaming of the
2273 * headphone (true line-out) control as "Master".
2274 * The quirk-list must be terminated with a zero-filled entry.
2276 * Returns zero if successful, or a negative error code on failure.
2279 int snd_ac97_tune_hardware(ac97_t *ac97, struct ac97_quirk *quirk, int override)
2283 snd_assert(quirk, return -EINVAL);
2285 if (override != AC97_TUNE_DEFAULT) {
2286 result = apply_quirk(ac97, override);
2288 snd_printk(KERN_ERR "applying quirk type %d failed (%d)\n", override, result);
2292 for (; quirk->vendor; quirk++) {
2293 if (quirk->vendor != ac97->subsystem_vendor)
2295 if ((! quirk->mask && quirk->device == ac97->subsystem_device) ||
2296 quirk->device == (quirk->mask & ac97->subsystem_device)) {
2297 snd_printdd("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, ac97->subsystem_device);
2298 result = apply_quirk(ac97, quirk->type);
2300 snd_printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
2312 EXPORT_SYMBOL(snd_ac97_write);
2313 EXPORT_SYMBOL(snd_ac97_read);
2314 EXPORT_SYMBOL(snd_ac97_write_cache);
2315 EXPORT_SYMBOL(snd_ac97_update);
2316 EXPORT_SYMBOL(snd_ac97_update_bits);
2317 EXPORT_SYMBOL(snd_ac97_bus);
2318 EXPORT_SYMBOL(snd_ac97_mixer);
2319 EXPORT_SYMBOL(snd_ac97_pcm_assign);
2320 EXPORT_SYMBOL(snd_ac97_pcm_open);
2321 EXPORT_SYMBOL(snd_ac97_pcm_close);
2322 EXPORT_SYMBOL(snd_ac97_tune_hardware);
2323 EXPORT_SYMBOL(snd_ac97_set_rate);
2325 EXPORT_SYMBOL(snd_ac97_resume);
2326 EXPORT_SYMBOL(snd_ac97_suspend);
2333 static int __init alsa_ac97_init(void)
2338 static void __exit alsa_ac97_exit(void)
2342 module_init(alsa_ac97_init)
2343 module_exit(alsa_ac97_exit)