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