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