kconfig: fix default value for choice input
[pandora-kernel.git] / sound / oss / ac97_codec.c
1 /*
2  * ac97_codec.c: Generic AC97 mixer/modem module
3  *
4  * Derived from ac97 mixer in maestro and trident driver.
5  *
6  * Copyright 2000 Silicon Integrated System Corporation
7  *
8  *      This program is free software; you can redistribute it and/or modify
9  *      it under the terms of the GNU General Public License as published by
10  *      the Free Software Foundation; either version 2 of the License, or
11  *      (at your option) any later version.
12  *
13  *      This program is distributed in the hope that it will be useful,
14  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *      GNU General Public License for more details.
17  *
18  *      You should have received a copy of the GNU General Public License
19  *      along with this program; if not, write to the Free Software
20  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  **************************************************************************
23  *
24  * The Intel Audio Codec '97 specification is available at the Intel
25  * audio homepage: http://developer.intel.com/ial/scalableplatforms/audio/
26  *
27  * The specification itself is currently available at:
28  * ftp://download.intel.com/ial/scalableplatforms/ac97r22.pdf
29  *
30  **************************************************************************
31  *
32  * History
33  * May 02, 2003 Liam Girdwood <liam.girdwood@wolfsonmicro.com>
34  *      Removed non existant WM9700
35  *      Added support for WM9705, WM9708, WM9709, WM9710, WM9711
36  *      WM9712 and WM9717
37  * Mar 28, 2002 Randolph Bentson <bentson@holmsjoen.com>
38  *      corrections to support WM9707 in ViewPad 1000
39  * v0.4 Mar 15 2000 Ollie Lho
40  *      dual codecs support verified with 4 channels output
41  * v0.3 Feb 22 2000 Ollie Lho
42  *      bug fix for record mask setting
43  * v0.2 Feb 10 2000 Ollie Lho
44  *      add ac97_read_proc for /proc/driver/{vendor}/ac97
45  * v0.1 Jan 14 2000 Ollie Lho <ollie@sis.com.tw> 
46  *      Isolated from trident.c to support multiple ac97 codec
47  */
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/slab.h>
51 #include <linux/string.h>
52 #include <linux/errno.h>
53 #include <linux/bitops.h>
54 #include <linux/delay.h>
55 #include <linux/pci.h>
56 #include <linux/ac97_codec.h>
57 #include <asm/uaccess.h>
58 #include <linux/mutex.h>
59
60 #define CODEC_ID_BUFSZ 14
61
62 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel);
63 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel, 
64                              unsigned int left, unsigned int right);
65 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val );
66 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask);
67 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg);
68
69 static int ac97_init_mixer(struct ac97_codec *codec);
70
71 static int wolfson_init03(struct ac97_codec * codec);
72 static int wolfson_init04(struct ac97_codec * codec);
73 static int wolfson_init05(struct ac97_codec * codec);
74 static int wolfson_init11(struct ac97_codec * codec);
75 static int wolfson_init13(struct ac97_codec * codec);
76 static int tritech_init(struct ac97_codec * codec);
77 static int tritech_maestro_init(struct ac97_codec * codec);
78 static int sigmatel_9708_init(struct ac97_codec *codec);
79 static int sigmatel_9721_init(struct ac97_codec *codec);
80 static int sigmatel_9744_init(struct ac97_codec *codec);
81 static int ad1886_init(struct ac97_codec *codec);
82 static int eapd_control(struct ac97_codec *codec, int);
83 static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
84 static int cmedia_init(struct ac97_codec * codec);
85 static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
86 static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode);
87
88
89 /*
90  *      AC97 operations.
91  *
92  *      If you are adding a codec then you should be able to use
93  *              eapd_ops - any codec that supports EAPD amp control (most)
94  *              null_ops - any ancient codec that supports nothing
95  *
96  *      The three functions are
97  *              init - used for non AC97 standard initialisation
98  *              amplifier - used to do amplifier control (1=on 0=off)
99  *              digital - switch to digital modes (0 = analog)
100  *
101  *      Not all codecs support all features, not all drivers use all the
102  *      operations yet
103  */
104  
105 static struct ac97_ops null_ops = { NULL, NULL, NULL };
106 static struct ac97_ops default_ops = { NULL, eapd_control, NULL };
107 static struct ac97_ops default_digital_ops = { NULL, eapd_control, generic_digital_control};
108 static struct ac97_ops wolfson_ops03 = { wolfson_init03, NULL, NULL };
109 static struct ac97_ops wolfson_ops04 = { wolfson_init04, NULL, NULL };
110 static struct ac97_ops wolfson_ops05 = { wolfson_init05, NULL, NULL };
111 static struct ac97_ops wolfson_ops11 = { wolfson_init11, NULL, NULL };
112 static struct ac97_ops wolfson_ops13 = { wolfson_init13, NULL, NULL };
113 static struct ac97_ops tritech_ops = { tritech_init, NULL, NULL };
114 static struct ac97_ops tritech_m_ops = { tritech_maestro_init, NULL, NULL };
115 static struct ac97_ops sigmatel_9708_ops = { sigmatel_9708_init, NULL, NULL };
116 static struct ac97_ops sigmatel_9721_ops = { sigmatel_9721_init, NULL, NULL };
117 static struct ac97_ops sigmatel_9744_ops = { sigmatel_9744_init, NULL, NULL };
118 static struct ac97_ops crystal_digital_ops = { NULL, eapd_control, crystal_digital_control };
119 static struct ac97_ops ad1886_ops = { ad1886_init, eapd_control, NULL };
120 static struct ac97_ops cmedia_ops = { NULL, eapd_control, NULL};
121 static struct ac97_ops cmedia_digital_ops = { cmedia_init, eapd_control, cmedia_digital_control};
122
123 /* sorted by vendor/device id */
124 static const struct {
125         u32 id;
126         char *name;
127         struct ac97_ops *ops;
128         int flags;
129 } ac97_codec_ids[] = {
130         {0x41445303, "Analog Devices AD1819",   &null_ops},
131         {0x41445340, "Analog Devices AD1881",   &null_ops},
132         {0x41445348, "Analog Devices AD1881A",  &null_ops},
133         {0x41445360, "Analog Devices AD1885",   &default_ops},
134         {0x41445361, "Analog Devices AD1886",   &ad1886_ops},
135         {0x41445370, "Analog Devices AD1981",   &null_ops},
136         {0x41445372, "Analog Devices AD1981A",  &null_ops},
137         {0x41445374, "Analog Devices AD1981B",  &null_ops},
138         {0x41445460, "Analog Devices AD1885",   &default_ops},
139         {0x41445461, "Analog Devices AD1886",   &ad1886_ops},
140         {0x414B4D00, "Asahi Kasei AK4540",      &null_ops},
141         {0x414B4D01, "Asahi Kasei AK4542",      &null_ops},
142         {0x414B4D02, "Asahi Kasei AK4543",      &null_ops},
143         {0x414C4326, "ALC100P",                 &null_ops},
144         {0x414C4710, "ALC200/200P",             &null_ops},
145         {0x414C4720, "ALC650",                  &default_digital_ops},
146         {0x434D4941, "CMedia",                  &cmedia_ops,            AC97_NO_PCM_VOLUME },
147         {0x434D4942, "CMedia",                  &cmedia_ops,            AC97_NO_PCM_VOLUME },
148         {0x434D4961, "CMedia",                  &cmedia_digital_ops,    AC97_NO_PCM_VOLUME },
149         {0x43525900, "Cirrus Logic CS4297",     &default_ops},
150         {0x43525903, "Cirrus Logic CS4297",     &default_ops},
151         {0x43525913, "Cirrus Logic CS4297A rev A", &default_ops},
152         {0x43525914, "Cirrus Logic CS4297A rev B", &default_ops},
153         {0x43525923, "Cirrus Logic CS4298",     &null_ops},
154         {0x4352592B, "Cirrus Logic CS4294",     &null_ops},
155         {0x4352592D, "Cirrus Logic CS4294",     &null_ops},
156         {0x43525931, "Cirrus Logic CS4299 rev A", &crystal_digital_ops},
157         {0x43525933, "Cirrus Logic CS4299 rev C", &crystal_digital_ops},
158         {0x43525934, "Cirrus Logic CS4299 rev D", &crystal_digital_ops},
159         {0x43585430, "CXT48",                   &default_ops,           AC97_DELUDED_MODEM },
160         {0x43585442, "CXT66",                   &default_ops,           AC97_DELUDED_MODEM },
161         {0x44543031, "Diamond Technology DT0893", &default_ops},
162         {0x45838308, "ESS Allegro ES1988",      &null_ops},
163         {0x49434511, "ICE1232",                 &null_ops}, /* I hope --jk */
164         {0x4e534331, "National Semiconductor LM4549", &null_ops},
165         {0x53494c22, "Silicon Laboratory Si3036", &null_ops},
166         {0x53494c23, "Silicon Laboratory Si3038", &null_ops},
167         {0x545200FF, "TriTech TR?????",         &tritech_m_ops},
168         {0x54524102, "TriTech TR28022",         &null_ops},
169         {0x54524103, "TriTech TR28023",         &null_ops},
170         {0x54524106, "TriTech TR28026",         &null_ops},
171         {0x54524108, "TriTech TR28028",         &tritech_ops},
172         {0x54524123, "TriTech TR A5",           &null_ops},
173         {0x574D4C03, "Wolfson WM9703/07/08/17", &wolfson_ops03},
174         {0x574D4C04, "Wolfson WM9704M/WM9704Q", &wolfson_ops04},
175         {0x574D4C05, "Wolfson WM9705/WM9710",   &wolfson_ops05},
176         {0x574D4C09, "Wolfson WM9709",          &null_ops},
177         {0x574D4C12, "Wolfson WM9711/9712",     &wolfson_ops11},
178         {0x574D4C13, "Wolfson WM9713",  &wolfson_ops13, AC97_DEFAULT_POWER_OFF},
179         {0x83847600, "SigmaTel STAC????",       &null_ops},
180         {0x83847604, "SigmaTel STAC9701/3/4/5", &null_ops},
181         {0x83847605, "SigmaTel STAC9704",       &null_ops},
182         {0x83847608, "SigmaTel STAC9708",       &sigmatel_9708_ops},
183         {0x83847609, "SigmaTel STAC9721/23",    &sigmatel_9721_ops},
184         {0x83847644, "SigmaTel STAC9744/45",    &sigmatel_9744_ops},
185         {0x83847652, "SigmaTel STAC9752/53",    &default_ops},
186         {0x83847656, "SigmaTel STAC9756/57",    &sigmatel_9744_ops},
187         {0x83847666, "SigmaTel STAC9750T",      &sigmatel_9744_ops},
188         {0x83847684, "SigmaTel STAC9783/84?",   &null_ops},
189         {0x57454301, "Winbond 83971D",          &null_ops},
190 };
191
192 static const char *ac97_stereo_enhancements[] =
193 {
194         /*   0 */ "No 3D Stereo Enhancement",
195         /*   1 */ "Analog Devices Phat Stereo",
196         /*   2 */ "Creative Stereo Enhancement",
197         /*   3 */ "National Semi 3D Stereo Enhancement",
198         /*   4 */ "YAMAHA Ymersion",
199         /*   5 */ "BBE 3D Stereo Enhancement",
200         /*   6 */ "Crystal Semi 3D Stereo Enhancement",
201         /*   7 */ "Qsound QXpander",
202         /*   8 */ "Spatializer 3D Stereo Enhancement",
203         /*   9 */ "SRS 3D Stereo Enhancement",
204         /*  10 */ "Platform Tech 3D Stereo Enhancement",
205         /*  11 */ "AKM 3D Audio",
206         /*  12 */ "Aureal Stereo Enhancement",
207         /*  13 */ "Aztech 3D Enhancement",
208         /*  14 */ "Binaura 3D Audio Enhancement",
209         /*  15 */ "ESS Technology Stereo Enhancement",
210         /*  16 */ "Harman International VMAx",
211         /*  17 */ "Nvidea 3D Stereo Enhancement",
212         /*  18 */ "Philips Incredible Sound",
213         /*  19 */ "Texas Instruments 3D Stereo Enhancement",
214         /*  20 */ "VLSI Technology 3D Stereo Enhancement",
215         /*  21 */ "TriTech 3D Stereo Enhancement",
216         /*  22 */ "Realtek 3D Stereo Enhancement",
217         /*  23 */ "Samsung 3D Stereo Enhancement",
218         /*  24 */ "Wolfson Microelectronics 3D Enhancement",
219         /*  25 */ "Delta Integration 3D Enhancement",
220         /*  26 */ "SigmaTel 3D Enhancement",
221         /*  27 */ "Winbond 3D Stereo Enhancement",
222         /*  28 */ "Rockwell 3D Stereo Enhancement",
223         /*  29 */ "Reserved 29",
224         /*  30 */ "Reserved 30",
225         /*  31 */ "Reserved 31"
226 };
227
228 /* this table has default mixer values for all OSS mixers. */
229 static struct mixer_defaults {
230         int mixer;
231         unsigned int value;
232 } mixer_defaults[SOUND_MIXER_NRDEVICES] = {
233         /* all values 0 -> 100 in bytes */
234         {SOUND_MIXER_VOLUME,    0x4343},
235         {SOUND_MIXER_BASS,      0x4343},
236         {SOUND_MIXER_TREBLE,    0x4343},
237         {SOUND_MIXER_PCM,       0x4343},
238         {SOUND_MIXER_SPEAKER,   0x4343},
239         {SOUND_MIXER_LINE,      0x4343},
240         {SOUND_MIXER_MIC,       0x0000},
241         {SOUND_MIXER_CD,        0x4343},
242         {SOUND_MIXER_ALTPCM,    0x4343},
243         {SOUND_MIXER_IGAIN,     0x4343},
244         {SOUND_MIXER_LINE1,     0x4343},
245         {SOUND_MIXER_PHONEIN,   0x4343},
246         {SOUND_MIXER_PHONEOUT,  0x4343},
247         {SOUND_MIXER_VIDEO,     0x4343},
248         {-1,0}
249 };
250
251 /* table to scale scale from OSS mixer value to AC97 mixer register value */    
252 static struct ac97_mixer_hw {
253         unsigned char offset;
254         int scale;
255 } ac97_hw[SOUND_MIXER_NRDEVICES]= {
256         [SOUND_MIXER_VOLUME]    =       {AC97_MASTER_VOL_STEREO,64},
257         [SOUND_MIXER_BASS]      =       {AC97_MASTER_TONE,      16},
258         [SOUND_MIXER_TREBLE]    =       {AC97_MASTER_TONE,      16},
259         [SOUND_MIXER_PCM]       =       {AC97_PCMOUT_VOL,       32},
260         [SOUND_MIXER_SPEAKER]   =       {AC97_PCBEEP_VOL,       16},
261         [SOUND_MIXER_LINE]      =       {AC97_LINEIN_VOL,       32},
262         [SOUND_MIXER_MIC]       =       {AC97_MIC_VOL,          32},
263         [SOUND_MIXER_CD]        =       {AC97_CD_VOL,           32},
264         [SOUND_MIXER_ALTPCM]    =       {AC97_HEADPHONE_VOL,    64},
265         [SOUND_MIXER_IGAIN]     =       {AC97_RECORD_GAIN,      16},
266         [SOUND_MIXER_LINE1]     =       {AC97_AUX_VOL,          32},
267         [SOUND_MIXER_PHONEIN]   =       {AC97_PHONE_VOL,        32},
268         [SOUND_MIXER_PHONEOUT]  =       {AC97_MASTER_VOL_MONO,  64},
269         [SOUND_MIXER_VIDEO]     =       {AC97_VIDEO_VOL,        32},
270 };
271
272 /* the following tables allow us to go from OSS <-> ac97 quickly. */
273 enum ac97_recsettings {
274         AC97_REC_MIC=0,
275         AC97_REC_CD,
276         AC97_REC_VIDEO,
277         AC97_REC_AUX,
278         AC97_REC_LINE,
279         AC97_REC_STEREO, /* combination of all enabled outputs..  */
280         AC97_REC_MONO,        /*.. or the mono equivalent */
281         AC97_REC_PHONE
282 };
283
284 static const unsigned int ac97_rm2oss[] = {
285         [AC97_REC_MIC]   = SOUND_MIXER_MIC,
286         [AC97_REC_CD]    = SOUND_MIXER_CD,
287         [AC97_REC_VIDEO] = SOUND_MIXER_VIDEO,
288         [AC97_REC_AUX]   = SOUND_MIXER_LINE1,
289         [AC97_REC_LINE]  = SOUND_MIXER_LINE,
290         [AC97_REC_STEREO]= SOUND_MIXER_IGAIN,
291         [AC97_REC_PHONE] = SOUND_MIXER_PHONEIN
292 };
293
294 /* indexed by bit position */
295 static const unsigned int ac97_oss_rm[] = {
296         [SOUND_MIXER_MIC]       = AC97_REC_MIC,
297         [SOUND_MIXER_CD]        = AC97_REC_CD,
298         [SOUND_MIXER_VIDEO]     = AC97_REC_VIDEO,
299         [SOUND_MIXER_LINE1]     = AC97_REC_AUX,
300         [SOUND_MIXER_LINE]      = AC97_REC_LINE,
301         [SOUND_MIXER_IGAIN]     = AC97_REC_STEREO,
302         [SOUND_MIXER_PHONEIN]   = AC97_REC_PHONE
303 };
304
305 static LIST_HEAD(codecs);
306 static LIST_HEAD(codec_drivers);
307 static DEFINE_MUTEX(codec_mutex);
308
309 /* reads the given OSS mixer from the ac97 the caller must have insured that the ac97 knows
310    about that given mixer, and should be holding a spinlock for the card */
311 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel) 
312 {
313         u16 val;
314         int ret = 0;
315         int scale;
316         struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
317
318         val = codec->codec_read(codec , mh->offset);
319
320         if (val & AC97_MUTE) {
321                 ret = 0;
322         } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
323                 /* nice stereo mixers .. */
324                 int left,right;
325
326                 left = (val >> 8)  & 0x7f;
327                 right = val  & 0x7f;
328
329                 if (oss_channel == SOUND_MIXER_IGAIN) {
330                         right = (right * 100) / mh->scale;
331                         left = (left * 100) / mh->scale;
332                 } else {
333                         /* these may have 5 or 6 bit resolution */
334                         if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
335                                 scale = (1 << codec->bit_resolution);
336                         else
337                                 scale = mh->scale;
338
339                         right = 100 - ((right * 100) / scale);
340                         left = 100 - ((left * 100) / scale);
341                 }
342                 ret = left | (right << 8);
343         } else if (oss_channel == SOUND_MIXER_SPEAKER) {
344                 ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
345         } else if (oss_channel == SOUND_MIXER_PHONEIN) {
346                 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
347         } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
348                 scale = (1 << codec->bit_resolution);
349                 ret = 100 - (((val & 0x1f) * 100) / scale);
350         } else if (oss_channel == SOUND_MIXER_MIC) {
351                 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
352                 /*  the low bit is optional in the tone sliders and masking
353                     it lets us avoid the 0xf 'bypass'.. */
354         } else if (oss_channel == SOUND_MIXER_BASS) {
355                 ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
356         } else if (oss_channel == SOUND_MIXER_TREBLE) {
357                 ret = 100 - (((val & 0xe) * 100) / mh->scale);
358         }
359
360 #ifdef DEBUG
361         printk("ac97_codec: read OSS mixer %2d (%s ac97 register 0x%02x), "
362                "0x%04x -> 0x%04x\n",
363                oss_channel, codec->id ? "Secondary" : "Primary",
364                mh->offset, val, ret);
365 #endif
366
367         return ret;
368 }
369
370 /* write the OSS encoded volume to the given OSS encoded mixer, again caller's job to
371    make sure all is well in arg land, call with spinlock held */
372 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
373                       unsigned int left, unsigned int right)
374 {
375         u16 val = 0;
376         int scale;
377         struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
378
379 #ifdef DEBUG
380         printk("ac97_codec: wrote OSS mixer %2d (%s ac97 register 0x%02x), "
381                "left vol:%2d, right vol:%2d:",
382                oss_channel, codec->id ? "Secondary" : "Primary",
383                mh->offset, left, right);
384 #endif
385
386         if (AC97_STEREO_MASK & (1 << oss_channel)) {
387                 /* stereo mixers */
388                 if (left == 0 && right == 0) {
389                         val = AC97_MUTE;
390                 } else {
391                         if (oss_channel == SOUND_MIXER_IGAIN) {
392                                 right = (right * mh->scale) / 100;
393                                 left = (left * mh->scale) / 100;
394                                 if (right >= mh->scale)
395                                         right = mh->scale-1;
396                                 if (left >= mh->scale)
397                                         left = mh->scale-1;
398                         } else {
399                                 /* these may have 5 or 6 bit resolution */
400                                 if (oss_channel == SOUND_MIXER_VOLUME ||
401                                     oss_channel == SOUND_MIXER_ALTPCM)
402                                         scale = (1 << codec->bit_resolution);
403                                 else
404                                         scale = mh->scale;
405
406                                 right = ((100 - right) * scale) / 100;
407                                 left = ((100 - left) * scale) / 100;
408                                 if (right >= scale)
409                                         right = scale-1;
410                                 if (left >= scale)
411                                         left = scale-1;
412                         }
413                         val = (left << 8) | right;
414                 }
415         } else if (oss_channel == SOUND_MIXER_BASS) {
416                 val = codec->codec_read(codec , mh->offset) & ~0x0f00;
417                 left = ((100 - left) * mh->scale) / 100;
418                 if (left >= mh->scale)
419                         left = mh->scale-1;
420                 val |= (left << 8) & 0x0e00;
421         } else if (oss_channel == SOUND_MIXER_TREBLE) {
422                 val = codec->codec_read(codec , mh->offset) & ~0x000f;
423                 left = ((100 - left) * mh->scale) / 100;
424                 if (left >= mh->scale)
425                         left = mh->scale-1;
426                 val |= left & 0x000e;
427         } else if(left == 0) {
428                 val = AC97_MUTE;
429         } else if (oss_channel == SOUND_MIXER_SPEAKER) {
430                 left = ((100 - left) * mh->scale) / 100;
431                 if (left >= mh->scale)
432                         left = mh->scale-1;
433                 val = left << 1;
434         } else if (oss_channel == SOUND_MIXER_PHONEIN) {
435                 left = ((100 - left) * mh->scale) / 100;
436                 if (left >= mh->scale)
437                         left = mh->scale-1;
438                 val = left;
439         } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
440                 scale = (1 << codec->bit_resolution);
441                 left = ((100 - left) * scale) / 100;
442                 if (left >= mh->scale)
443                         left = mh->scale-1;
444                 val = left;
445         } else if (oss_channel == SOUND_MIXER_MIC) {
446                 val = codec->codec_read(codec , mh->offset) & ~0x801f;
447                 left = ((100 - left) * mh->scale) / 100;
448                 if (left >= mh->scale)
449                         left = mh->scale-1;
450                 val |= left;
451                 /*  the low bit is optional in the tone sliders and masking
452                     it lets us avoid the 0xf 'bypass'.. */
453         }
454 #ifdef DEBUG
455         printk(" 0x%04x", val);
456 #endif
457
458         codec->codec_write(codec, mh->offset, val);
459
460 #ifdef DEBUG
461         val = codec->codec_read(codec, mh->offset);
462         printk(" -> 0x%04x\n", val);
463 #endif
464 }
465
466 /* a thin wrapper for write_mixer */
467 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val ) 
468 {
469         unsigned int left,right;
470
471         /* cleanse input a little */
472         right = ((val >> 8)  & 0xff) ;
473         left = (val  & 0xff) ;
474
475         if (right > 100) right = 100;
476         if (left > 100) left = 100;
477
478         codec->mixer_state[oss_mixer] = (right << 8) | left;
479         codec->write_mixer(codec, oss_mixer, left, right);
480 }
481
482 /* read or write the recmask, the ac97 can really have left and right recording
483    inputs independantly set, but OSS doesn't seem to want us to express that to
484    the user. the caller guarantees that we have a supported bit set, and they
485    must be holding the card's spinlock */
486 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask) 
487 {
488         unsigned int val;
489
490         if (rw) {
491                 /* read it from the card */
492                 val = codec->codec_read(codec, AC97_RECORD_SELECT);
493 #ifdef DEBUG
494                 printk("ac97_codec: ac97 recmask to set to 0x%04x\n", val);
495 #endif
496                 return (1 << ac97_rm2oss[val & 0x07]);
497         }
498
499         /* else, write the first set in the mask as the
500            output */    
501         /* clear out current set value first (AC97 supports only 1 input!) */
502         val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
503         if (mask != val)
504             mask &= ~val;
505        
506         val = ffs(mask); 
507         val = ac97_oss_rm[val-1];
508         val |= val << 8;  /* set both channels */
509
510 #ifdef DEBUG
511         printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
512 #endif
513
514         codec->codec_write(codec, AC97_RECORD_SELECT, val);
515
516         return 0;
517 };
518
519 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
520 {
521         int i, val = 0;
522
523         if (cmd == SOUND_MIXER_INFO) {
524                 mixer_info info;
525                 memset(&info, 0, sizeof(info));
526                 strlcpy(info.id, codec->name, sizeof(info.id));
527                 strlcpy(info.name, codec->name, sizeof(info.name));
528                 info.modify_counter = codec->modcnt;
529                 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
530                         return -EFAULT;
531                 return 0;
532         }
533         if (cmd == SOUND_OLD_MIXER_INFO) {
534                 _old_mixer_info info;
535                 memset(&info, 0, sizeof(info));
536                 strlcpy(info.id, codec->name, sizeof(info.id));
537                 strlcpy(info.name, codec->name, sizeof(info.name));
538                 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
539                         return -EFAULT;
540                 return 0;
541         }
542
543         if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
544                 return -EINVAL;
545
546         if (cmd == OSS_GETVERSION)
547                 return put_user(SOUND_VERSION, (int __user *)arg);
548
549         if (_SIOC_DIR(cmd) == _SIOC_READ) {
550                 switch (_IOC_NR(cmd)) {
551                 case SOUND_MIXER_RECSRC: /* give them the current record source */
552                         if (!codec->recmask_io) {
553                                 val = 0;
554                         } else {
555                                 val = codec->recmask_io(codec, 1, 0);
556                         }
557                         break;
558
559                 case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
560                         val = codec->supported_mixers;
561                         break;
562
563                 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
564                         val = codec->record_sources;
565                         break;
566
567                 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
568                         val = codec->stereo_mixers;
569                         break;
570
571                 case SOUND_MIXER_CAPS:
572                         val = SOUND_CAP_EXCL_INPUT;
573                         break;
574
575                 default: /* read a specific mixer */
576                         i = _IOC_NR(cmd);
577
578                         if (!supported_mixer(codec, i)) 
579                                 return -EINVAL;
580
581                         /* do we ever want to touch the hardware? */
582                         /* val = codec->read_mixer(codec, i); */
583                         val = codec->mixer_state[i];
584                         break;
585                 }
586                 return put_user(val, (int __user *)arg);
587         }
588
589         if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
590                 codec->modcnt++;
591                 if (get_user(val, (int __user *)arg))
592                         return -EFAULT;
593
594                 switch (_IOC_NR(cmd)) {
595                 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
596                         if (!codec->recmask_io) return -EINVAL;
597                         if (!val) return 0;
598                         if (!(val &= codec->record_sources)) return -EINVAL;
599
600                         codec->recmask_io(codec, 0, val);
601
602                         return 0;
603                 default: /* write a specific mixer */
604                         i = _IOC_NR(cmd);
605
606                         if (!supported_mixer(codec, i)) 
607                                 return -EINVAL;
608
609                         ac97_set_mixer(codec, i, val);
610
611                         return 0;
612                 }
613         }
614         return -EINVAL;
615 }
616
617 /* entry point for /proc/driver/controller_vendor/ac97/%d */
618 int ac97_read_proc (char *page, char **start, off_t off,
619                     int count, int *eof, void *data)
620 {
621         int len = 0, cap, extid, val, id1, id2;
622         struct ac97_codec *codec;
623         int is_ac97_20 = 0;
624
625         if ((codec = data) == NULL)
626                 return -ENODEV;
627
628         id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
629         id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
630         len += sprintf (page+len, "Vendor name      : %s\n", codec->name);
631         len += sprintf (page+len, "Vendor id        : %04X %04X\n", id1, id2);
632
633         extid = codec->codec_read(codec, AC97_EXTENDED_ID);
634         extid &= ~((1<<2)|(1<<4)|(1<<5)|(1<<10)|(1<<11)|(1<<12)|(1<<13));
635         len += sprintf (page+len, "AC97 Version     : %s\n",
636                         extid ? "2.0 or later" : "1.0");
637         if (extid) is_ac97_20 = 1;
638
639         cap = codec->codec_read(codec, AC97_RESET);
640         len += sprintf (page+len, "Capabilities     :%s%s%s%s%s%s\n",
641                         cap & 0x0001 ? " -dedicated MIC PCM IN channel-" : "",
642                         cap & 0x0002 ? " -reserved1-" : "",
643                         cap & 0x0004 ? " -bass & treble-" : "",
644                         cap & 0x0008 ? " -simulated stereo-" : "",
645                         cap & 0x0010 ? " -headphone out-" : "",
646                         cap & 0x0020 ? " -loudness-" : "");
647         val = cap & 0x00c0;
648         len += sprintf (page+len, "DAC resolutions  :%s%s%s\n",
649                         " -16-bit-",
650                         val & 0x0040 ? " -18-bit-" : "",
651                         val & 0x0080 ? " -20-bit-" : "");
652         val = cap & 0x0300;
653         len += sprintf (page+len, "ADC resolutions  :%s%s%s\n",
654                         " -16-bit-",
655                         val & 0x0100 ? " -18-bit-" : "",
656                         val & 0x0200 ? " -20-bit-" : "");
657         len += sprintf (page+len, "3D enhancement   : %s\n",
658                         ac97_stereo_enhancements[(cap >> 10) & 0x1f]);
659
660         val = codec->codec_read(codec, AC97_GENERAL_PURPOSE);
661         len += sprintf (page+len, "POP path         : %s 3D\n"
662                         "Sim. stereo      : %s\n"
663                         "3D enhancement   : %s\n"
664                         "Loudness         : %s\n"
665                         "Mono output      : %s\n"
666                         "MIC select       : %s\n"
667                         "ADC/DAC loopback : %s\n",
668                         val & 0x8000 ? "post" : "pre",
669                         val & 0x4000 ? "on" : "off",
670                         val & 0x2000 ? "on" : "off",
671                         val & 0x1000 ? "on" : "off",
672                         val & 0x0200 ? "MIC" : "MIX",
673                         val & 0x0100 ? "MIC2" : "MIC1",
674                         val & 0x0080 ? "on" : "off");
675
676         extid = codec->codec_read(codec, AC97_EXTENDED_ID);
677         cap = extid;
678         len += sprintf (page+len, "Ext Capabilities :%s%s%s%s%s%s%s\n",
679                         cap & 0x0001 ? " -var rate PCM audio-" : "",
680                         cap & 0x0002 ? " -2x PCM audio out-" : "",
681                         cap & 0x0008 ? " -var rate MIC in-" : "",
682                         cap & 0x0040 ? " -PCM center DAC-" : "",
683                         cap & 0x0080 ? " -PCM surround DAC-" : "",
684                         cap & 0x0100 ? " -PCM LFE DAC-" : "",
685                         cap & 0x0200 ? " -slot/DAC mappings-" : "");
686         if (is_ac97_20) {
687                 len += sprintf (page+len, "Front DAC rate   : %d\n",
688                                 codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE));
689         }
690
691         return len;
692 }
693
694 /**
695  *      codec_id        -  Turn id1/id2 into a PnP string
696  *      @id1: Vendor ID1
697  *      @id2: Vendor ID2
698  *      @buf: CODEC_ID_BUFSZ byte buffer
699  *
700  *      Fills buf with a zero terminated PnP ident string for the id1/id2
701  *      pair. For convenience the return is the passed in buffer pointer.
702  */
703  
704 static char *codec_id(u16 id1, u16 id2, char *buf)
705 {
706         if(id1&0x8080) {
707                 snprintf(buf, CODEC_ID_BUFSZ, "0x%04x:0x%04x", id1, id2);
708         } else {
709                 buf[0] = (id1 >> 8);
710                 buf[1] = (id1 & 0xFF);
711                 buf[2] = (id2 >> 8);
712                 snprintf(buf+3, CODEC_ID_BUFSZ - 3, "%d", id2&0xFF);
713         }
714         return buf;
715 }
716  
717 /**
718  *      ac97_check_modem - Check if the Codec is a modem
719  *      @codec: codec to check
720  *
721  *      Return true if the device is an AC97 1.0 or AC97 2.0 modem
722  */
723  
724 static int ac97_check_modem(struct ac97_codec *codec)
725 {
726         /* Check for an AC97 1.0 soft modem (ID1) */
727         if(codec->codec_read(codec, AC97_RESET) & 2)
728                 return 1;
729         /* Check for an AC97 2.x soft modem */
730         codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
731         if(codec->codec_read(codec, AC97_EXTENDED_MODEM_ID) & 1)
732                 return 1;
733         return 0;
734 }
735
736
737 /**
738  *      ac97_alloc_codec - Allocate an AC97 codec
739  *
740  *      Returns a new AC97 codec structure. AC97 codecs may become
741  *      refcounted soon so this interface is needed. Returns with
742  *      one reference taken.
743  */
744  
745 struct ac97_codec *ac97_alloc_codec(void)
746 {
747         struct ac97_codec *codec = kmalloc(sizeof(struct ac97_codec), GFP_KERNEL);
748         if(!codec)
749                 return NULL;
750
751         memset(codec, 0, sizeof(*codec));
752         spin_lock_init(&codec->lock);
753         INIT_LIST_HEAD(&codec->list);
754         return codec;
755 }
756
757 EXPORT_SYMBOL(ac97_alloc_codec);
758
759 /**
760  *      ac97_release_codec -    Release an AC97 codec
761  *      @codec: codec to release
762  *
763  *      Release an allocated AC97 codec. This will be refcounted in
764  *      time but for the moment is trivial. Calls the unregister
765  *      handler if the codec is now defunct.
766  */
767  
768 void ac97_release_codec(struct ac97_codec *codec)
769 {
770         /* Remove from the list first, we don't want to be
771            "rediscovered" */
772         mutex_lock(&codec_mutex);
773         list_del(&codec->list);
774         mutex_unlock(&codec_mutex);
775         /*
776          *      The driver needs to deal with internal
777          *      locking to avoid accidents here. 
778          */
779         if(codec->driver)
780                 codec->driver->remove(codec, codec->driver);
781         kfree(codec);
782 }
783
784 EXPORT_SYMBOL(ac97_release_codec);
785
786 /**
787  *      ac97_probe_codec - Initialize and setup AC97-compatible codec
788  *      @codec: (in/out) Kernel info for a single AC97 codec
789  *
790  *      Reset the AC97 codec, then initialize the mixer and
791  *      the rest of the @codec structure.
792  *
793  *      The codec_read and codec_write fields of @codec are
794  *      required to be setup and working when this function
795  *      is called.  All other fields are set by this function.
796  *
797  *      codec_wait field of @codec can optionally be provided
798  *      when calling this function.  If codec_wait is not %NULL,
799  *      this function will call codec_wait any time it is
800  *      necessary to wait for the audio chip to reach the
801  *      codec-ready state.  If codec_wait is %NULL, then
802  *      the default behavior is to call schedule_timeout.
803  *      Currently codec_wait is used to wait for AC97 codec
804  *      reset to complete. 
805  *
806  *     Some codecs will power down when a register reset is
807  *     performed. We now check for such codecs.
808  *
809  *      Returns 1 (true) on success, or 0 (false) on failure.
810  */
811  
812 int ac97_probe_codec(struct ac97_codec *codec)
813 {
814         u16 id1, id2;
815         u16 audio;
816         int i;
817         char cidbuf[CODEC_ID_BUFSZ];
818         u16 f;
819         struct list_head *l;
820         struct ac97_driver *d;
821         
822         /* wait for codec-ready state */
823         if (codec->codec_wait)
824                 codec->codec_wait(codec);
825         else
826                 udelay(10);
827
828         /* will the codec power down if register reset ? */
829         id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
830         id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
831         codec->name = NULL;
832         codec->codec_ops = &null_ops;
833         for (i = 0; i < ARRAY_SIZE(ac97_codec_ids); i++) {
834                 if (ac97_codec_ids[i].id == ((id1 << 16) | id2)) {
835                         codec->type = ac97_codec_ids[i].id;
836                         codec->name = ac97_codec_ids[i].name;
837                         codec->codec_ops = ac97_codec_ids[i].ops;
838                         codec->flags = ac97_codec_ids[i].flags;
839                         break;
840                 }
841         }
842
843         codec->model = (id1 << 16) | id2;
844         if ((codec->flags & AC97_DEFAULT_POWER_OFF) == 0) {
845                 /* reset codec and wait for the ready bit before we continue */
846                 codec->codec_write(codec, AC97_RESET, 0L);
847                 if (codec->codec_wait)
848                         codec->codec_wait(codec);
849                 else
850                         udelay(10);
851         }
852
853         /* probing AC97 codec, AC97 2.0 says that bit 15 of register 0x00 (reset) should
854          * be read zero.
855          *
856          * FIXME: is the following comment outdated?  -jgarzik
857          * Probing of AC97 in this way is not reliable, it is not even SAFE !!
858          */
859         if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
860                 printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
861                        (codec->id & 0x2) ? (codec->id&1 ? "4th" : "Tertiary")
862                        : (codec->id&1 ? "Secondary":  "Primary"));
863                 return 0;
864         }
865         
866         /* probe for Modem Codec */
867         codec->modem = ac97_check_modem(codec);
868
869         /* enable SPDIF */
870         f = codec->codec_read(codec, AC97_EXTENDED_STATUS);
871         if((codec->codec_ops == &null_ops) && (f & 4))
872                 codec->codec_ops = &default_digital_ops;
873         
874         /* A device which thinks its a modem but isnt */
875         if(codec->flags & AC97_DELUDED_MODEM)
876                 codec->modem = 0;
877                 
878         if (codec->name == NULL)
879                 codec->name = "Unknown";
880         printk(KERN_INFO "ac97_codec: AC97 %s codec, id: %s (%s)\n", 
881                 codec->modem ? "Modem" : (audio ? "Audio" : ""),
882                codec_id(id1, id2, cidbuf), codec->name);
883
884         if(!ac97_init_mixer(codec))
885                 return 0;
886                 
887         /* 
888          *      Attach last so the caller can override the mixer
889          *      callbacks.
890          */
891          
892         mutex_lock(&codec_mutex);
893         list_add(&codec->list, &codecs);
894
895         list_for_each(l, &codec_drivers) {
896                 d = list_entry(l, struct ac97_driver, list);
897                 if ((codec->model ^ d->codec_id) & d->codec_mask)
898                         continue;
899                 if(d->probe(codec, d) == 0)
900                 {
901                         codec->driver = d;
902                         break;
903                 }
904         }
905
906         mutex_unlock(&codec_mutex);
907         return 1;
908 }
909
910 static int ac97_init_mixer(struct ac97_codec *codec)
911 {
912         u16 cap;
913         int i;
914
915         cap = codec->codec_read(codec, AC97_RESET);
916
917         /* mixer masks */
918         codec->supported_mixers = AC97_SUPPORTED_MASK;
919         codec->stereo_mixers = AC97_STEREO_MASK;
920         codec->record_sources = AC97_RECORD_MASK;
921         if (!(cap & 0x04))
922                 codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
923         if (!(cap & 0x10))
924                 codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
925
926
927         /* detect bit resolution */
928         codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
929         if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x2020)
930                 codec->bit_resolution = 6;
931         else
932                 codec->bit_resolution = 5;
933
934         /* generic OSS to AC97 wrapper */
935         codec->read_mixer = ac97_read_mixer;
936         codec->write_mixer = ac97_write_mixer;
937         codec->recmask_io = ac97_recmask_io;
938         codec->mixer_ioctl = ac97_mixer_ioctl;
939
940         /* initialize mixer channel volumes */
941         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
942                 struct mixer_defaults *md = &mixer_defaults[i];
943                 if (md->mixer == -1) 
944                         break;
945                 if (!supported_mixer(codec, md->mixer)) 
946                         continue;
947                 ac97_set_mixer(codec, md->mixer, md->value);
948         }
949
950         /* codec specific initialization for 4-6 channel output or secondary codec stuff */
951         if (codec->codec_ops->init != NULL) {
952                 codec->codec_ops->init(codec);
953         }
954
955         /*
956          *      Volume is MUTE only on this device. We have to initialise
957          *      it but its useless beyond that.
958          */
959         if(codec->flags & AC97_NO_PCM_VOLUME)
960         {
961                 codec->supported_mixers &= ~SOUND_MASK_PCM;
962                 printk(KERN_WARNING "AC97 codec does not have proper volume support.\n");
963         }
964         return 1;
965 }
966
967 #define AC97_SIGMATEL_ANALOG    0x6c    /* Analog Special */
968 #define AC97_SIGMATEL_DAC2INVERT 0x6e
969 #define AC97_SIGMATEL_BIAS1     0x70
970 #define AC97_SIGMATEL_BIAS2     0x72
971 #define AC97_SIGMATEL_MULTICHN  0x74    /* Multi-Channel programming */
972 #define AC97_SIGMATEL_CIC1      0x76
973 #define AC97_SIGMATEL_CIC2      0x78
974
975
976 static int sigmatel_9708_init(struct ac97_codec * codec)
977 {
978         u16 codec72, codec6c;
979
980         codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
981         codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
982
983         if ((codec72==0) && (codec6c==0)) {
984                 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
985                 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
986                 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
987                 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
988         } else if ((codec72==0x8000) && (codec6c==0)) {
989                 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
990                 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
991                 codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
992         } else if ((codec72==0x8000) && (codec6c==0x0080)) {
993                 /* nothing */
994         }
995         codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
996         return 0;
997 }
998
999
1000 static int sigmatel_9721_init(struct ac97_codec * codec)
1001 {
1002         /* Only set up secondary codec */
1003         if (codec->id == 0)
1004                 return 0;
1005
1006         codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
1007
1008         /* initialize SigmaTel STAC9721/23 as secondary codec, decoding AC link
1009            sloc 3,4 = 0x01, slot 7,8 = 0x00, */
1010         codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
1011
1012         /* we don't have the crystal when we are on an AMR card, so use
1013            BIT_CLK as our clock source. Write the magic word ABBA and read
1014            back to enable register 0x78 */
1015         codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
1016         codec->codec_read(codec, AC97_SIGMATEL_CIC1);
1017
1018         /* sync all the clocks*/
1019         codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
1020
1021         return 0;
1022 }
1023
1024
1025 static int sigmatel_9744_init(struct ac97_codec * codec)
1026 {
1027         // patch for SigmaTel
1028         codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
1029         codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000); // is this correct? --jk
1030         codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
1031         codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
1032         codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
1033         return 0;
1034 }
1035
1036 static int cmedia_init(struct ac97_codec *codec)
1037 {
1038         /* Initialise the CMedia 9739 */
1039         /*
1040                 We could set various options here
1041                 Register 0x20 bit 0x100 sets mic as center bass
1042                 Also do multi_channel_ctrl &=~0x3000 |=0x1000
1043                 
1044                 For now we set up the GPIO and PC beep 
1045         */
1046         
1047         u16 v;
1048         
1049         /* MIC */
1050         codec->codec_write(codec, 0x64, 0x3000);
1051         v = codec->codec_read(codec, 0x64);
1052         v &= ~0x8000;
1053         codec->codec_write(codec, 0x64, v);
1054         codec->codec_write(codec, 0x70, 0x0100);
1055         codec->codec_write(codec, 0x72, 0x0020);
1056         return 0;
1057 }
1058         
1059 #define AC97_WM97XX_FMIXER_VOL 0x72
1060 #define AC97_WM97XX_RMIXER_VOL 0x74
1061 #define AC97_WM97XX_TEST 0x5a
1062 #define AC97_WM9704_RPCM_VOL 0x70
1063 #define AC97_WM9711_OUT3VOL 0x16
1064
1065 static int wolfson_init03(struct ac97_codec * codec)
1066 {
1067         /* this is known to work for the ViewSonic ViewPad 1000 */
1068         codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
1069         codec->codec_write(codec, AC97_GENERAL_PURPOSE, 0x8000);
1070         return 0;
1071 }
1072
1073 static int wolfson_init04(struct ac97_codec * codec)
1074 {
1075         codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
1076         codec->codec_write(codec, AC97_WM97XX_RMIXER_VOL, 0x0808);
1077
1078         // patch for DVD noise
1079         codec->codec_write(codec, AC97_WM97XX_TEST, 0x0200);
1080
1081         // init vol as PCM vol
1082         codec->codec_write(codec, AC97_WM9704_RPCM_VOL,
1083                 codec->codec_read(codec, AC97_PCMOUT_VOL));
1084
1085         /* set rear surround volume */
1086         codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
1087         return 0;
1088 }
1089
1090 /* WM9705, WM9710 */
1091 static int wolfson_init05(struct ac97_codec * codec)
1092 {
1093         /* set front mixer volume */
1094         codec->codec_write(codec, AC97_WM97XX_FMIXER_VOL, 0x0808);
1095         return 0;
1096 }
1097
1098 /* WM9711, WM9712 */
1099 static int wolfson_init11(struct ac97_codec * codec)
1100 {
1101         /* stop pop's during suspend/resume */
1102         codec->codec_write(codec, AC97_WM97XX_TEST,
1103                 codec->codec_read(codec, AC97_WM97XX_TEST) & 0xffbf);
1104
1105         /* set out3 volume */
1106         codec->codec_write(codec, AC97_WM9711_OUT3VOL, 0x0808);
1107         return 0;
1108 }
1109
1110 /* WM9713 */
1111 static int wolfson_init13(struct ac97_codec * codec)
1112 {
1113         codec->codec_write(codec, AC97_RECORD_GAIN, 0x00a0);
1114         codec->codec_write(codec, AC97_POWER_CONTROL, 0x0000);
1115         codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0xDA00);
1116         codec->codec_write(codec, AC97_EXTEND_MODEM_STAT, 0x3810);
1117         codec->codec_write(codec, AC97_PHONE_VOL, 0x0808);
1118         codec->codec_write(codec, AC97_PCBEEP_VOL, 0x0808);
1119
1120         return 0;
1121 }
1122
1123 static int tritech_init(struct ac97_codec * codec)
1124 {
1125         codec->codec_write(codec, 0x26, 0x0300);
1126         codec->codec_write(codec, 0x26, 0x0000);
1127         codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
1128         codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
1129         return 0;
1130 }
1131
1132
1133 /* copied from drivers/sound/maestro.c */
1134 static int tritech_maestro_init(struct ac97_codec * codec)
1135 {
1136         /* no idea what this does */
1137         codec->codec_write(codec, 0x2A, 0x0001);
1138         codec->codec_write(codec, 0x2C, 0x0000);
1139         codec->codec_write(codec, 0x2C, 0XFFFF);
1140         return 0;
1141 }
1142
1143
1144
1145 /* 
1146  *      Presario700 workaround 
1147  *      for Jack Sense/SPDIF Register mis-setting causing
1148  *      no audible output
1149  *      by Santiago Nullo 04/05/2002
1150  */
1151
1152 #define AC97_AD1886_JACK_SENSE 0x72
1153
1154 static int ad1886_init(struct ac97_codec * codec)
1155 {
1156         /* from AD1886 Specs */
1157         codec->codec_write(codec, AC97_AD1886_JACK_SENSE, 0x0010);
1158         return 0;
1159 }
1160
1161
1162
1163
1164 /*
1165  *      This is basically standard AC97. It should work as a default for
1166  *      almost all modern codecs. Note that some cards wire EAPD *backwards*
1167  *      That side of it is up to the card driver not us to cope with.
1168  *
1169  */
1170
1171 static int eapd_control(struct ac97_codec * codec, int on)
1172 {
1173         if(on)
1174                 codec->codec_write(codec, AC97_POWER_CONTROL,
1175                         codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
1176         else
1177                 codec->codec_write(codec, AC97_POWER_CONTROL,
1178                         codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
1179         return 0;
1180 }
1181
1182 static int generic_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1183 {
1184         u16 reg;
1185         
1186         reg = codec->codec_read(codec, AC97_SPDIF_CONTROL);
1187         
1188         switch(rate)
1189         {
1190                 /* Off by default */
1191                 default:
1192                 case 0:
1193                         reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1194                         codec->codec_write(codec, AC97_EXTENDED_STATUS, (reg & ~AC97_EA_SPDIF));
1195                         if(rate == 0)
1196                                 return 0;
1197                         return -EINVAL;
1198                 case 1:
1199                         reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
1200                         break;
1201                 case 2:
1202                         reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
1203                         break;
1204                 case 3:
1205                         reg = (reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
1206                         break;
1207         }
1208         
1209         reg &= ~AC97_SC_CC_MASK;
1210         reg |= (mode & AUDIO_CCMASK) << 6;
1211         
1212         if(mode & AUDIO_DIGITAL)
1213                 reg |= 2;
1214         if(mode & AUDIO_PRO)
1215                 reg |= 1;
1216         if(mode & AUDIO_DRS)
1217                 reg |= 0x4000;
1218
1219         codec->codec_write(codec, AC97_SPDIF_CONTROL, reg);
1220
1221         reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1222         reg &= (AC97_EA_SLOT_MASK);
1223         reg |= AC97_EA_VRA | AC97_EA_SPDIF | slots;
1224         codec->codec_write(codec, AC97_EXTENDED_STATUS, reg);
1225         
1226         reg = codec->codec_read(codec, AC97_EXTENDED_STATUS);
1227         if(!(reg & 0x0400))
1228         {
1229                 codec->codec_write(codec, AC97_EXTENDED_STATUS, reg & ~ AC97_EA_SPDIF);
1230                 return -EINVAL;
1231         }
1232         return 0;
1233 }
1234
1235 /*
1236  *      Crystal digital audio control (CS4299)
1237  */
1238  
1239 static int crystal_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1240 {
1241         u16 cv;
1242
1243         if(mode & AUDIO_DIGITAL)
1244                 return -EINVAL;
1245                 
1246         switch(rate)
1247         {
1248                 case 0: cv = 0x0; break;        /* SPEN off */
1249                 case 48000: cv = 0x8004; break; /* 48KHz digital */
1250                 case 44100: cv = 0x8104; break; /* 44.1KHz digital */
1251                 case 32768:                     /* 32Khz */
1252                 default:
1253                         return -EINVAL;
1254         }
1255         codec->codec_write(codec, 0x68, cv);
1256         return 0;
1257 }
1258
1259 /*
1260  *      CMedia digital audio control
1261  *      Needs more work.
1262  */
1263  
1264 static int cmedia_digital_control(struct ac97_codec *codec, int slots, int rate, int mode)
1265 {
1266         u16 cv;
1267
1268         if(mode & AUDIO_DIGITAL)
1269                 return -EINVAL;
1270                 
1271         switch(rate)
1272         {
1273                 case 0:         cv = 0x0001; break;     /* SPEN off */
1274                 case 48000:     cv = 0x0009; break;     /* 48KHz digital */
1275                 default:
1276                         return -EINVAL;
1277         }
1278         codec->codec_write(codec, 0x2A, 0x05c4);
1279         codec->codec_write(codec, 0x6C, cv);
1280         
1281         /* Switch on mix to surround */
1282         cv = codec->codec_read(codec, 0x64);
1283         cv &= ~0x0200;
1284         if(mode)
1285                 cv |= 0x0200;
1286         codec->codec_write(codec, 0x64, cv);
1287         return 0;
1288 }
1289
1290
1291 /* copied from drivers/sound/maestro.c */
1292 #if 0  /* there has been 1 person on the planet with a pt101 that we
1293         know of.  If they care, they can put this back in :) */
1294 static int pt101_init(struct ac97_codec * codec)
1295 {
1296         printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
1297         /* who knows.. */
1298         codec->codec_write(codec, 0x2A, 0x0001);
1299         codec->codec_write(codec, 0x2C, 0x0000);
1300         codec->codec_write(codec, 0x2C, 0xFFFF);
1301         codec->codec_write(codec, 0x10, 0x9F1F);
1302         codec->codec_write(codec, 0x12, 0x0808);
1303         codec->codec_write(codec, 0x14, 0x9F1F);
1304         codec->codec_write(codec, 0x16, 0x9F1F);
1305         codec->codec_write(codec, 0x18, 0x0404);
1306         codec->codec_write(codec, 0x1A, 0x0000);
1307         codec->codec_write(codec, 0x1C, 0x0000);
1308         codec->codec_write(codec, 0x02, 0x0404);
1309         codec->codec_write(codec, 0x04, 0x0808);
1310         codec->codec_write(codec, 0x0C, 0x801F);
1311         codec->codec_write(codec, 0x0E, 0x801F);
1312         return 0;
1313 }
1314 #endif
1315         
1316
1317 EXPORT_SYMBOL(ac97_read_proc);
1318 EXPORT_SYMBOL(ac97_probe_codec);
1319
1320 /*
1321  *      AC97 library support routines
1322  */     
1323  
1324 /**
1325  *      ac97_set_dac_rate       -       set codec rate adaption
1326  *      @codec: ac97 code
1327  *      @rate: rate in hertz
1328  *
1329  *      Set the DAC rate. Assumes the codec supports VRA. The caller is
1330  *      expected to have checked this little detail.
1331  */
1332  
1333 unsigned int ac97_set_dac_rate(struct ac97_codec *codec, unsigned int rate)
1334 {
1335         unsigned int new_rate = rate;
1336         u32 dacp;
1337         u32 mast_vol, phone_vol, mono_vol, pcm_vol;
1338         u32 mute_vol = 0x8000;  /* The mute volume? */
1339
1340         if(rate != codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE))
1341         {
1342                 /* Mute several registers */
1343                 mast_vol = codec->codec_read(codec, AC97_MASTER_VOL_STEREO);
1344                 mono_vol = codec->codec_read(codec, AC97_MASTER_VOL_MONO);
1345                 phone_vol = codec->codec_read(codec, AC97_HEADPHONE_VOL);
1346                 pcm_vol = codec->codec_read(codec, AC97_PCMOUT_VOL);
1347                 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mute_vol);
1348                 codec->codec_write(codec, AC97_MASTER_VOL_MONO, mute_vol);
1349                 codec->codec_write(codec, AC97_HEADPHONE_VOL, mute_vol);
1350                 codec->codec_write(codec, AC97_PCMOUT_VOL, mute_vol);
1351                 
1352                 /* Power down the DAC */
1353                 dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1354                 codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0200);
1355                 /* Load the rate and read the effective rate */
1356                 codec->codec_write(codec, AC97_PCM_FRONT_DAC_RATE, rate);
1357                 new_rate=codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE);
1358                 /* Power it back up */
1359                 codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1360
1361                 /* Restore volumes */
1362                 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mast_vol);
1363                 codec->codec_write(codec, AC97_MASTER_VOL_MONO, mono_vol);
1364                 codec->codec_write(codec, AC97_HEADPHONE_VOL, phone_vol);
1365                 codec->codec_write(codec, AC97_PCMOUT_VOL, pcm_vol);
1366         }
1367         return new_rate;
1368 }
1369
1370 EXPORT_SYMBOL(ac97_set_dac_rate);
1371
1372 /**
1373  *      ac97_set_adc_rate       -       set codec rate adaption
1374  *      @codec: ac97 code
1375  *      @rate: rate in hertz
1376  *
1377  *      Set the ADC rate. Assumes the codec supports VRA. The caller is
1378  *      expected to have checked this little detail.
1379  */
1380
1381 unsigned int ac97_set_adc_rate(struct ac97_codec *codec, unsigned int rate)
1382 {
1383         unsigned int new_rate = rate;
1384         u32 dacp;
1385
1386         if(rate != codec->codec_read(codec, AC97_PCM_LR_ADC_RATE))
1387         {
1388                 /* Power down the ADC */
1389                 dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1390                 codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0100);
1391                 /* Load the rate and read the effective rate */
1392                 codec->codec_write(codec, AC97_PCM_LR_ADC_RATE, rate);
1393                 new_rate=codec->codec_read(codec, AC97_PCM_LR_ADC_RATE);
1394                 /* Power it back up */
1395                 codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1396         }
1397         return new_rate;
1398 }
1399
1400 EXPORT_SYMBOL(ac97_set_adc_rate);
1401
1402 int ac97_save_state(struct ac97_codec *codec)
1403 {
1404         return 0;       
1405 }
1406
1407 EXPORT_SYMBOL(ac97_save_state);
1408
1409 int ac97_restore_state(struct ac97_codec *codec)
1410 {
1411         int i;
1412         unsigned int left, right, val;
1413
1414         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1415                 if (!supported_mixer(codec, i)) 
1416                         continue;
1417
1418                 val = codec->mixer_state[i];
1419                 right = val >> 8;
1420                 left = val  & 0xff;
1421                 codec->write_mixer(codec, i, left, right);
1422         }
1423         return 0;
1424 }
1425
1426 EXPORT_SYMBOL(ac97_restore_state);
1427
1428 /**
1429  *      ac97_register_driver    -       register a codec helper
1430  *      @driver: Driver handler
1431  *
1432  *      Register a handler for codecs matching the codec id. The handler
1433  *      attach function is called for all present codecs and will be 
1434  *      called when new codecs are discovered.
1435  */
1436  
1437 int ac97_register_driver(struct ac97_driver *driver)
1438 {
1439         struct list_head *l;
1440         struct ac97_codec *c;
1441         
1442         mutex_lock(&codec_mutex);
1443         INIT_LIST_HEAD(&driver->list);
1444         list_add(&driver->list, &codec_drivers);
1445         
1446         list_for_each(l, &codecs)
1447         {
1448                 c = list_entry(l, struct ac97_codec, list);
1449                 if(c->driver != NULL || ((c->model ^ driver->codec_id) & driver->codec_mask))
1450                         continue;
1451                 if(driver->probe(c, driver))
1452                         continue;
1453                 c->driver = driver;
1454         }
1455         mutex_unlock(&codec_mutex);
1456         return 0;
1457 }
1458
1459 EXPORT_SYMBOL_GPL(ac97_register_driver);
1460
1461 /**
1462  *      ac97_unregister_driver  -       unregister a codec helper
1463  *      @driver: Driver handler
1464  *
1465  *      Unregister a handler for codecs matching the codec id. The handler
1466  *      remove function is called for all matching codecs.
1467  */
1468  
1469 void ac97_unregister_driver(struct ac97_driver *driver)
1470 {
1471         struct list_head *l;
1472         struct ac97_codec *c;
1473         
1474         mutex_lock(&codec_mutex);
1475         list_del_init(&driver->list);
1476
1477         list_for_each(l, &codecs)
1478         {
1479                 c = list_entry(l, struct ac97_codec, list);
1480                 if (c->driver == driver) {
1481                         driver->remove(c, driver);
1482                         c->driver = NULL;
1483                 }
1484         }
1485         
1486         mutex_unlock(&codec_mutex);
1487 }
1488
1489 EXPORT_SYMBOL_GPL(ac97_unregister_driver);
1490
1491 static int swap_headphone(int remove_master)
1492 {
1493         struct list_head *l;
1494         struct ac97_codec *c;
1495         
1496         if (remove_master) {
1497                 mutex_lock(&codec_mutex);
1498                 list_for_each(l, &codecs)
1499                 {
1500                         c = list_entry(l, struct ac97_codec, list);
1501                         if (supported_mixer(c, SOUND_MIXER_PHONEOUT))
1502                                 c->supported_mixers &= ~SOUND_MASK_PHONEOUT;
1503                 }
1504                 mutex_unlock(&codec_mutex);
1505         } else
1506                 ac97_hw[SOUND_MIXER_PHONEOUT].offset = AC97_MASTER_VOL_STEREO;
1507
1508         /* Scale values already match */
1509         ac97_hw[SOUND_MIXER_VOLUME].offset = AC97_MASTER_VOL_MONO;
1510         return 0;
1511 }
1512
1513 static int apply_quirk(int quirk)
1514 {
1515         switch (quirk) {
1516         case AC97_TUNE_NONE:
1517                 return 0;
1518         case AC97_TUNE_HP_ONLY:
1519                 return swap_headphone(1);
1520         case AC97_TUNE_SWAP_HP:
1521                 return swap_headphone(0);
1522         case AC97_TUNE_SWAP_SURROUND:
1523                 return -ENOSYS; /* not yet implemented */
1524         case AC97_TUNE_AD_SHARING:
1525                 return -ENOSYS; /* not yet implemented */
1526         case AC97_TUNE_ALC_JACK:
1527                 return -ENOSYS; /* not yet implemented */
1528         }
1529         return -EINVAL;
1530 }
1531
1532 /**
1533  *      ac97_tune_hardware - tune up the hardware
1534  *      @pdev: pci_dev pointer
1535  *      @quirk: quirk list
1536  *      @override: explicit quirk value (overrides if not AC97_TUNE_DEFAULT)
1537  *
1538  *      Do some workaround for each pci device, such as renaming of the
1539  *      headphone (true line-out) control as "Master".
1540  *      The quirk-list must be terminated with a zero-filled entry.
1541  *
1542  *      Returns zero if successful, or a negative error code on failure.
1543  */
1544
1545 int ac97_tune_hardware(struct pci_dev *pdev, struct ac97_quirk *quirk, int override)
1546 {
1547         int result;
1548
1549         if (!quirk)
1550                 return -EINVAL;
1551
1552         if (override != AC97_TUNE_DEFAULT) {
1553                 result = apply_quirk(override);
1554                 if (result < 0)
1555                         printk(KERN_ERR "applying quirk type %d failed (%d)\n", override, result);
1556                 return result;
1557         }
1558
1559         for (; quirk->vendor; quirk++) {
1560                 if (quirk->vendor != pdev->subsystem_vendor)
1561                         continue;
1562                 if ((! quirk->mask && quirk->device == pdev->subsystem_device) ||
1563                     quirk->device == (quirk->mask & pdev->subsystem_device)) {
1564 #ifdef DEBUG
1565                         printk("ac97 quirk for %s (%04x:%04x)\n", quirk->name, ac97->subsystem_vendor, pdev->subsystem_device);
1566 #endif
1567                         result = apply_quirk(quirk->type);
1568                         if (result < 0)
1569                                 printk(KERN_ERR "applying quirk type %d for %s failed (%d)\n", quirk->type, quirk->name, result);
1570                         return result;
1571                 }
1572         }
1573         return 0;
1574 }
1575
1576 EXPORT_SYMBOL_GPL(ac97_tune_hardware);
1577
1578 MODULE_LICENSE("GPL");