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