3 * ac97_codec.c: Generic AC97 mixer/modem module
5 * Derived from ac97 mixer in maestro and trident driver.
7 * Copyright 2000 Silicon Integrated System Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 **************************************************************************
25 * The Intel Audio Codec '97 specification is available at the Intel
26 * audio homepage: http://developer.intel.com/ial/scalableplatforms/audio/
28 * The specification itself is currently available at:
29 * ftp://download.intel.com/ial/scalableplatforms/ac97r22.pdf
31 **************************************************************************
34 * Mar 28, 2002 Randolph Bentson <bentson@holmsjoen.com>
35 * corrections to support WM9707 in ViewPad 1000
36 * v0.4 Mar 15 2000 Ollie Lho
37 * dual codecs support verified with 4 channels output
38 * v0.3 Feb 22 2000 Ollie Lho
39 * bug fix for record mask setting
40 * v0.2 Feb 10 2000 Ollie Lho
41 * add ac97_read_proc for /proc/driver/{vendor}/ac97
42 * v0.1 Jan 14 2000 Ollie Lho <ollie@sis.com.tw>
43 * Isolated from trident.c to support multiple ac97 codec
45 #include <linux/module.h>
46 #include <linux/version.h>
47 #include <linux/kernel.h>
48 #include <linux/string.h>
49 #include <linux/errno.h>
50 #include <linux/bitops.h>
51 #include <linux/delay.h>
52 #include <linux/ac97_codec.h>
53 #include <asm/uaccess.h>
55 #define CODEC_ID_BUFSZ 14
57 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel);
58 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
59 unsigned int left, unsigned int right);
60 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val );
61 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask);
62 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg);
64 static int ac97_init_mixer(struct ac97_codec *codec);
66 static int wolfson_init00(struct ac97_codec * codec);
67 static int wolfson_init03(struct ac97_codec * codec);
68 static int wolfson_init04(struct ac97_codec * codec);
69 static int tritech_init(struct ac97_codec * codec);
70 static int tritech_maestro_init(struct ac97_codec * codec);
71 static int sigmatel_9708_init(struct ac97_codec *codec);
72 static int sigmatel_9721_init(struct ac97_codec *codec);
73 static int sigmatel_9744_init(struct ac97_codec *codec);
74 static int ad1886_init(struct ac97_codec *codec);
75 static int eapd_control(struct ac97_codec *codec, int);
76 static int crystal_digital_control(struct ac97_codec *codec, int mode);
82 * If you are adding a codec then you should be able to use
83 * eapd_ops - any codec that supports EAPD amp control (most)
84 * null_ops - any ancient codec that supports nothing
86 * The three functions are
87 * init - used for non AC97 standard initialisation
88 * amplifier - used to do amplifier control (1=on 0=off)
89 * digital - switch to digital modes (0 = analog)
91 * Not all codecs support all features, not all drivers use all the
95 static struct ac97_ops null_ops = { NULL, NULL, NULL };
96 static struct ac97_ops default_ops = { NULL, eapd_control, NULL };
97 static struct ac97_ops wolfson_ops00 = { wolfson_init00, NULL, NULL };
98 static struct ac97_ops wolfson_ops03 = { wolfson_init03, NULL, NULL };
99 static struct ac97_ops wolfson_ops04 = { wolfson_init04, NULL, NULL };
100 static struct ac97_ops tritech_ops = { tritech_init, NULL, NULL };
101 static struct ac97_ops tritech_m_ops = { tritech_maestro_init, NULL, NULL };
102 static struct ac97_ops sigmatel_9708_ops = { sigmatel_9708_init, NULL, NULL };
103 static struct ac97_ops sigmatel_9721_ops = { sigmatel_9721_init, NULL, NULL };
104 static struct ac97_ops sigmatel_9744_ops = { sigmatel_9744_init, NULL, NULL };
105 static struct ac97_ops crystal_digital_ops = { NULL, eapd_control, crystal_digital_control };
106 static struct ac97_ops ad1886_ops = { ad1886_init, eapd_control, NULL };
108 /* sorted by vendor/device id */
109 static const struct {
112 struct ac97_ops *ops;
113 } ac97_codec_ids[] = {
114 {0x41445303, "Analog Devices AD1819", &null_ops},
115 {0x41445340, "Analog Devices AD1881", &null_ops},
116 {0x41445348, "Analog Devices AD1881A", &null_ops},
117 {0x41445360, "Analog Devices AD1885", &default_ops},
118 {0x41445361, "Analog Devices AD1886", &ad1886_ops},
119 {0x41445460, "Analog Devices AD1885", &default_ops},
120 {0x41445461, "Analog Devices AD1886", &ad1886_ops},
121 {0x414B4D00, "Asahi Kasei AK4540", &null_ops},
122 {0x414B4D01, "Asahi Kasei AK4542", &null_ops},
123 {0x414B4D02, "Asahi Kasei AK4543", &null_ops},
124 {0x414C4710, "ALC200/200P", &null_ops},
125 {0x414C4720, "ALC650", &null_ops},
126 {0x43525900, "Cirrus Logic CS4297", &default_ops},
127 {0x43525903, "Cirrus Logic CS4297", &default_ops},
128 {0x43525913, "Cirrus Logic CS4297A rev A", &default_ops},
129 {0x43525914, "Cirrus Logic CS4297A rev B", &default_ops},
130 {0x43525923, "Cirrus Logic CS4298", &null_ops},
131 {0x4352592B, "Cirrus Logic CS4294", &null_ops},
132 {0x4352592D, "Cirrus Logic CS4294", &null_ops},
133 {0x43525931, "Cirrus Logic CS4299 rev A", &crystal_digital_ops},
134 {0x43525933, "Cirrus Logic CS4299 rev C", &crystal_digital_ops},
135 {0x43525934, "Cirrus Logic CS4299 rev D", &crystal_digital_ops},
136 {0x45838308, "ESS Allegro ES1988", &null_ops},
137 {0x49434511, "ICE1232", &null_ops}, /* I hope --jk */
138 {0x4e534331, "National Semiconductor LM4549", &null_ops},
139 {0x53494c22, "Silicon Laboratory Si3036", &null_ops},
140 {0x53494c23, "Silicon Laboratory Si3038", &null_ops},
141 {0x545200FF, "TriTech TR?????", &tritech_m_ops},
142 {0x54524102, "TriTech TR28022", &null_ops},
143 {0x54524103, "TriTech TR28023", &null_ops},
144 {0x54524106, "TriTech TR28026", &null_ops},
145 {0x54524108, "TriTech TR28028", &tritech_ops},
146 {0x54524123, "TriTech TR A5", &null_ops},
147 {0x574D4C00, "Wolfson WM9700A", &wolfson_ops00},
148 {0x574D4C03, "Wolfson WM9703/WM9707", &wolfson_ops03},
149 {0x574D4C04, "Wolfson WM9704M/WM9704Q", &wolfson_ops04},
150 {0x83847600, "SigmaTel STAC????", &null_ops},
151 {0x83847604, "SigmaTel STAC9701/3/4/5", &null_ops},
152 {0x83847605, "SigmaTel STAC9704", &null_ops},
153 {0x83847608, "SigmaTel STAC9708", &sigmatel_9708_ops},
154 {0x83847609, "SigmaTel STAC9721/23", &sigmatel_9721_ops},
155 {0x83847644, "SigmaTel STAC9744/45", &sigmatel_9744_ops},
156 {0x83847656, "SigmaTel STAC9756/57", &sigmatel_9744_ops},
157 {0x83847666, "SigmaTel STAC9750T", &sigmatel_9744_ops},
158 {0x83847684, "SigmaTel STAC9783/84?", &null_ops},
159 {0x57454301, "Winbond 83971D", &null_ops},
162 static const char *ac97_stereo_enhancements[] =
164 /* 0 */ "No 3D Stereo Enhancement",
165 /* 1 */ "Analog Devices Phat Stereo",
166 /* 2 */ "Creative Stereo Enhancement",
167 /* 3 */ "National Semi 3D Stereo Enhancement",
168 /* 4 */ "YAMAHA Ymersion",
169 /* 5 */ "BBE 3D Stereo Enhancement",
170 /* 6 */ "Crystal Semi 3D Stereo Enhancement",
171 /* 7 */ "Qsound QXpander",
172 /* 8 */ "Spatializer 3D Stereo Enhancement",
173 /* 9 */ "SRS 3D Stereo Enhancement",
174 /* 10 */ "Platform Tech 3D Stereo Enhancement",
175 /* 11 */ "AKM 3D Audio",
176 /* 12 */ "Aureal Stereo Enhancement",
177 /* 13 */ "Aztech 3D Enhancement",
178 /* 14 */ "Binaura 3D Audio Enhancement",
179 /* 15 */ "ESS Technology Stereo Enhancement",
180 /* 16 */ "Harman International VMAx",
181 /* 17 */ "Nvidea 3D Stereo Enhancement",
182 /* 18 */ "Philips Incredible Sound",
183 /* 19 */ "Texas Instruments 3D Stereo Enhancement",
184 /* 20 */ "VLSI Technology 3D Stereo Enhancement",
185 /* 21 */ "TriTech 3D Stereo Enhancement",
186 /* 22 */ "Realtek 3D Stereo Enhancement",
187 /* 23 */ "Samsung 3D Stereo Enhancement",
188 /* 24 */ "Wolfson Microelectronics 3D Enhancement",
189 /* 25 */ "Delta Integration 3D Enhancement",
190 /* 26 */ "SigmaTel 3D Enhancement",
191 /* 27 */ "Winbond 3D Stereo Enhancement",
192 /* 28 */ "Rockwell 3D Stereo Enhancement",
193 /* 29 */ "Reserved 29",
194 /* 30 */ "Reserved 30",
195 /* 31 */ "Reserved 31"
198 /* this table has default mixer values for all OSS mixers. */
199 static struct mixer_defaults {
202 } mixer_defaults[SOUND_MIXER_NRDEVICES] = {
203 /* all values 0 -> 100 in bytes */
204 {SOUND_MIXER_VOLUME, 0x4343},
205 {SOUND_MIXER_BASS, 0x4343},
206 {SOUND_MIXER_TREBLE, 0x4343},
207 {SOUND_MIXER_PCM, 0x4343},
208 {SOUND_MIXER_SPEAKER, 0x4343},
209 {SOUND_MIXER_LINE, 0x4343},
210 {SOUND_MIXER_MIC, 0x0000},
211 {SOUND_MIXER_CD, 0x4343},
212 {SOUND_MIXER_ALTPCM, 0x4343},
213 {SOUND_MIXER_IGAIN, 0x4343},
214 {SOUND_MIXER_LINE1, 0x4343},
215 {SOUND_MIXER_PHONEIN, 0x4343},
216 {SOUND_MIXER_PHONEOUT, 0x4343},
217 {SOUND_MIXER_VIDEO, 0x4343},
221 /* table to scale scale from OSS mixer value to AC97 mixer register value */
222 static struct ac97_mixer_hw {
223 unsigned char offset;
225 } ac97_hw[SOUND_MIXER_NRDEVICES]= {
226 [SOUND_MIXER_VOLUME] = {AC97_MASTER_VOL_STEREO,64},
227 [SOUND_MIXER_BASS] = {AC97_MASTER_TONE, 16},
228 [SOUND_MIXER_TREBLE] = {AC97_MASTER_TONE, 16},
229 [SOUND_MIXER_PCM] = {AC97_PCMOUT_VOL, 32},
230 [SOUND_MIXER_SPEAKER] = {AC97_PCBEEP_VOL, 16},
231 [SOUND_MIXER_LINE] = {AC97_LINEIN_VOL, 32},
232 [SOUND_MIXER_MIC] = {AC97_MIC_VOL, 32},
233 [SOUND_MIXER_CD] = {AC97_CD_VOL, 32},
234 [SOUND_MIXER_ALTPCM] = {AC97_HEADPHONE_VOL, 64},
235 [SOUND_MIXER_IGAIN] = {AC97_RECORD_GAIN, 16},
236 [SOUND_MIXER_LINE1] = {AC97_AUX_VOL, 32},
237 [SOUND_MIXER_PHONEIN] = {AC97_PHONE_VOL, 32},
238 [SOUND_MIXER_PHONEOUT] = {AC97_MASTER_VOL_MONO, 64},
239 [SOUND_MIXER_VIDEO] = {AC97_VIDEO_VOL, 32},
242 /* the following tables allow us to go from OSS <-> ac97 quickly. */
243 enum ac97_recsettings {
249 AC97_REC_STEREO, /* combination of all enabled outputs.. */
250 AC97_REC_MONO, /*.. or the mono equivalent */
254 static const unsigned int ac97_rm2oss[] = {
255 [AC97_REC_MIC] = SOUND_MIXER_MIC,
256 [AC97_REC_CD] = SOUND_MIXER_CD,
257 [AC97_REC_VIDEO] = SOUND_MIXER_VIDEO,
258 [AC97_REC_AUX] = SOUND_MIXER_LINE1,
259 [AC97_REC_LINE] = SOUND_MIXER_LINE,
260 [AC97_REC_STEREO]= SOUND_MIXER_IGAIN,
261 [AC97_REC_PHONE] = SOUND_MIXER_PHONEIN
264 /* indexed by bit position */
265 static const unsigned int ac97_oss_rm[] = {
266 [SOUND_MIXER_MIC] = AC97_REC_MIC,
267 [SOUND_MIXER_CD] = AC97_REC_CD,
268 [SOUND_MIXER_VIDEO] = AC97_REC_VIDEO,
269 [SOUND_MIXER_LINE1] = AC97_REC_AUX,
270 [SOUND_MIXER_LINE] = AC97_REC_LINE,
271 [SOUND_MIXER_IGAIN] = AC97_REC_STEREO,
272 [SOUND_MIXER_PHONEIN] = AC97_REC_PHONE
275 /* reads the given OSS mixer from the ac97 the caller must have insured that the ac97 knows
276 about that given mixer, and should be holding a spinlock for the card */
277 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel)
282 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
284 val = codec->codec_read(codec , mh->offset);
286 if (val & AC97_MUTE) {
288 } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
289 /* nice stereo mixers .. */
292 left = (val >> 8) & 0x7f;
295 if (oss_channel == SOUND_MIXER_IGAIN) {
296 right = (right * 100) / mh->scale;
297 left = (left * 100) / mh->scale;
299 /* these may have 5 or 6 bit resolution */
300 if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
301 scale = (1 << codec->bit_resolution);
305 right = 100 - ((right * 100) / scale);
306 left = 100 - ((left * 100) / scale);
308 ret = left | (right << 8);
309 } else if (oss_channel == SOUND_MIXER_SPEAKER) {
310 ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
311 } else if (oss_channel == SOUND_MIXER_PHONEIN) {
312 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
313 } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
314 scale = (1 << codec->bit_resolution);
315 ret = 100 - (((val & 0x1f) * 100) / scale);
316 } else if (oss_channel == SOUND_MIXER_MIC) {
317 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
318 /* the low bit is optional in the tone sliders and masking
319 it lets us avoid the 0xf 'bypass'.. */
320 } else if (oss_channel == SOUND_MIXER_BASS) {
321 ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
322 } else if (oss_channel == SOUND_MIXER_TREBLE) {
323 ret = 100 - (((val & 0xe) * 100) / mh->scale);
327 printk("ac97_codec: read OSS mixer %2d (%s ac97 register 0x%02x), "
328 "0x%04x -> 0x%04x\n",
329 oss_channel, codec->id ? "Secondary" : "Primary",
330 mh->offset, val, ret);
336 /* write the OSS encoded volume to the given OSS encoded mixer, again caller's job to
337 make sure all is well in arg land, call with spinlock held */
338 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
339 unsigned int left, unsigned int right)
343 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
346 printk("ac97_codec: wrote OSS mixer %2d (%s ac97 register 0x%02x), "
347 "left vol:%2d, right vol:%2d:",
348 oss_channel, codec->id ? "Secondary" : "Primary",
349 mh->offset, left, right);
352 if (AC97_STEREO_MASK & (1 << oss_channel)) {
354 if (left == 0 && right == 0) {
357 if (oss_channel == SOUND_MIXER_IGAIN) {
358 right = (right * mh->scale) / 100;
359 left = (left * mh->scale) / 100;
360 if (right >= mh->scale)
362 if (left >= mh->scale)
365 /* these may have 5 or 6 bit resolution */
366 if (oss_channel == SOUND_MIXER_VOLUME ||
367 oss_channel == SOUND_MIXER_ALTPCM)
368 scale = (1 << codec->bit_resolution);
372 right = ((100 - right) * scale) / 100;
373 left = ((100 - left) * scale) / 100;
379 val = (left << 8) | right;
381 } else if (oss_channel == SOUND_MIXER_BASS) {
382 val = codec->codec_read(codec , mh->offset) & ~0x0f00;
383 left = ((100 - left) * mh->scale) / 100;
384 if (left >= mh->scale)
386 val |= (left << 8) & 0x0e00;
387 } else if (oss_channel == SOUND_MIXER_TREBLE) {
388 val = codec->codec_read(codec , mh->offset) & ~0x000f;
389 left = ((100 - left) * mh->scale) / 100;
390 if (left >= mh->scale)
392 val |= left & 0x000e;
393 } else if(left == 0) {
395 } else if (oss_channel == SOUND_MIXER_SPEAKER) {
396 left = ((100 - left) * mh->scale) / 100;
397 if (left >= mh->scale)
400 } else if (oss_channel == SOUND_MIXER_PHONEIN) {
401 left = ((100 - left) * mh->scale) / 100;
402 if (left >= mh->scale)
405 } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
406 scale = (1 << codec->bit_resolution);
407 left = ((100 - left) * scale) / 100;
408 if (left >= mh->scale)
411 } else if (oss_channel == SOUND_MIXER_MIC) {
412 val = codec->codec_read(codec , mh->offset) & ~0x801f;
413 left = ((100 - left) * mh->scale) / 100;
414 if (left >= mh->scale)
417 /* the low bit is optional in the tone sliders and masking
418 it lets us avoid the 0xf 'bypass'.. */
421 printk(" 0x%04x", val);
424 codec->codec_write(codec, mh->offset, val);
427 val = codec->codec_read(codec, mh->offset);
428 printk(" -> 0x%04x\n", val);
432 /* a thin wrapper for write_mixer */
433 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val )
435 unsigned int left,right;
437 /* cleanse input a little */
438 right = ((val >> 8) & 0xff) ;
439 left = (val & 0xff) ;
441 if (right > 100) right = 100;
442 if (left > 100) left = 100;
444 codec->mixer_state[oss_mixer] = (right << 8) | left;
445 codec->write_mixer(codec, oss_mixer, left, right);
448 /* read or write the recmask, the ac97 can really have left and right recording
449 inputs independantly set, but OSS doesn't seem to want us to express that to
450 the user. the caller guarantees that we have a supported bit set, and they
451 must be holding the card's spinlock */
452 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask)
457 /* read it from the card */
458 val = codec->codec_read(codec, AC97_RECORD_SELECT);
460 printk("ac97_codec: ac97 recmask to set to 0x%04x\n", val);
462 return (1 << ac97_rm2oss[val & 0x07]);
465 /* else, write the first set in the mask as the
467 /* clear out current set value first (AC97 supports only 1 input!) */
468 val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
473 val = ac97_oss_rm[val-1];
474 val |= val << 8; /* set both channels */
477 printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
480 codec->codec_write(codec, AC97_RECORD_SELECT, val);
485 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
489 if (cmd == SOUND_MIXER_INFO) {
491 strncpy(info.id, codec->name, sizeof(info.id));
492 strncpy(info.name, codec->name, sizeof(info.name));
493 info.modify_counter = codec->modcnt;
494 if (copy_to_user((void *)arg, &info, sizeof(info)))
498 if (cmd == SOUND_OLD_MIXER_INFO) {
499 _old_mixer_info info;
500 strncpy(info.id, codec->name, sizeof(info.id));
501 strncpy(info.name, codec->name, sizeof(info.name));
502 if (copy_to_user((void *)arg, &info, sizeof(info)))
507 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
510 if (cmd == OSS_GETVERSION)
511 return put_user(SOUND_VERSION, (int *)arg);
513 if (_SIOC_DIR(cmd) == _SIOC_READ) {
514 switch (_IOC_NR(cmd)) {
515 case SOUND_MIXER_RECSRC: /* give them the current record source */
516 if (!codec->recmask_io) {
519 val = codec->recmask_io(codec, 1, 0);
523 case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
524 val = codec->supported_mixers;
527 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
528 val = codec->record_sources;
531 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
532 val = codec->stereo_mixers;
535 case SOUND_MIXER_CAPS:
536 val = SOUND_CAP_EXCL_INPUT;
539 default: /* read a specific mixer */
542 if (!supported_mixer(codec, i))
545 /* do we ever want to touch the hardware? */
546 /* val = codec->read_mixer(codec, i); */
547 val = codec->mixer_state[i];
550 return put_user(val, (int *)arg);
553 if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
555 if (get_user(val, (int *)arg))
558 switch (_IOC_NR(cmd)) {
559 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
560 if (!codec->recmask_io) return -EINVAL;
562 if (!(val &= codec->record_sources)) return -EINVAL;
564 codec->recmask_io(codec, 0, val);
567 default: /* write a specific mixer */
570 if (!supported_mixer(codec, i))
573 ac97_set_mixer(codec, i, val);
581 /* entry point for /proc/driver/controller_vendor/ac97/%d */
582 int ac97_read_proc (char *page, char **start, off_t off,
583 int count, int *eof, void *data)
585 int len = 0, cap, extid, val, id1, id2;
586 struct ac97_codec *codec;
589 if ((codec = data) == NULL)
592 id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
593 id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
594 len += sprintf (page+len, "Vendor name : %s\n", codec->name);
595 len += sprintf (page+len, "Vendor id : %04X %04X\n", id1, id2);
597 extid = codec->codec_read(codec, AC97_EXTENDED_ID);
598 extid &= ~((1<<2)|(1<<4)|(1<<5)|(1<<10)|(1<<11)|(1<<12)|(1<<13));
599 len += sprintf (page+len, "AC97 Version : %s\n",
600 extid ? "2.0 or later" : "1.0");
601 if (extid) is_ac97_20 = 1;
603 cap = codec->codec_read(codec, AC97_RESET);
604 len += sprintf (page+len, "Capabilities :%s%s%s%s%s%s\n",
605 cap & 0x0001 ? " -dedicated MIC PCM IN channel-" : "",
606 cap & 0x0002 ? " -reserved1-" : "",
607 cap & 0x0004 ? " -bass & treble-" : "",
608 cap & 0x0008 ? " -simulated stereo-" : "",
609 cap & 0x0010 ? " -headphone out-" : "",
610 cap & 0x0020 ? " -loudness-" : "");
612 len += sprintf (page+len, "DAC resolutions :%s%s%s\n",
614 val & 0x0040 ? " -18-bit-" : "",
615 val & 0x0080 ? " -20-bit-" : "");
617 len += sprintf (page+len, "ADC resolutions :%s%s%s\n",
619 val & 0x0100 ? " -18-bit-" : "",
620 val & 0x0200 ? " -20-bit-" : "");
621 len += sprintf (page+len, "3D enhancement : %s\n",
622 ac97_stereo_enhancements[(cap >> 10) & 0x1f]);
624 val = codec->codec_read(codec, AC97_GENERAL_PURPOSE);
625 len += sprintf (page+len, "POP path : %s 3D\n"
627 "3D enhancement : %s\n"
631 "ADC/DAC loopback : %s\n",
632 val & 0x8000 ? "post" : "pre",
633 val & 0x4000 ? "on" : "off",
634 val & 0x2000 ? "on" : "off",
635 val & 0x1000 ? "on" : "off",
636 val & 0x0200 ? "MIC" : "MIX",
637 val & 0x0100 ? "MIC2" : "MIC1",
638 val & 0x0080 ? "on" : "off");
640 extid = codec->codec_read(codec, AC97_EXTENDED_ID);
642 len += sprintf (page+len, "Ext Capabilities :%s%s%s%s%s%s%s\n",
643 cap & 0x0001 ? " -var rate PCM audio-" : "",
644 cap & 0x0002 ? " -2x PCM audio out-" : "",
645 cap & 0x0008 ? " -var rate MIC in-" : "",
646 cap & 0x0040 ? " -PCM center DAC-" : "",
647 cap & 0x0080 ? " -PCM surround DAC-" : "",
648 cap & 0x0100 ? " -PCM LFE DAC-" : "",
649 cap & 0x0200 ? " -slot/DAC mappings-" : "");
651 len += sprintf (page+len, "Front DAC rate : %d\n",
652 codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE));
659 * codec_id - Turn id1/id2 into a PnP string
662 * @buf: CODEC_ID_BUFSZ byte buffer
664 * Fills buf with a zero terminated PnP ident string for the id1/id2
665 * pair. For convenience the return is the passed in buffer pointer.
668 static char *codec_id(u16 id1, u16 id2, char *buf)
671 snprintf(buf, CODEC_ID_BUFSZ, "0x%04x:0x%04x", id1, id2);
674 buf[1] = (id1 & 0xFF);
676 snprintf(buf+3, CODEC_ID_BUFSZ - 3, "%d", id2&0xFF);
682 * ac97_probe_codec - Initialize and setup AC97-compatible codec
683 * @codec: (in/out) Kernel info for a single AC97 codec
685 * Reset the AC97 codec, then initialize the mixer and
686 * the rest of the @codec structure.
688 * The codec_read and codec_write fields of @codec are
689 * required to be setup and working when this function
690 * is called. All other fields are set by this function.
692 * codec_wait field of @codec can optionally be provided
693 * when calling this function. If codec_wait is not %NULL,
694 * this function will call codec_wait any time it is
695 * necessary to wait for the audio chip to reach the
696 * codec-ready state. If codec_wait is %NULL, then
697 * the default behavior is to call schedule_timeout.
698 * Currently codec_wait is used to wait for AC97 codec
701 * Returns 1 (true) on success, or 0 (false) on failure.
704 int ac97_probe_codec(struct ac97_codec *codec)
709 char cidbuf[CODEC_ID_BUFSZ];
711 /* probing AC97 codec, AC97 2.0 says that bit 15 of register 0x00 (reset) should
714 * FIXME: is the following comment outdated? -jgarzik
715 * Probing of AC97 in this way is not reliable, it is not even SAFE !!
717 codec->codec_write(codec, AC97_RESET, 0L);
719 /* also according to spec, we wait for codec-ready state */
720 if (codec->codec_wait)
721 codec->codec_wait(codec);
725 if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
726 printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
727 (codec->id & 0x2) ? (codec->id&1 ? "4th" : "Tertiary")
728 : (codec->id&1 ? "Secondary": "Primary"));
732 /* probe for Modem Codec */
733 codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
734 modem = codec->codec_read(codec, AC97_EXTENDED_MODEM_ID) & 1;
739 codec->codec_ops = &null_ops;
741 id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
742 id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
743 for (i = 0; i < ARRAY_SIZE(ac97_codec_ids); i++) {
744 if (ac97_codec_ids[i].id == ((id1 << 16) | id2)) {
745 codec->type = ac97_codec_ids[i].id;
746 codec->name = ac97_codec_ids[i].name;
747 codec->codec_ops = ac97_codec_ids[i].ops;
751 if (codec->name == NULL)
752 codec->name = "Unknown";
753 printk(KERN_INFO "ac97_codec: AC97 %s codec, id: %s (%s)\n",
754 modem ? "Modem" : (audio ? "Audio" : ""),
755 codec_id(id1, id2, cidbuf), codec->name);
757 return ac97_init_mixer(codec);
760 static int ac97_init_mixer(struct ac97_codec *codec)
765 cap = codec->codec_read(codec, AC97_RESET);
768 codec->supported_mixers = AC97_SUPPORTED_MASK;
769 codec->stereo_mixers = AC97_STEREO_MASK;
770 codec->record_sources = AC97_RECORD_MASK;
772 codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
774 codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
776 /* detect bit resolution */
777 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
778 if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x2020)
779 codec->bit_resolution = 6;
781 codec->bit_resolution = 5;
783 /* generic OSS to AC97 wrapper */
784 codec->read_mixer = ac97_read_mixer;
785 codec->write_mixer = ac97_write_mixer;
786 codec->recmask_io = ac97_recmask_io;
787 codec->mixer_ioctl = ac97_mixer_ioctl;
789 /* codec specific initialization for 4-6 channel output or secondary codec stuff */
790 if (codec->codec_ops->init != NULL) {
791 codec->codec_ops->init(codec);
794 /* initialize mixer channel volumes */
795 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
796 struct mixer_defaults *md = &mixer_defaults[i];
799 if (!supported_mixer(codec, md->mixer))
801 ac97_set_mixer(codec, md->mixer, md->value);
807 #define AC97_SIGMATEL_ANALOG 0x6c /* Analog Special */
808 #define AC97_SIGMATEL_DAC2INVERT 0x6e
809 #define AC97_SIGMATEL_BIAS1 0x70
810 #define AC97_SIGMATEL_BIAS2 0x72
811 #define AC97_SIGMATEL_MULTICHN 0x74 /* Multi-Channel programming */
812 #define AC97_SIGMATEL_CIC1 0x76
813 #define AC97_SIGMATEL_CIC2 0x78
816 static int sigmatel_9708_init(struct ac97_codec * codec)
818 u16 codec72, codec6c;
820 codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
821 codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
823 if ((codec72==0) && (codec6c==0)) {
824 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
825 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
826 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
827 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
828 } else if ((codec72==0x8000) && (codec6c==0)) {
829 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
830 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
831 codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
832 } else if ((codec72==0x8000) && (codec6c==0x0080)) {
835 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
840 static int sigmatel_9721_init(struct ac97_codec * codec)
842 /* Only set up secondary codec */
846 codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
848 /* initialize SigmaTel STAC9721/23 as secondary codec, decoding AC link
849 sloc 3,4 = 0x01, slot 7,8 = 0x00, */
850 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
852 /* we don't have the crystal when we are on an AMR card, so use
853 BIT_CLK as our clock source. Write the magic word ABBA and read
854 back to enable register 0x78 */
855 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
856 codec->codec_read(codec, AC97_SIGMATEL_CIC1);
858 /* sync all the clocks*/
859 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
865 static int sigmatel_9744_init(struct ac97_codec * codec)
867 // patch for SigmaTel
868 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
869 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000); // is this correct? --jk
870 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
871 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
872 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
877 static int wolfson_init00(struct ac97_codec * codec)
879 /* This initialization is suspect, but not known to be wrong.
880 It was copied from the initialization for the WM9704Q, but
881 that same sequence is known to fail for the WM9707. Thus
882 this warning may help someone with hardware to test
884 codec->codec_write(codec, 0x72, 0x0808);
885 codec->codec_write(codec, 0x74, 0x0808);
887 // patch for DVD noise
888 codec->codec_write(codec, 0x5a, 0x0200);
890 // init vol as PCM vol
891 codec->codec_write(codec, 0x70,
892 codec->codec_read(codec, AC97_PCMOUT_VOL));
894 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
899 static int wolfson_init03(struct ac97_codec * codec)
901 /* this is known to work for the ViewSonic ViewPad 1000 */
902 codec->codec_write(codec, 0x72, 0x0808);
903 codec->codec_write(codec, 0x20, 0x8000);
908 static int wolfson_init04(struct ac97_codec * codec)
910 codec->codec_write(codec, 0x72, 0x0808);
911 codec->codec_write(codec, 0x74, 0x0808);
913 // patch for DVD noise
914 codec->codec_write(codec, 0x5a, 0x0200);
916 // init vol as PCM vol
917 codec->codec_write(codec, 0x70,
918 codec->codec_read(codec, AC97_PCMOUT_VOL));
920 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
925 static int tritech_init(struct ac97_codec * codec)
927 codec->codec_write(codec, 0x26, 0x0300);
928 codec->codec_write(codec, 0x26, 0x0000);
929 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
930 codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
935 /* copied from drivers/sound/maestro.c */
936 static int tritech_maestro_init(struct ac97_codec * codec)
938 /* no idea what this does */
939 codec->codec_write(codec, 0x2A, 0x0001);
940 codec->codec_write(codec, 0x2C, 0x0000);
941 codec->codec_write(codec, 0x2C, 0XFFFF);
948 * Presario700 workaround
949 * for Jack Sense/SPDIF Register mis-setting causing
951 * by Santiago Nullo 04/05/2002
954 #define AC97_AD1886_JACK_SENSE 0x72
956 static int ad1886_init(struct ac97_codec * codec)
958 /* from AD1886 Specs */
959 codec->codec_write(codec, AC97_AD1886_JACK_SENSE, 0x0010);
967 * This is basically standard AC97. It should work as a default for
968 * almost all modern codecs. Note that some cards wire EAPD *backwards*
969 * That side of it is up to the card driver not us to cope with.
973 static int eapd_control(struct ac97_codec * codec, int on)
976 codec->codec_write(codec, AC97_POWER_CONTROL,
977 codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
979 codec->codec_write(codec, AC97_POWER_CONTROL,
980 codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
985 * Crystal digital audio control (CS4299
988 static int crystal_digital_control(struct ac97_codec *codec, int mode)
994 case 0: cv = 0x0; break; /* SPEN off */
995 case 1: cv = 0x8004; break; /* 48KHz digital */
996 case 2: cv = 0x8104; break; /* 44.1KHz digital */
998 return -1; /* Not supported yet(eg AC3) */
1000 codec->codec_write(codec, 0x68, cv);
1004 /* copied from drivers/sound/maestro.c */
1005 #if 0 /* there has been 1 person on the planet with a pt101 that we
1006 know of. If they care, they can put this back in :) */
1007 static int pt101_init(struct ac97_codec * codec)
1009 printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
1011 codec->codec_write(codec, 0x2A, 0x0001);
1012 codec->codec_write(codec, 0x2C, 0x0000);
1013 codec->codec_write(codec, 0x2C, 0xFFFF);
1014 codec->codec_write(codec, 0x10, 0x9F1F);
1015 codec->codec_write(codec, 0x12, 0x0808);
1016 codec->codec_write(codec, 0x14, 0x9F1F);
1017 codec->codec_write(codec, 0x16, 0x9F1F);
1018 codec->codec_write(codec, 0x18, 0x0404);
1019 codec->codec_write(codec, 0x1A, 0x0000);
1020 codec->codec_write(codec, 0x1C, 0x0000);
1021 codec->codec_write(codec, 0x02, 0x0404);
1022 codec->codec_write(codec, 0x04, 0x0808);
1023 codec->codec_write(codec, 0x0C, 0x801F);
1024 codec->codec_write(codec, 0x0E, 0x801F);
1030 EXPORT_SYMBOL(ac97_read_proc);
1031 EXPORT_SYMBOL(ac97_probe_codec);
1034 * AC97 library support routines
1038 * ac97_set_dac_rate - set codec rate adaption
1040 * @rate: rate in hertz
1042 * Set the DAC rate. Assumes the codec supports VRA. The caller is
1043 * expected to have checked this little detail.
1046 unsigned int ac97_set_dac_rate(struct ac97_codec *codec, unsigned int rate)
1048 unsigned int new_rate = rate;
1050 u32 mast_vol, phone_vol, mono_vol, pcm_vol;
1051 u32 mute_vol = 0x8000; /* The mute volume? */
1053 if(rate != codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE))
1055 /* Mute several registers */
1056 mast_vol = codec->codec_read(codec, AC97_MASTER_VOL_STEREO);
1057 mono_vol = codec->codec_read(codec, AC97_MASTER_VOL_MONO);
1058 phone_vol = codec->codec_read(codec, AC97_HEADPHONE_VOL);
1059 pcm_vol = codec->codec_read(codec, AC97_PCMOUT_VOL);
1060 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mute_vol);
1061 codec->codec_write(codec, AC97_MASTER_VOL_MONO, mute_vol);
1062 codec->codec_write(codec, AC97_HEADPHONE_VOL, mute_vol);
1063 codec->codec_write(codec, AC97_PCMOUT_VOL, mute_vol);
1065 /* Power down the DAC */
1066 dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1067 codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0200);
1068 /* Load the rate and read the effective rate */
1069 codec->codec_write(codec, AC97_PCM_FRONT_DAC_RATE, rate);
1070 new_rate=codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE);
1071 /* Power it back up */
1072 codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1074 /* Restore volumes */
1075 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, mast_vol);
1076 codec->codec_write(codec, AC97_MASTER_VOL_MONO, mono_vol);
1077 codec->codec_write(codec, AC97_HEADPHONE_VOL, phone_vol);
1078 codec->codec_write(codec, AC97_PCMOUT_VOL, pcm_vol);
1083 EXPORT_SYMBOL(ac97_set_dac_rate);
1086 * ac97_set_adc_rate - set codec rate adaption
1088 * @rate: rate in hertz
1090 * Set the ADC rate. Assumes the codec supports VRA. The caller is
1091 * expected to have checked this little detail.
1094 unsigned int ac97_set_adc_rate(struct ac97_codec *codec, unsigned int rate)
1096 unsigned int new_rate = rate;
1099 if(rate != codec->codec_read(codec, AC97_PCM_LR_ADC_RATE))
1101 /* Power down the ADC */
1102 dacp=codec->codec_read(codec, AC97_POWER_CONTROL);
1103 codec->codec_write(codec, AC97_POWER_CONTROL, dacp|0x0100);
1104 /* Load the rate and read the effective rate */
1105 codec->codec_write(codec, AC97_PCM_LR_ADC_RATE, rate);
1106 new_rate=codec->codec_read(codec, AC97_PCM_LR_ADC_RATE);
1107 /* Power it back up */
1108 codec->codec_write(codec, AC97_POWER_CONTROL, dacp);
1113 EXPORT_SYMBOL(ac97_set_adc_rate);
1115 int ac97_save_state(struct ac97_codec *codec)
1120 EXPORT_SYMBOL(ac97_save_state);
1122 int ac97_restore_state(struct ac97_codec *codec)
1125 unsigned int left, right, val;
1127 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1128 if (!supported_mixer(codec, i))
1131 val = codec->mixer_state[i];
1134 codec->write_mixer(codec, i, left, right);
1139 EXPORT_SYMBOL(ac97_restore_state);
1141 MODULE_LICENSE("GPL");