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