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");
57 int (*patch)(ac97_t *ac97);
58 int (*mpatch)(ac97_t *ac97);
61 static const ac97_codec_id_t snd_ac97_codec_id_vendors[] = {
62 { 0x414b4d00, 0xffffff00, "Asahi Kasei", NULL, NULL },
63 { 0x41445300, 0xffffff00, "Analog Devices", NULL, NULL },
64 { 0x414c4300, 0xffffff00, "Realtek", NULL, NULL },
65 { 0x414c4700, 0xffffff00, "Realtek", NULL, NULL },
66 { 0x434d4900, 0xffffff00, "C-Media Electronics", NULL, NULL },
67 { 0x43525900, 0xffffff00, "Cirrus Logic", NULL, NULL },
68 { 0x43585400, 0xffffff00, "Conexant", NULL, NULL },
69 { 0x44543000, 0xffffff00, "Diamond Technology", NULL, NULL },
70 { 0x454d4300, 0xffffff00, "eMicro", NULL, NULL },
71 { 0x45838300, 0xffffff00, "ESS Technology", NULL, NULL },
72 { 0x48525300, 0xffffff00, "Intersil", NULL, NULL },
73 { 0x49434500, 0xffffff00, "ICEnsemble", NULL, NULL },
74 { 0x49544500, 0xffffff00, "ITE Tech.Inc", NULL, NULL },
75 { 0x4e534300, 0xffffff00, "National Semiconductor", NULL, NULL },
76 { 0x50534300, 0xffffff00, "Philips", NULL, NULL },
77 { 0x53494c00, 0xffffff00, "Silicon Laboratory", NULL, NULL },
78 { 0x54524100, 0xffffff00, "TriTech", NULL, NULL },
79 { 0x54584e00, 0xffffff00, "Texas Instruments", NULL, NULL },
80 { 0x56494100, 0xffffff00, "VIA Technologies", NULL, NULL },
81 { 0x57454300, 0xffffff00, "Winbond", NULL, NULL },
82 { 0x574d4c00, 0xffffff00, "Wolfson", NULL, NULL },
83 { 0x594d4800, 0xffffff00, "Yamaha", NULL, NULL },
84 { 0x83847600, 0xffffff00, "SigmaTel", NULL, NULL },
85 { 0, 0, NULL, NULL, NULL }
88 static const ac97_codec_id_t snd_ac97_codec_ids[] = {
89 { 0x414b4d00, 0xffffffff, "AK4540", NULL, NULL },
90 { 0x414b4d01, 0xffffffff, "AK4542", NULL, NULL },
91 { 0x414b4d02, 0xffffffff, "AK4543", NULL, NULL },
92 { 0x414b4d06, 0xffffffff, "AK4544A", NULL, NULL },
93 { 0x414b4d07, 0xffffffff, "AK4545", NULL, NULL },
94 { 0x41445303, 0xffffffff, "AD1819", patch_ad1819, NULL },
95 { 0x41445340, 0xffffffff, "AD1881", patch_ad1881, NULL },
96 { 0x41445348, 0xffffffff, "AD1881A", patch_ad1881, NULL },
97 { 0x41445360, 0xffffffff, "AD1885", patch_ad1885, NULL },
98 { 0x41445361, 0xffffffff, "AD1886", patch_ad1886, NULL },
99 { 0x41445362, 0xffffffff, "AD1887", patch_ad1881, NULL },
100 { 0x41445363, 0xffffffff, "AD1886A", patch_ad1881, NULL },
101 { 0x41445368, 0xffffffff, "AD1888", patch_ad1888, NULL },
102 { 0x41445370, 0xffffffff, "AD1980", patch_ad1980, NULL },
103 { 0x41445372, 0xffffffff, "AD1981A", patch_ad1981a, NULL },
104 { 0x41445374, 0xffffffff, "AD1981B", patch_ad1981b, NULL },
105 { 0x41445375, 0xffffffff, "AD1985", patch_ad1985, NULL },
106 { 0x414c4300, 0xffffff00, "ALC100/100P", NULL, NULL },
107 { 0x414c4710, 0xfffffff0, "ALC200/200P", NULL, NULL },
108 { 0x414c4721, 0xffffffff, "ALC650D", NULL, NULL }, /* already patched */
109 { 0x414c4722, 0xffffffff, "ALC650E", NULL, NULL }, /* already patched */
110 { 0x414c4723, 0xffffffff, "ALC650F", NULL, NULL }, /* already patched */
111 { 0x414c4720, 0xfffffff0, "ALC650", patch_alc650, NULL },
112 { 0x414c4760, 0xfffffff0, "ALC655", patch_alc655, NULL },
113 { 0x414c4780, 0xfffffff0, "ALC658", patch_alc655, NULL },
114 { 0x414c4790, 0xfffffff0, "ALC850", patch_alc850, NULL },
115 { 0x414c4730, 0xffffffff, "ALC101", NULL, NULL },
116 { 0x414c4740, 0xfffffff0, "ALC202", NULL, NULL },
117 { 0x414c4750, 0xfffffff0, "ALC250", NULL, NULL },
118 { 0x414c4770, 0xfffffff0, "ALC203", NULL, NULL },
119 { 0x434d4941, 0xffffffff, "CMI9738", patch_cm9738, NULL },
120 { 0x434d4961, 0xffffffff, "CMI9739", patch_cm9739, NULL },
121 { 0x434d4978, 0xffffffff, "CMI9761", patch_cm9761, NULL },
122 { 0x434d4982, 0xffffffff, "CMI9761", patch_cm9761, NULL },
123 { 0x434d4983, 0xffffffff, "CMI9761", patch_cm9761, NULL },
124 { 0x43525900, 0xfffffff8, "CS4297", NULL, NULL },
125 { 0x43525910, 0xfffffff8, "CS4297A", patch_cirrus_spdif, NULL },
126 { 0x43525920, 0xfffffff8, "CS4298", patch_cirrus_spdif, NULL },
127 { 0x43525928, 0xfffffff8, "CS4294", NULL, NULL },
128 { 0x43525930, 0xfffffff8, "CS4299", patch_cirrus_cs4299, NULL },
129 { 0x43525948, 0xfffffff8, "CS4201", NULL, NULL },
130 { 0x43525958, 0xfffffff8, "CS4205", patch_cirrus_spdif, NULL },
131 { 0x43525960, 0xfffffff8, "CS4291", NULL, NULL },
132 { 0x43525970, 0xfffffff8, "CS4202", NULL, NULL },
133 { 0x43585421, 0xffffffff, "HSD11246", NULL, NULL }, // SmartMC II
134 { 0x43585428, 0xfffffff8, "Cx20468", patch_conexant, NULL }, // SmartAMC fixme: the mask might be different
135 { 0x44543031, 0xfffffff0, "DT0398", NULL, NULL },
136 { 0x454d4328, 0xffffffff, "28028", NULL, NULL }, // same as TR28028?
137 { 0x45838308, 0xffffffff, "ESS1988", NULL, NULL },
138 { 0x48525300, 0xffffff00, "HMP9701", NULL, NULL },
139 { 0x49434501, 0xffffffff, "ICE1230", NULL, NULL },
140 { 0x49434511, 0xffffffff, "ICE1232", NULL, NULL }, // alias VIA VT1611A?
141 { 0x49434514, 0xffffffff, "ICE1232A", NULL, NULL },
142 { 0x49434551, 0xffffffff, "VT1616", patch_vt1616, NULL },
143 { 0x49434552, 0xffffffff, "VT1616i", patch_vt1616, NULL }, // VT1616 compatible (chipset integrated)
144 { 0x49544520, 0xffffffff, "IT2226E", NULL, NULL },
145 { 0x49544561, 0xffffffff, "IT2646E", patch_it2646, NULL },
146 { 0x4e534300, 0xffffffff, "LM4540/43/45/46/48", NULL, NULL }, // only guess --jk
147 { 0x4e534331, 0xffffffff, "LM4549", NULL, NULL },
148 { 0x4e534350, 0xffffffff, "LM4550", NULL, NULL },
149 { 0x50534304, 0xffffffff, "UCB1400", NULL, NULL },
150 { 0x53494c20, 0xffffffe0, "Si3036/8", NULL, mpatch_si3036 },
151 { 0x54524102, 0xffffffff, "TR28022", NULL, NULL },
152 { 0x54524106, 0xffffffff, "TR28026", NULL, NULL },
153 { 0x54524108, 0xffffffff, "TR28028", patch_tritech_tr28028, NULL }, // added by xin jin [07/09/99]
154 { 0x54524123, 0xffffffff, "TR28602", NULL, NULL }, // only guess --jk [TR28023 = eMicro EM28023 (new CT1297)]
155 { 0x54584e20, 0xffffffff, "TLC320AD9xC", NULL, NULL },
156 { 0x56494161, 0xffffffff, "VIA1612A", NULL, NULL }, // modified ICE1232 with S/PDIF
157 { 0x57454301, 0xffffffff, "W83971D", NULL, NULL },
158 { 0x574d4c00, 0xffffffff, "WM9701A", NULL, NULL },
159 { 0x574d4C03, 0xffffffff, "WM9703/WM9707/WM9708/WM9717", patch_wolfson03, NULL},
160 { 0x574d4C04, 0xffffffff, "WM9704M/WM9704Q", patch_wolfson04, NULL},
161 { 0x574d4C05, 0xffffffff, "WM9705/WM9710", patch_wolfson05, NULL},
162 { 0x574d4C09, 0xffffffff, "WM9709", NULL, NULL},
163 { 0x574d4C12, 0xffffffff, "WM9711/WM9712", patch_wolfson11, NULL},
164 { 0x594d4800, 0xffffffff, "YMF743", NULL, NULL },
165 { 0x594d4802, 0xffffffff, "YMF752", NULL, NULL },
166 { 0x594d4803, 0xffffffff, "YMF753", patch_yamaha_ymf753, NULL },
167 { 0x83847600, 0xffffffff, "STAC9700/83/84", patch_sigmatel_stac9700, NULL },
168 { 0x83847604, 0xffffffff, "STAC9701/3/4/5", NULL, NULL },
169 { 0x83847605, 0xffffffff, "STAC9704", NULL, NULL },
170 { 0x83847608, 0xffffffff, "STAC9708/11", patch_sigmatel_stac9708, NULL },
171 { 0x83847609, 0xffffffff, "STAC9721/23", patch_sigmatel_stac9721, NULL },
172 { 0x83847644, 0xffffffff, "STAC9744", patch_sigmatel_stac9744, NULL },
173 { 0x83847650, 0xffffffff, "STAC9750/51", NULL, NULL }, // patch?
174 { 0x83847652, 0xffffffff, "STAC9752/53", NULL, NULL }, // patch?
175 { 0x83847656, 0xffffffff, "STAC9756/57", patch_sigmatel_stac9756, NULL },
176 { 0x83847658, 0xffffffff, "STAC9758/59", patch_sigmatel_stac9758, NULL },
177 { 0x83847666, 0xffffffff, "STAC9766/67", NULL, NULL }, // patch?
178 { 0, 0, NULL, NULL, NULL }
181 const char *snd_ac97_stereo_enhancements[] =
183 /* 0 */ "No 3D Stereo Enhancement",
184 /* 1 */ "Analog Devices Phat Stereo",
185 /* 2 */ "Creative Stereo Enhancement",
186 /* 3 */ "National Semi 3D Stereo Enhancement",
187 /* 4 */ "YAMAHA Ymersion",
188 /* 5 */ "BBE 3D Stereo Enhancement",
189 /* 6 */ "Crystal Semi 3D Stereo Enhancement",
190 /* 7 */ "Qsound QXpander",
191 /* 8 */ "Spatializer 3D Stereo Enhancement",
192 /* 9 */ "SRS 3D Stereo Enhancement",
193 /* 10 */ "Platform Tech 3D Stereo Enhancement",
194 /* 11 */ "AKM 3D Audio",
195 /* 12 */ "Aureal Stereo Enhancement",
196 /* 13 */ "Aztech 3D Enhancement",
197 /* 14 */ "Binaura 3D Audio Enhancement",
198 /* 15 */ "ESS Technology Stereo Enhancement",
199 /* 16 */ "Harman International VMAx",
200 /* 17 */ "Nvidea/IC Ensemble/KS Waves 3D Stereo Enhancement",
201 /* 18 */ "Philips Incredible Sound",
202 /* 19 */ "Texas Instruments 3D Stereo Enhancement",
203 /* 20 */ "VLSI Technology 3D Stereo Enhancement",
204 /* 21 */ "TriTech 3D Stereo Enhancement",
205 /* 22 */ "Realtek 3D Stereo Enhancement",
206 /* 23 */ "Samsung 3D Stereo Enhancement",
207 /* 24 */ "Wolfson Microelectronics 3D Enhancement",
208 /* 25 */ "Delta Integration 3D Enhancement",
209 /* 26 */ "SigmaTel 3D Enhancement",
210 /* 27 */ "IC Ensemble/KS Waves",
211 /* 28 */ "Rockwell 3D Stereo Enhancement",
212 /* 29 */ "Reserved 29",
213 /* 30 */ "Reserved 30",
214 /* 31 */ "Reserved 31"
218 * Shared AC97 controllers (ICH, ATIIXP...)
220 static DECLARE_MUTEX(shared_codec_mutex);
221 static ac97_t *shared_codec[AC97_SHARED_TYPES][4];
228 static int snd_ac97_valid_reg(ac97_t *ac97, unsigned short reg)
230 if (ac97->limited_regs && ! test_bit(reg, ac97->reg_accessed))
233 /* filter some registers for buggy codecs */
237 if (reg <= 0x1c || reg == 0x20 || reg == 0x26 || reg >= 0x7c)
240 case AC97_ID_AD1819: /* AD1819 */
241 case AC97_ID_AD1881: /* AD1881 */
242 case AC97_ID_AD1881A: /* AD1881A */
243 if (reg >= 0x3a && reg <= 0x6e) /* 0x59 */
246 case AC97_ID_AD1885: /* AD1885 */
247 case AC97_ID_AD1886: /* AD1886 */
248 case AC97_ID_AD1886A: /* AD1886A - !!verify!! --jk */
249 case AC97_ID_AD1887: /* AD1887 - !!verify!! --jk */
252 if (reg >= 0x3c && reg <= 0x6e) /* 0x59 */
255 case AC97_ID_STAC9700:
256 case AC97_ID_STAC9704:
257 case AC97_ID_STAC9705:
258 case AC97_ID_STAC9708:
259 case AC97_ID_STAC9721:
260 case AC97_ID_STAC9744:
261 case AC97_ID_STAC9756:
262 if (reg <= 0x3a || reg >= 0x5a)
270 * snd_ac97_write - write a value on the given register
271 * @ac97: the ac97 instance
272 * @reg: the register to change
273 * @value: the value to set
275 * Writes a value on the given register. This will invoke the write
276 * callback directly after the register check.
277 * This function doesn't change the register cache unlike
278 * #snd_ca97_write_cache(), so use this only when you don't want to
279 * reflect the change to the suspend/resume state.
281 void snd_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short value)
283 if (!snd_ac97_valid_reg(ac97, reg))
285 if ((ac97->id & 0xffffff00) == AC97_ID_ALC100) {
286 /* Fix H/W bug of ALC100/100P */
287 if (reg == AC97_MASTER || reg == AC97_HEADPHONE)
288 ac97->bus->ops->write(ac97, AC97_RESET, 0); /* reset audio codec */
290 ac97->bus->ops->write(ac97, reg, value);
294 * snd_ac97_read - read a value from the given register
296 * @ac97: the ac97 instance
297 * @reg: the register to read
299 * Reads a value from the given register. This will invoke the read
300 * callback directly after the register check.
302 * Returns the read value.
304 unsigned short snd_ac97_read(ac97_t *ac97, unsigned short reg)
306 if (!snd_ac97_valid_reg(ac97, reg))
308 return ac97->bus->ops->read(ac97, reg);
311 /* read a register - return the cached value if already read */
312 static inline unsigned short snd_ac97_read_cache(ac97_t *ac97, unsigned short reg)
314 if (! test_bit(reg, ac97->reg_accessed)) {
315 ac97->regs[reg] = ac97->bus->ops->read(ac97, reg);
316 // set_bit(reg, ac97->reg_accessed);
318 return ac97->regs[reg];
322 * snd_ac97_write_cache - write a value on the given register and update the cache
323 * @ac97: the ac97 instance
324 * @reg: the register to change
325 * @value: the value to set
327 * Writes a value on the given register and updates the register
328 * cache. The cached values are used for the cached-read and the
331 void snd_ac97_write_cache(ac97_t *ac97, unsigned short reg, unsigned short value)
333 if (!snd_ac97_valid_reg(ac97, reg))
335 down(&ac97->reg_mutex);
336 ac97->regs[reg] = value;
337 ac97->bus->ops->write(ac97, reg, value);
338 set_bit(reg, ac97->reg_accessed);
339 up(&ac97->reg_mutex);
343 * snd_ac97_update - update the value on the given register
344 * @ac97: the ac97 instance
345 * @reg: the register to change
346 * @value: the value to set
348 * Compares the value with the register cache and updates the value
349 * only when the value is changed.
351 * Returns 1 if the value is changed, 0 if no change, or a negative
354 int snd_ac97_update(ac97_t *ac97, unsigned short reg, unsigned short value)
358 if (!snd_ac97_valid_reg(ac97, reg))
360 down(&ac97->reg_mutex);
361 change = ac97->regs[reg] != value;
363 ac97->regs[reg] = value;
364 ac97->bus->ops->write(ac97, reg, value);
366 up(&ac97->reg_mutex);
371 * snd_ac97_update_bits - update the bits on the given register
372 * @ac97: the ac97 instance
373 * @reg: the register to change
374 * @mask: the bit-mask to change
375 * @value: the value to set
377 * Updates the masked-bits on the given register only when the value
380 * Returns 1 if the bits are changed, 0 if no change, or a negative
383 int snd_ac97_update_bits(ac97_t *ac97, unsigned short reg, unsigned short mask, unsigned short value)
387 if (!snd_ac97_valid_reg(ac97, reg))
389 down(&ac97->reg_mutex);
390 change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
391 up(&ac97->reg_mutex);
395 /* no lock version - see snd_ac97_updat_bits() */
396 int snd_ac97_update_bits_nolock(ac97_t *ac97, unsigned short reg,
397 unsigned short mask, unsigned short value)
400 unsigned short old, new;
402 old = snd_ac97_read_cache(ac97, reg);
403 new = (old & ~mask) | value;
406 ac97->regs[reg] = new;
407 ac97->bus->ops->write(ac97, reg, new);
412 static int snd_ac97_ad18xx_update_pcm_bits(ac97_t *ac97, int codec, unsigned short mask, unsigned short value)
415 unsigned short old, new, cfg;
417 down(&ac97->page_mutex);
418 old = ac97->spec.ad18xx.pcmreg[codec];
419 new = (old & ~mask) | value;
422 down(&ac97->reg_mutex);
423 cfg = snd_ac97_read_cache(ac97, AC97_AD_SERIAL_CFG);
424 ac97->spec.ad18xx.pcmreg[codec] = new;
425 /* select single codec */
426 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
428 ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
429 /* update PCM bits */
430 ac97->bus->ops->write(ac97, AC97_PCM, new);
431 /* select all codecs */
432 ac97->bus->ops->write(ac97, AC97_AD_SERIAL_CFG,
434 up(&ac97->reg_mutex);
436 up(&ac97->page_mutex);
444 static int snd_ac97_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
446 static char *texts[8] = {
447 "Mic", "CD", "Video", "Aux", "Line",
448 "Mix", "Mix Mono", "Phone"
451 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
453 uinfo->value.enumerated.items = 8;
454 if (uinfo->value.enumerated.item > 7)
455 uinfo->value.enumerated.item = 7;
456 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
460 static int snd_ac97_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
462 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
465 val = snd_ac97_read_cache(ac97, AC97_REC_SEL);
466 ucontrol->value.enumerated.item[0] = (val >> 8) & 7;
467 ucontrol->value.enumerated.item[1] = (val >> 0) & 7;
471 static int snd_ac97_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
473 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
476 if (ucontrol->value.enumerated.item[0] > 7 ||
477 ucontrol->value.enumerated.item[1] > 7)
479 val = (ucontrol->value.enumerated.item[0] << 8) |
480 (ucontrol->value.enumerated.item[1] << 0);
481 return snd_ac97_update(ac97, AC97_REC_SEL, val);
484 #define AC97_ENUM_DOUBLE(xname, reg, shift, invert) \
485 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_info_enum_double, \
486 .get = snd_ac97_get_enum_double, .put = snd_ac97_put_enum_double, \
487 .private_value = reg | (shift << 8) | (invert << 24) }
489 static int snd_ac97_info_enum_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
491 static char *texts1[2] = { "pre 3D", "post 3D" };
492 static char *texts2[2] = { "Mix", "Mic" };
493 static char *texts3[2] = { "Mic1", "Mic2" };
495 int reg = kcontrol->private_value & 0xff;
496 int shift = (kcontrol->private_value >> 8) & 0xff;
499 case AC97_GENERAL_PURPOSE:
501 case 15: texts = texts1; break;
502 case 9: texts = texts2; break;
503 case 8: texts = texts3; break;
506 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
508 uinfo->value.enumerated.items = 2;
509 if (uinfo->value.enumerated.item > 1)
510 uinfo->value.enumerated.item = 1;
511 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
515 static int snd_ac97_get_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 val = (snd_ac97_read_cache(ac97, reg) >> shift) & 1;
526 ucontrol->value.enumerated.item[0] = val;
530 static int snd_ac97_put_enum_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
532 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
534 int reg = kcontrol->private_value & 0xff;
535 int shift = (kcontrol->private_value >> 8) & 0xff;
536 int invert = (kcontrol->private_value >> 24) & 0xff;
538 if (ucontrol->value.enumerated.item[0] > 1)
540 val = !!ucontrol->value.enumerated.item[0];
543 return snd_ac97_update_bits(ac97, reg, 1 << shift, val << shift);
546 int snd_ac97_info_volsw(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
548 int mask = (kcontrol->private_value >> 16) & 0xff;
549 int shift = (kcontrol->private_value >> 8) & 0x0f;
550 int rshift = (kcontrol->private_value >> 12) & 0x0f;
552 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
553 uinfo->count = shift == rshift ? 1 : 2;
554 uinfo->value.integer.min = 0;
555 uinfo->value.integer.max = mask;
559 int snd_ac97_get_volsw(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
561 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
562 int reg = kcontrol->private_value & 0xff;
563 int shift = (kcontrol->private_value >> 8) & 0x0f;
564 int rshift = (kcontrol->private_value >> 12) & 0x0f;
565 int mask = (kcontrol->private_value >> 16) & 0xff;
566 int invert = (kcontrol->private_value >> 24) & 0x01;
568 ucontrol->value.integer.value[0] = (snd_ac97_read_cache(ac97, reg) >> shift) & mask;
570 ucontrol->value.integer.value[1] = (snd_ac97_read_cache(ac97, reg) >> rshift) & mask;
572 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
574 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
579 int snd_ac97_put_volsw(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
581 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
582 int reg = kcontrol->private_value & 0xff;
583 int shift = (kcontrol->private_value >> 8) & 0x0f;
584 int rshift = (kcontrol->private_value >> 12) & 0x0f;
585 int mask = (kcontrol->private_value >> 16) & 0xff;
586 int invert = (kcontrol->private_value >> 24) & 0x01;
587 unsigned short val, val2, val_mask;
589 val = (ucontrol->value.integer.value[0] & mask);
592 val_mask = mask << shift;
594 if (shift != rshift) {
595 val2 = (ucontrol->value.integer.value[1] & mask);
598 val_mask |= mask << rshift;
599 val |= val2 << rshift;
601 return snd_ac97_update_bits(ac97, reg, val_mask, val);
604 #define AC97_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
605 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), .info = snd_ac97_info_volsw, \
606 .get = snd_ac97_get_volsw, .put = snd_ac97_put_volsw, \
607 .private_value = (reg) | ((shift_left) << 8) | ((shift_right) << 12) | ((mask) << 16) | ((invert) << 24) }
609 static int snd_ac97_getput_page(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol,
610 int (*func)(snd_kcontrol_t *, snd_ctl_elem_value_t *))
612 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
613 int reg = kcontrol->private_value & 0xff;
616 if ((ac97->ext_id & AC97_EI_REV_MASK) >= AC97_EI_REV_23 &&
617 (reg >= 0x60 && reg < 0x70)) {
618 unsigned short page_save;
619 unsigned short page = (kcontrol->private_value >> 25) & 0x0f;
620 down(&ac97->page_mutex); /* lock paging */
621 page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
622 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
623 err = func(kcontrol, ucontrol);
624 snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
625 up(&ac97->page_mutex); /* unlock paging */
627 err = func(kcontrol, ucontrol);
631 /* for rev2.3 paging */
632 int snd_ac97_page_get_volsw(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
634 return snd_ac97_getput_page(kcontrol, ucontrol, snd_ac97_get_volsw);
637 /* for rev2.3 paging */
638 int snd_ac97_page_put_volsw(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
640 return snd_ac97_getput_page(kcontrol, ucontrol, snd_ac97_put_volsw);
643 static const snd_kcontrol_new_t snd_ac97_controls_master_mono[2] = {
644 AC97_SINGLE("Master Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
645 AC97_SINGLE("Master Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1)
648 static const snd_kcontrol_new_t snd_ac97_controls_tone[2] = {
649 AC97_SINGLE("Tone Control - Bass", AC97_MASTER_TONE, 8, 15, 1),
650 AC97_SINGLE("Tone Control - Treble", AC97_MASTER_TONE, 0, 15, 1)
653 static const snd_kcontrol_new_t snd_ac97_controls_pc_beep[2] = {
654 AC97_SINGLE("PC Speaker Playback Switch", AC97_PC_BEEP, 15, 1, 1),
655 AC97_SINGLE("PC Speaker Playback Volume", AC97_PC_BEEP, 1, 15, 1)
658 static const snd_kcontrol_new_t snd_ac97_controls_mic_boost =
659 AC97_SINGLE("Mic Boost (+20dB)", AC97_MIC, 6, 1, 0);
662 static const snd_kcontrol_new_t snd_ac97_control_capture_src = {
663 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
664 .name = "Capture Source",
665 .info = snd_ac97_info_mux,
666 .get = snd_ac97_get_mux,
667 .put = snd_ac97_put_mux,
670 static const snd_kcontrol_new_t snd_ac97_control_capture_vol =
671 AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0);
673 static const snd_kcontrol_new_t snd_ac97_controls_mic_capture[2] = {
674 AC97_SINGLE("Mic Capture Switch", AC97_REC_GAIN_MIC, 15, 1, 1),
675 AC97_SINGLE("Mic Capture Volume", AC97_REC_GAIN_MIC, 0, 15, 0)
679 AC97_GENERAL_PCM_OUT = 0,
680 AC97_GENERAL_STEREO_ENHANCEMENT,
682 AC97_GENERAL_LOUDNESS,
685 AC97_GENERAL_LOOPBACK
686 } ac97_general_index_t;
688 static const snd_kcontrol_new_t snd_ac97_controls_general[7] = {
689 AC97_ENUM_DOUBLE("PCM Out Path & Mute", AC97_GENERAL_PURPOSE, 15, 0),
690 AC97_SINGLE("Simulated Stereo Enhancement", AC97_GENERAL_PURPOSE, 14, 1, 0),
691 AC97_SINGLE("3D Control - Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
692 AC97_SINGLE("Loudness (bass boost)", AC97_GENERAL_PURPOSE, 12, 1, 0),
693 AC97_ENUM_DOUBLE("Mono Output Select", AC97_GENERAL_PURPOSE, 9, 0),
694 AC97_ENUM_DOUBLE("Mic Select", AC97_GENERAL_PURPOSE, 8, 0),
695 AC97_SINGLE("ADC/DAC Loopback", AC97_GENERAL_PURPOSE, 7, 1, 0)
698 const snd_kcontrol_new_t snd_ac97_controls_3d[2] = {
699 AC97_SINGLE("3D Control - Center", AC97_3D_CONTROL, 8, 15, 0),
700 AC97_SINGLE("3D Control - Depth", AC97_3D_CONTROL, 0, 15, 0)
703 static const snd_kcontrol_new_t snd_ac97_controls_center[2] = {
704 AC97_SINGLE("Center Playback Switch", AC97_CENTER_LFE_MASTER, 7, 1, 1),
705 AC97_SINGLE("Center Playback Volume", AC97_CENTER_LFE_MASTER, 0, 31, 1)
708 static const snd_kcontrol_new_t snd_ac97_controls_lfe[2] = {
709 AC97_SINGLE("LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 1, 1),
710 AC97_SINGLE("LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 31, 1)
713 static const snd_kcontrol_new_t snd_ac97_controls_surround[2] = {
714 AC97_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
715 AC97_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
718 static const snd_kcontrol_new_t snd_ac97_control_eapd =
719 AC97_SINGLE("External Amplifier", AC97_POWERDOWN, 15, 1, 1);
721 /* change the existing EAPD control as inverted */
722 static void set_inv_eapd(ac97_t *ac97, snd_kcontrol_t *kctl)
724 kctl->private_value = AC97_SINGLE_VALUE(AC97_POWERDOWN, 15, 1, 0);
725 snd_ac97_update_bits(ac97, AC97_POWERDOWN, (1<<15), (1<<15)); /* EAPD up */
726 ac97->scaps |= AC97_SCAP_INV_EAPD;
729 static int snd_ac97_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
731 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
736 static int snd_ac97_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
738 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
739 IEC958_AES0_NONAUDIO |
740 IEC958_AES0_CON_EMPHASIS_5015 |
741 IEC958_AES0_CON_NOT_COPYRIGHT;
742 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
743 IEC958_AES1_CON_ORIGINAL;
744 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
748 static int snd_ac97_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
750 /* FIXME: AC'97 spec doesn't say which bits are used for what */
751 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
752 IEC958_AES0_NONAUDIO |
754 IEC958_AES0_PRO_EMPHASIS_5015;
758 static int snd_ac97_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
760 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
762 down(&ac97->reg_mutex);
763 ucontrol->value.iec958.status[0] = ac97->spdif_status & 0xff;
764 ucontrol->value.iec958.status[1] = (ac97->spdif_status >> 8) & 0xff;
765 ucontrol->value.iec958.status[2] = (ac97->spdif_status >> 16) & 0xff;
766 ucontrol->value.iec958.status[3] = (ac97->spdif_status >> 24) & 0xff;
767 up(&ac97->reg_mutex);
771 static int snd_ac97_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
773 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
774 unsigned int new = 0;
775 unsigned short val = 0;
778 new = val = ucontrol->value.iec958.status[0] & (IEC958_AES0_PROFESSIONAL|IEC958_AES0_NONAUDIO);
779 if (ucontrol->value.iec958.status[0] & IEC958_AES0_PROFESSIONAL) {
780 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_PRO_FS|IEC958_AES0_PRO_EMPHASIS_5015);
781 switch (new & IEC958_AES0_PRO_FS) {
782 case IEC958_AES0_PRO_FS_44100: val |= 0<<12; break;
783 case IEC958_AES0_PRO_FS_48000: val |= 2<<12; break;
784 case IEC958_AES0_PRO_FS_32000: val |= 3<<12; break;
785 default: val |= 1<<12; break;
787 if ((new & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
790 new |= ucontrol->value.iec958.status[0] & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT);
791 new |= ((ucontrol->value.iec958.status[1] & (IEC958_AES1_CON_CATEGORY|IEC958_AES1_CON_ORIGINAL)) << 8);
792 new |= ((ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) << 24);
793 if ((new & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
795 if (!(new & IEC958_AES0_CON_NOT_COPYRIGHT))
797 val |= ((new >> 8) & 0xff) << 4; // category + original
798 switch ((new >> 24) & 0xff) {
799 case IEC958_AES3_CON_FS_44100: val |= 0<<12; break;
800 case IEC958_AES3_CON_FS_48000: val |= 2<<12; break;
801 case IEC958_AES3_CON_FS_32000: val |= 3<<12; break;
802 default: val |= 1<<12; break;
806 down(&ac97->reg_mutex);
807 change = ac97->spdif_status != new;
808 ac97->spdif_status = new;
810 if (ac97->flags & AC97_CS_SPDIF) {
811 int x = (val >> 12) & 0x03;
813 case 0: x = 1; break; // 44.1
814 case 2: x = 0; break; // 48.0
815 default: x = 0; break; // illegal.
817 change |= snd_ac97_update_bits_nolock(ac97, AC97_CSR_SPDIF, 0x3fff, ((val & 0xcfff) | (x << 12)));
818 } else if (ac97->flags & AC97_CX_SPDIF) {
820 v = new & (IEC958_AES0_CON_EMPHASIS_5015|IEC958_AES0_CON_NOT_COPYRIGHT) ? 0 : AC97_CXR_COPYRGT;
821 v |= new & IEC958_AES0_NONAUDIO ? AC97_CXR_SPDIF_AC3 : AC97_CXR_SPDIF_PCM;
822 change |= snd_ac97_update_bits_nolock(ac97, AC97_CXR_AUDIO_MISC,
823 AC97_CXR_SPDIF_MASK | AC97_CXR_COPYRGT,
826 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
827 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
829 change |= snd_ac97_update_bits_nolock(ac97, AC97_SPDIF, 0x3fff, val);
830 if (extst & AC97_EA_SPDIF) {
831 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
834 up(&ac97->reg_mutex);
839 static int snd_ac97_put_spsa(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
841 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
842 int reg = kcontrol->private_value & 0xff;
843 int shift = (kcontrol->private_value >> 8) & 0xff;
844 int mask = (kcontrol->private_value >> 16) & 0xff;
845 // int invert = (kcontrol->private_value >> 24) & 0xff;
846 unsigned short value, old, new;
849 value = (ucontrol->value.integer.value[0] & mask);
851 down(&ac97->reg_mutex);
854 old = snd_ac97_read_cache(ac97, reg);
855 new = (old & ~mask) | value;
859 unsigned short extst = snd_ac97_read_cache(ac97, AC97_EXTENDED_STATUS);
860 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0); /* turn off */
861 change = snd_ac97_update_bits_nolock(ac97, reg, mask, value);
862 if (extst & AC97_EA_SPDIF)
863 snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
865 up(&ac97->reg_mutex);
869 const snd_kcontrol_new_t snd_ac97_controls_spdif[5] = {
871 .access = SNDRV_CTL_ELEM_ACCESS_READ,
872 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
873 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
874 .info = snd_ac97_spdif_mask_info,
875 .get = snd_ac97_spdif_cmask_get,
878 .access = SNDRV_CTL_ELEM_ACCESS_READ,
879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
880 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
881 .info = snd_ac97_spdif_mask_info,
882 .get = snd_ac97_spdif_pmask_get,
885 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
886 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
887 .info = snd_ac97_spdif_mask_info,
888 .get = snd_ac97_spdif_default_get,
889 .put = snd_ac97_spdif_default_put,
892 AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),AC97_EXTENDED_STATUS, 2, 1, 0),
894 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
895 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA",
896 .info = snd_ac97_info_volsw,
897 .get = snd_ac97_get_volsw,
898 .put = snd_ac97_put_spsa,
899 .private_value = AC97_SINGLE_VALUE(AC97_EXTENDED_STATUS, 4, 3, 0)
903 #define AD18XX_PCM_BITS(xname, codec, lshift, rshift, mask) \
904 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_bits, \
905 .get = snd_ac97_ad18xx_pcm_get_bits, .put = snd_ac97_ad18xx_pcm_put_bits, \
906 .private_value = (codec) | ((lshift) << 8) | ((rshift) << 12) | ((mask) << 16) }
908 static int snd_ac97_ad18xx_pcm_info_bits(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
910 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
911 int mask = (kcontrol->private_value >> 16) & 0x0f;
912 int lshift = (kcontrol->private_value >> 8) & 0x0f;
913 int rshift = (kcontrol->private_value >> 12) & 0x0f;
915 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
916 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
920 uinfo->value.integer.min = 0;
921 uinfo->value.integer.max = mask;
925 static int snd_ac97_ad18xx_pcm_get_bits(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
927 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
928 int codec = kcontrol->private_value & 3;
929 int lshift = (kcontrol->private_value >> 8) & 0x0f;
930 int rshift = (kcontrol->private_value >> 12) & 0x0f;
931 int mask = (kcontrol->private_value >> 16) & 0xff;
933 ucontrol->value.integer.value[0] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> lshift) & mask);
934 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES))
935 ucontrol->value.integer.value[1] = mask - ((ac97->spec.ad18xx.pcmreg[codec] >> rshift) & mask);
939 static int snd_ac97_ad18xx_pcm_put_bits(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
941 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
942 int codec = kcontrol->private_value & 3;
943 int lshift = (kcontrol->private_value >> 8) & 0x0f;
944 int rshift = (kcontrol->private_value >> 12) & 0x0f;
945 int mask = (kcontrol->private_value >> 16) & 0xff;
946 unsigned short val, valmask;
948 val = (mask - (ucontrol->value.integer.value[0] & mask)) << lshift;
949 valmask = mask << lshift;
950 if (lshift != rshift && (ac97->flags & AC97_STEREO_MUTES)) {
951 val |= (mask - (ucontrol->value.integer.value[1] & mask)) << rshift;
952 valmask |= mask << rshift;
954 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, valmask, val);
957 #define AD18XX_PCM_VOLUME(xname, codec) \
958 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ac97_ad18xx_pcm_info_volume, \
959 .get = snd_ac97_ad18xx_pcm_get_volume, .put = snd_ac97_ad18xx_pcm_put_volume, \
960 .private_value = codec }
962 static int snd_ac97_ad18xx_pcm_info_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
964 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
966 uinfo->value.integer.min = 0;
967 uinfo->value.integer.max = 31;
971 static int snd_ac97_ad18xx_pcm_get_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
973 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
974 int codec = kcontrol->private_value & 3;
976 down(&ac97->page_mutex);
977 ucontrol->value.integer.value[0] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 0) & 31);
978 ucontrol->value.integer.value[1] = 31 - ((ac97->spec.ad18xx.pcmreg[codec] >> 8) & 31);
979 up(&ac97->page_mutex);
983 static int snd_ac97_ad18xx_pcm_put_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
985 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
986 int codec = kcontrol->private_value & 3;
987 unsigned short val1, val2;
989 val1 = 31 - (ucontrol->value.integer.value[0] & 31);
990 val2 = 31 - (ucontrol->value.integer.value[1] & 31);
991 return snd_ac97_ad18xx_update_pcm_bits(ac97, codec, 0x1f1f, (val1 << 8) | val2);
994 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_pcm[2] = {
995 AD18XX_PCM_BITS("PCM Playback Switch", 0, 15, 7, 1),
996 AD18XX_PCM_VOLUME("PCM Playback Volume", 0)
999 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_surround[2] = {
1000 AD18XX_PCM_BITS("Surround Playback Switch", 1, 15, 7, 1),
1001 AD18XX_PCM_VOLUME("Surround Playback Volume", 1)
1004 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_center[2] = {
1005 AD18XX_PCM_BITS("Center Playback Switch", 2, 15, 15, 1),
1006 AD18XX_PCM_BITS("Center Playback Volume", 2, 8, 8, 31)
1009 static const snd_kcontrol_new_t snd_ac97_controls_ad18xx_lfe[2] = {
1010 AD18XX_PCM_BITS("LFE Playback Switch", 2, 7, 7, 1),
1011 AD18XX_PCM_BITS("LFE Playback Volume", 2, 0, 0, 31)
1018 static void snd_ac97_powerdown(ac97_t *ac97);
1020 static int snd_ac97_bus_free(ac97_bus_t *bus)
1023 snd_ac97_bus_proc_done(bus);
1025 if (bus->private_free)
1026 bus->private_free(bus);
1032 static int snd_ac97_bus_dev_free(snd_device_t *device)
1034 ac97_bus_t *bus = device->device_data;
1035 return snd_ac97_bus_free(bus);
1038 static int snd_ac97_free(ac97_t *ac97)
1041 snd_ac97_proc_done(ac97);
1043 ac97->bus->codec[ac97->num] = NULL;
1044 if (ac97->bus->shared_type) {
1045 down(&shared_codec_mutex);
1046 shared_codec[ac97->bus->shared_type-1][ac97->num] = NULL;
1047 up(&shared_codec_mutex);
1050 if (ac97->private_free)
1051 ac97->private_free(ac97);
1057 static int snd_ac97_dev_free(snd_device_t *device)
1059 ac97_t *ac97 = device->device_data;
1060 snd_ac97_powerdown(ac97); /* for avoiding click noises during shut down */
1061 return snd_ac97_free(ac97);
1064 static int snd_ac97_try_volume_mix(ac97_t * ac97, int reg)
1066 unsigned short val, mask = 0x8000;
1068 if (! snd_ac97_valid_reg(ac97, reg))
1072 case AC97_MASTER_TONE:
1073 return ac97->caps & 0x04 ? 1 : 0;
1074 case AC97_HEADPHONE:
1075 return ac97->caps & 0x10 ? 1 : 0;
1076 case AC97_REC_GAIN_MIC:
1077 return ac97->caps & 0x01 ? 1 : 0;
1078 case AC97_3D_CONTROL:
1079 if (ac97->caps & 0x7c00) {
1080 val = snd_ac97_read(ac97, reg);
1081 /* if nonzero - fixed and we can't set it */
1085 case AC97_CENTER_LFE_MASTER: /* center */
1086 if ((ac97->ext_id & AC97_EI_CDAC) == 0)
1089 case AC97_CENTER_LFE_MASTER+1: /* lfe */
1090 if ((ac97->ext_id & AC97_EI_LDAC) == 0)
1092 reg = AC97_CENTER_LFE_MASTER;
1095 case AC97_SURROUND_MASTER:
1096 if ((ac97->ext_id & AC97_EI_SDAC) == 0)
1101 if (ac97->limited_regs && test_bit(reg, ac97->reg_accessed))
1102 return 1; /* allow without check */
1104 val = snd_ac97_read(ac97, reg);
1105 if (!(val & mask)) {
1106 /* nothing seems to be here - mute flag is not set */
1107 /* try another test */
1108 snd_ac97_write_cache(ac97, reg, val | mask);
1109 val = snd_ac97_read(ac97, reg);
1111 return 0; /* nothing here */
1113 return 1; /* success, useable */
1116 static void check_volume_resolution(ac97_t *ac97, int reg, unsigned char *lo_max, unsigned char *hi_max)
1118 unsigned short cbit[3] = { 0x20, 0x10, 0x01 };
1119 unsigned char max[3] = { 63, 31, 15 };
1122 *lo_max = *hi_max = 0;
1123 for (i = 0 ; i < ARRAY_SIZE(cbit); i++) {
1125 snd_ac97_write(ac97, reg, 0x8080 | cbit[i] | (cbit[i] << 8));
1126 val = snd_ac97_read(ac97, reg);
1127 if (! *lo_max && (val & cbit[i]))
1129 if (! *hi_max && (val & (cbit[i] << 8)))
1131 if (*lo_max && *hi_max)
1136 int snd_ac97_try_bit(ac97_t * ac97, int reg, int bit)
1138 unsigned short mask, val, orig, res;
1141 orig = snd_ac97_read(ac97, reg);
1143 snd_ac97_write(ac97, reg, val);
1144 res = snd_ac97_read(ac97, reg);
1145 snd_ac97_write_cache(ac97, reg, orig);
1149 /* check the volume resolution of center/lfe */
1150 static void snd_ac97_change_volume_params2(ac97_t * ac97, int reg, int shift, unsigned char *max)
1152 unsigned short val, val1;
1155 val = 0x8080 | (0x20 << shift);
1156 snd_ac97_write(ac97, reg, val);
1157 val1 = snd_ac97_read(ac97, reg);
1161 /* reset volume to zero */
1162 snd_ac97_write_cache(ac97, reg, 0x8080);
1165 static inline int printable(unsigned int x)
1168 if (x < ' ' || x >= 0x71) {
1170 return x - 0x71 + 'A';
1176 snd_kcontrol_t *snd_ac97_cnew(const snd_kcontrol_new_t *_template, ac97_t * ac97)
1178 snd_kcontrol_new_t template;
1179 memcpy(&template, _template, sizeof(template));
1180 snd_runtime_check(!template.index, return NULL);
1181 template.index = ac97->num;
1182 return snd_ctl_new1(&template, ac97);
1186 * create mute switch(es) for normal stereo controls
1188 static int snd_ac97_cmute_new_stereo(snd_card_t *card, char *name, int reg, int check_stereo, ac97_t *ac97)
1190 snd_kcontrol_t *kctl;
1192 unsigned short val, val1, mute_mask;
1194 if (! snd_ac97_valid_reg(ac97, reg))
1198 val = snd_ac97_read(ac97, reg);
1199 if (check_stereo || (ac97->flags & AC97_STEREO_MUTES)) {
1200 /* check whether both mute bits work */
1201 val1 = val | 0x8080;
1202 snd_ac97_write(ac97, reg, val1);
1203 if (val1 == snd_ac97_read(ac97, reg))
1206 if (mute_mask == 0x8080) {
1207 snd_kcontrol_new_t tmp = AC97_DOUBLE(name, reg, 15, 7, 1, 1);
1208 tmp.index = ac97->num;
1209 kctl = snd_ctl_new1(&tmp, ac97);
1211 snd_kcontrol_new_t tmp = AC97_SINGLE(name, reg, 15, 1, 1);
1212 tmp.index = ac97->num;
1213 kctl = snd_ctl_new1(&tmp, ac97);
1215 err = snd_ctl_add(card, kctl);
1218 /* mute as default */
1219 snd_ac97_write_cache(ac97, reg, val | mute_mask);
1224 * create a volume for normal stereo/mono controls
1226 static int snd_ac97_cvol_new(snd_card_t *card, char *name, int reg, unsigned int lo_max,
1227 unsigned int hi_max, ac97_t *ac97)
1230 snd_kcontrol_t *kctl;
1232 if (! snd_ac97_valid_reg(ac97, reg))
1236 snd_kcontrol_new_t tmp = AC97_DOUBLE(name, reg, 8, 0, lo_max, 1);
1237 tmp.index = ac97->num;
1238 kctl = snd_ctl_new1(&tmp, ac97);
1241 snd_kcontrol_new_t tmp = AC97_SINGLE(name, reg, 0, lo_max, 1);
1242 tmp.index = ac97->num;
1243 kctl = snd_ctl_new1(&tmp, ac97);
1245 err = snd_ctl_add(card, kctl);
1248 snd_ac97_write_cache(ac97, reg,
1249 (snd_ac97_read(ac97, reg) & 0x8080) |
1250 lo_max | (hi_max << 8));
1255 * create a mute-switch and a volume for normal stereo/mono controls
1257 static int snd_ac97_cmix_new_stereo(snd_card_t *card, const char *pfx, int reg, int check_stereo, ac97_t *ac97)
1261 unsigned char lo_max, hi_max;
1263 if (! snd_ac97_valid_reg(ac97, reg))
1266 if (snd_ac97_try_bit(ac97, reg, 15)) {
1267 sprintf(name, "%s Switch", pfx);
1268 if ((err = snd_ac97_cmute_new_stereo(card, name, reg, check_stereo, ac97)) < 0)
1271 check_volume_resolution(ac97, reg, &lo_max, &hi_max);
1273 sprintf(name, "%s Volume", pfx);
1274 if ((err = snd_ac97_cvol_new(card, name, reg, lo_max, hi_max, ac97)) < 0)
1280 #define snd_ac97_cmix_new(card, pfx, reg, ac97) snd_ac97_cmix_new_stereo(card, pfx, reg, 0, ac97)
1281 #define snd_ac97_cmute_new(card, name, reg, ac97) snd_ac97_cmute_new_stereo(card, name, reg, 0, ac97)
1283 static unsigned int snd_ac97_determine_spdif_rates(ac97_t *ac97);
1285 static int snd_ac97_mixer_build(ac97_t * ac97)
1287 snd_card_t *card = ac97->bus->card;
1288 snd_kcontrol_t *kctl;
1293 /* build master controls */
1294 /* AD claims to remove this control from AD1887, although spec v2.2 does not allow this */
1295 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER)) {
1296 if (ac97->flags & AC97_HAS_NO_MASTER_VOL)
1297 err = snd_ac97_cmute_new(card, "Master Playback Switch", AC97_MASTER, ac97);
1299 err = snd_ac97_cmix_new(card, "Master Playback", AC97_MASTER, ac97);
1304 ac97->regs[AC97_CENTER_LFE_MASTER] = 0x8080;
1306 /* build center controls */
1307 if (snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER)) {
1308 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_center[0], ac97))) < 0)
1310 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_center[1], ac97))) < 0)
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 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max);
1318 /* build LFE controls */
1319 if (snd_ac97_try_volume_mix(ac97, AC97_CENTER_LFE_MASTER+1)) {
1320 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_lfe[0], ac97))) < 0)
1322 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_lfe[1], ac97))) < 0)
1324 snd_ac97_change_volume_params2(ac97, AC97_CENTER_LFE_MASTER, 8, &max);
1325 kctl->private_value &= ~(0xff << 16);
1326 kctl->private_value |= (int)max << 16;
1327 snd_ac97_write_cache(ac97, AC97_CENTER_LFE_MASTER, ac97->regs[AC97_CENTER_LFE_MASTER] | max << 8);
1330 /* build surround controls */
1331 if (snd_ac97_try_volume_mix(ac97, AC97_SURROUND_MASTER)) {
1332 /* Surround Master (0x38) is with stereo mutes */
1333 if ((err = snd_ac97_cmix_new_stereo(card, "Surround Playback", AC97_SURROUND_MASTER, 1, ac97)) < 0)
1337 /* build headphone controls */
1338 if (snd_ac97_try_volume_mix(ac97, AC97_HEADPHONE)) {
1339 if ((err = snd_ac97_cmix_new(card, "Headphone Playback", AC97_HEADPHONE, ac97)) < 0)
1343 /* build master mono controls */
1344 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_MONO)) {
1345 if ((err = snd_ac97_cmix_new(card, "Master Mono Playback", AC97_MASTER_MONO, ac97)) < 0)
1349 /* build master tone controls */
1350 if (snd_ac97_try_volume_mix(ac97, AC97_MASTER_TONE)) {
1351 for (idx = 0; idx < 2; idx++) {
1352 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_tone[idx], ac97))) < 0)
1354 if (ac97->id == AC97_ID_YMF753) {
1355 kctl->private_value &= ~(0xff << 16);
1356 kctl->private_value |= 7 << 16;
1359 snd_ac97_write_cache(ac97, AC97_MASTER_TONE, 0x0f0f);
1362 /* build PC Speaker controls */
1363 if ((ac97->flags & AC97_HAS_PC_BEEP) ||
1364 snd_ac97_try_volume_mix(ac97, AC97_PC_BEEP)) {
1365 for (idx = 0; idx < 2; idx++)
1366 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_pc_beep[idx], ac97))) < 0)
1368 snd_ac97_write_cache(ac97, AC97_PC_BEEP,
1369 snd_ac97_read(ac97, AC97_PC_BEEP) | 0x801e);
1372 /* build Phone controls */
1373 if (snd_ac97_try_volume_mix(ac97, AC97_PHONE)) {
1374 if ((err = snd_ac97_cmix_new(card, "Phone Playback", AC97_PHONE, ac97)) < 0)
1378 /* build MIC controls */
1379 if (snd_ac97_try_volume_mix(ac97, AC97_MIC)) {
1380 if ((err = snd_ac97_cmix_new(card, "Mic Playback", AC97_MIC, ac97)) < 0)
1382 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_boost, ac97))) < 0)
1386 /* build Line controls */
1387 if (snd_ac97_try_volume_mix(ac97, AC97_LINE)) {
1388 if ((err = snd_ac97_cmix_new(card, "Line Playback", AC97_LINE, ac97)) < 0)
1392 /* build CD controls */
1393 if (snd_ac97_try_volume_mix(ac97, AC97_CD)) {
1394 if ((err = snd_ac97_cmix_new(card, "CD Playback", AC97_CD, ac97)) < 0)
1398 /* build Video controls */
1399 if (snd_ac97_try_volume_mix(ac97, AC97_VIDEO)) {
1400 if ((err = snd_ac97_cmix_new(card, "Video Playback", AC97_VIDEO, ac97)) < 0)
1404 /* build Aux controls */
1405 if (snd_ac97_try_volume_mix(ac97, AC97_AUX)) {
1406 if ((err = snd_ac97_cmix_new(card, "Aux Playback", AC97_AUX, ac97)) < 0)
1410 /* build PCM controls */
1411 if (ac97->flags & AC97_AD_MULTI) {
1412 unsigned short init_val;
1413 if (ac97->flags & AC97_STEREO_MUTES)
1417 for (idx = 0; idx < 2; idx++)
1418 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_pcm[idx], ac97))) < 0)
1420 ac97->spec.ad18xx.pcmreg[0] = init_val;
1421 if (ac97->scaps & AC97_SCAP_SURROUND_DAC) {
1422 for (idx = 0; idx < 2; idx++)
1423 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_surround[idx], ac97))) < 0)
1425 ac97->spec.ad18xx.pcmreg[1] = init_val;
1427 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) {
1428 for (idx = 0; idx < 2; idx++)
1429 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_center[idx], ac97))) < 0)
1431 for (idx = 0; idx < 2; idx++)
1432 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_ad18xx_lfe[idx], ac97))) < 0)
1434 ac97->spec.ad18xx.pcmreg[2] = init_val;
1436 snd_ac97_write_cache(ac97, AC97_PCM, init_val);
1438 if (ac97->flags & AC97_HAS_NO_PCM_VOL)
1439 err = snd_ac97_cmute_new(card, "PCM Playback Switch", AC97_PCM, ac97);
1441 err = snd_ac97_cmix_new(card, "PCM Playback", AC97_PCM, ac97);
1446 /* build Capture controls */
1447 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_src, ac97))) < 0)
1449 if (snd_ac97_try_bit(ac97, AC97_REC_GAIN, 15)) {
1450 if ((err = snd_ac97_cmute_new(card, "Capture Switch", AC97_REC_GAIN, ac97)) < 0)
1453 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_control_capture_vol, ac97))) < 0)
1455 snd_ac97_write_cache(ac97, AC97_REC_SEL, 0x0000);
1456 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x0000);
1458 /* build MIC Capture controls */
1459 if (snd_ac97_try_volume_mix(ac97, AC97_REC_GAIN_MIC)) {
1460 for (idx = 0; idx < 2; idx++)
1461 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_mic_capture[idx], ac97))) < 0)
1463 snd_ac97_write_cache(ac97, AC97_REC_GAIN_MIC, 0x0000);
1466 /* build PCM out path & mute control */
1467 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 15)) {
1468 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_PCM_OUT], ac97))) < 0)
1472 /* build Simulated Stereo Enhancement control */
1473 if (ac97->caps & 0x0008) {
1474 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_STEREO_ENHANCEMENT], ac97))) < 0)
1478 /* build 3D Stereo Enhancement control */
1479 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 13)) {
1480 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_3D], ac97))) < 0)
1484 /* build Loudness control */
1485 if (ac97->caps & 0x0020) {
1486 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOUDNESS], ac97))) < 0)
1490 /* build Mono output select control */
1491 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 9)) {
1492 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MONO], ac97))) < 0)
1496 /* build Mic select control */
1497 if (snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 8)) {
1498 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_MIC], ac97))) < 0)
1502 /* build ADC/DAC loopback control */
1503 if (enable_loopback && snd_ac97_try_bit(ac97, AC97_GENERAL_PURPOSE, 7)) {
1504 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_general[AC97_GENERAL_LOOPBACK], ac97))) < 0)
1508 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, ~AC97_GP_DRSS_MASK, 0x0000);
1510 /* build 3D controls */
1511 if (ac97->build_ops->build_3d) {
1512 ac97->build_ops->build_3d(ac97);
1514 if (snd_ac97_try_volume_mix(ac97, AC97_3D_CONTROL)) {
1517 snd_ac97_write(ac97, AC97_3D_CONTROL, val);
1518 val = snd_ac97_read(ac97, AC97_3D_CONTROL);
1519 val = val == 0x0606;
1520 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
1523 kctl->private_value = AC97_3D_CONTROL | (9 << 8) | (7 << 16);
1524 if ((err = snd_ctl_add(card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[1], ac97))) < 0)
1527 kctl->private_value = AC97_3D_CONTROL | (1 << 8) | (7 << 16);
1528 snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
1532 /* build S/PDIF controls */
1533 if (ac97->ext_id & AC97_EI_SPDIF) {
1534 if (ac97->build_ops->build_spdif) {
1535 if ((err = ac97->build_ops->build_spdif(ac97)) < 0)
1538 for (idx = 0; idx < 5; idx++)
1539 if ((err = snd_ctl_add(card, snd_ac97_cnew(&snd_ac97_controls_spdif[idx], ac97))) < 0)
1541 if (ac97->build_ops->build_post_spdif) {
1542 if ((err = ac97->build_ops->build_post_spdif(ac97)) < 0)
1545 /* set default PCM S/PDIF params */
1546 /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1547 snd_ac97_write_cache(ac97, AC97_SPDIF, 0x2a20);
1548 ac97->rates[AC97_RATES_SPDIF] = snd_ac97_determine_spdif_rates(ac97);
1550 ac97->spdif_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
1553 /* build chip specific controls */
1554 if (ac97->build_ops->build_specific)
1555 if ((err = ac97->build_ops->build_specific(ac97)) < 0)
1558 if (snd_ac97_try_bit(ac97, AC97_POWERDOWN, 15)) {
1559 kctl = snd_ac97_cnew(&snd_ac97_control_eapd, ac97);
1562 if (ac97->scaps & AC97_SCAP_INV_EAPD)
1563 set_inv_eapd(ac97, kctl);
1564 if ((err = snd_ctl_add(card, kctl)) < 0)
1571 static int snd_ac97_modem_build(snd_card_t * card, ac97_t * ac97)
1574 //printk("AC97_GPIO_CFG = %x\n",snd_ac97_read(ac97,AC97_GPIO_CFG));
1575 snd_ac97_write(ac97, AC97_GPIO_CFG, 0xffff & ~(AC97_GPIO_LINE1_OH));
1576 snd_ac97_write(ac97, AC97_GPIO_POLARITY, 0xffff & ~(AC97_GPIO_LINE1_OH));
1577 snd_ac97_write(ac97, AC97_GPIO_STICKY, 0xffff);
1578 snd_ac97_write(ac97, AC97_GPIO_WAKEUP, 0x0);
1579 snd_ac97_write(ac97, AC97_MISC_AFE, 0x0);
1583 static int snd_ac97_test_rate(ac97_t *ac97, int reg, int shadow_reg, int rate)
1588 tmp = ((unsigned int)rate * ac97->bus->clock) / 48000;
1589 snd_ac97_write_cache(ac97, reg, tmp & 0xffff);
1591 snd_ac97_write_cache(ac97, shadow_reg, tmp & 0xffff);
1592 val = snd_ac97_read(ac97, reg);
1593 return val == (tmp & 0xffff);
1596 static void snd_ac97_determine_rates(ac97_t *ac97, int reg, int shadow_reg, unsigned int *r_result)
1598 unsigned int result = 0;
1600 if (ac97->bus->no_vra) {
1601 *r_result = SNDRV_PCM_RATE_48000;
1602 if ((ac97->flags & AC97_DOUBLE_RATE) &&
1603 reg == AC97_PCM_FRONT_DAC_RATE)
1604 *r_result |= SNDRV_PCM_RATE_96000;
1608 if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
1609 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1611 /* test a non-standard rate */
1612 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11000))
1613 result |= SNDRV_PCM_RATE_CONTINUOUS;
1614 /* let's try to obtain standard rates */
1615 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 8000))
1616 result |= SNDRV_PCM_RATE_8000;
1617 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 11025))
1618 result |= SNDRV_PCM_RATE_11025;
1619 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 16000))
1620 result |= SNDRV_PCM_RATE_16000;
1621 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 22050))
1622 result |= SNDRV_PCM_RATE_22050;
1623 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 32000))
1624 result |= SNDRV_PCM_RATE_32000;
1625 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 44100))
1626 result |= SNDRV_PCM_RATE_44100;
1627 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 48000))
1628 result |= SNDRV_PCM_RATE_48000;
1629 if ((ac97->flags & AC97_DOUBLE_RATE) &&
1630 reg == AC97_PCM_FRONT_DAC_RATE) {
1631 /* test standard double rates */
1632 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1633 AC97_EA_DRA, AC97_EA_DRA);
1634 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 64000 / 2))
1635 result |= SNDRV_PCM_RATE_64000;
1636 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 88200 / 2))
1637 result |= SNDRV_PCM_RATE_88200;
1638 if (snd_ac97_test_rate(ac97, reg, shadow_reg, 96000 / 2))
1639 result |= SNDRV_PCM_RATE_96000;
1640 /* some codecs don't support variable double rates */
1641 if (!snd_ac97_test_rate(ac97, reg, shadow_reg, 76100 / 2))
1642 result &= ~SNDRV_PCM_RATE_CONTINUOUS;
1643 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
1649 /* check AC97_SPDIF register to accept which sample rates */
1650 static unsigned int snd_ac97_determine_spdif_rates(ac97_t *ac97)
1652 unsigned int result = 0;
1654 static unsigned short ctl_bits[] = {
1655 AC97_SC_SPSR_44K, AC97_SC_SPSR_32K, AC97_SC_SPSR_48K
1657 static unsigned int rate_bits[] = {
1658 SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_32000, SNDRV_PCM_RATE_48000
1661 for (i = 0; i < (int)ARRAY_SIZE(ctl_bits); i++) {
1662 snd_ac97_update_bits(ac97, AC97_SPDIF, AC97_SC_SPSR_MASK, ctl_bits[i]);
1663 if ((snd_ac97_read(ac97, AC97_SPDIF) & AC97_SC_SPSR_MASK) == ctl_bits[i])
1664 result |= rate_bits[i];
1669 void snd_ac97_get_name(ac97_t *ac97, unsigned int id, char *name, int modem)
1671 const ac97_codec_id_t *pid;
1673 sprintf(name, "0x%x %c%c%c", id,
1674 printable(id >> 24),
1675 printable(id >> 16),
1676 printable(id >> 8));
1677 for (pid = snd_ac97_codec_id_vendors; pid->id; pid++)
1678 if (pid->id == (id & pid->mask)) {
1679 strcpy(name, pid->name);
1681 if (!modem && pid->patch)
1683 else if (modem && pid->mpatch)
1691 for (pid = snd_ac97_codec_ids; pid->id; pid++)
1692 if (pid->id == (id & pid->mask)) {
1694 strcat(name, pid->name);
1695 if (pid->mask != 0xffffffff)
1696 sprintf(name + strlen(name), " rev %d", id & ~pid->mask);
1698 if (!modem && pid->patch)
1700 else if (modem && pid->mpatch)
1705 sprintf(name + strlen(name), " id %x", id & 0xff);
1709 * snd_ac97_get_short_name - retrieve codec name
1710 * @ac97: the codec instance
1712 * Returns the short identifying name of the codec.
1714 const char *snd_ac97_get_short_name(ac97_t *ac97)
1716 const ac97_codec_id_t *pid;
1718 for (pid = snd_ac97_codec_ids; pid->id; pid++)
1719 if (pid->id == (ac97->id & pid->mask))
1721 return "unknown codec";
1725 /* wait for a while until registers are accessible after RESET
1726 * return 0 if ok, negative not ready
1728 static int ac97_reset_wait(ac97_t *ac97, int timeout, int with_modem)
1730 unsigned long end_time;
1733 end_time = jiffies + timeout;
1736 /* use preliminary reads to settle the communication */
1737 snd_ac97_read(ac97, AC97_RESET);
1738 snd_ac97_read(ac97, AC97_VENDOR_ID1);
1739 snd_ac97_read(ac97, AC97_VENDOR_ID2);
1742 val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
1743 if (val != 0xffff && (val & 1) != 0)
1746 if (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) {
1747 /* probably only Xbox issue - all registers are read as zero */
1748 val = snd_ac97_read(ac97, AC97_VENDOR_ID1);
1749 if (val != 0 && val != 0xffff)
1752 /* because the PCM or MASTER volume registers can be modified,
1753 * the REC_GAIN register is used for tests
1755 /* test if we can write to the record gain volume register */
1756 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
1757 if ((snd_ac97_read(ac97, AC97_REC_GAIN) & 0x7fff) == 0x0a05)
1760 set_current_state(TASK_UNINTERRUPTIBLE);
1761 schedule_timeout(1);
1762 } while (time_after_eq(end_time, jiffies));
1767 * snd_ac97_bus - create an AC97 bus component
1768 * @card: the card instance
1769 * @num: the bus number
1770 * @ops: the bus callbacks table
1771 * @private_data: private data pointer for the new instance
1772 * @rbus: the pointer to store the new AC97 bus instance.
1774 * Creates an AC97 bus component. An ac97_bus_t instance is newly
1775 * allocated and initialized.
1777 * The ops table must include valid callbacks (at least read and
1778 * write). The other callbacks, wait and reset, are not mandatory.
1780 * The clock is set to 48000. If another clock is needed, set
1781 * (*rbus)->clock manually.
1783 * The AC97 bus instance is registered as a low-level device, so you don't
1784 * have to release it manually.
1786 * Returns zero if successful, or a negative error code on failure.
1788 int snd_ac97_bus(snd_card_t *card, int num, ac97_bus_ops_t *ops,
1789 void *private_data, ac97_bus_t **rbus)
1793 static snd_device_ops_t dev_ops = {
1794 .dev_free = snd_ac97_bus_dev_free,
1797 snd_assert(card != NULL, return -EINVAL);
1798 snd_assert(rbus != NULL, return -EINVAL);
1799 bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
1805 bus->private_data = private_data;
1807 spin_lock_init(&bus->bus_lock);
1808 snd_ac97_bus_proc_init(bus);
1809 if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
1810 snd_ac97_bus_free(bus);
1817 /* build_ops to do nothing */
1818 static struct snd_ac97_build_ops null_build_ops;
1821 * snd_ac97_mixer - create an Codec97 component
1822 * @bus: the AC97 bus which codec is attached to
1823 * @template: the template of ac97, including index, callbacks and
1825 * @rac97: the pointer to store the new ac97 instance.
1827 * Creates an Codec97 component. An ac97_t instance is newly
1828 * allocated and initialized from the template. The codec
1829 * is then initialized by the standard procedure.
1831 * The template must include the codec number (num) and address (addr),
1832 * and the private data (private_data).
1834 * The ac97 instance is registered as a low-level device, so you don't
1835 * have to release it manually.
1837 * Returns zero if successful, or a negative error code on failure.
1839 int snd_ac97_mixer(ac97_bus_t *bus, ac97_template_t *template, ac97_t **rac97)
1845 unsigned long end_time;
1847 static snd_device_ops_t ops = {
1848 .dev_free = snd_ac97_dev_free,
1851 snd_assert(rac97 != NULL, return -EINVAL);
1853 snd_assert(bus != NULL && template != NULL, return -EINVAL);
1854 snd_assert(template->num < 4 && bus->codec[template->num] == NULL, return -EINVAL);
1856 snd_assert(bus->shared_type <= AC97_SHARED_TYPES, return -EINVAL);
1857 if (bus->shared_type) {
1858 /* already shared? */
1859 down(&shared_codec_mutex);
1860 ac97 = shared_codec[bus->shared_type-1][template->num];
1862 if ((ac97_is_audio(ac97) && (template->scaps & AC97_SCAP_SKIP_AUDIO)) ||
1863 (ac97_is_modem(ac97) && (template->scaps & AC97_SCAP_SKIP_MODEM))) {
1864 up(&shared_codec_mutex);
1865 return -EACCES; /* skip this */
1868 up(&shared_codec_mutex);
1872 ac97 = kcalloc(1, sizeof(*ac97), GFP_KERNEL);
1875 ac97->private_data = template->private_data;
1876 ac97->private_free = template->private_free;
1878 ac97->pci = template->pci;
1879 ac97->num = template->num;
1880 ac97->addr = template->addr;
1881 ac97->scaps = template->scaps;
1882 ac97->limited_regs = template->limited_regs;
1883 memcpy(ac97->reg_accessed, template->reg_accessed, sizeof(ac97->reg_accessed));
1884 bus->codec[ac97->num] = ac97;
1885 init_MUTEX(&ac97->reg_mutex);
1886 init_MUTEX(&ac97->page_mutex);
1889 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_VENDOR_ID, &ac97->subsystem_vendor);
1890 pci_read_config_word(ac97->pci, PCI_SUBSYSTEM_ID, &ac97->subsystem_device);
1892 if (bus->ops->reset) {
1893 bus->ops->reset(ac97);
1897 snd_ac97_write(ac97, AC97_RESET, 0); /* reset to defaults */
1899 bus->ops->wait(ac97);
1902 if (ac97->scaps & AC97_SCAP_SKIP_AUDIO)
1903 err = ac97_reset_wait(ac97, HZ/2, 1);
1905 err = ac97_reset_wait(ac97, HZ/2, 0);
1907 err = ac97_reset_wait(ac97, HZ/2, 1);
1910 snd_printk(KERN_WARNING "AC'97 %d does not respond - RESET\n", ac97->num);
1911 /* proceed anyway - it's often non-critical */
1915 ac97->id = snd_ac97_read(ac97, AC97_VENDOR_ID1) << 16;
1916 ac97->id |= snd_ac97_read(ac97, AC97_VENDOR_ID2);
1917 if (! (ac97->scaps & AC97_SCAP_DETECT_BY_VENDOR) &&
1918 (ac97->id == 0x00000000 || ac97->id == 0xffffffff)) {
1919 snd_printk(KERN_ERR "AC'97 %d access is not valid [0x%x], removing mixer.\n", ac97->num, ac97->id);
1920 snd_ac97_free(ac97);
1924 /* test for AC'97 */
1925 if (!(ac97->scaps & AC97_SCAP_SKIP_AUDIO) && !(ac97->scaps & AC97_SCAP_AUDIO)) {
1926 /* test if we can write to the record gain volume register */
1927 snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a06);
1928 if (((err = snd_ac97_read(ac97, AC97_REC_GAIN)) & 0x7fff) == 0x0a06)
1929 ac97->scaps |= AC97_SCAP_AUDIO;
1931 if (ac97->scaps & AC97_SCAP_AUDIO) {
1932 ac97->caps = snd_ac97_read(ac97, AC97_RESET);
1933 ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
1934 if (ac97->ext_id == 0xffff) /* invalid combination */
1938 /* test for MC'97 */
1939 if (!(ac97->scaps & AC97_SCAP_SKIP_MODEM) && !(ac97->scaps & AC97_SCAP_MODEM)) {
1940 ac97->ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
1941 if (ac97->ext_mid == 0xffff) /* invalid combination */
1943 if (ac97->ext_mid & 1)
1944 ac97->scaps |= AC97_SCAP_MODEM;
1947 if (!ac97_is_audio(ac97) && !ac97_is_modem(ac97)) {
1948 if (!(ac97->scaps & (AC97_SCAP_SKIP_AUDIO|AC97_SCAP_SKIP_MODEM)))
1949 snd_printk(KERN_ERR "AC'97 %d access error (not audio or modem codec)\n", ac97->num);
1950 snd_ac97_free(ac97);
1954 if (bus->ops->reset) // FIXME: always skipping?
1957 /* FIXME: add powerdown control */
1958 if (ac97_is_audio(ac97)) {
1959 /* nothing should be in powerdown mode */
1960 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
1961 snd_ac97_write_cache(ac97, AC97_RESET, 0); /* reset to defaults */
1963 /* nothing should be in powerdown mode */
1964 snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0);
1965 snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0);
1966 end_time = jiffies + (HZ / 10);
1968 if ((snd_ac97_read(ac97, AC97_POWERDOWN) & 0x0f) == 0x0f)
1970 set_current_state(TASK_UNINTERRUPTIBLE);
1971 schedule_timeout(1);
1972 } while (time_after_eq(end_time, jiffies));
1973 snd_printk(KERN_WARNING "AC'97 %d analog subsections not ready\n", ac97->num);
1976 /* FIXME: add powerdown control */
1977 if (ac97_is_modem(ac97)) {
1980 /* nothing should be in powerdown mode */
1981 /* note: it's important to set the rate at first */
1982 tmp = AC97_MEA_GPIO;
1983 if (ac97->ext_mid & AC97_MEI_LINE1) {
1984 snd_ac97_write_cache(ac97, AC97_LINE1_RATE, 12000);
1985 tmp |= AC97_MEA_ADC1 | AC97_MEA_DAC1;
1987 if (ac97->ext_mid & AC97_MEI_LINE2) {
1988 snd_ac97_write_cache(ac97, AC97_LINE2_RATE, 12000);
1989 tmp |= AC97_MEA_ADC2 | AC97_MEA_DAC2;
1991 if (ac97->ext_mid & AC97_MEI_HANDSET) {
1992 snd_ac97_write_cache(ac97, AC97_HANDSET_RATE, 12000);
1993 tmp |= AC97_MEA_HADC | AC97_MEA_HDAC;
1995 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0xff00 & ~(tmp << 8));
1997 /* nothing should be in powerdown mode */
1998 snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0xff00 & ~(tmp << 8));
1999 end_time = jiffies + (HZ / 10);
2001 if ((snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS) & tmp) == tmp)
2003 set_current_state(TASK_UNINTERRUPTIBLE);
2004 schedule_timeout(1);
2005 } while (time_after_eq(end_time, jiffies));
2006 snd_printk(KERN_WARNING "MC'97 %d converters and GPIO not ready (0x%x)\n", ac97->num, snd_ac97_read(ac97, AC97_EXTENDED_MSTATUS));
2010 if (ac97_is_audio(ac97))
2011 ac97->addr = (ac97->ext_id & AC97_EI_ADDR_MASK) >> AC97_EI_ADDR_SHIFT;
2013 ac97->addr = (ac97->ext_mid & AC97_MEI_ADDR_MASK) >> AC97_MEI_ADDR_SHIFT;
2014 if (ac97->ext_id & 0x01c9) { /* L/R, MIC, SDAC, LDAC VRA support */
2015 reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
2016 reg |= ac97->ext_id & 0x01c0; /* LDAC/SDAC/CDAC */
2018 reg |= ac97->ext_id & 0x0009; /* VRA/VRM */
2019 snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
2021 if ((ac97->ext_id & AC97_EI_DRA) && bus->dra) {
2022 /* Intel controllers require double rate data to be put in
2023 * slots 7+8, so let's hope the codec supports it. */
2024 snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE, AC97_GP_DRSS_MASK, AC97_GP_DRSS_78);
2025 if ((snd_ac97_read(ac97, AC97_GENERAL_PURPOSE) & AC97_GP_DRSS_MASK) == AC97_GP_DRSS_78)
2026 ac97->flags |= AC97_DOUBLE_RATE;
2028 if (ac97->ext_id & AC97_EI_VRA) { /* VRA support */
2029 snd_ac97_determine_rates(ac97, AC97_PCM_FRONT_DAC_RATE, 0, &ac97->rates[AC97_RATES_FRONT_DAC]);
2030 snd_ac97_determine_rates(ac97, AC97_PCM_LR_ADC_RATE, 0, &ac97->rates[AC97_RATES_ADC]);
2032 ac97->rates[AC97_RATES_FRONT_DAC] = SNDRV_PCM_RATE_48000;
2033 if (ac97->flags & AC97_DOUBLE_RATE)
2034 ac97->rates[AC97_RATES_FRONT_DAC] |= SNDRV_PCM_RATE_96000;
2035 ac97->rates[AC97_RATES_ADC] = SNDRV_PCM_RATE_48000;
2037 if (ac97->ext_id & AC97_EI_SPDIF) {
2038 /* codec specific code (patch) should override these values */
2039 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_32000;
2041 if (ac97->ext_id & AC97_EI_VRM) { /* MIC VRA support */
2042 snd_ac97_determine_rates(ac97, AC97_PCM_MIC_ADC_RATE, 0, &ac97->rates[AC97_RATES_MIC_ADC]);
2044 ac97->rates[AC97_RATES_MIC_ADC] = SNDRV_PCM_RATE_48000;
2046 if (ac97->ext_id & AC97_EI_SDAC) { /* SDAC support */
2047 snd_ac97_determine_rates(ac97, AC97_PCM_SURR_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_SURR_DAC]);
2048 ac97->scaps |= AC97_SCAP_SURROUND_DAC;
2050 if (ac97->ext_id & AC97_EI_LDAC) { /* LDAC support */
2051 snd_ac97_determine_rates(ac97, AC97_PCM_LFE_DAC_RATE, AC97_PCM_FRONT_DAC_RATE, &ac97->rates[AC97_RATES_LFE_DAC]);
2052 ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
2054 /* additional initializations */
2056 bus->ops->init(ac97);
2057 snd_ac97_get_name(ac97, ac97->id, name, !ac97_is_audio(ac97));
2058 snd_ac97_get_name(NULL, ac97->id, name, !ac97_is_audio(ac97)); // ac97->id might be changed in the special setup code
2059 if (! ac97->build_ops)
2060 ac97->build_ops = &null_build_ops;
2062 if (ac97_is_audio(ac97)) {
2064 if (card->mixername[0] == '\0') {
2065 strcpy(card->mixername, name);
2067 if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
2068 strcat(card->mixername, ",");
2069 strcat(card->mixername, name);
2072 sprintf(comp, "AC97a:%08x", ac97->id);
2073 if ((err = snd_component_add(card, comp)) < 0) {
2074 snd_ac97_free(ac97);
2077 if (snd_ac97_mixer_build(ac97) < 0) {
2078 snd_ac97_free(ac97);
2082 if (ac97_is_modem(ac97)) {
2084 if (card->mixername[0] == '\0') {
2085 strcpy(card->mixername, name);
2087 if (strlen(card->mixername) + 1 + strlen(name) + 1 <= sizeof(card->mixername)) {
2088 strcat(card->mixername, ",");
2089 strcat(card->mixername, name);
2092 sprintf(comp, "AC97m:%08x", ac97->id);
2093 if ((err = snd_component_add(card, comp)) < 0) {
2094 snd_ac97_free(ac97);
2097 if (snd_ac97_modem_build(card, ac97) < 0) {
2098 snd_ac97_free(ac97);
2102 /* make sure the proper powerdown bits are cleared */
2104 reg = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
2105 if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
2106 reg &= ~AC97_EA_PRJ;
2107 if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
2108 reg &= ~(AC97_EA_PRI | AC97_EA_PRK);
2109 snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, reg);
2111 snd_ac97_proc_init(ac97);
2112 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ac97, &ops)) < 0) {
2113 snd_ac97_free(ac97);
2118 if (bus->shared_type) {
2119 down(&shared_codec_mutex);
2120 shared_codec[bus->shared_type-1][ac97->num] = ac97;
2121 up(&shared_codec_mutex);
2129 * Power down the chip.
2131 * MASTER and HEADPHONE registers are muted but the register cache values
2132 * are not changed, so that the values can be restored in snd_ac97_resume().
2134 static void snd_ac97_powerdown(ac97_t *ac97)
2136 unsigned short power;
2138 if (ac97_is_audio(ac97)) {
2139 /* some codecs have stereo mute bits */
2140 snd_ac97_write(ac97, AC97_MASTER, 0x9f9f);
2141 snd_ac97_write(ac97, AC97_HEADPHONE, 0x9f9f);
2144 power = ac97->regs[AC97_POWERDOWN] | 0x8000; /* EAPD */
2145 power |= 0x4000; /* Headphone amplifier powerdown */
2146 power |= 0x0300; /* ADC & DAC powerdown */
2147 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2149 power |= 0x0400; /* Analog Mixer powerdown (Vref on) */
2150 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2153 /* FIXME: this causes click noises on some boards at resume */
2154 power |= 0x3800; /* AC-link powerdown, internal Clk disable */
2155 snd_ac97_write(ac97, AC97_POWERDOWN, power);
2162 * snd_ac97_suspend - General suspend function for AC97 codec
2163 * @ac97: the ac97 instance
2165 * Suspends the codec, power down the chip.
2167 void snd_ac97_suspend(ac97_t *ac97)
2169 if (ac97->build_ops->suspend)
2170 ac97->build_ops->suspend(ac97);
2171 snd_ac97_powerdown(ac97);
2175 * restore ac97 status
2177 void snd_ac97_restore_status(ac97_t *ac97)
2181 for (i = 2; i < 0x7c ; i += 2) {
2182 if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
2184 /* restore only accessible registers
2185 * some chip (e.g. nm256) may hang up when unsupported registers
2188 if (test_bit(i, ac97->reg_accessed)) {
2189 snd_ac97_write(ac97, i, ac97->regs[i]);
2190 snd_ac97_read(ac97, i);
2196 * restore IEC958 status
2198 void snd_ac97_restore_iec958(ac97_t *ac97)
2200 if (ac97->ext_id & AC97_EI_SPDIF) {
2201 if (ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_SPDIF) {
2202 /* reset spdif status */
2203 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
2204 snd_ac97_write(ac97, AC97_EXTENDED_STATUS, ac97->regs[AC97_EXTENDED_STATUS]);
2205 if (ac97->flags & AC97_CS_SPDIF)
2206 snd_ac97_write(ac97, AC97_CSR_SPDIF, ac97->regs[AC97_CSR_SPDIF]);
2208 snd_ac97_write(ac97, AC97_SPDIF, ac97->regs[AC97_SPDIF]);
2209 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF); /* turn on again */
2215 * snd_ac97_resume - General resume function for AC97 codec
2216 * @ac97: the ac97 instance
2218 * Do the standard resume procedure, power up and restoring the
2219 * old register values.
2221 void snd_ac97_resume(ac97_t *ac97)
2225 if (ac97->bus->ops->reset) {
2226 ac97->bus->ops->reset(ac97);
2230 snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2231 snd_ac97_write(ac97, AC97_RESET, 0);
2233 snd_ac97_write(ac97, AC97_POWERDOWN, 0);
2234 snd_ac97_write(ac97, AC97_GENERAL_PURPOSE, 0);
2236 snd_ac97_write(ac97, AC97_POWERDOWN, ac97->regs[AC97_POWERDOWN]);
2237 if (ac97_is_audio(ac97)) {
2238 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8101);
2239 for (i = HZ/10; i >= 0; i--) {
2240 if (snd_ac97_read(ac97, AC97_MASTER) == 0x8101)
2242 set_current_state(TASK_UNINTERRUPTIBLE);
2243 schedule_timeout(1);
2245 /* FIXME: extra delay */
2246 ac97->bus->ops->write(ac97, AC97_MASTER, 0x8000);
2247 if (snd_ac97_read(ac97, AC97_MASTER) != 0x8000) {
2248 set_current_state(TASK_UNINTERRUPTIBLE);
2249 schedule_timeout(HZ/4);
2252 for (i = HZ/10; i >= 0; i--) {
2253 unsigned short val = snd_ac97_read(ac97, AC97_EXTENDED_MID);
2254 if (val != 0xffff && (val & 1) != 0)
2256 set_current_state(TASK_UNINTERRUPTIBLE);
2257 schedule_timeout(1);
2262 if (ac97->bus->ops->init)
2263 ac97->bus->ops->init(ac97);
2265 if (ac97->build_ops->resume)
2266 ac97->build_ops->resume(ac97);
2268 snd_ac97_restore_status(ac97);
2269 snd_ac97_restore_iec958(ac97);
2278 static void set_ctl_name(char *dst, const char *src, const char *suffix)
2281 sprintf(dst, "%s %s", src, suffix);
2286 /* remove the control with the given name and optional suffix */
2287 int snd_ac97_remove_ctl(ac97_t *ac97, const char *name, const char *suffix)
2289 snd_ctl_elem_id_t id;
2290 memset(&id, 0, sizeof(id));
2291 set_ctl_name(id.name, name, suffix);
2292 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2293 return snd_ctl_remove_id(ac97->bus->card, &id);
2296 static snd_kcontrol_t *ctl_find(ac97_t *ac97, const char *name, const char *suffix)
2298 snd_ctl_elem_id_t sid;
2299 memset(&sid, 0, sizeof(sid));
2300 set_ctl_name(sid.name, name, suffix);
2301 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2302 return snd_ctl_find_id(ac97->bus->card, &sid);
2305 /* rename the control with the given name and optional suffix */
2306 int snd_ac97_rename_ctl(ac97_t *ac97, const char *src, const char *dst, const char *suffix)
2308 snd_kcontrol_t *kctl = ctl_find(ac97, src, suffix);
2310 set_ctl_name(kctl->id.name, dst, suffix);
2316 /* rename both Volume and Switch controls - don't check the return value */
2317 void snd_ac97_rename_vol_ctl(ac97_t *ac97, const char *src, const char *dst)
2319 snd_ac97_rename_ctl(ac97, src, dst, "Switch");
2320 snd_ac97_rename_ctl(ac97, src, dst, "Volume");
2324 int snd_ac97_swap_ctl(ac97_t *ac97, const char *s1, const char *s2, const char *suffix)
2326 snd_kcontrol_t *kctl1, *kctl2;
2327 kctl1 = ctl_find(ac97, s1, suffix);
2328 kctl2 = ctl_find(ac97, s2, suffix);
2329 if (kctl1 && kctl2) {
2330 set_ctl_name(kctl1->id.name, s2, suffix);
2331 set_ctl_name(kctl2->id.name, s1, suffix);
2337 /* ac97 tune: use Headphone control as master */
2338 static int tune_hp_only(ac97_t *ac97)
2340 if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2342 snd_ac97_remove_ctl(ac97, "Master Playback", "Switch");
2343 snd_ac97_remove_ctl(ac97, "Master Playback", "Volume");
2344 snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2348 /* ac97 tune: swap Headphone and Master controls */
2349 static int tune_swap_hp(ac97_t *ac97)
2351 if (ctl_find(ac97, "Headphone Playback Switch", NULL) == NULL)
2353 snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Line-Out Playback");
2354 snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2358 /* ac97 tune: swap Surround and Master controls */
2359 static int tune_swap_surround(ac97_t *ac97)
2361 if (snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Switch") ||
2362 snd_ac97_swap_ctl(ac97, "Master Playback", "Surround Playback", "Volume"))
2367 /* ac97 tune: set up mic sharing for AD codecs */
2368 static int tune_ad_sharing(ac97_t *ac97)
2370 unsigned short scfg;
2371 if ((ac97->id & 0xffffff00) != 0x41445300) {
2372 snd_printk(KERN_ERR "ac97_quirk AD_SHARING is only for AD codecs\n");
2375 /* Turn on OMS bit to route microphone to back panel */
2376 scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
2377 snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x0200);
2381 static const snd_kcontrol_new_t snd_ac97_alc_jack_detect =
2382 AC97_SINGLE("Jack Detect", AC97_ALC650_CLOCK, 5, 1, 0);
2384 /* ac97 tune: set up ALC jack-select */
2385 static int tune_alc_jack(ac97_t *ac97)
2387 if ((ac97->id & 0xffffff00) != 0x414c4700) {
2388 snd_printk(KERN_ERR "ac97_quirk ALC_JACK is only for Realtek codecs\n");
2391 snd_ac97_update_bits(ac97, 0x7a, 0x20, 0x20); /* select jack detect function */
2392 snd_ac97_update_bits(ac97, 0x7a, 0x01, 0x01); /* Line-out auto mute */
2393 return snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_alc_jack_detect, ac97));
2396 /* ac97 tune: inversed EAPD bit */
2397 static int tune_inv_eapd(ac97_t *ac97)
2399 snd_kcontrol_t *kctl = ctl_find(ac97, "External Amplifier", NULL);
2402 set_inv_eapd(ac97, kctl);
2406 static int master_mute_sw_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2408 int err = snd_ac97_put_volsw(kcontrol, ucontrol);
2410 ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
2411 int shift = (kcontrol->private_value >> 8) & 0x0f;
2412 int rshift = (kcontrol->private_value >> 12) & 0x0f;
2413 unsigned short mask;
2414 if (shift != rshift)
2418 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000,
2419 (ac97->regs[AC97_MASTER] & mask) == mask ?
2425 /* ac97 tune: EAPD controls mute LED bound with the master mute */
2426 static int tune_mute_led(ac97_t *ac97)
2428 snd_kcontrol_t *msw = ctl_find(ac97, "Master Playback Switch", NULL);
2431 msw->put = master_mute_sw_put;
2432 snd_ac97_remove_ctl(ac97, "External Amplifier", NULL);
2433 snd_ac97_update_bits(ac97, AC97_POWERDOWN, 0x8000, 0x8000); /* mute LED on */
2437 struct quirk_table {
2439 int (*func)(ac97_t *);
2442 static struct quirk_table applicable_quirks[] = {
2444 { "hp_only", tune_hp_only },
2445 { "swap_hp", tune_swap_hp },
2446 { "swap_surround", tune_swap_surround },
2447 { "ad_sharing", tune_ad_sharing },
2448 { "alc_jack", tune_alc_jack },
2449 { "inv_eapd", tune_inv_eapd },
2450 { "mute_led", tune_mute_led },
2453 /* apply the quirk with the given type */
2454 static int apply_quirk(ac97_t *ac97, int type)
2458 else if (type >= ARRAY_SIZE(applicable_quirks))
2460 if (applicable_quirks[type].func)
2461 return applicable_quirks[type].func(ac97);
2465 /* apply the quirk with the given name */
2466 static int apply_quirk_str(ac97_t *ac97, const char *typestr)
2469 struct quirk_table *q;
2471 for (i = 0; i < ARRAY_SIZE(applicable_quirks); i++) {
2472 q = &applicable_quirks[i];
2473 if (q->name && ! strcmp(typestr, q->name))
2474 return apply_quirk(ac97, i);
2476 /* for compatibility, accept the numbers, too */
2477 if (*typestr >= '0' && *typestr <= '9')
2478 return apply_quirk(ac97, (int)simple_strtol(typestr, NULL, 10));
2483 * snd_ac97_tune_hardware - tune up the hardware
2484 * @ac97: the ac97 instance
2485 * @quirk: quirk list
2486 * @override: explicit quirk value (overrides the list if non-NULL)
2488 * Do some workaround for each pci device, such as renaming of the
2489 * headphone (true line-out) control as "Master".
2490 * The quirk-list must be terminated with a zero-filled entry.
2492 * Returns zero if successful, or a negative error code on failure.
2495 int snd_ac97_tune_hardware(ac97_t *ac97, struct ac97_quirk *quirk, const char *override)
2499 snd_assert(quirk, return -EINVAL);
2501 /* quirk overriden? */
2502 if (override && strcmp(override, "-1") && strcmp(override, "default")) {
2503 result = apply_quirk_str(ac97, override);
2505 snd_printk(KERN_ERR "applying quirk type %s failed (%d)\n", override, result);
2509 for (; quirk->vendor; quirk++) {
2510 if (quirk->vendor != ac97->subsystem_vendor)
2512 if ((! quirk->mask && quirk->device == ac97->subsystem_device) ||
2513 quirk->device == (quirk->mask & ac97->subsystem_device)) {
2514 if (quirk->codec_id && quirk->codec_id != ac97->id)
2516 snd_printdd("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, ac97->subsystem_device);
2517 result = apply_quirk(ac97, quirk->type);
2519 snd_printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
2531 EXPORT_SYMBOL(snd_ac97_write);
2532 EXPORT_SYMBOL(snd_ac97_read);
2533 EXPORT_SYMBOL(snd_ac97_write_cache);
2534 EXPORT_SYMBOL(snd_ac97_update);
2535 EXPORT_SYMBOL(snd_ac97_update_bits);
2536 EXPORT_SYMBOL(snd_ac97_get_short_name);
2537 EXPORT_SYMBOL(snd_ac97_bus);
2538 EXPORT_SYMBOL(snd_ac97_mixer);
2539 EXPORT_SYMBOL(snd_ac97_pcm_assign);
2540 EXPORT_SYMBOL(snd_ac97_pcm_open);
2541 EXPORT_SYMBOL(snd_ac97_pcm_close);
2542 EXPORT_SYMBOL(snd_ac97_pcm_double_rate_rules);
2543 EXPORT_SYMBOL(snd_ac97_tune_hardware);
2544 EXPORT_SYMBOL(snd_ac97_set_rate);
2546 EXPORT_SYMBOL(snd_ac97_resume);
2547 EXPORT_SYMBOL(snd_ac97_suspend);
2554 static int __init alsa_ac97_init(void)
2559 static void __exit alsa_ac97_exit(void)
2563 module_init(alsa_ac97_init)
2564 module_exit(alsa_ac97_exit)