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