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