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