1
2 /*
3 * ac97_codec.c: Generic AC97 mixer/modem module
4 *
5 * Derived from ac97 mixer in maestro and trident driver.
6 *
7 * Copyright 2000 Silicon Integrated System Corporation
8 *
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.
13 *
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.
18 *
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.
22 *
23 **************************************************************************
24 *
25 * The Intel Audio Codec '97 specification is available at the Intel
26 * audio homepage: http://developer.intel.com/ial/scalableplatforms/audio/
27 *
28 * The specification itself is currently available at:
29 * ftp://download.intel.com/ial/scalableplatforms/ac97r22.pdf
30 *
31 **************************************************************************
32 *
33 * History
34 * v0.4 Mar 15 2000 Ollie Lho
35 * dual codecs support verified with 4 channels output
36 * v0.3 Feb 22 2000 Ollie Lho
37 * bug fix for record mask setting
38 * v0.2 Feb 10 2000 Ollie Lho
39 * add ac97_read_proc for /proc/driver/{vendor}/ac97
40 * v0.1 Jan 14 2000 Ollie Lho <ollie@sis.com.tw>
41 * Isolated from trident.c to support multiple ac97 codec
42 */
43 #include <linux/module.h>
44 #include <linux/version.h>
45 #include <linux/kernel.h>
46 #include <linux/string.h>
47 #include <linux/errno.h>
48 #include <linux/bitops.h>
49 #include <linux/delay.h>
50 #include <linux/ac97_codec.h>
51 #include <asm/uaccess.h>
52
53 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel);
54 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
55 unsigned int left, unsigned int right);
56 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val );
57 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask);
58 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg);
59
60 static int ac97_init_mixer(struct ac97_codec *codec);
61
62 static int wolfson_init(struct ac97_codec * codec);
63 static int tritech_init(struct ac97_codec * codec);
64 static int tritech_maestro_init(struct ac97_codec * codec);
65 static int sigmatel_9708_init(struct ac97_codec *codec);
66 static int sigmatel_9721_init(struct ac97_codec *codec);
67 static int sigmatel_9744_init(struct ac97_codec *codec);
68 static int eapd_control(struct ac97_codec *codec, int);
69 static int crystal_digital_control(struct ac97_codec *codec, int mode);
70
71
72 /*
73 * AC97 operations.
74 *
75 * If you are adding a codec then you should be able to use
76 * eapd_ops - any codec that supports EAPD amp control (most)
77 * null_ops - any ancient codec that supports nothing
78 *
79 * The three functions are
80 * init - used for non AC97 standard initialisation
81 * amplifier - used to do amplifier control (1=on 0=off)
82 * digital - switch to digital modes (0 = analog)
83 *
84 * Not all codecs support all features, not all drivers use all the
85 * operations yet
86 */
87
88 static struct ac97_ops null_ops = { NULL, NULL, NULL };
89 static struct ac97_ops default_ops = { NULL, eapd_control, NULL };
90 static struct ac97_ops wolfson_ops = { wolfson_init, NULL, NULL };
91 static struct ac97_ops tritech_ops = { tritech_init, NULL, NULL };
92 static struct ac97_ops tritech_m_ops = { tritech_maestro_init, NULL, NULL };
93 static struct ac97_ops sigmatel_9708_ops = { sigmatel_9708_init, NULL, NULL };
94 static struct ac97_ops sigmatel_9721_ops = { sigmatel_9721_init, NULL, NULL };
95 static struct ac97_ops sigmatel_9744_ops = { sigmatel_9744_init, NULL, NULL };
96 static struct ac97_ops crystal_digital_ops = { NULL, eapd_control, crystal_digital_control };
97
98 /* sorted by vendor/device id */
99 static const struct {
100 u32 id;
101 char *name;
102 struct ac97_ops *ops;
103 } ac97_codec_ids[] = {
104 {0x41445303, "Analog Devices AD1819", &null_ops},
105 {0x41445340, "Analog Devices AD1881", &null_ops},
106 {0x41445348, "Analog Devices AD1881A", &null_ops},
107 {0x41445460, "Analog Devices AD1885", &default_ops},
108 {0x414B4D00, "Asahi Kasei AK4540", &null_ops},
109 {0x414B4D01, "Asahi Kasei AK4542", &null_ops},
110 {0x414B4D02, "Asahi Kasei AK4543", &null_ops},
111 {0x414C4710, "ALC200/200P", &null_ops},
112 {0x43525900, "Cirrus Logic CS4297", &default_ops},
113 {0x43525903, "Cirrus Logic CS4297", &default_ops},
114 {0x43525913, "Cirrus Logic CS4297A rev A", &default_ops},
115 {0x43525914, "Cirrus Logic CS4297A rev B", &default_ops},
116 {0x43525923, "Cirrus Logic CS4298", &null_ops},
117 {0x4352592B, "Cirrus Logic CS4294", &null_ops},
118 {0x4352592D, "Cirrus Logic CS4294", &null_ops},
119 {0x43525931, "Cirrus Logic CS4299 rev A", &crystal_digital_ops},
120 {0x43525933, "Cirrus Logic CS4299 rev C", &crystal_digital_ops},
121 {0x43525934, "Cirrus Logic CS4299 rev D", &crystal_digital_ops},
122 {0x45838308, "ESS Allegro ES1988", &null_ops},
123 {0x49434511, "ICE1232", &null_ops}, /* I hope --jk */
124 {0x4e534331, "National Semiconductor LM4549", &null_ops},
125 {0x53494c22, "Silicon Laboratory Si3036", &null_ops},
126 {0x53494c23, "Silicon Laboratory Si3038", &null_ops},
127 {0x545200FF, "TriTech TR?????", &tritech_m_ops},
128 {0x54524102, "TriTech TR28022", &null_ops},
129 {0x54524103, "TriTech TR28023", &null_ops},
130 {0x54524106, "TriTech TR28026", &null_ops},
131 {0x54524108, "TriTech TR28028", &tritech_ops},
132 {0x54524123, "TriTech TR?????", &null_ops},
133 {0x574D4C00, "Wolfson WM9704", &wolfson_ops},
134 {0x574D4C03, "Wolfson WM9703/9704", &wolfson_ops},
135 {0x574D4C04, "Wolfson WM9704 (quad)", &wolfson_ops},
136 {0x83847600, "SigmaTel STAC????", &null_ops},
137 {0x83847604, "SigmaTel STAC9701/3/4/5", &null_ops},
138 {0x83847605, "SigmaTel STAC9704", &null_ops},
139 {0x83847608, "SigmaTel STAC9708", &sigmatel_9708_ops},
140 {0x83847609, "SigmaTel STAC9721/23", &sigmatel_9721_ops},
141 {0x83847644, "SigmaTel STAC9744/45", &sigmatel_9744_ops},
142 {0x83847656, "SigmaTel STAC9756/57", &sigmatel_9744_ops},
143 {0x83847684, "SigmaTel STAC9783/84?", &null_ops},
144 {0x57454301, "Winbond 83971D", &null_ops},
145 {0,}
146 };
147
148 static const char *ac97_stereo_enhancements[] =
149 {
150 /* 0 */ "No 3D Stereo Enhancement",
151 /* 1 */ "Analog Devices Phat Stereo",
152 /* 2 */ "Creative Stereo Enhancement",
153 /* 3 */ "National Semi 3D Stereo Enhancement",
154 /* 4 */ "YAMAHA Ymersion",
155 /* 5 */ "BBE 3D Stereo Enhancement",
156 /* 6 */ "Crystal Semi 3D Stereo Enhancement",
157 /* 7 */ "Qsound QXpander",
158 /* 8 */ "Spatializer 3D Stereo Enhancement",
159 /* 9 */ "SRS 3D Stereo Enhancement",
160 /* 10 */ "Platform Tech 3D Stereo Enhancement",
161 /* 11 */ "AKM 3D Audio",
162 /* 12 */ "Aureal Stereo Enhancement",
163 /* 13 */ "Aztech 3D Enhancement",
164 /* 14 */ "Binaura 3D Audio Enhancement",
165 /* 15 */ "ESS Technology Stereo Enhancement",
166 /* 16 */ "Harman International VMAx",
167 /* 17 */ "Nvidea 3D Stereo Enhancement",
168 /* 18 */ "Philips Incredible Sound",
169 /* 19 */ "Texas Instruments 3D Stereo Enhancement",
170 /* 20 */ "VLSI Technology 3D Stereo Enhancement",
171 /* 21 */ "TriTech 3D Stereo Enhancement",
172 /* 22 */ "Realtek 3D Stereo Enhancement",
173 /* 23 */ "Samsung 3D Stereo Enhancement",
174 /* 24 */ "Wolfson Microelectronics 3D Enhancement",
175 /* 25 */ "Delta Integration 3D Enhancement",
176 /* 26 */ "SigmaTel 3D Enhancement",
177 /* 27 */ "Winbond 3D Stereo Enhancement",
178 /* 28 */ "Rockwell 3D Stereo Enhancement",
179 /* 29 */ "Reserved 29",
180 /* 30 */ "Reserved 30",
181 /* 31 */ "Reserved 31"
182 };
183
184 /* this table has default mixer values for all OSS mixers. */
185 static struct mixer_defaults {
186 int mixer;
187 unsigned int value;
188 } mixer_defaults[SOUND_MIXER_NRDEVICES] = {
189 /* all values 0 -> 100 in bytes */
190 {SOUND_MIXER_VOLUME, 0x4343},
191 {SOUND_MIXER_BASS, 0x4343},
192 {SOUND_MIXER_TREBLE, 0x4343},
193 {SOUND_MIXER_PCM, 0x4343},
194 {SOUND_MIXER_SPEAKER, 0x4343},
195 {SOUND_MIXER_LINE, 0x4343},
196 {SOUND_MIXER_MIC, 0x0000},
197 {SOUND_MIXER_CD, 0x4343},
198 {SOUND_MIXER_ALTPCM, 0x4343},
199 {SOUND_MIXER_IGAIN, 0x4343},
200 {SOUND_MIXER_LINE1, 0x4343},
201 {SOUND_MIXER_PHONEIN, 0x4343},
202 {SOUND_MIXER_PHONEOUT, 0x4343},
203 {SOUND_MIXER_VIDEO, 0x4343},
204 {-1,0}
205 };
206
207 /* table to scale scale from OSS mixer value to AC97 mixer register value */
208 static struct ac97_mixer_hw {
209 unsigned char offset;
210 int scale;
211 } ac97_hw[SOUND_MIXER_NRDEVICES]= {
212 [SOUND_MIXER_VOLUME] = {AC97_MASTER_VOL_STEREO,64},
213 [SOUND_MIXER_BASS] = {AC97_MASTER_TONE, 16},
214 [SOUND_MIXER_TREBLE] = {AC97_MASTER_TONE, 16},
215 [SOUND_MIXER_PCM] = {AC97_PCMOUT_VOL, 32},
216 [SOUND_MIXER_SPEAKER] = {AC97_PCBEEP_VOL, 16},
217 [SOUND_MIXER_LINE] = {AC97_LINEIN_VOL, 32},
218 [SOUND_MIXER_MIC] = {AC97_MIC_VOL, 32},
219 [SOUND_MIXER_CD] = {AC97_CD_VOL, 32},
220 [SOUND_MIXER_ALTPCM] = {AC97_HEADPHONE_VOL, 64},
221 [SOUND_MIXER_IGAIN] = {AC97_RECORD_GAIN, 16},
222 [SOUND_MIXER_LINE1] = {AC97_AUX_VOL, 32},
223 [SOUND_MIXER_PHONEIN] = {AC97_PHONE_VOL, 32},
224 [SOUND_MIXER_PHONEOUT] = {AC97_MASTER_VOL_MONO, 64},
225 [SOUND_MIXER_VIDEO] = {AC97_VIDEO_VOL, 32},
226 };
227
228 /* the following tables allow us to go from OSS <-> ac97 quickly. */
229 enum ac97_recsettings {
230 AC97_REC_MIC=0,
231 AC97_REC_CD,
232 AC97_REC_VIDEO,
233 AC97_REC_AUX,
234 AC97_REC_LINE,
235 AC97_REC_STEREO, /* combination of all enabled outputs.. */
236 AC97_REC_MONO, /*.. or the mono equivalent */
237 AC97_REC_PHONE
238 };
239
240 static const unsigned int ac97_rm2oss[] = {
241 [AC97_REC_MIC] = SOUND_MIXER_MIC,
242 [AC97_REC_CD] = SOUND_MIXER_CD,
243 [AC97_REC_VIDEO] = SOUND_MIXER_VIDEO,
244 [AC97_REC_AUX] = SOUND_MIXER_LINE1,
245 [AC97_REC_LINE] = SOUND_MIXER_LINE,
246 [AC97_REC_STEREO]= SOUND_MIXER_IGAIN,
247 [AC97_REC_PHONE] = SOUND_MIXER_PHONEIN
248 };
249
250 /* indexed by bit position */
251 static const unsigned int ac97_oss_rm[] = {
252 [SOUND_MIXER_MIC] = AC97_REC_MIC,
253 [SOUND_MIXER_CD] = AC97_REC_CD,
254 [SOUND_MIXER_VIDEO] = AC97_REC_VIDEO,
255 [SOUND_MIXER_LINE1] = AC97_REC_AUX,
256 [SOUND_MIXER_LINE] = AC97_REC_LINE,
257 [SOUND_MIXER_IGAIN] = AC97_REC_STEREO,
258 [SOUND_MIXER_PHONEIN] = AC97_REC_PHONE
259 };
260
261 /* reads the given OSS mixer from the ac97 the caller must have insured that the ac97 knows
262 about that given mixer, and should be holding a spinlock for the card */
263 static int ac97_read_mixer(struct ac97_codec *codec, int oss_channel)
264 {
265 u16 val;
266 int ret = 0;
267 int scale;
268 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
269
270 val = codec->codec_read(codec , mh->offset);
271
272 if (val & AC97_MUTE) {
273 ret = 0;
274 } else if (AC97_STEREO_MASK & (1 << oss_channel)) {
275 /* nice stereo mixers .. */
276 int left,right;
277
278 left = (val >> 8) & 0x7f;
279 right = val & 0x7f;
280
281 if (oss_channel == SOUND_MIXER_IGAIN) {
282 right = (right * 100) / mh->scale;
283 left = (left * 100) / mh->scale;
284 } else {
285 /* these may have 5 or 6 bit resolution */
286 if(oss_channel == SOUND_MIXER_VOLUME || oss_channel == SOUND_MIXER_ALTPCM)
287 scale = (1 << codec->bit_resolution);
288 else
289 scale = mh->scale;
290
291 right = 100 - ((right * 100) / scale);
292 left = 100 - ((left * 100) / scale);
293 }
294 ret = left | (right << 8);
295 } else if (oss_channel == SOUND_MIXER_SPEAKER) {
296 ret = 100 - ((((val & 0x1e)>>1) * 100) / mh->scale);
297 } else if (oss_channel == SOUND_MIXER_PHONEIN) {
298 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
299 } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
300 scale = (1 << codec->bit_resolution);
301 ret = 100 - (((val & 0x1f) * 100) / scale);
302 } else if (oss_channel == SOUND_MIXER_MIC) {
303 ret = 100 - (((val & 0x1f) * 100) / mh->scale);
304 /* the low bit is optional in the tone sliders and masking
305 it lets us avoid the 0xf 'bypass'.. */
306 } else if (oss_channel == SOUND_MIXER_BASS) {
307 ret = 100 - ((((val >> 8) & 0xe) * 100) / mh->scale);
308 } else if (oss_channel == SOUND_MIXER_TREBLE) {
309 ret = 100 - (((val & 0xe) * 100) / mh->scale);
310 }
311
312 #ifdef DEBUG
313 printk("ac97_codec: read OSS mixer %2d (%s ac97 register 0x%02x), "
314 "0x%04x -> 0x%04x\n",
315 oss_channel, codec->id ? "Secondary" : "Primary",
316 mh->offset, val, ret);
317 #endif
318
319 return ret;
320 }
321
322 /* write the OSS encoded volume to the given OSS encoded mixer, again caller's job to
323 make sure all is well in arg land, call with spinlock held */
324 static void ac97_write_mixer(struct ac97_codec *codec, int oss_channel,
325 unsigned int left, unsigned int right)
326 {
327 u16 val = 0;
328 int scale;
329 struct ac97_mixer_hw *mh = &ac97_hw[oss_channel];
330
331 #ifdef DEBUG
332 printk("ac97_codec: wrote OSS mixer %2d (%s ac97 register 0x%02x), "
333 "left vol:%2d, right vol:%2d:",
334 oss_channel, codec->id ? "Secondary" : "Primary",
335 mh->offset, left, right);
336 #endif
337
338 if (AC97_STEREO_MASK & (1 << oss_channel)) {
339 /* stereo mixers */
340 if (left == 0 && right == 0) {
341 val = AC97_MUTE;
342 } else {
343 if (oss_channel == SOUND_MIXER_IGAIN) {
344 right = (right * mh->scale) / 100;
345 left = (left * mh->scale) / 100;
346 if (right >= mh->scale)
347 right = mh->scale-1;
348 if (left >= mh->scale)
349 left = mh->scale-1;
350 } else {
351 /* these may have 5 or 6 bit resolution */
352 if (oss_channel == SOUND_MIXER_VOLUME ||
353 oss_channel == SOUND_MIXER_ALTPCM)
354 scale = (1 << codec->bit_resolution);
355 else
356 scale = mh->scale;
357
358 right = ((100 - right) * scale) / 100;
359 left = ((100 - left) * scale) / 100;
360 if (right >= scale)
361 right = scale-1;
362 if (left >= scale)
363 left = scale-1;
364 }
365 val = (left << 8) | right;
366 }
367 } else if (oss_channel == SOUND_MIXER_BASS) {
368 val = codec->codec_read(codec , mh->offset) & ~0x0f00;
369 left = ((100 - left) * mh->scale) / 100;
370 if (left >= mh->scale)
371 left = mh->scale-1;
372 val |= (left << 8) & 0x0e00;
373 } else if (oss_channel == SOUND_MIXER_TREBLE) {
374 val = codec->codec_read(codec , mh->offset) & ~0x000f;
375 left = ((100 - left) * mh->scale) / 100;
376 if (left >= mh->scale)
377 left = mh->scale-1;
378 val |= left & 0x000e;
379 } else if(left == 0) {
380 val = AC97_MUTE;
381 } else if (oss_channel == SOUND_MIXER_SPEAKER) {
382 left = ((100 - left) * mh->scale) / 100;
383 if (left >= mh->scale)
384 left = mh->scale-1;
385 val = left << 1;
386 } else if (oss_channel == SOUND_MIXER_PHONEIN) {
387 left = ((100 - left) * mh->scale) / 100;
388 if (left >= mh->scale)
389 left = mh->scale-1;
390 val = left;
391 } else if (oss_channel == SOUND_MIXER_PHONEOUT) {
392 scale = (1 << codec->bit_resolution);
393 left = ((100 - left) * scale) / 100;
394 if (left >= mh->scale)
395 left = mh->scale-1;
396 val = left;
397 } else if (oss_channel == SOUND_MIXER_MIC) {
398 val = codec->codec_read(codec , mh->offset) & ~0x801f;
399 left = ((100 - left) * mh->scale) / 100;
400 if (left >= mh->scale)
401 left = mh->scale-1;
402 val |= left;
403 /* the low bit is optional in the tone sliders and masking
404 it lets us avoid the 0xf 'bypass'.. */
405 }
406 #ifdef DEBUG
407 printk(" 0x%04x", val);
408 #endif
409
410 codec->codec_write(codec, mh->offset, val);
411
412 #ifdef DEBUG
413 val = codec->codec_read(codec, mh->offset);
414 printk(" -> 0x%04x\n", val);
415 #endif
416 }
417
418 /* a thin wrapper for write_mixer */
419 static void ac97_set_mixer(struct ac97_codec *codec, unsigned int oss_mixer, unsigned int val )
420 {
421 unsigned int left,right;
422
423 /* cleanse input a little */
424 right = ((val >> 8) & 0xff) ;
425 left = (val & 0xff) ;
426
427 if (right > 100) right = 100;
428 if (left > 100) left = 100;
429
430 codec->mixer_state[oss_mixer] = (right << 8) | left;
431 codec->write_mixer(codec, oss_mixer, left, right);
432 }
433
434 /* read or write the recmask, the ac97 can really have left and right recording
435 inputs independantly set, but OSS doesn't seem to want us to express that to
436 the user. the caller guarantees that we have a supported bit set, and they
437 must be holding the card's spinlock */
438 static int ac97_recmask_io(struct ac97_codec *codec, int rw, int mask)
439 {
440 unsigned int val;
441
442 if (rw) {
443 /* read it from the card */
444 val = codec->codec_read(codec, AC97_RECORD_SELECT);
445 #ifdef DEBUG
446 printk("ac97_codec: ac97 recmask to set to 0x%04x\n", val);
447 #endif
448 return (1 << ac97_rm2oss[val & 0x07]);
449 }
450
451 /* else, write the first set in the mask as the
452 output */
453 /* clear out current set value first (AC97 supports only 1 input!) */
454 val = (1 << ac97_rm2oss[codec->codec_read(codec, AC97_RECORD_SELECT) & 0x07]);
455 if (mask != val)
456 mask &= ~val;
457
458 val = ffs(mask);
459 val = ac97_oss_rm[val-1];
460 val |= val << 8; /* set both channels */
461
462 #ifdef DEBUG
463 printk("ac97_codec: setting ac97 recmask to 0x%04x\n", val);
464 #endif
465
466 codec->codec_write(codec, AC97_RECORD_SELECT, val);
467
468 return 0;
469 };
470
471 static int ac97_mixer_ioctl(struct ac97_codec *codec, unsigned int cmd, unsigned long arg)
472 {
473 int i, val = 0;
474
475 if (cmd == SOUND_MIXER_INFO) {
476 mixer_info info;
477 strncpy(info.id, codec->name, sizeof(info.id));
478 strncpy(info.name, codec->name, sizeof(info.name));
479 info.modify_counter = codec->modcnt;
480 if (copy_to_user((void *)arg, &info, sizeof(info)))
481 return -EFAULT;
482 return 0;
483 }
484 if (cmd == SOUND_OLD_MIXER_INFO) {
485 _old_mixer_info info;
486 strncpy(info.id, codec->name, sizeof(info.id));
487 strncpy(info.name, codec->name, sizeof(info.name));
488 if (copy_to_user((void *)arg, &info, sizeof(info)))
489 return -EFAULT;
490 return 0;
491 }
492
493 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
494 return -EINVAL;
495
496 if (cmd == OSS_GETVERSION)
497 return put_user(SOUND_VERSION, (int *)arg);
498
499 if (_SIOC_DIR(cmd) == _SIOC_READ) {
500 switch (_IOC_NR(cmd)) {
501 case SOUND_MIXER_RECSRC: /* give them the current record source */
502 if (!codec->recmask_io) {
503 val = 0;
504 } else {
505 val = codec->recmask_io(codec, 1, 0);
506 }
507 break;
508
509 case SOUND_MIXER_DEVMASK: /* give them the supported mixers */
510 val = codec->supported_mixers;
511 break;
512
513 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
514 val = codec->record_sources;
515 break;
516
517 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
518 val = codec->stereo_mixers;
519 break;
520
521 case SOUND_MIXER_CAPS:
522 val = SOUND_CAP_EXCL_INPUT;
523 break;
524
525 default: /* read a specific mixer */
526 i = _IOC_NR(cmd);
527
528 if (!supported_mixer(codec, i))
529 return -EINVAL;
530
531 /* do we ever want to touch the hardware? */
532 /* val = codec->read_mixer(codec, i); */
533 val = codec->mixer_state[i];
534 break;
535 }
536 return put_user(val, (int *)arg);
537 }
538
539 if (_SIOC_DIR(cmd) == (_SIOC_WRITE|_SIOC_READ)) {
540 codec->modcnt++;
541 if (get_user(val, (int *)arg))
542 return -EFAULT;
543
544 switch (_IOC_NR(cmd)) {
545 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
546 if (!codec->recmask_io) return -EINVAL;
547 if (!val) return 0;
548 if (!(val &= codec->record_sources)) return -EINVAL;
549
550 codec->recmask_io(codec, 0, val);
551
552 return 0;
553 default: /* write a specific mixer */
554 i = _IOC_NR(cmd);
555
556 if (!supported_mixer(codec, i))
557 return -EINVAL;
558
559 ac97_set_mixer(codec, i, val);
560
561 return 0;
562 }
563 }
564 return -EINVAL;
565 }
566
567 /* entry point for /proc/driver/controller_vendor/ac97/%d */
568 int ac97_read_proc (char *page, char **start, off_t off,
569 int count, int *eof, void *data)
570 {
571 int len = 0, cap, extid, val, id1, id2;
572 struct ac97_codec *codec;
573 int is_ac97_20 = 0;
574
575 if ((codec = data) == NULL)
576 return -ENODEV;
577
578 id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
579 id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
580 len += sprintf (page+len, "Vendor name : %s\n", codec->name);
581 len += sprintf (page+len, "Vendor id : %04X %04X\n", id1, id2);
582
583 extid = codec->codec_read(codec, AC97_EXTENDED_ID);
584 extid &= ~((1<<2)|(1<<4)|(1<<5)|(1<<10)|(1<<11)|(1<<12)|(1<<13));
585 len += sprintf (page+len, "AC97 Version : %s\n",
586 extid ? "2.0 or later" : "1.0");
587 if (extid) is_ac97_20 = 1;
588
589 cap = codec->codec_read(codec, AC97_RESET);
590 len += sprintf (page+len, "Capabilities :%s%s%s%s%s%s\n",
591 cap & 0x0001 ? " -dedicated MIC PCM IN channel-" : "",
592 cap & 0x0002 ? " -reserved1-" : "",
593 cap & 0x0004 ? " -bass & treble-" : "",
594 cap & 0x0008 ? " -simulated stereo-" : "",
595 cap & 0x0010 ? " -headphone out-" : "",
596 cap & 0x0020 ? " -loudness-" : "");
597 val = cap & 0x00c0;
598 len += sprintf (page+len, "DAC resolutions :%s%s%s\n",
599 " -16-bit-",
600 val & 0x0040 ? " -18-bit-" : "",
601 val & 0x0080 ? " -20-bit-" : "");
602 val = cap & 0x0300;
603 len += sprintf (page+len, "ADC resolutions :%s%s%s\n",
604 " -16-bit-",
605 val & 0x0100 ? " -18-bit-" : "",
606 val & 0x0200 ? " -20-bit-" : "");
607 len += sprintf (page+len, "3D enhancement : %s\n",
608 ac97_stereo_enhancements[(cap >> 10) & 0x1f]);
609
610 val = codec->codec_read(codec, AC97_GENERAL_PURPOSE);
611 len += sprintf (page+len, "POP path : %s 3D\n"
612 "Sim. stereo : %s\n"
613 "3D enhancement : %s\n"
614 "Loudness : %s\n"
615 "Mono output : %s\n"
616 "MIC select : %s\n"
617 "ADC/DAC loopback : %s\n",
618 val & 0x8000 ? "post" : "pre",
619 val & 0x4000 ? "on" : "off",
620 val & 0x2000 ? "on" : "off",
621 val & 0x1000 ? "on" : "off",
622 val & 0x0200 ? "MIC" : "MIX",
623 val & 0x0100 ? "MIC2" : "MIC1",
624 val & 0x0080 ? "on" : "off");
625
626 extid = codec->codec_read(codec, AC97_EXTENDED_ID);
627 cap = extid;
628 len += sprintf (page+len, "Ext Capabilities :%s%s%s%s%s%s%s\n",
629 cap & 0x0001 ? " -var rate PCM audio-" : "",
630 cap & 0x0002 ? " -2x PCM audio out-" : "",
631 cap & 0x0008 ? " -var rate MIC in-" : "",
632 cap & 0x0040 ? " -PCM center DAC-" : "",
633 cap & 0x0080 ? " -PCM surround DAC-" : "",
634 cap & 0x0100 ? " -PCM LFE DAC-" : "",
635 cap & 0x0200 ? " -slot/DAC mappings-" : "");
636 if (is_ac97_20) {
637 len += sprintf (page+len, "Front DAC rate : %d\n",
638 codec->codec_read(codec, AC97_PCM_FRONT_DAC_RATE));
639 }
640
641 return len;
642 }
643
644 /**
645 * ac97_probe_codec - Initialize and setup AC97-compatible codec
646 * @codec: (in/out) Kernel info for a single AC97 codec
647 *
648 * Reset the AC97 codec, then initialize the mixer and
649 * the rest of the @codec structure.
650 *
651 * The codec_read and codec_write fields of @codec are
652 * required to be setup and working when this function
653 * is called. All other fields are set by this function.
654 *
655 * codec_wait field of @codec can optionally be provided
656 * when calling this function. If codec_wait is not %NULL,
657 * this function will call codec_wait any time it is
658 * necessary to wait for the audio chip to reach the
659 * codec-ready state. If codec_wait is %NULL, then
660 * the default behavior is to call schedule_timeout.
661 * Currently codec_wait is used to wait for AC97 codec
662 * reset to complete.
663 *
664 * Returns 1 (true) on success, or 0 (false) on failure.
665 */
666
667 int ac97_probe_codec(struct ac97_codec *codec)
668 {
669 u16 id1, id2;
670 u16 audio, modem;
671 int i;
672
673 /* probing AC97 codec, AC97 2.0 says that bit 15 of register 0x00 (reset) should
674 * be read zero.
675 *
676 * FIXME: is the following comment outdated? -jgarzik
677 * Probing of AC97 in this way is not reliable, it is not even SAFE !!
678 */
679 codec->codec_write(codec, AC97_RESET, 0L);
680
681 /* also according to spec, we wait for codec-ready state */
682 if (codec->codec_wait)
683 codec->codec_wait(codec);
684 else
685 udelay(10);
686
687 if ((audio = codec->codec_read(codec, AC97_RESET)) & 0x8000) {
688 printk(KERN_ERR "ac97_codec: %s ac97 codec not present\n",
689 codec->id ? "Secondary" : "Primary");
690 return 0;
691 }
692
693 /* probe for Modem Codec */
694 codec->codec_write(codec, AC97_EXTENDED_MODEM_ID, 0L);
695 modem = codec->codec_read(codec, AC97_EXTENDED_MODEM_ID);
696
697 codec->name = NULL;
698 codec->codec_ops = &null_ops;
699
700 id1 = codec->codec_read(codec, AC97_VENDOR_ID1);
701 id2 = codec->codec_read(codec, AC97_VENDOR_ID2);
702 for (i = 0; i < ARRAY_SIZE(ac97_codec_ids); i++) {
703 if (ac97_codec_ids[i].id == ((id1 << 16) | id2)) {
704 codec->type = ac97_codec_ids[i].id;
705 codec->name = ac97_codec_ids[i].name;
706 codec->codec_ops = ac97_codec_ids[i].ops;
707 break;
708 }
709 }
710 if (codec->name == NULL)
711 codec->name = "Unknown";
712 printk(KERN_INFO "ac97_codec: AC97 %s codec, id: 0x%04x:"
713 "0x%04x (%s)\n", audio ? "Audio" : (modem ? "Modem" : ""),
714 id1, id2, codec->name);
715
716 return ac97_init_mixer(codec);
717 }
718
719 static int ac97_init_mixer(struct ac97_codec *codec)
720 {
721 u16 cap;
722 int i;
723
724 cap = codec->codec_read(codec, AC97_RESET);
725
726 /* mixer masks */
727 codec->supported_mixers = AC97_SUPPORTED_MASK;
728 codec->stereo_mixers = AC97_STEREO_MASK;
729 codec->record_sources = AC97_RECORD_MASK;
730 if (!(cap & 0x04))
731 codec->supported_mixers &= ~(SOUND_MASK_BASS|SOUND_MASK_TREBLE);
732 if (!(cap & 0x10))
733 codec->supported_mixers &= ~SOUND_MASK_ALTPCM;
734
735 /* detect bit resolution */
736 codec->codec_write(codec, AC97_MASTER_VOL_STEREO, 0x2020);
737 if(codec->codec_read(codec, AC97_MASTER_VOL_STEREO) == 0x1f1f)
738 codec->bit_resolution = 5;
739 else
740 codec->bit_resolution = 6;
741
742 /* generic OSS to AC97 wrapper */
743 codec->read_mixer = ac97_read_mixer;
744 codec->write_mixer = ac97_write_mixer;
745 codec->recmask_io = ac97_recmask_io;
746 codec->mixer_ioctl = ac97_mixer_ioctl;
747
748 /* codec specific initialization for 4-6 channel output or secondary codec stuff */
749 if (codec->codec_ops->init != NULL) {
750 codec->codec_ops->init(codec);
751 }
752
753 /* initialize mixer channel volumes */
754 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
755 struct mixer_defaults *md = &mixer_defaults[i];
756 if (md->mixer == -1)
757 break;
758 if (!supported_mixer(codec, md->mixer))
759 continue;
760 ac97_set_mixer(codec, md->mixer, md->value);
761 }
762
763 return 1;
764 }
765
766 #define AC97_SIGMATEL_ANALOG 0x6c /* Analog Special */
767 #define AC97_SIGMATEL_DAC2INVERT 0x6e
768 #define AC97_SIGMATEL_BIAS1 0x70
769 #define AC97_SIGMATEL_BIAS2 0x72
770 #define AC97_SIGMATEL_MULTICHN 0x74 /* Multi-Channel programming */
771 #define AC97_SIGMATEL_CIC1 0x76
772 #define AC97_SIGMATEL_CIC2 0x78
773
774
775 static int sigmatel_9708_init(struct ac97_codec * codec)
776 {
777 u16 codec72, codec6c;
778
779 codec72 = codec->codec_read(codec, AC97_SIGMATEL_BIAS2) & 0x8000;
780 codec6c = codec->codec_read(codec, AC97_SIGMATEL_ANALOG);
781
782 if ((codec72==0) && (codec6c==0)) {
783 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
784 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1000);
785 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
786 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0007);
787 } else if ((codec72==0x8000) && (codec6c==0)) {
788 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
789 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x1001);
790 codec->codec_write(codec, AC97_SIGMATEL_DAC2INVERT, 0x0008);
791 } else if ((codec72==0x8000) && (codec6c==0x0080)) {
792 /* nothing */
793 }
794 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
795 return 0;
796 }
797
798
799 static int sigmatel_9721_init(struct ac97_codec * codec)
800 {
801 /* Only set up secondary codec */
802 if (codec->id == 0)
803 return 0;
804
805 codec->codec_write(codec, AC97_SURROUND_MASTER, 0L);
806
807 /* initialize SigmaTel STAC9721/23 as secondary codec, decoding AC link
808 sloc 3,4 = 0x01, slot 7,8 = 0x00, */
809 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x00);
810
811 /* we don't have the crystal when we are on an AMR card, so use
812 BIT_CLK as our clock source. Write the magic word ABBA and read
813 back to enable register 0x78 */
814 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
815 codec->codec_read(codec, AC97_SIGMATEL_CIC1);
816
817 /* sync all the clocks*/
818 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x3802);
819
820 return 0;
821 }
822
823
824 static int sigmatel_9744_init(struct ac97_codec * codec)
825 {
826 // patch for SigmaTel
827 codec->codec_write(codec, AC97_SIGMATEL_CIC1, 0xabba);
828 codec->codec_write(codec, AC97_SIGMATEL_CIC2, 0x0000); // is this correct? --jk
829 codec->codec_write(codec, AC97_SIGMATEL_BIAS1, 0xabba);
830 codec->codec_write(codec, AC97_SIGMATEL_BIAS2, 0x0002);
831 codec->codec_write(codec, AC97_SIGMATEL_MULTICHN, 0x0000);
832 return 0;
833 }
834
835
836 static int wolfson_init(struct ac97_codec * codec)
837 {
838 codec->codec_write(codec, 0x72, 0x0808);
839 codec->codec_write(codec, 0x74, 0x0808);
840
841 // patch for DVD noise
842 codec->codec_write(codec, 0x5a, 0x0200);
843
844 // init vol as PCM vol
845 codec->codec_write(codec, 0x70,
846 codec->codec_read(codec, AC97_PCMOUT_VOL));
847
848 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
849 return 0;
850 }
851
852
853 static int tritech_init(struct ac97_codec * codec)
854 {
855 codec->codec_write(codec, 0x26, 0x0300);
856 codec->codec_write(codec, 0x26, 0x0000);
857 codec->codec_write(codec, AC97_SURROUND_MASTER, 0x0000);
858 codec->codec_write(codec, AC97_RESERVED_3A, 0x0000);
859 return 0;
860 }
861
862
863 /* copied from drivers/sound/maestro.c */
864 static int tritech_maestro_init(struct ac97_codec * codec)
865 {
866 /* no idea what this does */
867 codec->codec_write(codec, 0x2A, 0x0001);
868 codec->codec_write(codec, 0x2C, 0x0000);
869 codec->codec_write(codec, 0x2C, 0XFFFF);
870 return 0;
871 }
872
873
874 /*
875 * This is basically standard AC97. It should work as a default for
876 * almost all modern codecs. Note that some cards wire EAPD *backwards*
877 * That side of it is up to the card driver not us to cope with.
878 *
879 */
880
881 static int eapd_control(struct ac97_codec * codec, int on)
882 {
883 if(on)
884 codec->codec_write(codec, AC97_POWER_CONTROL,
885 codec->codec_read(codec, AC97_POWER_CONTROL)|0x8000);
886 else
887 codec->codec_write(codec, AC97_POWER_CONTROL,
888 codec->codec_read(codec, AC97_POWER_CONTROL)&~0x8000);
889 return 0;
890 }
891
892 /*
893 * Crystal digital audio control (CS4299
894 */
895
896 static int crystal_digital_control(struct ac97_codec *codec, int mode)
897 {
898 u16 cv;
899
900 switch(mode)
901 {
902 case 0: cv = 0x0; break; /* SPEN off */
903 case 1: cv = 0x8004; break; /* 48KHz digital */
904 case 2: cv = 0x8104; break; /* 44.1KHz digital */
905 default:
906 return -1; /* Not supported yet(eg AC3) */
907 }
908 codec->codec_write(codec, 0x68, cv);
909 return 0;
910 }
911
912 /* copied from drivers/sound/maestro.c */
913 #if 0 /* there has been 1 person on the planet with a pt101 that we
914 know of. If they care, they can put this back in :) */
915 static int pt101_init(struct ac97_codec * codec)
916 {
917 printk(KERN_INFO "ac97_codec: PT101 Codec detected, initializing but _not_ installing mixer device.\n");
918 /* who knows.. */
919 codec->codec_write(codec, 0x2A, 0x0001);
920 codec->codec_write(codec, 0x2C, 0x0000);
921 codec->codec_write(codec, 0x2C, 0xFFFF);
922 codec->codec_write(codec, 0x10, 0x9F1F);
923 codec->codec_write(codec, 0x12, 0x0808);
924 codec->codec_write(codec, 0x14, 0x9F1F);
925 codec->codec_write(codec, 0x16, 0x9F1F);
926 codec->codec_write(codec, 0x18, 0x0404);
927 codec->codec_write(codec, 0x1A, 0x0000);
928 codec->codec_write(codec, 0x1C, 0x0000);
929 codec->codec_write(codec, 0x02, 0x0404);
930 codec->codec_write(codec, 0x04, 0x0808);
931 codec->codec_write(codec, 0x0C, 0x801F);
932 codec->codec_write(codec, 0x0E, 0x801F);
933 return 0;
934 }
935 #endif
936
937
938 EXPORT_SYMBOL(ac97_read_proc);
939 EXPORT_SYMBOL(ac97_probe_codec);
940
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.