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