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